diff --git a/src/desock.c b/src/desock.c index 85a28c7..1b121f2 100644 --- a/src/desock.c +++ b/src/desock.c @@ -1,6 +1,8 @@ #define _GNU_SOURCE +#include #include +#include #include #include #include @@ -8,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -18,11 +21,12 @@ #define PREENY_MAX_FD 8192 #define PREENY_SOCKET_OFFSET 500 #define READ_BUF_SIZE 65536 +#define INPUT_FILE_PATH_ENV "PREENY_DESOCK_INFILE" #define PREENY_SOCKET(x) (x+PREENY_SOCKET_OFFSET) int preeny_desock_shutdown_flag = 0; -int preeny_desock_did_accept = 0; +int in_fd = 0; pthread_t *preeny_socket_threads_to_front[PREENY_MAX_FD] = { 0 }; pthread_t *preeny_socket_threads_to_back[PREENY_MAX_FD] = { 0 }; @@ -36,6 +40,7 @@ int preeny_socket_sync(int from, int to, int timeout) int n; int r; + r = poll(&poll_in, 1, timeout); if (r < 0) { @@ -141,7 +146,8 @@ void *preeny_socket_sync_to_front(void *fd) { int front_fd = (int)fd; int back_fd = PREENY_SOCKET(front_fd); - preeny_socket_sync_loop(0, back_fd); + /* Read fd here */ + preeny_socket_sync_loop(in_fd, back_fd); return NULL; } @@ -160,6 +166,11 @@ __attribute__((constructor)) void preeny_desock_orig() original_accept = dlsym(RTLD_NEXT, "accept"); original_bind = dlsym(RTLD_NEXT, "bind"); original_connect = dlsym(RTLD_NEXT, "connect"); + + if (getenv(INPUT_FILE_PATH_ENV) != NULL) { + in_fd = open(getenv(INPUT_FILE_PATH_ENV), O_RDONLY); + assert(in_fd != -1 && "Input file not found"); + } } int socket(int domain, int type, int protocol) @@ -187,6 +198,7 @@ int socket(int domain, int type, int protocol) front_socket = fds[0]; back_socket = dup2(fds[1], PREENY_SOCKET(front_socket)); + printf("front = %d, back = %d\n", front_socket, back_socket); close(fds[1]); preeny_debug("... dup into socketpair (%d, %d)\n", fds[0], back_socket); @@ -213,10 +225,6 @@ int socket(int domain, int type, int protocol) int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { - if (preeny_desock_did_accept) - exit(0); - preeny_desock_did_accept = 1; - //initialize a sockaddr_in for the peer struct sockaddr_in peer_addr; memset(&peer_addr, '0', sizeof(struct sockaddr_in));