diff mncc/mtsock.c @ 2:053f04687106

mncc: initial import from old ThemWi
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 08 Jun 2024 23:12:12 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mncc/mtsock.c	Sat Jun 08 23:12:12 2024 +0000
@@ -0,0 +1,80 @@
+/*
+ * In this module we implement the MT call socket
+ * to which other ThemWi system sw components connect
+ * in order to send MT calls toward the GSM network.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <syslog.h>
+#include <unistd.h>
+#include "../include/mncc.h"
+#include "struct.h"
+
+static char mtcall_socket_pathname[] = "/var/gsm/mtcall_socket";
+
+int mtcall_listener;
+struct socket_conn *mtcall_socket_head;
+
+create_mtcall_socket()
+{
+	struct sockaddr_un sa;
+	unsigned sa_len;
+	int rc;
+
+	mtcall_listener = socket(AF_UNIX, SOCK_SEQPACKET, 0);
+	if (mtcall_listener < 0) {
+		syslog(LOG_CRIT, "socket(AF_UNIX, SOCK_SEQPACKET, 0): %m");
+		return(-1);
+	}
+	unlink(mtcall_socket_pathname);
+	fill_sockaddr_un(mtcall_socket_pathname, &sa, &sa_len);
+	rc = bind(mtcall_listener, (struct sockaddr *) &sa, sa_len);
+	if (rc < 0) {
+		syslog(LOG_ERR, "bind to %s: %m", mtcall_socket_pathname);
+		return(-1);
+	}
+	rc = listen(mtcall_listener, 3);
+	if (rc < 0) {
+		syslog(LOG_CRIT, "listen on UNIX socket: %m");
+		return(-1);
+	}
+	chmod(mtcall_socket_pathname, 0775);
+	update_max_fd(mtcall_listener);
+	return(0);
+}
+
+void
+mtsock_accept_handler()
+{
+	struct sockaddr_un sa;
+	socklen_t sa_len;
+	int fd;
+	struct socket_conn *conn;
+
+	sa_len = sizeof sa;
+	fd = accept(mtcall_listener, (struct sockaddr *) &sa, &sa_len);
+	if (fd < 0) {
+		syslog(LOG_CRIT, "accept on UNIX socket: %m");
+		exit(1);
+	}
+	conn = malloc(sizeof(struct socket_conn));
+	if (!conn) {
+		syslog(LOG_CRIT, "malloc for mtcall socket conn: %m");
+		close(fd);
+		return;
+	}
+	syslog(LOG_INFO, "new MT call socket connection");
+	bzero(conn, sizeof(struct socket_conn));
+	conn->fd = fd;
+	conn->next = mtcall_socket_head;
+	mtcall_socket_head = conn;
+	update_max_fd(fd);
+}