diff sip-in/sip_udp.c @ 47:62f39c7cee15

themwi-sip-in skeleton started
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 06 Sep 2022 20:33:56 -0800
parents
children 15c9e1f8f756
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sip-in/sip_udp.c	Tue Sep 06 20:33:56 2022 -0800
@@ -0,0 +1,85 @@
+/*
+ * In this module we implement our UDP socket for SIP,
+ * and the associated lowest-level protocol handling.
+ */
+
+#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/out_msg.h"
+
+extern struct in_addr sip_bind_ip;
+extern unsigned sip_bind_port;
+
+int sip_socket;
+
+open_sip_udp_socket()
+{
+	struct sockaddr_in sin;
+	int rc;
+
+	sip_socket = socket(AF_INET, SOCK_DGRAM, 0);
+	if (sip_socket < 0) {
+		syslog(LOG_CRIT, "socket(AF_INET, SOCK_DGRAM, 0): %m");
+		return(-1);
+	}
+	sin.sin_family = AF_INET;
+	sin.sin_addr = sip_bind_ip;
+	sin.sin_port = htons(sip_bind_port);
+	rc = bind(sip_socket, (struct sockaddr *) &sin, sizeof sin);
+	if (rc < 0) {
+		syslog(LOG_CRIT, "bind of SIP UDP socket: %m");
+		return(-1);
+	}
+	update_max_fd(sip_socket);
+	return(0);
+}
+
+void
+sip_socket_select()
+{
+	struct sip_pkt_rx pkt;
+	struct sockaddr_in sin;
+	socklen_t addrlen;
+	int rc;
+
+	addrlen = sizeof sin;
+	rc = recvfrom(sip_socket, pkt.pkt_buffer, MAX_SIP_RX_PACKET, 0,
+			(struct sockaddr *) &sin, &addrlen);
+	if (rc <= 0)
+		return;
+	pkt.pkt_length = rc;
+	log_sip_msg_rx(pkt.pkt_buffer, pkt.pkt_length, &sin);
+	rc = parse_incoming_sip_msg(&pkt);
+	if (rc < 0) {
+		/* parse errors */
+		if (rc == -2 && pkt.parse_msgtype == SIP_MSG_TYPE_REQ)
+			syslog(LOG_ERR,
+				"SIP %.16s msg exceeds MAX_HEADER_FIELDS",
+				pkt.req_method);
+		else if (rc == -2 && pkt.parse_msgtype == SIP_MSG_TYPE_RESP)
+			syslog(LOG_ERR,
+				"SIP response msg exceeds MAX_HEADER_FIELDS");
+		/* in any case, silently discard */
+		return;
+	}
+	/* dispatch good-so-far SIP message */
+	if (pkt.parse_msgtype == SIP_MSG_TYPE_REQ)
+		process_sip_request(&pkt, &sin);
+}
+
+void
+sip_tx_packet(msg, sin)
+	struct sip_msg_out *msg;
+	struct sockaddr_in *sin;
+{
+	log_sip_msg_tx(msg->buf, msg->msg_len, sin);
+	/* actual UDP send to BulkVS omitted at this development stage */
+}