annotate mgw/ctrl_sock.c @ 149:cfc249906145

mgw/ctrl_sock.c: #include <sys/stat.h> per chmod(2) man page
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 11 Oct 2022 13:48:39 -0800
parents f062c32a5116
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * In this module we implement the logic of listening on the
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * TMGW control socket and accepting control connections.
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
149
cfc249906145 mgw/ctrl_sock.c: #include <sys/stat.h> per chmod(2) man page
Mychaela Falconia <falcon@freecalypso.org>
parents: 127
diff changeset
7 #include <sys/stat.h>
127
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
8 #include <sys/time.h>
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <sys/socket.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <sys/un.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <netinet/in.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <stdio.h>
95
f280328e7e2e themwi-mgw: initial implementation of PSTN to GSM forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 35
diff changeset
13 #include <stdint.h>
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <stdlib.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include <string.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include <strings.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 #include <syslog.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 #include <unistd.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 #include "struct.h"
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 #include "select.h"
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 static char ctrl_socket_pathname[] = "/var/gsm/tmgw_socket";
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 extern void ctrl_message_handler();
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 void
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 ctrlsock_accept_handler(listener_fd)
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 {
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 struct sockaddr_un sa;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 socklen_t sa_len;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 int conn_fd;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 struct ctrl_conn *conn;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 sa_len = sizeof sa;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 conn_fd = accept(listener_fd, (struct sockaddr *) &sa, &sa_len);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (conn_fd < 0) {
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 syslog(LOG_CRIT, "accept on UNIX socket: %m");
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 exit(1);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 conn = malloc(sizeof(struct ctrl_conn));
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 if (!conn) {
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 syslog(LOG_CRIT, "malloc for ctrl socket conn: %m");
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 close(conn_fd);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 return;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 }
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 bzero(conn, sizeof(struct ctrl_conn));
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 update_max_fd(conn_fd);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 FD_SET(conn_fd, &select_for_read);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 select_handlers[conn_fd] = ctrl_message_handler;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 select_data[conn_fd] = (void *) conn;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 syslog(LOG_INFO, "accepted ctrl connection");
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 create_ctrl_socket()
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 {
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 struct sockaddr_un sa;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 unsigned sa_len;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 int fd, rc;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 if (fd < 0) {
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 syslog(LOG_CRIT, "socket(AF_UNIX, SOCK_SEQPACKET, 0): %m");
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 return(-1);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 }
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 unlink(ctrl_socket_pathname);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 fill_sockaddr_un(ctrl_socket_pathname, &sa, &sa_len);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 rc = bind(fd, (struct sockaddr *) &sa, sa_len);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 if (rc < 0) {
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 syslog(LOG_ERR, "bind to %s: %m", ctrl_socket_pathname);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 return(-1);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 }
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 rc = listen(fd, 3);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 if (rc < 0) {
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 syslog(LOG_CRIT, "listen on UNIX socket: %m");
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 return(-1);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 }
35
db7ed6a55ba4 themwi-{mgw,mncc}: chmod connection-accepting sockets to 775
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
77 chmod(ctrl_socket_pathname, 0775);
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 update_max_fd(fd);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 FD_SET(fd, &select_for_read);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 select_handlers[fd] = ctrlsock_accept_handler;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 return(0);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 }