view mncc/main.c @ 38:020ba624bdd8

mgw source: #include <stdlib.h> in files using tmgw_ctrl.h
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 10 Jul 2022 00:36:31 -0800
parents ccc5ab6d8388
children 2ebad02adbe5
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;

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();
	}
}