[#23] Move to NetAddr::MAC for better handling of MACs

This commit is contained in:
Oliver Gorwits
2014-05-24 13:12:32 +01:00
parent 980aaa4535
commit 562b3d26db
12 changed files with 51 additions and 32 deletions
+1
View File
@@ -4,6 +4,7 @@
* Increase default snmptimeout from 1 to 3 seconds
* Add documentation note about non-standard pg port
* [#23] Move to NetAddr::MAC for better handling of MACs
[BUG FIXES]
+1 -1
View File
@@ -33,7 +33,7 @@ requires 'MCE' => 1.408;
requires 'Net::Domain' => 1.23;
requires 'Net::DNS' => 0.72;
requires 'Net::LDAP' => 0;
requires 'Net::MAC' => 2.103622;
requires 'NetAddr::MAC' => 0.82;
requires 'Net::NBName' => 0.26;
requires 'NetAddr::IP' => 4.068;
requires 'Opcode' => 1.07;
+3 -3
View File
@@ -10,7 +10,7 @@ use NetAddr::IP::Lite ':lower';
use List::MoreUtils ();
use Encode;
use Try::Tiny;
use Net::MAC;
use NetAddr::MAC;
use base 'Exporter';
our @EXPORT = ();
@@ -713,8 +713,8 @@ sub store_neighbors {
$device->ip, $remote_ip, $port, $remote_id;
if (!defined $neigh) {
my $mac = Net::MAC->new(mac => $remote_id, 'die' => 0, verbose => 0);
if (not $mac->get_error) {
my $mac = NetAddr::MAC->new(mac => $remote_id);
if (not $mac->errstr) {
$neigh = $devices->single({mac => $remote_id});
}
}
+3 -3
View File
@@ -7,7 +7,7 @@ package App::Netdisco::DB::Result::Node;
use strict;
use warnings;
use Net::MAC;
use NetAddr::MAC;
use base 'DBIx::Class::Core';
__PACKAGE__->table("node");
@@ -175,10 +175,10 @@ sub time_last_stamp { return (shift)->get_column('time_last_stamp') }
=head2 net_mac
Returns the C<mac> column instantiated into a L<Net::MAC> object.
Returns the C<mac> column instantiated into a L<NetAddr::MAC> object.
=cut
sub net_mac { return Net::MAC->new(mac => (shift)->mac) }
sub net_mac { return NetAddr::MAC->new(mac => (shift)->mac) }
1;
@@ -7,7 +7,7 @@ package App::Netdisco::DB::Result::NodeIp;
use strict;
use warnings;
use Net::MAC;
use NetAddr::MAC;
use base 'DBIx::Class::Core';
__PACKAGE__->table("node_ip");
@@ -221,10 +221,10 @@ sub time_last_stamp { return (shift)->get_column('time_last_stamp') }
=head2 net_mac
Returns the C<mac> column instantiated into a L<Net::MAC> object.
Returns the C<mac> column instantiated into a L<NetAddr::MAC> object.
=cut
sub net_mac { return Net::MAC->new(mac => (shift)->mac) }
sub net_mac { return NetAddr::MAC->new(mac => (shift)->mac) }
1;
@@ -7,6 +7,8 @@ package App::Netdisco::DB::Result::NodeNbt;
use strict;
use warnings;
use NetAddr::MAC;
use base 'DBIx::Class::Core';
__PACKAGE__->table("node_nbt");
__PACKAGE__->add_columns(
@@ -176,10 +178,10 @@ sub time_last_stamp { return (shift)->get_column('time_last_stamp') }
=head2 net_mac
Returns the C<mac> column instantiated into a L<Net::MAC> object.
Returns the C<mac> column instantiated into a L<NetAddr::MAC> object.
=cut
sub net_mac { return Net::MAC->new(mac => (shift)->mac) }
sub net_mac { return NetAddr::MAC->new(mac => (shift)->mac) }
1;
@@ -262,8 +262,8 @@ Compared to the current Netdisco, the handler routines are very small. This is
because (a) they don't include any HTML - this is delegated to a template, and
(b) they don't include an SQL - this is delegated to DBIx::Class. Small
routines are more manageable, and easier to maintain. You'll also notice use
of modules such as L<Net::MAC> and L<NetAddr::IP::Lite> to simplify and make
more robust the handling of data.
of modules such as L<NetAddr::MAC> and L<NetAddr::IP::Lite> to simplify and
make more robust the handling of data.
In fact, many sections of the web application have been factored out into
separate Plugin modules. For more information see the
+4 -4
View File
@@ -3,7 +3,7 @@ package App::Netdisco::Util::Node;
use Dancer qw/:syntax :script/;
use Dancer::Plugin::DBIC 'schema';
use Net::MAC;
use NetAddr::MAC;
use App::Netdisco::Util::Permission 'check_acl';
use base 'Exporter';
@@ -66,18 +66,18 @@ MAC address does not belong to an interface on any known Device
sub check_mac {
my ($device, $node, $port_macs) = @_;
my $mac = Net::MAC->new(mac => $node, 'die' => 0, verbose => 0);
my $mac = NetAddr::MAC->new(mac => $node);
$port_macs ||= {};
# incomplete MAC addresses (BayRS frame relay DLCI, etc)
if ($mac->get_error) {
if ($mac->errstr) {
debug sprintf ' [%s] check_mac - mac [%s] malformed - skipping',
$device->ip, $node;
return 0;
}
else {
# lower case, hex, colon delimited, 8-bit groups
$node = lc $mac->as_IEEE;
$node = lc $mac->as_microsoft;
}
# broadcast MAC addresses
+21 -7
View File
@@ -55,6 +55,11 @@ hook 'before' => sub {
{ name => 'n_archived', label => 'Archived Data', default => '' },
]);
# not stored in the cookie - global defaults
params->{'age_num'} ||= 3;
params->{'age_unit'} ||= 'months';
params->{'mac_format'} ||= 'microsoft';
return unless (request->path eq uri_for('/device')->path
or index(request->path, uri_for('/ajax/content/device')->path) == 0);
@@ -101,15 +106,10 @@ hook 'before' => sub {
if $col->{default} eq 'on';
}
# not stored in the cookie
params->{'age_num'} ||= 3;
params->{'age_unit'} ||= 'months';
params->{'mac_format'} ||= 'IEEE';
if (param('reset')) {
params->{'age_num'} = 3;
params->{'age_unit'} = 'months';
params->{'mac_format'} = 'IEEE';
params->{'mac_format'} = 'microsoft';
# nuke the port params cookie
cookie('nd_ports-form' => '', expires => '-1 day');
@@ -129,7 +129,21 @@ hook 'before_template' => sub {
$tokens->{device_ports}->query_param($key, params->{$key});
}
# for Net::MAC method
# for NetAddr::MAC method
$tokens->{mac_formats} = {
basic => 'Basic',
# bpr => 'BPR',
cisco => 'Cisco',
ieee => 'IEEE',
# ipv6 => 'IPv6 Suffix',
microsoft => 'Microsoft',
singledash => 'Single Dash',
sun => 'Sun',
tokenring => 'Token Ring',
# eui48 => 'EUI48',
# eui64 => 'EUI64',
};
$tokens->{mac_format_call} = 'as_'. params->{'mac_format'}
if params->{'mac_format'};
@@ -6,7 +6,7 @@ use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use NetAddr::IP::Lite ':lower';
use Net::MAC ();
use NetAddr::MAC ();
use App::Netdisco::Web::Plugin;
use App::Netdisco::Util::Web 'sql_match';
@@ -22,7 +22,7 @@ ajax '/ajax/content/search/node' => require_login sub {
my $agenot = param('age_invert') || '0';
my ( $start, $end ) = param('daterange') =~ /(\d+-\d+-\d+)/gmx;
my $mac = Net::MAC->new(mac => $node, 'die' => 0, verbose => 0);
my $mac = NetAddr::MAC->new(mac => $node);
my @active = (param('archived') ? () : (-bool => 'active'));
my @times = ();
@@ -48,7 +48,7 @@ ajax '/ajax/content/search/node' => require_login sub {
my @where_mac =
($using_wildcards ? \['me.mac::text ILIKE ?', $likeval]
: ($mac->get_error ? \'0=1' : ('me.mac' => $mac->as_IEEE)) );
: ($mac->errstr ? \'0=1' : ('me.mac' => $mac->as_microsoft)) );
my $sightings = schema('netdisco')->resultset('Node')
->search({-and => [@where_mac, @active, @times]}, {
+3 -2
View File
@@ -125,8 +125,9 @@
<li>
<em class="muted">MAC address format:</em><br/>
<select id="nd_mac-format" name="mac_format">
[% FOREACH format IN [ 'IEEE', 'Cisco', 'Microsoft', 'Sun' ] %]
<option[% ' selected="selected"' IF params.mac_format == format %]>[% format %]</option>
[% FOREACH format IN mac_formats.keys.sort %]
<option[% ' selected="selected"' IF params.mac_format == format %]
value="[% format %]">[% mac_formats.$format %]</option>
[% END %]
</select>
</li>
+3 -2
View File
@@ -55,8 +55,9 @@
<div class="clearfix">
<em class="muted">MAC address format:</em><br/>
<select id="nd_node-mac-format" name="mac_format">
[% FOREACH format IN [ 'IEEE', 'Cisco', 'Microsoft', 'Sun' ] %]
<option[% ' selected="selected"' IF params.mac_format == format %]>[% format %]</option>
[% FOREACH format IN mac_formats.keys.sort %]
<option[% ' selected="selected"' IF params.mac_format == format %]
value="[% format %]">[% mac_formats.$format %]</option>
[% END %]
</select>
</div>