From f4bbec8bf04aa22601cfdea2b9c7513cf01d9d79 Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Sun, 8 Jan 2017 20:53:15 +0545 Subject: Helper format_duid() for DUID input --- src/etc/inc/util.inc | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'src/etc/inc/util.inc') diff --git a/src/etc/inc/util.inc b/src/etc/inc/util.inc index 4d157d7..ad94157 100644 --- a/src/etc/inc/util.inc +++ b/src/etc/inc/util.inc @@ -2553,7 +2553,35 @@ function validateipaddr(&$addr, $type, $label, &$err_msg, $alias=false) { return false; } -/* returns true if $dhcp6duid is a valid duid entrry */ +/* format a string to look (more) like the expected DUID format: + * 1) Replace any "-" with ":" + * 2) If the user inputs 14 components, then add the expected "0e:00:" to the front. + * This is convenience, because the actual DUID (which is reported in logs) is the last 14 components. + * 3) If any components are input with just a single char (hex digit hopefully), put a "0" in front. + * + * The final result should be closer to: + * + * "0e:00:00:01:00:01:nn:nn:nn:nn:nn:nn:nn:nn:nn:nn" + * + * This function does not validate the input. is_duid() will do validation. +*/ +function format_duid($dhcp6duid) { + $formatted_dhcp6duid = strtolower(str_replace("-", ":", $dhcp6duid)); + $values = explode(":", $formatted_dhcp6duid); + if (count($values) == 14) { + $formatted_dhcp6duid = '0e:00:' . $formatted_dhcp6duid; + $values = explode(":", $formatted_dhcp6duid); + } + foreach ($values as $idx => $value) { + if (strlen($value) == 1) { + $values[$idx] = "0" . $value; + } + } + $formatted_dhcp6duid = implode(":", $values); + return $formatted_dhcp6duid; +} + +/* returns true if $dhcp6duid is a valid duid entry */ function is_duid($dhcp6duid) { $values = explode(":", $dhcp6duid); if (count($values) != 16 || strlen($dhcp6duid) != 47) { -- cgit v1.1