Browse code

siptrace: helper function to link sip msg attributes

Daniel-Constantin Mierla authored on 10/04/2020 09:02:55
Showing 1 changed files
... ...
@@ -577,12 +577,12 @@ static int sip_trace_insert_db(db_key_t *db_keys, db_val_t *db_vals,
577 577
 		int db_nkeys, char *dtext)
578 578
 {
579 579
 	LM_DBG("storing info - %s\n", dtext);
580
-	if(trace_db_mode = 2 && db_funcs.insert_async != NULL) {
580
+	if(trace_db_mode == 2 && db_funcs.insert_async != NULL) {
581 581
 		if(db_funcs.insert_async(db_con, db_keys, db_vals, db_nkeys) < 0) {
582 582
 			LM_ERR("error storing trace - async - %s\n", dtext);
583 583
 			return -1;
584 584
 		}
585
-	} else if(trace_db_mode != 1 && db_funcs.insert_delayed != NULL) {
585
+	} else if(trace_db_mode == 1 && db_funcs.insert_delayed != NULL) {
586 586
 		if(db_funcs.insert_delayed(db_con, db_keys, db_vals, db_nkeys) < 0) {
587 587
 			LM_ERR("error storing trace - delayed - %s\n", dtext);
588 588
 			return -1;
... ...
@@ -907,7 +907,8 @@ static int sip_trace_helper(sip_msg_t *msg, dest_info_t *dst, str *duri,
907 907
 		}
908 908
 
909 909
 		if (msg->first_line.type != SIP_REQUEST ||
910
-				(trace_type == SIPTRACE_DIALOG && msg->first_line.u.request.method_value != METHOD_INVITE)) {
910
+				(trace_type == SIPTRACE_DIALOG
911
+				 && msg->first_line.u.request.method_value != METHOD_INVITE)) {
911 912
 			LM_WARN("When tracing a %s sip_trace() has to be initiated on the %s\n",
912 913
 					trace_type == SIPTRACE_TRANSACTION ? "transaction" : "dialog",
913 914
 					trace_type == SIPTRACE_TRANSACTION ? "request message" : "initial invite");
... ...
@@ -956,7 +957,8 @@ static int sip_trace_helper(sip_msg_t *msg, dest_info_t *dst, str *duri,
956 957
 			} else {
957 958
 				/* serialize what's in info */
958 959
 				/* save correlation id in siptrace_info avp
959
-				 * we want to have traced user avp value at the moment of sip_trace function call*/
960
+				 * we want to have traced user avp value at the moment
961
+				 * of sip_trace function call */
960 962
 				if (trace_add_info_xavp(info) < 0) {
961 963
 					LM_ERR("failed to serialize siptrace info! Won't trace dialog!\n");
962 964
 					return -1;
... ...
@@ -985,7 +987,8 @@ trace_current:
985 987
 }
986 988
 
987 989
 /**
988
- * Send sip trace with destination and correlation id and specify what messages to be traced
990
+ * Send sip trace with destination and correlation id
991
+ * and specify what messages to be traced
989 992
  */
990 993
 static int ki_sip_trace_dst_cid_flag(sip_msg_t *msg, str *duri, str *cid, str* sflag)
991 994
 {
... ...
@@ -1116,6 +1119,30 @@ static int w_sip_trace3(sip_msg_t *msg, char *dest, char *correlation_id, char *
1116 1119
 			(correlation_id)?&correlation_id_str:NULL, NULL, trace_type);
1117 1120
 }
1118 1121
 
1122
+/**
1123
+ * link call-id, method, from-tag and to-tag
1124
+ */
1125
+static int sip_trace_msg_attrs(sip_msg_t *msg, siptrace_data_t *sto)
1126
+{
1127
+	if(sip_trace_prepare(msg) < 0) {
1128
+		return -1;
1129
+	}
1130
+
1131
+	sto->callid = msg->callid->body;
1132
+
1133
+	if(msg->first_line.type == SIP_REQUEST) {
1134
+		sto->method = msg->first_line.u.request.method;
1135
+	} else {
1136
+		sto->method = get_cseq(msg)->method;
1137
+	}
1138
+
1139
+	sto->fromtag = get_from(msg)->tag_value;
1140
+	sto->totag = get_to(msg)->tag_value;
1141
+
1142
+	return 0;
1143
+
1144
+}
1145
+
1119 1146
 static int sip_trace(sip_msg_t *msg, dest_info_t *dst,
1120 1147
 		str *correlation_id_str, char *dir)
1121 1148
 {
... ...
@@ -1138,20 +1165,8 @@ static int sip_trace(sip_msg_t *msg, dest_info_t *dst,
1138 1165
 		return -1;
1139 1166
 	}
1140 1167
 
1141
-	if(sip_trace_prepare(msg) < 0)
1168
+	if(sip_trace_msg_attrs(msg, &sto) < 0) {
1142 1169
 		return -1;
1143
-
1144
-	sto.callid = msg->callid->body;
1145
-
1146
-	if(msg->first_line.type == SIP_REQUEST) {
1147
-		sto.method = msg->first_line.u.request.method;
1148
-	} else {
1149
-		if(parse_headers(msg, HDR_CSEQ_F, 0) != 0 || msg->cseq == NULL
1150
-				|| msg->cseq->parsed == NULL) {
1151
-			LM_ERR("cannot parse cseq header\n");
1152
-			return -1;
1153
-		}
1154
-		sto.method = get_cseq(msg)->method;
1155 1170
 	}
1156 1171
 
1157 1172
 	if(msg->first_line.type == SIP_REPLY) {
... ...
@@ -1233,9 +1248,6 @@ static int sip_trace(sip_msg_t *msg, dest_info_t *dst,
1233 1248
 		sto.dir = "out";
1234 1249
 	}
1235 1250
 
1236
-	sto.fromtag = get_from(msg)->tag_value;
1237
-	sto.totag = get_to(msg)->tag_value;
1238
-
1239 1251
 #ifdef STATISTICS
1240 1252
 	if(msg->first_line.type == SIP_REPLY) {
1241 1253
 		sto.stat = siptrace_rpl;
... ...
@@ -1357,8 +1369,8 @@ static void trace_onreq_out(struct cell *t, int type, struct tmcb_params *ps)
1357 1369
 		}
1358 1370
 	}
1359 1371
 
1360
-	/* for incoming cancel this is the only play(i've found) where I have the CANCEL transaction
1361
-	 * and can register a callback for the reply */
1372
+	/* for incoming cancel this is the only place where can get the CANCEL
1373
+	 * transaction and can register a callback for the reply */
1362 1374
 	memset(&sto, 0, sizeof(siptrace_data_t));
1363 1375
 
1364 1376
 	if(traced_user_avp.n != 0)
... ...
@@ -1370,8 +1382,9 @@ static void trace_onreq_out(struct cell *t, int type, struct tmcb_params *ps)
1370 1382
 		return;
1371 1383
 	}
1372 1384
 
1373
-	if(sip_trace_prepare(msg) < 0)
1385
+	if(sip_trace_msg_attrs(msg, &sto) < 0) {
1374 1386
 		return;
1387
+	}
1375 1388
 
1376 1389
 	if(ps->send_buf.len > 0) {
1377 1390
 		sto.body = ps->send_buf;
... ...
@@ -1380,8 +1393,6 @@ static void trace_onreq_out(struct cell *t, int type, struct tmcb_params *ps)
1380 1393
 		sto.body.len = sizeof("No request buffer") - 1;
1381 1394
 	}
1382 1395
 
1383
-	sto.callid = msg->callid->body;
1384
-
1385 1396
 	if(ps->send_buf.len > 10) {
1386 1397
 		sto.method.s = ps->send_buf.s;
1387 1398
 		sto.method.len = 0;
... ...
@@ -1453,9 +1464,6 @@ static void trace_onreq_out(struct cell *t, int type, struct tmcb_params *ps)
1453 1464
 		sto.dir = "out";
1454 1465
 	}
1455 1466
 
1456
-	sto.fromtag = get_from(msg)->tag_value;
1457
-	sto.totag = get_to(msg)->tag_value;
1458
-
1459 1467
 #ifdef STATISTICS
1460 1468
 	sto.stat = siptrace_req;
1461 1469
 #endif
... ...
@@ -1501,16 +1509,13 @@ static void trace_onreply_in(struct cell *t, int type, struct tmcb_params *ps)
1501 1509
 		return;
1502 1510
 	}
1503 1511
 
1504
-	if(sip_trace_prepare(msg) < 0)
1512
+	if(sip_trace_msg_attrs(msg, &sto) < 0) {
1505 1513
 		return;
1514
+	}
1506 1515
 
1507 1516
 	sto.body.s = msg->buf;
1508 1517
 	sto.body.len = msg->len;
1509 1518
 
1510
-	sto.callid = msg->callid->body;
1511
-
1512
-	sto.method = get_cseq(msg)->method;
1513
-
1514 1519
 	sto.status.s = int2strbuf(ps->code, statusbuf, INT2STR_MAX_LEN, &sto.status.len);
1515 1520
 	if(sto.status.s == 0) {
1516 1521
 		LM_ERR("failure to get the status string\n");
... ...
@@ -1545,18 +1550,17 @@ static void trace_onreply_in(struct cell *t, int type, struct tmcb_params *ps)
1545 1550
 
1546 1551
 	sto.dir = "in";
1547 1552
 
1548
-	sto.fromtag = get_from(msg)->tag_value;
1549
-	sto.totag = get_to(msg)->tag_value;
1550 1553
 #ifdef STATISTICS
1551 1554
 	sto.stat = siptrace_rpl;
1552 1555
 #endif
1553 1556
 
1554 1557
 	if (info->uriState == STRACE_RAW_URI) {
1555
-		LM_BUG("uriState must be either UNUSED or PARSED here! must be a bug! Message won't be traced!\n");
1558
+		LM_BUG("uriState must be either UNUSED or PARSED here - skip tracing!\n");
1556 1559
 		return;
1557 1560
 	}
1558 1561
 
1559
-	sip_trace_store(&sto, info->uriState == STRACE_PARSED_URI ? &info->u.dest_info : NULL, NULL);
1562
+	sip_trace_store(&sto, (info->uriState == STRACE_PARSED_URI)
1563
+			? &info->u.dest_info : NULL, NULL);
1560 1564
 	return;
1561 1565
 }
1562 1566
 
... ...
@@ -1605,8 +1609,9 @@ static void trace_onreply_out(struct cell *t, int type, struct tmcb_params *ps)
1605 1609
 		faked = 1;
1606 1610
 	}
1607 1611
 
1608
-	if(sip_trace_prepare(msg) < 0)
1612
+	if(sip_trace_msg_attrs(msg, &sto) < 0) {
1609 1613
 		return;
1614
+	}
1610 1615
 
1611 1616
 	if(faked == 0) {
1612 1617
 		if(ps->send_buf.len > 0) {
... ...
@@ -1633,9 +1638,6 @@ static void trace_onreply_out(struct cell *t, int type, struct tmcb_params *ps)
1633 1638
 		}
1634 1639
 	}
1635 1640
 
1636
-	sto.callid = msg->callid->body;
1637
-	sto.method = get_cseq(msg)->method;
1638
-
1639 1641
 	if(trace_local_ip.s && trace_local_ip.len > 0) {
1640 1642
 		sto.fromip = trace_local_ip;
1641 1643
 	} else {
... ...
@@ -1677,8 +1679,6 @@ static void trace_onreply_out(struct cell *t, int type, struct tmcb_params *ps)
1677 1679
 	}
1678 1680
 
1679 1681
 	sto.dir = "out";
1680
-	sto.fromtag = get_from(msg)->tag_value;
1681
-	sto.totag = get_to(msg)->tag_value;
1682 1682
 
1683 1683
 #ifdef STATISTICS
1684 1684
 	sto.stat = siptrace_rpl;
... ...
@@ -1813,15 +1813,13 @@ static void trace_sl_onreply_out(sl_cbp_t *slcbp)
1813 1813
 
1814 1814
 	msg = req;
1815 1815
 
1816
-	if(sip_trace_prepare(msg) < 0)
1816
+	if(sip_trace_msg_attrs(msg, &sto) < 0) {
1817 1817
 		return;
1818
+	}
1818 1819
 
1819 1820
 	sto.body.s = (slcbp->reply) ? slcbp->reply->s : "";
1820 1821
 	sto.body.len = (slcbp->reply) ? slcbp->reply->len : 0;
1821 1822
 
1822
-	sto.callid = msg->callid->body;
1823
-	sto.method = msg->first_line.u.request.method;
1824
-
1825 1823
 	if(trace_local_ip.len > 0) {
1826 1824
 		sto.fromip = trace_local_ip;
1827 1825
 	} else {
... ...
@@ -1862,8 +1860,6 @@ static void trace_sl_onreply_out(sl_cbp_t *slcbp)
1862 1860
 	}
1863 1861
 
1864 1862
 	sto.dir = "out";
1865
-	sto.fromtag = get_from(msg)->tag_value;
1866
-	sto.totag = get_to(msg)->tag_value;
1867 1863
 
1868 1864
 #ifdef STATISTICS
1869 1865
 	sto.stat = siptrace_rpl;
... ...
@@ -2069,26 +2065,17 @@ int siptrace_net_data_recv(sr_event_param_t *evp)
2069 2065
 			goto afterdb;
2070 2066
 		}
2071 2067
 
2072
-		if(sip_trace_prepare(&tmsg) < 0) {
2068
+		if(sip_trace_msg_attrs(&tmsg, &sto) < 0) {
2073 2069
 			free_sip_msg(&tmsg);
2074 2070
 			goto afterdb;
2075 2071
 		}
2076 2072
 
2077
-		sto.callid = tmsg.callid->body;
2078
-		if(tmsg.first_line.type == SIP_REQUEST) {
2079
-			sto.method = tmsg.first_line.u.request.method;
2080
-		} else {
2081
-			sto.method = get_cseq(&tmsg)->method;
2082
-		}
2083
-
2084 2073
 		if(tmsg.first_line.type == SIP_REPLY) {
2085 2074
 			sto.status = tmsg.first_line.u.reply.status;
2086 2075
 		} else {
2087 2076
 			sto.status.s = "";
2088 2077
 			sto.status.len = 0;
2089 2078
 		}
2090
-		sto.fromtag = get_from(&tmsg)->tag_value;
2091
-		sto.totag = get_to(&tmsg)->tag_value;
2092 2079
 
2093 2080
 		gettimeofday(&sto.tv, NULL);
2094 2081
 		sip_trace_store_db(&sto);
... ...
@@ -2180,26 +2167,17 @@ int siptrace_net_data_send(sr_event_param_t *evp)
2180 2167
 			goto afterdb;
2181 2168
 		}
2182 2169
 
2183
-		if(sip_trace_prepare(&tmsg) < 0) {
2170
+		if(sip_trace_msg_attrs(&tmsg, &sto) < 0) {
2184 2171
 			free_sip_msg(&tmsg);
2185 2172
 			goto afterdb;
2186 2173
 		}
2187 2174
 
2188
-		sto.callid = tmsg.callid->body;
2189
-		if(tmsg.first_line.type == SIP_REQUEST) {
2190
-			sto.method = tmsg.first_line.u.request.method;
2191
-		} else {
2192
-			sto.method = get_cseq(&tmsg)->method;
2193
-		}
2194
-
2195 2175
 		if(tmsg.first_line.type == SIP_REPLY) {
2196 2176
 			sto.status = tmsg.first_line.u.reply.status;
2197 2177
 		} else {
2198 2178
 			sto.status.s = "";
2199 2179
 			sto.status.len = 0;
2200 2180
 		}
2201
-		sto.fromtag = get_from(&tmsg)->tag_value;
2202
-		sto.totag = get_to(&tmsg)->tag_value;
2203 2181
 
2204 2182
 		gettimeofday(&sto.tv, NULL);
2205 2183
 		sip_trace_store_db(&sto);