From 212ac19139a303448076d3699428c375c5439e19 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 13 Dec 2008 19:50:01 +0100 Subject: [PATCH] Convert IPv4 setting to use new resolver framework --- plugins/Makefile.am | 4 ++-- plugins/ipv4.c | 25 ++++++++++++++++------ plugins/resolvconf.c | 60 ++++++++++++++++++---------------------------------- plugins/resolvfile.c | 48 ++++++++++++++++++----------------------- 4 files changed, 61 insertions(+), 76 deletions(-) diff --git a/plugins/Makefile.am b/plugins/Makefile.am index b9011f9..c4af117 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -21,9 +21,9 @@ dhclient_la_SOURCES = dhclient.c inet.h inet.c dhclient_la_CFLAGS = @GLIB_CFLAGS@ @GDBUS_CFLAGS@ -DDHCLIENT=\"@DHCLIENT@\" \ -DSTATEDIR=\""$(statedir)"\" -DSCRIPTDIR=\""$(scriptdir)"\" -ipv4_la_SOURCES = ipv4.c +ipv4_la_SOURCES = ipv4.c inet.h inet.c -resolvconf_la_SOURCES = resolvconf.c inet.h inet.c +resolvconf_la_SOURCES = resolvconf.c resolvfile_la_SOURCES = resolvfile.c diff --git a/plugins/ipv4.c b/plugins/ipv4.c index 173c691..ddff153 100644 --- a/plugins/ipv4.c +++ b/plugins/ipv4.c @@ -37,8 +37,11 @@ #include #include +#include #include +#include "inet.h" + enum connman_ipv4_method { CONNMAN_IPV4_METHOD_UNKNOWN = 0, CONNMAN_IPV4_METHOD_OFF = 1, @@ -57,7 +60,7 @@ struct connman_ipv4 { }; static int set_ipv4(struct connman_element *element, - struct connman_ipv4 *ipv4) + struct connman_ipv4 *ipv4, const char *nameserver) { struct ifreq ifr; struct rtentry rt; @@ -131,6 +134,8 @@ static int set_ipv4(struct connman_element *element, return -1; } + connman_resolver_append(ifr.ifr_name, NULL, nameserver); + return 0; } @@ -156,6 +161,8 @@ static int clear_ipv4(struct connman_element *element) DBG("ifname %s", ifr.ifr_name); + connman_resolver_remove_all(ifr.ifr_name); + addr = (struct sockaddr_in *) &ifr.ifr_addr; addr->sin_family = AF_INET; addr->sin_addr.s_addr = INADDR_ANY; @@ -175,9 +182,10 @@ static int clear_ipv4(struct connman_element *element) static int ipv4_probe(struct connman_element *element) { - struct connman_element *resolver; + struct connman_element *connection; struct connman_ipv4 ipv4; const char *address = NULL, *netmask = NULL, *gateway = NULL; + const char *nameserver = NULL; DBG("element %p name %s", element, element->name); @@ -188,6 +196,9 @@ static int ipv4_probe(struct connman_element *element) connman_element_get_value(element, CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway); + connman_element_get_value(element, + CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver); + DBG("address %s", address); DBG("netmask %s", netmask); DBG("gateway %s", gateway); @@ -200,14 +211,14 @@ static int ipv4_probe(struct connman_element *element) ipv4.netmask.s_addr = inet_addr(netmask); ipv4.gateway.s_addr = inet_addr(gateway); - set_ipv4(element, &ipv4); + set_ipv4(element, &ipv4, nameserver); - resolver = connman_element_create(NULL); + connection = connman_element_create(NULL); - resolver->type = CONNMAN_ELEMENT_TYPE_RESOLVER; - resolver->index = element->index; + connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION; + connection->index = element->index; - connman_element_register(resolver, element); + connman_element_register(connection, element); return 0; } diff --git a/plugins/resolvconf.c b/plugins/resolvconf.c index e1d5337..41f649c 100644 --- a/plugins/resolvconf.c +++ b/plugins/resolvconf.c @@ -23,41 +23,31 @@ #include #endif +#include #include #include #include -#include +#include #include -#include "inet.h" +#include #define RESOLVCONF "/sbin/resolvconf" -static int resolvconf_probe(struct connman_element *element) +static int resolvconf_append(const char *interface, const char *domain, + const char *server) { - const char *nameserver = NULL; - struct connman_element *internet; - gchar *cmd, *name; + char *cmd; int err; - DBG("element %p name %s", element, element->name); + DBG("interface %s server %s", interface, server); if (access(RESOLVCONF, X_OK) < 0) return -errno; - connman_element_get_value(element, - CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver); - - if (nameserver == NULL) - return -EINVAL; - - name = inet_index2name(element->index); - cmd = g_strdup_printf("echo \"nameserver %s\" | %s -a %s", - nameserver, RESOLVCONF, name); - - g_free(name); + server, RESOLVCONF, interface); DBG("%s", cmd); @@ -66,27 +56,18 @@ static int resolvconf_probe(struct connman_element *element) g_free(cmd); - internet = connman_element_create(NULL); - - internet->type = CONNMAN_ELEMENT_TYPE_CONNECTION; - - connman_element_register(internet, element); - - return 0; + return err; } -static void resolvconf_remove(struct connman_element *element) +static int resolvconf_remove(const char *interface, const char *domain, + const char *server) { - gchar *cmd, *name; + char *cmd; int err; - DBG("element %p name %s", element, element->name); - - name = inet_index2name(element->index); + DBG("interface %s server %s", interface, server); - cmd = g_strdup_printf("%s -d %s", RESOLVCONF, name); - - g_free(name); + cmd = g_strdup_printf("%s -d %s", RESOLVCONF, interface); DBG("%s", cmd); @@ -94,24 +75,25 @@ static void resolvconf_remove(struct connman_element *element) err = 0; g_free(cmd); + + return err; } -static struct connman_driver resolvconf_driver = { +static struct connman_resolver resolvconf_resolver = { .name = "resolvconf", - .type = CONNMAN_ELEMENT_TYPE_RESOLVER, - .priority = CONNMAN_DRIVER_PRIORITY_HIGH, - .probe = resolvconf_probe, + .priority = CONNMAN_RESOLVER_PRIORITY_DEFAULT, + .append = resolvconf_append, .remove = resolvconf_remove, }; static int resolvconf_init(void) { - return connman_driver_register(&resolvconf_driver); + return connman_resolver_register(&resolvconf_resolver); } static void resolvconf_exit(void) { - connman_driver_unregister(&resolvconf_driver); + connman_resolver_unregister(&resolvconf_resolver); } CONNMAN_PLUGIN_DEFINE(resolvconf, "Name resolver plugin", VERSION, diff --git a/plugins/resolvfile.c b/plugins/resolvfile.c index deb39c9..440656a 100644 --- a/plugins/resolvfile.c +++ b/plugins/resolvfile.c @@ -24,29 +24,25 @@ #endif #include +#include #include #include #include #include #include -#include +#include #include -static int resolvfile_probe(struct connman_element *element) +#include + +static int resolvfile_append(const char *interface, const char *domain, + const char *server) { - const char *nameserver = NULL; - struct connman_element *internet; - gchar *cmd; + char *cmd; int fd, len, err; - DBG("element %p name %s", element, element->name); - - connman_element_get_value(element, - CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver); - - if (nameserver == NULL) - return -EINVAL; + DBG("server %s", server); fd = open("/etc/resolv.conf", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); @@ -55,7 +51,7 @@ static int resolvfile_probe(struct connman_element *element) err = ftruncate(fd, 0); - cmd = g_strdup_printf("nameserver %s\n", nameserver); + cmd = g_strdup_printf("nameserver %s\n", server); len = write(fd, cmd, strlen(cmd)); @@ -63,36 +59,32 @@ static int resolvfile_probe(struct connman_element *element) close(fd); - internet = connman_element_create(NULL); - - internet->type = CONNMAN_ELEMENT_TYPE_CONNECTION; - - connman_element_register(internet, element); - return 0; } -static void resolvfile_remove(struct connman_element *element) +static int resolvfile_remove(const char *interface, const char *domain, + const char *server) { - DBG("element %p name %s", element, element->name); + DBG("server %s", server); + + return 0; } -static struct connman_driver resolvfile_driver = { - .name = "resolvconf", - .type = CONNMAN_ELEMENT_TYPE_RESOLVER, - .priority = CONNMAN_DRIVER_PRIORITY_LOW, - .probe = resolvfile_probe, +static struct connman_resolver resolvfile_resolver = { + .name = "resolvfile", + .priority = CONNMAN_RESOLVER_PRIORITY_LOW, + .append = resolvfile_append, .remove = resolvfile_remove, }; static int resolvfile_init(void) { - return connman_driver_register(&resolvfile_driver); + return connman_resolver_register(&resolvfile_resolver); } static void resolvfile_exit(void) { - connman_driver_unregister(&resolvfile_driver); + connman_resolver_unregister(&resolvfile_resolver); } CONNMAN_PLUGIN_DEFINE(resolvfile, "Name resolver plugin", VERSION, -- 2.7.4