diff sip-in/prack.c @ 108:0d6435808bcd

sip-in: implement 100rel for 180 Ringing response
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 28 Sep 2022 14:29:10 -0800
parents
children 9b87894704eb
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sip-in/prack.c	Wed Sep 28 14:29:10 2022 -0800
@@ -0,0 +1,117 @@
+/*
+ * Here we implement our handling of SIP PRACK, expected from callers
+ * when we send them a reliable 180 Ringing response.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <ctype.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 char *get_single_header();
+extern struct call *find_call_by_sip_id();
+
+void
+handle_sip_prack(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 *rack, *orig_method, *cp;
+	unsigned rseq, orig_num;
+	int rc;
+
+	call = find_call_by_sip_id(ess->call_id);
+	if (!call) {
+		start_response_out_msg(&resp, "481 Call-ID not found");
+error_resp:	rc = add_resp_basic_headers(&resp, ess, req->req_method);
+		if (rc < 0)
+			return;
+		out_msg_finish(&resp);
+		sip_tx_packet(&resp, sin);
+		return;
+	}
+	rack = get_single_header(req, "RAck", (char *) 0, (int *) 0);
+	if (!rack) {
+		start_response_out_msg(&resp, "400 Missing RAck header");
+		goto error_resp;
+	}
+	if (!isdigit(*rack)) {
+malformed:	start_response_out_msg(&resp, "400 Malformed RAck header");
+		goto error_resp;
+	}
+	rseq = strtoul(rack, &cp, 10);
+	if (!isspace(*cp))
+		goto malformed;
+	while (isspace(*cp))
+		cp++;
+	if (!isdigit(*cp))
+		goto malformed;
+	orig_num = strtoul(cp, &cp, 10);
+	if (!isspace(*cp))
+		goto malformed;
+	while (isspace(*cp))
+		cp++;
+	if (!isupper(*cp))
+		goto malformed;
+	orig_method = cp;
+	while (isalnum(*cp))
+		cp++;
+	if (*cp)
+		goto malformed;
+	if (rseq != 1 || orig_num != call->invite_cseq ||
+	    strcmp(orig_method, "INVITE")) {
+		start_response_out_msg(&resp,
+			"481 RAck fails to match our 100rel response");
+		goto error_resp;
+	}
+	switch (call->sip_state) {
+	case SIP_STATE_RINGING_REL:
+		call->sip_state = SIP_STATE_RINGING;
+		start_response_out_msg(&resp, "200 OK");
+		rc = add_resp_basic_headers(&resp, ess, req->req_method);
+		if (rc < 0) {
+			syslog(LOG_ERR, "PRACK 200 response length exceeded");
+			call->sip_state = SIP_STATE_MSG_SIZE_ERR;
+			call->overall_state = OVERALL_STATE_TEARDOWN;
+			disconnect_mncc(call, GSM48_CAUSE_LOC_PRN_S_LU,
+					GSM48_CC_CAUSE_INTERWORKING);
+			disconnect_tmgw(call);
+			/* TODO: transition from TEARDOWN to DEAD_SIP */
+			return;
+		}
+		out_msg_finish(&resp);
+		sip_tx_packet(&resp, sin);
+		return;
+	case SIP_STATE_RINGING:
+	case SIP_STATE_INVITE_200:
+		start_response_out_msg(&resp, "200 OK");
+		rc = add_resp_basic_headers(&resp, ess, req->req_method);
+		if (rc < 0)
+			return;
+		out_msg_finish(&resp);
+		sip_tx_packet(&resp, sin);
+		return;
+	case SIP_STATE_INVITE_PROC:
+	case SIP_STATE_CONNECTED:
+	case SIP_STATE_BYE_SENT:
+	case SIP_STATE_INVITE_ERR:
+	case SIP_STATE_ENDED:
+		start_response_out_msg(&resp,
+				       "481 No outstanding 100rel response");
+		goto error_resp;
+	}
+}