From: Roy Marples Date: Fri, 11 May 2007 10:51:32 +0000 (+0000) Subject: Allow -I to have an optional argument, when missing don't use DUID's X-Git-Tag: v3.2.3~263 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ff962a3c31431520b0e565a3755afa55afe1027f;p=thirdparty%2Fdhcpcd.git Allow -I to have an optional argument, when missing don't use DUID's --- diff --git a/dhcp.c b/dhcp.c index a0dade42..a4688be5 100644 --- a/dhcp.c +++ b/dhcp.c @@ -234,12 +234,11 @@ size_t send_message (const interface_t *iface, const dhcp_t *dhcp, } *p++ = DHCP_CLIENTID; - if (options->clientid[0]) { - l = strlen (options->clientid); - *p++ = l + 1; + if (options->clientid_len > 0) { + *p++ = options->clientid_len + 1; *p++ = 0; /* string */ - memcpy (p, options, l); - p += l; + memcpy (p, options, options->clientid_len); + p += options->clientid_len; #ifdef ENABLE_DUID } else if (iface->duid) { *p++ = iface->duid_length + 5; diff --git a/dhcpcd.8 b/dhcpcd.8 index 3ed93b4f..18781984 100644 --- a/dhcpcd.8 +++ b/dhcpcd.8 @@ -18,8 +18,8 @@ dhcpcd \%[\-s\ ipaddr] \%[\-t\ timeout] \%[\-u\ userClass] -\%[\-F\ none | ptr | both] -\%[\-I\ clientID] +\%[\-F\ [ none | ptr | both ] ] +\%[\-I\ [ clientID ] ] \%[interface] .in -.5i .SH DESCRIPTION @@ -217,7 +217,8 @@ Specifies the client identifier string. If not specified then will attempt to create a client identifier according to \fBRFC 4361\fR and store the DUID part in /var/lib/dhcpcd/dhcpcd.duid, otherwise .B dhcpcd -uses the MAC address of the network interface. +uses the MAC address of the network interface. If \fB-I\fR is not given +an option then we use the MAC address of the network interface. .TP .BI \-M Prevents diff --git a/dhcpcd.c b/dhcpcd.c index 3ac30448..4997e405 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -127,7 +127,7 @@ int main(int argc, char **argv) {"fqdn", optional_argument, NULL, 'F'}, {"nogateway", no_argument, NULL, 'G'}, {"sethostname", no_argument, NULL, 'H'}, - {"clientid", required_argument, NULL, 'I'}, + {"clientid", optional_argument, NULL, 'I'}, {"nomtu", no_argument, NULL, 'M'}, {"nontp", no_argument, NULL, 'N'}, {"nodns", no_argument, NULL, 'R'}, @@ -156,8 +156,9 @@ int main(int argc, char **argv) options.daemonise = true; options.timeout = DEFAULT_TIMEOUT; - while ((ch = getopt_long(argc, argv, "ac:dh:i:kl:m:nps:t:u:EF:GHI:MNRY", longopts, + while ((ch = getopt_long(argc, argv, "ac:dh:i:kl:m:nps:t:u:EF::GHI::MNRY", longopts, &option_index)) != -1) + { switch (ch) { case 0: if (longopts[option_index].flag) @@ -252,16 +253,19 @@ int main(int argc, char **argv) options.dolastlease = true; break; case 'F': - if (strncmp (optarg, "none", strlen (optarg)) == 0) - options.fqdn = FQDN_NONE; - else if (strncmp (optarg, "ptr", strlen (optarg)) == 0) - options.fqdn = FQDN_PTR; - else if (strncmp (optarg, "both", strlen (optarg)) == 0) + if (optarg) { + if (strncmp (optarg, "none", strlen (optarg)) == 0) + options.fqdn = FQDN_NONE; + else if (strncmp (optarg, "ptr", strlen (optarg)) == 0) + options.fqdn = FQDN_PTR; + else if (strncmp (optarg, "both", strlen (optarg)) == 0) + options.fqdn = FQDN_BOTH; + else { + logger (LOG_ERR, "invalid value `%s' for FQDN", optarg); + exit (EXIT_FAILURE); + } + } else options.fqdn = FQDN_BOTH; - else { - logger (LOG_ERR, "invalid value `%s' for FQDN", optarg); - exit (EXIT_FAILURE); - } break; case 'G': options.dogateway = false; @@ -270,12 +274,16 @@ int main(int argc, char **argv) options.dohostname = true; break; case 'I': - if (strlen (optarg) > CLIENT_ID_MAX_LEN) { - logger (LOG_ERR, "`%s' is too long for ClientID, max is %d", - optarg, CLIENT_ID_MAX_LEN); - exit (EXIT_FAILURE); + if (optarg && strlen(optarg) > 0) { + if (strlen (optarg) > CLIENT_ID_MAX_LEN) { + logger (LOG_ERR, "`%s' is too long for ClientID, max is %d", + optarg, CLIENT_ID_MAX_LEN); + exit (EXIT_FAILURE); + } else + strlcpy (options.clientid, optarg, sizeof (options.clientid)); + options.clientid_len = strlen (options.clientid); } else - strlcpy (options.clientid, optarg, sizeof (options.clientid)); + options.clientid_len = -1; break; case 'M': options.domtu = false; @@ -296,7 +304,7 @@ int main(int argc, char **argv) usage (); exit (EXIT_FAILURE); } - + } if (doversion) printf (""PACKAGE" "VERSION"\n"); diff --git a/dhcpcd.h b/dhcpcd.h index ff1086f2..a5be73b0 100644 --- a/dhcpcd.h +++ b/dhcpcd.h @@ -41,7 +41,9 @@ typedef struct options_t { char hostname[MAXHOSTNAMELEN]; int fqdn; char classid[CLASS_ID_MAX_LEN]; + int classid_len; char clientid[CLIENT_ID_MAX_LEN]; + int clientid_len; char userclass[USERCLASS_MAX_LEN]; int userclass_len; unsigned leasetime; diff --git a/duid.c b/duid.c index 0376d99c..78f02034 100644 --- a/duid.c +++ b/duid.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "config.h" #include "common.h" diff --git a/info.c b/info.c index 8b1eb7a7..e9f0a2f8 100644 --- a/info.c +++ b/info.c @@ -171,14 +171,16 @@ bool write_info(const interface_t *iface, const dhcp_t *dhcp, fprintf (f, "REBINDTIME='%u'\n", dhcp->rebindtime); fprintf (f, "INTERFACE='%s'\n", iface->name); fprintf (f, "CLASSID='%s'\n", cleanmetas (options->classid)); - if (options->clientid[0]) - fprintf (f, "CLIENTID='%s'\n", cleanmetas (options->clientid)); + if (options->clientid_len > 0) + fprintf (f, "CLIENTID='00:%s'\n", cleanmetas (options->clientid)); #ifdef ENABLE_DUID - else if (iface->duid_length > 0) { + else if (iface->duid_length > 0 && options->clientid_len != -1) { unsigned char duid[256]; unsigned char *p = duid; uint32_t ul; + *p++ = 255; + /* IAID is 4 bytes, so if the interface name is 4 bytes then use it */ if (strlen (iface->name) == 4) { memcpy (p, iface->name, 4); @@ -196,7 +198,8 @@ bool write_info(const interface_t *iface, const dhcp_t *dhcp, } #endif else - fprintf (f, "CLIENTID='%s'\n", hwaddr_ntoa (iface->hwaddr, iface->hwlen)); + fprintf (f, "CLIENTID='%.2X:%s'\n", iface->family, + hwaddr_ntoa (iface->hwaddr, iface->hwlen)); fprintf (f, "DHCPCHADDR='%s'\n", hwaddr_ntoa (iface->hwaddr, iface->hwlen)); #ifdef ENABLE_INFO_COMPAT