Fix previous commit -- handle filling empty options properly.
Clean up pidfile defaults and help message. Clean up the write/sendto fixes to be a bit more readable.
This commit is contained in:
parent
31d15af0fa
commit
5f55dbc3ff
11
ndhc/ndhc.c
11
ndhc/ndhc.c
@ -1,6 +1,6 @@
|
|||||||
/* dhcpc.c
|
/* ndhc.c
|
||||||
*
|
*
|
||||||
* ndhc DHCP client
|
* ndhc DHCP client, originally based on udhcpc
|
||||||
*
|
*
|
||||||
* Nicholas J. Kain <njkain at gmail dot com> 2004-2010
|
* Nicholas J. Kain <njkain at gmail dot com> 2004-2010
|
||||||
* Russ Dill <Russ.Dill@asu.edu> July 2001
|
* Russ Dill <Russ.Dill@asu.edu> July 2001
|
||||||
@ -93,6 +93,7 @@ static void show_usage(void)
|
|||||||
" -f, --foreground Do not fork after getting lease\n"
|
" -f, --foreground Do not fork after getting lease\n"
|
||||||
" -b, --background Fork to background if lease cannot be\n"
|
" -b, --background Fork to background if lease cannot be\n"
|
||||||
" immediately negotiated.\n"
|
" immediately negotiated.\n"
|
||||||
|
" -p, --pidfile File to which the pid will be written\n"
|
||||||
" -i, --interface=INTERFACE Interface to use (default: eth0)\n"
|
" -i, --interface=INTERFACE Interface to use (default: eth0)\n"
|
||||||
" -n, --now Exit with failure if lease cannot be\n"
|
" -n, --now Exit with failure if lease cannot be\n"
|
||||||
" immediately negotiated.\n"
|
" immediately negotiated.\n"
|
||||||
@ -458,8 +459,8 @@ static int do_work(void)
|
|||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
char pidfile[MAX_PATH_LENGTH] = "";
|
char pidfile[MAX_PATH_LENGTH] = PID_FILE_DEFAULT;
|
||||||
char chroot_dir[MAX_PATH_LENGTH] = PID_FILE_DEFAULT;
|
char chroot_dir[MAX_PATH_LENGTH] = "";
|
||||||
int c, len;
|
int c, len;
|
||||||
struct passwd *pwd;
|
struct passwd *pwd;
|
||||||
uid_t uid = 0;
|
uid_t uid = 0;
|
||||||
@ -507,7 +508,7 @@ int main(int argc, char **argv)
|
|||||||
client_config.background_if_no_lease = 1;
|
client_config.background_if_no_lease = 1;
|
||||||
break;
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
strlcpy(pidfile, optarg, MAX_PATH_LENGTH);
|
strlcpy(pidfile, optarg, sizeof pidfile);
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
case 'H':
|
case 'H':
|
||||||
|
@ -152,7 +152,7 @@ int raw_packet(struct dhcpMessage *payload, uint32_t source_ip,
|
|||||||
int remain = sizeof(struct udp_dhcp_packet);
|
int remain = sizeof(struct udp_dhcp_packet);
|
||||||
int sent = 0;
|
int sent = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
result = sendto(fd, &packet + sent, remain, 0,
|
result = sendto(fd, &packet + sent, remain - sent, 0,
|
||||||
(struct sockaddr *)&dest, sizeof dest);
|
(struct sockaddr *)&dest, sizeof dest);
|
||||||
if (result == -1) {
|
if (result == -1) {
|
||||||
if (errno == EINTR)
|
if (errno == EINTR)
|
||||||
@ -160,9 +160,8 @@ int raw_packet(struct dhcpMessage *payload, uint32_t source_ip,
|
|||||||
log_error("raw_packet: sendto failed: %s", strerror(errno));
|
log_error("raw_packet: sendto failed: %s", strerror(errno));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
remain =- result;
|
|
||||||
sent += result;
|
sent += result;
|
||||||
if (remain == 0)
|
if (remain == sent)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
out_fd:
|
out_fd:
|
||||||
@ -203,16 +202,15 @@ int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip,
|
|||||||
int remain = sizeof(struct dhcpMessage);
|
int remain = sizeof(struct dhcpMessage);
|
||||||
int sent = 0;
|
int sent = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
result = write(fd, payload + sent, remain);
|
result = write(fd, payload + sent, remain - sent);
|
||||||
if (result == -1) {
|
if (result == -1) {
|
||||||
if (errno == EINTR)
|
if (errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
log_error("kernel_packet: write failed: %s", strerror(errno));
|
log_error("kernel_packet: write failed: %s", strerror(errno));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
remain =- result;
|
|
||||||
sent += result;
|
sent += result;
|
||||||
if (remain == 0)
|
if (remain == sent)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
out_fd:
|
out_fd:
|
||||||
|
@ -54,7 +54,8 @@ static int sprintip(char *dest, size_t size, char *pre, unsigned char *ip)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Fill dest with the text of option 'option'. */
|
/* Fill dest with the text of option 'option'. */
|
||||||
static void fill_options(char *dest, unsigned char *option,
|
/* Returns 0 if successful, -1 if nothing was filled in. */
|
||||||
|
static int fill_options(char *dest, unsigned char *option,
|
||||||
struct dhcp_option *type_p, unsigned int maxlen)
|
struct dhcp_option *type_p, unsigned int maxlen)
|
||||||
{
|
{
|
||||||
int type, optlen;
|
int type, optlen;
|
||||||
@ -62,9 +63,12 @@ static void fill_options(char *dest, unsigned char *option,
|
|||||||
int16_t val_s16;
|
int16_t val_s16;
|
||||||
uint32_t val_u32;
|
uint32_t val_u32;
|
||||||
int32_t val_s32;
|
int32_t val_s32;
|
||||||
int len = option[OPT_LEN - 2];
|
|
||||||
char *odest;
|
char *odest;
|
||||||
|
|
||||||
|
if (!option)
|
||||||
|
return -1;
|
||||||
|
int len = option[OPT_LEN - 2];
|
||||||
|
|
||||||
odest = dest;
|
odest = dest;
|
||||||
|
|
||||||
dest += snprintf(dest, maxlen, "%s=", type_p->name);
|
dest += snprintf(dest, maxlen, "%s=", type_p->name);
|
||||||
@ -113,10 +117,11 @@ static void fill_options(char *dest, unsigned char *option,
|
|||||||
"%ld ", (long) ntohl(val_s32));
|
"%ld ", (long) ntohl(val_s32));
|
||||||
break;
|
break;
|
||||||
case OPTION_STRING:
|
case OPTION_STRING:
|
||||||
if ( (maxlen - (dest - odest)) < (unsigned)len) return;
|
if ( (maxlen - (dest - odest)) < (unsigned)len)
|
||||||
|
return -1;
|
||||||
memcpy(dest, option, len);
|
memcpy(dest, option, len);
|
||||||
dest[len] = '\0';
|
dest[len] = '\0';
|
||||||
return; /* Short circuit this case */
|
return 0; /* Short circuit this case */
|
||||||
}
|
}
|
||||||
option += optlen;
|
option += optlen;
|
||||||
len -= optlen;
|
len -= optlen;
|
||||||
@ -124,6 +129,7 @@ static void fill_options(char *dest, unsigned char *option,
|
|||||||
break;
|
break;
|
||||||
*(dest++) = ':';
|
*(dest++) = ':';
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int open_ifch(void) {
|
static int open_ifch(void) {
|
||||||
@ -147,22 +153,20 @@ static int open_ifch(void) {
|
|||||||
static void sockwrite(int fd, const char *buf, size_t count)
|
static void sockwrite(int fd, const char *buf, size_t count)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
int remain = count;
|
|
||||||
int sent = 0;
|
int sent = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
ret = write(fd, buf + sent, remain);
|
ret = write(fd, buf + sent, count - sent);
|
||||||
if (ret == -1) {
|
if (ret == -1) {
|
||||||
if (errno == EINTR)
|
if (errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
log_error("sockwrite: write failed: %s", strerror(errno));
|
log_error("sockwrite: write failed: %s", strerror(errno));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
remain =- ret;
|
|
||||||
sent += ret;
|
sent += ret;
|
||||||
if (remain == 0)
|
if (sent == count)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
log_line("writing: %s", (char *)buf);
|
log_line("writing: %s", buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void deconfig_if(void)
|
static void deconfig_if(void)
|
||||||
@ -190,13 +194,15 @@ static void translate_option(int sockfd, struct dhcpMessage *packet, int opt)
|
|||||||
unsigned char *p;
|
unsigned char *p;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (!packet) return;
|
if (!packet)
|
||||||
|
return;
|
||||||
|
|
||||||
memset(buf, '\0', sizeof(buf));
|
memset(buf, '\0', sizeof(buf));
|
||||||
memset(buf2, '\0', sizeof(buf2));
|
memset(buf2, '\0', sizeof(buf2));
|
||||||
|
|
||||||
p = get_option(packet, options[opt].code);
|
p = get_option(packet, options[opt].code);
|
||||||
fill_options(buf2, p, &options[opt], sizeof(buf2) - 1);
|
if (fill_options(buf2, p, &options[opt], sizeof(buf2) - 1) == -1)
|
||||||
|
return;
|
||||||
snprintf(buf, sizeof buf, "%s:", buf2);
|
snprintf(buf, sizeof buf, "%s:", buf2);
|
||||||
for (i=0; i<256; i++) {
|
for (i=0; i<256; i++) {
|
||||||
if (buf[i] == '\0') break;
|
if (buf[i] == '\0') break;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user