aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--networking/udhcpc.c702
1 files changed, 372 insertions, 330 deletions
diff --git a/networking/udhcpc.c b/networking/udhcpc.c
index 5f9b5c488..01e6f5ef9 100644
--- a/networking/udhcpc.c
+++ b/networking/udhcpc.c
@@ -20,7 +20,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
-
+
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
@@ -41,7 +41,7 @@
#include <netinet/udp.h>
#include <sys/types.h>
#include <sys/wait.h>
-
+
#if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h>
@@ -53,11 +53,12 @@
#include "libbb.h"
static int state;
-static unsigned long requested_ip; /* = 0 */
+static unsigned long requested_ip; /* = 0 */
static unsigned long server_addr;
static unsigned long timeout;
-static int packet_num; /* = 0 */
+static int packet_num; /* = 0 */
static int fd_main;
+
/* #define DEBUG */
#define VERSION "0.9.7"
@@ -148,7 +149,7 @@ static int listen_mode;
#define OPT_DATA 2
enum {
- OPTION_IP=1,
+ OPTION_IP = 1,
OPTION_IP_PAIR,
OPTION_STRING,
OPTION_BOOLEAN,
@@ -159,8 +160,8 @@ enum {
OPTION_S32
};
-#define OPTION_REQ 0x10 /* have the client request this option */
-#define OPTION_LIST 0x20 /* There can be a list of 1 or more of these */
+#define OPTION_REQ 0x10 /* have the client request this option */
+#define OPTION_LIST 0x20 /* There can be a list of 1 or more of these */
#ifdef SYSLOG
# define LOG(level, str, args...) do { printf(str, ## args); \
@@ -189,20 +190,6 @@ enum {
# define DEBUG(level, str, args...) do {;} while(0)
#endif
-struct client_config_t {
- char foreground; /* Do not fork */
- char quit_after_lease; /* Quit after obtaining lease */
- char abort_if_no_lease; /* Abort if no lease */
- char *interface; /* The name of the interface to use */
- char *pidfile; /* Optionally store the process ID */
- char *script; /* User script to run at dhcp events */
- unsigned char *clientid; /* Optional client id to use */
- unsigned char *hostname; /* Optional hostname to use */
- int ifindex; /* Index number of the interface to use */
- unsigned char arp[6]; /* Our arp address */
-};
-extern struct client_config_t client_config;
-
struct dhcpMessage {
u_int8_t op;
u_int8_t htype;
@@ -219,21 +206,34 @@ struct dhcpMessage {
u_int8_t sname[64];
u_int8_t file[128];
u_int32_t cookie;
- u_int8_t options[308]; /* 312 - cookie */
+ u_int8_t options[308]; /* 312 - cookie */
+};
+
+struct client_config_t {
+ char foreground; /* Do not fork */
+ char quit_after_lease; /* Quit after obtaining lease */
+ char abort_if_no_lease; /* Abort if no lease */
+ char *interface; /* The name of the interface to use */
+ char *pidfile; /* Optionally store the process ID */
+ char *script; /* User script to run at dhcp events */
+ unsigned char *clientid; /* Optional client id to use */
+ unsigned char *hostname; /* Optional hostname to use */
+ int ifindex; /* Index number of the interface to use */
+ unsigned char arp[6]; /* Our arp address */
};
struct client_config_t client_config = {
/* Default options. */
- abort_if_no_lease: 0,
- foreground: 0,
- quit_after_lease: 0,
- interface: "eth0",
- pidfile: NULL,
- script: DEFAULT_SCRIPT,
- clientid: NULL,
- hostname: NULL,
- ifindex: 0,
- arp: "\0\0\0\0\0\0", /* appease gcc-3.0 */
+ abort_if_no_lease:0,
+ foreground:0,
+ quit_after_lease:0,
+ interface:"eth0",
+ pidfile:NULL,
+ script:DEFAULT_SCRIPT,
+ clientid:NULL,
+ hostname:NULL,
+ ifindex:0,
+ arp:"\0\0\0\0\0\0", /* appease gcc-3.0 */
};
struct dhcp_option {
@@ -249,70 +249,72 @@ struct udp_dhcp_packet {
};
static const struct dhcp_option options[] = {
- /* name[10] flags code */
- {"subnet", OPTION_IP | OPTION_REQ, 0x01},
- {"timezone", OPTION_S32, 0x02},
- {"router", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03},
- {"timesvr", OPTION_IP | OPTION_LIST, 0x04},
- {"namesvr", OPTION_IP | OPTION_LIST, 0x05},
- {"dns", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06},
- {"logsvr", OPTION_IP | OPTION_LIST, 0x07},
- {"cookiesvr", OPTION_IP | OPTION_LIST, 0x08},
- {"lprsvr", OPTION_IP | OPTION_LIST, 0x09},
- {"hostname", OPTION_STRING | OPTION_REQ, 0x0c},
- {"bootsize", OPTION_U16, 0x0d},
- {"domain", OPTION_STRING | OPTION_REQ, 0x0f},
- {"swapsvr", OPTION_IP, 0x10},
- {"rootpath", OPTION_STRING, 0x11},
- {"ipttl", OPTION_U8, 0x17},
- {"mtu", OPTION_U16, 0x1a},
- {"broadcast", OPTION_IP | OPTION_REQ, 0x1c},
- {"ntpsrv", OPTION_IP | OPTION_LIST, 0x2a},
- {"wins", OPTION_IP | OPTION_LIST, 0x2c},
- {"requestip", OPTION_IP, 0x32},
- {"lease", OPTION_U32, 0x33},
- {"dhcptype", OPTION_U8, 0x35},
- {"serverid", OPTION_IP, 0x36},
- {"message", OPTION_STRING, 0x38},
- {"tftp", OPTION_STRING, 0x42},
- {"bootfile", OPTION_STRING, 0x43},
- {"", 0x00, 0x00}
+ /* name[10] flags code */
+ {"subnet", OPTION_IP | OPTION_REQ, 0x01},
+ {"timezone", OPTION_S32, 0x02},
+ {"router", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03},
+ {"timesvr", OPTION_IP | OPTION_LIST, 0x04},
+ {"namesvr", OPTION_IP | OPTION_LIST, 0x05},
+ {"dns", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06},
+ {"logsvr", OPTION_IP | OPTION_LIST, 0x07},
+ {"cookiesvr", OPTION_IP | OPTION_LIST, 0x08},
+ {"lprsvr", OPTION_IP | OPTION_LIST, 0x09},
+ {"hostname", OPTION_STRING | OPTION_REQ, 0x0c},
+ {"bootsize", OPTION_U16, 0x0d},
+ {"domain", OPTION_STRING | OPTION_REQ, 0x0f},
+ {"swapsvr", OPTION_IP, 0x10},
+ {"rootpath", OPTION_STRING, 0x11},
+ {"ipttl", OPTION_U8, 0x17},
+ {"mtu", OPTION_U16, 0x1a},
+ {"broadcast", OPTION_IP | OPTION_REQ, 0x1c},
+ {"ntpsrv", OPTION_IP | OPTION_LIST, 0x2a},
+ {"wins", OPTION_IP | OPTION_LIST, 0x2c},
+ {"requestip", OPTION_IP, 0x32},
+ {"lease", OPTION_U32, 0x33},
+ {"dhcptype", OPTION_U8, 0x35},
+ {"serverid", OPTION_IP, 0x36},
+ {"message", OPTION_STRING, 0x38},
+ {"tftp", OPTION_STRING, 0x42},
+ {"bootfile", OPTION_STRING, 0x43},
+ {"", 0x00, 0x00}
};
/* Lengths of the different option types */
static const unsigned char option_lengths[] = {
- [OPTION_IP] = 4,
- [OPTION_IP_PAIR] = 8,
- [OPTION_BOOLEAN] = 1,
- [OPTION_STRING] = 1,
- [OPTION_U8] = 1,
- [OPTION_U16] = 2,
- [OPTION_S16] = 2,
- [OPTION_U32] = 4,
- [OPTION_S32] = 4
+ [OPTION_IP] = 4,
+ [OPTION_IP_PAIR] = 8,
+ [OPTION_BOOLEAN] = 1,
+ [OPTION_STRING] = 1,
+ [OPTION_U8] = 1,
+ [OPTION_U16] = 2,
+ [OPTION_S16] = 2,
+ [OPTION_U32] = 4,
+ [OPTION_S32] = 4
};
/* get a rough idea of how long an option will be (rounding up...) */
static const unsigned char max_option_length[] = {
- [OPTION_IP] = sizeof("255.255.255.255 "),
- [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2,
- [OPTION_STRING] = 1,
- [OPTION_BOOLEAN] = sizeof("yes "),
- [OPTION_U8] = sizeof("255 "),
- [OPTION_U16] = sizeof("65535 "),
- [OPTION_S16] = sizeof("-32768 "),
- [OPTION_U32] = sizeof("4294967295 "),
- [OPTION_S32] = sizeof("-2147483684 "),
+ [OPTION_IP] = sizeof("255.255.255.255 "),
+ [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2,
+ [OPTION_STRING] = 1,
+ [OPTION_BOOLEAN] = sizeof("yes "),
+ [OPTION_U8] = sizeof("255 "),
+ [OPTION_U16] = sizeof("65535 "),
+ [OPTION_S16] = sizeof("-32768 "),
+ [OPTION_U32] = sizeof("4294967295 "),
+ [OPTION_S32] = sizeof("-2147483684 "),
};
/* return the position of the 'end' option (no bounds checking) */
-static int end_option(unsigned char *optionptr)
+static int end_option(unsigned char *optionptr)
{
int i = 0;
-
+
while (optionptr[i] != DHCP_END) {
- if (optionptr[i] == DHCP_PADDING) i++;
- else i += optionptr[i + OPT_LEN] + 2;
+ if (optionptr[i] == DHCP_PADDING)
+ i++;
+ else
+ i += optionptr[i + OPT_LEN] + 2;
}
return i;
}
@@ -322,10 +324,11 @@ static int end_option(unsigned char *optionptr)
static int add_option_string(unsigned char *optionptr, unsigned char *string)
{
int end = end_option(optionptr);
-
+
/* end position + string length + option code/length + end option */
if (end + string[OPT_LEN] + 2 + 1 >= 308) {
- LOG(LOG_ERR, "Option 0x%02x did not fit into the packet!", string[OPT_CODE]);
+ LOG(LOG_ERR, "Option 0x%02x did not fit into the packet!",
+ string[OPT_CODE]);
return 0;
}
DEBUG(LOG_INFO, "adding option 0x%02x", string[OPT_CODE]);
@@ -335,7 +338,8 @@ static int add_option_string(unsigned char *optionptr, unsigned char *string)
}
/* add a one to four byte option to a packet */
-static int add_simple_option(unsigned char *optionptr, unsigned char code, u_int32_t data)
+static int add_simple_option(unsigned char *optionptr, unsigned char code,
+ u_int32_t data)
{
char length = 0;
int i;
@@ -344,55 +348,40 @@ static int add_simple_option(unsigned char *optionptr, unsigned char code, u_int
u_int16_t *u16;
u_int32_t *u32;
u_int32_t aligned;
+
u8 = (unsigned char *) &aligned;
- u16 = (u_int16_t *) &aligned;
+ u16 = (u_int16_t *) & aligned;
u32 = &aligned;
for (i = 0; options[i].code; i++)
if (options[i].code == code) {
length = option_lengths[options[i].flags & TYPE_MASK];
}
-
+
if (!length) {
DEBUG(LOG_ERR, "Could not add option 0x%02x", code);
return 0;
}
-
+
option[OPT_CODE] = code;
option[OPT_LEN] = length;
switch (length) {
- case 1: *u8 = data; break;
- case 2: *u16 = data; break;
- case 4: *u32 = data; break;
+ case 1:
+ *u8 = data;
+ break;
+ case 2:
+ *u16 = data;
+ break;
+ case 4:
+ *u32 = data;
+ break;
}
memcpy(option + 2, &aligned, length);
return add_option_string(optionptr, option);
}
-#if 0
-void init_header(struct dhcpMessage *packet, char type)
-{
- memset(packet, 0, sizeof(struct dhcpMessage));
- switch (type) {
- case DHCPDISCOVER:
- case DHCPREQUEST:
- case DHCPRELEASE:
- case DHCPINFORM:
- packet->op = BOOTREQUEST;
- break;
- case DHCPOFFER:
- case DHCPACK:
- case DHCPNAK:
- packet->op = BOOTREPLY;
- }
- packet->htype = ETH_10MB;
- packet->hlen = ETH_10MB_LEN;
- packet->cookie = htonl(DHCP_MAGIC);
- packet->options[0] = DHCP_END;
- add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
-}
-#endif
+
static u_int16_t checksum(void *addr, int count)
{
/* Compute Internet Checksum for "count" bytes
@@ -401,26 +390,29 @@ static u_int16_t checksum(void *addr, int count)
register int32_t sum = 0;
u_int16_t *source = (u_int16_t *) addr;
- while( count > 1 ) {
+ while (count > 1) {
/* This is the inner loop */
sum += *source++;
count -= 2;
}
/* Add left-over byte, if any */
- if( count > 0 )
- sum += * (unsigned char *) source;
+ if (count > 0) {
+ sum += *(unsigned char *) source;
+ }
/* Fold 32-bit sum to 16 bits */
- while (sum>>16)
+ while (sum >> 16) {
sum = (sum & 0xffff) + (sum >> 16);
+ }
return ~sum;
}
/* Constuct a ip/udp header for a packet, and specify the source and dest hardware address */
-static int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port,
- u_int32_t dest_ip, int dest_port, unsigned char *dest_arp, int ifindex)
+static int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip,
+ int source_port, u_int32_t dest_ip, int dest_port,
+ unsigned char *dest_arp, int ifindex)
{
int l_fd;
int result;
@@ -431,16 +423,16 @@ static int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int sour
DEBUG(LOG_ERR, "socket call failed: %s", sys_errlist[errno]);
return -1;
}
-
+
memset(&dest, 0, sizeof(dest));
memset(&packet, 0, sizeof(packet));
-
+
dest.sll_family = AF_PACKET;
dest.sll_protocol = htons(ETH_P_IP);
dest.sll_ifindex = ifindex;
dest.sll_halen = 6;
memcpy(dest.sll_addr, dest_arp, 6);
- if (bind(l_fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) {
+ if (bind(l_fd, (struct sockaddr *) &dest, sizeof(struct sockaddr_ll)) < 0) {
DEBUG(LOG_ERR, "bind call failed: %s", sys_errlist[errno]);
close(l_fd);
return -1;
@@ -451,18 +443,20 @@ static int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int sour
packet.ip.daddr = dest_ip;
packet.udp.source = htons(source_port);
packet.udp.dest = htons(dest_port);
- packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage)); /* cheat on the psuedo-header */
+ packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage)); /* cheat on the psuedo-header */
packet.ip.tot_len = packet.udp.len;
memcpy(&(packet.data), payload, sizeof(struct dhcpMessage));
packet.udp.check = checksum(&packet, sizeof(struct udp_dhcp_packet));
-
+
packet.ip.tot_len = htons(sizeof(struct udp_dhcp_packet));
packet.ip.ihl = sizeof(packet.ip) >> 2;
packet.ip.version = IPVERSION;
packet.ip.ttl = IPDEFTTL;
packet.ip.check = checksum(&(packet.ip), sizeof(packet.ip));
- result = sendto(l_fd, &packet, sizeof(struct udp_dhcp_packet), 0, (struct sockaddr *) &dest, sizeof(dest));
+ result =
+ sendto(l_fd, &packet, sizeof(struct udp_dhcp_packet), 0,
+ (struct sockaddr *) &dest, sizeof(dest));
if (result <= 0) {
DEBUG(LOG_ERR, "write on socket failed: %s", sys_errlist[errno]);
}
@@ -471,34 +465,41 @@ static int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int sour
}
/* Let the kernel do all the work for packet generation */
-static int kernel_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port,
- u_int32_t dest_ip, int dest_port)
+static int kernel_packet(struct dhcpMessage *payload, u_int32_t source_ip,
+ int source_port, u_int32_t dest_ip, int dest_port)
{
int n = 1;
int l_fd, result;
struct sockaddr_in client;
-
- if ((l_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
+
+ if ((l_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
return -1;
-
- if (setsockopt(l_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1)
+ }
+
+ if (setsockopt(l_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) ==
+ -1) {
return -1;
+ }
memset(&client, 0, sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons(source_port);
client.sin_addr.s_addr = source_ip;
- if (bind(l_fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1)
+ if (bind(l_fd, (struct sockaddr *) &client, sizeof(struct sockaddr)) ==
+ -1) {
return -1;
+ }
memset(&client, 0, sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons(dest_port);
- client.sin_addr.s_addr = dest_ip;
+ client.sin_addr.s_addr = dest_ip;
- if (connect(l_fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1)
+ if (connect(l_fd, (struct sockaddr *) &client, sizeof(struct sockaddr)) ==
+ -1) {
return -1;
+ }
result = write(l_fd, payload, sizeof(struct dhcpMessage));
close(l_fd);
@@ -508,12 +509,13 @@ static int kernel_packet(struct dhcpMessage *payload, u_int32_t source_ip, int s
/* initialize a packet with the proper defaults */
static void init_packet(struct dhcpMessage *packet, char type)
{
- struct vendor {
+ struct vendor {
char vendor, length;
- char str[sizeof("udhcp "VERSION)];
- } vendor_id = { DHCP_VENDOR, sizeof("udhcp "VERSION) - 1, "udhcp "VERSION};
-
-// init_header(packet, type);
+ char str[sizeof("udhcp " VERSION)];
+ }
+ vendor_id = {
+ DHCP_VENDOR, sizeof("udhcp " VERSION) - 1, "udhcp " VERSION};
+
memset(packet, 0, sizeof(struct dhcpMessage));
switch (type) {
case DHCPDISCOVER:
@@ -535,7 +537,9 @@ static void init_packet(struct dhcpMessage *packet, char type)
memcpy(packet->chaddr, client_config.arp, 6);
add_option_string(packet->options, client_config.clientid);
- if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
+ if (client_config.hostname) {
+ add_option_string(packet->options, client_config.hostname);
+ }
add_option_string(packet->options, (unsigned char *) &vendor_id);
}
@@ -547,11 +551,13 @@ static void add_requests(struct dhcpMessage *packet)
{
int end = end_option(packet->options);
int i, len = 0;
-
+
packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
- for (i = 0; options[i].code; i++)
- if (options[i].flags & OPTION_REQ)
+ for (i = 0; options[i].code; i++) {
+ if (options[i].flags & OPTION_REQ) {
packet->options[end + OPT_DATA + len++] = options[i].code;
+ }
+ }
packet->options[end + OPT_LEN] = len;
packet->options[end + OPT_DATA + len] = DHCP_END;
@@ -564,17 +570,18 @@ static inline int send_discover(unsigned long xid, unsigned long requested)
init_packet(&packet, DHCPDISCOVER);
packet.xid = xid;
- if (requested)
+ if (requested) {
add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
-
+ }
add_requests(&packet);
DEBUG(LOG_DEBUG, "Sending discover...");
- return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
- SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
+ return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
+ SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
}
/* Broadcasts a DHCP request message */
-static inline int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
+static inline int send_selecting(unsigned long xid, unsigned long server,
+ unsigned long requested)
{
struct dhcpMessage packet;
struct in_addr addr;
@@ -584,17 +591,18 @@ static inline int send_selecting(unsigned long xid, unsigned long server, unsign
add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
add_simple_option(packet.options, DHCP_SERVER_ID, server);
-
+
add_requests(&packet);
addr.s_addr = requested;
DEBUG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr));
- return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
- SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
+ return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
+ SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
}
/* Unicasts or broadcasts a DHCP renew message */
-static int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
+static int send_renew(unsigned long xid, unsigned long server,
+ unsigned long ciaddr)
{
struct dhcpMessage packet;
@@ -604,45 +612,31 @@ static int send_renew(unsigned long xid, unsigned long server, unsigned long cia
add_requests(&packet);
DEBUG(LOG_DEBUG, "Sending renew...");
- if (server)
- return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
+ if (server) {
+ return kernel_packet(&packet, ciaddr, CLIENT_PORT, server,
+ SERVER_PORT);
+ }
return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
- SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
-}
+ SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
+}
/* Create a random xid */
static unsigned long random_xid(void)
{
static int initialized;
+
if (!initialized) {
srand(time(0));
initialized++;
}
return rand();
}
-#if 0
-/* Unicasts a DHCP release message */
-int send_release(unsigned long server, unsigned long ciaddr)
-{
- struct dhcpMessage packet;
-
- init_packet(&packet, DHCPRELEASE);
- packet.xid = random_xid();
- packet.ciaddr = ciaddr;
-
- add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
- add_simple_option(packet.options, DHCP_SERVER_ID, server);
-
- LOG(LOG_DEBUG, "Sending release...");
- return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
-}
-#endif
/* just a little helper */
static void change_mode(int new_mode)
{
DEBUG(LOG_INFO, "entering %s listen mode",
- new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
+ new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
close(fd_main);
fd_main = -1;
listen_mode = new_mode;
@@ -655,8 +649,8 @@ static void renew_requested(int sig)
sig = 0;
LOG(LOG_INFO, "Received SIGUSR1");
if (state == BOUND || state == RENEWING || state == REBINDING ||
- state == RELEASED) {
- change_mode(LISTEN_KERNEL);
+ state == RELEASED) {
+ change_mode(LISTEN_KERNEL);
packet_num = 0;
state = RENEW_REQUESTED;
}
@@ -676,7 +670,7 @@ static unsigned char *get_option(struct dhcpMessage *packet, int code)
int i, length;
unsigned char *optionptr;
int over = 0, done = 0, curr = OPTION_FIELD;
-
+
optionptr = packet->options;
i = 0;
length = 308;
@@ -691,7 +685,7 @@ static unsigned char *get_option(struct dhcpMessage *packet, int code)
return NULL;
}
return optionptr + i + 2;
- }
+ }
switch (optionptr[i + OPT_CODE]) {
case DHCP_PADDING:
i++;
@@ -715,7 +709,9 @@ static unsigned char *get_option(struct dhcpMessage *packet, int code)
i = 0;
length = 64;
curr = SNAME_FIELD;
- } else done = 1;
+ } else {
+ done = 1;
+ }
break;
default:
i += optionptr[OPT_LEN + i] + 2;
@@ -723,21 +719,16 @@ static unsigned char *get_option(struct dhcpMessage *packet, int code)
}
return NULL;
}
-#if 0
-static int upper_length(int length, struct dhcp_option *option)
-{
- return max_option_length[option->flags & TYPE_MASK] *
- (length / option_lengths[option->flags & TYPE_MASK]);
-}
-#endif
-static int sprintip(char *dest, char *pre, unsigned char *ip) {
+static int sprintip(char *dest, char *pre, unsigned char *ip)
+{
return sprintf(dest, "%s%d.%d.%d.%d ", pre, ip[0], ip[1], ip[2], ip[3]);
}
/* Fill dest with the text of option 'option'. */
-extern inline void fill_options(char *dest, unsigned char *option, const struct dhcp_option *type_p)
+static inline void fill_options(char *dest, unsigned char *option,
+ const struct dhcp_option *type_p)
{
int type, optlen;
u_int16_t val_u16;
@@ -750,7 +741,7 @@ extern inline void fill_options(char *dest, unsigned char *option, const struct
type = type_p->flags & TYPE_MASK;
optlen = option_lengths[type];
- for(;;) {
+ for (;;) {
switch (type) {
case OPTION_IP_PAIR:
dest += sprintip(dest, "", option);
@@ -759,7 +750,7 @@ extern inline void fill_options(char *dest, unsigned char *option, const struct
optlen = 4;
case OPTION_IP: /* Works regardless of host byte order. */
dest += sprintip(dest, "", option);
- break;
+ break;
case OPTION_BOOLEAN:
dest += sprintf(dest, *option ? "yes " : "no ");
break;
@@ -785,11 +776,13 @@ extern inline void fill_options(char *dest, unsigned char *option, const struct
case OPTION_STRING:
memcpy(dest, option, len);
dest[len] = '\0';
- return; /* Short circuit this case */
+ return; /* Short circuit this case */
}
option += optlen;
len -= optlen;
- if (len <= 0) break;
+ if (len <= 0) {
+ break;
+ }
}
}
@@ -800,8 +793,9 @@ static char *find_env(const char *prefix, char *defaultstr)
const int len = strlen(prefix);
for (ptr = environ; *ptr != NULL; ptr++) {
- if (strncmp(prefix, *ptr, len) == 0)
+ if (strncmp(prefix, *ptr, len) == 0) {
return *ptr;
+ }
}
return defaultstr;
}
@@ -816,19 +810,28 @@ static char **fill_envp(struct dhcpMessage *packet)
unsigned char *temp;
char over = 0;
- if (packet == NULL)
+ if (packet == NULL) {
num_options = 0;
- else {
- for (i = 0; options[i].code; i++)
- if (get_option(packet, options[i].code))
+ } else {
+ for (i = 0; options[i].code; i++) {
+ if (get_option(packet, options[i].code)) {
num_options++;
- if (packet->siaddr) num_options++;
- if ((temp = get_option(packet, DHCP_OPTION_OVER)))
+ }
+ }
+ if (packet->siaddr) {
+ num_options++;
+ }
+ if ((temp = get_option(packet, DHCP_OPTION_OVER))) {
over = *temp;
- if (!(over & FILE_FIELD) && packet->file[0]) num_options++;
- if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++;
+ }
+ if (!(over & FILE_FIELD) && packet->file[0]) {
+ num_options++;
+ }
+ if (!(over & SNAME_FIELD) && packet->sname[0]) {
+ num_options++;
+ }
}
-
+
envp = xmalloc((num_options + 5) * sizeof(char *));
envp[0] = xmalloc(sizeof("interface=") + strlen(client_config.interface));
sprintf(envp[0], "interface=%s", client_config.interface);
@@ -844,8 +847,11 @@ static char **fill_envp(struct dhcpMessage *packet)
sprintip(envp[3], "ip=", (unsigned char *) &packet->yiaddr);
for (i = 0, j = 4; options[i].code; i++) {
if ((temp = get_option(packet, options[i].code))) {
- envp[j] = xmalloc(max_option_length[(&options[i])->flags & TYPE_MASK] * (temp[OPT_LEN - 2] / option_lengths[(&options[i])->flags & TYPE_MASK]) + strlen((&options[i])->name) + 2);
-// envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2], &options[i]) + strlen(options[i].name) + 2);
+ envp[j] =
+ xmalloc(max_option_length[(&options[i])->flags & TYPE_MASK] *
+ (temp[OPT_LEN - 2] /
+ option_lengths[(&options[i])->flags & TYPE_MASK]) +
+ strlen((&options[i])->name) + 2);
fill_options(envp[j], temp, &options[i]);
j++;
}
@@ -865,7 +871,7 @@ static char **fill_envp(struct dhcpMessage *packet)
packet->sname[sizeof(packet->sname) - 1] = '\0';
envp[j] = xmalloc(sizeof("sname=") + strlen(packet->sname));
sprintf(envp[j++], "sname=%s", packet->sname);
- }
+ }
envp[j] = NULL;
return envp;
}
@@ -876,8 +882,9 @@ static void run_script(struct dhcpMessage *packet, const char *name)
int pid;
char **envp;
- if (client_config.script == NULL)
+ if (client_config.script == NULL) {
return;
+ }
/* call script */
pid = fork();
@@ -886,17 +893,16 @@ static void run_script(struct dhcpMessage *packet, const char *name)
return;
} else if (pid == 0) {
envp = fill_envp(packet);
-
+
/* close fd's? */
-
+
/* exec script */
DEBUG(LOG_INFO, "execle'ing %s", client_config.script);
- execle(client_config.script, client_config.script,
- name, NULL, envp);
+ execle(client_config.script, client_config.script, name, NULL, envp);
LOG(LOG_ERR, "script %s failed: %s",
- client_config.script, sys_errlist[errno]);
+ client_config.script, sys_errlist[errno]);
exit(1);
- }
+ }
}
/* SIGUSR2 handler (release) */
@@ -911,12 +917,13 @@ static void release_requested(int sig)
init_packet(&packet, DHCPRELEASE);
packet.xid = random_xid();
packet.ciaddr = requested_ip;
-
+
add_simple_option(packet.options, DHCP_REQUESTED_IP, requested_ip);
add_simple_option(packet.options, DHCP_SERVER_ID, server_addr);
DEBUG(LOG_DEBUG, "Sending release...");
- kernel_packet(&packet, requested_ip, CLIENT_PORT, server_addr, SERVER_PORT);
+ kernel_packet(&packet, requested_ip, CLIENT_PORT, server_addr,
+ SERVER_PORT);
run_script(NULL, "deconfig");
}
@@ -929,16 +936,18 @@ static void release_requested(int sig)
static int pidfile_acquire(char *pidfile)
{
int pid_fd;
- if (pidfile == NULL) return -1;
+ if (pidfile == NULL) {
+ return -1;
+ }
pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644);
if (pid_fd < 0) {
LOG(LOG_ERR, "Unable to open pidfile %s: %s\n",
- pidfile, strerror(errno));
+ pidfile, strerror(errno));
} else {
lockf(pid_fd, F_LOCK, 0);
}
-
+
return pid_fd;
}
@@ -947,8 +956,9 @@ static void pidfile_write_release(int pid_fd)
{
FILE *out;
- if (pid_fd < 0) return;
-
+ if (pid_fd < 0) {
+ return;
+ }
if ((out = fdopen(pid_fd, "w")) != NULL) {
fprintf(out, "%d\n", getpid());
fclose(out);
@@ -961,7 +971,9 @@ static void pidfile_write_release(int pid_fd)
static void exit_client(int retval)
{
unlink(client_config.pidfile);
- if (client_config.pidfile) unlink(client_config.pidfile);
+ if (client_config.pidfile) {
+ unlink(client_config.pidfile);
+ }
CLOSE_LOG();
exit(retval);
}
@@ -976,28 +988,30 @@ static void terminate(int sig)
}
-extern inline int read_interface(char *interface, int *ifindex, u_int32_t *addr, unsigned char *arp)
+static inline int read_interface(char *interface, int *ifindex,
+ u_int32_t * addr, unsigned char *arp)
{
int l_fd;
struct ifreq ifr;
struct sockaddr_in *s_in;
memset(&ifr, 0, sizeof(struct ifreq));
- if((l_fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) {
+ if ((l_fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) {
ifr.ifr_addr.sa_family = AF_INET;
strcpy(ifr.ifr_name, interface);
- if (addr) {
+ if (addr) {
if (ioctl(l_fd, SIOCGIFADDR, &ifr) == 0) {
s_in = (struct sockaddr_in *) &ifr.ifr_addr;
*addr = s_in->sin_addr.s_addr;
- DEBUG(LOG_INFO, "%s (our ip) = %s", ifr.ifr_name, inet_ntoa(s_in->sin_addr));
+ DEBUG(LOG_INFO, "%s (our ip) = %s", ifr.ifr_name,
+ inet_ntoa(s_in->sin_addr));
} else {
LOG(LOG_ERR, "SIOCGIFADDR failed!: %s", sys_errlist[errno]);
return -1;
}
}
-
+
if (ioctl(l_fd, SIOCGIFINDEX, &ifr) == 0) {
DEBUG(LOG_INFO, "adapter index %d", ifr.ifr_ifindex);
*ifindex = ifr.ifr_ifindex;
@@ -1007,8 +1021,9 @@ extern inline int read_interface(char *interface, int *ifindex, u_int32_t *addr,
}
if (ioctl(l_fd, SIOCGIFHWADDR, &ifr) == 0) {
memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);
- DEBUG(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
- arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
+ DEBUG(LOG_INFO,
+ "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
+ arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
} else {
LOG(LOG_ERR, "SIOCGIFHWADDR failed!: %s", sys_errlist[errno]);
return -1;
@@ -1022,7 +1037,7 @@ extern inline int read_interface(char *interface, int *ifindex, u_int32_t *addr,
}
-extern inline int listen_socket(unsigned int ip, int port, char *inf)
+static inline int listen_socket(unsigned int ip, int port, char *inf)
{
struct ifreq interface;
int l_fd;
@@ -1034,32 +1049,36 @@ extern inline int listen_socket(unsigned int ip, int port, char *inf)
DEBUG(LOG_ERR, "socket call failed: %s", sys_errlist[errno]);
return -1;
}
-
+
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = ip;
- if (setsockopt(l_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) {
+ if (setsockopt(l_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) ==
+ -1) {
close(l_fd);
return -1;
}
- if (setsockopt(l_fd, SOL_SOCKET, SO_BROADCAST, (char *) &n, sizeof(n)) == -1) {
+ if (setsockopt(l_fd, SOL_SOCKET, SO_BROADCAST, (char *) &n, sizeof(n)) ==
+ -1) {
close(l_fd);
return -1;
}
strncpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ);
- if (setsockopt(l_fd, SOL_SOCKET, SO_BINDTODEVICE,(char *)&interface, sizeof(interface)) < 0) {
+ if (setsockopt
+ (l_fd, SOL_SOCKET, SO_BINDTODEVICE, (char *) &interface,
+ sizeof(interface)) < 0) {
close(l_fd);
return -1;
}
- if (bind(l_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) {
+ if (bind(l_fd, (struct sockaddr *) &addr, sizeof(struct sockaddr)) == -1) {
close(l_fd);
return -1;
}
-
+
return l_fd;
}
@@ -1074,7 +1093,7 @@ static int raw_socket(int ifindex)
DEBUG(LOG_ERR, "socket call failed: %s", sys_errlist[errno]);
return -1;
}
-
+
sock.sll_family = AF_PACKET;
sock.sll_protocol = htons(ETH_P_IP);
sock.sll_ifindex = ifindex;
@@ -1111,23 +1130,24 @@ static int get_packet(struct dhcpMessage *packet, int l_fd)
return -2;
}
DEBUG(LOG_INFO, "Received a packet");
-
- if (packet->op == BOOTREQUEST && (vendor = get_option(packet, DHCP_VENDOR))) {
+
+ if (packet->op == BOOTREQUEST
+ && (vendor = get_option(packet, DHCP_VENDOR))) {
for (i = 0; broken_vendors[i][0]; i++) {
- if (vendor[OPT_LEN - 2] == (unsigned char) strlen(broken_vendors[i]) &&
- !strncmp(vendor, broken_vendors[i], vendor[OPT_LEN - 2])) {
- DEBUG(LOG_INFO, "broken client (%s), forcing broadcast",
- broken_vendors[i]);
- packet->flags |= htons(BROADCAST_FLAG);
+ if (vendor[OPT_LEN - 2] ==
+ (unsigned char) strlen(broken_vendors[i])
+ && !strncmp(vendor, broken_vendors[i], vendor[OPT_LEN - 2])) {
+ DEBUG(LOG_INFO, "broken client (%s), forcing broadcast",
+ broken_vendors[i]);
+ packet->flags |= htons(BROADCAST_FLAG);
}
}
}
-
return bytes;
}
-extern inline int get_raw_packet(struct dhcpMessage *payload, int l_fd)
+static inline int get_raw_packet(struct dhcpMessage *payload, int l_fd)
{
int bytes;
struct udp_dhcp_packet packet;
@@ -1138,30 +1158,31 @@ extern inline int get_raw_packet(struct dhcpMessage *payload, int l_fd)
bytes = read(l_fd, &packet, sizeof(struct udp_dhcp_packet));
if (bytes < 0) {
DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring");
- usleep(500000); /* possible down interface, looping condition */
+ usleep(500000); /* possible down interface, looping condition */
return -1;
}
-
+
if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
DEBUG(LOG_INFO, "message too short, ignoring");
return -1;
}
-
+
if (bytes < ntohs(packet.ip.tot_len)) {
DEBUG(LOG_INFO, "Truncated packet");
return -1;
}
-
+
/* ignore any extra garbage bytes */
bytes = ntohs(packet.ip.tot_len);
-
+
/* Make sure its the right packet for us, and that it passes sanity checks */
if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION ||
- packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
- bytes > (int) sizeof(struct udp_dhcp_packet) ||
- ntohs(packet.udp.len) != (short) (bytes - sizeof(packet.ip))) {
- DEBUG(LOG_INFO, "unrelated/bogus packet");
- return -1;
+ packet.ip.ihl != sizeof(packet.ip) >> 2
+ || packet.udp.dest != htons(CLIENT_PORT)
+ || bytes > (int) sizeof(struct udp_dhcp_packet)
+ || ntohs(packet.udp.len) != (short) (bytes - sizeof(packet.ip))) {
+ DEBUG(LOG_INFO, "unrelated/bogus packet");
+ return -1;
}
/* check IP checksum */
@@ -1171,7 +1192,7 @@ extern inline int get_raw_packet(struct dhcpMessage *payload, int l_fd)
DEBUG(LOG_INFO, "bad IP header checksum, ignoring");
return -1;
}
-
+
/* verify the UDP checksum by replacing the header with a psuedo header */
source = packet.ip.saddr;
dest = packet.ip.daddr;
@@ -1182,14 +1203,15 @@ extern inline int get_raw_packet(struct dhcpMessage *payload, int l_fd)
packet.ip.protocol = IPPROTO_UDP;
packet.ip.saddr = source;
packet.ip.daddr = dest;
- packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
+ packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
if (check && check != checksum(&packet, bytes)) {
DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring");
return -1;
}
-
- memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
-
+
+ memcpy(payload, &(packet.data),
+ bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
+
if (ntohl(payload->cookie) != DHCP_MAGIC) {
LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring");
return -1;
@@ -1214,31 +1236,39 @@ int udhcpc_main(int argc, char *argv[])
time_t now;
static struct option l_options[] = {
- {"clientid", required_argument, 0, 'c'},
- {"foreground", no_argument, 0, 'f'},
- {"hostname", required_argument, 0, 'H'},
- {"help", no_argument, 0, 'h'},
- {"interface", required_argument, 0, 'i'},
- {"now", no_argument, 0, 'n'},
- {"pidfile", required_argument, 0, 'p'},
- {"quit", no_argument, 0, 'q'},
- {"request", required_argument, 0, 'r'},
- {"script", required_argument, 0, 's'},
- {"version", no_argument, 0, 'v'},
+ {"clientid", required_argument, 0, 'c'},
+ {"foreground", no_argument, 0, 'f'},
+ {"hostname", required_argument, 0, 'H'},
+ {"help", no_argument, 0, 'h'},
+ {"interface", required_argument, 0, 'i'},
+ {"now", no_argument, 0, 'n'},
+ {"pidfile", required_argument, 0, 'p'},
+ {"quit", no_argument, 0, 'q'},
+ {"request", required_argument, 0, 'r'},
+ {"script", required_argument, 0, 's'},
+ {"version", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
/* get options */
while (1) {
int option_index = 0;
- c = getopt_long(argc, argv, "c:fH:hi:np:qr:s:v", l_options, &option_index);
- if (c == -1) break;
-
+
+ c = getopt_long(argc, argv, "c:fH:hi:np:qr:s:v", l_options,
+ &option_index);
+ if (c == -1) {
+ break;
+ }
+
switch (c) {
case 'c':
len = strlen(optarg);
- if (len > 255) len = 255;
- if (client_config.clientid) free(client_config.clientid);
+ if (len > 255) {
+ len = 255;
+ }
+ if (client_config.clientid) {
+ free(client_config.clientid);
+ }
client_config.clientid = xmalloc(len + 2);
client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
client_config.clientid[OPT_LEN] = len;
@@ -1250,32 +1280,34 @@ int udhcpc_main(int argc, char *argv[])
break;
case 'H':
len = strlen(optarg);
- if (len > 255) len = 255;
- if (client_config.hostname) free(client_config.hostname);
+ if (len > 255) {
+ len = 255;
+ }
+ if (client_config.hostname) {
+ free(client_config.hostname);
+ }
client_config.hostname = xmalloc(len + 2);
client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
client_config.hostname[OPT_LEN] = len;
strncpy(client_config.hostname + 2, optarg, len);
break;
case 'h':
- puts(
-"Usage: udhcpcd [OPTIONS]\n\n"
-" -c, --clientid=CLIENTID Client identifier\n"
-" -H, --hostname=HOSTNAME Client hostname\n"
-" -f, --foreground Do not fork after getting lease\n"
-" -i, --interface=INTERFACE Interface to use (default: eth0)\n"
-" -n, --now Exit with failure if lease cannot be\n"
-" immediately negotiated.\n"
-" -p, --pidfile=file Store process ID of daemon in file\n"
-" -q, --quit Quit after obtaining lease\n"
-" -r, --request=IP IP address to request (default: none)\n"
-" -s, --script=file Run file at dhcp events (default:\n"
-" " DEFAULT_SCRIPT ")\n"
-" -v, --version Display version"
- );
+ puts("Usage: udhcpcd [OPTIONS]\n\n"
+ " -c, --clientid=CLIENTID Client identifier\n"
+ " -H, --hostname=HOSTNAME Client hostname\n"
+ " -f, --foreground Do not fork after getting lease\n"
+ " -i, --interface=INTERFACE Interface to use (default: eth0)\n"
+ " -n, --now Exit with failure if lease cannot be\n"
+ " immediately negotiated.\n"
+ " -p, --pidfile=file Store process ID of daemon in file\n"
+ " -q, --quit Quit after obtaining lease\n"
+ " -r, --request=IP IP address to request (default: none)\n"
+ " -s, --script=file Run file at dhcp events (default:\n"
+ " " DEFAULT_SCRIPT ")\n"
+ " -v, --version Display version");
return 0;
case 'i':
- client_config.interface = optarg;
+ client_config.interface = optarg;
break;
case 'n':
client_config.abort_if_no_lease = 1;
@@ -1305,10 +1337,12 @@ int udhcpc_main(int argc, char *argv[])
pid_fd = pidfile_acquire(client_config.pidfile);
pidfile_write_release(pid_fd);
- if (read_interface(client_config.interface, &client_config.ifindex, NULL, client_config.arp) < 0) {
+ if (read_interface
+ (client_config.interface, &client_config.ifindex, NULL,
+ client_config.arp) < 0) {
exit_client(1);
}
-
+
if (!client_config.clientid) {
client_config.clientid = xmalloc(6 + 3);
client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
@@ -1321,7 +1355,7 @@ int udhcpc_main(int argc, char *argv[])
signal(SIGUSR1, renew_requested);
signal(SIGUSR2, release_requested);
signal(SIGTERM, terminate);
-
+
state = INIT_SELECTING;
run_script(NULL, "deconfig");
change_mode(LISTEN_RAW);
@@ -1333,12 +1367,15 @@ int udhcpc_main(int argc, char *argv[])
if (listen_mode != LISTEN_NONE && fd_main < 0) {
if (listen_mode == LISTEN_KERNEL) {
- fd_main = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
+ fd_main =
+ listen_socket(INADDR_ANY, CLIENT_PORT,
+ client_config.interface);
} else {
fd_main = raw_socket(client_config.ifindex);
}
if (fd_main < 0) {
- LOG(LOG_ERR, "FATAL: couldn't listen on socket, %s", sys_errlist[errno]);
+ LOG(LOG_ERR, "FATAL: couldn't listen on socket, %s",
+ sys_errlist[errno]);
exit_client(0);
}
}
@@ -1350,7 +1387,7 @@ int udhcpc_main(int argc, char *argv[])
DEBUG(LOG_INFO, "Waiting on select...\n");
retval = select(fd_main + 1, &rfds, NULL, NULL, &tv);
} else {
- retval = 0; /* If we already timed out, fall through */
+ retval = 0; /* If we already timed out, fall through */
}
now = time(0);
@@ -1363,7 +1400,7 @@ int udhcpc_main(int argc, char *argv[])
xid = random_xid();
}
/* send discover packet */
- send_discover(xid, requested_ip); /* broadcast */
+ send_discover(xid, requested_ip); /* broadcast */
timeout = now + ((packet_num == 2) ? 10 : 2);
packet_num++;
@@ -1382,9 +1419,9 @@ int udhcpc_main(int argc, char *argv[])
if (packet_num < 3) {
/* send request packet */
if (state == RENEW_REQUESTED) {
- send_renew(xid, server_addr, requested_ip); /* unicast */
+ send_renew(xid, server_addr, requested_ip); /* unicast */
} else {
- send_selecting(xid, server_addr, requested_ip); /* broadcast */
+ send_selecting(xid, server_addr, requested_ip); /* broadcast */
}
timeout = now + ((packet_num == 2) ? 10 : 2);
packet_num++;
@@ -1411,8 +1448,8 @@ int udhcpc_main(int argc, char *argv[])
DEBUG(LOG_INFO, "Entering rebinding state");
} else {
/* send a request packet */
- send_renew(xid, server_addr, requested_ip); /* unicast */
-
+ send_renew(xid, server_addr, requested_ip); /* unicast */
+
t1 = (t2 - t1) / 2 + t1;
timeout = t1 + start;
}
@@ -1429,7 +1466,7 @@ int udhcpc_main(int argc, char *argv[])
change_mode(LISTEN_RAW);
} else {
/* send a request packet */
- send_renew(xid, 0, requested_ip); /* broadcast */
+ send_renew(xid, 0, requested_ip); /* broadcast */
t2 = (lease - t2) / 2 + t2;
timeout = t2 + start;
@@ -1440,17 +1477,19 @@ int udhcpc_main(int argc, char *argv[])
timeout = 0x7fffffff;
break;
}
- } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd_main, &rfds)) {
+ } else if (retval > 0 && listen_mode != LISTEN_NONE
+ && FD_ISSET(fd_main, &rfds)) {
/* a packet is ready, read it */
-
+
if (listen_mode == LISTEN_KERNEL) {
len = get_packet(&packet, fd_main);
} else {
len = get_raw_packet(&packet, fd_main);
}
if (len == -1 && errno != EINTR) {
- DEBUG(LOG_INFO, "error on read, %s, reopening socket", sys_errlist[errno]);
- change_mode(listen_mode); /* just close and reopen */
+ DEBUG(LOG_INFO, "error on read, %s, reopening socket",
+ sys_errlist[errno]);
+ change_mode(listen_mode); /* just close and reopen */
}
if (len < 0) {
continue;
@@ -1458,15 +1497,15 @@ int udhcpc_main(int argc, char *argv[])
if (packet.xid != xid) {
DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)",
- (unsigned long) packet.xid, xid);
+ (unsigned long) packet.xid, xid);
continue;
}
-
+
if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
continue;
}
-
+
switch (state) {
case INIT_SELECTING:
/* Must be a DHCPOFFER to one of our xid's */
@@ -1475,7 +1514,7 @@ int udhcpc_main(int argc, char *argv[])
memcpy(&server_addr, temp, 4);
xid = packet.xid;
requested_ip = packet.yiaddr;
-
+
/* enter requesting state */
state = REQUESTING;
timeout = now;
@@ -1491,43 +1530,47 @@ int udhcpc_main(int argc, char *argv[])
case REBINDING:
if (*message == DHCPACK) {
if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
- LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease");
+ LOG(LOG_ERR,
+ "No lease time with ACK, using 1 hour lease");
lease = 60 * 60;
} else {
memcpy(&lease, temp, 4);
lease = ntohl(lease);
}
-
+
/* enter bound state */
t1 = lease / 2;
-
+
/* little fixed point for n * .875 */
t2 = (lease * 0x7) >> 3;
temp_addr.s_addr = packet.yiaddr;
- LOG(LOG_INFO, "Lease of %s obtained, lease time %ld", inet_ntoa(temp_addr), lease);
+ LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
+ inet_ntoa(temp_addr), lease);
start = now;
timeout = t1 + start;
requested_ip = packet.yiaddr;
- run_script(&packet, ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
+ run_script(&packet,
+ ((state == RENEWING
+ || state == REBINDING) ? "renew" : "bound"));
state = BOUND;
change_mode(LISTEN_NONE);
{
int pid_fd2;
+
if (client_config.quit_after_lease) {
exit_client(0);
} else if (!client_config.foreground) {
- pid_fd2 = pidfile_acquire(client_config.pidfile); /* hold lock during fork. */
+ pid_fd2 = pidfile_acquire(client_config.pidfile); /* hold lock during fork. */
if (daemon(0, 0) == -1) {
perror("fork");
exit_client(1);
}
- client_config.foreground = 1; /* Do not fork again. */
+ client_config.foreground = 1; /* Do not fork again. */
pidfile_write_release(pid_fd2);
}
}
- }
- else if (*message == DHCPNAK) {
+ } else if (*message == DHCPNAK) {
/* return to init state */
LOG(LOG_INFO, "Received DHCP NAK");
run_script(&packet, "nak");
@@ -1539,20 +1582,19 @@ int udhcpc_main(int argc, char *argv[])
requested_ip = 0;
packet_num = 0;
change_mode(LISTEN_RAW);
- sleep(3); /* avoid excessive network traffic */
+ sleep(3); /* avoid excessive network traffic */
}
break;
- /* case BOUND, RELEASED: - ignore all packets */
- }
- }
- else if (retval == -1 && errno == EINTR) {
+ /* case BOUND, RELEASED: - ignore all packets */
+ }
+ } else if (retval == -1 && errno == EINTR) {
/* a signal was caught */
-
+
} else {
/* An error occured */
DEBUG(LOG_ERR, "Error on select");
}
-
+
}
return 0;
}