Avoid trying to disconnect a neighbour twice
[oweals/gnunet.git] / src / transport / gnunet-helper-transport-bluetooth.c
index af460c832b49fbe62f327c360d04186ca3b94b8a..2b1dc532409e43da27bf431820f8926311549235 100644 (file)
@@ -1,8 +1,8 @@
 /*
    This file is part of GNUnet.
-   (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
+   Copyright (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
    Copyright (c) 2007, 2008, Andy Green <andy@warmcat.com>
-   Copyright (C) 2009 Thomas d'Otreppe
+   Copyright Copyright (C) 2009 Thomas d'Otreppe
 
    GNUnet is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
@@ -965,7 +965,7 @@ check_crc_buf_osdep (const unsigned char *buf, size_t len)
     sdp_list_t *search_list = 0, *attrid_list = 0, *response_list = 0, *it = 0;
     uuid_t svc_uuid;
     uint32_t range = 0x0000ffff;
-    uint8_t channel = -1;
+    int channel = -1;
 
     /* Connect to the local SDP server */
     session = sdp_connect (BDADDR_ANY, &dest, 0);
@@ -1002,9 +1002,12 @@ check_crc_buf_osdep (const unsigned char *buf, size_t len)
 
     sdp_close (session);
 
-    if (channel == -1)
-      fprintf (stderr, "Failed to find the listening channel for interface `%.*s': %s\n",
-              IFNAMSIZ, dev->iface, strerror (errno));
+    if (-1 == channel)
+      fprintf (stderr,
+               "Failed to find the listening channel for interface `%.*s': %s\n",
+               IFNAMSIZ,
+               dev->iface,
+               strerror (errno));
 
     return channel;
   }
@@ -1538,22 +1541,33 @@ stdin_send_hw (void *cls, const struct GNUNET_MessageHeader *hdr)
         addr_rc.rc_channel = get_channel (dev, addr_rc.rc_bdaddr);
 
         *sendsocket = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
-        if (connect (*sendsocket, (struct sockaddr *)&addr_rc, sizeof (addr_rc)) == 0)
+        if ( (-1 < *sendsocket) &&
+             (0 == connect (*sendsocket,
+                            (struct sockaddr *) &addr_rc,
+                            sizeof (addr_rc))) )
         {
           neighbours.fds[neighbours.pos++] = *sendsocket;
           connection_successful = 1;
           char addr[19] = { 0 };
           ba2str (&(neighbours.devices[neighbours.pos - 1]), addr);
           fprintf (stderr, "LOG : Connected to %s\n", addr);
-
           break;
         }
         else
         {
           char addr[19] = { 0 };
           errno_copy = errno;  //Save a copy for later
+
+          if (-1 != *sendsocket)
+          {
+            (void) close (*sendsocket);
+            *sendsocket = -1;
+          }
           ba2str (&(neighbours.devices[neighbours.pos]), addr);
-          fprintf (stderr, "LOG : Couldn't connect on device %s, error : %s\n", addr, strerror(errno));
+          fprintf (stderr,
+                   "LOG : Couldn't connect on device %s, error : %s\n",
+                   addr,
+                   strerror (errno));
           if (errno != ECONNREFUSED) //FIXME be sure that this works
           {
             fprintf (stderr, "LOG : Removes %d device from the list\n", neighbours.pos);
@@ -1937,13 +1951,13 @@ main (int argc, char *argv[])
           fprintf (stderr, "LOG : %s sends a message to STDOUT\n", dev.iface); //FIXME: debugging message
 
         }
-        if (sendsocket != -1)
+        if (-1 != sendsocket)
         {
           if (FD_ISSET (sendsocket , &wfds))
           {
-            ssize_t ret =
-        write (sendsocket, write_pout.buf + write_std.pos,
-               write_pout.size - write_pout.pos);
+            ssize_t ret = write (sendsocket,
+                                 write_pout.buf + write_std.pos,
+                                 write_pout.size - write_pout.pos);
             if (0 > ret) //FIXME should I first check the error type?
             {
               fprintf (stderr, "Failed to write to bluetooth device: %s. Closing the socket!\n",
@@ -2090,7 +2104,8 @@ main (int argc, char *argv[])
     stdin_mst = NULL;
     sdp_close (dev.session);
     (void) close (dev.fd_rfcomm);
-    (void) close (sendsocket);
+    if (-1 != sendsocket)
+      (void) close (sendsocket);
 
     for (i = 0; i < crt_rfds; i++)
       (void) close (rfds_list[i]);