*
* Connection Manager
*
- * Copyright (C) 2007 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
#include <config.h>
#endif
-#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include "connman.h"
-struct rtnl_data {
- unsigned ifi_flags;
-};
+static GStaticRWLock rtnl_lock = G_STATIC_RW_LOCK_INIT;
+static GSList *rtnl_list = NULL;
-static struct rtnl_data *get_rtnl_data(struct connman_iface *iface)
+static gint compare_priority(gconstpointer a, gconstpointer b)
{
- if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
- return NULL;
+ const struct connman_rtnl *rtnl1 = a;
+ const struct connman_rtnl *rtnl2 = b;
- if (iface->rtnl_data == NULL)
- iface->rtnl_data = g_try_new0(struct rtnl_data, 1);
+ return rtnl2->priority - rtnl1->priority;
+}
+
+/**
+ * connman_rtnl_register:
+ * @rtnl: RTNL module
+ *
+ * Register a new RTNL module
+ *
+ * Returns: %0 on success
+ */
+int connman_rtnl_register(struct connman_rtnl *rtnl)
+{
+ DBG("rtnl %p name %s", rtnl, rtnl->name);
+
+ g_static_rw_lock_writer_lock(&rtnl_lock);
+
+ rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
+ compare_priority);
- return iface->rtnl_data;
+ g_static_rw_lock_writer_unlock(&rtnl_lock);
+
+ return 0;
+}
+
+/**
+ * connman_rtnl_unregister:
+ * @rtnl: RTNL module
+ *
+ * Remove a previously registered RTNL module
+ */
+void connman_rtnl_unregister(struct connman_rtnl *rtnl)
+{
+ DBG("rtnl %p name %s", rtnl, rtnl->name);
+
+ g_static_rw_lock_writer_lock(&rtnl_lock);
+
+ rtnl_list = g_slist_remove(rtnl_list, rtnl);
+
+ g_static_rw_lock_writer_unlock(&rtnl_lock);
+}
+
+static void process_link_flags(int index, short flags)
+{
+ GSList *list;
+
+ DBG("idex %d", index);
+
+ g_static_rw_lock_reader_lock(&rtnl_lock);
+
+ for (list = rtnl_list; list; list = list->next) {
+ struct connman_rtnl *rtnl = list->data;
+
+ if (rtnl->link_flags)
+ rtnl->link_flags(index, flags);
+ }
+
+ g_static_rw_lock_reader_unlock(&rtnl_lock);
}
static inline void print_inet(struct rtattr *attr, const char *name, int family)
if (family == AF_INET) {
struct in_addr addr;
addr = *((struct in_addr *) RTA_DATA(attr));
- printf(" attr %s (len %jd) %s\n",
+ DBG(" attr %s (len %jd) %s\n",
name, RTA_PAYLOAD(attr), inet_ntoa(addr));
} else
- printf(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
+ DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
}
static inline void print_char(struct rtattr *attr, const char *name)
{
- printf(" attr %s (len %jd) %s\n", name, RTA_PAYLOAD(attr),
+ DBG(" attr %s (len %jd) %s\n", name, RTA_PAYLOAD(attr),
(char *) RTA_DATA(attr));
}
static inline void print_byte(struct rtattr *attr, const char *name)
{
- printf(" attr %s (len %jd) 0x%02x\n", name, RTA_PAYLOAD(attr),
+ DBG(" attr %s (len %jd) 0x%02x\n", name, RTA_PAYLOAD(attr),
*((unsigned char *) RTA_DATA(attr)));
}
static inline void print_attr(struct rtattr *attr, const char *name)
{
if (name)
- printf(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
+ DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
else
- printf(" attr %d (len %jd)\n",
+ DBG(" attr %d (len %jd)\n",
attr->rta_type, RTA_PAYLOAD(attr));
}
static void rtnl_link(struct nlmsghdr *hdr)
{
- struct connman_iface *iface;
- struct rtnl_data *data;
struct ifinfomsg *msg;
struct rtattr *attr;
int bytes;
DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
- iface = __connman_iface_find(msg->ifi_index);
- if (iface == NULL)
- return;
-
- data = get_rtnl_data(iface);
- if (data == NULL)
- return;
-
- if ((data->ifi_flags & IFF_RUNNING) != (msg->ifi_flags & IFF_RUNNING)) {
- if (msg->ifi_flags & IFF_RUNNING)
- connman_iface_indicate_carrier_on(iface);
- else
- connman_iface_indicate_carrier_off(iface);
- }
-
- if ((data->ifi_flags & IFF_UP) != (msg->ifi_flags & IFF_UP)) {
- if (msg->ifi_flags & IFF_UP)
- connman_iface_indicate_enabled(iface);
- else
- connman_iface_indicate_disabled(iface);
- }
-
- data->ifi_flags = msg->ifi_flags;
-
for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
attr = RTA_NEXT(attr, bytes)) {
switch (attr->rta_type) {
print_attr(attr, "master");
break;
case IFLA_WIRELESS:
- if (iface->driver->rtnl_wireless)
- iface->driver->rtnl_wireless(iface,
- RTA_DATA(attr), RTA_PAYLOAD(attr));
+ print_attr(attr, "wireless");
break;
case IFLA_PROTINFO:
print_attr(attr, "protinfo");
break;
}
}
+
+ process_link_flags(msg->ifi_index, msg->ifi_flags);
}
static void rtnl_addr(struct nlmsghdr *hdr)
{
- struct connman_iface *iface;
- struct rtnl_data *data;
struct ifaddrmsg *msg;
struct rtattr *attr;
int bytes;
DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
- iface = __connman_iface_find(msg->ifa_index);
- if (iface == NULL)
- return;
-
- data = get_rtnl_data(iface);
- if (data == NULL)
- return;
-
for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
attr = RTA_NEXT(attr, bytes)) {
switch (attr->rta_type) {
channel = g_io_channel_unix_new(sk);
g_io_channel_set_close_on_unref(channel, TRUE);
- g_io_add_watch(channel,
- G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
- netlink_event, NULL);
+ g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
+ netlink_event, NULL);
return 0;
}