/*
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
}
#endif
+/**
+ * In bluez library, the maximum name length of a device is 8
+ */
+#define BLUEZ_DEVNAME_SIZE 8
+
/**
* struct for storing the information of the hardware. There is only
* one of these.
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);
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;
}
return count;
}
+
/**
* Open the bluetooth interface for reading/writing
*
* @param dev pointer to the device struct
- * @return 0 on success
+ * @return 0 on success, non-zero on error
*/
static int
open_device (struct HardwareInfos *dev)
addr.btAddr = 0;
addr.port = BT_PORT_ANY;
- if (GNUNET_NETWORK_socket_bind (dev->handle, (const SOCKADDR*)&addr, sizeof (SOCKADDR_BTH), 0) != GNUNET_OK)
+ if (GNUNET_OK !=
+ GNUNET_NETWORK_socket_bind (dev->handle, (const SOCKADDR*)&addr, sizeof (SOCKADDR_BTH)))
{
fprintf (stderr, "Failed to bind the socket: ");
if (GetLastError() == WSAENETDOWN)
if (fd_hci < 0)
{
- fprintf (stderr, "Failed to create HCI socket: %s\n", strerror (errno));
+ fprintf (stderr,
+ "Failed to create HCI socket: %s\n",
+ strerror (errno));
return -1;
}
if (ioctl (fd_hci, HCIGETDEVLIST, (void *) &request) < 0)
{
- fprintf (stderr, "ioctl(HCIGETDEVLIST) on interface `%.*s' failed: %s\n",
- IFNAMSIZ, dev->iface, strerror (errno));
+ fprintf (stderr,
+ "ioctl(HCIGETDEVLIST) on interface `%.*s' failed: %s\n",
+ IFNAMSIZ,
+ dev->iface,
+ strerror (errno));
+ (void) close (fd_hci);
return 1;
}
memset (&dev_info, 0, sizeof(struct hci_dev_info));
dev_info.dev_id = request.dev[i].dev_id;
- strncpy (dev_info.name, dev->iface, IFNAMSIZ);
+ strncpy (dev_info.name, dev->iface, BLUEZ_DEVNAME_SIZE);
if (ioctl (fd_hci, HCIGETDEVINFO, (void *) &dev_info))
{
- fprintf (stderr, "ioctl(HCIGETDEVINFO) on interface `%.*s' failed: %s\n",
- IFNAMSIZ, dev->iface, strerror (errno));
+ fprintf (stderr,
+ "ioctl(HCIGETDEVINFO) on interface `%.*s' failed: %s\n",
+ IFNAMSIZ,
+ dev->iface,
+ strerror (errno));
+ (void) close (fd_hci);
return 1;
}
- if (strcmp (dev_info.name, dev->iface) == 0)
+ if (strncmp (dev_info.name, dev->iface, BLUEZ_DEVNAME_SIZE) == 0)
{
dev_id = dev_info.dev_id; //the device was found
/* Bring the interface up */
if (ioctl (fd_hci, HCIDEVUP, dev_info.dev_id))
{
- fprintf (stderr, "ioctl(HCIDEVUP) on interface `%.*s' failed: %s\n",
- IFNAMSIZ, dev->iface, strerror (errno));
+ fprintf (stderr,
+ "ioctl(HCIDEVUP) on interface `%.*s' failed: %s\n",
+ IFNAMSIZ,
+ dev->iface,
+ strerror (errno));
+ (void) close (fd_hci);
return 1;
}
}
if (ioctl (fd_hci, HCISETSCAN, (unsigned long) &dev_req))
{
- fprintf (stderr, "ioctl(HCISETSCAN) on interface `%.*s' failed: %s\n",
- IFNAMSIZ, dev->iface, strerror (errno));
+ fprintf (stderr,
+ "ioctl(HCISETSCAN) on interface `%.*s' failed: %s\n",
+ IFNAMSIZ,
+ dev->iface,
+ strerror (errno));
+ (void) close (fd_hci);
return 1;
}
}
/* Check if the interface was not found */
- if (dev_id == -1)
+ if (-1 == dev_id)
{
- fprintf (stderr, "The interface %s was not found\n", dev->iface);
+ fprintf (stderr,
+ "The interface %s was not found\n",
+ dev->iface);
+ (void) close (fd_hci);
return 1;
}
if (bind_socket (dev->fd_rfcomm, &rc_addr) != 0)
{
- fprintf (stderr, "Failed to bind interface `%.*s': %s\n", IFNAMSIZ,
- dev->iface, strerror (errno));
+ fprintf (stderr,
+ "Failed to bind interface `%.*s': %s\n",
+ IFNAMSIZ,
+ dev->iface,
+ strerror (errno));
return 1;
}
/* Register a SDP service */
if (register_service (dev, rc_addr.rc_channel) != 0)
{
- fprintf (stderr, "Failed to register a service on interface `%.*s': %s\n", IFNAMSIZ,
+ fprintf (stderr,
+ "Failed to register a service on interface `%.*s': %s\n",
+ IFNAMSIZ,
dev->iface, strerror (errno));
return 1;
}
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);
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",
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]);