From b89c694bc9ffb5dcf94f9bfda49cb95460aff0c5 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Mon, 11 Jul 2011 17:02:32 -0400 Subject: [PATCH] Convert all uses of inet_ntoa() to inet_ntop(). inet_ntop() is POSIX, supports ipv6 (doesn't matter to ndhc), and does not use a stupid internal static buffer. This fixes some cosmetic print bugs. --- ndhc/arp.c | 4 +++- ndhc/dhcp.c | 6 ++++-- ndhc/state.c | 30 ++++++++++++++++++++++-------- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/ndhc/arp.c b/ndhc/arp.c index 090b6df..db9ed3e 100644 --- a/ndhc/arp.c +++ b/ndhc/arp.c @@ -464,9 +464,11 @@ void arp_set_defense_mode(struct client_state_t *cs) void arp_success(struct client_state_t *cs) { + char clibuf[INET_ADDRSTRLEN]; struct in_addr temp_addr = {.s_addr = arp_dhcp_packet.yiaddr}; + inet_ntop(AF_INET, &temp_addr, clibuf, sizeof clibuf); log_line("Lease of %s obtained. Lease time is %ld seconds.", - inet_ntoa(temp_addr), cs->lease); + clibuf, cs->lease); cs->clientAddr = arp_dhcp_packet.yiaddr; cs->dhcpState = DS_BOUND; cs->init = 0; diff --git a/ndhc/dhcp.c b/ndhc/dhcp.c index aea262a..6a3b0ce 100644 --- a/ndhc/dhcp.c +++ b/ndhc/dhcp.c @@ -596,6 +596,7 @@ int send_discover(struct client_state_t *cs) int send_selecting(struct client_state_t *cs) { + char clibuf[INET_ADDRSTRLEN]; struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid); add_u32_option(&packet, DHCP_REQUESTED_IP, cs->clientAddr); add_u32_option(&packet, DHCP_SERVER_ID, cs->serverAddr); @@ -604,8 +605,9 @@ int send_selecting(struct client_state_t *cs) add_option_request_list(&packet); add_option_vendor(&packet); add_option_hostname(&packet); - log_line("Sending select for %s...", - inet_ntoa((struct in_addr){.s_addr = cs->clientAddr})); + inet_ntop(AF_INET, &(struct in_addr){.s_addr = cs->clientAddr}, + clibuf, sizeof clibuf); + log_line("Sending select for %s...", clibuf); return send_dhcp_raw(&packet); } diff --git a/ndhc/state.c b/ndhc/state.c index ab05a69..7a485d5 100644 --- a/ndhc/state.c +++ b/ndhc/state.c @@ -174,10 +174,13 @@ static int validate_serverid(struct client_state_t *cs, struct dhcpmsg *packet, return 0; } if (memcmp(&cs->serverAddr, temp, 4)) { + char svrbuf[INET_ADDRSTRLEN]; uint32_t iptmp; memcpy(&iptmp, temp, 4); + inet_ntop(AF_INET, &(struct in_addr){.s_addr=iptmp}, + svrbuf, sizeof svrbuf); log_line("Received %s with an unexpected server id: %s. Ignoring it.", - typemsg, inet_ntoa((struct in_addr){.s_addr=iptmp})); + typemsg, svrbuf); return 0; } return 1; @@ -214,8 +217,10 @@ static void an_packet(struct client_state_t *cs, struct dhcpmsg *packet, // have received a lease with a different IP than what we had before. if (cs->dhcpState == DS_REQUESTING || memcmp(&packet->yiaddr, &cs->clientAddr, 4)) { - log_line("Accepted a firm offer for %s. Validating...", - inet_ntoa((struct in_addr){.s_addr=cs->clientAddr})); + char clibuf[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &(struct in_addr){.s_addr=cs->clientAddr}, + clibuf, sizeof clibuf); + log_line("Accepted a firm offer for %s. Validating...", clibuf); if (arp_check(cs, packet) == -1) { log_warning("Failed to make arp socket. Searching for new lease..."); reinit_selecting(cs, 3000); @@ -242,15 +247,20 @@ static void selecting_packet(struct client_state_t *cs, struct dhcpmsg *packet, uint8_t *temp = NULL; ssize_t optlen; if ((temp = get_option_data(packet, DHCP_SERVER_ID, &optlen))) { + char clibuf[INET_ADDRSTRLEN]; + char svrbuf[INET_ADDRSTRLEN]; memcpy(&cs->serverAddr, temp, 4); cs->xid = packet->xid; cs->clientAddr = packet->yiaddr; cs->dhcpState = DS_REQUESTING; dhcp_wake_ts = curms(); num_dhcp_requests = 0; + inet_ntop(AF_INET, &(struct in_addr){.s_addr=cs->clientAddr}, + clibuf, sizeof clibuf); + inet_ntop(AF_INET, &(struct in_addr){.s_addr=cs->serverAddr}, + svrbuf, sizeof svrbuf); log_line("Received an offer of %s from server %s.", - inet_ntoa((struct in_addr){.s_addr=cs->clientAddr}), - inet_ntoa((struct in_addr){.s_addr=cs->serverAddr})); + clibuf, svrbuf); } else { log_line("Invalid offer received: it didn't have a server id."); } @@ -282,9 +292,13 @@ static void selecting_timeout(struct client_state_t *cs, long long nowts) static void xmit_release(struct client_state_t *cs) { - log_line("Unicasting a release of %s to %s.", - inet_ntoa((struct in_addr){.s_addr=cs->clientAddr}), - inet_ntoa((struct in_addr){.s_addr=cs->serverAddr})); + char clibuf[INET_ADDRSTRLEN]; + char svrbuf[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &(struct in_addr){.s_addr=cs->clientAddr}, + clibuf, sizeof clibuf); + inet_ntop(AF_INET, &(struct in_addr){.s_addr=cs->serverAddr}, + svrbuf, sizeof svrbuf); + log_line("Unicasting a release of %s to %s.", clibuf, svrbuf); send_release(cs); print_release(cs); }