Harald Welte | 3327288 | 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> |
Holger Hans Peter Freyther | 460d84a | 2012-01-15 00:38:35 +0100 | [diff] [blame] | 5 | * (C) 2012 by Holger Hans Peter Freyther |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <errno.h> |
Harald Welte | f5db356 | 2010-12-22 22:56:15 +0100 | [diff] [blame] | 29 | #include <assert.h> |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 30 | #include <sys/socket.h> |
| 31 | #include <sys/un.h> |
| 32 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 33 | #include <osmocom/core/talloc.h> |
| 34 | #include <osmocom/core/select.h> |
| 35 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 36 | |
| 37 | #include <openbsc/debug.h> |
| 38 | #include <openbsc/mncc.h> |
| 39 | #include <openbsc/gsm_data.h> |
| 40 | |
| 41 | struct mncc_sock_state { |
| 42 | struct gsm_network *net; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 43 | struct osmo_fd listen_bfd; /* fd for listen socket */ |
| 44 | struct osmo_fd conn_bfd; /* fd for connection to lcr */ |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
Harald Welte | bd93eaf | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 47 | /* input from CC code into mncc_sock */ |
Holger Hans Peter Freyther | 7f66183 | 2011-01-06 14:13:44 +0100 | [diff] [blame] | 48 | int mncc_sock_from_cc(struct gsm_network *net, struct msgb *msg) |
Harald Welte | bd93eaf | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 49 | { |
Holger Hans Peter Freyther | 8fbe3e9 | 2011-01-06 13:50:52 +0100 | [diff] [blame] | 50 | struct gsm_mncc *mncc_in = (struct gsm_mncc *) msgb_data(msg); |
Harald Welte | bd93eaf | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 51 | int msg_type = mncc_in->msg_type; |
| 52 | |
| 53 | /* Check if we currently have a MNCC handler connected */ |
Holger Hans Peter Freyther | 41cc7ea | 2011-10-26 18:37:09 +0200 | [diff] [blame] | 54 | if (net->mncc_state->conn_bfd.fd < 0) { |
Harald Welte | bd93eaf | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 55 | LOGP(DMNCC, LOGL_ERROR, "mncc_sock receives %s for external CC app " |
| 56 | "but socket is gone\n", get_mncc_name(msg_type)); |
Andreas Eversberg | 5e2832e | 2013-03-11 08:12:43 +0100 | [diff] [blame] | 57 | if (!mncc_is_data_frame(msg_type)) { |
Harald Welte | bd93eaf | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 58 | /* release the request */ |
| 59 | struct gsm_mncc mncc_out; |
| 60 | memset(&mncc_out, 0, sizeof(mncc_out)); |
| 61 | mncc_out.callref = mncc_in->callref; |
| 62 | mncc_set_cause(&mncc_out, GSM48_CAUSE_LOC_PRN_S_LU, |
| 63 | GSM48_CC_CAUSE_TEMP_FAILURE); |
| 64 | mncc_tx_to_cc(net, MNCC_REL_REQ, &mncc_out); |
| 65 | } |
| 66 | /* free the original message */ |
| 67 | msgb_free(msg); |
Holger Hans Peter Freyther | 7f66183 | 2011-01-06 14:13:44 +0100 | [diff] [blame] | 68 | return -1; |
Harald Welte | bd93eaf | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* FIXME: check for some maximum queue depth? */ |
| 72 | |
| 73 | /* Actually enqueue the message and mark socket write need */ |
| 74 | msgb_enqueue(&net->upqueue, msg); |
Holger Hans Peter Freyther | 41cc7ea | 2011-10-26 18:37:09 +0200 | [diff] [blame] | 75 | net->mncc_state->conn_bfd.when |= BSC_FD_WRITE; |
Holger Hans Peter Freyther | 7f66183 | 2011-01-06 14:13:44 +0100 | [diff] [blame] | 76 | return 0; |
Harald Welte | bd93eaf | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 79 | /* FIXME: move this to libosmocore */ |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 80 | int osmo_unixsock_listen(struct osmo_fd *bfd, int type, const char *path); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 81 | |
| 82 | static void mncc_sock_close(struct mncc_sock_state *state) |
| 83 | { |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 84 | struct osmo_fd *bfd = &state->conn_bfd; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 85 | |
Harald Welte | 5be4c72 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 86 | LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has LOST connection\n"); |
| 87 | |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 88 | close(bfd->fd); |
| 89 | bfd->fd = -1; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 90 | osmo_fd_unregister(bfd); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 91 | |
| 92 | /* re-enable the generation of ACCEPT for new connections */ |
| 93 | state->listen_bfd.when |= BSC_FD_READ; |
| 94 | |
Harald Welte | e8d1617 | 2010-12-22 23:17:50 +0100 | [diff] [blame] | 95 | /* release all exisitng calls */ |
| 96 | gsm0408_clear_all_trans(state->net, GSM48_PDISC_CC); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 97 | |
| 98 | /* flush the queue */ |
| 99 | while (!llist_empty(&state->net->upqueue)) { |
| 100 | struct msgb *msg = msgb_dequeue(&state->net->upqueue); |
| 101 | msgb_free(msg); |
| 102 | } |
| 103 | } |
| 104 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 105 | static int mncc_sock_read(struct osmo_fd *bfd) |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 106 | { |
| 107 | struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data; |
| 108 | struct gsm_mncc *mncc_prim; |
| 109 | struct msgb *msg; |
| 110 | int rc; |
| 111 | |
| 112 | msg = msgb_alloc(sizeof(*mncc_prim)+256, "mncc_sock_rx"); |
| 113 | if (!msg) |
| 114 | return -ENOMEM; |
| 115 | |
| 116 | mncc_prim = (struct gsm_mncc *) msg->tail; |
| 117 | |
| 118 | rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0); |
| 119 | if (rc == 0) |
| 120 | goto close; |
| 121 | |
| 122 | if (rc < 0) { |
| 123 | if (errno == EAGAIN) |
| 124 | return 0; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 125 | goto close; |
| 126 | } |
| 127 | |
| 128 | rc = mncc_tx_to_cc(state->net, mncc_prim->msg_type, mncc_prim); |
| 129 | |
| 130 | /* as we always synchronously process the message in mncc_send() and |
| 131 | * its callbacks, we can free the message here. */ |
| 132 | msgb_free(msg); |
| 133 | |
| 134 | return rc; |
| 135 | |
| 136 | close: |
Harald Welte | bd0f33c | 2010-12-23 02:47:53 +0100 | [diff] [blame] | 137 | msgb_free(msg); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 138 | mncc_sock_close(state); |
| 139 | return -1; |
| 140 | } |
| 141 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 142 | static int mncc_sock_write(struct osmo_fd *bfd) |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 143 | { |
| 144 | struct mncc_sock_state *state = bfd->data; |
| 145 | struct gsm_network *net = state->net; |
| 146 | int rc; |
| 147 | |
| 148 | while (!llist_empty(&net->upqueue)) { |
Harald Welte | f5db356 | 2010-12-22 22:56:15 +0100 | [diff] [blame] | 149 | struct msgb *msg, *msg2; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 150 | struct gsm_mncc *mncc_prim; |
| 151 | |
| 152 | /* peek at the beginning of the queue */ |
| 153 | msg = llist_entry(net->upqueue.next, struct msgb, list); |
| 154 | mncc_prim = (struct gsm_mncc *)msg->data; |
| 155 | |
| 156 | bfd->when &= ~BSC_FD_WRITE; |
| 157 | |
Peter Stuge | 3912c7e | 2011-08-11 04:37:17 +0200 | [diff] [blame] | 158 | /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */ |
| 159 | if (!msgb_length(msg)) { |
| 160 | LOGP(DMNCC, LOGL_ERROR, "message type (%d) with ZERO " |
| 161 | "bytes!\n", mncc_prim->msg_type); |
| 162 | goto dontsend; |
| 163 | } |
| 164 | |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 165 | /* try to send it over the socket */ |
| 166 | rc = write(bfd->fd, msgb_data(msg), msgb_length(msg)); |
| 167 | if (rc == 0) |
| 168 | goto close; |
| 169 | if (rc < 0) { |
| 170 | if (errno == EAGAIN) { |
| 171 | bfd->when |= BSC_FD_WRITE; |
| 172 | break; |
| 173 | } |
| 174 | goto close; |
| 175 | } |
Peter Stuge | 3912c7e | 2011-08-11 04:37:17 +0200 | [diff] [blame] | 176 | |
| 177 | dontsend: |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 178 | /* _after_ we send it, we can deueue */ |
Harald Welte | f5db356 | 2010-12-22 22:56:15 +0100 | [diff] [blame] | 179 | msg2 = msgb_dequeue(&net->upqueue); |
| 180 | assert(msg == msg2); |
| 181 | msgb_free(msg); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 182 | } |
| 183 | return 0; |
| 184 | |
| 185 | close: |
| 186 | mncc_sock_close(state); |
| 187 | |
| 188 | return -1; |
| 189 | } |
| 190 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 191 | static int mncc_sock_cb(struct osmo_fd *bfd, unsigned int flags) |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 192 | { |
| 193 | int rc = 0; |
| 194 | |
| 195 | if (flags & BSC_FD_READ) |
| 196 | rc = mncc_sock_read(bfd); |
| 197 | if (rc < 0) |
| 198 | return rc; |
| 199 | |
| 200 | if (flags & BSC_FD_WRITE) |
| 201 | rc = mncc_sock_write(bfd); |
| 202 | |
| 203 | return rc; |
| 204 | } |
| 205 | |
Holger Hans Peter Freyther | f0e71e7 | 2011-10-21 14:12:46 +0200 | [diff] [blame] | 206 | /** |
| 207 | * Send a version indication to the remote. |
| 208 | */ |
| 209 | static void queue_hello(struct mncc_sock_state *mncc) |
| 210 | { |
| 211 | struct gsm_mncc_hello *hello; |
| 212 | struct msgb *msg; |
| 213 | |
| 214 | msg = msgb_alloc(512, "mncc hello"); |
| 215 | if (!msg) { |
| 216 | LOGP(DMNCC, LOGL_ERROR, "Failed to allocate hello.\n"); |
| 217 | mncc_sock_close(mncc); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | hello = (struct gsm_mncc_hello *) msgb_put(msg, sizeof(*hello)); |
| 222 | hello->msg_type = MNCC_SOCKET_HELLO; |
| 223 | hello->version = MNCC_SOCK_VERSION; |
Holger Hans Peter Freyther | 460d84a | 2012-01-15 00:38:35 +0100 | [diff] [blame] | 224 | hello->mncc_size = sizeof(struct gsm_mncc); |
| 225 | hello->data_frame_size = sizeof(struct gsm_data_frame); |
| 226 | hello->called_offset = offsetof(struct gsm_mncc, called); |
| 227 | hello->signal_offset = offsetof(struct gsm_mncc, signal); |
| 228 | hello->emergency_offset = offsetof(struct gsm_mncc, emergency); |
| 229 | hello->lchan_type_offset = offsetof(struct gsm_mncc, lchan_type); |
Holger Hans Peter Freyther | f0e71e7 | 2011-10-21 14:12:46 +0200 | [diff] [blame] | 230 | |
| 231 | msgb_enqueue(&mncc->net->upqueue, msg); |
| 232 | mncc->conn_bfd.when |= BSC_FD_WRITE; |
| 233 | } |
| 234 | |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 235 | /* accept a new connection */ |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 236 | static int mncc_sock_accept(struct osmo_fd *bfd, unsigned int flags) |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 237 | { |
| 238 | struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 239 | struct osmo_fd *conn_bfd = &state->conn_bfd; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 240 | struct sockaddr_un un_addr; |
| 241 | socklen_t len; |
| 242 | int rc; |
| 243 | |
| 244 | len = sizeof(un_addr); |
| 245 | rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len); |
| 246 | if (rc < 0) { |
Harald Welte | 5be4c72 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 247 | LOGP(DMNCC, LOGL_ERROR, "Failed to accept a new connection\n"); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 248 | return -1; |
| 249 | } |
| 250 | |
Harald Welte | 5be4c72 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 251 | if (conn_bfd->fd >= 0) { |
| 252 | LOGP(DMNCC, LOGL_NOTICE, "MNCC app connects but we already have " |
| 253 | "another active connection ?!?\n"); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 254 | /* We already have one MNCC app connected, this is all we support */ |
| 255 | state->listen_bfd.when &= ~BSC_FD_READ; |
| 256 | close(rc); |
Harald Welte | 5be4c72 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 257 | return 0; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | conn_bfd->fd = rc; |
| 261 | conn_bfd->when = BSC_FD_READ; |
| 262 | conn_bfd->cb = mncc_sock_cb; |
| 263 | conn_bfd->data = state; |
| 264 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 265 | if (osmo_fd_register(conn_bfd) != 0) { |
Harald Welte | 5be4c72 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 266 | LOGP(DMNCC, LOGL_ERROR, "Failed to register new connection fd\n"); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 267 | close(conn_bfd->fd); |
| 268 | conn_bfd->fd = -1; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 269 | return -1; |
| 270 | } |
| 271 | |
Harald Welte | 5be4c72 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 272 | LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has connection with external " |
| 273 | "call control application\n"); |
| 274 | |
Holger Hans Peter Freyther | f0e71e7 | 2011-10-21 14:12:46 +0200 | [diff] [blame] | 275 | queue_hello(state); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame^] | 280 | int mncc_sock_init(struct gsm_network *net, const char *sock_path) |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 281 | { |
| 282 | struct mncc_sock_state *state; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 283 | struct osmo_fd *bfd; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 284 | int rc; |
| 285 | |
| 286 | state = talloc_zero(tall_bsc_ctx, struct mncc_sock_state); |
| 287 | if (!state) |
| 288 | return -ENOMEM; |
| 289 | |
| 290 | state->net = net; |
| 291 | state->conn_bfd.fd = -1; |
| 292 | |
| 293 | bfd = &state->listen_bfd; |
| 294 | |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame^] | 295 | rc = osmo_unixsock_listen(bfd, SOCK_SEQPACKET, sock_path); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 296 | if (rc < 0) { |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame^] | 297 | LOGP(DMNCC, LOGL_ERROR, "Could not create unix socket: %s: %s\n", |
| 298 | sock_path, strerror(errno)); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 299 | talloc_free(state); |
| 300 | return rc; |
| 301 | } |
| 302 | |
| 303 | bfd->when = BSC_FD_READ; |
| 304 | bfd->cb = mncc_sock_accept; |
| 305 | bfd->data = state; |
| 306 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 307 | rc = osmo_fd_register(bfd); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 308 | if (rc < 0) { |
Harald Welte | 5be4c72 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 309 | LOGP(DMNCC, LOGL_ERROR, "Could not register listen fd: %d\n", rc); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 310 | close(bfd->fd); |
| 311 | talloc_free(state); |
| 312 | return rc; |
| 313 | } |
| 314 | |
Holger Hans Peter Freyther | 41cc7ea | 2011-10-26 18:37:09 +0200 | [diff] [blame] | 315 | net->mncc_state = state; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 316 | |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame^] | 317 | LOGP(DMNCC, LOGL_NOTICE, "MNCC socket at %s\n", sock_path); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | /* FIXME: move this to libosmocore */ |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 322 | int osmo_unixsock_listen(struct osmo_fd *bfd, int type, const char *path) |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 323 | { |
| 324 | struct sockaddr_un local; |
| 325 | unsigned int namelen; |
| 326 | int rc; |
| 327 | |
| 328 | bfd->fd = socket(AF_UNIX, type, 0); |
| 329 | |
| 330 | if (bfd->fd < 0) { |
| 331 | fprintf(stderr, "Failed to create Unix Domain Socket.\n"); |
| 332 | return -1; |
| 333 | } |
| 334 | |
| 335 | local.sun_family = AF_UNIX; |
| 336 | strncpy(local.sun_path, path, sizeof(local.sun_path)); |
| 337 | local.sun_path[sizeof(local.sun_path) - 1] = '\0'; |
| 338 | unlink(local.sun_path); |
| 339 | |
| 340 | /* we use the same magic that X11 uses in Xtranssock.c for |
| 341 | * calculating the proper length of the sockaddr */ |
| 342 | #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
| 343 | local.sun_len = strlen(local.sun_path); |
| 344 | #endif |
| 345 | #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 346 | namelen = SUN_LEN(&local); |
| 347 | #else |
| 348 | namelen = strlen(local.sun_path) + |
| 349 | offsetof(struct sockaddr_un, sun_path); |
| 350 | #endif |
| 351 | |
| 352 | rc = bind(bfd->fd, (struct sockaddr *) &local, namelen); |
| 353 | if (rc != 0) { |
| 354 | fprintf(stderr, "Failed to bind the unix domain socket. '%s'\n", |
| 355 | local.sun_path); |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | if (listen(bfd->fd, 0) != 0) { |
| 360 | fprintf(stderr, "Failed to listen.\n"); |
| 361 | return -1; |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |