Net-MAC-2.103622000755001750001750 011506447773 13076 5ustar00oliveroliver000000000000README000644001750001750 2572511506447773 14071 0ustar00oliveroliver000000000000Net-MAC-2.103622NAME Net::MAC - Perl extension for representing and manipulating MAC addresses VERSION version 2.103622 SYNOPSIS use Net::MAC; my $mac = Net::MAC->new('mac' => '08:20:00:AB:CD:EF'); # Example: convert to a different MAC address format (dotted-decimal) my $dec_mac = $mac->convert( 'base' => 10, # convert from base 16 to base 10 'bit_group' => 8, # octet grouping 'delimiter' => '.' # dot-delimited ); print "$dec_mac\n"; # Should print 8.32.0.171.205.239 # Example: find out whether a MAC is base 16 or base 10 my $base = $mac->get_base(); if ($base == 16) { print "$mac is in hexadecimal format\n"; } elsif ($base == 10) { print "$mac is in decimal format\n"; } else { die "This MAC is neither base 10 nor base 16"; } DESCRIPTION This is a module that allows you to - store a MAC address in a Perl object - find out information about a stored MAC address - convert a MAC address into a specified format - easily compare two MAC addresses for string or numeric equality There are quite a few different ways that MAC addresses may be represented in textual form. The most common is arguably colon-delimited octets in hexadecimal form. When working with Cisco devices, however, you are more likely to encounter addresses that are dot-delimited 16-bit groups in hexadecimal form. In the Windows world, addresses are usually dash-delimited octets in hexadecimal form. MAC addresses in a Sun ethers file are usually non-zero-padded, colon-delimited hexadecimal octets. And sometimes, you come across dot-delimited octets in decimal form (certain Cisco SNMP MIBS actually use this). Hence the need for a common way to represent and manipulate MAC addresses in Perl. There is a surprising amount of complexity involved in converting MAC addresses between types. This module does not attempt to understand all possible ways of representing a MAC address in a string, though most of the common ways of representing MAC addresses are supported. METHODS new() method (constructor) The new() method creates a new Net::MAC object. Possible arguments are mac a string representing a MAC address base a number corresponding to the numeric base of the MAC possible values: 10 16 delimiter the delimiter in the MAC address string from above possible values: : - . space bit_group the number of bits between each delimiter possible values: 8 16 48 zero_padded whether bit groups have leading zero characters (Net::MAC only allows zero-padding for bit groups of 8 bits) possible values: 0 1 format the name of a MAC address format specification which takes the place of the base,delimiter,bit_group and zero_padded options above verbose write informational messages (useful for debugging) possible values: 0 1 die die() on invalid MAC address (default is to die on invalid MAC) possible values: 0 1 (default is 1) When the new() method is called with a 'mac' argument and nothing else, the object will attempt to auto-discover metadata like bit grouping, number base, delimiter, etc. If the MAC is in an invalid or unknown format, the object will call the croak() function. If you don't want the object to croak(), you can give the new() method a die argument, such as: my $m_obj = Net::MAC->new('mac' => '000adf012345', 'die' => 0); There are cases where the auto-discovery will not be able to guess the numeric base of a MAC. If this happens, try giving the new() method a hint, like so: # Example: this MAC is actually in decimal-dotted notation, not hex my $mac = Net::MAC->new('mac' => '10.0.0.12.14.8', 'base' => 10); This is necessary for cases like the one above, where the class has no way of knowing that an address is decimal instead of hexadecimal. If you have installed a custom MAC address format into the class (see below) then you can also pass the "format" option as a hint: my $mac = Net::MAC->new('mac' => 'ab01~ab01~ab01', 'format' => 'My_Format'); class methods set_format_for() When discovering MAC address formats, and converting between different formats (using "convert" or "as_*") the module can use predefined common formats or you can install your own for local circumstances. For example consider a fictional device which uses MAC addresses formatted like "ab01~ab01~ab01", which would otherwise not be understood. You can install a new Format for this address style: Net::MAC->set_format_for( 'My_Format_Name' => { base => 16, bit_group => 16, delimiter => '~', }); Now when using either the "format" option to "new()", or the "convert()" or "as_*" methods, the module will recognise this new format "My_Format_Name". The Hashref supplied can include any of the standard options for formats as listed elsewhere in this documentation. my $mac = Net::MAC->new('mac' => 'ab01~ab01~ab01', 'format' => 'My_Format_Name'); Custom formats sharing the same name as one shipping with the module (such as "Cisco") will override that built-in format. accessor methods get_mac() method Returns the MAC address stored in the object. get_base() method Returns the numeric base of the MAC address. There are two possible return values: 16 hexadecimal (common) 10 decimal (uncommon) get_delimiter() method Returns the delimiter, if any, in the specified MAC address. A valid delimiter matches the following regular expression: /\:|\-|\.|\s/ In other words, either a colon, a dash, a dot, or a space. If there is no delimiter, this method will return the undefined value (undef). If an invalid delimiter is found (like an asterisk or something), the object will call the croak() function. get_bit_group() method Returns the number of bits between the delimiters. A MAC address is a 48 bit address, usually delimited into 8 bit groupings (called octets), i.e. 08:20:00:AB:CD:EF Sometimes, MAC addresses are specified with fewer than 5 delimiters, or even no delimiters at all: 0820.00ab.cdef # get_bit_group() returns 16 082000abcdef # get_bit_group() returns 48, no delimiters at all get_zero_padded() method Returns a boolean value indicating whether or not the bit groups are zero-padded. A return value of 0 (false) means that the bit groups are not zero-padded, and a return value of 1 (true) means that they are zero-padded: 00.80.02.ac.4f.ff # get_zero_padded() returns 1 0:80:2:ac:4f:ff # get zero_padded() returns 0 0.125.85.122.155.64 # get_zero_padded() returns 0 Net::MAC only allows bit groups of 8 bits to be zero-padded. convert() method Convert an already-defined Net::MAC object into a different MAC address format. With this function you can change the delimiter, the bit grouping, or the numeric base. # Example: convert to a different MAC address format (dotted-decimal) my $new_mac_obj = $existing_mac_obj->convert( 'base' => 16, # convert to base 16, if necessary 'bit_group' => 16, # 16 bit grouping 'delimiter' => '.' # dot-delimited ); Note that if any of the above arguments are not provided, they will be set to the following default values: base 16 bit_group 8 (i.e. a delimiter will be used) delimiter : Conversion to common formats The most common formats have shortcut conversion methods that can be used instead of the convert() method with its many options. as_Cisco() method Cisco routers seem to usually represent MAC addresses in hexadecimal, dot-delimited, 16 bit groups. my $mac = Net::MAC->new(mac => '00-02-03-AA-AB-FF'); my $cisco_mac = $mac->as_Cisco(); print "$cisco_mac"; # should print 0002.03aa.abff as_IEEE() method The IEEE 802 2001 specification represents MAC addresses in hexadecimal, colon-delimited, upper case, 8 bit groups. my $mac = Net::MAC->new(mac => '00-02-03-AA-AB-FF'); my $IEEE_mac = Net::MAC->as_IEEE(); print "$IEEE_mac"; # should print 00:02:03:AA:AB:FF as_Microsoft() method Microsoft usually represents MAC addresses in hexadecimal, dash delimited, upper case, 8 bit groups. my $mac = Net::MAC->new(mac => '00:02:03:AA:AB:FF'); my $microsoft_mac = $mac->as_Microsoft(); print "$microsoft_mac"; # should print 00-02-03-AA-AB-FF as_Sun() method Sun represents MAC addresses in hexadecimal, colon-delimited, non-zero-padded, lower case, 8 bit groups. my $mac = Net::MAC->new(mac => '00-02-03-AA-AB-FF'); my $sun_mac = $mac->as_Sun(); print "$sun_mac"; # should print 0:2:3:aa:ab:ff Stringification The stringification operator "" has been overloaded to allow for the meaningful use of the instance variable in a string. my $mac = Net::MAC->new(mac => '00:0a:23:4f:ff:ef'); print "object created for MAC address $mac"; # Should print: # object created for MAC address 00:0a:23:4f:ff:ef MAC address comparison The Perl operators 'eq' and 'ne' (string comparison) and '==' '!=' (numeric comparison) have been overloaded to allow simple, meaningful comparisons of two MAC addresses. Example (two MAC addresses numerically identical but in different formats): my $d = Net::MAC->new(mac => '0.8.1.9.16.16', base => 10); my $h = Net::MAC->new(mac => '00:08:01:0A:10:10', base => 16); if ($d == $h) { print "$d and $h are numerically equal"; } if ($d ne $h) { print " but $d and $h are not the same string"; } BUGS Malformed MAC addresses Net::MAC can't handle MAC addresses where whole leading zero octets are omitted. Example: 7.122.32.41.5 (should be 0.7.122.32.41.5) Arguably, that's their problem and not mine, but maybe someday I'll get around to supporting that case as well. Case is not preserved Net::MAC doesn't reliably preserve case in a MAC address. I might add a flag to the new() and convert() methods to do this. I might not. Case is however altered when using the as_foo() formatted output methods. SEE ALSO Net::MacMap Net::MAC::Vendor MAINTAINER Oliver Gorwits CONTRIBUTORS Oliver Gorwits, Robin Crook, Kevin Brintnall AUTHOR Karl Ward COPYRIGHT AND LICENSE This software is Copyright (c) 2010 by Karl Ward . This is free software, licensed under: The GNU General Public License, Version 2, June 1991 Changes000644001750001750 176711506447773 14464 0ustar00oliveroliver000000000000Net-MAC-2.1036222.103622 2010-12-28 21:00:37 Europe/London - Fix for converting between two as_ formats, the delimiter discovery is a bit more relaxed 2.103621 2010-12-28 14:36:42 Europe/London - Add custom format support (rt.cpan #63017) 1.103620 2010-12-28 12:55:41 Europe/London - Port to Dist::Zilla 1.6 2010-06-02 10:49 GMT - Fix test for changes to Test::Simple (RT.cpan#57867) 1.5 2008-11-08 17:42 GMT - Move to Module::Install - Add basic pod test 1.4 2008-05-01 14:53 GMT+1 - Fix rt.cpan#34680 Unspecified delimiter to convert() when bit_group() is not 48 1.3 2008-03-30 19:12 GMT+1 - Added Changes file. - Added INSTALL file. - POD fixes. - Remove TAB chars in source and run through perltidy -pbp - Merged bugfix patch from Kevin Brintnall (RT.cpan#34476) (maintainer release: Oliver Gorwits) 1.2 2007-09-08 - Checkpoint for Changes file; undocumented. t000755001750001750 011506447773 13262 5ustar00oliveroliver000000000000Net-MAC-2.103622pod.t000644001750001750 21411506447773 14345 0ustar00oliveroliver000000000000Net-MAC-2.103622/t#!perl -T use Test::More; eval "use Test::Pod 1.14"; plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; all_pod_files_ok(); LICENSE000644001750001750 3526411506447773 14215 0ustar00oliveroliver000000000000Net-MAC-2.103622This software is Copyright (c) 2010 by Karl Ward . This is free software, licensed under: The GNU General Public License, Version 2, June 1991 The General Public License (GPL) Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 675 Mass Ave, Cambridge, MA 02139, USA. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS META.yml000644001750001750 125711506447773 14434 0ustar00oliveroliver000000000000Net-MAC-2.103622--- abstract: 'Perl extension for representing and manipulating MAC addresses ' author: - 'Karl Ward ' build_requires: Test::More: 0 configure_requires: ExtUtils::MakeMaker: 6.31 dynamic_config: 0 generated_by: 'Dist::Zilla version 4.200000, CPAN::Meta::Converter version 2.102400' license: gpl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Net-MAC requires: Carp: 0 integer: 0 overload: 0 perl: 5.006000 resources: bugtracker: https://rt.cpan.org/Public/Dist/Display.html?Name=Net-MAC homepage: https://github.com/ollyg/Net-MAC/wiki repository: git://github.com/ollyg/Net-MAC.git version: 2.103622 MANIFEST000644001750001750 36111506447773 14267 0ustar00oliveroliver000000000000Net-MAC-2.103622Changes LICENSE MANIFEST META.json META.yml Makefile.PL README examples/gen_macs.pl lib/Net/MAC.pm t/100_base10_macs.pl t/100_base16_macs.pl t/10_convert_args.t t/20_custom_format.t t/Net-MAC.t t/bulk10.t t/bulk16.t t/pod-coverage.t t/pod.t META.json000644001750001750 234011506447773 14576 0ustar00oliveroliver000000000000Net-MAC-2.103622{ "abstract" : "Perl extension for representing and manipulating MAC addresses ", "author" : [ "Karl Ward " ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 4.200000, CPAN::Meta::Converter version 2.102400", "license" : [ "gpl_2" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Net-MAC", "prereqs" : { "configure" : { "requires" : { "ExtUtils::MakeMaker" : "6.31" } }, "runtime" : { "requires" : { "Carp" : 0, "integer" : 0, "overload" : 0, "perl" : "5.006000" } }, "test" : { "requires" : { "Test::More" : 0 } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://rt.cpan.org/Public/Dist/Display.html?Name=Net-MAC" }, "homepage" : "https://github.com/ollyg/Net-MAC/wiki", "repository" : { "type" : "git", "url" : "git://github.com/ollyg/Net-MAC.git", "web" : "https://github.com/ollyg/Net-MAC" } }, "version" : "2.103622" } bulk10.t000644001750001750 113511506447773 14704 0ustar00oliveroliver000000000000Net-MAC-2.103622/t# $Id$ # This is a Test::More test script for Net::MAC. This script should be # runnable with `make test'. use Test::More tests => 401; #use Test::More qw(no_plan); # FIXME BEGIN { use_ok('Net::MAC') }; # Bulk testing operator overloading (double quotes, numeric/string equality) require 't/100_base10_macs.pl'; foreach my $mac_key (keys %$mac) { #diag("mac $mac_key"); my $mac_obj = Net::MAC->new(mac => $mac_key, %{$mac->{mac_key}}); ok($mac_key eq "$mac_obj"); ok("$mac_obj" eq $mac_key); ok($mac_obj->get_internal_mac() == $mac_obj); ok($mac_obj == $mac_obj->get_internal_mac()); } bulk16.t000644001750001750 113511506447773 14712 0ustar00oliveroliver000000000000Net-MAC-2.103622/t# $Id$ # This is a Test::More test script for Net::MAC. This script should be # runnable with `make test'. use Test::More tests => 401; #use Test::More qw(no_plan); # FIXME BEGIN { use_ok('Net::MAC') }; # Bulk testing operator overloading (double quotes, numeric/string equality) require 't/100_base16_macs.pl'; foreach my $mac_key (keys %$mac) { #diag("mac $mac_key"); my $mac_obj = Net::MAC->new(mac => $mac_key, %{$mac->{mac_key}}); ok($mac_key eq "$mac_obj"); ok("$mac_obj" eq $mac_key); ok($mac_obj->get_internal_mac() == $mac_obj); ok($mac_obj == $mac_obj->get_internal_mac()); } Net-MAC.t000644001750001750 1224411506447773 14755 0ustar00oliveroliver000000000000Net-MAC-2.103622/t# $Id$ # Before `make install' is performed this script should be runnable with # `make test'. After `make install' it should work as `perl Net-MAC.t' ######################### use Test::More tests => 150; BEGIN { use_ok('Net::MAC') }; # Creating base 16 Net::MAC objects my @macs = (); my $hex_mac = Net::MAC->new('mac' => '08:20:00:AB:CD:EF'); ok($hex_mac); is($hex_mac->get_mac(), '08:20:00:AB:CD:EF'); is($hex_mac->get_bit_group(), 8); is($hex_mac->get_base(), 16); is($hex_mac->get_delimiter(), ':'); #ok($hex_mac->get_internal_mac() eq '082000ABCDEF'); is($hex_mac->get_internal_mac(), '082000abcdef'); ## check AUTOLOAD as_* methods ## also, check that the sub gets installed properly by running it twice for my $round (1,2) { is($hex_mac->as_Cisco, '0820.00ab.cdef', "as_Cisco (round $round)"); is($hex_mac->as_IEEE, '08:20:00:AB:CD:EF', "as_IEEE (round $round)"); is($hex_mac->as_Microsoft, '08-20-00-AB-CD-EF', "as_Microsoft (round $round)"); is($hex_mac->as_Sun, '8:20:0:ab:cd:ef', "as_Sun (round $round)"); } # Converting a base 16 MAC to a base 10 MAC my $dec_mac = $hex_mac->convert( 'base' => 10, 'bit_group' => 8, 'delimiter' => '.' ); ok($dec_mac); is($dec_mac->get_mac(), '8.32.0.171.205.239'); is($dec_mac->get_bit_group(), 8); is($dec_mac->get_base(), 10); # Converting a base 10 MAC to a base 16 MAC my $hex_mac_2 = $dec_mac->convert( 'base' => 16, 'bit_group' => 16, 'delimiter' => ':' ); ok($hex_mac_2); is($hex_mac_2->get_mac(), '0820:00ab:cdef'); is($hex_mac_2->get_bit_group(), 16); is($hex_mac_2->get_base(), 16); is($hex_mac_2->get_internal_mac(), '082000abcdef'); # Creating a base 10 Net::MAC object my $dec_mac_2 = Net::MAC->new( 'mac' => '0.7.14.6.43.3', 'base' => 10 ); ok($dec_mac_2); is($dec_mac_2->get_mac(), '0.7.14.6.43.3'); is($dec_mac_2->get_bit_group(), 8); is($dec_mac_2->get_base(), 10); is($dec_mac_2->get_internal_mac(), '00070e062b03'); my $hex_mac_3 = $dec_mac_2->convert( 'base' => 16, 'bit_group' => 16, 'delimiter' => '.' ); ok($hex_mac_3); is($hex_mac_3->get_mac(), '0007.0e06.2b03'); is($hex_mac_3->get_bit_group(), 16); is($hex_mac_3->get_base(), 16); is($hex_mac_3->get_internal_mac(), '00070e062b03'); # Creating a base 16 dash delimited Net::MAC object my $hex_mac_4 = Net::MAC->new('mac' => '12-23-34-45-a4-ff'); ok($hex_mac_4); is($hex_mac_4->get_mac(), '12-23-34-45-a4-ff'); is($hex_mac_4->get_bit_group(), 8); is($hex_mac_4->get_base(), 16); is($hex_mac_4->get_internal_mac(),'12233445a4ff'); my (%delim_mac) = ( '.' => ['08.00.20.ab.cd.ef', '8.0.20.ab.cd.ef', '08.00.20.AB.CD.EF', '122.255.0.16.1.1'], ':' => ['08:00:20:ab:cd:ef', '8:0:20:ab:cd:ef', '08:00:20:AB:CD:EF'], '-' => ['08-00-20-ab-cd-ef', '8-0-20-ab-cd-ef', '08-00-20-AB-CD-EF'], ' ' => ['08 00 20 ab cd ef', '8 0 20 ab cd ef', '08 00 20 AB CD EF'], 'none' => ['080020abcdef', '080020ABCDEF'], ); foreach my $delim (keys %delim_mac) { foreach my $test_mac (@{$delim_mac{$delim}}) { my $mac = Net::MAC->new('mac' => $test_mac); is($mac, $test_mac, "test with delimiter '$delim'"); my $test_delim = $mac->get_delimiter(); if ($delim eq 'none') { #diag "null delimiter"; ok(!defined($test_delim), 'delimiter \"none\"'); } else { is($test_delim, $delim, "delimiter '$delim'"); } } } my (%base_mac) = ( '10' => ['122.255.0.16.1.1', '0.0.90.12.255.255', '8.0.20.55.1.1'], '16' => ['08.00.20.ab.cd.ef', '8:0:20:ab:cd:ef', '8:0:20:AB:CD:EF'] ); foreach my $base (keys %base_mac) { foreach my $test_mac_2 (@{$base_mac{$base}}) { my $mac = Net::MAC->new( 'mac' => $test_mac_2, 'base' => $base ); is($mac, $test_mac_2, "mac correct for base '$base'"); my $mac_base = $mac->get_base(); is($mac_base, $base, "base $base"); } } my (%bit_mac) = ( 48 => ['8080abe4c9ff', '8080ABE4C9FF', 'ABCDEFABCDEF', '0123456789ab'], 16 => ['8080.abe4.c9ff', '8080.ABE4.C9FF', 'ABCD.EFAB.CDEF', '0123.4567.89ab'], 8 => ['80.80.ab.e4.c9.ff', '80:80:ab:e4:c9:ff', '80-80-ab-e4-c9-ff', '80 80 AB E4 C9 FF'] ); foreach my $bit (keys %bit_mac) { foreach my $test_mac_3 (@{$bit_mac{$bit}}) { my $mac = Net::MAC->new('mac' => $test_mac_3); is($mac, $test_mac_3, "mac correct for grouping '$bit'"); my $mac_bit = $mac->get_bit_group(); is($mac_bit, $bit, "bit grouping correct $bit"); } } # Test against a battery of base 16 MAC addresses my @mac = ('08.00.20.ab.cd.ef', '8:0:20:ab:cd:ef', '8:0:20:AB:CD:EF', '8080abe4c9ff', '8080ABE4C9FF', 'ABCDEFABCDEF', '0123456789ab', '8080.abe4.c9ff', '8080.ABE4.C9FF', 'ABCD.EFAB.CDEF', '0123.4567.89ab', '80.80.ab.e4.c9.ff', '80:80:ab:e4:c9:ff', '80-80-ab-e4-c9-ff', '80 80 AB E4 C9 FF'); foreach my $test_mac (@mac) { ok(Net::MAC->new('mac' => $test_mac)); } no warnings; my @invalid_mac = (':::::', ' : : : : : ', '..', '\s\s\s\s\s', '-----', '---', ' - - ', ' ', '99.6', '888:76.12', '1', '000000000000000000111111', '256.256.256.256.256.256', '128.123.123.234.345.456', 'abcdefghijkl'); foreach my $invalid_mac (@invalid_mac) { my $no_die = Net::MAC->new(mac => $invalid_mac, die => 0); ok($no_die, "testing 'die' attribute for invalid mac '$invalid_mac'"); ok($no_die->get_error(), "testing get_error() method for invalid mac '$invalid_mac'"); } use warnings; Makefile.PL000644001750001750 211511506447773 15127 0ustar00oliveroliver000000000000Net-MAC-2.103622 use strict; use warnings; BEGIN { require 5.006000; } use ExtUtils::MakeMaker 6.31; my %WriteMakefileArgs = ( 'ABSTRACT' => 'Perl extension for representing and manipulating MAC addresses ', 'AUTHOR' => 'Karl Ward ', 'BUILD_REQUIRES' => { 'Test::More' => '0' }, 'CONFIGURE_REQUIRES' => { 'ExtUtils::MakeMaker' => '6.31' }, 'DISTNAME' => 'Net-MAC', 'EXE_FILES' => [], 'LICENSE' => 'gpl', 'NAME' => 'Net::MAC', 'PREREQ_PM' => { 'Carp' => '0', 'integer' => '0', 'overload' => '0' }, 'VERSION' => '2.103622', 'test' => { 'TESTS' => 't/*.t' } ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.56) } ) { my $br = delete $WriteMakefileArgs{BUILD_REQUIRES}; my $pp = $WriteMakefileArgs{PREREQ_PM}; for my $mod ( keys %$br ) { if ( exists $pp->{$mod} ) { $pp->{$mod} = $br->{$mod} if $br->{$mod} > $pp->{$mod}; } else { $pp->{$mod} = $br->{$mod}; } } } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); Net000755001750001750 011506447773 14313 5ustar00oliveroliver000000000000Net-MAC-2.103622/libMAC.pm000644001750001750 7146011506447773 15440 0ustar00oliveroliver000000000000Net-MAC-2.103622/lib/Net# Net::MAC - Perl extension for representing and manipulating MAC addresses # Copyright (C) 2005-2008 Karl Ward # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA package Net::MAC; BEGIN { $Net::MAC::VERSION = '2.103622'; } use 5.006000; use strict; use Carp; use warnings; use overload '""' => sub { return $_[0]->get_mac(); }, '==' => \&_compare_value, '!=' => \&_compare_value_ne, 'eq' => \&_compare_string, 'ne' => \&_compare_string_ne; our $AUTOLOAD; # Constructor. sub new { my ( $class, %arg ) = @_; my ($self) = {}; # Anonymous hash bless( $self, $class ); # Now the hash is an object if (%arg) { $self->_init(%arg); } $self->_discover(); return ($self); } { # Closure for class data and class methods # # CLASS DATA # # These are the valid private attributes of the object, with their # default values, if applicable. my %_attrs = ( '_mac' => undef, '_base' => 16, '_delimiter' => ':', '_bit_group' => 48, '_zero_padded' => 1, '_case' => 'upper', # FIXME: does IEEE specify upper? '_groups' => undef, '_internal_mac' => undef, '_die' => 1, # die() on invalid MAC address format '_error' => undef, '_verbose' => 0 ); # new formats supplied by the user are stored here my %_user_format_for = (); # Preset formats we will accept for use by ->convert, via ->as_foo my %_format_for = ( Cisco => { base => 16, bit_group => 16, delimiter => '.', }, IEEE => { base => 16, bit_group => 8, delimiter => ':', zero_padded => 1, case => 'upper', }, Microsoft => { base => 16, bit_group => 8, delimiter => '-', case => 'upper', }, Sun => { base => 16, bit_group => 8, delimiter => ':', zero_padded => 0, case => 'lower' } ); # # CLASS METHODS # # Returns a copy of the instance. sub _clone { my ($self) = @_; my ($clone) = {%$self}; # No need for deep copying here. bless( $clone, ref $self ); return ($clone); } # Verify that an attribute is valid (called by the AUTOLOAD sub) sub _accessible { my ( $self, $name ) = @_; if ( exists $_attrs{$name} ) { #$self->verbose("attribute $name is valid"); return 1; } else { return 0; } } # Initialize the object (only called by the constructor) sub _init { my ( $self, %arg ) = @_; if ( defined $arg{'verbose'} ) { $self->{'_verbose'} = $arg{'verbose'}; delete $arg{'verbose'}; } # Set the '_die' attribute to default at the first $self->_default('die'); # passed a "format" as shorthand for the specific vars if (exists $arg{'format'}) { my $f; $f = $_format_for{$arg{'format'}} if exists $_format_for{$arg{'format'}}; $f = $_user_format_for{$arg{'format'}} if exists $_user_format_for{$arg{'format'}}; %arg = (%arg, %$f) if (defined $f and ref $f eq 'HASH'); delete $arg{'format'}; } foreach my $key ( keys %_attrs ) { $key =~ s/^_+//; if ( ( defined $arg{$key} ) && ( $self->_accessible("_$key") ) ) { $self->verbose("setting \"$key\" to \"$arg{$key}\""); $self->{"_$key"} = $arg{$key}; } } my ($mesg) = "initialized object into class " . ref($self); $self->verbose($mesg); return (1); } # Set an attribute to its default value sub _default { my ( $self, $key ) = @_; if ( $self->_accessible("_$key") && $_attrs{"_$key"} ) { $self->verbose( "setting \"$key\" to default value \"" . $_attrs{"_$key"} . "\"" ); $self->{"_$key"} = $_attrs{"_$key"}; return (1); } else { $self->verbose("no default value for attribute \"$key\""); return (0); # FIXME: die() here? } } sub _format { my ( $self, $identifier ) = @_; # built-ins first if (exists $_format_for{$identifier} and ref $_format_for{$identifier} eq 'HASH') { return %{$_format_for{$identifier}}; } # then user-supplied if (exists $_user_format_for{$identifier} and ref $_user_format_for{$identifier} eq 'HASH') { return %{$_user_format_for{$identifier}}; } return (undef); } # program in a new custom MAC address format supplied by the user sub _set_format_for { my ($self, $identifier, $format) = @_; croak "missing identifier for custom format\n" unless defined $identifier and length $identifier; croak "missing HASH ref custom format\n" unless defined $format and ref $format eq 'HASH'; $_user_format_for{$identifier} = $format; } } # End closure # program in a new custom MAC address format supplied by the user sub set_format_for { goto &_set_format_for } # Automatic accessor methods via AUTOLOAD # See Object Oriented Perl, 3.3, Damian Conway sub Net::MAC::AUTOLOAD { no strict 'refs'; my ( $self, $value ) = @_; if ( ( $AUTOLOAD =~ /.*::get(_\w+)/ ) && ( $self->_accessible($1) ) ) { #$self->verbose("get$1 method"); my $attr_name = $1; *{$AUTOLOAD} = sub { return $_[0]->{$attr_name} }; return ( $self->{$attr_name} ); } if ( $AUTOLOAD =~ /.*::set(_\w+)/ && $self->_accessible($1) ) { my $attr_name = $1; *{$AUTOLOAD} = sub { $_[0]->{$attr_name} = $_[1]; return; }; $self->{$1} = $value; return; } if ( $AUTOLOAD =~ /.*::as_(\w+)/ && $_[0]->_format($1) ) { my $fmt = $1; *{$AUTOLOAD} = sub { return $_[0]->convert( $_[0]->_format($fmt) ) }; return ( $self->convert( $_[0]->_format($fmt) ) ); } croak "No such method: $AUTOLOAD"; } # Just for kicks, report an error if we know of one. sub DESTROY { my ($self) = @_; my $error = $self->get_error(); if ($error) { $self->verbose("Net::MAC detected an error: $error"); return (1); } } # Discover the metadata for this MAC, using hints if necessary sub _discover { my ($self) = @_; my $mac = $self->get_mac(); # Check for undefined MAC or invalid characters if ( !( defined $mac ) ) { $self->error( "discovery of MAC address metadata failed, no MAC address supplied" ); } elsif ( !( $mac =~ /[a-fA-F0-9]/ ) ) { # Doesn't have hex/dec numbers $self->error( "discovery of MAC address metadata failed, no meaningful characters in $mac" ); } # XXX: this isn't a very effective check for anything elsif ( $mac =~ /[g-z]/i ) { $self->error( "discovery of MAC address metadata failed, invalid characters in MAC address \"$mac\"" ); } unless ( $self->get_delimiter() ) { $self->_find_delimiter(); } unless ( $self->get_base() ) { $self->_find_base(); } unless ( $self->get_bit_group() ) { $self->_find_bit_group(); } unless ( $self->get_zero_padded() ) { $self->_find_zero_padded(); } $self->_write_internal_mac(); $self->_check_internal_mac(); return (1); } # Find the delimiter for this MAC address sub _find_delimiter { my ($self) = @_; my $mac = $self->get_mac(); # XXX: why not just look for any non hexadec char? if ( $mac =~ m/([^a-zA-Z0-9]+)/ ) { # Found a delimiter $self->set_delimiter($1); $self->verbose("setting attribute \"delimiter\" to \"$1\""); return (1); } else { $self->set_delimiter(undef); $self->verbose("setting attribute \"delimiter\" to undef"); return (1); } $self->error("internal Net::MAC failure for MAC \"$mac\""); return (0); # Bizarre failure if we get to this line. } # Find the numeric base for this MAC address sub _find_base { my ($self) = @_; my $mac = $self->get_mac(); # XXX this will fail for 00:00:00:00:00:00 ?? if ( $mac =~ /[a-fA-F]/ ) { # It's hexadecimal $self->set_base(16); return (1); } my @groups = split( /[^a-zA-Z0-9]+/, $mac ); my $is_decimal = 0; foreach my $group (@groups) { if ( length($group) == 3 ) { # It's decimal, sanity check it $is_decimal = 1; if ( $group > 255 ) { $self->error("invalid decimal MAC \"$mac\""); return (0); } } } if ($is_decimal) { $self->set_base(10); return (1); } # There are no obvious indicators, so we'll default the value $self->_default('base'); return (1); } # Find the bit grouping for this MAC address sub _find_bit_group { my ($self) = @_; my $mac = $self->get_mac(); if ( $mac =~ m/([^a-zA-Z0-9]+)/ ) { # Found a delimiter my $delimiter = ($1 eq ' ' ? '\s' : '\\'. $1); my @groups = split( /$delimiter/, $mac ); if ( ( @groups > 3 ) && ( @groups % 2 ) ) { $self->error("invalid MAC address format: $mac"); } elsif (@groups) { use integer; my $n = @groups; my $t_bg = 48 / $n; if ( ( $t_bg == 8 ) || ( $t_bg == 16 ) ) { $self->set_bit_group($t_bg); $self->verbose( "setting attribute \"bit_group\" to \"$t_bg\""); return (1); } else { $self->error("invalid MAC address format: $mac"); return (0); } } } else { # No delimiter, bit grouping is 48 bits # Sanity check the length of the MAC address in characters if ( length($mac) != 12 ) { $self->error( "invalid MAC format, not 12 characters in hexadecimal MAC \"$mac\"" ); return (0); } else { $self->_default('bit_group'); return (1); } } # If we get here the MAC is invalid or there's a bug in Net::MAC $self->error("invalid MAC address format \"$mac\""); } # FIXME: untested # Find whether this MAC address has zero-padded bit groups sub _find_zero_padded { my ($self) = @_; # Zero-padding is only allowed for 8 bit grouping unless ( $self->get_bit_group() && ( $self->get_bit_group() == 8 ) ) { return (0); # False } my $delimiter = $self->get_delimiter(); if ( $delimiter eq ' ' ) { $delimiter = '\s'; } my @groups = split( /\Q$delimiter\E/, $self->get_mac() ); foreach my $group (@groups) { if ( $group =~ /^0./ ) { $self->set_zero_padded(1); return (1); # True, zero-padded group. } } $self->set_zero_padded(0); return (0); # False, if we got this far. } # Write an internal representation of the MAC address. # This is mainly useful for conversion between formats. sub _write_internal_mac { my ($self) = @_; my $mac = $self->get_mac(); $mac =~ s/(\w)/\l$1/g; #my @groups = $self->get_groups(); my @groups; my $delimiter = $self->get_delimiter(); if ($delimiter) { $delimiter = ($delimiter eq ' ' ? '\s' : '\\'. $delimiter); @groups = split( /$delimiter/, $mac ); } else { @groups = $mac; } # Hex base if ( ( defined $self->get_base() ) && ( $self->get_base() == 16 ) ) { my $bit_group; if ( defined $self->get_bit_group() ) { $bit_group = $self->get_bit_group(); } else { $bit_group = 48; } my ($chars) = $bit_group / 4; my ($internal_mac); foreach my $element (@groups) { my $format = '%0' . $chars . 's'; $internal_mac .= sprintf( $format, $element ); } $self->set_internal_mac($internal_mac); return (1); } else { # Decimal base if ( @groups == 6 ) { # Decimal addresses can only have octet grouping my @hex_groups; foreach my $group (@groups) { my $hex = sprintf( "%02x", $group ); push( @hex_groups, $hex ); } my $imac = join( '', @hex_groups ); $self->set_internal_mac($imac); return (1); } else { $self->error("unsupported MAC address format \"$mac\""); return (0); } } $self->error("internal Net::MAC failure for MAC \"$mac\""); return (0); # FIXME: die() here? } # Check the internal MAC address for errors (last check) sub _check_internal_mac { my ($self) = @_; if ( !defined( $self->get_internal_mac() ) ) { my $mac = $self->get_mac(); $self->error("invalid MAC address \"$mac\""); return (0); } elsif ( length( $self->get_internal_mac() ) != 12 ) { my $mac = $self->get_mac(); $self->error("invalid MAC address \"$mac\""); return (0); } else { return (1) } } # Convert a MAC address object into a different format sub convert { my ( $self, %arg ) = @_; my $imac = $self->get_internal_mac(); my @groups; my $bit_group = $arg{'bit_group'} || 8; # not _default value my $offset = 0; use integer; my $size = $bit_group / 4; no integer; while ( $offset < length($imac) ) { my $group = substr( $imac, $offset, $size ); if ( ( $bit_group == 8 ) && ( exists $arg{zero_padded} ) && ( $arg{zero_padded} == 0 ) ) { $group =~ s/^0//; } push( @groups, $group ); $offset += $size; } # Convert to base 10 if necessary if ( ( exists $arg{'base'} ) && ( $arg{'base'} == 10 ) ) { # Convert to decimal base my @dec_groups; foreach my $group (@groups) { my $dec_group = hex($group); push( @dec_groups, $dec_group ); } @groups = @dec_groups; } my $mac_string; if ( exists $arg{delimiter} ) { #warn "\nconvert delimiter $arg{'delimiter'}\n"; #my $delimiter = $arg{'delimiter'}; #$delimiter =~ s/(:|\-|\.)/\\$1/; $mac_string = join( $arg{'delimiter'}, @groups ); #warn "\nconvert groups @groups\n"; } elsif ($bit_group != 48) { # use default delimiter $mac_string = join( ':', @groups ); } else { $mac_string = join( '', @groups ); } if ( exists $arg{case} && $arg{case} =~ /^(upper|lower)$/ ) { for ($mac_string) { $_ = $arg{case} eq 'upper' ? uc : lc; } } # Construct the argument list for the new Net::MAC object $arg{'mac'} = $mac_string; # foreach my $test (keys %arg) { # warn "\nconvert arg $test is $arg{$test}\n"; # } my $new_mac = Net::MAC->new(%arg); return ($new_mac); } # Overloading the == operator (numerical comparison) sub _compare_value { my ( $arg_1, $arg_2, $reversed ) = @_; my ( $mac_1, $mac_2 ); if ( UNIVERSAL::isa( $arg_2, 'Net::MAC' ) ) { $mac_2 = $arg_2->get_internal_mac(); } else { my $temp = Net::MAC->new( mac => $arg_2 ); $mac_2 = $temp->get_internal_mac(); } $mac_1 = $arg_1->get_internal_mac(); if ( $mac_1 eq $mac_2 ) { return (1); } else { return (0); } } # Overloading the != operator (numeric comparison) sub _compare_value_ne { my ( $arg_1, $arg_2 ) = @_; if ( $arg_1 == $arg_2 ) { return (0); } else { return (1); } } # Overloading the eq operator (string comparison) sub _compare_string { my ( $arg_1, $arg_2, $reversed ) = @_; my ( $mac_1, $mac_2 ); if ( UNIVERSAL::isa( $arg_2, 'Net::MAC' ) ) { $mac_2 = $arg_2->get_mac(); } else { my $temp = Net::MAC->new( mac => $arg_2 ); $mac_2 = $temp->get_mac(); } $mac_1 = $arg_1->get_mac(); if ( $mac_1 eq $mac_2 ) { return (1); } else { return (0); } } # Overloading the ne operator (string comparison) sub _compare_string_ne { my ( $arg_1, $arg_2 ) = @_; if ( $arg_1 eq $arg_2 ) { return (0); } else { return (1); } } # Print verbose messages about internal workings of this class sub verbose { my ( $self, $message ) = @_; if ( ( defined($message) ) && ( $self->{'_verbose'} ) ) { chomp($message); print "$message\n"; } } # carp(), croak(), or ignore errors, depending on the attributes of the object. # If the object is configured to stay alive despite errors, this method will # store the error message in the '_error' attribute of the object, accessible # via the get_error() method. sub error { my ( $self, $message ) = @_; if ( $self->get_die() ) { # die attribute is set to 1 croak $message; } elsif ( $self->get_verbose() ) { # die attribute is set to 0 $self->set_error($message); carp $message; # Be verbose, carp() the message } else { # die attribute is set to 0, verbose is set to 0 $self->set_error($message); # Just store the error } return (1); } 1; # Necessary for usage statement # ABSTRACT: Perl extension for representing and manipulating MAC addresses __END__ =pod =head1 NAME Net::MAC - Perl extension for representing and manipulating MAC addresses =head1 VERSION version 2.103622 =head1 SYNOPSIS use Net::MAC; my $mac = Net::MAC->new('mac' => '08:20:00:AB:CD:EF'); # Example: convert to a different MAC address format (dotted-decimal) my $dec_mac = $mac->convert( 'base' => 10, # convert from base 16 to base 10 'bit_group' => 8, # octet grouping 'delimiter' => '.' # dot-delimited ); print "$dec_mac\n"; # Should print 8.32.0.171.205.239 # Example: find out whether a MAC is base 16 or base 10 my $base = $mac->get_base(); if ($base == 16) { print "$mac is in hexadecimal format\n"; } elsif ($base == 10) { print "$mac is in decimal format\n"; } else { die "This MAC is neither base 10 nor base 16"; } =head1 DESCRIPTION This is a module that allows you to - store a MAC address in a Perl object - find out information about a stored MAC address - convert a MAC address into a specified format - easily compare two MAC addresses for string or numeric equality There are quite a few different ways that MAC addresses may be represented in textual form. The most common is arguably colon-delimited octets in hexadecimal form. When working with Cisco devices, however, you are more likely to encounter addresses that are dot-delimited 16-bit groups in hexadecimal form. In the Windows world, addresses are usually dash-delimited octets in hexadecimal form. MAC addresses in a Sun ethers file are usually non-zero-padded, colon-delimited hexadecimal octets. And sometimes, you come across dot-delimited octets in decimal form (certain Cisco SNMP MIBS actually use this). Hence the need for a common way to represent and manipulate MAC addresses in Perl. There is a surprising amount of complexity involved in converting MAC addresses between types. This module does not attempt to understand all possible ways of representing a MAC address in a string, though most of the common ways of representing MAC addresses are supported. =head1 METHODS =head2 new() method (constructor) The new() method creates a new Net::MAC object. Possible arguments are mac a string representing a MAC address base a number corresponding to the numeric base of the MAC possible values: 10 16 delimiter the delimiter in the MAC address string from above possible values: : - . space bit_group the number of bits between each delimiter possible values: 8 16 48 zero_padded whether bit groups have leading zero characters (Net::MAC only allows zero-padding for bit groups of 8 bits) possible values: 0 1 format the name of a MAC address format specification which takes the place of the base,delimiter,bit_group and zero_padded options above verbose write informational messages (useful for debugging) possible values: 0 1 die die() on invalid MAC address (default is to die on invalid MAC) possible values: 0 1 (default is 1) When the new() method is called with a 'mac' argument and nothing else, the object will attempt to auto-discover metadata like bit grouping, number base, delimiter, etc. If the MAC is in an invalid or unknown format, the object will call the croak() function. If you don't want the object to croak(), you can give the new() method a die argument, such as: my $m_obj = Net::MAC->new('mac' => '000adf012345', 'die' => 0); There are cases where the auto-discovery will not be able to guess the numeric base of a MAC. If this happens, try giving the new() method a hint, like so: # Example: this MAC is actually in decimal-dotted notation, not hex my $mac = Net::MAC->new('mac' => '10.0.0.12.14.8', 'base' => 10); This is necessary for cases like the one above, where the class has no way of knowing that an address is decimal instead of hexadecimal. If you have installed a custom MAC address format into the class (see below) then you can also pass the C option as a hint: my $mac = Net::MAC->new('mac' => 'ab01~ab01~ab01', 'format' => 'My_Format'); =head2 class methods =head3 set_format_for() When discovering MAC address formats, and converting between different formats (using C or C) the module can use predefined common formats or you can install your own for local circumstances. For example consider a fictional device which uses MAC addresses formatted like C, which would otherwise not be understood. You can install a new Format for this address style: Net::MAC->set_format_for( 'My_Format_Name' => { base => 16, bit_group => 16, delimiter => '~', }); Now when using either the C option to C, or the C or C methods, the module will recognise this new format C. The Hashref supplied can include any of the standard options for formats as listed elsewhere in this documentation. my $mac = Net::MAC->new('mac' => 'ab01~ab01~ab01', 'format' => 'My_Format_Name'); Custom formats sharing the same name as one shipping with the module (such as C) will override that built-in format. =head2 accessor methods =head3 get_mac() method Returns the MAC address stored in the object. =head3 get_base() method Returns the numeric base of the MAC address. There are two possible return values: 16 hexadecimal (common) 10 decimal (uncommon) =head3 get_delimiter() method Returns the delimiter, if any, in the specified MAC address. A valid delimiter matches the following regular expression: /\:|\-|\.|\s/ In other words, either a colon, a dash, a dot, or a space. If there is no delimiter, this method will return the undefined value (undef). If an invalid delimiter is found (like an asterisk or something), the object will call the croak() function. =head3 get_bit_group() method Returns the number of bits between the delimiters. A MAC address is a 48 bit address, usually delimited into 8 bit groupings (called octets), i.e. 08:20:00:AB:CD:EF Sometimes, MAC addresses are specified with fewer than 5 delimiters, or even no delimiters at all: 0820.00ab.cdef # get_bit_group() returns 16 082000abcdef # get_bit_group() returns 48, no delimiters at all =head3 get_zero_padded() method Returns a boolean value indicating whether or not the bit groups are zero-padded. A return value of 0 (false) means that the bit groups are not zero-padded, and a return value of 1 (true) means that they are zero-padded: 00.80.02.ac.4f.ff # get_zero_padded() returns 1 0:80:2:ac:4f:ff # get zero_padded() returns 0 0.125.85.122.155.64 # get_zero_padded() returns 0 Net::MAC only allows bit groups of 8 bits to be zero-padded. =head2 convert() method Convert an already-defined Net::MAC object into a different MAC address format. With this function you can change the delimiter, the bit grouping, or the numeric base. # Example: convert to a different MAC address format (dotted-decimal) my $new_mac_obj = $existing_mac_obj->convert( 'base' => 16, # convert to base 16, if necessary 'bit_group' => 16, # 16 bit grouping 'delimiter' => '.' # dot-delimited ); Note that if any of the above arguments are not provided, they will be set to the following default values: base 16 bit_group 8 (i.e. a delimiter will be used) delimiter : =head2 Conversion to common formats The most common formats have shortcut conversion methods that can be used instead of the convert() method with its many options. =head3 as_Cisco() method Cisco routers seem to usually represent MAC addresses in hexadecimal, dot-delimited, 16 bit groups. my $mac = Net::MAC->new(mac => '00-02-03-AA-AB-FF'); my $cisco_mac = $mac->as_Cisco(); print "$cisco_mac"; # should print 0002.03aa.abff =head3 as_IEEE() method The IEEE 802 2001 specification represents MAC addresses in hexadecimal, colon-delimited, upper case, 8 bit groups. my $mac = Net::MAC->new(mac => '00-02-03-AA-AB-FF'); my $IEEE_mac = Net::MAC->as_IEEE(); print "$IEEE_mac"; # should print 00:02:03:AA:AB:FF =head3 as_Microsoft() method Microsoft usually represents MAC addresses in hexadecimal, dash delimited, upper case, 8 bit groups. my $mac = Net::MAC->new(mac => '00:02:03:AA:AB:FF'); my $microsoft_mac = $mac->as_Microsoft(); print "$microsoft_mac"; # should print 00-02-03-AA-AB-FF =head3 as_Sun() method Sun represents MAC addresses in hexadecimal, colon-delimited, non-zero-padded, lower case, 8 bit groups. my $mac = Net::MAC->new(mac => '00-02-03-AA-AB-FF'); my $sun_mac = $mac->as_Sun(); print "$sun_mac"; # should print 0:2:3:aa:ab:ff =head2 Stringification The stringification operator "" has been overloaded to allow for the meaningful use of the instance variable in a string. my $mac = Net::MAC->new(mac => '00:0a:23:4f:ff:ef'); print "object created for MAC address $mac"; # Should print: # object created for MAC address 00:0a:23:4f:ff:ef =head2 MAC address comparison The Perl operators 'eq' and 'ne' (string comparison) and '==' '!=' (numeric comparison) have been overloaded to allow simple, meaningful comparisons of two MAC addresses. Example (two MAC addresses numerically identical but in different formats): my $d = Net::MAC->new(mac => '0.8.1.9.16.16', base => 10); my $h = Net::MAC->new(mac => '00:08:01:0A:10:10', base => 16); if ($d == $h) { print "$d and $h are numerically equal"; } if ($d ne $h) { print " but $d and $h are not the same string"; } =head1 BUGS =head2 Malformed MAC addresses Net::MAC can't handle MAC addresses where whole leading zero octets are omitted. Example: 7.122.32.41.5 (should be 0.7.122.32.41.5) Arguably, that's their problem and not mine, but maybe someday I'll get around to supporting that case as well. =head2 Case is not preserved Net::MAC doesn't reliably preserve case in a MAC address. I might add a flag to the new() and convert() methods to do this. I might not. Case is however altered when using the as_foo() formatted output methods. =head1 SEE ALSO Net::MacMap Net::MAC::Vendor =head1 MAINTAINER Oliver Gorwits =head1 CONTRIBUTORS Oliver Gorwits, Robin Crook, Kevin Brintnall =head1 AUTHOR Karl Ward =head1 COPYRIGHT AND LICENSE This software is Copyright (c) 2010 by Karl Ward . This is free software, licensed under: The GNU General Public License, Version 2, June 1991 =cut pod-coverage.t000644001750001750 31611506447773 16141 0ustar00oliveroliver000000000000Net-MAC-2.103622/t#!perl -T use Test::More; eval "use Test::Pod::Coverage 1.04"; plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; all_pod_coverage_ok( {also_private => [ qr/./, ]} ); 10_convert_args.t000644001750001750 117011506447773 16601 0ustar00oliveroliver000000000000Net-MAC-2.103622/t# $Id$ use Test::More tests => 5; #use Test::More qw(no_plan); BEGIN { use_ok('Net::MAC') }; my $macaddr = '01:ab:01:ab:01:ab'; my $mac = eval{ Net::MAC->new(mac => $macaddr) }; isa_ok($mac, 'Net::MAC'); my $mac_defaults = $mac->convert; is("$mac_defaults", '01:ab:01:ab:01:ab', 'convert() using defaults'); my $mac_bitgroup = $mac->convert(bit_group => 8); is("$mac_bitgroup", '01:ab:01:ab:01:ab', 'convert() using bit_group and default delimiter'); my $mac_bitgroup_delim = $mac->convert(bit_group => 12, delimiter => '-'); is("$mac_bitgroup_delim", '01a-b01-ab0-1ab', 'convert() using bit_group 12 and hyphen delimiter'); examples000755001750001750 011506447773 14635 5ustar00oliveroliver000000000000Net-MAC-2.103622gen_macs.pl000755001750001750 155211506447773 17113 0ustar00oliveroliver000000000000Net-MAC-2.103622/examples#!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Data::Dumper; my ($count, $base); &Getopt::Long::GetOptions( 'count=i' => \$count, 'base=i' => \$base ); my @delimiter = ('.', ':', '-', ' '); my $z = 1; # $z == 1 means zero padding my %mac; for (my $i=0; $i<$count; $i++) { my $mac = ''; my $delimiter = $delimiter[int(rand(4))]; for (my $j=0; $j<6; $j++) { # 6 octets for (my $k=0; $k<2; $k++) { my $random = sprintf('%x', int(rand(16))); if (($k == 0) && ($random eq '0') && ($z == 0)) { next; # Zero padding is turned off } $mac .= $random; } unless ($j == 5) { # Avoid trailing delimiter $mac .= $delimiter; } } $mac{$mac} = {base => $base, bit_group => 16, delimiter => $delimiter, zero_padding => $z}; $z = int(rand(2)); } my $dump = Data::Dumper->new([\%mac], ['mac']); print $dump->Dump(); 100_base16_macs.pl000644001750001750 6575211506447773 16461 0ustar00oliveroliver000000000000Net-MAC-2.103622/t$mac = { 'bc.19.24.0.fd.78' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, 'a3:84:47:92:ce:1d' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, 'ed.5b.9d.10.4f.98' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '99 be c7 ea d5 bb' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, 'bd.76.00.6d.3d.ad' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, 'ce-81-22-0-2a-23' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, 'b1:38:7f:6e:3e:97' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, '92-c-c4-1b-92-d3' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, 'fb.4e.70.3.f5.c6' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '49.b1.1a.d2.83.82' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '47:58:9e:62:b:7' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, 'fd:69:cc:b4:e0:fa' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, 'ca.b1.9b.f1.e8.06' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, 'ae.b7.41.01.f3.f7' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '20.98.55.7b.89.45' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '0d:58:b6:30:f1:6e' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, 'ad be be e9 03 3e' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, '83-83-f7-c7-6e-34' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, '68 42 ac df c8 3e' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, 'c7:9e:9c:dc:9f:62' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, 'ac-ca-a0-4e-50-19' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, 'a4 a9 75 0a ab 2d' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, '3c 5f 52 f5 61 d2' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, '06-7c-75-81-5e-45' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, 'ca 60 5a 17 e0 f4' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, 'ad 1f 18 06 fb 3d' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, '2b 68 87 ac 65 ec' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, 'a4 a8 72 be 1d fc' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, 'f5:5f:b8:de:68:fe' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, '48.15.56.c3.68.8f' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '94.9b.ca.bb.7e.50' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '8f-1d-75-50-31-90' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, 'b0-7-ce-7d-2a-5c' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '88 7b f6 9c cc 87' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, '96.7e.f6.96.50.bf' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, 'ed.20.c2.77.b4.44' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, 'e:54:28:e0:bc:7' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, 'af-f6-0a-d2-e6-f3' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, 'cf.1e.d5.9c.f5.fd' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '3a:da:2e:6d:a0:f8' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '42-e6-72-2e-5c-f0' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '13.2a.9b.b4.da.6e' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, 'aa e1 90 6 f9 1' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, 'c0-d9-dd-61-70-2' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, 'd9 34 52 a4 69 6a' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, 'aa e7 e9 dd 80 44' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, 'c-b9-5c-af-31-b' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, 'b6:61:0:cc:c1:a1' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, '78 bf 8e dd bd 3a' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, '24:f3:d3:e4:9f:5f' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '7a.ea.c5.f2.f9.c7' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, 'a7:e7:b7:6a:e8:49' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '2f.e5.f5.74.49.b4' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '58-28-7f-28-10-5a' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, 'e9 91 ff eb be 4d' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, 'eb.65.a8.1a.66.d2' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '62:47:8b:85:fd:e7' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, '54.9f.f.b8.55.3a' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '47:fb:83:d5:8a:c7' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, 'e5:79:99:12:e:f3' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, 'a2 6e 5 e0 86 78' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, 'f9 fe 9f e3 4c 37' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, '99-39-2a-9c-e4-41' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, '9e.bf.4d.8e.8b.dc' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '08:6a:05:10:72:4a' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '8-8d-91-66-b4-f5' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, 'e0.bc.f.6c.d9.19' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '58:ed:35:4f:1:e0' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, '9b:40:60:e4:6f:10' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '3e.8b.f6.9.32.ef' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, 'e0 86 aa 9b f6 30' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, '28.3a.75.61.6f.93' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '36:5e:1f:41:d4:42' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, '07.f3.25.06.0c.ae' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '80:30:f2:a0:9e:3e' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, '8e-89-cf-b4-fc-f0' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '98-0b-d2-f0-b4-f5' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, 'cb:7e:f8:80:f4:b8' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, '05:13:c3:a3:a6:27' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, 'ec.b8.30.0.66.6f' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, 'f3-aa-bf-4a-8d-86' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '15 6b 92 79 ff 54' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, 'e7.35.4c.f7.b8.3' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '1b.54.bd.91.36.5e' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, 'bf-75-4a-b0-db-27' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, '6-9e-c-ef-a0-0' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, 'c.0.bb.e1.d5.71' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '59.2a.de.18.c6.b7' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '6f.dc.d.fd.39.17' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '48:de:78:5e:66:82' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '9d:1c:7a:4e:ad:4a' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, 'c4-e8-5-92-cc-31' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, 'e5-c1-b1-58-44-d8' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '2a-1b-95-e0-ad-6b' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, 'd7.88.df.3c.db.32' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '89 87 ef 67 e4 07' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, '74-59-52-9a-e5-ca' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '35.c8.d6.80.f6.9d' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '1f-de-0b-f1-3b-52' => { 'base' => 16, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, '89-5c-e0-67-d-8f' => { 'base' => 16, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 } }; 20_custom_format.t000644001750001750 164011506447773 16772 0ustar00oliveroliver000000000000Net-MAC-2.103622/tuse Test::More tests => 6; #use Test::More qw(no_plan); BEGIN { use_ok('Net::MAC') }; # test class method eval{ Net::MAC->set_format_for( 'Custom' ) }; like($@, qr/missing HASH ref custom format/, 'missing HASH ref custom format'); my $rc = eval{ Net::MAC->set_format_for( Custom => { base => 16, bit_group => 16, delimiter => '~', }, ) }; ok($rc, 'Class method set_format_for set the new format'); my $macaddr = '01ab~01ab~01ab'; #eval{ Net::MAC->new(mac => $macaddr) }; #like($@, qr/invalid MAC format/, 'unspecified custom format dies'); my $mac = eval{ Net::MAC->new(mac => $macaddr, format => 'Custom') }; isa_ok($mac, 'Net::MAC', 'known custom format succeeds'); is($mac->as_Cisco, '01ab.01ab.01ab', 'as_foo working from custom format'); is(Net::MAC->new(mac => '01ab.01ab.01ab')->as_Custom, '01ab~01ab~01ab', 'as_Custom working from Cisco format'); 100_base10_macs.pl000644001750001750 6605411506447773 16447 0ustar00oliveroliver000000000000Net-MAC-2.103622/t$mac = { '72-1d-9f-22-cd-94' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '38-d0-04-e4-5e-bd' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, 'c0-d3-4d-00-6b-05' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, '5a-ea-b1-cb-2a-8c' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '9c:ab:8d:71:a0:31' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '77 f1 42 6c 1c ea' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, '65 22 73 b5 d4 ca' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, 'be e3 7d 61 48 32' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, '5e:59:6d:4e:ff:dc' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, 'a0:27:23:57:cb:e0' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '54-80-1d-fd-4c-92' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '90.28.fc.33.9c.30' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '5a-a7-cc-b8-e5-ed' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, 'a9.2a.aa.ff.76.c' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '1e-89-3c-42-25-40' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '8c-78-25-7c-e5-15' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, '49:63:4c:aa:fc:a3' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, 'ff:5e:dc:1b:4c:80' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, '92 2f 21 f7 e7 d9' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, 'c5 2f 77 26 08 f9' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, 'a0:13:b4:30:ed:d3' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '09.9f.86.59.8e.59' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, 'd0.88.14.67.38.66' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '19-fc-87-3d-a0-1a' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, 'fe.27.48.92.77.31' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '1b.97.b3.48.7d.f3' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '22 49 1f 73 b1 ac' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, '1c.fd.f2.e0.1e.9' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, 'e4:5c:08:67:a2:26' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, 'c9:f1:98:40:3d:7' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, 'ce:77:a:96:79:8e' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, '2f ef f0 4f bc 6e' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, '3c.76.bc.16.1e.70' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '23:a8:f3:98:d4:39' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, 'ae a6 7d aa f2 e4' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, 'f ee 1c f1 df 22' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, '85.fb.4d.00.f8.72' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, 'a8-f8-24-8f-f1-85' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '4a.3a.95.9a.66.de' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '65 be 42 0c ce 5c' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, '99.17.5b.1c.63.c7' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '6c-6b-5f-23-30-f6' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '2d:09:5b:b6:ec:33' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '23 83 bf f8 13 f1' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, '7 3f 9e 45 30 6d' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, '18 37 1c 76 91 c7' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, 'c8.15.45.d1.e5.40' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '9d:f5:4a:94:04:4e' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '05:44:8d:74:4c:9e' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '1d 94 27 b5 f5 22' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, '9f.64.c6.b0.2.41' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, 'e3 84 b7 db ed 7' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, 'da 33 87 3f 7d 91' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, 'e7 17 e0 c1 b8 0d' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, '64-23-78-a7-83-4a' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '97-8a-dd-bc-5-64' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '80.f6.c5.63.b8.79' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '07:a9:83:58:4c:8b' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '95:58:50:74:7f:fb' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, 'bb-2e-d2-c1-e6-41' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, '17-6b-19-c5-3-17' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, 'f3 68 92 6 4a ac' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, '49.86.61.32.9d.3a' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, 'c0:31:d2:87:5b:67' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '99.8e.87.90.2c.33' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, 'f2-2e-3f-78-d-e2' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, 'c7-1e-96-8a-6e-8b' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, 'fe:b0:b2:63:42:aa' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '1e:a4:bd:fe:15:91' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '55.34.74.07.0d.fe' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '1d.3.a1.a.f7.8d' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '8c.94.b7.cb.52.8d' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '50:7b:4c:da:18:f2' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '3d.bb.12.2d.c9.40' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, 'e2-0-eb-26-cd-88' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, 'a.63.b9.33.40.49' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '0d:02:0c:ab:9a:9d' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, 'cf:6f:73:18:23:44' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, '17-b0-b2-01-36-93' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, '5c ad 6c 9f e7 4f' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, '2f.95.e0.bb.15.e7' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '4f 2d 79 bf e5 ed' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, 'a8-b-60-d9-62-4a' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, 'be.d.69.42.de.5e' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, '4d.d8.27.a7.d3.d5' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '.', 'bit_group' => 16 }, 'e5 4d 4e 71 60 92' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, 'f9-73-54-5-9a-ca' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '60 25 5d 4d 52 6f' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, 'f8:a8:5c:4a:5c:33' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, '90.90.b1.49.ca.7a' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '42 ed b2 53 6b c2' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ' ', 'bit_group' => 16 }, 'bf:5e:61:56:c4:a0' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ':', 'bit_group' => 16 }, '1d 6 6a 19 f0 ca' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 }, '47-ca-35-a1-c9-af' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, '63-12-9-77-f6-2e' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => '-', 'bit_group' => 16 }, 'd9:9b:cb:27:f8:e5' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, 'c0.3e.86.39.f0.ff' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '.', 'bit_group' => 16 }, '22:c6:15:c1:08:dc' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => ':', 'bit_group' => 16 }, '5e-c1-63-01-9e-6c' => { 'base' => 10, 'zero_padding' => 1, 'delimiter' => '-', 'bit_group' => 16 }, 'dc d6 70 83 16 19' => { 'base' => 10, 'zero_padding' => 0, 'delimiter' => ' ', 'bit_group' => 16 } };