aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-helper-transport-bluetooth.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/gnunet-helper-transport-bluetooth.c')
-rw-r--r--src/transport/gnunet-helper-transport-bluetooth.c352
1 files changed, 18 insertions, 334 deletions
diff --git a/src/transport/gnunet-helper-transport-bluetooth.c b/src/transport/gnunet-helper-transport-bluetooth.c
index 2ac705996..4696f48f8 100644
--- a/src/transport/gnunet-helper-transport-bluetooth.c
+++ b/src/transport/gnunet-helper-transport-bluetooth.c
@@ -21,30 +21,23 @@
21 */ 21 */
22#include "gnunet_config.h" 22#include "gnunet_config.h"
23 23
24#ifdef MINGW 24#define SOCKTYPE int
25 #include "platform.h" 25#include <bluetooth/bluetooth.h>
26 #include "gnunet_util_lib.h" 26#include <bluetooth/hci.h>
27 #include <bthdef.h> 27#include <bluetooth/hci_lib.h>
28 #include <ws2bth.h> 28#include <bluetooth/rfcomm.h>
29#else 29#include <bluetooth/sdp.h>
30 #define SOCKTYPE int 30#include <bluetooth/sdp_lib.h>
31 #include <bluetooth/bluetooth.h> 31#include <errno.h>
32 #include <bluetooth/hci.h> 32#include <linux/if.h>
33 #include <bluetooth/hci_lib.h> 33#include <stdio.h>
34 #include <bluetooth/rfcomm.h> 34#include <stdlib.h>
35 #include <bluetooth/sdp.h> 35#include <sys/ioctl.h>
36 #include <bluetooth/sdp_lib.h> 36#include <sys/param.h>
37 #include <errno.h> 37#include <sys/socket.h>
38 #include <linux/if.h> 38#include <sys/stat.h>
39 #include <stdio.h> 39#include <sys/types.h>
40 #include <stdlib.h> 40#include <unistd.h>
41 #include <sys/ioctl.h>
42 #include <sys/param.h>
43 #include <sys/socket.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #include <unistd.h>
47#endif
48 41
49#include "plugin_transport_wlan.h" 42#include "plugin_transport_wlan.h"
50#include "gnunet_protocols.h" 43#include "gnunet_protocols.h"
@@ -67,34 +60,6 @@
67 */ 60 */
68#define MAX_LOOPS 5 61#define MAX_LOOPS 5
69 62
70#ifdef MINGW
71/* Maximum size of the interface's name */
72 #define IFNAMSIZ 16
73
74 #ifndef NS_BTH
75 #define NS_BTH 16
76 #endif
77/**
78 * A copy of the MAC Address.
79 */
80struct GNUNET_TRANSPORT_WLAN_MacAddress_Copy {
81 UINT8 mac[MAC_ADDR_SIZE];
82};
83
84/**
85 * The UUID used for the SDP service.
86 * {31191E56-FA7E-4517-870E-71B86BBCC52F}
87 */
88 #define GNUNET_BLUETOOTH_SDP_UUID \
89 { \
90 0x31, 0x19, 0x1E, 0x56, \
91 0xFA, 0x7E, \
92 0x45, 0x17, \
93 0x87, 0x0E, \
94 0x71, 0xB8, 0x6B, 0xBC, 0xC5, 0x2F \
95 }
96#endif
97
98/** 63/**
99 * In bluez library, the maximum name length of a device is 8 64 * In bluez library, the maximum name length of a device is 8
100 */ 65 */
@@ -110,17 +75,6 @@ struct HardwareInfos {
110 */ 75 */
111 char iface[IFNAMSIZ]; 76 char iface[IFNAMSIZ];
112 77
113 #ifdef MINGW
114 /**
115 * socket handle
116 */
117 struct GNUNET_NETWORK_Handle *handle;
118
119 /**
120 * MAC address of our own bluetooth interface.
121 */
122 struct GNUNET_TRANSPORT_WLAN_MacAddress_Copy pl_mac;
123 #else
124 /** 78 /**
125 * file descriptor for the rfcomm socket 79 * file descriptor for the rfcomm socket
126 */ 80 */
@@ -135,7 +89,6 @@ struct HardwareInfos {
135 * SDP session 89 * SDP session
136 */ 90 */
137 sdp_session_t *session; 91 sdp_session_t *session;
138 #endif
139}; 92};
140 93
141/** 94/**
@@ -606,61 +559,6 @@ check_crc_buf_osdep(const unsigned char *buf, size_t len)
606 559
607 560
608/* ************** end of clone ***************** */ 561/* ************** end of clone ***************** */
609
610#ifdef MINGW
611/**
612 * Function used to get the code of last error and to print the type of error.
613 */
614static void
615print_last_error()
616{
617 LPVOID lpMsgBuf = NULL;
618
619 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
620 NULL, GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, NULL))
621 fprintf(stderr, "%s\n", (char *)lpMsgBuf);
622 else
623 fprintf(stderr, "Failed to format the message for the last error! Error number : %d\n", GetLastError());
624}
625
626/**
627 * Function used to initialize the Windows Sockets
628 */
629static void
630initialize_windows_sockets()
631{
632 WSADATA wsaData;
633 WORD wVersionRequested = MAKEWORD(2, 0);
634
635 if (WSAStartup(wVersionRequested, &wsaData) != NO_ERROR)
636 {
637 fprintf(stderr, "Error initializing window sockets!\n");
638 print_last_error();
639 ExitProcess(2);
640 }
641}
642
643/**
644 * Function used to convert the GUID.
645 * @param bytes the GUID represented as a char array
646 * @param uuid pointer to the GUID
647 */
648static void
649convert_guid(char *bytes, GUID * uuid)
650{
651 int i;
652
653 uuid->Data1 = ((bytes[0] << 24) & 0xff000000) | ((bytes[1] << 16) & 0x00ff0000) | ((bytes[2] << 8) & 0x0000ff00) | (bytes[3] & 0x000000ff);
654 uuid->Data2 = ((bytes[4] << 8) & 0xff00) | (bytes[5] & 0x00ff);
655 uuid->Data3 = ((bytes[6] << 8) & 0xff00) | (bytes[7] & 0x00ff);
656
657 for (i = 0; i < 8; i++)
658 {
659 uuid->Data4[i] = bytes[i + 8];
660 }
661}
662#endif
663
664#ifdef LINUX 562#ifdef LINUX
665/** 563/**
666 * Function for assigning a port number 564 * Function for assigning a port number
@@ -688,79 +586,6 @@ bind_socket(int socket, struct sockaddr_rc *addr)
688} 586}
689#endif 587#endif
690 588
691#ifdef MINGW
692/**
693 * Function used for creating the service record and registering it.
694 *
695 * @param dev pointer to the device struct
696 * @return 0 on success
697 */
698static int
699register_service(struct HardwareInfos *dev)
700{
701 /* advertise the service */
702 CSADDR_INFO addr_info;
703 WSAQUERYSET wqs;
704 GUID guid;
705 unsigned char uuid[] = GNUNET_BLUETOOTH_SDP_UUID;
706 SOCKADDR_BTH addr;
707 int addr_len = sizeof(SOCKADDR_BTH);
708 int fd;
709
710 /* get the port on which we are listening on */
711 memset(&addr, 0, sizeof(SOCKADDR_BTH));
712 fd = GNUNET_NETWORK_get_fd(dev->handle);
713 if (fd <= 0)
714 {
715 fprintf(stderr, "Failed to get the file descriptor\n");
716 return -1;
717 }
718 if (SOCKET_ERROR == getsockname(fd, (SOCKADDR*)&addr, &addr_len))
719 {
720 fprintf(stderr, "Failed to get the port on which we are listening on: \n");
721 print_last_error();
722 return -1;
723 }
724
725 /* save the device address */
726 GNUNET_memcpy(&dev->pl_mac, &addr.btAddr, sizeof(BTH_ADDR));
727
728 /* set the address information */
729 memset(&addr_info, 0, sizeof(CSADDR_INFO));
730 addr_info.iProtocol = BTHPROTO_RFCOMM;
731 addr_info.iSocketType = SOCK_STREAM;
732 addr_info.LocalAddr.lpSockaddr = (LPSOCKADDR)&addr;
733 addr_info.LocalAddr.iSockaddrLength = sizeof(addr);
734 addr_info.RemoteAddr.lpSockaddr = (LPSOCKADDR)&addr;
735 addr_info.RemoteAddr.iSockaddrLength = sizeof(addr);
736
737 convert_guid((char *)uuid, &guid);
738
739 /* register the service */
740 memset(&wqs, 0, sizeof(WSAQUERYSET));
741 wqs.dwSize = sizeof(WSAQUERYSET);
742 wqs.dwNameSpace = NS_BTH;
743 wqs.lpszServiceInstanceName = "GNUnet Bluetooth Service";
744 wqs.lpszComment = "This is the service used by the GNUnnet plugin transport";
745 wqs.lpServiceClassId = &guid;
746 wqs.dwNumberOfCsAddrs = 1;
747 wqs.lpcsaBuffer = &addr_info;
748 wqs.lpBlob = 0;
749
750 if (SOCKET_ERROR == WSASetService(&wqs, RNRSERVICE_REGISTER, 0))
751 {
752 fprintf(stderr, "Failed to register the SDP service: ");
753 print_last_error();
754 return -1;
755 }
756 else
757 {
758 fprintf(stderr, "The SDP service was registered\n");
759 }
760
761 return 0;
762}
763#else
764/** 589/**
765 * Function used for creating the service record and registering it. 590 * Function used for creating the service record and registering it.
766 * 591 *
@@ -849,96 +674,7 @@ register_service(struct HardwareInfos *dev, int rc_channel)
849 674
850 return 0; 675 return 0;
851} 676}
852#endif
853 677
854#ifdef MINGW
855/**
856 * Function for searching and browsing for a service. This will return the
857 * port number on which the service is running.
858 *
859 * @param dest target address
860 * @return channel
861 */
862static int
863get_channel(const char *dest)
864{
865 HANDLE h;
866 WSAQUERYSET *wqs;
867 DWORD wqs_len = sizeof(WSAQUERYSET);
868 int done = 0;
869 int channel = -1;
870 GUID guid;
871 unsigned char uuid[] = GNUNET_BLUETOOTH_SDP_UUID;
872
873 convert_guid((char *)uuid, &guid);
874
875 wqs = (WSAQUERYSET*)malloc(wqs_len);
876 ZeroMemory(wqs, wqs_len);
877
878 wqs->dwSize = sizeof(WSAQUERYSET);
879 wqs->lpServiceClassId = &guid;
880 wqs->dwNameSpace = NS_BTH;
881 wqs->dwNumberOfCsAddrs = 0;
882 wqs->lpszContext = (LPSTR)dest;
883
884 if (SOCKET_ERROR == WSALookupServiceBegin(wqs, LUP_FLUSHCACHE | LUP_RETURN_ALL, &h))
885 {
886 if (GetLastError() == WSASERVICE_NOT_FOUND)
887 {
888 fprintf(stderr, "WARNING! The device with address %s wasn't found. Skipping the message!", dest);
889 return -1;
890 }
891 else
892 {
893 fprintf(stderr, "Failed to find the port number: ");
894 print_last_error();
895 ExitProcess(2);
896 return -1;
897 }
898 }
899
900 /* search the sdp service */
901 while (!done)
902 {
903 if (SOCKET_ERROR == WSALookupServiceNext(h, LUP_FLUSHCACHE | LUP_RETURN_ALL, &wqs_len, wqs))
904 {
905 int error = WSAGetLastError();
906
907 switch (error)
908 {
909 case WSAEFAULT:
910 free(wqs);
911 wqs = (WSAQUERYSET*)malloc(wqs_len);
912 break;
913
914 case WSANO_DATA:
915 fprintf(stderr, "Failed! The address was valid but there was no data record of requested type\n");
916 done = 1;
917 break;
918
919 case WSA_E_NO_MORE:
920 done = 1;
921 break;
922
923 default:
924 fprintf(stderr, "Failed to look over the services: ");
925 print_last_error();
926 WSALookupServiceEnd(h);
927 ExitProcess(2);
928 }
929 }
930 else
931 {
932 channel = ((SOCKADDR_BTH*)wqs->lpcsaBuffer->RemoteAddr.lpSockaddr)->port;
933 }
934 }
935
936 free(wqs);
937 WSALookupServiceEnd(h);
938
939 return channel;
940}
941#else
942/** 678/**
943 * Function used for searching and browsing for a service. This will return the 679 * Function used for searching and browsing for a service. This will return the
944 * port number on which the service is running. 680 * port number on which the service is running.
@@ -1011,7 +747,6 @@ get_channel(struct HardwareInfos *dev, bdaddr_t dest)
1011 747
1012 return channel; 748 return channel;
1013} 749}
1014#endif
1015 750
1016/** 751/**
1017 * Read from the socket and put the result into the buffer for transmission to 'stdout'. 752 * Read from the socket and put the result into the buffer for transmission to 'stdout'.
@@ -1030,22 +765,14 @@ read_from_the_socket(void *sock,
1030{ 765{
1031 unsigned char tmpbuf[buf_size]; 766 unsigned char tmpbuf[buf_size];
1032 ssize_t count; 767 ssize_t count;
1033
1034 #ifdef MINGW
1035 count = GNUNET_NETWORK_socket_recv((struct GNUNET_NETWORK_Handle *)sock, tmpbuf, buf_size);
1036 #else
1037 count = read(*((int *)sock), tmpbuf, buf_size); 768 count = read(*((int *)sock), tmpbuf, buf_size);
1038 #endif
1039 769
1040 if (0 > count) 770 if (0 > count)
1041 { 771 {
1042 if (EAGAIN == errno) 772 if (EAGAIN == errno)
1043 return 0; 773 return 0;
1044 #if MINGW 774
1045 print_last_error();
1046 #else
1047 fprintf(stderr, "Failed to read from the HCI socket: %s\n", strerror(errno)); 775 fprintf(stderr, "Failed to read from the HCI socket: %s\n", strerror(errno));
1048 #endif
1049 776
1050 return -1; 777 return -1;
1051 } 778 }
@@ -1088,43 +815,6 @@ read_from_the_socket(void *sock,
1088static int 815static int
1089open_device(struct HardwareInfos *dev) 816open_device(struct HardwareInfos *dev)
1090{ 817{
1091 #ifdef MINGW
1092 SOCKADDR_BTH addr;
1093
1094 /* bind the RFCOMM socket to the interface */
1095 addr.addressFamily = AF_BTH;
1096 addr.btAddr = 0;
1097 addr.port = BT_PORT_ANY;
1098
1099 if (GNUNET_OK !=
1100 GNUNET_NETWORK_socket_bind(dev->handle, (const SOCKADDR*)&addr, sizeof(SOCKADDR_BTH)))
1101 {
1102 fprintf(stderr, "Failed to bind the socket: ");
1103 if (GetLastError() == WSAENETDOWN)
1104 {
1105 fprintf(stderr, "Please make sure that your Bluetooth device is ON!\n");
1106 ExitProcess(2);
1107 }
1108 print_last_error();
1109 return -1;
1110 }
1111
1112 /* start listening on the socket */
1113 if (GNUNET_NETWORK_socket_listen(dev->handle, 4) != GNUNET_OK)
1114 {
1115 fprintf(stderr, "Failed to listen on the socket: ");
1116 print_last_error();
1117 return -1;
1118 }
1119
1120 /* register the sdp service */
1121 if (register_service(dev) != 0)
1122 {
1123 fprintf(stderr, "Failed to register a service: ");
1124 print_last_error();
1125 return 1;
1126 }
1127 #else
1128 int i, dev_id = -1, fd_hci; 818 int i, dev_id = -1, fd_hci;
1129 struct { 819 struct {
1130 struct hci_dev_list_req list; 820 struct hci_dev_list_req list;
@@ -1279,7 +969,6 @@ open_device(struct HardwareInfos *dev)
1279 dev->iface, strerror(errno)); 969 dev->iface, strerror(errno));
1280 return 1; 970 return 1;
1281 } 971 }
1282 #endif
1283 972
1284 return 0; 973 return 0;
1285} 974}
@@ -1299,12 +988,7 @@ mac_set(struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader,
1299{ 988{
1300 taIeeeHeader->frame_control = htons(IEEE80211_FC0_TYPE_DATA); 989 taIeeeHeader->frame_control = htons(IEEE80211_FC0_TYPE_DATA);
1301 taIeeeHeader->addr3 = mac_bssid_gnunet; 990 taIeeeHeader->addr3 = mac_bssid_gnunet;
1302
1303 #ifdef MINGW
1304 GNUNET_memcpy(&taIeeeHeader->addr2, &dev->pl_mac, sizeof(struct GNUNET_TRANSPORT_WLAN_MacAddress));
1305 #else
1306 taIeeeHeader->addr2 = dev->pl_mac; 991 taIeeeHeader->addr2 = dev->pl_mac;
1307 #endif
1308} 992}
1309 993
1310#ifdef LINUX 994#ifdef LINUX