Record hostname for each connected client

Signed-off-by: Jim Ramsay <jramsay@redhat.com>
pull/83/head
Jim Ramsay 2022-10-31 12:02:43 -04:00 committed by Andri Yngvason
parent 73e1089f06
commit 86bd2ced85
3 changed files with 30 additions and 1 deletions

View File

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

View File

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

View File

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