From 6113615ecfb8026a6a3ce0af38fc79ec8abec748 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Fri, 28 Sep 2007 13:23:51 +0000 Subject: Rework configuration file to use roles. --- network/bridge.c | 35 +++++++++++++++++++----- network/bridge.h | 9 ++++--- network/common.c | 24 ++++++++++++++--- network/common.h | 5 ++-- network/connection.c | 15 ++--------- network/connection.h | 9 +------ network/main.c | 75 ++++++++++++++++++++++++++-------------------------- network/manager.c | 22 ++++++--------- network/manager.h | 8 ++++-- network/network.conf | 29 ++++++++++---------- network/server.c | 31 +++++++--------------- network/server.h | 10 +------ 12 files changed, 136 insertions(+), 136 deletions(-) (limited to 'network') diff --git a/network/bridge.c b/network/bridge.c index 8ef06190..24f1bcc4 100644 --- a/network/bridge.c +++ b/network/bridge.c @@ -31,15 +31,20 @@ #include #include #include - #include #include +#include +#include +#include + #include "bridge.h" static int bridge_socket = -1; +static const char *gn_bridge; +static const char *nap_bridge; -int bridge_init(void) +int bridge_init(const char *gn_iface, const char *nap_iface) { #if 0 struct stat st; @@ -52,6 +57,8 @@ int bridge_init(void) if (bridge_socket < 0) return -errno; + gn_bridge = gn_iface; + nap_bridge = nap_iface; return 0; } @@ -62,9 +69,10 @@ void bridge_cleanup(void) bridge_socket = -1; } -int bridge_create(const char *name) +int bridge_create(int id) { int err; + const char *name = bridge_get_name(id); err = ioctl(bridge_socket, SIOCBRADDBR, name); if (err < 0) @@ -73,9 +81,10 @@ int bridge_create(const char *name) return 0; } -int bridge_remove(const char *name) +int bridge_remove(int id) { int err; + const char *name = bridge_get_name(id); err = ioctl(bridge_socket, SIOCBRDELBR, name); if (err < 0) @@ -84,15 +93,16 @@ int bridge_remove(const char *name) return 0; } -int bridge_add_interface(const char *bridge, const char *dev) +int bridge_add_interface(int id, const char *dev) { struct ifreq ifr; int ifindex = if_nametoindex(dev); + const char *name = bridge_get_name(id); - if (ifindex == 0) + if (ifindex == 0) return -ENODEV; - strncpy(ifr.ifr_name, bridge, IFNAMSIZ); + strncpy(ifr.ifr_name, name, IFNAMSIZ); ifr.ifr_ifindex = ifindex; if (ioctl(bridge_socket, SIOCBRADDIF, &ifr) < 0) @@ -100,3 +110,14 @@ int bridge_add_interface(const char *bridge, const char *dev) return 0; } + +const char *bridge_get_name(int id) +{ + if (id == BNEP_SVC_GN) + return gn_bridge; + + if (id == BNEP_SVC_NAP) + return nap_bridge; + + return NULL; +} diff --git a/network/bridge.h b/network/bridge.h index 2630b892..0861cfb7 100644 --- a/network/bridge.h +++ b/network/bridge.h @@ -21,9 +21,10 @@ * */ -int bridge_init(void); +int bridge_init(const char *gn_iface, const char *nap_iface); void bridge_cleanup(void); -int bridge_create(const char *name); -int bridge_remove(const char *name); -int bridge_add_interface(const char *bridge, const char *dev); +int bridge_create(int id); +int bridge_remove(int id); +int bridge_add_interface(int id, const char *dev); +const char *bridge_get_name(int id); diff --git a/network/common.c b/network/common.c index dce12415..991073ec 100644 --- a/network/common.c +++ b/network/common.c @@ -63,6 +63,10 @@ static struct { { NULL } }; +static const char *panu = NULL; +static const char *gn = NULL; +static const char *nap = NULL; + struct bnep_data { char *devname; int pid; @@ -129,7 +133,8 @@ const char *bnep_name(uint16_t id) return NULL; } -int bnep_init(void) +int bnep_init(const char *panu_script, const char *gn_script, + const char *nap_script) { ctl = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_BNEP); @@ -140,6 +145,9 @@ int bnep_init(void) return -err; } + panu = panu_script; + gn = gn_script; + nap = nap_script; return 0; } @@ -212,11 +220,11 @@ static void bnep_setup(gpointer data) { } -int bnep_if_up(const char *devname, const char *script) +int bnep_if_up(const char *devname, uint16_t id) { int sd, err, pid; struct ifreq ifr; - const char *argv[3]; + const char *argv[3], *script; struct bnep_data *bnep; sd = socket(AF_INET6, SOCK_DGRAM, 0); @@ -232,6 +240,16 @@ int bnep_if_up(const char *devname, const char *script) return -err; } + if (!id) + return 0; + + if (id == BNEP_SVC_PANU) + script = panu; + else if (id == BNEP_SVC_GN) + script = gn; + else + script = nap; + if (!script) return 0; diff --git a/network/common.h b/network/common.h index de9d753d..2b475864 100644 --- a/network/common.h +++ b/network/common.h @@ -24,7 +24,8 @@ #define MAX_PATH_LENGTH 64 /* D-Bus path */ #define NETWORK_PATH "/org/bluez/network" -int bnep_init(void); +int bnep_init(const char *panu_script, const char *gn_script, + const char *nap_script); int bnep_cleanup(void); uint16_t bnep_service_id(const char *svc); @@ -35,7 +36,7 @@ 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, const char *script); +int bnep_if_up(const char *devname, uint16_t id); int bnep_if_down(const char *devname); int read_remote_name(bdaddr_t *src, bdaddr_t *dst, char *buf, size_t size); diff --git a/network/connection.c b/network/connection.c index fb1b2cdf..6157e377 100644 --- a/network/connection.c +++ b/network/connection.c @@ -62,7 +62,6 @@ struct network_conn { char dev[16]; /* Interface name */ char *name; /* Service Name */ char *desc; /* Service Description*/ - char *script; /* Interface up script*/ uint16_t id; /* Role: Service Class Identifier */ conn_state state; int sk; @@ -74,7 +73,6 @@ struct __service_16 { } __attribute__ ((packed)); static DBusConnection *connection = NULL; -static struct connection_conf *conf = NULL; static const char *prefix = NULL; static gboolean bnep_watchdog_cb(GIOChannel *chan, GIOCondition cond, @@ -157,7 +155,7 @@ static gboolean bnep_connect_cb(GIOChannel *chan, GIOCondition cond, goto failed; } - bnep_if_up(nc->dev, nc->script); + bnep_if_up(nc->dev, nc->id); dbus_connection_emit_signal(connection, nc->path, NETWORK_CONNECTION_INTERFACE, "Connected", @@ -666,12 +664,6 @@ int connection_register(const char *path, bdaddr_t *src, bdaddr_t *dst, nc->desc = g_strdup(desc); memset(nc->dev, 0, 16); strncpy(nc->dev, prefix, strlen(prefix)); - if (id == BNEP_SVC_PANU) - nc->script = conf->panu_script; - else if (id == BNEP_SVC_GN) - nc->script = conf->gn_script; - else - nc->script = conf->nap_script; nc->state = DISCONNECTED; info("Registered connection path:%s", path); @@ -795,11 +787,9 @@ gboolean connection_is_connected(const char *path) return (nc->state == CONNECTED); } -int connection_init(DBusConnection *conn, const char *iface_prefix, - struct connection_conf *conn_conf) +int connection_init(DBusConnection *conn, const char *iface_prefix) { connection = dbus_connection_ref(conn); - conf = conn_conf; prefix = iface_prefix; return 0; @@ -809,6 +799,5 @@ void connection_exit() { dbus_connection_unref(connection); connection = NULL; - conf = NULL; prefix = NULL; } diff --git a/network/connection.h b/network/connection.h index 7cafa122..5e560811 100644 --- a/network/connection.h +++ b/network/connection.h @@ -21,14 +21,7 @@ * */ -struct connection_conf { - char *panu_script; - char *gn_script; - char *nap_script; -}; - -int connection_init(DBusConnection *conn, const char *iface_prefix, - struct connection_conf *conn_conf); +int connection_init(DBusConnection *conn, const char *iface_prefix); void connection_exit(); int connection_register(const char *path, bdaddr_t *src, bdaddr_t *dst, uint16_t id, const char *name, const char *desc); diff --git a/network/main.c b/network/main.c index 6260ac4e..3ede75fe 100644 --- a/network/main.c +++ b/network/main.c @@ -52,13 +52,12 @@ static struct network_conf conf = { .connection_enabled = TRUE, .server_enabled = TRUE, .iface_prefix = NULL, - .conn.panu_script = NULL, - .conn.gn_script = NULL, - .conn.nap_script = NULL, - .server.panu_iface = NULL, - .server.gn_iface = NULL, - .server.nap_iface = NULL, - .server.disable_security = FALSE + .panu_script = NULL, + .gn_script = NULL, + .nap_script = NULL, + .gn_iface = NULL, + .nap_iface = NULL, + .security = TRUE }; static void sig_term(int sig) @@ -70,6 +69,7 @@ static void read_config(const char *file) { GKeyFile *keyfile; GError *err = NULL; + const char *disabled; keyfile = g_key_file_new(); @@ -80,72 +80,75 @@ static void read_config(const char *file) return; } - conf.iface_prefix = g_key_file_get_string(keyfile, "Connection", - "InterfacePrefix", &err); - if (!conf.iface_prefix) - conf.iface_prefix = g_strdup(IFACE_PREFIX); + disabled = g_key_file_get_string(keyfile, "General", + "Disable", &err); if (err) { debug("%s: %s", file, err->message); g_error_free(err); err = NULL; + } else { + if (strstr(disabled, "Connection")) + conf.connection_enabled = FALSE; + if (strstr(disabled, "Server")) + conf.server_enabled = FALSE; } - conf.conn.panu_script = g_key_file_get_string(keyfile, "Connection", - "PANUScript", &err); + conf.iface_prefix = g_key_file_get_string(keyfile, "General", + "InterfacePrefix", &err); + if (!conf.iface_prefix) + conf.iface_prefix = g_strdup(IFACE_PREFIX); if (err) { debug("%s: %s", file, err->message); g_error_free(err); err = NULL; } - conf.conn.gn_script = g_key_file_get_string(keyfile, "Connection", - "GNScript", &err); + conf.security = g_key_file_get_boolean(keyfile, "General", + "DisableSecurity", &err); if (err) { debug("%s: %s", file, err->message); g_error_free(err); err = NULL; } - conf.conn.nap_script = g_key_file_get_string(keyfile, "Connection", - "NAPScript", &err); + conf.panu_script = g_key_file_get_string(keyfile, "PANU Role", + "Script", &err); if (err) { debug("%s: %s", file, err->message); g_error_free(err); err = NULL; } - conf.server.panu_iface = g_key_file_get_string(keyfile, "Server", - "PANUInterface", &err); - if (!conf.server.panu_iface) - conf.server.panu_iface = g_strdup(PANU_IFACE); + conf.gn_script = g_key_file_get_string(keyfile, "GN Role", + "Script", &err); if (err) { debug("%s: %s", file, err->message); g_error_free(err); err = NULL; } - conf.server.gn_iface = g_key_file_get_string(keyfile, "Server", - "GNInterface", &err); - if (!conf.server.gn_iface) - conf.server.gn_iface = g_strdup(GN_IFACE); + conf.nap_script = g_key_file_get_string(keyfile, "NAP Role", + "Script", &err); if (err) { debug("%s: %s", file, err->message); g_error_free(err); err = NULL; } - conf.server.nap_iface = g_key_file_get_string(keyfile, "Server", - "NAPInterface", &err); - if (!conf.server.nap_iface) - conf.server.nap_iface = g_strdup(NAP_IFACE); + conf.gn_iface = g_key_file_get_string(keyfile, "GN Role", + "Interface", &err); + if (!conf.gn_iface) + conf.gn_iface = g_strdup(GN_IFACE); if (err) { debug("%s: %s", file, err->message); g_error_free(err); err = NULL; } - conf.server.disable_security = g_key_file_get_boolean(keyfile, "Server", - "DisableSecurity", &err); + conf.nap_iface = g_key_file_get_string(keyfile, "NAP Role", + "Interface", &err); + if (!conf.nap_iface) + conf.nap_iface = g_strdup(NAP_IFACE); if (err) { debug("%s: %s", file, err->message); g_error_free(err); @@ -153,12 +156,10 @@ static void read_config(const char *file) } debug("Config options: InterfacePrefix=%s, PANUScript=%s, GNScript=%s, " - "NAPScript=%s, PANUInterface=%s, GNInterface=%s, " - "NAPInterface=%s, DisableSecurity=%s", conf.iface_prefix, - conf.conn.panu_script, conf.conn.gn_script, conf.conn.nap_script, - conf.server.panu_iface, conf.server.gn_iface, - conf.server.nap_iface, - conf.server.disable_security ? "true" : "false"); + "NAPScript=%s, GNInterface=%s, NAPInterface=%s, Security=%s", + conf.iface_prefix, conf.panu_script, conf.gn_script, + conf.nap_script, conf.gn_iface, conf.nap_iface, + conf.security ? "true" : "false"); g_key_file_free(keyfile); } diff --git a/network/manager.c b/network/manager.c index 5e1ab2f0..c82d1c03 100644 --- a/network/manager.c +++ b/network/manager.c @@ -923,21 +923,18 @@ int network_init(DBusConnection *conn, struct network_conf *service_conf) { conf = service_conf; - if (bridge_init() < 0) { + if (bridge_init(conf->gn_iface, conf->nap_iface) < 0) { error("Can't init bridge module"); return -1; } - if (bridge_create(conf->server.panu_iface) < 0) - error("Can't create PANU bridge"); - - if (bridge_create(conf->server.gn_iface) < 0) + if (bridge_create(BNEP_SVC_GN) < 0) error("Can't create GN bridge"); - if (bridge_create(conf->server.nap_iface) < 0) + if (bridge_create(BNEP_SVC_NAP) < 0) error("Can't create NAP bridge"); - if (bnep_init()) { + if (bnep_init(conf->panu_script, conf->gn_script, conf->nap_script)) { error("Can't init bnep module"); return -1; } @@ -948,10 +945,10 @@ int network_init(DBusConnection *conn, struct network_conf *service_conf) * (setup connection request) contains the destination service * field that defines which service the source is connecting to. */ - if (server_init(conn, conf->iface_prefix, &conf->server) < 0) + if (server_init(conn, conf->iface_prefix) < 0) return -1; - if (connection_init(conn, conf->iface_prefix, &conf->conn) < 0) + if (connection_init(conn, conf->iface_prefix) < 0) return -1; if (!dbus_connection_create_object_path(conn, NETWORK_PATH, @@ -993,13 +990,10 @@ void network_exit(void) connection = NULL; - if (bridge_remove(conf->server.panu_iface) < 0) - error("Can't remove PANU bridge"); - - if (bridge_remove(conf->server.gn_iface) < 0) + if (bridge_remove(BNEP_SVC_GN) < 0) error("Can't remove GN bridge"); - if (bridge_remove(conf->server.nap_iface) < 0) + if (bridge_remove(BNEP_SVC_NAP) < 0) error("Can't remove NAP bridge"); bnep_cleanup(); diff --git a/network/manager.h b/network/manager.h index 0efacc14..622665e8 100644 --- a/network/manager.h +++ b/network/manager.h @@ -30,9 +30,13 @@ struct network_conf { gboolean connection_enabled; gboolean server_enabled; + gboolean security; char *iface_prefix; - struct connection_conf conn; - struct server_conf server; + char *panu_script; + char *gn_script; + char *nap_script; + char *gn_iface; + char *nap_iface; }; int network_init(DBusConnection *conn, struct network_conf *service_conf); diff --git a/network/network.conf b/network/network.conf index 66136002..29e77fff 100644 --- a/network/network.conf +++ b/network/network.conf @@ -12,27 +12,26 @@ # (up to 16 characters) InterfacePrefix=bnep%d -[Connection] +# Disable link encryption: default=false +DisableSecurity=false -# PAN user connection interface up script. default: -PANUScript=/usr/sbin/avahi-autoipd +[PANU Role] -# Group Network connection interface up script. default: -GNScript=/usr/sbin/avahi-autoipd +# PAN user connection interface up script. default:none +Script=avahi-autoipd -# Network Access Point connection interface up script. default: -NAPScript=/sbin/dhclient +[GN Role] -[Server] +# Network Interface name for Group Network server. default:pan1 +Interface=pan0 -# Network Interface name for PAN user server. default:pan0 -PANUInterface=pan0 +# Group Network connection interface up script. default:none +Script=avahi-autoipd -# Network Interface name for Group Network server. default:pan1 -GNInterface=pan1 +[NAP Role] # Network Interface name for Network Access Point server. default:pan2 -NAPInterface=pan2 +Interface=pan1 -# Disable link encryption: default=false -DisableSecurity=false +# Network Access Point connection interface up script. default:none +Script=/sbin/dhclient diff --git a/network/server.c b/network/server.c index b1257760..80ab49a1 100644 --- a/network/server.c +++ b/network/server.c @@ -74,7 +74,6 @@ struct setup_session { /* Main server structure */ struct network_server { bdaddr_t src; /* Bluetooth Local Address */ - char *bridge; /* Bridge interface */ char *iface; /* Routing interface */ char *name; /* Server service name */ char *range; /* IP Address range */ @@ -86,7 +85,6 @@ struct network_server { GSList *clients; /* Active connections */ }; -static struct server_conf *conf = NULL; static GIOChannel *bnep_io = NULL; static DBusConnection *connection = NULL; static GSList *setup_sessions = NULL; @@ -304,6 +302,7 @@ static void authorization_callback(DBusPendingCall *pcall, void *data) char path[MAX_PATH_LENGTH], devname[16]; uint16_t response = BNEP_CONN_NOT_ALLOWED; DBusError derr; + const char *bridge; if (!g_slist_find(setup_sessions, s)) { dbus_message_unref(reply); @@ -337,14 +336,18 @@ static void authorization_callback(DBusPendingCall *pcall, void *data) info("Authorization succedded. New connection: %s", devname); response = BNEP_SUCCESS; - if (bridge_add_interface(ns->bridge, devname) < 0) { + if (bridge_add_interface(ns->id, devname) < 0) { error("Can't add %s to the bridge: %s(%d)", devname, strerror(errno), errno); goto failed; } - bnep_if_up(devname, NULL); - bnep_if_up(ns->bridge, NULL); + bridge = bridge_get_name(ns->id); + if (bridge) { + bnep_if_up(devname, 0); + bnep_if_up(bridge, ns->id); + } else + bnep_if_up(devname, ns->id); ns->clients = g_slist_append(ns->clients, g_strdup(s->address)); @@ -594,8 +597,7 @@ static gboolean connect_event(GIOChannel *chan, return TRUE; } -int server_init(DBusConnection *conn, const char *iface_prefix, - struct server_conf *server_conf) +int server_init(DBusConnection *conn, const char *iface_prefix) { struct l2cap_options l2o; struct sockaddr_l2 l2a; @@ -656,7 +658,6 @@ int server_init(DBusConnection *conn, const char *iface_prefix, } connection = dbus_connection_ref(conn); - conf = server_conf; prefix = iface_prefix; bnep_io = g_io_channel_unix_new(sk); @@ -687,7 +688,6 @@ void server_exit() dbus_connection_unref(connection); connection = NULL; - conf = NULL; } static uint32_t add_server_record(struct network_server *ns) @@ -1195,13 +1195,6 @@ int server_register(const char *path, bdaddr_t *src, uint16_t id) ns->name = g_strdup("BlueZ PANU service"); ns->path = g_strdup(path); - ns->id = id; - if (id == BNEP_SVC_NAP) - ns->bridge = conf->nap_iface; - else if (id == BNEP_SVC_GN) - ns->bridge = conf->gn_iface; - else - ns->bridge = conf->panu_iface; bacpy(&ns->src, src); info("Registered server path:%s", path); @@ -1220,12 +1213,6 @@ int server_register_from_file(const char *path, const bdaddr_t *src, bacpy(&ns->src, src); ns->path = g_strdup(path); ns->id = id; - if (id == BNEP_SVC_NAP) - ns->bridge = conf->nap_iface; - else if (id == BNEP_SVC_GN) - ns->bridge = conf->gn_iface; - else - ns->bridge = conf->panu_iface; ns->name = textfile_get(filename, "name"); if (!ns->name) { /* Name is mandatory */ diff --git a/network/server.h b/network/server.h index 233c7837..9f49cad7 100644 --- a/network/server.h +++ b/network/server.h @@ -21,15 +21,7 @@ * */ -struct server_conf { - char *panu_iface; - char *gn_iface; - char *nap_iface; - gboolean disable_security; -}; - -int server_init(DBusConnection *conn, const char *iface_prefix, - struct server_conf *server_conf); +int server_init(DBusConnection *conn, const char *iface_prefix); void server_exit(); int server_register(const char *path, bdaddr_t *src, uint16_t id); int server_register_from_file(const char *path, const bdaddr_t *src, -- cgit