blob: acd1666fa5ebaec5ff314d1eada672d76beb904d [file] [log] [blame]
Harald Welte31bbbf42010-12-23 00:02:51 +01001/* mncc_sock.c: Tie the MNCC interface to a unix domain socket */
2
3/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2009 by Andreas Eversberg <Andreas.Eversberg@versatel.de>
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <stdio.h>
24#include <unistd.h>
25#include <stdlib.h>
26#include <string.h>
27#include <errno.h>
Harald Welte49a2dde2010-12-22 22:56:15 +010028#include <assert.h>
Harald Welte31bbbf42010-12-23 00:02:51 +010029#include <sys/types.h>
30#include <sys/socket.h>
31#include <sys/un.h>
32
33#include <osmocore/talloc.h>
34#include <osmocore/select.h>
Harald Welteea057d92010-12-23 01:26:29 +010035#include <osmocore/protocol/gsm_04_08.h>
Harald Welte31bbbf42010-12-23 00:02:51 +010036
37#include <openbsc/debug.h>
38#include <openbsc/mncc.h>
39#include <openbsc/gsm_data.h>
40
41struct mncc_sock_state {
42 struct gsm_network *net;
43 struct bsc_fd listen_bfd; /* fd for listen socket */
44 struct bsc_fd conn_bfd; /* fd for connection to lcr */
45};
46
47/* FIXME: avoid this */
48static struct mncc_sock_state *g_state;
49
Harald Welteea057d92010-12-23 01:26:29 +010050/* input from CC code into mncc_sock */
51void mncc_sock_from_cc(struct gsm_network *net, struct msgb *msg)
52{
53 struct gsm_mncc *mncc_in = msgb_data(msg);
54 int msg_type = mncc_in->msg_type;
55
56 /* Check if we currently have a MNCC handler connected */
57 if (g_state->conn_bfd.fd < 0) {
58 LOGP(DMNCC, LOGL_ERROR, "mncc_sock receives %s for external CC app "
59 "but socket is gone\n", get_mncc_name(msg_type));
60 if (msg_type != GSM_TCHF_FRAME &&
61 msg_type != GSM_TCHF_FRAME_EFR) {
62 /* release the request */
63 struct gsm_mncc mncc_out;
64 memset(&mncc_out, 0, sizeof(mncc_out));
65 mncc_out.callref = mncc_in->callref;
66 mncc_set_cause(&mncc_out, GSM48_CAUSE_LOC_PRN_S_LU,
67 GSM48_CC_CAUSE_TEMP_FAILURE);
68 mncc_tx_to_cc(net, MNCC_REL_REQ, &mncc_out);
69 }
70 /* free the original message */
71 msgb_free(msg);
72 return;
73 }
74
75 /* FIXME: check for some maximum queue depth? */
76
77 /* Actually enqueue the message and mark socket write need */
78 msgb_enqueue(&net->upqueue, msg);
79 g_state->conn_bfd.when |= BSC_FD_WRITE;
80}
81
Harald Welte31bbbf42010-12-23 00:02:51 +010082void mncc_sock_write_pending(void)
83{
84 g_state->conn_bfd.when |= BSC_FD_WRITE;
85}
86
87/* FIXME: move this to libosmocore */
88int osmo_unixsock_listen(struct bsc_fd *bfd, int type, const char *path);
89
90static void mncc_sock_close(struct mncc_sock_state *state)
91{
92 struct bsc_fd *bfd = &state->conn_bfd;
93
94 close(bfd->fd);
95 bfd->fd = -1;
96 bsc_unregister_fd(bfd);
97
98 /* re-enable the generation of ACCEPT for new connections */
99 state->listen_bfd.when |= BSC_FD_READ;
100
101 /* FIXME: make sure we don't enqueue anymore */
Harald Welte371efe52010-12-22 23:17:50 +0100102
103 /* release all exisitng calls */
104 gsm0408_clear_all_trans(state->net, GSM48_PDISC_CC);
Harald Welte31bbbf42010-12-23 00:02:51 +0100105
106 /* flush the queue */
107 while (!llist_empty(&state->net->upqueue)) {
108 struct msgb *msg = msgb_dequeue(&state->net->upqueue);
109 msgb_free(msg);
110 }
111}
112
113static int mncc_sock_read(struct bsc_fd *bfd)
114{
115 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
116 struct gsm_mncc *mncc_prim;
117 struct msgb *msg;
118 int rc;
119
120 msg = msgb_alloc(sizeof(*mncc_prim)+256, "mncc_sock_rx");
121 if (!msg)
122 return -ENOMEM;
123
124 mncc_prim = (struct gsm_mncc *) msg->tail;
125
126 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
127 if (rc == 0)
128 goto close;
129
130 if (rc < 0) {
131 if (errno == EAGAIN)
132 return 0;
133 fprintf(stderr, "Err from socket: %s\n", strerror(errno));
134 goto close;
135 }
136
137 rc = mncc_tx_to_cc(state->net, mncc_prim->msg_type, mncc_prim);
138
139 /* as we always synchronously process the message in mncc_send() and
140 * its callbacks, we can free the message here. */
141 msgb_free(msg);
142
143 return rc;
144
145close:
146 mncc_sock_close(state);
147 return -1;
148}
149
150static int mncc_sock_write(struct bsc_fd *bfd)
151{
152 struct mncc_sock_state *state = bfd->data;
153 struct gsm_network *net = state->net;
154 int rc;
155
156 while (!llist_empty(&net->upqueue)) {
Harald Welte49a2dde2010-12-22 22:56:15 +0100157 struct msgb *msg, *msg2;
Harald Welte31bbbf42010-12-23 00:02:51 +0100158 struct gsm_mncc *mncc_prim;
159
160 /* peek at the beginning of the queue */
161 msg = llist_entry(net->upqueue.next, struct msgb, list);
162 mncc_prim = (struct gsm_mncc *)msg->data;
163
164 bfd->when &= ~BSC_FD_WRITE;
165
166 /* try to send it over the socket */
167 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
168 if (rc == 0)
169 goto close;
170 if (rc < 0) {
171 if (errno == EAGAIN) {
172 bfd->when |= BSC_FD_WRITE;
173 break;
174 }
175 goto close;
176 }
177 /* _after_ we send it, we can deueue */
Harald Welte49a2dde2010-12-22 22:56:15 +0100178 msg2 = msgb_dequeue(&net->upqueue);
179 assert(msg == msg2);
180 msgb_free(msg);
Harald Welte31bbbf42010-12-23 00:02:51 +0100181 }
182 return 0;
183
184close:
185 mncc_sock_close(state);
186
187 return -1;
188}
189
190static int mncc_sock_cb(struct bsc_fd *bfd, unsigned int flags)
191{
192 int rc = 0;
193
194 if (flags & BSC_FD_READ)
195 rc = mncc_sock_read(bfd);
196 if (rc < 0)
197 return rc;
198
199 if (flags & BSC_FD_WRITE)
200 rc = mncc_sock_write(bfd);
201
202 return rc;
203}
204
205/* accept a new connection */
206static int mncc_sock_accept(struct bsc_fd *bfd, unsigned int flags)
207{
208 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
209 struct bsc_fd *conn_bfd = &state->conn_bfd;
210 struct sockaddr_un un_addr;
211 socklen_t len;
212 int rc;
213
214 len = sizeof(un_addr);
215 rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len);
216 if (rc < 0) {
217 fprintf(stderr, "Failed to accept a new connection.\n");
218 return -1;
219 }
220
221 if (conn_bfd->fd > 0) {
222 /* We already have one MNCC app connected, this is all we support */
223 state->listen_bfd.when &= ~BSC_FD_READ;
224 close(rc);
225 }
226
227 conn_bfd->fd = rc;
228 conn_bfd->when = BSC_FD_READ;
229 conn_bfd->cb = mncc_sock_cb;
230 conn_bfd->data = state;
231
232 if (bsc_register_fd(conn_bfd) != 0) {
233 fprintf(stderr, "Failed to register the fd.\n");
234 close(conn_bfd->fd);
235 conn_bfd->fd = -1;
236 state->listen_bfd.when |= ~BSC_FD_READ;
237 return -1;
238 }
239
240 return 0;
241}
242
243
244int mncc_sock_init(struct gsm_network *net)
245{
246 struct mncc_sock_state *state;
247 struct bsc_fd *bfd;
248 int rc;
249
250 state = talloc_zero(tall_bsc_ctx, struct mncc_sock_state);
251 if (!state)
252 return -ENOMEM;
253
254 state->net = net;
255 state->conn_bfd.fd = -1;
256
257 bfd = &state->listen_bfd;
258
259 rc = osmo_unixsock_listen(bfd, SOCK_SEQPACKET, "/tmp/bsc_mncc");
260 if (rc < 0) {
261 talloc_free(state);
262 return rc;
263 }
264
265 bfd->when = BSC_FD_READ;
266 bfd->cb = mncc_sock_accept;
267 bfd->data = state;
268
269 rc = bsc_register_fd(bfd);
270 if (rc < 0) {
271 fprintf(stderr, "Failed to register the bfd.\n");
272 close(bfd->fd);
273 talloc_free(state);
274 return rc;
275 }
276
277 g_state = state;
278
279 return 0;
280}
281
282/* FIXME: move this to libosmocore */
283int osmo_unixsock_listen(struct bsc_fd *bfd, int type, const char *path)
284{
285 struct sockaddr_un local;
286 unsigned int namelen;
287 int rc;
288
289 bfd->fd = socket(AF_UNIX, type, 0);
290
291 if (bfd->fd < 0) {
292 fprintf(stderr, "Failed to create Unix Domain Socket.\n");
293 return -1;
294 }
295
296 local.sun_family = AF_UNIX;
297 strncpy(local.sun_path, path, sizeof(local.sun_path));
298 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
299 unlink(local.sun_path);
300
301 /* we use the same magic that X11 uses in Xtranssock.c for
302 * calculating the proper length of the sockaddr */
303#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
304 local.sun_len = strlen(local.sun_path);
305#endif
306#if defined(BSD44SOCKETS) || defined(SUN_LEN)
307 namelen = SUN_LEN(&local);
308#else
309 namelen = strlen(local.sun_path) +
310 offsetof(struct sockaddr_un, sun_path);
311#endif
312
313 rc = bind(bfd->fd, (struct sockaddr *) &local, namelen);
314 if (rc != 0) {
315 fprintf(stderr, "Failed to bind the unix domain socket. '%s'\n",
316 local.sun_path);
317 return -1;
318 }
319
320 if (listen(bfd->fd, 0) != 0) {
321 fprintf(stderr, "Failed to listen.\n");
322 return -1;
323 }
324
325 return 0;
326}