2007-03-22 04:28:55 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2007-03-23 01:36:56 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/ioctl.h>
|
2007-04-17 04:54:02 +08:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <net/if.h>
|
2007-03-23 01:36:56 +08:00
|
|
|
|
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
#include <bluetooth/l2cap.h>
|
|
|
|
#include <bluetooth/bnep.h>
|
|
|
|
|
|
|
|
#include "logging.h"
|
2007-03-22 04:28:55 +08:00
|
|
|
#include "common.h"
|
2007-08-30 01:50:49 +08:00
|
|
|
#include "textfile.h"
|
2007-03-23 01:36:56 +08:00
|
|
|
|
|
|
|
static int ctl;
|
|
|
|
|
2007-03-24 03:10:37 +08:00
|
|
|
#define PANU_UUID "00001115-0000-1000-8000-00805f9b34fb"
|
|
|
|
#define NAP_UUID "00001116-0000-1000-8000-00805f9b34fb"
|
|
|
|
#define GN_UUID "00001117-0000-1000-8000-00805f9b34fb"
|
|
|
|
|
2007-03-23 01:36:56 +08:00
|
|
|
static struct {
|
2007-03-24 03:10:37 +08:00
|
|
|
const char *name; /* Friendly name */
|
|
|
|
const char *uuid128; /* UUID 128 */
|
|
|
|
uint16_t id; /* Service class identifier */
|
2007-03-23 01:36:56 +08:00
|
|
|
} __svc[] = {
|
2007-03-24 05:04:08 +08:00
|
|
|
{ "panu", PANU_UUID, BNEP_SVC_PANU },
|
|
|
|
{ "gn", GN_UUID, BNEP_SVC_GN },
|
|
|
|
{ "nap", NAP_UUID, BNEP_SVC_NAP },
|
2007-03-23 01:36:56 +08:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2007-03-24 03:10:37 +08:00
|
|
|
uint16_t bnep_service_id(const char *svc)
|
2007-03-23 01:36:56 +08:00
|
|
|
{
|
|
|
|
int i;
|
2007-03-24 04:33:54 +08:00
|
|
|
uint16_t id;
|
2007-03-24 03:10:37 +08:00
|
|
|
|
|
|
|
/* Friendly service name */
|
|
|
|
for (i = 0; __svc[i].name; i++)
|
|
|
|
if (!strcasecmp(svc, __svc[i].name)) {
|
|
|
|
return __svc[i].id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* UUID 128 string */
|
|
|
|
for (i = 0; __svc[i].uuid128; i++)
|
|
|
|
if (!strcasecmp(svc, __svc[i].uuid128)) {
|
|
|
|
return __svc[i].id;
|
2007-03-23 01:36:56 +08:00
|
|
|
}
|
2007-03-24 03:10:37 +08:00
|
|
|
|
2007-03-24 04:33:54 +08:00
|
|
|
/* Try convert to HEX */
|
|
|
|
id = strtol(svc, NULL, 16);
|
|
|
|
if ((id < BNEP_SVC_PANU) || (id > BNEP_SVC_GN))
|
|
|
|
return 0;
|
2007-03-24 03:10:37 +08:00
|
|
|
|
2007-03-24 04:33:54 +08:00
|
|
|
return id;
|
2007-03-23 01:36:56 +08:00
|
|
|
}
|
|
|
|
|
2007-03-24 03:10:37 +08:00
|
|
|
const char *bnep_uuid(uint16_t id)
|
2007-03-23 01:36:56 +08:00
|
|
|
{
|
|
|
|
int i;
|
2007-03-24 03:10:37 +08:00
|
|
|
|
|
|
|
for (i = 0; __svc[i].uuid128; i++)
|
|
|
|
if (__svc[i].id == id)
|
|
|
|
return __svc[i].uuid128;
|
2007-03-23 01:36:56 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-03-24 05:04:08 +08:00
|
|
|
const char *bnep_name(uint16_t id)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; __svc[i].name; i++)
|
|
|
|
if (__svc[i].id == id)
|
|
|
|
return __svc[i].name;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-03-23 01:36:56 +08:00
|
|
|
int bnep_init(void)
|
|
|
|
{
|
|
|
|
ctl = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_BNEP);
|
2007-03-28 06:11:29 +08:00
|
|
|
|
2007-03-23 01:36:56 +08:00
|
|
|
if (ctl < 0) {
|
2007-03-29 21:33:27 +08:00
|
|
|
int err = errno;
|
|
|
|
error("Failed to open control socket: %s (%d)",
|
|
|
|
strerror(err), err);
|
|
|
|
return -err;
|
2007-03-23 01:36:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bnep_cleanup(void)
|
|
|
|
{
|
|
|
|
close(ctl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-28 06:11:29 +08:00
|
|
|
int bnep_kill_connection(bdaddr_t *dst)
|
2007-03-23 01:36:56 +08:00
|
|
|
{
|
|
|
|
struct bnep_conndel_req req;
|
|
|
|
|
2007-03-28 06:11:29 +08:00
|
|
|
baswap((bdaddr_t *)&req.dst, dst);
|
2007-03-23 01:36:56 +08:00
|
|
|
req.flags = 0;
|
2007-03-28 06:11:29 +08:00
|
|
|
if (ioctl(ctl, BNEPCONNDEL, &req)) {
|
2007-03-29 21:33:27 +08:00
|
|
|
int err = errno;
|
|
|
|
error("Failed to kill connection: %s (%d)",
|
|
|
|
strerror(err), err);
|
|
|
|
return -err;
|
2007-03-23 01:36:56 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bnep_kill_all_connections(void)
|
|
|
|
{
|
|
|
|
struct bnep_connlist_req req;
|
2007-03-29 21:33:27 +08:00
|
|
|
struct bnep_conninfo ci[7];
|
2007-04-17 04:54:02 +08:00
|
|
|
int i, err;
|
2007-03-23 01:36:56 +08:00
|
|
|
|
2007-03-29 21:33:27 +08:00
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
req.cnum = 7;
|
2007-03-23 01:36:56 +08:00
|
|
|
req.ci = ci;
|
2007-03-28 06:11:29 +08:00
|
|
|
if (ioctl(ctl, BNEPGETCONNLIST, &req)) {
|
2007-04-17 04:54:02 +08:00
|
|
|
err = errno;
|
2007-03-29 21:33:27 +08:00
|
|
|
error("Failed to get connection list: %s (%d)",
|
|
|
|
strerror(err), err);
|
|
|
|
return -err;
|
2007-03-23 01:36:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i < req.cnum; i++) {
|
|
|
|
struct bnep_conndel_req req;
|
|
|
|
memcpy(req.dst, ci[i].dst, ETH_ALEN);
|
|
|
|
req.flags = 0;
|
2007-03-29 21:33:27 +08:00
|
|
|
ioctl(ctl, BNEPCONNDEL, &req);
|
2007-03-23 01:36:56 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bnep_connadd(int sk, uint16_t role, char *dev)
|
|
|
|
{
|
|
|
|
struct bnep_connadd_req req;
|
|
|
|
|
|
|
|
strncpy(req.device, dev, 16);
|
|
|
|
req.device[15] = '\0';
|
|
|
|
req.sock = sk;
|
|
|
|
req.role = role;
|
2007-04-17 04:54:02 +08:00
|
|
|
if (ioctl(ctl, BNEPCONNADD, &req) < 0) {
|
2007-03-28 23:24:22 +08:00
|
|
|
int err = errno;
|
|
|
|
error("Failed to add device %s: %s(%d)",
|
|
|
|
dev, strerror(err), err);
|
|
|
|
return -err;
|
2007-03-23 01:36:56 +08:00
|
|
|
}
|
2007-04-17 04:54:02 +08:00
|
|
|
|
2007-03-23 01:36:56 +08:00
|
|
|
strncpy(dev, req.device, 16);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-04-17 04:54:02 +08:00
|
|
|
|
|
|
|
int bnep_if_up(const char *devname, int up)
|
|
|
|
{
|
|
|
|
int sd, err;
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
|
|
|
sd = socket(AF_INET6, SOCK_DGRAM, 0);
|
|
|
|
strcpy(ifr.ifr_name, devname);
|
|
|
|
|
|
|
|
if (up)
|
|
|
|
ifr.ifr_flags |= IFF_UP;
|
|
|
|
else
|
|
|
|
ifr.ifr_flags &= ~IFF_UP;
|
|
|
|
|
|
|
|
if((ioctl(sd, SIOCSIFFLAGS, (caddr_t) &ifr)) < 0) {
|
|
|
|
err = errno;
|
|
|
|
error("Could not bring up %d. %s(%d)", devname, strerror(err),
|
|
|
|
err);
|
|
|
|
return -err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-08-30 01:50:49 +08:00
|
|
|
|
|
|
|
int read_remote_name(bdaddr_t *src, bdaddr_t *dst, char *buf, size_t size)
|
|
|
|
{
|
|
|
|
char filename[PATH_MAX + 1], addr[18], *str;
|
|
|
|
|
|
|
|
ba2str(src, addr);
|
|
|
|
create_name(filename, PATH_MAX, STORAGEDIR, addr, "names");
|
|
|
|
|
|
|
|
ba2str(dst, addr);
|
|
|
|
str = textfile_get(filename, addr);
|
|
|
|
if (!str)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
snprintf(buf, size, "%s", str);
|
|
|
|
free(str);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|