Autodetect if the clientid parameter is a valid mac address and treat it

accordingly.  Don't require an explicit parameter for it.
This commit is contained in:
Nicholas J. Kain 2011-07-03 05:45:05 -04:00
parent f08c174725
commit aa61d71f7d

View File

@ -1,5 +1,5 @@
/* ndhc.c - DHCP client /* ndhc.c - DHCP client
* Time-stamp: <2011-07-03 05:33:42 njk> * Time-stamp: <2011-07-03 05:43:58 njk>
* *
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com> * (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
* *
@ -127,17 +127,23 @@ static void signal_dispatch()
} }
} }
static int get_clientid_mac_string(char *str, size_t slen) static int is_string_hwaddr(char *str, size_t slen)
{ {
if (slen != 17) if (slen == 17 && str[2] == ':' && str[5] == ':' && str[8] == ':' &&
return 0; str[11] == ':' && str[14] == ':' &&
if (str[2] == ':' && str[5] == ':' && str[8] == ':' && str[11] == ':' &&
str[14] == ':' &&
isxdigit(str[0]) && isxdigit(str[1]) && isxdigit(str[3]) && isxdigit(str[0]) && isxdigit(str[1]) && isxdigit(str[3]) &&
isxdigit(str[4]) && isxdigit(str[6]) && isxdigit(str[7]) && isxdigit(str[4]) && isxdigit(str[6]) && isxdigit(str[7]) &&
isxdigit(str[9]) && isxdigit(str[10]) && isxdigit(str[12]) && isxdigit(str[9]) && isxdigit(str[10]) && isxdigit(str[12]) &&
isxdigit(str[13]) && isxdigit(str[15]) && isxdigit(str[16])) isxdigit(str[13]) && isxdigit(str[15]) && isxdigit(str[16])
{ )
return 1;
return 0;
}
static int get_clientid_mac_string(char *str, size_t slen)
{
if (!is_string_hwaddr(str, slen))
return 0;
client_config.clientid[0] = strtol(str, NULL, 16); client_config.clientid[0] = strtol(str, NULL, 16);
client_config.clientid[1] = strtol(str+3, NULL, 16); client_config.clientid[1] = strtol(str+3, NULL, 16);
client_config.clientid[2] = strtol(str+6, NULL, 16); client_config.clientid[2] = strtol(str+6, NULL, 16);
@ -146,8 +152,6 @@ static int get_clientid_mac_string(char *str, size_t slen)
client_config.clientid[5] = strtol(str+15, NULL, 16); client_config.clientid[5] = strtol(str+15, NULL, 16);
client_config.clientid[6] = '\0'; client_config.clientid[6] = '\0';
return 1; return 1;
}
return 0;
} }
static void do_work(void) static void do_work(void)
@ -205,7 +209,6 @@ int main(int argc, char **argv)
gid_t gid = 0; gid_t gid = 0;
static struct option arg_options[] = { static struct option arg_options[] = {
{"clientid", required_argument, 0, 'c'}, {"clientid", required_argument, 0, 'c'},
{"mac-clientid",required_argument, 0, 'm'},
{"foreground", no_argument, 0, 'f'}, {"foreground", no_argument, 0, 'f'},
{"background", no_argument, 0, 'b'}, {"background", no_argument, 0, 'b'},
{"pidfile", required_argument, 0, 'p'}, {"pidfile", required_argument, 0, 'p'},
@ -226,18 +229,15 @@ int main(int argc, char **argv)
while (1) { while (1) {
int option_index = 0; int option_index = 0;
c = getopt_long(argc, argv, "c:m:fbp:H:h:i:np:l:qr:u:C:vV:", arg_options, c = getopt_long(argc, argv, "c:fbp:H:h:i:np:l:qr:u:C:vV:", arg_options,
&option_index); &option_index);
if (c == -1) break; if (c == -1) break;
switch (c) { switch (c) {
case 'c': case 'c':
if (!get_clientid_mac_string(optarg, strlen(optarg)))
strlcpy(client_config.clientid, optarg, strlcpy(client_config.clientid, optarg,
sizeof client_config.clientid); sizeof client_config.clientid);
break;
case 'm':
if (!get_clientid_mac_string(optarg, strlen(optarg)))
suicide("clientid %s is not a valid ethernet MAC", optarg);
else else
client_config.clientid_mac = 1; client_config.clientid_mac = 1;
break; break;