about summary refs log tree commit diff
path: root/cptc.c
diff options
context:
space:
mode:
Diffstat (limited to 'cptc.c')
-rw-r--r--cptc.c32
1 files changed, 6 insertions, 26 deletions
diff --git a/cptc.c b/cptc.c
index 1dafddc..32cdfcc 100644
--- a/cptc.c
+++ b/cptc.c
@@ -1,3 +1,4 @@
+#define _GNU_SOURCE
 #include "cptc.h"
 
 #include <stdbool.h>
@@ -8,7 +9,6 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
-#include <pthread.h>
 #include <unistd.h>
 
 #include <curl/curl.h>
@@ -23,30 +23,11 @@
     } while (0)             \
 
 
-struct RequestHandlerArgs
-{
-    int fd;
-    int n;
-    bool accept;
-};
-
-static void *requestHandlerThread(struct RequestHandlerArgs *arg)
-{
-    int fd = arg->fd;
-    int n = arg->n;
-    arg->accept = true;
-    CPTC_requestHandler(fd, n);
-    close(fd);
-    pthread_exit(NULL);
-}
-
-
-void CPTC(const char *ip, in_port_t port)
+void CPTC(const char *ip, in_port_t port, unsigned id)
 {
     struct sockaddr_in addr, peer;
     socklen_t peer_size;
     int fd, peerfd;
-    int n = 0;
 
     curl_easy_init();
 
@@ -54,6 +35,8 @@ void CPTC(const char *ip, in_port_t port)
         error("socket()", 1);
     if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int))) < 0)
         error("setsockopt()", 1);
+    if ((setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &(int){1}, sizeof(int))) < 0)
+        error("setsockopt()", 1);
 
     addr = (struct sockaddr_in)
     {
@@ -78,10 +61,7 @@ void CPTC(const char *ip, in_port_t port)
         }
         printf("Connection from %s:%u!\n", inet_ntoa(peer.sin_addr), peer.sin_port);
 
-        pthread_t thread;
-        struct RequestHandlerArgs arg = {peerfd, n++, false};
-        pthread_create(&thread, NULL, (void *(*)(void *))requestHandlerThread, &arg);
-        pthread_detach(thread);
-        while (!arg.accept);
+        CPTC_requestHandler(peerfd, id);
+        close(peerfd);
     }
 }