Code

connection: Log peer information after accepting a connection.
authorSebastian Harl <sh@tokkee.org>
Fri, 16 Jan 2015 13:31:42 +0000 (14:31 +0100)
committerSebastian Harl <sh@tokkee.org>
Fri, 16 Jan 2015 13:31:42 +0000 (14:31 +0100)
src/frontend/connection.c

index 6b359d347a9f14e3026ebcd6a6df37708eab7da7..62e5f9de6cb717c6e5e8807771fae01d040f7de1 100644 (file)
 #include <arpa/inet.h>
 #include <fcntl.h>
 
+#include <sys/socket.h>
+#include <sys/un.h>
+
 #include <stdlib.h>
 #include <string.h>
 
 #include <pthread.h>
+#include <netdb.h>
 
 /*
  * private variables
@@ -410,6 +414,7 @@ sdb_conn_t *
 sdb_connection_accept(int fd, sdb_conn_setup_cb setup, void *user_data)
 {
        sdb_conn_t *conn;
+       const char *peer = "unknown";
 
        if (fd < 0)
                return NULL;
@@ -422,6 +427,22 @@ sdb_connection_accept(int fd, sdb_conn_setup_cb setup, void *user_data)
                sdb_object_deref(SDB_OBJ(conn));
                return NULL;
        }
+
+       if (conn->username)
+               peer = conn->username;
+
+       if (conn->client_addr.ss_family == AF_UNIX) {
+               sdb_log(SDB_LOG_INFO,
+                               "frontend: Accepted connection from peer %s", peer);
+       }
+       else {
+               char host[1024] = "<unknown>", port[32] = "";
+               getnameinfo((struct sockaddr *)&conn->client_addr,
+                               conn->client_addr_len, host, sizeof(host), port, sizeof(port),
+                               NI_NUMERICHOST | NI_NUMERICSERV);
+               sdb_log(SDB_LOG_INFO, "frontend: Accepted connection from "
+                               "peer %s at %s:%s", peer, host, port);
+       }
        return conn;
 } /* sdb_connection_create */