diff mncc/main.c @ 15:ccc5ab6d8388

first version of themwi-mncc for ThemWi2
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 26 Jun 2022 16:31:47 -0800
parents
children 2ebad02adbe5
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mncc/main.c	Sun Jun 26 16:31:47 2022 -0800
@@ -0,0 +1,80 @@
+/*
+ * Main module for ThemWi MNCC daemon.
+ */
+
+#include <sys/types.h>
+#include <sys/errno.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <signal.h>
+#include <syslog.h>
+#include <unistd.h>
+#include "struct.h"
+
+extern int mncc_socket;
+extern int mtcall_listener;
+extern struct socket_conn *mtcall_socket_head;
+
+static int max_fd;
+
+update_max_fd(newfd)
+{
+	if (newfd > max_fd)
+		max_fd = newfd;
+}
+
+main(argc, argv)
+	char **argv;
+{
+	fd_set fds;
+	struct socket_conn *conn, **connp;
+	int c;
+
+	openlog("themwi-mncc", 0, LOG_LOCAL5);
+	if (read_number_db() < 0) {
+		fprintf(stderr, "error reading number database\n");
+		exit(1);
+	}
+	if (open_mncc_socket() < 0) {
+		fprintf(stderr, "error connecting to GSM MNCC socket\n");
+		exit(1);
+	}
+	if (create_mtcall_socket() < 0) {
+		fprintf(stderr, "error creating MT call socket\n");
+		exit(1);
+	}
+	signal(SIGPIPE, SIG_IGN);
+	/* main select loop */
+	for (;;) {
+		FD_ZERO(&fds);
+		FD_SET(mncc_socket, &fds);
+		FD_SET(mtcall_listener, &fds);
+		for (connp = &mtcall_socket_head; conn = *connp; ) {
+			if (conn->fd < 0) {
+				*connp = conn->next;
+				free(conn);
+				continue;
+			}
+			FD_SET(conn->fd, &fds);
+			connp = &conn->next;
+		}
+		c = select(max_fd+1, &fds, 0, 0, 0);
+		if (c < 0) {
+			if (errno == EINTR)
+				continue;
+			syslog(LOG_CRIT, "select: %m");
+			exit(1);
+		}
+		if (FD_ISSET(mncc_socket, &fds))
+			mncc_socket_select();
+		if (FD_ISSET(mtcall_listener, &fds))
+			mtsock_accept_handler();
+		for (conn = mtcall_socket_head; conn; conn = conn->next)
+			if (FD_ISSET(conn->fd, &fds))
+				extsock_read_select(conn);
+		gc_call_list();
+	}
+}