Aron Griffis
2009-Apr-07 02:06 UTC
[klibc] [PATCH] ipconfig: send hostname in DHCP request
If a hostname is requested, for example -d ::::foo::dhcp, then include the hostname in the DHCP discover/request. This patch also allows the vendor class identifier to be omitted from the DHCP discover/request by specifying -i "" Signed-off-by: Aron Griffis <agriffis at n01se.net> --- usr/kinit/ipconfig/dhcp_proto.c | 49 ++++++++++++++++++++++++++++++-------- usr/kinit/ipconfig/main.c | 4 +++ usr/kinit/ipconfig/netdev.h | 1 + 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/usr/kinit/ipconfig/dhcp_proto.c b/usr/kinit/ipconfig/dhcp_proto.c index d4f2c09..775a5ca 100644 --- a/usr/kinit/ipconfig/dhcp_proto.c +++ b/usr/kinit/ipconfig/dhcp_proto.c @@ -49,24 +49,26 @@ static uint8_t dhcp_end[] = { /* Both iovecs below have to have the same structure, since dhcp_send() pokes at the internals */ -#define DHCP_IOV_LEN 6 +#define DHCP_IOV_LEN 7 -static struct iovec dhcp_discover_iov[] = { +static struct iovec dhcp_discover_iov[DHCP_IOV_LEN] = { /* [0] = ip + udp header */ /* [1] = bootp header */ [2] = {dhcp_discover_hdr, sizeof(dhcp_discover_hdr)}, [3] = {dhcp_params, sizeof(dhcp_params)}, - /* [4] = DHCP vendor class */ - [5] = {dhcp_end, sizeof(dhcp_end)} + /* [4] = optional vendor class */ + /* [5] = optional hostname */ + /* [6] = {dhcp_end, sizeof(dhcp_end)} */ }; -static struct iovec dhcp_request_iov[] = { +static struct iovec dhcp_request_iov[DHCP_IOV_LEN] = { /* [0] = ip + udp header */ /* [1] = bootp header */ [2] = {dhcp_request_hdr, sizeof(dhcp_request_hdr)}, [3] = {dhcp_params, sizeof(dhcp_params)}, - /* [4] = DHCP vendor class */ - [5] = {dhcp_end, sizeof(dhcp_end)} + /* [4] = optional vendor class */ + /* [5] = optional hostname */ + /* [6] = {dhcp_end, sizeof(dhcp_end)} */ }; /* @@ -164,6 +166,8 @@ static int dhcp_recv(struct netdev *dev) static int dhcp_send(struct netdev *dev, struct iovec *vec) { struct bootp_hdr bootp; + char dhcp_hostname[SYS_NMLN+2]; + int i = 4; memset(&bootp, 0, sizeof(struct bootp_hdr)); @@ -179,12 +183,35 @@ static int dhcp_send(struct netdev *dev, struct iovec *vec) vec[1].iov_base = &bootp; vec[1].iov_len = sizeof(struct bootp_hdr); - vec[4].iov_base = vendor_class_identifier; - vec[4].iov_len = vendor_class_identifier_len; - DEBUG(("xid %08x secs %d ", bootp.xid, ntohs(bootp.secs))); - return packet_send(dev, vec, DHCP_IOV_LEN); + if (vendor_class_identifier_len > 2) { + vec[i].iov_base = vendor_class_identifier; + vec[i].iov_len = vendor_class_identifier_len; + i++; + + DEBUG(("vendor_class_identifier \"%.*s\" ", + vendor_class_identifier_len-2, + vendor_class_identifier+2)); + } + + if (dev->reqhostname[0] != '\0') { + int len = strlen(dev->reqhostname); + dhcp_hostname[0] = 12; + dhcp_hostname[1] = len; + memcpy(dhcp_hostname+2, dev->reqhostname, len); + + vec[i].iov_base = dhcp_hostname; + vec[i].iov_len = len+2; + i++; + + DEBUG(("hostname %.*s ", len, dhcp_hostname+2)); + } + + vec[i].iov_base = dhcp_end; + vec[i].iov_len = sizeof(dhcp_end); + + return packet_send(dev, vec, i); } /* diff --git a/usr/kinit/ipconfig/main.c b/usr/kinit/ipconfig/main.c index 2ded0f3..619edf7 100644 --- a/usr/kinit/ipconfig/main.c +++ b/usr/kinit/ipconfig/main.c @@ -522,6 +522,8 @@ static int parse_device(struct netdev *dev, const char *ip) case 4: strncpy(dev->hostname, ip, SYS_NMLN - 1); dev->hostname[SYS_NMLN - 1] = '\0'; + memcpy(dev->reqhostname, dev->hostname, + SYS_NMLN); break; case 5: dev->name = ip; @@ -569,6 +571,8 @@ static void bringup_one_dev(struct netdev *template, struct netdev *dev) dev->ip_nameserver[1] = template->ip_nameserver[1]; if (template->hostname[0] != '\0') strcpy(dev->hostname, template->hostname); + if (template->reqhostname[0] != '\0') + strcpy(dev->reqhostname, template->reqhostname); dev->caps &= template->caps; bringup_device(dev); diff --git a/usr/kinit/ipconfig/netdev.h b/usr/kinit/ipconfig/netdev.h index fb6640a..a25a544 100644 --- a/usr/kinit/ipconfig/netdev.h +++ b/usr/kinit/ipconfig/netdev.h @@ -35,6 +35,7 @@ struct netdev { uint32_t ip_gateway; /* my gateway */ uint32_t ip_nameserver[2]; /* two nameservers */ uint32_t serverid; /* dhcp serverid */ + char reqhostname[SYS_NMLN]; /* requested hostname */ char hostname[SYS_NMLN]; /* hostname */ char dnsdomainname[SYS_NMLN]; /* dns domain name */ char nisdomainname[SYS_NMLN]; /* nis domain name */ -- 1.6.0.4
Aron Griffis
2009-Apr-22 19:07 UTC
[klibc] [PATCH] ipconfig: send hostname in DHCP request
Hi, I didn't get any replies to this patch. Any interest or objections to including it? Thanks, Aron
H. Peter Anvin
2009-Jul-17 02:27 UTC
[klibc] [PATCH] ipconfig: send hostname in DHCP request
Aron Griffis wrote:> Aron Griffis wrote: [Sun Jun 21 2009, 10:58:21PM EDT] >> Sorry this took so long. I think you should be able to pull the >> following changesets from >> http://agriffis.n01se.net/git/klibc.git > > ping?Sorry, I have been out of town... I will try to look at it hopefully tomorrow or otherwise when I get back (Sunday). -hpa
Possibly Parallel Threads
- [klibc:master] ipconfig: Append padding if DHCP packet length < 300 octets
- ipconfig:About the length of 'options' field of DHCP packet
- [patch] ipconfig add dhcp file preseeding support
- ipconfig: DHCP fixes
- [klibc:master] ipconfig: Write $DOMAINSEARCH as domain-search