summaryrefslogtreecommitdiff
path: root/network
diff options
context:
space:
mode:
authorAdrian Bunk <adrian.bunk@movial.com>2011-06-03 09:17:04 +0000
committerAdrian Bunk <adrian.bunk@movial.com>2011-06-03 09:17:04 +0000
commit799757ccf1d03c33c75bc597cd5ef77741dcb6a7 (patch)
treea8c3be85c730de28b012586591b76301033d3d21 /network
Imported upstream 4.91upstream-4.91upstreampackaging
Diffstat (limited to 'network')
-rw-r--r--network/common.c262
-rw-r--r--network/common.h42
-rw-r--r--network/connection.c622
-rw-r--r--network/connection.h28
-rw-r--r--network/main.c59
-rw-r--r--network/manager.c222
-rw-r--r--network/manager.h25
-rw-r--r--network/network.conf6
-rw-r--r--network/server.c809
-rw-r--r--network/server.h29
10 files changed, 2104 insertions, 0 deletions
diff --git a/network/common.c b/network/common.c
new file mode 100644
index 0000000..ef72679
--- /dev/null
+++ b/network/common.c
@@ -0,0 +1,262 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2010 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
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/param.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+#include <net/if.h>
+#include <linux/sockios.h>
+
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/l2cap.h>
+#include <bluetooth/bnep.h>
+
+#include <glib.h>
+
+#include "log.h"
+#include "common.h"
+
+static int ctl;
+
+static struct {
+ const char *name; /* Friendly name */
+ const char *uuid128; /* UUID 128 */
+ uint16_t id; /* Service class identifier */
+} __svc[] = {
+ { "panu", PANU_UUID, BNEP_SVC_PANU },
+ { "gn", GN_UUID, BNEP_SVC_GN },
+ { "nap", NAP_UUID, BNEP_SVC_NAP },
+ { NULL }
+};
+
+uint16_t bnep_service_id(const char *svc)
+{
+ int i;
+ uint16_t id;
+
+ /* 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;
+ }
+
+ /* Try convert to HEX */
+ id = strtol(svc, NULL, 16);
+ if ((id < BNEP_SVC_PANU) || (id > BNEP_SVC_GN))
+ return 0;
+
+ return id;
+}
+
+const char *bnep_uuid(uint16_t id)
+{
+ int i;
+
+ for (i = 0; __svc[i].uuid128; i++)
+ if (__svc[i].id == id)
+ return __svc[i].uuid128;
+ return NULL;
+}
+
+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;
+}
+
+int bnep_init(void)
+{
+ ctl = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_BNEP);
+
+ if (ctl < 0) {
+ int err = errno;
+ error("Failed to open control socket: %s (%d)",
+ strerror(err), err);
+ return -err;
+ }
+
+ return 0;
+}
+
+int bnep_cleanup(void)
+{
+ close(ctl);
+ return 0;
+}
+
+int bnep_kill_connection(bdaddr_t *dst)
+{
+ struct bnep_conndel_req req;
+
+ memset(&req, 0, sizeof(req));
+ baswap((bdaddr_t *)&req.dst, dst);
+ req.flags = 0;
+ if (ioctl(ctl, BNEPCONNDEL, &req)) {
+ int err = errno;
+ error("Failed to kill connection: %s (%d)",
+ strerror(err), err);
+ return -err;
+ }
+ return 0;
+}
+
+int bnep_kill_all_connections(void)
+{
+ struct bnep_connlist_req req;
+ struct bnep_conninfo ci[7];
+ unsigned int i;
+ int err;
+
+ memset(&req, 0, sizeof(req));
+ req.cnum = 7;
+ req.ci = ci;
+ if (ioctl(ctl, BNEPGETCONNLIST, &req)) {
+ err = errno;
+ error("Failed to get connection list: %s (%d)",
+ strerror(err), err);
+ return -err;
+ }
+
+ for (i = 0; i < req.cnum; i++) {
+ struct bnep_conndel_req del;
+
+ memset(&del, 0, sizeof(del));
+ memcpy(del.dst, ci[i].dst, ETH_ALEN);
+ del.flags = 0;
+ ioctl(ctl, BNEPCONNDEL, &del);
+ }
+ return 0;
+}
+
+int bnep_connadd(int sk, uint16_t role, char *dev)
+{
+ struct bnep_connadd_req req;
+
+ memset(&req, 0, sizeof(req));
+ strncpy(req.device, dev, 16);
+ req.device[15] = '\0';
+ req.sock = sk;
+ req.role = role;
+ if (ioctl(ctl, BNEPCONNADD, &req) < 0) {
+ int err = errno;
+ error("Failed to add device %s: %s(%d)",
+ dev, strerror(err), err);
+ return -err;
+ }
+
+ strncpy(dev, req.device, 16);
+ return 0;
+}
+
+int bnep_if_up(const char *devname)
+{
+ struct ifreq ifr;
+ int sk, err;
+
+ sk = socket(AF_INET, SOCK_DGRAM, 0);
+
+ memset(&ifr, 0, sizeof(ifr));
+ strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1);
+
+ ifr.ifr_flags |= IFF_UP;
+ ifr.ifr_flags |= IFF_MULTICAST;
+
+ err = ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr);
+
+ close(sk);
+
+ if (err < 0) {
+ error("Could not bring up %s", devname);
+ return err;
+ }
+
+ return 0;
+}
+
+int bnep_if_down(const char *devname)
+{
+ struct ifreq ifr;
+ int sk, err;
+
+ sk = socket(AF_INET, SOCK_DGRAM, 0);
+
+ memset(&ifr, 0, sizeof(ifr));
+ strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1);
+
+ ifr.ifr_flags &= ~IFF_UP;
+
+ /* Bring down the interface */
+ err = ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr);
+
+ close(sk);
+
+ return 0;
+}
+
+int bnep_add_to_bridge(const char *devname, const char *bridge)
+{
+ int ifindex = if_nametoindex(devname);
+ struct ifreq ifr;
+ int sk, err;
+
+ if (!devname || !bridge)
+ return -EINVAL;
+
+ sk = socket(AF_INET, SOCK_STREAM, 0);
+ if (sk < 0)
+ return -1;
+
+ memset(&ifr, 0, sizeof(ifr));
+ strncpy(ifr.ifr_name, bridge, IFNAMSIZ - 1);
+ ifr.ifr_ifindex = ifindex;
+
+ err = ioctl(sk, SIOCBRADDIF, &ifr);
+
+ close(sk);
+
+ if (err < 0)
+ return err;
+
+ info("bridge %s: interface %s added", bridge, devname);
+
+ return 0;
+}
diff --git a/network/common.h b/network/common.h
new file mode 100644
index 0000000..fefb754
--- /dev/null
+++ b/network/common.h
@@ -0,0 +1,42 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2010 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
+ *
+ */
+
+#define PANU_UUID "00001115-0000-1000-8000-00805f9b34fb"
+#define NAP_UUID "00001116-0000-1000-8000-00805f9b34fb"
+#define GN_UUID "00001117-0000-1000-8000-00805f9b34fb"
+#define BNEP_SVC_UUID "0000000f-0000-1000-8000-00805f9b34fb"
+
+int bnep_init(void);
+int bnep_cleanup(void);
+
+uint16_t bnep_service_id(const char *svc);
+const char *bnep_uuid(uint16_t id);
+const char *bnep_name(uint16_t id);
+
+int bnep_kill_connection(bdaddr_t *dst);
+int bnep_kill_all_connections(void);
+
+int bnep_connadd(int sk, uint16_t role, char *dev);
+int bnep_if_up(const char *devname);
+int bnep_if_down(const char *devname);
+int bnep_add_to_bridge(const char *devname, const char *bridge);
diff --git a/network/connection.c b/network/connection.c
new file mode 100644
index 0000000..f4dd74e
--- /dev/null
+++ b/network/connection.c
@@ -0,0 +1,622 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2010 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
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <netinet/in.h>
+
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/bnep.h>
+#include <bluetooth/sdp.h>
+
+#include <glib.h>
+#include <gdbus.h>
+
+#include "log.h"
+#include "glib-helper.h"
+#include "btio.h"
+#include "dbus-common.h"
+#include "adapter.h"
+#include "device.h"
+
+#include "error.h"
+#include "common.h"
+#include "connection.h"
+
+#define NETWORK_PEER_INTERFACE "org.bluez.Network"
+
+typedef enum {
+ CONNECTED,
+ CONNECTING,
+ DISCONNECTED
+} conn_state;
+
+struct network_peer {
+ bdaddr_t src;
+ bdaddr_t dst;
+ char *path; /* D-Bus path */
+ struct btd_device *device;
+ GSList *connections;
+};
+
+struct network_conn {
+ DBusMessage *msg;
+ char dev[16]; /* Interface name */
+ uint16_t id; /* Role: Service Class Identifier */
+ conn_state state;
+ GIOChannel *io;
+ guint watch; /* Disconnect watch */
+ guint dc_id;
+ struct network_peer *peer;
+};
+
+struct __service_16 {
+ uint16_t dst;
+ uint16_t src;
+} __attribute__ ((packed));
+
+static DBusConnection *connection = NULL;
+static GSList *peers = NULL;
+
+static struct network_peer *find_peer(GSList *list, const char *path)
+{
+ GSList *l;
+
+ for (l = list; l; l = l->next) {
+ struct network_peer *peer = l->data;
+
+ if (!strcmp(peer->path, path))
+ return peer;
+ }
+
+ return NULL;
+}
+
+static struct network_conn *find_connection(GSList *list, uint16_t id)
+{
+ GSList *l;
+
+ for (l = list; l; l = l->next) {
+ struct network_conn *nc = l->data;
+
+ if (nc->id == id)
+ return nc;
+ }
+
+ return NULL;
+}
+
+static gboolean bnep_watchdog_cb(GIOChannel *chan, GIOCondition cond,
+ gpointer data)
+{
+ struct network_conn *nc = data;
+
+ if (connection != NULL) {
+ gboolean connected = FALSE;
+ const char *property = "";
+ emit_property_changed(connection, nc->peer->path,
+ NETWORK_PEER_INTERFACE, "Connected",
+ DBUS_TYPE_BOOLEAN, &connected);
+ emit_property_changed(connection, nc->peer->path,
+ NETWORK_PEER_INTERFACE, "Interface",
+ DBUS_TYPE_STRING, &property);
+ emit_property_changed(connection, nc->peer->path,
+ NETWORK_PEER_INTERFACE, "UUID",
+ DBUS_TYPE_STRING, &property);
+ device_remove_disconnect_watch(nc->peer->device, nc->dc_id);
+ nc->dc_id = 0;
+ if (nc->watch) {
+ g_dbus_remove_watch(connection, nc->watch);
+ nc->watch = 0;
+ }
+ }
+
+ info("%s disconnected", nc->dev);
+
+ bnep_if_down(nc->dev);
+ nc->state = DISCONNECTED;
+ memset(nc->dev, 0, sizeof(nc->dev));
+ strcpy(nc->dev, "bnep%d");
+
+ return FALSE;
+}
+
+static void cancel_connection(struct network_conn *nc, const char *err_msg)
+{
+ DBusMessage *reply;
+
+ if (nc->watch) {
+ g_dbus_remove_watch(connection, nc->watch);
+ nc->watch = 0;
+ }
+
+ if (nc->msg && err_msg) {
+ reply = btd_error_failed(nc->msg, err_msg);
+ g_dbus_send_message(connection, reply);
+ }
+
+ g_io_channel_shutdown(nc->io, TRUE, NULL);
+ g_io_channel_unref(nc->io);
+ nc->io = NULL;
+
+ nc->state = DISCONNECTED;
+}
+
+static void connection_destroy(DBusConnection *conn, void *user_data)
+{
+ struct network_conn *nc = user_data;
+
+ if (nc->state == CONNECTED) {
+ bnep_if_down(nc->dev);
+ bnep_kill_connection(&nc->peer->dst);
+ } else if (nc->io)
+ cancel_connection(nc, NULL);
+}
+
+static void disconnect_cb(struct btd_device *device, gboolean removal,
+ void *user_data)
+{
+ struct network_conn *nc = user_data;
+
+ info("Network: disconnect %s", nc->peer->path);
+
+ connection_destroy(NULL, user_data);
+}
+
+static gboolean bnep_setup_cb(GIOChannel *chan, GIOCondition cond,
+ gpointer data)
+{
+ struct network_conn *nc = data;
+ struct bnep_control_rsp *rsp;
+ struct timeval timeo;
+ char pkt[BNEP_MTU];
+ ssize_t r;
+ int sk;
+ const char *pdev, *uuid;
+ gboolean connected;
+
+ if (cond & G_IO_NVAL)
+ return FALSE;
+
+ if (cond & (G_IO_HUP | G_IO_ERR)) {
+ error("Hangup or error on l2cap server socket");
+ goto failed;
+ }
+
+ sk = g_io_channel_unix_get_fd(chan);
+
+ memset(pkt, 0, BNEP_MTU);
+ r = read(sk, pkt, sizeof(pkt) -1);
+ if (r < 0) {
+ error("IO Channel read error");
+ goto failed;
+ }
+
+ if (r == 0) {
+ error("No packet received on l2cap socket");
+ goto failed;
+ }
+
+ errno = EPROTO;
+
+ if ((size_t) r < sizeof(*rsp)) {
+ error("Packet received is not bnep type");
+ goto failed;
+ }
+
+ rsp = (void *) pkt;
+ if (rsp->type != BNEP_CONTROL) {
+ error("Packet received is not bnep type");
+ goto failed;
+ }
+
+ if (rsp->ctrl != BNEP_SETUP_CONN_RSP)
+ return TRUE;
+
+ r = ntohs(rsp->resp);
+
+ if (r != BNEP_SUCCESS) {
+ error("bnep failed");
+ goto failed;
+ }
+
+ memset(&timeo, 0, sizeof(timeo));
+ timeo.tv_sec = 0;
+
+ setsockopt(sk, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
+
+ if (bnep_connadd(sk, BNEP_SVC_PANU, nc->dev)) {
+ error("%s could not be added", nc->dev);
+ goto failed;
+ }
+
+ bnep_if_up(nc->dev);
+ pdev = nc->dev;
+ uuid = bnep_uuid(nc->id);
+
+ g_dbus_send_reply(connection, nc->msg,
+ DBUS_TYPE_STRING, &pdev,
+ DBUS_TYPE_INVALID);
+
+ connected = TRUE;
+ emit_property_changed(connection, nc->peer->path,
+ NETWORK_PEER_INTERFACE, "Connected",
+ DBUS_TYPE_BOOLEAN, &connected);
+ emit_property_changed(connection, nc->peer->path,
+ NETWORK_PEER_INTERFACE, "Interface",
+ DBUS_TYPE_STRING, &pdev);
+ emit_property_changed(connection, nc->peer->path,
+ NETWORK_PEER_INTERFACE, "UUID",
+ DBUS_TYPE_STRING, &uuid);
+
+ nc->state = CONNECTED;
+ nc->dc_id = device_add_disconnect_watch(nc->peer->device, disconnect_cb,
+ nc, NULL);
+
+ info("%s connected", nc->dev);
+ /* Start watchdog */
+ g_io_add_watch(chan, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
+ (GIOFunc) bnep_watchdog_cb, nc);
+ g_io_channel_unref(nc->io);
+ nc->io = NULL;
+
+ return FALSE;
+
+failed:
+ cancel_connection(nc, "bnep setup failed");
+
+ return FALSE;
+}
+
+static int bnep_connect(struct network_conn *nc)
+{
+ struct bnep_setup_conn_req *req;
+ struct __service_16 *s;
+ struct timeval timeo;
+ unsigned char pkt[BNEP_MTU];
+ int fd;
+
+ /* Send request */
+ req = (void *) pkt;
+ req->type = BNEP_CONTROL;
+ req->ctrl = BNEP_SETUP_CONN_REQ;
+ req->uuid_size = 2; /* 16bit UUID */
+ s = (void *) req->service;
+ s->dst = htons(nc->id);
+ s->src = htons(BNEP_SVC_PANU);
+
+ memset(&timeo, 0, sizeof(timeo));
+ timeo.tv_sec = 30;
+
+ fd = g_io_channel_unix_get_fd(nc->io);
+ setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
+
+ if (send(fd, pkt, sizeof(*req) + sizeof(*s), 0) < 0)
+ return -errno;
+
+ g_io_add_watch(nc->io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
+ (GIOFunc) bnep_setup_cb, nc);
+
+ return 0;
+}
+
+static void connect_cb(GIOChannel *chan, GError *err, gpointer data)
+{
+ struct network_conn *nc = data;
+ const char *err_msg;
+ int perr;
+
+ if (err) {
+ error("%s", err->message);
+ err_msg = err->message;
+ goto failed;
+ }
+
+ perr = bnep_connect(nc);
+ if (perr < 0) {
+ err_msg = strerror(-perr);
+ error("bnep connect(): %s (%d)", err_msg, -perr);
+ goto failed;
+ }
+
+ return;
+
+failed:
+ cancel_connection(nc, err_msg);
+}
+
+/* Connect and initiate BNEP session */
+static DBusMessage *connection_connect(DBusConnection *conn,
+ DBusMessage *msg, void *data)
+{
+ struct network_peer *peer = data;
+ struct network_conn *nc;
+ const char *svc;
+ uint16_t id;
+ GError *err = NULL;
+
+ if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &svc,
+ DBUS_TYPE_INVALID) == FALSE)
+ return NULL;
+
+ id = bnep_service_id(svc);
+ nc = find_connection(peer->connections, id);
+ if (!nc)
+ return btd_error_not_supported(msg);
+
+ if (nc->state != DISCONNECTED)
+ return btd_error_already_connected(msg);
+
+ nc->io = bt_io_connect(BT_IO_L2CAP, connect_cb, nc,
+ NULL, &err,
+ BT_IO_OPT_SOURCE_BDADDR, &peer->src,
+ BT_IO_OPT_DEST_BDADDR, &peer->dst,
+ BT_IO_OPT_PSM, BNEP_PSM,
+ BT_IO_OPT_OMTU, BNEP_MTU,
+ BT_IO_OPT_IMTU, BNEP_MTU,
+ BT_IO_OPT_INVALID);
+ if (!nc->io) {
+ DBusMessage *reply;
+ error("%s", err->message);
+ reply = btd_error_failed(msg, err->message);
+ g_error_free(err);
+ return reply;
+ }
+
+ nc->state = CONNECTING;
+ nc->msg = dbus_message_ref(msg);
+ nc->watch = g_dbus_add_disconnect_watch(conn,
+ dbus_message_get_sender(msg),
+ connection_destroy,
+ nc, NULL);
+
+ return NULL;
+}
+
+static DBusMessage *connection_cancel(DBusConnection *conn,
+ DBusMessage *msg, void *data)
+{
+ struct network_conn *nc = data;
+ const char *owner = dbus_message_get_sender(nc->msg);
+ const char *caller = dbus_message_get_sender(msg);
+
+ if (!g_str_equal(owner, caller))
+ return btd_error_not_authorized(msg);
+
+ connection_destroy(conn, nc);
+
+ return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
+}
+
+static DBusMessage *connection_disconnect(DBusConnection *conn,
+ DBusMessage *msg, void *data)
+{
+ struct network_peer *peer = data;
+ GSList *l;
+
+ for (l = peer->connections; l; l = l->next) {
+ struct network_conn *nc = l->data;
+
+ if (nc->state == DISCONNECTED)
+ continue;
+
+ return connection_cancel(conn, msg, nc);
+ }
+
+ return btd_error_not_connected(msg);
+}
+
+static DBusMessage *connection_get_properties(DBusConnection *conn,
+ DBusMessage *msg, void *data)
+{
+ struct network_peer *peer = data;
+ struct network_conn *nc = NULL;
+ DBusMessage *reply;
+ DBusMessageIter iter;
+ DBusMessageIter dict;
+ dbus_bool_t connected;
+ const char *property;
+ GSList *l;
+
+ reply = dbus_message_new_method_return(msg);
+ if (!reply)
+ return NULL;
+
+ dbus_message_iter_init_append(reply, &iter);
+
+ dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
+ DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
+ DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
+
+ /* Connected */
+ for (l = peer->connections; l; l = l->next) {
+ struct network_conn *tmp = l->data;
+
+ if (tmp->state != CONNECTED)
+ continue;
+
+ nc = tmp;
+ break;
+ }
+
+ connected = nc ? TRUE : FALSE;
+ dict_append_entry(&dict, "Connected", DBUS_TYPE_BOOLEAN, &connected);
+
+ /* Interface */
+ property = nc ? nc->dev : "";
+ dict_append_entry(&dict, "Interface", DBUS_TYPE_STRING, &property);
+
+ /* UUID */
+ property = nc ? bnep_uuid(nc->id) : "";
+ dict_append_entry(&dict, "UUID", DBUS_TYPE_STRING, &property);
+
+ dbus_message_iter_close_container(&iter, &dict);
+
+ return reply;
+}
+
+static void connection_free(struct network_conn *nc)
+{
+ if (nc->dc_id)
+ device_remove_disconnect_watch(nc->peer->device, nc->dc_id);
+
+ connection_destroy(connection, nc);
+
+ g_free(nc);
+ nc = NULL;
+}
+
+static void peer_free(struct network_peer *peer)
+{
+ g_slist_foreach(peer->connections, (GFunc) connection_free, NULL);
+ g_slist_free(peer->connections);
+ btd_device_unref(peer->device);
+ g_free(peer->path);
+ g_free(peer);
+}
+
+static void path_unregister(void *data)
+{
+ struct network_peer *peer = data;
+
+ DBG("Unregistered interface %s on path %s",
+ NETWORK_PEER_INTERFACE, peer->path);
+
+ peers = g_slist_remove(peers, peer);
+ peer_free(peer);
+}
+
+static GDBusMethodTable connection_methods[] = {
+ { "Connect", "s", "s", connection_connect,
+ G_DBUS_METHOD_FLAG_ASYNC },
+ { "Disconnect", "", "", connection_disconnect },
+ { "GetProperties", "", "a{sv}",connection_get_properties },
+ { }
+};
+
+static GDBusSignalTable connection_signals[] = {
+ { "PropertyChanged", "sv" },
+ { }
+};
+
+void connection_unregister(const char *path, uint16_t id)
+{
+ struct network_peer *peer;
+ struct network_conn *nc;
+
+ peer = find_peer(peers, path);
+ if (!peer)
+ return;
+
+ nc = find_connection(peer->connections, id);
+ if (!nc)
+ return;
+
+ peer->connections = g_slist_remove(peer->connections, nc);
+ connection_free(nc);
+ if (peer->connections)
+ return;
+
+ g_dbus_unregister_interface(connection, path, NETWORK_PEER_INTERFACE);
+}
+
+static struct network_peer *create_peer(struct btd_device *device,
+ const char *path, bdaddr_t *src,
+ bdaddr_t *dst)
+{
+ struct network_peer *peer;
+
+ peer = g_new0(struct network_peer, 1);
+ peer->device = btd_device_ref(device);
+ peer->path = g_strdup(path);
+ bacpy(&peer->src, src);
+ bacpy(&peer->dst, dst);
+
+ if (g_dbus_register_interface(connection, path,
+ NETWORK_PEER_INTERFACE,
+ connection_methods,
+ connection_signals, NULL,
+ peer, path_unregister) == FALSE) {
+ error("D-Bus failed to register %s interface",
+ NETWORK_PEER_INTERFACE);
+ peer_free(peer);
+ return NULL;
+ }
+
+ DBG("Registered interface %s on path %s",
+ NETWORK_PEER_INTERFACE, path);
+
+ return peer;
+}
+
+int connection_register(struct btd_device *device, const char *path,
+ bdaddr_t *src, bdaddr_t *dst, uint16_t id)
+{
+ struct network_peer *peer;
+ struct network_conn *nc;
+
+ if (!path)
+ return -EINVAL;
+
+ peer = find_peer(peers, path);
+ if (!peer) {
+ peer = create_peer(device, path, src, dst);
+ if (!peer)
+ return -1;
+ peers = g_slist_append(peers, peer);
+ }
+
+ nc = find_connection(peer->connections, id);
+ if (nc)
+ return 0;
+
+ nc = g_new0(struct network_conn, 1);
+ nc->id = id;
+ memset(nc->dev, 0, sizeof(nc->dev));
+ strcpy(nc->dev, "bnep%d");
+ nc->state = DISCONNECTED;
+ nc->peer = peer;
+
+ peer->connections = g_slist_append(peer->connections, nc);
+
+ return 0;
+}
+
+int connection_init(DBusConnection *conn)
+{
+ connection = dbus_connection_ref(conn);
+
+ return 0;
+}
+
+void connection_exit(void)
+{
+ dbus_connection_unref(connection);
+ connection = NULL;
+}
diff --git a/network/connection.h b/network/connection.h
new file mode 100644
index 0000000..5ea4147
--- /dev/null
+++ b/network/connection.h
@@ -0,0 +1,28 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2010 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
+ *
+ */
+
+int connection_init(DBusConnection *conn);
+void connection_exit(void);
+int connection_register(struct btd_device *device, const char *path,
+ bdaddr_t *src, bdaddr_t *dst, uint16_t id);
+void connection_unregister(const char *path, uint16_t id);
diff --git a/network/main.c b/network/main.c
new file mode 100644
index 0000000..88e77ee
--- /dev/null
+++ b/network/main.c
@@ -0,0 +1,59 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2010 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
+
+#include <errno.h>
+
+#include <gdbus.h>
+
+#include "plugin.h"
+#include "manager.h"
+
+static DBusConnection *connection;
+
+static int network_init(void)
+{
+ connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
+ if (connection == NULL)
+ return -EIO;
+
+ if (network_manager_init(connection) < 0) {
+ dbus_connection_unref(connection);
+ return -EIO;
+ }
+
+ return 0;
+}
+
+static void network_exit(void)
+{
+ network_manager_exit();
+
+ dbus_connection_unref(connection);
+}
+
+BLUETOOTH_PLUGIN_DEFINE(network, VERSION,
+ BLUETOOTH_PLUGIN_PRIORITY_DEFAULT, network_init, network_exit)
diff --git a/network/manager.c b/network/manager.c
new file mode 100644
index 0000000..321640b
--- /dev/null
+++ b/network/manager.c
@@ -0,0 +1,222 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2010 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
+
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/bnep.h>
+#include <bluetooth/sdp.h>
+
+#include <glib.h>
+#include <gdbus.h>
+
+#include "log.h"
+
+#include "adapter.h"
+#include "device.h"
+#include "manager.h"
+#include "common.h"
+#include "connection.h"
+#include "server.h"
+
+static DBusConnection *connection = NULL;
+
+static gboolean conf_security = TRUE;
+
+static void read_config(const char *file)
+{
+ GKeyFile *keyfile;
+ GError *err = NULL;
+
+ keyfile = g_key_file_new();
+
+ if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
+ g_clear_error(&err);
+ goto done;
+ }
+
+ conf_security = !g_key_file_get_boolean(keyfile, "General",
+ "DisableSecurity", &err);
+ if (err) {
+ DBG("%s: %s", file, err->message);
+ g_clear_error(&err);
+ }
+
+done:
+ g_key_file_free(keyfile);
+
+ DBG("Config options: Security=%s",
+ conf_security ? "true" : "false");
+}
+
+static int network_probe(struct btd_device *device, GSList *uuids, uint16_t id)
+{
+ struct btd_adapter *adapter = device_get_adapter(device);
+ const gchar *path = device_get_path(device);
+ bdaddr_t src, dst;
+
+ DBG("path %s", path);
+
+ adapter_get_address(adapter, &src);
+ device_get_address(device, &dst);
+
+ return connection_register(device, path, &src, &dst, id);
+}
+
+static void network_remove(struct btd_device *device, uint16_t id)
+{
+ const gchar *path = device_get_path(device);
+
+ DBG("path %s", path);
+
+ connection_unregister(path, id);
+}
+
+static int panu_probe(struct btd_device *device, GSList *uuids)
+{
+ return network_probe(device, uuids, BNEP_SVC_PANU);
+}
+
+static void panu_remove(struct btd_device *device)
+{
+ network_remove(device, BNEP_SVC_PANU);
+}
+
+static int gn_probe(struct btd_device *device, GSList *uuids)
+{
+ return network_probe(device, uuids, BNEP_SVC_GN);
+}
+
+static void gn_remove(struct btd_device *device)
+{
+ network_remove(device, BNEP_SVC_GN);
+}
+
+static int nap_probe(struct btd_device *device, GSList *uuids)
+{
+ return network_probe(device, uuids, BNEP_SVC_NAP);
+}
+
+static void nap_remove(struct btd_device *device)
+{
+ network_remove(device, BNEP_SVC_NAP);
+}
+
+static int network_server_probe(struct btd_adapter *adapter)
+{
+ const gchar *path = adapter_get_path(adapter);
+
+ DBG("path %s", path);
+
+ return server_register(adapter);
+}
+
+static void network_server_remove(struct btd_adapter *adapter)
+{
+ const gchar *path = adapter_get_path(adapter);
+
+ DBG("path %s", path);
+
+ server_unregister(adapter);
+}
+
+static struct btd_device_driver network_panu_driver = {
+ .name = "network-panu",
+ .uuids = BTD_UUIDS(PANU_UUID),
+ .probe = panu_probe,
+ .remove = panu_remove,
+};
+
+static struct btd_device_driver network_gn_driver = {
+ .name = "network-gn",
+ .uuids = BTD_UUIDS(GN_UUID),
+ .probe = gn_probe,
+ .remove = gn_remove,
+};
+
+static struct btd_device_driver network_nap_driver = {
+ .name = "network-nap",
+ .uuids = BTD_UUIDS(NAP_UUID),
+ .probe = nap_probe,
+ .remove = nap_remove,
+};
+
+static struct btd_adapter_driver network_server_driver = {
+ .name = "network-server",
+ .probe = network_server_probe,
+ .remove = network_server_remove,
+};
+
+int network_manager_init(DBusConnection *conn)
+{
+ read_config(CONFIGDIR "/network.conf");
+
+ if (bnep_init()) {
+ error("Can't init bnep module");
+ return -1;
+ }
+
+ /*
+ * There is one socket to handle the incomming connections. NAP,
+ * GN and PANU servers share the same PSM. The initial BNEP message
+ * (setup connection request) contains the destination service
+ * field that defines which service the source is connecting to.
+ */
+
+ if (server_init(conn, conf_security) < 0)
+ return -1;
+
+ /* Register network server if it doesn't exist */
+ btd_register_adapter_driver(&network_server_driver);
+
+ if (connection_init(conn) < 0)
+ return -1;
+
+ btd_register_device_driver(&network_panu_driver);
+ btd_register_device_driver(&network_gn_driver);
+ btd_register_device_driver(&network_nap_driver);
+
+ connection = dbus_connection_ref(conn);
+
+ return 0;
+}
+
+void network_manager_exit(void)
+{
+ server_exit();
+
+ btd_unregister_device_driver(&network_panu_driver);
+ btd_unregister_device_driver(&network_gn_driver);
+ btd_unregister_device_driver(&network_nap_driver);
+
+ connection_exit();
+
+ btd_unregister_adapter_driver(&network_server_driver);
+
+ dbus_connection_unref(connection);
+ connection = NULL;
+
+ bnep_cleanup();
+}
diff --git a/network/manager.h b/network/manager.h
new file mode 100644
index 0000000..27bc13f
--- /dev/null
+++ b/network/manager.h
@@ -0,0 +1,25 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2010 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
+ *
+ */
+
+int network_manager_init(DBusConnection *conn);
+void network_manager_exit(void);
diff --git a/network/network.conf b/network/network.conf
new file mode 100644
index 0000000..5f11639
--- /dev/null
+++ b/network/network.conf
@@ -0,0 +1,6 @@
+# Configuration file for the network service
+
+[General]
+
+# Disable link encryption: default=false
+#DisableSecurity=true
diff --git a/network/server.c b/network/server.c
new file mode 100644
index 0000000..d1da8a9
--- /dev/null
+++ b/network/server.c
@@ -0,0 +1,809 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2010 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
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/bnep.h>
+#include <bluetooth/sdp.h>
+#include <bluetooth/sdp_lib.h>
+#include <netinet/in.h>
+
+#include <glib.h>
+#include <gdbus.h>
+
+#include "../src/dbus-common.h"
+#include "../src/adapter.h"
+
+#include "log.h"
+#include "error.h"
+#include "sdpd.h"
+#include "btio.h"
+#include "glib-helper.h"
+
+#include "common.h"
+#include "server.h"
+
+#define NETWORK_SERVER_INTERFACE "org.bluez.NetworkServer"
+#define SETUP_TIMEOUT 1
+
+/* Pending Authorization */
+struct network_session {
+ bdaddr_t dst; /* Remote Bluetooth Address */
+ GIOChannel *io; /* Pending connect channel */
+ guint watch; /* BNEP socket watch */
+};
+
+struct network_adapter {
+ struct btd_adapter *adapter; /* Adapter pointer */
+ GIOChannel *io; /* Bnep socket */
+ struct network_session *setup; /* Setup in progress */
+ GSList *servers; /* Server register to adapter */
+};
+
+/* Main server structure */
+struct network_server {
+ bdaddr_t src; /* Bluetooth Local Address */
+ char *iface; /* DBus interface */
+ char *name; /* Server service name */
+ char *bridge; /* Bridge name */
+ uint32_t record_id; /* Service record id */
+ uint16_t id; /* Service class identifier */
+ GSList *sessions; /* Active connections */
+ struct network_adapter *na; /* Adapter reference */
+ guint watch_id; /* Client service watch */
+};
+
+static DBusConnection *connection = NULL;
+static GSList *adapters = NULL;
+static gboolean security = TRUE;
+
+static struct network_adapter *find_adapter(GSList *list,
+ struct btd_adapter *adapter)
+{
+ GSList *l;
+
+ for (l = list; l; l = l->next) {
+ struct network_adapter *na = l->data;
+
+ if (na->adapter == adapter)
+ return na;
+ }
+
+ return NULL;
+}
+
+static struct network_server *find_server(GSList *list, uint16_t id)
+{
+ GSList *l;
+
+ for (l = list; l; l = l->next) {
+ struct network_server *ns = l->data;
+
+ if (ns->id == id)
+ return ns;
+ }
+
+ return NULL;
+}
+
+static void add_lang_attr(sdp_record_t *r)
+{
+ sdp_lang_attr_t base_lang;
+ sdp_list_t *langs = 0;
+
+ /* UTF-8 MIBenum (http://www.iana.org/assignments/character-sets) */
+ base_lang.code_ISO639 = (0x65 << 8) | 0x6e;
+ base_lang.encoding = 106;
+ base_lang.base_offset = SDP_PRIMARY_LANG_BASE;
+ langs = sdp_list_append(0, &base_lang);
+ sdp_set_lang_attr(r, langs);
+ sdp_list_free(langs, 0);
+}
+
+static sdp_record_t *server_record_new(const char *name, uint16_t id)
+{
+ sdp_list_t *svclass, *pfseq, *apseq, *root, *aproto;
+ uuid_t root_uuid, pan, l2cap, bnep;
+ sdp_profile_desc_t profile[1];
+ sdp_list_t *proto[2];
+ sdp_data_t *v, *p;
+ uint16_t psm = BNEP_PSM, version = 0x0100;
+ uint16_t security_desc = (security ? 0x0001 : 0x0000);
+ uint16_t net_access_type = 0xfffe;
+ uint32_t max_net_access_rate = 0;
+ const char *desc = "Network service";
+ sdp_record_t *record;
+
+ record = sdp_record_alloc();
+ if (!record)
+ return NULL;
+
+ record->attrlist = NULL;
+ record->pattern = NULL;
+
+ switch (id) {
+ case BNEP_SVC_NAP:
+ sdp_uuid16_create(&pan, NAP_SVCLASS_ID);
+ svclass = sdp_list_append(NULL, &pan);
+ sdp_set_service_classes(record, svclass);
+
+ sdp_uuid16_create(&profile[0].uuid, NAP_PROFILE_ID);
+ profile[0].version = 0x0100;
+ pfseq = sdp_list_append(NULL, &profile[0]);
+ sdp_set_profile_descs(record, pfseq);
+
+ sdp_set_info_attr(record, name, NULL, desc);
+
+ sdp_attr_add_new(record, SDP_ATTR_NET_ACCESS_TYPE,
+ SDP_UINT16, &net_access_type);
+ sdp_attr_add_new(record, SDP_ATTR_MAX_NET_ACCESSRATE,
+ SDP_UINT32, &max_net_access_rate);
+ break;
+ case BNEP_SVC_GN:
+ sdp_uuid16_create(&pan, GN_SVCLASS_ID);
+ svclass = sdp_list_append(NULL, &pan);
+ sdp_set_service_classes(record, svclass);
+
+ sdp_uuid16_create(&profile[0].uuid, GN_PROFILE_ID);
+ profile[0].version = 0x0100;
+ pfseq = sdp_list_append(NULL, &profile[0]);
+ sdp_set_profile_descs(record, pfseq);
+
+ sdp_set_info_attr(record, name, NULL, desc);
+ break;
+ case BNEP_SVC_PANU:
+ sdp_uuid16_create(&pan, PANU_SVCLASS_ID);
+ svclass = sdp_list_append(NULL, &pan);
+ sdp_set_service_classes(record, svclass);
+
+ sdp_uuid16_create(&profile[0].uuid, PANU_PROFILE_ID);
+ profile[0].version = 0x0100;
+ pfseq = sdp_list_append(NULL, &profile[0]);
+ sdp_set_profile_descs(record, pfseq);
+
+ sdp_set_info_attr(record, name, NULL, desc);
+ break;
+ default:
+ sdp_record_free(record);
+ return NULL;
+ }
+
+ sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
+ root = sdp_list_append(NULL, &root_uuid);
+ sdp_set_browse_groups(record, root);
+
+ sdp_uuid16_create(&l2cap, L2CAP_UUID);
+ proto[0] = sdp_list_append(NULL, &l2cap);
+ p = sdp_data_alloc(SDP_UINT16, &psm);
+ proto[0] = sdp_list_append(proto[0], p);
+ apseq = sdp_list_append(NULL, proto[0]);
+
+ sdp_uuid16_create(&bnep, BNEP_UUID);
+ proto[1] = sdp_list_append(NULL, &bnep);
+ v = sdp_data_alloc(SDP_UINT16, &version);
+ proto[1] = sdp_list_append(proto[1], v);
+
+ /* Supported protocols */
+ {
+ uint16_t ptype[] = {
+ 0x0800, /* IPv4 */
+ 0x0806, /* ARP */
+ };
+ sdp_data_t *head, *pseq;
+ int p;
+
+ for (p = 0, head = NULL; p < 2; p++) {
+ sdp_data_t *data = sdp_data_alloc(SDP_UINT16, &ptype[p]);
+ if (head)
+ sdp_seq_append(head, data);
+ else
+ head = data;
+ }
+ pseq = sdp_data_alloc(SDP_SEQ16, head);
+ proto[1] = sdp_list_append(proto[1], pseq);
+ }
+
+ apseq = sdp_list_append(apseq, proto[1]);
+
+ aproto = sdp_list_append(NULL, apseq);
+ sdp_set_access_protos(record, aproto);
+
+ add_lang_attr(record);
+
+ sdp_attr_add_new(record, SDP_ATTR_SECURITY_DESC,
+ SDP_UINT16, &security_desc);
+
+ sdp_data_free(p);
+ sdp_data_free(v);
+ sdp_list_free(apseq, NULL);
+ sdp_list_free(root, NULL);
+ sdp_list_free(aproto, NULL);
+ sdp_list_free(proto[0], NULL);
+ sdp_list_free(proto[1], NULL);
+ sdp_list_free(svclass, NULL);
+ sdp_list_free(pfseq, NULL);
+
+ return record;
+}
+
+static ssize_t send_bnep_ctrl_rsp(int sk, uint16_t val)
+{
+ struct bnep_control_rsp rsp;
+
+ rsp.type = BNEP_CONTROL;
+ rsp.ctrl = BNEP_SETUP_CONN_RSP;
+ rsp.resp = htons(val);
+
+ return send(sk, &rsp, sizeof(rsp), 0);
+}
+
+static int server_connadd(struct network_server *ns,
+ struct network_session *session,
+ uint16_t dst_role)
+{
+ char devname[16];
+ int err, nsk;
+
+ memset(devname, 0, sizeof(devname));
+ strcpy(devname, "bnep%d");
+
+ nsk = g_io_channel_unix_get_fd(session->io);
+ err = bnep_connadd(nsk, dst_role, devname);
+ if (err < 0)
+ return err;
+
+ info("Added new connection: %s", devname);
+
+ if (bnep_add_to_bridge(devname, ns->bridge) < 0) {
+ error("Can't add %s to the bridge %s: %s(%d)",
+ devname, ns->bridge, strerror(errno), errno);
+ return -EPERM;
+ }
+
+ bnep_if_up(devname);
+
+ ns->sessions = g_slist_append(ns->sessions, session);
+
+ return 0;
+}
+
+static uint16_t bnep_setup_chk(uint16_t dst_role, uint16_t src_role)
+{
+ /* Allowed PAN Profile scenarios */
+ switch (dst_role) {
+ case BNEP_SVC_NAP:
+ case BNEP_SVC_GN:
+ if (src_role == BNEP_SVC_PANU)
+ return 0;
+ return BNEP_CONN_INVALID_SRC;
+ case BNEP_SVC_PANU:
+ if (src_role == BNEP_SVC_PANU ||
+ src_role == BNEP_SVC_GN ||
+ src_role == BNEP_SVC_NAP)
+ return 0;
+
+ return BNEP_CONN_INVALID_SRC;
+ }
+
+ return BNEP_CONN_INVALID_DST;
+}
+
+static uint16_t bnep_setup_decode(struct bnep_setup_conn_req *req,
+ uint16_t *dst_role, uint16_t *src_role)
+{
+ uint8_t *dest, *source;
+
+ dest = req->service;
+ source = req->service + req->uuid_size;
+
+ switch (req->uuid_size) {
+ case 2: /* UUID16 */
+ *dst_role = ntohs(bt_get_unaligned((uint16_t *) dest));
+ *src_role = ntohs(bt_get_unaligned((uint16_t *) source));
+ break;
+ case 4: /* UUID32 */
+ case 16: /* UUID128 */
+ *dst_role = ntohl(bt_get_unaligned((uint32_t *) dest));
+ *src_role = ntohl(bt_get_unaligned((uint32_t *) source));
+ break;
+ default:
+ return BNEP_CONN_INVALID_SVC;
+ }
+
+ return 0;
+}
+
+static void session_free(void *data)
+{
+ struct network_session *session = data;
+
+ if (session->watch)
+ g_source_remove(session->watch);
+
+ if (session->io)
+ g_io_channel_unref(session->io);
+
+ g_free(session);
+}
+
+static void setup_destroy(void *user_data)
+{
+ struct network_adapter *na = user_data;
+ struct network_session *setup = na->setup;
+
+ if (!setup)
+ return;
+
+ na->setup = NULL;
+
+ session_free(setup);
+}
+
+static gboolean bnep_setup(GIOChannel *chan,
+ GIOCondition cond, gpointer user_data)
+{
+ struct network_adapter *na = user_data;
+ struct network_server *ns;
+ uint8_t packet[BNEP_MTU];
+ struct bnep_setup_conn_req *req = (void *) packet;
+ uint16_t src_role, dst_role, rsp = BNEP_CONN_NOT_ALLOWED;
+ int n, sk;
+
+ if (cond & G_IO_NVAL)
+ return FALSE;
+
+ if (cond & (G_IO_ERR | G_IO_HUP)) {
+ error("Hangup or error on BNEP socket");
+ return FALSE;
+ }
+
+ sk = g_io_channel_unix_get_fd(chan);
+
+ /* Reading BNEP_SETUP_CONNECTION_REQUEST_MSG */
+ n = read(sk, packet, sizeof(packet));
+ if (n < 0) {
+ error("read(): %s(%d)", strerror(errno), errno);
+ return FALSE;
+ }
+
+ /* Highest known Control command ID
+ * is BNEP_FILTER_MULT_ADDR_RSP = 0x06 */
+ if (req->type == BNEP_CONTROL &&
+ req->ctrl > BNEP_FILTER_MULT_ADDR_RSP) {
+ uint8_t pkt[3];
+
+ pkt[0] = BNEP_CONTROL;
+ pkt[1] = BNEP_CMD_NOT_UNDERSTOOD;
+ pkt[2] = req->ctrl;
+
+ send(sk, pkt, sizeof(pkt), 0);
+
+ return FALSE;
+ }
+
+ if (req->type != BNEP_CONTROL || req->ctrl != BNEP_SETUP_CONN_REQ)
+ return FALSE;
+
+ rsp = bnep_setup_decode(req, &dst_role, &src_role);
+ if (rsp)
+ goto reply;
+
+ rsp = bnep_setup_chk(dst_role, src_role);
+ if (rsp)
+ goto reply;
+
+ ns = find_server(na->servers, dst_role);
+ if (!ns) {
+ error("Server unavailable: (0x%x)", dst_role);
+ goto reply;
+ }
+
+ if (!ns->record_id) {
+ error("Service record not available");
+ goto reply;
+ }
+
+ if (!ns->bridge) {
+ error("Bridge interface not configured");
+ goto reply;
+ }
+
+ if (server_connadd(ns, na->setup, dst_role) < 0)
+ goto reply;
+
+ na->setup = NULL;
+
+ rsp = BNEP_SUCCESS;
+
+reply:
+ send_bnep_ctrl_rsp(sk, rsp);
+
+ return FALSE;
+}
+
+static void connect_event(GIOChannel *chan, GError *err, gpointer user_data)
+{
+ struct network_adapter *na = user_data;
+
+ if (err) {
+ error("%s", err->message);
+ setup_destroy(na);
+ return;
+ }
+
+ g_io_channel_set_close_on_unref(chan, TRUE);
+
+ na->setup->watch = g_io_add_watch_full(chan, G_PRIORITY_DEFAULT,
+ G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+ bnep_setup, na, setup_destroy);
+}
+
+static void auth_cb(DBusError *derr, void *user_data)
+{
+ struct network_adapter *na = user_data;
+ GError *err = NULL;
+
+ if (derr) {
+ error("Access denied: %s", derr->message);
+ goto reject;
+ }
+
+ if (!bt_io_accept(na->setup->io, connect_event, na, NULL,
+ &err)) {
+ error("bt_io_accept: %s", err->message);
+ g_error_free(err);
+ goto reject;
+ }
+
+ return;
+
+reject:
+ g_io_channel_shutdown(na->setup->io, TRUE, NULL);
+ setup_destroy(na);
+}
+
+static void confirm_event(GIOChannel *chan, gpointer user_data)
+{
+ struct network_adapter *na = user_data;
+ struct network_server *ns;
+ int perr;
+ bdaddr_t src, dst;
+ char address[18];
+ GError *err = NULL;
+
+ bt_io_get(chan, BT_IO_L2CAP, &err,
+ BT_IO_OPT_SOURCE_BDADDR, &src,
+ BT_IO_OPT_DEST_BDADDR, &dst,
+ BT_IO_OPT_DEST, address,
+ BT_IO_OPT_INVALID);
+ if (err) {
+ error("%s", err->message);
+ g_error_free(err);
+ goto drop;
+ }
+
+ DBG("BNEP: incoming connect from %s", address);
+
+ if (na->setup) {
+ error("Refusing connect from %s: setup in progress", address);
+ goto drop;
+ }
+
+ ns = find_server(na->servers, BNEP_SVC_NAP);
+ if (!ns)
+ goto drop;
+
+ if (!ns->record_id)
+ goto drop;
+
+ if (!ns->bridge)
+ goto drop;
+
+ na->setup = g_new0(struct network_session, 1);
+ bacpy(&na->setup->dst, &dst);
+ na->setup->io = g_io_channel_ref(chan);
+
+ perr = btd_request_authorization(&src, &dst, BNEP_SVC_UUID,
+ auth_cb, na);
+ if (perr < 0) {
+ error("Refusing connect from %s: %s (%d)", address,
+ strerror(-perr), -perr);
+ setup_destroy(na);
+ goto drop;
+ }
+
+ return;
+
+drop:
+ g_io_channel_shutdown(chan, TRUE, NULL);
+}
+
+int server_init(DBusConnection *conn, gboolean secure)
+{
+ security = secure;
+ connection = dbus_connection_ref(conn);
+
+ return 0;
+}
+
+void server_exit(void)
+{
+ dbus_connection_unref(connection);
+ connection = NULL;
+}
+
+static uint32_t register_server_record(struct network_server *ns)
+{
+ sdp_record_t *record;
+
+ record = server_record_new(ns->name, ns->id);
+ if (!record) {
+ error("Unable to allocate new service record");
+ return 0;
+ }
+
+ if (add_record_to_server(&ns->src, record) < 0) {
+ error("Failed to register service record");
+ sdp_record_free(record);
+ return 0;
+ }
+
+ DBG("got record id 0x%x", record->handle);
+
+ return record->handle;
+}
+
+static void server_disconnect(DBusConnection *conn, void *user_data)
+{
+ struct network_server *ns = user_data;
+
+ ns->watch_id = 0;
+
+ if (ns->record_id) {
+ remove_record_from_server(ns->record_id);
+ ns->record_id = 0;
+ }
+
+ g_free(ns->bridge);
+ ns->bridge = NULL;
+}
+
+static DBusMessage *register_server(DBusConnection *conn,
+ DBusMessage *msg, void *data)
+{
+ struct network_server *ns = data;
+ DBusMessage *reply;
+ const char *uuid, *bridge;
+
+ if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &uuid,
+ DBUS_TYPE_STRING, &bridge, DBUS_TYPE_INVALID))
+ return NULL;
+
+ if (g_strcmp0(uuid, "nap"))
+ return btd_error_failed(msg, "Invalid UUID");
+
+ if (ns->record_id)
+ return btd_error_already_exists(msg);
+
+ reply = dbus_message_new_method_return(msg);
+ if (!reply)
+ return NULL;
+
+ ns->record_id = register_server_record(ns);
+ if (!ns->record_id)
+ return btd_error_failed(msg, "SDP record registration failed");
+
+ g_free(ns->bridge);
+ ns->bridge = g_strdup(bridge);
+
+ ns->watch_id = g_dbus_add_disconnect_watch(conn,
+ dbus_message_get_sender(msg),
+ server_disconnect, ns, NULL);
+
+ return reply;
+}
+
+static DBusMessage *unregister_server(DBusConnection *conn,
+ DBusMessage *msg, void *data)
+{
+ struct network_server *ns = data;
+ DBusMessage *reply;
+ const char *uuid;
+
+ if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &uuid,
+ DBUS_TYPE_INVALID))
+ return NULL;
+
+ if (g_strcmp0(uuid, "nap"))
+ return btd_error_failed(msg, "Invalid UUID");
+
+ reply = dbus_message_new_method_return(msg);
+ if (!reply)
+ return NULL;
+
+ g_dbus_remove_watch(conn, ns->watch_id);
+
+ server_disconnect(conn, ns);
+
+ return reply;
+}
+
+static void adapter_free(struct network_adapter *na)
+{
+ if (na->io != NULL) {
+ g_io_channel_shutdown(na->io, TRUE, NULL);
+ g_io_channel_unref(na->io);
+ }
+
+ setup_destroy(na);
+ btd_adapter_unref(na->adapter);
+ g_free(na);
+}
+
+static void server_free(struct network_server *ns)
+{
+ if (!ns)
+ return;
+
+ /* FIXME: Missing release/free all bnepX interfaces */
+ if (ns->record_id)
+ remove_record_from_server(ns->record_id);
+
+ g_free(ns->iface);
+ g_free(ns->name);
+ g_free(ns->bridge);
+
+ if (ns->sessions) {
+ g_slist_foreach(ns->sessions, (GFunc) session_free, NULL);
+ g_slist_free(ns->sessions);
+ }
+
+ g_free(ns);
+}
+
+static void path_unregister(void *data)
+{
+ struct network_server *ns = data;
+ struct network_adapter *na = ns->na;
+
+ DBG("Unregistered interface %s on path %s",
+ ns->iface, adapter_get_path(na->adapter));
+
+ na->servers = g_slist_remove(na->servers, ns);
+ server_free(ns);
+
+ if (na->servers)
+ return;
+
+ adapters = g_slist_remove(adapters, na);
+ adapter_free(na);
+}
+
+static GDBusMethodTable server_methods[] = {
+ { "Register", "ss", "", register_server },
+ { "Unregister", "s", "", unregister_server },
+ { }
+};
+
+static struct network_adapter *create_adapter(struct btd_adapter *adapter)
+{
+ struct network_adapter *na;
+ GError *err = NULL;
+ bdaddr_t src;
+
+ na = g_new0(struct network_adapter, 1);
+ na->adapter = btd_adapter_ref(adapter);
+
+ adapter_get_address(adapter, &src);
+
+ na->io = bt_io_listen(BT_IO_L2CAP, NULL, confirm_event, na,
+ NULL, &err,
+ BT_IO_OPT_SOURCE_BDADDR, &src,
+ BT_IO_OPT_PSM, BNEP_PSM,
+ BT_IO_OPT_OMTU, BNEP_MTU,
+ BT_IO_OPT_IMTU, BNEP_MTU,
+ BT_IO_OPT_SEC_LEVEL,
+ security ? BT_IO_SEC_MEDIUM : BT_IO_SEC_LOW,
+ BT_IO_OPT_INVALID);
+ if (!na->io) {
+ error("%s", err->message);
+ g_error_free(err);
+ adapter_free(na);
+ return NULL;
+ }
+
+ return na;
+}
+
+int server_register(struct btd_adapter *adapter)
+{
+ struct network_adapter *na;
+ struct network_server *ns;
+ const char *path;
+
+ na = find_adapter(adapters, adapter);
+ if (!na) {
+ na = create_adapter(adapter);
+ if (!na)
+ return -EINVAL;
+ adapters = g_slist_append(adapters, na);
+ }
+
+ ns = find_server(na->servers, BNEP_SVC_NAP);
+ if (ns)
+ return 0;
+
+ ns = g_new0(struct network_server, 1);
+
+ ns->iface = g_strdup(NETWORK_SERVER_INTERFACE);
+ ns->name = g_strdup("Network service");
+
+ path = adapter_get_path(adapter);
+
+ if (!g_dbus_register_interface(connection, path, ns->iface,
+ server_methods, NULL, NULL,
+ ns, path_unregister)) {
+ error("D-Bus failed to register %s interface",
+ ns->iface);
+ server_free(ns);
+ return -1;
+ }
+
+ adapter_get_address(adapter, &ns->src);
+ ns->id = BNEP_SVC_NAP;
+ ns->na = na;
+ ns->record_id = 0;
+ na->servers = g_slist_append(na->servers, ns);
+
+ DBG("Registered interface %s on path %s", ns->iface, path);
+
+ return 0;
+}
+
+int server_unregister(struct btd_adapter *adapter)
+{
+ struct network_adapter *na;
+ struct network_server *ns;
+ uint16_t id = BNEP_SVC_NAP;
+
+ na = find_adapter(adapters, adapter);
+ if (!na)
+ return -EINVAL;
+
+ ns = find_server(na->servers, id);
+ if (!ns)
+ return -EINVAL;
+
+ g_dbus_unregister_interface(connection, adapter_get_path(adapter),
+ ns->iface);
+
+ return 0;
+}
diff --git a/network/server.h b/network/server.h
new file mode 100644
index 0000000..d88acab
--- /dev/null
+++ b/network/server.h
@@ -0,0 +1,29 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2010 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
+ *
+ */
+
+int server_init(DBusConnection *conn, gboolean secure);
+void server_exit(void);
+int server_register(struct btd_adapter *adapter);
+int server_unregister(struct btd_adapter *adapter);
+
+int server_find_data(const char *path, const char *pattern);