annotate utils/sip-rx-test.c @ 147:94b5831c017f

sip-manual-out code: split bye_in.c from uas.c
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 08 Oct 2022 23:01:32 -0800
parents 5995660dcbac
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This test program is the next level above sip-udp-dump: it binds
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * to UDP port 5060, waits for a packet to come in, and when a SIP
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * packet does arrive (call one of the numbers from BulkVS while this
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * test program is running, to make BulkVS send SIP INVITE), it does
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * two things with the captured packet:
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 *
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 * 1) The captured packet is written raw into one file named on the
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 * command line;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 *
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 * 2) The packet is parsed with parse_incoming_sip_msg(), and the
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 * parsed structure is written out into the other file named on
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 * the command line.
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 *
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 * This program is intended to serve as a unit test for the parsing
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 * function.
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 */
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 #include <sys/types.h>
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 #include <sys/file.h>
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 #include <sys/socket.h>
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 #include <netinet/in.h>
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 #include <arpa/inet.h>
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 #include <stdio.h>
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 #include <stdlib.h>
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 #include <string.h>
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 #include <strings.h>
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 #include <unistd.h>
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 #include "../libsip/parse.h"
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 static int sock;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 static struct sockaddr_in sin;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 static struct sip_pkt_rx pkt;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 static void
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 save_raw_packet(filename)
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 char *filename;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 {
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 int fd;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 if (fd < 0) {
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 perror(filename);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 exit(1);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 }
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 write(fd, pkt.pkt_buffer, pkt.pkt_length);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 close(fd);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 static void
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 write_parse_output(filename)
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 char *filename;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 {
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 FILE *of;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 unsigned n;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 of = fopen(filename, "w");
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 if (!of) {
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 perror(filename);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 exit(1);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 }
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 switch (pkt.parse_msgtype) {
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 case SIP_MSG_TYPE_REQ:
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 fprintf(of, "Message is a request\n");
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 fprintf(of, "Method: %s\n", pkt.req_method);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 fprintf(of, "Request-URI: %s\n", pkt.req_uri);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 break;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 case SIP_MSG_TYPE_RESP:
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 fprintf(of, "Message is a response\n");
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 fprintf(of, "Status code: %u\n", pkt.status_code);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 fprintf(of, "Status string: %s\n", pkt.status_str);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 break;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 default:
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 fprintf(of, "Parsed returned unknown message type %d\n",
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 pkt.parse_msgtype);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 }
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 fprintf(of, "Number of header fields: %u\n", pkt.num_hdr_fields);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 for (n = 0; n < pkt.num_hdr_fields; n++) {
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 fprintf(of, "Header field %u:\n", n);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 fprintf(of, " Field name: %s\n",
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 pkt.hdr_fields[n].field_name);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 fprintf(of, " Field value: %s\n",
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 pkt.hdr_fields[n].field_value);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 }
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 fprintf(of, "Message body length: %u\n", pkt.msg_body_len);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 fclose(of);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 }
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 main(argc, argv)
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 char **argv;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 {
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 int rc;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 socklen_t addrlen;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 if (argc != 3) {
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 fprintf(stderr, "usage: %s raw-file parsed-file\n", argv[0]);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 exit(1);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 }
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 sock = socket(AF_INET, SOCK_DGRAM, 0);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 if (sock < 0) {
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 perror("socket");
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 exit(1);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 }
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 sin.sin_family = AF_INET;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 sin.sin_addr.s_addr = INADDR_ANY;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 sin.sin_port = htons(5060);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 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: 42
diff changeset
108 if (rc < 0) {
42
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 perror("bind");
43
5995660dcbac sip-rx-test, sip-udp-dump: fix bind error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
110 exit(1);
5995660dcbac sip-rx-test, sip-udp-dump: fix bind error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
111 }
42
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 addrlen = sizeof sin;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 rc = recvfrom(sock, pkt.pkt_buffer, MAX_SIP_RX_PACKET, 0,
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 (struct sockaddr *) &sin, &addrlen);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 if (rc < 0) {
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 perror("recvfrom");
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 exit(1);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 }
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 pkt.pkt_length = rc;
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 printf("Rx from %s:%u, %u bytes\n", inet_ntoa(sin.sin_addr),
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 ntohs(sin.sin_port), pkt.pkt_length);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 save_raw_packet(argv[1]);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 rc = parse_incoming_sip_msg(&pkt);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 if (rc < 0) {
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 printf("Parse error: %d\n", rc);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 exit(0);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 }
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 write_parse_output(argv[2]);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 exit(0);
891ebfb55e6b sip-rx-test program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 }