annotate sip-manual-out/uac.c @ 71:d74b545a3c2a

sip-manual-out: new test program
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 20 Sep 2022 10:14:18 -0800
parents
children d7b6b8973a83
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
71
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * Here we implement processing of SIP responses to the requests we sent out.
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/socket.h>
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <netinet/in.h>
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <arpa/inet.h>
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <string.h>
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <strings.h>
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include "../libsip/parse.h"
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include "../libsip/out_msg.h"
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #define MAX_TO_TAG 63
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 extern char *get_single_header();
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 extern struct in_addr sip_bind_ip;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 extern unsigned sip_bind_port;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 extern char call_id[], from_uri[], to_uri[];
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 char to_tag[MAX_TO_TAG+1];
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 add_req_boilerplate(msg, cseq)
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 struct sip_msg_out *msg;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 char *cseq;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 {
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 char strbuf[256];
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 int rc;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 sprintf(strbuf, "SIP/2.0/UDP %s:%u",
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 inet_ntoa(sip_bind_ip), sip_bind_port);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 rc = out_msg_add_header(msg, "Via", strbuf);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (rc < 0)
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 return rc;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 rc = out_msg_add_header(msg, "From", from_uri);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 if (rc < 0)
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 return rc;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 if (to_tag[0]) {
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 sprintf(strbuf, "<%s>;tag=%s", to_uri, to_tag);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 rc = out_msg_add_header(msg, "To", strbuf);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 } else
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 rc = out_msg_add_header(msg, "To", to_uri);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (rc < 0)
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 return rc;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 rc = out_msg_add_header(msg, "Call-ID", call_id);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 if (rc < 0)
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 return rc;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 rc = out_msg_add_header(msg, "CSeq", cseq);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (rc < 0)
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 return rc;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 return out_msg_add_header(msg, "Max-Forwards", "70");
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 add_contact_header(msg)
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 struct sip_msg_out *msg;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 {
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 char strbuf[80];
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 sprintf(strbuf, "<sip:%s:%u;transport=udp>",
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 inet_ntoa(sip_bind_ip), sip_bind_port);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 return out_msg_add_header(msg, "Contact", strbuf);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 }
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 static void
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 send_ack(sin)
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 struct sockaddr_in *sin;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 {
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 struct sip_msg_out msg;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 int rc;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 rc = start_request_out_msg(&msg, "ACK", to_uri);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (rc < 0) {
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 msg_size_err: fprintf(stderr, "composing ACK message: size error\n");
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 return;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 }
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 rc = add_req_boilerplate(&msg, "1 ACK");
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 if (rc < 0)
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 goto msg_size_err;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 out_msg_finish(&msg);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 sip_tx_packet(&msg, sin);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 }
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 static void
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 handle_invite_response(msg, sin)
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 struct sip_pkt_rx *msg;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 struct sockaddr_in *sin;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 {
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 printf("Response to INVITE: %s\n", msg->status_str);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 if (msg->status_code >= 200) {
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 printf("Sending ACK\n");
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 send_ack(sin);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 }
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 }
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 void
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 process_sip_response(msg, sin)
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 struct sip_pkt_rx *msg;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 struct sockaddr_in *sin;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 {
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 char *call_id_hdr, *cseq_hdr;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 call_id_hdr = get_single_header(msg, "Call-ID", "i", (int *) 0);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 if (!call_id_hdr) {
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 printf("Got SIP response w/o Call-ID header\n");
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 return;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 if (strcmp(call_id_hdr, call_id)) {
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 printf("Got SIP response with wrong Call-ID\n");
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 return;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 }
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 cseq_hdr = get_single_header(msg, "CSeq", (char *) 0, (int *) 0);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 if (!cseq_hdr) {
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 printf("Got SIP response w/o CSeq header\n");
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 return;
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 }
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 if (!strcmp(cseq_hdr, "1 INVITE"))
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 handle_invite_response(msg, sin);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 else
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 printf("Got SIP resp for our Call-ID with unknown CSeq %s\n",
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 cseq_hdr);
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 }