Theoretical correctness fix:

Handle EAGAIN and EWOULDBLOCK more gracefully when dealing with safe_read().
All occurrences of safe_read() should only be invoked on fds that have signaled
ready-to-read state via the epoll() mechanism, so this change should not
result in any observable difference, but it is best to be safe.

Additionally, a constant stack variable is converted to an equivalent
macro define for cleanliness.

Finally, print the error type encountered if reading data from an ARP response
fails with a read error.
This commit is contained in:
Nicholas J. Kain 2011-05-30 10:54:05 -04:00
parent 03f0e8719e
commit d72b24a2fe
24 changed files with 26 additions and 13 deletions

10
ifchd/ifchd.c Executable file → Normal file
View File

@ -1,5 +1,5 @@
/* ifchd.c - interface change daemon
* Time-stamp: <2011-05-01 19:03:48 njk>
* Time-stamp: <2011-05-30 10:30:20 njk>
*
* (C) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
*
@ -664,10 +664,12 @@ static void process_client_fd(int fd)
memset(buf, '\0', sizeof buf);
r = safe_read(sks[sqidx], buf, sizeof buf / 2 - 1);
if (r <= 0) {
if (r != 0)
log_line("error reading from client fd: %s", strerror(errno));
if (r == 0)
goto fail;
else if (r < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return;
log_line("error reading from client fd: %s", strerror(errno));
}
/* Discard everything and close connection if we risk overflow.

0
ifchd/linux.c Executable file → Normal file
View File

0
ndhc/CMakeLists.txt Executable file → Normal file
View File

0
ndhc/README Executable file → Normal file
View File

7
ndhc/arp.c Executable file → Normal file
View File

@ -1,5 +1,5 @@
/* arp.c - arp ping checking
* Time-stamp: <2011-04-19 16:21:14 njk>
* Time-stamp: <2011-05-30 10:23:54 njk>
*
* Copyright 2010-2011 Nicholas J. Kain <njkain@gmail.com>
*
@ -233,7 +233,10 @@ void handle_arp_response(struct client_state_t *cs)
int r = safe_read(cs->arpFd, (char *)&arpreply + arpreply_offset,
sizeof arpreply - arpreply_offset);
if (r < 0) {
log_warning("handle_arp_response: short read");
if (errno == EWOULDBLOCK || errno == EAGAIN)
return;
log_warning("handle_arp_response: ARP response read failed: %s",
strerror(errno));
switch (cs->dhcpState) {
case DS_ARP_CHECK: arp_failed(cs); break;
case DS_ARP_GW_CHECK: arp_gw_failed(cs); break;

0
ndhc/arp.h Executable file → Normal file
View File

0
ndhc/config.h Executable file → Normal file
View File

13
ndhc/dhcpmsg.c Executable file → Normal file
View File

@ -1,5 +1,5 @@
/* dhcpmsg.c - dhcp packet generation and sending functions
* Time-stamp: <2011-03-30 23:57:43 nk>
* Time-stamp: <2011-05-30 10:43:28 njk>
*
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
* (c) 2001 Russ Dill <Russ.Dill@asu.edu>
@ -217,11 +217,12 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
{
struct ip_udp_dhcp_packet packet;
uint16_t check;
const int packet_size = sizeof(struct ip_udp_dhcp_packet);
memset(&packet, 0, packet_size);
int len = safe_read(fd, (char *)&packet, packet_size);
if (len == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
memset(&packet, 0, IP_UPD_DHCP_SIZE);
int len = safe_read(fd, (char *)&packet, IP_UPD_DHCP_SIZE);
if (len == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return -2;
log_line("get_raw_packet: read error %s", strerror(errno));
usleep(500000); /* possible down interface, looping condition */
return -1;
@ -252,7 +253,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
sleep(1);
return -2;
}
if (len > packet_size) {
if (len > IP_UPD_DHCP_SIZE) {
log_line("Data longer than that of a IP+UDP+DHCP message: %d", len);
sleep(1);
return -2;

0
ndhc/dhcpmsg.h Executable file → Normal file
View File

0
ndhc/ifchange.c Executable file → Normal file
View File

0
ndhc/ifchange.h Executable file → Normal file
View File

0
ndhc/ndhc.c Executable file → Normal file
View File

0
ndhc/netlink.c Executable file → Normal file
View File

0
ndhc/netlink.h Executable file → Normal file
View File

0
ndhc/options.c Executable file → Normal file
View File

0
ndhc/options.h Executable file → Normal file
View File

9
ndhc/packet.c Executable file → Normal file
View File

@ -1,5 +1,5 @@
/* packet.c - send and react to DHCP message packets
* Time-stamp: <2011-03-31 15:22:59 nk>
* Time-stamp: <2011-05-30 10:39:56 njk>
*
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
* (c) 2001 Russ Dill <Russ.Dill@asu.edu>
@ -48,6 +48,8 @@ int get_packet(struct dhcpMessage *packet, int fd)
memset(packet, 0, DHCP_SIZE);
bytes = safe_read(fd, (char *)packet, DHCP_SIZE);
if (bytes == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return -2;
log_line("Read on listen socket failed: %s", strerror(errno));
return -1;
}
@ -326,6 +328,11 @@ void handle_packet(struct client_state_t *cs)
if (len < 0)
return;
if (len < DHCP_SIZE) {
log_line("Received short DHCP packet -- ignoring");
return;
}
if (packet.xid != cs->xid) {
log_line("Ignoring XID %lx (our xid is %lx).",
(uint32_t) packet.xid, cs->xid);

0
ndhc/packet.h Executable file → Normal file
View File

0
ndhc/socket.c Executable file → Normal file
View File

0
ndhc/socket.h Executable file → Normal file
View File

0
ndhc/sys.c Executable file → Normal file
View File

0
ndhc/sys.h Executable file → Normal file
View File

0
ndhc/timeout.c Executable file → Normal file
View File

0
ndhc/timeout.h Executable file → Normal file
View File