Browse code

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

(cherry picked from commit 9429083c87b1323e31983cc71551676c199bf640)

Daniel-Constantin Mierla authored on 29/06/2021 06:09:22
Showing 2 changed files
... ...
@@ -172,7 +172,8 @@ curl_con_pkg_t *curl_get_pkg_connection(curl_con_t *con)
172 172
 
173 173
 	ccp = _curl_con_pkg_root;
174 174
 	while(ccp) {
175
-		if(ccp->conid == con->conid) {
175
+		if(ccp->conid == con->conid && ccp->name.len == con->name.len
176
+				&& strncmp(ccp->name.s, con->name.s, con->name.len) == 0) {
176 177
 			return ccp;
177 178
 		}
178 179
 		ccp = ccp->next;
... ...
@@ -814,8 +815,9 @@ curl_con_t *curl_init_con(str *name)
814 815
 		cc = cc->next;
815 816
 	}
816 817
 
817
-	cc = (curl_con_t *)shm_malloc(sizeof(
818
-			curl_con_t)); /* Connection structures are shared by all children processes */
818
+	/* Connection structures are shared by all children processes */
819
+	cc = (curl_con_t *)shm_malloc(sizeof(curl_con_t)
820
+			+ (name->len + 1)*sizeof(char));
819 821
 	if(cc == NULL) {
820 822
 		LM_ERR("no shm memory\n");
821 823
 		return NULL;
... ...
@@ -823,7 +825,8 @@ curl_con_t *curl_init_con(str *name)
823 825
 
824 826
 	/* Each structure is allocated in package memory so each process can write into it without
825 827
 	   any locks or such stuff */
826
-	ccp = (curl_con_pkg_t *)pkg_malloc(sizeof(curl_con_pkg_t));
828
+	ccp = (curl_con_pkg_t *)pkg_malloc(sizeof(curl_con_pkg_t)
829
+			+ (name->len + 1)*sizeof(char));
827 830
 	if(ccp == NULL) {
828 831
 		/* We failed to allocate ccp, so let's free cc and quit */
829 832
 		shm_free(cc);
... ...
@@ -831,17 +834,21 @@ curl_con_t *curl_init_con(str *name)
831 834
 		return NULL;
832 835
 	}
833 836
 
834
-	memset(cc, 0, sizeof(curl_con_t));
837
+	memset(cc, 0, sizeof(curl_con_t) + (name->len + 1)*sizeof(char));
835 838
 	cc->next = _curl_con_root;
836 839
 	cc->conid = conid;
840
+	cc->name.s = (char*)cc + sizeof(curl_con_t);
841
+	memcpy(cc->name.s, name->s, name->len);
842
+	cc->name.len = name->len;
837 843
 	_curl_con_root = cc;
838
-	cc->name = *name;
839 844
 
840 845
 	/* Put the new ccp first in line */
841
-	memset(ccp, 0, sizeof(curl_con_pkg_t));
846
+	memset(ccp, 0, sizeof(curl_con_pkg_t) + (name->len + 1)*sizeof(char));
842 847
 	ccp->next = _curl_con_pkg_root;
843 848
 	ccp->conid = conid;
844
-	ccp->curl = NULL;
849
+	ccp->name.s = (char*)ccp + sizeof(curl_con_pkg_t);
850
+	memcpy(ccp->name.s, name->s, name->len);
851
+	ccp->name.len = name->len;
845 852
 	_curl_con_pkg_root = ccp;
846 853
 
847 854
 	LM_DBG("CURL: Added connection [%.*s]\n", name->len, name->s);
... ...
@@ -93,7 +93,7 @@ enum connection_status
93 93
 typedef struct _curl_con
94 94
 {
95 95
 	str name;						  /*!< Connection name */
96
-	unsigned int conid;				  /*!< Connection ID */
96
+	unsigned int conid;				  /*!< Connection hash ID */
97 97
 	enum connection_status connstate; /*!< Connection status */
98 98
 	str url;		/*!< The URL without schema (host + base URL)*/
99 99
 	str schema;		/*!< The URL schema */
... ...
@@ -125,7 +125,8 @@ typedef struct _curl_con
125 125
 /*! Per-process copy of connection object -stored in pkg memory */
126 126
 typedef struct _curl_con_pkg
127 127
 {
128
-	unsigned int conid; /*!< Connection ID (referring to core connection id */
128
+	str name; /*!< Connection name */
129
+	unsigned int conid; /*!< Connection hash ID */
129 130
 	char redirecturl
130 131
 			[512]; /*!< Last redirect URL - to use for $curlredirect(curlcon) pv */
131 132
 	unsigned int last_result; /*!< Last result of accessing this connection */