Cosmetic cleanups to packet.c.
This commit is contained in:
parent
6f2986803f
commit
5c2398857d
@ -57,7 +57,6 @@ static int create_udp_socket(uint32_t ip, uint16_t port, char *iface)
|
|||||||
log_error("create_udp_socket: socket failed: %s", strerror(errno));
|
log_error("create_udp_socket: socket failed: %s", strerror(errno));
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
int opt = 1;
|
int opt = 1;
|
||||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1) {
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1) {
|
||||||
log_error("create_udp_socket: set reuse addr failed: %s",
|
log_error("create_udp_socket: set reuse addr failed: %s",
|
||||||
@ -103,11 +102,9 @@ static int create_udp_socket(uint32_t ip, uint16_t port, char *iface)
|
|||||||
static int create_udp_listen_socket(unsigned int ip, int port, char *inf)
|
static int create_udp_listen_socket(unsigned int ip, int port, char *inf)
|
||||||
{
|
{
|
||||||
log_line("Opening listen socket on 0x%08x:%d %s", ip, port, inf);
|
log_line("Opening listen socket on 0x%08x:%d %s", ip, port, inf);
|
||||||
|
|
||||||
int fd = create_udp_socket(ip, port, inf);
|
int fd = create_udp_socket(ip, port, inf);
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
int opt = 1;
|
int opt = 1;
|
||||||
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof opt) == -1) {
|
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof opt) == -1) {
|
||||||
log_error("create_udp_listen_socket: set broadcast failed: %s",
|
log_error("create_udp_listen_socket: set broadcast failed: %s",
|
||||||
@ -115,7 +112,6 @@ static int create_udp_listen_socket(unsigned int ip, int port, char *inf)
|
|||||||
close(fd);
|
close(fd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -365,16 +361,12 @@ static int create_raw_listen_socket(int ifindex)
|
|||||||
};
|
};
|
||||||
|
|
||||||
log_line("Opening raw socket on ifindex %d", ifindex);
|
log_line("Opening raw socket on ifindex %d", ifindex);
|
||||||
|
|
||||||
struct sockaddr_ll sa = {
|
struct sockaddr_ll sa = {
|
||||||
.sll_family = AF_PACKET,
|
.sll_family = AF_PACKET,
|
||||||
.sll_protocol = htons(ETH_P_IP),
|
.sll_protocol = htons(ETH_P_IP),
|
||||||
.sll_ifindex = ifindex,
|
.sll_ifindex = ifindex,
|
||||||
};
|
};
|
||||||
int fd = create_raw_socket(&sa, &filter_prog);
|
return create_raw_socket(&sa, &filter_prog);
|
||||||
if (fd == -1)
|
|
||||||
return -1;
|
|
||||||
return fd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Broadcast a DHCP message using a raw socket.
|
// Broadcast a DHCP message using a raw socket.
|
||||||
@ -449,19 +441,13 @@ void change_listen_mode(struct client_state_t *cs, int new_mode)
|
|||||||
close(cs->listenFd);
|
close(cs->listenFd);
|
||||||
cs->listenFd = -1;
|
cs->listenFd = -1;
|
||||||
}
|
}
|
||||||
switch (new_mode) {
|
if (new_mode != (LM_RAW || LM_KERNEL)) {
|
||||||
case LM_NONE:
|
log_line("Stopped listening for DHCP packets.");
|
||||||
log_line("Stopped listening for DHCP packets.");
|
return;
|
||||||
return;
|
|
||||||
case LM_RAW:
|
|
||||||
cs->listenFd = create_raw_listen_socket(client_config.ifindex);
|
|
||||||
break;
|
|
||||||
case LM_KERNEL:
|
|
||||||
cs->listenFd =
|
|
||||||
create_udp_listen_socket(INADDR_ANY, DHCP_CLIENT_PORT,
|
|
||||||
client_config.interface);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
cs->listenFd = LM_RAW ? create_raw_listen_socket(client_config.ifindex) :
|
||||||
|
create_udp_listen_socket(INADDR_ANY, DHCP_CLIENT_PORT,
|
||||||
|
client_config.interface);
|
||||||
if (cs->listenFd < 0) {
|
if (cs->listenFd < 0) {
|
||||||
log_error("FATAL: couldn't listen on socket: %s.", strerror(errno));
|
log_error("FATAL: couldn't listen on socket: %s.", strerror(errno));
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@ -489,7 +475,6 @@ void handle_packet(struct client_state_t *cs)
|
|||||||
// Transient issue handled by packet collection functions.
|
// Transient issue handled by packet collection functions.
|
||||||
if (len == -2 || (len == -1 && errno == EINTR))
|
if (len == -2 || (len == -1 && errno == EINTR))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
log_error("Error when reading from listening socket: %s. Reopening listening socket.",
|
log_error("Error when reading from listening socket: %s. Reopening listening socket.",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
change_listen_mode(cs, cs->listenMode);
|
change_listen_mode(cs, cs->listenMode);
|
||||||
@ -499,18 +484,15 @@ void handle_packet(struct client_state_t *cs)
|
|||||||
log_line("Packet is too short to contain magic cookie. Ignoring.");
|
log_line("Packet is too short to contain magic cookie. Ignoring.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ntohl(packet.cookie) != DHCP_MAGIC) {
|
if (ntohl(packet.cookie) != DHCP_MAGIC) {
|
||||||
log_line("Packet with bad magic number. Ignoring.");
|
log_line("Packet with bad magic number. Ignoring.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (packet.xid != cs->xid) {
|
if (packet.xid != cs->xid) {
|
||||||
log_line("Packet XID %lx does not equal our XID %lx. Ignoring.",
|
log_line("Packet XID %lx does not equal our XID %lx. Ignoring.",
|
||||||
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))) {
|
||||||
log_line("Packet does not specify a DHCP message type. Ignoring.");
|
log_line("Packet does not specify a DHCP message type. Ignoring.");
|
||||||
return;
|
return;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user