changeset 17:4e0a73be9e37

sip-udp-dump utility written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 26 Jun 2022 18:12:32 -0800
parents 4c2000b3aed7
children 87c077b23996
files .hgignore utils/Makefile utils/sip-udp-dump.c
diffstat 3 files changed, 92 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sun Jun 26 16:33:30 2022 -0800
+++ b/.hgignore	Sun Jun 26 18:12:32 2022 -0800
@@ -4,6 +4,7 @@
 
 ^mncc/themwi-mncc$
 
+^utils/sip-udp-dump$
 ^utils/themwi-check-own$
 ^utils/themwi-dump-numdb$
 ^utils/themwi-short-dial$
--- a/utils/Makefile	Sun Jun 26 16:33:30 2022 -0800
+++ b/utils/Makefile	Sun Jun 26 18:12:32 2022 -0800
@@ -1,12 +1,16 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	themwi-check-own themwi-dump-numdb themwi-short-dial themwi-update-numdb
+PROGS=	sip-udp-dump themwi-check-own themwi-dump-numdb themwi-short-dial \
+	themwi-update-numdb
 LIBNUMDB=../libnumdb/libnumdb.a
 LIBUTIL=../libutil/libutil.a
 INSTBIN=/usr/local/bin
 
 all:	${PROGS}
 
+sip-udp-dump:	sip-udp-dump.c
+	${CC} ${CFLAGS} -o $@ $@.c
+
 themwi-check-own:	themwi-check-own.o ${LIBNUMDB} ${LIBUTIL}
 	${CC} ${CFLAGS} -o $@ $@.o ${LIBNUMDB} ${LIBUTIL}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utils/sip-udp-dump.c	Sun Jun 26 18:12:32 2022 -0800
@@ -0,0 +1,86 @@
+/*
+ * This debug utility binds to UDP port 5060 (SIP) and dumps any/all
+ * packets received at this port.  A one-line summary is printed to
+ * stdout, and the full SIP INVITE packet content is written to a file.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <time.h>
+#include <unistd.h>
+
+static int sock;
+static FILE *logf;
+static char dgram[2048];
+static unsigned dgram_len;
+static struct sockaddr_in sin;
+static time_t curtime;
+
+static void
+log_full_packet()
+{
+	fprintf(logf, "From %s:%u %u bytes %s", inet_ntoa(sin.sin_addr),
+		ntohs(sin.sin_port), dgram_len, ctime(&curtime));
+	fwrite(dgram, 1, dgram_len, logf);
+	putc('\n', logf);
+	fflush(logf);
+}
+
+static void
+print_header_line()
+{
+	char *cp;
+
+	cp = index(dgram, '\n');
+	if (cp)
+		*cp = '\0';
+	puts(dgram);
+}
+
+main(argc, argv)
+	char **argv;
+{
+	int rc;
+	socklen_t addrlen;
+
+	if (argc != 2) {
+		fprintf(stderr, "usage: %s logfile\n", argv[0]);
+		exit(1);
+	}
+	logf = fopen(argv[1], "a");
+	if (!logf) {
+		perror(argv[1]);
+		exit(1);
+	}
+	sock = socket(AF_INET, SOCK_DGRAM, 0);
+	if (sock < 0) {
+		perror("socket");
+		exit(1);
+	}
+	sin.sin_family = AF_INET;
+	sin.sin_addr.s_addr = INADDR_ANY;
+	sin.sin_port = htons(5060);
+	rc = bind(sock, (struct sockaddr *) &sin, sizeof sin);
+	if (rc < 0)
+		perror("bind");
+	for (;;) {
+		addrlen = sizeof sin;
+		rc = recvfrom(sock, dgram, sizeof(dgram) - 1, 0,
+				(struct sockaddr *) &sin, &addrlen);
+		if (rc < 0) {
+			perror("recvfrom");
+			exit(1);
+		}
+		time(&curtime);
+		dgram_len = rc;
+		dgram[dgram_len] = '\0';
+		log_full_packet();
+		print_header_line();
+	}
+}