changeset 123:a36b731bfef9

sip-manual-out: implement sending BYE and CANCEL
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 29 Sep 2022 17:21:11 -0800
parents 07e4cc5f824c
children 7e04d28fae8b
files sip-manual-out/Makefile sip-manual-out/disc_cmd.c sip-manual-out/main.c sip-manual-out/uac.c
diffstat 4 files changed, 88 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/sip-manual-out/Makefile	Thu Sep 29 15:03:12 2022 -0800
+++ b/sip-manual-out/Makefile	Thu Sep 29 17:21:11 2022 -0800
@@ -1,7 +1,7 @@
 CC=	gcc
 CFLAGS=	-O2
 PROG=	sip-manual-out
-OBJS=	dummy_rtp.o main.o readconf.o sip_log.o sip_udp.o uac.o uas.o
+OBJS=	disc_cmd.o dummy_rtp.o main.o readconf.o sip_log.o sip_udp.o uac.o uas.o
 LIBS=	../libsip/libsip.a ../libutil/libutil.a
 INSTBIN=/usr/local/bin
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sip-manual-out/disc_cmd.c	Thu Sep 29 17:21:11 2022 -0800
@@ -0,0 +1,76 @@
+/*
+ * In this module we implement user-driven sending of CANCEL and BYE
+ * disconnection requests.
+ */
+
+#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/out_msg.h"
+
+extern struct sockaddr_in sip_dest_sin;
+extern char to_uri[];
+
+send_cancel_req()
+{
+	struct sip_msg_out msg;
+	int rc;
+
+	rc = start_request_out_msg(&msg, "CANCEL", to_uri);
+	if (rc < 0) {
+msg_size_err:	fprintf(stderr, "composing CANCEL message: size error\n");
+		return(-1);
+	}
+	rc = add_req_boilerplate(&msg, "1 CANCEL", 0);
+	if (rc < 0)
+		goto msg_size_err;
+	out_msg_finish(&msg);
+	sip_tx_packet(&msg, &sip_dest_sin);
+	return(0);
+}
+
+send_bye_req()
+{
+	struct sip_msg_out msg;
+	int rc;
+
+	rc = start_request_out_msg(&msg, "BYE", to_uri);
+	if (rc < 0) {
+msg_size_err:	fprintf(stderr, "composing BYE message: size error\n");
+		return(-1);
+	}
+	rc = add_req_boilerplate(&msg, "2 BYE", 1);
+	if (rc < 0)
+		goto msg_size_err;
+	out_msg_finish(&msg);
+	sip_tx_packet(&msg, &sip_dest_sin);
+	return(0);
+}
+
+void
+select_stdin()
+{
+	char buf[256], *cp;
+
+	fgets(buf, sizeof buf, stdin);
+	cp = index(buf, '\n');
+	if (cp) {
+		while (cp > buf && isspace(cp[-1]))
+			cp--;
+		*cp = '\0';
+	}
+	for (cp = buf; isspace(*cp); cp++)
+		;
+	if (!*cp)
+		return;
+	if (!strcmp(cp, "b") || !strcasecmp(cp, "bye"))
+		send_bye_req();
+	else if (!strcmp(cp, "c") || !strcasecmp(cp, "cancel"))
+		send_cancel_req();
+	else
+		fprintf(stderr, "error: non-understood stdin command\n");
+}
--- a/sip-manual-out/main.c	Thu Sep 29 15:03:12 2022 -0800
+++ b/sip-manual-out/main.c	Thu Sep 29 17:21:11 2022 -0800
@@ -40,7 +40,7 @@
 msg_size_err:	fprintf(stderr, "composing INVITE req: msg size error\n");
 		exit(1);
 	}
-	rc = add_req_boilerplate(&msg, "1 INVITE");
+	rc = add_req_boilerplate(&msg, "1 INVITE", 0);
 	if (rc < 0)
 		goto msg_size_err;
 	rc = add_contact_header(&msg);
@@ -140,6 +140,7 @@
 	/* main select loop */
 	for (;;) {
 		FD_ZERO(&fds);
+		FD_SET(0, &fds);
 		FD_SET(sip_socket, &fds);
 		rc = select(sip_socket+1, &fds, 0, 0, 0);
 		if (rc < 0) {
@@ -149,6 +150,8 @@
 			exit(1);
 		}
 		gettimeofday(&cur_event_time, 0);
+		if (FD_ISSET(0, &fds))
+			select_stdin();
 		if (FD_ISSET(sip_socket, &fds))
 			sip_socket_select();
 	}
--- a/sip-manual-out/uac.c	Thu Sep 29 15:03:12 2022 -0800
+++ b/sip-manual-out/uac.c	Thu Sep 29 17:21:11 2022 -0800
@@ -26,7 +26,7 @@
 
 char to_tag[MAX_TO_TAG+1];
 
-add_req_boilerplate(msg, cseq)
+add_req_boilerplate(msg, cseq, add_to_tag)
 	struct sip_msg_out *msg;
 	char *cseq;
 {
@@ -41,7 +41,7 @@
 	rc = out_msg_add_header(msg, "From", from_uri);
 	if (rc < 0)
 		return rc;
-	if (to_tag[0]) {
+	if (add_to_tag && to_tag[0]) {
 		sprintf(strbuf, "<%s>;tag=%s", to_uri, to_tag);
 		rc = out_msg_add_header(msg, "To", strbuf);
 	} else
@@ -80,7 +80,7 @@
 msg_size_err:	fprintf(stderr, "composing ACK message: size error\n");
 		return;
 	}
-	rc = add_req_boilerplate(&msg, "1 ACK");
+	rc = add_req_boilerplate(&msg, "1 ACK", 1);
 	if (rc < 0)
 		goto msg_size_err;
 	out_msg_finish(&msg);
@@ -129,6 +129,10 @@
 	}
 	if (rid.cseq_num == 1 && !strcmp(rid.cseq_method, "INVITE"))
 		handle_invite_response(msg, sin);
+	else if (rid.cseq_num == 1 && !strcmp(rid.cseq_method, "CANCEL"))
+		printf("Response to CANCEL: %s\n", msg->status_str);
+	else if (rid.cseq_num == 2 && !strcmp(rid.cseq_method, "BYE"))
+		printf("Response to BYE: %s\n", msg->status_str);
 	else
 		printf("Got SIP resp for our Call-ID with unknown CSeq %u %s\n",
 			rid.cseq_num, rid.cseq_method);