From: Roy Marples Date: Sun, 29 Jun 2008 18:28:19 +0000 (+0000) Subject: Save a few bytes by treating some strings like DHCP options, having the first byte... X-Git-Tag: v4.0.2~265 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=abc5592709affc9ec3f88ec786b624b69ee9f28f;p=thirdparty%2Fdhcpcd.git Save a few bytes by treating some strings like DHCP options, having the first byte as the length. --- diff --git a/client.c b/client.c index 1e6b0e50..59321090 100644 --- a/client.c +++ b/client.c @@ -465,7 +465,7 @@ client_setup(struct if_state *state, const struct options *options) struct interface *iface = state->interface; struct dhcp_lease *lease = &state->lease; struct in_addr addr; - size_t duid_len = 0; + size_t len = 0; #ifdef ENABLE_DUID unsigned char *duid = NULL; uint32_t ul; @@ -524,54 +524,55 @@ client_setup(struct if_state *state, const struct options *options) if (*options->clientid) { /* Attempt to see if the ClientID is a hardware address */ - iface->clientid_len = hwaddr_aton(NULL, options->clientid); - if (iface->clientid_len) { - iface->clientid = xmalloc(iface->clientid_len); - hwaddr_aton(iface->clientid, options->clientid); + if ((len = hwaddr_aton(NULL, options->clientid))) { + iface->clientid = xmalloc(len + 1); + iface->clientid[0] = len; + hwaddr_aton(iface->clientid + 1, options->clientid); } else { /* Nope, so mark it as-is */ - iface->clientid_len = strlen(options->clientid) + 1; - iface->clientid = xmalloc(iface->clientid_len); - *iface->clientid = '\0'; - memcpy(iface->clientid + 1, - options->clientid, iface->clientid_len - 1); + len = strlen(options->clientid) + 1; + iface->clientid = xmalloc(len + 1); + iface->clientid[0] = len; + iface->clientid[1] = 0; /* string */ + memcpy(iface->clientid + 2, options->clientid, len); } } else if (options->options & DHCPCD_CLIENTID) { #ifdef ENABLE_DUID if (options->options & DHCPCD_DUID) { duid = xmalloc(DUID_LEN); - duid_len = get_duid(duid, iface); - if (duid_len == 0) + if ((len = get_duid(duid, iface)) == 0) logger(LOG_ERR, "get_duid: %s", strerror(errno)); } - if (duid_len > 0) { + if (len > 0) { logger(LOG_INFO, "DUID = %s", - hwaddr_ntoa(duid, duid_len)); + hwaddr_ntoa(duid, len)); - iface->clientid_len = duid_len + 5; - iface->clientid = xmalloc(iface->clientid_len); - *iface->clientid = 255; /* RFC 4361 */ + len += 5; + iface->clientid = xmalloc(len); + iface->clientid[0] = len; + iface->clientid[1] = 255; /* RFC 4361 */ /* IAID is 4 bytes, so if the iface name is 4 bytes * use it */ if (strlen(iface->name) == 4) { - memcpy(iface->clientid + 1, iface->name, 4); + memcpy(iface->clientid + 2, iface->name, 4); } else { /* Name isn't 4 bytes, so use the index */ ul = htonl(if_nametoindex(iface->name)); - memcpy(iface->clientid + 1, &ul, 4); + memcpy(iface->clientid + 2, &ul, 4); } - memcpy(iface->clientid + 5, duid, duid_len); + memcpy(iface->clientid + 6, duid, len); free(duid); } #endif - if (duid_len == 0) { - iface->clientid_len = iface->hwlen + 1; - iface->clientid = xmalloc(iface->clientid_len); - *iface->clientid = iface->family; + if (len == 0) { + len = iface->hwlen + 1; + iface->clientid = xmalloc(len); + iface->clientid[0] = len; + iface->clientid[1] = iface->family; memcpy(iface->clientid + 1, iface->hwaddr, iface->hwlen); } } diff --git a/dhcp.c b/dhcp.c index c8abd0fb..e2e3a8f0 100644 --- a/dhcp.c +++ b/dhcp.c @@ -775,26 +775,23 @@ make_message(struct dhcp_message **message, p += 2; } - if (iface->clientid_len > 0) { + if (iface->clientid[0]) { *p++ = DHCP_CLIENTID; - *p++ = iface->clientid_len; - memcpy(p, iface->clientid, iface->clientid_len); - p+= iface->clientid_len; + memcpy(p, iface->clientid, iface->clientid[0] + 1); + p += iface->clientid[0] + 1; } if (type != DHCP_DECLINE && type != DHCP_RELEASE) { - if (options->userclass_len > 0) { + if (options->userclass[0]) { *p++ = DHCP_USERCLASS; - *p++ = options->userclass_len; - memcpy(p, &options->userclass, options->userclass_len); - p += options->userclass_len; + memcpy(p, options->userclass, options->userclass[0] + 1); + p += options->userclass[0] + 1; } - if (*options->classid > 0) { + if (options->classid[0]) { *p++ = DHCP_CLASSID; - *p++ = l = strlen(options->classid); - memcpy(p, options->classid, l); - p += l; + memcpy(p, options->classid, options->classid[0] + 1); + p += options->classid[0] + 1; } } diff --git a/dhcpcd.c b/dhcpcd.c index 2c125f27..693d3923 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -205,9 +205,7 @@ add_environ(struct options *options, const char *value, int uniq) static int parse_option(int opt, char *oarg, struct options *options) { - static int userclasses = 0; int i; - int j; char *p; size_t s; size_t olen; @@ -236,14 +234,16 @@ parse_option(int opt, char *oarg, struct options *options) case 'i': if (!oarg) { *options->classid = '\0'; - } else if (olen >= CLASS_ID_MAX_LEN) { + } else if (olen >= CLASSID_MAX_LEN) { logger(LOG_ERR, "`%s' too long for ClassID string, max is %d", - oarg, CLASS_ID_MAX_LEN); + oarg, CLASSID_MAX_LEN); return -1; - } else - strlcpy(options->classid, oarg, + } else { + options->classid[0] = strlen(oarg); + strlcpy(options->classid + 1, oarg, sizeof(options->classid)); + } break; case 'l': if (*oarg == '-') { @@ -314,19 +314,16 @@ parse_option(int opt, char *oarg, struct options *options) } break; case 'u': - j = 0; - for (i = 0; i < userclasses; i++) - j += (int)options->userclass[j] + 1; - if (j + 1 + olen >= USERCLASS_MAX_LEN) { + if (options->userclass[0] + olen + 1 >= USERCLASS_MAX_LEN) { logger(LOG_ERR, "userclass overrun, max is %d", USERCLASS_MAX_LEN); return -1; } - userclasses++; - memcpy(options->userclass + j + 1, oarg, olen); - options->userclass[j] = olen; - options->userclass_len += olen + 1; + p = options->userclass + options->userclass[0] + 1; + *p++ = olen; + memcpy(p, oarg, olen); + options->userclass[0] += olen + 1; break; case 'A': options->options &= ~DHCPCD_ARP; @@ -371,18 +368,20 @@ parse_option(int opt, char *oarg, struct options *options) break; case 'I': if (oarg) { - if (olen >= CLIENT_ID_MAX_LEN) { + if (olen >= CLIENTID_MAX_LEN) { logger(LOG_ERR, "`%s' is too long for" " ClientID, max is %d", - oarg, CLIENT_ID_MAX_LEN); + oarg, CLIENTID_MAX_LEN); return -1; } - if (strlcpy(options->clientid, oarg, - sizeof(options->clientid)) == 0) { + if (strlcpy(options->clientid + 1, oarg, + CLIENTID_MAX_LEN) == 0) + { /* empty string disabled duid */ options->options &= ~DHCPCD_DUID; options->options &= ~DHCPCD_CLIENTID; - } + } else + options->clientid[0] = strlen(oarg); } else { options->clientid[0] = '\0'; options->options &= ~DHCPCD_DUID; @@ -448,8 +447,8 @@ main(int argc, char **argv) options = xzalloc(sizeof(*options)); options->script = SCRIPT; - snprintf(options->classid, CLASS_ID_MAX_LEN, "%s %s", - PACKAGE, VERSION); + options->classid[0] = snprintf(options->classid + 1, CLASSID_MAX_LEN, + "%s %s", PACKAGE, VERSION); options->options |= DHCPCD_GATEWAY | DHCPCD_DAEMONISE | DHCPCD_CLIENTID; #ifdef ENABLE_ARP diff --git a/dhcpcd.h b/dhcpcd.h index ac1a651a..7b1ee396 100644 --- a/dhcpcd.h +++ b/dhcpcd.h @@ -41,8 +41,8 @@ #define DEFAULT_TIMEOUT 30 #define DEFAULT_LEASETIME 3600 /* 1 hour */ -#define CLASS_ID_MAX_LEN 48 -#define CLIENT_ID_MAX_LEN 48 +#define CLASSID_MAX_LEN 48 +#define CLIENTID_MAX_LEN 48 #define USERCLASS_MAX_LEN 255 #ifdef THERE_IS_NO_FORK @@ -72,12 +72,11 @@ struct options { char interface[IF_NAMESIZE]; char hostname[MAXHOSTNAMELEN]; int fqdn; - char classid[CLASS_ID_MAX_LEN]; - char clientid[CLIENT_ID_MAX_LEN]; - char userclass[USERCLASS_MAX_LEN]; + char classid[CLASSID_MAX_LEN + 1]; + char clientid[CLIENTID_MAX_LEN + 1]; + char userclass[USERCLASS_MAX_LEN + 1]; uint8_t reqmask[256 / 8]; uint8_t nomask[256 / 8]; - size_t userclass_len; uint32_t leasetime; time_t timeout; int metric; diff --git a/net.h b/net.h index aef09e33..5f98b526 100644 --- a/net.h +++ b/net.h @@ -119,7 +119,6 @@ struct interface time_t start_uptime; unsigned char *clientid; - size_t clientid_len; }; uint32_t get_netmask(uint32_t);