Browse code

core: dedicated struct for TCP closed events

this will allow to differentiate between the different reasons for
connection closed events

Camille Oudot authored on 06/10/2016 14:42:40
Showing 2 changed files
... ...
@@ -335,6 +335,19 @@ typedef struct tcp_event_info {
335 335
 	struct tcp_connection *con;
336 336
 } tcp_event_info_t;
337 337
 
338
+enum tcp_closed_reason {
339
+	TCP_CLOSED_EOF = 0,
340
+	TCP_CLOSED_TIMEOUT,
341
+	TCP_CLOSED_RESET,
342
+
343
+	_TCP_CLOSED_REASON_MAX /* /!\ keep this one always at the end */
344
+};
345
+
346
+typedef struct tcp_closed_event_info {
347
+	enum tcp_closed_reason reason;
348
+	struct tcp_connection *con;
349
+} tcp_closed_event_info_t;
350
+
338 351
 typedef struct ws_event_info {
339 352
 	int type;
340 353
 	char *buf;
... ...
@@ -186,17 +186,16 @@ int tcp_http11_continue(struct tcp_connection *c)
186 186
 }
187 187
 #endif /* HTTP11 */
188 188
 
189
-static int tcp_make_closed_event(struct receive_info* rcv_info, struct tcp_connection* con)
189
+static int tcp_emit_closed_event(struct tcp_connection *con, enum tcp_closed_reason reason)
190 190
 {
191 191
 	int ret;
192
-	tcp_event_info_t tev;
192
+	tcp_closed_event_info_t tev;
193 193
 
194 194
 	ret = 0;
195 195
 	LM_DBG("TCP closed event creation triggered\n");
196 196
 	if(likely(sr_event_enabled(SREV_TCP_CLOSED))) {
197
-		memset(&tev, 0, sizeof(tcp_event_info_t));
198
-		tev.type = SREV_TCP_CLOSED;
199
-		tev.rcv = rcv_info;
197
+		memset(&tev, 0, sizeof(tcp_closed_event_info_t));
198
+		tev.reason = reason;
200 199
 		tev.con = con;
201 200
 		ret = sr_event_exec(SREV_TCP_CLOSED, (void*)(&tev));
202 201
 	} else {
... ...
@@ -291,19 +290,18 @@ again:
291 291
 						strerror(errno), errno,
292 292
 						ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
293 293
 				LOG(cfg_get(core, core_cfg, corelog),"-> [%s]:%u)\n", ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
294
-				if((errno == ECONNRESET || errno == ETIMEDOUT) && likely(c->rcv.proto_reserved1 != 0)){
295
-					tcp_make_closed_event(&c->rcv, c);
294
+				if (errno == ETIMEDOUT) {
295
+					tcp_emit_closed_event(c, TCP_CLOSED_TIMEOUT);
296
+				} else if (errno == ECONNRESET) {
297
+					tcp_emit_closed_event(c, TCP_CLOSED_RESET);
296 298
 				}
297
-
298 299
 				return -1;
299 300
 			}
300 301
 		}else if (unlikely((bytes_read==0) || 
301 302
 					(*flags & RD_CONN_FORCE_EOF))){
302 303
 			c->state=S_CONN_EOF;
303 304
 			*flags|=RD_CONN_EOF;
304
-			if (likely(c->rcv.proto_reserved1 != 0)){
305
-				tcp_make_closed_event(&c->rcv, c);
306
-			}
305
+			tcp_emit_closed_event(c, TCP_CLOSED_EOF);
307 306
 			LM_DBG("EOF on %p, FD %d ([%s]:%u ->", c, fd, ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
308 307
 			LM_DBG("-> [%s]:%u)\n", ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
309 308
 		}else{