comparison sip-out/main.c @ 154:e54b0a9e322f

beginning of themwi-sip-out
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 11 Oct 2022 23:04:01 -0800
parents sip-in/main.c@6aa63cf4620a
children cb440d003976
comparison
equal deleted inserted replaced
153:99fd4ae573ae 154:e54b0a9e322f
1 /*
2 * Main module for themwi-sip-out.
3 */
4
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <sys/time.h>
8 #include <sys/errno.h>
9 #include <netinet/in.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <strings.h>
14 #include <signal.h>
15 #include <syslog.h>
16 #include <unistd.h>
17 #include "../include/out_routes.h"
18 #include "call.h"
19
20 extern unsigned cfg_retrans_timeout;
21
22 extern int mgw_socket, mgw_is_connected;
23 extern int sip_socket, mncc_listener;
24 extern struct mncc_conn *mncc_conn_list;
25
26 static int max_fd;
27
28 struct timeval cur_event_time;
29
30 update_max_fd(newfd)
31 {
32 if (newfd > max_fd)
33 max_fd = newfd;
34 }
35
36 main(argc, argv)
37 char **argv;
38 {
39 fd_set fds;
40 struct mncc_conn *conn, **connp;
41 int rc, need_retrans, dead_sip_flag;
42 struct timeval timeout;
43 time_t dead_sip_time;
44
45 openlog("themwi-sip-out", 0, LOG_LOCAL5);
46 read_config_file();
47 if (read_out_routes_db() < 0) {
48 fprintf(stderr, "error reading out-routes.bin database\n");
49 exit(1);
50 }
51 if (create_outcall_socket() < 0) {
52 fprintf(stderr, "error creating outcall socket\n");
53 exit(1);
54 }
55 if (open_sip_udp_socket() < 0) {
56 fprintf(stderr, "error opening SIP UDP socket\n");
57 exit(1);
58 }
59 if (argv[1]) {
60 rc = open_sip_log_file(argv[1]);
61 if (rc < 0)
62 exit(1); /* error msg already printed */
63 }
64 signal(SIGPIPE, SIG_IGN);
65 /* main select loop */
66 for (;;) {
67 FD_ZERO(&fds);
68 FD_SET(sip_socket, &fds);
69 FD_SET(mncc_listener, &fds);
70 if (mgw_is_connected)
71 FD_SET(mgw_socket, &fds);
72 for (connp = &mncc_conn_list; conn = *connp; ) {
73 if (conn->fd < 0) {
74 *connp = conn->next;
75 free(conn);
76 continue;
77 }
78 FD_SET(conn->fd, &fds);
79 connp = &conn->next;
80 }
81 need_retrans = dead_sip_flag = 0;
82 scan_call_list_for_timeouts(&need_retrans, &dead_sip_flag,
83 &dead_sip_time);
84 if (need_retrans) {
85 timeout.tv_sec = cfg_retrans_timeout / 1000;
86 timeout.tv_usec = (cfg_retrans_timeout % 1000) * 1000;
87 rc = select(max_fd+1, &fds, 0, 0, &timeout);
88 } else if (dead_sip_flag) {
89 if (cur_event_time.tv_sec >= dead_sip_time)
90 timeout.tv_sec = 0;
91 else
92 timeout.tv_sec =
93 dead_sip_time - cur_event_time.tv_sec;
94 timeout.tv_usec = 0;
95 rc = select(max_fd+1, &fds, 0, 0, &timeout);
96 } else
97 rc = select(max_fd+1, &fds, 0, 0, 0);
98 if (rc < 0) {
99 if (errno == EINTR)
100 continue;
101 syslog(LOG_CRIT, "select: %m");
102 exit(1);
103 }
104 gettimeofday(&cur_event_time, 0);
105 if (rc) {
106 if (FD_ISSET(mncc_listener, &fds))
107 mncc_listener_select();
108 for (conn = mncc_conn_list; conn; conn = conn->next)
109 if (FD_ISSET(conn->fd, &fds))
110 mncc_socket_select(conn);
111 if (FD_ISSET(sip_socket, &fds))
112 sip_socket_select();
113 if (mgw_is_connected && FD_ISSET(mgw_socket, &fds))
114 mgw_socket_select();
115 } else if (need_retrans)
116 run_periodic_retrans();
117 clear_dead_calls();
118 }
119 }