diff sip-out/reinvite.c @ 165:9419fe55824f

themwi-sip-out complete to the point of compiling and linking
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 12 Oct 2022 17:05:45 -0800
parents sip-manual-out/reinvite.c@b51247739897
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sip-out/reinvite.c	Wed Oct 12 17:05:45 2022 -0800
@@ -0,0 +1,97 @@
+/*
+ * Here we handle incoming INVITE requests in the UAS role: even though
+ * we are strictly outbound, BulkVS servers will send us periodic
+ * re-INVITEs as keepalives, and we have to play along.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/time.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/out_routes.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
+invite_found_call(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;
+	int rc;
+
+	switch (call->sip_state) {
+	case SIP_STATE_INV_SENT:
+	case SIP_STATE_100_RCVD:
+		start_response_out_msg(&resp, "491 Outbound INVITE pending");
+		break;
+	case SIP_STATE_CONNECTED:
+		start_response_out_msg(&resp, "200 OK");
+		break;
+	case SIP_STATE_CANCEL_SENT:
+	case SIP_STATE_BYE_SENT:
+	case SIP_STATE_ACCEPT_100:
+	case SIP_STATE_ACCEPT_200:
+	case SIP_STATE_ENDED:
+		start_response_out_msg(&resp, "488 Call terminated");
+		break;
+	case SIP_STATE_MSG_SIZE_ERR:
+		return;
+	default:
+		syslog(LOG_CRIT,
+			"FATAL: invalid SIP state 0x%x on incoming re-INVITE",
+			call->sip_state);
+		exit(1);
+	}
+	rc = add_resp_basic_headers(&resp, ess, req->req_method);
+	if (rc < 0) {
+		syslog(LOG_ERR, "Re-INVITE response message size error");
+		return;
+	}
+	out_msg_finish(&resp);
+	sip_tx_packet(&resp, sin);
+}
+
+static void
+invite_unknown_call(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, "405 This gateway is outbound only");
+	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_invite_req(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)
+		invite_found_call(req, ess, sin, call);
+	else
+		invite_unknown_call(req, ess, sin);
+}