FreeCalypso > hg > themwi-system-sw
annotate sip-out/main.c @ 217:9f6a148ceb25
tcpserv-dump: brown paper bag
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 24 Jul 2023 22:33:01 -0800 |
parents | e54b0a9e322f |
children | cb440d003976 |
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(); |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
47 if (read_out_routes_db() < 0) { |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
48 fprintf(stderr, "error reading out-routes.bin database\n"); |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
49 exit(1); |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
50 } |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
51 if (create_outcall_socket() < 0) { |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
52 fprintf(stderr, "error creating outcall socket\n"); |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 exit(1); |
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 if (open_sip_udp_socket() < 0) { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 fprintf(stderr, "error opening SIP UDP socket\n"); |
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 (argv[1]) { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 rc = open_sip_log_file(argv[1]); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 if (rc < 0) |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 exit(1); /* error msg already printed */ |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 } |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 signal(SIGPIPE, SIG_IGN); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 /* main select loop */ |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 for (;;) { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 FD_ZERO(&fds); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 FD_SET(sip_socket, &fds); |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
69 FD_SET(mncc_listener, &fds); |
107
372209628038
sip-in: handle themwi-mgw shutdown without terminating
Mychaela Falconia <falcon@freecalypso.org>
parents:
68
diff
changeset
|
70 if (mgw_is_connected) |
372209628038
sip-in: handle themwi-mgw shutdown without terminating
Mychaela Falconia <falcon@freecalypso.org>
parents:
68
diff
changeset
|
71 FD_SET(mgw_socket, &fds); |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
72 for (connp = &mncc_conn_list; conn = *connp; ) { |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
73 if (conn->fd < 0) { |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
74 *connp = conn->next; |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
75 free(conn); |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
76 continue; |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
77 } |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
78 FD_SET(conn->fd, &fds); |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
79 connp = &conn->next; |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
80 } |
112
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
81 need_retrans = dead_sip_flag = 0; |
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
82 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
|
83 &dead_sip_time); |
68
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
84 if (need_retrans) { |
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
85 timeout.tv_sec = cfg_retrans_timeout / 1000; |
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
86 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
|
87 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
|
88 } else if (dead_sip_flag) { |
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
89 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
|
90 timeout.tv_sec = 0; |
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
91 else |
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
92 timeout.tv_sec = |
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
93 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
|
94 timeout.tv_usec = 0; |
6aa63cf4620a
sip-in call clearing: select timeout implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
111
diff
changeset
|
95 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
|
96 } else |
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
97 rc = select(max_fd+1, &fds, 0, 0, 0); |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 if (rc < 0) { |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 if (errno == EINTR) |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 continue; |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 syslog(LOG_CRIT, "select: %m"); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 exit(1); |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 } |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 gettimeofday(&cur_event_time, 0); |
111
7a4d4b8d5f04
sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
107
diff
changeset
|
105 if (rc) { |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
106 if (FD_ISSET(mncc_listener, &fds)) |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
107 mncc_listener_select(); |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
108 for (conn = mncc_conn_list; conn; conn = conn->next) |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
109 if (FD_ISSET(conn->fd, &fds)) |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
110 mncc_socket_select(conn); |
111
7a4d4b8d5f04
sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
107
diff
changeset
|
111 if (FD_ISSET(sip_socket, &fds)) |
7a4d4b8d5f04
sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
107
diff
changeset
|
112 sip_socket_select(); |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
113 if (mgw_is_connected && FD_ISSET(mgw_socket, &fds)) |
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
114 mgw_socket_select(); |
111
7a4d4b8d5f04
sip-in call clearing: unlink and free implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
107
diff
changeset
|
115 } else if (need_retrans) |
68
709b78a4ebf0
sip-in: implement retransmission of INVITE responses
Mychaela Falconia <falcon@freecalypso.org>
parents:
56
diff
changeset
|
116 run_periodic_retrans(); |
154
e54b0a9e322f
beginning of themwi-sip-out
Mychaela Falconia <falcon@freecalypso.org>
parents:
112
diff
changeset
|
117 clear_dead_calls(); |
47
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
118 } |
62f39c7cee15
themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
119 } |