diff options
Diffstat (limited to 'libnetutils/dhcpclient.c')
-rw-r--r-- | libnetutils/dhcpclient.c | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/libnetutils/dhcpclient.c b/libnetutils/dhcpclient.c index ff00432..6755ba1 100644 --- a/libnetutils/dhcpclient.c +++ b/libnetutils/dhcpclient.c @@ -36,8 +36,8 @@ #include <dirent.h> -#include <netutils/ifc.h> #include "dhcpmsg.h" +#include "ifc_utils.h" #include "packet.h" #define VERBOSE 2 @@ -85,12 +85,16 @@ int fatal(const char *reason) // exit(1); } -const char *ipaddr(in_addr_t addr) +const char *ipaddr(uint32_t addr) { - struct in_addr in_addr; - - in_addr.s_addr = addr; - return inet_ntoa(in_addr); + static char buf[32]; + + sprintf(buf,"%d.%d.%d.%d", + addr & 255, + ((addr >> 8) & 255), + ((addr >> 16) & 255), + (addr >> 24)); + return buf; } typedef struct dhcp_info dhcp_info; @@ -124,11 +128,31 @@ void get_dhcp_info(uint32_t *ipaddr, uint32_t *gateway, uint32_t *mask, *lease = last_good_info.lease; } -static int dhcp_configure(const char *ifname, dhcp_info *info) +static int ifc_configure(const char *ifname, dhcp_info *info) { + char dns_prop_name[PROPERTY_KEY_MAX]; + + if (ifc_set_addr(ifname, info->ipaddr)) { + printerr("failed to set ipaddr %s: %s\n", ipaddr(info->ipaddr), strerror(errno)); + return -1; + } + if (ifc_set_mask(ifname, info->netmask)) { + printerr("failed to set netmask %s: %s\n", ipaddr(info->netmask), strerror(errno)); + return -1; + } + if (ifc_create_default_route(ifname, info->gateway)) { + printerr("failed to set default route %s: %s\n", ipaddr(info->gateway), strerror(errno)); + return -1; + } + + snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname); + property_set(dns_prop_name, info->dns1 ? ipaddr(info->dns1) : ""); + snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname); + property_set(dns_prop_name, info->dns2 ? ipaddr(info->dns2) : ""); + last_good_info = *info; - return ifc_configure(ifname, info->ipaddr, info->netmask, info->gateway, - info->dns1, info->dns2); + + return 0; } static const char *dhcp_type_to_name(uint32_t type) @@ -425,7 +449,7 @@ int dhcp_init_ifc(const char *ifname) printerr("timed out\n"); if ( info.type == DHCPOFFER ) { printerr("no acknowledgement from DHCP server\nconfiguring %s with offered parameters\n", ifname); - return dhcp_configure(ifname, &info); + return ifc_configure(ifname, &info); } errno = ETIME; close(s); @@ -506,7 +530,7 @@ int dhcp_init_ifc(const char *ifname) if (info.type == DHCPACK) { printerr("configuring %s\n", ifname); close(s); - return dhcp_configure(ifname, &info); + return ifc_configure(ifname, &info); } else if (info.type == DHCPNAK) { printerr("configuration request denied\n"); close(s); |