}
#define DROP_CONFIG { \
- memset (&dhcp->address, 0, sizeof (struct in_addr)); \
if (! options->persistent) \
- configure (options, iface, dhcp); \
+ configure (options, iface, dhcp, false); \
free_dhcp (dhcp); \
memset (dhcp, 0, sizeof (dhcp_t)); \
}
} else {
logger (LOG_INFO, "using last known IP address %s",
inet_ntoa (dhcp->address));
- if (configure (options, iface, dhcp)) {
+ if (configure (options, iface, dhcp, true)) {
retval = EXIT_FAILURE;
goto eexit;
}
xid = 0;
- if (configure (options, iface, dhcp) < 0 && ! daemonised) {
+ if (configure (options, iface, dhcp, true) < 0 && ! daemonised) {
retval = EXIT_FAILURE;
goto eexit;
}
argv = alloca ((n + 1) * sizeof (*argv));
if (argv == NULL) {
errno = ENOMEM;
- return -1;
+ return (-1);
}
va_start (va, args);
} else if (pid == -1)
logger (LOG_ERR, "vfork: %s", strerror (errno));
- return 0;
+ return (0);
}
static void exec_script (const char *script, const char *infofile,
logger (LOG_ERR, "fopen `%s': %s", RESOLVFILE, strerror (errno));
}
- if (f) {
- fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname);
- if (dhcp->dnssearch)
- fprintf (f, "search %s\n", dhcp->dnssearch);
- else if (dhcp->dnsdomain) {
- fprintf (f, "search %s\n", dhcp->dnsdomain);
- }
+ if (! f)
+ return (-1);
- for (address = dhcp->dnsservers; address; address = address->next)
- fprintf (f, "nameserver %s\n", inet_ntoa (address->address));
+ fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname);
+ if (dhcp->dnssearch)
+ fprintf (f, "search %s\n", dhcp->dnssearch);
+ else if (dhcp->dnsdomain) {
+ fprintf (f, "search %s\n", dhcp->dnsdomain);
+ }
- if (*resolvconf)
- pclose (f);
- else
- fclose (f);
- } else
- return -1;
+ for (address = dhcp->dnsservers; address; address = address->next)
+ fprintf (f, "nameserver %s\n", inet_ntoa (address->address));
+
+ if (*resolvconf)
+ pclose (f);
+ else
+ fclose (f);
/* Refresh the local resolver */
res_init ();
- return 0;
+ return (0);
}
static void restore_resolv(const char *ifname)
#endif
int configure (const options_t *options, interface_t *iface,
- const dhcp_t *dhcp)
+ const dhcp_t *dhcp, bool up)
{
route_t *route = NULL;
route_t *new_route = NULL;
char curhostname[MAXHOSTNAMELEN] = {0};
if (! options || ! iface || ! dhcp)
- return -1;
+ 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
for (route = iface->previous_routes; route; route = route->next)
if (route->destination.s_addr || options->dogateway) {
int have = 0;
- if (dhcp->address.s_addr != 0)
+ if (up)
for (new_route = dhcp->routes; new_route; new_route = new_route->next)
if (new_route->destination.s_addr == route->destination.s_addr
&& new_route->netmask.s_addr == route->netmask.s_addr
}
}
- /* If we don't have an address, then return */
- if (dhcp->address.s_addr == 0) {
+ /* If we aren't up, then reset the interface as much as we can */
+ if (! up) {
if (iface->previous_routes) {
free_route (iface->previous_routes);
iface->previous_routes = NULL;
#ifdef ENABLE_INFO
/* If we haven't created an info file, do so now */
- if (! dhcp->frominfo && iface->previous_address.s_addr == 0)
- write_info (iface, dhcp, options);
+ if (! dhcp->frominfo)
+ write_info (iface, dhcp, options, false);
#endif
/* Only reset things if we had set them before */
exec_script (options->script, iface->infofile, "down");
- return 0;
+ return (0);
}
/* Set the MTU requested.
if (! options->doinform || ! has_address (iface->name, dhcp->address))
if (add_address (iface->name, dhcp->address, dhcp->netmask,
dhcp->broadcast) < 0 && errno != EEXIST)
- return -1;
+ return (false);
/* Now delete the old address if different */
if (iface->previous_address.s_addr != dhcp->address.s_addr &&
#ifdef ENABLE_INFO
if (! dhcp->frominfo)
- write_info (iface, dhcp, options);
+ write_info (iface, dhcp, options, true);
#endif
if (iface->previous_address.s_addr != dhcp->address.s_addr ||
} else
exec_script (options->script, iface->infofile, "up");
- return 0;
+ return (0);
}