From d4c3083c422183abe71cdb6dd97c3ffbdf0c18ff Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Sun, 5 Nov 2023 10:29:01 +0000 Subject: [PATCH] server: Remove DNS lookup DNS lookup is slow and can even fail. Under some circumstances, it will even block for a significant amount of time until it completes. The user of this library can do the lookup instead if they wish. --- src/server.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/server.c b/src/server.c index 0119eb4..6381b4a 100644 --- a/src/server.c +++ b/src/server.c @@ -82,6 +82,8 @@ static void on_encode_frame_done(struct encoder*, struct rcbuf*, uint64_t pts); static bool client_has_encoding(const struct nvnc_client* client, enum rfb_encodings encoding); static void process_fb_update_requests(struct nvnc_client* client); +static void sockaddr_to_string(char* dst, size_t sz, + const struct sockaddr* addr); #if defined(GIT_VERSION) EXPORT const char nvnc_version[] = GIT_VERSION; @@ -1673,21 +1675,23 @@ 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) { - 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); - } + 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); } } @@ -1774,6 +1778,11 @@ static void sockaddr_to_string(char* dst, size_t sz, const struct sockaddr* addr case AF_INET6: inet_ntop(addr->sa_family, &sa_in6->sin6_addr, dst, sz); break; + default: + nvnc_log(NVNC_LOG_DEBUG, + "Don't know how to convert sa_family %d to string", + addr->sa_family); + break; } } @@ -2265,6 +2274,8 @@ 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')