mac-patches

Netdisco doesn't handle some forms of MAC address correctly. In particular, some 3Com gear shows MAC addresses in the form 0800-2b01-0203, which have to be converted to another format when pasting into the Netdisco search box.

The following patch for netdisco.pm 1.90 fixes the is_mac() subroutine to recognise this format:

--- netdisco.pm.1.90 Fri Jun 13 15:52:31 2008

+++ netdisco.pm Fri Jun 13 15:54:11 2008

@@ -759,6 +759,7 @@

08002b:010203

08002b-010203

0800.2b01.0203

+ 0800-2b01-0203

08-00-2b-01-02-03

08:00:2b:01:02:03

@@ -771,8 +772,9 @@

#'08002b:010203', '08002b-010203'

return 1 if ($mac =~ /^${hex}{6}[:-]{1}${hex}{6}$/);

- #'0800.2b01.0203'

+ #'0800.2b01.0203', '0800-2b01-0203'

return 1 if ($mac =~ /^${hex}{4}\.${hex}{4}\.${hex}{4}$/);

+ return 1 if ($mac =~ /^${hex}{4}-${hex}{4}-${hex}{4}$/);

# '08-00-2b-01-02-03','08:00:2b:01:02:03'

return 1 if ($mac =~ /^${hex}{2}-${hex}{2}-${hex}{2}-${hex}{2}-${hex}{2}-${hex}{2}$/);

return 1 if ($mac =~ /^${hex}{2}:${hex}{2}:${hex}{2}:${hex}{2}:${hex}{2}:${hex}{2}$/);

However, that isn't the whole story, as this routine only returns a boolean value to confirm if a string looks like a reasonable MAC address. We still need to ensure that the form of MAC address will be recognised as such by the SQL query. In lieu of postgres understanding more formats (backend/utils/adt/mac.c?), we must mangle it ourselves to make it recognisable as such before passing it to SQL. For this particular format, this patch to node.html 1.28 will do it:

--- node.html.1.28 Fri Dec 21 21:50:40 2007

+++ node.html Mon Jun 16 11:17:36 2008

@@ -289,6 +290,13 @@

if ($node =~ /^${hex}{2} ${hex}{2} ${hex}{2} ${hex}{2} ${hex}{2} ${hex}{2}$/){

$node =~ s/\s+/:/g;

}

+

+ # Match on a particular format of MAC address and convert to a

+ # format that is_mac() and SQL will recognise as being acceptable

+ if ($node =~ /^${hex}{4}-${hex}{4}-${hex}{4}$/){

+ $node =~ s/-/./g;

+ }

+

my $wildhex = "[0-9a-fA-F?]";

my $is_partial_mac = ($node =~ /^([?*]+)?(${wildhex}{2}:)+(${wildhex}{2})([?*]+)?$/);

my $could_be_partial_mac = ($node =~ /^${hex}+$/ && length($node) < 13 && (length($node) % 2) == 0);

How To Use

    1. Patch the source!

Notes

The real work on searching on MAC address is done in node.html. This appears to use roughly the following logic:

    1. strip surrounding whitespace from the node search term;

    2. where the node is a sequence of six pairs of hex digits separated by spaces, replace the spaces by colons;

    3. determine if the node search term is deliberately specified as a partial MAC address by looking for sequences of hex digits with metacharacters '?' and '*' (ie, deliberately wildcarded by the user in the input field);

    4. determine if the node search term could be a partial MAC address by looking for an even-numbered sequence of hex characters of length less than 13;

    5. set $is_mac true if the node search term if any of the following are true:

      1. is_mac() returns true (as fixed by the patch above);

      2. it is a partial MAC (according to the test above);

      3. it could be a partial MAC (according to the test above);

    6. if $is_mac is true, then prepare to search by MAC address

      1. if it could be a partial MAC, then turn it into a partial MAC by adding wildcard characters

      2. set the SQL 'where' clause to the MAC address if it is complete, or to a SQL wildcard match if not

      3. perform the lookup using add_node_ips, which performs a SQL search using the 'where' clause computed

    1. node search term was a not MAC address, so do other stuff for Netbios search etc

IIt may be cleaner to deal with this by passing a MAC address into a subroutine that cleans out all punctuation, then tests the result to see if it is a sequence of hexadecimalcharacters, and returns it in a canonical form that is suitable for use in the SQL query.

Files

None.

Status

Submitted to netdisco-users list 16 Jun 2008; Oliver said he would incorporate.