diff --git a/include/common.h b/include/common.h index 11e8cc3..8ba3699 100644 --- a/include/common.h +++ b/include/common.h @@ -69,6 +69,7 @@ struct nvnc_client { struct nvnc_common common; int ref; struct stream* net_stream; + char hostname[256]; struct nvnc* server; enum nvnc_client_state state; bool has_pixfmt; diff --git a/include/neatvnc.h b/include/neatvnc.h index 7e24399..622df31 100644 --- a/include/neatvnc.h +++ b/include/neatvnc.h @@ -131,6 +131,7 @@ 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); void nvnc_set_name(struct nvnc* self, const char* name); diff --git a/src/server.c b/src/server.c index e5a1a10..6d91ed5 100644 --- a/src/server.c +++ b/src/server.c @@ -1102,6 +1102,24 @@ static void on_client_event(struct stream* stream, enum stream_event event) client->buffer_index = 0; } +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) { + if (peer->sa_family == AF_UNIX) { + snprintf(client->hostname, sizeof(client->hostname), + "unix domain socket"); + } else { + getnameinfo(peer, peerlen, + client->hostname, sizeof(client->hostname), + NULL, 0, // no need for port + 0); + } + } +} + static void on_connection(void* obj) { struct nvnc* server = aml_get_userdata(obj); @@ -1123,6 +1141,8 @@ static void on_connection(void* obj) int one = 1; setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); + record_peer_hostname(fd, client); + client->net_stream = stream_new(fd, on_client_event, client); if (!client->net_stream) { nvnc_log(NVNC_LOG_WARNING, "OOM"); @@ -1148,7 +1168,7 @@ static void on_connection(void* obj) client->state = VNC_CLIENT_STATE_WAITING_FOR_VERSION; - nvnc_log(NVNC_LOG_INFO, "New client connection: %p (ref %d)", client, client->ref); + nvnc_log(NVNC_LOG_INFO, "New client connection from %s: %p (ref %d)", client->hostname, client, client->ref); return; @@ -1604,6 +1624,13 @@ struct nvnc* nvnc_client_get_server(const struct nvnc_client* client) return client->server; } +EXPORT +const char* nvnc_client_get_hostname(const struct nvnc_client* client) { + if (client->hostname[0] == '\0') + return NULL; + return client->hostname; +} + EXPORT bool nvnc_client_supports_cursor(const struct nvnc_client* client) {