comparison sip-out/mgw_sock.c @ 154:e54b0a9e322f

beginning of themwi-sip-out
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 11 Oct 2022 23:04:01 -0800
parents sip-in/mgw_sock.c@372209628038
children
comparison
equal deleted inserted replaced
153:99fd4ae573ae 154:e54b0a9e322f
1 /*
2 * In this module we implement our local socket interface to themwi-mgw.
3 */
4
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <sys/un.h>
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <syslog.h>
12 #include "../include/tmgw_ctrl.h"
13
14 static char tmgw_socket_pathname[] = "/var/gsm/tmgw_socket";
15
16 int mgw_socket, mgw_is_connected;
17
18 connect_tmgw_socket()
19 {
20 struct sockaddr_un sa;
21 unsigned sa_len;
22 int rc;
23
24 if (mgw_is_connected)
25 return(0);
26 mgw_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
27 if (mgw_socket < 0) {
28 syslog(LOG_CRIT, "socket(AF_UNIX, SOCK_SEQPACKET, 0): %m");
29 return(-1);
30 }
31 fill_sockaddr_un(tmgw_socket_pathname, &sa, &sa_len);
32 rc = connect(mgw_socket, (struct sockaddr *) &sa, sa_len);
33 if (rc < 0) {
34 syslog(LOG_ERR, "connect to %s: %m", tmgw_socket_pathname);
35 close(mgw_socket);
36 return(-1);
37 }
38 update_max_fd(mgw_socket);
39 mgw_is_connected = 1;
40 return(0);
41 }
42
43 void
44 mgw_socket_select()
45 {
46 struct tmgw_ctrl_resp msg;
47 int rc;
48
49 rc = recv(mgw_socket, &msg, sizeof msg, 0);
50 if (rc <= 0) {
51 syslog(LOG_ERR, "themwi-mgw socket disconnected");
52 close(mgw_socket);
53 mgw_is_connected = 0;
54 shutdown_mgw_conn();
55 return;
56 }
57 if (rc != sizeof(struct tmgw_ctrl_resp)) {
58 syslog(LOG_CRIT,
59 "response packet from TMGW has wrong length: %d bytes",
60 rc);
61 exit(1);
62 }
63 process_tmgw_response(&msg);
64 }
65
66 send_req_to_tmgw(msg)
67 struct tmgw_ctrl_req *msg;
68 {
69 return send(mgw_socket, msg, sizeof(struct tmgw_ctrl_req), 0);
70 }