Browse code

core: added tcp_script_mode global parameter

- control if the tcp connection should be closed if received message
processing resulted in error return code

Daniel-Constantin Mierla authored on 14/02/2022 07:49:57
Showing 1 changed files
... ...
@@ -94,6 +94,10 @@ int is_msg_complete(struct tcp_req* r);
94 94
 
95 95
 int ksr_tcp_accept_hep3=0;
96 96
 int ksr_tcp_accept_haproxy=0;
97
+
98
+#define TCP_SCRIPT_MODE_CONTINUE (1<<0)
99
+int ksr_tcp_script_mode=0;
100
+
97 101
 /**
98 102
  * control cloning of TCP receive buffer
99 103
  * - needed for operations working directly inside the buffer
... ...
@@ -1342,6 +1346,7 @@ static int hep3_process_msg(char* tcpbuf, unsigned int len,
1342 1346
 int receive_tcp_msg(char* tcpbuf, unsigned int len,
1343 1347
 		struct receive_info* rcv_info, struct tcp_connection* con)
1344 1348
 {
1349
+	int ret = 0;
1345 1350
 #ifdef TCP_CLONE_RCVBUF
1346 1351
 	static char *buf = NULL;
1347 1352
 	static unsigned int bsize = 0;
... ...
@@ -1360,7 +1365,11 @@ int receive_tcp_msg(char* tcpbuf, unsigned int len,
1360 1365
 		if(unlikely(con->req.flags&F_TCP_REQ_HEP3))
1361 1366
 			return hep3_process_msg(tcpbuf, len, rcv_info, con);
1362 1367
 
1363
-		return receive_msg(tcpbuf, len, rcv_info);
1368
+		ret = receive_msg(tcpbuf, len, rcv_info);
1369
+		if (ksr_tcp_script_mode&TCP_SCRIPT_MODE_CONTINUE) {
1370
+			return 0;
1371
+		}
1372
+		return ret;
1364 1373
 	}
1365 1374
 
1366 1375
 	/* min buffer size is BUF_SIZE */
... ...
@@ -1401,7 +1410,11 @@ int receive_tcp_msg(char* tcpbuf, unsigned int len,
1401 1410
 #endif
1402 1411
 	if(unlikely(con->req.flags&F_TCP_REQ_HEP3))
1403 1412
 		return hep3_process_msg(tcpbuf, len, rcv_info, con);
1404
-	return receive_msg(buf, len, rcv_info);
1413
+	ret = receive_msg(buf, len, rcv_info);
1414
+	if (ksr_tcp_script_mode&TCP_SCRIPT_MODE_CONTINUE) {
1415
+		return 0;
1416
+	}
1417
+	return ret;
1405 1418
 #else /* TCP_CLONE_RCVBUF */
1406 1419
 #ifdef READ_MSRP
1407 1420
 	if(unlikely(con->req.flags&F_TCP_REQ_MSRP_FRAME))
... ...
@@ -1413,7 +1426,11 @@ int receive_tcp_msg(char* tcpbuf, unsigned int len,
1413 1426
 #endif
1414 1427
 	if(unlikely(con->req.flags&F_TCP_REQ_HEP3))
1415 1428
 		return hep3_process_msg(tcpbuf, len, rcv_info, con);
1416
-	return receive_msg(tcpbuf, len, rcv_info);
1429
+	ret = receive_msg(tcpbuf, len, rcv_info);
1430
+	if (ksr_tcp_script_mode&TCP_SCRIPT_MODE_CONTINUE) {
1431
+		return 0;
1432
+	}
1433
+	return ret;
1417 1434
 #endif /* TCP_CLONE_RCVBUF */
1418 1435
 }
1419 1436
 
Browse code

tcpclose event_routes issue

Viktor authored on 23/12/2021 22:18:04 • Daniel-Constantin Mierla committed on 07/01/2022 11:23:41
Showing 1 changed files
... ...
@@ -268,6 +268,11 @@ again:
268 268
 						ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
269 269
 				LOG(cfg_get(core, core_cfg, corelog),"-> [%s]:%u)\n",
270 270
 						ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
271
+				if (errno == ETIMEDOUT) {
272
+					c->event = TCP_CLOSED_TIMEOUT;
273
+				} else if (errno == ECONNRESET) {
274
+					c->event = TCP_CLOSED_RESET;
275
+				}
271 276
 				return -1;
272 277
 			}
273 278
 		}else if (unlikely((bytes_read==0) ||
... ...
@@ -279,6 +284,7 @@ again:
279 284
 					ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
280 285
 			c->state=S_CONN_EOF;
281 286
 			*flags|=RD_CONN_EOF;
287
+			c->event=TCP_CLOSED_EOF;
282 288
 		}else{
283 289
 			if (unlikely(c->state==S_CONN_CONNECT || c->state==S_CONN_ACCEPT)){
284 290
 				TCP_STATS_ESTABLISHED(c->state);
... ...
@@ -1670,6 +1676,7 @@ static ticks_t tcpconn_read_timeout(ticks_t t, struct timer_ln* tl, void* data)
1670 1676
 	}
1671 1677
 	if(tcp_conn_lst!=NULL) {
1672 1678
 		tcpconn_listrm(tcp_conn_lst, c, c_next, c_prev);
1679
+		c->event = TCP_CLOSED_TIMEOUT;
1673 1680
 		release_tcpconn(c, (c->state<0)?CONN_ERROR:CONN_RELEASE, tcpmain_sock);
1674 1681
 	}
1675 1682
 	return 0;
Browse code

tcpclose event_routes issue

Viktor authored on 20/12/2021 10:14:31 • Daniel-Constantin Mierla committed on 07/01/2022 11:23:41
Showing 1 changed files
... ...
@@ -174,26 +174,6 @@ int tcp_http11_continue(struct tcp_connection *c)
174 174
 }
175 175
 #endif /* HTTP11 */
176 176
 
177
-static int tcp_emit_closed_event(struct tcp_connection *con, enum tcp_closed_reason reason)
178
-{
179
-	int ret;
180
-	tcp_closed_event_info_t tev;
181
-	sr_event_param_t evp = {0};
182
-
183
-	ret = 0;
184
-	LM_DBG("TCP closed event creation triggered (reason: %d)\n", reason);
185
-	if(likely(sr_event_enabled(SREV_TCP_CLOSED))) {
186
-		memset(&tev, 0, sizeof(tcp_closed_event_info_t));
187
-		tev.reason = reason;
188
-		tev.con = con;
189
-		evp.data = (void*)(&tev);
190
-		ret = sr_event_exec(SREV_TCP_CLOSED, &evp);
191
-	} else {
192
-		LM_DBG("no callback registering for handling TCP closed event\n");
193
-	}
194
-	return ret;
195
-}
196
-
197 177
 
198 178
 /** reads data from an existing tcp connection.
199 179
  * Side-effects: blocklisting, sets connection state to S_CONN_OK, tcp stats.
... ...
@@ -288,11 +268,6 @@ again:
288 268
 						ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
289 269
 				LOG(cfg_get(core, core_cfg, corelog),"-> [%s]:%u)\n",
290 270
 						ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
291
-				if (errno == ETIMEDOUT) {
292
-					tcp_emit_closed_event(c, TCP_CLOSED_TIMEOUT);
293
-				} else if (errno == ECONNRESET) {
294
-					tcp_emit_closed_event(c, TCP_CLOSED_RESET);
295
-				}
296 271
 				return -1;
297 272
 			}
298 273
 		}else if (unlikely((bytes_read==0) ||
... ...
@@ -304,7 +279,6 @@ again:
304 279
 					ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
305 280
 			c->state=S_CONN_EOF;
306 281
 			*flags|=RD_CONN_EOF;
307
-			tcp_emit_closed_event(c, TCP_CLOSED_EOF);
308 282
 		}else{
309 283
 			if (unlikely(c->state==S_CONN_CONNECT || c->state==S_CONN_ACCEPT)){
310 284
 				TCP_STATS_ESTABLISHED(c->state);
Browse code

core: more use of ksr_exit() macro to follow --atexit param

Daniel-Constantin Mierla authored on 09/02/2021 09:50:03
Showing 1 changed files
... ...
@@ -52,6 +52,7 @@
52 52
 #include "ut.h"
53 53
 #include "trim.h"
54 54
 #include "pt.h"
55
+#include "daemonize.h"
55 56
 #include "cfg/cfg_struct.h"
56 57
 #ifdef CORE_TLS
57 58
 #include "tls/tls_server.h"
... ...
@@ -2009,7 +2010,7 @@ void tcp_receive_loop(int unix_sock)
2009 2010
 error:
2010 2011
 	destroy_io_wait(&io_w);
2011 2012
 	LM_CRIT("exiting...");
2012
-	exit(-1);
2013
+	ksr_exit(-1);
2013 2014
 }
2014 2015
 
2015 2016
 
Browse code

core: tcp - log connection state and flags on debug message

Daniel-Constantin Mierla authored on 08/12/2020 12:07:44
Showing 1 changed files
... ...
@@ -296,9 +296,11 @@ again:
296 296
 			}
297 297
 		}else if (unlikely((bytes_read==0) ||
298 298
 					(*flags & RD_CONN_FORCE_EOF))){
299
-			LM_DBG("EOF on %p, FD %d, bytes %d, flags %x ([%s]:%u -> [%s]:%u)",
300
-					c, fd, bytes_read, *flags, ip_addr2a(&c->rcv.src_ip),
301
-					c->rcv.src_port, ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
299
+			LM_DBG("EOF on connection %p (state: %u, flags: %x) - FD %d,"
300
+					" bytes %d, rd-flags %x ([%s]:%u -> [%s]:%u)",
301
+					c, c->state, c->flags, fd, bytes_read, *flags,
302
+					ip_addr2a(&c->rcv.src_ip), c->rcv.src_port,
303
+					ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
302 304
 			c->state=S_CONN_EOF;
303 305
 			*flags|=RD_CONN_EOF;
304 306
 			tcp_emit_closed_event(c, TCP_CLOSED_EOF);
Browse code

core: tcp - debug messages merged, print bytes read and flags

Daniel-Constantin Mierla authored on 08/12/2020 11:50:56
Showing 1 changed files
... ...
@@ -296,12 +296,12 @@ again:
296 296
 			}
297 297
 		}else if (unlikely((bytes_read==0) ||
298 298
 					(*flags & RD_CONN_FORCE_EOF))){
299
+			LM_DBG("EOF on %p, FD %d, bytes %d, flags %x ([%s]:%u -> [%s]:%u)",
300
+					c, fd, bytes_read, *flags, ip_addr2a(&c->rcv.src_ip),
301
+					c->rcv.src_port, ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
299 302
 			c->state=S_CONN_EOF;
300 303
 			*flags|=RD_CONN_EOF;
301 304
 			tcp_emit_closed_event(c, TCP_CLOSED_EOF);
302
-			LM_DBG("EOF on %p, FD %d ([%s]:%u ->", c, fd,
303
-					ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
304
-			LM_DBG("-> [%s]:%u)\n", ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
305 305
 		}else{
306 306
 			if (unlikely(c->state==S_CONN_CONNECT || c->state==S_CONN_ACCEPT)){
307 307
 				TCP_STATS_ESTABLISHED(c->state);
Browse code

core: check if the tcp clone rcv buffer is set when applying changes

Daniel-Constantin Mierla authored on 26/11/2020 12:54:42
Showing 1 changed files
... ...
@@ -109,6 +109,11 @@ int tcp_set_clone_rcvbuf(int v)
109 109
 	return r;
110 110
 }
111 111
 
112
+int tcp_get_clone_rcvbuf(void)
113
+{
114
+	return tcp_clone_rcvbuf;
115
+}
116
+
112 117
 #ifdef READ_HTTP11
113 118
 int tcp_http11_continue(struct tcp_connection *c)
114 119
 {
Browse code

core: switch to use blocklist structures and functions

Daniel-Constantin Mierla authored on 30/10/2020 09:25:09
Showing 1 changed files
... ...
@@ -58,9 +58,9 @@
58 58
 #else
59 59
 #include "tls_hooks.h"
60 60
 #endif /* CORE_TLS */
61
-#ifdef USE_DST_BLACKLIST
62
-#include "dst_blacklist.h"
63
-#endif /* USE_DST_BLACKLIST */
61
+#ifdef USE_DST_BLOCKLIST
62
+#include "dst_blocklist.h"
63
+#endif /* USE_DST_BLOCKLIST */
64 64
 
65 65
 #define HANDLE_IO_INLINE
66 66
 #include "io_wait.h"
... ...
@@ -190,10 +190,10 @@ static int tcp_emit_closed_event(struct tcp_connection *con, enum tcp_closed_rea
190 190
 
191 191
 
192 192
 /** reads data from an existing tcp connection.
193
- * Side-effects: blacklisting, sets connection state to S_CONN_OK, tcp stats.
193
+ * Side-effects: blocklisting, sets connection state to S_CONN_OK, tcp stats.
194 194
  * @param fd - connection file descriptor
195 195
  * @param c - tcp connection structure. c->state might be changed and
196
- *             receive info might be used for blacklisting.
196
+ *             receive info might be used for blocklisting.
197 197
  * @param buf - buffer where the received data will be stored.
198 198
  * @param b_size - buffer size.
199 199
  * @param flags - value/result - used to signal a seen or "forced" EOF on the
... ...
@@ -235,20 +235,20 @@ again:
235 235
 				if (unlikely(c->state==S_CONN_CONNECT)){
236 236
 					switch(errno){
237 237
 						case ECONNRESET:
238
-#ifdef USE_DST_BLACKLIST
239
-							dst_blacklist_su(BLST_ERR_CONNECT, c->rcv.proto,
238
+#ifdef USE_DST_BLOCKLIST
239
+							dst_blocklist_su(BLST_ERR_CONNECT, c->rcv.proto,
240 240
 												&c->rcv.src_su,
241 241
 												&c->send_flags, 0);
242
-#endif /* USE_DST_BLACKLIST */
242
+#endif /* USE_DST_BLOCKLIST */
243 243
 							TCP_EV_CONNECT_RST(errno, TCP_LADDR(c),
244 244
 									TCP_LPORT(c), TCP_PSU(c), TCP_PROTO(c));
245 245
 							break;
246 246
 						case ETIMEDOUT:
247
-#ifdef USE_DST_BLACKLIST
248
-							dst_blacklist_su(BLST_ERR_CONNECT, c->rcv.proto,
247
+#ifdef USE_DST_BLOCKLIST
248
+							dst_blocklist_su(BLST_ERR_CONNECT, c->rcv.proto,
249 249
 												&c->rcv.src_su,
250 250
 												&c->send_flags, 0);
251
-#endif /* USE_DST_BLACKLIST */
251
+#endif /* USE_DST_BLOCKLIST */
252 252
 							TCP_EV_CONNECT_TIMEOUT(errno, TCP_LADDR(c),
253 253
 									TCP_LPORT(c), TCP_PSU(c), TCP_PROTO(c));
254 254
 							break;
... ...
@@ -261,18 +261,18 @@ again:
261 261
 						switch(errno){
262 262
 							case ECONNRESET:
263 263
 								TCP_STATS_CON_RESET();
264
-#ifdef USE_DST_BLACKLIST
265
-								dst_blacklist_su(BLST_ERR_SEND, c->rcv.proto,
264
+#ifdef USE_DST_BLOCKLIST
265
+								dst_blocklist_su(BLST_ERR_SEND, c->rcv.proto,
266 266
 													&c->rcv.src_su,
267 267
 													&c->send_flags, 0);
268
-#endif /* USE_DST_BLACKLIST */
268
+#endif /* USE_DST_BLOCKLIST */
269 269
 								break;
270 270
 							case ETIMEDOUT:
271
-#ifdef USE_DST_BLACKLIST
272
-								dst_blacklist_su(BLST_ERR_SEND, c->rcv.proto,
271
+#ifdef USE_DST_BLOCKLIST
272
+								dst_blocklist_su(BLST_ERR_SEND, c->rcv.proto,
273 273
 													&c->rcv.src_su,
274 274
 													&c->send_flags, 0);
275
-#endif /* USE_DST_BLACKLIST */
275
+#endif /* USE_DST_BLOCKLIST */
276 276
 								break;
277 277
 						}
278 278
 				}
Browse code

core/tcp: Convert RD_CONN_ flags to enum

Easier to recognize the correct flags in the forest of flags.

Alex Hermann authored on 09/06/2020 07:01:53 • Henning Westerholt committed on 20/10/2020 10:17:10
Showing 1 changed files
... ...
@@ -218,7 +218,7 @@ static int tcp_emit_closed_event(struct tcp_connection *con, enum tcp_closed_rea
218 218
  * EOF checking should be done by checking the RD_CONN_EOF flag.
219 219
  */
220 220
 int tcp_read_data(int fd, struct tcp_connection *c,
221
-					char* buf, int b_size, int* flags)
221
+					char* buf, int b_size, rd_conn_flags_t* flags)
222 222
 {
223 223
 	int bytes_read;
224 224
 
... ...
@@ -329,7 +329,7 @@ again:
329 329
  * (to distinguish from reads that would block which could return 0)
330 330
  * RD_CONN_SHORT_READ is also set in *flags for short reads.
331 331
  * sets also r->error */
332
-int tcp_read(struct tcp_connection *c, int* flags)
332
+int tcp_read(struct tcp_connection *c, rd_conn_flags_t* flags)
333 333
 {
334 334
 	int bytes_free, bytes_read;
335 335
 	struct tcp_req *r;
... ...
@@ -369,7 +369,7 @@ int tcp_read(struct tcp_connection *c, int* flags)
369 369
  * when either r->body!=0 or r->state==H_BODY =>
370 370
  * all headers have been read. It should be called in a while loop.
371 371
  * returns < 0 if error or 0 if EOF */
372
-int tcp_read_headers(struct tcp_connection *c, int* read_flags)
372
+int tcp_read_headers(struct tcp_connection *c, rd_conn_flags_t* read_flags)
373 373
 {
374 374
 	int bytes, remaining;
375 375
 	char *p;
... ...
@@ -1082,7 +1082,7 @@ int msrp_process_msg(char* tcpbuf, unsigned int len,
1082 1082
 #endif
1083 1083
 
1084 1084
 #ifdef READ_WS
1085
-static int tcp_read_ws(struct tcp_connection *c, int* read_flags)
1085
+static int tcp_read_ws(struct tcp_connection *c, rd_conn_flags_t* read_flags)
1086 1086
 {
1087 1087
 	int bytes;
1088 1088
 	uint32_t size, pos, mask_present, len;
... ...
@@ -1237,7 +1237,7 @@ static int ws_process_msg(char* tcpbuf, unsigned int len,
1237 1237
 }
1238 1238
 #endif
1239 1239
 
1240
-static int tcp_read_hep3(struct tcp_connection *c, int* read_flags)
1240
+static int tcp_read_hep3(struct tcp_connection *c, rd_conn_flags_t* read_flags)
1241 1241
 {
1242 1242
 	int bytes;
1243 1243
 	uint32_t size, len;
... ...
@@ -1429,7 +1429,7 @@ int receive_tcp_msg(char* tcpbuf, unsigned int len,
1429 1429
 #endif /* TCP_CLONE_RCVBUF */
1430 1430
 }
1431 1431
 
1432
-int tcp_read_req(struct tcp_connection* con, int* bytes_read, int* read_flags)
1432
+int tcp_read_req(struct tcp_connection* con, int* bytes_read, rd_conn_flags_t* read_flags)
1433 1433
 {
1434 1434
 	int bytes;
1435 1435
 	int total_bytes;
... ...
@@ -1712,7 +1712,7 @@ inline static int handle_io(struct fd_map* fm, short events, int idx)
1712 1712
 {
1713 1713
 	int ret;
1714 1714
 	int n;
1715
-	int read_flags;
1715
+	rd_conn_flags_t read_flags;
1716 1716
 	struct tcp_connection* con;
1717 1717
 	int s;
1718 1718
 	long resp;
Browse code

core: helper functions to search variants of str in another str

Daniel-Constantin Mierla authored on 03/09/2020 10:47:50
Showing 1 changed files
... ...
@@ -110,26 +110,6 @@ int tcp_set_clone_rcvbuf(int v)
110 110
 }
111 111
 
112 112
 #ifdef READ_HTTP11
113
-static inline char *strfindcasestrz(str *haystack, char *needlez)
114
-{
115
-	int i,j;
116
-	str needle;
117
-
118
-	needle.s = needlez;
119
-	needle.len = strlen(needlez);
120
-	for(i=0;i<haystack->len-needle.len;i++) {
121
-		for(j=0;j<needle.len;j++) {
122
-			if ( !((haystack->s[i+j]==needle.s[j]) ||
123
-					( isalpha((int)haystack->s[i+j])
124
-						&& ((haystack->s[i+j])^(needle.s[j]))==0x20 )) )
125
-				break;
126
-		}
127
-		if (j==needle.len)
128
-			return haystack->s+i;
129
-	}
130
-	return 0;
131
-}
132
-
133 113
 int tcp_http11_continue(struct tcp_connection *c)
134 114
 {
135 115
 	struct dest_info dst;
... ...
@@ -161,7 +141,7 @@ int tcp_http11_continue(struct tcp_connection *c)
161 141
 		return 0;
162 142
 
163 143
 	/* check for Expect header */
164
-	if(strfindcasestrz(&msg, "Expect: 100-continue")!=NULL)
144
+	if(str_casesearch_strz(&msg, "Expect: 100-continue")!=NULL)
165 145
 	{
166 146
 		init_dst_from_rcv(&dst, &c->rcv);
167 147
 		if (tcp_send(&dst, 0, HTTP11CONTINUE, HTTP11CONTINUE_LEN) < 0) {
... ...
@@ -169,7 +149,7 @@ int tcp_http11_continue(struct tcp_connection *c)
169 149
 		}
170 150
 	}
171 151
 	/* check for Transfer-Encoding header */
172
-	if(strfindcasestrz(&msg, "Transfer-Encoding: chunked")!=NULL)
152
+	if(str_casesearch_strz(&msg, "Transfer-Encoding: chunked")!=NULL)
173 153
 	{
174 154
 		c->req.flags |= F_TCP_REQ_BCHUNKED;
175 155
 		ret = 1;
... ...
@@ -178,7 +158,7 @@ int tcp_http11_continue(struct tcp_connection *c)
178 158
 	 * - HTTP Via format is different that SIP Via
179 159
 	 * - workaround: replace with Hia to be ignored by SIP parser
180 160
 	 */
181
-	if((p=strfindcasestrz(&msg, "\nVia:"))!=NULL)
161
+	if((p=str_casesearch_strz(&msg, "\nVia:"))!=NULL)
182 162
 	{
183 163
 		p++;
184 164
 		*p = 'H';
Browse code

core: tcp - debug message when removing connection from list

Daniel-Constantin Mierla authored on 08/07/2020 12:44:34
Showing 1 changed files
... ...
@@ -1881,6 +1881,13 @@ read_error:
1881 1881
 							ip_addr2a(&con->rcv.dst_ip), con->rcv.dst_port);
1882 1882
 				}
1883 1883
 				if(tcp_conn_lst!=NULL) {
1884
+					LM_DBG("removing from list %p id %d fd %d,"
1885
+							" state %d, flags %x, main fd %d, refcnt %d"
1886
+							" ([%s]:%u -> [%s]:%u)\n",
1887
+							con, con->id, con->fd, con->state,
1888
+							con->flags, con->s, atomic_get(&con->refcnt),
1889
+							ip_addr2a(&con->rcv.src_ip), con->rcv.src_port,
1890
+							ip_addr2a(&con->rcv.dst_ip), con->rcv.dst_port);
1884 1891
 					tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev);
1885 1892
 					local_timer_del(&tcp_reader_ltimer, &con->timer);
1886 1893
 					if (unlikely(resp!=CONN_EOF))
Browse code

core: tcp - safety checks on emptly local tcp connection list

Daniel-Constantin Mierla authored on 03/07/2020 12:02:20
Showing 1 changed files
... ...
@@ -1689,10 +1689,10 @@ void release_tcpconn(struct tcp_connection* c, long state, int unix_sock)
1689 1689
 static ticks_t tcpconn_read_timeout(ticks_t t, struct timer_ln* tl, void* data)
1690 1690
 {
1691 1691
 	struct tcp_connection *c;
1692
-	
1693
-	c=(struct tcp_connection*)data; 
1692
+
1693
+	c=(struct tcp_connection*)data;
1694 1694
 	/* or (struct tcp...*)(tl-offset(c->timer)) */
1695
-	
1695
+
1696 1696
 	if (likely(!(c->state<0) && TICKS_LT(t, c->timeout))){
1697 1697
 		/* timeout extended, exit */
1698 1698
 		return (ticks_t)(c->timeout - t);
... ...
@@ -1706,9 +1706,10 @@ static ticks_t tcpconn_read_timeout(ticks_t t, struct timer_ln* tl, void* data)
1706 1706
 					ip_addr2a(&c->rcv.src_ip), c->rcv.src_port,
1707 1707
 					ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
1708 1708
 	}
1709
-	tcpconn_listrm(tcp_conn_lst, c, c_next, c_prev);
1710
-	release_tcpconn(c, (c->state<0)?CONN_ERROR:CONN_RELEASE, tcpmain_sock);
1711
-	
1709
+	if(tcp_conn_lst!=NULL) {
1710
+		tcpconn_listrm(tcp_conn_lst, c, c_next, c_prev);
1711
+		release_tcpconn(c, (c->state<0)?CONN_ERROR:CONN_RELEASE, tcpmain_sock);
1712
+	}
1712 1713
 	return 0;
1713 1714
 }
1714 1715
 
... ...
@@ -1836,8 +1837,10 @@ repeat_1st_read:
1836 1837
 							ip_addr2a(&ec->rcv.dst_ip), ec->rcv.dst_port);
1837 1838
 
1838 1839
 				}
1839
-				tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev);
1840
-				local_timer_del(&tcp_reader_ltimer, &con->timer);
1840
+				if(tcp_conn_lst!=NULL) {
1841
+					tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev);
1842
+					local_timer_del(&tcp_reader_ltimer, &con->timer);
1843
+				}
1841 1844
 				goto con_error;
1842 1845
 			}
1843 1846
 			break;
... ...
@@ -1877,11 +1880,13 @@ read_error:
1877 1880
 							ip_addr2a(&con->rcv.src_ip), con->rcv.src_port,
1878 1881
 							ip_addr2a(&con->rcv.dst_ip), con->rcv.dst_port);
1879 1882
 				}
1880
-				tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev);
1881
-				local_timer_del(&tcp_reader_ltimer, &con->timer);
1882
-				if (unlikely(resp!=CONN_EOF))
1883
-					con->state=S_CONN_BAD;
1884
-				release_tcpconn(con, resp, tcpmain_sock);
1883
+				if(tcp_conn_lst!=NULL) {
1884
+					tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev);
1885
+					local_timer_del(&tcp_reader_ltimer, &con->timer);
1886
+					if (unlikely(resp!=CONN_EOF))
1887
+						con->state=S_CONN_BAD;
1888
+					release_tcpconn(con, resp, tcpmain_sock);
1889
+				}
1885 1890
 			}else{
1886 1891
 #ifdef USE_TLS
1887 1892
 				if (unlikely(read_flags & RD_CONN_REPEAT_READ))
... ...
@@ -1889,8 +1894,8 @@ read_error:
1889 1894
 #endif /* USE_TLS */
1890 1895
 				/* update timeout */
1891 1896
 				con->timeout=get_ticks_raw()+S_TO_TICKS(TCP_CHILD_TIMEOUT);
1892
-				/* ret= 0 (read the whole socket buffer) if short read & 
1893
-				 *  !POLLPRI,  bytes read otherwise */
1897
+				/* ret= 0 (read the whole socket buffer) if short read
1898
+				 * & !POLLPRI,  bytes read otherwise */
1894 1899
 				ret&=(((read_flags & RD_CONN_SHORT_READ) &&
1895 1900
 						!(events & POLLPRI)) - 1);
1896 1901
 			}
... ...
@@ -1904,7 +1909,7 @@ read_error:
1904 1909
 			LM_CRIT("unknown fd type %d\n", fm->type);
1905 1910
 			goto error;
1906 1911
 	}
1907
-	
1912
+
1908 1913
 	return ret;
1909 1914
 con_error:
1910 1915
 	con->state=S_CONN_BAD;
Browse code

core: tcp - more details on error log messages

Daniel-Constantin Mierla authored on 03/07/2020 08:49:43
Showing 1 changed files
... ...
@@ -1846,8 +1846,11 @@ repeat_1st_read:
1846 1846
 			if (unlikely(con->state==S_CONN_BAD)){
1847 1847
 				resp=CONN_ERROR;
1848 1848
 				if (!(con->send_flags.f & SND_F_CON_CLOSE))
1849
-					LM_WARN("F_TCPCONN connection marked as bad: %p id %d refcnt %d\n",
1850
-							con, con->id, atomic_get(&con->refcnt));
1849
+					LM_WARN("F_TCPCONN connection marked as bad: %p id %d fd %d"
1850
+							" refcnt %d ([%s]:%u -> [%s]:%u)\n",
1851
+							con, con->id, con->fd, atomic_get(&con->refcnt),
1852
+							ip_addr2a(&con->rcv.src_ip), con->rcv.src_port,
1853
+							ip_addr2a(&con->rcv.dst_ip), con->rcv.dst_port);
1851 1854
 				goto read_error;
1852 1855
 			}
1853 1856
 			read_flags=((
Browse code

core: tcp - log details of the exiting tcp connection on insert failure

Daniel-Constantin Mierla authored on 03/06/2020 10:45:05
Showing 1 changed files
... ...
@@ -1720,7 +1720,7 @@ static ticks_t tcpconn_read_timeout(ticks_t t, struct timer_ln* tl, void* data)
1720 1720
  *          idx - index in the fd_array (or -1 if not known)
1721 1721
  * return: -1 on error, or when we are not interested any more on reads
1722 1722
  *            from this fd (e.g.: we are closing it )
1723
- *          0 on EAGAIN or when by some other way it is known that no more 
1723
+ *          0 on EAGAIN or when by some other way it is known that no more
1724 1724
  *            io events are queued on the fd (the receive buffer is empty).
1725 1725
  *            Usefull to detect when there are no more io events queued for
1726 1726
  *            sigio_rt, epoll_et, kqueue.
... ...
@@ -1728,7 +1728,7 @@ static ticks_t tcpconn_read_timeout(ticks_t t, struct timer_ln* tl, void* data)
1728 1728
  *            queued -- the receive buffer might still be non-empty)
1729 1729
  */
1730 1730
 inline static int handle_io(struct fd_map* fm, short events, int idx)
1731
-{	
1731
+{
1732 1732
 	int ret;
1733 1733
 	int n;
1734 1734
 	int read_flags;
... ...
@@ -1736,10 +1736,11 @@ inline static int handle_io(struct fd_map* fm, short events, int idx)
1736 1736
 	int s;
1737 1737
 	long resp;
1738 1738
 	ticks_t t;
1739
-	
1739
+	fd_map_t *ee = NULL;
1740
+
1740 1741
 	/* update the local config */
1741 1742
 	cfg_update();
1742
-	
1743
+
1743 1744
 	switch(fm->type){
1744 1745
 		case F_TCPMAIN:
1745 1746
 again:
... ...
@@ -1803,7 +1804,7 @@ repeat_1st_read:
1803 1804
 			if (unlikely(read_flags & RD_CONN_REPEAT_READ))
1804 1805
 				goto repeat_1st_read;
1805 1806
 #endif /* USE_TLS */
1806
-			
1807
+
1807 1808
 			/* must be before io_watch_add, io_watch_add might catch some
1808 1809
 			 * already existing events => might call handle_io and
1809 1810
 			 * handle_io might decide to del. the new connection =>
... ...
@@ -1816,13 +1817,25 @@ repeat_1st_read:
1816 1817
 			local_timer_reinit(&con->timer);
1817 1818
 			local_timer_add(&tcp_reader_ltimer, &con->timer,
1818 1819
 								S_TO_TICKS(TCP_CHILD_TIMEOUT), t);
1819
-			if (unlikely(io_watch_add(&io_w, s, POLLIN, F_TCPCONN, con)<0)){
1820
+			if (unlikely(io_watch_add(&io_w, s, POLLIN, F_TCPCONN, con)<0)) {
1820 1821
 				LM_CRIT("io_watch_add failed for %p id %d fd %d, state %d, flags %x,"
1821 1822
 							" main fd %d, refcnt %d ([%s]:%u -> [%s]:%u)\n",
1822 1823
 							con, con->id, con->fd, con->state, con->flags,
1823 1824
 							con->s, atomic_get(&con->refcnt),
1824 1825
 							ip_addr2a(&con->rcv.src_ip), con->rcv.src_port,
1825 1826
 							ip_addr2a(&con->rcv.dst_ip), con->rcv.dst_port);
1827
+				ee = get_fd_map(&io_w, s);
1828
+				if(ee!=0 && ee->type==F_TCPCONN) {
1829
+					tcp_connection_t *ec;
1830
+					ec = (tcp_connection_t*)ee->data;
1831
+					LM_CRIT("existing tcp con %p id %d fd %d, state %d, flags %x,"
1832
+							" main fd %d, refcnt %d ([%s]:%u -> [%s]:%u)\n",
1833
+							ec, ec->id, ec->fd, ec->state, ec->flags,
1834
+							ec->s, atomic_get(&ec->refcnt),
1835
+							ip_addr2a(&ec->rcv.src_ip), ec->rcv.src_port,
1836
+							ip_addr2a(&ec->rcv.dst_ip), ec->rcv.dst_port);
1837
+
1838
+				}
1826 1839
 				tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev);
1827 1840
 				local_timer_del(&tcp_reader_ltimer, &con->timer);
1828 1841
 				goto con_error;
Browse code

core: removed obsolete DYN_BUF define condition

Daniel-Constantin Mierla authored on 21/05/2020 08:03:57
Showing 1 changed files
... ...
@@ -1375,12 +1375,8 @@ int receive_tcp_msg(char* tcpbuf, unsigned int len,
1375 1375
 		struct receive_info* rcv_info, struct tcp_connection* con)
1376 1376
 {
1377 1377
 #ifdef TCP_CLONE_RCVBUF
1378
-#ifdef DYN_BUF
1379
-	char *buf = NULL;
1380
-#else
1381 1378
 	static char *buf = NULL;
1382 1379
 	static unsigned int bsize = 0;
1383
-#endif
1384 1380
 	int blen;
1385 1381
 
1386 1382
 	/* cloning is disabled via parameter */
... ...
@@ -1404,13 +1400,6 @@ int receive_tcp_msg(char* tcpbuf, unsigned int len,
1404 1400
 	if(blen < BUF_SIZE)
1405 1401
 		blen = BUF_SIZE;
1406 1402
 
1407
-#ifdef DYN_BUF
1408
-	buf=pkg_malloc(blen+1);
1409
-	if (buf==0) {
1410
-		PKG_MEM_ERROR;
1411
-		return -1;
1412
-	}
1413
-#else
1414 1403
 	/* allocate buffer when needed
1415 1404
 	 * - no buffer yet
1416 1405
 	 * - existing buffer too small (min size is BUF_SIZE - to accomodate most
... ...
@@ -1431,7 +1420,6 @@ int receive_tcp_msg(char* tcpbuf, unsigned int len,
1431 1420
 		}
1432 1421
 		bsize = blen;
1433 1422
 	}
1434
-#endif
1435 1423
 
1436 1424
 	memcpy(buf, tcpbuf, len);
1437 1425
 	buf[len] = '\0';
Browse code

core: use generic PKG_MEM_ERROR, SHM_ERROR and _CRITICAL helper defines in core

- refactoring of the core to use generic PKG_MEM_ERROR, SHM_ERROR,
PKG_MEM_CRITICAL, SHM_MEM_CRITICAL and SYS_MEM_ERROR helper defines
- unify many different error messages in different spellings
- add a few missing error handler for allocation errors after (found
with a complete review of all memory allocation functions in core)
- no other functional changes, change has been reviewed two times

Henning Westerholt authored on 23/12/2018 21:31:03
Showing 1 changed files
... ...
@@ -1407,7 +1407,7 @@ int receive_tcp_msg(char* tcpbuf, unsigned int len,
1407 1407
 #ifdef DYN_BUF
1408 1408
 	buf=pkg_malloc(blen+1);
1409 1409
 	if (buf==0) {
1410
-		LM_ERR("could not allocate receive buffer\n");
1410
+		PKG_MEM_ERROR;
1411 1411
 		return -1;
1412 1412
 	}
1413 1413
 #else
... ...
@@ -1426,7 +1426,7 @@ int receive_tcp_msg(char* tcpbuf, unsigned int len,
1426 1426
 			free(buf);
1427 1427
 		buf=malloc(blen+1);
1428 1428
 		if (buf==0) {
1429
-			LM_ERR("could not allocate receive buffer\n");
1429
+			SYS_MEM_ERROR;
1430 1430
 			return -1;
1431 1431
 		}
1432 1432
 		bsize = blen;
Browse code

core: Add PROXY protocol implementation (#1765)

- introduce new global variable `ksr_tcp_accept_haproxy`.
- this variable can be modified by using the `tcp_accept_haproxy=yes` core
configuration parameter.
- when active, inbound TCP connections are expected to behave according
to the PROXY protocol[1].
- Both the v1 (human-readable) and v2 (binary) versions of the protocol
are supported.
- [1]: https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt

Sebastian Lauwers authored on 19/12/2018 08:05:14 • Henning Westerholt committed on 19/12/2018 08:05:14
Showing 1 changed files
... ...
@@ -39,7 +39,6 @@
39 39
 #include <unistd.h>
40 40
 #include <stdlib.h> /* for abort() */
41 41
 
42
-
43 42
 #include "dprint.h"
44 43
 #include "tcp_conn.h"
45 44
 #include "tcp_read.h"
... ...
@@ -93,6 +92,7 @@ static ticks_t tcp_reader_prev_ticks;
93 92
 int is_msg_complete(struct tcp_req* r);
94 93
 
95 94
 int ksr_tcp_accept_hep3=0;
95
+int ksr_tcp_accept_haproxy=0;
96 96
 /**
97 97
  * control cloning of TCP receive buffer
98 98
  * - needed for operations working directly inside the buffer
Browse code

core: small spelling fix (bellow -> below)

Henning Westerholt authored on 05/10/2018 19:38:27
Showing 1 changed files
... ...
@@ -1333,7 +1333,7 @@ static int hep3_process_msg(char* tcpbuf, unsigned int len,
1333 1333
 	/* fill in msg */
1334 1334
 	msg.buf=tcpbuf;
1335 1335
 	msg.len=len;
1336
-	/* zero termination (termination of orig message bellow not that
1336
+	/* zero termination (termination of orig message below not that
1337 1337
 	 * useful as most of the work is done with scratch-pad; -jiri  */
1338 1338
 	/* buf[len]=0; */ /* WARNING: zero term removed! */
1339 1339
 	msg.rcv=*rcv_info;
Browse code

core: tcp - more safety check to avoid jumping out if receive buf for ws proto

Daniel-Constantin Mierla authored on 07/09/2018 06:22:53
Showing 1 changed files
... ...
@@ -241,16 +241,17 @@ int tcp_read_data(int fd, struct tcp_connection *c,
241 241
 					char* buf, int b_size, int* flags)
242 242
 {
243 243
 	int bytes_read;
244
-	
244
+
245 245
 again:
246 246
 	bytes_read=read(fd, buf, b_size);
247
-	
247
+
248 248
 	if (likely(bytes_read!=b_size)){
249 249
 		if(unlikely(bytes_read==-1)){
250 250
 			if (errno == EWOULDBLOCK || errno == EAGAIN){
251 251
 				bytes_read=0; /* nothing has been read */
252
-			}else if (errno == EINTR) goto again;
253
-			else{
252
+			}else if (errno == EINTR){
253
+				goto again;
254
+			}else{
254 255
 				if (unlikely(c->state==S_CONN_CONNECT)){
255 256
 					switch(errno){
256 257
 						case ECONNRESET:
... ...
@@ -299,7 +300,8 @@ again:
299 300
 						"error reading: %s (%d) ([%s]:%u ->",
300 301
 						strerror(errno), errno,
301 302
 						ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
302
-				LOG(cfg_get(core, core_cfg, corelog),"-> [%s]:%u)\n", ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
303
+				LOG(cfg_get(core, core_cfg, corelog),"-> [%s]:%u)\n",
304
+						ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
303 305
 				if (errno == ETIMEDOUT) {
304 306
 					tcp_emit_closed_event(c, TCP_CLOSED_TIMEOUT);
305 307
 				} else if (errno == ECONNRESET) {
... ...
@@ -307,12 +309,13 @@ again:
307 309
 				}
308 310
 				return -1;
309 311
 			}
310
-		}else if (unlikely((bytes_read==0) || 
312
+		}else if (unlikely((bytes_read==0) ||
311 313
 					(*flags & RD_CONN_FORCE_EOF))){
312 314
 			c->state=S_CONN_EOF;
313 315
 			*flags|=RD_CONN_EOF;
314 316
 			tcp_emit_closed_event(c, TCP_CLOSED_EOF);
315
-			LM_DBG("EOF on %p, FD %d ([%s]:%u ->", c, fd, ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
317
+			LM_DBG("EOF on %p, FD %d ([%s]:%u ->", c, fd,
318
+					ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
316 319
 			LM_DBG("-> [%s]:%u)\n", ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
317 320
 		}else{
318 321
 			if (unlikely(c->state==S_CONN_CONNECT || c->state==S_CONN_ACCEPT)){
... ...
@@ -336,9 +339,9 @@ again:
336 339
 /* reads next available bytes
337 340
  *   c- tcp connection used for reading, tcp_read changes also c->state on
338 341
  *      EOF and c->req.error on read error
339
- *   * flags - value/result - used to signal a seen or "forced" EOF on the 
340
- *     connection (when it is known that no more data will come after the 
341
- *     current socket buffer is emptied )=> return/signal EOF on the first 
342
+ *   * flags - value/result - used to signal a seen or "forced" EOF on the
343
+ *     connection (when it is known that no more data will come after the
344
+ *     current socket buffer is emptied )=> return/signal EOF on the first
342 345
  *     short read (=> don't use it on POLLPRI, as OOB data will cause short
343 346
  *      reads even if there are still remaining bytes in the socket buffer)
344 347
  * return number of bytes read, 0 on EOF or -1 on error,
... ...
@@ -354,8 +357,8 @@ int tcp_read(struct tcp_connection *c, int* flags)
354 357
 
355 358
 	r=&c->req;
356 359
 	fd=c->fd;
357
-	bytes_free=r->b_size- (int)(r->pos - r->buf);
358
-	
360
+	bytes_free=r->b_size - (unsigned int)(r->pos - r->buf);
361
+
359 362
 	if (unlikely(bytes_free<=0)){
360 363
 		LM_ERR("buffer overrun, dropping ([%s]:%u -> [%s]:%u)\n",
361 364
 				ip_addr2a(&c->rcv.src_ip), c->rcv.src_port,
... ...
@@ -1114,10 +1117,28 @@ static int tcp_read_ws(struct tcp_connection *c, int* read_flags)
1114 1117
 #endif
1115 1118
 		bytes = tcp_read(c, read_flags);
1116 1119
 
1117
-	if (bytes <= 0)
1118
-	{
1119
-		if (likely(r->parsed >= r->pos))
1120
-			return 0;
1120
+	if (bytes < 0) {
1121
+		/* read error */
1122
+		return bytes;
1123
+	}
1124
+	if (r->parsed == r->pos) {
1125
+		/* nothing else to parse */
1126
+		return bytes;
1127
+	}
1128
+	if (r->parsed > r->pos) {
1129
+		LM_ERR("req buf pos (%p) before parsed (%p) [%d]\n", r->pos, r->parsed,
1130
+				bytes);
1131
+		return -1;
1132
+	}
1133
+	if(r->pos > r->buf + r->b_size) {
1134
+		LM_ERR("req pos (%p) over buf (%p / %u) - parsed (%p) [%d]\n", r->pos,
1135
+				r->buf, r->b_size, r->parsed, bytes);
1136
+		return -1;
1137
+	}
1138
+	if(r->buf > r->parsed) {
1139
+		LM_ERR("req parsed (%p) before buf (%p / %u) - pos (%p) [%d]\n",
1140
+				r->parsed, r->buf, r->b_size, r->pos, bytes);
1141
+		return -1;
1121 1142
 	}
1122 1143
 
1123 1144
 	size = r->pos - r->parsed;
... ...
@@ -1154,11 +1175,11 @@ static int tcp_read_ws(struct tcp_connection *c, int* read_flags)
1154 1175
 		goto skip;
1155 1176
 	pos++;
1156 1177
 	mask_present = p[pos] & 0x80;
1157
-	len = (p[pos++] & 0xff) & ~0x80;
1178
+	len = (p[pos] & 0xff) & ~0x80;
1179
+	pos++;
1158 1180
 
1159 1181
 	/* Work out real length */
1160
-	if (len == 126)
1161
-	{
1182
+	if (len == 126) {
1162 1183
 		/* 2 bytes store the payload size */
1163 1184
 		if (size < pos + 2)
1164 1185
 			goto skip;
... ...
@@ -1187,8 +1208,7 @@ static int tcp_read_ws(struct tcp_connection *c, int* read_flags)
1187 1208
 	}
1188 1209
 
1189 1210
 	/* Skip mask */
1190
-	if (mask_present)
1191
-	{
1211
+	if (mask_present) {
1192 1212
 		if (size < pos + 4)
1193 1213
 			goto skip;
1194 1214
 		pos += 4;
... ...
@@ -1452,10 +1472,10 @@ int tcp_read_req(struct tcp_connection* con, int* bytes_read, int* read_flags)
1452 1472
 	char c;
1453 1473
 	int ret;
1454 1474
 
1455
-		bytes=-1;
1456
-		total_bytes=0;
1457
-		resp=CONN_RELEASE;
1458
-		req=&con->req;
1475
+	bytes=-1;
1476
+	total_bytes=0;
1477
+	resp=CONN_RELEASE;
1478
+	req=&con->req;
1459 1479
 
1460 1480
 again:
1461 1481
 		if (likely(req->error==TCP_REQ_OK)){
... ...
@@ -1479,10 +1499,10 @@ again:
1479 1499
 			}
1480 1500
 #endif
1481 1501
 
1482
-			if (unlikely(bytes==-1)){
1502
+			if (unlikely(bytes<0)){
1483 1503
 				LOG(cfg_get(core, core_cfg, corelog),
1484
-						"ERROR: tcp_read_req: error reading - c: %p r: %p\n",
1485
-						con, req);
1504
+						"ERROR: tcp_read_req: error reading - c: %p r: %p (%d)\n",
1505
+						con, req, bytes);
1486 1506
 				resp=CONN_ERROR;
1487 1507
 				goto end_req;
1488 1508
 			}
... ...
@@ -1610,14 +1630,14 @@ again:
1610 1630
 #endif
1611 1631
 				ret = receive_tcp_msg(req->start, req->parsed-req->start,
1612 1632
 									&con->rcv, con);
1613
-				
1633
+
1614 1634
 			if (unlikely(ret < 0)) {
1615 1635
 				*req->parsed=c;
1616 1636
 				resp=CONN_ERROR;
1617 1637
 				goto end_req;
1618 1638
 			}
1619 1639
 			*req->parsed=c;
1620
-			
1640
+
1621 1641
 			/* prepare for next request */
1622 1642
 			size=req->pos-req->parsed;
1623 1643
 			req->start=req->buf;
... ...
@@ -1628,8 +1648,8 @@ again:
1628 1648
 			req->content_len=0;
1629 1649
 			req->bytes_to_go=0;
1630 1650
 			req->pos=req->buf+size;
1631
-			
1632
-			if (unlikely(size)){ 
1651
+
1652
+			if (unlikely(size)){
1633 1653
 				memmove(req->buf, req->parsed, size);
1634 1654
 				req->parsed=req->buf; /* fix req->parsed after using it */
1635 1655
 #ifdef EXTRA_DEBUG
... ...
@@ -1645,8 +1665,7 @@ again:
1645 1665
 			}
1646 1666
 			req->parsed=req->buf; /* fix req->parsed */
1647 1667
 		}
1648
-		
1649
-		
1668
+
1650 1669
 	end_req:
1651 1670
 		if (likely(bytes_read)) *bytes_read=total_bytes;
1652 1671
 		return resp;
Browse code

core: tcp - improved safety check for available byte in tcp read buffer

Daniel-Constantin Mierla authored on 05/09/2018 09:48:02
Showing 1 changed files
... ...
@@ -356,7 +356,7 @@ int tcp_read(struct tcp_connection *c, int* flags)
356 356
 	fd=c->fd;
357 357
 	bytes_free=r->b_size- (int)(r->pos - r->buf);
358 358
 	
359
-	if (unlikely(bytes_free==0)){
359
+	if (unlikely(bytes_free<=0)){
360 360
 		LM_ERR("buffer overrun, dropping ([%s]:%u -> [%s]:%u)\n",
361 361
 				ip_addr2a(&c->rcv.src_ip), c->rcv.src_port,
362 362
 				ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
Browse code

core: fix typos

Thanks, lintian
> I: kamailio: spelling-error-in-binary usr/sbin/kamailio lenght length
> I: kamailio: spelling-error-in-binary usr/sbin/kamailio orderd ordered
> I: kamailio: spelling-error-in-binary usr/sbin/kamailio uknown unknown

Victor Seva authored on 08/02/2018 08:53:43
Showing 1 changed files
... ...
@@ -1176,7 +1176,7 @@ static int tcp_read_ws(struct tcp_connection *c, int* read_flags)
1176 1176
 		   handled to 2^32 - which should be plenty for SIP! */
1177 1177
 		if((p[pos] & 0xff)!=0 || (p[pos + 1] & 0xff)!=0
1178 1178
 				|| (p[pos + 2] & 0xff)!=0 || (p[pos + 3] & 0xff)!=0) {
1179
-			LM_WARN("advertised lenght is too large (more than 2^32)\n");
1179
+			LM_WARN("advertised length is too large (more than 2^32)\n");
1180 1180
 			goto skip;
1181 1181
 		}
1182 1182
 		len = ((p[pos + 4] & 0xff) << 24)
... ...
@@ -1194,9 +1194,9 @@ static int tcp_read_ws(struct tcp_connection *c, int* read_flags)
1194 1194
 		pos += 4;
1195 1195
 	}
1196 1196
 
1197
-	/* check if advertised lenght fits in read buffer */
1197
+	/* check if advertised length fits in read buffer */
1198 1198
 	if(len>=r->b_size) {
1199
-		LM_WARN("advertised lenght (%u) greater than buffer size (%u)\n",
1199
+		LM_WARN("advertised length (%u) greater than buffer size (%u)\n",
1200 1200
 				len, r->b_size);
1201 1201
 		goto skip;
1202 1202
 	}
... ...
@@ -1282,9 +1282,9 @@ static int tcp_read_hep3(struct tcp_connection *c, int* read_flags)
1282 1282
 
1283 1283
 	len = ((uint32_t)(p[4] & 0xff) <<  8) + (p[5] & 0xff);
1284 1284
 
1285
-	/* check if advertised lenght fits in read buffer */
1285
+	/* check if advertised length fits in read buffer */
1286 1286
 	if(len>=r->b_size) {
1287
-		LM_WARN("advertised lenght (%u) greater than buffer size (%u)\n",
1287
+		LM_WARN("advertised length (%u) greater than buffer size (%u)\n",
1288 1288
 				len, r->b_size);
1289 1289
 		goto skip;
1290 1290
 	}
... ...
@@ -1878,7 +1878,7 @@ read_error:
1878 1878
 						fm->fd, fm->type, fm->data);
1879 1879
 			goto error;
1880 1880
 		default:
1881
-			LM_CRIT("uknown fd type %d\n", fm->type); 
1881
+			LM_CRIT("unknown fd type %d\n", fm->type);
1882 1882
 			goto error;
1883 1883
 	}
1884 1884
 	
Browse code

core: tcp - pass updated recv info to receive_msg() inside hep3_process_msg()

- based on GH #1379
- credits for troubleshooting and initial patch submission to
Amit Zigelman <amit.zigelman@vonage.com>

Daniel-Constantin Mierla authored on 20/12/2017 18:42:10
Showing 1 changed files
... ...
@@ -1336,7 +1336,7 @@ static int hep3_process_msg(char* tcpbuf, unsigned int len,
1336 1336
 		return -1;
1337 1337
 	}
1338 1338
 
1339
-	ret = receive_msg(msg.buf, msg.len, rcv_info);
1339
+	ret = receive_msg(msg.buf, msg.len, &msg.rcv);
1340 1340
 	LM_DBG("running hep3-enclosed sip request route returned %d\n", ret);
1341 1341
 	free_sip_msg(&msg)