From f503cbef257bd9e67b8bd6963acc8e80901bdbc3 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Sun, 31 Dec 2023 17:59:52 +0000 Subject: [PATCH] Replace nvnc_client_get_hostname with nvnc_client_get_address This is a more accurate name for what is returned since c76129b2d24f5d6c7642d37bb1f6ac4e1025d9e8. --- include/common.h | 1 - include/neatvnc.h | 4 +++- src/server.c | 40 +++++++++++----------------------------- 3 files changed, 14 insertions(+), 31 deletions(-) diff --git a/include/common.h b/include/common.h index dd06813..77a974c 100644 --- a/include/common.h +++ b/include/common.h @@ -83,7 +83,6 @@ struct nvnc_client { struct nvnc_common common; int ref; struct stream* net_stream; - char hostname[256]; char username[256]; struct nvnc* server; enum nvnc_client_state state; diff --git a/include/neatvnc.h b/include/neatvnc.h index 535baea..806531a 100644 --- a/include/neatvnc.h +++ b/include/neatvnc.h @@ -22,6 +22,7 @@ #include #include #include +#include #define NVNC_NO_PTS UINT64_MAX @@ -141,7 +142,8 @@ void* nvnc_get_userdata(const void* self); struct nvnc* nvnc_client_get_server(const struct nvnc_client* client); bool nvnc_client_supports_cursor(const struct nvnc_client* client); -const char* nvnc_client_get_hostname(const struct nvnc_client* client); +int nvnc_client_get_address(const struct nvnc_client* client, + struct sockaddr* restrict addr, socklen_t* restrict addrlen); const char* nvnc_client_get_auth_username(const struct nvnc_client* client); struct nvnc_client* nvnc_client_first(struct nvnc* self); diff --git a/src/server.c b/src/server.c index ee8cafb..8f5c048 100644 --- a/src/server.c +++ b/src/server.c @@ -1672,26 +1672,6 @@ static void on_client_event(struct stream* stream, enum stream_event event) client->buffer_index = 0; } -// TODO: Remove this when nvnc_client_get_hostname gets renamed. -static void record_peer_hostname(int fd, struct nvnc_client* client) -{ - struct sockaddr_storage storage; - struct sockaddr* peer = (struct sockaddr*)&storage; - socklen_t peerlen = sizeof(storage); - if (getpeername(fd, peer, &peerlen) < 0) { - nvnc_log(NVNC_LOG_WARNING, "Failed to get address for client: %m"); - return; - } - - if (peer->sa_family == AF_UNIX) { - snprintf(client->hostname, sizeof(client->hostname), - "unix domain socket"); - } else { - sockaddr_to_string(client->hostname, sizeof(client->hostname), - peer); - } -} - static void on_connection(void* obj) { struct nvnc* server = aml_get_userdata(obj); @@ -1713,8 +1693,6 @@ static void on_connection(void* obj) int one = 1; setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); - record_peer_hostname(fd, client); - #ifdef ENABLE_WEBSOCKET if (server->socket_type == NVNC__SOCKET_WEBSOCKET) { @@ -1749,7 +1727,14 @@ static void on_connection(void* obj) client->state = VNC_CLIENT_STATE_WAITING_FOR_VERSION; - nvnc_log(NVNC_LOG_INFO, "New client connection from %s: %p (ref %d)", client->hostname, client, client->ref); + char ip_address[256]; + struct sockaddr_storage addr; + socklen_t addrlen = sizeof(addr); + nvnc_client_get_address(client, (struct sockaddr*)&addr, &addrlen); + sockaddr_to_string(ip_address, sizeof(ip_address), + (struct sockaddr*)&addr); + nvnc_log(NVNC_LOG_INFO, "New client connection from %s: %p (ref %d)", + ip_address, client, client->ref); return; @@ -2273,13 +2258,10 @@ struct nvnc* nvnc_client_get_server(const struct nvnc_client* client) return client->server; } -// TODO: This function should be renamed to nvnc_client_get_address and it -// should return the sockaddr. EXPORT -const char* nvnc_client_get_hostname(const struct nvnc_client* client) { - if (client->hostname[0] == '\0') - return NULL; - return client->hostname; +int nvnc_client_get_address(const struct nvnc_client* client, + struct sockaddr* restrict addr, socklen_t* restrict addrlen) { + return getpeername(client->net_stream->fd, addr, addrlen); } EXPORT