FreeCalypso > hg > themwi-system-sw
comparison sip-out/mncc_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 | mncc/mtsock.c@db7ed6a55ba4 |
children |
comparison
equal
deleted
inserted
replaced
153:99fd4ae573ae | 154:e54b0a9e322f |
---|---|
1 /* | |
2 * In this module we implement the MNCC socket interface to themwi-sip-out. | |
3 * We have an outcall socket on which we accept connections, and each | |
4 * accepted connection becomes a struct mncc_conn for us. | |
5 */ | |
6 | |
7 #include <sys/types.h> | |
8 #include <sys/stat.h> | |
9 #include <sys/socket.h> | |
10 #include <sys/un.h> | |
11 #include <sys/time.h> | |
12 #include <netinet/in.h> | |
13 #include <stdio.h> | |
14 #include <stdint.h> | |
15 #include <stdlib.h> | |
16 #include <string.h> | |
17 #include <strings.h> | |
18 #include <syslog.h> | |
19 #include <unistd.h> | |
20 #include "../include/mncc.h" | |
21 #include "../include/out_routes.h" | |
22 #include "call.h" | |
23 | |
24 static char outcall_socket_pathname[] = "/var/gsm/outcall_socket"; | |
25 | |
26 int mncc_listener; | |
27 struct mncc_conn *mncc_conn_list; | |
28 | |
29 create_outcall_socket() | |
30 { | |
31 struct sockaddr_un sa; | |
32 unsigned sa_len; | |
33 int rc; | |
34 | |
35 mncc_listener = socket(AF_UNIX, SOCK_SEQPACKET, 0); | |
36 if (mncc_listener < 0) { | |
37 syslog(LOG_CRIT, "socket(AF_UNIX, SOCK_SEQPACKET, 0): %m"); | |
38 return(-1); | |
39 } | |
40 unlink(outcall_socket_pathname); | |
41 fill_sockaddr_un(outcall_socket_pathname, &sa, &sa_len); | |
42 rc = bind(mncc_listener, (struct sockaddr *) &sa, sa_len); | |
43 if (rc < 0) { | |
44 syslog(LOG_ERR, "bind to %s: %m", outcall_socket_pathname); | |
45 return(-1); | |
46 } | |
47 rc = listen(mncc_listener, 3); | |
48 if (rc < 0) { | |
49 syslog(LOG_CRIT, "listen on UNIX socket: %m"); | |
50 return(-1); | |
51 } | |
52 chmod(outcall_socket_pathname, 0775); | |
53 update_max_fd(mncc_listener); | |
54 return(0); | |
55 } | |
56 | |
57 void | |
58 mncc_listener_select() | |
59 { | |
60 struct sockaddr_un sa; | |
61 socklen_t sa_len; | |
62 int fd; | |
63 struct mncc_conn *conn; | |
64 | |
65 sa_len = sizeof sa; | |
66 fd = accept(mncc_listener, (struct sockaddr *) &sa, &sa_len); | |
67 if (fd < 0) { | |
68 syslog(LOG_CRIT, "accept on UNIX socket: %m"); | |
69 exit(1); | |
70 } | |
71 conn = malloc(sizeof(struct mncc_conn)); | |
72 if (!conn) { | |
73 syslog(LOG_CRIT, "malloc for outcall socket conn: %m"); | |
74 close(fd); | |
75 return; | |
76 } | |
77 syslog(LOG_INFO, "new outcall socket connection"); | |
78 conn->fd = fd; | |
79 conn->next = mncc_conn_list; | |
80 mncc_conn_list = conn; | |
81 update_max_fd(fd); | |
82 } | |
83 | |
84 static void | |
85 dispatch_mncc_msg(mncc, msg, msglen) | |
86 struct mncc_conn *mncc; | |
87 union mncc_msg *msg; | |
88 unsigned msglen; | |
89 { | |
90 switch (msg->msg_type) { | |
91 case MNCC_SETUP_IND: | |
92 handle_setup_ind(mncc, msg, msglen); | |
93 return; | |
94 case MNCC_SETUP_COMPL_IND: | |
95 case MNCC_NOTIFY_IND: | |
96 case MNCC_DISC_IND: | |
97 case MNCC_FACILITY_IND: | |
98 case MNCC_START_DTMF_IND: | |
99 case MNCC_STOP_DTMF_IND: | |
100 case MNCC_MODIFY_IND: | |
101 case MNCC_HOLD_IND: | |
102 case MNCC_RETRIEVE_IND: | |
103 case MNCC_USERINFO_IND: | |
104 case MNCC_REL_IND: | |
105 case MNCC_REL_CNF: | |
106 handle_mncc_signal(mncc, msg, msglen); | |
107 return; | |
108 case MNCC_RTP_CREATE: | |
109 handle_rtp_create(mncc, msg, msglen); | |
110 return; | |
111 case MNCC_RTP_CONNECT: | |
112 syslog(LOG_ERR, "MNCC_RTP_CONNECT error from OsmoMSC"); | |
113 return; | |
114 case MNCC_RTP_FREE: | |
115 syslog(LOG_ERR, "MNCC_RTP_FREE bogon from OsmoMSC"); | |
116 return; | |
117 default: | |
118 syslog(LOG_CRIT, | |
119 "FATAL: received unexpected MNCC message type 0x%x", | |
120 msg->msg_type); | |
121 exit(1); | |
122 } | |
123 } | |
124 | |
125 void | |
126 mncc_socket_select(conn) | |
127 struct mncc_conn *conn; | |
128 { | |
129 union mncc_msg msg; | |
130 int rc; | |
131 | |
132 rc = recv(conn->fd, &msg, sizeof msg, 0); | |
133 if (rc <= 0) { | |
134 syslog(LOG_ERR, "outcall socket disconnected"); | |
135 close(conn->fd); | |
136 conn->fd = -1; | |
137 shutdown_mncc_socket(conn); | |
138 return; | |
139 } | |
140 if (rc < 4) { | |
141 syslog(LOG_CRIT, "short read from MNCC socket: %d bytes", rc); | |
142 exit(1); | |
143 } | |
144 dispatch_mncc_msg(conn, &msg, rc); | |
145 } | |
146 | |
147 send_mncc_to_sock(mncc, msg, msglen) | |
148 struct mncc_conn *mncc; | |
149 union mncc_msg *msg; | |
150 unsigned msglen; | |
151 { | |
152 return send(mncc->fd, msg, msglen, 0); | |
153 } |