diff options
author | Simon Rettberg | 2015-07-10 16:21:34 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-07-10 16:21:34 +0200 |
commit | 16e76678e1d6a868901c79d06e2cc7955b600006 (patch) | |
tree | eadf2e1a401af474def92a63207f0762dffec6ef | |
parent | Send search requests on the same connection the explicit bind was done on, so... (diff) | |
download | ldadp-16e76678e1d6a868901c79d06e2cc7955b600006.tar.gz ldadp-16e76678e1d6a868901c79d06e2cc7955b600006.tar.xz ldadp-16e76678e1d6a868901c79d06e2cc7955b600006.zip |
[server.c] Pay attention to failed sends, and return error in related functions
-rw-r--r-- | server.c | 50 |
1 files changed, 33 insertions, 17 deletions
@@ -24,7 +24,7 @@ static void server_init(); static server_t *server_create(const char *server); static void server_callback(void *data, int haveIn, int haveOut, int doCleanup); static void server_haveIn(epoll_server_t *server); -static void server_haveOut(epoll_server_t * const server); +static BOOL server_haveOut(epoll_server_t * const server); static BOOL server_ensureConnected(server_t *server); static BOOL server_ensureSendBuffer(epoll_server_t * const s, const size_t len); static int server_connectInternal(server_t *server); @@ -169,8 +169,12 @@ server_t *server_getFromBase(struct string *in) uint32_t server_searchRequest(server_t *server, struct SearchRequest *req) { - if (!server_ensureConnected(server)) return 0; epoll_server_t * const s = &server->con; + if (s->fd != -1 && !s->kill) { + uint32_t msgId = server_searchRequestOnConnection(s, req); + if (msgId != 0) return msgId; + } + if (!server_ensureConnected(server)) return 0; return server_searchRequestOnConnection(s, req); } @@ -183,7 +187,7 @@ uint32_t server_searchRequestOnConnection(epoll_server_t *server, struct SearchR char *bufoff = buffer + 50; fmt_ldapsearchrequest(bufoff, req); fmt_ldapmessage(bufoff - headerLen, msgid, SearchRequest, bodyLen); - server_send(server, bufoff - headerLen, headerLen + bodyLen, FALSE); + if (!server_send(server, bufoff - headerLen, headerLen + bodyLen, FALSE)) return 0; return msgid; } @@ -228,7 +232,10 @@ uint32_t server_tryUserBind(server_t *server, struct string *binddn, struct stri } fmt_ldapbindrequeststring(bufoff, 3, binddn, password); fmt_ldapmessage(bufoff - headerLen, id, BindRequest, bodyLen); - server_send(con, bufoff - headerLen, bodyLen + headerLen, FALSE); + if (!server_send(con, bufoff - headerLen, bodyLen + headerLen, FALSE)) { + server_free(con); + return 0; + } *newcon = con; return id; } @@ -273,9 +280,11 @@ void server_free(epoll_server_t *server) } server->sbPos = server->sbFill = 0; if (server->dynamic) { - //printf("Freeing Bind-AD-Connection\n"); + printf("[Server] Freeing Bind-AD-Connection\n"); free(server->sendBuffer); free(server); + } else { + printf("[Server] Closed shared anonymous connection\n"); } } @@ -323,7 +332,7 @@ static void server_haveIn(epoll_server_t *server) ret = read(server->fd, server->readBuffer + server->rbPos, buflen); if (ret < 0 && errno == EINTR) continue; if (ret < 0 && errno == EAGAIN) break; - if (ret < 0) printf("[Proxy] AD Server %s gone while reading (ret=%d, errno=%d).\n", server->serverData->addr, (int)ret, errno); + //if (ret < 0) printf("[Proxy] AD Server %s gone while reading (ret=%d, errno=%d).\n", server->serverData->addr, (int)ret, errno); if (ret <= 0) { server->kill = TRUE; return; @@ -334,7 +343,7 @@ static void server_haveIn(epoll_server_t *server) if (ret <= 0) { int err = SSL_get_error(server->ssl, ret); if (SSL_BLOCKED(err)) break; - if (err != 0) printf("[Proxy] AD Server %s gone while reading (ret=%d, err=%d).\n", server->serverData->addr, (int)ret, err); + //if (err != 0) printf("[Proxy] AD Server %s gone while reading (ret=%d, err=%d).\n", server->serverData->addr, (int)ret, err); server->kill = TRUE; return; } @@ -375,6 +384,7 @@ BOOL server_send(epoll_server_t *server, const char *buffer, size_t len, const B const int ret = write(server->fd, buffer, len); if (ret == 0 || (ret < 0 && errno != EINTR && errno != EAGAIN)) { printf("[Proxy] AD Server %s gone when trying to send.\n", server->serverData->addr); + server->kill = TRUE; return FALSE; } server->lastActive = time(NULL); @@ -393,12 +403,13 @@ BOOL server_send(epoll_server_t *server, const char *buffer, size_t len, const B // Finally append to buffer memcpy(server->sendBuffer + server->sbFill, buffer, len); server->sbFill += len; - if (!cork) server_haveOut(server); + if (!cork) return server_haveOut(server); return TRUE; } -static void server_haveOut(epoll_server_t * const server) +static BOOL server_haveOut(epoll_server_t * const server) { + if (server->kill) return FALSE; while (server->sbPos < server->sbFill) { const ssize_t tosend = server->sbFill - server->sbPos; ssize_t ret; @@ -406,10 +417,11 @@ static void server_haveOut(epoll_server_t * const server) // Plain ret = write(server->fd, server->sendBuffer + server->sbPos, tosend); if (ret < 0 && errno == EINTR) continue; - if (ret < 0 && errno == EAGAIN) return; + if (ret < 0 && errno == EAGAIN) return TRUE; if (ret <= 0) { printf("[Proxy] AD Server %s gone while flushing send buffer (ret=%d, errno=%d)\n", server->serverData->addr, (int)ret, errno); - return; + server->kill = TRUE; + return FALSE; } } else { // SSL @@ -418,14 +430,14 @@ static void server_haveOut(epoll_server_t * const server) int err = SSL_get_error(server->ssl, ret); if (SSL_BLOCKED(err)) { server->writeBlocked = TRUE; - return; // Blocking + return TRUE; // Blocking } else if (err == SSL_ERROR_SSL) { ssl_printErrors(NULL); } printf("[Proxy] AD Server %s gone while flushing send buffer (ret=%d, err=%d)\n", server->serverData->addr, (int)ret, err); ERR_print_errors_fp(stdout); server->kill = TRUE; - return; // Closed + return FALSE; // Closed } } server->lastActive = time(NULL); @@ -435,9 +447,10 @@ static void server_haveOut(epoll_server_t * const server) server->sbFill -= server->sbPos; server->sbPos = 0; } - if (server->ssl == NULL && ret != tosend) return; + if (server->ssl == NULL && ret != tosend) return TRUE; } server->sbPos = server->sbFill = 0; + return TRUE; } static BOOL server_ensureConnected(server_t *server) @@ -448,7 +461,10 @@ static BOOL server_ensureConnected(server_t *server) con->bound = FALSE; con->sbPos = con->sbFill = 0; int sock = server_connectInternal(server); - if (sock == -1) return FALSE; + if (sock == -1) { + printf("[Server] Creating socket for shared connection failed.\n"); + return FALSE; + } helper_nonblock(sock); con->fd = sock; con->callback = &server_callback; @@ -460,6 +476,7 @@ static BOOL server_ensureConnected(server_t *server) } // SSL if (!server_connectSsl(con)) { + printf("[Proxy] SSL handshake failed for shared connection of %s\n", server->addr); close(con->fd); con->fd = -1; return FALSE; @@ -477,8 +494,7 @@ static BOOL server_ensureConnected(server_t *server) } fmt_ldapbindrequest(bufoff, 3, server->bind, server->password); fmt_ldapmessage(bufoff - headerLen, MSGID_BIND, BindRequest, bodyLen); - server_send(con, bufoff - headerLen, bodyLen + headerLen, FALSE); - return TRUE; + return server_send(con, bufoff - headerLen, bodyLen + headerLen, FALSE); } static BOOL server_ensureSendBuffer(epoll_server_t * const s, const size_t len) |