From 9d7ad2f11c618ab397f5c6f5295db40a4aee023f Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Fri, 12 Nov 2010 17:24:54 -0500 Subject: [PATCH] Fix some strlcpy() misuse that cuts off the last character of the copied string. Clean up some log messages. --- ifchd/ifchd.c | 8 +++++--- ndhc/clientpacket.c | 9 ++++----- ndhc/dhcpc.c | 2 -- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/ifchd/ifchd.c b/ifchd/ifchd.c index 790e381..8e9b79d 100644 --- a/ifchd/ifchd.c +++ b/ifchd/ifchd.c @@ -1,5 +1,5 @@ /* ifchd.c - interface change daemon - * Time-stamp: <2010-11-12 14:27:47 njk> + * Time-stamp: <2010-11-12 17:22:34 njk> * * (C) 2004-2010 Nicholas J. Kain * @@ -357,7 +357,7 @@ static int stream_onto_list(int i) curl[i]->str = xmalloc(e - s + 1); - strlcpy(curl[i]->str, ibuf[i] + s, e - s); + strlcpy(curl[i]->str, ibuf[i] + s, e - s + 1); last[i] = curl[i]; s = e + 1; } @@ -378,6 +378,8 @@ static void execute_list(int i) p = curl[i]->str; + log_line("execute_list - p = '%s'", p); + switch (state[i]) { case STATE_NOTHING: if (strncmp(p, CMD_INTERFACE, sizeof(CMD_INTERFACE)) == 0) @@ -683,7 +685,7 @@ read_again: } /* Append new stream input avoiding overflow. */ - strlcpy(ibuf[i] + index, buf, sizeof(ibuf[i]) - index - 1); + strlcpy(ibuf[i] + index, buf, sizeof(ibuf[i]) - index); /* Decompose ibuf contents onto strlist. */ index = stream_onto_list(i); diff --git a/ndhc/clientpacket.c b/ndhc/clientpacket.c index 755b52d..d88ce5f 100644 --- a/ndhc/clientpacket.c +++ b/ndhc/clientpacket.c @@ -178,10 +178,10 @@ int get_raw_packet(struct dhcpMessage *payload, int fd) uint16_t check; ssize_t len = 0; - const ssize_t wanted = sizeof(struct iphdr) + sizeof(struct udphdr); + const ssize_t header_size = sizeof(struct iphdr) + sizeof(struct udphdr); memset(&packet, 0, sizeof(struct udp_dhcp_packet)); - while (len < wanted) { + while (len < header_size) { ssize_t r = read(fd, &packet + len, sizeof(struct udp_dhcp_packet) - len); if (r == 0) @@ -205,8 +205,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd) return -2; } - log_line("len: %d wanted: %d", len, wanted); - if (len < wanted) { + if (len < header_size) { log_line("Message too short to contain IP + UDP headers, ignoring"); sleep(1); return -2; @@ -284,6 +283,6 @@ int get_raw_packet(struct dhcpMessage *payload, int fd) log_error("received bogus message (bad magic) -- ignoring"); return -2; } - log_line("oooooh!!! got some!"); + log_line("Received valid DHCP message."); return len - (sizeof(packet.ip) + sizeof(packet.udp)); } diff --git a/ndhc/dhcpc.c b/ndhc/dhcpc.c index ab32628..0b4e949 100644 --- a/ndhc/dhcpc.c +++ b/ndhc/dhcpc.c @@ -295,8 +295,6 @@ static void handle_packet(void) struct in_addr temp_addr; struct dhcpMessage packet; - log_line("got a packet"); - if (listen_mode == LISTEN_KERNEL) len = get_packet(&packet, fd); else