]> git.meshlink.io Git - catta/blobdiff - avahi-dnsconfd/main.c
* Compile tests only when --enable-tests was specified on the configure command line
[catta] / avahi-dnsconfd / main.c
index 26b479ec39544b984615cffb25290fb0c698b0ac..84b0f6873cd076696c689ba85d504ecf57794084 100644 (file)
@@ -42,6 +42,7 @@
 
 #include <avahi-common/llist.h>
 #include <avahi-common/malloc.h>
+#include <avahi-common/address.h>
 
 #include <libdaemon/dfork.h>
 #include <libdaemon/dsignal.h>
@@ -60,8 +61,6 @@ static enum {
     BROWSING
 } state = ACKWAIT;
 
-static int quit = 0;
-
 static enum {
     DAEMON_RUN,
     DAEMON_KILL,
@@ -71,13 +70,18 @@ static enum {
     DAEMON_CHECK
 } command = DAEMON_RUN;
 
+static int quit = 0;
 static int daemonize = 0;
 
+#if !HAVE_DECL_ENVIRON
+extern char **environ;
+#endif
+
 typedef struct DNSServerInfo DNSServerInfo;
 
 struct DNSServerInfo {
-    int interface;
-    int protocol;
+    AvahiIfIndex interface;
+    AvahiProtocol protocol;
     char *address;
     AVAHI_LLIST_FIELDS(DNSServerInfo, servers);
 };
@@ -93,7 +97,7 @@ static void server_info_free(DNSServerInfo *i) {
     avahi_free(i);
 }
 
-static DNSServerInfo* get_server_info(int interface, int protocol, const char *address) {
+static DNSServerInfo* get_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
     DNSServerInfo *i;
     assert(address);
 
@@ -106,7 +110,7 @@ static DNSServerInfo* get_server_info(int interface, int protocol, const char *a
     return NULL;
 }
 
-static DNSServerInfo* new_server_info(int interface, int protocol, const char *address) {
+static DNSServerInfo* new_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
     DNSServerInfo *i;
     
     assert(address);
@@ -192,7 +196,7 @@ static ssize_t loop_write(int fd, const void*data, size_t size) {
     return ret;
 }
 
-static char *concat_dns_servers(int interface) {
+static char *concat_dns_servers(AvahiIfIndex interface) {
     DNSServerInfo *i;
     char *r = NULL;
     
@@ -212,37 +216,6 @@ static char *concat_dns_servers(int interface) {
     return r;
 }
 
-static char *getifname(int interface, char *name, size_t len) {
-    int fd = -1;
-    char *ret = NULL;
-    struct ifreq ifr;
-
-    assert(interface >= 0);
-    
-    if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
-        daemon_log(LOG_ERR, "socket(): %s", strerror(errno));
-        goto finish;
-    }
-
-    memset(&ifr, 0, sizeof(ifr));
-    ifr.ifr_ifindex = interface;
-    
-    if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
-        daemon_log(LOG_ERR, "SIOCGIFNAME: %s\n", strerror(errno));
-        goto finish;
-    }
-
-    strncpy(name, ifr.ifr_name, len-1);
-    name[len-1] = 0;
-    ret = name;
-    
-finish:
-    if (fd >= 0)
-        close(fd);
-    
-    return ret;
-}
-
 static void set_env(const char *name, const char *value) {
     char **e;
     size_t l;
@@ -257,7 +230,7 @@ static void set_env(const char *name, const char *value) {
         if (strlen(*e) < l+1)
             continue;
         
-        if (strncmp(*e, name, l) != 0 || *e[l] != '=')
+        if (strncmp(*e, name, l) != 0 || (*e)[l] != '=')
             continue;
 
         /* We simply free the record, sicne we know that we created it previously */
@@ -269,15 +242,15 @@ static void set_env(const char *name, const char *value) {
     assert(0);
 }
 
-static void run_script(int new, int interface, int protocol, const char *address) {
+static void run_script(int new, AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
     char *p;
     int ret;
     char ia[16], pa[16];
-    char name[IFNAMSIZ+1];
+    char name[IF_NAMESIZE];
 
     assert(interface > 0);
 
-    if (!getifname(interface, name, sizeof(name))) 
+    if (!if_indextoname(interface, name)) 
         return;
     
     p = concat_dns_servers(interface);
@@ -290,10 +263,10 @@ static void run_script(int new, int interface, int protocol, const char *address
 
     set_env(ENV_INTERFACE, name);
     
-    snprintf(ia, sizeof(ia), "%i", interface);
-    snprintf(pa, sizeof(pa), "%i", protocol);
+    snprintf(ia, sizeof(ia), "%i", (int) interface);
+    snprintf(pa, sizeof(pa), "%i", (int) protocol);
 
-    if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, NULL) < 0)
+    if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, avahi_proto_to_string(protocol), NULL) < 0)
         daemon_log(LOG_WARNING, "Failed to run script");
     else if (ret != 0)
         daemon_log(LOG_WARNING, "Script returned with non-zero exit code %i", ret);
@@ -311,9 +284,9 @@ static int new_line(const char *l) {
         daemon_log(LOG_INFO, "Successfully connected to Avahi daemon.");
         state = BROWSING;
     } else {
-        int interface;
-        int protocol;
-        int port;
+        AvahiIfIndex interface;
+        AvahiProtocol protocol;
+        int i_interface, i_protocol, port;
         char a[64];
         
         assert(state == BROWSING); 
@@ -323,16 +296,19 @@ static int new_line(const char *l) {
             return -1;
         }
 
-        if (sscanf(l+1, "%i %i %64s %i", &interface, &protocol, a, &port) != 4) {
+        if (sscanf(l+1, "%i %i %64s %i", &i_interface, &i_protocol, a, &port) != 4) {
             daemon_log(LOG_ERR, "Failed to parse browsing line: %s", l);
             return -1;
         }
 
+        interface = (AvahiIfIndex) i_interface;
+        protocol = (AvahiProtocol) i_protocol;
+
         if (*l == '>') {
             if (port != 53)
                 daemon_log(LOG_WARNING, "DNS server with port address != 53 found, ignoring");
             else {
-                daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%u)", a, interface, protocol);
+                daemon_log(LOG_INFO, "New DNS Server %s (interface: %i.%s)", a, interface, avahi_proto_to_string(protocol));
                 new_server_info(interface, protocol, a);
                 run_script(1, interface, protocol, a);
             }
@@ -341,7 +317,7 @@ static int new_line(const char *l) {
 
             if (port == 53) 
                 if ((i = get_server_info(interface, protocol, a))) {
-                    daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%u)", a, interface, protocol);
+                    daemon_log(LOG_INFO, "DNS Server %s removed (interface: %i.%s)", a, interface, avahi_proto_to_string(protocol));
                     server_info_free(i);
                     run_script(0, interface, protocol, a);
                 }
@@ -375,8 +351,8 @@ fail:
 
 static void free_dns_server_info_list(void) {
     while (servers) {
-        int interface = servers->interface;
-        int protocol = servers->protocol;
+        AvahiIfIndex interface = servers->interface;
+        AvahiProtocol protocol = servers->protocol;
         char *address = avahi_strdup(servers->address);
         server_info_free(servers);