Skip to content

Commit

Permalink
http_client: clone name in http connection structures
Browse files Browse the repository at this point in the history
- use name to lookup private memory structure, hash id is not unique,
can be collisions for different names
  • Loading branch information
miconda committed Jun 29, 2021
1 parent 7741ad7 commit 9429083
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
23 changes: 15 additions & 8 deletions src/modules/http_client/curlcon.c
Expand Up @@ -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;
Expand Down Expand Up @@ -814,34 +815,40 @@ 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;
}

/* 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);
LM_ERR("no pkg memory available\n");
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);
Expand Down
5 changes: 3 additions & 2 deletions src/modules/http_client/http_client.h
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 9429083

Please sign in to comment.