view mncc/main.c @ 197:73bbe7da45cd

add LICENSE
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 26 Mar 2023 15:29:48 -0800
parents 2ebad02adbe5
children
line wrap: on
line source

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

struct socket_conn outcall_conn;

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);
	outcall_conn.fd = -1;
	/* 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;
		}
		if (outcall_conn.fd >= 0)
			FD_SET(outcall_conn.fd, &fds);
		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);
		if (outcall_conn.fd >= 0 && FD_ISSET(outcall_conn.fd, &fds))
			extsock_read_select(&outcall_conn);
		gc_call_list();
	}
}