]> git.meshlink.io Git - catta/blobdiff - avahi-autoipd/main.c
Rename GA's error to GA_ERROR for more consistency with other glib bindings
[catta] / avahi-autoipd / main.c
index 26a516ae98ae4c79c748013a245a4a589a781b85..d4b9b6deb0ab292afb6579e983c07aa7334d512e 100644 (file)
 
 #ifndef __linux__
 #include <pcap.h>
+
+/* Old versions of PCAP defined it as D_IN */
+#ifndef PCAP_D_IN
+#define PCAP_D_IN D_IN
+#endif
+
 #endif
 
 #include <avahi-common/malloc.h>
@@ -77,6 +83,8 @@
 #include "main.h"
 #include "iface.h"
 
+
+
 /* An implementation of RFC 3927 */
 
 /* Constants from the RFC */
@@ -136,6 +144,7 @@ static int no_chroot = 0;
 #endif
 static int no_drop_root = 0;
 static int wrote_pid_file = 0;
+static char *action_script = NULL;
 
 static enum {
     DAEMON_RUN,
@@ -248,13 +257,16 @@ fail:
 static int save_address(const char *fn, uint32_t addr) {
     FILE *f;
     char buf[32];
+    mode_t u;
 
     assert(fn);
-    
+
+    u = umask(0033);
     if (!(f = fopen(fn, "w"))) {
         daemon_log(LOG_ERR, "fopen() failed: %s", strerror(errno));
         goto fail;
     }
+    umask(u);
 
     fprintf(f, "%s\n", inet_ntop(AF_INET, &addr, buf, sizeof (buf)));
     fclose(f);
@@ -265,6 +277,8 @@ fail:
     if (f)
         fclose(f);
 
+    umask(u);
+    
     return -1;
 }
 
@@ -504,7 +518,7 @@ static int send_packet(int fd, int iface, ArpPacket *packet, size_t packet_len)
     sa.sll_halen = ETHER_ADDRLEN;
     memset(sa.sll_addr, 0xFF, ETHER_ADDRLEN);
 
-    if (sendto(fd, packet, packet_len, 0, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
+    if (sendto(fd, packet->ether_payload, packet_len, 0, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
         daemon_log(LOG_ERR, "sendto() failed: %s", strerror(errno));
         return -1;
     }
@@ -767,7 +781,6 @@ int is_ll_address(uint32_t addr) {
         ((ntohl(addr) & 0x0000FF00) != 0xFF00);
 }
 
-
 static struct timeval *elapse_time(struct timeval *tv, unsigned msec, unsigned jitter) {
     assert(tv);
 
@@ -836,7 +849,7 @@ static FILE* fork_dispatcher(void) {
             }
             
             if (daemon_exec("/", &k,
-                            AVAHI_IPCONF_SCRIPT, AVAHI_IPCONF_SCRIPT,
+                            action_script, action_script,
                             callout_event_table[info.event],
                             name,
                             inet_ntop(AF_INET, &info.address, buf, sizeof(buf)), NULL) < 0) {
@@ -998,7 +1011,7 @@ static int drop_privs(void) {
 #elif defined(HAVE_SETREGID)
         r = setregid(gr->gr_gid, gr->gr_gid);
 #else
-#error "No API to drop priviliges"
+#error "No API to drop privileges"
 #endif
 
         if (r < 0) {
@@ -1014,7 +1027,7 @@ static int drop_privs(void) {
 #elif defined(HAVE_SETREUID)
         r = setreuid(pw->pw_uid, pw->pw_uid);
 #else
-#error "No API to drop priviliges"
+#error "No API to drop privileges"
 #endif
         
         if (r < 0) {
@@ -1028,7 +1041,7 @@ static int drop_privs(void) {
         
         daemon_log(LOG_INFO, "Successfully dropped root privileges.");
     }
-    
+
     return 0;
 }
 
@@ -1037,7 +1050,7 @@ static int loop(int iface, uint32_t addr) {
         FD_ARP,
         FD_IFACE,
         FD_SIGNAL,
-        FD_MAX,
+        FD_MAX
     };
 
     int fd = -1, ret = -1;
@@ -1203,13 +1216,13 @@ static int loop(int iface, uint32_t addr) {
                 if (info.sender_ip_address == addr) {
                     /* Normal conflict */
                     conflict = 1;
-                    daemon_log(LOG_INFO, "Recieved conflicting normal ARP packet.");
+                    daemon_log(LOG_INFO, "Received conflicting normal ARP packet.");
                 } else if (state == STATE_WAITING_PROBE || state == STATE_PROBING || state == STATE_WAITING_ANNOUNCE) {
                     /* Probe conflict */
                     conflict = info.target_ip_address == addr && memcmp(hw_address, info.sender_hw_address, ETHER_ADDRLEN);
 
                     if (conflict)
-                        daemon_log(LOG_INFO, "Recieved conflicting probe ARP packet.");
+                        daemon_log(LOG_INFO, "Received conflicting probe ARP packet.");
                 }
 
                 if (conflict) {
@@ -1413,11 +1426,13 @@ static void help(FILE *f, const char *a0) {
             "    -D --daemonize      Daemonize after startup\n"
             "    -s --syslog         Write log messages to syslog(3) instead of STDERR\n"
             "    -k --kill           Kill a running daemon\n"
-            "    -r --refresh        Request a running daemon to refresh it's IP address\n"
+            "    -r --refresh        Request a running daemon refresh its IP address\n"
             "    -c --check          Return 0 if a daemon is already running\n"
             "    -V --version        Show version\n"
             "    -S --start=ADDRESS  Start with this address from the IPv4LL range\n"
             "                        169.254.0.0/16\n"
+            "    -t --script=script  Action script to run (defaults to\n"
+            "                        "AVAHI_IPCONF_SCRIPT")\n"
             "    -w --wait           Wait until an address has been acquired before\n"
             "                        daemonizing\n"
             "       --force-bind     Assign an IPv4LL address even if a routable address\n"
@@ -1453,6 +1468,7 @@ static int parse_command_line(int argc, char *argv[]) {
         { "check",         no_argument,       NULL, 'c' },
         { "version",       no_argument,       NULL, 'V' },
         { "start",         required_argument, NULL, 'S' },
+        { "script",        required_argument, NULL, 't' },
         { "wait",          no_argument,       NULL, 'w' },
         { "force-bind",    no_argument,       NULL, OPTION_FORCE_BIND },
         { "no-drop-root",  no_argument,       NULL, OPTION_NO_DROP_ROOT },
@@ -1464,7 +1480,7 @@ static int parse_command_line(int argc, char *argv[]) {
         { NULL, 0, NULL, 0 }
     };
 
-    while ((c = getopt_long(argc, argv, "hDskrcVS:w", long_options, NULL)) >= 0) {
+    while ((c = getopt_long(argc, argv, "hDskrcVS:t:w", long_options, NULL)) >= 0) {
 
         switch(c) {
             case 's':
@@ -1495,6 +1511,10 @@ static int parse_command_line(int argc, char *argv[]) {
                     return -1;
                 }
                 break;
+            case 't':
+                avahi_free(action_script);
+                action_script = avahi_strdup(optarg);
+                break;
             case 'w':
                 wait_for_address = 1;
                 break;
@@ -1543,6 +1563,9 @@ static int parse_command_line(int argc, char *argv[]) {
         fprintf(stderr, "Too many arguments\n");
         return -1;
     }
+
+    if (!action_script)
+        action_script = avahi_strdup(AVAHI_IPCONF_SCRIPT);
         
     return 0;
 }
@@ -1674,6 +1697,7 @@ finish:
     avahi_free(pid_file_name);
     avahi_free(argv0);
     avahi_free(interface_name);
+    avahi_free(action_script);
 
     return r;
 }