diff sip-out/main.c @ 154:e54b0a9e322f

beginning of themwi-sip-out
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 11 Oct 2022 23:04:01 -0800
parents sip-in/main.c@6aa63cf4620a
children cb440d003976
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sip-out/main.c	Tue Oct 11 23:04:01 2022 -0800
@@ -0,0 +1,119 @@
+/*
+ * Main module for themwi-sip-out.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/errno.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <signal.h>
+#include <syslog.h>
+#include <unistd.h>
+#include "../include/out_routes.h"
+#include "call.h"
+
+extern unsigned cfg_retrans_timeout;
+
+extern int mgw_socket, mgw_is_connected;
+extern int sip_socket, mncc_listener;
+extern struct mncc_conn *mncc_conn_list;
+
+static int max_fd;
+
+struct timeval cur_event_time;
+
+update_max_fd(newfd)
+{
+	if (newfd > max_fd)
+		max_fd = newfd;
+}
+
+main(argc, argv)
+	char **argv;
+{
+	fd_set fds;
+	struct mncc_conn *conn, **connp;
+	int rc, need_retrans, dead_sip_flag;
+	struct timeval timeout;
+	time_t dead_sip_time;
+
+	openlog("themwi-sip-out", 0, LOG_LOCAL5);
+	read_config_file();
+	if (read_out_routes_db() < 0) {
+		fprintf(stderr, "error reading out-routes.bin database\n");
+		exit(1);
+	}
+	if (create_outcall_socket() < 0) {
+		fprintf(stderr, "error creating outcall socket\n");
+		exit(1);
+	}
+	if (open_sip_udp_socket() < 0) {
+		fprintf(stderr, "error opening SIP UDP socket\n");
+		exit(1);
+	}
+	if (argv[1]) {
+		rc = open_sip_log_file(argv[1]);
+		if (rc < 0)
+			exit(1);	/* error msg already printed */
+	}
+	signal(SIGPIPE, SIG_IGN);
+	/* main select loop */
+	for (;;) {
+		FD_ZERO(&fds);
+		FD_SET(sip_socket, &fds);
+		FD_SET(mncc_listener, &fds);
+		if (mgw_is_connected)
+			FD_SET(mgw_socket, &fds);
+		for (connp = &mncc_conn_list; conn = *connp; ) {
+			if (conn->fd < 0) {
+				*connp = conn->next;
+				free(conn);
+				continue;
+			}
+			FD_SET(conn->fd, &fds);
+			connp = &conn->next;
+		}
+		need_retrans = dead_sip_flag = 0;
+		scan_call_list_for_timeouts(&need_retrans, &dead_sip_flag,
+					    &dead_sip_time);
+		if (need_retrans) {
+			timeout.tv_sec = cfg_retrans_timeout / 1000;
+			timeout.tv_usec = (cfg_retrans_timeout % 1000) * 1000;
+			rc = select(max_fd+1, &fds, 0, 0, &timeout);
+		} else if (dead_sip_flag) {
+			if (cur_event_time.tv_sec >= dead_sip_time)
+				timeout.tv_sec = 0;
+			else
+				timeout.tv_sec =
+					dead_sip_time - cur_event_time.tv_sec;
+			timeout.tv_usec = 0;
+			rc = select(max_fd+1, &fds, 0, 0, &timeout);
+		} else
+			rc = select(max_fd+1, &fds, 0, 0, 0);
+		if (rc < 0) {
+			if (errno == EINTR)
+				continue;
+			syslog(LOG_CRIT, "select: %m");
+			exit(1);
+		}
+		gettimeofday(&cur_event_time, 0);
+		if (rc) {
+			if (FD_ISSET(mncc_listener, &fds))
+				mncc_listener_select();
+			for (conn = mncc_conn_list; conn; conn = conn->next)
+				if (FD_ISSET(conn->fd, &fds))
+					mncc_socket_select(conn);
+			if (FD_ISSET(sip_socket, &fds))
+				sip_socket_select();
+			if (mgw_is_connected && FD_ISSET(mgw_socket, &fds))
+				mgw_socket_select();
+		} else if (need_retrans)
+			run_periodic_retrans();
+		clear_dead_calls();
+	}
+}