FreeCalypso > hg > themwi-system-sw
annotate sip-out/main.c @ 268:d6630a2d6e80
doc: beginning of numbering plan documentation
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 13 Nov 2023 18:20:57 -0800 |
parents | cb440d003976 |
children |
rev | line source |
---|---|
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
2 * Main module for themwi-sip-out. |
47
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> |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
6 #include <sys/socket.h> |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <sys/time.h> |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <sys/errno.h> |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
9 #include <netinet/in.h> |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <stdio.h> |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <stdlib.h> |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <string.h> |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <strings.h> |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 #include <signal.h> |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 #include <syslog.h> |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 #include <unistd.h> |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
17 #include "../include/out_routes.h" |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
18 #include "call.h" |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 |
68
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
20 extern unsigned cfg_retrans_timeout; |
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
21 |
107
372209628038
sip-in: handle themwi-mgw shutdown without terminating
Mychaela Falconia <falcon@freecalypso.org>
parents:
68
diff
changeset
|
22 extern int mgw_socket, mgw_is_connected; |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
23 extern int sip_socket, mncc_listener; |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
24 extern struct mncc_conn *mncc_conn_list; |
47
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 static int max_fd; |
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 struct timeval cur_event_time; |
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 update_max_fd(newfd) |
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 if (newfd > max_fd) |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 max_fd = newfd; |
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 |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 main(argc, argv) |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 char **argv; |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 fd_set fds; |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
40 struct mncc_conn *conn, **connp; |
112
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
41 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
|
42 struct timeval timeout; |
112
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
43 time_t dead_sip_time; |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
45 openlog("themwi-sip-out", 0, LOG_LOCAL5); |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 read_config_file(); |
257
cb440d003976
sip-out: add libnumdb2 infra in prep for E911 routing
Mychaela Falconia <falcon@freecalypso.org>
parents:
154
diff
changeset
|
47 if (read_number_db() < 0) { |
cb440d003976
sip-out: add libnumdb2 infra in prep for E911 routing
Mychaela Falconia <falcon@freecalypso.org>
parents:
154
diff
changeset
|
48 fprintf(stderr, "error reading number database\n"); |
cb440d003976
sip-out: add libnumdb2 infra in prep for E911 routing
Mychaela Falconia <falcon@freecalypso.org>
parents:
154
diff
changeset
|
49 exit(1); |
cb440d003976
sip-out: add libnumdb2 infra in prep for E911 routing
Mychaela Falconia <falcon@freecalypso.org>
parents:
154
diff
changeset
|
50 } |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
51 if (read_out_routes_db() < 0) { |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
52 fprintf(stderr, "error reading out-routes.bin database\n"); |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
53 exit(1); |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
54 } |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
55 if (create_outcall_socket() < 0) { |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
56 fprintf(stderr, "error creating outcall socket\n"); |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 exit(1); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 } |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 if (open_sip_udp_socket() < 0) { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 fprintf(stderr, "error opening SIP UDP socket\n"); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 exit(1); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 } |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 if (argv[1]) { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 rc = open_sip_log_file(argv[1]); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 if (rc < 0) |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 exit(1); /* error msg already printed */ |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 } |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 signal(SIGPIPE, SIG_IGN); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 /* main select loop */ |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 for (;;) { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 FD_ZERO(&fds); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 FD_SET(sip_socket, &fds); |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
73 FD_SET(mncc_listener, &fds); |
107
372209628038
sip-in: handle themwi-mgw shutdown without terminating
Mychaela Falconia <falcon@freecalypso.org>
parents:
68
diff
changeset
|
74 if (mgw_is_connected) |
372209628038
sip-in: handle themwi-mgw shutdown without terminating
Mychaela Falconia <falcon@freecalypso.org>
parents:
68
diff
changeset
|
75 FD_SET(mgw_socket, &fds); |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
76 for (connp = &mncc_conn_list; conn = *connp; ) { |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
77 if (conn->fd < 0) { |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
78 *connp = conn->next; |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
79 free(conn); |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
80 continue; |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
81 } |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
82 FD_SET(conn->fd, &fds); |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
83 connp = &conn->next; |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
84 } |
112
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
85 need_retrans = dead_sip_flag = 0; |
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
86 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
|
87 &dead_sip_time); |
68
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
88 if (need_retrans) { |
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
89 timeout.tv_sec = cfg_retrans_timeout / 1000; |
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
90 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
|
91 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
|
92 } else if (dead_sip_flag) { |
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
93 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
|
94 timeout.tv_sec = 0; |
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
95 else |
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
96 timeout.tv_sec = |
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
97 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
|
98 timeout.tv_usec = 0; |
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
99 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
|
100 } else |
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
101 rc = select(max_fd+1, &fds, 0, 0, 0); |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 if (rc < 0) { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 if (errno == EINTR) |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 continue; |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 syslog(LOG_CRIT, "select: %m"); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 exit(1); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 } |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 gettimeofday(&cur_event_time, 0); |
111
7a4d4b8d5f04
sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
107
diff
changeset
|
109 if (rc) { |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
110 if (FD_ISSET(mncc_listener, &fds)) |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
111 mncc_listener_select(); |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
112 for (conn = mncc_conn_list; conn; conn = conn->next) |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
113 if (FD_ISSET(conn->fd, &fds)) |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
114 mncc_socket_select(conn); |
111
7a4d4b8d5f04
sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
107
diff
changeset
|
115 if (FD_ISSET(sip_socket, &fds)) |
7a4d4b8d5f04
sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
107
diff
changeset
|
116 sip_socket_select(); |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
117 if (mgw_is_connected && FD_ISSET(mgw_socket, &fds)) |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
118 mgw_socket_select(); |
111
7a4d4b8d5f04
sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
107
diff
changeset
|
119 } else if (need_retrans) |
68
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
120 run_periodic_retrans(); |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
121 clear_dead_calls(); |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
122 } |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
123 } |