From: Roy Marples Date: Mon, 6 Oct 2008 07:30:17 +0000 (+0000) Subject: Simplify the logic of what to put in the message, and ensure that the MTU size is... X-Git-Tag: v5.0.0~226 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7dc5bd24b9c334b9ec495976e17fb4f0a0da235c;p=thirdparty%2Fdhcpcd.git Simplify the logic of what to put in the message, and ensure that the MTU size is in the DISCOVER message. --- diff --git a/dhcp.c b/dhcp.c index 2658e89d..bfa5f262 100644 --- a/dhcp.c +++ b/dhcp.c @@ -747,6 +747,13 @@ encode_rfc1035(const char *src, uint8_t *dst, size_t len) return p - dst; } +#define PUTADDR(_type, _val) \ +{ \ + *p++ = _type; \ + *p++ = 4; \ + memcpy(p, &_val.s_addr, 4); \ + p += 4; \ +} ssize_t make_message(struct dhcp_message **message, const struct interface *iface, @@ -808,7 +815,16 @@ make_message(struct dhcp_message **message, *p++ = 1; *p++ = type; - if (type == DHCP_REQUEST) { + if (iface->clientid) { + *p++ = DHO_CLIENTID; + memcpy(p, iface->clientid, iface->clientid[0] + 1); + p += iface->clientid[0] + 1; + } + + if (type == DHCP_DISCOVER || + type == DHCP_INFORM || + type == DHCP_REQUEST) + { *p++ = DHO_MAXMESSAGESIZE; *p++ = 2; sz = get_mtu(iface->name); @@ -819,15 +835,7 @@ make_message(struct dhcp_message **message, sz = htons(sz); memcpy(p, &sz, 2); p += 2; - } - if (iface->clientid) { - *p++ = DHO_CLIENTID; - memcpy(p, iface->clientid, iface->clientid[0] + 1); - p += iface->clientid[0] + 1; - } - - if (type != DHCP_DECLINE && type != DHCP_RELEASE) { if (ifo->userclass[0]) { *p++ = DHO_USERCLASS; memcpy(p, ifo->userclass, ifo->userclass[0] + 1); @@ -840,39 +848,27 @@ make_message(struct dhcp_message **message, ifo->vendorclassid[0] + 1); p += ifo->vendorclassid[0] + 1; } - } - if (type == DHCP_DISCOVER || type == DHCP_REQUEST) { -#define PUTADDR(_type, _val) \ - { \ - *p++ = _type; \ - *p++ = 4; \ - memcpy(p, &_val.s_addr, 4); \ - p += 4; \ - } - if (lease->addr.s_addr && - lease->addr.s_addr != iface->addr.s_addr && - !IN_LINKLOCAL(ntohl(lease->addr.s_addr))) - { - PUTADDR(DHO_IPADDRESS, lease->addr); - if (lease->server.s_addr) - PUTADDR(DHO_SERVERID, lease->server); - } -#undef PUTADDR - if (ifo->leasetime != 0) { - *p++ = DHO_LEASETIME; - *p++ = 4; - ul = htonl(ifo->leasetime); - memcpy(p, &ul, 4); - p += 4; + if (type != DHCP_INFORM) { + if (lease->addr.s_addr && + lease->addr.s_addr != iface->addr.s_addr && + !IN_LINKLOCAL(ntohl(lease->addr.s_addr))) + { + PUTADDR(DHO_IPADDRESS, lease->addr); + if (lease->server.s_addr) + PUTADDR(DHO_SERVERID, lease->server); + } + + if (ifo->leasetime != 0) { + *p++ = DHO_LEASETIME; + *p++ = 4; + ul = htonl(ifo->leasetime); + memcpy(p, &ul, 4); + p += 4; + } } - } - if (type == DHCP_DISCOVER || - type == DHCP_INFORM || - type == DHCP_REQUEST) - { if (ifo->hostname[0]) { *p++ = DHO_HOSTNAME; memcpy(p, ifo->hostname, ifo->hostname[0] + 1); @@ -917,11 +913,11 @@ make_message(struct dhcp_message **message, has_option_mask(ifo->requestmask, opt->option))) continue; switch (opt->option) { - case DHO_RENEWALTIME: /* FALLTHROUGH */ - case DHO_REBINDTIME: - if (type == DHCP_INFORM) - continue; - break; + case DHO_RENEWALTIME: /* FALLTHROUGH */ + case DHO_REBINDTIME: + if (type == DHCP_INFORM) + continue; + break; } *p++ = opt->option; }