annotate sip-in/bye_out.c @ 155:2730ccb44549

sip-out: initial UAC response handling
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 11 Oct 2022 23:30:00 -0800
parents 4e16aeafbfbf
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
81
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * In this module we implement our UAC functionality of sending BYE.
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/socket.h>
109
9b87894704eb sip-in: first step toward final call clearing
Mychaela Falconia <falcon@freecalypso.org>
parents: 94
diff changeset
7 #include <sys/time.h>
81
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <netinet/in.h>
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <arpa/inet.h>
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdio.h>
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdint.h>
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <stdlib.h>
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <string.h>
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <strings.h>
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include <syslog.h>
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include "../libsip/parse.h"
94
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
17 #include "../libsip/resp_ident.h"
81
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 #include "../libsip/out_msg.h"
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 #include "call.h"
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
82
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
21 extern struct call *find_call_by_sip_id();
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
22
81
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 extern struct in_addr sip_bind_ip;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 extern unsigned sip_bind_port;
92
a9137bdb6047 sip-in: make BYE Max-Forwards configurable
Mychaela Falconia <falcon@freecalypso.org>
parents: 82
diff changeset
25 extern unsigned max_forwards;
109
9b87894704eb sip-in: first step toward final call clearing
Mychaela Falconia <falcon@freecalypso.org>
parents: 94
diff changeset
26 extern unsigned sip_linger_acked;
9b87894704eb sip-in: first step toward final call clearing
Mychaela Falconia <falcon@freecalypso.org>
parents: 94
diff changeset
27 extern unsigned sip_linger_error;
81
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 fill_bye_out_msg(msg, call)
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 struct sip_msg_out *msg;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 struct call *call;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 {
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 char strbuf[80];
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 int rc;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 rc = start_request_out_msg_urilen(msg, "BYE", call->from_uri,
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 call->from_uri_len);
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 if (rc < 0)
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 return rc;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 sprintf(strbuf, "SIP/2.0/UDP %s:%u",
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 inet_ntoa(sip_bind_ip), sip_bind_port);
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 rc = out_msg_add_header(msg, "Via", strbuf);
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (rc < 0)
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 return rc;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 rc = out_msg_add_header(msg, "From", call->invite_to);
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (rc < 0)
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 return rc;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 rc = out_msg_add_header(msg, "To", call->invite_from);
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 if (rc < 0)
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 return rc;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 rc = out_msg_add_header(msg, "Call-ID", call->sip_call_id);
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (rc < 0)
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 return rc;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 rc = out_msg_add_header(msg, "CSeq", "1 BYE");
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 if (rc < 0)
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 return rc;
92
a9137bdb6047 sip-in: make BYE Max-Forwards configurable
Mychaela Falconia <falcon@freecalypso.org>
parents: 82
diff changeset
57 sprintf(strbuf, "%u", max_forwards);
a9137bdb6047 sip-in: make BYE Max-Forwards configurable
Mychaela Falconia <falcon@freecalypso.org>
parents: 82
diff changeset
58 rc = out_msg_add_header(msg, "Max-Forwards", strbuf);
81
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 if (rc < 0)
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 return rc;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 out_msg_finish(msg);
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 return 0;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 }
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 void
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 initiate_bye(call)
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 struct call *call;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 {
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 struct sip_msg_out msg;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 int rc;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 rc = fill_bye_out_msg(&msg, call);
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 if (rc < 0) {
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 syslog(LOG_ERR, "outgoing BYE request msg length exceeded");
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 call->sip_state = SIP_STATE_MSG_SIZE_ERR;
110
c1c94b7fc2e2 sip-in call clearing: DEAD_SIP transition implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 109
diff changeset
76 sip_mark_end_time(call, sip_linger_error);
c1c94b7fc2e2 sip-in call clearing: DEAD_SIP transition implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 109
diff changeset
77 transition_dead_sip(call);
81
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 return;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 }
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 sip_tx_packet(&msg, &call->udp_sin);
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 call->sip_state = SIP_STATE_BYE_SENT;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 call->sip_tx_count = 1;
915f0f397fb6 sip-in: beginning of outgoing BYE support
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 }
82
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
84
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
85 void
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
86 process_sip_response(msg, sin)
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
87 struct sip_pkt_rx *msg;
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
88 struct sockaddr_in *sin;
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
89 {
94
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
90 struct sip_resp_ident rid;
82
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
91 struct call *call;
94
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
92 int rc;
82
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
93
94
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
94 rc = sip_resp_extract_ident(msg, &rid);
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
95 if (rc < 0) {
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
96 syslog(LOG_ERR, "SIP %03u response: bad or missing %s header",
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
97 msg->status_code, rid.error_field);
82
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
98 return;
94
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
99 }
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
100 call = find_call_by_sip_id(rid.call_id);
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
101 if (!call) {
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
102 syslog(LOG_ERR, "SIP %03u response: unmatched Call-ID",
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
103 msg->status_code);
82
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
104 return;
94
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
105 }
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
106 if (rid.cseq_num != 1 || strcmp(rid.cseq_method, "BYE")) {
82
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
107 syslog(LOG_ERR,
94
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
108 "UAC received %03u response with unknown CSeq %u %.32s",
2c22b40408fb sip-in BYE UAC: use new libsip function for response ident
Mychaela Falconia <falcon@freecalypso.org>
parents: 92
diff changeset
109 msg->status_code, rid.cseq_num, rid.cseq_method);
82
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
110 return;
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
111 }
144
4e16aeafbfbf sip-in: syslog non-200 responses to BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 110
diff changeset
112 if (msg->status_code != 200)
4e16aeafbfbf sip-in: syslog non-200 responses to BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 110
diff changeset
113 syslog(LOG_ERR, "Call in%06u: non-200 response to BYE: %.64s",
4e16aeafbfbf sip-in: syslog non-200 responses to BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 110
diff changeset
114 call->in_tag_num, msg->status_str);
82
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
115 if (msg->status_code < 200)
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
116 return;
109
9b87894704eb sip-in: first step toward final call clearing
Mychaela Falconia <falcon@freecalypso.org>
parents: 94
diff changeset
117 if (call->sip_state == SIP_STATE_BYE_SENT) {
82
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
118 call->sip_state = SIP_STATE_ENDED;
109
9b87894704eb sip-in: first step toward final call clearing
Mychaela Falconia <falcon@freecalypso.org>
parents: 94
diff changeset
119 if (msg->status_code <= 299)
9b87894704eb sip-in: first step toward final call clearing
Mychaela Falconia <falcon@freecalypso.org>
parents: 94
diff changeset
120 sip_mark_end_time(call, sip_linger_acked);
9b87894704eb sip-in: first step toward final call clearing
Mychaela Falconia <falcon@freecalypso.org>
parents: 94
diff changeset
121 else
9b87894704eb sip-in: first step toward final call clearing
Mychaela Falconia <falcon@freecalypso.org>
parents: 94
diff changeset
122 sip_mark_end_time(call, sip_linger_error);
110
c1c94b7fc2e2 sip-in call clearing: DEAD_SIP transition implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 109
diff changeset
123 transition_dead_sip(call);
109
9b87894704eb sip-in: first step toward final call clearing
Mychaela Falconia <falcon@freecalypso.org>
parents: 94
diff changeset
124 }
82
ff4b76a107a1 sip-in: process responses as UAC for BYE
Mychaela Falconia <falcon@freecalypso.org>
parents: 81
diff changeset
125 }