view mncc/mtsock.c @ 241:d7c2a2c0b922

utils/Makefile: only using libnumdb2 now
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 14 Aug 2023 15:42:49 -0800
parents db7ed6a55ba4
children
line wrap: on
line source

/*
 * 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);
}