+ if (i == STDIN_FILENO)
+ {
+ ssize_t ret =
+ read (i, readbuf, sizeof (readbuf));
+ if (0 > ret)
+ {
+ fprintf (stderr,
+ "Read error from STDIN: %s\n",
+ strerror (errno));
+ break;
+ }
+ if (0 == ret)
+ {
+ /* stop reading... */
+ stdin_open = 0;
+ }
+ else
+ {
+ mst_receive (stdin_mst, readbuf, ret);
+ fprintf (stderr, "LOG : %s receives a message from STDIN\n", dev.iface); //FIXME: debugging message
+ }
+ }
+ else if (i == dev.fd_rfcomm)
+ {
+ int readsocket;
+ struct sockaddr_rc addr = { 0 };
+ unsigned int opt = sizeof (addr);
+
+ readsocket = accept (dev.fd_rfcomm, (struct sockaddr *) &addr, &opt);
+ fprintf(stderr, "LOG : %s accepts a message\n", dev.iface); //FIXME: debugging message
+ if (readsocket == -1)
+ {
+ fprintf (stderr, "Failed to accept a connection on interface: %.*s\n", IFNAMSIZ,
+ strerror (errno));
+ break;
+ }
+ else
+ {
+ FD_SET (readsocket, &rfds);
+ maxfd = MAX (maxfd, readsocket);
+
+ if (crt_rfds < MAX_PORTS)
+ rfds_list[crt_rfds++] = readsocket;
+ else
+ {
+ fprintf (stderr, "The limit for the read file descriptors list was \
+ reached\n");
+ break;
+ }
+ }
+
+ }
+ else
+ {
+ struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rrm;
+ ssize_t ret;
+ fprintf (stderr, "LOG : %s reads something from the socket\n", dev.iface);//FIXME : debugging message
+ rrm = (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) write_std.buf;
+ ret =
+ read_from_the_socket ((void *)&i, (unsigned char *) &rrm->frame,
+ sizeof (write_std.buf)
+ - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)
+ + sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame),
+ rrm);
+ if (0 >= ret)
+ {
+ int j;
+ FD_CLR (i, &rfds);
+ close (i);
+ /* Remove the socket from the list */
+ for (j = 0; j < crt_rfds; j++)
+ {
+ if (rfds_list[j] == i)
+ {
+ rfds_list[j] ^= rfds_list[crt_rfds - 1];
+ rfds_list[crt_rfds - 1] ^= rfds_list[j];
+ rfds_list[j] ^= rfds_list[crt_rfds - 1];
+ crt_rfds -= 1;
+ break;
+ }
+ }
+
+ fprintf (stderr, "Read error from raw socket: %s\n", strerror (errno));
+ break;
+ }
+ if ((0 < ret) && (0 == mac_test (&rrm->frame, &dev)))
+ {
+ write_std.size = ret
+ + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)
+ - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame);
+ rrm->header.size = htons (write_std.size);
+ rrm->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER);
+ }
+ }
+ }
+ }
+ }
+ }
+ /* Error handling, try to clean up a bit at least */
+ mst_destroy (stdin_mst);
+ stdin_mst = NULL;
+ sdp_close (dev.session);
+ (void) close (dev.fd_rfcomm);
+ if (-1 != sendsocket)
+ (void) close (sendsocket);
+
+ for (i = 0; i < crt_rfds; i++)
+ (void) close (rfds_list[i]);
+
+ for (i = 0; i < neighbours.size; i++)
+ (void) close (neighbours.fds[i]);
+ #else
+ struct HardwareInfos dev;
+ struct GNUNET_NETWORK_Handle *sendsocket;
+ struct GNUNET_NETWORK_FDSet *rfds;
+ struct GNUNET_NETWORK_FDSet *wfds;
+ struct GNUNET_NETWORK_Handle *rfds_list[MAX_PORTS];
+ char readbuf[MAXLINE] = { 0 };
+ SOCKADDR_BTH acc_addr = { 0 };
+ int addr_len = sizeof (SOCKADDR_BTH);
+ int broadcast, i, stdin_open, crt_rfds = 0;
+ HANDLE stdin_handle = GetStdHandle (STD_INPUT_HANDLE);
+ HANDLE stdout_handle = GetStdHandle (STD_OUTPUT_HANDLE);
+ struct MessageStreamTokenizer *stdin_mst;
+
+ /* check the handles */
+ if (stdin_handle == INVALID_HANDLE_VALUE)
+ {
+ fprintf (stderr, "Failed to get the stdin handle\n");
+ ExitProcess (2);
+ }
+
+ if (stdout_handle == INVALID_HANDLE_VALUE)
+ {
+ fprintf (stderr, "Failed to get the stdout handle\n");
+ ExitProcess (2);
+ }
+
+ /* initialize windows sockets */
+ initialize_windows_sockets();
+
+ // /* test bluetooth socket family support */ --> it return false because the GNUNET_NETWORK_test_pf should also receive the type of socket (BTHPROTO_RFCOMM)
+ // if (GNUNET_NETWORK_test_pf (AF_BTH) != GNUNET_OK)
+ // {
+ // fprintf (stderr, "AF_BTH family is not supported\n");
+ // ExitProcess (2);
+ // }
+
+ /* create the socket */
+ dev.handle = GNUNET_NETWORK_socket_create (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
+ if (dev.handle == NULL)
+ {
+ fprintf (stderr, "Failed to create RFCOMM socket: ");
+ print_last_error();
+ ExitProcess (2);
+ }
+
+
+ if (open_device (&dev) == -1)
+ {
+ fprintf (stderr, "Failed to open the device\n");
+ print_last_error();
+ if (GNUNET_NETWORK_socket_close (dev.handle) != GNUNET_OK)
+ {
+ fprintf (stderr, "Failed to close the socket!\n");
+ print_last_error();
+ }
+ ExitProcess (2);
+ }
+
+ if (GNUNET_OK != GNUNET_NETWORK_socket_set_blocking (dev.handle, 1) )
+ {
+ fprintf (stderr, "Failed to change the socket mode\n");
+ ExitProcess (2);
+ }
+
+ memset (&write_std, 0, sizeof (write_std));
+ memset (&write_pout, 0, sizeof (write_pout));
+ stdin_open = 1;
+
+ rfds = GNUNET_NETWORK_fdset_create ();
+ wfds = GNUNET_NETWORK_fdset_create ();
+
+ /* Send MAC address of the bluetooth interface to STDOUT first */
+ {
+ struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg;
+
+ macmsg.hdr.size = htons (sizeof (macmsg));
+ macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
+ GNUNET_memcpy (&macmsg.mac, &dev.pl_mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress_Copy));
+ GNUNET_memcpy (write_std.buf, &macmsg, sizeof (macmsg));
+ write_std.size = sizeof (macmsg);
+ }
+
+
+ stdin_mst = mst_create (&stdin_send_hw, &dev);
+ stdin_open = 1;
+
+ int pos = 0;
+ int stdin_pos = -1;
+ int stdout_pos = -1;
+ while (1)
+ {
+ broadcast = 0;
+ pos = 0;
+ stdin_pos = -1;
+ stdout_pos = -1;
+ sendsocket = NULL; //FIXME ???memleaks
+
+ GNUNET_NETWORK_fdset_zero (rfds);
+ if ((0 == write_pout.size) && (1 == stdin_open))
+ {
+ stdin_pos = pos;
+ pos +=1;
+ GNUNET_NETWORK_fdset_handle_set (rfds, (struct GNUNET_DISK_FileHandle*) &stdin_handle);
+ }
+
+ if (0 == write_std.size)
+ {
+ pos += 1;
+ GNUNET_NETWORK_fdset_set (rfds, dev.handle);
+ }
+
+ for (i = 0; i < crt_rfds; i++)
+ {
+ pos += 1;
+ GNUNET_NETWORK_fdset_set (rfds, rfds_list[i]);
+ }
+
+ GNUNET_NETWORK_fdset_zero (wfds);
+ if (0 < write_std.size)
+ {
+ stdout_pos = pos;
+ GNUNET_NETWORK_fdset_handle_set (wfds, (struct GNUNET_DISK_FileHandle*) &stdout_handle);
+ // printf ("%s\n", write_std.buf);
+ // memset (write_std.buf, 0, write_std.size);
+ // write_std.size = 0;
+ }
+
+ if (0 < write_pout.size)
+ {
+ if (strcmp (argv[1], "ff:ff:ff:ff:ff:ff") == 0) {
+ fprintf(stderr, "LOG: BROADCAST! Skipping the message\n");
+ // skip the message
+ broadcast = 1;
+ memset (write_pout.buf, 0, write_pout.size);
+ write_pout.size = 0;
+ }
+ else
+ {
+ SOCKADDR_BTH addr;
+ fprintf (stderr, "LOG : has a new message for %s\n", argv[1]);
+ sendsocket = GNUNET_NETWORK_socket_create (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
+
+ if (sendsocket == NULL)
+ {
+ fprintf (stderr, "Failed to create RFCOMM socket: \n");
+ print_last_error();
+ ExitProcess (2);
+ }
+
+ memset (&addr, 0, sizeof (addr));
+ //addr.addressFamily = AF_BTH;
+ if (SOCKET_ERROR ==
+ WSAStringToAddress (argv[1], AF_BTH, NULL, (LPSOCKADDR) &addr, &addr_len))
+ {
+ fprintf (stderr, "Failed to translate the address: ");
+ print_last_error();
+ ExitProcess ( 2 ) ;