]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Re format for standard tabs.
authorRoy Marples <roy@marples.name>
Wed, 16 Jan 2008 16:38:47 +0000 (16:38 +0000)
committerRoy Marples <roy@marples.name>
Wed, 16 Jan 2008 16:38:47 +0000 (16:38 +0000)
arp.c
common.c
configure.c
dhcpcd.c
duid.c
info.c
interface.c
ipv4ll.c
signal.c
socket.c
socket.h

diff --git a/arp.c b/arp.c
index 514914dd2e5b117a2d25352e0b34bbfd6fe2485d..9680f04fb3732cf1e449a063d97911c4bbea0d21 100644 (file)
--- a/arp.c
+++ b/arp.c
 #define ar_sha(ap) (((unsigned char *) ((ap) + 1)) + 0)
 #define ar_spa(ap) (((unsigned char *) ((ap) + 1)) + (ap)->ar_hln)
 #define ar_tha(ap) (((unsigned char *) ((ap) + 1)) + \
-                                       (ap)->ar_hln + (ap)->ar_pln)
+                   (ap)->ar_hln + (ap)->ar_pln)
 #define ar_tpa(ap) (((unsigned char *) ((ap) + 1)) + \
-                                       2 * (ap)->ar_hln + (ap)->ar_pln)
+                   2 * (ap)->ar_hln + (ap)->ar_pln)
 #endif
 
 #ifndef arphdr_len
 #define arphdr_len2(ar_hln, ar_pln) (sizeof (struct arphdr) + \
-                                                                        2 * (ar_hln) + 2 * (ar_pln))
+                                    2 * (ar_hln) + 2 * (ar_pln))
 #define arphdr_len(ap) (arphdr_len2 ((ap)->ar_hln, (ap)->ar_pln))
 #endif
 
 #ifdef ENABLE_ARP
 
 static int send_arp (const interface_t *iface, int op, struct in_addr sip,
-                                        const unsigned char *taddr, struct in_addr tip)
+                    const unsigned char *taddr, struct in_addr tip)
 {
        struct arphdr *arp;
        int arpsize = arphdr_len2 (iface->hwlen, sizeof (struct in_addr));
@@ -106,7 +106,7 @@ static int send_arp (const interface_t *iface, int op, struct in_addr sip,
        memcpy (ar_tpa (arp), &tip, arp->ar_pln);
 
        retval = send_packet (iface, ETHERTYPE_ARP,
-                                                 (unsigned char *) arp, arphdr_len (arp));
+                             (unsigned char *) arp, arphdr_len (arp));
        free (arp);
        return (retval);
 }
@@ -130,9 +130,10 @@ int arp_claim (interface_t *iface, struct in_addr address)
        }
 
        if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr)) &&
-               ! IN_LINKLOCAL (ntohl (address.s_addr)))
-               logger (LOG_INFO, "checking %s is available on attached networks",
-                               inet_ntoa (address));
+           ! IN_LINKLOCAL (ntohl (address.s_addr)))
+               logger (LOG_INFO,
+                       "checking %s is available on attached networks",
+                       inet_ntoa (address));
 
        if (! open_socket (iface, true))
                return (-1);
@@ -159,7 +160,8 @@ int arp_claim (interface_t *iface, struct in_addr address)
                        tv.tv_usec = timeout;
 
                        maxfd = signal_fd_set (&rset, iface->fd);
-                       if ((s = select (maxfd + 1, &rset, NULL, NULL, &tv)) == -1) {
+                       s = select (maxfd + 1, &rset, NULL, NULL, &tv);
+                       if (s == -1) {
                                if (errno == EINTR) {
                                        if (signal_exists (NULL) == -1) {
                                                errno = 0;
@@ -167,8 +169,9 @@ int arp_claim (interface_t *iface, struct in_addr address)
                                        } else
                                                break;
                                }
-                               
-                               logger (LOG_ERR, "select: `%s'", strerror (errno));
+
+                               logger (LOG_ERR, "select: `%s'",
+                                       strerror (errno));
                                break;
                        }
                }
@@ -178,9 +181,11 @@ int arp_claim (interface_t *iface, struct in_addr address)
                        if (nprobes < NPROBES) {
                                nprobes ++;
                                timeout = PROBE_INTERVAL;
-                               logger (LOG_DEBUG, "sending ARP probe #%d", nprobes);
+                               logger (LOG_DEBUG, "sending ARP probe #%d",
+                                       nprobes);
                                if (send_arp (iface, ARPOP_REQUEST,
-                                                         null_address, NULL, address) == -1)
+                                             null_address, NULL,
+                                             address) == -1)
                                        break;
 
                                /* IEEE1394 cannot set ARP target address
@@ -191,9 +196,11 @@ int arp_claim (interface_t *iface, struct in_addr address)
                        } else if (nclaims < NCLAIMS) {
                                nclaims ++;
                                timeout = CLAIM_INTERVAL;
-                               logger (LOG_DEBUG, "sending ARP claim #%d", nclaims);
+                               logger (LOG_DEBUG, "sending ARP claim #%d",
+                                       nclaims);
                                if (send_arp (iface, ARPOP_REQUEST,
-                                                         address, iface->hwaddr, address) == -1)
+                                             address, iface->hwaddr,
+                                             address) == -1)
                                        break;
                        } else {
                                /* No replies, so done */
@@ -233,8 +240,8 @@ int arp_claim (interface_t *iface, struct in_addr address)
 
                        memset (reply, 0, iface->buffer_length);
                        if ((bytes = get_packet (iface, (unsigned char *) reply,
-                                                                        buffer,
-                                                                        &buflen, &bufpos)) == -1)
+                                                buffer,
+                                                &buflen, &bufpos)) == -1)
                                break;
 
                        /* Only these types are recognised */
@@ -247,24 +254,25 @@ int arp_claim (interface_t *iface, struct in_addr address)
                        if (reply->ar_pln != sizeof (struct in_addr))
                                continue;
                        if ((unsigned) bytes < sizeof (reply) + 
-                               2 * (4 + reply->ar_hln))
+                           2 * (4 + reply->ar_hln))
                                continue;
 
                        rp.c = (unsigned char *) ar_spa (reply);
                        rh.c = (unsigned char *) ar_sha (reply);
 
-                       /* Ensure the ARP reply is for the address we asked for */
+                       /* Ensure the ARP reply is for the our address */
                        if (rp.a->s_addr != address.s_addr)
                                continue;
 
-                       /* Some systems send a reply back from our hwaddress - weird */
+                       /* Some systems send a reply back from our hwaddress,
+                        * which is weird */
                        if (reply->ar_hln == iface->hwlen &&
-                               memcmp (rh.c, iface->hwaddr, iface->hwlen) == 0)
+                           memcmp (rh.c, iface->hwaddr, iface->hwlen) == 0)
                                continue;
 
                        logger (LOG_ERR, "ARPOP_REPLY received from %s (%s)",
-                                       inet_ntoa (*rp.a),
-                                       hwaddr_ntoa (rh.c, reply->ar_hln));
+                               inet_ntoa (*rp.a),
+                               hwaddr_ntoa (rh.c, reply->ar_hln));
                        retval = -1;
                        goto eexit;
                }
index 747e4f0e04f45913126667856141a126201acd92..24c4039c7ff5bbd42a8e5ce79799d29e71669b51 100644 (file)
--- a/common.c
+++ b/common.c
@@ -79,8 +79,8 @@ void srandomdev (void)
 
        fd = open ("/dev/urandom", 0);
        if (fd == -1 || read (fd,  &seed, sizeof (seed)) == -1) {
-               logger (LOG_WARNING, "Could not load seed from /dev/urandom: %s",
-                               strerror (errno));
+               logger (LOG_WARNING, "Could not read from /dev/urandom: %s",
+                       strerror (errno));
                seed = time (0);
        }
        if (fd >= 0)
@@ -183,8 +183,10 @@ void writepid (int fd, pid_t pid)
        if (ftruncate (fd, 0) == -1) {
                logger (LOG_ERR, "ftruncate: %s", strerror (errno));
        } else {
+               ssize_t len;
                snprintf (spid, sizeof (spid), "%u", pid);
-               if (pwrite (fd, spid, strlen (spid), 0) != (ssize_t) strlen (spid))
+               len = pwrite (fd, spid, strlen (spid), 0);
+               if (len != (ssize_t) strlen (spid))
                        logger (LOG_ERR, "pwrite: %s", strerror (errno));
        }
 }
index f67a5b41073375ee8f3a27b14c0825d39e4a7a64..b0944182acc105c8829c42dc36fda69f4c561594 100644 (file)
@@ -67,7 +67,7 @@ static int file_in_path (const char *file)
 
        if (! p) {
                errno = ENOENT;
-               return -1;
+               return (-1);
        }
 
        path = strdup (p);
@@ -80,7 +80,7 @@ static int file_in_path (const char *file)
                }
        }
        free (path);
-       return retval;
+       return (retval);
 }
 
 /* IMPORTANT: Ensure that the last parameter is NULL when calling */
@@ -108,7 +108,7 @@ static int exec_cmd (const char *cmd, const char *args, ...)
        if ((pid = vfork ()) == 0) {
                if (execvp (cmd, argv) && errno != ENOENT)
                        logger (LOG_ERR, "error executing \"%s\": %s",
-                                       cmd, strerror (errno));
+                               cmd, strerror (errno));
                _exit (0);
        } else if (pid == -1) {
                logger (LOG_ERR, "vfork: %s", strerror (errno));
@@ -121,7 +121,7 @@ static int exec_cmd (const char *cmd, const char *args, ...)
 }
 
 static void exec_script (const char *script, const char *infofile,
-                                                const char *arg)
+                        const char *arg)
 {
        struct stat buf;
 
@@ -156,7 +156,8 @@ static int make_resolv (const char *ifname, const dhcp_t *dhcp)
                resolvconf = xmalloc (sizeof (char) * len);
                snprintf (resolvconf, len, "resolvconf -a %s", ifname);
                if ((f = popen (resolvconf , "w")))
-                       logger (LOG_DEBUG, "sending DNS information to resolvconf");
+                       logger (LOG_DEBUG,
+                               "sending DNS information to resolvconf");
                else if (errno == EEXIST)
                        logger (LOG_ERR, "popen: %s", strerror (errno));
 
@@ -206,6 +207,30 @@ static void restore_resolv (const char *ifname)
 #endif
 }
 
+static bool in_addresses (const address_t *addresses, struct in_addr addr)
+{
+       const address_t *address;
+
+       for (address = addresses; address; address = address->next)
+               if (address->address.s_addr == addr.s_addr)
+                       return (true);
+
+       return (false);
+}
+
+static bool in_routes (const route_t *routes, route_t *route)
+{
+       const route_t *r;
+       
+       for (r = routes; r; r=r->next)
+               if (r->destination.s_addr == route->destination.s_addr &&
+                   r->netmask.s_addr == route->netmask.s_addr &&
+                   r->gateway.s_addr == route->gateway.s_addr)
+                       return (true);
+
+       return (false);
+}
+
 #ifdef ENABLE_NTP
 static int _make_ntp (const char *file, const char *ifname, const dhcp_t *dhcp)
 {
@@ -224,47 +249,44 @@ static int _make_ntp (const char *file, const char *ifname, const dhcp_t *dhcp)
           We do this because ntp has to be restarted to work with a changed config */
        if (! (f = fopen (file, "r"))) {
                if (errno != ENOENT) {
-                       logger (LOG_ERR, "fopen `%s': %s", file, strerror (errno));
-                       return -1;
+                       logger (LOG_ERR, "fopen `%s': %s",
+                               file, strerror (errno));
+                       return (-1);
                }
        } else {
-               while ((line = getline (f))) {
+               while (tomatch != 0 && (line = getline (f))) {
+                       struct in_addr addr;
+                       
                        a = line;
                        token = strsep (&a, " ");
-                       if (! token || strcmp (token, "server") != 0) {
-                               free (line);
-                               continue;
-                       }
+                       if (! token || strcmp (token, "server") != 0)
+                               goto next;
 
-                       if ((token = strsep (&a, " \n")) == NULL) {
-                               free (line);
-                               continue;
-                       }
+                       if ((token = strsep (&a, " \n")) == NULL)
+                               goto next;
 
-                       for (address = dhcp->ntpservers; address; address = address->next)
-                               if (strcmp (token, inet_ntoa (address->address)) == 0) {
-                                       tomatch--;
-                                       break;
-                               }
+                       if (inet_aton (token, &addr) == 0 &&
+                           in_addresses (dhcp->ntpservers, addr))
+                               tomatch--;
 
-                       if (tomatch == 0) {
-                               free (line);
-                               break;
-                       }
+next:
+                       free (line);
                }
                fclose (f);
 
-               /* File has the same name servers that we do, so no need to restart ntp */
+               /* File has the same name servers that we do,
+                * so no need to restart ntp */
                if (tomatch == 0) {
-                       logger (LOG_DEBUG, "%s already configured, skipping", file);
-                       return 0;
+                       logger (LOG_DEBUG, "%s already configured, skipping",
+                               file);
+                       return (0);
                }
        }
 
        logger (LOG_DEBUG, "writing %s", file);
        if (! (f = fopen (file, "w"))) {
                logger (LOG_ERR, "fopen `%s': %s", file, strerror (errno));
-               return -1;
+               return (-1);
        }
 
        fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname);
@@ -284,7 +306,7 @@ static int _make_ntp (const char *file, const char *ifname, const dhcp_t *dhcp)
        }
        fclose (f);
 
-       return 1;
+       return (1);
 }
 
 static int make_ntp (const char *ifname, const dhcp_t *dhcp)
@@ -312,28 +334,32 @@ static int make_ntp (const char *ifname, const dhcp_t *dhcp)
 #ifdef NTPCHECK
                if (system (NTPCHECK) == 0)
 #endif
-                       retval += exec_cmd (NTPSERVICE, NTPRESTARTARGS, (char *) NULL);
+                       retval += exec_cmd (NTPSERVICE, NTPRESTARTARGS,
+                                           (char *) NULL);
        }
 #endif
 
 #if defined (NTPSERVICE) && defined (OPENNTPSERVICE)
        if (restart_openntp &&
-               (strcmp (NTPSERVICE, OPENNTPSERVICE) != 0 || ! restart_ntp))
+           (strcmp (NTPSERVICE, OPENNTPSERVICE) != 0 || ! restart_ntp))
        {
 #ifdef OPENNTPCHECK
                if (system (OPENNTPCHECK) == 0)
 #endif
-                       retval += exec_cmd (OPENNTPSERVICE, OPENNTPRESTARTARGS, (char *) NULL);
+                       retval += exec_cmd (OPENNTPSERVICE,
+                                           OPENNTPRESTARTARGS, (char *) NULL);
        }
 #elif defined (OPENNTPSERVICE) && ! defined (NTPSERVICE)
        if (restart_openntp) {
 #ifdef OPENNTPCHECK
                if (system (OPENNTPCHECK) == 0)
 #endif
-                       retval += exec_cmd (OPENNTPSERVICE, OPENNTPRESTARTARGS, (char *) NULL);
+                       retval += exec_cmd (OPENNTPSERVICE,
+                                           OPENNTPRESTARTARGS, (char *) NULL);
+       }
 #endif
 
-       return retval;
+       return (retval);
 }
 #endif
 
@@ -348,7 +374,7 @@ static int make_nis (const char *ifname, const dhcp_t *dhcp)
        logger (LOG_DEBUG, "writing "NISFILE);
        if (! (f = fopen(NISFILE, "w"))) {
                logger (LOG_ERR, "fopen `%s': %s", NISFILE, strerror (errno));
-               return -1;
+               return (-1);
        }
 
        prefix = xmalloc (sizeof (char) * PREFIXSIZE);
@@ -359,7 +385,8 @@ static int make_nis (const char *ifname, const dhcp_t *dhcp)
                setdomainname (dhcp->nisdomain, strlen (dhcp->nisdomain));
 
                if (dhcp->nisservers)
-                       snprintf (prefix, PREFIXSIZE, "domain %s server", dhcp->nisdomain);
+                       snprintf (prefix, PREFIXSIZE, "domain %s server",
+                                 dhcp->nisdomain);
                else
                        fprintf (f, "domain %s broadcast\n", dhcp->nisdomain);
        }
@@ -376,17 +403,100 @@ static int make_nis (const char *ifname, const dhcp_t *dhcp)
        if (system (NISCHECK) == 0)
 #endif
                exec_cmd (NISSERVICE, NISRESTARTARGS, (char *) NULL);
-       return 0;
+       return (0);
 }
 #endif
 
+static char *lookuphostname (char *hostname, const dhcp_t *dhcp,
+                            const options_t *options)
+{
+       union {
+               struct sockaddr sa;
+               struct sockaddr_in sin;
+       } su;
+       socklen_t salen;
+       char *addr;
+       struct addrinfo hints;
+       struct addrinfo *res;
+       int result;
+       char *p;
+
+       logger (LOG_DEBUG, "Looking up hostname via DNS");
+       addr = xmalloc (sizeof (char) * NI_MAXHOST);
+       salen = sizeof (struct sockaddr);
+       memset (&su.sa, 0, salen);
+       su.sin.sin_family = AF_INET;
+       memcpy (&su.sin.sin_addr, &dhcp->address, sizeof (struct in_addr));
+
+       if ((result = getnameinfo (&su.sa, salen, addr, NI_MAXHOST,
+                                  NULL, 0, NI_NAMEREQD)) != 0) {
+               logger (LOG_ERR,
+                       "Failed to lookup hostname via DNS: %s",
+                       gai_strerror (result));
+               free (addr);
+               return (NULL);
+       }
+       
+       /* Check for a malicious PTR record */
+       memset (&hints, 0, sizeof (hints));
+       hints.ai_socktype = SOCK_DGRAM;
+       hints.ai_flags = AI_NUMERICHOST;
+       result = getaddrinfo (addr, "0", &hints, &res);
+       freeaddrinfo (res);
+       if (result == 0)
+               logger (LOG_ERR, "malicious PTR record detected");
+       if (result == 0 || ! *addr) {
+               free (addr);
+               return (NULL);
+       }
+
+       p = strchr (addr, '.');
+       if (p) {
+               switch (options->dohostname) {
+                       case 1: /* -H */
+                       case 4: /* -HHHH */
+                               break;
+                       case 2: /* -HH */
+                       case 5: /* -HHHHH */
+                               /* Strip out the domain if it matches */
+                               p++;
+                               if (*p && dhcp->dnssearch) {
+                                       char *s = xstrdup (dhcp->dnssearch);
+                                       char *sp = s;
+                                       char *t;
+
+                                       while ((t = strsep (&sp, " ")))
+                                               if (strcmp (t, p) == 0) {
+                                                       *--p = '\0';
+                                                       break;
+                                               }
+                                       free (s);
+                               } else if (dhcp->dnsdomain) {
+                                       if (strcmp (dhcp->dnsdomain, p) == 0)
+                                               *--p = '\0';
+                               }
+                               break;
+                       case 3: /* -HHH */
+                       case 6: /* -HHHHHH */
+                               /* Just strip the domain */
+                               *p = '\0';
+                               break;
+                       default: /* Too many H! */
+                               break;
+               }
+       }
+
+       strlcpy (hostname, addr, MAXHOSTNAMELEN);
+       free (addr);
+       return (hostname);
+}
+
 int configure (const options_t *options, interface_t *iface,
-                          const dhcp_t *dhcp, bool up)
+              const dhcp_t *dhcp, bool up)
 {
        route_t *route = NULL;
        route_t *new_routes = NULL;
        route_t *new_route = NULL;
-       route_t *old_route = NULL;
        char *newhostname = NULL;
        char *curhostname = NULL;
        int remember;
@@ -394,6 +504,7 @@ int configure (const options_t *options, interface_t *iface,
        bool haslinklocal = false;
 #endif
 #ifdef THERE_IS_NO_FORK
+       int skip = 0;
        size_t skiplen;
        char *skipp;
 #endif
@@ -407,24 +518,12 @@ int configure (const options_t *options, interface_t *iface,
        /* Remove old routes
           Always do this as the interface may have >1 address not added by us
           so the routes we added may still exist */
-       if (iface->previous_routes) {
-               for (route = iface->previous_routes; route; route = route->next)
-                       if (route->destination.s_addr || options->dogateway) {
-                               int have = 0;
-                               if (up)
-                                       for (new_route = dhcp->routes; new_route; new_route = new_route->next)
-                                               if (new_route->destination.s_addr == route->destination.s_addr
-                                                       && new_route->netmask.s_addr == route->netmask.s_addr
-                                                       && new_route->gateway.s_addr == route->gateway.s_addr)
-                                               {
-                                                       have = 1;
-                                                       break;
-                                               }
-                               if (! have)
-                                       del_route (iface->name, route->destination, route->netmask,
-                                                          route->gateway, options->metric);
-                       }
-       }
+       for (route = iface->previous_routes; route; route = route->next)
+               if ((route->destination.s_addr || options->dogateway) &&
+                   (! up || ! in_routes (dhcp->routes, route)))
+                       del_route (iface->name, route->destination,
+                                  route->netmask, route->gateway,
+                                  options->metric);
 
        /* If we aren't up, then reset the interface as much as we can */
        if (! up) {
@@ -448,24 +547,25 @@ int configure (const options_t *options, interface_t *iface,
                /* Only reset things if we had set them before */
                if (iface->previous_address.s_addr != 0) {
                        if (! options->keep_address) {
-                               del_address (iface->name, iface->previous_address,
-                                                        iface->previous_netmask);
-                               memset (&iface->previous_address, 0, sizeof (struct in_addr));
-                               memset (&iface->previous_netmask, 0, sizeof (struct in_addr));
+                               del_address (iface->name,
+                                            iface->previous_address,
+                                            iface->previous_netmask);
+                               memset (&iface->previous_address,
+                                       0, sizeof (struct in_addr));
+                               memset (&iface->previous_netmask,
+                                       0, sizeof (struct in_addr));
                        }
                }
 
                restore_resolv (iface->name);
-               /* we currently don't have a resolvconf style programs for ntp/nis */
-
                exec_script (options->script, iface->infofile, "down");
 
                return (0);
        }
 
        /* Set the MTU requested.
-          If the DHCP server no longer sends one OR it's invalid then we restore
-          the original MTU */
+          If the DHCP server no longer sends one OR it's invalid then
+          we restore the original MTU */
        if (options->domtu) {
                unsigned short mtu = iface->mtu;
                if (dhcp->mtu)
@@ -480,19 +580,21 @@ int configure (const options_t *options, interface_t *iface,
        /* This also changes netmask */
        if (! options->doinform || ! has_address (iface->name, dhcp->address))
                if (add_address (iface->name, dhcp->address, dhcp->netmask,
-                                                dhcp->broadcast) == -1 && errno != EEXIST)
+                                dhcp->broadcast) == -1 && errno != EEXIST)
                        return (false);
-       
+
        /* Now delete the old address if different */
-       if (iface->previous_address.s_addr != dhcp->address.s_addr) {
-               if (iface->previous_address.s_addr != 0 && ! options->keep_address)
-                       del_address (iface->name, iface->previous_address, iface->previous_netmask);
-       }
+       if (iface->previous_address.s_addr != dhcp->address.s_addr &&
+           iface->previous_address.s_addr != 0 &&
+           ! options->keep_address)
+               del_address (iface->name,
+                            iface->previous_address, iface->previous_netmask);
 
 #ifdef __linux__
        /* On linux, we need to change the subnet route to have our metric. */
-       if (iface->previous_address.s_addr != dhcp->address.s_addr
-               && options->metric > 0 && dhcp->netmask.s_addr != INADDR_BROADCAST)
+       if (iface->previous_address.s_addr != dhcp->address.s_addr &&
+           options->metric > 0 &&
+           dhcp->netmask.s_addr != INADDR_BROADCAST)
        {
                struct in_addr td;
                struct in_addr tg;
@@ -514,66 +616,54 @@ int configure (const options_t *options, interface_t *iface,
 #endif
 
        /* Remember added routes */
-       if (dhcp->routes) {
-#ifdef THERE_IS_NO_FORK
-               int skip = 0;
-#endif
-
-               for (route = dhcp->routes; route; route = route->next) {
+       for (route = dhcp->routes; route; route = route->next) {
 #ifdef ENABLE_IPV4LL
-                       /* Check if we have already got a link locale route dished
-                        * out by the DHCP server */
-                       if (route->destination.s_addr == htonl (LINKLOCAL_ADDR) &&
-                               route->netmask.s_addr == htonl (LINKLOCAL_MASK))
-                               haslinklocal = true;
+               /* Check if we have already got a link locale route dished
+                * out by the DHCP server */
+               if (route->destination.s_addr == htonl (LINKLOCAL_ADDR) &&
+                   route->netmask.s_addr == htonl (LINKLOCAL_MASK))
+                       haslinklocal = true;
 #endif
-                       /* Don't set default routes if not asked to */
-                       if (route->destination.s_addr == 0 && route->netmask.s_addr == 0
-                               && ! options->dogateway)
-                               continue;
-
-                       remember = add_route (iface->name, route->destination,
-                                                                 route->netmask,  route->gateway,
-                                                                 options->metric);
-                       /* If we failed to add the route, we may have already added it
-                          ourselves. If so, remember it again. */
-                       if (remember < 0)
-                               for (old_route = iface->previous_routes; old_route;
-                                        old_route = old_route->next)
-                                       if (old_route->destination.s_addr == route->destination.s_addr
-                                               && old_route->netmask.s_addr == route->netmask.s_addr
-                                               && old_route->gateway.s_addr == route->gateway.s_addr)
-                                       {
-                                               remember = 1;
-                                               break;
-                                       }
-
-                       if (remember >= 0) {
-                               if (! new_routes) {
-                                       new_routes = xmalloc (sizeof (route_t));
-                                       memset (new_routes, 0, sizeof (route_t));
-                                       new_route = new_routes;
-                               } else {
-                                       new_route->next = xmalloc (sizeof (route_t));
-                                       new_route = new_route->next;
-                               }
-                               memcpy (new_route, route, sizeof (route_t));
-                               new_route -> next = NULL;
+               /* Don't set default routes if not asked to */
+               if (route->destination.s_addr == 0 &&
+                   route->netmask.s_addr == 0 &&
+                   ! options->dogateway)
+                       continue;
+
+               remember = add_route (iface->name, route->destination,
+                                     route->netmask,  route->gateway,
+                                     options->metric);
+               /* If we failed to add the route, we may have already added it
+                  ourselves. If so, remember it again. */
+               if (remember < 0 && in_routes (iface->previous_routes, route))
+                       remember = 1;
+
+               if (remember >= 0) {
+                       if (! new_routes) {
+                               new_routes = xmalloc (sizeof (route_t));
+                               memset (new_routes, 0, sizeof (route_t));
+                               new_route = new_routes;
+                       } else {
+                               new_route->next = xmalloc (sizeof (route_t));
+                               new_route = new_route->next;
                        }
+                       memcpy (new_route, route, sizeof (route_t));
+                       new_route -> next = NULL;
+               }
 #ifdef THERE_IS_NO_FORK
-                       /* If we have daemonised yet we need to record which routes
-                        * we failed to add so we can skip them */
-                       else if (! options->daemonised) {
-                               /* We can never have more than 255 / 4 routes, so 3 chars is
-                                * plently */
-                               if (*skipp)
-                                       *skipp++ = ',';
-                               skipp += snprintf (skipp, dhcpcd_skiproutes + skiplen - skipp,
-                                                                  "%d", skip);
-                       }
-                       skip++;
-#endif
+               /* If we have daemonised yet we need to record which routes
+                * we failed to add so we can skip them */
+               else if (! options->daemonised) {
+                       /* We can never have more than 255 / 4 routes,
+                        * so 3 chars is plently */
+                       if (*skipp)
+                               *skipp++ = ',';
+                       skipp += snprintf (skipp,
+                                          dhcpcd_skiproutes + skiplen - skipp,
+                                          "%d", skip);
                }
+               skip++;
+#endif
        }
 
 #ifdef THERE_IS_NO_FORK
@@ -589,8 +679,8 @@ int configure (const options_t *options, interface_t *iface,
        /* Ensure we always add the link local route if we got a private
         * address and isn't link local itself */
        if (options->doipv4ll &&
-               ! haslinklocal &&
-               IN_PRIVATE (ntohl (dhcp->address.s_addr)))
+           ! haslinklocal &&
+           IN_PRIVATE (ntohl (dhcp->address.s_addr)))
        {
                struct in_addr dest;
                struct in_addr mask;
@@ -600,7 +690,7 @@ int configure (const options_t *options, interface_t *iface,
                mask.s_addr = htonl (LINKLOCAL_MASK);
                gate.s_addr = 0;
                remember = add_route (iface->name, dest, mask, gate,
-                                                         options->metric);
+                                     options->metric);
 
                if (remember >= 0) {
                        if (! new_routes) {
@@ -640,99 +730,35 @@ int configure (const options_t *options, interface_t *iface,
 
        curhostname = xmalloc (sizeof (char) * MAXHOSTNAMELEN);
        *curhostname = '\0';
-       newhostname = xmalloc (sizeof (char) * MAXHOSTNAMELEN);
-       *newhostname = '\0';
-
-       /* Now we have made a resolv.conf we can obtain a hostname if we need it */
-       if (options->dohostname && (! dhcp->hostname || options->dohostname > 3)) { 
-               union {
-                       struct sockaddr sa;
-                       struct sockaddr_in sin;
-               } su;
-               socklen_t salen;
-               char *addr;
-               struct addrinfo hints, *res;
-               int result;
-
-               addr = xmalloc (sizeof (char) * NI_MAXHOST);
-               salen = sizeof (struct sockaddr);
-               memset (&su.sa, 0, salen);
-               su.sin.sin_family = AF_INET;
-               memcpy (&su.sin.sin_addr, &dhcp->address, sizeof (struct in_addr));
-
-               logger (LOG_DEBUG, "Looking up hostname via DNS");
-               if ((result = getnameinfo (&su.sa, salen, addr, NI_MAXHOST,
-                                                NULL, 0, NI_NAMEREQD)) != 0)
-                       logger (LOG_ERR, "Failed to lookup hostname via DNS: %s", gai_strerror (result));
-               else {
-                       /* Check for a malicious PTR record */
-                       memset (&hints, 0, sizeof (hints));
-                       hints.ai_socktype = SOCK_DGRAM;
-                       hints.ai_flags = AI_NUMERICHOST;
-                       if (getaddrinfo (addr, "0", &hints, &res) == 0) {
-                               freeaddrinfo (res);
-                               addr[0] = '\0';
-                               logger (LOG_ERR, "malicious PTR record detected");
-                       } else if (*addr) {
-                               char *p = strchr (addr, '.');
-                               if (p) {
-                                       switch (options->dohostname) {
-                                               case 1: /* -H */
-                                               case 4: /* -HHHH */
-                                                       break;
-                                               case 2: /* -HH */
-                                               case 5: /* -HHHHH */
-                                                       /* Strip out the domain if it matches */
-                                                       p++;
-                                                       if (*p && dhcp->dnssearch) {
-                                                               char *s = xstrdup (dhcp->dnssearch);
-                                                               char *sp = s;
-                                                               char *t;
-
-                                                               while ((t = strsep (&sp, " ")))
-                                                                       if (strcmp (t, p) == 0) {
-                                                                               *--p = '\0';
-                                                                               break;
-                                                                       }
-                                                               free (s);
-                                                       } else if (dhcp->dnsdomain) {
-                                                               if (strcmp (dhcp->dnsdomain, p) == 0)
-                                                                       *--p = '\0';
-                                                       }
-                                                       break;
-                                               case 3: /* -HHH */
-                                               case 6: /* -HHHHHH */
-                                                       /* Just strip the domain */
-                                                       *p = '\0';
-                                                       break;
-                                               default: /* Too many H! */
-                                                       break;
-                                       }
-                               }
-                               strlcpy (newhostname, addr, MAXHOSTNAMELEN);
-                       }
-               }
-               free (addr);
-       }
 
        gethostname (curhostname, MAXHOSTNAMELEN);
-
-       if (options->dohostname
-               || strlen (curhostname) == 0
-               || strcmp (curhostname, "(none)") == 0
-               || strcmp (curhostname, "localhost") == 0)
+       if (options->dohostname ||
+           strlen (curhostname) == 0 ||
+           strcmp (curhostname, "(none)") == 0 ||
+           strcmp (curhostname, "localhost") == 0)
        {
+               newhostname = xmalloc (sizeof (char) * MAXHOSTNAMELEN);
+
                if (dhcp->hostname)
                        strlcpy (newhostname, dhcp->hostname, MAXHOSTNAMELEN);
+               else
+                       *newhostname = '\0';
+
+               /* Now we have made a resolv.conf we can obtain a hostname
+                * if we need it */
+               if (! *newhostname && options->dohostname > 3)
+                       lookuphostname (newhostname, dhcp, options);
 
                if (*newhostname) {
-                       logger (LOG_INFO, "setting hostname to `%s'", newhostname);
+                       logger (LOG_INFO, "setting hostname to `%s'",
+                               newhostname);
                        sethostname (newhostname, strlen (newhostname));
                }
+
+               free (newhostname);
        }
-       
+
        free (curhostname);
-       free (newhostname);
 
 #ifdef ENABLE_INFO
        if (! dhcp->frominfo)
@@ -740,12 +766,12 @@ int configure (const options_t *options, interface_t *iface,
 #endif
 
        if (iface->previous_address.s_addr != dhcp->address.s_addr ||
-               iface->previous_netmask.s_addr != dhcp->netmask.s_addr)
+           iface->previous_netmask.s_addr != dhcp->netmask.s_addr)
        {
                memcpy (&iface->previous_address,
-                               &dhcp->address, sizeof (struct in_addr));
+                       &dhcp->address, sizeof (struct in_addr));
                memcpy (&iface->previous_netmask,
-                               &dhcp->netmask, sizeof (struct in_addr));
+                       &dhcp->netmask, sizeof (struct in_addr));
                exec_script (options->script, iface->infofile, "new");
        } else
                exec_script (options->script, iface->infofile, "up");
index 8b9eec23881cc23a46d6f87cb9ff0c80793a99fc..e1aee886f5866cb244e217e19fb19d3489cef2e7 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -113,7 +113,7 @@ static int atoint (const char *s)
        errno = 0;
        n = strtol (s, &t, 0);
        if ((errno != 0 && n == 0) || s == t ||
-               (errno == ERANGE && (n == LONG_MAX || n == LONG_MIN)))
+           (errno == ERANGE && (n == LONG_MAX || n == LONG_MIN)))
        {
                logger (LOG_ERR, "`%s' out of range", s);
                exit (EXIT_FAILURE);
@@ -141,9 +141,9 @@ static pid_t read_pid (const char *pidfile)
 static void usage (void)
 {
        printf ("usage: "PACKAGE" [-adknpEGHMNRSTY] [-c script] [-h hostname] [-i classID]\n"
-               "              [-l leasetime] [-m metric] [-r ipaddress] [-s ipaddress]\n"
-                       "              [-t timeout] [-u userclass] [-F none | ptr | both]\n"
-                       "              [-I clientID] <interface>\n");
+               "              [-l leasetime] [-m metric] [-r ipaddress] [-s ipaddress]\n"
+               "              [-t timeout] [-u userclass] [-F none | ptr | both]\n"
+               "              [-I clientID] <interface>\n");
 }
 
 int main(int argc, char **argv)
@@ -169,7 +169,8 @@ int main(int argc, char **argv)
        options = xmalloc (sizeof (options_t));
        memset (options, 0, sizeof (options_t));
        options->script = (char *) DEFAULT_SCRIPT;
-       snprintf (options->classid, CLASS_ID_MAX_LEN, "%s %s", PACKAGE, VERSION);
+       snprintf (options->classid, CLASS_ID_MAX_LEN, "%s %s",
+                 PACKAGE, VERSION);
        options->classid_len = strlen (options->classid);
 
        options->doarp = true;
@@ -185,21 +186,22 @@ int main(int argc, char **argv)
 
        gethostname (options->hostname, sizeof (options->hostname));
        if (strcmp (options->hostname, "(none)") == 0 ||
-               strcmp (options->hostname, "localhost") == 0)
+           strcmp (options->hostname, "localhost") == 0)
                memset (options->hostname, 0, sizeof (options->hostname));
 
        /* Don't set any optional arguments here so we retain POSIX
         * compatibility with getopt */
        while ((opt = getopt_long(argc, argv, EXTRA_OPTS
-                                                         "c:dh:i:kl:m:npr:s:t:u:xAEF:GHI:LMNRSTY",
-                                                         longopts, &option_index)) != -1)
+                                 "c:dh:i:kl:m:npr:s:t:u:xAEF:GHI:LMNRSTY",
+                                 longopts, &option_index)) != -1)
        {
                switch (opt) {
                        case 0:
                                if (longopts[option_index].flag)
                                        break;
-                               logger (LOG_ERR, "option `%s' should set a flag",
-                                               longopts[option_index].name);
+                               logger (LOG_ERR,
+                                       "option `%s' should set a flag",
+                                       longopts[option_index].name);
                                exit (EXIT_FAILURE);
                                break;
                        case 'c':
@@ -227,25 +229,30 @@ int main(int argc, char **argv)
 #endif
                        case 'h':
                                if (! optarg)
-                                       memset (options->hostname, 0, sizeof (options->hostname));
+                                       memset (options->hostname, 0,
+                                               sizeof (options->hostname));
                                else if (strlen (optarg) > MAXHOSTNAMELEN) {
-                                       logger (LOG_ERR, "`%s' too long for HostName string, max is %d",
-                                                       optarg, MAXHOSTNAMELEN);
+                                       logger (LOG_ERR,
+                                               "`%s' too long for HostName string, max is %d",
+                                               optarg, MAXHOSTNAMELEN);
                                        exit (EXIT_FAILURE);
                                } else
-                                       strlcpy (options->hostname, optarg, sizeof (options->hostname));
+                                       strlcpy (options->hostname, optarg,
+                                                sizeof (options->hostname));
                                break;
                        case 'i':
                                if (! optarg) {
-                                       memset (options->classid, 0, sizeof (options->classid));
+                                       memset (options->classid, 0,
+                                               sizeof (options->classid));
                                        options->classid_len = 0;
                                } else if (strlen (optarg) > CLASS_ID_MAX_LEN) {
-                                       logger (LOG_ERR, "`%s' too long for ClassID string, max is %d",
-                                                       optarg, CLASS_ID_MAX_LEN);
+                                       logger (LOG_ERR,
+                                               "`%s' too long for ClassID string, max is %d",
+                                               optarg, CLASS_ID_MAX_LEN);
                                        exit (EXIT_FAILURE);
                                } else
                                        options->classid_len = strlcpy (options->classid, optarg,
-                                                                                                  sizeof (options->classid));
+                                                                       sizeof (options->classid));
                                break;
                        case 'k':
                                sig = SIGHUP;
@@ -253,7 +260,8 @@ int main(int argc, char **argv)
                        case 'l':
                                options->leasetime = atoint (optarg);
                                if (options->leasetime <= 0) {
-                                       logger (LOG_ERR, "leasetime must be a positive value");
+                                       logger (LOG_ERR,
+                                               "leasetime must be a positive value");
                                        exit (EXIT_FAILURE);
                                }
                                break;
@@ -280,7 +288,7 @@ int main(int argc, char **argv)
                                                 * address */
                                                *slash++ = '\0';
                                                if (sscanf (slash, "%d", &cidr) != 1 ||
-                                                       inet_cidrtoaddr (cidr, &options->request_netmask) != 0) {
+                                                   inet_cidrtoaddr (cidr, &options->request_netmask) != 0) {
                                                        logger (LOG_ERR, "`%s' is not a valid CIDR", slash);
                                                        exit (EXIT_FAILURE);
                                                }
@@ -291,7 +299,7 @@ int main(int argc, char **argv)
                                if (! options->doinform)
                                        options->dorequest = true;
                                if (strlen (optarg) > 0 &&
-                                       ! inet_aton (optarg, &options->request_address))
+                                   ! inet_aton (optarg, &options->request_address))
                                { 
                                        logger (LOG_ERR, "`%s' is not a valid IP address", optarg);
                                        exit (EXIT_FAILURE);
@@ -311,7 +319,7 @@ int main(int argc, char **argv)
                                                offset += (int) options->userclass[offset] + 1;
                                        if (offset + 1 + strlen (optarg) > USERCLASS_MAX_LEN) {
                                                logger (LOG_ERR, "userclass overrun, max is %d",
-                                                               USERCLASS_MAX_LEN);
+                                                       USERCLASS_MAX_LEN);
                                                exit (EXIT_FAILURE);
                                        }
                                        userclasses++;
@@ -325,14 +333,16 @@ int main(int argc, char **argv)
                                break;
                        case 'A':
 #ifndef ENABLE_ARP
-                               logger (LOG_ERR, "arp support not compiled into dhcpcd");
+                               logger (LOG_ERR,
+                                       "arp not compiled into dhcpcd");
                                exit (EXIT_FAILURE);
 #endif
                                options->doarp = false;
                                break;
                        case 'E':
 #ifndef ENABLE_INFO
-                               logger (LOG_ERR, "info support not compiled into dhcpcd");
+                               logger (LOG_ERR,
+                                       "info not compiled into dhcpcd");
                                exit (EXIT_FAILURE);
 #endif
                                options->dolastlease = true;
@@ -359,11 +369,11 @@ int main(int argc, char **argv)
                                if (optarg) {
                                        if (strlen (optarg) > CLIENT_ID_MAX_LEN) {
                                                logger (LOG_ERR, "`%s' is too long for ClientID, max is %d",
-                                                               optarg, CLIENT_ID_MAX_LEN);
+                                                       optarg, CLIENT_ID_MAX_LEN);
                                                exit (EXIT_FAILURE);
                                        }
                                        options->clientid_len = strlcpy (options->clientid, optarg,
-                                                                                                       sizeof (options->clientid));
+                                                                        sizeof (options->clientid));
                                        /* empty string disabled duid */
                                        if (options->clientid_len == 0)
                                                options->clientid_len = -1;
@@ -410,36 +420,36 @@ int main(int argc, char **argv)
                printf (""PACKAGE" "VERSION"\n");
                printf ("Compile time options:"
 #ifdef ENABLE_ARP
-                               " ARP"
+                       " ARP"
 #endif
 #ifdef ENABLE_DUID
-                               " DUID"
+                       " DUID"
 #endif
 #ifdef ENABLE_INFO
-                               " INFO"
+                       " INFO"
 #endif
 #ifdef ENABLE_INFO_COMPAT
-                               " INFO_COMPAT"
+                       " INFO_COMPAT"
 #endif
 #ifdef ENABLE_IPV4LL
-                               " IPV4LL"
+                       " IPV4LL"
 #endif
 #ifdef ENABLE_NIS
-                               " NIS"
+                       " NIS"
 #endif
 #ifdef ENABLE_NTP
-                               " NTP"
+                       " NTP"
 #endif
 #ifdef SERVICE
-                               " " SERVICE
+                       " " SERVICE
 #endif
 #ifdef ENABLE_RESOLVCONF
-                               " RESOLVCONF"
+                       " RESOLVCONF"
 #endif
 #ifdef THERE_IS_NO_FORK
-                               " THERE_IS_NO_FORK"
+                       " THERE_IS_NO_FORK"
 #endif
-                               "\n");
+                       "\n");
        }
 
        if (dohelp)
@@ -450,19 +460,20 @@ int main(int argc, char **argv)
        dhcpcd_argc = argc;
        if (! realpath (argv[0], dhcpcd)) {
                logger (LOG_ERR, "unable to resolve the path `%s': %s",
-                               argv[0], strerror (errno));
+                       argv[0], strerror (errno));
                goto abort;
        }
 #endif
 
        if (optind < argc) {
                if (strlen (argv[optind]) > IF_NAMESIZE) {
-                       logger (LOG_ERR, "`%s' is too long for an interface name (max=%d)",
-                                       argv[optind], IF_NAMESIZE);
+                       logger (LOG_ERR,
+                               "`%s' too long for an interface name (max=%d)",
+                               argv[optind], IF_NAMESIZE);
                        goto abort;
                }
                strlcpy (options->interface, argv[optind],
-                                sizeof (options->interface));
+                        sizeof (options->interface));
        } else {
                /* If only version was requested then exit now */
                if (doversion || dohelp) {
@@ -481,17 +492,19 @@ int main(int argc, char **argv)
                options->fqdn = FQDN_DISABLE;
 
        if (options->request_address.s_addr == 0 && options->doinform) {
-               if ((options->request_address.s_addr = get_address (options->interface)) != 0)
+               if ((options->request_address.s_addr =
+                    get_address (options->interface)) != 0)
                        options->keep_address = true;
        }
 
        if (IN_LINKLOCAL (ntohl (options->request_address.s_addr))) {
-               logger (LOG_ERR, "you are not allowed to request a link local address");
+               logger (LOG_ERR,
+                       "you are not allowed to request a link local address");
                goto abort;
        }
 
        if (geteuid ()) {
-               logger (LOG_ERR, "you need to be root to run "PACKAGE);
+               logger (LOG_ERR, "you need to be root to run " PACKAGE);
                goto abort;
        }
 
@@ -499,29 +512,32 @@ int main(int argc, char **argv)
        snprintf (prefix, IF_NAMESIZE, "%s: ", options->interface);
        setlogprefix (prefix);
        snprintf (options->pidfile, sizeof (options->pidfile), PIDFILE,
-                         options->interface);
+                 options->interface);
        free (prefix);
 
        chdir ("/");
        umask (022);
-       
+
        if (mkdir (INFODIR, S_IRUSR | S_IWUSR |S_IXUSR | S_IRGRP | S_IXGRP
-                          | S_IROTH | S_IXOTH) && errno != EEXIST)
+                  | S_IROTH | S_IXOTH) && errno != EEXIST)
        {
-               logger (LOG_ERR, "mkdir(\"%s\",0): %s\n", INFODIR, strerror (errno));
+               logger (LOG_ERR,
+                       "mkdir(\"%s\",0): %s\n", INFODIR, strerror (errno));
                goto abort;
        }
 
        if (mkdir (ETCDIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP
-                          | S_IROTH | S_IXOTH) && errno != EEXIST)
+                  | S_IROTH | S_IXOTH) && errno != EEXIST)
        {
-               logger (LOG_ERR, "mkdir(\"%s\",0): %s\n", ETCDIR, strerror (errno));
+               logger (LOG_ERR,
+                       "mkdir(\"%s\",0): %s\n", ETCDIR, strerror (errno));
                goto abort;
        }
 
        if (options->test) {
                if (options->dorequest || options->doinform) {
-                       logger (LOG_ERR, "cannot test with --inform or --request");
+                       logger (LOG_ERR,
+                               "cannot test with --inform or --request");
                        goto abort;
                }
 
@@ -531,7 +547,8 @@ int main(int argc, char **argv)
                }
 
                if (sig != 0) {
-                       logger (LOG_ERR, "cannot test with --release or --renew");
+                       logger (LOG_ERR,
+                               "cannot test with --release or --renew");
                        goto abort;
                }
        }
@@ -540,10 +557,12 @@ int main(int argc, char **argv)
                int killed = -1;
                pid = read_pid (options->pidfile);
                if (pid != 0)
-                       logger (LOG_INFO, "sending signal %d to pid %d", sig, pid);
+                       logger (LOG_INFO, "sending signal %d to pid %d",
+                               sig, pid);
 
                if (! pid || (killed = kill (pid, sig)))
-                       logger (sig == SIGALRM ? LOG_INFO : LOG_ERR, ""PACKAGE" not running");
+                       logger (sig == SIGALRM ? LOG_INFO : LOG_ERR,
+                               ""PACKAGE" not running");
 
                if (pid != 0 && (sig != SIGALRM || killed != 0))
                        unlink (options->pidfile);
@@ -558,27 +577,34 @@ int main(int argc, char **argv)
        }
 
        if (! options->test && ! options->daemonised) {
-               if ((pid = read_pid (options->pidfile)) > 0 && kill (pid, 0) == 0) {
-                       logger (LOG_ERR, ""PACKAGE" already running on pid %d (%s)",
-                                       pid, options->pidfile);
+               if ((pid = read_pid (options->pidfile)) > 0 &&
+                   kill (pid, 0) == 0)
+               {
+                       logger (LOG_ERR, ""PACKAGE
+                               " already running on pid %d (%s)",
+                               pid, options->pidfile);
                        goto abort;
                }
 
-               pidfd = open (options->pidfile, O_WRONLY | O_CREAT | O_NONBLOCK, 0660);
+               pidfd = open (options->pidfile,
+                             O_WRONLY | O_CREAT | O_NONBLOCK, 0660);
                if (pidfd == -1) {
-                       logger (LOG_ERR, "open `%s': %s", options->pidfile, strerror (errno));
+                       logger (LOG_ERR, "open `%s': %s",
+                               options->pidfile, strerror (errno));
                        goto abort;
                }
 
-               /* Lock the file so that only one instance of dhcpcd runs on an interface */
+               /* Lock the file so that only one instance of dhcpcd runs
+                * on an interface */
                if (flock (pidfd, LOCK_EX | LOCK_NB) == -1) {
-                       logger (LOG_ERR, "flock `%s': %s", options->pidfile, strerror (errno));
+                       logger (LOG_ERR, "flock `%s': %s",
+                               options->pidfile, strerror (errno));
                        goto abort;
                }
 
                /* dhcpcd.sh should not interhit this fd */
                if ((i = fcntl (pidfd, F_GETFD, 0)) == -1 ||
-                       fcntl (pidfd, F_SETFD, i | FD_CLOEXEC) == -1)
+                   fcntl (pidfd, F_SETFD, i | FD_CLOEXEC) == -1)
                        logger (LOG_ERR, "fcntl: %s", strerror (errno));
 
                writepid (pidfd, getpid ());
@@ -607,6 +633,6 @@ abort:
 #endif
 
        logger (LOG_INFO, "exiting");
-       
+
        exit (retval);
 }
diff --git a/duid.c b/duid.c
index f29cc9c843dbb17fd8ce84f1f4725de4fa7cab79..ebb5965b8fff6631eba145eaf9cede0539ac4287 100644 (file)
--- a/duid.c
+++ b/duid.c
@@ -105,7 +105,7 @@ void get_duid (interface_t *iface)
        /* Finally, add the MAC address of the interface */
        memcpy (p, iface->hwaddr, iface->hwlen);
        p += iface->hwlen;
-       
+
        iface->duid_length = p - iface->duid;
 
        if (! (fp = fopen (DUIDFILE, "w")))
diff --git a/info.c b/info.c
index bf9ddbab89218e805e99f7686c87265f7adf4f28..51b4ac251d340b738a64b0e36422e8034b3be0be 100644 (file)
--- a/info.c
+++ b/info.c
@@ -77,7 +77,20 @@ static char *cleanmetas (const char *cstr)
        return (new);
 }
 
-static void print_clean (FILE *f, const char *name, const char *value) {
+
+static void print_addresses (FILE *f, const address_t *addresses)
+{
+       const address_t *addr;
+
+       for (addr = addresses; addr; addr = addr->next) {
+               fprintf (f, "%s", inet_ntoa (addr->address));
+               if (addr->next)
+                       fprintf (f, " ");
+       }
+}
+
+static void print_clean (FILE *f, const char *name, const char *value)
+{
        char *clean;
 
        if (! value)
@@ -89,11 +102,10 @@ static void print_clean (FILE *f, const char *name, const char *value) {
 }
 
 bool write_info(const interface_t *iface, const dhcp_t *dhcp,
-                               const options_t *options, bool overwrite)
+               const options_t *options, bool overwrite)
 {
        FILE *f;
        route_t *route;
-       address_t *address;
        struct stat sb;
 
        if (options->test)
@@ -104,7 +116,8 @@ bool write_info(const interface_t *iface, const dhcp_t *dhcp,
 
                logger (LOG_DEBUG, "writing %s", iface->infofile);
                if ((f = fopen (iface->infofile, "w")) == NULL) {
-                       logger (LOG_ERR, "fopen `%s': %s", iface->infofile, strerror (errno));
+                       logger (LOG_ERR, "fopen `%s': %s",
+                               iface->infofile, strerror (errno));
                        return (false);
                }
        }
@@ -151,11 +164,7 @@ bool write_info(const interface_t *iface, const dhcp_t *dhcp,
 
        if (dhcp->dnsservers) {
                fprintf (f, "DNSSERVERS='");
-               for (address = dhcp->dnsservers; address; address = address->next) {
-                       fprintf (f, "%s", inet_ntoa (address->address));
-                       if (address->next)
-                               fprintf (f, " ");
-               }
+               print_addresses (f, dhcp->dnsservers);
                fprintf (f, "'\n");
        }
 
@@ -168,22 +177,14 @@ bool write_info(const interface_t *iface, const dhcp_t *dhcp,
 
        if (dhcp->ntpservers) {
                fprintf (f, "NTPSERVERS='");
-               for (address = dhcp->ntpservers; address; address = address->next) {
-                       fprintf (f, "%s", inet_ntoa (address->address));
-                       if (address->next)
-                               fprintf (f, " ");
-               }
+               print_addresses (f, dhcp->ntpservers);
                fprintf (f, "'\n");
        }
 
        print_clean (f, "NISDOMAIN", dhcp->nisdomain);
        if (dhcp->nisservers) {
                fprintf (f, "NISSERVERS='");
-               for (address = dhcp->nisservers; address; address = address->next) {
-                       fprintf (f, "%s", inet_ntoa (address->address));
-                       if (address->next)
-                               fprintf (f, " ");
-               }
+               print_addresses (f, dhcp->nisservers);
                fprintf (f, "'\n");
        }
 
@@ -218,7 +219,8 @@ bool write_info(const interface_t *iface, const dhcp_t *dhcp,
                p = duid = xmalloc (iface->duid_length + 6);
                *p++ = 255;
 
-               /* IAID is 4 bytes, so if the interface name is 4 bytes then use it */
+               /* IAID is 4 bytes, so if the interface name is 4 bytes
+                * then use it */
                if (strlen (iface->name) == 4) {
                        memcpy (p, iface->name, 4);
                } else {
@@ -237,17 +239,20 @@ bool write_info(const interface_t *iface, const dhcp_t *dhcp,
 #endif
        else
                fprintf (f, "CLIENTID='%.2X:%s'\n", iface->family,
-                                hwaddr_ntoa (iface->hwaddr, iface->hwlen));
-       fprintf (f, "DHCPCHADDR='%s'\n", hwaddr_ntoa (iface->hwaddr, iface->hwlen));
+                        hwaddr_ntoa (iface->hwaddr, iface->hwlen));
+       fprintf (f, "DHCPCHADDR='%s'\n", hwaddr_ntoa (iface->hwaddr,
+                                                     iface->hwlen));
 
 #ifdef ENABLE_INFO_COMPAT
        /* Support the old .info settings if we need to */
        fprintf (f, "\n# dhcpcd-1.x and 2.x compatible variables\n");
        if (dhcp->dnsservers) {
+               address_t *addr;
+
                fprintf (f, "DNS='");
-               for (address = dhcp->dnsservers; address; address = address->next) {
-                       fprintf (f, "%s", inet_ntoa (address->address));
-                       if (address->next)
+               for (addr = dhcp->dnsservers; addr; addr = addr->next) {
+                       fprintf (f, "%s", inet_ntoa (addr->address));
+                       if (addr->next)
                                fprintf (f, ",");
                }
                fprintf (f, "'\n");
@@ -274,33 +279,33 @@ bool write_info(const interface_t *iface, const dhcp_t *dhcp,
 }
 
 static bool parse_address (struct in_addr *addr,
-                                                  const char *value, const char *var)
+                          const char *value, const char *var)
 {
        if (inet_aton (value, addr) == 0) {
                logger (LOG_ERR, "%s `%s': %s", var, value,
-                               strerror (errno));
+                       strerror (errno));
                return (false);
        }
        return (true);
 }
 
 static bool parse_uint (unsigned int *i,
-                                               const char *value, const char *var)
+                       const char *value, const char *var)
 {
        if (sscanf (value, "%u", i) != 1) {
                logger (LOG_ERR, "%s `%s': not a valid number",
-                               var, value);
+                       var, value);
                return (false);
        }
        return (true);
 }
 
 static bool parse_ushort (unsigned short *s,
-                                                 const char *value, const char *var)
+                         const char *value, const char *var)
 {
        if (sscanf (value, "%hu", s) != 1) {
                logger (LOG_ERR, "%s `%s': not a valid number",
-                               var, value);
+                       var, value);
                return (false);
        }
        return (true);
@@ -345,12 +350,13 @@ bool read_info (const interface_t *iface, dhcp_t *dhcp)
 
        if (stat (iface->infofile, &sb) != 0) {
                logger (LOG_ERR, "lease information file `%s' does not exist",
-                               iface->infofile);
+                       iface->infofile);
                return (false);
        }
 
        if (! (fp = fopen (iface->infofile, "r"))) {
-               logger (LOG_ERR, "fopen `%s': %s", iface->infofile, strerror (errno));
+               logger (LOG_ERR, "fopen `%s': %s",
+                       iface->infofile, strerror (errno));
                return (false);
        }
 
@@ -371,7 +377,7 @@ bool read_info (const interface_t *iface, dhcp_t *dhcp)
                /* Skip comments */
                if (*var == '#')
                        goto next;
-               
+
                /* If we don't have an equals sign then skip it */
                if (! (p = strchr (var, '=')))
                        goto next;      
@@ -410,7 +416,7 @@ bool read_info (const interface_t *iface, dhcp_t *dhcp)
 
                                if (! dest || ! net || ! gate) {
                                        logger (LOG_ERR, "read_info ROUTES `%s,%s,%s': invalid route",
-                                                       dest, net, gate);
+                                               dest, net, gate);
                                        goto next;
                                }
 
@@ -419,19 +425,19 @@ bool read_info (const interface_t *iface, dhcp_t *dhcp)
                                memset (route, 0, sizeof (route_t));
                                if (inet_aton (dest, &route->destination) == 0) {
                                        logger (LOG_ERR, "read_info ROUTES `%s': not a valid destination address",
-                                                       dest);
+                                               dest);
                                        free (route);
                                        goto next;
                                }
                                if (inet_aton (dest, &route->netmask) == 0) {
                                        logger (LOG_ERR, "read_info ROUTES `%s': not a valid netmask address",
-                                                       net);
+                                               net);
                                        free (route);
                                        goto next;
                                }
                                if (inet_aton (dest, &route->gateway) == 0) {
                                        logger (LOG_ERR, "read_info ROUTES `%s': not a valid gateway address",
-                                                       gate);
+                                               gate);
                                        free (route);
                                        goto next;
                                }
index 0e27450eb929a90762f2d68d9a8e7b923c8de58b..0225c00e03935944dfdf0215ef2a70dedf581d58 100644 (file)
@@ -110,7 +110,7 @@ int inet_cidrtoaddr (int cidr, struct in_addr *addr) {
        if (ocets > 0) {
                memset (&addr->s_addr, 255, ocets - 1);
                memset ((unsigned char *) &addr->s_addr + (ocets - 1),
-                               (256 - (1 << (32 - cidr) % 8)), 1);
+                       (256 - (1 << (32 - cidr) % 8)), 1);
        }
 
        return (0);
@@ -145,16 +145,16 @@ char *hwaddr_ntoa (const unsigned char *hwaddr, int hwlen)
                        *p ++= ':';
                p += snprintf (p, 3, "%.2x", hwaddr[i]);
        }
-               
+
        *p ++= '\0';
 
        return (buffer);
 }
 
 static int _do_interface (const char *ifname,
-                                                 unsigned char *hwaddr, int *hwlen,
-                                                 struct in_addr *addr,
-                                                 bool flush, bool get)
+                         unsigned char *hwaddr, int *hwlen,
+                         struct in_addr *addr,
+                         bool flush, bool get)
 {
        int s;
        struct ifconf ifc;
@@ -176,7 +176,8 @@ static int _do_interface (const char *ifname,
                ifc.ifc_buf = xmalloc (len);
                if (ioctl (s, SIOCGIFCONF, &ifc) == -1) {
                        if (errno != EINVAL || lastlen != 0) {
-                               logger (LOG_ERR, "ioctl SIOCGIFCONF: %s", strerror (errno));
+                               logger (LOG_ERR, "ioctl SIOCGIFCONF: %s",
+                                       strerror (errno));
                                close (s);
                                free (ifc.ifc_buf);     
                                return -1;
@@ -222,28 +223,34 @@ static int _do_interface (const char *ifname,
                if (hwaddr && hwlen && ifr->ifr_addr.sa_family == AF_LINK) {
                        struct sockaddr_dl sdl;
 
-                       memcpy (&sdl, &ifr->ifr_addr, sizeof (struct sockaddr_dl));
+                       memcpy (&sdl, &ifr->ifr_addr,
+                               sizeof (struct sockaddr_dl));
                        *hwlen = sdl.sdl_alen;
-                       memcpy (hwaddr, sdl.sdl_data + sdl.sdl_nlen, sdl.sdl_alen);
+                       memcpy (hwaddr, sdl.sdl_data + sdl.sdl_nlen,
+                               sdl.sdl_alen);
                        retval = 1;
                        break;
                }
 #endif
 
                if (ifr->ifr_addr.sa_family == AF_INET) {
-                       memcpy (&address, &ifr->ifr_addr, sizeof (struct sockaddr_in));
+                       memcpy (&address, &ifr->ifr_addr,
+                               sizeof (struct sockaddr_in));
                        if (flush) {
                                struct sockaddr_in netmask;
 
                                if (ioctl (s, SIOCGIFNETMASK, ifr) == -1) {
-                                       logger (LOG_ERR, "ioctl SIOCGIFNETMASK: %s",
-                                                       strerror (errno));
+                                       logger (LOG_ERR,
+                                               "ioctl SIOCGIFNETMASK: %s",
+                                               strerror (errno));
                                        continue;
                                }
-                               memcpy (&netmask, &ifr->ifr_addr, sizeof (struct sockaddr_in));
+                               memcpy (&netmask, &ifr->ifr_addr,
+                                       sizeof (struct sockaddr_in));
 
                                if (del_address (ifname,
-                                                                address.sin_addr, netmask.sin_addr) == -1)
+                                                address.sin_addr,
+                                                netmask.sin_addr) == -1)
                                        retval = -1;
                        } else if (get) {
                                addr->s_addr = address.sin_addr.s_addr;
@@ -306,7 +313,9 @@ interface_t *read_interface (const char *ifname, int metric)
                        hwlen = INFINIBAND_ADDR_LEN;
                        break;
                default:
-                       logger (LOG_ERR, "interface is not Ethernet, FireWire, InfiniBand or Token Ring");
+                       logger (LOG_ERR,
+                               "interface is not Ethernet, FireWire, " \
+                               "InfiniBand or Token Ring");
                        goto exit;
        }
 
@@ -337,11 +346,13 @@ interface_t *read_interface (const char *ifname, int metric)
        }
 
        if (ifr.ifr_mtu < MTU_MIN) {
-               logger (LOG_DEBUG, "MTU of %d is too low, setting to %d", ifr.ifr_mtu, MTU_MIN);
+               logger (LOG_DEBUG, "MTU of %d is too low, setting to %d",
+                       ifr.ifr_mtu, MTU_MIN);
                ifr.ifr_mtu = MTU_MIN;
                strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
                if (ioctl (s, SIOCSIFMTU, &ifr) == -1) {
-                       logger (LOG_ERR, "ioctl SIOCSIFMTU,: %s", strerror (errno));
+                       logger (LOG_ERR, "ioctl SIOCSIFMTU,: %s",
+                               strerror (errno));
                        goto exit;
                }
        }
@@ -361,7 +372,8 @@ interface_t *read_interface (const char *ifname, int metric)
        if (! (ifr.ifr_flags & IFF_UP) || ! (ifr.ifr_flags & IFF_RUNNING)) {
                ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
                if (ioctl (s, SIOCSIFFLAGS, &ifr) == -1) {
-                       logger (LOG_ERR, "ioctl SIOCSIFFLAGS: %s", strerror (errno));
+                       logger (LOG_ERR, "ioctl SIOCSIFFLAGS: %s",
+                               strerror (errno));
                        goto exit;
                }
        }
@@ -380,7 +392,7 @@ interface_t *read_interface (const char *ifname, int metric)
        iface->mtu = iface->previous_mtu = mtu;
 
        logger (LOG_INFO, "hardware address = %s",
-                       hwaddr_ntoa (iface->hwaddr, iface->hwlen));
+               hwaddr_ntoa (iface->hwaddr, iface->hwlen));
 
        /* 0 is a valid fd, so init to -1 */
        iface->fd = -1;
@@ -439,12 +451,11 @@ int set_mtu (const char *ifname, short int mtu)
        return (r == 0 ? 0 : -1);
 }
 
-static void log_route( 
-                                         struct in_addr destination,
-                                         struct in_addr netmask,
-                                         struct in_addr gateway,
-                                         int metric,
-                                         int change, int del)
+static void log_route (struct in_addr destination,
+                      struct in_addr netmask,
+                      struct in_addr gateway,
+                      int metric,
+                      int change, int del)
 {
        char *dstd = xstrdup (inet_ntoa (destination));
 
@@ -456,31 +467,31 @@ static void log_route(
 #endif
 
        if (gateway.s_addr == destination.s_addr ||
-               gateway.s_addr == INADDR_ANY)
+           gateway.s_addr == INADDR_ANY)
                logger (LOG_INFO, "%s route to %s/%d" METRIC,
-                               change ? "changing" : del ? "removing" : "adding",
-                               dstd, inet_ntocidr (netmask)
+                       change ? "changing" : del ? "removing" : "adding",
+                       dstd, inet_ntocidr (netmask)
 #ifdef __linux__
-                               , metric
+                       , metric
 #endif
-                          );
+                      );
        else if (destination.s_addr == INADDR_ANY)
                logger (LOG_INFO, "%s default route via %s" METRIC,
-                               change ? "changing" : del ? "removing" : "adding",
-                               inet_ntoa (gateway)
+                       change ? "changing" : del ? "removing" : "adding",
+                       inet_ntoa (gateway)
 
 #ifdef __linux__
-                               , metric
+                       , metric
 #endif
-                          );
+                      );
        else
                logger (LOG_INFO, "%s route to %s/%d via %s" METRIC,
-                               change ? "changing" : del ? "removing" : "adding",
-                               dstd, inet_ntocidr (netmask), inet_ntoa (gateway)
+                       change ? "changing" : del ? "removing" : "adding",
+                       dstd, inet_ntocidr (netmask), inet_ntoa (gateway)
 #ifdef __linux__
-                               , metric
+                       , metric
 #endif
-                          );
+                      );
 
        free (dstd);
 }
@@ -490,13 +501,14 @@ static void log_route(
 /* Darwin doesn't define this for some very odd reason */
 #ifndef SA_SIZE
 # define SA_SIZE(sa)                                           \
-    (  (!(sa) || ((struct sockaddr *)(sa))->sa_len == 0) ?     \
-       sizeof(long)            :                               \
-       1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) )
+       (  (!(sa) || ((struct sockaddr *)(sa))->sa_len == 0) ?  \
+          sizeof(long)         :                               \
+          1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) )
 #endif
 
 static int do_address (const char *ifname, struct in_addr address,
-                                          struct in_addr netmask, struct in_addr broadcast, int del)
+                      struct in_addr netmask, struct in_addr broadcast,
+                      int del)
 {
        int s;
        struct ifaliasreq ifa;
@@ -513,37 +525,38 @@ static int do_address (const char *ifname, struct in_addr address,
        strlcpy (ifa.ifra_name, ifname, sizeof (ifa.ifra_name));
 
 #define ADDADDR(_var, _addr) { \
-               union { struct sockaddr *sa; struct sockaddr_in *sin; } _s; \
-               _s.sa = &_var; \
-               _s.sin->sin_family = AF_INET; \
-               _s.sin->sin_len = sizeof (struct sockaddr_in); \
-               memcpy (&_s.sin->sin_addr, &_addr, sizeof (struct in_addr)); \
-       }
+       union { struct sockaddr *sa; struct sockaddr_in *sin; } _s; \
+       _s.sa = &_var; \
+       _s.sin->sin_family = AF_INET; \
+       _s.sin->sin_len = sizeof (struct sockaddr_in); \
+       memcpy (&_s.sin->sin_addr, &_addr, sizeof (struct in_addr)); \
+}
 
        ADDADDR (ifa.ifra_addr, address);
        ADDADDR (ifa.ifra_mask, netmask);
-       if (! del)
-               ADDADDR (ifa.ifra_broadaddr, broadcast);
+if (! del)
+       ADDADDR (ifa.ifra_broadaddr, broadcast);
 
 #undef ADDADDR
 
        if (ioctl (s, del ? SIOCDIFADDR : SIOCAIFADDR, &ifa) == -1) {
-               logger (LOG_ERR, "ioctl %s: %s", del ? "SIOCDIFADDR" : "SIOCAIFADDR",
-                               strerror (errno));
+               logger (LOG_ERR, "ioctl %s: %s",
+                       del ? "SIOCDIFADDR" : "SIOCAIFADDR",
+                       strerror (errno));
                close (s);
                return -1;
        }
 
-       close (s);
-       return 0;
+close (s);
+return 0;
 }
 
 static int do_route (const char *ifname,
-                                        struct in_addr destination,
-                                        struct in_addr netmask,
-                                        struct in_addr gateway,
-                                        int metric,
-                                        int change, int del)
+                    struct in_addr destination,
+                    struct in_addr netmask,
+                    struct in_addr gateway,
+                    int metric,
+                    int change, int del)
 {
        int s;
        struct rtm 
@@ -562,7 +575,7 @@ static int do_route (const char *ifname,
                struct sockaddr_dl sdl;
                struct sockaddr_storage ss;
        } su;
-       
+
        int l;
 
        if (! ifname)
@@ -597,7 +610,7 @@ static int do_route (const char *ifname,
        ADDADDR (destination);
 
        if (netmask.s_addr == INADDR_BROADCAST ||
-               gateway.s_addr == INADDR_ANY)
+           gateway.s_addr == INADDR_ANY)
        {
                /* Make us a link layer socket */
                unsigned char *hwaddr;
@@ -615,7 +628,7 @@ static int do_route (const char *ifname,
                memcpy (&su.sdl.sdl_data, ifname, su.sdl.sdl_nlen);
                su.sdl.sdl_alen = hwlen;
                memcpy (((unsigned char *) &su.sdl.sdl_data) + su.sdl.sdl_nlen,
-                               hwaddr, su.sdl.sdl_alen);
+                       hwaddr, su.sdl.sdl_alen);
 
                l = SA_SIZE (&(su.sa));
                memcpy (bp, &su, l);
@@ -708,7 +721,8 @@ static int send_netlink(struct nlmsghdr *hdr)
 
                if (bytes == -1) {
                        if (errno != EINTR)
-                               logger (LOG_ERR, "recvmsg: %s", strerror (errno));
+                               logger (LOG_ERR, "recvmsg: %s",
+                                       strerror (errno));
                        continue;
                }
 
@@ -718,7 +732,8 @@ static int send_netlink(struct nlmsghdr *hdr)
                }
 
                if (msg.msg_namelen != sizeof (nl)) {
-                       logger (LOG_ERR, "netlink: sender address length mismatch");
+                       logger (LOG_ERR,
+                               "netlink: sender address length mismatch");
                        goto eexit;
                }
 
@@ -737,18 +752,19 @@ static int send_netlink(struct nlmsghdr *hdr)
 
                        /* Ensure it's our message */
                        if (nl.nl_pid != 0 ||
-                               (pid_t) h.nlm->nlmsg_pid != mypid ||
-                               h.nlm->nlmsg_seq != seq)
+                           (pid_t) h.nlm->nlmsg_pid != mypid ||
+                           h.nlm->nlmsg_seq != seq)
                        {
                                /* Next Message */
                                bytes -= NLMSG_ALIGN (len);
                                h.buffer += NLMSG_ALIGN (len);
                                continue;
                        }
-                       
+
                        /* We get an NLMSG_ERROR back with a code of zero for success */
                        if (h.nlm->nlmsg_type != NLMSG_ERROR) {
-                               logger (LOG_ERR, "netlink: unexpected reply %d", h.nlm->nlmsg_type);
+                               logger (LOG_ERR, "netlink: unexpected reply %d",
+                                       h.nlm->nlmsg_type);
                                goto eexit;
                        }
 
@@ -766,7 +782,8 @@ static int send_netlink(struct nlmsghdr *hdr)
                        errno = -err->error;
                        /* Don't report on something already existing */
                        if (errno != EEXIST)
-                               logger (LOG_ERR, "netlink: %s", strerror (errno));
+                               logger (LOG_ERR, "netlink: %s",
+                                       strerror (errno));
                        goto eexit;
                }
        }
@@ -781,13 +798,14 @@ eexit:
        ((struct rtattr *) (((ptrdiff_t) (nmsg)) + NLMSG_ALIGN ((nmsg)->nlmsg_len)))
 
 static int add_attr_l(struct nlmsghdr *n, unsigned int maxlen, int type,
-                                         const void *data, int alen)
+                     const void *data, int alen)
 {
        int len = RTA_LENGTH(alen);
        struct rtattr *rta;
 
        if (NLMSG_ALIGN (n->nlmsg_len) + RTA_ALIGN (len) > maxlen) {
-               logger (LOG_ERR, "add_attr_l: message exceeded bound of %d\n", maxlen);
+               logger (LOG_ERR, "add_attr_l: message exceeded bound of %d\n",
+                       maxlen);
                return -1;
        }
 
@@ -801,13 +819,14 @@ static int add_attr_l(struct nlmsghdr *n, unsigned int maxlen, int type,
 }
 
 static int add_attr_32(struct nlmsghdr *n, unsigned int maxlen, int type,
-                                          uint32_t data)
+                      uint32_t data)
 {
        int len = RTA_LENGTH (sizeof (uint32_t));
        struct rtattr *rta;
 
        if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen) {
-               logger (LOG_ERR, "add_attr32: message exceeded bound of %d\n", maxlen);
+               logger (LOG_ERR, "add_attr32: message exceeded bound of %d\n",
+                       maxlen);
                return -1;
        }
 
@@ -835,8 +854,8 @@ struct nlmr
 };
 
 static int do_address(const char *ifname,
-                                         struct in_addr address, struct in_addr netmask,
-                                         struct in_addr broadcast, int del)
+                     struct in_addr address, struct in_addr netmask,
+                     struct in_addr broadcast, int del)
 {
        struct nlma *nlm;
        int retval;
@@ -853,8 +872,8 @@ static int do_address(const char *ifname,
                nlm->hdr.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
        nlm->hdr.nlmsg_type = del ? RTM_DELADDR : RTM_NEWADDR;
        if (! (nlm->ifa.ifa_index = if_nametoindex (ifname))) {
-               logger (LOG_ERR, "if_nametoindex: Couldn't find index for interface `%s'",
-                               ifname);
+               logger (LOG_ERR, "if_nametoindex: no index for interface `%s'",
+                       ifname);
                free (nlm);
                return -1;
        }
@@ -864,13 +883,13 @@ static int do_address(const char *ifname,
 
        /* This creates the aliased interface */
        add_attr_l (&nlm->hdr, sizeof (struct nlma), IFA_LABEL,
-                               ifname, strlen (ifname) + 1);
+                   ifname, strlen (ifname) + 1);
 
        add_attr_l (&nlm->hdr, sizeof (struct nlma), IFA_LOCAL,
-                               &address.s_addr, sizeof (address.s_addr));
+                   &address.s_addr, sizeof (address.s_addr));
        if (! del)
                add_attr_l (&nlm->hdr, sizeof (struct nlma), IFA_BROADCAST,
-                                       &broadcast.s_addr, sizeof (broadcast.s_addr));
+                           &broadcast.s_addr, sizeof (broadcast.s_addr));
 
        retval = send_netlink (&nlm->hdr);
        free (nlm);
@@ -878,10 +897,10 @@ static int do_address(const char *ifname,
 }
 
 static int do_route (const char *ifname,
-                                        struct in_addr destination,
-                                        struct in_addr netmask,
-                                        struct in_addr gateway,
-                                        int metric, int change, int del)
+                    struct in_addr destination,
+                    struct in_addr netmask,
+                    struct in_addr gateway,
+                    int metric, int change, int del)
 {
        struct nlmr *nlm;
        unsigned int ifindex;
@@ -893,8 +912,8 @@ static int do_route (const char *ifname,
        log_route (destination, netmask, gateway, metric, change, del);
 
        if (! (ifindex = if_nametoindex (ifname))) {
-               logger (LOG_ERR, "if_nametoindex: Couldn't find index for interface `%s'",
-                               ifname);
+               logger (LOG_ERR, "if_nametoindex: no index for interface `%s'",
+                       ifname);
                return -1;
        }
 
@@ -917,7 +936,7 @@ static int do_route (const char *ifname,
                nlm->hdr.nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
                nlm->rt.rtm_protocol = RTPROT_BOOT;
                if (netmask.s_addr == INADDR_BROADCAST ||
-                       gateway.s_addr == INADDR_ANY)
+                   gateway.s_addr == INADDR_ANY)
                        nlm->rt.rtm_scope = RT_SCOPE_LINK;
                else
                        nlm->rt.rtm_scope = RT_SCOPE_UNIVERSE;
@@ -926,11 +945,11 @@ static int do_route (const char *ifname,
 
        nlm->rt.rtm_dst_len = inet_ntocidr (netmask);
        add_attr_l (&nlm->hdr, sizeof (struct nlmr), RTA_DST,
-                               &destination.s_addr, sizeof (destination.s_addr));
+                   &destination.s_addr, sizeof (destination.s_addr));
        if (netmask.s_addr != INADDR_BROADCAST &&
-               destination.s_addr != gateway.s_addr)
+           destination.s_addr != gateway.s_addr)
                add_attr_l (&nlm->hdr, sizeof (struct nlmr), RTA_GATEWAY,
-                                       &gateway.s_addr, sizeof (gateway.s_addr));
+                           &gateway.s_addr, sizeof (gateway.s_addr));
 
        add_attr_32 (&nlm->hdr, sizeof (struct nlmr), RTA_OIF, ifindex);
        add_attr_32 (&nlm->hdr, sizeof (struct nlmr), RTA_PRIORITY, metric);
@@ -941,48 +960,47 @@ static int do_route (const char *ifname,
 }
 
 #else
-#error "Platform not supported!"
-#error "We currently support BPF and Linux sockets."
-#error "Other platforms may work using BPF. If yours does, please let me know"
-#error "so I can add it to our list."
+# error "Platform not supported!"
+# error "We currently support BPF and Linux sockets."
+# error "Other platforms may work using BPF. If yours does, please let me know"
+# error "so I can add it to our list."
 #endif
 
-
 int add_address (const char *ifname, struct in_addr address,
-                                struct in_addr netmask, struct in_addr broadcast)
+                struct in_addr netmask, struct in_addr broadcast)
 {
        logger (LOG_INFO, "adding IP address %s/%d",
-                       inet_ntoa (address), inet_ntocidr (netmask));
+               inet_ntoa (address), inet_ntocidr (netmask));
 
        return (do_address (ifname, address, netmask, broadcast, 0));
 }
 
 int del_address (const char *ifname,
-                                struct in_addr address, struct in_addr netmask)
+                struct in_addr address, struct in_addr netmask)
 {
        struct in_addr t;
 
        logger (LOG_INFO, "removing IP address %s/%d",
-                       inet_ntoa (address), inet_ntocidr (netmask));
+               inet_ntoa (address), inet_ntocidr (netmask));
 
        memset (&t, 0, sizeof (t));
        return (do_address (ifname, address, netmask, t, 1));
 }
 
 int add_route (const char *ifname, struct in_addr destination,
-                          struct in_addr netmask, struct in_addr gateway, int metric)
+              struct in_addr netmask, struct in_addr gateway, int metric)
 {
        return (do_route (ifname, destination, netmask, gateway, metric, 0, 0));
 }
 
 int change_route (const char *ifname, struct in_addr destination,
-                                 struct in_addr netmask, struct in_addr gateway, int metric)
+                 struct in_addr netmask, struct in_addr gateway, int metric)
 {
        return (do_route (ifname, destination, netmask, gateway, metric, 1, 0));
 }
 
 int del_route (const char *ifname, struct in_addr destination,
-                          struct in_addr netmask, struct in_addr gateway, int metric)
+              struct in_addr netmask, struct in_addr gateway, int metric)
 {
        return (do_route (ifname, destination, netmask, gateway, metric, 0, 1));
 }
index fd46c814d0300d1a6c2917490e7aaa9558aa237a..e6ecf87cfe8bfb84177b873354fb5e2b1a9b8d97 100644 (file)
--- a/ipv4ll.c
+++ b/ipv4ll.c
@@ -45,7 +45,7 @@ int ipv4ll_get_address (interface_t *iface, dhcp_t *dhcp) {
 
        while (1) {
                addr.s_addr = htonl (LINKLOCAL_ADDR |
-                                                        ((abs (random ()) % 0xFD00) + 0x0100));
+                                    ((abs (random ()) % 0xFD00) + 0x0100));
                errno = 0;
                if (! arp_claim (iface, addr))
                        break;
index 2cde258ae1ef426125058db3fcdff8f868a9482e..3d02f6a5f86e84bd7c153e184f90b0a7923f6864 100644 (file)
--- a/signal.c
+++ b/signal.c
@@ -46,8 +46,8 @@ static void signal_handler (int sig)
        unsigned int i = 0;
 
        /* Silently ignore this signal and wait for it. This stops zombies.
-          We do this here instead of client.c so that we don't spam the log file
-          with "waiting on select messages" */
+          We do this here instead of client.c so that we don't spam the log
+          file with "waiting on select messages" */
        if (sig == SIGCHLD) {
                wait (0);
                return;
@@ -72,15 +72,15 @@ int signal_fd_set (fd_set *rset, int fd)
        FD_SET (signal_pipe[0], rset);
        if (fd >= 0)
                FD_SET (fd, rset);
-       return signal_pipe[0] > fd ? signal_pipe[0] : fd;
+       return (signal_pipe[0] > fd ? signal_pipe[0] : fd);
 }
 
 /* Check if we have a signal or not */
 int signal_exists (const fd_set *rset)
 {
        if (signals[0] || (rset && FD_ISSET (signal_pipe[0], rset)))
-               return 0;
-       return -1;
+               return (0);
+       return (-1);
 }
 
 /* Read a signal from the signal pipe. Returns 0 if there is
@@ -118,7 +118,7 @@ int signal_read (fd_set *rset)
                        FD_CLR (signal_pipe[0], rset);
        }
 
-       return sig;
+       return (sig);
 }
 
 /* Call this before doing anything else. Sets up the socket pair
@@ -133,7 +133,7 @@ void signal_setup (void)
        /* Stop any scripts from inheriting us */
        for (i = 0; i < 2; i++)
                if ((flags = fcntl (signal_pipe[i], F_GETFD, 0)) == -1 ||
-                       fcntl (signal_pipe[i], F_SETFD, flags | FD_CLOEXEC) == -1)
+                   fcntl (signal_pipe[i], F_SETFD, flags | FD_CLOEXEC) == -1)
                        logger (LOG_ERR ,"fcntl: %s", strerror (errno));
 
        signal (SIGHUP, signal_handler);
index acc40cc701003e0330d79513021ebd20ef6bd0b7..27a3d533daca4ed866f751ff6cfe8e815a2e3fda 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -86,8 +86,8 @@ static uint16_t checksum (unsigned char *addr, uint16_t len)
 }
 
 void make_dhcp_packet(struct udp_dhcp_packet *packet,
-                                         const unsigned char *data, int length,
-                                         struct in_addr source, struct in_addr dest)
+                     const unsigned char *data, int length,
+                     struct in_addr source, struct in_addr dest)
 {
        struct ip *ip = &packet->ip;
        struct udphdr *udp = &packet->udp;
@@ -115,14 +115,14 @@ void make_dhcp_packet(struct udp_dhcp_packet *packet,
        udp->uh_ulen = htons (sizeof (struct udphdr) + length);
        ip->ip_len = udp->uh_ulen;
        udp->uh_sum = checksum ((unsigned char *) packet,
-                                                       sizeof (struct udp_dhcp_packet));
+                               sizeof (struct udp_dhcp_packet));
 
        ip->ip_v = IPVERSION;
        ip->ip_hl = 5;
        ip->ip_id = 0;
        ip->ip_tos = IPTOS_LOWDELAY;
        ip->ip_len = htons (sizeof (struct ip) + sizeof (struct udphdr) +
-                                               length);
+                           length);
        ip->ip_id = 0;
        ip->ip_off = 0;
        ip->ip_ttl = IPDEFTTL;
@@ -154,7 +154,7 @@ static int valid_dhcp_packet (unsigned char *data)
        d.data = data;
        d.packet->ip.ip_sum = 0;
        if (ipsum != checksum ((unsigned char *) &d.packet->ip,
-                                                  sizeof (struct ip)))
+                              sizeof (struct ip)))
        {
                logger (LOG_DEBUG, "bad IP header checksum, ignoring");
                retval = -1;
@@ -255,7 +255,7 @@ int open_socket (interface_t *iface, bool arp)
        }
 
        if ((flags = fcntl (fd, F_GETFD, 0)) == -1
-               || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1)
+           || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1)
        {
                logger (LOG_ERR, "fcntl: %s", strerror (errno));
                close (fd);
@@ -265,8 +265,9 @@ int open_socket (interface_t *iface, bool arp)
        memset (&ifr, 0, sizeof (struct ifreq));
        strlcpy (ifr.ifr_name, iface->name, sizeof (ifr.ifr_name));
        if (ioctl (fd, BIOCSETIF, &ifr) == -1) {
-               logger (LOG_ERR, "cannot attach interface `%s' to bpf device `%s': %s",
-                               iface->name, device, strerror (errno));
+               logger (LOG_ERR,
+                       "cannot attach interface `%s' to bpf device `%s': %s",
+                       iface->name, device, strerror (errno));
                close (fd);
                return -1;
        }
@@ -308,7 +309,7 @@ int open_socket (interface_t *iface, bool arp)
 }
 
 int send_packet (const interface_t *iface, int type,
-                                const unsigned char *data, int len)
+                const unsigned char *data, int len)
 {
        int retval = -1;
        struct iovec iov[2];
@@ -337,7 +338,7 @@ int send_packet (const interface_t *iface, int type,
 /* BPF requires that we read the entire buffer.
    So we pass the buffer in the API so we can loop on >1 dhcp packet. */
 int get_packet (const interface_t *iface, unsigned char *data,
-                               unsigned char *buffer, int *buffer_len, int *buffer_pos)
+               unsigned char *buffer, int *buffer_len, int *buffer_pos)
 {
        union
        {
@@ -373,7 +374,7 @@ int get_packet (const interface_t *iface, unsigned char *data,
 
                /* Ensure that the entire packet is in our buffer */
                if (*buffer_pos + bpf.packet->bh_hdrlen + bpf.packet->bh_caplen
-                       > (unsigned) *buffer_len)
+                   > (unsigned) *buffer_len)
                        break;
 
                hdr.buffer = bpf.buffer + bpf.packet->bh_hdrlen;
@@ -381,7 +382,8 @@ int get_packet (const interface_t *iface, unsigned char *data,
 
                /* If it's an ARP reply, then just send it back */
                if (hdr.hw->ether_type == htons (ETHERTYPE_ARP)) {
-                       len = bpf.packet->bh_caplen - sizeof (struct ether_header);
+                       len = bpf.packet->bh_caplen - 
+                               sizeof (struct ether_header);
                        memcpy (data, payload, len);
                } else {
                        if (valid_dhcp_packet (payload) >= 0) {
@@ -391,15 +393,16 @@ int get_packet (const interface_t *iface, unsigned char *data,
                                        struct udp_dhcp_packet *packet;
                                } pay;
                                pay.buffer = payload;
-                               len = ntohs (pay.packet->ip.ip_len) - sizeof (struct ip) -
+                               len = ntohs (pay.packet->ip.ip_len) -
+                                       sizeof (struct ip) -
                                        sizeof (struct udphdr);
                                memcpy (data, &pay.packet->dhcp, len);
                        }
                }
 
                /* Update the buffer_pos pointer */
-               bpf.buffer +=
-                       BPF_WORDALIGN (bpf.packet->bh_hdrlen + bpf.packet->bh_caplen);
+               bpf.buffer += BPF_WORDALIGN (bpf.packet->bh_hdrlen +
+                                            bpf.packet->bh_caplen);
                if (bpf.buffer - buffer <  *buffer_len)
                        *buffer_pos = bpf.buffer - buffer;
                else
@@ -437,7 +440,7 @@ int open_socket (interface_t *iface, bool arp)
        }
 
        if ((flags = fcntl (fd, F_GETFD, 0)) == -1
-               || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1)
+           || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1)
        {
                logger (LOG_ERR, "fcntl: %s", strerror (errno));
                close (fd);
@@ -451,8 +454,9 @@ int open_socket (interface_t *iface, bool arp)
        else
                su.sll.sll_protocol = htons (ETH_P_IP);
        if (! (su.sll.sll_ifindex = if_nametoindex (iface->name))) {
-               logger (LOG_ERR, "if_nametoindex: Couldn't find index for interface `%s'",
-                               iface->name);
+               logger (LOG_ERR,
+                       "if_nametoindex: no index for interface `%s'",
+                       iface->name);
                close (fd);
                return -1;
        }
@@ -475,7 +479,7 @@ int open_socket (interface_t *iface, bool arp)
 }
 
 int send_packet (const interface_t *iface, const int type,
-                                const unsigned char *data, const int len)
+                const unsigned char *data, const int len)
 {
        union sockunion {
                struct sockaddr sa;
@@ -491,15 +495,15 @@ int send_packet (const interface_t *iface, const int type,
        su.sll.sll_family = AF_PACKET;
        su.sll.sll_protocol = htons (type);
        if (! (su.sll.sll_ifindex = if_nametoindex (iface->name))) {
-               logger (LOG_ERR, "if_nametoindex: Couldn't find index for interface `%s'",
-                               iface->name);
+               logger (LOG_ERR, "if_nametoindex: no index for interface `%s'",
+                       iface->name);
                return -1;
        }
        su.sll.sll_halen = iface->hwlen;
        memset(&su.sll.sll_addr, 0xff, iface->hwlen);
 
        if ((retval = sendto (iface->fd, data, len, 0, &su.sa,
-                                                 sizeof (struct sockaddr_storage))) == -1)
+                             sizeof (struct sockaddr_storage))) == -1)
 
                logger (LOG_ERR, "sendto: %s", strerror (errno));
        return retval;
@@ -508,7 +512,7 @@ int send_packet (const interface_t *iface, const int type,
 /* Linux has no need for the buffer as we can read as much as we want.
    We only have the buffer listed to keep the same API. */
 int get_packet (const interface_t *iface, unsigned char *data,
-                               unsigned char *buffer, int *buffer_len, int *buffer_pos)
+               unsigned char *buffer, int *buffer_len, int *buffer_pos)
 {
        long bytes;
        union
@@ -553,12 +557,13 @@ int get_packet (const interface_t *iface, unsigned char *data,
        bytes = ntohs (pay.packet->ip.ip_len);
 
        /* This is like our BPF filter above */
-       if (pay.packet->ip.ip_p != IPPROTO_UDP || pay.packet->ip.ip_v != IPVERSION ||
-               pay.packet->ip.ip_hl != sizeof (pay.packet->ip) >> 2 ||
-               pay.packet->udp.uh_dport != htons (DHCP_CLIENT_PORT) ||
-               bytes > (int) sizeof (struct udp_dhcp_packet) ||
-               ntohs (pay.packet->udp.uh_ulen)
-               != (uint16_t) (bytes - sizeof (pay.packet->ip)))
+       if (pay.packet->ip.ip_p != IPPROTO_UDP ||
+           pay.packet->ip.ip_v != IPVERSION ||
+           pay.packet->ip.ip_hl != sizeof (pay.packet->ip) >> 2 ||
+           pay.packet->udp.uh_dport != htons (DHCP_CLIENT_PORT) ||
+           bytes > (int) sizeof (struct udp_dhcp_packet) ||
+           ntohs (pay.packet->udp.uh_ulen)
+           != (uint16_t) (bytes - sizeof (pay.packet->ip)))
        {
                return -1;
        }
@@ -567,7 +572,7 @@ int get_packet (const interface_t *iface, unsigned char *data,
                return -1;
 
        memcpy(data, &pay.packet->dhcp,
-                  bytes - (sizeof (pay.packet->ip) + sizeof (pay.packet->udp)));
+              bytes - (sizeof (pay.packet->ip) + sizeof (pay.packet->udp)));
 
        return bytes - (sizeof (pay.packet->ip) + sizeof (pay.packet->udp));
 }
index 6fe8ab39e926a3b52bd3038d8409b23919f48575..7bd9e8997bb54dc917b5c0ae066625794417cb40 100644 (file)
--- a/socket.h
+++ b/socket.h
 #include "interface.h"
 
 void make_dhcp_packet(struct udp_dhcp_packet *packet,
-                                         const unsigned char *data, int length,
-                                         struct in_addr source, struct in_addr dest);
+                     const unsigned char *data, int length,
+                     struct in_addr source, struct in_addr dest);
 
 int open_socket (interface_t *iface, bool arp);
 int send_packet (const interface_t *iface, int type,
-                                const unsigned char *data, int len);
+                const unsigned char *data, int len);
 int get_packet (const interface_t *iface, unsigned char *data,
-                               unsigned char *buffer, int *buffer_len, int *buffer_pos);
+               unsigned char *buffer, int *buffer_len, int *buffer_pos);
 #endif