diff mncc/mtsock.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 fd43f179ff1d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mncc/mtsock.c	Sun Jun 26 16:31:47 2022 -0800
@@ -0,0 +1,75 @@
+/*
+ * 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/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 "../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);
+	}
+	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;
+
+	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;
+	}
+	bzero(conn, sizeof(struct socket_conn));
+	conn->fd = fd;
+	conn->next = mtcall_socket_head;
+	mtcall_socket_head = conn;
+	update_max_fd(fd);
+}