view sip-in/main.c @ 75:dd845c4933e1

sip-manual-out: swallow any received ACK w/o sending 501 response
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 20 Sep 2022 12:28:37 -0800
parents 709b78a4ebf0
children 372209628038
line wrap: on
line source

/*
 * Main module for themwi-sip-in.
 */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
#include <syslog.h>
#include <unistd.h>

extern unsigned cfg_retrans_timeout;

extern int mgw_socket, sip_socket;
extern int gsm_socket, gsm_is_connected;

static int max_fd;

struct timeval cur_event_time;

update_max_fd(newfd)
{
	if (newfd > max_fd)
		max_fd = newfd;
}

main(argc, argv)
	char **argv;
{
	fd_set fds;
	int rc, need_retrans;
	struct timeval timeout;

	openlog("themwi-sip-in", 0, LOG_LOCAL5);
	read_config_file();
	if (read_number_db() < 0) {
		fprintf(stderr, "error reading number database\n");
		exit(1);
	}
	if (open_tmgw_socket() < 0) {
		fprintf(stderr, "error connecting to themwi-mgw socket\n");
		exit(1);
	}
	if (open_sip_udp_socket() < 0) {
		fprintf(stderr, "error opening SIP UDP socket\n");
		exit(1);
	}
	if (argv[1]) {
		rc = open_sip_log_file(argv[1]);
		if (rc < 0)
			exit(1);	/* error msg already printed */
	}
	signal(SIGPIPE, SIG_IGN);
	/* main select loop */
	for (;;) {
		FD_ZERO(&fds);
		FD_SET(mgw_socket, &fds);
		FD_SET(sip_socket, &fds);
		if (gsm_is_connected)
			FD_SET(gsm_socket, &fds);
		need_retrans = 0;
		scan_call_list_for_timeouts(&need_retrans);
		if (need_retrans) {
			timeout.tv_sec = cfg_retrans_timeout / 1000;
			timeout.tv_usec = (cfg_retrans_timeout % 1000) * 1000;
			rc = select(max_fd+1, &fds, 0, 0, &timeout);
		} else
			rc = select(max_fd+1, &fds, 0, 0, 0);
		if (rc < 0) {
			if (errno == EINTR)
				continue;
			syslog(LOG_CRIT, "select: %m");
			exit(1);
		}
		gettimeofday(&cur_event_time, 0);
		if (rc == 0) {
			run_periodic_retrans();
			continue;
		}
		if (gsm_is_connected && FD_ISSET(gsm_socket, &fds))
			gsm_socket_select();
		if (FD_ISSET(sip_socket, &fds))
			sip_socket_select();
		if (FD_ISSET(mgw_socket, &fds))
			mgw_socket_select();
	}
}