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