Log client port on new connections.

Closes #1911. Thanks to twegener-embertec.
pull/1920/head
Roger A. Light 5 years ago
parent a7f044bcea
commit 232a759320

@ -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

@ -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;

@ -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){

@ -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);
}
}

@ -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);

@ -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;
}

@ -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);
}
}

Loading…
Cancel
Save