From 5af229a86eced9409583c6b12b144d87360d0fb9 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Wed, 2 Nov 2022 00:18:13 +0000 Subject: [PATCH] Apply #31 --- TODO.txt | 4 ++-- src/HttpServer.c | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/TODO.txt b/TODO.txt index beba613..6db6f1d 100644 --- a/TODO.txt +++ b/TODO.txt @@ -59,8 +59,8 @@ Phase 2: Building a foundation file live there as well. [x] Allow logging to the syslog [x] Fix memory leaks -[ ] Fix bug where the socket stays open after quit. - [ ] Possibly related to not closing the connections with fclose()? +[x] Fix bug where the socket stays open after quit. + [x] Possibly related to not closing the connections with fclose()? (see HttpServer.c, grep for fclose) [ ] Figure out how to write unit tests for array/hashmap/etc [ ] Add recipe to td script to upload patches to the Matrix room diff --git a/src/HttpServer.c b/src/HttpServer.c index 1b9cc58..0f390c6 100644 --- a/src/HttpServer.c +++ b/src/HttpServer.c @@ -41,6 +41,8 @@ #include #include +static const char ENABLE = 1; + struct HttpServer { int sd; @@ -357,6 +359,16 @@ HttpServerCreate(unsigned short port, unsigned int nThreads, unsigned int maxCon goto error; } + if (setsockopt(server->sd, SOL_SOCKET, SO_REUSEADDR, &ENABLE, sizeof(int)) < 0) + { + goto error; + } + + if (setsockopt(server->sd, SOL_SOCKET, SO_REUSEPORT, &ENABLE, sizeof(int)) < 0) + { + goto error; + } + sa.sin_family = AF_INET; sa.sin_port = htons(port); sa.sin_addr.s_addr = htonl(INADDR_ANY); @@ -598,9 +610,7 @@ bad_request: finish: Free(line); -#if 0 fclose(fp); -#endif } return NULL;