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