b1ee.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011-2012 Intel Corporation
  7. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #define _GNU_SOURCE
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <unistd.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <getopt.h>
  22. #include <ctype.h>
  23. #include <netdb.h>
  24. #include <arpa/inet.h>
  25. #include "lib/bluetooth.h"
  26. #include "lib/hci.h"
  27. #include "src/shared/mainloop.h"
  28. #define DEFAULT_HOST_PORT "45550" /* 0xb1ee */
  29. #define DEFAULT_SNIFFER_PORT "45551" /* 0xb1ef */
  30. static int sniffer_fd;
  31. static int server_fd;
  32. static int vhci_fd;
  33. static void usage(void)
  34. {
  35. printf("b1ee - Bluetooth device testing tool over internet\n"
  36. "Usage:\n");
  37. printf("\tb1ee [options] <host>\n");
  38. printf("options:\n"
  39. "\t-p, --port <port> Specify the server port\n"
  40. "\t-s, --sniffer-port <port> Specify the sniffer port\n"
  41. "\t-v, --version Show version information\n"
  42. "\t-h, --help Show help options\n");
  43. }
  44. static const struct option main_options[] = {
  45. { "port", required_argument, NULL, 'p' },
  46. { "sniffer-port", required_argument, NULL, 's' },
  47. { "version", no_argument, NULL, 'v' },
  48. { "help", no_argument, NULL, 'h' },
  49. { }
  50. };
  51. static char *set_port(char *str)
  52. {
  53. char *c;
  54. if (str == NULL || str[0] == '\0')
  55. return NULL;
  56. for (c = str; *c != '\0'; c++)
  57. if (isdigit(*c) == 0)
  58. return NULL;
  59. if (atol(str) > 65535)
  60. return NULL;
  61. return strdup(str);
  62. }
  63. static void sniffer_read_callback(int fd, uint32_t events, void *user_data)
  64. {
  65. static uint8_t buf[4096];
  66. ssize_t len;
  67. if (events & (EPOLLERR | EPOLLHUP))
  68. return;
  69. again:
  70. len = recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
  71. if (len < 0) {
  72. if (errno == EAGAIN)
  73. goto again;
  74. return;
  75. }
  76. printf("Sniffer received: %zi bytes\n", len);
  77. }
  78. static uint8_t *server_pkt_data;
  79. static uint8_t server_pkt_type;
  80. static uint16_t server_pkt_expect;
  81. static uint16_t server_pkt_len;
  82. static uint16_t server_pkt_offset;
  83. static void server_read_callback(int fd, uint32_t events, void *user_data)
  84. {
  85. static uint8_t buf[4096];
  86. uint8_t *ptr = buf;
  87. ssize_t len;
  88. uint16_t count;
  89. if (events & (EPOLLERR | EPOLLHUP))
  90. return;
  91. again:
  92. len = recv(fd, buf + server_pkt_offset,
  93. sizeof(buf) - server_pkt_offset, MSG_DONTWAIT);
  94. if (len < 0) {
  95. if (errno == EAGAIN)
  96. goto again;
  97. return;
  98. }
  99. count = server_pkt_offset + len;
  100. while (count > 0) {
  101. hci_event_hdr *evt_hdr;
  102. if (!server_pkt_data) {
  103. server_pkt_type = ptr[0];
  104. switch (server_pkt_type) {
  105. case HCI_EVENT_PKT:
  106. if (count < HCI_EVENT_HDR_SIZE + 1) {
  107. server_pkt_offset += len;
  108. return;
  109. }
  110. evt_hdr = (hci_event_hdr *) (ptr + 1);
  111. server_pkt_expect = HCI_EVENT_HDR_SIZE +
  112. evt_hdr->plen + 1;
  113. server_pkt_data = malloc(server_pkt_expect);
  114. server_pkt_len = 0;
  115. break;
  116. default:
  117. fprintf(stderr, "Unknown packet from server\n");
  118. return;
  119. }
  120. server_pkt_offset = 0;
  121. }
  122. if (count >= server_pkt_expect) {
  123. ssize_t written;
  124. memcpy(server_pkt_data + server_pkt_len,
  125. ptr, server_pkt_expect);
  126. ptr += server_pkt_expect;
  127. count -= server_pkt_expect;
  128. written = write(vhci_fd, server_pkt_data,
  129. server_pkt_len + server_pkt_expect);
  130. if (written != server_pkt_len + server_pkt_expect)
  131. fprintf(stderr, "Write to /dev/vhci failed\n");
  132. free(server_pkt_data);
  133. server_pkt_data = NULL;
  134. } else {
  135. memcpy(server_pkt_data + server_pkt_len, ptr, count);
  136. server_pkt_len += count;
  137. server_pkt_expect -= count;
  138. count = 0;
  139. }
  140. }
  141. }
  142. static void vhci_read_callback(int fd, uint32_t events, void *user_data)
  143. {
  144. unsigned char buf[4096];
  145. ssize_t len, written;
  146. if (events & (EPOLLERR | EPOLLHUP))
  147. return;
  148. len = read(fd, buf, sizeof(buf));
  149. if (len < 0)
  150. return;
  151. written = write(server_fd, buf, len);
  152. if (written != len)
  153. fprintf(stderr, "Write to server failed\n");
  154. }
  155. static void signal_callback(int signum, void *user_data)
  156. {
  157. switch (signum) {
  158. case SIGINT:
  159. case SIGTERM:
  160. mainloop_quit();
  161. break;
  162. }
  163. }
  164. static int do_connect(const char *node, const char *service)
  165. {
  166. struct addrinfo hints;
  167. struct addrinfo *info, *res;
  168. int err, fd = -1;
  169. memset(&hints, 0, sizeof(hints));
  170. hints.ai_family = PF_UNSPEC;
  171. hints.ai_socktype = SOCK_STREAM;
  172. err = getaddrinfo(node, service, &hints, &res);
  173. if (err) {
  174. perror(gai_strerror(err));
  175. exit(1);
  176. }
  177. for (info = res; info; info = info->ai_next) {
  178. char str[INET6_ADDRSTRLEN];
  179. inet_ntop(info->ai_family, info->ai_addr->sa_data,
  180. str, sizeof(str));
  181. fd = socket(info->ai_family, info->ai_socktype,
  182. info->ai_protocol);
  183. if (fd < 0)
  184. continue;
  185. printf("Trying to connect to %s on port %s\n", str, service);
  186. if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
  187. perror("Failed to connect");
  188. close(fd);
  189. continue;
  190. }
  191. printf("Successfully connected to %s on port %s\n",
  192. str, service);
  193. break;
  194. }
  195. freeaddrinfo(res);
  196. if (res == NULL)
  197. exit(1);
  198. return fd;
  199. }
  200. int main(int argc, char *argv[])
  201. {
  202. const char sniff_cmd[] = { 0x01, 0x00,
  203. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  204. char *server_port = NULL, *sniffer_port = NULL;
  205. int ret = EXIT_FAILURE;
  206. ssize_t written;
  207. for (;;) {
  208. int opt;
  209. opt = getopt_long(argc, argv, "s:p:vh", main_options, NULL);
  210. if (opt < 0)
  211. break;
  212. switch (opt) {
  213. case 'p':
  214. server_port = set_port(optarg);
  215. if (server_port == NULL)
  216. goto usage;
  217. break;
  218. case 's':
  219. sniffer_port = set_port(optarg);
  220. if (sniffer_port == NULL)
  221. goto usage;
  222. break;
  223. case 'v':
  224. printf("%s\n", VERSION);
  225. ret = EXIT_SUCCESS;
  226. goto done;
  227. case 'h':
  228. ret = EXIT_SUCCESS;
  229. goto usage;
  230. default:
  231. goto usage;
  232. }
  233. }
  234. argc = argc - optind;
  235. argv = argv + optind;
  236. optind = 0;
  237. if (argv[0] == NULL || argv[0][0] == '\0')
  238. goto usage;
  239. server_fd = do_connect(argv[0], server_port ? : DEFAULT_HOST_PORT);
  240. sniffer_fd = do_connect(argv[0],
  241. sniffer_port ? : DEFAULT_SNIFFER_PORT);
  242. written = write(sniffer_fd, sniff_cmd, sizeof(sniff_cmd));
  243. if (written < 0)
  244. perror("Failed to enable sniffer");
  245. vhci_fd = open("/dev/vhci", O_RDWR | O_NONBLOCK);
  246. if (vhci_fd < 0) {
  247. perror("Failed to /dev/vhci");
  248. close(server_fd);
  249. exit(1);
  250. }
  251. mainloop_init();
  252. mainloop_add_fd(sniffer_fd, EPOLLIN, sniffer_read_callback, NULL, NULL);
  253. mainloop_add_fd(server_fd, EPOLLIN, server_read_callback, NULL, NULL);
  254. mainloop_add_fd(vhci_fd, EPOLLIN, vhci_read_callback, NULL, NULL);
  255. ret = mainloop_run_with_signal(signal_callback, NULL);
  256. goto done;
  257. usage:
  258. usage();
  259. done:
  260. free(server_port);
  261. free(sniffer_port);
  262. return ret;
  263. }