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)
(cherry picked from commit 459f40b52a8a6f8b2ef70a6986c686448ea572a8)
(cherry picked from commit 810ea55bcbd557f4544dd1a1ee21bcff558ae553)

Daniel-Constantin Mierla authored on 29/06/2021 06:09:22 • Henning Westerholt committed on 22/10/2021 12:02:59
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);
... ...
@@ -91,7 +91,7 @@ enum connection_status
91 91
 typedef struct _curl_con
92 92
 {
93 93
 	str name;						  /*!< Connection name */
94
-	unsigned int conid;				  /*!< Connection ID */
94
+	unsigned int conid;				  /*!< Connection hash ID */
95 95
 	enum connection_status connstate; /*!< Connection status */
96 96
 	str url;		/*!< The URL without schema (host + base URL)*/
97 97
 	str schema;		/*!< The URL schema */
... ...
@@ -123,7 +123,8 @@ typedef struct _curl_con
123 123
 /*! Per-process copy of connection object -stored in pkg memory */
124 124
 typedef struct _curl_con_pkg
125 125
 {
126
-	unsigned int conid; /*!< Connection ID (referring to core connection id */
126
+	str name; /*!< Connection name */
127
+	unsigned int conid; /*!< Connection hash ID */
127 128
 	char redirecturl
128 129
 			[512]; /*!< Last redirect URL - to use for $curlredirect(curlcon) pv */
129 130
 	unsigned int last_result; /*!< Last result of accessing this connection */