From 4e1e8579499d16247af59127334c47b959a711d3 Mon Sep 17 00:00:00 2001 From: Joseph Henry Date: Mon, 12 Oct 2015 21:26:32 -0400 Subject: [PATCH] Minor bug fixes --- ext/lwipopts.h | 2 +- netcon/NetconEthernetTap.cpp | 18 +++++++------- netcon/intercept.c | 46 +++++++++++++++++------------------- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/ext/lwipopts.h b/ext/lwipopts.h index e7b78a8fc..4f867e71c 100644 --- a/ext/lwipopts.h +++ b/ext/lwipopts.h @@ -130,7 +130,7 @@ * MEMP_NUM_RAW_PCB: Number of raw connection PCBs * (requires the LWIP_RAW option) */ -#define MEMP_NUM_RAW_PCB 128 +#define MEMP_NUM_RAW_PCB 1 /** * MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One diff --git a/netcon/NetconEthernetTap.cpp b/netcon/NetconEthernetTap.cpp index 0e2599eab..136da1f6e 100644 --- a/netcon/NetconEthernetTap.cpp +++ b/netcon/NetconEthernetTap.cpp @@ -386,14 +386,9 @@ void NetconEthernetTap::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsi void NetconEthernetTap::phyOnTcpWritable(PhySocket *sock,void **uptr) {} /* - * Creates a new NetconClient for the accepted RPC connection (unix domain socket) - * - * Subsequent socket connections from this client will be associated with this - * NetconClient object. + * Add a new PhySocket for the client connection */ -void NetconEthernetTap::phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN) -{ - //fprintf(stderr, "phyOnUnixAccept() NEW CLIENT RPC: %d\n", _phy.getDescriptor(sockN)); +void NetconEthernetTap::phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN) { rpc_sockets.push_back(sockN); } @@ -505,7 +500,8 @@ int NetconEthernetTap::send_return_value(int fd, int retval, int _errno = 0) [ ] EINVAL - (accept4()) invalid value in flags. [ ] EMFILE - The per-process limit of open file descriptors has been reached. [ ] ENFILE - The system limit on the total number of open files has been reached. - [ ] ENOBUFS, ENOMEM - Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory. + [ ] ENOBUFS, ENOMEM - Not enough free memory. This often means that the memory allocation is + limited by the socket buffer limits, not by the system memory. [i] ENOTSOCK - The descriptor references a file, not a socket. [i] EOPNOTSUPP - The referenced socket is not of type SOCK_STREAM. [ ] EPROTO - Protocol error. @@ -707,13 +703,13 @@ err_t NetconEthernetTap::nc_poll(void* arg, struct tcp_pcb *tpcb) //fprintf(stderr, "nc_poll(): now = %u\n", now); //fprintf(stderr, "nc_poll\n"); -/* + Larg *l = (Larg*)arg; TcpConnection *conn = l->conn; NetconEthernetTap *tap = l->tap; if(conn && conn->idx) // if valid connection and non-zero index (indicating data present) tap->handle_write(conn); -*/ + return ERR_OK; } @@ -815,6 +811,8 @@ void NetconEthernetTap::handle_bind(PhySocket *sock, void **uptr, struct bind_st if(err == ERR_USE) send_return_value(conn, -1, EADDRINUSE); if(err == ERR_MEM) + send_return_value(conn, -1, ENOMEM); // FIXME: Likely won't happen + if(err == ERR_BUF) send_return_value(conn, -1, ENOMEM); } else { diff --git a/netcon/intercept.c b/netcon/intercept.c index f4fff1f9f..f2a31b450 100755 --- a/netcon/intercept.c +++ b/netcon/intercept.c @@ -112,11 +112,6 @@ void load_symbols(void); void set_up_intercept(); int checkpid(); -/* defined in unistd.h, but we don't include it because -it conflicts with our overriden symbols for read/write */ -#define STDIN_FILENO 0 -#define STDOUT_FILENO 1 -#define STDERR_FILENO 2 #define BUF_SZ 1024 #define SERVICE_CONNECT_ATTEMPTS 30 @@ -134,6 +129,9 @@ pthread_mutex_t loglock; ------------------- Intercept<--->Service Comm mechanisms----------------------- ------------------------------------------------------------------------------*/ +// TODO: Find minimum BUF_SZ for RPC +// TODO: Refactor RPC send logic + static int is_initialized = 0; static int fdret_sock; // used for fd-transfers static int newfd; // used for "this_end" socket @@ -142,10 +140,8 @@ static char* af_sock_name = "/tmp/.ztnc_e5cd7a9e1c5311ab"; static int thispid; /* -* -* Check for forking -* -*/ + * Check for forking + */ int checkpid() { if(thispid != getpid()) { printf("clone/fork detected. re-initializing this instance.\n"); @@ -157,9 +153,20 @@ int checkpid() { } /* -* + * Sends an RPC command to the service + */ +void send_command(int rpc_fd, char *cmd) +{ + int n_write = write(rpc_fd, cmd, BUF_SZ); + if(n_write < 0){ + dwr("Error writing command to service (CMD = %d)\n", cmd[0]); + errno = 0; + //return -1; + } + +} +/* * Reads a return value from the service and sets errno (if applicable) -* */ int get_retval() { @@ -541,11 +548,7 @@ int socket(SOCKET_SIG) cmd[0] = RPC_FD_MAP_COMPLETION; memcpy(&cmd[1], &newfd, sizeof(newfd)); if(newfd > -1) { - int n_write = write(fdret_sock, cmd, BUF_SZ); - if(n_write < 0) { - dwr("Error writing perceived FD to service.\n"); - return get_retval(); - } + send_command(fdret_sock, cmd); pthread_mutex_unlock(&lock); errno = ERR_OK; // OK return newfd; @@ -611,7 +614,7 @@ int connect(CONNECT_SIG) cmd[0] = RPC_CONNECT; memcpy(&cmd[1], &rpc_st, sizeof(struct connect_st)); pthread_mutex_lock(&lock); - write(fdret_sock,cmd, BUF_SZ); + send_command(fdret_sock, cmd); if(fdret_sock >= 0) { int retval; @@ -675,7 +678,6 @@ int bind(BIND_SIG) #ifdef DUMMY dwr("bind(%d)\n", sockfd); return realbind(sockfd, addr, addrlen); - #else /* make sure we don't touch any standard outputs */ if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO) @@ -711,7 +713,7 @@ int bind(BIND_SIG) cmd[0]=RPC_BIND; memcpy(&cmd[1], &rpc_st, sizeof(struct bind_st)); pthread_mutex_lock(&lock); - write(fdret_sock, cmd, BUF_SZ); + send_command(fdret_sock, cmd); pthread_mutex_unlock(&lock); errno = ERR_OK; return get_retval(); @@ -730,7 +732,6 @@ int accept4(ACCEPT4_SIG) #ifdef DUMMY dwr("accept4(%d)\n", sockfd); return accept(sockfd, addr, addrlen); - #else return accept(sockfd, addr, addrlen); #endif @@ -747,7 +748,6 @@ int accept(ACCEPT_SIG) { #ifdef DUMMY return realaccept(sockfd, addr, addrlen); - #else /* make sure we don't touch any standard outputs */ if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO) @@ -820,9 +820,7 @@ int listen(LISTEN_SIG) #ifdef DUMMY dwr("listen(%d)\n", sockfd); return reallisten(sockfd, backlog); - #else - /* make sure we don't touch any standard outputs */ if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO) return(reallisten(sockfd, backlog)); @@ -838,7 +836,7 @@ int listen(LISTEN_SIG) cmd[0] = RPC_LISTEN; memcpy(&cmd[1], &rpc_st, sizeof(struct listen_st)); pthread_mutex_lock(&lock); - write(fdret_sock,cmd, BUF_SZ); + send_command(fdret_sock, cmd); pthread_mutex_unlock(&lock); errno = ERR_OK; return get_retval();