# HG changeset patch # User Mychaela Falconia # Date 1656295952 28800 # Node ID 4e0a73be9e374b7c4f9003e5e2c7c36da518b105 # Parent 4c2000b3aed7c2ab0be275d77247aeed4f5775de sip-udp-dump utility written, compiles diff -r 4c2000b3aed7 -r 4e0a73be9e37 .hgignore --- 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$ diff -r 4c2000b3aed7 -r 4e0a73be9e37 utils/Makefile --- 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} diff -r 4c2000b3aed7 -r 4e0a73be9e37 utils/sip-udp-dump.c --- /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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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(); + } +}