FreeCalypso > hg > themwi-system-sw
annotate sip-in/main.c @ 95:f280328e7e2e
themwi-mgw: initial implementation of PSTN to GSM forwarding
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 25 Sep 2022 19:17:44 -0800 |
parents | 709b78a4ebf0 |
children | 372209628038 |
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 |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 extern int mgw_socket, sip_socket; |
56
7005d5c535e8
sip-in: mtcall socket connection implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
47
diff
changeset
|
19 extern int gsm_socket, gsm_is_connected; |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 static int max_fd; |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 struct timeval cur_event_time; |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 update_max_fd(newfd) |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 if (newfd > max_fd) |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 max_fd = newfd; |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 } |
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 main(argc, argv) |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 char **argv; |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 fd_set fds; |
68
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
35 int rc, need_retrans; |
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
36 struct timeval timeout; |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 openlog("themwi-sip-in", 0, LOG_LOCAL5); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 read_config_file(); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 if (read_number_db() < 0) { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 fprintf(stderr, "error reading number database\n"); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 exit(1); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 } |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 if (open_tmgw_socket() < 0) { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 fprintf(stderr, "error connecting to themwi-mgw socket\n"); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 exit(1); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 } |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 if (open_sip_udp_socket() < 0) { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 fprintf(stderr, "error opening SIP UDP socket\n"); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 exit(1); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 } |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 if (argv[1]) { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 rc = open_sip_log_file(argv[1]); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 if (rc < 0) |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 exit(1); /* error msg already printed */ |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 } |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 signal(SIGPIPE, SIG_IGN); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 /* main select loop */ |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 for (;;) { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 FD_ZERO(&fds); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 FD_SET(mgw_socket, &fds); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 FD_SET(sip_socket, &fds); |
56
7005d5c535e8
sip-in: mtcall socket connection implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
47
diff
changeset
|
63 if (gsm_is_connected) |
7005d5c535e8
sip-in: mtcall socket connection implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
47
diff
changeset
|
64 FD_SET(gsm_socket, &fds); |
68
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
65 need_retrans = 0; |
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
66 scan_call_list_for_timeouts(&need_retrans); |
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); |
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
71 } else |
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
72 rc = select(max_fd+1, &fds, 0, 0, 0); |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 if (rc < 0) { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 if (errno == EINTR) |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 continue; |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 syslog(LOG_CRIT, "select: %m"); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 exit(1); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 } |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 gettimeofday(&cur_event_time, 0); |
68
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
80 if (rc == 0) { |
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
81 run_periodic_retrans(); |
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
82 continue; |
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
83 } |
56
7005d5c535e8
sip-in: mtcall socket connection implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
47
diff
changeset
|
84 if (gsm_is_connected && FD_ISSET(gsm_socket, &fds)) |
7005d5c535e8
sip-in: mtcall socket connection implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
47
diff
changeset
|
85 gsm_socket_select(); |
7005d5c535e8
sip-in: mtcall socket connection implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
47
diff
changeset
|
86 if (FD_ISSET(sip_socket, &fds)) |
7005d5c535e8
sip-in: mtcall socket connection implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
47
diff
changeset
|
87 sip_socket_select(); |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 if (FD_ISSET(mgw_socket, &fds)) |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 mgw_socket_select(); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 } |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 } |