annotate utils/sip-udp-dump.c @ 256:63252528a56c

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