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 2 changed files
... ...
@@ -79,6 +79,7 @@
79 79
 #include "tcp_stats.h"
80 80
 #include "tcp_ev.h"
81 81
 #include "tsend.h"
82
+#include "events.h"
82 83
 #include "timer_ticks.h"
83 84
 #include "local_timer.h"
84 85
 #ifdef CORE_TLS
... ...
@@ -3548,6 +3549,26 @@ again:
3548 3549
 }
3549 3550
 
3550 3551
 
3552
+static int tcp_emit_closed_event(struct tcp_connection *con, enum tcp_closed_reason reason)
3553
+{
3554
+	int ret;
3555
+	tcp_closed_event_info_t tev;
3556
+	sr_event_param_t evp = {0};
3557
+
3558
+	ret = 0;
3559
+	LM_DBG("TCP closed event creation triggered (reason: %d)\n", reason);
3560
+	if(likely(sr_event_enabled(SREV_TCP_CLOSED))) {
3561
+		memset(&tev, 0, sizeof(tcp_closed_event_info_t));
3562
+		tev.reason = reason;
3563
+		tev.con = con;
3564
+		evp.data = (void*)(&tev);
3565
+		ret = sr_event_exec(SREV_TCP_CLOSED, &evp);
3566
+	} else {
3567
+		LM_DBG("no callback registering for handling TCP closed event\n");
3568
+	}
3569
+	return ret;
3570
+}
3571
+
3551 3572
 
3552 3573
 /* handles io from a tcp child process
3553 3574
  * params: tcp_c - pointer in the tcp_children array, to the entry for
... ...
@@ -3628,6 +3649,7 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
3628 3649
 				/* if refcnt was 1 => it was used only in the
3629 3650
 				   tcp reader => it's not hashed or watched for IO
3630 3651
 				   anymore => no need to io_watch_del() */
3652
+				tcp_emit_closed_event(c, TCP_CLOSED_EOF);
3631 3653
 				tcpconn_destroy(tcpconn);
3632 3654
 				break;
3633 3655
 			}
... ...
@@ -3639,6 +3661,7 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
3639 3661
 						tcpconn->flags &= ~F_CONN_WRITE_W;
3640 3662
 					}
3641 3663
 #endif /* TCP_ASYNC */
3664
+					tcp_emit_closed_event(c, TCP_CLOSED_EOF);
3642 3665
 					tcpconn_put_destroy(tcpconn);
3643 3666
 				}
3644 3667
 #ifdef TCP_ASYNC
... ...
@@ -3690,6 +3713,7 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
3690 3713
 							io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
3691 3714
 							tcpconn->flags&=~F_CONN_WRITE_W;
3692 3715
 						}
3716
+						tcp_emit_closed_event(c, TCP_CLOSED_EOF);
3693 3717
 						tcpconn_put_destroy(tcpconn);
3694 3718
 					} else if (unlikely(tcpconn->flags & F_CONN_WRITE_W)){
3695 3719
 						BUG("unhashed connection watched for write\n");
... ...
@@ -3726,6 +3750,7 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
3726 3750
 						tcpconn->flags&=~F_CONN_WRITE_W;
3727 3751
 					}
3728 3752
 #endif /* TCP_ASYNC */
3753
+					tcp_emit_closed_event(c, TCP_CLOSED_EOF);
3729 3754
 					tcpconn_put_destroy(tcpconn);
3730 3755
 				}
3731 3756
 #ifdef TCP_ASYNC
... ...
@@ -3757,6 +3782,7 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
3757 3782
 #endif /* TCP_ASYNC */
3758 3783
 				if (tcpconn_try_unhash(tcpconn))
3759 3784
 					tcpconn_put(tcpconn);
3785
+				tcp_emit_closed_event(c, TCP_CLOSED_EOF);
3760 3786
 				tcpconn_put_destroy(tcpconn); /* deref & delete if refcnt==0 */
3761 3787
 				break;
3762 3788
 		default:
... ...
@@ -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);