comparison mgw/ctrl_sock.c @ 32:b3f74df7b808

beginning of themwi-mgw
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 09 Jul 2022 22:51:44 -0800
parents
children db7ed6a55ba4
comparison
equal deleted inserted replaced
31:08d7794cdd0a 32:b3f74df7b808
1 /*
2 * In this module we implement the logic of listening on the
3 * TMGW control socket and accepting control connections.
4 */
5
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <sys/un.h>
9 #include <netinet/in.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <strings.h>
14 #include <syslog.h>
15 #include <unistd.h>
16 #include "struct.h"
17 #include "select.h"
18
19 static char ctrl_socket_pathname[] = "/var/gsm/tmgw_socket";
20
21 extern void ctrl_message_handler();
22
23 void
24 ctrlsock_accept_handler(listener_fd)
25 {
26 struct sockaddr_un sa;
27 socklen_t sa_len;
28 int conn_fd;
29 struct ctrl_conn *conn;
30
31 sa_len = sizeof sa;
32 conn_fd = accept(listener_fd, (struct sockaddr *) &sa, &sa_len);
33 if (conn_fd < 0) {
34 syslog(LOG_CRIT, "accept on UNIX socket: %m");
35 exit(1);
36 }
37 conn = malloc(sizeof(struct ctrl_conn));
38 if (!conn) {
39 syslog(LOG_CRIT, "malloc for ctrl socket conn: %m");
40 close(conn_fd);
41 return;
42 }
43 bzero(conn, sizeof(struct ctrl_conn));
44 update_max_fd(conn_fd);
45 FD_SET(conn_fd, &select_for_read);
46 select_handlers[conn_fd] = ctrl_message_handler;
47 select_data[conn_fd] = (void *) conn;
48 syslog(LOG_INFO, "accepted ctrl connection");
49 }
50
51 create_ctrl_socket()
52 {
53 struct sockaddr_un sa;
54 unsigned sa_len;
55 int fd, rc;
56
57 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
58 if (fd < 0) {
59 syslog(LOG_CRIT, "socket(AF_UNIX, SOCK_SEQPACKET, 0): %m");
60 return(-1);
61 }
62 unlink(ctrl_socket_pathname);
63 fill_sockaddr_un(ctrl_socket_pathname, &sa, &sa_len);
64 rc = bind(fd, (struct sockaddr *) &sa, sa_len);
65 if (rc < 0) {
66 syslog(LOG_ERR, "bind to %s: %m", ctrl_socket_pathname);
67 return(-1);
68 }
69 rc = listen(fd, 3);
70 if (rc < 0) {
71 syslog(LOG_CRIT, "listen on UNIX socket: %m");
72 return(-1);
73 }
74 update_max_fd(fd);
75 FD_SET(fd, &select_for_read);
76 select_handlers[fd] = ctrlsock_accept_handler;
77 return(0);
78 }