Browse code

siptrace: replaced sequeces of string concatenations with safer formatted print

- used to build proxy-like addresses

Daniel-Constantin Mierla authored on 11/01/2018 10:44:51
Showing 3 changed files
... ...
@@ -1339,12 +1339,16 @@ static void trace_sl_onreply_out(sl_cbp_t *slcbp)
1339 1339
 	if(trace_local_ip.len > 0) {
1340 1340
 		sto.fromip = trace_local_ip;
1341 1341
 	} else {
1342
-		siptrace_copy_proto(msg->rcv.proto, sto.fromip_buff);
1343
-		strcat(sto.fromip_buff, ip_addr2a(&req->rcv.dst_ip));
1344
-		strcat(sto.fromip_buff, ":");
1345
-		strcat(sto.fromip_buff, int2str(req->rcv.dst_port, NULL));
1346
-		sto.fromip.s = sto.fromip_buff;
1347
-		sto.fromip.len = strlen(sto.fromip_buff);
1342
+		sto.fromip.len = snprintf(sto.fromip_buff, SIPTRACE_ADDR_MAX, "%s:%s:%d",
1343
+				siptrace_proto_name(req->rcv.proto),
1344
+				ip_addr2a(&req->rcv.dst_ip), req->rcv.dst_port);
1345
+		if(sto.fromip.len<0 || sto.fromip.len>=SIPTRACE_ADDR_MAX) {
1346
+			LM_ERR("failed to format toip buffer (%d)\n", sto.fromip.len);
1347
+			sto.fromip.s = "any:255.255.255.255";
1348
+			sto.fromip.len = 19;
1349
+		} else {
1350
+			sto.fromip.s = sto.fromip_buff;
1351
+		}
1348 1352
 	}
1349 1353
 
1350 1354
 	strcpy(statusbuf, int2str(slcbp->code, &sto.status.len));
... ...
@@ -1356,13 +1360,16 @@ static void trace_sl_onreply_out(sl_cbp_t *slcbp)
1356 1360
 		sto.toip.len = 19;
1357 1361
 	} else {
1358 1362
 		su2ip_addr(&to_ip, &slcbp->dst->to);
1359
-		siptrace_copy_proto(req->rcv.proto, sto.toip_buff);
1360
-		strcat(sto.toip_buff, ip_addr2a(&to_ip));
1361
-		strcat(sto.toip_buff, ":");
1362
-		strcat(sto.toip_buff,
1363
-				int2str((unsigned long)su_getport(&slcbp->dst->to), &len));
1364
-		sto.toip.s = sto.toip_buff;
1365
-		sto.toip.len = strlen(sto.toip_buff);
1363
+		sto.toip.len = snprintf(sto.toip_buff, SIPTRACE_ADDR_MAX, "%s:%s:%d",
1364
+				siptrace_proto_name(req->rcv.proto), ip_addr2a(&to_ip),
1365
+				(int)su_getport(&slcbp->dst->to));
1366
+		if(sto.toip.len<0 || sto.toip.len>=SIPTRACE_ADDR_MAX) {
1367
+			LM_ERR("failed to format toip buffer (%d)\n", sto.toip.len);
1368
+			sto.toip.s = "any:255.255.255.255";
1369
+			sto.toip.len = 19;
1370
+		} else {
1371
+			sto.toip.s = sto.toip_buff;
1372
+		}
1366 1373
 	}
1367 1374
 
1368 1375
 	sto.dir = "out";
... ...
@@ -318,3 +318,24 @@ error:
318 318
 	}
319 319
 	return -1;
320 320
 }
321
+
322
+/**
323
+ *
324
+ */
325
+char* siptrace_proto_name(int vproto)
326
+{
327
+	switch(vproto) {
328
+		case PROTO_TCP:
329
+			return "tcp";
330
+		case PROTO_TLS:
331
+			return "tls";
332
+		case PROTO_SCTP:
333
+			return "sctp";
334
+		case PROTO_WS:
335
+			return "ws";
336
+		case PROTO_WSS:
337
+			return "wss";
338
+		default:
339
+			return "udp";
340
+	}
341
+}
... ...
@@ -70,4 +70,6 @@ int trace_send_duplicate(char *buf, int len, struct dest_info *dst2);
70 70
 		siptrace_copy_proto_olen(vproto, vbuf, __olen); \
71 71
 	} while(0)
72 72
 
73
-#endif
73
+char* siptrace_proto_name(int vproto);
74
+
75
+#endif
74 76
\ No newline at end of file