libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 464d174103165453f149141d505ec8a09cc2fb17
parent 1e82d32256c98de9bda8b61444eb53c626f76561
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Mon, 31 Jan 2022 18:29:45 +0300

MHD_ip_limit_add(): do not use syscalls while holding the lock

Diffstat:
Msrc/microhttpd/daemon.c | 44+++++++++++++++++++++++---------------------
1 file changed, 23 insertions(+), 21 deletions(-)

diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c @@ -378,9 +378,9 @@ MHD_ip_limit_add (struct MHD_Daemon *daemon, const struct sockaddr *addr, socklen_t addrlen) { - struct MHD_IPCount *key; - void **nodep; - void *node; + struct MHD_IPCount *newkeyp; + struct MHD_IPCount *keyp; + struct MHD_IPCount **nodep; enum MHD_Result result; daemon = MHD_get_master (daemon); @@ -388,45 +388,47 @@ MHD_ip_limit_add (struct MHD_Daemon *daemon, if (0 == daemon->per_ip_connection_limit) return MHD_YES; - if (NULL == (key = malloc (sizeof(*key)))) + newkeyp = (struct MHD_IPCount *) malloc (sizeof(struct MHD_IPCount)); + if (NULL == newkeyp) return MHD_NO; /* Initialize key */ if (MHD_NO == MHD_ip_addr_to_key (addr, addrlen, - key)) + newkeyp)) { - /* Allow unhandled address types through */ - free (key); - return MHD_YES; + free (newkeyp); + return MHD_YES; /* Allow unhandled address types through */ } + MHD_ip_count_lock (daemon); /* Search for the IP address */ - if (NULL == (nodep = tsearch (key, - &daemon->per_ip_connection_count, - &MHD_ip_addr_compare))) + nodep = (struct MHD_IPCount **) tsearch (newkeyp, + &daemon->per_ip_connection_count, + &MHD_ip_addr_compare); + if (NULL == nodep) { + MHD_ip_count_unlock (daemon); + free (newkeyp); #ifdef HAVE_MESSAGES MHD_DLOG (daemon, _ ("Failed to add IP connection count node.\n")); #endif - MHD_ip_count_unlock (daemon); - free (key); return MHD_NO; } - node = *nodep; - /* If we got an existing node back, free the one we created */ - if (node != key) - free (key); - key = (struct MHD_IPCount *) node; + keyp = *nodep; /* Test if there is room for another connection; if so, * increment count */ - result = (key->count < daemon->per_ip_connection_limit) ? MHD_YES : MHD_NO; + result = (keyp->count < daemon->per_ip_connection_limit) ? MHD_YES : MHD_NO; if (MHD_NO != result) - ++key->count; - + ++keyp->count; MHD_ip_count_unlock (daemon); + + /* If we got an existing node back, free the one we created */ + if (keyp != newkeyp) + free (newkeyp); + return result; }