diff pcap-study/rtp-stream-dump.c @ 14:e7f43262efa1

rtp-stream-dump: add hex dump option
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 28 Jul 2024 01:53:41 +0000
parents d59dbdfe1778
children
line wrap: on
line diff
--- a/pcap-study/rtp-stream-dump.c	Sat Jul 27 23:39:50 2024 +0000
+++ b/pcap-study/rtp-stream-dump.c	Sun Jul 28 01:53:41 2024 +0000
@@ -25,6 +25,7 @@
 static int stream_init_flag, toad_init_flag;
 static unsigned last_seq, last_tstamp, stream_ssrc;
 static struct timeval last_pkt_time;
+static int do_hexdump;
 
 static void
 check_dl_type()
@@ -86,6 +87,22 @@
 }
 
 static void
+hexdump_payload(payload, payload_len)
+	u_char *payload;
+	unsigned payload_len;
+{
+	unsigned off;
+
+	for (off = 0; off < payload_len; off++) {
+		if ((off & 3) == 0)
+			putchar(' ');
+		printf(" %02X", payload[off]);
+		if ((off & 15) == 15 || off == payload_len - 1)
+			putchar('\n');
+	}
+}
+
+static void
 process_packet(pkt, caplen, pkt_idx, pkt_time)
 	u_char *pkt;
 	unsigned caplen, pkt_idx;
@@ -136,9 +153,10 @@
 		exit(1);
 	}
 	rtp_stream_logic(pkt + 28, pkt_idx);
+	payload_len = udplen - 20;
 	printf("pkt #%u seq 0x%04X ts 0x%08X: pt=%u M=%u len=%u", pkt_idx,
 		last_seq, last_tstamp, pkt[29] & 0x7F, pkt[29] >> 7,
-		udplen - 20);
+		payload_len);
 	if (toad_init_flag && timercmp(pkt_time, &last_pkt_time, >=)) {
 		timersub(pkt_time, &last_pkt_time, &toad);
 		if (toad.tv_sec)
@@ -151,6 +169,8 @@
 		}
 	}
 	putchar('\n');
+	if (do_hexdump)
+		hexdump_payload(pkt + 40, payload_len);
 	bcopy(pkt_time, &last_pkt_time, sizeof(struct timeval));
 	toad_init_flag = 1;
 }
@@ -163,9 +183,19 @@
 	struct pcap_pkthdr pkthdr;
 	unsigned pkt_idx;
 
-	if (argc != 5) {
+	switch (argc) {
+	case 5:
+		do_hexdump = 0;
+		break;
+	case 6:
+		if (strcmp(argv[5], "hex"))
+			goto usage;
+		do_hexdump = 1;
+		break;
+	default:
+	usage:
 		fprintf(stderr,
-			"usage: %s pcap-file src|dest ip-addr udp-port\n",
+			"usage: %s pcap-file src|dest ip-addr udp-port [hex]\n",
 			argv[0]);
 		exit(1);
 	}