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