master slave mode working

This commit is contained in:
quackerd 2023-03-14 14:47:29 +01:00
parent ddf0b5ce2d
commit 51bbd571ad
2 changed files with 24 additions and 12 deletions

View File

@ -566,7 +566,9 @@ dsmbr_slave_accept_master(int slave_ctrl_sock)
int ret; int ret;
int conn_fd; int conn_fd;
struct sockaddr addr; struct sockaddr addr;
struct sockaddr_in * inaddr;
socklen_t addrlen; socklen_t addrlen;
char ip[INET_ADDRSTRLEN];
ret = accept(slave_ctrl_sock, &addr, &addrlen); ret = accept(slave_ctrl_sock, &addr, &addrlen);
if (ret < 0) { if (ret < 0) {
@ -574,7 +576,17 @@ dsmbr_slave_accept_master(int slave_ctrl_sock)
} }
conn_fd = ret; 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; return conn_fd;
} }

View File

@ -3,10 +3,8 @@
#include <sys/event.h> #include <sys/event.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
#include <openssl/err.h> #include <openssl/err.h>
@ -14,7 +12,6 @@
#include <pthread.h> #include <pthread.h>
#include <pthread_np.h> #include <pthread_np.h>
#include <unistd.h> #include <unistd.h>
#include <bsock/bsock.h> #include <bsock/bsock.h>
#include "logger.h" #include "logger.h"
@ -34,9 +31,7 @@
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
extern "C" {
LOGGER_VAR_DECL; LOGGER_VAR_DECL;
}
static constexpr int NEVENT = 64; static constexpr int NEVENT = 64;
static constexpr int SOCK_BACKLOG = 10000; static constexpr int SOCK_BACKLOG = 10000;
@ -601,10 +596,10 @@ loop_main(int m_kq, std::vector<struct ppd_thread_ctx *> *workers)
} }
// new connection // new connection
struct sockaddr_in client_addr; struct sockaddr client_addr;
socklen_t client_addr_size = sizeof(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) { if (conn_fd < 0) {
W("accept() failed: %d\n", errno); W("accept() failed: %d\n", errno);
@ -622,11 +617,16 @@ loop_main(int m_kq, std::vector<struct ppd_thread_ctx *> *workers)
} }
} }
char ipaddr[INET_ADDRSTRLEN + 1]; if (getpeername(conn_fd, &client_addr, &client_addr_size) < 0) {
strncpy(ipaddr, inet_ntoa(client_addr.sin_addr), INET_ADDRSTRLEN); E("getpeername failed: %d\n", errno);
ipaddr[INET_ADDRSTRLEN] = 0; }
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; struct ppd_conn *conn = new struct ppd_conn;