-getting ATS and transport to compile again (part of #3047)
[oweals/gnunet.git] / src / transport / gnunet-helper-transport-bluetooth.c
index f171e64fa4f829da6f61ea1cb34b416a2bce1516..43eccf0aa8383f17dab2cd2af758978472144b10 100644 (file)
@@ -71,6 +71,9 @@
   /* Maximum size of the interface's name */
   #define IFNAMSIZ 16
 
+  #ifndef NS_BTH
+    #define NS_BTH 16
+  #endif
   /**
    * A copy of the MAC Address.
    */
     UINT8 mac[MAC_ADDR_SIZE];
   };
 
-  // {31191E56-FA7E-4517-870E-71B86BBCC52F}
+  /** 
+   * The UUID used for the SDP service.
+   * {31191E56-FA7E-4517-870E-71B86BBCC52F}
+   */
   #define GNUNET_BLUETOOTH_SDP_UUID \
     { \
       0x31, 0x19, 0x1E, 0x56, \
@@ -611,12 +617,12 @@ check_crc_buf_osdep (const unsigned char *buf, size_t len)
   print_last_error()
   {
     LPVOID lpMsgBuf = NULL;
-    FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
-                    NULL, GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, NULL);
-    fprintf (stderr, "%s\n", (char *)lpMsgBuf) ;
-    //FIXME : it crashes
-    // if (lpMsgBuf)
-    //   free (lpMsgBuf);
+
+    if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+                    NULL, GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, NULL))
+      fprintf (stderr, "%s\n", (char *)lpMsgBuf);
+    else
+      fprintf (stderr, "Failed to format the message for the last error! Error number : %d\n", GetLastError());
   }
 
   /**
@@ -692,7 +698,7 @@ check_crc_buf_osdep (const unsigned char *buf, size_t len)
   static int
   register_service (struct HardwareInfos *dev) 
   {
-    // advertise the service
+    /* advertise the service */
     CSADDR_INFO addr_info;
     WSAQUERYSET wqs;
     GUID guid;
@@ -738,7 +744,7 @@ check_crc_buf_osdep (const unsigned char *buf, size_t len)
     wqs.lpServiceClassId = &guid;
     wqs.dwNumberOfCsAddrs = 1;
     wqs.lpcsaBuffer = &addr_info ;
-    wqs.lpBlob = 0; //FIXME it should be 0 for a new entry
+    wqs.lpBlob = 0;
 
     if (SOCKET_ERROR == WSASetService (&wqs , RNRSERVICE_REGISTER, 0)) 
     {
@@ -900,7 +906,6 @@ check_crc_buf_osdep (const unsigned char *buf, size_t len)
         {
         case WSAEFAULT:
           free (wqs);
-          //fprintf (stderr, "WSAFAULT error! (the memory allocated wasn't enought\n");
           wqs = (WSAQUERYSET*)malloc (wqs_len);
           break;
         case WSANO_DATA:
@@ -909,7 +914,6 @@ check_crc_buf_osdep (const unsigned char *buf, size_t len)
           break;
         case WSA_E_NO_MORE:
           done = 1;
-          // fprintf (stderr, "No matching service found\n");
           break;
         default:
           fprintf (stderr, "Failed to look over the services: ");
@@ -964,7 +968,6 @@ check_crc_buf_osdep (const unsigned char *buf, size_t len)
     {
      fprintf (stderr, "Failed to connect to the SDP server on interface `%.*s': %s\n",
               IFNAMSIZ, dev->iface, strerror (errno));
-     //FIXME exit?
      return -1;
     }
     
@@ -978,7 +981,6 @@ check_crc_buf_osdep (const unsigned char *buf, size_t len)
       for (it = response_list; it; it = it->next)
       {
         sdp_record_t *record = (sdp_record_t*) it->data;
-        //TODO print some record informations to be sure everything is good
         sdp_list_t *proto_list = 0;
         if (sdp_get_access_protos (record, &proto_list) == 0)
         {
@@ -1041,7 +1043,7 @@ read_from_the_socket (void *sock,
   }
   
   #ifdef LINUX
-   /* Get the channel used */ //FIXME probably not needed anymore
+   /* Get the channel used */
    int len;
    struct sockaddr_rc  rc_addr = { 0 }; 
 
@@ -1080,15 +1082,19 @@ open_device (struct HardwareInfos *dev)
   #ifdef MINGW
     SOCKADDR_BTH addr;
 
-    /* bind the hci socket to the interface */
+    /* bind the RFCOMM socket to the interface */
     addr.addressFamily = AF_BTH;
     addr.btAddr = 0;
     addr.port = BT_PORT_ANY;
 
-    //FIXME flags parameter
     if (GNUNET_NETWORK_socket_bind (dev->handle, (const SOCKADDR*)&addr, sizeof (SOCKADDR_BTH), 0) != GNUNET_OK)
     {
       fprintf (stderr, "Failed to bind the socket: ");
+      if (GetLastError() == WSAENETDOWN)
+      {
+        fprintf (stderr, "Please make sure that your Bluetooth device is ON!\n");
+        ExitProcess (2);
+      }
       print_last_error();
       return -1;
     }
@@ -1650,7 +1656,7 @@ main (int argc, char *argv[])
 
     if (-1 == dev.fd_rfcomm)
     {
-      fprintf (stderr, "Failed to create a HCI socket: %s\n", strerror (raw_eno));
+      fprintf (stderr, "Failed to create a RFCOMM socket: %s\n", strerror (raw_eno));
       return 1;
     }
     if (dev.fd_rfcomm >= FD_SETSIZE)
@@ -1709,11 +1715,8 @@ main (int argc, char *argv[])
     stdin_mst = mst_create (&stdin_send_hw, &dev);  
     stdin_open = 1;
     
-    fprintf (stderr, "\n-----------------------------------------------\n      Check if the program exits\n-----------------------------------------------\n");
    /**
-    * TODO : When a connection fails I should ignore only the CONTROL messages. 
-    * For DATA messages I should retry to send the message until it doesn't fail
-    * Also I should make the time out of a mac endpoint smaller and check if the rate 
+    * TODO : I should make the time out of a mac endpoint smaller and check if the rate 
     * from get_wlan_header (plugin_transport_bluetooth.c) is correct.
     */ 
    while (1)
@@ -1761,14 +1764,12 @@ main (int argc, char *argv[])
                   sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0)
         {
           fprintf (stderr, "LOG : %s has a broadcast message (pos %d, size %d)\n", dev.iface, neighbours.pos, neighbours.size); //FIXME: debugging message
-          // broadcast = 1; // IF I HAVE A BROADCAST MESSAGE I skip.
-          // memset (&write_pout, 0, sizeof (write_pout));
          
           if (send_broadcast(&dev, &sendsocket) != 0) //if the searching wasn't successful don't get stuck on the select stage
           {
             broadcast = 1;
             memset (&write_pout, 0, sizeof (write_pout)); //remove the message
-            fprintf (stderr, "LOG : Skip the broadcast message (pos %d, size %d)\n", neighbours.pos, neighbours.size);
+            fprintf (stderr, "LOG : Skipping the broadcast message (pos %d, size %d)\n", neighbours.pos, neighbours.size);
           }
           else
           {
@@ -1959,8 +1960,10 @@ main (int argc, char *argv[])
           read (i, readbuf, sizeof (readbuf));
               if (0 > ret)
               {
-                fprintf (stderr, "Read error from STDIN: %s\n", strerror (errno));
-                break; break;
+                fprintf (stderr,
+                        "Read error from STDIN: %s\n",
+                        strerror (errno));
+                break; 
               }
               if (0 == ret)
               {
@@ -2147,8 +2150,6 @@ main (int argc, char *argv[])
    
     stdin_mst = mst_create (&stdin_send_hw, &dev);  
     stdin_open = 1;
-    
-    fprintf (stderr, "\n-----------------------------------------------\n      Check if the program exits\n-----------------------------------------------\n");
 
     int pos = 0;
     int stdin_pos = -1;
@@ -2159,29 +2160,25 @@ main (int argc, char *argv[])
       pos = 0;
       stdin_pos = -1;
       stdout_pos = -1;
-      sendsocket = NULL; //FIXME memleaks
-      fprintf (stderr, "---------------------------------------------------\n");
+      sendsocket = NULL; //FIXME ???memleaks
       
       GNUNET_NETWORK_fdset_zero (rfds);    
       if ((0 == write_pout.size) && (1 == stdin_open))
       {
         stdin_pos = pos;
         pos +=1;
-        fprintf (stderr, "LOG: set STDIN_FILENO\n");
-        GNUNET_NETWORK_fdset_handle_set (rfds, (struct GNUNET_DISK_FileHandle*) &stdin_handle); //FIXME create a GNUNET_STRUCT handle for stdin
+        GNUNET_NETWORK_fdset_handle_set (rfds, (struct GNUNET_DISK_FileHandle*) &stdin_handle); 
       }
 
       if (0 == write_std.size)
       {
         pos += 1;
-        fprintf (stderr, "LOG: set the listening socket %d\n", GNUNET_NETWORK_get_fd (dev.handle));
         GNUNET_NETWORK_fdset_set (rfds, dev.handle);
       }
       
       for (i = 0; i < crt_rfds; i++)
       {
         pos += 1;
-        fprintf (stderr, "LOG: adding %d read socket\n", i);
         GNUNET_NETWORK_fdset_set (rfds, rfds_list[i]);
       }
 
@@ -2189,9 +2186,7 @@ main (int argc, char *argv[])
       if (0 < write_std.size)
       {
         stdout_pos = pos;
-        fprintf (stderr, "LOG: set STDOUT_FILENO\n");
         GNUNET_NETWORK_fdset_handle_set (wfds, (struct GNUNET_DISK_FileHandle*) &stdout_handle);
-        fprintf (stderr, "LOG: after setting STDOUT_FILENO\n");
         // printf ("%s\n", write_std.buf);
         // memset (write_std.buf, 0, write_std.size);
         // write_std.size = 0; 
@@ -2200,7 +2195,7 @@ main (int argc, char *argv[])
       if (0 < write_pout.size)
       {   
         if (strcmp (argv[1], "ff:ff:ff:ff:ff:ff") == 0) {
-          fprintf(stderr, "BROADCAST\n");
+          fprintf(stderr, "LOG: BROADCAST! Skipping the message\n");
           // skip the message
           broadcast = 1;
           memset (write_pout.buf, 0, write_pout.size);
@@ -2228,7 +2223,7 @@ main (int argc, char *argv[])
             print_last_error();
             ExitProcess ( 2 ) ;
           }
-          addr.port = get_channel (argv[1]); //crapa aici
+          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]);
@@ -2238,8 +2233,6 @@ main (int argc, char *argv[])
           }
           else
           {
-            fprintf (stderr, "got the channel %d\n", addr.port);
-
             if (GNUNET_OK != GNUNET_NETWORK_socket_connect (sendsocket, (LPSOCKADDR)&addr, addr_len))
             {
               fprintf (stderr, "Failed to connect: ");
@@ -2260,18 +2253,15 @@ main (int argc, char *argv[])
      
       if (broadcast == 0)
       {
-        fprintf (stderr, "before select\n");
         int retval = GNUNET_NETWORK_socket_select (rfds, wfds, NULL, GNUNET_TIME_relative_get_forever_());
-        fprintf (stderr, "after select %d\n", retval);
         if (retval < 0)
         {
-          fprintf (stderr, "select error\n");
+          fprintf (stderr, "Select error\n");
           ExitProcess (2);
         }
         //if (GNUNET_NETWORK_fdset_isset (wfds, (struct GNUNET_NETWORK_Handle*)&stdout_handle))
         if (retval == stdout_pos)
         {
-          fprintf (stderr, "-------------STDOUT------------\n");
           fprintf(stderr, "LOG : sends a message to STDOUT\n"); //FIXME: debugging message
           //ssize_t ret;
           //ret = GNUNET_NETWORK_socket_send ((struct GNUNET_NETWORK_Handle *)&stdout_handle,  write_std.buf + write_std.pos, write_std.size - write_std.pos);
@@ -2283,8 +2273,7 @@ main (int argc, char *argv[])
             print_last_error();
             break;
           }
-          fprintf (stderr, "--->sended: %d\n", ret);
-          Sleep(5000);
+
           if (ret <= 0)
           {
             fprintf (stderr, "Failed to write to STDOUT\n");
@@ -2302,7 +2291,6 @@ main (int argc, char *argv[])
         {
           if (GNUNET_NETWORK_fdset_isset (wfds, sendsocket))
           {
-            fprintf (stderr, "-------------SEND------------\n");
             ssize_t ret;
             ret = GNUNET_NETWORK_socket_send (sendsocket, write_pout.buf + write_pout.pos, 
                  write_pout.size - write_pout.pos);
@@ -2317,7 +2305,6 @@ main (int argc, char *argv[])
                 print_last_error();
               }
               ExitProcess (2);
-              //break;
             }
             else
             {
@@ -2343,9 +2330,8 @@ main (int argc, char *argv[])
         }
         
         //if (GNUNET_NETWORK_fdset_isset (rfds, (struct GNUNET_NETWORK_Handle*)&stdin_handle))
-        if (retval == stdin_pos) //FALSEEE!!!! NUUUUU EEE VOOIEE!!_----------------------------------------------------------?
+        if (retval == stdin_pos)
         {
-          fprintf (stderr, "-------------STDIN------------\n");
           //ssize_t ret; 
           //ret = GNUNET_NETWORK_socket_recv ((struct GNUNET_NETWORK_Handle *)&stdin_handle, readbuf, sizeof (write_pout.buf));
           //ret = read (STDIN_FILENO, readbuf, sizeof (readbuf));
@@ -2368,7 +2354,6 @@ main (int argc, char *argv[])
         else
         if (GNUNET_NETWORK_fdset_isset (rfds, dev.handle))
         {
-          fprintf (stderr, "-------------ACCEPT------------\n");
           fprintf (stderr, "LOG: accepting connection\n");
           struct GNUNET_NETWORK_Handle *readsocket;
           readsocket = GNUNET_NETWORK_socket_accept (dev.handle, (LPSOCKADDR)&acc_addr, &addr_len);  
@@ -2410,7 +2395,6 @@ main (int argc, char *argv[])
                   - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage) 
                   + sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame), 
                   rrm);
-            fprintf (stderr, "MESSAGE: %s\n", readbuf);
             if (0 >= ret)
             {
 
@@ -2461,4 +2445,4 @@ main (int argc, char *argv[])
     WSACleanup();
   #endif
   return 1;                     /* we never exit 'normally' */
-}
\ No newline at end of file
+}