diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sip-in/bye_in.c	Tue Sep 20 19:35:34 2022 -0800
@@ -0,0 +1,94 @@
+/*
+ * Here we implement our handling of incoming SIP BYE requests.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <syslog.h>
+#include "../include/gsm48_const.h"
+#include "../libsip/parse.h"
+#include "../libsip/uas_basic.h"
+#include "../libsip/out_msg.h"
+#include "call.h"
+
+extern struct call *find_call_by_sip_id();
+
+void
+handle_sip_bye(req, ess, sin)
+	struct sip_pkt_rx *req;
+	struct uas_parse_hdrs *ess;
+	struct sockaddr_in *sin;
+{
+	struct call *call;
+	struct sip_msg_out resp;
+	char cseq_str[32];
+	int rc;
+
+	call = find_call_by_sip_id(ess->call_id);
+	if (!call) {
+		start_response_out_msg(&resp, "481 Call-ID not found");
+		rc = add_resp_basic_headers(&resp, ess, req->req_method);
+		if (rc < 0)
+			return;
+		out_msg_finish(&resp);
+		sip_tx_packet(&resp, sin);
+		return;
+	}
+	switch (call->sip_state) {
+	case SIP_STATE_INVITE_PROC:
+	case SIP_STATE_RINGING:
+	case SIP_STATE_RINGING_PRACK:
+		call->overall_state = OVERALL_STATE_TEARDOWN;
+		disconnect_mncc(call, GSM48_CAUSE_LOC_NET_BEYOND,
+				GSM48_CC_CAUSE_NORM_CALL_CLEAR);
+		disconnect_tmgw(call);
+		strcpy(call->invite_fail, "487 Call attempt terminated");
+		signal_invite_error(call);
+		break;
+	case SIP_STATE_INVITE_200:
+	case SIP_STATE_CONNECTED:
+		call->overall_state = OVERALL_STATE_TEARDOWN;
+		disconnect_mncc(call, GSM48_CAUSE_LOC_NET_BEYOND,
+				GSM48_CC_CAUSE_NORM_CALL_CLEAR);
+		disconnect_tmgw(call);
+		call->sip_state = SIP_STATE_ENDED;
+		break;
+	case SIP_STATE_BYE_SENT:
+		call->sip_state = SIP_STATE_ENDED;
+		break;
+	case SIP_STATE_INVITE_ERR:
+	case SIP_STATE_ENDED:
+		break;
+	case SIP_STATE_MSG_SIZE_ERR:
+		return;
+	}
+	/* send 200 response to BYE */
+	start_response_out_msg(&resp, "200 OK");
+	rc = out_msg_add_header(&resp, "From", call->invite_from);
+	if (rc < 0) {
+msg_size_err:	syslog(LOG_ERR, "BYE 200 response length exceeded");
+		call->sip_state = SIP_STATE_MSG_SIZE_ERR;
+		return;
+	}
+	rc = out_msg_add_header(&resp, "To", call->invite_to);
+	if (rc < 0)
+		goto msg_size_err;
+	rc = out_msg_add_header(&resp, "Call-ID", call->sip_call_id);
+	if (rc < 0)
+		goto msg_size_err;
+	sprintf(cseq_str, "%u BYE", ess->cseq_num);
+	rc = out_msg_add_header(&resp, "CSeq", cseq_str);
+	if (rc < 0)
+		goto msg_size_err;
+	rc = out_msg_add_header(&resp, "Via", ess->via);
+	if (rc < 0)
+		goto msg_size_err;
+	out_msg_finish(&resp);
+	sip_tx_packet(&resp, sin);
+}