comparison mncc/main.c @ 2:053f04687106

mncc: initial import from old ThemWi
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 08 Jun 2024 23:12:12 +0000
parents
children 847690ea7f4a
comparison
equal deleted inserted replaced
1:b161dbfffdaa 2:053f04687106
1 /*
2 * Main module for ThemWi MNCC daemon.
3 */
4
5 #include <sys/types.h>
6 #include <sys/errno.h>
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <strings.h>
12 #include <signal.h>
13 #include <syslog.h>
14 #include <unistd.h>
15 #include "struct.h"
16
17 extern int mncc_socket;
18 extern int mtcall_listener;
19 extern struct socket_conn *mtcall_socket_head;
20
21 struct socket_conn outcall_conn;
22
23 static int max_fd;
24
25 update_max_fd(newfd)
26 {
27 if (newfd > max_fd)
28 max_fd = newfd;
29 }
30
31 main(argc, argv)
32 char **argv;
33 {
34 fd_set fds;
35 struct socket_conn *conn, **connp;
36 int c;
37
38 openlog("themwi-mncc", 0, LOG_LOCAL5);
39 if (read_number_db() < 0) {
40 fprintf(stderr, "error reading number database\n");
41 exit(1);
42 }
43 if (open_mncc_socket() < 0) {
44 fprintf(stderr, "error connecting to GSM MNCC socket\n");
45 exit(1);
46 }
47 if (create_mtcall_socket() < 0) {
48 fprintf(stderr, "error creating MT call socket\n");
49 exit(1);
50 }
51 signal(SIGPIPE, SIG_IGN);
52 outcall_conn.fd = -1;
53 /* main select loop */
54 for (;;) {
55 FD_ZERO(&fds);
56 FD_SET(mncc_socket, &fds);
57 FD_SET(mtcall_listener, &fds);
58 for (connp = &mtcall_socket_head; conn = *connp; ) {
59 if (conn->fd < 0) {
60 *connp = conn->next;
61 free(conn);
62 continue;
63 }
64 FD_SET(conn->fd, &fds);
65 connp = &conn->next;
66 }
67 if (outcall_conn.fd >= 0)
68 FD_SET(outcall_conn.fd, &fds);
69 c = select(max_fd+1, &fds, 0, 0, 0);
70 if (c < 0) {
71 if (errno == EINTR)
72 continue;
73 syslog(LOG_CRIT, "select: %m");
74 exit(1);
75 }
76 if (FD_ISSET(mncc_socket, &fds))
77 mncc_socket_select();
78 if (FD_ISSET(mtcall_listener, &fds))
79 mtsock_accept_handler();
80 for (conn = mtcall_socket_head; conn; conn = conn->next)
81 if (FD_ISSET(conn->fd, &fds))
82 extsock_read_select(conn);
83 if (outcall_conn.fd >= 0 && FD_ISSET(outcall_conn.fd, &fds))
84 extsock_read_select(&outcall_conn);
85 gc_call_list();
86 }
87 }