From af78831af05d04b56e8f27b55c329c196ba631be Mon Sep 17 00:00:00 2001 From: Wirlaburla Date: Wed, 1 Jan 2025 21:41:48 -0600 Subject: [PATCH] add client IP output on new connection --- src/main.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index bf2c215..f80e3bc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -235,7 +235,9 @@ int main(int argc , char *argv[]) { if (fds[i].fd == master_socket && fds[i].revents == POLLIN) { // Handle new connections do { - newsock = accept(master_socket, NULL, NULL); + struct sockaddr_in client_name; + socklen_t client_len = sizeof(client_name); + newsock = accept(master_socket, (struct sockaddr*)&client_name, &client_len); if (newsock < 0) { if (errno != EWOULDBLOCK) { logger->print(LOGLEVEL_ERROR, "net: accept() failed: {}", strerror(errno)); @@ -262,8 +264,17 @@ int main(int argc , char *argv[]) { // Set non-blocking mode int flags = fcntl(newsock, F_GETFL, 0); fcntl(newsock, F_SETFL, flags | O_NONBLOCK); + + char client_addr[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &client_name.sin_addr, client_addr, sizeof(client_addr)); - logger->print(LOGLEVEL_DEBUG, "client {} accepted in slot {}", newsock, slot); + logger->print( + LOGLEVEL_DEBUG, + "client {} (ip: {}) accepted in slot {}", + newsock, + client_addr, + slot + ); fds[slot].fd = newsock; fds[slot].events = POLLIN;