From: Roy Marples Date: Thu, 20 Mar 2008 16:47:51 +0000 (+0000) Subject: Change code style to match the BSDs in the hope the might adpot it instead of dhclient. X-Git-Tag: v4.0.2~542 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8d212424765794740d61d4b4fe5db3f2f6800eb9;p=thirdparty%2Fdhcpcd.git Change code style to match the BSDs in the hope the might adpot it instead of dhclient. --- diff --git a/arp.c b/arp.c index 794850c7..b390fa3d 100644 --- a/arp.c +++ b/arp.c @@ -29,6 +29,7 @@ #include #include #include + #include #ifdef __linux__ #include @@ -37,6 +38,7 @@ #include #include #include + #include #include #include @@ -59,55 +61,57 @@ /* Linux does not seem to define these handy macros */ #ifndef ar_sha -#define ar_sha(ap) (((caddr_t) ((ap) + 1)) + 0) -#define ar_spa(ap) (((caddr_t) ((ap) + 1)) + (ap)->ar_hln) -#define ar_tha(ap) (((caddr_t) ((ap) + 1)) + (ap)->ar_hln + (ap)->ar_pln) -#define ar_tpa(ap) (((caddr_t) ((ap) + 1)) + 2 * (ap)->ar_hln + (ap)->ar_pln) +#define ar_sha(ap) (((caddr_t)((ap) + 1)) + 0) +#define ar_spa(ap) (((caddr_t)((ap) + 1)) + (ap)->ar_hln) +#define ar_tha(ap) (((caddr_t)((ap) + 1)) + (ap)->ar_hln + (ap)->ar_pln) +#define ar_tpa(ap) (((caddr_t)((ap) + 1)) + 2 * (ap)->ar_hln + (ap)->ar_pln) #endif #ifndef arphdr_len -#define arphdr_len2(ar_hln, ar_pln) (sizeof (struct arphdr) + \ +#define arphdr_len2(ar_hln, ar_pln) (sizeof(struct arphdr) + \ 2 * (ar_hln) + 2 * (ar_pln)) -#define arphdr_len(ap) (arphdr_len2 ((ap)->ar_hln, (ap)->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) +static int +send_arp(const struct interface *iface, int op, struct in_addr sip, + const unsigned char *taddr, struct in_addr tip) { struct arphdr *arp; - size_t arpsize = arphdr_len2 (iface->hwlen, sizeof (sip)); + size_t arpsize = arphdr_len2(iface->hwlen, sizeof(sip)); caddr_t tha; int retval; - arp = xzalloc (arpsize); - arp->ar_hrd = htons (iface->family); - arp->ar_pro = htons (ETHERTYPE_IP); + arp = xzalloc(arpsize); + arp->ar_hrd = htons(iface->family); + arp->ar_pro = htons(ETHERTYPE_IP); arp->ar_hln = iface->hwlen; - arp->ar_pln = sizeof (sip); - arp->ar_op = htons (op); - memcpy (ar_sha (arp), iface->hwaddr, (size_t) arp->ar_hln); - memcpy (ar_spa (arp), &sip, (size_t) arp->ar_pln); + arp->ar_pln = sizeof(sip); + arp->ar_op = htons(op); + memcpy(ar_sha(arp), iface->hwaddr, (size_t)arp->ar_hln); + memcpy(ar_spa(arp), &sip, (size_t)arp->ar_pln); if (taddr) { /* NetBSD can return NULL from ar_tha, which is probably wrong * but we still need to deal with it */ - if (! (tha = ar_tha (arp))) { - free (arp); + if (! (tha = ar_tha(arp))) { + free(arp); errno = EINVAL; - return (-1); + return -1; } - memcpy (tha, taddr, (size_t) arp->ar_hln); + memcpy(tha, taddr, (size_t)arp->ar_hln); } - memcpy (ar_tpa (arp), &tip, (size_t) arp->ar_pln); + memcpy(ar_tpa(arp), &tip, (size_t)arp->ar_pln); - retval = send_packet (iface, ETHERTYPE_ARP, - (unsigned char *) arp, arphdr_len (arp)); - free (arp); - return (retval); + retval = send_packet(iface, ETHERTYPE_ARP, + (unsigned char *) arp, arphdr_len(arp)); + free(arp); + return retval; } -int arp_claim (interface_t *iface, struct in_addr address) +int +arp_claim(struct interface *iface, struct in_addr address) { struct arphdr *reply = NULL; long timeout = 0; @@ -120,54 +124,59 @@ int arp_claim (interface_t *iface, struct in_addr address) { -1, POLLIN, 0 }, { -1, POLLIN, 0 } }; - - if (! iface) - return (-1); - - if (! iface->arpable) { - logger (LOG_DEBUG, "interface `%s' is not ARPable", iface->name); - return (0); + size_t bufpos = 0; + size_t buflen = iface->buffer_length; + int bytes; + int s = 0; + struct timeval stopat; + struct timeval now; + union { + unsigned char *c; + struct in_addr *a; + } rp; + union { + unsigned char *c; + struct ether_addr *a; + } rh; + + if (!iface->arpable) { + logger(LOG_DEBUG, "interface `%s' is not ARPable", iface->name); + return 0; } - 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)); + 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)); - if (! open_socket (iface, ETHERTYPE_ARP)) - return (-1); + if (!open_socket(iface, ETHERTYPE_ARP)) + return -1; - fds[0].fd = signal_fd (); + fds[0].fd = signal_fd(); fds[1].fd = iface->fd; - - memset (&null_address, 0, sizeof (null_address)); - - buffer = xmalloc (iface->buffer_length); - reply = xmalloc (iface->buffer_length); + memset(&null_address, 0, sizeof(null_address)); + buffer = xmalloc(iface->buffer_length); + reply = xmalloc(iface->buffer_length); for (;;) { - size_t bufpos = 0; - size_t buflen = iface->buffer_length; - int bytes; - int s = 0; - struct timeval stopat; - struct timeval now; + bufpos = 0; + buflen = iface->buffer_length; + s = 0; /* Only poll if we have a timeout */ if (timeout > 0) { - s = poll (fds, 2, timeout); + s = poll(fds, 2, timeout); if (s == -1) { if (errno == EINTR) { - if (signal_exists (NULL) == -1) { + if (signal_exists(NULL) == -1) { errno = 0; continue; } else break; } - logger (LOG_ERR, "poll: `%s'", - strerror (errno)); + logger(LOG_ERR, "poll: `%s'", strerror(errno)); break; } } @@ -177,11 +186,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); - if (send_arp (iface, ARPOP_REQUEST, - null_address, NULL, - address) == -1) + logger(LOG_DEBUG, "sending ARP probe #%d", + nprobes); + if (send_arp(iface, ARPOP_REQUEST, + null_address, NULL, + address) == -1) break; /* IEEE1394 cannot set ARP target address @@ -192,11 +201,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); - if (send_arp (iface, ARPOP_REQUEST, - address, iface->hwaddr, - address) == -1) + logger(LOG_DEBUG, "sending ARP claim #%d", + nclaims); + if (send_arp(iface, ARPOP_REQUEST, + address, iface->hwaddr, + address) == -1) break; } else { /* No replies, so done */ @@ -205,7 +214,7 @@ int arp_claim (interface_t *iface, struct in_addr address) } /* Setup our stop time */ - if (get_time (&stopat) != 0) + if (get_time(&stopat) != 0) break; stopat.tv_usec += timeout; @@ -213,48 +222,39 @@ int arp_claim (interface_t *iface, struct in_addr address) } /* We maybe ARP flooded, so check our time */ - if (get_time (&now) != 0) + if (get_time(&now) != 0) break; - if (timercmp (&now, &stopat, >)) { + if (timercmp(&now, &stopat, >)) { timeout = 0; continue; } - if (! fds[1].revents & POLLIN) + if (!(fds[1].revents & POLLIN)) continue; - memset (buffer, 0, buflen); + memset(buffer, 0, buflen); do { - union { - unsigned char *c; - struct in_addr *a; - } rp; - union { - unsigned char *c; - struct ether_addr *a; - } rh; - - memset (reply, 0, iface->buffer_length); - if ((bytes = get_packet (iface, (unsigned char *) reply, - buffer, - &buflen, &bufpos)) == -1) + memset(reply, 0, iface->buffer_length); + if ((bytes = get_packet(iface, (unsigned char *) reply, + buffer, + &buflen, &bufpos)) == -1) break; /* Only these types are recognised */ - if (reply->ar_op != htons (ARPOP_REPLY)) + if (reply->ar_op != htons(ARPOP_REPLY)) continue; /* Protocol must be IP. */ - if (reply->ar_pro != htons (ETHERTYPE_IP)) + if (reply->ar_pro != htons(ETHERTYPE_IP)) continue; - if (reply->ar_pln != sizeof (address)) + if (reply->ar_pln != sizeof(address)) continue; - if ((unsigned) bytes < sizeof (reply) + - 2 * (4 + reply->ar_hln)) + if ((unsigned)bytes < sizeof(reply) + + 2 * (4 +reply->ar_hln)) continue; - rp.c = (unsigned char *) ar_spa (reply); - rh.c = (unsigned char *) ar_sha (reply); + rp.c = (unsigned char *)ar_spa(reply); + rh.c = (unsigned char *)ar_sha(reply); /* Ensure the ARP reply is for the our address */ if (rp.a->s_addr != address.s_addr) @@ -263,22 +263,22 @@ int arp_claim (interface_t *iface, struct in_addr address) /* Some systems send a reply back from our hwaddress, * which is wierd */ 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, (size_t) reply->ar_hln)); + logger(LOG_ERR, "ARPOP_REPLY received from %s (%s)", + inet_ntoa(*rp.a), + hwaddr_ntoa(rh.c, (size_t)reply->ar_hln)); retval = -1; goto eexit; } while (bufpos != 0); } eexit: - close (iface->fd); + close(iface->fd); iface->fd = -1; - free (buffer); - free (reply); - return (retval); + free(buffer); + free(reply); + return retval; } #endif diff --git a/arp.h b/arp.h index 3b7e8ef5..e8928ce7 100644 --- a/arp.h +++ b/arp.h @@ -33,7 +33,7 @@ #include "interface.h" -int arp_claim (interface_t *iface, struct in_addr address); +int arp_claim(struct interface *, struct in_addr); #endif #endif diff --git a/client.c b/client.c index 8ce27211..23dd9701 100644 --- a/client.c +++ b/client.c @@ -28,9 +28,11 @@ #include #include #include + #ifdef __linux__ # include #endif + #include #include #include @@ -105,14 +107,14 @@ #define POLLFD_SIGNAL 0 #define POLLFD_IFACE 1 -typedef struct _state { +struct if_state { int *pidfd; bool forked; int state; uint32_t xid; - dhcp_t *dhcp; + struct dhcp *dhcp; int socket; - interface_t *interface; + struct interface *interface; time_t start; time_t last_sent; time_t last_type; @@ -123,9 +125,10 @@ typedef struct _state { unsigned char *buffer; size_t buffer_len; size_t buffer_pos; -} state_t; +}; -static pid_t daemonise (int *pidfd) +static pid_t +daemonise(int *pidfd) { pid_t pid; sigset_t full; @@ -135,154 +138,156 @@ static pid_t daemonise (int *pidfd) int i; #endif - sigfillset (&full); - sigprocmask (SIG_SETMASK, &full, &old); + sigfillset(&full); + sigprocmask(SIG_SETMASK, &full, &old); #ifndef THERE_IS_NO_FORK - logger (LOG_DEBUG, "forking to background"); + logger(LOG_DEBUG, "forking to background"); switch (pid = fork()) { case -1: - logger (LOG_ERR, "fork: %s", strerror (errno)); - exit (EXIT_FAILURE); + logger(LOG_ERR, "fork: %s", strerror(errno)); + exit(EXIT_FAILURE); /* NOT REACHED */ case 0: - setsid (); - close_fds (); + setsid(); + close_fds(); break; default: - /* Reset our signals as we're the parent about to exit. */ - signal_reset (); + /* Reset signals as we're the parent about to exit. */ + signal_reset(); break; } #else - logger (LOG_INFO, "forking to background"); + logger(LOG_INFO, "forking to background"); /* We need to add --daemonise to our options */ - argv = xmalloc (sizeof (char *) * (dhcpcd_argc + 4)); + argv = xmalloc (sizeof(char *) * (dhcpcd_argc + 4)); argv[0] = dhcpcd; for (i = 1; i < dhcpcd_argc; i++) argv[i] = dhcpcd_argv[i]; - argv[i] = (char *) "--daemonised"; + argv[i] = (char *)"--daemonised"; if (dhcpcd_skiproutes) { - argv[++i] = (char *) "--skiproutes"; + argv[++i] = (char *)"--skiproutes"; argv[++i] = dhcpcd_skiproutes; } argv[i + 1] = NULL; - switch (pid = vfork ()) { + switch (pid = vfork()) { case -1: - logger (LOG_ERR, "vfork: %s", strerror (errno)); - _exit (EXIT_FAILURE); + logger(LOG_ERR, "vfork: %s", strerror (errno)); + _exit(EXIT_FAILURE); case 0: - signal_reset (); - sigprocmask (SIG_SETMASK, &old, NULL); - execvp (dhcpcd, argv); - logger (LOG_ERR, "execl `%s': %s", dhcpcd, - strerror (errno)); - _exit (EXIT_FAILURE); + signal_reset(); + sigprocmask(SIG_SETMASK, &old, NULL); + execvp(dhcpcd, argv); + logger(LOG_ERR, "execl `%s': %s", dhcpcd, + strerror(errno)); + _exit(EXIT_FAILURE); } - free (argv); + free(argv); #endif /* Done with the fd now */ if (pid != 0) { - writepid (*pidfd, pid); - close (*pidfd); + writepid(*pidfd, pid); + close(*pidfd); *pidfd = -1; } - sigprocmask (SIG_SETMASK, &old, NULL); - return (pid); + sigprocmask(SIG_SETMASK, &old, NULL); + return pid; } #ifdef ENABLE_INFO -static bool get_old_lease (state_t *state, const options_t *options) +static bool +get_old_lease(struct if_state *state, const struct options *options) { - interface_t *iface = state->interface; - dhcp_t *dhcp = state->dhcp; + struct interface *iface = state->interface; + struct dhcp *dhcp = state->dhcp; struct timeval tv; unsigned int offset = 0; - if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) - logger (LOG_INFO, "trying to use old lease in `%s'", - iface->infofile); - if (! read_info (iface, dhcp)) - return (false); + if (!IN_LINKLOCAL(ntohl(iface->previous_address.s_addr))) + logger(LOG_INFO, "trying to use old lease in `%s'", + iface->infofile); + if (!read_info(iface, dhcp)) + return false; /* Vitaly important we remove the server information here */ - memset (&dhcp->serveraddress, 0, sizeof (dhcp->serveraddress)); - memset (dhcp->servername, 0, sizeof (dhcp->servername)); + memset(&dhcp->serveraddress, 0, sizeof(dhcp->serveraddress)); + memset(dhcp->servername, 0, sizeof(dhcp->servername)); #ifdef ENABLE_ARP /* Check that no-one is using the address */ if ((options->dolastlease || - (IN_LINKLOCAL (ntohl (dhcp->address.s_addr)) && - (! options->doipv4ll || - arp_claim (iface, dhcp->address))))) + (IN_LINKLOCAL(ntohl (dhcp->address.s_addr)) && + (!options->doipv4ll || + arp_claim(iface, dhcp->address))))) { - memset (&dhcp->address, 0, sizeof (dhcp->address)); - memset (&dhcp->netmask, 0, sizeof (dhcp->netmask)); - memset (&dhcp->broadcast, 0, sizeof (dhcp->broadcast)); - return (false); + memset(&dhcp->address, 0, sizeof(dhcp->address)); + memset(&dhcp->netmask, 0, sizeof(dhcp->netmask)); + memset(&dhcp->broadcast, 0, sizeof(dhcp->broadcast)); + return false; } /* Ok, lets use this */ - if (IN_LINKLOCAL (dhcp->address.s_addr)) - return (true); + if (IN_LINKLOCAL(dhcp->address.s_addr)) + return true; #endif /* Ensure that we can still use the lease */ - if (gettimeofday (&tv, NULL) == -1) { - logger (LOG_ERR, "gettimeofday: %s", strerror (errno)); - return (false); + if (gettimeofday(&tv, NULL) == -1) { + logger(LOG_ERR, "gettimeofday: %s", strerror(errno)); + return false; } offset = tv.tv_sec - dhcp->leasedfrom; if (dhcp->leasedfrom && tv.tv_sec - dhcp->leasedfrom > dhcp->leasetime) { - logger (LOG_ERR, "lease expired %u seconds ago", - offset + dhcp->leasetime); - return (false); + logger(LOG_ERR, "lease expired %u seconds ago", + offset + dhcp->leasetime); + return false; } if (dhcp->leasedfrom == 0) offset = 0; state->timeout = dhcp->renewaltime - offset; - iface->start_uptime = uptime (); - return (true); + iface->start_uptime = uptime(); + return true; } #endif #ifdef THERE_IS_NO_FORK -static void remove_skiproutes (dhcp_t *dhcp, interface_t *iface) +static void +remove_skiproutes(struct dhcp *dhcp, struct interface *iface) { int i = -1; - route_t *route; - route_t *newroute; + struct route *route; + struct route *newroute; + char *sk; + char *skp; + char *token; - free_route (iface->previous_routes); + free_route(iface->previous_routes); iface->previous_routes = NULL; - NSTAILQ_FOREACH (route, dhcp->routes, entries) { + NSTAILQ_FOREACH(route, dhcp->routes, entries) { i++; /* Check that we did add this route or not */ if (dhcpcd_skiproutes) { - char *sk = xstrdup (dhcpcd_skiproutes); - char *skp = sk; - char *token; - bool found = false; - - while ((token = strsep (&skp, ","))) { - if (isdigit (*token) && atoi (token) == i) { + sk = skp = xstrdup (dhcpcd_skiproutes); + found = false; + while ((token = strsep(&skp, ","))) { + if (isdigit(*token) && atoi(token) == i) { found = true; break; } } - free (sk); + free(sk); if (found) continue; } @@ -292,21 +297,22 @@ static void remove_skiproutes (dhcp_t *dhcp, interface_t *iface) STAILQ_INIT (iface->previous_routes); } - newroute = xmalloc (sizeof (*newroute)); - memcpy (newroute, route, sizeof (*newroute)); - STAILQ_INSERT_TAIL (iface->previous_routes, newroute, entries); + newroute = xmalloc(sizeof (*newroute)); + memcpy(newroute, route, sizeof(*newroute)); + STAILQ_INSERT_TAIL(iface->previous_routes, newroute, entries); } /* We no longer need this argument */ - free (dhcpcd_skiproutes); + free(dhcpcd_skiproutes); dhcpcd_skiproutes = NULL; } #endif -static bool client_setup (state_t *state, const options_t *options) +static bool +client_setup(struct if_state *state, const struct options *options) { - dhcp_t *dhcp = state->dhcp; - interface_t *iface = state->interface; + struct dhcp *dhcp = state->dhcp; + struct interface *iface = state->interface; state->state = STATE_INIT; state->last_type = DHCP_DISCOVER; @@ -318,19 +324,19 @@ static bool client_setup (state_t *state, const options_t *options) (options->doinform || options->dorequest || options->daemonised)) { #ifdef ENABLE_INFO - if (! get_old_lease (state, options)) + if (!get_old_lease(state, options)) #endif { - free (dhcp); - return (false); + free(dhcp); + return false; } state->timeout = 0; - if (! options->daemonised && - IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) + if (!options->daemonised && + IN_LINKLOCAL(ntohl(dhcp->address.s_addr))) { - logger (LOG_ERR, "cannot request a link local address"); - return (false); + logger(LOG_ERR, "cannot request a link local address"); + return false; } #ifdef THERE_IS_NO_FORK if (options->daemonised) { @@ -338,7 +344,7 @@ static bool client_setup (state_t *state, const options_t *options) state->timeout = dhcp->renewaltime; iface->previous_address = dhcp->address; iface->previous_netmask = dhcp->netmask; - remove_skiproutes (dhcp, iface); + remove_skiproutes(dhcp, iface); } #endif @@ -346,7 +352,7 @@ static bool client_setup (state_t *state, const options_t *options) dhcp->address = options->request_address; dhcp->netmask = options->request_netmask; if (dhcp->netmask.s_addr == 0) - dhcp->netmask.s_addr = get_netmask (dhcp->address.s_addr); + dhcp->netmask.s_addr = get_netmask(dhcp->address.s_addr); dhcp->broadcast.s_addr = dhcp->address.s_addr | ~dhcp->netmask.s_addr; } @@ -355,17 +361,17 @@ static bool client_setup (state_t *state, const options_t *options) * After all, we ARE a DHCP client whose job it is to configure the * interface. We only do this on start, so persistent addresses * can be added afterwards by the user if needed. */ - if (! options->test && ! options->daemonised) { - if (! options->doinform) { + if (!options->test && !options->daemonised) { + if (!options->doinform) { flush_addresses (iface->name); } else { /* The inform address HAS to be configured for it to * work with most DHCP servers */ if (options->doinform && - has_address (iface->name, dhcp->address) < 1) + has_address(iface->name, dhcp->address) < 1) { - add_address (iface->name, dhcp->address, - dhcp->netmask, dhcp->broadcast); + add_address(iface->name, dhcp->address, + dhcp->netmask, dhcp->broadcast); iface->previous_address = dhcp->address; iface->previous_netmask = dhcp->netmask; } @@ -374,126 +380,133 @@ static bool client_setup (state_t *state, const options_t *options) if (*options->clientid) { /* Attempt to see if the ClientID is a hardware address */ - iface->clientid_len = hwaddr_aton (NULL, options->clientid); + iface->clientid_len = hwaddr_aton(NULL, options->clientid); if (iface->clientid_len) { - iface->clientid = xmalloc (iface->clientid_len); - hwaddr_aton (iface->clientid, options->clientid); + iface->clientid = xmalloc(iface->clientid_len); + hwaddr_aton(iface->clientid, options->clientid); } else { /* Nope, so mark it as-is */ - iface->clientid_len = strlen (options->clientid) + 1; - iface->clientid = xmalloc (iface->clientid_len); + iface->clientid_len = strlen(options->clientid) + 1; + iface->clientid = xmalloc(iface->clientid_len); *iface->clientid = '\0'; - memcpy (iface->clientid + 1, - options->clientid, iface->clientid_len - 1); + memcpy(iface->clientid + 1, + options->clientid, iface->clientid_len - 1); } } else { #ifdef ENABLE_DUID unsigned char *duid = NULL; size_t duid_len = 0; + uint32_t ul; if (options->doduid) { - duid = xmalloc (DUID_LEN); - duid_len = get_duid (duid, iface); + duid = xmalloc(DUID_LEN); + duid_len = get_duid(duid, iface); } if (duid_len > 0) { - logger (LOG_INFO, "DUID = %s", hwaddr_ntoa (duid, duid_len)); + logger(LOG_INFO, "DUID = %s", + hwaddr_ntoa(duid, duid_len)); iface->clientid_len = duid_len + 5; - iface->clientid = xmalloc (iface->clientid_len); + iface->clientid = xmalloc(iface->clientid_len); *iface->clientid = 255; /* RFC 4361 */ - /* IAID is 4 bytes, so if the iface name is 4 bytes use it */ - if (strlen (iface->name) == 4) { - memcpy (iface->clientid + 1, iface->name, 4); + /* IAID is 4 bytes, so if the iface name is 4 bytes + * use it */ + if (strlen(iface->name) == 4) { + memcpy(iface->clientid + 1, iface->name, 4); } else { /* Name isn't 4 bytes, so use the index */ - uint32_t ul = htonl (if_nametoindex (iface->name)); - memcpy (iface->clientid + 1, &ul, 4); + ul = htonl(if_nametoindex(iface->name)); + memcpy(iface->clientid + 1, &ul, 4); } - memcpy (iface->clientid + 5, duid, duid_len); - free (duid); + memcpy(iface->clientid + 5, duid, duid_len); + free(duid); } else { #else { #endif iface->clientid_len = iface->hwlen + 1; - iface->clientid = xmalloc (iface->clientid_len); + iface->clientid = xmalloc(iface->clientid_len); *iface->clientid = iface->family; - memcpy (iface->clientid + 1, iface->hwaddr, iface->hwlen); + memcpy(iface->clientid + 1, iface->hwaddr, iface->hwlen); } } - return (true); + return true; } -static bool do_socket (state_t *state, int mode) +static bool +do_socket(struct if_state *state, int mode) { if (state->interface->fd >= 0) - close (state->interface->fd); + close(state->interface->fd); #ifdef __linux if (mode == SOCKET_CLOSED && state->interface->listen_fd >= 0) { - close (state->interface->listen_fd); + close(state->interface->listen_fd); state->interface->listen_fd = -1; } #endif state->interface->fd = -1; if (mode == SOCKET_OPEN) - if (open_socket (state->interface, ETHERTYPE_IP) == -1) - return (false); + if (open_socket(state->interface, ETHERTYPE_IP) == -1) + return false; state->socket = mode; - return (true); + return true; } -static bool _send_message (state_t *state, int type, const options_t *options) +static bool +_send_message(struct if_state *state, int type, const struct options *options) { ssize_t retval; state->last_type = type; - state->last_sent = uptime (); - retval = send_message (state->interface, state->dhcp, state->xid, - type, options); - return (retval == -1 ? false : true); + state->last_sent = uptime(); + retval = send_message(state->interface, state->dhcp, state->xid, + type, options); + return retval == -1 ? false : true; } -static void drop_config (state_t *state, const options_t *options) +static void +drop_config(struct if_state *state, const struct options *options) { if (! state->persistent) - configure (options, state->interface, state->dhcp, false); + configure(options, state->interface, state->dhcp, false); - free_dhcp (state->dhcp); - memset (state->dhcp, 0, sizeof (*state->dhcp)); + free_dhcp(state->dhcp); + memset(state->dhcp, 0, sizeof(*state->dhcp)); } -static int wait_for_packet (struct pollfd *fds, state_t *state, - const options_t *options) +static int +wait_for_packet(struct pollfd *fds, struct if_state *state, + const struct options *options) { - dhcp_t *dhcp = state->dhcp; - interface_t *iface = state->interface; + struct dhcp *dhcp = state->dhcp; + struct interface *iface = state->interface; int timeout = 0; int retval = 0; - if (! (state->timeout > 0 || - (options->timeout == 0 && - (state->state != STATE_INIT || state->xid)))) { + if (!(state->timeout > 0 || + (options->timeout == 0 && + (state->state != STATE_INIT || state->xid)))) { /* We need to zero our signal fd, otherwise we will block * trying to read a signal. */ fds[POLLFD_SIGNAL].revents = 0; - return (0); + return 0; } fds[POLLFD_IFACE].fd = iface->fd; if ((options->timeout == 0 && state->xid) || - (dhcp->leasetime == (unsigned) -1 && + (dhcp->leasetime == (unsigned)-1 && state->state == STATE_BOUND)) { - logger (LOG_DEBUG, "waiting for infinity"); + logger(LOG_DEBUG, "waiting for infinity"); while (retval == 0) { if (iface->fd == -1) - retval = poll (fds, 1, INFTIM); + retval = poll(fds, 1, INFTIM); else { /* Slow down our requests */ if (timeout < TIMEOUT_MINI_INF) @@ -501,7 +514,7 @@ static int wait_for_packet (struct pollfd *fds, state_t *state, else if (timeout > TIMEOUT_MINI_INF) timeout = TIMEOUT_MINI_INF; - retval = poll (fds, 2, timeout * 1000); + retval = poll(fds, 2, timeout * 1000); if (retval == -1 && errno == EINTR) { /* If interupted, continue as normal as * the signal will be delivered down @@ -510,37 +523,36 @@ static int wait_for_packet (struct pollfd *fds, state_t *state, continue; } if (retval == 0) - _send_message (state, state->last_type, - options); + _send_message(state, state->last_type, + options); } } - return (retval); + return retval; } /* Resend our message if we're getting loads of packets. * As we use BPF or LPF, we shouldn't hit this as much, but it's * still nice to have. */ - if (iface->fd > -1 && uptime () - state->last_sent >= TIMEOUT_MINI) - _send_message (state, state->last_type, options); + if (iface->fd > -1 && uptime() - state->last_sent >= TIMEOUT_MINI) + _send_message(state, state->last_type, options); - logger (LOG_DEBUG, "waiting for %ld seconds", - (unsigned long) state->timeout); + logger(LOG_DEBUG, "waiting for %lu seconds", state->timeout); /* If we're waiting for a reply, then we re-send the last * DHCP request periodically in-case of a bad line */ retval = 0; while (state->timeout > 0 && retval == 0) { if (iface->fd == -1) - timeout = (int) state->timeout; + timeout = (int)state->timeout; else { timeout = TIMEOUT_MINI; if (state->timeout < timeout) - timeout = (int) state->timeout; + timeout = (int)state->timeout; } timeout *= 1000; - state->start = uptime (); - retval = poll (fds, iface->fd == -1 ? 1 : 2, timeout); - state->timeout -= uptime () - state->start; + state->start = uptime(); + retval = poll(fds, iface->fd == -1 ? 1 : 2, timeout); + state->timeout -= uptime() - state->start; if (retval == -1 && errno == EINTR) { /* If interupted, continue as normal as the signal * will be delivered down the pipe */ @@ -548,127 +560,130 @@ static int wait_for_packet (struct pollfd *fds, state_t *state, continue; } if (retval == 0 && iface->fd != -1 && state->timeout > 0) - _send_message (state, state->last_type, options); + _send_message(state, state->last_type, options); } - return (retval); + return retval; } -static bool handle_signal (int sig, state_t *state, const options_t *options) +static bool +handle_signal (int sig, struct if_state *state, const struct options *options) { switch (sig) { - case SIGINT: - logger (LOG_INFO, "received SIGINT, stopping"); - return (false); - case SIGTERM: - logger (LOG_INFO, "received SIGTERM, stopping"); - return (false); - - case SIGALRM: - logger (LOG_INFO, "received SIGALRM, renewing lease"); - switch (state->state) { - case STATE_BOUND: - case STATE_RENEWING: - case STATE_REBINDING: - state->state = STATE_RENEW_REQUESTED; - break; - case STATE_RENEW_REQUESTED: - case STATE_REQUESTING: - case STATE_RELEASED: - state->state = STATE_INIT; - break; - } - state->timeout = 0; - state->xid = 0; - return (true); + case SIGINT: + logger(LOG_INFO, "received SIGINT, stopping"); + return false; + case SIGTERM: + logger(LOG_INFO, "received SIGTERM, stopping"); + return false; + + case SIGALRM: + logger (LOG_INFO, "received SIGALRM, renewing lease"); + switch (state->state) { + case STATE_BOUND: + case STATE_RENEWING: + case STATE_REBINDING: + state->state = STATE_RENEW_REQUESTED; + break; + case STATE_RENEW_REQUESTED: + case STATE_REQUESTING: + case STATE_RELEASED: + state->state = STATE_INIT; + break; + } + state->timeout = 0; + state->xid = 0; + return true; - case SIGHUP: - if (state->state != STATE_BOUND && - state->state != STATE_RENEWING && - state->state != STATE_REBINDING) - { - logger (LOG_ERR, - "received SIGHUP, but we no have lease to release"); - return (false); - } + case SIGHUP: + if (state->state != STATE_BOUND && + state->state != STATE_RENEWING && + state->state != STATE_REBINDING) + { + logger(LOG_ERR, + "received SIGHUP, but no lease to release"); + return false; + } - logger (LOG_INFO, "received SIGHUP, releasing lease"); - if (! IN_LINKLOCAL (ntohl (state->dhcp->address.s_addr))) { - do_socket (state, SOCKET_OPEN); - state->xid = (uint32_t) random (); - if ((open_socket (state->interface, false)) >= 0) - _send_message (state, DHCP_RELEASE, options); - do_socket (state, SOCKET_CLOSED); + logger (LOG_INFO, "received SIGHUP, releasing lease"); + if (!IN_LINKLOCAL(ntohl(state->dhcp->address.s_addr))) { + do_socket(state, SOCKET_OPEN); + state->xid = (uint32_t)random(); + if ((open_socket(state->interface, false)) >= 0) + _send_message(state, DHCP_RELEASE, + options); + do_socket(state, SOCKET_CLOSED); } - unlink (state->interface->infofile); - return (false); + unlink(state->interface->infofile); + return false; - default: - logger (LOG_ERR, - "received signal %d, but don't know what to do with it", - sig); + default: + logger (LOG_ERR, + "received signal %d, but don't know what to do with it", + sig); } - return (false); + return false; } -static int handle_timeout (state_t *state, const options_t *options) +static int +handle_timeout (struct if_state *state, const struct options *options) { - dhcp_t *dhcp = state->dhcp; - interface_t *iface = state->interface; + struct dhcp *dhcp = state->dhcp; + struct interface *iface = state->interface; /* No NAK, so reset the backoff */ state->nakoff = 1; if (state->state == STATE_INIT && state->xid != 0) { if (iface->previous_address.s_addr != 0 && - ! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr)) && - ! options->doinform) + !IN_LINKLOCAL(ntohl(iface->previous_address.s_addr)) && + !options->doinform) { - logger (LOG_ERR, "lost lease"); - if (! options->persistent) - drop_config (state, options); - } else if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) - logger (LOG_ERR, "timed out"); + logger(LOG_ERR, "lost lease"); + if (!options->persistent) + drop_config(state, options); + } else if (!IN_LINKLOCAL(ntohl(iface->previous_address.s_addr))) + logger(LOG_ERR, "timed out"); - do_socket (state, SOCKET_CLOSED); - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); + do_socket(state, SOCKET_CLOSED); + free_dhcp(dhcp); + memset(dhcp, 0, sizeof(*dhcp)); #ifdef ENABLE_INFO - if (! options->test && + if (!options->test && (options->doipv4ll || options->dolastlease)) { errno = 0; - if (! get_old_lease (state, options)) + if (!get_old_lease (state, options)) { if (errno == EINTR) - return (0); + return 0; if (options->dolastlease) - return (-1); - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); + return -1; + free_dhcp(dhcp); + memset(dhcp, 0, sizeof(*dhcp)); } else if (errno == EINTR) - return (0); + return 0; } #endif #ifdef ENABLE_IPV4LL - if (! options->test && options->doipv4ll && - (! dhcp->address.s_addr || - (! IN_LINKLOCAL (ntohl (dhcp->address.s_addr)) && - ! options->dolastlease))) + if (!options->test && options->doipv4ll && + (!dhcp->address.s_addr || + (!IN_LINKLOCAL(ntohl(dhcp->address.s_addr)) && + !options->dolastlease))) { - logger (LOG_INFO, "probing for an IPV4LL address"); - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); - if (ipv4ll_get_address (iface, dhcp) == -1) { - if (! state->daemonised) - return (-1); + logger(LOG_INFO, "probing for an IPV4LL address"); + free_dhcp(dhcp); + memset(dhcp, 0, sizeof (*dhcp)); + if (ipv4ll_get_address(iface, dhcp) == -1) { + if (!state->daemonised) + return -1; /* start over */ state->xid = 0; - return (0); + return 0; } state->timeout = dhcp->renewaltime; } @@ -676,215 +691,216 @@ static int handle_timeout (state_t *state, const options_t *options) #if defined (ENABLE_INFO) || defined (ENABLE_IPV4LL) if (dhcp->address.s_addr) { - if (! state->daemonised && - IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) - logger (LOG_WARNING, "using IPV4LL address %s", - inet_ntoa (dhcp->address)); - if (configure (options, iface, dhcp, true) == -1 && - ! state->daemonised) - return (-1); + if (!state->daemonised && + IN_LINKLOCAL(ntohl(dhcp->address.s_addr))) + logger(LOG_WARNING, "using IPV4LL address %s", + inet_ntoa(dhcp->address)); + if (configure(options, iface, dhcp, true) == -1 && + !state->daemonised) + return -1; state->state = STATE_BOUND; - if (! state->daemonised && options->daemonise) { + if (!state->daemonised && options->daemonise) { switch (daemonise (state->pidfd)) { - case -1: - return (-1); - case 0: - state->daemonised = true; - return (0); - default: - state->persistent = true; - state->forked = true; - return (-1); + case -1: + return -1; + case 0: + state->daemonised = true; + return 0; + default: + state->persistent = true; + state->forked = true; + return -1; } } state->timeout = dhcp->renewaltime; state->xid = 0; - return (0); + return 0; } #endif - if (! state->daemonised) - return (-1); + if (!state->daemonised) + return -1; } switch (state->state) { - case STATE_INIT: - state->xid = (uint32_t) random (); - do_socket (state, SOCKET_OPEN); - state->timeout = options->timeout; - iface->start_uptime = uptime (); - if (dhcp->address.s_addr == 0) { - if (! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) - logger (LOG_INFO, "broadcasting for a lease"); + case STATE_INIT: + state->xid = (uint32_t) random (); + do_socket (state, SOCKET_OPEN); + state->timeout = options->timeout; + iface->start_uptime = uptime (); + if (dhcp->address.s_addr == 0) { + if (!IN_LINKLOCAL(ntohl(iface->previous_address.s_addr))) + logger(LOG_INFO, "broadcasting for a lease"); _send_message (state, DHCP_DISCOVER, options); - } else if (options->doinform) { - logger (LOG_INFO, "broadcasting inform for %s", - inet_ntoa (dhcp->address)); - _send_message (state, DHCP_INFORM, options); - state->state = STATE_REQUESTING; - } else { - logger (LOG_INFO, "broadcasting for a lease of %s", - inet_ntoa (dhcp->address)); - _send_message (state, DHCP_REQUEST, options); - state->state = STATE_REQUESTING; - } - - break; - case STATE_BOUND: - case STATE_RENEW_REQUESTED: - if (IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) { - memset (&dhcp->address, 0, sizeof (dhcp->address)); - state->state = STATE_INIT; - state->xid = 0; - break; - } - state->state = STATE_RENEWING; - state->xid = (uint32_t) random (); - /* FALLTHROUGH */ - case STATE_RENEWING: - iface->start_uptime = uptime (); - logger (LOG_INFO, "renewing lease of %s", inet_ntoa - (dhcp->address)); - do_socket (state, SOCKET_OPEN); - _send_message (state, DHCP_REQUEST, options); - state->timeout = dhcp->rebindtime - dhcp->renewaltime; - state->state = STATE_REBINDING; - break; - case STATE_REBINDING: - logger (LOG_ERR, "lost lease, attemping to rebind"); - memset (&dhcp->address, 0, sizeof (dhcp->address)); - do_socket (state, SOCKET_OPEN); - if (state->xid == 0) - state->xid = (uint32_t) random (); - dhcp->serveraddress.s_addr = 0; - _send_message (state, DHCP_REQUEST, options); - state->timeout = dhcp->leasetime - dhcp->rebindtime; + } else if (options->doinform) { + logger(LOG_INFO, "broadcasting inform for %s", + inet_ntoa(dhcp->address)); + _send_message(state, DHCP_INFORM, options); state->state = STATE_REQUESTING; - break; - case STATE_REQUESTING: - state->state = STATE_INIT; - do_socket (state, SOCKET_CLOSED); - state->timeout = 0; - break; + } else { + logger(LOG_INFO, "broadcasting for a lease of %s", + inet_ntoa (dhcp->address)); + _send_message(state, DHCP_REQUEST, options); + state->state = STATE_REQUESTING; + } - case STATE_RELEASED: - dhcp->leasetime = 0; + break; + case STATE_BOUND: + case STATE_RENEW_REQUESTED: + if (IN_LINKLOCAL(ntohl (dhcp->address.s_addr))) { + memset(&dhcp->address, 0, sizeof(dhcp->address)); + state->state = STATE_INIT; + state->xid = 0; break; + } + state->state = STATE_RENEWING; + state->xid = (uint32_t) random (); + /* FALLTHROUGH */ + case STATE_RENEWING: + iface->start_uptime = uptime(); + logger(LOG_INFO, "renewing lease of %s", + inet_ntoa(dhcp->address)); + do_socket(state, SOCKET_OPEN); + _send_message(state, DHCP_REQUEST, options); + state->timeout = dhcp->rebindtime - dhcp->renewaltime; + state->state = STATE_REBINDING; + break; + case STATE_REBINDING: + logger(LOG_ERR, "lost lease, attemping to rebind"); + memset(&dhcp->address, 0, sizeof(dhcp->address)); + do_socket(state, SOCKET_OPEN); + if (state->xid == 0) + state->xid = (uint32_t)random(); + dhcp->serveraddress.s_addr = 0; + _send_message(state, DHCP_REQUEST, options); + state->timeout = dhcp->leasetime - dhcp->rebindtime; + state->state = STATE_REQUESTING; + break; + case STATE_REQUESTING: + state->state = STATE_INIT; + do_socket(state, SOCKET_CLOSED); + state->timeout = 0; + break; + case STATE_RELEASED: + dhcp->leasetime = 0; + break; } - return (0); + return 0; } -static int handle_dhcp (state_t *state, int type, const options_t *options) +static int +handle_dhcp(struct if_state *state, int type, const struct options *options) { struct timespec ts; - interface_t *iface = state->interface; - dhcp_t *dhcp = state->dhcp; + struct interface *iface = state->interface; + struct dhcp *dhcp = state->dhcp; + char *addr; /* We should restart on a NAK */ if (type == DHCP_NAK) { - logger (LOG_INFO, "received NAK: %s", dhcp->message); + logger(LOG_INFO, "received NAK: %s", dhcp->message); state->state = STATE_INIT; state->timeout = 0; state->xid = 0; - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); + free_dhcp(dhcp); + memset(dhcp, 0, sizeof(*dhcp)); /* If we constantly get NAKS then we should slowly back off */ if (state->nakoff > 0) { - logger (LOG_DEBUG, "sleeping for %ld seconds", - (long) state->nakoff); + logger(LOG_DEBUG, "sleeping for %lu seconds", + (unsigned long)state->nakoff); ts.tv_sec = state->nakoff; ts.tv_nsec = 0; state->nakoff *= 2; if (state->nakoff > NAKOFF_MAX) state->nakoff = NAKOFF_MAX; - nanosleep (&ts, NULL); + nanosleep(&ts, NULL); } - return (0); + return 0; } /* No NAK, so reset the backoff */ state->nakoff = 1; if (type == DHCP_OFFER && state->state == STATE_INIT) { - char *addr = strdup (inet_ntoa (dhcp->address)); + addr = strdup(inet_ntoa(dhcp->address)); if (dhcp->servername[0]) - logger (LOG_INFO, "offered %s from %s `%s'", - addr, inet_ntoa (dhcp->serveraddress), - dhcp->servername); + logger(LOG_INFO, "offered %s from %s `%s'", + addr, inet_ntoa(dhcp->serveraddress), + dhcp->servername); else - logger (LOG_INFO, "offered %s from %s", - addr, inet_ntoa (dhcp->serveraddress)); - free (addr); + logger(LOG_INFO, "offered %s from %s", + addr, inet_ntoa(dhcp->serveraddress)); + free(addr); #ifdef ENABLE_INFO if (options->test) { - write_info (iface, dhcp, options, false); + write_info(iface, dhcp, options, false); errno = 0; - return (-1); + return -1; } #endif - _send_message (state, DHCP_REQUEST, options); + _send_message(state, DHCP_REQUEST, options); state->state = STATE_REQUESTING; - return (0); + return 0; } if (type == DHCP_OFFER) { - logger (LOG_INFO, "got subsequent offer of %s, ignoring ", - inet_ntoa (dhcp->address)); - return (0); + logger(LOG_INFO, "got subsequent offer of %s, ignoring ", + inet_ntoa(dhcp->address)); + return 0; } /* We should only be dealing with acks */ if (type != DHCP_ACK) { - logger (LOG_ERR, "%d not an ACK or OFFER", type); - return (0); + logger(LOG_ERR, "%d not an ACK or OFFER", type); + return 0; } switch (state->state) { - case STATE_RENEW_REQUESTED: - case STATE_REQUESTING: - case STATE_RENEWING: - case STATE_REBINDING: - break; - default: - logger (LOG_ERR, "wrong state %d", state->state); + case STATE_RENEW_REQUESTED: + case STATE_REQUESTING: + case STATE_RENEWING: + case STATE_REBINDING: + break; + default: + logger (LOG_ERR, "wrong state %d", state->state); } - do_socket (state, SOCKET_CLOSED); + do_socket(state, SOCKET_CLOSED); #ifdef ENABLE_ARP - if (options->doarp && iface->previous_address.s_addr != - dhcp->address.s_addr) + if (options->doarp && + iface->previous_address.s_addr != dhcp->address.s_addr) { errno = 0; - if (arp_claim (iface, dhcp->address)) { - do_socket (state, SOCKET_OPEN); - _send_message (state, DHCP_DECLINE, options); - do_socket (state, SOCKET_CLOSED); + if (arp_claim(iface, dhcp->address)) { + do_socket(state, SOCKET_OPEN); + _send_message(state, DHCP_DECLINE, options); + do_socket(state, SOCKET_CLOSED); - free_dhcp (dhcp); - memset (dhcp, 0, sizeof (*dhcp)); + free_dhcp(dhcp); + memset(dhcp, 0, sizeof(*dhcp)); state->xid = 0; state->timeout = 0; state->state = STATE_INIT; /* RFC 2131 says that we should wait for 10 seconds * before doing anything else */ - logger (LOG_INFO, "sleeping for 10 seconds"); + logger(LOG_INFO, "sleeping for 10 seconds"); ts.tv_sec = 10; ts.tv_nsec = 0; - nanosleep (&ts, NULL); - return (0); + nanosleep(&ts, NULL); + return 0; } else if (errno == EINTR) - return (0); + return 0; } #endif @@ -894,66 +910,66 @@ static int handle_dhcp (state_t *state, int type, const options_t *options) else dhcp->address = iface->previous_address; - logger (LOG_INFO, "received approval for %s", - inet_ntoa (dhcp->address)); + logger(LOG_INFO, "received approval for %s", + inet_ntoa(dhcp->address)); if (iface->previous_netmask.s_addr != dhcp->netmask.s_addr) { - add_address (iface->name, dhcp->address, - dhcp->netmask, dhcp->broadcast); + add_address(iface->name, dhcp->address, + dhcp->netmask, dhcp->broadcast); iface->previous_netmask.s_addr = dhcp->netmask.s_addr; } state->timeout = options->leasetime; if (state->timeout == 0) state->timeout = DEFAULT_LEASETIME; state->state = STATE_INIT; - } else if (dhcp->leasetime == (unsigned) -1) { + } else if (dhcp->leasetime == (unsigned)-1) { dhcp->renewaltime = dhcp->rebindtime = dhcp->leasetime; state->timeout = 1; /* So we wait for infinity */ - logger (LOG_INFO, "leased %s for infinity", - inet_ntoa (dhcp->address)); + logger(LOG_INFO, "leased %s for infinity", + inet_ntoa(dhcp->address)); state->state = STATE_BOUND; } else { - if (! dhcp->leasetime) { + if (!dhcp->leasetime) { dhcp->leasetime = DEFAULT_LEASETIME; logger(LOG_INFO, "no lease time supplied, assuming %d seconds", dhcp->leasetime); } - logger (LOG_INFO, "leased %s for %u seconds", - inet_ntoa (dhcp->address), dhcp->leasetime); + logger(LOG_INFO, "leased %s for %u seconds", + inet_ntoa(dhcp->address), dhcp->leasetime); if (dhcp->rebindtime >= dhcp->leasetime) { dhcp->rebindtime = (dhcp->leasetime * 0.875); - logger (LOG_ERR, - "rebind time greater than lease " - "time, forcing to %u seconds", - dhcp->rebindtime); + logger(LOG_ERR, + "rebind time greater than lease " + "time, forcing to %u seconds", + dhcp->rebindtime); } if (dhcp->renewaltime > dhcp->rebindtime) { dhcp->renewaltime = (dhcp->leasetime * 0.5); - logger (LOG_ERR, - "renewal time greater than rebind time, " - "forcing to %u seconds", - dhcp->renewaltime); + logger(LOG_ERR, + "renewal time greater than rebind time, " + "forcing to %u seconds", + dhcp->renewaltime); } - if (! dhcp->renewaltime) { + if (!dhcp->renewaltime) { dhcp->renewaltime = (dhcp->leasetime * 0.5); - logger (LOG_INFO, - "no renewal time supplied, assuming %d seconds", - dhcp->renewaltime); + logger(LOG_INFO, + "no renewal time supplied, assuming %d seconds", + dhcp->renewaltime); } else - logger (LOG_DEBUG, "renew in %u seconds", - dhcp->renewaltime); + logger(LOG_DEBUG, "renew in %u seconds", + dhcp->renewaltime); - if (! dhcp->rebindtime) { + if (!dhcp->rebindtime) { dhcp->rebindtime = (dhcp->leasetime * 0.875); - logger (LOG_INFO, - "no rebind time supplied, assuming %d seconds", - dhcp->rebindtime); + logger(LOG_INFO, + "no rebind time supplied, assuming %d seconds", + dhcp->rebindtime); } else - logger (LOG_DEBUG, "rebind in %u seconds", - dhcp->rebindtime); + logger(LOG_DEBUG, "rebind in %u seconds", + dhcp->rebindtime); state->timeout = dhcp->renewaltime; state->state = STATE_BOUND; @@ -961,40 +977,41 @@ static int handle_dhcp (state_t *state, int type, const options_t *options) state->xid = 0; - if (configure (options, iface, dhcp, true) == -1 && - ! state->daemonised) - return (-1); - - if (! state->daemonised && options->daemonise) { - switch (daemonise (state->pidfd)) { - case 0: - state->daemonised = true; - return (0); - case -1: - return (-1); - default: - state->persistent = true; - state->forked = true; - return (-1); + if (configure(options, iface, dhcp, true) == -1 && + !state->daemonised) + return -1; + + if (!state->daemonised && options->daemonise) { + switch (daemonise(state->pidfd)) { + case 0: + state->daemonised = true; + return 0; + case -1: + return -1; + default: + state->persistent = true; + state->forked = true; + return -1; } } - return (0); + return 0; } -static int handle_packet (state_t *state, const options_t *options) +static int +handle_packet(struct if_state *state, const struct options *options) { - interface_t *iface = state->interface; + struct interface *iface = state->interface; bool valid = false; int type; - struct dhcp_t *new_dhcp; - dhcpmessage_t message; + struct dhcp *new_dhcp; + struct dhcp_message message; /* Allocate our buffer space for BPF. * We cannot do this until we have opened our socket as we don't * know how much of a buffer we need until then. */ - if (! state->buffer) - state->buffer = xmalloc (iface->buffer_length); + if (!state->buffer) + state->buffer = xmalloc(iface->buffer_length); state->buffer_len = iface->buffer_length; state->buffer_pos = 0; @@ -1002,30 +1019,31 @@ static int handle_packet (state_t *state, const options_t *options) * The benefit is that if we get >1 DHCP packet in our buffer and * the first one fails for any reason, we can use the next. */ - memset (&message, 0, sizeof (message)); - new_dhcp = xmalloc (sizeof (*new_dhcp)); + memset(&message, 0, sizeof(message)); + new_dhcp = xmalloc(sizeof(*new_dhcp)); do { - if (get_packet (iface, (unsigned char *) &message, - state->buffer, - &state->buffer_len, &state->buffer_pos) == -1) + if (get_packet(iface, (unsigned char *)&message, + state->buffer, + &state->buffer_len, &state->buffer_pos) == -1) break; if (state->xid != message.xid) { - logger (LOG_DEBUG, - "ignoring packet with xid 0x%x as it's not ours (0x%x)", - message.xid, state->xid); + logger(LOG_DEBUG, + "ignoring packet with xid 0x%x as" + " it's not ours (0x%x)", + message.xid, state->xid); continue; } - logger (LOG_DEBUG, "got a packet with xid 0x%x", message.xid); - memset (new_dhcp, 0, sizeof (*new_dhcp)); - type = parse_dhcpmessage (new_dhcp, &message); + logger(LOG_DEBUG, "got a packet with xid 0x%x", message.xid); + memset(new_dhcp, 0, sizeof(*new_dhcp)); + type = parse_dhcpmessage(new_dhcp, &message); if (type == -1) { - logger (LOG_ERR, "failed to parse packet"); - free_dhcp (new_dhcp); + logger(LOG_ERR, "failed to parse packet"); + free_dhcp(new_dhcp); /* We don't abort on this, so return zero */ - return (0); + return 0; } /* If we got here then the DHCP packet is valid and appears to @@ -1036,24 +1054,25 @@ static int handle_packet (state_t *state, const options_t *options) } while (state->buffer_pos != 0); /* No packets for us, so wait until we get one */ - if (! valid) { - free (new_dhcp); - return (0); + if (!valid) { + free(new_dhcp); + return 0; } /* new_dhcp is now our master DHCP message */ - free_dhcp (state->dhcp); - free (state->dhcp); + free_dhcp(state->dhcp); + free(state->dhcp); state->dhcp = new_dhcp; new_dhcp = NULL; - return (handle_dhcp (state, type, options)); + return handle_dhcp(state, type, options); } -int dhcp_run (const options_t *options, int *pidfd) +int +dhcp_run(const struct options *options, int *pidfd) { - interface_t *iface; - state_t *state = NULL; + struct interface *iface; + struct if_state *state = NULL; struct pollfd fds[] = { { -1, POLLIN, 0 }, { -1, POLLIN, 0 } @@ -1061,48 +1080,45 @@ int dhcp_run (const options_t *options, int *pidfd) int retval = -1; int sig; - if (! options) - return (-1); - - iface = read_interface (options->interface, options->metric); - if (! iface) + iface = read_interface(options->interface, options->metric); + if (!iface) goto eexit; - state = xzalloc (sizeof (*state)); - state->dhcp = xzalloc (sizeof (*state->dhcp)); + state = xzalloc(sizeof(*state)); + state->dhcp = xzalloc(sizeof(*state->dhcp)); state->pidfd = pidfd; state->interface = iface; - if (! client_setup (state, options)) + if (!client_setup(state, options)) goto eexit; - if (signal_init () == -1) + if (signal_init() == -1) goto eexit; - if (signal_setup () == -1) + if (signal_setup() == -1) goto eexit; - fds[POLLFD_SIGNAL].fd = signal_fd (); + fds[POLLFD_SIGNAL].fd = signal_fd(); for (;;) { - retval = wait_for_packet (fds, state, options); + retval = wait_for_packet(fds, state, options); /* We should always handle our signals first */ - if ((sig = (signal_read (&fds[POLLFD_SIGNAL]))) != -1) { - if (handle_signal (sig, state, options)) + if ((sig = (signal_read(&fds[POLLFD_SIGNAL]))) != -1) { + if (handle_signal(sig, state, options)) retval = 0; else retval = -1; } else if (retval == 0) - retval = handle_timeout (state, options); + retval = handle_timeout(state, options); else if (retval > 0 && state->socket != SOCKET_CLOSED && fds[POLLFD_IFACE].revents & POLLIN) - retval = handle_packet (state, options); + retval = handle_packet(state, options); else if (retval == -1 && errno == EINTR) { /* The interupt will be handled above */ retval = 0; } else { - logger (LOG_ERR, "poll: %s", strerror (errno)); + logger(LOG_ERR, "poll: %s", strerror(errno)); retval = -1; } @@ -1112,11 +1128,11 @@ int dhcp_run (const options_t *options, int *pidfd) eexit: if (iface) { - do_socket (state, SOCKET_CLOSED); - drop_config (state, options); - free_route (iface->previous_routes); - free (iface->clientid); - free (iface); + do_socket(state, SOCKET_CLOSED); + drop_config(state, options); + free_route(iface->previous_routes); + free(iface->clientid); + free(iface); } if (state) { @@ -1124,13 +1140,13 @@ eexit: retval = 0; if (state->daemonised) - unlink (options->pidfile); + unlink(options->pidfile); - free_dhcp (state->dhcp); - free (state->dhcp); - free (state->buffer); - free (state); + free_dhcp(state->dhcp); + free(state->dhcp); + free(state->buffer); + free(state); } - return (retval); + return retval; } diff --git a/client.h b/client.h index fa6ea9bb..35a5e37c 100644 --- a/client.h +++ b/client.h @@ -30,6 +30,6 @@ #include "dhcpcd.h" -int dhcp_run (const options_t *options, int *pidfd); +int dhcp_run(const struct options *, int *); #endif diff --git a/common.c b/common.c index 99471bce..94fe290b 100644 --- a/common.c +++ b/common.c @@ -26,6 +26,7 @@ */ #include + #include #include #include @@ -39,105 +40,109 @@ /* Handy routine to read very long lines in text files. * This means we read the whole line and avoid any nasty buffer overflows. */ -char *get_line (FILE *fp) +ssize_t +get_line(char **line, size_t *len, FILE *fp) { - char *line = NULL; char *p; - size_t len = 0; size_t last = 0; - if (feof (fp)) - return (NULL); + if (feof(fp)) + return 0; do { - len += BUFSIZ; - line = xrealloc (line, sizeof (char) * len); - p = line + last; - memset (p, 0, BUFSIZ); - fgets (p, BUFSIZ, fp); - last += strlen (p); - } while (! feof (fp) && line[last - 1] != '\n'); + if (*line == NULL || last != 0) { + *len += BUFSIZ; + *line = xrealloc(*line, *len); + } + p = *line + last; + memset(p, 0, BUFSIZ); + fgets(p, BUFSIZ, fp); + last += strlen(p); + } while (! feof(fp) && (*line)[last - 1] != '\n'); /* Trim the trailing newline */ - if (*line && line[--last] == '\n') - line[last] = '\0'; + if (**line && (*line)[last - 1] == '\n') + (*line)[last - 1] = '\0'; - return (line); + return last; } /* OK, this should be in dhcpcd.c * It's here to make dhcpcd more readable */ #ifndef HAVE_SRANDOMDEV -void srandomdev (void) +void srandomdev(void) { int fd; unsigned long seed; - fd = open ("/dev/urandom", 0); - if (fd == -1 || read (fd, &seed, sizeof (seed)) == -1) { - logger (LOG_WARNING, "Could not read from /dev/urandom: %s", - strerror (errno)); - seed = time (0); + fd = open("/dev/urandom", 0); + if (fd == -1 || read(fd, &seed, sizeof(seed)) == -1) { + logger(LOG_WARNING, "Could not read from /dev/urandom: %s", + strerror(errno)); + seed = time(0); } if (fd >= 0) close(fd); - srandom (seed); + srandom(seed); } #endif /* strlcpy is nice, shame glibc does not define it */ #ifndef HAVE_STRLCPY -size_t strlcpy (char *dst, const char *src, size_t size) +size_t +strlcpy(char *dst, const char *src, size_t size) { const char *s = src; size_t n = size; if (n && --n) do { - if (! (*dst++ = *src++)) + if (!(*dst++ = *src++)) break; } while (--n); - if (! n) { + if (!n) { if (size) *dst = '\0'; while (*src++); } - return (src - s - 1); + return src - s - 1; } #endif /* Close our fd's */ -int close_fds (void) +int +close_fds(void) { int fd; - if ((fd = open ("/dev/null", O_RDWR)) == -1) { - logger (LOG_ERR, "open `/dev/null': %s", strerror (errno)); - return (-1); + if ((fd = open("/dev/null", O_RDWR)) == -1) { + logger(LOG_ERR, "open `/dev/null': %s", strerror(errno)); + return -1; } - dup2 (fd, fileno (stdin)); - dup2 (fd, fileno (stdout)); - dup2 (fd, fileno (stderr)); + dup2(fd, fileno(stdin)); + dup2(fd, fileno(stdout)); + dup2(fd, fileno(stderr)); if (fd > 2) - close (fd); - return (0); + close(fd); + return 0; } -int close_on_exec (int fd) +int +close_on_exec(int fd) { int flags; - if ((flags = fcntl (fd, F_GETFD, 0)) == -1 - || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1) + if ((flags = fcntl(fd, F_GETFD, 0)) == -1 + || fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) { - logger (LOG_ERR, "fcntl: %s", strerror (errno)); - return (-1); + logger(LOG_ERR, "fcntl: %s", strerror(errno)); + return -1; } - return (0); + return 0; } /* Handy function to get the time. @@ -145,105 +150,111 @@ int close_on_exec (int fd) * Which is why we use CLOCK_MONOTONIC, but it is not available on all * platforms. */ -int get_time (struct timeval *tp) +int +get_time(struct timeval *tp) { #if defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC) struct timespec ts; static clockid_t posix_clock; static int posix_clock_set = 0; - if (! posix_clock_set) { - if (sysconf (_SC_MONOTONIC_CLOCK) >= 0) + if (!posix_clock_set) { + if (sysconf(_SC_MONOTONIC_CLOCK) >= 0) posix_clock = CLOCK_MONOTONIC; else posix_clock = CLOCK_REALTIME; posix_clock_set = 1; } - if (clock_gettime (posix_clock, &ts) == -1) { - logger (LOG_ERR, "clock_gettime: %s", strerror (errno)); - return (-1); + if (clock_gettime(posix_clock, &ts) == -1) { + logger(LOG_ERR, "clock_gettime: %s", strerror(errno)); + return -1; } tp->tv_sec = ts.tv_sec; tp->tv_usec = ts.tv_nsec / 1000; - return (0); + return 0; #else - if (gettimeofday (tp, NULL) == -1) { - logger (LOG_ERR, "gettimeofday: %s", strerror (errno)); - return (-1); + if (gettimeofday(tp, NULL) == -1) { + logger(LOG_ERR, "gettimeofday: %s", strerror(errno)); + return -1; } - return (0); + return 0; #endif } -time_t uptime (void) +time_t +uptime(void) { struct timeval tp; - if (get_time (&tp) == -1) - return (-1); + if (get_time(&tp) == -1) + return -1; - return (tp.tv_sec); + return tp.tv_sec; } -void writepid (int fd, pid_t pid) +void +writepid(int fd, pid_t pid) { char spid[16]; - if (ftruncate (fd, (off_t) 0) == -1) { - logger (LOG_ERR, "ftruncate: %s", strerror (errno)); + ssize_t len; + + if (ftruncate(fd, (off_t)0) == -1) { + logger(LOG_ERR, "ftruncate: %s", strerror(errno)); } else { - ssize_t len; - snprintf (spid, sizeof (spid), "%u", pid); - len = pwrite (fd, spid, strlen (spid), (off_t) 0); - if (len != (ssize_t) strlen (spid)) - logger (LOG_ERR, "pwrite: %s", strerror (errno)); + snprintf(spid, sizeof(spid), "%u", pid); + len = pwrite(fd, spid, strlen(spid), (off_t)0); + if (len != (ssize_t)strlen(spid)) + logger(LOG_ERR, "pwrite: %s", strerror(errno)); } } -void *xmalloc (size_t s) +void * +xmalloc(size_t s) { - void *value = malloc (s); + void *value = malloc(s); if (value) - return (value); - + return value; logger (LOG_ERR, "memory exhausted"); - exit (EXIT_FAILURE); /* NOTREACHED */ } -void *xzalloc (size_t s) +void * +xzalloc(size_t s) { - void *value = xmalloc (s); - memset (value, 0, s); - return (value); + void *value = xmalloc(s); + + memset(value, 0, s); + return value; } -void *xrealloc (void *ptr, size_t s) +void * +xrealloc(void *ptr, size_t s) { - void *value = realloc (ptr, s); + void *value = realloc(ptr, s); if (value) return (value); - - logger (LOG_ERR, "memory exhausted"); - exit (EXIT_FAILURE); + logger(LOG_ERR, "memory exhausted"); + exit(EXIT_FAILURE); /* NOTREACHED */ } -char *xstrdup (const char *str) +char * +xstrdup(const char *str) { char *value; if (! str) - return (NULL); + return NULL; - if ((value = strdup (str))) - return (value); + if ((value = strdup(str))) + return value; - logger (LOG_ERR, "memory exhausted"); - exit (EXIT_FAILURE); + logger(LOG_ERR, "memory exhausted"); + exit(EXIT_FAILURE); /* NOTREACHED */ } diff --git a/common.h b/common.h index 46f1886d..f5b93ae4 100644 --- a/common.h +++ b/common.h @@ -44,25 +44,25 @@ #ifdef __GLIBC__ # if ! defined(__UCLIBC__) && ! defined (__dietlibc__) # undef HAVE_STRLCPY -size_t strlcpy (char *dst, const char *src, size_t size); +size_t strlcpy(char *, const char *, size_t); # endif #endif #define HAVE_SRANDOMDEV #if defined(__GLIBC__) || defined(__NetBSD__) # undef HAVE_SRANDOMDEV -void srandomdev (void); +void srandomdev(void); #endif -int close_fds (void); -int close_on_exec (int fd); -char *get_line (FILE *fp); -int get_time (struct timeval *tp); -time_t uptime (void); -void writepid (int fd, pid_t pid); -void *xrealloc (void *ptr, size_t size); -void *xmalloc (size_t size); -void *xzalloc (size_t size); -char *xstrdup (const char *str); +int close_fds(void); +int close_on_exec(int); +ssize_t get_line(char **, size_t *, FILE *); +int get_time(struct timeval *); +time_t uptime(void); +void writepid(int, pid_t); +void *xrealloc(void *, size_t); +void *xmalloc(size_t); +void *xzalloc(size_t); +char *xstrdup(const char *); #endif diff --git a/configure.c b/configure.c index 0969f738..a8410833 100644 --- a/configure.c +++ b/configure.c @@ -59,35 +59,37 @@ #include "signal.h" #include "socket.h" -static int file_in_path (const char *file) +static int +file_in_path(const char *file) { - char *p = getenv ("PATH"); + char *p = getenv("PATH"); char *path; char *token; struct stat s; char mypath[PATH_MAX]; int retval = -1; - if (! p) { + if (!p) { errno = ENOENT; - return (-1); + return -1; } - path = strdup (p); + path = strdup(p); p = path; - while ((token = strsep (&p, ":"))) { - snprintf (mypath, PATH_MAX, "%s/%s", token, file); - if (stat (mypath, &s) == 0) { + while ((token = strsep(&p, ":"))) { + snprintf(mypath, PATH_MAX, "%s/%s", token, file); + if (stat(mypath, &s) == 0) { retval = 0; break; } } - free (path); - return (retval); + free(path); + return(retval); } /* IMPORTANT: Ensure that the last parameter is NULL when calling */ -static int exec_cmd (const char *cmd, const char *args, ...) +static int +exec_cmd(const char *cmd, const char *args, ...) { va_list va; char **argv; @@ -97,254 +99,253 @@ static int exec_cmd (const char *cmd, const char *args, ...) sigset_t full; sigset_t old; - va_start (va, args); - while (va_arg (va, char *) != NULL) + va_start(va, args); + while (va_arg(va, char *) != NULL) n++; - va_end (va); - argv = xmalloc (sizeof (char *) * (n + 2)); + va_end(va); + argv = xmalloc(sizeof(char *) * (n + 2)); - va_start (va, args); + va_start(va, args); n = 2; - argv[0] = (char *) cmd; - argv[1] = (char *) args; - while ((argv[n] = va_arg (va, char *)) != NULL) + argv[0] = (char *)cmd; + argv[1] = (char *)args; + while ((argv[n] = va_arg(va, char *)) != NULL) n++; - va_end (va); + va_end(va); /* OK, we need to block signals */ - sigfillset (&full); - sigprocmask (SIG_SETMASK, &full, &old); + sigfillset(&full); + sigprocmask(SIG_SETMASK, &full, &old); #ifdef THERE_IS_NO_FORK - signal_reset (); - pid = vfork (); + signal_reset(); + pid = vfork(); #else pid = fork(); #endif switch (pid) { - case -1: - logger (LOG_ERR, "vfork: %s", strerror (errno)); - ret = -1; - break; - case 0: + case -1: + logger(LOG_ERR, "vfork: %s", strerror(errno)); + ret = -1; + break; + case 0: #ifndef THERE_IS_NO_FORK - signal_reset (); -#endif - sigprocmask (SIG_SETMASK, &old, NULL); - if (execvp (cmd, argv) && errno != ENOENT) - logger (LOG_ERR, "error executing \"%s\": %s", - cmd, strerror (errno)); - _exit (111); - /* NOTREACHED */ + signal_reset(); +#endif + sigprocmask (SIG_SETMASK, &old, NULL); + if (execvp(cmd, argv) && errno != ENOENT) + logger (LOG_ERR, "error executing \"%s\": %s", + cmd, strerror(errno)); + _exit(111); + /* NOTREACHED */ } #ifdef THERE_IS_NO_FORK - signal_setup (); + signal_setup(); #endif /* Restore our signals */ - sigprocmask (SIG_SETMASK, &old, NULL); + sigprocmask(SIG_SETMASK, &old, NULL); - free (argv); - return (ret); + free(argv); + return ret; } -static void exec_script (const char *script, const char *infofile, - const char *arg) +static void +exec_script(const char *script, const char *infofile, const char *arg) { struct stat buf; - if (! script || ! infofile || ! arg) - return; - - if (stat (script, &buf) == -1) { - if (strcmp (script, DEFAULT_SCRIPT) != 0) - logger (LOG_ERR, "`%s': %s", script, strerror (ENOENT)); + if (stat(script, &buf) == -1) { + if (strcmp(script, DEFAULT_SCRIPT) != 0) + logger(LOG_ERR, "`%s': %s", script, strerror(ENOENT)); return; } #ifdef ENABLE_INFO - logger (LOG_DEBUG, "exec \"%s\" \"%s\" \"%s\"", script, infofile, arg); - exec_cmd (script, infofile, arg, (char *) NULL); + logger(LOG_DEBUG, "exec \"%s\" \"%s\" \"%s\"", script, infofile, arg); + exec_cmd(script, infofile, arg, (char *)NULL); #else - logger (LOG_DEBUG, "exec \"%s\" \"\" \"%s\"", script, arg); - exec_cmd (script, "", arg, (char *) NULL); + logger(LOG_DEBUG, "exec \"%s\" \"\" \"%s\"", script, arg); + exec_cmd(script, "", arg, (char *)NULL); #endif } -static int make_resolv (const char *ifname, const dhcp_t *dhcp) +static int +make_resolv(const char *ifname, const struct dhcp *dhcp) { FILE *f = NULL; - address_t *address; + struct address *address; #ifdef ENABLE_RESOLVCONF char *resolvconf = NULL; - - if (file_in_path ("resolvconf") == 0) { - size_t len = strlen ("resolvconf -a ") + strlen (ifname) + 1; - 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"); + size_t len; + + if (file_in_path("resolvconf") == 0) { + len = strlen("resolvconf -a ") + strlen(ifname) + 1; + 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"); else if (errno == EEXIST) - logger (LOG_ERR, "popen: %s", strerror (errno)); + logger(LOG_ERR, "popen: %s", strerror(errno)); - if (ferror (f)) - logger (LOG_ERR, "ferror"); - free (resolvconf); + if (ferror(f)) + logger(LOG_ERR, "ferror"); + free(resolvconf); } #endif - if (! f) { - logger (LOG_DEBUG, "writing "RESOLVFILE); - if (! (f = fopen(RESOLVFILE, "w"))) - logger (LOG_ERR, "fopen `%s': %s", RESOLVFILE, strerror (errno)); + if (!f) { + logger(LOG_DEBUG, "writing "RESOLVFILE); + if (!(f = fopen(RESOLVFILE, "w"))) + logger(LOG_ERR, "fopen `%s': %s", + RESOLVFILE, strerror(errno)); } - if (! f) - return (-1); + if (!f) + return -1; - fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname); + fprintf(f, "# Generated by dhcpcd for interface %s\n", ifname); if (dhcp->dnssearch) - fprintf (f, "search %s\n", dhcp->dnssearch); + fprintf(f, "search %s\n", dhcp->dnssearch); else if (dhcp->dnsdomain) { - fprintf (f, "search %s\n", dhcp->dnsdomain); + fprintf(f, "search %s\n", dhcp->dnsdomain); } - STAILQ_FOREACH (address, dhcp->dnsservers, entries) - fprintf (f, "nameserver %s\n", inet_ntoa (address->address)); + STAILQ_FOREACH(address, dhcp->dnsservers, entries) + fprintf(f, "nameserver %s\n", inet_ntoa(address->address)); #ifdef ENABLE_RESOLVCONF if (resolvconf) - pclose (f); + pclose(f); else #endif - fclose (f); + fclose(f); /* Refresh the local resolver */ - res_init (); - return (0); + res_init(); + return 0; } -static void restore_resolv (const char *ifname) +static void +restore_resolv(const char *ifname) { #ifdef ENABLE_RESOLVCONF - if (file_in_path ("resolvconf") == 0) { - logger (LOG_DEBUG, "removing information from resolvconf"); - exec_cmd("resolvconf", "-d", ifname, (char *) NULL); + if (file_in_path("resolvconf") == 0) { + logger(LOG_DEBUG, "removing information from resolvconf"); + exec_cmd("resolvconf", "-d", ifname, (char *)NULL); } #endif } -static bool in_addresses (const struct address_head *addresses, - struct in_addr address) +static bool +in_addresses(const struct address_head *addresses, struct in_addr address) { - const address_t *addr; + const struct address *addr; STAILQ_FOREACH (addr, addresses, entries) if (addr->address.s_addr == address.s_addr) - return (true); + return true; - return (false); + return false; } -static bool in_routes (const struct route_head *routes, route_t *route) +static bool +in_routes(const struct route_head *routes, struct route *route) { - const route_t *r; + const struct route *r; if (! routes) - return (false); + return false; STAILQ_FOREACH (r, routes, entries) 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 true; - return (false); + return false; } #ifdef ENABLE_NTP -static int _make_ntp (const char *file, const char *ifname, const dhcp_t *dhcp) +static int +_make_ntp(const char *file, const char *ifname, const struct dhcp *dhcp) { FILE *f; - address_t *address; + struct address *address; char *a; - char *line; + char *line = NULL; + size_t len = 0; int tomatch = 0; char *token; bool ntp = false; + struct in_addr addr; - STAILQ_FOREACH (address, dhcp->ntpservers, entries) + STAILQ_FOREACH(address, dhcp->ntpservers, entries) tomatch++; /* Check that we really need to update the servers. * We do this because ntp has to be restarted to * work with a changed config. */ - if (! (f = fopen (file, "r"))) { + 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 (tomatch != 0 && (line = get_line (f))) { - struct in_addr addr; - + while (tomatch != 0 && (get_line(&line, &len, f))) { a = line; - token = strsep (&a, " "); - if (! token || strcmp (token, "server") != 0) - goto next; - - if ((token = strsep (&a, " \n")) == NULL) - goto next; - - if (inet_aton (token, &addr) == 1 && - in_addresses (dhcp->ntpservers, addr)) + token = strsep(&a, " "); + if (! token || strcmp(token, "server") != 0) + continue; + if ((token = strsep(&a, " \n")) == NULL) + continue; + if (inet_aton(token, &addr) == 1 && + in_addresses(dhcp->ntpservers, addr)) tomatch--; - -next: - free (line); } - fclose (f); + fclose(f); + free(line); /* 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); + logger(LOG_DEBUG, "writing %s", file); + if (!(f = fopen(file, "w"))) { + logger(LOG_ERR, "fopen `%s': %s", file, strerror(errno)); + return -1; } - fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname); + fprintf(f, "# Generated by dhcpcd for interface %s\n", ifname); #ifdef NTPFILE - if (strcmp (file, NTPFILE) == 0) { + if (strcmp(file, NTPFILE) == 0) { ntp = true; - fprintf (f, "restrict default noquery notrust nomodify\n"); - fprintf (f, "restrict 127.0.0.1\n"); + fprintf(f, "restrict default noquery notrust nomodify\n"); + fprintf(f, "restrict 127.0.0.1\n"); } #endif - STAILQ_FOREACH (address, dhcp->ntpservers, entries) { - a = inet_ntoa (address->address); + STAILQ_FOREACH(address, dhcp->ntpservers, entries) { + a = inet_ntoa(address->address); if (ntp) - fprintf (f, "restrict %s nomodify notrap noquery\n", a); - fprintf (f, "server %s\n", a); + fprintf(f, "restrict %s nomodify notrap noquery\n", a); + fprintf(f, "server %s\n", a); } - fclose (f); + fclose(f); - return (1); + return 1; } -static int make_ntp (const char *ifname, const dhcp_t *dhcp) +static int +make_ntp(const char *ifname, const struct dhcp *dhcp) { /* On some systems we have only have one ntp service, but we don't * know which configuration file we're using. So we need to write @@ -355,95 +356,97 @@ static int make_ntp (const char *ifname, const dhcp_t *dhcp) int retval = 0; #ifdef NTPFILE - if (_make_ntp (NTPFILE, ifname, dhcp) > 0) + if (_make_ntp(NTPFILE, ifname, dhcp) > 0) restart_ntp = true; #endif #ifdef OPENNTPFILE - if (_make_ntp (OPENNTPFILE, ifname, dhcp) > 0) + if (_make_ntp(OPENNTPFILE, ifname, dhcp) > 0) restart_openntp = true; #endif #ifdef NTPSERVICE if (restart_ntp) { #ifdef NTPCHECK - if (system (NTPCHECK) == 0) + 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) + if (system(OPENNTPCHECK) == 0) #endif retval += exec_cmd (OPENNTPSERVICE, - OPENNTPRESTARTARGS, (char *) NULL); + OPENNTPRESTARTARGS, (char *)NULL); } #elif defined (OPENNTPSERVICE) && ! defined (NTPSERVICE) if (restart_openntp) { #ifdef OPENNTPCHECK - if (system (OPENNTPCHECK) == 0) + 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 #ifdef ENABLE_NIS #define PREFIXSIZE 256 -static int make_nis (const char *ifname, const dhcp_t *dhcp) +static int +make_nis(const char *ifname, const struct dhcp *dhcp) { FILE *f; - address_t *address; + struct address *address; char *prefix; - logger (LOG_DEBUG, "writing "NISFILE); - if (! (f = fopen(NISFILE, "w"))) { - logger (LOG_ERR, "fopen `%s': %s", NISFILE, strerror (errno)); - return (-1); + logger(LOG_DEBUG, "writing "NISFILE); + if (!(f = fopen(NISFILE, "w"))) { + logger(LOG_ERR, "fopen `%s': %s", NISFILE, strerror(errno)); + return -1; } - prefix = xmalloc (sizeof (char) * PREFIXSIZE); + prefix = xmalloc(sizeof(char) * PREFIXSIZE); *prefix = '\0'; - fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname); + fprintf(f, "# Generated by dhcpcd for interface %s\n", ifname); if (dhcp->nisdomain) { - setdomainname (dhcp->nisdomain, (int) strlen (dhcp->nisdomain)); + setdomainname(dhcp->nisdomain, (int)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); + fprintf(f, "domain %s broadcast\n", dhcp->nisdomain); } else - snprintf (prefix, PREFIXSIZE, "%s", "ypserver"); + snprintf(prefix, PREFIXSIZE, "%s", "ypserver"); - NSTAILQ_FOREACH (address, dhcp->nisservers, entries) - fprintf (f, "%s %s\n", prefix, inet_ntoa (address->address)); + NSTAILQ_FOREACH(address, dhcp->nisservers, entries) + fprintf(f, "%s %s\n", prefix, inet_ntoa(address->address)); - free (prefix); - fclose (f); + free(prefix); + fclose(f); #ifdef NISCHECK - if (system (NISCHECK) == 0) + if (system(NISCHECK) == 0) #endif - exec_cmd (NISSERVICE, NISRESTARTARGS, (char *) NULL); - return (0); + exec_cmd(NISSERVICE, NISRESTARTARGS, (char *)NULL); + return 0; } #endif -static char *lookuphostname (char *hostname, const dhcp_t *dhcp, - const options_t *options) +static char * +lookuphostname(char *hostname, const struct dhcp *dhcp, + const struct options *options) { union { struct sockaddr sa; @@ -454,88 +457,94 @@ static char *lookuphostname (char *hostname, const dhcp_t *dhcp, struct addrinfo hints; struct addrinfo *res = NULL; int result; - char *p; + char *p, *s, *sp, *t; - logger (LOG_DEBUG, "Looking up hostname via DNS"); - addr = xmalloc (sizeof (char) * NI_MAXHOST); - salen = sizeof (su.sa); - memset (&su.sa, 0, salen); + logger(LOG_DEBUG, "Looking up hostname via DNS"); + addr = xmalloc(sizeof(char) * NI_MAXHOST); + salen = sizeof(su.sa); + memset(&su.sa, 0, salen); su.sin.sin_family = AF_INET; - memcpy (&su.sin.sin_addr, &dhcp->address, sizeof (su.sin.sin_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); + memcpy(&su.sin.sin_addr, &dhcp->address, sizeof(su.sin.sin_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)); + memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_NUMERICHOST; - result = getaddrinfo (addr, "0", &hints, &res); + result = getaddrinfo(addr, "0", &hints, &res); if (res) - freeaddrinfo (res); + freeaddrinfo(res); if (result == 0) logger (LOG_ERR, "malicious PTR record detected"); - if (result == 0 || ! *addr) { - free (addr); - return (NULL); + if (result == 0 || !*addr) { + free(addr); + return NULL; } - p = strchr (addr, '.'); + 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) + 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) { + s = sp = xstrdup(dhcp->dnssearch); + while ((t = strsep(&sp, " "))) + if (strcmp(t, p) == 0) { *--p = '\0'; - } - break; - case 3: /* -HHH */ - case 6: /* -HHHHHH */ - /* Just strip the domain */ - *p = '\0'; - break; - default: /* Too many H! */ - break; + 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); + strlcpy(hostname, addr, MAXHOSTNAMELEN); + free(addr); + return hostname; } -int configure (const options_t *options, interface_t *iface, - const dhcp_t *dhcp, bool up) +int +configure (const struct options *options, struct interface *iface, + const struct dhcp *dhcp, bool up) { - route_t *route = NULL; + struct route *route = NULL; struct route_head *new_routes = NULL; - route_t *new_route = NULL; + struct route *new_route = NULL; char *newhostname = NULL; char *curhostname = NULL; int remember; + unsigned short mtu; + struct in_addr td; + struct in_addr tg; + struct in_addr dest; + struct in_addr mask; + struct in_addr gate; + #ifdef ENABLE_IPV4LL bool haslinklocal = false; #endif @@ -545,85 +554,83 @@ int configure (const options_t *options, interface_t *iface, char *skipp; #endif - if (! options || ! iface || ! dhcp) - return (-1); - if (dhcp->address.s_addr == 0) up = 0; /* 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. */ - NSTAILQ_FOREACH (route, iface->previous_routes, entries) + NSTAILQ_FOREACH(route, iface->previous_routes, entries) 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); + (!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) { + if (!up) { if (iface->previous_routes) { - free_route (iface->previous_routes); + free_route(iface->previous_routes); iface->previous_routes = NULL; } /* Restore the original MTU value */ if (iface->mtu && iface->previous_mtu != iface->mtu) { - set_mtu (iface->name, iface->mtu); + set_mtu(iface->name, iface->mtu); iface->previous_mtu = iface->mtu; } #ifdef ENABLE_INFO /* If we haven't created an info file, do so now */ - if (! dhcp->frominfo) - write_info (iface, dhcp, options, false); + if (!dhcp->frominfo) + write_info(iface, dhcp, options, false); #endif /* 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 (iface->previous_address)); - memset (&iface->previous_netmask, - 0, sizeof (iface->previous_netmask)); + if (!options->keep_address) { + del_address(iface->name, + iface->previous_address, + iface->previous_netmask); + memset(&iface->previous_address, + 0, sizeof (iface->previous_address)); + memset(&iface->previous_netmask, + 0, sizeof (iface->previous_netmask)); } } - restore_resolv (iface->name); - exec_script (options->script, iface->infofile, "down"); + restore_resolv(iface->name); + exec_script(options->script, iface->infofile, "down"); - return (0); + 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 (options->domtu) { - unsigned short mtu = iface->mtu; + mtu = iface->mtu; if (dhcp->mtu) mtu = dhcp->mtu; if (mtu != iface->previous_mtu) { - if (set_mtu (iface->name, mtu) == 0) + if (set_mtu(iface->name, mtu) == 0) iface->previous_mtu = mtu; } } /* 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) - return (false); + if (!options->doinform || !has_address (iface->name, dhcp->address)) + if (add_address(iface->name, dhcp->address, dhcp->netmask, + dhcp->broadcast) == -1 && + errno != EEXIST) + return false; /* Now delete the old address if different */ 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); + 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. */ @@ -631,32 +638,29 @@ int configure (const options_t *options, interface_t *iface, options->metric > 0 && dhcp->netmask.s_addr != INADDR_BROADCAST) { - struct in_addr td; - struct in_addr tg; - memset (&td, 0, sizeof (td)); - memset (&tg, 0, sizeof (tg)); td.s_addr = dhcp->address.s_addr & dhcp->netmask.s_addr; + tg.s_addr = 0; add_route (iface->name, td, dhcp->netmask, tg, options->metric); del_route (iface->name, td, dhcp->netmask, tg, 0); } #endif #ifdef THERE_IS_NO_FORK - free (dhcpcd_skiproutes); + free(dhcpcd_skiproutes); /* We can never have more than 255 routes. So we need space * for 255 3 digit numbers and commas */ skiplen = 255 * 4 + 1; - skipp = dhcpcd_skiproutes = xmalloc (sizeof (char) * skiplen); + skipp = dhcpcd_skiproutes = xmalloc(sizeof(char) * skiplen); *skipp = '\0'; #endif /* Remember added routes */ - NSTAILQ_FOREACH (route, dhcp->routes, entries) { + NSTAILQ_FOREACH(route, dhcp->routes, entries) { #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)) + 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 */ @@ -665,34 +669,34 @@ int configure (const options_t *options, interface_t *iface, ! options->dogateway) continue; - remember = add_route (iface->name, route->destination, - route->netmask, route->gateway, - options->metric); + 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)) + if (remember < 0 && in_routes(iface->previous_routes, route)) remember = 1; if (remember >= 0) { if (! new_routes) { - new_routes = xmalloc (sizeof (*new_routes)); - STAILQ_INIT (new_routes); + new_routes = xmalloc(sizeof(*new_routes)); + STAILQ_INIT(new_routes); } - new_route = xmalloc (sizeof (route_t)); - memcpy (new_route, route, sizeof (*new_route)); - STAILQ_INSERT_TAIL (new_routes, new_route, entries); + new_route = xmalloc(sizeof (struct route)); + memcpy(new_route, route, sizeof (*new_route)); + STAILQ_INSERT_TAIL(new_routes, new_route, entries); } #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) { + 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); + skipp += snprintf(skipp, + dhcpcd_skiproutes + skiplen - skipp, + "%d", skip); } skip++; #endif @@ -702,7 +706,7 @@ int configure (const options_t *options, interface_t *iface, if (*dhcpcd_skiproutes) *skipp = '\0'; else { - free (dhcpcd_skiproutes); + free(dhcpcd_skiproutes); dhcpcd_skiproutes = NULL; } #endif @@ -711,41 +715,37 @@ 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; - struct in_addr gate; - - dest.s_addr = htonl (LINKLOCAL_ADDR); - mask.s_addr = htonl (LINKLOCAL_MASK); + dest.s_addr = htonl(LINKLOCAL_ADDR); + mask.s_addr = htonl(LINKLOCAL_MASK); gate.s_addr = 0; - remember = add_route (iface->name, dest, mask, gate, - options->metric); + remember = add_route(iface->name, dest, mask, gate, + options->metric); if (remember >= 0) { if (! new_routes) { - new_routes = xmalloc (sizeof (*new_routes)); - STAILQ_INIT (new_routes); + new_routes = xmalloc(sizeof(*new_routes)); + STAILQ_INIT(new_routes); } - new_route = xmalloc (sizeof (*new_route)); + new_route = xmalloc(sizeof(*new_route)); new_route->destination.s_addr = dest.s_addr; new_route->netmask.s_addr = mask.s_addr; new_route->gateway.s_addr = gate.s_addr; - STAILQ_INSERT_TAIL (new_routes, new_route, entries); + STAILQ_INSERT_TAIL(new_routes, new_route, entries); } } #endif if (iface->previous_routes) - free_route (iface->previous_routes); + free_route(iface->previous_routes); iface->previous_routes = new_routes; if (options->dodns && dhcp->dnsservers) make_resolv(iface->name, dhcp); else - logger (LOG_DEBUG, "no dns information to write"); + logger(LOG_DEBUG, "no dns information to write"); #ifdef ENABLE_NTP if (options->dontp && dhcp->ntpservers) @@ -757,53 +757,53 @@ int configure (const options_t *options, interface_t *iface, make_nis(iface->name, dhcp); #endif - curhostname = xmalloc (sizeof (char) * MAXHOSTNAMELEN); + curhostname = xmalloc(sizeof(char) * MAXHOSTNAMELEN); *curhostname = '\0'; - gethostname (curhostname, MAXHOSTNAMELEN); + gethostname(curhostname, MAXHOSTNAMELEN); if (options->dohostname || - strlen (curhostname) == 0 || - strcmp (curhostname, "(none)") == 0 || - strcmp (curhostname, "localhost") == 0) + strlen(curhostname) == 0 || + strcmp(curhostname, "(none)") == 0 || + strcmp(curhostname, "localhost") == 0) { - newhostname = xmalloc (sizeof (char) * MAXHOSTNAMELEN); + newhostname = xmalloc(sizeof(char) * MAXHOSTNAMELEN); if (dhcp->hostname) - strlcpy (newhostname, dhcp->hostname, MAXHOSTNAMELEN); + 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); + lookuphostname(newhostname, dhcp, options); if (*newhostname) { - logger (LOG_INFO, "setting hostname to `%s'", - newhostname); - sethostname (newhostname, (int) strlen (newhostname)); + logger(LOG_INFO, "setting hostname to `%s'", + newhostname); + sethostname(newhostname, (int)strlen(newhostname)); } - free (newhostname); + free(newhostname); } - free (curhostname); + free(curhostname); #ifdef ENABLE_INFO - if (! dhcp->frominfo) - write_info (iface, dhcp, options, true); + if (!dhcp->frominfo) + write_info(iface, dhcp, options, true); #endif if (iface->previous_address.s_addr != dhcp->address.s_addr || iface->previous_netmask.s_addr != dhcp->netmask.s_addr) { - memcpy (&iface->previous_address, - &dhcp->address, sizeof (iface->previous_address)); - memcpy (&iface->previous_netmask, - &dhcp->netmask, sizeof (iface->previous_netmask)); - exec_script (options->script, iface->infofile, "new"); + memcpy(&iface->previous_address, + &dhcp->address, sizeof(iface->previous_address)); + memcpy(&iface->previous_netmask, + &dhcp->netmask, sizeof(iface->previous_netmask)); + exec_script(options->script, iface->infofile, "new"); } else - exec_script (options->script, iface->infofile, "up"); + exec_script(options->script, iface->infofile, "up"); - return (0); + return 0; } diff --git a/configure.h b/configure.h index 3166947f..8dfbf1b2 100644 --- a/configure.h +++ b/configure.h @@ -32,7 +32,7 @@ #include "interface.h" #include "dhcp.h" -int configure (const options_t *options, interface_t *iface, - const dhcp_t *dhcp, bool up); +int configure(const struct options *, struct interface *, + const struct dhcp *, bool up); #endif diff --git a/dhcp.c b/dhcp.c index 14b40d83..23023d03 100644 --- a/dhcp.c +++ b/dhcp.c @@ -31,7 +31,6 @@ #include #include - #include #include @@ -61,12 +60,12 @@ } while (0) #endif -typedef struct message { +struct message { int value; const char *name; -} dhcp_message_t; +}; -static dhcp_message_t dhcp_messages[] = { +static struct message dhcp_messages[] = { { DHCP_DISCOVER, "DHCP_DISCOVER" }, { DHCP_OFFER, "DHCP_OFFER" }, { DHCP_REQUEST, "DHCP_REQUEST" }, @@ -78,21 +77,24 @@ static dhcp_message_t dhcp_messages[] = { { -1, NULL } }; -static const char *dhcp_message (int type) +static const char * +dhcp_message(int type) { - dhcp_message_t *d; + struct message *d; + for (d = dhcp_messages; d->name; d++) if (d->value == type) - return (d->name); + return d->name; - return (NULL); + return NULL; } -ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, - uint32_t xid, char type, const options_t *options) +ssize_t +send_message(const struct interface *iface, const struct dhcp *dhcp, + uint32_t xid, char type, const struct options *options) { struct udp_dhcp_packet *packet; - dhcpmessage_t *message; + struct dhcp_message *message; unsigned char *m; unsigned char *p; unsigned char *n_params = NULL; @@ -105,23 +107,20 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, size_t message_length; ssize_t retval; - if (!iface || !options || !dhcp) - return -1; - - memset (&from, 0, sizeof (from)); - memset (&to, 0, sizeof (to)); + memset (&from, 0, sizeof(from)); + memset (&to, 0, sizeof(to)); if (type == DHCP_RELEASE) to.s_addr = dhcp->serveraddress.s_addr; - message = xzalloc (sizeof (*message)); - m = (unsigned char *) message; - p = (unsigned char *) &message->options; + message = xzalloc(sizeof (*message)); + m = (unsigned char *)message; + p = (unsigned char *)&message->options; if ((type == DHCP_INFORM || type == DHCP_RELEASE || type == DHCP_REQUEST) && - ! IN_LINKLOCAL (ntohl (iface->previous_address.s_addr))) + !IN_LINKLOCAL(ntohl(iface->previous_address.s_addr))) { message->ciaddr = iface->previous_address.s_addr; from.s_addr = iface->previous_address.s_addr; @@ -142,29 +141,29 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, message->op = DHCP_BOOTREQUEST; message->hwtype = iface->family; switch (iface->family) { - case ARPHRD_ETHER: - case ARPHRD_IEEE802: - message->hwlen = ETHER_ADDR_LEN; - memcpy (&message->chaddr, &iface->hwaddr, - ETHER_ADDR_LEN); - break; - case ARPHRD_IEEE1394: - case ARPHRD_INFINIBAND: - message->hwlen = 0; - if (message->ciaddr == 0) - message->flags = htons (BROADCAST_FLAG); - break; - default: - logger (LOG_ERR, "dhcp: unknown hardware type %d", - iface->family); + case ARPHRD_ETHER: + case ARPHRD_IEEE802: + message->hwlen = ETHER_ADDR_LEN; + memcpy(&message->chaddr, &iface->hwaddr, + ETHER_ADDR_LEN); + break; + case ARPHRD_IEEE1394: + case ARPHRD_INFINIBAND: + message->hwlen = 0; + if (message->ciaddr == 0) + message->flags = htons(BROADCAST_FLAG); + break; + default: + logger (LOG_ERR, "dhcp: unknown hardware type %d", + iface->family); } - if (up < 0 || up > (time_t) UINT16_MAX) - message->secs = htons ((uint16_t) UINT16_MAX); + if (up < 0 || up > (time_t)UINT16_MAX) + message->secs = htons((uint16_t)UINT16_MAX); else - message->secs = htons (up); + message->secs = htons(up); message->xid = xid; - message->cookie = htonl (MAGIC_COOKIE); + message->cookie = htonl(MAGIC_COOKIE); *p++ = DHCP_MESSAGETYPE; *p++ = 1; @@ -173,33 +172,33 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, if (type == DHCP_REQUEST) { *p++ = DHCP_MAXMESSAGESIZE; *p++ = 2; - sz = get_mtu (iface->name); + sz = get_mtu(iface->name); if (sz < MTU_MIN) { - if (set_mtu (iface->name, MTU_MIN) == 0) + if (set_mtu(iface->name, MTU_MIN) == 0) sz = MTU_MIN; } - sz = htons (sz); - memcpy (p, &sz, 2); + sz = htons(sz); + memcpy(p, &sz, 2); p += 2; } *p++ = DHCP_CLIENTID; *p++ = iface->clientid_len; - memcpy (p, iface->clientid, iface->clientid_len); + memcpy(p, iface->clientid, iface->clientid_len); p+= iface->clientid_len; if (type != DHCP_DECLINE && type != DHCP_RELEASE) { if (options->userclass_len > 0) { *p++ = DHCP_USERCLASS; *p++ = options->userclass_len; - memcpy (p, &options->userclass, options->userclass_len); + memcpy(p, &options->userclass, options->userclass_len); p += options->userclass_len; } if (*options->classid > 0) { *p++ = DHCP_CLASSID; - *p++ = l = strlen (options->classid); - memcpy (p, options->classid, l); + *p++ = l = strlen(options->classid); + memcpy(p, options->classid, l); p += l; } } @@ -208,21 +207,20 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, #define PUTADDR(_type, _val) { \ *p++ = _type; \ *p++ = 4; \ - memcpy (p, &_val.s_addr, 4); \ + memcpy(p, &_val.s_addr, 4); \ p += 4; \ } - if (IN_LINKLOCAL (ntohl (dhcp->address.s_addr))) - logger (LOG_ERR, - "cannot request a link local address"); + if (IN_LINKLOCAL(ntohl (dhcp->address.s_addr))) + logger(LOG_ERR, "cannot request a link local address"); else { if (dhcp->address.s_addr && dhcp->address.s_addr != iface->previous_address.s_addr) { - PUTADDR (DHCP_ADDRESS, dhcp->address); + PUTADDR(DHCP_ADDRESS, dhcp->address); if (dhcp->serveraddress.s_addr) - PUTADDR (DHCP_SERVERIDENTIFIER, - dhcp->serveraddress); + PUTADDR(DHCP_SERVERIDENTIFIER, + dhcp->serveraddress); } } #undef PUTADDR @@ -230,8 +228,8 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, if (options->leasetime != 0) { *p++ = DHCP_LEASETIME; *p++ = 4; - ul = htonl (options->leasetime); - memcpy (p, &ul, 4); + ul = htonl(options->leasetime); + memcpy(p, &ul, 4); p += 4; } } @@ -243,13 +241,13 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, if (options->hostname[0]) { if (options->fqdn == FQDN_DISABLE) { *p++ = DHCP_HOSTNAME; - *p++ = l = strlen (options->hostname); - memcpy (p, options->hostname, l); + *p++ = l = strlen(options->hostname); + memcpy(p, options->hostname, l); p += l; } else { /* Draft IETF DHC-FQDN option (81) */ *p++ = DHCP_FQDN; - *p++ = (l = strlen (options->hostname)) + 3; + *p++ = (l = strlen(options->hostname)) + 3; /* Flags: 0000NEOS * S: 1 => Client requests Server to update * a RR in DNS as well as PTR @@ -262,7 +260,7 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, *p++ = options->fqdn & 0x9; *p++ = 0; /* from server for PTR RR */ *p++ = 0; /* from server for A RR if S=1 */ - memcpy (p, options->hostname, l); + memcpy(p, options->hostname, l); p += l; } } @@ -274,7 +272,7 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, * RFC2131 Section 3.5 states that the REQUEST must include the * list from the DISCOVER message, so I think this is ok. */ - if (type == DHCP_DISCOVER && ! options->test) + if (type == DHCP_DISCOVER && !options->test) *p++ = DHCP_DNSSERVER; else { if (type != DHCP_INFORM) { @@ -328,48 +326,46 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, message_length = p - m; - packet = xzalloc (sizeof (*packet)); - make_dhcp_packet (packet, (unsigned char *) message, message_length, - from, to); - free (message); - - logger (LOG_DEBUG, "sending %s with xid 0x%x", - dhcp_message (type), xid); - retval = send_packet (iface, ETHERTYPE_IP, (unsigned char *) packet, - message_length + - sizeof (packet->ip) + sizeof (packet->udp)); - free (packet); - return (retval); + packet = xzalloc(sizeof(*packet)); + make_dhcp_packet(packet, (unsigned char *)message, message_length, + from, to); + free(message); + + logger(LOG_DEBUG, "sending %s with xid 0x%x",dhcp_message(type), xid); + retval = send_packet(iface, ETHERTYPE_IP, (unsigned char *)packet, + message_length + + sizeof(packet->ip) + sizeof(packet->udp)); + free(packet); + return retval; } /* Decode an RFC3397 DNS search order option into a space * seperated string. Returns length of string (including * terminating zero) or zero on error. out may be NULL * to just determine output length. */ -static unsigned int decode_search (const unsigned char *p, int len, char *out) +static unsigned int +decode_search(const unsigned char *p, int len, char *out) { const unsigned char *r, *q = p; unsigned int count = 0, l, hops; + unsigned int ltype; while (q - p < len) { r = NULL; hops = 0; while ((l = *q++)) { - unsigned int label_type = l & 0xc0; - if (label_type == 0x80 || label_type == 0x40) + ltype = l & 0xc0; + if (ltype == 0x80 || ltype == 0x40) return 0; - else if (label_type == 0xc0) { /* pointer */ + else if (ltype == 0xc0) { /* pointer */ l = (l & 0x3f) << 8; l |= *q++; - /* save source of first jump. */ if (!r) r = q; - hops++; if (hops > 255) return 0; - q = p + l; if (q - p >= len) return 0; @@ -377,18 +373,16 @@ static unsigned int decode_search (const unsigned char *p, int len, char *out) /* straightforward name segment, add with '.' */ count += l + 1; if (out) { - memcpy (out, q, l); + memcpy(out, q, l); out += l; *out++ = '.'; } q += l; } } - /* change last dot to space */ if (out) *(out - 1) = ' '; - if (r) q = r; } @@ -402,13 +396,14 @@ static unsigned int decode_search (const unsigned char *p, int len, char *out) /* Add our classless static routes to the routes variable * and return the last route set */ -static struct route_head *decode_CSR (const unsigned char *p, int len) +static struct route_head * +decode_CSR(const unsigned char *p, int len) { const unsigned char *q = p; unsigned int cidr; unsigned int ocets; struct route_head *routes = NULL; - route_t *route; + struct route *route; /* Minimum is 5 -first is CIDR and a router length of 4 */ if (len < 5) @@ -416,94 +411,96 @@ static struct route_head *decode_CSR (const unsigned char *p, int len) while (q - p < len) { if (! routes) { - routes = xmalloc (sizeof (*routes)); - STAILQ_INIT (routes); + routes = xmalloc(sizeof (*routes)); + STAILQ_INIT(routes); } - route = xzalloc (sizeof (*route)); + route = xzalloc(sizeof(*route)); cidr = *q++; if (cidr > 32) { - logger (LOG_ERR, - "invalid CIDR of %d in classless static route", - cidr); - free_route (routes); - return (NULL); + logger(LOG_ERR, + "invalid CIDR of %d in classless static route", + cidr); + free_route(routes); + return NULL; } ocets = (cidr + 7) / 8; if (ocets > 0) { - memcpy (&route->destination.s_addr, q, (size_t) ocets); + memcpy(&route->destination.s_addr, q, (size_t)ocets); q += ocets; } /* Now enter the netmask */ if (ocets > 0) { - memset (&route->netmask.s_addr, 255, (size_t) ocets - 1); - memset ((unsigned char *) &route->netmask.s_addr + - (ocets - 1), - (256 - (1 << (32 - cidr) % 8)), 1); + memset(&route->netmask.s_addr, 255, (size_t)ocets - 1); + memset((unsigned char *)&route->netmask.s_addr + + (ocets - 1), + (256 - (1 << (32 - cidr) % 8)), 1); } /* Finally, snag the router */ - memcpy (&route->gateway.s_addr, q, 4); + memcpy(&route->gateway.s_addr, q, 4); q += 4; - STAILQ_INSERT_TAIL (routes, route, entries); + STAILQ_INSERT_TAIL(routes, route, entries); } - return (routes); + return routes; } -void free_dhcp (dhcp_t *dhcp) +void +free_dhcp(struct dhcp *dhcp) { if (! dhcp) return; - free_route (dhcp->routes); - free (dhcp->hostname); - free_address (dhcp->dnsservers); - free (dhcp->dnsdomain); - free (dhcp->dnssearch); - free_address (dhcp->ntpservers); - free (dhcp->nisdomain); - free_address (dhcp->nisservers); - free (dhcp->rootpath); - free (dhcp->sipservers); + free_route(dhcp->routes); + free(dhcp->hostname); + free_address(dhcp->dnsservers); + free(dhcp->dnsdomain); + free(dhcp->dnssearch); + free_address(dhcp->ntpservers); + free(dhcp->nisdomain); + free_address(dhcp->nisservers); + free(dhcp->rootpath); + free(dhcp->sipservers); if (dhcp->fqdn) { - free (dhcp->fqdn->name); - free (dhcp->fqdn); + free(dhcp->fqdn->name); + free(dhcp->fqdn); } } -static bool dhcp_add_address (struct address_head **addresses, - const unsigned char *data, - int length) +static bool +dhcp_add_address(struct address_head **addresses, + const unsigned char *data, int length) { int i; - address_t *address; + struct address *address; for (i = 0; i < length; i += 4) { /* Sanity check */ if (i + 4 > length) { - logger (LOG_ERR, "invalid address length"); - return (false); + logger(LOG_ERR, "invalid address length"); + return false; } if (*addresses == NULL) { - *addresses = xmalloc (sizeof (**addresses)); - STAILQ_INIT (*addresses); + *addresses = xmalloc(sizeof(**addresses)); + STAILQ_INIT(*addresses); } - address = xzalloc (sizeof (*address)); - memcpy (&address->address.s_addr, data + i, 4); - STAILQ_INSERT_TAIL (*addresses, address, entries); + address = xzalloc(sizeof(*address)); + memcpy(&address->address.s_addr, data + i, 4); + STAILQ_INSERT_TAIL(*addresses, address, entries); } - return (true); + return true; } #ifdef ENABLE_INFO -static char *decode_sipservers (const unsigned char *data, int length) +static char * +decode_sipservers(const unsigned char *data, int length) { char *sip = NULL; char *p; @@ -512,59 +509,59 @@ static char *decode_sipservers (const unsigned char *data, int length) size_t len; length--; - switch (encoding) { - case 0: - if ((len = decode_search (data, length, NULL)) > 0) { - sip = xmalloc (len); - decode_search (data, length, sip); - } - break; + case 0: + if ((len = decode_search(data, length, NULL)) > 0) { + sip = xmalloc(len); + decode_search(data, length, sip); + } + break; - case 1: - if (length == 0 || length % 4 != 0) { - logger (LOG_ERR, - "invalid length %d for option 120", - length + 1); - break; - } - len = ((length / 4) * (4 * 4)) + 1; - sip = p = xmalloc (len); - while (length != 0) { - memcpy (&addr.s_addr, data, 4); - data += 4; - p += snprintf (p, len - (p - sip), - "%s ", inet_ntoa (addr)); - length -= 4; - } - *--p = '\0'; + case 1: + if (length == 0 || length % 4 != 0) { + logger (LOG_ERR, + "invalid length %d for option 120", + length + 1); break; + } + len = ((length / 4) * (4 * 4)) + 1; + sip = p = xmalloc(len); + while (length != 0) { + memcpy(&addr.s_addr, data, 4); + data += 4; + p += snprintf (p, len - (p - sip), + "%s ", inet_ntoa (addr)); + length -= 4; + } + *--p = '\0'; + break; - default: - logger (LOG_ERR, "unknown sip encoding %d", encoding); - break; + default: + logger (LOG_ERR, "unknown sip encoding %d", encoding); + break; } - return (sip); + return sip; } #endif /* This calculates the netmask that we should use for static routes. * This IS different from the calculation used to calculate the netmask * for an interface address. */ -static uint32_t route_netmask (uint32_t ip_in) +static uint32_t +route_netmask(uint32_t ip_in) { /* used to be unsigned long - check if error */ - uint32_t p = ntohl (ip_in); + uint32_t p = ntohl(ip_in); uint32_t t; - if (IN_CLASSA (p)) + if (IN_CLASSA(p)) t = ~IN_CLASSA_NET; else { - if (IN_CLASSB (p)) + if (IN_CLASSB(p)) t = ~IN_CLASSB_NET; else { - if (IN_CLASSC (p)) + if (IN_CLASSC(p)) t = ~IN_CLASSC_NET; else t = 0; @@ -574,51 +571,54 @@ static uint32_t route_netmask (uint32_t ip_in) while (t & p) t >>= 1; - return (htonl (~t)); + return (htonl(~t)); } -static struct route_head *decode_routes (const unsigned char *data, int length) +static struct route_head * +decode_routes(const unsigned char *data, int length) { int i; struct route_head *head = NULL; - route_t *route; + struct route *route; for (i = 0; i < length; i += 8) { if (! head) { - head = xmalloc (sizeof (*head)); - STAILQ_INIT (head); + head = xmalloc(sizeof(*head)); + STAILQ_INIT(head); } - route = xzalloc (sizeof (*route)); - memcpy (&route->destination.s_addr, data + i, 4); - memcpy (&route->gateway.s_addr, data + i + 4, 4); + route = xzalloc(sizeof(*route)); + memcpy(&route->destination.s_addr, data + i, 4); + memcpy(&route->gateway.s_addr, data + i + 4, 4); route->netmask.s_addr = - route_netmask (route->destination.s_addr); - STAILQ_INSERT_TAIL (head, route, entries); + route_netmask(route->destination.s_addr); + STAILQ_INSERT_TAIL(head, route, entries); } - return (head); + return head; } -static struct route_head *decode_routers (const unsigned char *data, int length) +static struct route_head * +decode_routers(const unsigned char *data, int length) { int i; struct route_head *head = NULL; - route_t *route = NULL; + struct route *route = NULL; for (i = 0; i < length; i += 4) { if (! head) { - head = xmalloc (sizeof (*head)); - STAILQ_INIT (head); + head = xmalloc(sizeof(*head)); + STAILQ_INIT(head); } - route = xzalloc (sizeof (*route)); - memcpy (&route->gateway.s_addr, data + i, 4); - STAILQ_INSERT_TAIL (head, route, entries); + route = xzalloc(sizeof (*route)); + memcpy(&route->gateway.s_addr, data + i, 4); + STAILQ_INSERT_TAIL(head, route, entries); } - return (head); + return head; } -int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message) +int +parse_dhcpmessage(struct dhcp *dhcp, const struct dhcp_message *message) { const unsigned char *p = message->options; const unsigned char *end = p; /* Add size later for gcc-3 issue */ @@ -635,24 +635,24 @@ int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message) bool parse_sname = false; bool parse_file = false; - end += sizeof (message->options); + end += sizeof(message->options); - if (gettimeofday (&tv, NULL) == -1) { - logger (LOG_ERR, "gettimeofday: %s", strerror (errno)); - return (-1); + if (gettimeofday(&tv, NULL) == -1) { + logger(LOG_ERR, "gettimeofday: %s", strerror(errno)); + return -1; } dhcp->address.s_addr = message->yiaddr; dhcp->leasedfrom = tv.tv_sec; dhcp->frominfo = false; dhcp->address.s_addr = message->yiaddr; - strlcpy (dhcp->servername, (char *) message->servername, - sizeof (dhcp->servername)); + strlcpy(dhcp->servername, (char *)message->servername, + sizeof(dhcp->servername)); #define LEN_ERR \ { \ - logger (LOG_ERR, "invalid length %d for option %d", \ - length, option); \ + logger(LOG_ERR, "invalid length %d for option %d", \ + length, option); \ p += length; \ continue; \ } @@ -660,22 +660,19 @@ int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message) parse_start: while (p < end) { option = *p++; - if (! option) + if (!option) continue; - if (option == DHCP_END) goto eexit; length = *p++; - if (option != DHCP_PAD && length == 0) { - logger (LOG_ERR, "option %d has zero length, skipping", - option); + logger(LOG_ERR, "option %d has zero length, skipping", + option); continue; } - if (p + length >= end) { - logger (LOG_ERR, "dhcp option exceeds message length"); + logger(LOG_ERR, "dhcp option exceeds message length"); retval = -1; goto eexit; } @@ -690,58 +687,58 @@ parse_start: if (length % _mult != 0) \ LEN_ERR; #define GET_UINT8(_val) \ - LENGTH (sizeof (uint8_t)); \ - memcpy (&_val, p, sizeof (uint8_t)); + LENGTH(sizeof(uint8_t)); \ + memcpy(&_val, p, sizeof(uint8_t)); #define GET_UINT16(_val) \ - LENGTH (sizeof (uint16_t)); \ - memcpy (&_val, p, sizeof (uint16_t)); + LENGTH (sizeof(uint16_t)); \ + memcpy(&_val, p, sizeof(uint16_t)); #define GET_UINT32(_val) \ - LENGTH (sizeof (uint32_t)); \ - memcpy (&_val, p, sizeof (uint32_t)); + LENGTH (sizeof(uint32_t)); \ + memcpy(&_val, p, sizeof(uint32_t)); #define GET_UINT16_H(_val) \ - GET_UINT16 (_val); \ - _val = ntohs (_val); + GET_UINT16(_val); \ + _val = ntohs(_val); #define GET_UINT32_H(_val) \ - GET_UINT32 (_val); \ - _val = ntohl (_val); + GET_UINT32(_val); \ + _val = ntohl(_val); switch (option) { - case DHCP_MESSAGETYPE: - retval = (int) *p; - break; - case DHCP_ADDRESS: - GET_UINT32 (dhcp->address.s_addr); - break; - case DHCP_NETMASK: - GET_UINT32 (dhcp->netmask.s_addr); - break; - case DHCP_BROADCAST: - GET_UINT32 (dhcp->broadcast.s_addr); - break; - case DHCP_SERVERIDENTIFIER: - GET_UINT32 (dhcp->serveraddress.s_addr); - break; - case DHCP_LEASETIME: - GET_UINT32_H (dhcp->leasetime); - break; - case DHCP_RENEWALTIME: - GET_UINT32_H (dhcp->renewaltime); - break; - case DHCP_REBINDTIME: - GET_UINT32_H (dhcp->rebindtime); - break; - case DHCP_MTU: - GET_UINT16_H (dhcp->mtu); - /* Minimum legal mtu is 68 accoridng to - * RFC 2132. In practise it's 576 which is the - * minimum maximum message size. */ - if (dhcp->mtu < MTU_MIN) { - logger (LOG_DEBUG, - "MTU %d is too low, minimum is %d; ignoring", - dhcp->mtu, MTU_MIN); - dhcp->mtu = 0; - } - break; + case DHCP_MESSAGETYPE: + retval = (int)*p; + break; + case DHCP_ADDRESS: + GET_UINT32(dhcp->address.s_addr); + break; + case DHCP_NETMASK: + GET_UINT32(dhcp->netmask.s_addr); + break; + case DHCP_BROADCAST: + GET_UINT32(dhcp->broadcast.s_addr); + break; + case DHCP_SERVERIDENTIFIER: + GET_UINT32(dhcp->serveraddress.s_addr); + break; + case DHCP_LEASETIME: + GET_UINT32_H(dhcp->leasetime); + break; + case DHCP_RENEWALTIME: + GET_UINT32_H(dhcp->renewaltime); + break; + case DHCP_REBINDTIME: + GET_UINT32_H(dhcp->rebindtime); + break; + case DHCP_MTU: + GET_UINT16_H (dhcp->mtu); + /* Minimum legal mtu is 68 accoridng to + * RFC 2132. In practise it's 576 which is the + * minimum maximum message size. */ + if (dhcp->mtu < MTU_MIN) { + logger(LOG_DEBUG, + "MTU %d is too low, minimum is %d; ignoring", + dhcp->mtu, MTU_MIN); + dhcp->mtu = 0; + } + break; #undef GET_UINT32_H #undef GET_UINT32 @@ -750,122 +747,119 @@ parse_start: #undef GET_UINT8 #define GETSTR(_var) { \ - MIN_LENGTH (sizeof (char)); \ + MIN_LENGTH(sizeof(char)); \ if (_var) free (_var); \ - _var = xmalloc ((size_t) length + 1); \ - memcpy (_var, p, (size_t) length); \ - memset (_var + length, 0, 1); \ + _var = xmalloc((size_t)length + 1); \ + memcpy(_var, p, (size_t)length); \ + memset(_var + length, 0, 1); \ } - case DHCP_HOSTNAME: - GETSTR (dhcp->hostname); - break; - case DHCP_DNSDOMAIN: - GETSTR (dhcp->dnsdomain); - break; - case DHCP_MESSAGE: - GETSTR (dhcp->message); - break; + case DHCP_HOSTNAME: + GETSTR(dhcp->hostname); + break; + case DHCP_DNSDOMAIN: + GETSTR(dhcp->dnsdomain); + break; + case DHCP_MESSAGE: + GETSTR(dhcp->message); + break; #ifdef ENABLE_INFO - case DHCP_ROOTPATH: - GETSTR (dhcp->rootpath); - break; + case DHCP_ROOTPATH: + GETSTR(dhcp->rootpath); + break; #endif #ifdef ENABLE_NIS - case DHCP_NISDOMAIN: - GETSTR (dhcp->nisdomain); - break; + case DHCP_NISDOMAIN: + GETSTR(dhcp->nisdomain); + break; #endif #undef GETSTR #define GETADDR(_var) \ - MULT_LENGTH (4); \ - if (! dhcp_add_address (&_var, p, length)) \ - { \ - retval = -1; \ - goto eexit; \ - } - case DHCP_DNSSERVER: - GETADDR (dhcp->dnsservers); - break; + MULT_LENGTH (4); \ + if (! dhcp_add_address (&_var, p, length)) { \ + retval = -1; \ + goto eexit; \ + } + case DHCP_DNSSERVER: + GETADDR(dhcp->dnsservers); + break; #ifdef ENABLE_NTP - case DHCP_NTPSERVER: - GETADDR (dhcp->ntpservers); - break; + case DHCP_NTPSERVER: + GETADDR(dhcp->ntpservers); + break; #endif #ifdef ENABLE_NIS - case DHCP_NISSERVER: - GETADDR (dhcp->nisservers); - break; + case DHCP_NISSERVER: + GETADDR(dhcp->nisservers); + break; #endif #undef GETADDR - case DHCP_DNSSEARCH: - MIN_LENGTH (1); - free (dhcp->dnssearch); - len = decode_search (p, length, NULL); - if (len > 0) { - dhcp->dnssearch = xmalloc (len); - decode_search (p, length, - dhcp->dnssearch); - } - break; + case DHCP_DNSSEARCH: + MIN_LENGTH(1); + free(dhcp->dnssearch); + len = decode_search(p, length, NULL); + if (len > 0) { + dhcp->dnssearch = xmalloc(len); + decode_search(p, length, dhcp->dnssearch); + } + break; - case DHCP_CSR: - MIN_LENGTH (5); - free_route (csr); - csr = decode_CSR (p, length); - break; + case DHCP_CSR: + MIN_LENGTH(5); + free_route(csr); + csr = decode_CSR(p, length); + break; - case DHCP_MSCSR: - MIN_LENGTH (5); - free_route (mscsr); - mscsr = decode_CSR (p, length); - break; + case DHCP_MSCSR: + MIN_LENGTH(5); + free_route(mscsr); + mscsr = decode_CSR(p, length); + break; #ifdef ENABLE_INFO - case DHCP_SIPSERVER: - free (dhcp->sipservers); - dhcp->sipservers = decode_sipservers (p,length); - break; + case DHCP_SIPSERVER: + free(dhcp->sipservers); + dhcp->sipservers = decode_sipservers(p, length); + break; #endif - case DHCP_STATICROUTE: - MULT_LENGTH (8); - free_route (routes); - routes = decode_routes (p, length); - break; + case DHCP_STATICROUTE: + MULT_LENGTH(8); + free_route(routes); + routes = decode_routes(p, length); + break; - case DHCP_ROUTERS: - MULT_LENGTH (4); - free_route (routers); - routers = decode_routers (p, length); - break; + case DHCP_ROUTERS: + MULT_LENGTH(4); + free_route(routers); + routers = decode_routers(p, length); + break; - case DHCP_OPTIONSOVERLOADED: - LENGTH (1); - /* The overloaded option in an overloaded option - * should be ignored, overwise we may get an - * infinite loop */ - if (! in_overload) { - if (*p & 1) - parse_file = true; - if (*p & 2) - parse_sname = true; - } - break; + case DHCP_OPTIONSOVERLOADED: + LENGTH(1); + /* The overloaded option in an overloaded option + * should be ignored, overwise we may get an + * infinite loop */ + if (!in_overload) { + if (*p & 1) + parse_file = true; + if (*p & 2) + parse_sname = true; + } + break; - case DHCP_FQDN: - /* We ignore replies about FQDN */ - break; + case DHCP_FQDN: + /* We ignore replies about FQDN */ + break; #undef LENGTH #undef MIN_LENGTH #undef MULT_LENGTH - default: - logger (LOG_DEBUG, - "no facility to parse DHCP code %u", - option); + default: + logger (LOG_DEBUG, + "no facility to parse DHCP code %u", option); break; } @@ -877,22 +871,22 @@ eexit: if (parse_file) { parse_file = false; p = message->bootfile; - end = p + sizeof (message->bootfile); + end = p + sizeof(message->bootfile); in_overload = true; goto parse_start; } else if (parse_sname) { parse_sname = false; p = message->servername; - end = p + sizeof (message->servername); - memset (dhcp->servername, 0, sizeof (dhcp->servername)); + end = p + sizeof(message->servername); + memset(dhcp->servername, 0, sizeof(dhcp->servername)); in_overload = true; goto parse_start; } /* Fill in any missing fields */ - if (! dhcp->netmask.s_addr) - dhcp->netmask.s_addr = get_netmask (dhcp->address.s_addr); - if (! dhcp->broadcast.s_addr) + if (!dhcp->netmask.s_addr) + dhcp->netmask.s_addr = get_netmask(dhcp->address.s_addr); + if (!dhcp->broadcast.s_addr) dhcp->broadcast.s_addr = dhcp->address.s_addr | ~dhcp->netmask.s_addr; @@ -900,21 +894,21 @@ eexit: * static routes and routers according to RFC 3442 */ if (csr) { dhcp->routes = csr; - free_route (mscsr); - free_route (routers); - free_route (routes); + free_route(mscsr); + free_route(routers); + free_route(routes); } else if (mscsr) { dhcp->routes = mscsr; - free_route (routers); - free_route (routes); + free_route(routers); + free_route(routes); } else { /* Ensure that we apply static routes before routers */ if (! routes) routes = routers; else if (routers) - STAILQ_CONCAT (routes, routers); + STAILQ_CONCAT(routes, routers); dhcp->routes = routes; } - return (retval); + return retval; } diff --git a/dhcp.h b/dhcp.h index cc66d137..7b2f86be 100644 --- a/dhcp.h +++ b/dhcp.h @@ -118,15 +118,15 @@ enum FQQN { FQDN_BOTH = 0x31 }; -typedef struct fqdn_t +struct fqdn { uint8_t flags; uint8_t r1; uint8_t r2; char *name; -} fqdn_t; +}; -typedef struct dhcp_t +struct dhcp { char version[11]; @@ -147,7 +147,7 @@ typedef struct dhcp_t struct route_head *routes; char *hostname; - fqdn_t *fqdn; + struct fqdn *fqdn; struct address_head *dnsservers; char *dnsdomain; @@ -164,7 +164,7 @@ typedef struct dhcp_t char *rootpath; bool frominfo; -} dhcp_t; +}; /* Sizes for DHCP options */ #define DHCP_CHADDR_LEN 16 @@ -181,7 +181,7 @@ typedef struct dhcp_t /* Some crappy DHCP servers require the BOOTP minimum length */ #define BOOTP_MESSAGE_LENTH_MIN 300 -typedef struct dhcpmessage_t +struct dhcp_message { unsigned char op; /* message type */ unsigned char hwtype; /* hardware address type */ @@ -199,18 +199,18 @@ typedef struct dhcpmessage_t unsigned char bootfile[BOOTFILE_LEN]; /* boot file name */ uint32_t cookie; unsigned char options[DHCP_OPTION_LEN]; /* message options - cookie */ -} dhcpmessage_t; +}; struct udp_dhcp_packet { struct ip ip; struct udphdr udp; - dhcpmessage_t dhcp; + struct dhcp_message dhcp; }; -ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, - uint32_t xid, char type, const options_t *options); -void free_dhcp (dhcp_t *dhcp); -int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message); +ssize_t send_message(const struct interface *, const struct dhcp *, + uint32_t, char, const struct options *); +void free_dhcp(struct dhcp *); +int parse_dhcpmessage (struct dhcp *, const struct dhcp_message *); #endif diff --git a/dhcpcd.c b/dhcpcd.c index 23379d5c..1e1359b1 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -30,7 +30,9 @@ const char copyright[] = "Copyright (c) 2006-2008 Roy Marples"; #include #include #include + #include + #include #include #include @@ -100,50 +102,54 @@ char *dhcpcd_skiproutes = NULL; #define EXTRA_OPTS "fg:" #endif -static int atoint (const char *s) +static int +atoint(const char *s) { char *t; long n; errno = 0; - n = strtol (s, &t, 0); + n = strtol(s, &t, 0); if ((errno != 0 && n == 0) || s == t || (errno == ERANGE && (n == LONG_MAX || n == LONG_MIN))) { - logger (LOG_ERR, "`%s' out of range", s); - return (-1); + logger(LOG_ERR, "`%s' out of range", s); + return -1; } - return ((int) n); + return (int)n; } -static pid_t read_pid (const char *pidfile) +static pid_t +read_pid(const char *pidfile) { FILE *fp; pid_t pid = 0; - if ((fp = fopen (pidfile, "r")) == NULL) { + if ((fp = fopen(pidfile, "r")) == NULL) { errno = ENOENT; return 0; } - fscanf (fp, "%d", &pid); - fclose (fp); + fscanf(fp, "%d", &pid); + fclose(fp); - return (pid); + return pid; } -static void usage (void) +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] \n"); + 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] \n"); } -int main(int argc, char **argv) +int +main(int argc, char **argv) { - options_t *options; + struct options *options; int userclasses = 0; int opt; int option_index = 0; @@ -151,20 +157,22 @@ int main(int argc, char **argv) pid_t pid; int debug = 0; int i; + int j; int pidfd = -1; int sig = 0; int retval = EXIT_FAILURE; + char *p; /* Close any un-needed fd's */ for (i = getdtablesize() - 1; i >= 3; --i) close (i); - openlog (PACKAGE, LOG_PID, LOG_LOCAL0); + openlog(PACKAGE, LOG_PID, LOG_LOCAL0); - options = xzalloc (sizeof (*options)); - options->script = (char *) DEFAULT_SCRIPT; - snprintf (options->classid, CLASS_ID_MAX_LEN, "%s %s", - PACKAGE, VERSION); + options = xzalloc(sizeof(*options)); + options->script = (char *)DEFAULT_SCRIPT; + snprintf(options->classid, CLASS_ID_MAX_LEN, "%s %s", + PACKAGE, VERSION); options->doarp = true; options->dodns = true; @@ -178,10 +186,10 @@ int main(int argc, char **argv) options->doduid = true; options->timeout = DEFAULT_TIMEOUT; - gethostname (options->hostname, sizeof (options->hostname)); - if (strcmp (options->hostname, "(none)") == 0 || - strcmp (options->hostname, "localhost") == 0) - memset (options->hostname, 0, sizeof (options->hostname)); + gethostname(options->hostname, sizeof(options->hostname)); + if (strcmp(options->hostname, "(none)") == 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 */ @@ -190,239 +198,241 @@ int main(int argc, char **argv) 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); - goto abort; - case 'c': - options->script = optarg; - break; - case 'd': - debug++; - switch (debug) { - case 1: - setloglevel (LOG_DEBUG); - break; - case 2: - options->daemonise = false; - break; - } + case 0: + if (longopts[option_index].flag) break; + logger(LOG_ERR, "option `%s' should set a flag", + longopts[option_index].name); + goto abort; + case 'c': + options->script = optarg; + break; + case 'd': + debug++; + switch (debug) { + case 1: + setloglevel(LOG_DEBUG); + break; + case 2: + options->daemonise = false; + break; + } + break; #ifdef THERE_IS_NO_FORK - case 'f': - options->daemonised = true; - close_fds (); - break; - case 'g': - dhcpcd_skiproutes = xstrdup (optarg); - break; + case 'f': + options->daemonised = true; + close_fds(); + break; + case 'g': + dhcpcd_skiproutes = xstrdup (optarg); + break; #endif - case 'h': - if (! optarg) - *options->hostname = '\0'; - else if (strlen (optarg) > MAXHOSTNAMELEN) { - logger (LOG_ERR, - "`%s' too long for HostName string, max is %d", - optarg, MAXHOSTNAMELEN); - goto abort; - } else - strlcpy (options->hostname, optarg, - sizeof (options->hostname)); - break; - case 'i': - if (! optarg) { - *options->classid = '\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); - goto abort; - } else - strlcpy (options->classid, optarg, - sizeof (options->classid)); - break; - case 'k': - sig = SIGHUP; - break; - case 'l': - if (*optarg == '-') { - logger (LOG_ERR, - "leasetime must be a positive value"); - goto abort; - } - errno = 0; - options->leasetime = (uint32_t) strtol (optarg, NULL, 0); - if (errno == EINVAL || errno == ERANGE) { - logger (LOG_ERR, "`%s' out of range", optarg); - goto abort; - } - break; - case 'm': - options->metric = atoint (optarg); - if (options->metric < 0) { - logger (LOG_ERR, - "metric must be a positive value"); - goto abort; - } - break; - case 'n': - sig = SIGALRM; - break; - case 'p': - options->persistent = true; + case 'h': + if (!optarg) + *options->hostname = '\0'; + else if (strlen(optarg) > MAXHOSTNAMELEN) { + logger(LOG_ERR, + "`%s' too long for HostName string," + " max is %d", optarg, MAXHOSTNAMELEN); + goto abort; + } else + strlcpy(options->hostname, optarg, + sizeof(options->hostname)); + break; + case 'i': + if (!optarg) { + *options->classid = '\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); + goto abort; + } else + strlcpy(options->classid, optarg, + sizeof(options->classid)); + break; + case 'k': + sig = SIGHUP; + break; + case 'l': + if (*optarg == '-') { + logger(LOG_ERR, + "leasetime must be a positive value"); + goto abort; + } + errno = 0; + options->leasetime = (uint32_t)strtol (optarg, NULL, 0); + if (errno == EINVAL || errno == ERANGE) { + logger(LOG_ERR, "`%s' out of range", optarg); + goto abort; + } + break; + case 'm': + options->metric = atoint(optarg); + if (options->metric < 0) { + logger(LOG_ERR, + "metric must be a positive value"); + goto abort; + } + break; + case 'n': + sig = SIGALRM; + break; + case 'p': + options->persistent = true; + break; + case 's': + options->doinform = true; + options->doarp = false; + if (!optarg || strlen(optarg) == 0) { + options->request_address.s_addr = 0; break; - case 's': - options->doinform = true; - options->doarp = false; - if (! optarg || strlen (optarg) == 0) { - options->request_address.s_addr = 0; - break; - } else { - char *slash = strchr (optarg, '/'); - if (slash) { - int cidr; - /* nullify the slash, so the -r option can read the - * address */ - *slash++ = '\0'; - if (sscanf (slash, "%d", &cidr) != 1 || - inet_cidrtoaddr (cidr, &options->request_netmask) != 0) { - logger (LOG_ERR, "`%s' is not a valid CIDR", slash); - goto abort; - } + } else { + if ((p = strchr(optarg, '/'))) { + /* nullify the slash, so the -r option + * can read the address */ + *p++ = '\0'; + if (sscanf(p, "%d", &i) != 1 || + inet_cidrtoaddr(i, &options->request_netmask) != 0) + { + logger(LOG_ERR, + "`%s' is not a valid CIDR", + p); + goto abort; } } - /* FALLTHROUGH */ - case 'r': - if (! options->doinform) - options->dorequest = true; - if (strlen (optarg) > 0 && - ! inet_aton (optarg, &options->request_address)) - { - logger (LOG_ERR, "`%s' is not a valid IP address", optarg); - goto abort; - } - break; - case 't': - options->timeout = atoint (optarg); - if (options->timeout < 0) { - logger (LOG_ERR, "timeout must be a positive value"); + } + /* FALLTHROUGH */ + case 'r': + if (!options->doinform) + options->dorequest = true; + if (strlen(optarg) > 0 && + ! inet_aton(optarg, &options->request_address)) + { + logger(LOG_ERR, + "`%s' is not a valid IP address", + optarg); + goto abort; + } + break; + case 't': + options->timeout = atoint (optarg); + if (options->timeout < 0) { + logger (LOG_ERR, "timeout must be a positive value"); + goto abort; + } + break; + case 'u': + j = 0; + for (i = 0; i < userclasses; i++) + j += (int)options->userclass[j] + 1; + if (j + 1 + strlen(optarg) > USERCLASS_MAX_LEN) { + logger(LOG_ERR, + "userclass overrun, max is %d", + USERCLASS_MAX_LEN); goto abort; } - break; - case 'u': - { - int offset = 0; - for (i = 0; i < userclasses; i++) - 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); - goto abort; - } - userclasses++; - memcpy (options->userclass + offset + 1 , optarg, strlen (optarg)); - options->userclass[offset] = strlen (optarg); - options->userclass_len += (strlen (optarg)) + 1; - } - break; - case 'x': - sig = SIGTERM; - break; - case 'A': + userclasses++; + memcpy (options->userclass + j + 1 , + optarg, strlen(optarg)); + options->userclass[j] = strlen(optarg); + options->userclass_len += (strlen(optarg)) + 1; + break; + case 'x': + sig = SIGTERM; + break; + case 'A': #ifndef ENABLE_ARP - logger (LOG_ERR, - "arp not compiled into dhcpcd"); - goto abort; + logger (LOG_ERR, "arp not compiled into dhcpcd"); + goto abort; #endif - options->doarp = false; - break; - case 'E': + options->doarp = false; + break; + case 'E': #ifndef ENABLE_INFO - logger (LOG_ERR, - "info not compiled into dhcpcd"); - goto abort; + logger (LOG_ERR, "info not compiled into dhcpcd"); + goto abort; #endif - options->dolastlease = true; + options->dolastlease = true; + break; + case 'F': + if (!optarg) { + options->fqdn = FQDN_BOTH; break; - case 'F': - if (! optarg) { - options->fqdn = FQDN_BOTH; - break; - } - if (strncmp (optarg, "none", strlen (optarg)) == 0) - options->fqdn = FQDN_NONE; - else if (strncmp (optarg, "ptr", strlen (optarg)) == 0) - options->fqdn = FQDN_PTR; - else if (strncmp (optarg, "both", strlen (optarg)) == 0) - options->fqdn = FQDN_BOTH; - else { - logger (LOG_ERR, "invalid value `%s' for FQDN", optarg); + } + if (strncmp(optarg, "none", strlen(optarg)) == 0) + options->fqdn = FQDN_NONE; + else if (strncmp(optarg, "ptr", strlen(optarg)) == 0) + options->fqdn = FQDN_PTR; + else if (strncmp(optarg, "both", strlen(optarg)) == 0) + options->fqdn = FQDN_BOTH; + else { + logger(LOG_ERR, "invalid value `%s' for FQDN", + optarg); + goto abort; + } + break; + case 'G': + options->dogateway = false; + break; + case 'H': + options->dohostname++; + break; + case 'I': + 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); goto abort; } - break; - case 'G': - options->dogateway = false; - break; - case 'H': - options->dohostname++; - break; - case 'I': - 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); - goto abort; - } - if (strlcpy (options->clientid, optarg, - sizeof (options->clientid)) == 0) - /* empty string disabled duid */ - options->doduid = false; - - } else { - memset (options->clientid, 0, sizeof (options->clientid)); + if (strlcpy(options->clientid, optarg, + sizeof(options->clientid)) == 0) + /* empty string disabled duid */ options->doduid = false; - } - break; - case 'L': - options->doipv4ll = false; - break; - case 'M': - options->domtu = false; - break; - case 'N': - options->dontp = false; - break; - case 'R': - options->dodns = false; - break; - case 'S': - options->domscsr++; - break; - case 'T': + } else { + memset(options->clientid, 0, + sizeof(options->clientid)); + options->doduid = false; + } + break; + case 'L': + options->doipv4ll = false; + break; + case 'M': + options->domtu = false; + break; + case 'N': + options->dontp = false; + break; + case 'R': + options->dodns = false; + break; + case 'S': + options->domscsr++; + break; + case 'T': #ifndef ENABLE_INFO - logger (LOG_ERR, "info support not compiled into dhcpcd"); - goto abort; + logger(LOG_ERR, "info support not compiled into dhcpcd"); + goto abort; #endif - options->test = true; - options->persistent = true; - break; - case 'Y': - options->donis = false; - break; - case '?': - usage (); - goto abort; - default: - usage (); - goto abort; + options->test = true; + options->persistent = true; + break; + case 'Y': + options->donis = false; + break; + case '?': + usage(); + goto abort; + default: + usage(); + goto abort; } } if (doversion) { - printf (""PACKAGE" "VERSION"\n"); - printf ("Compile time options:" + printf(""PACKAGE" "VERSION"\n"); + printf("Compile time options:" #ifdef ENABLE_ARP " ARP" #endif @@ -457,27 +467,27 @@ int main(int argc, char **argv) } if (dohelp) - usage (); + usage(); #ifdef THERE_IS_NO_FORK dhcpcd_argv = argv; dhcpcd_argc = argc; - if (! realpath (argv[0], dhcpcd)) { - logger (LOG_ERR, "unable to resolve the path `%s': %s", - argv[0], strerror (errno)); + if (!realpath(argv[0], dhcpcd)) { + logger(LOG_ERR, "unable to resolve the path `%s': %s", + argv[0], strerror(errno)); goto abort; } #endif if (optind < argc) { - if (strlen (argv[optind]) > IF_NAMESIZE) { - logger (LOG_ERR, - "`%s' too long for an interface name (max=%d)", - argv[optind], IF_NAMESIZE); + if (strlen(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)); + strlcpy(options->interface, argv[optind], + sizeof(options->interface)); } else { /* If only version was requested then exit now */ if (doversion || dohelp) { @@ -485,11 +495,11 @@ int main(int argc, char **argv) goto abort; } - logger (LOG_ERR, "no interface specified"); + logger(LOG_ERR, "no interface specified"); goto abort; } - if (strchr (options->hostname, '.')) { + if (strchr(options->hostname, '.')) { if (options->fqdn == FQDN_DISABLE) options->fqdn = FQDN_BOTH; } else @@ -497,80 +507,82 @@ int main(int argc, char **argv) if (options->request_address.s_addr == 0 && options->doinform) { if ((options->request_address.s_addr = - get_address (options->interface)) != 0) + 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"); + if (IN_LINKLOCAL(ntohl (options->request_address.s_addr))) { + logger(LOG_ERR, + "you are not allowed to request a link local address"); goto abort; } - if (geteuid ()) - logger (LOG_WARNING, PACKAGE " will not work correctly unless" - " run as root"); + if (geteuid()) + logger(LOG_WARNING, PACKAGE " will not work correctly unless" + " run as root"); - prefix = xmalloc (sizeof (char) * (IF_NAMESIZE + 3)); - snprintf (prefix, IF_NAMESIZE, "%s: ", options->interface); - setlogprefix (prefix); - snprintf (options->pidfile, sizeof (options->pidfile), PIDFILE, - options->interface); - free (prefix); + prefix = xmalloc(sizeof(char) * (IF_NAMESIZE + 3)); + snprintf(prefix, IF_NAMESIZE, "%s: ", options->interface); + setlogprefix(prefix); + snprintf(options->pidfile, sizeof(options->pidfile), PIDFILE, + options->interface); + free(prefix); - chdir ("/"); - umask (022); + chdir("/"); + umask(022); - if (mkdir (INFODIR, S_IRUSR | S_IWUSR |S_IXUSR | S_IRGRP | S_IXGRP - | S_IROTH | S_IXOTH) && errno != EEXIST) + if (mkdir(INFODIR, S_IRUSR | S_IWUSR |S_IXUSR | S_IRGRP | S_IXGRP + | 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; } if (options->dolastlease) { - logger (LOG_ERR, "cannot test with --lastlease"); + logger(LOG_ERR, "cannot test with --lastlease"); goto abort; } if (sig != 0) { - logger (LOG_ERR, - "cannot test with --release or --renew"); + logger(LOG_ERR, + "cannot test with --release or --renew"); goto abort; } } if (sig != 0) { - int killed = -1; - pid = read_pid (options->pidfile); + i = -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"); + if (!pid || (i = kill(pid, sig))) + logger(sig == SIGALRM ? LOG_INFO : LOG_ERR, + ""PACKAGE" not running"); - if (pid != 0 && (sig != SIGALRM || killed != 0)) - unlink (options->pidfile); + if (pid != 0 && (sig != SIGALRM || i != 0)) + unlink(options->pidfile); - if (killed == 0) { + if (i == 0) { retval = EXIT_SUCCESS; goto abort; } @@ -579,67 +591,67 @@ int main(int argc, char **argv) goto abort; } - if (! options->test && ! options->daemonised) { - if ((pid = read_pid (options->pidfile)) > 0 && - kill (pid, 0) == 0) + 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); + 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, 0664); + pidfd = open(options->pidfile, + O_WRONLY | O_CREAT | O_NONBLOCK, 0664); 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 */ - if (flock (pidfd, LOCK_EX | LOCK_NB) == -1) { - logger (LOG_ERR, "flock `%s': %s", - options->pidfile, strerror (errno)); + if (flock(pidfd, LOCK_EX | LOCK_NB) == -1) { + 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) - logger (LOG_ERR, "fcntl: %s", strerror (errno)); + if ((i = fcntl(pidfd, F_GETFD, 0)) == -1 || + fcntl(pidfd, F_SETFD, i | FD_CLOEXEC) == -1) + logger(LOG_ERR, "fcntl: %s", strerror(errno)); - writepid (pidfd, getpid ()); - logger (LOG_INFO, PACKAGE " " VERSION " starting"); + writepid(pidfd, getpid()); + logger(LOG_INFO, PACKAGE " " VERSION " starting"); } /* Seed random */ - srandomdev (); + srandomdev(); /* Massage our filters per platform */ - setup_packet_filters (); + setup_packet_filters(); - if (dhcp_run (options, &pidfd) == 0) + if (dhcp_run(options, &pidfd) == 0) retval = EXIT_SUCCESS; abort: /* If we didn't daemonise then we need to punt the pidfile now */ if (pidfd > -1) { - close (pidfd); - unlink (options->pidfile); + close(pidfd); + unlink(options->pidfile); } - free (options); + free(options); #ifdef THERE_IS_NO_FORK /* There may have been an error before the dhcp_run function * clears this, so just do it here to be safe */ - free (dhcpcd_skiproutes); + free(dhcpcd_skiproutes); #endif - logger (LOG_INFO, "exiting"); + logger(LOG_INFO, "exiting"); - exit (retval); + exit(retval); /* NOTREACHED */ } diff --git a/dhcpcd.h b/dhcpcd.h index f57093ff..0c04e729 100644 --- a/dhcpcd.h +++ b/dhcpcd.h @@ -30,8 +30,10 @@ #include #include + #include #include + #include #include @@ -51,7 +53,7 @@ extern int dhcpcd_argc; extern char *dhcpcd_skiproutes; #endif -typedef struct options_t { +struct options { char interface[IF_NAMESIZE]; char hostname[MAXHOSTNAMELEN]; int fqdn; @@ -89,6 +91,6 @@ typedef struct options_t { char *script; char pidfile[PATH_MAX]; -} options_t; +}; #endif diff --git a/duid.c b/duid.c index e4dd83b9..c2ad4a29 100644 --- a/duid.c +++ b/duid.c @@ -26,6 +26,7 @@ */ #include + #include #include #include @@ -43,7 +44,8 @@ #define THIRTY_YEARS_IN_SECONDS 946707779 -size_t get_duid (unsigned char *duid, const interface_t *iface) +size_t +get_duid(unsigned char *duid, const struct interface *iface) { FILE *f; uint16_t type = 0; @@ -53,66 +55,65 @@ size_t get_duid (unsigned char *duid, const interface_t *iface) int x = 0; unsigned char *p = duid; size_t len = 0; + char *line = NULL; - if (! iface) - return (0); - /* If we already have a DUID then use it as it's never supposed * to change once we have one even if the interfaces do */ - if ((f = fopen (DUIDFILE, "r"))) { - char *line = get_line (f); + if ((f = fopen(DUIDFILE, "r"))) { + get_line(&line, &len, f); if (line) { - len = hwaddr_aton (NULL, line); + len = hwaddr_aton(NULL, line); if (len && len <= DUID_LEN) - hwaddr_aton (duid, line); - free (line); - } - fclose (f); + hwaddr_aton(duid, line); + free(line); + } else + len = 0; + fclose(f); if (len) - return (len); + return len; } else { if (errno != ENOENT) { - logger (LOG_ERR, "fopen `%s': %s", - DUIDFILE, strerror (errno)); - return (0); + logger(LOG_ERR, "fopen `%s': %s", + DUIDFILE, strerror(errno)); + return 0; } } /* No file? OK, lets make one based on our interface */ - type = htons (1); /* DUI-D-LLT */ - memcpy (p, &type, 2); + type = htons(1); /* DUI-D-LLT */ + memcpy(p, &type, 2); p += 2; - hw = htons (iface->family); - memcpy (p, &hw, 2); + hw = htons(iface->family); + memcpy(p, &hw, 2); p += 2; /* time returns seconds from jan 1 1970, but DUID-LLT is * seconds from jan 1 2000 modulo 2^32 */ - t = time (NULL) - THIRTY_YEARS_IN_SECONDS; - ul = htonl (t & 0xffffffff); - memcpy (p, &ul, 4); + t = time(NULL) - THIRTY_YEARS_IN_SECONDS; + ul = htonl(t & 0xffffffff); + memcpy(p, &ul, 4); p += 4; /* Finally, add the MAC address of the interface */ - memcpy (p, iface->hwaddr, iface->hwlen); + memcpy(p, iface->hwaddr, iface->hwlen); p += iface->hwlen; len = p - duid; - if (! (f = fopen (DUIDFILE, "w"))) - logger (LOG_ERR, "fopen `%s': %s", DUIDFILE, strerror (errno)); + if (!(f = fopen(DUIDFILE, "w"))) + logger(LOG_ERR, "fopen `%s': %s", DUIDFILE, strerror(errno)); else { - x = fprintf (f, "%s\n", hwaddr_ntoa (duid, len)); - fclose (f); + x = fprintf(f, "%s\n", hwaddr_ntoa(duid, len)); + fclose(f); } /* Failed to write the duid? scrub it, we cannot use it */ if (x < 1) { len = 0; - unlink (DUIDFILE); + unlink(DUIDFILE); } - return (len); + return len; } #endif diff --git a/duid.h b/duid.h index 14929900..cb335942 100644 --- a/duid.h +++ b/duid.h @@ -37,6 +37,6 @@ #include "interface.h" -size_t get_duid (unsigned char *duid, const interface_t *iface); +size_t get_duid(unsigned char *, const struct interface *); #endif #endif diff --git a/info.c b/info.c index 8369b43f..855b72df 100644 --- a/info.c +++ b/info.c @@ -46,22 +46,24 @@ /* Create a malloced string of cstr, changing ' to '\'' * so the contents work in a shell */ -static char *cleanmetas (const char *cstr) +static char * +cleanmetas(const char *cstr) { const char *p = cstr; char *new; char *n; size_t len; + size_t pos; - if (cstr == NULL || (len = strlen (cstr)) == 0) - return (xstrdup ("")); + if (cstr == NULL || (len = strlen(cstr)) == 0) + return (xstrdup("")); - n = new = xmalloc (sizeof (char) * len + 2); + n = new = xmalloc(sizeof(char) * len + 2); do if (*p == '\'') { - size_t pos = n - new; + pos = n - new; len += 4; - new = xrealloc (new, sizeof (char) * len + 1); + new = xrealloc(new, sizeof(char) * len + 1); n = new + pos; *n++ = '\''; *n++ = '\\'; @@ -74,265 +76,271 @@ static char *cleanmetas (const char *cstr) /* Terminate the sucker */ *n = '\0'; - return (new); + return new; } -static void print_addresses (FILE *f, const struct address_head *addresses) +static void +print_addresses(FILE *f, const struct address_head *addresses) { - const address_t *addr; + const struct address *addr; - STAILQ_FOREACH (addr, addresses, entries) { - fprintf (f, "%s", inet_ntoa (addr->address)); - if (STAILQ_NEXT (addr, entries)) - fprintf (f, " "); + STAILQ_FOREACH(addr, addresses, entries) { + fprintf(f, "%s", inet_ntoa(addr->address)); + if (STAILQ_NEXT(addr, entries)) + fprintf(f, " "); } } -static void print_clean (FILE *f, const char *name, const char *value) +static void +print_clean(FILE *f, const char *name, const char *value) { char *clean; if (! value) return; - clean = cleanmetas (value); - fprintf (f, "%s='%s'\n", name, clean); - free (clean); + clean = cleanmetas(value); + fprintf(f, "%s='%s'\n", name, clean); + free(clean); } -bool write_info(const interface_t *iface, const dhcp_t *dhcp, - const options_t *options, bool overwrite) +bool +write_info(const struct interface *iface, const struct dhcp *dhcp, + const struct options *options, bool overwrite) { FILE *f; - route_t *route; + struct route *route; struct stat sb; + struct in_addr addr; + bool doneone; if (options->test) f = stdout; else { - if (! overwrite && stat (iface->infofile, &sb) == 0) - return (true); - - logger (LOG_DEBUG, "writing %s", iface->infofile); - if ((f = fopen (iface->infofile, "w")) == NULL) { - logger (LOG_ERR, "fopen `%s': %s", - iface->infofile, strerror (errno)); - return (false); + if (!overwrite && stat(iface->infofile, &sb) == 0) + return true; + + logger(LOG_DEBUG, "writing %s", iface->infofile); + if ((f = fopen(iface->infofile, "w")) == NULL) { + logger(LOG_ERR, "fopen `%s': %s", + iface->infofile, strerror(errno)); + return false; } } if (dhcp->address.s_addr) { - struct in_addr n; - n.s_addr = dhcp->address.s_addr & dhcp->netmask.s_addr; - fprintf (f, "IPADDR='%s'\n", inet_ntoa (dhcp->address)); - fprintf (f, "NETMASK='%s'\n", inet_ntoa (dhcp->netmask)); - fprintf (f, "NETWORK='%s'\n", inet_ntoa (n)); - fprintf (f, "BROADCAST='%s'\n", inet_ntoa (dhcp->broadcast)); + addr.s_addr = dhcp->address.s_addr & dhcp->netmask.s_addr; + fprintf(f, "IPADDR='%s'\n", inet_ntoa(dhcp->address)); + fprintf(f, "NETMASK='%s'\n", inet_ntoa(dhcp->netmask)); + fprintf(f, "NETWORK='%s'\n", inet_ntoa(addr)); + fprintf(f, "BROADCAST='%s'\n", inet_ntoa(dhcp->broadcast)); } if (dhcp->mtu > 0) - fprintf (f, "MTU='%d'\n", dhcp->mtu); + fprintf(f, "MTU='%d'\n", dhcp->mtu); if (dhcp->routes) { - bool doneone = false; - fprintf (f, "ROUTES='"); - STAILQ_FOREACH (route, dhcp->routes, entries) { + doneone = false; + fprintf(f, "ROUTES='"); + STAILQ_FOREACH(route, dhcp->routes, entries) { if (route->destination.s_addr != 0) { if (doneone) - fprintf (f, " "); - fprintf (f, "%s", inet_ntoa (route->destination)); - fprintf (f, ",%s", inet_ntoa (route->netmask)); - fprintf (f, ",%s", inet_ntoa (route->gateway)); + fprintf(f, " "); + fprintf(f, "%s", inet_ntoa(route->destination)); + fprintf(f, ",%s", inet_ntoa(route->netmask)); + fprintf(f, ",%s", inet_ntoa(route->gateway)); doneone = true; } } - fprintf (f, "'\n"); + fprintf(f, "'\n"); doneone = false; - fprintf (f, "GATEWAYS='"); - STAILQ_FOREACH (route, dhcp->routes, entries) { + fprintf(f, "GATEWAYS='"); + STAILQ_FOREACH(route, dhcp->routes, entries) { if (route->destination.s_addr == 0) { if (doneone) - fprintf (f, " "); - fprintf (f, "%s", inet_ntoa (route->gateway)); + fprintf(f, " "); + fprintf(f, "%s", inet_ntoa(route->gateway)); doneone = true; } } - fprintf (f, "'\n"); + fprintf(f, "'\n"); } - print_clean (f, "HOSTNAME", dhcp->hostname); - print_clean (f, "DNSDOMAIN", dhcp->dnsdomain); - print_clean (f, "DNSSEARCH", dhcp->dnssearch); + print_clean(f, "HOSTNAME", dhcp->hostname); + print_clean(f, "DNSDOMAIN", dhcp->dnsdomain); + print_clean(f, "DNSSEARCH", dhcp->dnssearch); if (dhcp->dnsservers) { - fprintf (f, "DNSSERVERS='"); - print_addresses (f, dhcp->dnsservers); - fprintf (f, "'\n"); + fprintf(f, "DNSSERVERS='"); + print_addresses(f, dhcp->dnsservers); + fprintf(f, "'\n"); } if (dhcp->fqdn) { - fprintf (f, "FQDNFLAGS='%u'\n", dhcp->fqdn->flags); - fprintf (f, "FQDNRCODE1='%u'\n", dhcp->fqdn->r1); - fprintf (f, "FQDNRCODE2='%u'\n", dhcp->fqdn->r2); - print_clean (f, "FQDNHOSTNAME", dhcp->fqdn->name); + fprintf(f, "FQDNFLAGS='%u'\n", dhcp->fqdn->flags); + fprintf(f, "FQDNRCODE1='%u'\n", dhcp->fqdn->r1); + fprintf(f, "FQDNRCODE2='%u'\n", dhcp->fqdn->r2); + print_clean(f, "FQDNHOSTNAME", dhcp->fqdn->name); } if (dhcp->ntpservers) { - fprintf (f, "NTPSERVERS='"); - print_addresses (f, dhcp->ntpservers); - fprintf (f, "'\n"); + fprintf(f, "NTPSERVERS='"); + print_addresses(f, dhcp->ntpservers); + fprintf(f, "'\n"); } - print_clean (f, "NISDOMAIN", dhcp->nisdomain); + print_clean(f, "NISDOMAIN", dhcp->nisdomain); if (dhcp->nisservers) { - fprintf (f, "NISSERVERS='"); - print_addresses (f, dhcp->nisservers); - fprintf (f, "'\n"); + fprintf(f, "NISSERVERS='"); + print_addresses(f, dhcp->nisservers); + fprintf(f, "'\n"); } - print_clean (f, "ROOTPATH", dhcp->rootpath); - print_clean (f, "SIPSERVERS", dhcp->sipservers); + print_clean(f, "ROOTPATH", dhcp->rootpath); + print_clean(f, "SIPSERVERS", dhcp->sipservers); if (dhcp->serveraddress.s_addr) - fprintf (f, "DHCPSID='%s'\n", inet_ntoa (dhcp->serveraddress)); + fprintf(f, "DHCPSID='%s'\n", inet_ntoa(dhcp->serveraddress)); if (dhcp->servername[0]) - print_clean (f, "DHCPSNAME", dhcp->servername); - - if (! options->doinform && dhcp->address.s_addr) { - if (! options->test) - fprintf (f, "LEASEDFROM='%u'\n", dhcp->leasedfrom); - fprintf (f, "LEASETIME='%u'\n", dhcp->leasetime); - fprintf (f, "RENEWALTIME='%u'\n", dhcp->renewaltime); - fprintf (f, "REBINDTIME='%u'\n", dhcp->rebindtime); + print_clean(f, "DHCPSNAME", dhcp->servername); + + if (!options->doinform && dhcp->address.s_addr) { + if (!options->test) + fprintf(f, "LEASEDFROM='%u'\n", dhcp->leasedfrom); + fprintf(f, "LEASETIME='%u'\n", dhcp->leasetime); + fprintf(f, "RENEWALTIME='%u'\n", dhcp->renewaltime); + fprintf(f, "REBINDTIME='%u'\n", dhcp->rebindtime); } - print_clean (f, "INTERFACE", iface->name); - print_clean (f, "CLASSID", options->classid); + print_clean(f, "INTERFACE", iface->name); + print_clean(f, "CLASSID", options->classid); if (iface->clientid_len > 0) { - fprintf (f, "CLIENTID='%s'\n", - hwaddr_ntoa (iface->clientid, iface->clientid_len)); + fprintf(f, "CLIENTID='%s'\n", + hwaddr_ntoa(iface->clientid, iface->clientid_len)); } - fprintf (f, "DHCPCHADDR='%s'\n", 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"); + fprintf(f, "\n# dhcpcd-1.x and 2.x compatible variables\n"); if (dhcp->dnsservers) { - address_t *addr; + struct address *a; - fprintf (f, "DNS='"); - STAILQ_FOREACH (addr, dhcp->dnsservers, entries) { - fprintf (f, "%s", inet_ntoa (addr->address)); - if (STAILQ_NEXT (addr, entries)) - fprintf (f, ","); + fprintf(f, "DNS='"); + STAILQ_FOREACH(a, dhcp->dnsservers, entries) { + fprintf(f, "%s", inet_ntoa(a->address)); + if (STAILQ_NEXT(a, entries)) + fprintf(f, ","); } - fprintf (f, "'\n"); + fprintf(f, "'\n"); } if (dhcp->routes) { - bool doneone = false; - fprintf (f, "GATEWAY='"); - STAILQ_FOREACH (route, dhcp->routes, entries) { + doneone = false; + fprintf(f, "GATEWAY='"); + STAILQ_FOREACH(route, dhcp->routes, entries) { if (route->destination.s_addr == 0) { if (doneone) - fprintf (f, ","); - fprintf (f, "%s", inet_ntoa (route->gateway)); + fprintf(f, ","); + fprintf(f, "%s", inet_ntoa(route->gateway)); doneone = true; } } - fprintf (f, "'\n"); + fprintf(f, "'\n"); } #endif - if (! options->test) - fclose (f); - return (true); + if (!options->test) + fclose(f); + return true; } -static bool parse_address (struct in_addr *addr, - const char *value, const char *var) +static bool +parse_address(struct in_addr *addr, const char *value, const char *var) { - if (inet_aton (value, addr) == 0) { - logger (LOG_ERR, "%s `%s': %s", var, value, - strerror (errno)); - return (false); + if (inet_aton(value, addr) == 0) { + logger(LOG_ERR, "%s `%s': %s", var, value, strerror(errno)); + return false; } - return (true); + return true; } -static bool parse_uint (unsigned int *i, - const char *value, const char *var) +static bool +parse_uint(unsigned int *i, const char *value, const char *var) { - if (sscanf (value, "%u", i) != 1) { - logger (LOG_ERR, "%s `%s': not a valid number", - var, value); - return (false); + if (sscanf(value, "%u", i) != 1) { + logger(LOG_ERR, "%s `%s': not a valid number", var, value); + return false; } - return (true); + return true; } -static bool parse_ushort (unsigned short *s, - const char *value, const char *var) +static bool +parse_ushort(unsigned short *s, const char *value, const char *var) { - if (sscanf (value, "%hu", s) != 1) { - logger (LOG_ERR, "%s `%s': not a valid number", - var, value); - return (false); + if (sscanf(value, "%hu", s) != 1) { + logger(LOG_ERR, "%s `%s': not a valid number", var, value); + return false; } - return (true); + return true; } -static struct address_head *parse_addresses (char *value, const char *var) +static struct address_head * +parse_addresses(char *value, const char *var) { char *token; char *p = value; struct address_head *head = NULL; + struct address *a; while ((token = strsep (&p, " "))) { - address_t *a = xzalloc (sizeof (*a)); - - if (inet_aton (token, &a->address) == 0) { - logger (LOG_ERR, "%s: invalid address `%s'", var, token); - free_address (head); - free (a); - return (NULL); + a = xzalloc (sizeof(*a)); + if (inet_aton(token, &a->address) == 0) { + logger(LOG_ERR, "%s: invalid address `%s'", var, token); + free_address(head); + free(a); + return NULL; } - if (! head) { - head = xmalloc (sizeof (*head)); - STAILQ_INIT (head); + if (!head) { + head = xmalloc(sizeof(*head)); + STAILQ_INIT(head); } - STAILQ_INSERT_TAIL (head, a, entries); + STAILQ_INSERT_TAIL(head, a, entries); } - return (head); + return head; } -bool read_info (const interface_t *iface, dhcp_t *dhcp) +bool +read_info(const struct interface *iface, struct dhcp *dhcp) { FILE *fp; - char *line; + char *line = NULL; + size_t len = 0; char *var; char *value; char *p; struct stat sb; + char *pp, *dest, *net, *gate; + struct route *route; - if (stat (iface->infofile, &sb) != 0) { - logger (LOG_ERR, "lease information file `%s' does not exist", - iface->infofile); - return (false); + if (stat(iface->infofile, &sb) != 0) { + logger(LOG_ERR, "lease information file `%s' does not exist", + iface->infofile); + return false; } - if (! (fp = fopen (iface->infofile, "r"))) { - logger (LOG_ERR, "fopen `%s': %s", - iface->infofile, strerror (errno)); - return (false); + if (!(fp = fopen(iface->infofile, "r"))) { + logger(LOG_ERR, "fopen `%s': %s", + iface->infofile, strerror(errno)); + return false; } dhcp->frominfo = true; - while ((line = get_line (fp))) { + while ((get_line(&line, &len, fp))) { var = line; /* Strip leading spaces/tabs */ @@ -340,17 +348,17 @@ bool read_info (const interface_t *iface, dhcp_t *dhcp) var++; /* Trim trailing \n */ - p = var + strlen (var) - 1; + p = var + strlen(var) - 1; if (*p == '\n') *p = 0; /* Skip comments */ if (*var == '#') - goto next; + continue; /* If we don't have an equals sign then skip it */ - if (! (p = strchr (var, '='))) - goto next; + if (!(p = strchr(var, '='))) + continue; /* Terminate the = so we have two strings */ *p = 0; @@ -359,113 +367,121 @@ bool read_info (const interface_t *iface, dhcp_t *dhcp) /* Strip leading and trailing quotes if present */ if (*value == '\'' || *value == '"') value++; - p = value + strlen (value) - 1; + p = value + strlen(value) - 1; if (*p == '\'' || *p == '"') *p = 0; /* Don't process null vars or values */ - if (! *var || ! *value) - goto next; + if (!*var || !*value) + continue; - if (strcmp (var, "IPADDR") == 0) - parse_address (&dhcp->address, value, "IPADDR"); - else if (strcmp (var, "NETMASK") == 0) + if (strcmp(var, "IPADDR") == 0) + parse_address(&dhcp->address, value, "IPADDR"); + else if (strcmp(var, "NETMASK") == 0) parse_address (&dhcp->netmask, value, "NETMASK"); - else if (strcmp (var, "BROADCAST") == 0) + else if (strcmp(var, "BROADCAST") == 0) parse_address (&dhcp->broadcast, value, "BROADCAST"); - else if (strcmp (var, "MTU") == 0) + else if (strcmp(var, "MTU") == 0) parse_ushort (&dhcp->mtu, value, "MTU"); - else if (strcmp (var, "ROUTES") == 0) { + else if (strcmp(var, "ROUTES") == 0) { p = value; while ((value = strsep (&p, " "))) { - char *pp = value; - char *dest = strsep (&pp, ","); - char *net = strsep (&pp, ","); - char *gate = strsep (&pp, ","); - route_t *route; - - if (! dest || ! net || ! gate) { - logger (LOG_ERR, "read_info ROUTES `%s,%s,%s': invalid route", + pp = value; + dest = strsep (&pp, ","); + net = strsep (&pp, ","); + gate = strsep (&pp, ","); + + if (!dest || !net || !gate) { + logger(LOG_ERR, + "read_info ROUTES `%s,%s,%s': " + "invalid route", dest, net, gate); - goto next; + continue; } /* See if we can create a route */ - route = xzalloc (sizeof (*route)); - if (inet_aton (dest, &route->destination) == 0) { - logger (LOG_ERR, "read_info ROUTES `%s': not a valid destination address", - dest); - free (route); - goto next; + route = xzalloc(sizeof(*route)); + if (inet_aton(dest, &route->destination) == 0) { + logger(LOG_ERR, + "read_info ROUTES `%s': " + "not a valid destination address", + dest); + free(route); + continue; } - if (inet_aton (dest, &route->netmask) == 0) { - logger (LOG_ERR, "read_info ROUTES `%s': not a valid netmask address", - net); - free (route); - goto next; + if (inet_aton(dest, &route->netmask) == 0) { + logger(LOG_ERR, + "read_info ROUTES `%s': " + "not a valid netmask address", + net); + free(route); + continue; } - if (inet_aton (dest, &route->gateway) == 0) { - logger (LOG_ERR, "read_info ROUTES `%s': not a valid gateway address", - gate); - free (route); - goto next; + if (inet_aton(dest, &route->gateway) == 0) { + logger(LOG_ERR, + "read_info ROUTES `%s': " + "not a valid gateway address", + gate); + free(route); + continue; } /* OK, now add our route */ - if (! dhcp->routes) { - dhcp->routes = xmalloc (sizeof (*dhcp->routes)); - STAILQ_INIT (dhcp->routes); + if (!dhcp->routes) { + dhcp->routes = xmalloc(sizeof(*dhcp->routes)); + STAILQ_INIT(dhcp->routes); } - STAILQ_INSERT_TAIL (dhcp->routes, route, entries); + STAILQ_INSERT_TAIL(dhcp->routes, route, entries); } - } else if (strcmp (var, "GATEWAYS") == 0) { + } else if (strcmp(var, "GATEWAYS") == 0) { p = value; while ((value = strsep (&p, " "))) { - route_t *route = xzalloc (sizeof (*route)); - if (parse_address (&route->gateway, value, "GATEWAYS")) { - if (! dhcp->routes) { - dhcp->routes = xmalloc (sizeof (*dhcp->routes)); - STAILQ_INIT (dhcp->routes); + route = xzalloc(sizeof(*route)); + if (parse_address(&route->gateway, value, + "GATEWAYS")) + { + if (!dhcp->routes) { + dhcp->routes = xmalloc(sizeof(*dhcp->routes)); + STAILQ_INIT(dhcp->routes); } - STAILQ_INSERT_TAIL (dhcp->routes, route, entries); + STAILQ_INSERT_TAIL(dhcp->routes, route, entries); } else - free (route); + free(route); } - } else if (strcmp (var, "HOSTNAME") == 0) - dhcp->hostname = xstrdup (value); + } else if (strcmp(var, "HOSTNAME") == 0) + dhcp->hostname = xstrdup(value); else if (strcmp (var, "DNSDOMAIN") == 0) - dhcp->dnsdomain = xstrdup (value); - else if (strcmp (var, "DNSSEARCH") == 0) - dhcp->dnssearch = xstrdup (value); - else if (strcmp (var, "DNSSERVERS") == 0) - dhcp->dnsservers = parse_addresses (value, "DNSSERVERS"); - else if (strcmp (var, "NTPSERVERS") == 0) - dhcp->ntpservers = parse_addresses (value, "NTPSERVERS"); - else if (strcmp (var, "NISDOMAIN") == 0) + dhcp->dnsdomain = xstrdup(value); + else if (strcmp(var, "DNSSEARCH") == 0) + dhcp->dnssearch = xstrdup(value); + else if (strcmp(var, "DNSSERVERS") == 0) + dhcp->dnsservers = parse_addresses(value, "DNSSERVERS"); + else if (strcmp(var, "NTPSERVERS") == 0) + dhcp->ntpservers = parse_addresses(value, "NTPSERVERS"); + else if (strcmp(var, "NISDOMAIN") == 0) dhcp->nisdomain = xstrdup (value); - else if (strcmp (var, "NISSERVERS") == 0) - dhcp->nisservers = parse_addresses (value, "NISSERVERS"); - else if (strcmp (var, "ROOTPATH") == 0) - dhcp->rootpath = xstrdup (value); - else if (strcmp (var, "DHCPSID") == 0) - parse_address (&dhcp->serveraddress, value, "DHCPSID"); - else if (strcmp (var, "DHCPSNAME") == 0) - strlcpy (dhcp->servername, value, sizeof (dhcp->servername)); - else if (strcmp (var, "LEASEDFROM") == 0) - parse_uint (&dhcp->leasedfrom, value, "LEASEDFROM"); - else if (strcmp (var, "LEASETIME") == 0) - parse_uint (&dhcp->leasetime, value, "LEASETIME"); - else if (strcmp (var, "RENEWALTIME") == 0) - parse_uint (&dhcp->renewaltime, value, "RENEWALTIME"); - else if (strcmp (var, "REBINDTIME") == 0) - parse_uint (&dhcp->rebindtime, value, "REBINDTIME"); - -next: - free (line); + else if (strcmp(var, "NISSERVERS") == 0) + dhcp->nisservers = parse_addresses(value, "NISSERVERS"); + else if (strcmp(var, "ROOTPATH") == 0) + dhcp->rootpath = xstrdup(value); + else if (strcmp(var, "DHCPSID") == 0) + parse_address(&dhcp->serveraddress, value, "DHCPSID"); + else if (strcmp(var, "DHCPSNAME") == 0) + strlcpy(dhcp->servername, value, + sizeof(dhcp->servername)); + else if (strcmp(var, "LEASEDFROM") == 0) + parse_uint(&dhcp->leasedfrom, value, "LEASEDFROM"); + else if (strcmp(var, "LEASETIME") == 0) + parse_uint(&dhcp->leasetime, value, "LEASETIME"); + else if (strcmp(var, "RENEWALTIME") == 0) + parse_uint(&dhcp->renewaltime, value, "RENEWALTIME"); + else if (strcmp(var, "REBINDTIME") == 0) + parse_uint(&dhcp->rebindtime, value, "REBINDTIME"); } fclose (fp); - return (true); + free(line); + return true; } #endif diff --git a/info.h b/info.h index 22966dba..1c875edc 100644 --- a/info.h +++ b/info.h @@ -33,10 +33,10 @@ #include "dhcp.h" #ifdef ENABLE_INFO -bool write_info (const interface_t *iface, const dhcp_t *dhcp, - const options_t *options, bool overwrite); +bool write_info(const struct interface *, const struct dhcp *, + const struct options *, bool); -bool read_info (const interface_t *iface, dhcp_t *dhcp); +bool read_info(const struct interface *, struct dhcp *); #endif #endif diff --git a/interface.c b/interface.c index d2ff8d6d..82633a8e 100644 --- a/interface.c +++ b/interface.c @@ -61,91 +61,96 @@ #include "interface.h" #include "logger.h" -void free_address (struct address_head *addresses) +void +free_address(struct address_head *addresses) { - address_t *p; - address_t *n; + struct address *p; + struct address *n; - if (! addresses) + if (!addresses) return; - - p = STAILQ_FIRST (addresses); + p = STAILQ_FIRST(addresses); while (p) { - n = STAILQ_NEXT (p, entries); - free (p); + n = STAILQ_NEXT(p, entries); + free(p); p = n; } - free (addresses); + free(addresses); } -void free_route (struct route_head *routes) +void +free_route(struct route_head *routes) { - route_t *p; - route_t *n; + struct route *p; + struct route *n; - if (! routes) + if (!routes) return; - - p = STAILQ_FIRST (routes); + p = STAILQ_FIRST(routes); while (p) { - n = STAILQ_NEXT (p, entries); - free (p); + n = STAILQ_NEXT(p, entries); + free(p); p = n; } - free (routes); + free(routes); } -int inet_ntocidr (struct in_addr address) +int +inet_ntocidr(struct in_addr address) { int cidr = 0; - uint32_t mask = htonl (address.s_addr); + uint32_t mask = htonl(address.s_addr); while (mask) { cidr++; mask <<= 1; } - return (cidr); + return cidr; } -int inet_cidrtoaddr (int cidr, struct in_addr *addr) { +int +inet_cidrtoaddr (int cidr, struct in_addr *addr) +{ int ocets; if (cidr < 0 || cidr > 32) { errno = EINVAL; - return (-1); + return -1; } ocets = (cidr + 7) / 8; - memset (addr, 0, sizeof (*addr)); + addr->s_addr = 0; if (ocets > 0) { - memset (&addr->s_addr, 255, (size_t) ocets - 1); - memset ((unsigned char *) &addr->s_addr + (ocets - 1), - (256 - (1 << (32 - cidr) % 8)), 1); + memset(&addr->s_addr, 255, (size_t)ocets - 1); + memset((unsigned char *)&addr->s_addr + (ocets - 1), + (256 - (1 << (32 - cidr) % 8)), 1); } - return (0); + return 0; } -uint32_t get_netmask (uint32_t addr) +uint32_t +get_netmask(uint32_t addr) { uint32_t dst; if (addr == 0) - return (0); + return 0; - dst = htonl (addr); - if (IN_CLASSA (dst)) - return (ntohl (IN_CLASSA_NET)); + dst = htonl(addr); + if (IN_CLASSA(dst)) + return ntohl(IN_CLASSA_NET); if (IN_CLASSB (dst)) - return (ntohl (IN_CLASSB_NET)); + return ntohl(IN_CLASSB_NET); if (IN_CLASSC (dst)) - return (ntohl (IN_CLASSC_NET)); + return ntohl(IN_CLASSC_NET); - return (0); + return 0; } -char *hwaddr_ntoa (const unsigned char *hwaddr, size_t hwlen) +char * +hwaddr_ntoa(const unsigned char *hwaddr, size_t hwlen) { static char buffer[(HWADDR_LEN * 3) + 1]; char *p = buffer; @@ -154,15 +159,16 @@ char *hwaddr_ntoa (const unsigned char *hwaddr, size_t hwlen) for (i = 0; i < hwlen && i < HWADDR_LEN; i++) { if (i > 0) *p ++= ':'; - p += snprintf (p, 3, "%.2x", hwaddr[i]); + p += snprintf(p, 3, "%.2x", hwaddr[i]); } *p ++= '\0'; - return (buffer); + return buffer; } -size_t hwaddr_aton (unsigned char *buffer, const char *addr) +size_t +hwaddr_aton(unsigned char *buffer, const char *addr) { char c[3]; const char *p = addr; @@ -174,56 +180,67 @@ size_t hwaddr_aton (unsigned char *buffer, const char *addr) c[0] = *p++; c[1] = *p++; /* Ensure that next data is EOL or a seperator with data */ - if (! (*p == '\0' || (*p == ':' && *(p + 1) != '\0'))) { + if (!(*p == '\0' || (*p == ':' && *(p + 1) != '\0'))) { errno = EINVAL; - return (0); + return 0; } /* Ensure that digits are hex */ - if (isxdigit ((int) c[0]) == 0 || isxdigit ((int) c[1]) == 0) { + if (isxdigit ((int)c[0]) == 0 || isxdigit((int)c[1]) == 0) { errno = EINVAL; - return (0); + return 0; } p++; if (bp) - *bp++ = (unsigned char) strtol (c, NULL, 16); + *bp++ = (unsigned char)strtol(c, NULL, 16); else len++; } if (bp) - return (bp - buffer); - return (len); + return bp - buffer; + return len; } -static int _do_interface (const char *ifname, - _unused unsigned char *hwaddr, _unused size_t *hwlen, - struct in_addr *addr, - bool flush, bool get) +static int +_do_interface(const char *ifname, + _unused unsigned char *hwaddr, _unused size_t *hwlen, + struct in_addr *addr, bool flush, bool get) { int s; struct ifconf ifc; int retval = 0; - int len = 10 * sizeof (struct ifreq); + int len = 10 * sizeof(struct ifreq); int lastlen = 0; char *p; + union { + char *buffer; + struct ifreq *ifr; + } ifreqs; + struct sockaddr_in address; + struct ifreq *ifr; + struct sockaddr_in netmask; + +#ifdef AF_LINK + struct sockaddr_dl sdl; +#endif - if ((s = socket (AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); + if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { + logger(LOG_ERR, "socket: %s", strerror(errno)); return -1; } /* Not all implementations return the needed buffer size for * SIOGIFCONF so we loop like so for all until it works */ - memset (&ifc, 0, sizeof (ifc)); + memset(&ifc, 0, sizeof(ifc)); for (;;) { ifc.ifc_len = len; - ifc.ifc_buf = xmalloc ((size_t) len); - if (ioctl (s, SIOCGIFCONF, &ifc) == -1) { + ifc.ifc_buf = xmalloc((size_t)len); + if (ioctl(s, SIOCGIFCONF, &ifc) == -1) { if (errno != EINVAL || lastlen != 0) { - logger (LOG_ERR, "ioctl SIOCGIFCONF: %s", - strerror (errno)); - close (s); - free (ifc.ifc_buf); + logger(LOG_ERR, "ioctl SIOCGIFCONF: %s", + strerror(errno)); + close(s); + free(ifc.ifc_buf); return -1; } } else { @@ -232,62 +249,51 @@ static int _do_interface (const char *ifname, lastlen = ifc.ifc_len; } - free (ifc.ifc_buf); + free(ifc.ifc_buf); ifc.ifc_buf = NULL; len *= 2; } for (p = ifc.ifc_buf; p < ifc.ifc_buf + ifc.ifc_len;) { - union { - char *buffer; - struct ifreq *ifr; - } ifreqs; - struct sockaddr_in address; - struct ifreq *ifr; - /* Cast the ifc buffer to an ifreq cleanly */ ifreqs.buffer = p; ifr = ifreqs.ifr; #ifdef __linux__ - p += sizeof (*ifr); + p += sizeof(*ifr); #else - p += offsetof (struct ifreq, ifr_ifru) + ifr->ifr_addr.sa_len; + p += offsetof(struct ifreq, ifr_ifru) + ifr->ifr_addr.sa_len; #endif - if (strcmp (ifname, ifr->ifr_name) != 0) + if (strcmp(ifname, ifr->ifr_name) != 0) continue; #ifdef AF_LINK if (hwaddr && hwlen && ifr->ifr_addr.sa_family == AF_LINK) { - struct sockaddr_dl sdl; - - memcpy (&sdl, &ifr->ifr_addr, sizeof (sdl)); + memcpy(&sdl, &ifr->ifr_addr, sizeof(sdl)); *hwlen = sdl.sdl_alen; - memcpy (hwaddr, sdl.sdl_data + sdl.sdl_nlen, - (size_t) sdl.sdl_alen); + memcpy(hwaddr, sdl.sdl_data + sdl.sdl_nlen, + (size_t)sdl.sdl_alen); retval = 1; break; } #endif if (ifr->ifr_addr.sa_family == AF_INET) { - memcpy (&address, &ifr->ifr_addr, sizeof (address)); + memcpy(&address, &ifr->ifr_addr, sizeof(address)); if (flush) { - struct sockaddr_in netmask; - - if (ioctl (s, SIOCGIFNETMASK, ifr) == -1) { - logger (LOG_ERR, - "ioctl SIOCGIFNETMASK: %s", - strerror (errno)); + if (ioctl(s, SIOCGIFNETMASK, ifr) == -1) { + logger(LOG_ERR, + "ioctl SIOCGIFNETMASK: %s", + strerror(errno)); continue; } - memcpy (&netmask, &ifr->ifr_addr, - sizeof (netmask)); + memcpy(&netmask, &ifr->ifr_addr, + sizeof(netmask)); - if (del_address (ifname, - address.sin_addr, - netmask.sin_addr) == -1) + if (del_address(ifname, + address.sin_addr, + netmask.sin_addr) == -1) retval = -1; } else if (get) { addr->s_addr = address.sin_addr.s_addr; @@ -301,16 +307,17 @@ static int _do_interface (const char *ifname, } - close (s); - free (ifc.ifc_buf); + close(s); + free(ifc.ifc_buf); return retval; } -interface_t *read_interface (const char *ifname, _unused int metric) +struct interface * +read_interface (const char *ifname, _unused int metric) { int s; struct ifreq ifr; - interface_t *iface = NULL; + struct interface *iface = NULL; unsigned char *hwaddr = NULL; size_t hwlen = 0; sa_family_t family = 0; @@ -319,114 +326,111 @@ interface_t *read_interface (const char *ifname, _unused int metric) char *p; #endif - if (! ifname) - return NULL; - - memset (&ifr, 0, sizeof (ifr)); - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + memset(&ifr, 0, sizeof(ifr)); + strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); - if ((s = socket (AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); + if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { + logger(LOG_ERR, "socket: %s", strerror(errno)); return NULL; } #ifdef __linux__ - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - if (ioctl (s, SIOCGIFHWADDR, &ifr) == -1) { - logger (LOG_ERR, "ioctl SIOCGIFHWADDR: %s", strerror (errno)); + strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); + if (ioctl(s, SIOCGIFHWADDR, &ifr) == -1) { + logger(LOG_ERR, "ioctl SIOCGIFHWADDR: %s", strerror(errno)); goto exit; } switch (ifr.ifr_hwaddr.sa_family) { - case ARPHRD_ETHER: - case ARPHRD_IEEE802: - hwlen = ETHER_ADDR_LEN; - break; - case ARPHRD_IEEE1394: - hwlen = EUI64_ADDR_LEN; - case ARPHRD_INFINIBAND: - hwlen = INFINIBAND_ADDR_LEN; - break; - default: - logger (LOG_ERR, - "interface is not Ethernet, FireWire, " \ - "InfiniBand or Token Ring"); - goto exit; + case ARPHRD_ETHER: + case ARPHRD_IEEE802: + hwlen = ETHER_ADDR_LEN; + break; + case ARPHRD_IEEE1394: + hwlen = EUI64_ADDR_LEN; + case ARPHRD_INFINIBAND: + hwlen = INFINIBAND_ADDR_LEN; + break; + default: + logger (LOG_ERR, + "interface is not Ethernet, FireWire, " \ + "InfiniBand or Token Ring"); + goto exit; } - hwaddr = xmalloc (sizeof (unsigned char) * HWADDR_LEN); - memcpy (hwaddr, ifr.ifr_hwaddr.sa_data, hwlen); + hwaddr = xmalloc(sizeof(unsigned char) * HWADDR_LEN); + memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, hwlen); family = ifr.ifr_hwaddr.sa_family; #else ifr.ifr_metric = metric; - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - if (ioctl (s, SIOCSIFMETRIC, &ifr) == -1) { - logger (LOG_ERR, "ioctl SIOCSIFMETRIC: %s", strerror (errno)); + strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); + if (ioctl(s, SIOCSIFMETRIC, &ifr) == -1) { + logger(LOG_ERR, "ioctl SIOCSIFMETRIC: %s", strerror(errno)); goto exit; } - hwaddr = xmalloc (sizeof (unsigned char) * HWADDR_LEN); - if (_do_interface (ifname, hwaddr, &hwlen, NULL, false, false) != 1) { - logger (LOG_ERR, "could not find interface %s", ifname); + hwaddr = xmalloc(sizeof(unsigned char) * HWADDR_LEN); + if (_do_interface(ifname, hwaddr, &hwlen, NULL, false, false) != 1) { + logger(LOG_ERR, "could not find interface %s", ifname); goto exit; } family = ARPHRD_ETHER; #endif - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - if (ioctl (s, SIOCGIFMTU, &ifr) == -1) { - logger (LOG_ERR, "ioctl SIOCGIFMTU: %s", strerror (errno)); + strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); + if (ioctl(s, SIOCGIFMTU, &ifr) == -1) { + logger(LOG_ERR, "ioctl SIOCGIFMTU: %s", strerror(errno)); goto exit; } 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)); + strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); + if (ioctl(s, SIOCSIFMTU, &ifr) == -1) { + logger(LOG_ERR, "ioctl SIOCSIFMTU,: %s", + strerror(errno)); goto exit; } } mtu = ifr.ifr_mtu; - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); #ifdef __linux__ /* We can only bring the real interface up */ - if ((p = strchr (ifr.ifr_name, ':'))) + if ((p = strchr(ifr.ifr_name, ':'))) *p = '\0'; #endif - if (ioctl (s, SIOCGIFFLAGS, &ifr) == -1) { - logger (LOG_ERR, "ioctl SIOCGIFFLAGS: %s", strerror (errno)); + if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) { + logger(LOG_ERR, "ioctl SIOCGIFFLAGS: %s", strerror(errno)); goto exit; } - if (! (ifr.ifr_flags & IFF_UP) || ! (ifr.ifr_flags & IFF_RUNNING)) { + if (!(ifr.ifr_flags & IFF_UP) || !(ifr.ifr_flags & IFF_RUNNING)) { ifr.ifr_flags |= IFF_UP | IFF_RUNNING; - if (ioctl (s, SIOCSIFFLAGS, &ifr) != 0) { - logger (LOG_ERR, "ioctl SIOCSIFFLAGS: %s", - strerror (errno)); + if (ioctl(s, SIOCSIFFLAGS, &ifr) != 0) { + logger(LOG_ERR, "ioctl SIOCSIFFLAGS: %s", + strerror(errno)); goto exit; } } - iface = xzalloc (sizeof (*iface)); - strlcpy (iface->name, ifname, IF_NAMESIZE); + iface = xzalloc(sizeof(*iface)); + strlcpy(iface->name, ifname, IF_NAMESIZE); #ifdef ENABLE_INFO - snprintf (iface->infofile, PATH_MAX, INFOFILE, ifname); + snprintf(iface->infofile, PATH_MAX, INFOFILE, ifname); #endif - memcpy (&iface->hwaddr, hwaddr, hwlen); + memcpy(&iface->hwaddr, hwaddr, hwlen); iface->hwlen = hwlen; iface->family = family; - iface->arpable = ! (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)); + iface->arpable = !(ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)); iface->mtu = iface->previous_mtu = mtu; - logger (LOG_INFO, "hardware address = %s", - hwaddr_ntoa (iface->hwaddr, iface->hwlen)); + logger(LOG_INFO, "hardware address = %s", + hwaddr_ntoa(iface->hwaddr, iface->hwlen)); /* 0 is a valid fd, so init to -1 */ iface->fd = -1; @@ -435,66 +439,66 @@ interface_t *read_interface (const char *ifname, _unused int metric) #endif exit: - close (s); - free (hwaddr); + close(s); + free(hwaddr); return iface; } -int get_mtu (const char *ifname) +int +get_mtu(const char *ifname) { struct ifreq ifr; int r; int s; if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return (-1); + logger(LOG_ERR, "socket: %s", strerror(errno)); + return -1; } - memset (&ifr, 0, sizeof (ifr)); - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); - r = ioctl (s, SIOCGIFMTU, &ifr); - close (s); + memset(&ifr, 0, sizeof(ifr)); + strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); + r = ioctl(s, SIOCGIFMTU, &ifr); + close(s); if (r == -1) { - logger (LOG_ERR, "ioctl SIOCGIFMTU: %s", strerror (errno)); - return (-1); + logger(LOG_ERR, "ioctl SIOCGIFMTU: %s", strerror(errno)); + return -1; } - return (ifr.ifr_mtu); + return ifr.ifr_mtu; } -int set_mtu (const char *ifname, short int mtu) +int +set_mtu(const char *ifname, short int mtu) { struct ifreq ifr; int r; int s; if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return (-1); + logger(LOG_ERR, "socket: %s", strerror(errno)); + return -1; } - memset (&ifr, 0, sizeof (ifr)); - logger (LOG_DEBUG, "setting MTU to %d", mtu); - strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + memset(&ifr, 0, sizeof(ifr)); + logger(LOG_DEBUG, "setting MTU to %d", mtu); + strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); ifr.ifr_mtu = mtu; - r = ioctl (s, SIOCSIFMTU, &ifr); - close (s); + r = ioctl(s, SIOCSIFMTU, &ifr); + close(s); if (r == -1) - logger (LOG_ERR, "ioctl SIOCSIFMTU: %s", strerror (errno)); + logger(LOG_ERR, "ioctl SIOCSIFMTU: %s", strerror(errno)); - return (r == 0 ? 0 : -1); + return r == 0 ? 0 : -1; } -static void log_route (struct in_addr destination, - struct in_addr netmask, - struct in_addr gateway, - _unused int metric, - int change, int del) +static void +log_route (struct in_addr destination, struct in_addr netmask, + struct in_addr gateway, _unused int metric, int change, int del) { - char *dstd = xstrdup (inet_ntoa (destination)); + char *dstd = xstrdup(inet_ntoa(destination)); #ifdef __linux__ #define METRIC " metric %d" @@ -504,32 +508,32 @@ static void log_route (struct in_addr destination, if (gateway.s_addr == destination.s_addr || gateway.s_addr == INADDR_ANY) - logger (LOG_INFO, "%s route to %s/%d" METRIC, - change ? "changing" : del ? "removing" : "adding", - dstd, inet_ntocidr (netmask) + logger(LOG_INFO, "%s route to %s/%d" METRIC, + 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) + logger(LOG_INFO, "%s default route via %s" METRIC, + 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) + logger(LOG_INFO, "%s route to %s/%d via %s" METRIC, + change ? "changing" : del ? "removing" : "adding", + dstd, inet_ntocidr(netmask), inet_ntoa(gateway) #ifdef __linux__ - , metric + , metric #endif - ); + ); - free (dstd); + free(dstd); } #if defined(BSD) || defined(__FreeBSD_kernel__) @@ -542,57 +546,53 @@ static void log_route (struct in_addr destination, 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) +static int +do_address(const char *ifname, struct in_addr address, + struct in_addr netmask, struct in_addr broadcast, int del) { int s; struct ifaliasreq ifa; - - if (! ifname) - return -1; + union { + struct sockaddr *sa; + struct sockaddr_in *sin; + } _s; if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); + logger(LOG_ERR, "socket: %s", strerror(errno)); return -1; } - memset (&ifa, 0, sizeof (ifa)); - strlcpy (ifa.ifra_name, ifname, sizeof (ifa.ifra_name)); + memset(&ifa, 0, sizeof(ifa)); + strlcpy(ifa.ifra_name, ifname, sizeof(ifa.ifra_name)); -#define ADDADDR(_var, _addr) { \ - union { struct sockaddr *sa; struct sockaddr_in *sin; } _s; \ +#define ADDADDR(_var, _addr) \ _s.sa = &_var; \ _s.sin->sin_family = AF_INET; \ - _s.sin->sin_len = sizeof (*_s.sin); \ - memcpy (&_s.sin->sin_addr, &_addr, sizeof (_s.sin->sin_addr)); \ -} - - ADDADDR (ifa.ifra_addr, address); - ADDADDR (ifa.ifra_mask, netmask); -if (! del) - ADDADDR (ifa.ifra_broadaddr, broadcast); + _s.sin->sin_len = sizeof(*_s.sin); \ + memcpy(&_s.sin->sin_addr, &_addr, sizeof(_s.sin->sin_addr)); + ADDADDR(ifa.ifra_addr, address); + ADDADDR(ifa.ifra_mask, netmask); + 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)); - close (s); + if (ioctl(s, del ? SIOCDIFADDR : SIOCAIFADDR, &ifa) == -1) { + 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) +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) { int s; static int seq; @@ -608,22 +608,21 @@ static int do_route (const char *ifname, struct rtm { struct rt_msghdr hdr; - char buffer[sizeof (su) * 3]; + char buffer[sizeof(su) * 3]; } rtm; char *bp = rtm.buffer; size_t l; + unsigned char *hwaddr; + size_t hwlen = 0; - if (! ifname) - return -1; - - log_route (destination, netmask, gateway, metric, change, del); + log_route(destination, netmask, gateway, metric, change, del); - if ((s = socket (PF_ROUTE, SOCK_RAW, 0)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); + if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) == -1) { + logger(LOG_ERR, "socket: %s", strerror(errno)); return -1; } - memset (&rtm, 0, sizeof (rtm)); + memset(&rtm, 0, sizeof(rtm)); rtm.hdr.rtm_version = RTM_VERSION; rtm.hdr.rtm_seq = ++seq; @@ -634,10 +633,10 @@ static int do_route (const char *ifname, rtm.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; #define ADDADDR(_addr) \ - memset (&su, 0, sizeof (su)); \ + memset (&su, 0, sizeof(su)); \ su.sin.sin_family = AF_INET; \ - su.sin.sin_len = sizeof (su.sin); \ - memcpy (&su.sin.sin_addr, &_addr, sizeof (su.sin.sin_addr)); \ + su.sin.sin_len = sizeof(su.sin); \ + memcpy (&su.sin.sin_addr, &_addr, sizeof(su.sin.sin_addr)); \ l = SA_SIZE (&(su.sa)); \ memcpy (bp, &(su), l); \ bp += l; @@ -648,45 +647,42 @@ static int do_route (const char *ifname, gateway.s_addr == INADDR_ANY) { /* Make us a link layer socket */ - unsigned char *hwaddr; - size_t hwlen = 0; - if (netmask.s_addr == INADDR_BROADCAST) rtm.hdr.rtm_flags |= RTF_HOST; - hwaddr = xmalloc (sizeof (unsigned char) * HWADDR_LEN); - _do_interface (ifname, hwaddr, &hwlen, NULL, false, false); - memset (&su, 0, sizeof (su)); - su.sdl.sdl_len = sizeof (su.sdl); + hwaddr = xmalloc(sizeof(unsigned char) * HWADDR_LEN); + _do_interface(ifname, hwaddr, &hwlen, NULL, false, false); + memset(&su, 0, sizeof(su)); + su.sdl.sdl_len = sizeof(su.sdl); su.sdl.sdl_family = AF_LINK; - su.sdl.sdl_nlen = strlen (ifname); - memcpy (&su.sdl.sdl_data, ifname, (size_t) su.sdl.sdl_nlen); + su.sdl.sdl_nlen = strlen(ifname); + memcpy(&su.sdl.sdl_data, ifname, (size_t)su.sdl.sdl_nlen); su.sdl.sdl_alen = hwlen; - memcpy (((unsigned char *) &su.sdl.sdl_data) + su.sdl.sdl_nlen, - hwaddr, (size_t) su.sdl.sdl_alen); + memcpy(((unsigned char *)&su.sdl.sdl_data) + su.sdl.sdl_nlen, + hwaddr, (size_t)su.sdl.sdl_alen); - l = SA_SIZE (&(su.sa)); - memcpy (bp, &su, l); + l = SA_SIZE(&(su.sa)); + memcpy(bp, &su, l); bp += l; - free (hwaddr); + free(hwaddr); } else { rtm.hdr.rtm_flags |= RTF_GATEWAY; - ADDADDR (gateway); + ADDADDR(gateway); } - ADDADDR (netmask); + ADDADDR(netmask); #undef ADDADDR rtm.hdr.rtm_msglen = l = bp - (char *)&rtm; - if (write (s, &rtm, l) == -1) { + if (write(s, &rtm, l) == -1) { /* Don't report error about routes already existing */ if (errno != EEXIST) - logger (LOG_ERR, "write: %s", strerror (errno)); - close (s); + logger(LOG_ERR, "write: %s", strerror(errno)); + close(s); return -1; } - close (s); + close(s); return 0; } @@ -696,7 +692,8 @@ static int do_route (const char *ifname, * send_netlink handles the actual transmission so we can work out * if there was an error or not. */ #define BUFFERLEN 256 -int send_netlink (struct nlmsghdr *hdr, netlink_callback callback, void *arg) +int +send_netlink (struct nlmsghdr *hdr, netlink_callback callback, void *arg) { int s; pid_t mypid = getpid (); @@ -711,27 +708,29 @@ int send_netlink (struct nlmsghdr *hdr, netlink_callback callback, void *arg) char *buffer; struct nlmsghdr *nlm; } h; + int len, l; + struct nlmsgerr *err; - if ((s = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); + if ((s = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) { + logger(LOG_ERR, "socket: %s", strerror(errno)); return -1; } - memset (&nl, 0, sizeof (nl)); + memset(&nl, 0, sizeof(nl)); nl.nl_family = AF_NETLINK; - if (bind (s, (struct sockaddr *) &nl, sizeof (nl)) == -1) { - logger (LOG_ERR, "bind: %s", strerror (errno)); - close (s); + if (bind(s, (struct sockaddr *)&nl, sizeof(nl)) == -1) { + logger(LOG_ERR, "bind: %s", strerror(errno)); + close(s); return -1; } - memset (&iov, 0, sizeof (iov)); + memset(&iov, 0, sizeof(iov)); iov.iov_base = hdr; iov.iov_len = hdr->nlmsg_len; - memset (&msg, 0, sizeof (msg)); + memset(&msg, 0, sizeof(msg)); msg.msg_name = &nl; - msg.msg_namelen = sizeof (nl); + msg.msg_namelen = sizeof(nl); msg.msg_iov = &iov; msg.msg_iovlen = 1; @@ -739,141 +738,142 @@ int send_netlink (struct nlmsghdr *hdr, netlink_callback callback, void *arg) hdr->nlmsg_flags |= NLM_F_ACK; hdr->nlmsg_seq = ++seq; - if (sendmsg (s, &msg, 0) == -1) { - logger (LOG_ERR, "write: %s", strerror (errno)); - close (s); + if (sendmsg(s, &msg, 0) == -1) { + logger(LOG_ERR, "write: %s", strerror(errno)); + close(s); return -1; } - buffer = xzalloc (sizeof (char) * BUFFERLEN); + buffer = xzalloc(sizeof(char) * BUFFERLEN); iov.iov_base = buffer; for (;;) { iov.iov_len = BUFFERLEN; - bytes = recvmsg (s, &msg, 0); + bytes = recvmsg(s, &msg, 0); if (bytes == -1) { if (errno != EINTR) logger (LOG_ERR, "recvmsg: %s", - strerror (errno)); + strerror(errno)); continue; } if (bytes == 0) { - logger (LOG_ERR, "netlink: EOF"); + logger(LOG_ERR, "netlink: EOF"); goto eexit; } - if (msg.msg_namelen != sizeof (nl)) { - logger (LOG_ERR, - "netlink: sender address length mismatch"); + if (msg.msg_namelen != sizeof(nl)) { + logger(LOG_ERR, + "netlink: sender address length mismatch"); goto eexit; } - for (h.buffer = buffer; bytes >= (signed) sizeof (*h.nlm); ) { - int len = h.nlm->nlmsg_len; - int l = len - sizeof (*h.nlm); - struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h.nlm); + for (h.buffer = buffer; bytes >= (signed) sizeof(*h.nlm); ) { + len = h.nlm->nlmsg_len; + l = len - sizeof(*h.nlm); + err = (struct nlmsgerr *)NLMSG_DATA(h.nlm); if (l < 0 || len > bytes) { if (msg.msg_flags & MSG_TRUNC) - logger (LOG_ERR, "netlink: truncated message"); + logger(LOG_ERR, + "netlink: truncated message"); else - logger (LOG_ERR, "netlink: malformed message"); + logger(LOG_ERR, + "netlink: malformed message"); goto eexit; } /* Ensure it's our message */ if (nl.nl_pid != 0 || - (pid_t) h.nlm->nlmsg_pid != mypid || + (pid_t)h.nlm->nlmsg_pid != mypid || h.nlm->nlmsg_seq != seq) { /* Next Message */ - bytes -= NLMSG_ALIGN (len); - h.buffer += NLMSG_ALIGN (len); + 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; } - if ((unsigned) l < sizeof (*err)) { - logger (LOG_ERR, "netlink: error truncated"); + if ((unsigned)l < sizeof(*err)) { + logger(LOG_ERR, "netlink: error truncated"); goto eexit; } if (err->error == 0) { - int retval = 0; - close (s); if (callback) { - if ((retval = callback (hdr, arg)) == -1) - logger (LOG_ERR, "netlink: callback failed"); + if ((l = callback (hdr, arg)) == -1) + logger(LOG_ERR, + "netlink: callback failed"); } - free (buffer); - return (retval); + free(buffer); + return l; } 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; } } eexit: - close (s); - free (buffer); + close(s); + free(buffer); return -1; } #define NLMSG_TAIL(nmsg) \ - ((struct rtattr *) (((ptrdiff_t) (nmsg)) + NLMSG_ALIGN ((nmsg)->nlmsg_len))) + ((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) +static int +add_attr_l(struct nlmsghdr *n, unsigned int maxlen, int type, + 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); + if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) { + logger(LOG_ERR, "add_attr_l: message exceeded bound of %d\n", + maxlen); return -1; } - rta = NLMSG_TAIL (n); + rta = NLMSG_TAIL(n); rta->rta_type = type; rta->rta_len = len; - memcpy (RTA_DATA (rta), data, alen); - n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + RTA_ALIGN (len); + memcpy(RTA_DATA(rta), data, alen); + n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len); return 0; } -static int add_attr_32(struct nlmsghdr *n, unsigned int maxlen, int type, - uint32_t data) +static int +add_attr_32(struct nlmsghdr *n, unsigned int maxlen, int type, uint32_t data) { - int len = RTA_LENGTH (sizeof (data)); + int len = RTA_LENGTH(sizeof(data)); struct rtattr *rta; - if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen) { - logger (LOG_ERR, "add_attr32: message exceeded bound of %d\n", - maxlen); + if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) { + logger(LOG_ERR, "add_attr32: message exceeded bound of %d\n", + maxlen); return -1; } - rta = NLMSG_TAIL (n); + rta = NLMSG_TAIL(n); rta->rta_type = type; rta->rta_len = len; - memcpy (RTA_DATA (rta), &data, sizeof (data)); - n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len; + memcpy(RTA_DATA(rta), &data, sizeof(data)); + n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len; return 0; } @@ -892,73 +892,64 @@ struct nlmr char buffer[256]; }; -static int do_address(const char *ifname, - struct in_addr address, struct in_addr netmask, - struct in_addr broadcast, int del) +static int +do_address(const char *ifname, + struct in_addr address, struct in_addr netmask, + struct in_addr broadcast, int del) { struct nlma *nlm; int retval; - if (!ifname) - return -1; - - nlm = xzalloc (sizeof (*nlm)); - nlm->hdr.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg)); + nlm = xzalloc(sizeof(*nlm)); + nlm->hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); nlm->hdr.nlmsg_flags = NLM_F_REQUEST; - if (! del) + if (!del) 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: no index for interface `%s'", - ifname); - free (nlm); + if (!(nlm->ifa.ifa_index = if_nametoindex(ifname))) { + logger(LOG_ERR, "if_nametoindex: no index for interface `%s'", + ifname); + free(nlm); return -1; } nlm->ifa.ifa_family = AF_INET; - - nlm->ifa.ifa_prefixlen = inet_ntocidr (netmask); - + nlm->ifa.ifa_prefixlen = inet_ntocidr(netmask); /* This creates the aliased interface */ - add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_LABEL, - ifname, strlen (ifname) + 1); - - add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_LOCAL, - &address.s_addr, sizeof (address.s_addr)); - if (! del) - add_attr_l (&nlm->hdr, sizeof (*nlm), IFA_BROADCAST, - &broadcast.s_addr, sizeof (broadcast.s_addr)); - - retval = send_netlink (&nlm->hdr, NULL, NULL); - free (nlm); + add_attr_l(&nlm->hdr, sizeof(*nlm), IFA_LABEL, + ifname, strlen(ifname) + 1); + add_attr_l(&nlm->hdr, sizeof(*nlm), IFA_LOCAL, + &address.s_addr, sizeof(address.s_addr)); + if (!del) + add_attr_l(&nlm->hdr, sizeof(*nlm), IFA_BROADCAST, + &broadcast.s_addr, sizeof(broadcast.s_addr)); + + retval = send_netlink(&nlm->hdr, NULL, NULL); + free(nlm); return retval; } -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) +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 nlmr *nlm; unsigned int ifindex; int retval; - if (! ifname) - return -1; - - log_route (destination, netmask, gateway, metric, change, del); + log_route(destination, netmask, gateway, metric, change, del); - if (! (ifindex = if_nametoindex (ifname))) { - logger (LOG_ERR, "if_nametoindex: no index for interface `%s'", - ifname); + if (!(ifindex = if_nametoindex(ifname))) { + logger(LOG_ERR, "if_nametoindex: no index for interface `%s'", + ifname); return -1; } - nlm = xzalloc (sizeof (*nlm)); - nlm->hdr.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg)); + nlm = xzalloc(sizeof(*nlm)); + nlm->hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); if (change) nlm->hdr.nlmsg_flags = NLM_F_REPLACE; - else if (! del) + else if (!del) nlm->hdr.nlmsg_flags = NLM_F_CREATE | NLM_F_EXCL; nlm->hdr.nlmsg_flags |= NLM_F_REQUEST; nlm->hdr.nlmsg_type = del ? RTM_DELROUTE : RTM_NEWROUTE; @@ -978,19 +969,19 @@ static int do_route (const char *ifname, nlm->rt.rtm_type = RTN_UNICAST; } - nlm->rt.rtm_dst_len = inet_ntocidr (netmask); - add_attr_l (&nlm->hdr, sizeof (*nlm), RTA_DST, - &destination.s_addr, sizeof (destination.s_addr)); + nlm->rt.rtm_dst_len = inet_ntocidr(netmask); + add_attr_l(&nlm->hdr, sizeof(*nlm), RTA_DST, + &destination.s_addr, sizeof(destination.s_addr)); if (netmask.s_addr != INADDR_BROADCAST && destination.s_addr != gateway.s_addr) - add_attr_l (&nlm->hdr, sizeof (*nlm), RTA_GATEWAY, - &gateway.s_addr, sizeof (gateway.s_addr)); + add_attr_l(&nlm->hdr, sizeof(*nlm), RTA_GATEWAY, + &gateway.s_addr, sizeof(gateway.s_addr)); - add_attr_32 (&nlm->hdr, sizeof (*nlm), RTA_OIF, ifindex); - add_attr_32 (&nlm->hdr, sizeof (*nlm), RTA_PRIORITY, metric); + add_attr_32(&nlm->hdr, sizeof(*nlm), RTA_OIF, ifindex); + add_attr_32(&nlm->hdr, sizeof(*nlm), RTA_PRIORITY, metric); - retval = send_netlink (&nlm->hdr, NULL, NULL); - free (nlm); + retval = send_netlink(&nlm->hdr, NULL, NULL); + free(nlm); return retval; } @@ -1001,60 +992,68 @@ static int do_route (const char *ifname, #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) +int +add_address(const char *ifname, struct in_addr address, + struct in_addr netmask, struct in_addr broadcast) { - logger (LOG_INFO, "adding IP address %s/%d", - inet_ntoa (address), inet_ntocidr (netmask)); + logger(LOG_INFO, "adding IP address %s/%d", + inet_ntoa(address), inet_ntocidr(netmask)); - return (do_address (ifname, address, netmask, broadcast, 0)); + return do_address(ifname, address, netmask, broadcast, 0); } -int del_address (const char *ifname, - struct in_addr address, struct in_addr netmask) +int +del_address(const char *ifname, 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)); + logger(LOG_INFO, "removing IP address %s/%d", + inet_ntoa(address), inet_ntocidr(netmask)); - memset (&t, 0, sizeof (t)); - return (do_address (ifname, address, netmask, t, 1)); + t.s_addr = 0; + 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) +int +add_route(const char *ifname, struct in_addr destination, + struct in_addr netmask, struct in_addr gateway, int metric) { - return (do_route (ifname, destination, netmask, gateway, metric, 0, 0)); + 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) +int +change_route(const char *ifname, struct in_addr destination, + struct in_addr netmask, struct in_addr gateway, int metric) { - return (do_route (ifname, destination, netmask, gateway, metric, 1, 0)); + 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) +int +del_route(const char *ifname, struct in_addr destination, + struct in_addr netmask, struct in_addr gateway, int metric) { - return (do_route (ifname, destination, netmask, gateway, metric, 0, 1)); + return do_route (ifname, destination, netmask, gateway, metric, 0, 1); } -int flush_addresses (const char *ifname) +int +flush_addresses(const char *ifname) { - return (_do_interface (ifname, NULL, NULL, NULL, true, false)); + return _do_interface(ifname, NULL, NULL, NULL, true, false); } -in_addr_t get_address (const char *ifname) +in_addr_t +get_address(const char *ifname) { struct in_addr address; - if (_do_interface (ifname, NULL, NULL, &address, false, true) > 0) - return (address.s_addr); - return (0); + + if (_do_interface(ifname, NULL, NULL, &address, false, true) > 0) + return address.s_addr; + return 0; } -int has_address (const char *ifname, struct in_addr address) +int +has_address(const char *ifname, struct in_addr address) { - return (_do_interface (ifname, NULL, NULL, &address, false, false)); + return _do_interface(ifname, NULL, NULL, &address, false, false); } diff --git a/interface.h b/interface.h index 962de41d..a9991273 100644 --- a/interface.h +++ b/interface.h @@ -31,9 +31,11 @@ #include #include #include + #include #include #include + #include #include @@ -90,23 +92,23 @@ #define NSTAILQ_FOREACH(var, head, field) \ if (head) STAILQ_FOREACH (var, head, field) -typedef struct route_t +struct route { struct in_addr destination; struct in_addr netmask; struct in_addr gateway; - STAILQ_ENTRY (route_t) entries; -} route_t; -STAILQ_HEAD (route_head, route_t); + STAILQ_ENTRY (route) entries; +}; +STAILQ_HEAD (route_head, route); -typedef struct address_t +struct address { struct in_addr address; - STAILQ_ENTRY (address_t) entries; -} address_t; -STAILQ_HEAD (address_head, address_t); + STAILQ_ENTRY (address) entries; +}; +STAILQ_HEAD (address_head, address); -typedef struct interface_t +struct interface { char name[IF_NAMESIZE]; sa_family_t family; @@ -134,39 +136,33 @@ typedef struct interface_t unsigned char *clientid; size_t clientid_len; -} interface_t; - -void free_address (struct address_head *addresses); -void free_route (struct route_head *routes); -uint32_t get_netmask (uint32_t addr); -char *hwaddr_ntoa (const unsigned char *hwaddr, size_t hwlen); -size_t hwaddr_aton (unsigned char *hwaddr, const char *addr); +}; -interface_t *read_interface (const char *ifname, int metric); -int get_mtu (const char *ifname); -int set_mtu (const char *ifname, short int mtu); +void free_address(struct address_head *); +void free_route(struct route_head *); +uint32_t get_netmask(uint32_t); +char *hwaddr_ntoa(const unsigned char *, size_t); +size_t hwaddr_aton(unsigned char *, const char *); -int add_address (const char *ifname, struct in_addr address, - struct in_addr netmask, struct in_addr broadcast); -int del_address (const char *ifname, struct in_addr address, - struct in_addr netmask); +struct interface *read_interface(const char *, int); +int get_mtu(const char *); +int set_mtu(const char *, short int); -int flush_addresses (const char *ifname); -in_addr_t get_address (const char *ifname); -int has_address (const char *ifname, struct in_addr address); +int add_address(const char *, struct in_addr, struct in_addr, struct in_addr); +int del_address(const char *, struct in_addr, struct in_addr); +int flush_addresses(const char *); +in_addr_t get_address(const char *); +int has_address(const char *, struct in_addr); -int add_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric); -int change_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric); -int del_route (const char *ifname, struct in_addr destination, - struct in_addr netmask, struct in_addr gateway, int metric); +int add_route(const char *, struct in_addr, struct in_addr, struct in_addr, int); +int change_route(const char *, struct in_addr, struct in_addr, struct in_addr, int); +int del_route(const char *, struct in_addr, struct in_addr, struct in_addr, int); -int inet_ntocidr (struct in_addr address); -int inet_cidrtoaddr (int cidr, struct in_addr *addr); +int inet_ntocidr(struct in_addr); +int inet_cidrtoaddr(int, struct in_addr *); #ifdef __linux__ -typedef int (*netlink_callback) (struct nlmsghdr *hdr, void *arg); -int send_netlink (struct nlmsghdr *hdr, netlink_callback callback, void *arg); +typedef int (*netlink_callback)(struct nlmsghdr *, void *); +int send_netlink(struct nlmsghdr *, netlink_callback, void *); #endif #endif diff --git a/ipv4ll.c b/ipv4ll.c index 9742b9a7..271f37b2 100644 --- a/ipv4ll.c +++ b/ipv4ll.c @@ -40,31 +40,32 @@ #define IPV4LL_LEASETIME 20 -int ipv4ll_get_address (interface_t *iface, dhcp_t *dhcp) { +int +ipv4ll_get_address(struct interface *iface, struct dhcp *dhcp) { struct in_addr addr; for (;;) { - addr.s_addr = htonl (LINKLOCAL_ADDR | - (((uint32_t) abs ((int) random ()) - % 0xFD00) + 0x0100)); + addr.s_addr = htonl(LINKLOCAL_ADDR | + (((uint32_t)abs((int)random()) + % 0xFD00) + 0x0100)); errno = 0; - if (! arp_claim (iface, addr)) + if (!arp_claim(iface, addr)) break; /* Our ARP may have been interrupted */ if (errno) - return (-1); + return -1; } dhcp->address.s_addr = addr.s_addr; - dhcp->netmask.s_addr = htonl (LINKLOCAL_MASK); - dhcp->broadcast.s_addr = htonl (LINKLOCAL_BRDC); + dhcp->netmask.s_addr = htonl(LINKLOCAL_MASK); + dhcp->broadcast.s_addr = htonl(LINKLOCAL_BRDC); /* Finally configure some DHCP like lease times */ dhcp->leasetime = IPV4LL_LEASETIME; - dhcp->renewaltime = (dhcp->leasetime * 0.5); - dhcp->rebindtime = (dhcp->leasetime * 0.875); + dhcp->renewaltime = dhcp->leasetime * 0.5; + dhcp->rebindtime = dhcp->leasetime * 0.875; - return (0); + return 0; } #endif diff --git a/ipv4ll.h b/ipv4ll.h index 4fa89439..a4dea074 100644 --- a/ipv4ll.h +++ b/ipv4ll.h @@ -33,7 +33,7 @@ #include "dhcp.h" #include "interface.h" -int ipv4ll_get_address (interface_t *iface, dhcp_t *dhcp); +int ipv4ll_get_address(struct interface *, struct dhcp *); #endif #endif diff --git a/logger.c b/logger.c index 99f845f4..6e976403 100644 --- a/logger.c +++ b/logger.c @@ -40,80 +40,82 @@ static int loglevel = LOG_WARNING; static char logprefix[12] = {0}; -int logtolevel (const char *priority) +int +logtolevel(const char *priority) { CODE *c; - if (isdigit ((int) *priority)) - return (atoi (priority)); - + if (isdigit((int)*priority)) + return atoi(priority); for (c = prioritynames; c->c_name; c++) - if (! strcasecmp (priority, c->c_name)) - return (c->c_val); - - return (-1); + if (!strcasecmp(priority, c->c_name)) + return c->c_val; + return -1; } -static const char *leveltolog (int level) { +static const char * +leveltolog(int level) { CODE *c; for (c = prioritynames; c->c_name; c++) if (c->c_val == level) - return (c->c_name); - - return (NULL); + return c->c_name; + return NULL; } -void setloglevel (int level) +void +setloglevel(int level) { loglevel = level; } -void setlogprefix (const char *prefix) +void +setlogprefix(const char *prefix) { - snprintf (logprefix, sizeof (logprefix), "%s", prefix); + snprintf(logprefix, sizeof(logprefix), "%s", prefix); } -void logger (int level, const char *fmt, ...) +void +logger(int level, const char *fmt, ...) { - va_list p; - va_list p2; + va_list p, p2; FILE *f = stderr; + size_t len, fmt2len; + char *fmt2, *pf; - va_start (p, fmt); - va_copy (p2, p); + va_start(p, fmt); + va_copy(p2, p); if (level <= LOG_ERR || level <= loglevel) { if (level == LOG_DEBUG || level == LOG_INFO) f = stdout; - fprintf (f, "%s, %s", leveltolog (level), logprefix); - vfprintf (f, fmt, p); - fputc ('\n', f); + fprintf(f, "%s, %s", leveltolog(level), logprefix); + vfprintf(f, fmt, p); + fputc('\n', f); /* stdout, stderr may be re-directed to some kind of buffer. * So we always flush to ensure it's written. */ - fflush (f); + fflush(f); } if (level < LOG_DEBUG || level <= loglevel) { - size_t len = strlen (logprefix); - size_t fmt2len = strlen (fmt) + len + 1; - char *fmt2 = malloc (sizeof (char) * fmt2len); - char *pf = fmt2; + len = strlen(logprefix); + fmt2len = strlen(fmt) + len + 1; + fmt2 = pf = malloc(sizeof(char) * fmt2len); if (fmt2) { - memcpy (pf, logprefix, len); + memcpy(pf, logprefix, len); pf += len; - strlcpy (pf, fmt, fmt2len - len); - vsyslog (level, fmt2, p2); - free (fmt2); + strlcpy(pf, fmt, fmt2len - len); + vsyslog(level, fmt2, p2); + free(fmt2); } else { - vsyslog (level, fmt, p2); - syslog (LOG_ERR, "logger: memory exhausted"); - exit (EXIT_FAILURE); + vsyslog(level, fmt, p2); + syslog(LOG_ERR, "logger: memory exhausted"); + exit(EXIT_FAILURE); } } - va_end (p2); - va_end (p); + va_end(p2); + va_end(p); } diff --git a/logger.h b/logger.h index 989f68d4..cde4864d 100644 --- a/logger.h +++ b/logger.h @@ -36,9 +36,9 @@ #include -int logtolevel (const char *priority); -void setloglevel (int level); -void setlogprefix (const char *prefix); -void logger (int level, const char *fmt, ...) _PRINTF_LIKE (2, 3); +int logtolevel(const char *); +void setloglevel(int); +void setlogprefix(const char *); +void logger(int, const char *, ...) _PRINTF_LIKE (2, 3); #endif diff --git a/signal.c b/signal.c index 9055c9fe..050e1dc2 100644 --- a/signal.c +++ b/signal.c @@ -27,6 +27,7 @@ #include #include + #include #include #include @@ -47,7 +48,8 @@ static const int handle_sigs[] = { SIGINT }; -static void signal_handler (int sig) +static void +signal_handler(int sig) { unsigned int i = 0; int serrno = errno; @@ -55,129 +57,134 @@ static void signal_handler (int sig) /* Add a signal to our stack */ while (signals[i]) i++; - if (i > sizeof (signals) / sizeof (signals[0])) - logger (LOG_ERR, "signal buffer overrun"); + if (i > sizeof(signals) / sizeof(signals[0])) + logger(LOG_ERR, "signal buffer overrun"); else signals[i] = sig; - if (write (signal_pipe[1], &sig, sizeof (sig)) == -1) - logger (LOG_ERR, "Could not send signal: %s", strerror (errno)); + if (write(signal_pipe[1], &sig, sizeof(sig)) == -1) + logger(LOG_ERR, "Could not send signal: %s", strerror(errno)); /* Restore errno */ errno = serrno; } -int signal_fd (void) +int +signal_fd(void) { return (signal_pipe[0]); } /* Check if we have a signal or not */ -int signal_exists (const struct pollfd *fd) +int +signal_exists(const struct pollfd *fd) { if (signals[0] || (fd && fd->revents & POLLIN)) - return (0); - return (-1); + return 0; + return -1; } /* Read a signal from the signal pipe. Returns 0 if there is * no signal, -1 on error (and sets errno appropriately), and * your signal on success */ -int signal_read (struct pollfd *fd) +int +signal_read(struct pollfd *fd) { int sig = -1; + unsigned int i = 0; + char buf[16]; + size_t bytes; /* Pop a signal off the our stack */ if (signals[0]) { - unsigned int i = 0; sig = signals[0]; - while (i < (sizeof (signals) / sizeof (signals[0])) - 1) { + while (i < (sizeof(signals) / sizeof(signals[0])) - 1) { signals[i] = signals[i + 1]; - if (! signals[++i]) + if (!signals[++i]) break; } } if (fd && fd->revents & POLLIN) { - char buf[16]; - size_t bytes; - - memset (buf, 0, sizeof (buf)); - bytes = read (signal_pipe[0], buf, sizeof (buf)); + memset(buf, 0, sizeof(buf)); + bytes = read(signal_pipe[0], buf, sizeof(buf)); - if (bytes >= sizeof (sig)) - memcpy (&sig, buf, sizeof (sig)); + if (bytes >= sizeof(sig)) + memcpy(&sig, buf, sizeof(sig)); /* We need to clear us from rset if nothing left in the buffer * in case we are called many times */ - if (bytes == sizeof (sig)) + if (bytes == sizeof(sig)) fd->revents = 0; } - return (sig); + return sig; } /* Call this before doing anything else. Sets up the socket pair * and installs the signal handler */ -int signal_init (void) +int +signal_init(void) { struct sigaction sa; - if (pipe (signal_pipe) == -1) { - logger (LOG_ERR, "pipe: %s", strerror (errno)); - return (-1); + if (pipe(signal_pipe) == -1) { + logger(LOG_ERR, "pipe: %s", strerror(errno)); + return -1; } /* Stop any scripts from inheriting us */ - close_on_exec (signal_pipe[0]); - close_on_exec (signal_pipe[1]); + close_on_exec(signal_pipe[0]); + close_on_exec(signal_pipe[1]); /* Ignore child signals and don't make zombies. * Because we do this, we don't need to be in signal_setup */ - memset (&sa, 0, sizeof (sa)); + memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_DFL; sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT; - if (sigaction (SIGCHLD, &sa, NULL) == -1) { - logger (LOG_ERR, "sigaction: %s", strerror (errno)); - return (-1); + if (sigaction(SIGCHLD, &sa, NULL) == -1) { + logger(LOG_ERR, "sigaction: %s", strerror(errno)); + return -1; } - memset (signals, 0, sizeof (signals)); - return (0); + memset(signals, 0, sizeof(signals)); + return 0; } -int signal_setup (void) +int +signal_setup(void) { unsigned int i; struct sigaction sa; - memset (&sa, 0, sizeof (sa)); + memset(&sa, 0, sizeof(sa)); sa.sa_handler = signal_handler; - sigemptyset (&sa.sa_mask); + sigemptyset(&sa.sa_mask); - for (i = 0; i < sizeof (handle_sigs) / sizeof (handle_sigs[0]); i++) - if (sigaction (handle_sigs[i], &sa, NULL) == -1) { - logger (LOG_ERR, "sigaction: %s", strerror (errno)); - return (-1); + for (i = 0; i < sizeof(handle_sigs) / sizeof(handle_sigs[0]); i++) + if (sigaction(handle_sigs[i], &sa, NULL) == -1) { + logger(LOG_ERR, "sigaction: %s", strerror(errno)); + return -1; } - return (0); + return 0; } -int signal_reset (void) +int +signal_reset(void) { struct sigaction sa; unsigned int i; - memset (&sa, 0, sizeof (sa)); + memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_DFL; - sigemptyset (&sa.sa_mask); + sigemptyset(&sa.sa_mask); - for (i = 0; i < sizeof (handle_sigs) / sizeof (handle_sigs[0]); i++) - if (sigaction (handle_sigs[i], &sa, NULL) == -1) { - logger (LOG_ERR, "sigaction: %s", strerror (errno)); - return (-1); + for (i = 0; i < sizeof(handle_sigs) / sizeof(handle_sigs[0]); i++) + if (sigaction(handle_sigs[i], &sa, NULL) == -1) { + logger(LOG_ERR, "sigaction: %s", strerror(errno)); + return -1; } - return (0); + return 0; } diff --git a/signal.h b/signal.h index 63a59060..948099d2 100644 --- a/signal.h +++ b/signal.h @@ -30,11 +30,11 @@ #include -int signal_init (void); -int signal_setup (void); -int signal_reset (void); -int signal_fd (void); -int signal_exists (const struct pollfd *fd); -int signal_read (struct pollfd *fd); +int signal_init(void); +int signal_setup(void); +int signal_reset(void); +int signal_fd(void); +int signal_exists(const struct pollfd *); +int signal_read(struct pollfd *); #endif diff --git a/socket.c b/socket.c index 034d41da..08db6095 100644 --- a/socket.c +++ b/socket.c @@ -29,6 +29,7 @@ #include #include #include + #include #include #include @@ -36,6 +37,7 @@ #include #undef __FAVOR_BSD #include + #include #include #include @@ -117,7 +119,8 @@ static struct bpf_insn arp_bpf_filter [] = { BPF_STMT (BPF_RET + BPF_K, 0), }; -void setup_packet_filters (void) +void +setup_packet_filters(void) { #ifdef __linux__ /* We need to massage the filters for Linux cooked packets */ @@ -132,7 +135,8 @@ void setup_packet_filters (void) #endif } -static uint16_t checksum (unsigned char *addr, uint16_t len) +static uint16_t +checksum(unsigned char *addr, uint16_t len) { uint32_t sum = 0; union @@ -141,6 +145,7 @@ static uint16_t checksum (unsigned char *addr, uint16_t len) uint16_t *i; } p; uint16_t nleft = len; + uint8_t a = 0; p.addr = addr; while (nleft > 1) { @@ -149,9 +154,8 @@ static uint16_t checksum (unsigned char *addr, uint16_t len) } if (nleft == 1) { - uint8_t a = 0; - memcpy (&a, p.i, 1); - sum += ntohs (a) << 8; + memcpy(&a, p.i, 1); + sum += ntohs(a) << 8; } sum = (sum >> 16) + (sum & 0xffff); @@ -160,9 +164,10 @@ static uint16_t checksum (unsigned char *addr, uint16_t len) return ~sum; } -void make_dhcp_packet(struct udp_dhcp_packet *packet, - const unsigned char *data, size_t length, - struct in_addr source, struct in_addr dest) +void +make_dhcp_packet(struct udp_dhcp_packet *packet, + const unsigned char *data, size_t length, + struct in_addr source, struct in_addr dest) { struct ip *ip = &packet->ip; struct udphdr *udp = &packet->udp; @@ -176,7 +181,7 @@ void make_dhcp_packet(struct udp_dhcp_packet *packet, * If we don't do the ordering like so then the udp checksum will be * broken, so find another way of doing it! */ - memcpy (&packet->dhcp, data, length); + memcpy(&packet->dhcp, data, length); ip->ip_p = IPPROTO_UDP; ip->ip_src.s_addr = source.s_addr; @@ -185,25 +190,26 @@ void make_dhcp_packet(struct udp_dhcp_packet *packet, else ip->ip_dst.s_addr = dest.s_addr; - udp->uh_sport = htons (DHCP_CLIENT_PORT); - udp->uh_dport = htons (DHCP_SERVER_PORT); - udp->uh_ulen = htons (sizeof (*udp) + length); + udp->uh_sport = htons(DHCP_CLIENT_PORT); + udp->uh_dport = htons(DHCP_SERVER_PORT); + udp->uh_ulen = htons(sizeof(*udp) + length); ip->ip_len = udp->uh_ulen; - udp->uh_sum = checksum ((unsigned char *) packet, sizeof (*packet)); + udp->uh_sum = checksum((unsigned char *)packet, sizeof(*packet)); ip->ip_v = IPVERSION; ip->ip_hl = 5; ip->ip_id = 0; ip->ip_tos = IPTOS_LOWDELAY; - ip->ip_len = htons (sizeof (*ip) + sizeof (*udp) + length); + ip->ip_len = htons (sizeof(*ip) + sizeof(*udp) + length); ip->ip_id = 0; - ip->ip_off = htons (IP_DF); /* Don't fragment */ + ip->ip_off = htons(IP_DF); /* Don't fragment */ ip->ip_ttl = IPDEFTTL; - ip->ip_sum = checksum ((unsigned char *) ip, sizeof (*ip)); + ip->ip_sum = checksum((unsigned char *)ip, sizeof(*ip)); } -static int valid_dhcp_packet (unsigned char *data) +static int +valid_dhcp_packet(unsigned char *data) { union { @@ -219,32 +225,32 @@ static int valid_dhcp_packet (unsigned char *data) int retval = 0; d.data = data; - bytes = ntohs (d.packet->ip.ip_len); + bytes = ntohs(d.packet->ip.ip_len); ipsum = d.packet->ip.ip_sum; iplen = d.packet->ip.ip_len; udpsum = d.packet->udp.uh_sum; d.data = data; d.packet->ip.ip_sum = 0; - if (ipsum != checksum ((unsigned char *) &d.packet->ip, - sizeof (d.packet->ip))) + if (ipsum != checksum((unsigned char *)&d.packet->ip, + sizeof(d.packet->ip))) { - logger (LOG_DEBUG, "bad IP header checksum, ignoring"); + logger(LOG_DEBUG, "bad IP header checksum, ignoring"); retval = -1; goto eexit; } - memcpy (&source, &d.packet->ip.ip_src, sizeof (d.packet->ip.ip_src)); - memcpy (&dest, &d.packet->ip.ip_dst, sizeof (d.packet->ip.ip_dst)); - memset (&d.packet->ip, 0, sizeof (d.packet->ip)); + memcpy(&source, &d.packet->ip.ip_src, sizeof(d.packet->ip.ip_src)); + memcpy(&dest, &d.packet->ip.ip_dst, sizeof(d.packet->ip.ip_dst)); + memset(&d.packet->ip, 0, sizeof(d.packet->ip)); d.packet->udp.uh_sum = 0; d.packet->ip.ip_p = IPPROTO_UDP; - memcpy (&d.packet->ip.ip_src, &source, sizeof (d.packet->ip.ip_src)); - memcpy (&d.packet->ip.ip_dst, &dest, sizeof (d.packet->ip.ip_dst)); + memcpy(&d.packet->ip.ip_src, &source, sizeof(d.packet->ip.ip_src)); + memcpy(&d.packet->ip.ip_dst, &dest, sizeof(d.packet->ip.ip_dst)); d.packet->ip.ip_len = d.packet->udp.uh_ulen; - if (udpsum && udpsum != checksum (d.data, bytes)) { - logger (LOG_ERR, "bad UDP checksum, ignoring"); + if (udpsum && udpsum != checksum(d.data, bytes)) { + logger(LOG_ERR, "bad UDP checksum, ignoring"); retval = -1; } @@ -257,7 +263,8 @@ eexit: } #if defined(BSD) || defined(__FreeBSD_kernel__) -int open_socket (interface_t *iface, int protocol) +int +open_socket(struct interface *iface, int protocol) { int n = 0; int fd = -1; @@ -267,168 +274,169 @@ int open_socket (interface_t *iface, int protocol) int buf = 0; struct bpf_program pf; - device = xmalloc (sizeof (char) * PATH_MAX); + device = xmalloc(sizeof(char) * PATH_MAX); do { - snprintf (device, PATH_MAX, "/dev/bpf%d", n++); - fd = open (device, O_RDWR); + snprintf(device, PATH_MAX, "/dev/bpf%d", n++); + fd = open(device, O_RDWR); } while (fd == -1 && errno == EBUSY); - free (device); + free(device); if (fd == -1) { - logger (LOG_ERR, "unable to open a BPF device"); + logger(LOG_ERR, "unable to open a BPF device"); return -1; } - close_on_exec (fd); + close_on_exec(fd); - memset (&ifr, 0, sizeof (ifr)); - 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)); - close (fd); + memset(&ifr, 0, sizeof(ifr)); + 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)); + close(fd); return -1; } /* Get the required BPF buffer length from the kernel. */ - if (ioctl (fd, BIOCGBLEN, &buf) == -1) { - logger (LOG_ERR, "ioctl BIOCGBLEN: %s", strerror (errno)); - close (fd); + if (ioctl(fd, BIOCGBLEN, &buf) == -1) { + logger (LOG_ERR, "ioctl BIOCGBLEN: %s", strerror(errno)); + close(fd); return -1; } iface->buffer_length = buf; flags = 1; - if (ioctl (fd, BIOCIMMEDIATE, &flags) == -1) { - logger (LOG_ERR, "ioctl BIOCIMMEDIATE: %s", strerror (errno)); - close (fd); + if (ioctl(fd, BIOCIMMEDIATE, &flags) == -1) { + logger(LOG_ERR, "ioctl BIOCIMMEDIATE: %s", strerror(errno)); + close(fd); return -1; } /* Install the DHCP filter */ if (protocol == ETHERTYPE_ARP) { pf.bf_insns = arp_bpf_filter; - pf.bf_len = sizeof (arp_bpf_filter) - / sizeof (arp_bpf_filter[0]); + pf.bf_len = sizeof(arp_bpf_filter) / sizeof(arp_bpf_filter[0]); } else { pf.bf_insns = dhcp_bpf_filter; - pf.bf_len = sizeof (dhcp_bpf_filter) - / sizeof (dhcp_bpf_filter[0]); + pf.bf_len = sizeof(dhcp_bpf_filter)/sizeof(dhcp_bpf_filter[0]); } - if (ioctl (fd, BIOCSETF, &pf) == -1) { - logger (LOG_ERR, "ioctl BIOCSETF: %s", strerror (errno)); - close (fd); + if (ioctl(fd, BIOCSETF, &pf) == -1) { + logger(LOG_ERR, "ioctl BIOCSETF: %s", strerror(errno)); + close(fd); return -1; } if (iface->fd > -1) - close (iface->fd); + close(iface->fd); iface->fd = fd; return fd; } -ssize_t send_packet (const interface_t *iface, int type, - const unsigned char *data, size_t len) +ssize_t +send_packet(const struct interface *iface, int type, + const unsigned char *data, size_t len) { ssize_t retval = -1; struct iovec iov[2]; + struct ether_header hw; if (iface->family == ARPHRD_ETHER) { - struct ether_header hw; - memset (&hw, 0, sizeof (hw)); - memset (&hw.ether_dhost, 0xff, ETHER_ADDR_LEN); - hw.ether_type = htons (type); + memset(&hw, 0, sizeof(hw)); + memset(&hw.ether_dhost, 0xff, ETHER_ADDR_LEN); + hw.ether_type = htons(type); iov[0].iov_base = &hw; - iov[0].iov_len = sizeof (hw); + iov[0].iov_len = sizeof(hw); } else { - logger (LOG_ERR, "unsupported interace type %d", iface->family); + logger(LOG_ERR, "unsupported interace type %d", iface->family); return -1; } - iov[1].iov_base = (unsigned char *) data; + iov[1].iov_base = (unsigned char *)data; iov[1].iov_len = len; if ((retval = writev(iface->fd, iov, 2)) == -1) - logger (LOG_ERR, "writev: %s", strerror (errno)); + logger(LOG_ERR, "writev: %s", strerror(errno)); return retval; } /* BPF requires that we read the entire buffer. * So we pass the buffer in the API so we can loop on >1 dhcp packet. */ -ssize_t get_packet (const interface_t *iface, unsigned char *data, - unsigned char *buffer, - size_t *buffer_len, size_t *buffer_pos) +ssize_t +get_packet(const struct interface *iface, unsigned char *data, + unsigned char *buffer, size_t *buffer_len, size_t *buffer_pos) { union { unsigned char *buffer; struct bpf_hdr *packet; } bpf; + union + { + unsigned char *buffer; + struct ether_header *hw; + } hdr; + union + { + unsigned char *buffer; + struct udp_dhcp_packet *packet; + } pay; + struct timespec ts; + size_t len + unsigned char *payload; + bool have_data; bpf.buffer = buffer; if (*buffer_pos < 1) { - memset (bpf.buffer, 0, iface->buffer_length); - *buffer_len = read (iface->fd, bpf.buffer, iface->buffer_length); + memset(bpf.buffer, 0, iface->buffer_length); + *buffer_len = read(iface->fd, bpf.buffer, iface->buffer_length); *buffer_pos = 0; if (*buffer_len < 1) { - struct timespec ts; - logger (LOG_ERR, "read: %s", strerror (errno)); + logger(LOG_ERR, "read: %s", strerror(errno)); ts.tv_sec = 3; ts.tv_nsec = 0; - nanosleep (&ts, NULL); - return (-1); + nanosleep(&ts, NULL); + return -1; } } else bpf.buffer += *buffer_pos; while (bpf.packet) { - size_t len = 0; - union - { - unsigned char *buffer; - struct ether_header *hw; - } hdr; - unsigned char *payload; - bool have_data = false; + len = 0; + have_data = false; /* Ensure that the entire packet is in our buffer */ - if (*buffer_pos + bpf.packet->bh_hdrlen + bpf.packet->bh_caplen - > (unsigned) *buffer_len) + if (*buffer_pos + + bpf.packet->bh_hdrlen + + bpf.packet->bh_caplen > (unsigned)*buffer_len) break; hdr.buffer = bpf.buffer + bpf.packet->bh_hdrlen; - payload = hdr.buffer + sizeof (*hdr.hw); + payload = hdr.buffer + sizeof(*hdr.hw); /* 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 (*hdr.hw); - memcpy (data, payload, len); + len = bpf.packet->bh_caplen - sizeof(*hdr.hw); + memcpy(data, payload, len); have_data = true; } else { - if (valid_dhcp_packet (payload) >= 0) { - union - { - unsigned char *buffer; - struct udp_dhcp_packet *packet; - } pay; + if (valid_dhcp_packet(payload) >= 0) { pay.buffer = payload; - len = ntohs (pay.packet->ip.ip_len) - - sizeof (pay.packet->ip) - - sizeof (pay.packet->udp); - memcpy (data, &pay.packet->dhcp, len); + len = ntohs(pay.packet->ip.ip_len) - + sizeof(pay.packet->ip) - + sizeof(pay.packet->udp); + memcpy(data, &pay.packet->dhcp, len); have_data = true; } } /* Update the buffer_pos pointer */ - bpf.buffer += BPF_WORDALIGN (bpf.packet->bh_hdrlen + - bpf.packet->bh_caplen); - if ((unsigned) (bpf.buffer - buffer) < *buffer_len) + bpf.buffer += BPF_WORDALIGN(bpf.packet->bh_hdrlen + + bpf.packet->bh_caplen); + if ((unsigned)(bpf.buffer - buffer) < *buffer_len) *buffer_pos = bpf.buffer - buffer; else *buffer_pos = 0; @@ -447,7 +455,8 @@ ssize_t get_packet (const interface_t *iface, unsigned char *data, #elif __linux__ -int open_socket (interface_t *iface, int protocol) +int +open_socket(struct interface *iface, int protocol) { int fd; union sockunion { @@ -465,88 +474,88 @@ int open_socket (interface_t *iface, int protocol) * We don't actually use this fd at all, instead using our packet * filter socket. */ if (iface->listen_fd == -1 && protocol == ETHERTYPE_IP) { - if ((fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); + if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { + logger(LOG_ERR, "socket: %s", strerror(errno)); } else { - memset (&su, 0, sizeof (su)); + memset(&su, 0, sizeof(su)); su.sin.sin_family = AF_INET; - su.sin.sin_port = htons (DHCP_CLIENT_PORT); - if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, - &n, sizeof (n)) == -1) - logger (LOG_ERR, "SO_REUSEADDR: %s", - strerror (errno)); - if (setsockopt (fd, SOL_SOCKET, SO_RCVBUF, - &n, sizeof (n)) == -1) - logger (LOG_ERR, "SO_RCVBUF: %s", - strerror (errno)); - memset (&ifr, 0, sizeof (ifr)); + su.sin.sin_port = htons(DHCP_CLIENT_PORT); + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, + &n, sizeof(n)) == -1) + logger(LOG_ERR, "SO_REUSEADDR: %s", + strerror(errno)); + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, + &n, sizeof(n)) == -1) + logger(LOG_ERR, "SO_RCVBUF: %s", + strerror(errno)); + memset (&ifr, 0, sizeof(ifr)); strncpy (ifr.ifr_name, iface->name, - sizeof (ifr.ifr_name)); - if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE, - &ifr, sizeof (ifr)) == -1) - logger (LOG_ERR, "SO_SOBINDTODEVICE: %s", - strerror (errno)); - if (bind (fd, &su.sa, sizeof (su)) == -1) { - logger (LOG_ERR, "bind: %s", strerror (errno)); - close (fd); + sizeof(ifr.ifr_name)); + if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, + &ifr, sizeof(ifr)) == -1) + logger(LOG_ERR, "SO_SOBINDTODEVICE: %s", + strerror(errno)); + if (bind(fd, &su.sa, sizeof(su)) == -1) { + logger(LOG_ERR, "bind: %s", strerror(errno)); + close(fd); } else { iface->listen_fd = fd; - close_on_exec (fd); + close_on_exec(fd); } } } - if ((fd = socket (PF_PACKET, SOCK_DGRAM, htons (protocol))) == -1) { - logger (LOG_ERR, "socket: %s", strerror (errno)); - return (-1); + if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(protocol))) == -1) { + logger(LOG_ERR, "socket: %s", strerror(errno)); + return -1; } - close_on_exec (fd); + close_on_exec(fd); - memset (&su, 0, sizeof (su)); + memset(&su, 0, sizeof(su)); su.sll.sll_family = PF_PACKET; - su.sll.sll_protocol = htons (protocol); - if (! (su.sll.sll_ifindex = if_nametoindex (iface->name))) { - logger (LOG_ERR, - "if_nametoindex: no index for interface `%s'", - iface->name); - close (fd); - return (-1); + su.sll.sll_protocol = htons(protocol); + if (!(su.sll.sll_ifindex = if_nametoindex(iface->name))) { + logger(LOG_ERR, + "if_nametoindex: no index for interface `%s'", + iface->name); + close(fd); + return -1; } /* Install the DHCP filter */ - memset (&pf, 0, sizeof (pf)); + memset(&pf, 0, sizeof(pf)); if (protocol == ETHERTYPE_ARP) { pf.filter = arp_bpf_filter; - pf.len = sizeof (arp_bpf_filter) / sizeof (arp_bpf_filter[0]); + pf.len = sizeof(arp_bpf_filter) / sizeof(arp_bpf_filter[0]); } else { pf.filter = dhcp_bpf_filter; - pf.len = sizeof (dhcp_bpf_filter) / sizeof (dhcp_bpf_filter[0]); + pf.len = sizeof(dhcp_bpf_filter) / sizeof(dhcp_bpf_filter[0]); } - if (setsockopt (fd, SOL_SOCKET, SO_ATTACH_FILTER, - &pf, sizeof (pf)) != 0) + if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &pf, sizeof(pf)) != 0) { - logger (LOG_ERR, "SO_ATTACH_FILTER: %s", strerror (errno)); - close (fd); - return (-1); + logger(LOG_ERR, "SO_ATTACH_FILTER: %s", strerror(errno)); + close(fd); + return -1; } - if (bind (fd, &su.sa, sizeof (su)) == -1) { - logger (LOG_ERR, "bind: %s", strerror (errno)); - close (fd); - return (-1); + if (bind(fd, &su.sa, sizeof(su)) == -1) { + logger(LOG_ERR, "bind: %s", strerror(errno)); + close(fd); + return -1; } if (iface->fd > -1) - close (iface->fd); + close(iface->fd); iface->fd = fd; iface->socket_protocol = protocol; iface->buffer_length = BUFFER_LENGTH; - return (fd); + return fd; } -ssize_t send_packet (const interface_t *iface, int type, - const unsigned char *data, size_t len) +ssize_t +send_packet(const struct interface *iface, int type, + const unsigned char *data, size_t len) { union sockunion { struct sockaddr sa; @@ -555,39 +564,34 @@ ssize_t send_packet (const interface_t *iface, int type, } su; ssize_t retval; - if (! iface) - return (-1); - - memset (&su, 0, sizeof (su)); + memset(&su, 0, sizeof(su)); su.sll.sll_family = AF_PACKET; - su.sll.sll_protocol = htons (type); + su.sll.sll_protocol = htons(type); - if (! (su.sll.sll_ifindex = if_nametoindex (iface->name))) { - logger (LOG_ERR, "if_nametoindex: no index for interface `%s'", - iface->name); - return (-1); + if (!(su.sll.sll_ifindex = if_nametoindex(iface->name))) { + logger(LOG_ERR, "if_nametoindex: no index for interface `%s'", + iface->name); + return -1; } - su.sll.sll_hatype = htons (iface->family); + su.sll.sll_hatype = htons(iface->family); su.sll.sll_halen = iface->hwlen; if (iface->family == ARPHRD_INFINIBAND) - memcpy (&su.sll.sll_addr, - &ipv4_bcast_addr, sizeof (ipv4_bcast_addr)); + memcpy(&su.sll.sll_addr, + &ipv4_bcast_addr, sizeof(ipv4_bcast_addr)); else - memset (&su.sll.sll_addr, 0xff, iface->hwlen); - - if ((retval = sendto (iface->fd, data, len, 0, &su.sa, - sizeof (su))) == -1) + memset(&su.sll.sll_addr, 0xff, iface->hwlen); - logger (LOG_ERR, "sendto: %s", strerror (errno)); - return (retval); + if ((retval = sendto(iface->fd, data, len,0,&su.sa,sizeof(su))) == -1) + logger(LOG_ERR, "sendto: %s", strerror(errno)); + return retval; } /* 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. */ -ssize_t get_packet (const interface_t *iface, unsigned char *data, - unsigned char *buffer, - size_t *buffer_len, size_t *buffer_pos) +ssize_t +get_packet(const struct interface *iface, unsigned char *data, + unsigned char *buffer, size_t *buffer_len, size_t *buffer_pos) { ssize_t bytes; union @@ -595,49 +599,48 @@ ssize_t get_packet (const interface_t *iface, unsigned char *data, unsigned char *buffer; struct udp_dhcp_packet *packet; } pay; + struct timespec ts; /* We don't use the given buffer, but we need to rewind the position */ *buffer_pos = 0; - memset (buffer, 0, iface->buffer_length); - bytes = read (iface->fd, buffer, iface->buffer_length); + memset(buffer, 0, iface->buffer_length); + bytes = read(iface->fd, buffer, iface->buffer_length); if (bytes == -1) { - struct timespec ts; - logger (LOG_ERR, "read: %s", strerror (errno)); + logger(LOG_ERR, "read: %s", strerror(errno)); ts.tv_sec = 3; ts.tv_nsec = 0; - nanosleep (&ts, NULL); - return (-1); + nanosleep(&ts, NULL); + return -1; } *buffer_len = bytes; /* If it's an ARP reply, then just send it back */ if (iface->socket_protocol == ETHERTYPE_ARP) { - memcpy (data, buffer, bytes); - return (bytes); + memcpy(data, buffer, bytes); + return bytes; } - if ((unsigned) bytes < (sizeof (pay.packet->ip) + - sizeof (pay.packet->udp))) + if ((unsigned)bytes < (sizeof(pay.packet->ip) +sizeof(pay.packet->udp))) { - logger (LOG_DEBUG, "message too short, ignoring"); - return (-1); + logger(LOG_DEBUG, "message too short, ignoring"); + return -1; } pay.buffer = buffer; - if (bytes < ntohs (pay.packet->ip.ip_len)) { - logger (LOG_DEBUG, "truncated packet, ignoring"); - return (-1); + if (bytes < ntohs(pay.packet->ip.ip_len)) { + logger(LOG_DEBUG, "truncated packet, ignoring"); + return -1; } - if (valid_dhcp_packet (buffer) == -1) - return (-1); + if (valid_dhcp_packet(buffer) == -1) + return -1; - bytes = ntohs (pay.packet->ip.ip_len) - - (sizeof (pay.packet->ip) + sizeof (pay.packet->udp)); - memcpy (data, &pay.packet->dhcp, bytes); - return (bytes); + bytes = ntohs(pay.packet->ip.ip_len) - + (sizeof(pay.packet->ip) + sizeof(pay.packet->udp)); + memcpy(data, &pay.packet->dhcp, bytes); + return bytes; } #else diff --git a/socket.h b/socket.h index bdf26d0a..5d371351 100644 --- a/socket.h +++ b/socket.h @@ -33,14 +33,13 @@ #include "dhcp.h" #include "interface.h" -void setup_packet_filters (void); -void make_dhcp_packet(struct udp_dhcp_packet *packet, - const unsigned char *data, size_t length, - struct in_addr source, struct in_addr dest); +void setup_packet_filters(void); +void make_dhcp_packet(struct udp_dhcp_packet *, const unsigned char *, + size_t, struct in_addr, struct in_addr); -int open_socket (interface_t *iface, int protocol); -ssize_t send_packet (const interface_t *iface, int type, - const unsigned char *data, size_t len); -ssize_t get_packet (const interface_t *iface, unsigned char *data, - unsigned char *buffer, size_t *buffer_len, size_t *buffer_pos); +int open_socket(struct interface *, int); +ssize_t send_packet(const struct interface *, int, + const unsigned char *, size_t); +ssize_t get_packet(const struct interface *, unsigned char *, + unsigned char *, size_t *, size_t *); #endif