diff --git a/ChangeLog.txt b/ChangeLog.txt index fa0e7f11..c7a416e4 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -98,6 +98,7 @@ Broker: `notifications_local_only` was set true. Closes #1902. - Bridges now obey MQTT v5 server-keepalive. - Add bridge support for the MQTT v5 maximum-qos property. +- Log client port on new connections. Closes #1911. Client library: - Client no longer generates random client ids for v3.1.1 clients, these are diff --git a/lib/mosquitto_internal.h b/lib/mosquitto_internal.h index 14073619..6810dc32 100644 --- a/lib/mosquitto_internal.h +++ b/lib/mosquitto_internal.h @@ -346,6 +346,7 @@ struct mosquitto { UT_hash_handle hh_sock; struct mosquitto *for_free_next; struct session_expiry_list *expiry_list_item; + uint16_t remote_port; #endif #ifdef WITH_EPOLL uint32_t events; diff --git a/src/context.c b/src/context.c index 9b1eb596..698a76ab 100644 --- a/src/context.c +++ b/src/context.c @@ -71,7 +71,7 @@ struct mosquitto *context__init(mosq_sock_t sock) context->address = NULL; if((int)sock >= 0){ - if(!net__socket_get_address(sock, address, 1024)){ + if(!net__socket_get_address(sock, address, 1024, &context->remote_port)){ context->address = mosquitto__strdup(address); } if(!context->address){ diff --git a/src/handle_connect.c b/src/handle_connect.c index ad21c5b2..7be9833a 100644 --- a/src/handle_connect.c +++ b/src/handle_connect.c @@ -184,19 +184,19 @@ int connect__on_authorised(struct mosquitto *context, void *auth_data_out, uint1 if(db.config->connection_messages == true){ if(context->is_bridge){ if(context->username){ - log__printf(NULL, MOSQ_LOG_NOTICE, "New bridge connected from %s as %s (p%d, c%d, k%d, u'%s').", - context->address, context->id, context->protocol, context->clean_start, context->keepalive, context->username); + log__printf(NULL, MOSQ_LOG_NOTICE, "New bridge connected from %s:%d as %s (p%d, c%d, k%d, u'%s').", + context->address, context->remote_port, context->id, context->protocol, context->clean_start, context->keepalive, context->username); }else{ - log__printf(NULL, MOSQ_LOG_NOTICE, "New bridge connected from %s as %s (p%d, c%d, k%d).", - context->address, context->id, context->protocol, context->clean_start, context->keepalive); + log__printf(NULL, MOSQ_LOG_NOTICE, "New bridge connected from %s:%d as %s (p%d, c%d, k%d).", + context->address, context->remote_port, context->id, context->protocol, context->clean_start, context->keepalive); } }else{ if(context->username){ - log__printf(NULL, MOSQ_LOG_NOTICE, "New client connected from %s as %s (p%d, c%d, k%d, u'%s').", - context->address, context->id, context->protocol, context->clean_start, context->keepalive, context->username); + log__printf(NULL, MOSQ_LOG_NOTICE, "New client connected from %s:%d as %s (p%d, c%d, k%d, u'%s').", + context->address, context->remote_port, context->id, context->protocol, context->clean_start, context->keepalive, context->username); }else{ - log__printf(NULL, MOSQ_LOG_NOTICE, "New client connected from %s as %s (p%d, c%d, k%d).", - context->address, context->id, context->protocol, context->clean_start, context->keepalive); + log__printf(NULL, MOSQ_LOG_NOTICE, "New client connected from %s:%d as %s (p%d, c%d, k%d).", + context->address, context->remote_port, context->id, context->protocol, context->clean_start, context->keepalive); } } diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index 25e868e4..613e8a74 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -607,7 +607,7 @@ void net__broker_init(void); void net__broker_cleanup(void); 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__socket_get_address(mosq_sock_t sock, char *buf, size_t len, uint16_t *remote_address); int net__tls_load_verify(struct mosquitto__listener *listener); int net__tls_server_ctx(struct mosquitto__listener *listener); int net__load_certificates(struct mosquitto__listener *listener); diff --git a/src/net.c b/src/net.c index 76464f7c..dad2a9fe 100644 --- a/src/net.c +++ b/src/net.c @@ -162,7 +162,7 @@ struct mosquitto *net__socket_accept(struct mosquitto__listener_sock *listensock if(!hosts_access(&wrap_req)){ /* Access is denied */ if(db.config->connection_messages == true){ - if(!net__socket_get_address(new_sock, address, 1024)){ + if(!net__socket_get_address(new_sock, address, 1024, NULL)){ log__printf(NULL, MOSQ_LOG_NOTICE, "Client connection from %s denied access by tcpd.", address); } } @@ -241,7 +241,8 @@ struct mosquitto *net__socket_accept(struct mosquitto__listener_sock *listensock #endif if(db.config->connection_messages == true){ - log__printf(NULL, MOSQ_LOG_NOTICE, "New connection from %s on port %d.", new_context->address, new_context->listener->port); + log__printf(NULL, MOSQ_LOG_NOTICE, "New connection from %s:%d on port %d.", + new_context->address, new_context->remote_port, new_context->listener->port); } return new_context; @@ -835,7 +836,7 @@ int net__socket_listen(struct mosquitto__listener *listener) } } -int net__socket_get_address(mosq_sock_t sock, char *buf, size_t len) +int net__socket_get_address(mosq_sock_t sock, char *buf, size_t len, uint16_t *remote_port) { struct sockaddr_storage addr; socklen_t addrlen; @@ -844,10 +845,16 @@ int net__socket_get_address(mosq_sock_t sock, char *buf, size_t len) addrlen = sizeof(addr); if(!getpeername(sock, (struct sockaddr *)&addr, &addrlen)){ if(addr.ss_family == AF_INET){ + if(remote_port){ + *remote_port = ntohs(((struct sockaddr_in *)&addr)->sin_port); + } if(inet_ntop(AF_INET, &((struct sockaddr_in *)&addr)->sin_addr.s_addr, buf, (socklen_t)len)){ return 0; } }else if(addr.ss_family == AF_INET6){ + if(remote_port){ + *remote_port = ntohs(((struct sockaddr_in6 *)&addr)->sin6_port); + } if(inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&addr)->sin6_addr.s6_addr, buf, (socklen_t)len)){ return 0; } diff --git a/src/websockets.c b/src/websockets.c index 10e09295..53c8379a 100644 --- a/src/websockets.c +++ b/src/websockets.c @@ -111,7 +111,7 @@ static void easy_address(int sock, struct mosquitto *mosq) { char address[1024]; - if(!net__socket_get_address(sock, address, 1024)){ + if(!net__socket_get_address(sock, address, 1024, &mosq->remote_port)){ mosq->address = mosquitto__strdup(address); } }