comparison mncc/mtsock.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
comparison
equal deleted inserted replaced
1:b161dbfffdaa 2:053f04687106
1 /*
2 * In this module we implement the MT call socket
3 * to which other ThemWi system sw components connect
4 * in order to send MT calls toward the GSM network.
5 */
6
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <strings.h>
16 #include <syslog.h>
17 #include <unistd.h>
18 #include "../include/mncc.h"
19 #include "struct.h"
20
21 static char mtcall_socket_pathname[] = "/var/gsm/mtcall_socket";
22
23 int mtcall_listener;
24 struct socket_conn *mtcall_socket_head;
25
26 create_mtcall_socket()
27 {
28 struct sockaddr_un sa;
29 unsigned sa_len;
30 int rc;
31
32 mtcall_listener = socket(AF_UNIX, SOCK_SEQPACKET, 0);
33 if (mtcall_listener < 0) {
34 syslog(LOG_CRIT, "socket(AF_UNIX, SOCK_SEQPACKET, 0): %m");
35 return(-1);
36 }
37 unlink(mtcall_socket_pathname);
38 fill_sockaddr_un(mtcall_socket_pathname, &sa, &sa_len);
39 rc = bind(mtcall_listener, (struct sockaddr *) &sa, sa_len);
40 if (rc < 0) {
41 syslog(LOG_ERR, "bind to %s: %m", mtcall_socket_pathname);
42 return(-1);
43 }
44 rc = listen(mtcall_listener, 3);
45 if (rc < 0) {
46 syslog(LOG_CRIT, "listen on UNIX socket: %m");
47 return(-1);
48 }
49 chmod(mtcall_socket_pathname, 0775);
50 update_max_fd(mtcall_listener);
51 return(0);
52 }
53
54 void
55 mtsock_accept_handler()
56 {
57 struct sockaddr_un sa;
58 socklen_t sa_len;
59 int fd;
60 struct socket_conn *conn;
61
62 sa_len = sizeof sa;
63 fd = accept(mtcall_listener, (struct sockaddr *) &sa, &sa_len);
64 if (fd < 0) {
65 syslog(LOG_CRIT, "accept on UNIX socket: %m");
66 exit(1);
67 }
68 conn = malloc(sizeof(struct socket_conn));
69 if (!conn) {
70 syslog(LOG_CRIT, "malloc for mtcall socket conn: %m");
71 close(fd);
72 return;
73 }
74 syslog(LOG_INFO, "new MT call socket connection");
75 bzero(conn, sizeof(struct socket_conn));
76 conn->fd = fd;
77 conn->next = mtcall_socket_head;
78 mtcall_socket_head = conn;
79 update_max_fd(fd);
80 }