Remove usage of reader/write lock
[connman] / src / rtnl.c
index a8131d0..ece4f16 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  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
@@ -23,7 +23,6 @@
 #include <config.h>
 #endif
 
-#include <stdio.h>
 #include <unistd.h>
 #include <string.h>
 #include <sys/socket.h>
 
 #include "connman.h"
 
+static GSList *rtnl_list = NULL;
+
+static gint compare_priority(gconstpointer a, gconstpointer b)
+{
+       const struct connman_rtnl *rtnl1 = a;
+       const struct connman_rtnl *rtnl2 = b;
+
+       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);
+
+       rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
+                                                       compare_priority);
+
+       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);
+
+       rtnl_list = g_slist_remove(rtnl_list, rtnl);
+}
+
+static void process_newlink(unsigned short type, int index,
+                                       unsigned flags, unsigned change)
+{
+       GSList *list;
+
+       DBG("index %d", index);
+
+       for (list = rtnl_list; list; list = list->next) {
+               struct connman_rtnl *rtnl = list->data;
+
+               if (rtnl->newlink)
+                       rtnl->newlink(type, index, flags, change);
+       }
+}
+
+static void process_dellink(unsigned short type, int index,
+                                       unsigned flags, unsigned change)
+{
+       GSList *list;
+
+       DBG("index %d", index);
+
+       for (list = rtnl_list; list; list = list->next) {
+               struct connman_rtnl *rtnl = list->data;
+
+               if (rtnl->dellink)
+                       rtnl->dellink(type, index, flags, change);
+       }
+}
+
 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 %d) %s\n",
+               DBG("  attr %s (len %jd) %s\n",
                                name, RTA_PAYLOAD(attr), inet_ntoa(addr));
        } else
-               printf("  attr %s (len %d)\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 %d) %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 %d) 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 %d)\n", name, RTA_PAYLOAD(attr));
+               DBG("  attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
        else
-               printf("  attr %d (len %d)\n",
+               DBG("  attr %d (len %jd)\n",
                                        attr->rta_type, RTA_PAYLOAD(attr));
 }
 
 static void rtnl_link(struct nlmsghdr *hdr)
 {
-       struct connman_iface *iface;
+#if 0
        struct ifinfomsg *msg;
        struct rtattr *attr;
        int bytes;
@@ -81,21 +151,6 @@ static void rtnl_link(struct nlmsghdr *hdr)
 
        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;
-
-       if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
-               return;
-
-       if (iface->carrier != ((msg->ifi_flags & IFF_RUNNING) != 0)) {
-               iface->carrier = ((msg->ifi_flags & IFF_RUNNING) != 0);
-               if (iface->driver->rtnl_carrier)
-                       iface->driver->rtnl_carrier(iface, iface->carrier);
-               else
-                       connman_iface_indicate_carrier(iface, iface->carrier);
-       }
-
        for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
                                        attr = RTA_NEXT(attr, bytes)) {
                switch (attr->rta_type) {
@@ -130,9 +185,7 @@ static void rtnl_link(struct nlmsghdr *hdr)
                        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");
@@ -157,11 +210,39 @@ static void rtnl_link(struct nlmsghdr *hdr)
                        break;
                }
        }
+#endif
+}
+
+static void rtnl_newlink(struct nlmsghdr *hdr)
+{
+       struct ifinfomsg *msg;
+
+       msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
+
+       DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
+
+       process_newlink(msg->ifi_type, msg->ifi_index,
+                                       msg->ifi_flags, msg->ifi_change);
+
+       rtnl_link(hdr);
+}
+
+static void rtnl_dellink(struct nlmsghdr *hdr)
+{
+       struct ifinfomsg *msg;
+
+       msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
+
+       DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
+
+       process_dellink(msg->ifi_type, msg->ifi_index,
+                                       msg->ifi_flags, msg->ifi_change);
+
+       rtnl_link(hdr);
 }
 
 static void rtnl_addr(struct nlmsghdr *hdr)
 {
-       struct connman_iface *iface;
        struct ifaddrmsg *msg;
        struct rtattr *attr;
        int bytes;
@@ -171,13 +252,6 @@ static void rtnl_addr(struct nlmsghdr *hdr)
 
        DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
 
-       iface = __connman_iface_find(msg->ifa_index);
-       if (iface == NULL)
-               return;
-
-       if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
-               return;
-
        for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
                                        attr = RTA_NEXT(attr, bytes)) {
                switch (attr->rta_type) {
@@ -259,7 +333,7 @@ static void rtnl_route(struct nlmsghdr *hdr)
 
 static void rtnl_message(void *buf, size_t len)
 {
-       DBG("buf %p len %d", buf, len);
+       DBG("buf %p len %zd", buf, len);
 
        while (len > 0) {
                struct nlmsghdr *hdr = buf;
@@ -273,36 +347,42 @@ static void rtnl_message(void *buf, size_t len)
                                        hdr->nlmsg_flags, hdr->nlmsg_seq);
 
                switch (hdr->nlmsg_type) {
-               case NLMSG_DONE:
-                       DBG("done");
-                       return;
                case NLMSG_NOOP:
-                       DBG("noop");
-                       return;
-               case NLMSG_OVERRUN:
-                       DBG("overrun");
+                       DBG("NOOP");
                        return;
                case NLMSG_ERROR:
                        err = NLMSG_DATA(hdr);
-                       DBG("error %d (%s)", -err->error,
+                       DBG("ERROR %d (%s)", -err->error,
                                                strerror(-err->error));
                        return;
+               case NLMSG_DONE:
+                       DBG("DONE");
+                       return;
+               case NLMSG_OVERRUN:
+                       DBG("OVERRUN");
+                       return;
                case RTM_NEWLINK:
-                       rtnl_link(hdr);
+                       DBG("NEWLINK");
+                       rtnl_newlink(hdr);
                        break;
                case RTM_DELLINK:
-                       rtnl_link(hdr);
+                       DBG("DELLINK");
+                       rtnl_dellink(hdr);
                        break;
                case RTM_NEWADDR:
+                       DBG("NEWADDR");
                        rtnl_addr(hdr);
                        break;
                case RTM_DELADDR:
+                       DBG("DELADDR");
                        rtnl_addr(hdr);
                        break;
                case RTM_NEWROUTE:
+                       DBG("NEWROUTE");
                        rtnl_route(hdr);
                        break;
                case RTM_DELROUTE:
+                       DBG("DELROUTE");
                        rtnl_route(hdr);
                        break;
                default:
@@ -318,14 +398,12 @@ static void rtnl_message(void *buf, size_t len)
 static gboolean netlink_event(GIOChannel *chan,
                                GIOCondition cond, gpointer data)
 {
-       unsigned char buf[256];
+       unsigned char buf[4096];
        gsize len;
        GIOError err;
 
-       if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
-               g_io_channel_unref(chan);
+       if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
                return FALSE;
-       }
 
        memset(buf, 0, sizeof(buf));
 
@@ -333,7 +411,6 @@ static gboolean netlink_event(GIOChannel *chan,
        if (err) {
                if (err == G_IO_ERROR_AGAIN)
                        return TRUE;
-               g_io_channel_unref(chan);
                return FALSE;
        }
 
@@ -349,7 +426,7 @@ int __connman_rtnl_send(const void *buf, size_t len)
        struct sockaddr_nl addr;
        int sk;
 
-       DBG("buf %p len %d", buf, len);
+       DBG("buf %p len %zd", buf, len);
 
        sk = g_io_channel_unix_get_fd(channel);
 
@@ -360,6 +437,26 @@ int __connman_rtnl_send(const void *buf, size_t len)
                        (struct sockaddr *) &addr, sizeof(addr));
 }
 
+int connman_rtnl_send_getlink(void)
+{
+       struct {
+               struct nlmsghdr hdr;
+               struct rtgenmsg msg;
+       } req;
+
+       DBG("");
+
+       memset(&req, 0, sizeof(req));
+       req.hdr.nlmsg_len = sizeof(req.hdr) + sizeof(req.msg);
+       req.hdr.nlmsg_type = RTM_GETLINK;
+       req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
+       req.hdr.nlmsg_pid = 0;
+       req.hdr.nlmsg_seq = 42;
+       req.msg.rtgen_family = AF_INET;
+
+       return __connman_rtnl_send(&req, sizeof(req));
+}
+
 int __connman_rtnl_init(void)
 {
        struct sockaddr_nl addr;
@@ -374,6 +471,7 @@ int __connman_rtnl_init(void)
        memset(&addr, 0, sizeof(addr));
        addr.nl_family = AF_NETLINK;
        addr.nl_groups = RTMGRP_LINK;
+       //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
        //addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
 
        if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
@@ -384,11 +482,8 @@ int __connman_rtnl_init(void)
        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_channel_unref(channel);
+       g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
+                                                       netlink_event, NULL);
 
        return 0;
 }
@@ -397,6 +492,7 @@ void __connman_rtnl_cleanup(void)
 {
        DBG("");
 
+       g_io_channel_shutdown(channel, TRUE, NULL);
        g_io_channel_unref(channel);
 
        channel = NULL;