+ }
+ }
+ }
+ }
+ /* 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 ) ;
+ }
+ addr.port = get_channel (argv[1]);
+ if (addr.port == -1)
+ {
+ fprintf (stderr, "Couldn't find the sdp service for the address: %s\n", argv[1]);
+ memset (write_pout.buf, 0, write_pout.size);