blob: 7e0a2088c07ca0ffad03664615aa55e23d03a2ba [file] [log] [blame]
Harald Welte33272882010-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 Weltef5db3562010-12-22 22:56:15 +010028#include <assert.h>
Harald Welte33272882010-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>
35
36#include <openbsc/debug.h>
37#include <openbsc/mncc.h>
38#include <openbsc/gsm_data.h>
39
40struct mncc_sock_state {
41 struct gsm_network *net;
42 struct bsc_fd listen_bfd; /* fd for listen socket */
43 struct bsc_fd conn_bfd; /* fd for connection to lcr */
44};
45
46/* FIXME: avoid this */
47static struct mncc_sock_state *g_state;
48
49void mncc_sock_write_pending(void)
50{
51 g_state->conn_bfd.when |= BSC_FD_WRITE;
52}
53
54/* FIXME: move this to libosmocore */
55int osmo_unixsock_listen(struct bsc_fd *bfd, int type, const char *path);
56
57static void mncc_sock_close(struct mncc_sock_state *state)
58{
59 struct bsc_fd *bfd = &state->conn_bfd;
60
61 close(bfd->fd);
62 bfd->fd = -1;
63 bsc_unregister_fd(bfd);
64
65 /* re-enable the generation of ACCEPT for new connections */
66 state->listen_bfd.when |= BSC_FD_READ;
67
68 /* FIXME: make sure we don't enqueue anymore */
Harald Weltee8d16172010-12-22 23:17:50 +010069
70 /* release all exisitng calls */
71 gsm0408_clear_all_trans(state->net, GSM48_PDISC_CC);
Harald Welte33272882010-12-23 00:02:51 +010072
73 /* flush the queue */
74 while (!llist_empty(&state->net->upqueue)) {
75 struct msgb *msg = msgb_dequeue(&state->net->upqueue);
76 msgb_free(msg);
77 }
78}
79
80static int mncc_sock_read(struct bsc_fd *bfd)
81{
82 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
83 struct gsm_mncc *mncc_prim;
84 struct msgb *msg;
85 int rc;
86
87 msg = msgb_alloc(sizeof(*mncc_prim)+256, "mncc_sock_rx");
88 if (!msg)
89 return -ENOMEM;
90
91 mncc_prim = (struct gsm_mncc *) msg->tail;
92
93 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
94 if (rc == 0)
95 goto close;
96
97 if (rc < 0) {
98 if (errno == EAGAIN)
99 return 0;
100 fprintf(stderr, "Err from socket: %s\n", strerror(errno));
101 goto close;
102 }
103
104 rc = mncc_tx_to_cc(state->net, mncc_prim->msg_type, mncc_prim);
105
106 /* as we always synchronously process the message in mncc_send() and
107 * its callbacks, we can free the message here. */
108 msgb_free(msg);
109
110 return rc;
111
112close:
113 mncc_sock_close(state);
114 return -1;
115}
116
117static int mncc_sock_write(struct bsc_fd *bfd)
118{
119 struct mncc_sock_state *state = bfd->data;
120 struct gsm_network *net = state->net;
121 int rc;
122
123 while (!llist_empty(&net->upqueue)) {
Harald Weltef5db3562010-12-22 22:56:15 +0100124 struct msgb *msg, *msg2;
Harald Welte33272882010-12-23 00:02:51 +0100125 struct gsm_mncc *mncc_prim;
126
127 /* peek at the beginning of the queue */
128 msg = llist_entry(net->upqueue.next, struct msgb, list);
129 mncc_prim = (struct gsm_mncc *)msg->data;
130
131 bfd->when &= ~BSC_FD_WRITE;
132
133 /* try to send it over the socket */
134 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
135 if (rc == 0)
136 goto close;
137 if (rc < 0) {
138 if (errno == EAGAIN) {
139 bfd->when |= BSC_FD_WRITE;
140 break;
141 }
142 goto close;
143 }
144 /* _after_ we send it, we can deueue */
Harald Weltef5db3562010-12-22 22:56:15 +0100145 msg2 = msgb_dequeue(&net->upqueue);
146 assert(msg == msg2);
147 msgb_free(msg);
Harald Welte33272882010-12-23 00:02:51 +0100148 }
149 return 0;
150
151close:
152 mncc_sock_close(state);
153
154 return -1;
155}
156
157static int mncc_sock_cb(struct bsc_fd *bfd, unsigned int flags)
158{
159 int rc = 0;
160
161 if (flags & BSC_FD_READ)
162 rc = mncc_sock_read(bfd);
163 if (rc < 0)
164 return rc;
165
166 if (flags & BSC_FD_WRITE)
167 rc = mncc_sock_write(bfd);
168
169 return rc;
170}
171
172/* accept a new connection */
173static int mncc_sock_accept(struct bsc_fd *bfd, unsigned int flags)
174{
175 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
176 struct bsc_fd *conn_bfd = &state->conn_bfd;
177 struct sockaddr_un un_addr;
178 socklen_t len;
179 int rc;
180
181 len = sizeof(un_addr);
182 rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len);
183 if (rc < 0) {
184 fprintf(stderr, "Failed to accept a new connection.\n");
185 return -1;
186 }
187
188 if (conn_bfd->fd > 0) {
189 /* We already have one MNCC app connected, this is all we support */
190 state->listen_bfd.when &= ~BSC_FD_READ;
191 close(rc);
192 }
193
194 conn_bfd->fd = rc;
195 conn_bfd->when = BSC_FD_READ;
196 conn_bfd->cb = mncc_sock_cb;
197 conn_bfd->data = state;
198
199 if (bsc_register_fd(conn_bfd) != 0) {
200 fprintf(stderr, "Failed to register the fd.\n");
201 close(conn_bfd->fd);
202 conn_bfd->fd = -1;
203 state->listen_bfd.when |= ~BSC_FD_READ;
204 return -1;
205 }
206
207 return 0;
208}
209
210
211int mncc_sock_init(struct gsm_network *net)
212{
213 struct mncc_sock_state *state;
214 struct bsc_fd *bfd;
215 int rc;
216
217 state = talloc_zero(tall_bsc_ctx, struct mncc_sock_state);
218 if (!state)
219 return -ENOMEM;
220
221 state->net = net;
222 state->conn_bfd.fd = -1;
223
224 bfd = &state->listen_bfd;
225
226 rc = osmo_unixsock_listen(bfd, SOCK_SEQPACKET, "/tmp/bsc_mncc");
227 if (rc < 0) {
228 talloc_free(state);
229 return rc;
230 }
231
232 bfd->when = BSC_FD_READ;
233 bfd->cb = mncc_sock_accept;
234 bfd->data = state;
235
236 rc = bsc_register_fd(bfd);
237 if (rc < 0) {
238 fprintf(stderr, "Failed to register the bfd.\n");
239 close(bfd->fd);
240 talloc_free(state);
241 return rc;
242 }
243
244 g_state = state;
245
246 return 0;
247}
248
249/* FIXME: move this to libosmocore */
250int osmo_unixsock_listen(struct bsc_fd *bfd, int type, const char *path)
251{
252 struct sockaddr_un local;
253 unsigned int namelen;
254 int rc;
255
256 bfd->fd = socket(AF_UNIX, type, 0);
257
258 if (bfd->fd < 0) {
259 fprintf(stderr, "Failed to create Unix Domain Socket.\n");
260 return -1;
261 }
262
263 local.sun_family = AF_UNIX;
264 strncpy(local.sun_path, path, sizeof(local.sun_path));
265 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
266 unlink(local.sun_path);
267
268 /* we use the same magic that X11 uses in Xtranssock.c for
269 * calculating the proper length of the sockaddr */
270#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
271 local.sun_len = strlen(local.sun_path);
272#endif
273#if defined(BSD44SOCKETS) || defined(SUN_LEN)
274 namelen = SUN_LEN(&local);
275#else
276 namelen = strlen(local.sun_path) +
277 offsetof(struct sockaddr_un, sun_path);
278#endif
279
280 rc = bind(bfd->fd, (struct sockaddr *) &local, namelen);
281 if (rc != 0) {
282 fprintf(stderr, "Failed to bind the unix domain socket. '%s'\n",
283 local.sun_path);
284 return -1;
285 }
286
287 if (listen(bfd->fd, 0) != 0) {
288 fprintf(stderr, "Failed to listen.\n");
289 return -1;
290 }
291
292 return 0;
293}