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
* 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>
*
@ -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)
return 0;
if (str[2] == ':' && str[5] == ':' && str[8] == ':' && str[11] == ':' &&
str[14] == ':' &&
if (slen == 17 && str[2] == ':' && str[5] == ':' && str[8] == ':' &&
str[11] == ':' && str[14] == ':' &&
isxdigit(str[0]) && isxdigit(str[1]) && isxdigit(str[3]) &&
isxdigit(str[4]) && isxdigit(str[6]) && isxdigit(str[7]) &&
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[1] = strtol(str+3, NULL, 16);
client_config.clientid[2] = strtol(str+6, NULL, 16);
@ -147,8 +153,6 @@ static int get_clientid_mac_string(char *str, size_t slen)
client_config.clientid[6] = '\0';
return 1;
}
return 0;
}
static void do_work(void)
{
@ -205,7 +209,6 @@ int main(int argc, char **argv)
gid_t gid = 0;
static struct option arg_options[] = {
{"clientid", required_argument, 0, 'c'},
{"mac-clientid",required_argument, 0, 'm'},
{"foreground", no_argument, 0, 'f'},
{"background", no_argument, 0, 'b'},
{"pidfile", required_argument, 0, 'p'},
@ -226,18 +229,15 @@ int main(int argc, char **argv)
while (1) {
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);
if (c == -1) break;
switch (c) {
case 'c':
if (!get_clientid_mac_string(optarg, strlen(optarg)))
strlcpy(client_config.clientid, optarg,
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
client_config.clientid_mac = 1;
break;