Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * BlueZ - Bluetooth protocol stack for Linux |
| 4 | * |
| 5 | * Copyright (C) 2011-2012 Intel Corporation |
| 6 | * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org> |
| 7 | * |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #ifdef HAVE_CONFIG_H |
| 26 | #include <config.h> |
| 27 | #endif |
| 28 | |
| 29 | #include <stdio.h> |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 30 | #include <errno.h> |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 31 | #include <ctype.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <unistd.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <getopt.h> |
| 37 | #include <stdbool.h> |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 38 | #include <termios.h> |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 39 | #include <sys/stat.h> |
| 40 | #include <sys/socket.h> |
| 41 | #include <sys/un.h> |
| 42 | |
Marcel Holtmann | 15097d3 | 2013-12-30 21:26:31 -0800 | [diff] [blame] | 43 | #include <netdb.h> |
| 44 | #include <arpa/inet.h> |
| 45 | |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 46 | #include "src/shared/util.h" |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 47 | #include "monitor/mainloop.h" |
| 48 | #include "monitor/bt.h" |
| 49 | |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 50 | #define BTPROTO_HCI 1 |
| 51 | struct sockaddr_hci { |
| 52 | sa_family_t hci_family; |
| 53 | unsigned short hci_dev; |
| 54 | unsigned short hci_channel; |
| 55 | }; |
| 56 | #define HCI_CHANNEL_USER 1 |
| 57 | |
| 58 | static uint16_t hci_index = 0; |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 59 | static bool client_active = false; |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 60 | static bool debug_enabled = false; |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 61 | |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 62 | static void hexdump_print(const char *str, void *user_data) |
| 63 | { |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 64 | printf("%s%s\n", (char *) user_data, str); |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 67 | struct proxy { |
| 68 | /* Receive commands, ACL and SCO data */ |
| 69 | int host_fd; |
| 70 | uint8_t host_buf[4096]; |
| 71 | uint16_t host_len; |
| 72 | bool host_shutdown; |
| 73 | |
| 74 | /* Receive events, ACL and SCO data */ |
| 75 | int dev_fd; |
| 76 | uint8_t dev_buf[4096]; |
| 77 | uint16_t dev_len; |
| 78 | bool dev_shutdown; |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 81 | static bool write_packet(int fd, const void *data, size_t size, |
| 82 | void *user_data) |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 83 | { |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 84 | while (size > 0) { |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 85 | ssize_t written; |
| 86 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 87 | written = write(fd, data, size); |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 88 | if (written < 0) { |
| 89 | if (errno == EAGAIN || errno == EINTR) |
| 90 | continue; |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 91 | return false; |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 94 | if (debug_enabled) |
| 95 | util_hexdump('<', data, written, hexdump_print, |
| 96 | user_data); |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 97 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 98 | data += written; |
| 99 | size -= written; |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 105 | static void host_read_destroy(void *user_data) |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 106 | { |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 107 | struct proxy *proxy = user_data; |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 108 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 109 | printf("Closing host descriptor\n"); |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 110 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 111 | if (proxy->host_shutdown) |
| 112 | shutdown(proxy->host_fd, SHUT_RDWR); |
| 113 | |
| 114 | close(proxy->host_fd); |
| 115 | proxy->host_fd = -1; |
| 116 | |
| 117 | if (proxy->dev_fd < 0) { |
| 118 | client_active = false; |
| 119 | free(proxy); |
| 120 | } else |
| 121 | mainloop_remove_fd(proxy->dev_fd); |
| 122 | } |
| 123 | |
| 124 | static void host_read_callback(int fd, uint32_t events, void *user_data) |
| 125 | { |
| 126 | struct proxy *proxy = user_data; |
| 127 | struct bt_hci_cmd_hdr *cmd_hdr; |
| 128 | struct bt_hci_acl_hdr *acl_hdr; |
| 129 | struct bt_hci_sco_hdr *sco_hdr; |
| 130 | ssize_t len; |
| 131 | uint16_t pktlen; |
| 132 | |
| 133 | if (events & (EPOLLERR | EPOLLHUP)) { |
| 134 | fprintf(stderr, "Error from host descriptor\n"); |
| 135 | mainloop_remove_fd(proxy->host_fd); |
| 136 | return; |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 139 | if (events & EPOLLRDHUP) { |
| 140 | fprintf(stderr, "Remote hangup of host descriptor\n"); |
| 141 | mainloop_remove_fd(proxy->host_fd); |
| 142 | return; |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 145 | len = read(proxy->host_fd, proxy->host_buf + proxy->host_len, |
| 146 | sizeof(proxy->host_buf) - proxy->host_len); |
| 147 | if (len < 0) { |
| 148 | if (errno == EAGAIN || errno == EINTR) |
| 149 | return; |
| 150 | |
| 151 | fprintf(stderr, "Read from host descriptor failed\n"); |
| 152 | mainloop_remove_fd(proxy->host_fd); |
| 153 | return; |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 156 | if (debug_enabled) |
| 157 | util_hexdump('>', proxy->host_buf + proxy->host_len, len, |
| 158 | hexdump_print, "H: "); |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 159 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 160 | proxy->host_len += len; |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 161 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 162 | process_packet: |
| 163 | if (proxy->host_len < 1) |
| 164 | return; |
| 165 | |
| 166 | switch (proxy->host_buf[0]) { |
| 167 | case BT_H4_CMD_PKT: |
| 168 | if (proxy->host_len < 1 + sizeof(*cmd_hdr)) |
| 169 | return; |
| 170 | |
| 171 | cmd_hdr = (void *) (proxy->host_buf + 1); |
| 172 | pktlen = 1 + sizeof(*cmd_hdr) + cmd_hdr->plen; |
| 173 | break; |
| 174 | case BT_H4_ACL_PKT: |
| 175 | if (proxy->host_len < 1 + sizeof(*acl_hdr)) |
| 176 | return; |
| 177 | |
| 178 | acl_hdr = (void *) (proxy->host_buf + 1); |
| 179 | pktlen = 1 + sizeof(*acl_hdr) + cpu_to_le16(acl_hdr->dlen); |
| 180 | break; |
| 181 | case BT_H4_SCO_PKT: |
| 182 | if (proxy->host_len < 1 + sizeof(*sco_hdr)) |
| 183 | return; |
| 184 | |
| 185 | sco_hdr = (void *) (proxy->host_buf + 1); |
| 186 | pktlen = 1 + sizeof(*sco_hdr) + sco_hdr->dlen; |
| 187 | break; |
| 188 | case 0xff: |
| 189 | /* Notification packet from /dev/vhci - ignore */ |
| 190 | proxy->host_len = 0; |
| 191 | return; |
| 192 | default: |
| 193 | fprintf(stderr, "Received unknown host packet type 0x%02x\n", |
| 194 | proxy->host_buf[0]); |
| 195 | mainloop_remove_fd(proxy->host_fd); |
| 196 | return; |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 199 | if (proxy->host_len < pktlen) |
| 200 | return; |
| 201 | |
| 202 | if (!write_packet(proxy->dev_fd, proxy->host_buf, pktlen, "D: ")) { |
| 203 | fprintf(stderr, "Write to device descriptor failed\n"); |
| 204 | mainloop_remove_fd(proxy->dev_fd); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | if (proxy->host_len > pktlen) { |
| 209 | memmove(proxy->host_buf, proxy->host_buf + pktlen, |
| 210 | proxy->host_len - pktlen); |
| 211 | proxy->host_len -= pktlen; |
| 212 | goto process_packet; |
| 213 | } |
| 214 | |
| 215 | proxy->host_len = 0; |
| 216 | } |
| 217 | |
| 218 | static void dev_read_destroy(void *user_data) |
| 219 | { |
| 220 | struct proxy *proxy = user_data; |
| 221 | |
| 222 | printf("Closing device descriptor\n"); |
| 223 | |
| 224 | if (proxy->dev_shutdown) |
| 225 | shutdown(proxy->dev_fd, SHUT_RDWR); |
| 226 | |
| 227 | close(proxy->dev_fd); |
| 228 | proxy->dev_fd = -1; |
| 229 | |
| 230 | if (proxy->host_fd < 0) { |
| 231 | client_active = false; |
| 232 | free(proxy); |
| 233 | } else |
| 234 | mainloop_remove_fd(proxy->host_fd); |
| 235 | } |
| 236 | |
| 237 | static void dev_read_callback(int fd, uint32_t events, void *user_data) |
| 238 | { |
| 239 | struct proxy *proxy = user_data; |
| 240 | struct bt_hci_evt_hdr *evt_hdr; |
| 241 | struct bt_hci_acl_hdr *acl_hdr; |
| 242 | struct bt_hci_sco_hdr *sco_hdr; |
| 243 | ssize_t len; |
| 244 | uint16_t pktlen; |
| 245 | |
| 246 | if (events & (EPOLLERR | EPOLLHUP)) { |
| 247 | fprintf(stderr, "Error from device descriptor\n"); |
| 248 | mainloop_remove_fd(proxy->dev_fd); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | if (events & EPOLLRDHUP) { |
| 253 | fprintf(stderr, "Remote hangup of device descriptor\n"); |
| 254 | mainloop_remove_fd(proxy->host_fd); |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | len = read(proxy->dev_fd, proxy->dev_buf + proxy->dev_len, |
| 259 | sizeof(proxy->dev_buf) - proxy->dev_len); |
| 260 | if (len < 0) { |
| 261 | if (errno == EAGAIN || errno == EINTR) |
| 262 | return; |
| 263 | |
| 264 | fprintf(stderr, "Read from device descriptor failed\n"); |
| 265 | mainloop_remove_fd(proxy->dev_fd); |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | if (debug_enabled) |
| 270 | util_hexdump('>', proxy->dev_buf + proxy->dev_len, len, |
| 271 | hexdump_print, "D: "); |
| 272 | |
| 273 | proxy->dev_len += len; |
| 274 | |
| 275 | process_packet: |
| 276 | if (proxy->dev_len < 1) |
| 277 | return; |
| 278 | |
| 279 | switch (proxy->dev_buf[0]) { |
| 280 | case BT_H4_EVT_PKT: |
| 281 | if (proxy->dev_len < 1 + sizeof(*evt_hdr)) |
| 282 | return; |
| 283 | |
| 284 | evt_hdr = (void *) (proxy->dev_buf + 1); |
| 285 | pktlen = 1 + sizeof(*evt_hdr) + evt_hdr->plen; |
| 286 | break; |
| 287 | case BT_H4_ACL_PKT: |
| 288 | if (proxy->dev_len < 1 + sizeof(*acl_hdr)) |
| 289 | return; |
| 290 | |
| 291 | acl_hdr = (void *) (proxy->dev_buf + 1); |
| 292 | pktlen = 1 + sizeof(*acl_hdr) + cpu_to_le16(acl_hdr->dlen); |
| 293 | break; |
| 294 | case BT_H4_SCO_PKT: |
| 295 | if (proxy->dev_len < 1 + sizeof(*sco_hdr)) |
| 296 | return; |
| 297 | |
| 298 | sco_hdr = (void *) (proxy->dev_buf + 1); |
| 299 | pktlen = 1 + sizeof(*sco_hdr) + sco_hdr->dlen; |
| 300 | break; |
| 301 | default: |
| 302 | fprintf(stderr, "Received unknown device packet type 0x%02x\n", |
| 303 | proxy->dev_buf[0]); |
| 304 | mainloop_remove_fd(proxy->dev_fd); |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | if (proxy->dev_len < pktlen) |
| 309 | return; |
| 310 | |
| 311 | if (!write_packet(proxy->host_fd, proxy->dev_buf, pktlen, "H: ")) { |
| 312 | fprintf(stderr, "Write to host descriptor failed\n"); |
| 313 | mainloop_remove_fd(proxy->host_fd); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | if (proxy->dev_len > pktlen) { |
| 318 | memmove(proxy->dev_buf, proxy->dev_buf + pktlen, |
| 319 | proxy->dev_len - pktlen); |
| 320 | proxy->dev_len -= pktlen; |
| 321 | goto process_packet; |
| 322 | } |
| 323 | |
| 324 | proxy->dev_len = 0; |
| 325 | } |
| 326 | |
| 327 | static bool setup_proxy(int host_fd, bool host_shutdown, |
| 328 | int dev_fd, bool dev_shutdown) |
| 329 | { |
| 330 | struct proxy *proxy; |
| 331 | |
| 332 | proxy = new0(struct proxy, 1); |
Marcel Holtmann | 8b8a531 | 2014-01-22 07:53:35 -0800 | [diff] [blame] | 333 | if (!proxy) |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 334 | return NULL; |
| 335 | |
| 336 | proxy->host_fd = host_fd; |
| 337 | proxy->host_shutdown = host_shutdown; |
| 338 | |
| 339 | proxy->dev_fd = dev_fd; |
| 340 | proxy->dev_shutdown = dev_shutdown; |
| 341 | |
| 342 | mainloop_add_fd(proxy->host_fd, EPOLLIN | EPOLLRDHUP, |
| 343 | host_read_callback, proxy, host_read_destroy); |
| 344 | |
| 345 | mainloop_add_fd(proxy->dev_fd, EPOLLIN | EPOLLRDHUP, |
| 346 | dev_read_callback, proxy, dev_read_destroy); |
| 347 | |
| 348 | return true; |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 349 | } |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 350 | |
| 351 | static int open_channel(uint16_t index) |
| 352 | { |
| 353 | struct sockaddr_hci addr; |
| 354 | int fd; |
| 355 | |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 356 | printf("Opening user channel for hci%u\n", hci_index); |
| 357 | |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 358 | fd = socket(PF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI); |
| 359 | if (fd < 0) { |
| 360 | perror("Failed to open Bluetooth socket"); |
| 361 | return -1; |
| 362 | } |
| 363 | |
| 364 | memset(&addr, 0, sizeof(addr)); |
| 365 | addr.hci_family = AF_BLUETOOTH; |
| 366 | addr.hci_dev = index; |
| 367 | addr.hci_channel = HCI_CHANNEL_USER; |
| 368 | |
| 369 | if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 370 | close(fd); |
| 371 | perror("Failed to bind Bluetooth socket"); |
| 372 | return -1; |
| 373 | } |
| 374 | |
| 375 | return fd; |
| 376 | } |
| 377 | |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 378 | static void server_callback(int fd, uint32_t events, void *user_data) |
| 379 | { |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 380 | union { |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 381 | struct sockaddr common; |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 382 | struct sockaddr_un sun; |
| 383 | struct sockaddr_in sin; |
| 384 | } addr; |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 385 | socklen_t len; |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 386 | int host_fd, dev_fd; |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 387 | |
| 388 | if (events & (EPOLLERR | EPOLLHUP)) { |
| 389 | mainloop_quit(); |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | memset(&addr, 0, sizeof(addr)); |
| 394 | len = sizeof(addr); |
| 395 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 396 | if (getsockname(fd, &addr.common, &len) < 0) { |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 397 | perror("Failed to get socket name"); |
| 398 | return; |
| 399 | } |
| 400 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 401 | host_fd = accept(fd, &addr.common, &len); |
| 402 | if (host_fd < 0) { |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 403 | perror("Failed to accept client socket"); |
| 404 | return; |
| 405 | } |
| 406 | |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 407 | if (client_active) { |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 408 | fprintf(stderr, "Active client already present\n"); |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 409 | close(host_fd); |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 410 | return; |
| 411 | } |
| 412 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 413 | dev_fd = open_channel(hci_index); |
| 414 | if (dev_fd < 0) { |
| 415 | close(host_fd); |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 416 | return; |
| 417 | } |
| 418 | |
| 419 | printf("New client connected\n"); |
| 420 | |
Marcel Holtmann | 8b8a531 | 2014-01-22 07:53:35 -0800 | [diff] [blame] | 421 | if (!setup_proxy(host_fd, true, dev_fd, false)) { |
| 422 | close(dev_fd); |
| 423 | close(host_fd); |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 424 | return; |
Marcel Holtmann | 8b8a531 | 2014-01-22 07:53:35 -0800 | [diff] [blame] | 425 | } |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 426 | |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 427 | client_active = true; |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static int open_unix(const char *path) |
| 431 | { |
| 432 | struct sockaddr_un addr; |
| 433 | int fd; |
| 434 | |
| 435 | unlink(path); |
| 436 | |
| 437 | fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 438 | if (fd < 0) { |
Marcel Holtmann | 15097d3 | 2013-12-30 21:26:31 -0800 | [diff] [blame] | 439 | perror("Failed to open Unix server socket"); |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | memset(&addr, 0, sizeof(addr)); |
| 444 | addr.sun_family = AF_UNIX; |
| 445 | strcpy(addr.sun_path, path); |
| 446 | |
| 447 | if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Marcel Holtmann | 15097d3 | 2013-12-30 21:26:31 -0800 | [diff] [blame] | 448 | perror("Failed to bind Unix server socket"); |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 449 | close(fd); |
| 450 | return -1; |
| 451 | } |
| 452 | |
| 453 | if (listen(fd, 1) < 0) { |
Marcel Holtmann | 15097d3 | 2013-12-30 21:26:31 -0800 | [diff] [blame] | 454 | perror("Failed to listen Unix server socket"); |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 455 | close(fd); |
| 456 | return -1; |
| 457 | } |
| 458 | |
| 459 | if (chmod(path, 0666) < 0) |
| 460 | perror("Failed to change mode"); |
| 461 | |
| 462 | return fd; |
| 463 | } |
| 464 | |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 465 | static int open_tcp(const char *address, unsigned int port) |
Marcel Holtmann | 15097d3 | 2013-12-30 21:26:31 -0800 | [diff] [blame] | 466 | { |
| 467 | struct sockaddr_in addr; |
| 468 | int fd, opt = 1; |
| 469 | |
| 470 | fd = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 471 | if (fd < 0) { |
| 472 | perror("Failed to open TCP server socket"); |
| 473 | return -1; |
| 474 | } |
| 475 | |
| 476 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
| 477 | |
| 478 | memset(&addr, 0, sizeof(addr)); |
| 479 | addr.sin_family = AF_INET; |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 480 | addr.sin_addr.s_addr = inet_addr(address); |
Marcel Holtmann | 15097d3 | 2013-12-30 21:26:31 -0800 | [diff] [blame] | 481 | addr.sin_port = htons(port); |
| 482 | |
| 483 | if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 484 | perror("Failed to bind TCP server socket"); |
| 485 | close(fd); |
| 486 | return -1; |
| 487 | } |
| 488 | |
| 489 | if (listen(fd, 1) < 0) { |
| 490 | perror("Failed to listen TCP server socket"); |
| 491 | close(fd); |
| 492 | return -1; |
| 493 | } |
| 494 | |
| 495 | return fd; |
| 496 | } |
| 497 | |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 498 | static int connect_tcp(const char *address, unsigned int port) |
| 499 | { |
| 500 | struct sockaddr_in addr; |
| 501 | int fd; |
| 502 | |
| 503 | fd = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 504 | if (fd < 0) { |
| 505 | perror("Failed to open TCP client socket"); |
| 506 | return -1; |
| 507 | } |
| 508 | |
| 509 | memset(&addr, 0, sizeof(addr)); |
| 510 | addr.sin_family = AF_INET; |
| 511 | addr.sin_addr.s_addr = inet_addr(address); |
| 512 | addr.sin_port = htons(port); |
| 513 | |
| 514 | if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 515 | perror("Failed to connect TCP client socket"); |
| 516 | close(fd); |
| 517 | return -1; |
| 518 | } |
| 519 | |
| 520 | return fd; |
| 521 | } |
| 522 | |
| 523 | static int open_vhci(uint8_t type) |
| 524 | { |
| 525 | uint8_t create_req[2] = { 0xff, type }; |
| 526 | ssize_t written; |
| 527 | int fd; |
| 528 | |
| 529 | fd = open("/dev/vhci", O_RDWR | O_CLOEXEC); |
| 530 | if (fd < 0) { |
| 531 | perror("Failed to open /dev/vhci device"); |
| 532 | return -1; |
| 533 | } |
| 534 | |
| 535 | written = write(fd, create_req, sizeof(create_req)); |
| 536 | if (written < 0) { |
| 537 | perror("Failed to set device type"); |
| 538 | close(fd); |
| 539 | return -1; |
| 540 | } |
| 541 | |
| 542 | return fd; |
| 543 | } |
| 544 | |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 545 | static void signal_callback(int signum, void *user_data) |
| 546 | { |
| 547 | switch (signum) { |
| 548 | case SIGINT: |
| 549 | case SIGTERM: |
| 550 | mainloop_quit(); |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | static void usage(void) |
| 556 | { |
| 557 | printf("btproxy - Bluetooth controller proxy\n" |
| 558 | "Usage:\n"); |
| 559 | printf("\tbtproxy [options]\n"); |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 560 | printf("Options:\n" |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 561 | "\t-c, --connect <address> Connect to server\n" |
| 562 | "\t-l, --listen [address] Use TCP server\n" |
| 563 | "\t-u, --unix [path] Use Unix server\n" |
| 564 | "\t-p, --port <port> Use specified TCP port\n" |
| 565 | "\t-i, --index <num> Use specified controller\n" |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 566 | "\t-d, --debug Enable debugging output\n" |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 567 | "\t-h, --help Show help options\n"); |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static const struct option main_options[] = { |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 571 | { "connect", required_argument, NULL, 'c' }, |
| 572 | { "listen", optional_argument, NULL, 'l' }, |
Marcel Holtmann | 15097d3 | 2013-12-30 21:26:31 -0800 | [diff] [blame] | 573 | { "unix", optional_argument, NULL, 'u' }, |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 574 | { "port", required_argument, NULL, 'p' }, |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 575 | { "index", required_argument, NULL, 'i' }, |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 576 | { "debug", no_argument, NULL, 'd' }, |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 577 | { "version", no_argument, NULL, 'v' }, |
| 578 | { "help", no_argument, NULL, 'h' }, |
| 579 | { } |
| 580 | }; |
| 581 | |
| 582 | int main(int argc, char *argv[]) |
| 583 | { |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 584 | const char *connect_address = NULL; |
| 585 | const char *server_address = NULL; |
| 586 | const char *unix_path = NULL; |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 587 | unsigned short tcp_port = 0xb1ee; /* 45550 */ |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 588 | const char *str; |
| 589 | sigset_t mask; |
| 590 | |
| 591 | for (;;) { |
| 592 | int opt; |
| 593 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 594 | opt = getopt_long(argc, argv, "c:l::u::p:i:dvh", |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 595 | main_options, NULL); |
| 596 | if (opt < 0) |
| 597 | break; |
| 598 | |
| 599 | switch (opt) { |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 600 | case 'c': |
| 601 | connect_address = optarg; |
| 602 | break; |
| 603 | case 'l': |
| 604 | if (optarg) |
| 605 | server_address = optarg; |
| 606 | else |
| 607 | server_address = "0.0.0.0"; |
| 608 | break; |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 609 | case 'u': |
| 610 | if (optarg) |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 611 | unix_path = optarg; |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 612 | else |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 613 | unix_path = "/tmp/bt-server-bredr"; |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 614 | break; |
Marcel Holtmann | 15097d3 | 2013-12-30 21:26:31 -0800 | [diff] [blame] | 615 | case 'p': |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 616 | tcp_port = atoi(optarg); |
Marcel Holtmann | 15097d3 | 2013-12-30 21:26:31 -0800 | [diff] [blame] | 617 | break; |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 618 | case 'i': |
| 619 | if (strlen(optarg) > 3 && !strncmp(optarg, "hci", 3)) |
| 620 | str = optarg + 3; |
| 621 | else |
| 622 | str = optarg; |
| 623 | if (!isdigit(*str)) { |
| 624 | usage(); |
| 625 | return EXIT_FAILURE; |
| 626 | } |
| 627 | hci_index = atoi(str); |
| 628 | break; |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 629 | case 'd': |
| 630 | debug_enabled = true; |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 631 | break; |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 632 | case 'v': |
| 633 | printf("%s\n", VERSION); |
| 634 | return EXIT_SUCCESS; |
| 635 | case 'h': |
| 636 | usage(); |
| 637 | return EXIT_SUCCESS; |
| 638 | default: |
| 639 | return EXIT_FAILURE; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | if (argc - optind > 0) { |
| 644 | fprintf(stderr, "Invalid command line parameters\n"); |
| 645 | return EXIT_FAILURE; |
| 646 | } |
| 647 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 648 | if (unix_path && server_address) { |
| 649 | fprintf(stderr, "Invalid to specify TCP and Unix servers\n"); |
| 650 | return EXIT_FAILURE; |
| 651 | } |
| 652 | |
| 653 | if (connect_address && (unix_path || server_address)) { |
| 654 | fprintf(stderr, "Invalid to specify client and server mode\n"); |
| 655 | return EXIT_FAILURE; |
| 656 | } |
| 657 | |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 658 | mainloop_init(); |
| 659 | |
| 660 | sigemptyset(&mask); |
| 661 | sigaddset(&mask, SIGINT); |
| 662 | sigaddset(&mask, SIGTERM); |
| 663 | |
| 664 | mainloop_set_signal(&mask, signal_callback, NULL, NULL); |
| 665 | |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 666 | if (connect_address) { |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 667 | int host_fd, dev_fd; |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 668 | |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 669 | printf("Connecting to %s:%u\n", connect_address, tcp_port); |
| 670 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 671 | dev_fd = connect_tcp(connect_address, tcp_port); |
| 672 | if (dev_fd < 0) |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 673 | return EXIT_FAILURE; |
| 674 | |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 675 | printf("Opening virtual device\n"); |
| 676 | |
Marcel Holtmann | 81294e5 | 2014-01-16 00:27:30 -0800 | [diff] [blame] | 677 | host_fd = open_vhci(0x00); |
| 678 | if (host_fd < 0) { |
| 679 | close(dev_fd); |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 680 | return EXIT_FAILURE; |
| 681 | } |
| 682 | |
Andrei Emeltchenko | e27ca4c | 2014-01-23 11:07:28 +0200 | [diff] [blame] | 683 | if (!setup_proxy(host_fd, false, dev_fd, true)) { |
| 684 | close(dev_fd); |
| 685 | close(host_fd); |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 686 | return EXIT_FAILURE; |
Andrei Emeltchenko | e27ca4c | 2014-01-23 11:07:28 +0200 | [diff] [blame] | 687 | } |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 688 | } else { |
Marcel Holtmann | f8c2b5e | 2013-12-31 23:16:16 -0800 | [diff] [blame] | 689 | int server_fd; |
| 690 | |
Marcel Holtmann | 98921e1 | 2013-12-30 23:34:18 -0800 | [diff] [blame] | 691 | if (unix_path) { |
| 692 | printf("Listening on %s\n", unix_path); |
| 693 | |
| 694 | server_fd = open_unix(unix_path); |
| 695 | } else if (server_address) { |
| 696 | printf("Listening on %s:%u\n", server_address, |
| 697 | tcp_port); |
| 698 | |
| 699 | server_fd = open_tcp(server_address, tcp_port); |
| 700 | } else { |
| 701 | fprintf(stderr, "Missing emulator device\n"); |
| 702 | return EXIT_FAILURE; |
| 703 | } |
| 704 | |
| 705 | if (server_fd < 0) |
| 706 | return EXIT_FAILURE; |
| 707 | |
| 708 | mainloop_add_fd(server_fd, EPOLLIN, server_callback, |
| 709 | NULL, NULL); |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 710 | } |
| 711 | |
Marcel Holtmann | 4317723 | 2013-12-30 20:48:31 -0800 | [diff] [blame] | 712 | return mainloop_run(); |
| 713 | } |