diff options
author | Claudiu Olteanu <claudiu@140774ce-b5e7-0310-ab8b-a85725594a96> | 2013-09-18 12:53:30 +0000 |
---|---|---|
committer | Claudiu Olteanu <claudiu@140774ce-b5e7-0310-ab8b-a85725594a96> | 2013-09-18 12:53:30 +0000 |
commit | 2b74f7a94df138bd6f732334033574bdc1727250 (patch) | |
tree | bd41e355d72445baee7df830665d38fe80c98987 | |
parent | da43a590f1c47e9907a5b44b978999e043fd74ee (diff) | |
download | gnunet-2b74f7a94df138bd6f732334033574bdc1727250.tar.gz gnunet-2b74f7a94df138bd6f732334033574bdc1727250.zip |
Adding W32 functionality
-rw-r--r-- | configure.ac | 4 | ||||
-rw-r--r-- | src/transport/Makefile.am | 19 | ||||
-rw-r--r-- | src/transport/gnunet-helper-transport-bluetooth.c | 2546 | ||||
-rw-r--r-- | src/transport/plugin_transport_wlan.h | 7 |
4 files changed, 1649 insertions, 927 deletions
diff --git a/configure.ac b/configure.ac index 8646b095c..40dd0bbf5 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -368,6 +368,10 @@ then | |||
368 | else | 368 | else |
369 | AM_CONDITIONAL(HAVE_LIBBLUETOOTH, false) | 369 | AM_CONDITIONAL(HAVE_LIBBLUETOOTH, false) |
370 | fi | 370 | fi |
371 | if test "$build_target" = "mingw" | ||
372 | then | ||
373 | bluetooth=1 | ||
374 | fi | ||
371 | 375 | ||
372 | 376 | ||
373 | # libcurl | 377 | # libcurl |
diff --git a/src/transport/Makefile.am b/src/transport/Makefile.am index 9bc58bf04..9e257f068 100644 --- a/src/transport/Makefile.am +++ b/src/transport/Makefile.am | |||
@@ -68,7 +68,17 @@ endif | |||
68 | 68 | ||
69 | if LINUX | 69 | if LINUX |
70 | if HAVE_LIBBLUETOOTH | 70 | if HAVE_LIBBLUETOOTH |
71 | AM_LDFLAGS = -lbluetooth | 71 | BT_BIN = gnunet-helper-transport-bluetooth |
72 | BT_PLUGIN_LA = libgnunet_plugin_transport_bluetooth.la | ||
73 | BT_PLUGIN_TEST = test_plugin_bluetooth | ||
74 | BT_API_TEST = test_transport_api_bluetooth | ||
75 | BT_REL_TEST = test_transport_api_reliability_bluetooth | ||
76 | BT_UREL_TEST = test_transport_api_unreliability_bluetooth | ||
77 | BT_QUOTA_TEST = test_quota_compliance_bluetooth \ | ||
78 | test_quota_compliance_bluetooth_asymmetric | ||
79 | endif | ||
80 | else | ||
81 | if MINGW | ||
72 | BT_BIN = gnunet-helper-transport-bluetooth | 82 | BT_BIN = gnunet-helper-transport-bluetooth |
73 | BT_PLUGIN_LA = libgnunet_plugin_transport_bluetooth.la | 83 | BT_PLUGIN_LA = libgnunet_plugin_transport_bluetooth.la |
74 | BT_PLUGIN_TEST = test_plugin_bluetooth | 84 | BT_PLUGIN_TEST = test_plugin_bluetooth |
@@ -175,6 +185,13 @@ gnunet_transport_wlan_receiver_LDADD = \ | |||
175 | 185 | ||
176 | gnunet_helper_transport_bluetooth_SOURCES = \ | 186 | gnunet_helper_transport_bluetooth_SOURCES = \ |
177 | gnunet-helper-transport-bluetooth.c | 187 | gnunet-helper-transport-bluetooth.c |
188 | if MINGW | ||
189 | gnunet_helper_transport_bluetooth_LDADD = \ | ||
190 | $(top_builddir)/src/util/libgnunetutil.la | ||
191 | gnunet_helper_transport_bluetooth_LDFLAGS = -lws2_32 | ||
192 | else | ||
193 | gnunet_helper_transport_bluetooth_LDFLAGS = -lbluetooth | ||
194 | endif | ||
178 | 195 | ||
179 | gnunet_transport_SOURCES = \ | 196 | gnunet_transport_SOURCES = \ |
180 | gnunet-transport.c | 197 | gnunet-transport.c |
diff --git a/src/transport/gnunet-helper-transport-bluetooth.c b/src/transport/gnunet-helper-transport-bluetooth.c index 3eb17de42..f171e64fa 100644 --- a/src/transport/gnunet-helper-transport-bluetooth.c +++ b/src/transport/gnunet-helper-transport-bluetooth.c | |||
@@ -20,26 +20,35 @@ | |||
20 | Boston, MA 02111-1307, USA. | 20 | Boston, MA 02111-1307, USA. |
21 | */ | 21 | */ |
22 | #include "gnunet_config.h" | 22 | #include "gnunet_config.h" |
23 | #include "plugin_transport_wlan.h" | ||
24 | #include "gnunet_protocols.h" | ||
25 | |||
26 | #ifdef MINGW | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include <bthdef.h> | ||
30 | #include <ws2bth.h> | ||
31 | #include "gnunet_protocols.h" | ||
32 | #else | ||
33 | #include <bluetooth/bluetooth.h> | ||
34 | #include <bluetooth/hci.h> | ||
35 | #include <bluetooth/hci_lib.h> | ||
36 | #include <bluetooth/rfcomm.h> | ||
37 | #include <bluetooth/sdp.h> | ||
38 | #include <bluetooth/sdp_lib.h> | ||
39 | #include <errno.h> | ||
40 | #include <linux/if.h> | ||
41 | #include <stdio.h> | ||
42 | #include <stdlib.h> | ||
43 | #include <sys/ioctl.h> | ||
44 | #include <sys/param.h> | ||
45 | #include <sys/socket.h> | ||
46 | #include <sys/stat.h> | ||
47 | #include <sys/types.h> | ||
48 | #include <unistd.h> | ||
49 | #endif | ||
23 | 50 | ||
24 | #include <bluetooth/bluetooth.h> | ||
25 | #include <bluetooth/hci.h> | ||
26 | #include <bluetooth/hci_lib.h> | ||
27 | #include <bluetooth/rfcomm.h> | ||
28 | #include <bluetooth/sdp.h> | ||
29 | #include <bluetooth/sdp_lib.h> | ||
30 | #include <errno.h> | ||
31 | #include <linux/if.h> | ||
32 | #include <stdio.h> | ||
33 | #include <stdlib.h> | ||
34 | #include <sys/ioctl.h> | ||
35 | #include <sys/param.h> | ||
36 | #include <sys/socket.h> | ||
37 | #include <sys/stat.h> | ||
38 | #include <sys/types.h> | ||
39 | #include <unistd.h> | ||
40 | 51 | ||
41 | #include "gnunet_protocols.h" | ||
42 | #include "plugin_transport_wlan.h" | ||
43 | 52 | ||
44 | /** | 53 | /** |
45 | * Maximum number of ports assignable for RFCOMMM protocol. | 54 | * Maximum number of ports assignable for RFCOMMM protocol. |
@@ -58,22 +67,55 @@ | |||
58 | */ | 67 | */ |
59 | #define MAX_LOOPS 5 | 68 | #define MAX_LOOPS 5 |
60 | 69 | ||
70 | #ifdef MINGW | ||
71 | /* Maximum size of the interface's name */ | ||
72 | #define IFNAMSIZ 16 | ||
73 | |||
74 | /** | ||
75 | * A copy of the MAC Address. | ||
76 | */ | ||
77 | struct GNUNET_TRANSPORT_WLAN_MacAddress_Copy | ||
78 | { | ||
79 | UINT8 mac[MAC_ADDR_SIZE]; | ||
80 | }; | ||
81 | |||
82 | // {31191E56-FA7E-4517-870E-71B86BBCC52F} | ||
83 | #define GNUNET_BLUETOOTH_SDP_UUID \ | ||
84 | { \ | ||
85 | 0x31, 0x19, 0x1E, 0x56, \ | ||
86 | 0xFA, 0x7E, \ | ||
87 | 0x45, 0x17, \ | ||
88 | 0x87, 0x0E, \ | ||
89 | 0x71, 0xB8, 0x6B, 0xBC, 0xC5, 0x2F \ | ||
90 | } | ||
91 | #endif | ||
92 | |||
61 | /** | 93 | /** |
62 | * struct for storing the information of the hardware. There is only | 94 | * struct for storing the information of the hardware. There is only |
63 | * one of these. | 95 | * one of these. |
64 | */ | 96 | */ |
65 | struct HardwareInfos | 97 | struct HardwareInfos |
66 | { | 98 | { |
67 | |||
68 | /** | 99 | /** |
69 | * file descriptor for the rfcomm socket | 100 | * Name of the interface, not necessarily 0-terminated (!). |
70 | */ | 101 | */ |
71 | int fd_rfcomm; | 102 | char iface[IFNAMSIZ]; |
72 | 103 | ||
104 | #ifdef MINGW | ||
73 | /** | 105 | /** |
74 | * Name of the interface, not necessarily 0-terminated (!). | 106 | * socket handle |
107 | */ | ||
108 | struct GNUNET_NETWORK_Handle *handle; | ||
109 | |||
110 | /** | ||
111 | * MAC address of our own bluetooth interface. | ||
75 | */ | 112 | */ |
76 | char iface[IFNAMSIZ]; | 113 | struct GNUNET_TRANSPORT_WLAN_MacAddress_Copy pl_mac; |
114 | #else | ||
115 | /** | ||
116 | * file descriptor for the rfcomm socket | ||
117 | */ | ||
118 | int fd_rfcomm; | ||
77 | 119 | ||
78 | /** | 120 | /** |
79 | * MAC address of our own bluetooth interface. | 121 | * MAC address of our own bluetooth interface. |
@@ -84,6 +126,7 @@ struct HardwareInfos | |||
84 | * SDP session | 126 | * SDP session |
85 | */ | 127 | */ |
86 | sdp_session_t *session ; | 128 | sdp_session_t *session ; |
129 | #endif | ||
87 | }; | 130 | }; |
88 | 131 | ||
89 | /** | 132 | /** |
@@ -110,29 +153,42 @@ struct SendBuffer | |||
110 | char buf[MAXLINE * 2]; | 153 | char buf[MAXLINE * 2]; |
111 | }; | 154 | }; |
112 | 155 | ||
113 | /** | 156 | #ifdef LINUX |
114 | * Devices buffer used to keep a list with all the discoverable devices in | 157 | /** |
115 | * order to send them HELLO messages one by one when it receive a broadcast message. | 158 | * Devices buffer used to keep a list with all the discoverable devices in |
116 | */ | 159 | * order to send them HELLO messages one by one when it receive a broadcast message. |
117 | struct BroadcastMessages | 160 | */ |
118 | { | 161 | struct BroadcastMessages |
119 | /* List with the discoverable devices' addresses */ | 162 | { |
120 | bdaddr_t devices[MAX_PORTS]; //FIXME I should use a linked list but 30 isn't such a big number | 163 | /* List with the discoverable devices' addresses */ |
121 | 164 | bdaddr_t devices[MAX_PORTS]; | |
122 | /* List with the open sockets */ | ||
123 | int fds[MAX_PORTS]; | ||
124 | 165 | ||
166 | /* List with the open sockets */ | ||
167 | int fds[MAX_PORTS]; | ||
125 | 168 | ||
126 | /* The number of the devices */ | ||
127 | int size; | ||
128 | |||
129 | /* The current position */ | ||
130 | int pos; | ||
131 | 169 | ||
132 | /* The device id */ | 170 | /* The number of the devices */ |
133 | int dev_id; | 171 | int size; |
134 | }; | 172 | |
173 | /* The current position */ | ||
174 | int pos; | ||
175 | |||
176 | /* The device id */ | ||
177 | int dev_id; | ||
178 | }; | ||
135 | 179 | ||
180 | /** | ||
181 | * Address used to identify the broadcast messages. | ||
182 | */ | ||
183 | static struct GNUNET_TRANSPORT_WLAN_MacAddress broadcast_address = {{255, 255, 255, 255, 255, 255}}; | ||
184 | |||
185 | /** | ||
186 | * Buffer with the discoverable devices. | ||
187 | */ | ||
188 | static struct BroadcastMessages neighbours; | ||
189 | |||
190 | static int searching_devices_count = 0; | ||
191 | #endif | ||
136 | 192 | ||
137 | /** | 193 | /** |
138 | * Buffer for data read from stdin to be transmitted to the bluetooth device | 194 | * Buffer for data read from stdin to be transmitted to the bluetooth device |
@@ -144,20 +200,8 @@ static struct SendBuffer write_pout; | |||
144 | */ | 200 | */ |
145 | static struct SendBuffer write_std; | 201 | static struct SendBuffer write_std; |
146 | 202 | ||
147 | /** | ||
148 | * Address used to identify the broadcast messages. | ||
149 | */ | ||
150 | static struct GNUNET_TRANSPORT_WLAN_MacAddress broadcast_address = {{255, 255, 255, 255, 255, 255}}; | ||
151 | 203 | ||
152 | /** | 204 | /* ****** this are the same functions as the ones used in gnunet-helper-transport-wlan.c ****** */ |
153 | * Buffer with the discoverable devices. | ||
154 | */ | ||
155 | static struct BroadcastMessages neighbours; | ||
156 | |||
157 | static int searching_devices_count = 0; | ||
158 | |||
159 | /* *********** specialized version of server_mst.c begins here ********** */ | ||
160 | /* ****** this is the same version as the one used in gnunet-helper-transport-wlan.c ****** */ | ||
161 | 205 | ||
162 | /** | 206 | /** |
163 | * To what multiple do we align messages? 8 byte should suffice for everyone | 207 | * To what multiple do we align messages? 8 byte should suffice for everyone |
@@ -179,9 +223,9 @@ static int searching_devices_count = 0; | |||
179 | * @param message the actual message | 223 | * @param message the actual message |
180 | */ | 224 | */ |
181 | typedef void (*MessageTokenizerCallback) (void *cls, | 225 | typedef void (*MessageTokenizerCallback) (void *cls, |
182 | const struct | 226 | const struct |
183 | GNUNET_MessageHeader * | 227 | GNUNET_MessageHeader * |
184 | message); | 228 | message); |
185 | 229 | ||
186 | /** | 230 | /** |
187 | * Handle to a message stream tokenizer. | 231 | * Handle to a message stream tokenizer. |
@@ -231,7 +275,7 @@ struct MessageStreamTokenizer | |||
231 | */ | 275 | */ |
232 | static struct MessageStreamTokenizer * | 276 | static struct MessageStreamTokenizer * |
233 | mst_create (MessageTokenizerCallback cb, | 277 | mst_create (MessageTokenizerCallback cb, |
234 | void *cb_cls) | 278 | void *cb_cls) |
235 | { | 279 | { |
236 | struct MessageStreamTokenizer *ret; | 280 | struct MessageStreamTokenizer *ret; |
237 | 281 | ||
@@ -437,8 +481,6 @@ do_align: | |||
437 | return ret; | 481 | return ret; |
438 | } | 482 | } |
439 | 483 | ||
440 | |||
441 | |||
442 | /** | 484 | /** |
443 | * Destroys a tokenizer. | 485 | * Destroys a tokenizer. |
444 | * | 486 | * |
@@ -451,11 +493,6 @@ mst_destroy (struct MessageStreamTokenizer *mst) | |||
451 | free (mst); | 493 | free (mst); |
452 | } | 494 | } |
453 | 495 | ||
454 | /* ***************** end of server_mst.c clone ***************** **/ | ||
455 | |||
456 | |||
457 | /* ****** same crc version as the one used in gnunet-helper-transport-wlan.c ****** */ | ||
458 | |||
459 | /** | 496 | /** |
460 | * Calculate crc32, the start of the calculation | 497 | * Calculate crc32, the start of the calculation |
461 | * | 498 | * |
@@ -564,197 +601,407 @@ check_crc_buf_osdep (const unsigned char *buf, size_t len) | |||
564 | 601 | ||
565 | 602 | ||
566 | 603 | ||
567 | /* ************** end of crc version ***************** */ | 604 | /* ************** end of clone ***************** */ |
568 | |||
569 | 605 | ||
606 | #ifdef MINGW | ||
607 | /** | ||
608 | * Function used to get the code of last error and to print the type of error. | ||
609 | */ | ||
610 | static void | ||
611 | print_last_error() | ||
612 | { | ||
613 | LPVOID lpMsgBuf = NULL; | ||
614 | FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | ||
615 | NULL, GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, NULL); | ||
616 | fprintf (stderr, "%s\n", (char *)lpMsgBuf) ; | ||
617 | //FIXME : it crashes | ||
618 | // if (lpMsgBuf) | ||
619 | // free (lpMsgBuf); | ||
620 | } | ||
570 | 621 | ||
622 | /** | ||
623 | * Function used to initialize the Windows Sockets | ||
624 | */ | ||
625 | static void | ||
626 | initialize_windows_sockets() | ||
627 | { | ||
628 | WSADATA wsaData ; | ||
629 | WORD wVersionRequested = MAKEWORD (2, 0); | ||
630 | if (WSAStartup (wVersionRequested, &wsaData) != NO_ERROR) | ||
631 | { | ||
632 | fprintf (stderr , "Error initializing window sockets!\n"); | ||
633 | print_last_error(); | ||
634 | ExitProcess (2) ; | ||
635 | } | ||
636 | } | ||
571 | 637 | ||
572 | /** | 638 | /** |
573 | * Function for assigning a port number | 639 | * Function used to convert the GUID. |
574 | * | 640 | * @param bytes the GUID represented as a char array |
575 | * @param socket the socket used to bind | 641 | * @param uuid pointer to the GUID |
576 | * @param addr pointer to the rfcomm address | 642 | */ |
577 | * @return 0 on success | 643 | static void |
578 | */ | 644 | convert_guid(char *bytes, GUID * uuid) |
579 | static int | ||
580 | bind_socket (int socket, struct sockaddr_rc *addr) | ||
581 | { | ||
582 | int port, status; | ||
583 | |||
584 | /* Bind every possible port (from 0 to 30) and stop when binding doesn't fail */ | ||
585 | //FIXME : it should start from port 1, but on my computer it doesn't work :) | ||
586 | for (port = 3; port <= 30; port++) | ||
587 | { | 645 | { |
588 | addr->rc_channel = port; | 646 | int i; |
589 | status = bind (socket, (struct sockaddr *) addr, sizeof (struct sockaddr_rc)); | 647 | uuid->Data1 = ((bytes[0] << 24) & 0xff000000) | ((bytes[1] << 16) & 0x00ff0000) | ((bytes[2] << 8) & 0x0000ff00) | (bytes[3] & 0x000000ff); |
590 | if (status == 0) | 648 | uuid->Data2 = ((bytes[4] << 8) & 0xff00) | (bytes[5] & 0x00ff); |
591 | return 0; | 649 | uuid->Data3 = ((bytes[6] << 8) & 0xff00) | (bytes[7] & 0x00ff); |
650 | |||
651 | for (i = 0; i < 8; i++) | ||
652 | { | ||
653 | uuid->Data4[i] = bytes[i + 8]; | ||
654 | } | ||
592 | } | 655 | } |
593 | 656 | #endif | |
594 | return -1; | ||
595 | } | ||
596 | 657 | ||
658 | #ifdef LINUX | ||
659 | /** | ||
660 | * Function for assigning a port number | ||
661 | * | ||
662 | * @param socket the socket used to bind | ||
663 | * @param addr pointer to the rfcomm address | ||
664 | * @return 0 on success | ||
665 | */ | ||
666 | static int | ||
667 | bind_socket (int socket, struct sockaddr_rc *addr) | ||
668 | { | ||
669 | int port, status; | ||
670 | |||
671 | /* Bind every possible port (from 0 to 30) and stop when binding doesn't fail */ | ||
672 | //FIXME : it should start from port 1, but on my computer it doesn't work :) | ||
673 | for (port = 3; port <= 30; port++) | ||
674 | { | ||
675 | addr->rc_channel = port; | ||
676 | status = bind (socket, (struct sockaddr *) addr, sizeof (struct sockaddr_rc)); | ||
677 | if (status == 0) | ||
678 | return 0; | ||
679 | } | ||
680 | |||
681 | return -1; | ||
682 | } | ||
683 | #endif | ||
597 | 684 | ||
598 | /** | 685 | #ifdef MINGW |
599 | * Function used for creating the service record and registering it. | ||
600 | * | ||
601 | * @param dev pointer to the device struct | ||
602 | * @param rc_channel the rfcomm channel | ||
603 | * @return 0 on success | ||
604 | */ | ||
605 | static int | ||
606 | register_service (struct HardwareInfos *dev, int rc_channel) | ||
607 | { | ||
608 | /** | 686 | /** |
609 | * 1. initializations | 687 | * Function used for creating the service record and registering it. |
610 | * 2. set the service ID, class, profile information | 688 | * |
611 | * 3. make the service record publicly browsable | 689 | * @param dev pointer to the device struct |
612 | * 4. register the RFCOMM channel | 690 | * @return 0 on success |
613 | * 5. set the name, provider and description | ||
614 | * 6. register the service record to the local SDP server | ||
615 | * 7. cleanup | ||
616 | */ | 691 | */ |
617 | uint8_t svc_uuid_int[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 692 | static int |
618 | dev->pl_mac.mac[5], dev->pl_mac.mac[4], dev->pl_mac.mac[3], | 693 | register_service (struct HardwareInfos *dev) |
619 | dev->pl_mac.mac[2], dev->pl_mac.mac[1], dev->pl_mac.mac[0]}; | ||
620 | const char *service_dsc = "Bluetooth plugin services"; | ||
621 | const char *service_prov = "GNUnet provider"; | ||
622 | uuid_t root_uuid, rfcomm_uuid, svc_uuid; | ||
623 | sdp_list_t *root_list = 0, *rfcomm_list = 0, *proto_list = 0, | ||
624 | *access_proto_list = 0, *svc_list = 0; | ||
625 | sdp_record_t *record = 0; | ||
626 | sdp_data_t *channel = 0; | ||
627 | |||
628 | record = sdp_record_alloc(); | ||
629 | |||
630 | /* Set the general service ID */ | ||
631 | sdp_uuid128_create (&svc_uuid, &svc_uuid_int); | ||
632 | svc_list = sdp_list_append (0, &svc_uuid); | ||
633 | sdp_set_service_classes (record, svc_list); | ||
634 | sdp_set_service_id (record, svc_uuid); | ||
635 | |||
636 | /* Make the service record publicly browsable */ | ||
637 | sdp_uuid16_create (&root_uuid, PUBLIC_BROWSE_GROUP); | ||
638 | root_list = sdp_list_append (0, &root_uuid); | ||
639 | sdp_set_browse_groups (record, root_list); | ||
640 | |||
641 | /* Register the RFCOMM channel */ | ||
642 | sdp_uuid16_create (&rfcomm_uuid, RFCOMM_UUID); | ||
643 | channel = sdp_data_alloc (SDP_UINT8, &rc_channel); | ||
644 | rfcomm_list = sdp_list_append (0, &rfcomm_uuid); | ||
645 | sdp_list_append (rfcomm_list, channel); | ||
646 | proto_list = sdp_list_append (0, rfcomm_list); | ||
647 | |||
648 | /* Set protocol information */ | ||
649 | access_proto_list = sdp_list_append (0, proto_list); | ||
650 | sdp_set_access_protos (record, access_proto_list); | ||
651 | |||
652 | /* Set the name, provider, and description */ | ||
653 | sdp_set_info_attr (record, dev->iface, service_prov, service_dsc); | ||
654 | |||
655 | /* Connect to the local SDP server */ | ||
656 | dev->session = sdp_connect (BDADDR_ANY, BDADDR_LOCAL, SDP_RETRY_IF_BUSY); | ||
657 | |||
658 | if (!dev->session) | ||
659 | { | 694 | { |
660 | fprintf (stderr, "Failed to connect to the SDP server on interface `%.*s': %s\n", | 695 | // advertise the service |
661 | IFNAMSIZ, dev->iface, strerror (errno)); | 696 | CSADDR_INFO addr_info; |
662 | //FIXME exit? | 697 | WSAQUERYSET wqs; |
663 | return 1; | 698 | GUID guid; |
699 | unsigned char uuid[] = GNUNET_BLUETOOTH_SDP_UUID; | ||
700 | SOCKADDR_BTH addr; | ||
701 | int addr_len = sizeof (SOCKADDR_BTH); | ||
702 | int fd; | ||
703 | /* get the port on which we are listening on */ | ||
704 | memset (& addr, 0, sizeof (SOCKADDR_BTH)); | ||
705 | fd = GNUNET_NETWORK_get_fd (dev->handle); | ||
706 | if (fd <= 0) | ||
707 | { | ||
708 | fprintf (stderr, "Failed to get the file descriptor\n"); | ||
709 | return -1; | ||
710 | } | ||
711 | if (SOCKET_ERROR == getsockname (fd, (SOCKADDR*)&addr, &addr_len)) | ||
712 | { | ||
713 | fprintf (stderr, "Failed to get the port on which we are listening on: \n"); | ||
714 | print_last_error(); | ||
715 | return -1; | ||
716 | } | ||
717 | |||
718 | /* save the device address */ | ||
719 | memcpy (&dev->pl_mac, &addr.btAddr, sizeof (BTH_ADDR)); | ||
720 | |||
721 | /* set the address information */ | ||
722 | memset (&addr_info, 0, sizeof (CSADDR_INFO)); | ||
723 | addr_info.iProtocol = BTHPROTO_RFCOMM; | ||
724 | addr_info.iSocketType = SOCK_STREAM; | ||
725 | addr_info.LocalAddr.lpSockaddr = (LPSOCKADDR)&addr; | ||
726 | addr_info.LocalAddr.iSockaddrLength = sizeof (addr); | ||
727 | addr_info.RemoteAddr.lpSockaddr = (LPSOCKADDR)&addr; | ||
728 | addr_info.RemoteAddr.iSockaddrLength = sizeof (addr); | ||
729 | |||
730 | convert_guid((char *) uuid, &guid); | ||
731 | |||
732 | /* register the service */ | ||
733 | memset (&wqs, 0, sizeof (WSAQUERYSET)); | ||
734 | wqs.dwSize = sizeof (WSAQUERYSET); | ||
735 | wqs.dwNameSpace = NS_BTH; | ||
736 | wqs.lpszServiceInstanceName = "GNUnet Bluetooth Service"; | ||
737 | wqs.lpszComment = "This is the service used by the GNUnnet plugin transport"; | ||
738 | wqs.lpServiceClassId = &guid; | ||
739 | wqs.dwNumberOfCsAddrs = 1; | ||
740 | wqs.lpcsaBuffer = &addr_info ; | ||
741 | wqs.lpBlob = 0; //FIXME it should be 0 for a new entry | ||
742 | |||
743 | if (SOCKET_ERROR == WSASetService (&wqs , RNRSERVICE_REGISTER, 0)) | ||
744 | { | ||
745 | fprintf (stderr, "Failed to register the SDP service: "); | ||
746 | print_last_error(); | ||
747 | return -1; | ||
748 | } | ||
749 | else | ||
750 | { | ||
751 | fprintf (stderr, "The SDP service was registered\n"); | ||
752 | } | ||
753 | |||
754 | return 0; | ||
664 | } | 755 | } |
665 | 756 | #else | |
666 | /* Register the service record */ | 757 | /** |
667 | if (sdp_record_register (dev->session, record, 0) < 0) | 758 | * Function used for creating the service record and registering it. |
759 | * | ||
760 | * @param dev pointer to the device struct | ||
761 | * @param rc_channel the rfcomm channel | ||
762 | * @return 0 on success | ||
763 | */ | ||
764 | static int | ||
765 | register_service (struct HardwareInfos *dev, int rc_channel) | ||
668 | { | 766 | { |
669 | fprintf (stderr, "Failed to register a service record on interface `%.*s': %s\n", | 767 | /** |
670 | IFNAMSIZ, dev->iface, strerror (errno)); | 768 | * 1. initializations |
671 | //FIXME exit? | 769 | * 2. set the service ID, class, profile information |
672 | return 1; | 770 | * 3. make the service record publicly browsable |
771 | * 4. register the RFCOMM channel | ||
772 | * 5. set the name, provider and description | ||
773 | * 6. register the service record to the local SDP server | ||
774 | * 7. cleanup | ||
775 | */ | ||
776 | uint8_t svc_uuid_int[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
777 | dev->pl_mac.mac[5], dev->pl_mac.mac[4], dev->pl_mac.mac[3], | ||
778 | dev->pl_mac.mac[2], dev->pl_mac.mac[1], dev->pl_mac.mac[0]}; | ||
779 | const char *service_dsc = "Bluetooth plugin services"; | ||
780 | const char *service_prov = "GNUnet provider"; | ||
781 | uuid_t root_uuid, rfcomm_uuid, svc_uuid; | ||
782 | sdp_list_t *root_list = 0, *rfcomm_list = 0, *proto_list = 0, | ||
783 | *access_proto_list = 0, *svc_list = 0; | ||
784 | sdp_record_t *record = 0; | ||
785 | sdp_data_t *channel = 0; | ||
786 | |||
787 | record = sdp_record_alloc(); | ||
788 | |||
789 | /* Set the general service ID */ | ||
790 | sdp_uuid128_create (&svc_uuid, &svc_uuid_int); | ||
791 | svc_list = sdp_list_append (0, &svc_uuid); | ||
792 | sdp_set_service_classes (record, svc_list); | ||
793 | sdp_set_service_id (record, svc_uuid); | ||
794 | |||
795 | /* Make the service record publicly browsable */ | ||
796 | sdp_uuid16_create (&root_uuid, PUBLIC_BROWSE_GROUP); | ||
797 | root_list = sdp_list_append (0, &root_uuid); | ||
798 | sdp_set_browse_groups (record, root_list); | ||
799 | |||
800 | /* Register the RFCOMM channel */ | ||
801 | sdp_uuid16_create (&rfcomm_uuid, RFCOMM_UUID); | ||
802 | channel = sdp_data_alloc (SDP_UINT8, &rc_channel); | ||
803 | rfcomm_list = sdp_list_append (0, &rfcomm_uuid); | ||
804 | sdp_list_append (rfcomm_list, channel); | ||
805 | proto_list = sdp_list_append (0, rfcomm_list); | ||
806 | |||
807 | /* Set protocol information */ | ||
808 | access_proto_list = sdp_list_append (0, proto_list); | ||
809 | sdp_set_access_protos (record, access_proto_list); | ||
810 | |||
811 | /* Set the name, provider, and description */ | ||
812 | sdp_set_info_attr (record, dev->iface, service_prov, service_dsc); | ||
813 | |||
814 | /* Connect to the local SDP server */ | ||
815 | dev->session = sdp_connect (BDADDR_ANY, BDADDR_LOCAL, SDP_RETRY_IF_BUSY); | ||
816 | |||
817 | if (!dev->session) | ||
818 | { | ||
819 | fprintf (stderr, "Failed to connect to the SDP server on interface `%.*s': %s\n", | ||
820 | IFNAMSIZ, dev->iface, strerror (errno)); | ||
821 | //FIXME exit? | ||
822 | return 1; | ||
823 | } | ||
824 | |||
825 | /* Register the service record */ | ||
826 | if (sdp_record_register (dev->session, record, 0) < 0) | ||
827 | { | ||
828 | fprintf (stderr, "Failed to register a service record on interface `%.*s': %s\n", | ||
829 | IFNAMSIZ, dev->iface, strerror (errno)); | ||
830 | //FIXME exit? | ||
831 | return 1; | ||
832 | } | ||
833 | |||
834 | /* Cleanup */ | ||
835 | sdp_data_free (channel); | ||
836 | sdp_list_free (root_list, 0); | ||
837 | sdp_list_free (rfcomm_list, 0); | ||
838 | sdp_list_free (proto_list, 0); | ||
839 | sdp_list_free (access_proto_list, 0); | ||
840 | sdp_list_free (svc_list, 0); | ||
841 | sdp_record_free (record); | ||
842 | |||
843 | return 0; | ||
673 | } | 844 | } |
674 | 845 | #endif | |
675 | /* Cleanup */ | ||
676 | sdp_data_free (channel); | ||
677 | sdp_list_free (root_list, 0); | ||
678 | sdp_list_free (rfcomm_list, 0); | ||
679 | sdp_list_free (proto_list, 0); | ||
680 | sdp_list_free (access_proto_list, 0); | ||
681 | sdp_list_free (svc_list, 0); | ||
682 | sdp_record_free (record); | ||
683 | |||
684 | return 0; | ||
685 | } | ||
686 | 846 | ||
687 | /** | 847 | #ifdef MINGW |
688 | * Function for searching and browsing for a service. This will return the | ||
689 | * port number on which the service is running. | ||
690 | * | ||
691 | * @param dev pointer to the device struct | ||
692 | * @param dest target address | ||
693 | * @return channel | ||
694 | */ | ||
695 | static int | ||
696 | get_channel(struct HardwareInfos *dev, bdaddr_t dest) | ||
697 | { | ||
698 | /** | 848 | /** |
699 | * 1. detect all nearby devices | 849 | * Function for searching and browsing for a service. This will return the |
700 | * 2. for each device: | 850 | * port number on which the service is running. |
701 | * 2.1. connect to the SDP server running | 851 | * |
702 | * 2.2. get a list of service records with the specific UUID | 852 | * @param dest target address |
703 | * 2.3. for each service record get a list of the protocol sequences and get | 853 | * @return channel |
704 | * the port number | ||
705 | */ | 854 | */ |
706 | uint8_t svc_uuid_int[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 855 | static int |
707 | dest.b[5], dest.b[4], dest.b[3], | 856 | get_channel(const char *dest) |
708 | dest.b[2], dest.b[1], dest.b[0]}; | ||
709 | sdp_session_t *session = 0; | ||
710 | sdp_list_t *search_list = 0, *attrid_list = 0, *response_list = 0, *it = 0; | ||
711 | uuid_t svc_uuid; | ||
712 | uint32_t range = 0x0000ffff; | ||
713 | uint8_t channel = -1; | ||
714 | |||
715 | /* Connect to the local SDP server */ | ||
716 | session = sdp_connect (BDADDR_ANY, &dest, 0); | ||
717 | if (!session) | ||
718 | { | 857 | { |
719 | fprintf (stderr, "Failed to connect to the SDP server on interface `%.*s': %s\n", | 858 | HANDLE h; |
720 | IFNAMSIZ, dev->iface, strerror (errno)); | 859 | WSAQUERYSET *wqs; |
721 | //FIXME exit? | 860 | DWORD wqs_len = sizeof (WSAQUERYSET); |
722 | return -1; | 861 | int done = 0; |
862 | int channel = -1; | ||
863 | GUID guid; | ||
864 | unsigned char uuid[] = GNUNET_BLUETOOTH_SDP_UUID; | ||
865 | convert_guid ((char *) uuid, &guid); | ||
866 | |||
867 | wqs = (WSAQUERYSET*)malloc (wqs_len); | ||
868 | ZeroMemory (wqs, wqs_len); | ||
869 | |||
870 | wqs->dwSize = sizeof (WSAQUERYSET) ; | ||
871 | wqs->lpServiceClassId = &guid; | ||
872 | wqs->dwNameSpace = NS_BTH; | ||
873 | wqs->dwNumberOfCsAddrs = 0; | ||
874 | wqs->lpszContext = (LPSTR)dest; | ||
875 | |||
876 | if (SOCKET_ERROR == WSALookupServiceBegin (wqs, LUP_FLUSHCACHE | LUP_RETURN_ALL, &h)) | ||
877 | { | ||
878 | if (GetLastError() == WSASERVICE_NOT_FOUND) | ||
879 | { | ||
880 | fprintf (stderr, "WARNING! The device with address %s wasn't found. Skipping the message!", dest); | ||
881 | return -1; | ||
882 | } | ||
883 | else | ||
884 | { | ||
885 | fprintf (stderr, "Failed to find the port number: "); | ||
886 | print_last_error(); | ||
887 | ExitProcess (2); | ||
888 | return -1; | ||
889 | } | ||
890 | } | ||
891 | |||
892 | /* search the sdp service */ | ||
893 | while (!done) | ||
894 | { | ||
895 | if (SOCKET_ERROR == WSALookupServiceNext (h, LUP_FLUSHCACHE | LUP_RETURN_ALL, &wqs_len, wqs)) | ||
896 | { | ||
897 | int error = WSAGetLastError(); | ||
898 | |||
899 | switch (error) | ||
900 | { | ||
901 | case WSAEFAULT: | ||
902 | free (wqs); | ||
903 | //fprintf (stderr, "WSAFAULT error! (the memory allocated wasn't enought\n"); | ||
904 | wqs = (WSAQUERYSET*)malloc (wqs_len); | ||
905 | break; | ||
906 | case WSANO_DATA: | ||
907 | fprintf (stderr, "Failed! The address was valid but there was no data record of requested type\n"); | ||
908 | done = 1; | ||
909 | break; | ||
910 | case WSA_E_NO_MORE: | ||
911 | done = 1; | ||
912 | // fprintf (stderr, "No matching service found\n"); | ||
913 | break; | ||
914 | default: | ||
915 | fprintf (stderr, "Failed to look over the services: "); | ||
916 | print_last_error(); | ||
917 | WSALookupServiceEnd (h); | ||
918 | ExitProcess (2); | ||
919 | } | ||
920 | } | ||
921 | else | ||
922 | { | ||
923 | channel = ((SOCKADDR_BTH*)wqs->lpcsaBuffer->RemoteAddr.lpSockaddr)->port; | ||
924 | } | ||
925 | } | ||
926 | |||
927 | free (wqs) ; | ||
928 | WSALookupServiceEnd (h); | ||
929 | |||
930 | return channel; | ||
723 | } | 931 | } |
724 | 932 | #else | |
725 | sdp_uuid128_create (&svc_uuid, &svc_uuid_int); | 933 | /** |
726 | search_list = sdp_list_append (0, &svc_uuid); | 934 | * Function used for searching and browsing for a service. This will return the |
727 | attrid_list = sdp_list_append (0, &range); | 935 | * port number on which the service is running. |
728 | 936 | * | |
729 | if (sdp_service_search_attr_req (session, search_list, | 937 | * @param dev pointer to the device struct |
730 | SDP_ATTR_REQ_RANGE, attrid_list, &response_list) == 0) | 938 | * @param dest target address |
939 | * @return channel | ||
940 | */ | ||
941 | static int | ||
942 | get_channel(struct HardwareInfos *dev, bdaddr_t dest) | ||
731 | { | 943 | { |
732 | for (it = response_list; it; it = it->next) | 944 | /** |
945 | * 1. detect all nearby devices | ||
946 | * 2. for each device: | ||
947 | * 2.1. connect to the SDP server running | ||
948 | * 2.2. get a list of service records with the specific UUID | ||
949 | * 2.3. for each service record get a list of the protocol sequences and get | ||
950 | * the port number | ||
951 | */ | ||
952 | uint8_t svc_uuid_int[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
953 | dest.b[5], dest.b[4], dest.b[3], | ||
954 | dest.b[2], dest.b[1], dest.b[0]}; | ||
955 | sdp_session_t *session = 0; | ||
956 | sdp_list_t *search_list = 0, *attrid_list = 0, *response_list = 0, *it = 0; | ||
957 | uuid_t svc_uuid; | ||
958 | uint32_t range = 0x0000ffff; | ||
959 | uint8_t channel = -1; | ||
960 | |||
961 | /* Connect to the local SDP server */ | ||
962 | session = sdp_connect (BDADDR_ANY, &dest, 0); | ||
963 | if (!session) | ||
733 | { | 964 | { |
734 | sdp_record_t *record = (sdp_record_t*) it->data; | 965 | fprintf (stderr, "Failed to connect to the SDP server on interface `%.*s': %s\n", |
735 | //TODO print some record informations to be sure everything is good | 966 | IFNAMSIZ, dev->iface, strerror (errno)); |
736 | sdp_list_t *proto_list = 0; | 967 | //FIXME exit? |
737 | if (sdp_get_access_protos (record, &proto_list) == 0) | 968 | return -1; |
969 | } | ||
970 | |||
971 | sdp_uuid128_create (&svc_uuid, &svc_uuid_int); | ||
972 | search_list = sdp_list_append (0, &svc_uuid); | ||
973 | attrid_list = sdp_list_append (0, &range); | ||
974 | |||
975 | if (sdp_service_search_attr_req (session, search_list, | ||
976 | SDP_ATTR_REQ_RANGE, attrid_list, &response_list) == 0) | ||
977 | { | ||
978 | for (it = response_list; it; it = it->next) | ||
738 | { | 979 | { |
739 | channel = sdp_get_proto_port (proto_list, RFCOMM_UUID); | 980 | sdp_record_t *record = (sdp_record_t*) it->data; |
740 | sdp_list_free (proto_list, 0); | 981 | //TODO print some record informations to be sure everything is good |
982 | sdp_list_t *proto_list = 0; | ||
983 | if (sdp_get_access_protos (record, &proto_list) == 0) | ||
984 | { | ||
985 | channel = sdp_get_proto_port (proto_list, RFCOMM_UUID); | ||
986 | sdp_list_free (proto_list, 0); | ||
987 | } | ||
988 | sdp_record_free (record); | ||
741 | } | 989 | } |
742 | sdp_record_free (record); | ||
743 | } | 990 | } |
991 | |||
992 | sdp_list_free (search_list, 0); | ||
993 | sdp_list_free (attrid_list, 0); | ||
994 | sdp_list_free (response_list, 0); | ||
995 | |||
996 | sdp_close (session); | ||
997 | |||
998 | if (channel == -1) | ||
999 | fprintf (stderr, "Failed to find the listening channel for interface `%.*s': %s\n", | ||
1000 | IFNAMSIZ, dev->iface, strerror (errno)); | ||
1001 | |||
1002 | return channel; | ||
744 | } | 1003 | } |
745 | 1004 | #endif | |
746 | sdp_list_free (search_list, 0); | ||
747 | sdp_list_free (attrid_list, 0); | ||
748 | sdp_list_free (response_list, 0); | ||
749 | |||
750 | sdp_close (session); | ||
751 | |||
752 | if (channel == -1) | ||
753 | fprintf (stderr, "Failed to find the listening channel for interface `%.*s': %s\n", | ||
754 | IFNAMSIZ, dev->iface, strerror (errno)); | ||
755 | |||
756 | return channel; | ||
757 | } | ||
758 | 1005 | ||
759 | /** | 1006 | /** |
760 | * Read from the socket and put the result into the buffer for transmission to 'stdout'. | 1007 | * Read from the socket and put the result into the buffer for transmission to 'stdout'. |
@@ -767,38 +1014,49 @@ get_channel(struct HardwareInfos *dev, bdaddr_t dest) | |||
767 | * @return number of bytes written to 'buf' | 1014 | * @return number of bytes written to 'buf' |
768 | */ | 1015 | */ |
769 | static ssize_t | 1016 | static ssize_t |
770 | read_from_the_socket (int sock, | 1017 | read_from_the_socket (void *sock, |
771 | unsigned char *buf, size_t buf_size, | 1018 | unsigned char *buf, size_t buf_size, |
772 | struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *ri) | 1019 | struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *ri) |
773 | { | 1020 | { |
774 | unsigned char tmpbuf[buf_size]; | 1021 | unsigned char tmpbuf[buf_size]; |
775 | ssize_t count; | 1022 | ssize_t count; |
776 | int len; | ||
777 | struct sockaddr_rc rc_addr = { 0 }; | ||
778 | |||
779 | count = read (sock, tmpbuf, buf_size); | ||
780 | 1023 | ||
1024 | #ifdef MINGW | ||
1025 | count = GNUNET_NETWORK_socket_recv ((struct GNUNET_NETWORK_Handle *)sock, tmpbuf, buf_size); | ||
1026 | #else | ||
1027 | count = read (*((int *)sock), tmpbuf, buf_size); | ||
1028 | #endif | ||
1029 | |||
781 | if (0 > count) | 1030 | if (0 > count) |
782 | { | 1031 | { |
783 | if (EAGAIN == errno) | 1032 | if (EAGAIN == errno) |
784 | return 0; | 1033 | return 0; |
785 | 1034 | #if MINGW | |
786 | fprintf (stderr, "Failed to read from the HCI socket: %s\n", strerror (errno)); | 1035 | print_last_error(); |
787 | return -1; | 1036 | #else |
788 | } | 1037 | fprintf (stderr, "Failed to read from the HCI socket: %s\n", strerror (errno)); |
789 | 1038 | #endif | |
790 | /* Get the channel used */ //FIXME probably not needed anymore | 1039 | |
791 | memset (&rc_addr, 0, sizeof (rc_addr)); | ||
792 | len = sizeof (rc_addr); | ||
793 | if (0 > getsockname (sock, (struct sockaddr *) &rc_addr, (socklen_t *) &len)) | ||
794 | { | ||
795 | fprintf (stderr, "getsockname() call failed : %s\n", strerror (errno)); | ||
796 | return -1; | 1040 | return -1; |
797 | } | 1041 | } |
798 | 1042 | ||
799 | memset (ri, 0, sizeof (*ri)); | 1043 | #ifdef LINUX |
800 | ri->ri_channel = rc_addr.rc_channel; | 1044 | /* Get the channel used */ //FIXME probably not needed anymore |
801 | 1045 | int len; | |
1046 | struct sockaddr_rc rc_addr = { 0 }; | ||
1047 | |||
1048 | memset (&rc_addr, 0, sizeof (rc_addr)); | ||
1049 | len = sizeof (rc_addr); | ||
1050 | if (0 > getsockname (*((int *)sock), (struct sockaddr *) &rc_addr, (socklen_t *) &len)) | ||
1051 | { | ||
1052 | fprintf (stderr, "getsockname() call failed : %s\n", strerror (errno)); | ||
1053 | return -1; | ||
1054 | } | ||
1055 | |||
1056 | memset (ri, 0, sizeof (*ri)); | ||
1057 | ri->ri_channel = rc_addr.rc_channel; | ||
1058 | #endif | ||
1059 | |||
802 | /* Detect CRC32 at the end */ | 1060 | /* Detect CRC32 at the end */ |
803 | if (0 == check_crc_buf_osdep (tmpbuf, count - sizeof (uint32_t))) | 1061 | if (0 == check_crc_buf_osdep (tmpbuf, count - sizeof (uint32_t))) |
804 | { | 1062 | { |
@@ -818,141 +1076,174 @@ read_from_the_socket (int sock, | |||
818 | */ | 1076 | */ |
819 | static int | 1077 | static int |
820 | open_device (struct HardwareInfos *dev) | 1078 | open_device (struct HardwareInfos *dev) |
821 | { | 1079 | { |
822 | int i, dev_id = -1, fd_hci; | 1080 | #ifdef MINGW |
823 | struct | 1081 | SOCKADDR_BTH addr; |
824 | { | ||
825 | struct hci_dev_list_req list; | ||
826 | struct hci_dev_req dev[HCI_MAX_DEV]; | ||
827 | } request; //used for detecting the local devices | ||
828 | struct sockaddr_rc rc_addr = { 0 }; //used for binding | ||
829 | |||
830 | /* Initialize the neighbour structure */ | ||
831 | neighbours.dev_id = -1; | ||
832 | for (i = 0; i < MAX_PORTS; i++) | ||
833 | neighbours.fds[i] = -1; | ||
834 | |||
835 | /* Open a HCI socket */ | ||
836 | fd_hci = socket (AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); | ||
837 | 1082 | ||
838 | if (fd_hci < 0) | 1083 | /* bind the hci socket to the interface */ |
839 | { | 1084 | addr.addressFamily = AF_BTH; |
840 | fprintf (stderr, "Failed to create HCI socket: %s\n", strerror (errno)); | 1085 | addr.btAddr = 0; |
841 | return -1; | 1086 | addr.port = BT_PORT_ANY; |
842 | } | ||
843 | |||
844 | memset (&request, 0, sizeof(request)); | ||
845 | request.list.dev_num = HCI_MAX_DEV; | ||
846 | 1087 | ||
847 | if (ioctl (fd_hci, HCIGETDEVLIST, (void *) &request) < 0) | 1088 | //FIXME flags parameter |
848 | { | 1089 | if (GNUNET_NETWORK_socket_bind (dev->handle, (const SOCKADDR*)&addr, sizeof (SOCKADDR_BTH), 0) != GNUNET_OK) |
849 | fprintf (stderr, "ioctl(HCIGETDEVLIST) on interface `%.*s' failed: %s\n", | 1090 | { |
850 | IFNAMSIZ, dev->iface, strerror (errno)); | 1091 | fprintf (stderr, "Failed to bind the socket: "); |
851 | return 1; | 1092 | print_last_error(); |
852 | } | 1093 | return -1; |
853 | 1094 | } | |
854 | /* Search for a device with dev->iface name */ | ||
855 | for (i = 0; i < request.list.dev_num; i++) | ||
856 | { | ||
857 | struct hci_dev_info dev_info; | ||
858 | 1095 | ||
859 | memset (&dev_info, 0, sizeof(struct hci_dev_info)); | 1096 | /* start listening on the socket */ |
860 | dev_info.dev_id = request.dev[i].dev_id; | 1097 | if (GNUNET_NETWORK_socket_listen (dev->handle, 4) != GNUNET_OK) |
861 | strncpy (dev_info.name, dev->iface, IFNAMSIZ); | 1098 | { |
1099 | fprintf (stderr, "Failed to listen on the socket: "); | ||
1100 | print_last_error(); | ||
1101 | return -1; | ||
1102 | } | ||
1103 | |||
1104 | /* register the sdp service */ | ||
1105 | if (register_service(dev) != 0) | ||
1106 | { | ||
1107 | fprintf (stderr, "Failed to register a service: "); | ||
1108 | print_last_error(); | ||
1109 | return 1; | ||
1110 | } | ||
1111 | #else | ||
1112 | int i, dev_id = -1, fd_hci; | ||
1113 | struct | ||
1114 | { | ||
1115 | struct hci_dev_list_req list; | ||
1116 | struct hci_dev_req dev[HCI_MAX_DEV]; | ||
1117 | } request; //used for detecting the local devices | ||
1118 | struct sockaddr_rc rc_addr = { 0 }; //used for binding | ||
862 | 1119 | ||
863 | if (ioctl (fd_hci, HCIGETDEVINFO, (void *) &dev_info)) | 1120 | /* Initialize the neighbour structure */ |
1121 | neighbours.dev_id = -1; | ||
1122 | for (i = 0; i < MAX_PORTS; i++) | ||
1123 | neighbours.fds[i] = -1; | ||
1124 | |||
1125 | /* Open a HCI socket */ | ||
1126 | fd_hci = socket (AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); | ||
1127 | |||
1128 | if (fd_hci < 0) | ||
1129 | { | ||
1130 | fprintf (stderr, "Failed to create HCI socket: %s\n", strerror (errno)); | ||
1131 | return -1; | ||
1132 | } | ||
1133 | |||
1134 | memset (&request, 0, sizeof(request)); | ||
1135 | request.list.dev_num = HCI_MAX_DEV; | ||
1136 | |||
1137 | if (ioctl (fd_hci, HCIGETDEVLIST, (void *) &request) < 0) | ||
864 | { | 1138 | { |
865 | fprintf (stderr, "ioctl(HCIGETDEVINFO) on interface `%.*s' failed: %s\n", | 1139 | fprintf (stderr, "ioctl(HCIGETDEVLIST) on interface `%.*s' failed: %s\n", |
866 | IFNAMSIZ, dev->iface, strerror (errno)); | 1140 | IFNAMSIZ, dev->iface, strerror (errno)); |
867 | return 1; | 1141 | return 1; |
868 | } | 1142 | } |
869 | 1143 | ||
870 | if (strcmp (dev_info.name, dev->iface) == 0) | 1144 | /* Search for a device with dev->iface name */ |
1145 | for (i = 0; i < request.list.dev_num; i++) | ||
871 | { | 1146 | { |
1147 | struct hci_dev_info dev_info; | ||
1148 | |||
1149 | memset (&dev_info, 0, sizeof(struct hci_dev_info)); | ||
1150 | dev_info.dev_id = request.dev[i].dev_id; | ||
1151 | strncpy (dev_info.name, dev->iface, IFNAMSIZ); | ||
872 | 1152 | ||
873 | dev_id = dev_info.dev_id; //the device was found | 1153 | if (ioctl (fd_hci, HCIGETDEVINFO, (void *) &dev_info)) |
874 | /** | ||
875 | * Copy the MAC address to the device structure | ||
876 | */ | ||
877 | memcpy (&dev->pl_mac, &dev_info.bdaddr, sizeof (bdaddr_t)); | ||
878 | |||
879 | /* Check if the interface is up */ | ||
880 | if (hci_test_bit (HCI_UP, (void *) &dev_info.flags) == 0) | ||
881 | { | 1154 | { |
882 | /* Bring the interface up */ | 1155 | fprintf (stderr, "ioctl(HCIGETDEVINFO) on interface `%.*s' failed: %s\n", |
883 | if (ioctl (fd_hci, HCIDEVUP, dev_info.dev_id)) | 1156 | IFNAMSIZ, dev->iface, strerror (errno)); |
884 | { | 1157 | return 1; |
885 | fprintf (stderr, "ioctl(HCIDEVUP) on interface `%.*s' failed: %s\n", | ||
886 | IFNAMSIZ, dev->iface, strerror (errno)); | ||
887 | return 1; | ||
888 | } | ||
889 | } | 1158 | } |
890 | 1159 | ||
891 | /* Check if the device is discoverable */ | 1160 | if (strcmp (dev_info.name, dev->iface) == 0) |
892 | if (hci_test_bit (HCI_PSCAN, (void *) &dev_info.flags) == 0 || | ||
893 | hci_test_bit (HCI_ISCAN, (void *) &dev_info.flags) == 0) | ||
894 | { | 1161 | { |
895 | /* Set interface Page Scan and Inqury Scan ON */ | ||
896 | struct hci_dev_req dev_req; | ||
897 | |||
898 | memset (&dev_req, 0, sizeof (dev_req)); | ||
899 | dev_req.dev_id = dev_info.dev_id; | ||
900 | dev_req.dev_opt = SCAN_PAGE | SCAN_INQUIRY; | ||
901 | 1162 | ||
902 | if (ioctl (fd_hci, HCISETSCAN, (unsigned long) &dev_req)) | 1163 | dev_id = dev_info.dev_id; //the device was found |
903 | { | 1164 | /** |
904 | fprintf (stderr, "ioctl(HCISETSCAN) on interface `%.*s' failed: %s\n", | 1165 | * Copy the MAC address to the device structure |
905 | IFNAMSIZ, dev->iface, strerror (errno)); | 1166 | */ |
906 | return 1; | 1167 | memcpy (&dev->pl_mac, &dev_info.bdaddr, sizeof (bdaddr_t)); |
1168 | |||
1169 | /* Check if the interface is up */ | ||
1170 | if (hci_test_bit (HCI_UP, (void *) &dev_info.flags) == 0) | ||
1171 | { | ||
1172 | /* Bring the interface up */ | ||
1173 | if (ioctl (fd_hci, HCIDEVUP, dev_info.dev_id)) | ||
1174 | { | ||
1175 | fprintf (stderr, "ioctl(HCIDEVUP) on interface `%.*s' failed: %s\n", | ||
1176 | IFNAMSIZ, dev->iface, strerror (errno)); | ||
1177 | return 1; | ||
1178 | } | ||
907 | } | 1179 | } |
908 | 1180 | ||
1181 | /* Check if the device is discoverable */ | ||
1182 | if (hci_test_bit (HCI_PSCAN, (void *) &dev_info.flags) == 0 || | ||
1183 | hci_test_bit (HCI_ISCAN, (void *) &dev_info.flags) == 0) | ||
1184 | { | ||
1185 | /* Set interface Page Scan and Inqury Scan ON */ | ||
1186 | struct hci_dev_req dev_req; | ||
1187 | |||
1188 | memset (&dev_req, 0, sizeof (dev_req)); | ||
1189 | dev_req.dev_id = dev_info.dev_id; | ||
1190 | dev_req.dev_opt = SCAN_PAGE | SCAN_INQUIRY; | ||
1191 | |||
1192 | if (ioctl (fd_hci, HCISETSCAN, (unsigned long) &dev_req)) | ||
1193 | { | ||
1194 | fprintf (stderr, "ioctl(HCISETSCAN) on interface `%.*s' failed: %s\n", | ||
1195 | IFNAMSIZ, dev->iface, strerror (errno)); | ||
1196 | return 1; | ||
1197 | } | ||
1198 | |||
1199 | } | ||
1200 | break; | ||
909 | } | 1201 | } |
910 | break; | 1202 | |
911 | } | 1203 | } |
912 | 1204 | ||
913 | } | 1205 | /* Check if the interface was not found */ |
914 | 1206 | if (dev_id == -1) | |
915 | /* Check if the interface was not found */ | 1207 | { |
916 | if (dev_id == -1) | 1208 | fprintf (stderr, "The interface %s was not found\n", dev->iface); |
917 | { | 1209 | return 1; |
918 | fprintf (stderr, "The interface %s was not found\n", dev->iface); | 1210 | } |
919 | return 1; | 1211 | |
920 | } | 1212 | /* Close the hci socket */ |
921 | 1213 | (void) close(fd_hci); | |
922 | /* Close the hci socket */ | 1214 | |
923 | (void) close(fd_hci); | 1215 | |
924 | 1216 | ||
925 | 1217 | /* Bind the rfcomm socket to the interface */ | |
926 | 1218 | memset (&rc_addr, 0, sizeof (rc_addr)); | |
927 | /* Bind the rfcomm socket to the interface */ | 1219 | rc_addr.rc_family = AF_BLUETOOTH; |
928 | memset (&rc_addr, 0, sizeof (rc_addr)); | 1220 | rc_addr.rc_bdaddr = *BDADDR_ANY; |
929 | rc_addr.rc_family = AF_BLUETOOTH; | 1221 | |
930 | rc_addr.rc_bdaddr = *BDADDR_ANY; | 1222 | if (bind_socket (dev->fd_rfcomm, &rc_addr) != 0) |
931 | 1223 | { | |
932 | if (bind_socket (dev->fd_rfcomm, &rc_addr) != 0) | 1224 | fprintf (stderr, "Failed to bind interface `%.*s': %s\n", IFNAMSIZ, |
933 | { | 1225 | dev->iface, strerror (errno)); |
934 | fprintf (stderr, "Failed to bind interface `%.*s': %s\n", IFNAMSIZ, | 1226 | return 1; |
935 | dev->iface, strerror (errno)); | 1227 | } |
936 | return 1; | 1228 | |
937 | } | 1229 | /* Register a SDP service */ |
938 | 1230 | if (register_service (dev, rc_addr.rc_channel) != 0) | |
939 | /* Register a SDP service */ | 1231 | { |
940 | if (register_service (dev, rc_addr.rc_channel) != 0) | 1232 | fprintf (stderr, "Failed to register a service on interface `%.*s': %s\n", IFNAMSIZ, |
941 | { | 1233 | dev->iface, strerror (errno)); |
942 | fprintf (stderr, "Failed to register a service on interface `%.*s': %s\n", IFNAMSIZ, | 1234 | return 1; |
943 | dev->iface, strerror (errno)); | 1235 | } |
944 | return 1; | 1236 | |
945 | } | 1237 | /* Switch socket in listening mode */ |
946 | 1238 | if (listen (dev->fd_rfcomm, 5) == -1) //FIXME: probably we need a bigger number | |
947 | /* Switch socket in listening mode */ | 1239 | { |
948 | if (listen (dev->fd_rfcomm, 5) == -1) //FIXME: probably we need a bigger number | 1240 | fprintf (stderr, "Failed to listen on socket for interface `%.*s': %s\n", IFNAMSIZ, |
949 | { | 1241 | dev->iface, strerror (errno)); |
950 | fprintf (stderr, "Failed to listen on socket for interface `%.*s': %s\n", IFNAMSIZ, | 1242 | return 1; |
951 | dev->iface, strerror (errno)); | 1243 | } |
952 | return 1; | ||
953 | } | ||
954 | |||
955 | 1244 | ||
1245 | #endif | ||
1246 | |||
956 | return 0; | 1247 | return 0; |
957 | } | 1248 | } |
958 | 1249 | ||
@@ -969,38 +1260,45 @@ static void | |||
969 | mac_set (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader, | 1260 | mac_set (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader, |
970 | const struct HardwareInfos *dev) | 1261 | const struct HardwareInfos *dev) |
971 | { | 1262 | { |
972 | taIeeeHeader->frame_control = htons (IEEE80211_FC0_TYPE_DATA); | 1263 | taIeeeHeader->frame_control = htons (IEEE80211_FC0_TYPE_DATA); |
973 | taIeeeHeader->addr2 = dev->pl_mac; | ||
974 | taIeeeHeader->addr3 = mac_bssid_gnunet; | 1264 | taIeeeHeader->addr3 = mac_bssid_gnunet; |
975 | } | ||
976 | 1265 | ||
977 | /** | 1266 | #ifdef MINGW |
978 | * Test if the given interface name really corresponds to a bluetooth | 1267 | memcpy (&taIeeeHeader->addr2, &dev->pl_mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)); |
979 | * device. | 1268 | #else |
980 | * | 1269 | taIeeeHeader->addr2 = dev->pl_mac; |
981 | * @param iface name of the interface | 1270 | #endif |
982 | * @return 0 on success, 1 on error | 1271 | } |
983 | **** similar with the one from gnunet-helper-transport-wlan.c **** | ||
984 | */ | ||
985 | static int | ||
986 | test_bluetooth_interface (const char *iface) | ||
987 | { | ||
988 | char strbuf[512]; | ||
989 | struct stat sbuf; | ||
990 | int ret; | ||
991 | 1272 | ||
992 | ret = snprintf (strbuf, sizeof (strbuf), | 1273 | #ifdef LINUX |
993 | "/sys/class/bluetooth/%s/subsystem", | 1274 | /** |
994 | iface); | 1275 | * Test if the given interface name really corresponds to a bluetooth |
995 | if ((ret < 0) || (ret >= sizeof (strbuf)) || (0 != stat (strbuf, &sbuf))) | 1276 | * device. |
1277 | * | ||
1278 | * @param iface name of the interface | ||
1279 | * @return 0 on success, 1 on error | ||
1280 | **** similar with the one from gnunet-helper-transport-wlan.c **** | ||
1281 | */ | ||
1282 | static int | ||
1283 | test_bluetooth_interface (const char *iface) | ||
996 | { | 1284 | { |
997 | fprintf (stderr, | 1285 | char strbuf[512]; |
998 | "Did not find 802.15.1 interface `%s'. Exiting.\n", | 1286 | struct stat sbuf; |
999 | iface); | 1287 | int ret; |
1000 | exit (1); | 1288 | |
1289 | ret = snprintf (strbuf, sizeof (strbuf), | ||
1290 | "/sys/class/bluetooth/%s/subsystem", | ||
1291 | iface); | ||
1292 | if ((ret < 0) || (ret >= sizeof (strbuf)) || (0 != stat (strbuf, &sbuf))) | ||
1293 | { | ||
1294 | fprintf (stderr, | ||
1295 | "Did not find 802.15.1 interface `%s'. Exiting.\n", | ||
1296 | iface); | ||
1297 | exit (1); | ||
1298 | } | ||
1299 | return 0; | ||
1001 | } | 1300 | } |
1002 | return 0; | 1301 | #endif |
1003 | } | ||
1004 | 1302 | ||
1005 | /** | 1303 | /** |
1006 | * Test incoming packets mac for being our own. | 1304 | * Test incoming packets mac for being our own. |
@@ -1049,7 +1347,7 @@ stdin_send_hw (void *cls, const struct GNUNET_MessageHeader *hdr) | |||
1049 | 1347 | ||
1050 | sendsize = ntohs (hdr->size); | 1348 | sendsize = ntohs (hdr->size); |
1051 | if ( (sendsize < | 1349 | if ( (sendsize < |
1052 | sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage)) || | 1350 | sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage)) || |
1053 | (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs (hdr->type)) ) | 1351 | (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs (hdr->type)) ) |
1054 | { | 1352 | { |
1055 | fprintf (stderr, "Received malformed message\n"); | 1353 | fprintf (stderr, "Received malformed message\n"); |
@@ -1074,222 +1372,224 @@ stdin_send_hw (void *cls, const struct GNUNET_MessageHeader *hdr) | |||
1074 | write_pout.size = sendsize; | 1372 | write_pout.size = sendsize; |
1075 | } | 1373 | } |
1076 | 1374 | ||
1077 | /** | 1375 | #ifdef LINUX |
1078 | * Broadcast a HELLO message for peer discovery | 1376 | /** |
1079 | * | 1377 | * Broadcast a HELLO message for peer discovery |
1080 | * @param dev pointer to the device struct | 1378 | * |
1081 | * @param dev pointer to the socket which was added to the set | 1379 | * @param dev pointer to the device struct |
1082 | * @return 0 on success | 1380 | * @param dev pointer to the socket which was added to the set |
1083 | */ | 1381 | * @return 0 on success |
1084 | static int | 1382 | */ |
1085 | send_broadcast (struct HardwareInfos *dev, int *sendsocket) | 1383 | static int |
1086 | { | 1384 | send_broadcast (struct HardwareInfos *dev, int *sendsocket) |
1087 | int new_device = 0; | 1385 | { |
1088 | int loops = 0; | 1386 | int new_device = 0; |
1387 | int loops = 0; | ||
1089 | 1388 | ||
1090 | search_for_devices: | 1389 | search_for_devices: |
1091 | if ((neighbours.size == neighbours.pos && new_device == 1) || neighbours.size == 0) | 1390 | if ((neighbours.size == neighbours.pos && new_device == 1) || neighbours.size == 0) |
1092 | { | 1391 | { |
1093 | inquiry_devices: //skip the conditions and force a inquiry for new devices | 1392 | inquiry_devices: //skip the conditions and force a inquiry for new devices |
1094 | { | 1393 | { |
1095 | /** | 1394 | /** |
1096 | * It means that I sent HELLO messages to all the devices from the list and I should search | 1395 | * It means that I sent HELLO messages to all the devices from the list and I should search |
1097 | * for new ones or that this is the first time when I do a search. | 1396 | * for new ones or that this is the first time when I do a search. |
1098 | */ | 1397 | */ |
1099 | inquiry_info *devices = NULL; | 1398 | inquiry_info *devices = NULL; |
1100 | int i, responses, max_responses = MAX_PORTS; | 1399 | int i, responses, max_responses = MAX_PORTS; |
1101 | 1400 | ||
1102 | /* sanity checks */ | 1401 | /* sanity checks */ |
1103 | if (neighbours.size >= MAX_PORTS) | 1402 | if (neighbours.size >= MAX_PORTS) |
1104 | { | 1403 | { |
1105 | fprintf (stderr, "%.*s reached the top limit for the discovarable devices\n", IFNAMSIZ, dev->iface); | 1404 | fprintf (stderr, "%.*s reached the top limit for the discovarable devices\n", IFNAMSIZ, dev->iface); |
1106 | return 2; | 1405 | return 2; |
1107 | } | 1406 | } |
1108 | 1407 | ||
1109 | /* Get the device id */ | 1408 | /* Get the device id */ |
1110 | if (neighbours.dev_id == -1) | 1409 | if (neighbours.dev_id == -1) |
1111 | { | 1410 | { |
1112 | char addr[19] = { 0 }; //the device MAC address | 1411 | char addr[19] = { 0 }; //the device MAC address |
1113 | 1412 | ||
1114 | ba2str ((bdaddr_t *) &dev->pl_mac, addr); | 1413 | ba2str ((bdaddr_t *) &dev->pl_mac, addr); |
1115 | neighbours.dev_id = hci_devid (addr); | 1414 | neighbours.dev_id = hci_devid (addr); |
1116 | if (neighbours.dev_id < 0) | 1415 | if (neighbours.dev_id < 0) |
1117 | { | 1416 | { |
1118 | fprintf (stderr, "Failed to get the device id for interface %.*s : %s\n", IFNAMSIZ, | 1417 | fprintf (stderr, "Failed to get the device id for interface %.*s : %s\n", IFNAMSIZ, |
1119 | dev->iface, strerror (errno)); | 1418 | dev->iface, strerror (errno)); |
1120 | return 1; | 1419 | return 1; |
1420 | } | ||
1121 | } | 1421 | } |
1122 | } | ||
1123 | |||
1124 | devices = malloc (max_responses * sizeof (inquiry_info)); | ||
1125 | if (devices == NULL) | ||
1126 | { | ||
1127 | fprintf (stderr, "Failed to allocate memory for inquiry info list on interface %.*s\n", IFNAMSIZ, | ||
1128 | dev->iface); | ||
1129 | return 1; | ||
1130 | } | ||
1131 | |||
1132 | responses = hci_inquiry (neighbours.dev_id, 8, max_responses, NULL, &devices, IREQ_CACHE_FLUSH); | ||
1133 | if (responses < 0) | ||
1134 | { | ||
1135 | fprintf (stderr, "Failed to inquiry on interface %.*s\n", IFNAMSIZ, dev->iface); | ||
1136 | return 1; | ||
1137 | } | ||
1138 | |||
1139 | fprintf (stderr, "LOG : Found %d devices\n", responses); //FIXME delete it after debugging stage | ||
1140 | |||
1141 | if (responses == 0) | ||
1142 | { | ||
1143 | fprintf (stderr, "LOG : No devices discoverable\n"); | ||
1144 | return 1; | ||
1145 | } | ||
1146 | 1422 | ||
1147 | for (i = 0; i < responses; i++) | 1423 | devices = malloc (max_responses * sizeof (inquiry_info)); |
1148 | { | 1424 | if (devices == NULL) |
1149 | int j; | ||
1150 | int found = 0; | ||
1151 | |||
1152 | /* sanity check */ | ||
1153 | if (i >= MAX_PORTS) | ||
1154 | { | 1425 | { |
1155 | fprintf (stderr, "%.*s reached the top limit for the discoverable devices (after inquiry)\n", IFNAMSIZ, | 1426 | fprintf (stderr, "Failed to allocate memory for inquiry info list on interface %.*s\n", IFNAMSIZ, |
1156 | dev->iface); | 1427 | dev->iface); |
1157 | return 2; | 1428 | return 1; |
1429 | } | ||
1430 | |||
1431 | responses = hci_inquiry (neighbours.dev_id, 8, max_responses, NULL, &devices, IREQ_CACHE_FLUSH); | ||
1432 | if (responses < 0) | ||
1433 | { | ||
1434 | fprintf (stderr, "Failed to inquiry on interface %.*s\n", IFNAMSIZ, dev->iface); | ||
1435 | return 1; | ||
1436 | } | ||
1437 | |||
1438 | fprintf (stderr, "LOG : Found %d devices\n", responses); //FIXME delete it after debugging stage | ||
1439 | |||
1440 | if (responses == 0) | ||
1441 | { | ||
1442 | fprintf (stderr, "LOG : No devices discoverable\n"); | ||
1443 | return 1; | ||
1158 | } | 1444 | } |
1159 | 1445 | ||
1160 | /* Search if the address already exists on the list */ | 1446 | for (i = 0; i < responses; i++) |
1161 | for (j = 0; j < neighbours.size; j++) | ||
1162 | { | 1447 | { |
1163 | if (memcmp (&(devices + i)->bdaddr, &(neighbours.devices[j]), sizeof (bdaddr_t)) == 0) | 1448 | int j; |
1449 | int found = 0; | ||
1450 | |||
1451 | /* sanity check */ | ||
1452 | if (i >= MAX_PORTS) | ||
1164 | { | 1453 | { |
1165 | found = 1; | 1454 | fprintf (stderr, "%.*s reached the top limit for the discoverable devices (after inquiry)\n", IFNAMSIZ, |
1166 | fprintf (stderr, "LOG : the device already exists on the list\n"); //FIXME debugging message | 1455 | dev->iface); |
1167 | break; | 1456 | return 2; |
1457 | } | ||
1458 | |||
1459 | /* Search if the address already exists on the list */ | ||
1460 | for (j = 0; j < neighbours.size; j++) | ||
1461 | { | ||
1462 | if (memcmp (&(devices + i)->bdaddr, &(neighbours.devices[j]), sizeof (bdaddr_t)) == 0) | ||
1463 | { | ||
1464 | found = 1; | ||
1465 | fprintf (stderr, "LOG : the device already exists on the list\n"); //FIXME debugging message | ||
1466 | break; | ||
1467 | } | ||
1168 | } | 1468 | } |
1169 | } | ||
1170 | 1469 | ||
1171 | if (found == 0) | 1470 | if (found == 0) |
1172 | { | 1471 | { |
1173 | char addr[19] = { 0 }; | 1472 | char addr[19] = { 0 }; |
1174 | 1473 | ||
1175 | ba2str (&(devices +i)->bdaddr, addr); | 1474 | ba2str (&(devices +i)->bdaddr, addr); |
1176 | fprintf (stderr, "LOG : %s was added to the list\n", addr); //FIXME debugging message | 1475 | fprintf (stderr, "LOG : %s was added to the list\n", addr); //FIXME debugging message |
1177 | memcpy (&(neighbours.devices[neighbours.size++]), &(devices + i)->bdaddr, sizeof (bdaddr_t)); | 1476 | memcpy (&(neighbours.devices[neighbours.size++]), &(devices + i)->bdaddr, sizeof (bdaddr_t)); |
1477 | } | ||
1478 | } | ||
1479 | |||
1480 | free (devices); | ||
1178 | } | 1481 | } |
1179 | } | ||
1180 | |||
1181 | free (devices); | ||
1182 | } | 1482 | } |
1183 | } | 1483 | |
1184 | 1484 | int connection_successful = 0; | |
1185 | int connection_successful = 0; | 1485 | struct sockaddr_rc addr_rc = { 0 }; |
1186 | struct sockaddr_rc addr_rc = { 0 }; | 1486 | int errno_copy = 0; |
1187 | int errno_copy = 0; | 1487 | addr_rc.rc_family = AF_BLUETOOTH; |
1188 | addr_rc.rc_family = AF_BLUETOOTH; | ||
1189 | 1488 | ||
1190 | /* Try to connect to a new device from the list */ | 1489 | /* Try to connect to a new device from the list */ |
1191 | while (neighbours.pos < neighbours.size) | 1490 | while (neighbours.pos < neighbours.size) |
1192 | { | ||
1193 | /* Check if we are already connected to this device */ | ||
1194 | if (neighbours.fds[neighbours.pos] == -1) | ||
1195 | { | 1491 | { |
1196 | 1492 | /* Check if we are already connected to this device */ | |
1197 | memset (&addr_rc.rc_bdaddr, 0, sizeof (addr_rc.rc_bdaddr)); | 1493 | if (neighbours.fds[neighbours.pos] == -1) |
1198 | memcpy (&addr_rc.rc_bdaddr, &(neighbours.devices[neighbours.pos]), sizeof (addr_rc.rc_bdaddr)); | ||
1199 | |||
1200 | addr_rc.rc_channel = get_channel (dev, addr_rc.rc_bdaddr); | ||
1201 | |||
1202 | *sendsocket = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); | ||
1203 | if (connect (*sendsocket, (struct sockaddr *)&addr_rc, sizeof (addr_rc)) == 0) | ||
1204 | { | 1494 | { |
1205 | neighbours.fds[neighbours.pos++] = *sendsocket; | ||
1206 | connection_successful = 1; | ||
1207 | char addr[19] = { 0 }; | ||
1208 | ba2str (&(neighbours.devices[neighbours.pos - 1]), addr); | ||
1209 | fprintf (stderr, "LOG : Connected to %s\n", addr); | ||
1210 | 1495 | ||
1211 | break; | 1496 | memset (&addr_rc.rc_bdaddr, 0, sizeof (addr_rc.rc_bdaddr)); |
1212 | } | 1497 | memcpy (&addr_rc.rc_bdaddr, &(neighbours.devices[neighbours.pos]), sizeof (addr_rc.rc_bdaddr)); |
1213 | else | 1498 | |
1214 | { | 1499 | addr_rc.rc_channel = get_channel (dev, addr_rc.rc_bdaddr); |
1215 | char addr[19] = { 0 }; | 1500 | |
1216 | errno_copy = errno; //Save a copy for later | 1501 | *sendsocket = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); |
1217 | ba2str (&(neighbours.devices[neighbours.pos]), addr); | 1502 | if (connect (*sendsocket, (struct sockaddr *)&addr_rc, sizeof (addr_rc)) == 0) |
1218 | fprintf (stderr, "LOG : Couldn't connect on device %s, error : %s\n", addr, strerror(errno)); | ||
1219 | if (errno != ECONNREFUSED) //FIXME be sure that this works | ||
1220 | { | 1503 | { |
1221 | fprintf (stderr, "LOG : Removes %d device from the list\n", neighbours.pos); | 1504 | neighbours.fds[neighbours.pos++] = *sendsocket; |
1222 | /* Remove the device from the list */ | 1505 | connection_successful = 1; |
1223 | memcpy (&neighbours.devices[neighbours.pos], &neighbours.devices[neighbours.size - 1], sizeof (bdaddr_t)); | 1506 | char addr[19] = { 0 }; |
1224 | memset (&neighbours.devices[neighbours.size - 1], 0, sizeof (bdaddr_t)); | 1507 | ba2str (&(neighbours.devices[neighbours.pos - 1]), addr); |
1225 | neighbours.fds[neighbours.pos] = neighbours.fds[neighbours.size - 1]; | 1508 | fprintf (stderr, "LOG : Connected to %s\n", addr); |
1226 | neighbours.fds[neighbours.size - 1] = -1; | 1509 | |
1227 | neighbours.size -= 1; | 1510 | break; |
1228 | } | 1511 | } |
1512 | else | ||
1513 | { | ||
1514 | char addr[19] = { 0 }; | ||
1515 | errno_copy = errno; //Save a copy for later | ||
1516 | ba2str (&(neighbours.devices[neighbours.pos]), addr); | ||
1517 | fprintf (stderr, "LOG : Couldn't connect on device %s, error : %s\n", addr, strerror(errno)); | ||
1518 | if (errno != ECONNREFUSED) //FIXME be sure that this works | ||
1519 | { | ||
1520 | fprintf (stderr, "LOG : Removes %d device from the list\n", neighbours.pos); | ||
1521 | /* Remove the device from the list */ | ||
1522 | memcpy (&neighbours.devices[neighbours.pos], &neighbours.devices[neighbours.size - 1], sizeof (bdaddr_t)); | ||
1523 | memset (&neighbours.devices[neighbours.size - 1], 0, sizeof (bdaddr_t)); | ||
1524 | neighbours.fds[neighbours.pos] = neighbours.fds[neighbours.size - 1]; | ||
1525 | neighbours.fds[neighbours.size - 1] = -1; | ||
1526 | neighbours.size -= 1; | ||
1527 | } | ||
1229 | 1528 | ||
1230 | neighbours.pos += 1; | 1529 | neighbours.pos += 1; |
1231 | 1530 | ||
1232 | if (neighbours.pos >= neighbours.size) | 1531 | if (neighbours.pos >= neighbours.size) |
1233 | neighbours.pos = 0; | 1532 | neighbours.pos = 0; |
1234 | 1533 | ||
1235 | loops += 1; | 1534 | loops += 1; |
1236 | 1535 | ||
1237 | if (loops == MAX_LOOPS) //don't get stuck trying to connect to one device | 1536 | if (loops == MAX_LOOPS) //don't get stuck trying to connect to one device |
1238 | return 1; | 1537 | return 1; |
1538 | } | ||
1539 | } | ||
1540 | else | ||
1541 | { | ||
1542 | fprintf (stderr, "LOG : Search for a new device\n"); //FIXME debugging message | ||
1543 | neighbours.pos += 1; | ||
1239 | } | 1544 | } |
1240 | } | 1545 | } |
1241 | else | 1546 | |
1547 | /* Cycle on the list */ | ||
1548 | if (neighbours.pos == neighbours.size) | ||
1242 | { | 1549 | { |
1243 | fprintf (stderr, "LOG : Search for a new device\n"); //FIXME debugging message | 1550 | neighbours.pos = 0; |
1244 | neighbours.pos += 1; | 1551 | searching_devices_count += 1; |
1245 | } | ||
1246 | } | ||
1247 | |||
1248 | /* Cycle on the list */ | ||
1249 | if (neighbours.pos == neighbours.size) | ||
1250 | { | ||
1251 | neighbours.pos = 0; | ||
1252 | searching_devices_count += 1; | ||
1253 | 1552 | ||
1254 | if (searching_devices_count == MAX_LOOPS) | 1553 | if (searching_devices_count == MAX_LOOPS) |
1255 | { | 1554 | { |
1256 | fprintf (stderr, "LOG : Force to inquiry for new devices\n"); | 1555 | fprintf (stderr, "LOG : Force to inquiry for new devices\n"); |
1257 | searching_devices_count = 0; | 1556 | searching_devices_count = 0; |
1258 | goto inquiry_devices; | 1557 | goto inquiry_devices; |
1558 | } | ||
1259 | } | 1559 | } |
1260 | } | 1560 | /* If a new device wasn't found, search an old one */ |
1261 | /* If a new device wasn't found, search an old one */ | 1561 | if (connection_successful == 0) |
1262 | if (connection_successful == 0) | ||
1263 | { | ||
1264 | int loop_check = neighbours.pos; | ||
1265 | while (neighbours.fds[neighbours.pos] == -1) | ||
1266 | { | 1562 | { |
1267 | if (neighbours.pos == neighbours.size) | 1563 | int loop_check = neighbours.pos; |
1268 | neighbours.pos = 0; | 1564 | while (neighbours.fds[neighbours.pos] == -1) |
1269 | |||
1270 | if (neighbours.pos == loop_check) | ||
1271 | { | 1565 | { |
1272 | if (errno_copy == ECONNREFUSED) | 1566 | if (neighbours.pos == neighbours.size) |
1273 | { | 1567 | neighbours.pos = 0; |
1274 | fprintf (stderr, "LOG : No device found. Go back and search again\n"); //FIXME debugging message | 1568 | |
1275 | new_device = 1; | 1569 | if (neighbours.pos == loop_check) |
1276 | loops += 1; | ||
1277 | goto search_for_devices; | ||
1278 | } | ||
1279 | else | ||
1280 | { | 1570 | { |
1281 | return 1; // Skip the broadcast message | 1571 | if (errno_copy == ECONNREFUSED) |
1572 | { | ||
1573 | fprintf (stderr, "LOG : No device found. Go back and search again\n"); //FIXME debugging message | ||
1574 | new_device = 1; | ||
1575 | loops += 1; | ||
1576 | goto search_for_devices; | ||
1577 | } | ||
1578 | else | ||
1579 | { | ||
1580 | return 1; // Skip the broadcast message | ||
1581 | } | ||
1282 | } | 1582 | } |
1583 | |||
1584 | neighbours.pos += 1; | ||
1283 | } | 1585 | } |
1284 | 1586 | ||
1285 | neighbours.pos += 1; | 1587 | *sendsocket = neighbours.fds[neighbours.pos++]; |
1286 | } | 1588 | } |
1287 | 1589 | ||
1288 | *sendsocket = neighbours.fds[neighbours.pos++]; | 1590 | return 0; |
1289 | } | 1591 | } |
1290 | 1592 | #endif | |
1291 | return 0; | ||
1292 | } | ||
1293 | 1593 | ||
1294 | /** | 1594 | /** |
1295 | * Main function of the helper. This code accesses a bluetooth interface | 1595 | * Main function of the helper. This code accesses a bluetooth interface |
@@ -1304,465 +1604,861 @@ send_broadcast (struct HardwareInfos *dev, int *sendsocket) | |||
1304 | */ | 1604 | */ |
1305 | int | 1605 | int |
1306 | main (int argc, char *argv[]) | 1606 | main (int argc, char *argv[]) |
1307 | { | 1607 | { |
1308 | struct HardwareInfos dev; | 1608 | #ifdef LINUX |
1309 | char readbuf[MAXLINE]; | 1609 | struct HardwareInfos dev; |
1310 | int maxfd; | 1610 | char readbuf[MAXLINE]; |
1311 | fd_set rfds; | 1611 | int maxfd; |
1312 | fd_set wfds; | 1612 | fd_set rfds; |
1313 | int stdin_open; | 1613 | fd_set wfds; |
1314 | struct MessageStreamTokenizer *stdin_mst; | 1614 | int stdin_open; |
1315 | int raw_eno, i; | 1615 | struct MessageStreamTokenizer *stdin_mst; |
1316 | uid_t uid; | 1616 | int raw_eno, i; |
1317 | int crt_rfds = 0, rfds_list[MAX_PORTS]; | 1617 | uid_t uid; |
1318 | int broadcast, sendsocket; | 1618 | int crt_rfds = 0, rfds_list[MAX_PORTS]; |
1319 | /* Assert privs so we can modify the firewall rules! */ | 1619 | int broadcast, sendsocket; |
1320 | uid = getuid (); | 1620 | /* Assert privs so we can modify the firewall rules! */ |
1321 | #ifdef HAVE_SETRESUID | 1621 | uid = getuid (); |
1322 | if (0 != setresuid (uid, 0, 0)) | 1622 | #ifdef HAVE_SETRESUID |
1323 | { | 1623 | if (0 != setresuid (uid, 0, 0)) |
1324 | fprintf (stderr, "Failed to setresuid to root: %s\n", strerror (errno)); | 1624 | { |
1325 | return 254; | 1625 | fprintf (stderr, "Failed to setresuid to root: %s\n", strerror (errno)); |
1326 | } | 1626 | return 254; |
1327 | #else | 1627 | } |
1328 | if (0 != seteuid (0)) | 1628 | #else |
1329 | { | 1629 | if (0 != seteuid (0)) |
1330 | fprintf (stderr, "Failed to seteuid back to root: %s\n", strerror (errno)); | 1630 | { |
1331 | return 254; | 1631 | fprintf (stderr, "Failed to seteuid back to root: %s\n", strerror (errno)); |
1332 | } | 1632 | return 254; |
1333 | #endif | 1633 | } |
1334 | 1634 | #endif | |
1335 | /* Make use of SGID capabilities on POSIX */ | ||
1336 | memset (&dev, 0, sizeof (dev)); | ||
1337 | dev.fd_rfcomm = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); | ||
1338 | raw_eno = errno; /* remember for later */ | ||
1339 | |||
1340 | /* Now that we've dropped root rights, we can do error checking */ | ||
1341 | if (2 != argc) | ||
1342 | { | ||
1343 | fprintf (stderr, "You must specify the name of the interface as the first \ | ||
1344 | and only argument to this program.\n"); | ||
1345 | if (-1 != dev.fd_rfcomm) | ||
1346 | (void) close (dev.fd_rfcomm); | ||
1347 | return 1; | ||
1348 | } | ||
1349 | 1635 | ||
1350 | if (-1 == dev.fd_rfcomm) | 1636 | /* Make use of SGID capabilities on POSIX */ |
1351 | { | 1637 | memset (&dev, 0, sizeof (dev)); |
1352 | fprintf (stderr, "Failed to create a HCI socket: %s\n", strerror (raw_eno)); | 1638 | dev.fd_rfcomm = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); |
1353 | return 1; | 1639 | raw_eno = errno; /* remember for later */ |
1354 | } | ||
1355 | if (dev.fd_rfcomm >= FD_SETSIZE) | ||
1356 | { | ||
1357 | fprintf (stderr, "File descriptor too large for select (%d > %d)\n", | ||
1358 | dev.fd_rfcomm, FD_SETSIZE); | ||
1359 | (void) close (dev.fd_rfcomm); | ||
1360 | return 1; | ||
1361 | } | ||
1362 | if (0 != test_bluetooth_interface (argv[1])) | ||
1363 | { | ||
1364 | (void) close (dev.fd_rfcomm); | ||
1365 | return 1; | ||
1366 | } | ||
1367 | strncpy (dev.iface, argv[1], IFNAMSIZ); | ||
1368 | if (0 != open_device (&dev)) | ||
1369 | { | ||
1370 | (void) close (dev.fd_rfcomm); | ||
1371 | return 1; | ||
1372 | } | ||
1373 | 1640 | ||
1374 | /* Drop privs */ | 1641 | /* Now that we've dropped root rights, we can do error checking */ |
1375 | { | 1642 | if (2 != argc) |
1376 | uid_t uid = getuid (); | ||
1377 | #ifdef HAVE_SETRESUID | ||
1378 | if (0 != setresuid (uid, uid, uid)) | ||
1379 | { | 1643 | { |
1380 | fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno)); | 1644 | fprintf (stderr, "You must specify the name of the interface as the first \ |
1645 | and only argument to this program.\n"); | ||
1381 | if (-1 != dev.fd_rfcomm) | 1646 | if (-1 != dev.fd_rfcomm) |
1382 | (void) close (dev.fd_rfcomm); | 1647 | (void) close (dev.fd_rfcomm); |
1383 | return 1; | 1648 | return 1; |
1384 | } | 1649 | } |
1385 | #else | 1650 | |
1386 | if (0 != (setuid (uid) | seteuid (uid))) | 1651 | if (-1 == dev.fd_rfcomm) |
1387 | { | 1652 | { |
1388 | fprintf (stderr, "Failed to setuid: %s\n", strerror (errno)); | 1653 | fprintf (stderr, "Failed to create a HCI socket: %s\n", strerror (raw_eno)); |
1389 | if (-1 != dev.fd_rfcomm) | ||
1390 | (void) close (dev.fd_rfcomm); | ||
1391 | return 1; | 1654 | return 1; |
1392 | } | 1655 | } |
1393 | #endif | 1656 | if (dev.fd_rfcomm >= FD_SETSIZE) |
1394 | } | ||
1395 | |||
1396 | /* Send MAC address of the bluetooth interface to STDOUT first */ | ||
1397 | { | ||
1398 | struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg; | ||
1399 | |||
1400 | macmsg.hdr.size = htons (sizeof (macmsg)); | ||
1401 | macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL); | ||
1402 | memcpy (&macmsg.mac, &dev.pl_mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)); | ||
1403 | memcpy (write_std.buf, &macmsg, sizeof (macmsg)); | ||
1404 | write_std.size = sizeof (macmsg); | ||
1405 | } | ||
1406 | |||
1407 | |||
1408 | stdin_mst = mst_create (&stdin_send_hw, &dev); | ||
1409 | stdin_open = 1; | ||
1410 | |||
1411 | fprintf (stderr, "\n-----------------------------------------------\n Check if the program exits\n-----------------------------------------------\n"); | ||
1412 | /** | ||
1413 | * TODO : When a connection fails I should ignore only the CONTROL messages. | ||
1414 | * For DATA messages I should retry to send the message until it doesn't fail | ||
1415 | * Also I should make the time out of a mac endpoint smaller and check if the rate | ||
1416 | * from get_wlan_header (plugin_transport_bluetooth.c) is correct. | ||
1417 | */ | ||
1418 | while (1) | ||
1419 | { | ||
1420 | maxfd = -1; | ||
1421 | broadcast = 0; | ||
1422 | sendsocket = -1; | ||
1423 | |||
1424 | FD_ZERO (&rfds); | ||
1425 | if ((0 == write_pout.size) && (1 == stdin_open)) | ||
1426 | { | 1657 | { |
1427 | FD_SET (STDIN_FILENO, &rfds); | 1658 | fprintf (stderr, "File descriptor too large for select (%d > %d)\n", |
1428 | maxfd = MAX (maxfd, STDIN_FILENO); | 1659 | dev.fd_rfcomm, FD_SETSIZE); |
1660 | (void) close (dev.fd_rfcomm); | ||
1661 | return 1; | ||
1429 | } | 1662 | } |
1430 | if (0 == write_std.size) | 1663 | if (0 != test_bluetooth_interface (argv[1])) |
1431 | { | 1664 | { |
1432 | FD_SET (dev.fd_rfcomm, &rfds); | 1665 | (void) close (dev.fd_rfcomm); |
1433 | maxfd = MAX (maxfd, dev.fd_rfcomm); | 1666 | return 1; |
1667 | } | ||
1668 | strncpy (dev.iface, argv[1], IFNAMSIZ); | ||
1669 | if (0 != open_device (&dev)) | ||
1670 | { | ||
1671 | (void) close (dev.fd_rfcomm); | ||
1672 | return 1; | ||
1434 | } | 1673 | } |
1435 | 1674 | ||
1436 | for (i = 0; i < crt_rfds; i++) // it can receive messages from multiple devices | 1675 | /* Drop privs */ |
1437 | { | 1676 | { |
1438 | FD_SET (rfds_list[i], &rfds); | 1677 | uid_t uid = getuid (); |
1439 | maxfd = MAX (maxfd, rfds_list[i]); | 1678 | #ifdef HAVE_SETRESUID |
1679 | if (0 != setresuid (uid, uid, uid)) | ||
1680 | { | ||
1681 | fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno)); | ||
1682 | if (-1 != dev.fd_rfcomm) | ||
1683 | (void) close (dev.fd_rfcomm); | ||
1684 | return 1; | ||
1685 | } | ||
1686 | #else | ||
1687 | if (0 != (setuid (uid) | seteuid (uid))) | ||
1688 | { | ||
1689 | fprintf (stderr, "Failed to setuid: %s\n", strerror (errno)); | ||
1690 | if (-1 != dev.fd_rfcomm) | ||
1691 | (void) close (dev.fd_rfcomm); | ||
1692 | return 1; | ||
1693 | } | ||
1694 | #endif | ||
1440 | } | 1695 | } |
1441 | FD_ZERO (&wfds); | 1696 | |
1442 | if (0 < write_std.size) | 1697 | /* Send MAC address of the bluetooth interface to STDOUT first */ |
1443 | { | 1698 | { |
1444 | FD_SET (STDOUT_FILENO, &wfds); | 1699 | struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg; |
1445 | maxfd = MAX (maxfd, STDOUT_FILENO); | 1700 | |
1701 | macmsg.hdr.size = htons (sizeof (macmsg)); | ||
1702 | macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL); | ||
1703 | memcpy (&macmsg.mac, &dev.pl_mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)); | ||
1704 | memcpy (write_std.buf, &macmsg, sizeof (macmsg)); | ||
1705 | write_std.size = sizeof (macmsg); | ||
1446 | } | 1706 | } |
1447 | if (0 < write_pout.size) //it can send messages only to one device per loop | ||
1448 | { | ||
1449 | struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *frame; | ||
1450 | /* Get the destination address */ | ||
1451 | frame = (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *) write_pout.buf; | ||
1452 | 1707 | ||
1453 | if (memcmp (&frame->addr1, &dev.pl_mac, | 1708 | |
1454 | sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0) | 1709 | stdin_mst = mst_create (&stdin_send_hw, &dev); |
1710 | stdin_open = 1; | ||
1711 | |||
1712 | fprintf (stderr, "\n-----------------------------------------------\n Check if the program exits\n-----------------------------------------------\n"); | ||
1713 | /** | ||
1714 | * TODO : When a connection fails I should ignore only the CONTROL messages. | ||
1715 | * For DATA messages I should retry to send the message until it doesn't fail | ||
1716 | * Also I should make the time out of a mac endpoint smaller and check if the rate | ||
1717 | * from get_wlan_header (plugin_transport_bluetooth.c) is correct. | ||
1718 | */ | ||
1719 | while (1) | ||
1720 | { | ||
1721 | maxfd = -1; | ||
1722 | broadcast = 0; | ||
1723 | sendsocket = -1; | ||
1724 | |||
1725 | FD_ZERO (&rfds); | ||
1726 | if ((0 == write_pout.size) && (1 == stdin_open)) | ||
1727 | { | ||
1728 | FD_SET (STDIN_FILENO, &rfds); | ||
1729 | maxfd = MAX (maxfd, STDIN_FILENO); | ||
1730 | } | ||
1731 | if (0 == write_std.size) | ||
1455 | { | 1732 | { |
1456 | broadcast = 1; | 1733 | FD_SET (dev.fd_rfcomm, &rfds); |
1457 | memset (&write_pout, 0, sizeof (write_pout)); //clear the buffer | 1734 | maxfd = MAX (maxfd, dev.fd_rfcomm); |
1458 | } | 1735 | } |
1459 | else if (memcmp (&frame->addr1, &broadcast_address, | 1736 | |
1460 | sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0) | 1737 | for (i = 0; i < crt_rfds; i++) // it can receive messages from multiple devices |
1738 | { | ||
1739 | FD_SET (rfds_list[i], &rfds); | ||
1740 | maxfd = MAX (maxfd, rfds_list[i]); | ||
1741 | } | ||
1742 | FD_ZERO (&wfds); | ||
1743 | if (0 < write_std.size) | ||
1461 | { | 1744 | { |
1462 | fprintf (stderr, "LOG : %s has a broadcast message (pos %d, size %d)\n", dev.iface, neighbours.pos, neighbours.size); //FIXME: debugging message | 1745 | FD_SET (STDOUT_FILENO, &wfds); |
1463 | // broadcast = 1; // IF I HAVE A BROADCAST MESSAGE I skip. | 1746 | maxfd = MAX (maxfd, STDOUT_FILENO); |
1464 | // memset (&write_pout, 0, sizeof (write_pout)); | 1747 | } |
1465 | 1748 | if (0 < write_pout.size) //it can send messages only to one device per loop | |
1466 | if (send_broadcast(&dev, &sendsocket) != 0) //if the searching wasn't successful don't get stuck on the select stage | 1749 | { |
1750 | struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *frame; | ||
1751 | /* Get the destination address */ | ||
1752 | frame = (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *) write_pout.buf; | ||
1753 | |||
1754 | if (memcmp (&frame->addr1, &dev.pl_mac, | ||
1755 | sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0) | ||
1467 | { | 1756 | { |
1468 | broadcast = 1; | 1757 | broadcast = 1; |
1469 | memset (&write_pout, 0, sizeof (write_pout)); //remove the message | 1758 | memset (&write_pout, 0, sizeof (write_pout)); //clear the buffer |
1470 | fprintf (stderr, "LOG : Skip the broadcast message (pos %d, size %d)\n", neighbours.pos, neighbours.size); | 1759 | } |
1471 | } | 1760 | else if (memcmp (&frame->addr1, &broadcast_address, |
1472 | else | 1761 | sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0) |
1473 | { | 1762 | { |
1474 | FD_SET (sendsocket, &wfds); | 1763 | fprintf (stderr, "LOG : %s has a broadcast message (pos %d, size %d)\n", dev.iface, neighbours.pos, neighbours.size); //FIXME: debugging message |
1475 | maxfd = MAX (maxfd, sendsocket); | 1764 | // broadcast = 1; // IF I HAVE A BROADCAST MESSAGE I skip. |
1476 | } | 1765 | // memset (&write_pout, 0, sizeof (write_pout)); |
1477 | } | 1766 | |
1478 | else | 1767 | if (send_broadcast(&dev, &sendsocket) != 0) //if the searching wasn't successful don't get stuck on the select stage |
1479 | { | 1768 | { |
1480 | int found = 0; | 1769 | broadcast = 1; |
1481 | int pos = 0; | 1770 | memset (&write_pout, 0, sizeof (write_pout)); //remove the message |
1482 | /* Search if the address already exists on the list */ | 1771 | fprintf (stderr, "LOG : Skip the broadcast message (pos %d, size %d)\n", neighbours.pos, neighbours.size); |
1483 | for (i = 0; i < neighbours.size; i++) | 1772 | } |
1773 | else | ||
1774 | { | ||
1775 | FD_SET (sendsocket, &wfds); | ||
1776 | maxfd = MAX (maxfd, sendsocket); | ||
1777 | } | ||
1778 | } | ||
1779 | else | ||
1484 | { | 1780 | { |
1485 | if (memcmp (&frame->addr1, &(neighbours.devices[i]), sizeof (bdaddr_t)) == 0) | 1781 | int found = 0; |
1782 | int pos = 0; | ||
1783 | /* Search if the address already exists on the list */ | ||
1784 | for (i = 0; i < neighbours.size; i++) | ||
1486 | { | 1785 | { |
1487 | pos = i; | 1786 | if (memcmp (&frame->addr1, &(neighbours.devices[i]), sizeof (bdaddr_t)) == 0) |
1488 | if (neighbours.fds[i] != -1) | ||
1489 | { | 1787 | { |
1490 | found = 1; //save the position where it was found | 1788 | pos = i; |
1491 | FD_SET (neighbours.fds[i], &wfds); | 1789 | if (neighbours.fds[i] != -1) |
1492 | maxfd = MAX (maxfd, neighbours.fds[i]); | 1790 | { |
1493 | sendsocket = neighbours.fds[i]; | 1791 | found = 1; //save the position where it was found |
1494 | fprintf (stderr, "LOG: the address was found in the list\n"); | 1792 | FD_SET (neighbours.fds[i], &wfds); |
1495 | break; | 1793 | maxfd = MAX (maxfd, neighbours.fds[i]); |
1794 | sendsocket = neighbours.fds[i]; | ||
1795 | fprintf (stderr, "LOG: the address was found in the list\n"); | ||
1796 | break; | ||
1797 | } | ||
1496 | } | 1798 | } |
1497 | } | 1799 | } |
1498 | } | 1800 | if (found == 0) |
1499 | if (found == 0) | ||
1500 | { | ||
1501 | int status; | ||
1502 | struct sockaddr_rc addr = { 0 }; | ||
1503 | |||
1504 | fprintf (stderr, "LOG : %s has a new message for %.2X:%.2X:%.2X:%.2X:%.2X:%.2X which isn't on the broadcast list\n", dev.iface, | ||
1505 | frame->addr1.mac[5], frame->addr1.mac[4], frame->addr1.mac[3], | ||
1506 | frame->addr1.mac[2], frame->addr1.mac[1], frame->addr1.mac[0]); //FIXME: debugging message | ||
1507 | |||
1508 | sendsocket = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); | ||
1509 | |||
1510 | if (sendsocket < 0) | ||
1511 | { | 1801 | { |
1512 | fprintf (stderr, "Failed to create a RFCOMM socket (sending stage): %s\n", | 1802 | int status; |
1513 | strerror (errno)); | 1803 | struct sockaddr_rc addr = { 0 }; |
1514 | return -1; | ||
1515 | } | ||
1516 | |||
1517 | memcpy (&addr.rc_bdaddr, &frame->addr1, sizeof (bdaddr_t)); | ||
1518 | addr.rc_family = AF_BLUETOOTH; | ||
1519 | addr.rc_channel = get_channel (&dev, addr.rc_bdaddr); | ||
1520 | 1804 | ||
1521 | int tries = 0; | 1805 | fprintf (stderr, "LOG : %s has a new message for %.2X:%.2X:%.2X:%.2X:%.2X:%.2X which isn't on the broadcast list\n", dev.iface, |
1522 | connect_retry: | 1806 | frame->addr1.mac[5], frame->addr1.mac[4], frame->addr1.mac[3], |
1523 | status = connect (sendsocket, (struct sockaddr *) &addr, sizeof (addr)); | 1807 | frame->addr1.mac[2], frame->addr1.mac[1], frame->addr1.mac[0]); //FIXME: debugging message |
1524 | if (0 != status && errno != EAGAIN) | 1808 | |
1525 | { | 1809 | sendsocket = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); |
1526 | if (errno == ECONNREFUSED && tries < 2) | 1810 | |
1811 | if (sendsocket < 0) | ||
1527 | { | 1812 | { |
1528 | fprintf (stderr, "LOG : %.*s failed to connect. Trying again!\n", IFNAMSIZ, dev.iface); | 1813 | fprintf (stderr, "Failed to create a RFCOMM socket (sending stage): %s\n", |
1529 | tries++; | 1814 | strerror (errno)); |
1530 | goto connect_retry; | 1815 | return -1; |
1531 | } | 1816 | } |
1532 | else if (errno == EBADF) | 1817 | |
1818 | memcpy (&addr.rc_bdaddr, &frame->addr1, sizeof (bdaddr_t)); | ||
1819 | addr.rc_family = AF_BLUETOOTH; | ||
1820 | addr.rc_channel = get_channel (&dev, addr.rc_bdaddr); | ||
1821 | |||
1822 | int tries = 0; | ||
1823 | connect_retry: | ||
1824 | status = connect (sendsocket, (struct sockaddr *) &addr, sizeof (addr)); | ||
1825 | if (0 != status && errno != EAGAIN) | ||
1533 | { | 1826 | { |
1534 | fprintf (stderr, "LOG : %s failed to connect : %s. Skip it!\n", dev.iface, strerror (errno)); | 1827 | if (errno == ECONNREFUSED && tries < 2) |
1535 | memset (&write_pout, 0, sizeof (write_pout)); | 1828 | { |
1536 | broadcast = 1; | 1829 | fprintf (stderr, "LOG : %.*s failed to connect. Trying again!\n", IFNAMSIZ, dev.iface); |
1830 | tries++; | ||
1831 | goto connect_retry; | ||
1832 | } | ||
1833 | else if (errno == EBADF) | ||
1834 | { | ||
1835 | fprintf (stderr, "LOG : %s failed to connect : %s. Skip it!\n", dev.iface, strerror (errno)); | ||
1836 | memset (&write_pout, 0, sizeof (write_pout)); | ||
1837 | broadcast = 1; | ||
1838 | } | ||
1839 | else | ||
1840 | { | ||
1841 | fprintf (stderr, "LOG : %s failed to connect : %s. Try again later!\n", dev.iface, strerror (errno)); | ||
1842 | memset (&write_pout, 0, sizeof (write_pout)); | ||
1843 | broadcast = 1; | ||
1844 | } | ||
1845 | |||
1537 | } | 1846 | } |
1538 | else | 1847 | else |
1539 | { | 1848 | { |
1540 | fprintf (stderr, "LOG : %s failed to connect : %s. Try again later!\n", dev.iface, strerror (errno)); | 1849 | FD_SET (sendsocket, &wfds); |
1541 | memset (&write_pout, 0, sizeof (write_pout)); | 1850 | maxfd = MAX (maxfd, sendsocket); |
1542 | broadcast = 1; | 1851 | fprintf (stderr, "LOG : Connection successful\n"); |
1852 | if (pos != 0) // save the socket | ||
1853 | { | ||
1854 | neighbours.fds[pos] = sendsocket; | ||
1855 | } | ||
1856 | else | ||
1857 | { | ||
1858 | /* Add the new device to the discovered devices list */ | ||
1859 | if (neighbours.size < MAX_PORTS) | ||
1860 | { | ||
1861 | neighbours.fds[neighbours.size] = sendsocket; | ||
1862 | memcpy (&(neighbours.devices[neighbours.size++]), &addr.rc_bdaddr, sizeof (bdaddr_t)); | ||
1863 | } | ||
1864 | else | ||
1865 | { | ||
1866 | fprintf (stderr, "The top limit for the discovarable devices' list was reached\n"); | ||
1867 | } | ||
1868 | } | ||
1543 | } | 1869 | } |
1544 | 1870 | } | |
1545 | } | 1871 | } |
1546 | else | 1872 | } |
1873 | |||
1874 | if (broadcast == 0) | ||
1875 | { | ||
1876 | /* Select a fd which is ready for action :) */ | ||
1877 | { | ||
1878 | int retval = select (maxfd + 1, &rfds, &wfds, NULL, NULL); | ||
1879 | if ((-1 == retval) && (EINTR == errno)) | ||
1880 | continue; | ||
1881 | if (0 > retval && errno != EBADF) // we handle BADF errors later | ||
1547 | { | 1882 | { |
1548 | FD_SET (sendsocket, &wfds); | 1883 | fprintf (stderr, "select failed: %s\n", strerror (errno)); |
1549 | maxfd = MAX (maxfd, sendsocket); | 1884 | break; |
1550 | fprintf (stderr, "LOG : Connection successful\n"); | 1885 | } |
1551 | if (pos != 0) // save the socket | 1886 | } |
1887 | if (FD_ISSET (STDOUT_FILENO , &wfds)) | ||
1888 | { | ||
1889 | ssize_t ret = | ||
1890 | write (STDOUT_FILENO, write_std.buf + write_std.pos, | ||
1891 | write_std.size - write_std.pos); | ||
1892 | if (0 > ret) | ||
1893 | { | ||
1894 | fprintf (stderr, "Failed to write to STDOUT: %s\n", strerror (errno)); | ||
1895 | break; | ||
1896 | } | ||
1897 | write_std.pos += ret; | ||
1898 | if (write_std.pos == write_std.size) | ||
1899 | { | ||
1900 | write_std.pos = 0; | ||
1901 | write_std.size = 0; | ||
1902 | } | ||
1903 | fprintf (stderr, "LOG : %s sends a message to STDOUT\n", dev.iface); //FIXME: debugging message | ||
1904 | |||
1905 | } | ||
1906 | if (sendsocket != -1) | ||
1907 | { | ||
1908 | if (FD_ISSET (sendsocket , &wfds)) | ||
1909 | { | ||
1910 | ssize_t ret = | ||
1911 | write (sendsocket, write_pout.buf + write_std.pos, | ||
1912 | write_pout.size - write_pout.pos); | ||
1913 | if (0 > ret) //FIXME should I first check the error type? | ||
1552 | { | 1914 | { |
1553 | neighbours.fds[pos] = sendsocket; | 1915 | fprintf (stderr, "Failed to write to bluetooth device: %s. Closing the socket!\n", |
1916 | strerror (errno)); | ||
1917 | for (i = 0; i < neighbours.size; i++) | ||
1918 | { | ||
1919 | if (neighbours.fds[i] == sendsocket) | ||
1920 | { | ||
1921 | (void) close(sendsocket); | ||
1922 | neighbours.fds[i] = -1; | ||
1923 | break; | ||
1924 | } | ||
1925 | } | ||
1926 | /* Remove the message */ | ||
1927 | memset (&write_pout.buf + write_std.pos, 0, (write_pout.size - write_pout.pos)); | ||
1928 | write_pout.pos = 0 ; | ||
1929 | write_pout.size = 0; | ||
1554 | } | 1930 | } |
1555 | else | 1931 | else |
1556 | { | 1932 | { |
1557 | /* Add the new device to the discovered devices list */ | 1933 | write_pout.pos += ret; |
1558 | if (neighbours.size < MAX_PORTS) | 1934 | if ((write_pout.pos != write_pout.size) && (0 != ret)) |
1935 | { | ||
1936 | /* We should not get partial sends with packet-oriented devices... */ | ||
1937 | fprintf (stderr, "Write error, partial send: %u/%u\n", | ||
1938 | (unsigned int) write_pout.pos, | ||
1939 | (unsigned int) write_pout.size); | ||
1940 | break; | ||
1941 | } | ||
1942 | |||
1943 | if (write_pout.pos == write_pout.size) | ||
1944 | { | ||
1945 | write_pout.pos = 0; | ||
1946 | write_pout.size = 0; | ||
1947 | } | ||
1948 | fprintf (stderr, "LOG : %s sends a message to a DEVICE\n", dev.iface); //FIXME: debugging message | ||
1949 | } | ||
1950 | } | ||
1951 | } | ||
1952 | for (i = 0; i <= maxfd; i++) | ||
1953 | { | ||
1954 | if (FD_ISSET (i, &rfds)) | ||
1955 | { | ||
1956 | if (i == STDIN_FILENO) | ||
1957 | { | ||
1958 | ssize_t ret = | ||
1959 | read (i, readbuf, sizeof (readbuf)); | ||
1960 | if (0 > ret) | ||
1559 | { | 1961 | { |
1560 | neighbours.fds[neighbours.size] = sendsocket; | 1962 | fprintf (stderr, "Read error from STDIN: %s\n", strerror (errno)); |
1561 | memcpy (&(neighbours.devices[neighbours.size++]), &addr.rc_bdaddr, sizeof (bdaddr_t)); | 1963 | break; break; |
1964 | } | ||
1965 | if (0 == ret) | ||
1966 | { | ||
1967 | /* stop reading... */ | ||
1968 | stdin_open = 0; | ||
1969 | } | ||
1970 | else | ||
1971 | { | ||
1972 | mst_receive (stdin_mst, readbuf, ret); | ||
1973 | fprintf (stderr, "LOG : %s receives a message from STDIN\n", dev.iface); //FIXME: debugging message | ||
1974 | } | ||
1975 | } | ||
1976 | else if (i == dev.fd_rfcomm) | ||
1977 | { | ||
1978 | int readsocket; | ||
1979 | struct sockaddr_rc addr = { 0 }; | ||
1980 | unsigned int opt = sizeof (addr); | ||
1981 | |||
1982 | readsocket = accept (dev.fd_rfcomm, (struct sockaddr *) &addr, &opt); | ||
1983 | fprintf(stderr, "LOG : %s accepts a message\n", dev.iface); //FIXME: debugging message | ||
1984 | if (readsocket == -1) | ||
1985 | { | ||
1986 | fprintf (stderr, "Failed to accept a connection on interface: %.*s\n", IFNAMSIZ, | ||
1987 | strerror (errno)); | ||
1988 | break; | ||
1562 | } | 1989 | } |
1563 | else | 1990 | else |
1564 | { | 1991 | { |
1565 | fprintf (stderr, "The top limit for the discovarable devices' list was reached\n"); | 1992 | FD_SET (readsocket, &rfds); |
1993 | maxfd = MAX (maxfd, readsocket); | ||
1994 | |||
1995 | if (crt_rfds < MAX_PORTS) | ||
1996 | rfds_list[crt_rfds++] = readsocket; | ||
1997 | else | ||
1998 | { | ||
1999 | fprintf (stderr, "The limit for the read file descriptors list was \ | ||
2000 | reached\n"); | ||
2001 | break; | ||
2002 | } | ||
2003 | } | ||
2004 | |||
2005 | } | ||
2006 | else | ||
2007 | { | ||
2008 | struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rrm; | ||
2009 | ssize_t ret; | ||
2010 | fprintf (stderr, "LOG : %s reads something from the socket\n", dev.iface);//FIXME : debugging message | ||
2011 | rrm = (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) write_std.buf; | ||
2012 | ret = | ||
2013 | read_from_the_socket ((void *)&i, (unsigned char *) &rrm->frame, | ||
2014 | sizeof (write_std.buf) | ||
2015 | - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) | ||
2016 | + sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame), | ||
2017 | rrm); | ||
2018 | if (0 >= ret) | ||
2019 | { | ||
2020 | int j; | ||
2021 | FD_CLR (i, &rfds); | ||
2022 | close (i); | ||
2023 | /* Remove the socket from the list */ | ||
2024 | for (j = 0; j < crt_rfds; j++) | ||
2025 | { | ||
2026 | if (rfds_list[j] == i) | ||
2027 | { | ||
2028 | rfds_list[j] ^= rfds_list[crt_rfds - 1]; | ||
2029 | rfds_list[crt_rfds - 1] ^= rfds_list[j]; | ||
2030 | rfds_list[j] ^= rfds_list[crt_rfds - 1]; | ||
2031 | crt_rfds -= 1; | ||
2032 | break; | ||
2033 | } | ||
2034 | } | ||
2035 | |||
2036 | fprintf (stderr, "Read error from raw socket: %s\n", strerror (errno)); | ||
2037 | break; | ||
2038 | } | ||
2039 | if ((0 < ret) && (0 == mac_test (&rrm->frame, &dev))) | ||
2040 | { | ||
2041 | write_std.size = ret | ||
2042 | + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) | ||
2043 | - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame); | ||
2044 | rrm->header.size = htons (write_std.size); | ||
2045 | rrm->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER); | ||
1566 | } | 2046 | } |
1567 | } | 2047 | } |
1568 | } | 2048 | } |
1569 | } | 2049 | } |
1570 | } | 2050 | } |
1571 | } | 2051 | } |
2052 | /* Error handling, try to clean up a bit at least */ | ||
2053 | mst_destroy (stdin_mst); | ||
2054 | stdin_mst = NULL; | ||
2055 | sdp_close (dev.session); | ||
2056 | (void) close (dev.fd_rfcomm); | ||
2057 | (void) close (sendsocket); | ||
2058 | |||
2059 | for (i = 0; i < crt_rfds; i++) | ||
2060 | (void) close (rfds_list[i]); | ||
2061 | |||
2062 | for (i = 0; i < neighbours.size; i++) | ||
2063 | (void) close (neighbours.fds[i]); | ||
2064 | #else | ||
2065 | struct HardwareInfos dev; | ||
2066 | struct GNUNET_NETWORK_Handle *sendsocket; | ||
2067 | struct GNUNET_NETWORK_FDSet *rfds; | ||
2068 | struct GNUNET_NETWORK_FDSet *wfds; | ||
2069 | struct GNUNET_NETWORK_Handle *rfds_list[MAX_PORTS]; | ||
2070 | char readbuf[MAXLINE] = { 0 }; | ||
2071 | SOCKADDR_BTH acc_addr = { 0 }; | ||
2072 | int addr_len = sizeof (SOCKADDR_BTH); | ||
2073 | int broadcast, i, stdin_open, crt_rfds = 0; | ||
2074 | HANDLE stdin_handle = GetStdHandle (STD_INPUT_HANDLE); | ||
2075 | HANDLE stdout_handle = GetStdHandle (STD_OUTPUT_HANDLE); | ||
2076 | struct MessageStreamTokenizer *stdin_mst; | ||
2077 | |||
2078 | /* check the handles */ | ||
2079 | if (stdin_handle == INVALID_HANDLE_VALUE) | ||
2080 | { | ||
2081 | fprintf (stderr, "Failed to get the stdin handle\n"); | ||
2082 | ExitProcess (2); | ||
2083 | } | ||
2084 | |||
2085 | if (stdout_handle == INVALID_HANDLE_VALUE) | ||
2086 | { | ||
2087 | fprintf (stderr, "Failed to get the stdout handle\n"); | ||
2088 | ExitProcess (2); | ||
2089 | } | ||
2090 | |||
2091 | /* initialize windows sockets */ | ||
2092 | initialize_windows_sockets(); | ||
1572 | 2093 | ||
1573 | if (broadcast == 0) | 2094 | // /* test bluetooth socket family support */ --> it return false because the GNUNET_NETWORK_test_pf should also receive the type of socket (BTHPROTO_RFCOMM) |
2095 | // if (GNUNET_NETWORK_test_pf (AF_BTH) != GNUNET_OK) | ||
2096 | // { | ||
2097 | // fprintf (stderr, "AF_BTH family is not supported\n"); | ||
2098 | // ExitProcess (2); | ||
2099 | // } | ||
2100 | |||
2101 | /* create the socket */ | ||
2102 | dev.handle = GNUNET_NETWORK_socket_create (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); | ||
2103 | if (dev.handle == NULL) | ||
1574 | { | 2104 | { |
1575 | /* Select a fd which is ready for action :) */ | 2105 | fprintf (stderr, "Failed to create RFCOMM socket: "); |
2106 | print_last_error(); | ||
2107 | ExitProcess (2); | ||
2108 | } | ||
2109 | |||
2110 | |||
2111 | if (open_device (&dev) == -1) | ||
2112 | { | ||
2113 | fprintf (stderr, "Failed to open the device\n"); | ||
2114 | print_last_error(); | ||
2115 | if (GNUNET_NETWORK_socket_close (dev.handle) != GNUNET_OK) | ||
1576 | { | 2116 | { |
1577 | int retval = select (maxfd + 1, &rfds, &wfds, NULL, NULL); | 2117 | fprintf (stderr, "Failed to close the socket!\n"); |
1578 | if ((-1 == retval) && (EINTR == errno)) | 2118 | print_last_error(); |
1579 | continue; | 2119 | } |
1580 | if (0 > retval && errno != EBADF) // we handle BADF errors later | 2120 | ExitProcess (2); |
1581 | { | 2121 | } |
1582 | fprintf (stderr, "select failed: %s\n", strerror (errno)); | 2122 | |
1583 | break; | 2123 | if (GNUNET_OK != GNUNET_NETWORK_socket_set_blocking (dev.handle, 1) ) |
1584 | } | 2124 | { |
2125 | fprintf (stderr, "Failed to change the socket mode\n"); | ||
2126 | ExitProcess (2); | ||
2127 | } | ||
2128 | |||
2129 | memset (&write_std, 0, sizeof (write_std)); | ||
2130 | memset (&write_pout, 0, sizeof (write_pout)); | ||
2131 | stdin_open = 1; | ||
2132 | |||
2133 | rfds = GNUNET_NETWORK_fdset_create (); | ||
2134 | wfds = GNUNET_NETWORK_fdset_create (); | ||
2135 | |||
2136 | /* Send MAC address of the bluetooth interface to STDOUT first */ | ||
2137 | { | ||
2138 | struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg; | ||
2139 | |||
2140 | macmsg.hdr.size = htons (sizeof (macmsg)); | ||
2141 | macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL); | ||
2142 | memcpy (&macmsg.mac, &dev.pl_mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress_Copy)); | ||
2143 | memcpy (write_std.buf, &macmsg, sizeof (macmsg)); | ||
2144 | write_std.size = sizeof (macmsg); | ||
2145 | } | ||
2146 | |||
2147 | |||
2148 | stdin_mst = mst_create (&stdin_send_hw, &dev); | ||
2149 | stdin_open = 1; | ||
2150 | |||
2151 | fprintf (stderr, "\n-----------------------------------------------\n Check if the program exits\n-----------------------------------------------\n"); | ||
2152 | |||
2153 | int pos = 0; | ||
2154 | int stdin_pos = -1; | ||
2155 | int stdout_pos = -1; | ||
2156 | while (1) | ||
2157 | { | ||
2158 | broadcast = 0; | ||
2159 | pos = 0; | ||
2160 | stdin_pos = -1; | ||
2161 | stdout_pos = -1; | ||
2162 | sendsocket = NULL; //FIXME memleaks | ||
2163 | fprintf (stderr, "---------------------------------------------------\n"); | ||
2164 | |||
2165 | GNUNET_NETWORK_fdset_zero (rfds); | ||
2166 | if ((0 == write_pout.size) && (1 == stdin_open)) | ||
2167 | { | ||
2168 | stdin_pos = pos; | ||
2169 | pos +=1; | ||
2170 | fprintf (stderr, "LOG: set STDIN_FILENO\n"); | ||
2171 | GNUNET_NETWORK_fdset_handle_set (rfds, (struct GNUNET_DISK_FileHandle*) &stdin_handle); //FIXME create a GNUNET_STRUCT handle for stdin | ||
1585 | } | 2172 | } |
1586 | if (FD_ISSET (STDOUT_FILENO , &wfds)) | 2173 | |
2174 | if (0 == write_std.size) | ||
1587 | { | 2175 | { |
1588 | ssize_t ret = | 2176 | pos += 1; |
1589 | write (STDOUT_FILENO, write_std.buf + write_std.pos, | 2177 | fprintf (stderr, "LOG: set the listening socket %d\n", GNUNET_NETWORK_get_fd (dev.handle)); |
1590 | write_std.size - write_std.pos); | 2178 | GNUNET_NETWORK_fdset_set (rfds, dev.handle); |
1591 | if (0 > ret) | 2179 | } |
1592 | { | 2180 | |
1593 | fprintf (stderr, "Failed to write to STDOUT: %s\n", strerror (errno)); | 2181 | for (i = 0; i < crt_rfds; i++) |
1594 | break; | ||
1595 | } | ||
1596 | write_std.pos += ret; | ||
1597 | if (write_std.pos == write_std.size) | ||
1598 | { | ||
1599 | write_std.pos = 0; | ||
1600 | write_std.size = 0; | ||
1601 | } | ||
1602 | fprintf (stderr, "LOG : %s sends a message to STDOUT\n", dev.iface); //FIXME: debugging message | ||
1603 | |||
1604 | } | ||
1605 | if (sendsocket != -1) | ||
1606 | { | 2182 | { |
1607 | if (FD_ISSET (sendsocket , &wfds)) | 2183 | pos += 1; |
2184 | fprintf (stderr, "LOG: adding %d read socket\n", i); | ||
2185 | GNUNET_NETWORK_fdset_set (rfds, rfds_list[i]); | ||
2186 | } | ||
2187 | |||
2188 | GNUNET_NETWORK_fdset_zero (wfds); | ||
2189 | if (0 < write_std.size) | ||
2190 | { | ||
2191 | stdout_pos = pos; | ||
2192 | fprintf (stderr, "LOG: set STDOUT_FILENO\n"); | ||
2193 | GNUNET_NETWORK_fdset_handle_set (wfds, (struct GNUNET_DISK_FileHandle*) &stdout_handle); | ||
2194 | fprintf (stderr, "LOG: after setting STDOUT_FILENO\n"); | ||
2195 | // printf ("%s\n", write_std.buf); | ||
2196 | // memset (write_std.buf, 0, write_std.size); | ||
2197 | // write_std.size = 0; | ||
2198 | } | ||
2199 | |||
2200 | if (0 < write_pout.size) | ||
2201 | { | ||
2202 | if (strcmp (argv[1], "ff:ff:ff:ff:ff:ff") == 0) { | ||
2203 | fprintf(stderr, "BROADCAST\n"); | ||
2204 | // skip the message | ||
2205 | broadcast = 1; | ||
2206 | memset (write_pout.buf, 0, write_pout.size); | ||
2207 | write_pout.size = 0; | ||
2208 | } | ||
2209 | else | ||
1608 | { | 2210 | { |
1609 | ssize_t ret = | 2211 | SOCKADDR_BTH addr; |
1610 | write (sendsocket, write_pout.buf + write_std.pos, | 2212 | fprintf (stderr, "LOG : has a new message for %s\n", argv[1]); |
1611 | write_pout.size - write_pout.pos); | 2213 | sendsocket = GNUNET_NETWORK_socket_create (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); |
1612 | if (0 > ret) //FIXME should I first check the error type? | 2214 | |
2215 | if (sendsocket == NULL) | ||
1613 | { | 2216 | { |
1614 | fprintf (stderr, "Failed to write to bluetooth device: %s. Closing the socket!\n", | 2217 | fprintf (stderr, "Failed to create RFCOMM socket: \n"); |
1615 | strerror (errno)); | 2218 | print_last_error(); |
1616 | for (i = 0; i < neighbours.size; i++) | 2219 | ExitProcess (2); |
1617 | { | 2220 | } |
1618 | if (neighbours.fds[i] == sendsocket) | 2221 | |
1619 | { | 2222 | memset (&addr, 0, sizeof (addr)); |
1620 | (void) close(sendsocket); | 2223 | //addr.addressFamily = AF_BTH; |
1621 | neighbours.fds[i] = -1; | 2224 | if (SOCKET_ERROR == |
1622 | break; | 2225 | WSAStringToAddress (argv[1], AF_BTH, NULL, (LPSOCKADDR) &addr, &addr_len)) |
1623 | } | 2226 | { |
1624 | } | 2227 | fprintf (stderr, "Failed to translate the address: "); |
1625 | /* Remove the message */ | 2228 | print_last_error(); |
1626 | memset (&write_pout.buf + write_std.pos, 0, (write_pout.size - write_pout.pos)); | 2229 | ExitProcess ( 2 ) ; |
1627 | write_pout.pos = 0 ; | 2230 | } |
2231 | addr.port = get_channel (argv[1]); //crapa aici | ||
2232 | if (addr.port == -1) | ||
2233 | { | ||
2234 | fprintf (stderr, "Couldn't find the sdp service for the address: %s\n", argv[1]); | ||
2235 | memset (write_pout.buf, 0, write_pout.size); | ||
1628 | write_pout.size = 0; | 2236 | write_pout.size = 0; |
2237 | broadcast = 1; //skipping the select part | ||
1629 | } | 2238 | } |
1630 | else | 2239 | else |
1631 | { | 2240 | { |
1632 | write_pout.pos += ret; | 2241 | fprintf (stderr, "got the channel %d\n", addr.port); |
1633 | if ((write_pout.pos != write_pout.size) && (0 != ret)) | 2242 | |
2243 | if (GNUNET_OK != GNUNET_NETWORK_socket_connect (sendsocket, (LPSOCKADDR)&addr, addr_len)) | ||
1634 | { | 2244 | { |
1635 | /* We should not get partial sends with packet-oriented devices... */ | 2245 | fprintf (stderr, "Failed to connect: "); |
1636 | fprintf (stderr, "Write error, partial send: %u/%u\n", | 2246 | print_last_error(); |
1637 | (unsigned int) write_pout.pos, | 2247 | ExitProcess (2); |
1638 | (unsigned int) write_pout.size); | ||
1639 | break; | ||
1640 | } | 2248 | } |
1641 | 2249 | ||
1642 | if (write_pout.pos == write_pout.size) | 2250 | if (GNUNET_OK != GNUNET_NETWORK_socket_set_blocking (sendsocket, 1) ) |
1643 | { | 2251 | { |
1644 | write_pout.pos = 0; | 2252 | fprintf (stderr, "Failed to change the socket mode\n"); |
1645 | write_pout.size = 0; | 2253 | ExitProcess (2); |
1646 | } | 2254 | } |
1647 | fprintf (stderr, "LOG : %s sends a message to a DEVICE\n", dev.iface); //FIXME: debugging message | 2255 | |
2256 | GNUNET_NETWORK_fdset_set (wfds, sendsocket); | ||
1648 | } | 2257 | } |
1649 | } | 2258 | } |
1650 | } | 2259 | } |
1651 | for (i = 0; i <= maxfd; i++) | 2260 | |
2261 | if (broadcast == 0) | ||
1652 | { | 2262 | { |
1653 | if (FD_ISSET (i, &rfds)) | 2263 | fprintf (stderr, "before select\n"); |
2264 | int retval = GNUNET_NETWORK_socket_select (rfds, wfds, NULL, GNUNET_TIME_relative_get_forever_()); | ||
2265 | fprintf (stderr, "after select %d\n", retval); | ||
2266 | if (retval < 0) | ||
2267 | { | ||
2268 | fprintf (stderr, "select error\n"); | ||
2269 | ExitProcess (2); | ||
2270 | } | ||
2271 | //if (GNUNET_NETWORK_fdset_isset (wfds, (struct GNUNET_NETWORK_Handle*)&stdout_handle)) | ||
2272 | if (retval == stdout_pos) | ||
1654 | { | 2273 | { |
1655 | if (i == STDIN_FILENO) | 2274 | fprintf (stderr, "-------------STDOUT------------\n"); |
2275 | fprintf(stderr, "LOG : sends a message to STDOUT\n"); //FIXME: debugging message | ||
2276 | //ssize_t ret; | ||
2277 | //ret = GNUNET_NETWORK_socket_send ((struct GNUNET_NETWORK_Handle *)&stdout_handle, write_std.buf + write_std.pos, write_std.size - write_std.pos); | ||
2278 | //ret = write (STDOUT_FILENO, write_std.buf + write_std.pos, write_std.size - write_std.pos); | ||
2279 | DWORD ret; | ||
2280 | if (FALSE == WriteFile (stdout_handle, write_std.buf + write_std.pos, write_std.size - write_std.pos, &ret, NULL)) | ||
1656 | { | 2281 | { |
1657 | ssize_t ret = | 2282 | fprintf (stderr, "Failed to write to STDOUT: "); |
1658 | read (i, readbuf, sizeof (readbuf)); | 2283 | print_last_error(); |
1659 | if (0 > ret) | 2284 | break; |
1660 | { | 2285 | } |
1661 | fprintf (stderr, "Read error from STDIN: %s\n", strerror (errno)); | 2286 | fprintf (stderr, "--->sended: %d\n", ret); |
1662 | break; break; | 2287 | Sleep(5000); |
1663 | } | 2288 | if (ret <= 0) |
1664 | if (0 == ret) | 2289 | { |
2290 | fprintf (stderr, "Failed to write to STDOUT\n"); | ||
2291 | ExitProcess (2); | ||
2292 | } | ||
2293 | |||
2294 | write_std.pos += ret; | ||
2295 | if (write_std.pos == write_std.size) | ||
2296 | { | ||
2297 | write_std.pos = 0; | ||
2298 | write_std.size = 0; | ||
2299 | } | ||
2300 | } | ||
2301 | if (sendsocket != NULL) | ||
2302 | { | ||
2303 | if (GNUNET_NETWORK_fdset_isset (wfds, sendsocket)) | ||
2304 | { | ||
2305 | fprintf (stderr, "-------------SEND------------\n"); | ||
2306 | ssize_t ret; | ||
2307 | ret = GNUNET_NETWORK_socket_send (sendsocket, write_pout.buf + write_pout.pos, | ||
2308 | write_pout.size - write_pout.pos); | ||
2309 | |||
2310 | if (GNUNET_SYSERR == ret) | ||
1665 | { | 2311 | { |
1666 | /* stop reading... */ | 2312 | fprintf (stderr, "Failed to send to the socket. Closing the socket. Error: \n"); |
1667 | stdin_open = 0; | 2313 | print_last_error(); |
2314 | if (GNUNET_NETWORK_socket_close (sendsocket) != GNUNET_OK) | ||
2315 | { | ||
2316 | fprintf (stderr, "Failed to close the sendsocket!\n"); | ||
2317 | print_last_error(); | ||
2318 | } | ||
2319 | ExitProcess (2); | ||
2320 | //break; | ||
1668 | } | 2321 | } |
1669 | else | 2322 | else |
1670 | { | 2323 | { |
1671 | mst_receive (stdin_mst, readbuf, ret); | 2324 | write_pout.pos += ret; |
1672 | fprintf (stderr, "LOG : %s receives a message from STDIN\n", dev.iface); //FIXME: debugging message | 2325 | if ((write_pout.pos != write_pout.size) && (0 != ret)) |
2326 | { | ||
2327 | /* we should not get partial sends with packet-oriented devices... */ | ||
2328 | fprintf (stderr, "Write error, partial send: %u/%u\n", | ||
2329 | (unsigned int) write_pout.pos, | ||
2330 | (unsigned int) write_pout.size); | ||
2331 | break; | ||
2332 | } | ||
2333 | |||
2334 | if (write_pout.pos == write_pout.size) | ||
2335 | { | ||
2336 | write_pout.pos = 0; | ||
2337 | write_pout.size = 0; | ||
2338 | |||
2339 | } | ||
2340 | fprintf(stderr, "LOG : sends a message to a DEVICE\n"); //FIXME: debugging message | ||
1673 | } | 2341 | } |
1674 | } | 2342 | } |
1675 | else if (i == dev.fd_rfcomm) | 2343 | } |
2344 | |||
2345 | //if (GNUNET_NETWORK_fdset_isset (rfds, (struct GNUNET_NETWORK_Handle*)&stdin_handle)) | ||
2346 | if (retval == stdin_pos) //FALSEEE!!!! NUUUUU EEE VOOIEE!!_----------------------------------------------------------? | ||
2347 | { | ||
2348 | fprintf (stderr, "-------------STDIN------------\n"); | ||
2349 | //ssize_t ret; | ||
2350 | //ret = GNUNET_NETWORK_socket_recv ((struct GNUNET_NETWORK_Handle *)&stdin_handle, readbuf, sizeof (write_pout.buf)); | ||
2351 | //ret = read (STDIN_FILENO, readbuf, sizeof (readbuf)); | ||
2352 | DWORD ret; | ||
2353 | if (FALSE == ReadFile (stdin_handle, readbuf, sizeof (readbuf), &ret, NULL)) /* do nothing asynchronous */ | ||
1676 | { | 2354 | { |
1677 | int readsocket; | 2355 | fprintf (stderr, "Read error from STDIN: "); |
1678 | struct sockaddr_rc addr = { 0 }; | 2356 | print_last_error(); |
1679 | unsigned int opt = sizeof (addr); | 2357 | break; |
1680 | 2358 | } | |
1681 | readsocket = accept (dev.fd_rfcomm, (struct sockaddr *) &addr, &opt); | 2359 | if (0 == ret) |
1682 | fprintf(stderr, "LOG : %s accepts a message\n", dev.iface); //FIXME: debugging message | 2360 | { |
1683 | if (readsocket == -1) | 2361 | /* stop reading... */ |
2362 | stdin_open = 0; | ||
2363 | } else { | ||
2364 | mst_receive (stdin_mst, readbuf, ret); | ||
2365 | fprintf (stderr, "LOG : receives a message from STDIN\n"); //FIXME: debugging message | ||
2366 | } | ||
2367 | } | ||
2368 | else | ||
2369 | if (GNUNET_NETWORK_fdset_isset (rfds, dev.handle)) | ||
2370 | { | ||
2371 | fprintf (stderr, "-------------ACCEPT------------\n"); | ||
2372 | fprintf (stderr, "LOG: accepting connection\n"); | ||
2373 | struct GNUNET_NETWORK_Handle *readsocket; | ||
2374 | readsocket = GNUNET_NETWORK_socket_accept (dev.handle, (LPSOCKADDR)&acc_addr, &addr_len); | ||
2375 | if (readsocket == NULL) | ||
2376 | { | ||
2377 | fprintf (stderr, "Accept error %d: ", GetLastError()); | ||
2378 | print_last_error(); | ||
2379 | ExitProcess (2); | ||
2380 | } | ||
2381 | else | ||
2382 | { | ||
2383 | if (GNUNET_OK != GNUNET_NETWORK_socket_set_blocking (readsocket, 1) ) | ||
1684 | { | 2384 | { |
1685 | fprintf (stderr, "Failed to accept a connection on interface: %.*s\n", IFNAMSIZ, | 2385 | fprintf (stderr, "Failed to change the socket mode\n"); |
1686 | strerror (errno)); | 2386 | ExitProcess (2); |
1687 | break; | ||
1688 | } | 2387 | } |
2388 | GNUNET_NETWORK_fdset_set (rfds, readsocket); | ||
2389 | |||
2390 | if (crt_rfds < MAX_PORTS) | ||
2391 | rfds_list[crt_rfds++] = readsocket; | ||
1689 | else | 2392 | else |
1690 | { | 2393 | { |
1691 | FD_SET (readsocket, &rfds); | 2394 | fprintf (stderr, "The limit for the read file descriptors list was reached\n"); |
1692 | maxfd = MAX (maxfd, readsocket); | 2395 | break; |
1693 | |||
1694 | if (crt_rfds < MAX_PORTS) | ||
1695 | rfds_list[crt_rfds++] = readsocket; | ||
1696 | else | ||
1697 | { | ||
1698 | fprintf (stderr, "The limit for the read file descriptors list was \ | ||
1699 | reached\n"); | ||
1700 | break; | ||
1701 | } | ||
1702 | } | 2396 | } |
1703 | 2397 | } | |
1704 | } | 2398 | } |
1705 | else | 2399 | else |
2400 | for (i = 0; i < crt_rfds; i++) | ||
2401 | { | ||
2402 | if (GNUNET_NETWORK_fdset_isset (rfds, rfds_list[i])) | ||
1706 | { | 2403 | { |
1707 | struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rrm; | 2404 | struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rrm; |
1708 | ssize_t ret; | 2405 | ssize_t ret; |
1709 | fprintf (stderr, "LOG : %s reads something from the socket\n", dev.iface);//FIXME : debugging message | 2406 | fprintf (stderr, "LOG: reading something from the socket\n");//FIXME : debugging message |
1710 | rrm = (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) write_std.buf; | 2407 | rrm = (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) write_std.buf; |
1711 | ret = | 2408 | ret = read_from_the_socket (rfds_list[i], (unsigned char *) &rrm->frame, |
1712 | read_from_the_socket (i, (unsigned char *) &rrm->frame, | 2409 | sizeof (write_std.buf) |
1713 | sizeof (write_std.buf) | 2410 | - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) |
1714 | - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) | 2411 | + sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame), |
1715 | + sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame), | 2412 | rrm); |
1716 | rrm); | 2413 | fprintf (stderr, "MESSAGE: %s\n", readbuf); |
1717 | if (0 >= ret) | 2414 | if (0 >= ret) |
1718 | { | 2415 | { |
1719 | int j; | 2416 | |
1720 | FD_CLR (i, &rfds); | 2417 | //TODO remove the socket from the list |
1721 | close (i); | 2418 | if (GNUNET_NETWORK_socket_close (rfds_list[i]) != GNUNET_OK) |
1722 | /* Remove the socket from the list */ | ||
1723 | for (j = 0; j < crt_rfds; j++) | ||
1724 | { | 2419 | { |
1725 | if (rfds_list[j] == i) | 2420 | fprintf (stderr, "Failed to close the sendsocket!\n"); |
1726 | { | 2421 | print_last_error(); |
1727 | rfds_list[j] ^= rfds_list[crt_rfds - 1]; | ||
1728 | rfds_list[crt_rfds - 1] ^= rfds_list[j]; | ||
1729 | rfds_list[j] ^= rfds_list[crt_rfds - 1]; | ||
1730 | crt_rfds -= 1; | ||
1731 | break; | ||
1732 | } | ||
1733 | } | 2422 | } |
1734 | 2423 | ||
1735 | fprintf (stderr, "Read error from raw socket: %s\n", strerror (errno)); | 2424 | fprintf (stderr, "Read error from raw socket: "); |
2425 | print_last_error(); | ||
1736 | break; | 2426 | break; |
2427 | |||
1737 | } | 2428 | } |
1738 | if ((0 < ret) && (0 == mac_test (&rrm->frame, &dev))) | 2429 | if ((0 < ret) && (0 == mac_test (&rrm->frame, &dev))) |
1739 | { | 2430 | { |
1740 | write_std.size = ret | 2431 | write_std.size = ret |
1741 | + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) | 2432 | + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) |
1742 | - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame); | 2433 | - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame); |
1743 | rrm->header.size = htons (write_std.size); | 2434 | rrm->header.size = htons (write_std.size); |
1744 | rrm->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER); | 2435 | rrm->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER); |
1745 | } | 2436 | } |
2437 | break; | ||
1746 | } | 2438 | } |
1747 | } | 2439 | } |
1748 | } | 2440 | } |
1749 | } | 2441 | } |
1750 | } | 2442 | |
1751 | /* Error handling, try to clean up a bit at least */ | 2443 | mst_destroy (stdin_mst); |
1752 | mst_destroy (stdin_mst); | 2444 | stdin_mst = NULL; |
1753 | stdin_mst = NULL; | ||
1754 | sdp_close (dev.session); | ||
1755 | (void) close (dev.fd_rfcomm); | ||
1756 | (void) close (sendsocket); | ||
1757 | |||
1758 | for (i = 0; i < crt_rfds; i++) | ||
1759 | (void) close (rfds_list[i]); | ||
1760 | |||
1761 | for (i = 0; i < neighbours.size; i++) | ||
1762 | (void) close (neighbours.fds[i]); | ||
1763 | |||
1764 | return 1; /* we never exit 'normally' */ | ||
1765 | } | ||
1766 | 2445 | ||
2446 | if (GNUNET_NETWORK_socket_close (dev.handle) != GNUNET_OK) | ||
2447 | { | ||
2448 | fprintf (stderr, "Failed to close the socket!\n"); | ||
2449 | print_last_error(); | ||
2450 | } | ||
1767 | 2451 | ||
2452 | for (i = 0; i < crt_rfds; i++) | ||
2453 | { | ||
2454 | if (GNUNET_NETWORK_socket_close (rfds_list[i]) != GNUNET_OK) | ||
2455 | { | ||
2456 | fprintf (stderr, "Failed to close the socket!\n"); | ||
2457 | print_last_error(); | ||
2458 | } | ||
2459 | } | ||
1768 | 2460 | ||
2461 | WSACleanup(); | ||
2462 | #endif | ||
2463 | return 1; /* we never exit 'normally' */ | ||
2464 | } \ No newline at end of file | ||
diff --git a/src/transport/plugin_transport_wlan.h b/src/transport/plugin_transport_wlan.h index b7f22de71..c6f990058 100644 --- a/src/transport/plugin_transport_wlan.h +++ b/src/transport/plugin_transport_wlan.h | |||
@@ -32,7 +32,12 @@ | |||
32 | /** | 32 | /** |
33 | * Number fo bytes in a mac address. | 33 | * Number fo bytes in a mac address. |
34 | */ | 34 | */ |
35 | #define MAC_ADDR_SIZE 6 | 35 | #ifdef MINGW |
36 | #define MAC_ADDR_SIZE 8 | ||
37 | typedef uint8_t u_int8_t; | ||
38 | #else | ||
39 | #define MAC_ADDR_SIZE 6 | ||
40 | #endif | ||
36 | 41 | ||
37 | /** | 42 | /** |
38 | * Value for "Management" in the 'frame_control' field of the | 43 | * Value for "Management" in the 'frame_control' field of the |