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