comparison mncc/mtsock.c @ 15:ccc5ab6d8388

first version of themwi-mncc for ThemWi2
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 26 Jun 2022 16:31:47 -0800
parents
children fd43f179ff1d
comparison
equal deleted inserted replaced
14:aea422af79dd 15:ccc5ab6d8388
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/socket.h>
9 #include <sys/un.h>
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <strings.h>
15 #include <syslog.h>
16 #include "../include/mncc.h"
17 #include "struct.h"
18
19 static char mtcall_socket_pathname[] = "/var/gsm/mtcall_socket";
20
21 int mtcall_listener;
22 struct socket_conn *mtcall_socket_head;
23
24 create_mtcall_socket()
25 {
26 struct sockaddr_un sa;
27 unsigned sa_len;
28 int rc;
29
30 mtcall_listener = socket(AF_UNIX, SOCK_SEQPACKET, 0);
31 if (mtcall_listener < 0) {
32 syslog(LOG_CRIT, "socket(AF_UNIX, SOCK_SEQPACKET, 0): %m");
33 return(-1);
34 }
35 unlink(mtcall_socket_pathname);
36 fill_sockaddr_un(mtcall_socket_pathname, &sa, &sa_len);
37 rc = bind(mtcall_listener, (struct sockaddr *) &sa, sa_len);
38 if (rc < 0) {
39 syslog(LOG_ERR, "bind to %s: %m", mtcall_socket_pathname);
40 return(-1);
41 }
42 rc = listen(mtcall_listener, 3);
43 if (rc < 0) {
44 syslog(LOG_CRIT, "listen on UNIX socket: %m");
45 return(-1);
46 }
47 update_max_fd(mtcall_listener);
48 return(0);
49 }
50
51 void
52 mtsock_accept_handler()
53 {
54 struct sockaddr_un sa;
55 socklen_t sa_len;
56 int fd;
57 struct socket_conn *conn;
58
59 fd = accept(mtcall_listener, (struct sockaddr *) &sa, &sa_len);
60 if (fd < 0) {
61 syslog(LOG_CRIT, "accept on UNIX socket: %m");
62 exit(1);
63 }
64 conn = malloc(sizeof(struct socket_conn));
65 if (!conn) {
66 syslog(LOG_CRIT, "malloc for mtcall socket conn: %m");
67 close(fd);
68 return;
69 }
70 bzero(conn, sizeof(struct socket_conn));
71 conn->fd = fd;
72 conn->next = mtcall_socket_head;
73 mtcall_socket_head = conn;
74 update_max_fd(fd);
75 }