comparison utils/sip-udp-dump.c @ 17:4e0a73be9e37

sip-udp-dump utility written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 26 Jun 2022 18:12:32 -0800
parents
children 87c077b23996
comparison
equal deleted inserted replaced
16:4c2000b3aed7 17:4e0a73be9e37
1 /*
2 * This debug utility binds to UDP port 5060 (SIP) and dumps any/all
3 * packets received at this port. A one-line summary is printed to
4 * stdout, and the full SIP INVITE packet content is written to a file.
5 */
6
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <strings.h>
15 #include <time.h>
16 #include <unistd.h>
17
18 static int sock;
19 static FILE *logf;
20 static char dgram[2048];
21 static unsigned dgram_len;
22 static struct sockaddr_in sin;
23 static time_t curtime;
24
25 static void
26 log_full_packet()
27 {
28 fprintf(logf, "From %s:%u %u bytes %s", inet_ntoa(sin.sin_addr),
29 ntohs(sin.sin_port), dgram_len, ctime(&curtime));
30 fwrite(dgram, 1, dgram_len, logf);
31 putc('\n', logf);
32 fflush(logf);
33 }
34
35 static void
36 print_header_line()
37 {
38 char *cp;
39
40 cp = index(dgram, '\n');
41 if (cp)
42 *cp = '\0';
43 puts(dgram);
44 }
45
46 main(argc, argv)
47 char **argv;
48 {
49 int rc;
50 socklen_t addrlen;
51
52 if (argc != 2) {
53 fprintf(stderr, "usage: %s logfile\n", argv[0]);
54 exit(1);
55 }
56 logf = fopen(argv[1], "a");
57 if (!logf) {
58 perror(argv[1]);
59 exit(1);
60 }
61 sock = socket(AF_INET, SOCK_DGRAM, 0);
62 if (sock < 0) {
63 perror("socket");
64 exit(1);
65 }
66 sin.sin_family = AF_INET;
67 sin.sin_addr.s_addr = INADDR_ANY;
68 sin.sin_port = htons(5060);
69 rc = bind(sock, (struct sockaddr *) &sin, sizeof sin);
70 if (rc < 0)
71 perror("bind");
72 for (;;) {
73 addrlen = sizeof sin;
74 rc = recvfrom(sock, dgram, sizeof(dgram) - 1, 0,
75 (struct sockaddr *) &sin, &addrlen);
76 if (rc < 0) {
77 perror("recvfrom");
78 exit(1);
79 }
80 time(&curtime);
81 dgram_len = rc;
82 dgram[dgram_len] = '\0';
83 log_full_packet();
84 print_header_line();
85 }
86 }