changeset 48:8117d8ee44a5

sip-in: beginning of INVITE handling
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 06 Sep 2022 22:07:51 -0800
parents 62f39c7cee15
children dec31b1a8b96
files sip-in/Makefile sip-in/invite.c sip-in/sip_uas.c
diffstat 3 files changed, 79 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/sip-in/Makefile	Tue Sep 06 20:33:56 2022 -0800
+++ b/sip-in/Makefile	Tue Sep 06 22:07:51 2022 -0800
@@ -1,7 +1,7 @@
 CC=	gcc
 CFLAGS=	-O2
 PROG=	themwi-sip-in
-OBJS=	main.o mgw_sock.o readconf.o sip_log.o sip_uas.o sip_udp.o
+OBJS=	invite.o main.o mgw_sock.o readconf.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/invite.c	Tue Sep 06 22:07:51 2022 -0800
@@ -0,0 +1,75 @@
+/*
+ * Here we implement our handling of SIP INVITE method.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <syslog.h>
+#include <unistd.h>
+#include "../libsip/parse.h"
+#include "../libsip/uas_basic.h"
+#include "../libsip/out_msg.h"
+
+void
+handle_sip_invite(req, ess, sin)
+	struct sip_pkt_rx *req;
+	struct uas_parse_hdrs *ess;
+	struct sockaddr_in *sin;
+{
+	char uri_user[13], *called_nanp;
+	struct sip_msg_out resp;
+	int rc;
+
+	/* check for existing Call-ID will go here */
+	/* extract called number from Request-URI */
+	rc = user_from_sip_uri(req->req_uri, uri_user, 12);
+	if (rc < 0) {
+not_nanp:	start_response_out_msg(&resp,
+			"416 Request-URI is not a NANP number");
+error_resp:	rc = add_resp_basic_headers(&resp, ess, req->req_method);
+		if (rc < 0) {
+			syslog(LOG_ERR,
+				"INVITE error response length exceeded");
+			return;
+		}
+		out_msg_finish(&resp);
+		sip_tx_packet(&resp, sin);
+			return;
+	}
+	if (uri_user[0] == '+') {
+		if (grok_number_string(uri_user+1, 0) != 11 ||
+		    uri_user[1] != '1')
+			goto not_nanp;
+		called_nanp = uri_user + 2;
+	} else switch (grok_number_string(uri_user)) {
+	case 10:
+		called_nanp = uri_user;
+		break;
+	case 11:
+		if (uri_user[0] != '1')
+			goto not_nanp;
+		called_nanp = uri_user + 1;
+		break;
+	default:
+		goto not_nanp;
+	}
+	if (!is_nanp_valid_prefix(called_nanp))
+		goto not_nanp;
+	/* it is valid NANP - but is it one of ours? */
+	refresh_number_db();
+	if (!is_nanp_locally_owned(called_nanp)) {
+		start_response_out_msg(&resp,
+			"404 Called number does not belong here");
+		goto error_resp;
+	}
+	/*
+	 * Remaining checks to be implemented:
+	 * Require and Supported headers
+	 * SDP message body
+	 */
+}
--- a/sip-in/sip_uas.c	Tue Sep 06 20:33:56 2022 -0800
+++ b/sip-in/sip_uas.c	Tue Sep 06 22:07:51 2022 -0800
@@ -34,10 +34,10 @@
 	struct sip_msg_out resp;
 	int rc;
 
-	start_response_out_msg(&resp, "405 Method not supported");
+	start_response_out_msg(&resp, "501 Method not supported");
 	rc = add_resp_basic_headers(&resp, ess, req->req_method);
 	if (rc < 0) {
-too_long:	syslog(LOG_ERR, "sending 405 error: response length exceeded");
+too_long:	syslog(LOG_ERR, "sending 501 error: response length exceeded");
 		return;
 	}
 	rc = out_msg_add_header(&resp, "Allow", "INVITE,ACK,CANCEL,BYE");
@@ -63,7 +63,7 @@
 	}
 	/* dispatch by method */
 	if (!strcmp(msg->req_method, "INVITE"))
-		method_tbi(msg, &ess, sin);
+		handle_sip_invite(msg, &ess, sin);
 	else if (!strcmp(msg->req_method, "ACK"))
 		method_tbi(msg, &ess, sin);
 	else if (!strcmp(msg->req_method, "CANCEL"))