diff sip-manual-out/uas.c @ 71:d74b545a3c2a

sip-manual-out: new test program
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 20 Sep 2022 10:14:18 -0800
parents sip-in/sip_uas.c@8cf85edca543
children dd845c4933e1
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sip-manual-out/uas.c	Tue Sep 20 10:14:18 2022 -0800
@@ -0,0 +1,113 @@
+/*
+ * UAS for sip-manual-out.
+ */
+
+#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 "../libsip/parse.h"
+#include "../libsip/uas_basic.h"
+#include "../libsip/out_msg.h"
+
+extern char call_id[];
+
+static void
+bye_correct_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;
+
+	printf("Received BYE for our call, responding with 200\n");
+	start_response_out_msg(&resp, "200 OK");
+	rc = add_resp_basic_headers(&resp, ess, req->req_method);
+	if (rc < 0) {
+		fprintf(stderr, "sending 200 response: msg length exceeded\n");
+		return;
+	}
+	out_msg_finish(&resp);
+	sip_tx_packet(&resp, sin);
+}
+
+static void
+bye_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;
+
+	printf("Received BYE for unknown call, responding with 481\n");
+	start_response_out_msg(&resp, "481 Call-ID not found");
+	rc = add_resp_basic_headers(&resp, ess, req->req_method);
+	if (rc < 0) {
+		fprintf(stderr, "sending 481 response: msg length exceeded\n");
+		return;
+	}
+	out_msg_finish(&resp);
+	sip_tx_packet(&resp, sin);
+}
+
+static void
+handle_bye(req, ess, sin)
+	struct sip_pkt_rx *req;
+	struct uas_parse_hdrs *ess;
+	struct sockaddr_in *sin;
+{
+	if (!strcmp(ess->call_id, call_id))
+		bye_correct_call(req, ess, sin);
+	else
+		bye_unknown_call(req, ess, sin);
+}
+
+static void
+unsupported_method(req, ess, sin)
+	struct sip_pkt_rx *req;
+	struct uas_parse_hdrs *ess;
+	struct sockaddr_in *sin;
+{
+	struct sip_msg_out resp;
+	int rc;
+
+	printf("SIP %.16s request: unsupported method\n", req->req_method);
+	start_response_out_msg(&resp, "501 Method not supported");
+	rc = add_resp_basic_headers(&resp, ess, req->req_method);
+	if (rc < 0) {
+too_long:	fprintf(stderr,
+			"sending 501 error: response length exceeded\n");
+		return;
+	}
+	rc = out_msg_add_header(&resp, "Allow", "BYE");
+	if (rc < 0)
+		goto too_long;
+	out_msg_finish(&resp);
+	sip_tx_packet(&resp, sin);
+}
+
+void
+process_sip_request(msg, sin)
+	struct sip_pkt_rx *msg;
+	struct sockaddr_in *sin;
+{
+	struct uas_parse_hdrs ess;
+	int rc;
+
+	rc = uas_get_basic_headers(msg, &ess);
+	if (rc < 0) {
+		printf("SIP %.16s request: bad or missing %s header\n",
+			msg->req_method, ess.error_field);
+		return;
+	}
+	/* dispatch by method */
+	if (!strcmp(msg->req_method, "BYE"))
+		handle_bye(msg, &ess, sin);
+	else
+		unsupported_method(msg, &ess, sin);
+}