changeset 79:b0df2b200d77

sip-in: implement SIP CANCEL
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 20 Sep 2022 18:58:11 -0800
parents 72b7d85d6354
children a9944b66dcc5
files sip-in/Makefile sip-in/cancel.c sip-in/sip_uas.c
diffstat 3 files changed, 111 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/sip-in/Makefile	Tue Sep 20 18:00:56 2022 -0800
+++ b/sip-in/Makefile	Tue Sep 20 18:58:11 2022 -0800
@@ -1,9 +1,9 @@
 CC=	gcc
 CFLAGS=	-O2
 PROG=	themwi-sip-in
-OBJS=	call_list.o call_setup.o disconnect.o invite.o main.o mgw_ops.o \
-	mgw_sock.o mncc_handle.o mncc_sock.o readconf.o retrans.o sip_ack.o \
-	sip_log.o sip_uas.o sip_udp.o
+OBJS=	call_list.o call_setup.o cancel.o disconnect.o invite.o main.o \
+	mgw_ops.o mgw_sock.o mncc_handle.o mncc_sock.o readconf.o retrans.o \
+	sip_ack.o sip_log.o sip_uas.o sip_udp.o
 LIBS=	../libnumdb/libnumdb.a ../libsip/libsip.a ../libutil/libutil.a
 INSTBIN=/usr/local/bin
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sip-in/cancel.c	Tue Sep 20 18:58:11 2022 -0800
@@ -0,0 +1,107 @@
+/*
+ * Here we implement our handling of SIP CANCEL method.
+ */
+
+#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();
+
+static void
+cancel_200_response(req, ess, sin, call)
+	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;
+
+	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, "CANCEL 200 response length exceeded");
+		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 CANCEL", call->invite_cseq);
+	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);
+}
+
+static void
+cancel_481_response(req, ess, sin)
+	struct sip_pkt_rx *req;
+	struct uas_parse_hdrs *ess;
+	struct sockaddr_in *sin;
+{
+	struct sip_msg_out resp;
+	int rc;
+
+	start_response_out_msg(&resp, "481 Call-ID/CSeq 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);
+}
+
+void
+handle_sip_cancel(req, ess, sin)
+	struct sip_pkt_rx *req;
+	struct uas_parse_hdrs *ess;
+	struct sockaddr_in *sin;
+{
+	struct call *call;
+
+	call = find_call_by_sip_id(ess->call_id);
+	if (!call) {
+		cancel_481_response(req, ess, sin);
+		return;
+	}
+	/* weed out wrong CSeq */
+	if (ess->cseq_num != call->invite_cseq) {
+		cancel_481_response(req, ess, sin);
+		return;
+	}
+	/* it is for us - act on it */
+	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_MSG_SIZE_ERR:
+		return;
+	}
+	cancel_200_response(req, ess, sin, call);
+}
--- a/sip-in/sip_uas.c	Tue Sep 20 18:00:56 2022 -0800
+++ b/sip-in/sip_uas.c	Tue Sep 20 18:58:11 2022 -0800
@@ -66,7 +66,7 @@
 	else if (!strcmp(msg->req_method, "ACK"))
 		handle_sip_ack(msg, &ess, sin);
 	else if (!strcmp(msg->req_method, "CANCEL"))
-		method_tbi(msg, &ess, sin);
+		handle_sip_cancel(msg, &ess, sin);
 	else if (!strcmp(msg->req_method, "BYE"))
 		method_tbi(msg, &ess, sin);
 	else