comparison sip-in/bye_in.c @ 80:a9944b66dcc5

sip-in: handle incoming BYE
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 20 Sep 2022 19:35:34 -0800
parents sip-in/cancel.c@b0df2b200d77
children 0d6435808bcd
comparison
equal deleted inserted replaced
79:b0df2b200d77 80:a9944b66dcc5
1 /*
2 * Here we implement our handling of incoming SIP BYE requests.
3 */
4
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <strings.h>
13 #include <syslog.h>
14 #include "../include/gsm48_const.h"
15 #include "../libsip/parse.h"
16 #include "../libsip/uas_basic.h"
17 #include "../libsip/out_msg.h"
18 #include "call.h"
19
20 extern struct call *find_call_by_sip_id();
21
22 void
23 handle_sip_bye(req, ess, sin)
24 struct sip_pkt_rx *req;
25 struct uas_parse_hdrs *ess;
26 struct sockaddr_in *sin;
27 {
28 struct call *call;
29 struct sip_msg_out resp;
30 char cseq_str[32];
31 int rc;
32
33 call = find_call_by_sip_id(ess->call_id);
34 if (!call) {
35 start_response_out_msg(&resp, "481 Call-ID not found");
36 rc = add_resp_basic_headers(&resp, ess, req->req_method);
37 if (rc < 0)
38 return;
39 out_msg_finish(&resp);
40 sip_tx_packet(&resp, sin);
41 return;
42 }
43 switch (call->sip_state) {
44 case SIP_STATE_INVITE_PROC:
45 case SIP_STATE_RINGING:
46 case SIP_STATE_RINGING_PRACK:
47 call->overall_state = OVERALL_STATE_TEARDOWN;
48 disconnect_mncc(call, GSM48_CAUSE_LOC_NET_BEYOND,
49 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
50 disconnect_tmgw(call);
51 strcpy(call->invite_fail, "487 Call attempt terminated");
52 signal_invite_error(call);
53 break;
54 case SIP_STATE_INVITE_200:
55 case SIP_STATE_CONNECTED:
56 call->overall_state = OVERALL_STATE_TEARDOWN;
57 disconnect_mncc(call, GSM48_CAUSE_LOC_NET_BEYOND,
58 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
59 disconnect_tmgw(call);
60 call->sip_state = SIP_STATE_ENDED;
61 break;
62 case SIP_STATE_BYE_SENT:
63 call->sip_state = SIP_STATE_ENDED;
64 break;
65 case SIP_STATE_INVITE_ERR:
66 case SIP_STATE_ENDED:
67 break;
68 case SIP_STATE_MSG_SIZE_ERR:
69 return;
70 }
71 /* send 200 response to BYE */
72 start_response_out_msg(&resp, "200 OK");
73 rc = out_msg_add_header(&resp, "From", call->invite_from);
74 if (rc < 0) {
75 msg_size_err: syslog(LOG_ERR, "BYE 200 response length exceeded");
76 call->sip_state = SIP_STATE_MSG_SIZE_ERR;
77 return;
78 }
79 rc = out_msg_add_header(&resp, "To", call->invite_to);
80 if (rc < 0)
81 goto msg_size_err;
82 rc = out_msg_add_header(&resp, "Call-ID", call->sip_call_id);
83 if (rc < 0)
84 goto msg_size_err;
85 sprintf(cseq_str, "%u BYE", ess->cseq_num);
86 rc = out_msg_add_header(&resp, "CSeq", cseq_str);
87 if (rc < 0)
88 goto msg_size_err;
89 rc = out_msg_add_header(&resp, "Via", ess->via);
90 if (rc < 0)
91 goto msg_size_err;
92 out_msg_finish(&resp);
93 sip_tx_packet(&resp, sin);
94 }