annotate sip-in/main.c @ 160:4b35a5a400f1

mgw/dtmf_defs.h: reduce DTMF_MIN_FRAMES to 5 (100 ms)
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 12 Oct 2022 13:05:26 -0800
parents 6aa63cf4620a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * Main module for themwi-sip-in.
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/time.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/errno.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdlib.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <string.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <strings.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <signal.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <syslog.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <unistd.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
68
709b78a4ebf0 sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents: 56
diff changeset
16 extern unsigned cfg_retrans_timeout;
709b78a4ebf0 sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents: 56
diff changeset
17
56
7005d5c535e8 sip-in: mtcall socket connection implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
18 extern int gsm_socket, gsm_is_connected;
107
372209628038 sip-in: handle themwi-mgw shutdown without terminating
Mychaela Falconia <falcon@freecalypso.org>
parents: 68
diff changeset
19 extern int mgw_socket, mgw_is_connected;
372209628038 sip-in: handle themwi-mgw shutdown without terminating
Mychaela Falconia <falcon@freecalypso.org>
parents: 68
diff changeset
20 extern int sip_socket;
47
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 static int max_fd;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 struct timeval cur_event_time;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 update_max_fd(newfd)
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 {
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 if (newfd > max_fd)
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 max_fd = newfd;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 }
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 main(argc, argv)
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 char **argv;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 {
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 fd_set fds;
112
6aa63cf4620a sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 111
diff changeset
36 int rc, need_retrans, dead_sip_flag;
68
709b78a4ebf0 sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents: 56
diff changeset
37 struct timeval timeout;
112
6aa63cf4620a sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 111
diff changeset
38 time_t dead_sip_time;
47
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 openlog("themwi-sip-in", 0, LOG_LOCAL5);
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 read_config_file();
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 if (read_number_db() < 0) {
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 fprintf(stderr, "error reading number database\n");
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 exit(1);
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 }
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (open_sip_udp_socket() < 0) {
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 fprintf(stderr, "error opening SIP UDP socket\n");
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 exit(1);
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 }
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (argv[1]) {
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 rc = open_sip_log_file(argv[1]);
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (rc < 0)
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 exit(1); /* error msg already printed */
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 }
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 signal(SIGPIPE, SIG_IGN);
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 /* main select loop */
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 for (;;) {
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 FD_ZERO(&fds);
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 FD_SET(sip_socket, &fds);
56
7005d5c535e8 sip-in: mtcall socket connection implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
60 if (gsm_is_connected)
7005d5c535e8 sip-in: mtcall socket connection implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
61 FD_SET(gsm_socket, &fds);
107
372209628038 sip-in: handle themwi-mgw shutdown without terminating
Mychaela Falconia <falcon@freecalypso.org>
parents: 68
diff changeset
62 if (mgw_is_connected)
372209628038 sip-in: handle themwi-mgw shutdown without terminating
Mychaela Falconia <falcon@freecalypso.org>
parents: 68
diff changeset
63 FD_SET(mgw_socket, &fds);
112
6aa63cf4620a sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 111
diff changeset
64 need_retrans = dead_sip_flag = 0;
6aa63cf4620a sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 111
diff changeset
65 scan_call_list_for_timeouts(&need_retrans, &dead_sip_flag,
6aa63cf4620a sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 111
diff changeset
66 &dead_sip_time);
68
709b78a4ebf0 sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents: 56
diff changeset
67 if (need_retrans) {
709b78a4ebf0 sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents: 56
diff changeset
68 timeout.tv_sec = cfg_retrans_timeout / 1000;
709b78a4ebf0 sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents: 56
diff changeset
69 timeout.tv_usec = (cfg_retrans_timeout % 1000) * 1000;
709b78a4ebf0 sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents: 56
diff changeset
70 rc = select(max_fd+1, &fds, 0, 0, &timeout);
112
6aa63cf4620a sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 111
diff changeset
71 } else if (dead_sip_flag) {
6aa63cf4620a sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 111
diff changeset
72 if (cur_event_time.tv_sec >= dead_sip_time)
6aa63cf4620a sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 111
diff changeset
73 timeout.tv_sec = 0;
6aa63cf4620a sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 111
diff changeset
74 else
6aa63cf4620a sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 111
diff changeset
75 timeout.tv_sec =
6aa63cf4620a sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 111
diff changeset
76 dead_sip_time - cur_event_time.tv_sec;
6aa63cf4620a sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 111
diff changeset
77 timeout.tv_usec = 0;
6aa63cf4620a sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 111
diff changeset
78 rc = select(max_fd+1, &fds, 0, 0, &timeout);
68
709b78a4ebf0 sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents: 56
diff changeset
79 } else
709b78a4ebf0 sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents: 56
diff changeset
80 rc = select(max_fd+1, &fds, 0, 0, 0);
47
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 if (rc < 0) {
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 if (errno == EINTR)
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 continue;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 syslog(LOG_CRIT, "select: %m");
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 exit(1);
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 }
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 gettimeofday(&cur_event_time, 0);
111
7a4d4b8d5f04 sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 107
diff changeset
88 if (rc) {
7a4d4b8d5f04 sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 107
diff changeset
89 if (gsm_is_connected && FD_ISSET(gsm_socket, &fds))
7a4d4b8d5f04 sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 107
diff changeset
90 gsm_socket_select();
7a4d4b8d5f04 sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 107
diff changeset
91 if (mgw_is_connected && FD_ISSET(mgw_socket, &fds))
7a4d4b8d5f04 sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 107
diff changeset
92 mgw_socket_select();
7a4d4b8d5f04 sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 107
diff changeset
93 if (FD_ISSET(sip_socket, &fds))
7a4d4b8d5f04 sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 107
diff changeset
94 sip_socket_select();
7a4d4b8d5f04 sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 107
diff changeset
95 } else if (need_retrans)
68
709b78a4ebf0 sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents: 56
diff changeset
96 run_periodic_retrans();
111
7a4d4b8d5f04 sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 107
diff changeset
97 clear_dead_sip_calls();
47
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 }
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 }