From 9429083c87b1323e31983cc71551676c199bf640 Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Tue, 29 Jun 2021 08:09:22 +0200 Subject: [PATCH] http_client: clone name in http connection structures - use name to lookup private memory structure, hash id is not unique, can be collisions for different names --- src/modules/http_client/curlcon.c | 23 +++++++++++++++-------- src/modules/http_client/http_client.h | 5 +++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/modules/http_client/curlcon.c b/src/modules/http_client/curlcon.c index 7df146493fb..b1bfa3bd484 100644 --- a/src/modules/http_client/curlcon.c +++ b/src/modules/http_client/curlcon.c @@ -172,7 +172,8 @@ curl_con_pkg_t *curl_get_pkg_connection(curl_con_t *con) ccp = _curl_con_pkg_root; while(ccp) { - if(ccp->conid == con->conid) { + if(ccp->conid == con->conid && ccp->name.len == con->name.len + && strncmp(ccp->name.s, con->name.s, con->name.len) == 0) { return ccp; } ccp = ccp->next; @@ -814,8 +815,9 @@ curl_con_t *curl_init_con(str *name) cc = cc->next; } - cc = (curl_con_t *)shm_malloc(sizeof( - curl_con_t)); /* Connection structures are shared by all children processes */ + /* Connection structures are shared by all children processes */ + cc = (curl_con_t *)shm_malloc(sizeof(curl_con_t) + + (name->len + 1)*sizeof(char)); if(cc == NULL) { LM_ERR("no shm memory\n"); return NULL; @@ -823,7 +825,8 @@ curl_con_t *curl_init_con(str *name) /* Each structure is allocated in package memory so each process can write into it without any locks or such stuff */ - ccp = (curl_con_pkg_t *)pkg_malloc(sizeof(curl_con_pkg_t)); + ccp = (curl_con_pkg_t *)pkg_malloc(sizeof(curl_con_pkg_t) + + (name->len + 1)*sizeof(char)); if(ccp == NULL) { /* We failed to allocate ccp, so let's free cc and quit */ shm_free(cc); @@ -831,17 +834,21 @@ curl_con_t *curl_init_con(str *name) return NULL; } - memset(cc, 0, sizeof(curl_con_t)); + memset(cc, 0, sizeof(curl_con_t) + (name->len + 1)*sizeof(char)); cc->next = _curl_con_root; cc->conid = conid; + cc->name.s = (char*)cc + sizeof(curl_con_t); + memcpy(cc->name.s, name->s, name->len); + cc->name.len = name->len; _curl_con_root = cc; - cc->name = *name; /* Put the new ccp first in line */ - memset(ccp, 0, sizeof(curl_con_pkg_t)); + memset(ccp, 0, sizeof(curl_con_pkg_t) + (name->len + 1)*sizeof(char)); ccp->next = _curl_con_pkg_root; ccp->conid = conid; - ccp->curl = NULL; + ccp->name.s = (char*)ccp + sizeof(curl_con_pkg_t); + memcpy(ccp->name.s, name->s, name->len); + ccp->name.len = name->len; _curl_con_pkg_root = ccp; LM_DBG("CURL: Added connection [%.*s]\n", name->len, name->s); diff --git a/src/modules/http_client/http_client.h b/src/modules/http_client/http_client.h index 29b451ba4d4..a37bdbe4a5b 100644 --- a/src/modules/http_client/http_client.h +++ b/src/modules/http_client/http_client.h @@ -93,7 +93,7 @@ enum connection_status typedef struct _curl_con { str name; /*!< Connection name */ - unsigned int conid; /*!< Connection ID */ + unsigned int conid; /*!< Connection hash ID */ enum connection_status connstate; /*!< Connection status */ str url; /*!< The URL without schema (host + base URL)*/ str schema; /*!< The URL schema */ @@ -125,7 +125,8 @@ typedef struct _curl_con /*! Per-process copy of connection object -stored in pkg memory */ typedef struct _curl_con_pkg { - unsigned int conid; /*!< Connection ID (referring to core connection id */ + str name; /*!< Connection name */ + unsigned int conid; /*!< Connection hash ID */ char redirecturl [512]; /*!< Last redirect URL - to use for $curlredirect(curlcon) pv */ unsigned int last_result; /*!< Last result of accessing this connection */