Consolidate DHCP_MAGIC check for packet.cookie field.

Minor cosmetic cleanups for DHCP packet receipt handling.
This commit is contained in:
Nicholas J. Kain 2011-06-11 04:27:56 -04:00
parent 7c6b07ca75
commit 9cfcfefd4e
2 changed files with 24 additions and 27 deletions

View File

@ -1,5 +1,5 @@
/* dhcpmsg.c - dhcp packet generation and sending functions /* dhcpmsg.c - dhcp packet generation and sending functions
* Time-stamp: <2011-05-30 10:43:28 njk> * Time-stamp: <2011-06-11 04:21:26 njk>
* *
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com> * (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
* (c) 2001 Russ Dill <Russ.Dill@asu.edu> * (c) 2001 Russ Dill <Russ.Dill@asu.edu>
@ -286,10 +286,6 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
memcpy(payload, &packet.data, memcpy(payload, &packet.data,
len - sizeof packet.ip - sizeof packet.udp); len - sizeof packet.ip - sizeof packet.udp);
if (ntohl(payload->cookie) != DHCP_MAGIC) { log_line("Received a packet via raw socket.");
log_error("Packet with bad magic number, ignoring");
return -2;
}
log_line("Received valid DHCP message.");
return len - sizeof packet.ip - sizeof packet.udp; return len - sizeof packet.ip - sizeof packet.udp;
} }

View File

@ -1,5 +1,5 @@
/* packet.c - send and react to DHCP message packets /* packet.c - send and react to DHCP message packets
* Time-stamp: <2011-06-11 04:07:56 njk> * Time-stamp: <2011-06-11 04:26:22 njk>
* *
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com> * (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
* (c) 2001 Russ Dill <Russ.Dill@asu.edu> * (c) 2001 Russ Dill <Russ.Dill@asu.edu>
@ -54,11 +54,7 @@ int get_packet(struct dhcpMessage *packet, int fd)
return -1; return -1;
} }
if (ntohl(packet->cookie) != DHCP_MAGIC) { log_line("Received a packet via cooked socket.");
log_error("Packet with bad magic number, ignoring.");
return -2;
}
log_line("Received a packet");
return bytes; return bytes;
} }
@ -320,28 +316,34 @@ void handle_packet(struct client_state_t *cs)
else /* LM_NONE */ else /* LM_NONE */
return; return;
if (len == -1 && errno != EINTR) { if (len < 0) {
log_error("reopening socket."); // Transient issue handled by packet collection functions.
change_listen_mode(cs, cs->listenMode); /* just close and reopen */ if (len == -2 || (len == -1 && errno == EINTR))
return;
log_error("Error when reading from listening socket: %s. Reopening listening socket.",
strerror(errno));
change_listen_mode(cs, cs->listenMode);
} }
if (len < 0)
return;
if (len < DHCP_SIZE - 308) { if (len < DHCP_SIZE - 308) {
log_line("Packet is too short to contain magic cookie -- ignoring"); log_line("Packet is too short to contain magic cookie. Ignoring.");
return;
}
if (ntohl(packet.cookie) != DHCP_MAGIC) {
log_line("Packet with bad magic number. Ignoring.");
return; return;
} }
if (packet.xid != cs->xid) { if (packet.xid != cs->xid) {
log_line("Ignoring XID %lx (our xid is %lx).", log_line("Packet XID %lx does not equal our XID %lx. Ignoring.",
(uint32_t) packet.xid, cs->xid); packet.xid, cs->xid);
return; return;
} }
if ((message = get_option_data(&packet, DHCP_MESSAGE_TYPE, &optlen)) if (!(message = get_option_data(&packet, DHCP_MESSAGE_TYPE, &optlen))) {
== NULL) { log_line("Packet does not specify a DHCP message type. Ignoring.");
log_line("couldnt get option from packet -- ignoring");
return; return;
} }
@ -350,9 +352,8 @@ void handle_packet(struct client_state_t *cs)
init_selecting_packet(cs, &packet, message); init_selecting_packet(cs, &packet, message);
break; break;
case DS_ARP_CHECK: case DS_ARP_CHECK:
/* We ignore dhcp packets for now. This state will // We ignore dhcp packets for now. This state will
* be changed by the callback for arp ping. // be changed by the callback for arp ping.
*/
break; break;
case DS_RENEW_REQUESTED: case DS_RENEW_REQUESTED:
case DS_REQUESTING: case DS_REQUESTING: