23 #include "gnunet_private_config.h"
25 #include <bluetooth/bluetooth.h>
26 #include <bluetooth/hci.h>
27 #include <bluetooth/hci_lib.h>
28 #include <bluetooth/rfcomm.h>
29 #include <bluetooth/sdp.h>
30 #include <bluetooth/sdp_lib.h>
35 #include <sys/ioctl.h>
36 #include <sys/param.h>
37 #include <sys/socket.h>
39 #include <sys/types.h>
66 #define BLUEZ_DEVNAME_SIZE 8
124 struct BroadcastMessages
156 static int searching_devices_count = 0;
176 #define ALIGN_FACTOR 8
181 #define MIN_BUFFER_SIZE sizeof(struct GNUNET_MessageHeader)
249 fprintf (stderr,
"Failed to allocate buffer for tokenizer\n");
253 if (NULL ==
ret->hdr)
255 fprintf (stderr,
"Failed to allocate buffer for alignment\n");
286 unsigned long offset;
290 ibuf = (
char *) mst->
hdr;
304 memmove (ibuf, &ibuf[mst->
off], mst->
pos);
325 want = ntohs (hdr->
size);
329 "Received invalid message from stdin\n");
337 memmove (ibuf, &ibuf[mst->
off], mst->
pos);
344 fprintf (stderr,
"Error! We should proceeded 0 bytes\n");
347 mst->
hdr = realloc (mst->
hdr, want);
348 if (NULL == mst->
hdr)
350 fprintf (stderr,
"Failed to allocate buffer for alignment\n");
353 ibuf = (
char *) mst->
hdr;
357 if (mst->
pos - mst->
off < want)
362 fprintf (stderr,
"The size of the buffer will be exceeded!\n");
370 if (mst->
pos - mst->
off < want)
389 "There should some valid bytes in the buffer on this stage\n");
396 offset = (
unsigned long)
buf;
402 want = ntohs (hdr->
size);
406 "Received invalid message from stdin\n");
429 if (NULL == mst->
hdr)
431 fprintf (stderr,
"Failed to allocate buffer for alignment\n");
434 ibuf = (
char *) mst->
hdr;
440 "Assertion failed\n");
473 static const unsigned long int crc_tbl_osdep[256] = {
474 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
475 0xE963A535, 0x9E6495A3,
476 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD,
477 0xE7B82D07, 0x90BF1D91,
478 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB,
479 0xF4D4B551, 0x83D385C7,
480 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
481 0xFA0F3D63, 0x8D080DF5,
482 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447,
483 0xD20D85FD, 0xA50AB56B,
484 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75,
485 0xDCD60DCF, 0xABD13D59,
486 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
487 0xCFBA9599, 0xB8BDA50F,
488 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11,
489 0xC1611DAB, 0xB6662D3D,
490 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F,
491 0x9FBFE4A5, 0xE8B8D433,
492 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
493 0x91646C97, 0xE6635C01,
494 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B,
495 0x8208F4C1, 0xF50FC457,
496 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49,
497 0x8CD37CF3, 0xFBD44C65,
498 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
499 0xA4D1C46D, 0xD3D6F4FB,
500 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5,
501 0xAA0A4C5F, 0xDD0D7CC9,
502 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3,
503 0xB966D409, 0xCE61E49F,
504 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
505 0xB7BD5C3B, 0xC0BA6CAD,
506 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF,
507 0x04DB2615, 0x73DC1683,
508 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D,
509 0x0A00AE27, 0x7D079EB1,
510 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
511 0x196C3671, 0x6E6B06E7,
512 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9,
513 0x17B7BE43, 0x60B08ED5,
514 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767,
515 0x3FB506DD, 0x48B2364B,
516 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
517 0x316E8EEF, 0x4669BE79,
518 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703,
519 0x220216B9, 0x5505262F,
520 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31,
521 0x2CD99E8B, 0x5BDEAE1D,
522 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
523 0x72076785, 0x05005713,
524 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D,
525 0x7CDCEFB7, 0x0BDBDF21,
526 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B,
527 0x6FB077E1, 0x18B74777,
528 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
529 0x616BFFD3, 0x166CCF45,
530 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7,
531 0x4969474D, 0x3E6E77DB,
532 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5,
533 0x47B2CF7F, 0x30B5FFE9,
534 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
535 0x54DE5729, 0x23D967BF,
536 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1,
537 0x5A05DF1B, 0x2D02EF8D
540 unsigned long crc = 0xFFFFFFFF;
543 crc = crc_tbl_osdep[(crc ^ *
buf) & 0xFF] ^ (crc >> 8);
563 if ((((crc) & 0xFF) ==
buf[0]) && (((crc >> 8) & 0xFF) ==
buf[1]) &&
564 ( ((crc >> 16) & 0xFF) ==
buf[2]) && ( ((crc >> 24) & 0xFF) ==
buf[3]) )
580 bind_socket (
int socket,
struct sockaddr_rc *addr)
588 addr->rc_channel =
port;
589 status = bind (socket, (
struct sockaddr *) addr,
sizeof(
struct
610 {uint8_t svc_uuid_int[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
624 const char *service_dsc =
"Bluetooth plugin services";
625 const char *service_prov =
"GNUnet provider";
626 uuid_t root_uuid, rfcomm_uuid, svc_uuid;
627 sdp_list_t *root_list = 0, *rfcomm_list = 0, *proto_list = 0,
628 *access_proto_list = 0, *svc_list = 0;
630 sdp_data_t *channel = 0;
632 record = sdp_record_alloc ();
635 sdp_uuid128_create (&svc_uuid, &svc_uuid_int);
636 svc_list = sdp_list_append (0, &svc_uuid);
637 sdp_set_service_classes (
record, svc_list);
638 sdp_set_service_id (
record, svc_uuid);
641 sdp_uuid16_create (&root_uuid, PUBLIC_BROWSE_GROUP);
642 root_list = sdp_list_append (0, &root_uuid);
643 sdp_set_browse_groups (
record, root_list);
646 sdp_uuid16_create (&rfcomm_uuid, RFCOMM_UUID);
647 channel = sdp_data_alloc (SDP_UINT8, &rc_channel);
648 rfcomm_list = sdp_list_append (0, &rfcomm_uuid);
649 sdp_list_append (rfcomm_list, channel);
650 proto_list = sdp_list_append (0, rfcomm_list);
653 access_proto_list = sdp_list_append (0, proto_list);
654 sdp_set_access_protos (
record, access_proto_list);
657 sdp_set_info_attr (
record, dev->
iface, service_prov, service_dsc);
660 dev->
session = sdp_connect (BDADDR_ANY, BDADDR_LOCAL, SDP_RETRY_IF_BUSY);
665 "Failed to connect to the SDP server on interface `%.*s': %s\n",
666 IFNAMSIZ, dev->
iface, strerror (errno));
675 "Failed to register a service record on interface `%.*s': %s\n",
676 IFNAMSIZ, dev->
iface, strerror (errno));
682 sdp_data_free (channel);
683 sdp_list_free (root_list, 0);
684 sdp_list_free (rfcomm_list, 0);
685 sdp_list_free (proto_list, 0);
686 sdp_list_free (access_proto_list, 0);
687 sdp_list_free (svc_list, 0);
704 {uint8_t svc_uuid_int[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
715 sdp_session_t *session = 0;
716 sdp_list_t *search_list = 0, *attrid_list = 0, *response_list = 0, *it = 0;
718 uint32_t range = 0x0000ffff;
722 session = sdp_connect (BDADDR_ANY, &
dest, 0);
726 "Failed to connect to the SDP server on interface `%.*s': %s\n",
727 IFNAMSIZ, dev->
iface, strerror (errno));
731 sdp_uuid128_create (&svc_uuid, &svc_uuid_int);
732 search_list = sdp_list_append (0, &svc_uuid);
733 attrid_list = sdp_list_append (0, &range);
735 if (sdp_service_search_attr_req (session, search_list,
736 SDP_ATTR_REQ_RANGE, attrid_list,
737 &response_list) == 0)
739 for (it = response_list; it; it = it->next)
741 sdp_record_t *
record = (sdp_record_t *) it->data;
742 sdp_list_t *proto_list = 0;
743 if (sdp_get_access_protos (
record, &proto_list) == 0)
745 channel = sdp_get_proto_port (proto_list, RFCOMM_UUID);
746 sdp_list_free (proto_list, 0);
752 sdp_list_free (search_list, 0);
753 sdp_list_free (attrid_list, 0);
754 sdp_list_free (response_list, 0);
760 "Failed to find the listening channel for interface `%.*s': %s\n",
781 unsigned char *
buf,
size_t buf_size,
784 unsigned char tmpbuf[buf_size];
786 count = read (*((
int *) sock), tmpbuf, buf_size);
793 fprintf (stderr,
"Failed to read from the HCI socket: %s\n", strerror (
802 struct sockaddr_rc rc_addr = { 0 };
804 memset (&rc_addr, 0,
sizeof(rc_addr));
805 len =
sizeof(rc_addr);
806 if (0 > getsockname (*((
int *) sock), (
struct sockaddr *) &rc_addr,
809 fprintf (stderr,
"getsockname() call failed : %s\n", strerror (errno));
813 memset (ri, 0,
sizeof(*ri));
820 count -=
sizeof(uint32_t);
838 int i, dev_id = -1, fd_hci;
841 struct hci_dev_list_req
list;
842 struct hci_dev_req dev[HCI_MAX_DEV];
844 struct sockaddr_rc rc_addr = { 0 };
852 fd_hci = socket (AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
857 "Failed to create HCI socket: %s\n",
863 request.list.dev_num = HCI_MAX_DEV;
865 if (ioctl (fd_hci, HCIGETDEVLIST, (
void *) &
request) < 0)
868 "ioctl(HCIGETDEVLIST) on interface `%.*s' failed: %s\n",
872 (void) close (fd_hci);
877 for (i = 0; i <
request.list.dev_num; i++)
879 struct hci_dev_info dev_info;
881 memset (&dev_info, 0,
sizeof(
struct hci_dev_info));
882 dev_info.dev_id =
request.dev[i].dev_id;
885 if (ioctl (fd_hci, HCIGETDEVINFO, (
void *) &dev_info))
888 "ioctl(HCIGETDEVINFO) on interface `%.*s' failed: %s\n",
892 (void) close (fd_hci);
898 dev_id = dev_info.dev_id;
905 if (hci_test_bit (HCI_UP, (
void *) &dev_info.flags) == 0)
908 if (ioctl (fd_hci, HCIDEVUP, dev_info.dev_id))
911 "ioctl(HCIDEVUP) on interface `%.*s' failed: %s\n",
915 (void) close (fd_hci);
921 if ((hci_test_bit (HCI_PSCAN, (
void *) &dev_info.flags) == 0) ||
922 (hci_test_bit (HCI_ISCAN, (
void *) &dev_info.flags) == 0) )
925 struct hci_dev_req dev_req;
927 memset (&dev_req, 0,
sizeof(dev_req));
928 dev_req.dev_id = dev_info.dev_id;
929 dev_req.dev_opt = SCAN_PAGE | SCAN_INQUIRY;
931 if (ioctl (fd_hci, HCISETSCAN, (
unsigned long) &dev_req))
934 "ioctl(HCISETSCAN) on interface `%.*s' failed: %s\n",
938 (void) close (fd_hci);
950 "The interface %s was not found\n",
952 (void) close (fd_hci);
957 (void) close (fd_hci);
961 memset (&rc_addr, 0,
sizeof(rc_addr));
962 rc_addr.rc_family = AF_BLUETOOTH;
963 rc_addr.rc_bdaddr = *BDADDR_ANY;
965 if (bind_socket (dev->
fd_rfcomm, &rc_addr) != 0)
968 "Failed to bind interface `%.*s': %s\n",
979 "Failed to register a service on interface `%.*s': %s\n",
981 dev->
iface, strerror (errno));
988 fprintf (stderr,
"Failed to listen on socket for interface `%.*s': %s\n",
990 dev->
iface, strerror (errno));
1026 test_bluetooth_interface (
const char *iface)
1032 ret = snprintf (strbuf,
sizeof(strbuf),
1033 "/sys/class/bluetooth/%s/subsystem",
1035 if ((
ret < 0) || (
ret >=
sizeof(strbuf)) || (0 != stat (strbuf, &sbuf)))
1038 "Did not find 802.15.1 interface `%s'. Exiting.\n",
1093 sendsize = ntohs (hdr->
size);
1098 fprintf (stderr,
"Received malformed message\n");
1105 fprintf (stderr,
"Packet too big for buffer\n");
1145 inquiry_info *devices = NULL;
1146 int i, responses, max_responses =
MAX_PORTS;
1152 "%.*s reached the top limit for the discovarable devices\n",
1161 char addr[19] = { 0 };
1163 ba2str ((bdaddr_t *) &dev->
pl_mac, addr);
1168 "Failed to get the device id for interface %.*s : %s\n",
1170 dev->
iface, strerror (errno));
1175 devices = malloc (max_responses *
sizeof(inquiry_info));
1176 if (devices == NULL)
1179 "Failed to allocate memory for inquiry info list on interface %.*s\n",
1185 responses = hci_inquiry (
neighbours.dev_id, 8, max_responses, NULL,
1186 &devices, IREQ_CACHE_FLUSH);
1189 fprintf (stderr,
"Failed to inquiry on interface %.*s\n", IFNAMSIZ,
1194 fprintf (stderr,
"LOG : Found %d devices\n", responses);
1198 fprintf (stderr,
"LOG : No devices discoverable\n");
1202 for (i = 0; i < responses; i++)
1211 "%.*s reached the top limit for the discoverable devices (after inquiry)\n",
1220 if (memcmp (&(devices + i)->bdaddr, &(
neighbours.devices[j]),
1221 sizeof(bdaddr_t)) == 0)
1224 fprintf (stderr,
"LOG : the device already exists on the list\n");
1231 char addr[19] = { 0 };
1233 ba2str (&(devices + i)->bdaddr, addr);
1234 fprintf (stderr,
"LOG : %s was added to the list\n", addr);
1237 bdaddr,
sizeof(bdaddr_t));
1245 int connection_successful = 0;
1246 struct sockaddr_rc addr_rc = { 0 };
1248 addr_rc.rc_family = AF_BLUETOOTH;
1256 memset (&addr_rc.rc_bdaddr, 0,
sizeof(addr_rc.rc_bdaddr));
1258 sizeof(addr_rc.rc_bdaddr));
1260 addr_rc.rc_channel =
get_channel (dev, addr_rc.rc_bdaddr);
1262 *sendsocket = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
1263 if ((-1 < *sendsocket) &&
1264 (0 == connect (*sendsocket,
1265 (
struct sockaddr *) &addr_rc,
1269 connection_successful = 1;
1270 char addr[19] = { 0 };
1272 fprintf (stderr,
"LOG : Connected to %s\n", addr);
1277 char addr[19] = { 0 };
1280 if (-1 != *sendsocket)
1282 (void) close (*sendsocket);
1287 "LOG : Couldn't connect on device %s, error : %s\n",
1290 if (errno != ECONNREFUSED)
1292 fprintf (stderr,
"LOG : Removes %d device from the list\n",
1318 fprintf (stderr,
"LOG : Search for a new device\n");
1327 searching_devices_count += 1;
1329 if (searching_devices_count ==
MAX_LOOPS)
1331 fprintf (stderr,
"LOG : Force to inquiry for new devices\n");
1332 searching_devices_count = 0;
1333 goto inquiry_devices;
1337 if (connection_successful == 0)
1347 if (errno_copy == ECONNREFUSED)
1349 fprintf (stderr,
"LOG : No device found. Go back and search again\n");
1352 goto search_for_devices;
1396 int broadcast, sendsocket;
1400 #ifdef HAVE_SETRESUID
1401 uid_t uid = getuid ();
1403 if (0 != setresuid (uid, 0, 0))
1406 "Failed to setresuid to root: %s\n",
1411 if (0 != seteuid (0))
1414 "Failed to seteuid back to root: %s\n", strerror (errno));
1421 memset (&dev, 0,
sizeof(dev));
1422 dev.
fd_rfcomm = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
1429 "You must specify the name of the interface as the first \
1430 and only argument to this program.\n");
1438 fprintf (stderr,
"Failed to create a RFCOMM socket: %s\n", strerror (
1444 fprintf (stderr,
"File descriptor too large for select (%d > %d)\n",
1449 if (0 != test_bluetooth_interface (argv[1]))
1454 strncpy (dev.
iface, argv[1], IFNAMSIZ);
1463 uid_t uid = getuid ();
1464 #ifdef HAVE_SETRESUID
1465 if (0 != setresuid (uid, uid, uid))
1467 fprintf (stderr,
"Failed to setresuid: %s\n", strerror (errno));
1473 if (0 != (setuid (uid) | seteuid (uid)))
1475 fprintf (stderr,
"Failed to setuid: %s\n", strerror (errno));
1487 macmsg.
hdr.
size = htons (
sizeof(macmsg));
1512 FD_SET (STDIN_FILENO, &rfds);
1513 maxfd = MAX (maxfd, STDIN_FILENO);
1521 for (i = 0; i < crt_rfds; i++)
1523 FD_SET (rfds_list[i], &rfds);
1524 maxfd = MAX (maxfd, rfds_list[i]);
1529 FD_SET (STDOUT_FILENO, &wfds);
1530 maxfd = MAX (maxfd, STDOUT_FILENO);
1544 else if (memcmp (&frame->
addr1, &broadcast_address,
1547 fprintf (stderr,
"LOG : %s has a broadcast message (pos %d, size %d)\n",
1550 if (send_broadcast (&dev, &sendsocket) != 0)
1555 "LOG : Skipping the broadcast message (pos %d, size %d)\n",
1560 FD_SET (sendsocket, &wfds);
1561 maxfd = MAX (maxfd, sendsocket);
1572 sizeof(bdaddr_t)) == 0)
1581 fprintf (stderr,
"LOG: the address was found in the list\n");
1589 struct sockaddr_rc addr = { 0 };
1592 "LOG : %s has a new message for %.2X:%.2X:%.2X:%.2X:%.2X:%.2X which isn't on the broadcast list\n",
1599 sendsocket = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
1604 "Failed to create a RFCOMM socket (sending stage): %s\n",
1610 addr.rc_family = AF_BLUETOOTH;
1611 addr.rc_channel =
get_channel (&dev, addr.rc_bdaddr);
1615 status = connect (sendsocket, (
struct sockaddr *) &addr,
1617 if ((0 !=
status) && (errno != EAGAIN) )
1619 if ((errno == ECONNREFUSED) && (tries < 2) )
1621 fprintf (stderr,
"LOG : %.*s failed to connect. Trying again!\n",
1622 IFNAMSIZ, dev.
iface);
1626 else if (errno == EBADF)
1628 fprintf (stderr,
"LOG : %s failed to connect : %s. Skip it!\n",
1629 dev.
iface, strerror (errno));
1636 "LOG : %s failed to connect : %s. Try again later!\n",
1645 FD_SET (sendsocket, &wfds);
1646 maxfd = MAX (maxfd, sendsocket);
1647 fprintf (stderr,
"LOG : Connection successful\n");
1659 &addr.rc_bdaddr,
sizeof(bdaddr_t));
1664 "The top limit for the discovarable devices' list was reached\n");
1676 int retval = select (maxfd + 1, &rfds, &wfds, NULL, NULL);
1677 if ((-1 == retval) && (EINTR == errno))
1679 if ((0 > retval) && (errno != EBADF) )
1681 fprintf (stderr,
"select failed: %s\n", strerror (errno));
1685 if (FD_ISSET (STDOUT_FILENO, &wfds))
1692 fprintf (stderr,
"Failed to write to STDOUT: %s\n", strerror (errno));
1701 fprintf (stderr,
"LOG : %s sends a message to STDOUT\n", dev.
iface);
1703 if (-1 != sendsocket)
1705 if (FD_ISSET (sendsocket, &wfds))
1707 ssize_t
ret = write (sendsocket,
1713 "Failed to write to bluetooth device: %s. Closing the socket!\n",
1719 (void) close (sendsocket);
1736 fprintf (stderr,
"Write error, partial send: %u/%u\n",
1747 fprintf (stderr,
"LOG : %s sends a message to a DEVICE\n",
1752 for (i = 0; i <= maxfd; i++)
1754 if (FD_ISSET (i, &rfds))
1756 if (i == STDIN_FILENO)
1759 read (i, readbuf,
sizeof(readbuf));
1763 "Read error from STDIN: %s\n",
1775 fprintf (stderr,
"LOG : %s receives a message from STDIN\n",
1782 struct sockaddr_rc addr = { 0 };
1783 unsigned int opt =
sizeof(addr);
1785 readsocket = accept (dev.
fd_rfcomm, (
struct sockaddr *) &addr,
1787 fprintf (stderr,
"LOG : %s accepts a message\n", dev.
iface);
1788 if (readsocket == -1)
1791 "Failed to accept a connection on interface: %.*s\n",
1798 FD_SET (readsocket, &rfds);
1799 maxfd = MAX (maxfd, readsocket);
1802 rfds_list[crt_rfds++] = readsocket;
1806 "The limit for the read file descriptors list was \
1816 fprintf (stderr,
"LOG : %s reads something from the socket\n",
1834 for (j = 0; j < crt_rfds; j++)
1836 if (rfds_list[j] == i)
1838 rfds_list[j] ^= rfds_list[crt_rfds - 1];
1839 rfds_list[crt_rfds - 1] ^= rfds_list[j];
1840 rfds_list[j] ^= rfds_list[crt_rfds - 1];
1846 fprintf (stderr,
"Read error from raw socket: %s\n", strerror (
1871 if (-1 != sendsocket)
1872 (void) close (sendsocket);
1874 for (i = 0; i < crt_rfds; i++)
1875 (
void) close (rfds_list[i]);
1885 char readbuf[
MAXLINE] = { 0 };
1886 SOCKADDR_BTH acc_addr = { 0 };
1887 int addr_len =
sizeof(SOCKADDR_BTH);
1888 int broadcast, i, stdin_open, crt_rfds = 0;
1889 HANDLE stdin_handle = GetStdHandle (STD_INPUT_HANDLE);
1890 HANDLE stdout_handle = GetStdHandle (STD_OUTPUT_HANDLE);
1894 if (stdin_handle == INVALID_HANDLE_VALUE)
1896 fprintf (stderr,
"Failed to get the stdin handle\n");
1900 if (stdout_handle == INVALID_HANDLE_VALUE)
1902 fprintf (stderr,
"Failed to get the stdout handle\n");
1907 initialize_windows_sockets ();
1919 if (dev.handle == NULL)
1921 fprintf (stderr,
"Failed to create RFCOMM socket: ");
1922 print_last_error ();
1929 fprintf (stderr,
"Failed to open the device\n");
1930 print_last_error ();
1933 fprintf (stderr,
"Failed to close the socket!\n");
1934 print_last_error ();
1941 fprintf (stderr,
"Failed to change the socket mode\n");
1956 macmsg.
hdr.
size = htons (
sizeof(macmsg));
1959 GNUNET_TRANSPORT_WLAN_MacAddress_Copy));
1970 int stdout_pos = -1;
1995 for (i = 0; i < crt_rfds; i++)
2015 if (strcmp (argv[1],
"ff:ff:ff:ff:ff:ff") == 0)
2017 fprintf (stderr,
"LOG: BROADCAST! Skipping the message\n");
2026 fprintf (stderr,
"LOG : has a new message for %s\n", argv[1]);
2030 if (sendsocket == NULL)
2032 fprintf (stderr,
"Failed to create RFCOMM socket: \n");
2033 print_last_error ();
2037 memset (&addr, 0,
sizeof(addr));
2040 WSAStringToAddress (argv[1], AF_BTH, NULL, (LPSOCKADDR) &addr,
2043 fprintf (stderr,
"Failed to translate the address: ");
2044 print_last_error ();
2048 if (addr.port == -1)
2051 "Couldn't find the sdp service for the address: %s\n",
2063 fprintf (stderr,
"Failed to connect: ");
2064 print_last_error ();
2070 fprintf (stderr,
"Failed to change the socket mode\n");
2085 fprintf (stderr,
"Select error\n");
2089 if (retval == stdout_pos)
2091 fprintf (stderr,
"LOG : sends a message to STDOUT\n");
2099 fprintf (stderr,
"Failed to write to STDOUT: ");
2100 print_last_error ();
2106 fprintf (stderr,
"Failed to write to STDOUT\n");
2117 if (sendsocket != NULL)
2129 "Failed to send to the socket. Closing the socket. Error: \n");
2130 print_last_error ();
2133 fprintf (stderr,
"Failed to close the sendsocket!\n");
2134 print_last_error ();
2144 fprintf (stderr,
"Write error, partial send: %u/%u\n",
2155 fprintf (stderr,
"LOG : sends a message to a DEVICE\n");
2161 if (retval == stdin_pos)
2167 if (FALSE == ReadFile (stdin_handle, readbuf,
sizeof(readbuf), &
ret,
2170 fprintf (stderr,
"Read error from STDIN: ");
2171 print_last_error ();
2182 fprintf (stderr,
"LOG : receives a message from STDIN\n");
2187 fprintf (stderr,
"LOG: accepting connection\n");
2190 (LPSOCKADDR) &acc_addr,
2192 if (readsocket == NULL)
2194 fprintf (stderr,
"Accept error %d: ", GetLastError ());
2195 print_last_error ();
2202 fprintf (stderr,
"Failed to change the socket mode\n");
2208 rfds_list[crt_rfds++] = readsocket;
2212 "The limit for the read file descriptors list was reached\n");
2218 for (i = 0; i < crt_rfds; i++)
2224 fprintf (stderr,
"LOG: reading something from the socket\n");
2228 char *) &rrm->
frame,
2240 fprintf (stderr,
"Failed to close the sendsocket!\n");
2241 print_last_error ();
2244 fprintf (stderr,
"Read error from raw socket: ");
2245 print_last_error ();
2270 fprintf (stderr,
"Failed to close the socket!\n");
2271 print_last_error ();
2274 for (i = 0; i < crt_rfds; i++)
2278 fprintf (stderr,
"Failed to close the socket!\n");
2279 print_last_error ();
static size_t do_align(size_t start_position, size_t end_position)
Given the start and end position of a block of data, return the end position of that data after align...
static int ret
Return value of the commandline.
static int list
Set if we should print a list of currently running services.
static uint16_t port
Port number.
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
struct GNUNET_MessageStreamTokenizer * stdin_mst
Tokenizer for the data we get from stdin.
#define MIN_BUFFER_SIZE
Smallest supported message.
static struct SendBuffer write_std
Buffer for data read from the bluetooth device to be transmitted to stdout.
static struct SendBuffer write_pout
Buffer for data read from stdin to be transmitted to the bluetooth device.
#define ALIGN_FACTOR
To what multiple do we align messages? 8 byte should suffice for everyone for now.
int main(int argc, char *argv[])
Main function of the helper.
static unsigned long calc_crc_osdep(const unsigned char *buf, size_t len)
Calculate crc32, the start of the calculation.
static int mac_test(const struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader, const struct HardwareInfos *dev)
Test incoming packets mac for being our own.
static void mac_set(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader, const struct HardwareInfos *dev)
Set the header to sane values to make attacks more difficult.
void(* MessageTokenizerCallback)(void *cls, const struct GNUNET_MessageHeader *message)
Functions with this signature are called whenever a complete message is received by the tokenizer.
static int mst_receive(struct MessageStreamTokenizer *mst, const char *buf, size_t size)
Add incoming data to the receive buffer and call the callback for all complete messages.
#define MAXLINE
Maximum size of a message allowed in either direction (used for our receive and sent buffers).
#define MAX_PORTS
Maximum number of ports assignable for RFCOMMM protocol.
#define BLUEZ_DEVNAME_SIZE
In bluez library, the maximum name length of a device is 8.
static int open_device(struct HardwareInfos *dev)
Open the bluetooth interface for reading/writing.
static void mst_destroy(struct MessageStreamTokenizer *mst)
Destroys a tokenizer.
static ssize_t read_from_the_socket(void *sock, unsigned char *buf, size_t buf_size, struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *ri)
Read from the socket and put the result into the buffer for transmission to 'stdout'.
static int get_channel(struct HardwareInfos *dev, bdaddr_t dest)
Function used for searching and browsing for a service.
static int register_service(struct HardwareInfos *dev, int rc_channel)
Function used for creating the service record and registering it.
static int check_crc_buf_osdep(const unsigned char *buf, size_t len)
Calculate and check crc of the bluetooth packet.
static struct MessageStreamTokenizer * mst_create(MessageTokenizerCallback cb, void *cb_cls)
Create a message stream tokenizer.
static void stdin_send_hw(void *cls, const struct GNUNET_MessageHeader *hdr)
Process data from the stdin.
#define MAX_LOOPS
Maximum number of loops without inquiring for new devices.
uint16_t status
See PRISM_STATUS_*-constants.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static struct GNUNET_CONTAINER_MultiPeerMap * neighbours
Map from PIDs to struct Neighbour entries.
static struct GNUNET_PeerIdentity all_zeros
Peer identity that is all zeros, used as a way to indicate "all peers".
#define IEEE80211_FC0_TYPE_DATA
static struct GNUNET_VPN_RedirectionRequest * request
Opaque redirection request handle.
Constants for network protocols.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
struct GNUNET_NETWORK_FDSet * GNUNET_NETWORK_fdset_create(void)
Creates an fd set.
void GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds)
Reset FD set (clears all file descriptors).
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_set_blocking(struct GNUNET_NETWORK_Handle *fd, int doBlock)
Set if a socket should use blocking or non-blocking IO.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_connect(const struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Connect a socket to some remote address.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc, struct sockaddr *address, socklen_t *address_len)
Accept a new connection on a socket.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
void GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_NETWORK_Handle *desc)
Add a socket to the FD set.
ssize_t GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc, const void *buffer, size_t length)
Send data (always non-blocking).
int GNUNET_NETWORK_socket_select(struct GNUNET_NETWORK_FDSet *rfds, struct GNUNET_NETWORK_FDSet *wfds, struct GNUNET_NETWORK_FDSet *efds, struct GNUNET_TIME_Relative timeout)
Check if sockets meet certain conditions.
int GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_NETWORK_Handle *desc)
Check whether a socket is part of the fd set.
void GNUNET_NETWORK_fdset_handle_set(struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h)
Add a file handle to the fd set.
#define GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER
Type of data messages from the plugin to the gnunet-wlan-helper.
#define GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL
Control message between the gnunet-wlan-helper and the daemon (with the MAC).
#define GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER
Type of data messages from the gnunet-wlan-helper to the plugin.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_forever_(void)
Return "forever".
static unsigned int size
Size of the "table".
header for transport plugin and the helper for wlan
static GNUNET_NETWORK_STRUCT_END const struct GNUNET_TRANSPORT_WLAN_MacAddress mac_bssid_gnunet
GNUnet bssid.
static const struct GNUNET_TRANSPORT_WLAN_MacAddress bc_all_mac
Broadcast MAC.
#define MAC_ADDR_SIZE
Number fo bytes in a mac address.
static struct GNUNET_TIME_Relative delta
unsigned int size
Number of entries in the map.
Handle used to access files (and pipes).
collection of IO descriptors
Format of a WLAN Control Message.
struct GNUNET_MessageHeader hdr
Message header.
struct GNUNET_TRANSPORT_WLAN_MacAddress mac
MAC Address of the local WLAN interface.
generic definitions for IEEE 802.11 frames
uint16_t frame_control
802.11 Frame Control field.
struct GNUNET_TRANSPORT_WLAN_MacAddress addr1
Address 1: destination address in ad-hoc mode or AP, BSSID if station,.
struct GNUNET_TRANSPORT_WLAN_MacAddress addr3
Address 3: BSSID in ad-hoc mode, Destination if station, source if AP.
struct GNUNET_TRANSPORT_WLAN_MacAddress addr2
Address 2: source address if in ad-hoc-mode or station, BSSID if AP.
Message from the WLAN helper to the plugin: we have received the given message with the given perform...
uint32_t ri_channel
IEEE80211_RADIOTAP_CHANNEL, 0 if unknown.
struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame frame
IEEE Frame.
struct GNUNET_MessageHeader header
Type is 'GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER'.
Message from the plugin to the WLAN helper: send the given message with the given connection paramete...
struct GNUNET_MessageHeader header
Type is 'GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER'.
struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame frame
IEEE Frame to transmit (the sender MAC address will be overwritten by the helper as it does not trust...
struct for storing the information of the hardware.
char iface[IFNAMSIZ]
Name of the interface, not necessarily 0-terminated (!).
sdp_session_t * session
SDP session.
int fd_rfcomm
file descriptor for the rfcomm socket
struct GNUNET_TRANSPORT_WLAN_MacAddress pl_mac
MAC address of our own bluetooth interface.
Handle to a message stream tokenizer.
void * cb_cls
Closure for cb.
struct GNUNET_MessageHeader * hdr
Beginning of the buffer.
MessageTokenizerCallback cb
Function to call on completed messages.
size_t curr_buf
Size of the buffer (starting at 'hdr').
size_t pos
How many bytes in buffer are valid right now?
size_t off
How many bytes in buffer have we already processed?
IO buffer used for buffering data in transit (to wireless or to stdout).
size_t pos
How many bytes that were stored in 'buf' did we already write to the destination? Always smaller than...
size_t size
How many bytes of data are stored in 'buf' for transmission right now? Data always starts at offset 0...
char buf[4096 *2]
Buffered data; twice the maximum allowed message size as we add some headers.