From 51bbd571adef98c445fac8b1b1d3b6d987c935ff Mon Sep 17 00:00:00 2001 From: quackerd Date: Tue, 14 Mar 2023 14:47:29 +0100 Subject: [PATCH] master slave mode working --- dsmbr/dsmbr.cc | 14 +++++++++++++- ppd/ppd.cc | 22 +++++++++++----------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/dsmbr/dsmbr.cc b/dsmbr/dsmbr.cc index 4e3daa0..3e42ce3 100755 --- a/dsmbr/dsmbr.cc +++ b/dsmbr/dsmbr.cc @@ -566,7 +566,9 @@ dsmbr_slave_accept_master(int slave_ctrl_sock) int ret; int conn_fd; struct sockaddr addr; + struct sockaddr_in * inaddr; socklen_t addrlen; + char ip[INET_ADDRSTRLEN]; ret = accept(slave_ctrl_sock, &addr, &addrlen); if (ret < 0) { @@ -574,7 +576,17 @@ dsmbr_slave_accept_master(int slave_ctrl_sock) } conn_fd = ret; - V("Accepted master connection %d.\n", conn_fd); + ret = getpeername(conn_fd, &addr, &addrlen); + if (ret < 0) { + E("getpeername failed: %d\n", errno); + } + + inaddr = (struct sockaddr_in *)&addr; + if (inet_ntop(AF_INET, &inaddr->sin_addr, ip, INET_ADDRSTRLEN) == NULL) { + E("inet_ntop failed: %d\n", errno); + } + + V("Accepted master connection %d from %s.\n", conn_fd, ip); return conn_fd; } diff --git a/ppd/ppd.cc b/ppd/ppd.cc index 4051d3b..b4dde2f 100755 --- a/ppd/ppd.cc +++ b/ppd/ppd.cc @@ -3,10 +3,8 @@ #include #include #include - #include #include - #include #include #include @@ -14,7 +12,6 @@ #include #include #include - #include #include "logger.h" @@ -34,9 +31,7 @@ #include #include -extern "C" { LOGGER_VAR_DECL; -} static constexpr int NEVENT = 64; static constexpr int SOCK_BACKLOG = 10000; @@ -601,10 +596,10 @@ loop_main(int m_kq, std::vector *workers) } // new connection - struct sockaddr_in client_addr; + struct sockaddr client_addr; socklen_t client_addr_size = sizeof(client_addr); - int conn_fd = accept(kev.ident, (struct sockaddr *)&client_addr, &client_addr_size); + int conn_fd = accept(kev.ident, &client_addr, &client_addr_size); if (conn_fd < 0) { W("accept() failed: %d\n", errno); @@ -622,11 +617,16 @@ loop_main(int m_kq, std::vector *workers) } } - char ipaddr[INET_ADDRSTRLEN + 1]; - strncpy(ipaddr, inet_ntoa(client_addr.sin_addr), INET_ADDRSTRLEN); - ipaddr[INET_ADDRSTRLEN] = 0; + if (getpeername(conn_fd, &client_addr, &client_addr_size) < 0) { + E("getpeername failed: %d\n", errno); + } - V("Accepted new connection %d from %s\n", conn_fd, ipaddr); + char client_ip[INET_ADDRSTRLEN]; + if (inet_ntop(AF_INET, &(((struct sockaddr_in *)&client_addr)->sin_addr), client_ip, sizeof(client_ip)) == NULL) { + E("inet_ntop failed: %d\n", errno); + } + + V("Accepted connection %d from %s.\n", conn_fd, client_ip); struct ppd_conn *conn = new struct ppd_conn;