Move change_listen_mode to packet.c.

This commit is contained in:
Nicholas J. Kain 2010-12-24 09:41:52 -05:00
parent 94ad810260
commit 59a0661eb9
5 changed files with 46 additions and 44 deletions

View File

@ -17,6 +17,7 @@
#include <errno.h> #include <errno.h>
#include "arpping.h" #include "arpping.h"
#include "clientpacket.h" #include "clientpacket.h"
#include "packet.h"
#include "sys.h" #include "sys.h"
#include "script.h" #include "script.h"
#include "dhcpd.h" #include "dhcpd.h"
@ -29,7 +30,6 @@ static int arpreply_offset;
static struct dhcpMessage arp_dhcp_packet; static struct dhcpMessage arp_dhcp_packet;
// from ndhc.c // from ndhc.c
void change_listen_mode(int new_mode);
void background(void); void background(void);
/* Returns fd of the arp socket, or -1 on failure. */ /* Returns fd of the arp socket, or -1 on failure. */
@ -107,7 +107,7 @@ static void arp_failed(struct client_state_t *cs)
cs->requestedIP = 0; cs->requestedIP = 0;
cs->timeout = 0; cs->timeout = 0;
cs->packetNum = 0; cs->packetNum = 0;
change_listen_mode(LM_RAW); change_listen_mode(cs, LM_RAW);
} }
// only called from timeout.c // only called from timeout.c
@ -135,7 +135,7 @@ void arp_success(struct client_state_t *cs)
? SCRIPT_RENEW : SCRIPT_BOUND)); ? SCRIPT_RENEW : SCRIPT_BOUND));
cs->dhcpState = DS_BOUND; cs->dhcpState = DS_BOUND;
change_listen_mode(LM_NONE); change_listen_mode(cs, LM_NONE);
if (client_config.quit_after_lease) if (client_config.quit_after_lease)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
if (!client_config.foreground) if (!client_config.foreground)

View File

@ -120,34 +120,6 @@ static void show_usage(void)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
/* Switch listen socket between raw (if-bound), kernel (ip-bound), and none */
void change_listen_mode(int new_mode)
{
log_line("entering %s listen mode",
new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
cs.listenMode = new_mode;
if (cs.listenFd >= 0) {
epoll_del(&cs, cs.listenFd);
close(cs.listenFd);
cs.listenFd = -1;
}
if (new_mode == LM_KERNEL) {
cs.listenFd = listen_socket(INADDR_ANY, CLIENT_PORT,
client_config.interface);
epoll_add(&cs, cs.listenFd);
}
else if (new_mode == LM_RAW) {
cs.listenFd = raw_socket(client_config.ifindex);
epoll_add(&cs, cs.listenFd);
}
else /* LM_NONE */
return;
if (cs.listenFd < 0) {
log_error("FATAL: couldn't listen on socket: %s.", strerror(errno));
exit(EXIT_FAILURE);
}
}
/* perform a renew */ /* perform a renew */
static void perform_renew(void) static void perform_renew(void)
{ {
@ -155,7 +127,7 @@ static void perform_renew(void)
retry: retry:
switch (cs.dhcpState) { switch (cs.dhcpState) {
case DS_BOUND: case DS_BOUND:
change_listen_mode(LM_KERNEL); change_listen_mode(&cs, LM_KERNEL);
case DS_ARP_CHECK: case DS_ARP_CHECK:
// Cancel arp ping in progress and treat as previous state. // Cancel arp ping in progress and treat as previous state.
epoll_del(&cs, cs.arpFd); epoll_del(&cs, cs.arpFd);
@ -170,7 +142,7 @@ static void perform_renew(void)
run_script(NULL, SCRIPT_DECONFIG); run_script(NULL, SCRIPT_DECONFIG);
case DS_REQUESTING: case DS_REQUESTING:
case DS_RELEASED: case DS_RELEASED:
change_listen_mode(LM_RAW); change_listen_mode(&cs, LM_RAW);
cs.dhcpState = DS_INIT_SELECTING; cs.dhcpState = DS_INIT_SELECTING;
break; break;
case DS_INIT_SELECTING: case DS_INIT_SELECTING:
@ -207,7 +179,7 @@ static void perform_release(void)
epoll_del(&cs, cs.arpFd); epoll_del(&cs, cs.arpFd);
cs.arpFd = -1; cs.arpFd = -1;
} }
change_listen_mode(LM_NONE); change_listen_mode(&cs, LM_NONE);
cs.dhcpState = DS_RELEASED; cs.dhcpState = DS_RELEASED;
cs.timeout = -1; cs.timeout = -1;
} }
@ -281,7 +253,7 @@ static void do_work(void)
if (cs.epollFd == -1) if (cs.epollFd == -1)
suicide("epoll_create1 failed"); suicide("epoll_create1 failed");
epoll_add(&cs, cs.signalFd); epoll_add(&cs, cs.signalFd);
change_listen_mode(LM_RAW); change_listen_mode(&cs, LM_RAW);
handle_timeout(&cs); handle_timeout(&cs);
for (;;) { for (;;) {

View File

@ -1,4 +1,4 @@
#include <stddef.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -11,7 +11,9 @@
#include "packet.h" #include "packet.h"
#include "clientpacket.h" #include "clientpacket.h"
#include "socket.h"
#include "script.h" #include "script.h"
#include "sys.h"
#include "log.h" #include "log.h"
#include "io.h" #include "io.h"
#include "dhcpd.h" #include "dhcpd.h"
@ -172,6 +174,34 @@ int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip,
return result; return result;
} }
/* Switch listen socket between raw (if-bound), kernel (ip-bound), and none */
void change_listen_mode(struct client_state_t *cs, int new_mode)
{
log_line("entering %s listen mode",
new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
cs->listenMode = new_mode;
if (cs->listenFd >= 0) {
epoll_del(cs, cs->listenFd);
close(cs->listenFd);
cs->listenFd = -1;
}
if (new_mode == LM_KERNEL) {
cs->listenFd = listen_socket(INADDR_ANY, CLIENT_PORT,
client_config.interface);
epoll_add(cs, cs->listenFd);
}
else if (new_mode == LM_RAW) {
cs->listenFd = raw_socket(client_config.ifindex);
epoll_add(cs, cs->listenFd);
}
else /* LM_NONE */
return;
if (cs->listenFd < 0) {
log_error("FATAL: couldn't listen on socket: %s.", strerror(errno));
exit(EXIT_FAILURE);
}
}
static void init_selecting_packet(struct client_state_t *cs, static void init_selecting_packet(struct client_state_t *cs,
struct dhcpMessage *packet, struct dhcpMessage *packet,
unsigned char *message) unsigned char *message)
@ -196,7 +226,6 @@ static void init_selecting_packet(struct client_state_t *cs,
} }
// from ndhc.c // from ndhc.c
void change_listen_mode(int new_mode);
void arp_check(struct dhcpMessage *packet); void arp_check(struct dhcpMessage *packet);
static void dhcp_ack_or_nak_packet(struct client_state_t *cs, static void dhcp_ack_or_nak_packet(struct client_state_t *cs,
@ -231,7 +260,7 @@ static void dhcp_ack_or_nak_packet(struct client_state_t *cs,
cs->timeout = 0; cs->timeout = 0;
cs->requestedIP = 0; cs->requestedIP = 0;
cs->packetNum = 0; cs->packetNum = 0;
change_listen_mode(LM_RAW); change_listen_mode(cs, LM_RAW);
// XXX: this isn't rfc compliant: should be exp backoff // XXX: this isn't rfc compliant: should be exp backoff
sleep(3); /* avoid excessive network traffic */ sleep(3); /* avoid excessive network traffic */
} }
@ -252,7 +281,7 @@ void handle_packet(struct client_state_t *cs)
if (len == -1 && errno != EINTR) { if (len == -1 && errno != EINTR) {
log_error("reopening socket."); log_error("reopening socket.");
change_listen_mode(cs->listenMode); /* just close and reopen */ change_listen_mode(cs, cs->listenMode); /* just close and reopen */
} }
if (len < 0) if (len < 0)

View File

@ -54,5 +54,6 @@ int raw_packet(struct dhcpMessage *payload, uint32_t source_ip,
unsigned char *dest_arp, int ifindex); unsigned char *dest_arp, int ifindex);
int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip, int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip,
int source_port, uint32_t dest_ip, int dest_port); int source_port, uint32_t dest_ip, int dest_port);
void change_listen_mode(struct client_state_t *cs, int new_mode);
void handle_packet(struct client_state_t *cs); void handle_packet(struct client_state_t *cs);
#endif #endif

View File

@ -4,12 +4,12 @@
#include "timeout.h" #include "timeout.h"
#include "config.h" #include "config.h"
#include "script.h" #include "script.h"
#include "packet.h"
#include "clientpacket.h" #include "clientpacket.h"
#include "arpping.h" #include "arpping.h"
#include "log.h" #include "log.h"
// from ndhc.c // from ndhc.c
void change_listen_mode(int new_mode);
void background(void); void background(void);
static void init_selecting_timeout(struct client_state_t *cs) static void init_selecting_timeout(struct client_state_t *cs)
@ -49,7 +49,7 @@ static void renew_requested_timeout(struct client_state_t *cs)
cs->dhcpState = DS_INIT_SELECTING; cs->dhcpState = DS_INIT_SELECTING;
cs->timeout = 0; cs->timeout = 0;
cs->packetNum = 0; cs->packetNum = 0;
change_listen_mode(LM_RAW); change_listen_mode(cs, LM_RAW);
} }
} }
@ -65,7 +65,7 @@ static void requesting_timeout(struct client_state_t *cs)
cs->dhcpState = DS_INIT_SELECTING; cs->dhcpState = DS_INIT_SELECTING;
cs->timeout = 0; cs->timeout = 0;
cs->packetNum = 0; cs->packetNum = 0;
change_listen_mode(LM_RAW); change_listen_mode(cs, LM_RAW);
} }
} }
@ -90,7 +90,7 @@ static void bound_timeout(struct client_state_t *cs)
{ {
/* Lease is starting to run out, time to enter renewing state */ /* Lease is starting to run out, time to enter renewing state */
cs->dhcpState = DS_RENEWING; cs->dhcpState = DS_RENEWING;
change_listen_mode(LM_KERNEL); change_listen_mode(cs, LM_KERNEL);
log_line("Entering renew state."); log_line("Entering renew state.");
renewing_timeout(cs); renewing_timeout(cs);
} }
@ -105,7 +105,7 @@ static void rebinding_timeout(struct client_state_t *cs)
run_script(NULL, SCRIPT_DECONFIG); run_script(NULL, SCRIPT_DECONFIG);
cs->timeout = 0; cs->timeout = 0;
cs->packetNum = 0; cs->packetNum = 0;
change_listen_mode(LM_RAW); change_listen_mode(cs, LM_RAW);
} else { } else {
/* send a request packet */ /* send a request packet */
send_renew(cs->xid, 0, cs->requestedIP); /* broadcast */ send_renew(cs->xid, 0, cs->requestedIP); /* broadcast */