diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index 311ceb82..44d6e66b 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -680,7 +680,7 @@ int send__auth(struct mosquitto *context, uint8_t reason_code, const void *auth_ * ============================================================ */ void net__broker_init(void); void net__broker_cleanup(void); -int net__socket_accept(struct mosquitto__listener_sock *listensock); +struct mosquitto *net__socket_accept(struct mosquitto__listener_sock *listensock); int net__socket_listen(struct mosquitto__listener *listener); int net__socket_get_address(mosq_sock_t sock, char *buf, size_t len); int net__tls_load_verify(struct mosquitto__listener *listener); diff --git a/src/mux_epoll.c b/src/mux_epoll.c index 32fd1c4c..b0e192e7 100644 --- a/src/mux_epoll.c +++ b/src/mux_epoll.c @@ -184,7 +184,6 @@ int mux_epoll__handle(void) struct mosquitto *context; struct mosquitto__listener_sock *listensock; int event_count; - int sock; memset(&ev, 0, sizeof(struct epoll_event)); sigprocmask(SIG_SETMASK, &my_sigblock, &origsig); @@ -211,12 +210,7 @@ int mux_epoll__handle(void) listensock = ep_events[i].data.ptr; if (ep_events[i].events & (EPOLLIN | EPOLLPRI)){ - while((sock = net__socket_accept(listensock)) != -1){ - context = NULL; - HASH_FIND(hh_sock, db.contexts_by_sock, &sock, sizeof(mosq_sock_t), context); - if(!context) { - log__printf(NULL, MOSQ_LOG_ERR, "Error in epoll accepting: no context"); - } + while((context = net__socket_accept(listensock)) != NULL){ context->events = EPOLLIN; mux__add_in(context); } diff --git a/src/mux_poll.c b/src/mux_poll.c index 8e2228ea..cc0842b8 100644 --- a/src/mux_poll.c +++ b/src/mux_poll.c @@ -172,7 +172,6 @@ int mux_poll__delete(struct mosquitto *context) int mux_poll__handle(struct mosquitto__listener_sock *listensock, int listensock_count) { struct mosquitto *context; - mosq_sock_t sock; int i; int fdcount; #ifndef WIN32 @@ -208,15 +207,9 @@ int mux_poll__handle(struct mosquitto__listener_sock *listensock, int listensock for(i=0; ipollfd_index = -1; - mux__add_in(context); - }else{ - log__printf(NULL, MOSQ_LOG_ERR, "Error in poll accepting: no context"); - } + while((context = net__socket_accept(&listensock[i])) != NULL){ + context->pollfd_index = -1; + mux__add_in(context); } } } diff --git a/src/net.c b/src/net.c index 3dc1afa8..f399ea9c 100644 --- a/src/net.c +++ b/src/net.c @@ -105,7 +105,7 @@ static void net__print_error(unsigned int log, const char *format_str) } -int net__socket_accept(struct mosquitto__listener_sock *listensock) +struct mosquitto *net__socket_accept(struct mosquitto__listener_sock *listensock) { mosq_sock_t new_sock = INVALID_SOCKET; struct mosquitto *new_context; @@ -144,13 +144,13 @@ int net__socket_accept(struct mosquitto__listener_sock *listensock) log__printf(NULL, MOSQ_LOG_WARNING, "Unable to accept new connection, system socket count has been exceeded. Try increasing \"ulimit -n\" or equivalent."); } - return -1; + return NULL; } G_SOCKET_CONNECTIONS_INC(); if(net__socket_nonblock(&new_sock)){ - return INVALID_SOCKET; + return NULL; } #ifdef WITH_WRAP @@ -165,7 +165,7 @@ int net__socket_accept(struct mosquitto__listener_sock *listensock) } } COMPAT_CLOSE(new_sock); - return -1; + return NULL; } #endif @@ -183,12 +183,12 @@ int net__socket_accept(struct mosquitto__listener_sock *listensock) new_context = context__init(new_sock); if(!new_context){ COMPAT_CLOSE(new_sock); - return -1; + return NULL; } new_context->listener = listensock->listener; if(!new_context->listener){ context__cleanup(new_context, true); - return -1; + return NULL; } new_context->listener->client_count++; @@ -197,7 +197,7 @@ int net__socket_accept(struct mosquitto__listener_sock *listensock) log__printf(NULL, MOSQ_LOG_NOTICE, "Client connection from %s denied: max_connections exceeded.", new_context->address); } context__cleanup(new_context, true); - return -1; + return NULL; } #ifdef WITH_TLS @@ -206,7 +206,7 @@ int net__socket_accept(struct mosquitto__listener_sock *listensock) new_context->ssl = SSL_new(new_context->listener->ssl_ctx); if(!new_context->ssl){ context__cleanup(new_context, true); - return -1; + return NULL; } SSL_set_ex_data(new_context->ssl, tls_ex_index_context, new_context); SSL_set_ex_data(new_context->ssl, tls_ex_index_listener, new_context->listener); @@ -232,7 +232,7 @@ int net__socket_accept(struct mosquitto__listener_sock *listensock) } } context__cleanup(new_context, true); - return -1; + return NULL; } } } @@ -242,7 +242,7 @@ int net__socket_accept(struct mosquitto__listener_sock *listensock) log__printf(NULL, MOSQ_LOG_NOTICE, "New connection from %s on port %d.", new_context->address, new_context->listener->port); } - return new_sock; + return new_context; } #ifdef WITH_TLS