]> git.meshlink.io Git - catta/blobdiff - avahi-dnsconfd/main.c
get rid of a lot of old svn cruft
[catta] / avahi-dnsconfd / main.c
index 078efc9adeaabede679e8c855a5fc72e8e00662e..6ae6c3afb6d8a726dc97862884e341556abb6ca6 100644 (file)
@@ -1,18 +1,16 @@
-/* $Id$ */
-
 /***
   This file is part of avahi.
+
   avahi is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.
+
   avahi is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
   Public License for more details.
+
   You should have received a copy of the GNU Lesser General Public
   License along with avahi; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 #include <config.h>
 #endif
 
-#include <sys/un.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/select.h>
 #include <sys/socket.h>
+#include <sys/un.h>
 #include <errno.h>
 #include <string.h>
 #include <stdio.h>
@@ -42,6 +40,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 +59,6 @@ static enum {
     BROWSING
 } state = ACKWAIT;
 
-static int quit = 0;
-
 static enum {
     DAEMON_RUN,
     DAEMON_KILL,
@@ -71,13 +68,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);
 };
@@ -88,12 +90,12 @@ static void server_info_free(DNSServerInfo *i) {
     assert(i);
 
     avahi_free(i->address);
-    
+
     AVAHI_LLIST_REMOVE(DNSServerInfo, servers, servers, 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,9 +108,9 @@ 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);
 
     i = avahi_new(DNSServerInfo, 1);
@@ -117,7 +119,7 @@ static DNSServerInfo* new_server_info(int interface, int protocol, const char *a
     i->address = avahi_strdup(address);
 
     AVAHI_LLIST_PREPEND(DNSServerInfo, servers, servers, i);
-    
+
     return i;
 }
 
@@ -125,7 +127,7 @@ static int set_cloexec(int fd) {
     int n;
 
     assert(fd >= 0);
-    
+
     if ((n = fcntl(fd, F_GETFD)) < 0)
         return -1;
 
@@ -148,7 +150,7 @@ static int open_socket(void) {
         daemon_log(LOG_ERR, "fcntl(): %s", strerror(errno));
         goto fail;
     }
-    
+
     memset(&sa, 0, sizeof(sa));
     sa.sun_family = AF_UNIX;
     strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
@@ -162,7 +164,7 @@ static int open_socket(void) {
     }
 
     return fd;
-    
+
 fail:
     if (fd >= 0)
         close(fd);
@@ -192,14 +194,24 @@ 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;
-    
+
     for (i = servers; i; i = i->servers_next)
         if (i->interface == interface || interface <= 0) {
+            DNSServerInfo *j;
             char *t;
 
+            /* Filter out double entries */
+            for (j = servers; j != i; j = j->servers_next)
+                if (j->interface == interface || interface <= 0)
+                    if (strcmp(i->address, j->address) == 0)
+                        break;
+
+            if (j != i)
+                continue;
+
             if (!r)
                 t = avahi_strdup(i->address);
             else
@@ -212,41 +224,10 @@ 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;
-    
+
     assert(name);
     assert(value);
 
@@ -256,7 +237,7 @@ static void set_env(const char *name, const char *value) {
         /* Search for the variable */
         if (strlen(*e) < l+1)
             continue;
-        
+
         if (strncmp(*e, name, l) != 0 || (*e)[l] != '=')
             continue;
 
@@ -269,31 +250,31 @@ 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);
     set_env(ENV_INTERFACE_DNS_SERVERS, p ? p : "");
-    avahi_free(p); 
+    avahi_free(p);
 
     p = concat_dns_servers(-1);
     set_env(ENV_DNS_SERVERS, p ? p : "");
-    avahi_free(p); 
+    avahi_free(p);
 
     set_env(ENV_INTERFACE, name);
-    
-    snprintf(ia, sizeof(ia), "%i", interface);
-    snprintf(pa, sizeof(pa), "%i", protocol);
 
-    if (daemon_exec("/", &ret, AVAHI_DNSCONF_SCRIPT, AVAHI_DNSCONF_SCRIPT, new ? "+" : "-", address, ia, pa, NULL) < 0)
+    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, 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,50 +292,53 @@ 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;
-        char a[64];
-        
-        assert(state == BROWSING); 
+        AvahiIfIndex interface;
+        AvahiProtocol protocol;
+        int i_interface, i_protocol, port;
+        char a[AVAHI_ADDRESS_STR_MAX];
+
+        assert(state == BROWSING);
 
         if (*l != '<' && *l != '>') {
             daemon_log(LOG_ERR, "Avahi sent us an invalid browsing line: %s", l);
             return -1;
         }
 
-        if (sscanf(l+1, "%i %i %64s %i", &interface, &protocol, a, &port) != 4) {
+        if (sscanf(l+1, "%i %i %39s %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);
             }
         } else {
             DNSServerInfo *i;
 
-            if (port == 53) 
+            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);
                 }
         }
 
     }
-    
+
     return 0;
 }
 
 static int do_connect(void) {
     int fd = -1;
-    
+
     if ((fd = open_socket()) < 0)
         goto fail;
 
@@ -369,17 +353,17 @@ static int do_connect(void) {
 fail:
     if (fd >= 0)
         close(fd);
-    
+
     return -1;
 }
 
 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);
-        
+
         run_script(0, interface, protocol, address);
         avahi_free(address);
     }
@@ -399,7 +383,7 @@ static void help(FILE *f, const char *argv0) {
 
 static int parse_command_line(int argc, char *argv[]) {
     int c;
-    
+
     static const struct option long_options[] = {
         { "help",      no_argument,       NULL, 'h' },
         { "daemonize", no_argument,       NULL, 'D' },
@@ -407,9 +391,9 @@ static int parse_command_line(int argc, char *argv[]) {
         { "version",   no_argument,       NULL, 'V' },
         { "refresh",   no_argument,       NULL, 'r' },
         { "check",     no_argument,       NULL, 'c' },
+        { NULL, 0, NULL, 0 }
     };
 
-    opterr = 0;
     while ((c = getopt_long(argc, argv, "hDkVrc", long_options, NULL)) >= 0) {
 
         switch(c) {
@@ -432,7 +416,6 @@ static int parse_command_line(int argc, char *argv[]) {
                 command = DAEMON_CHECK;
                 break;
             default:
-                fprintf(stderr, "Invalid command line argument: %c\n", c);
                 return -1;
         }
     }
@@ -441,7 +424,7 @@ static int parse_command_line(int argc, char *argv[]) {
         fprintf(stderr, "Too many arguments\n");
         return -1;
     }
-        
+
     return 0;
 }
 
@@ -451,14 +434,14 @@ static int run_daemon(void) {
     size_t buflen = 0;
 
     AVAHI_LLIST_HEAD_INIT(DNSServerInfo, servers);
-    
+
     daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0);
 
     /* Allocate some memory for our environment variables */
     putenv(avahi_strdup(ENV_INTERFACE"="));
     putenv(avahi_strdup(ENV_DNS_SERVERS"="));
     putenv(avahi_strdup(ENV_INTERFACE_DNS_SERVERS"="));
-    
+
     if ((fd = do_connect()) < 0)
         goto finish;
 
@@ -507,19 +490,19 @@ static int run_daemon(void) {
                 case SIGCHLD:
                     waitpid(-1, NULL, WNOHANG);
                     break;
-                    
+
                 case SIGHUP:
                     daemon_log(LOG_INFO, "Refreshing DNS Server list");
-                    
+
                     close(fd);
                     free_dns_server_info_list();
-                    
+
                     if ((fd = do_connect()) < 0)
                         goto finish;
-                    
+
                     break;
             }
-            
+
         } else if (FD_ISSET(fd, &rfds)) {
             ssize_t r;
             char *n;
@@ -545,27 +528,27 @@ static int run_daemon(void) {
             if (buflen >= sizeof(buf)-1) {
                 /* The incoming line is horribly long */
                 buf[sizeof(buf)-1] = 0;
-                
+
                 if (new_line(buf) < 0)
                     goto finish;
-                
+
                 buflen = 0;
             }
         }
     }
-    
+
 finish:
 
     free_dns_server_info_list();
 
     if (fd >= 0)
         close(fd);
-    
+
     daemon_signal_done();
 
     if (ret != 0 && daemonize)
         daemon_retval_send(1);
-    
+
     return ret;
 }
 
@@ -585,7 +568,7 @@ int main(int argc, char *argv[]) {
 
     daemon_pid_file_ident = daemon_log_ident = argv0;
     daemon_pid_file_proc = pid_file_proc;
-    
+
     if (parse_command_line(argc, argv) < 0)
         goto finish;
 
@@ -604,7 +587,7 @@ int main(int argc, char *argv[]) {
 
         if (daemonize) {
             daemon_retval_init();
-            
+
             if ((pid = daemon_fork()) < 0)
                 goto finish;
             else if (pid != 0) {
@@ -612,7 +595,7 @@ int main(int argc, char *argv[]) {
                 /** Parent **/
 
                 if ((ret = daemon_retval_wait(20)) < 0) {
-                    daemon_log(LOG_ERR, "Could not recieve return value from daemon process.");
+                    daemon_log(LOG_ERR, "Could not receive return value from daemon process.");
                     goto finish;
                 }
 
@@ -640,18 +623,18 @@ int main(int argc, char *argv[]) {
         r = 0;
     } else if (command == DAEMON_HELP) {
         help(stdout, argv0);
-        
+
         r = 0;
     } else if (command == DAEMON_VERSION) {
         printf("%s "PACKAGE_VERSION"\n", argv0);
-        
+
         r = 0;
     } else if (command == DAEMON_KILL) {
         if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
             daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
             goto finish;
         }
-        
+
         r = 0;
     } else if (command == DAEMON_REFRESH) {
         if (daemon_pid_file_kill(SIGHUP) < 0) {
@@ -668,7 +651,7 @@ finish:
 
     if (daemonize)
         daemon_retval_done();
-    
+
     if (wrote_pid_file)
         daemon_pid_file_remove();