diff --git a/Artistic b/Artistic new file mode 100644 index 0000000..5f22124 --- /dev/null +++ b/Artistic @@ -0,0 +1,131 @@ + + + + + The "Artistic License" + + Preamble + +The intent of this document is to state the conditions under which a +Package may be copied, such that the Copyright Holder maintains some +semblance of artistic control over the development of the package, +while giving the users of the package the right to use and distribute +the Package in a more-or-less customary fashion, plus the right to make +reasonable modifications. + +Definitions: + + "Package" refers to the collection of files distributed by the + Copyright Holder, and derivatives of that collection of files + created through textual modification. + + "Standard Version" refers to such a Package if it has not been + modified, or has been modified in accordance with the wishes + of the Copyright Holder as specified below. + + "Copyright Holder" is whoever is named in the copyright or + copyrights for the package. + + "You" is you, if you're thinking about copying or distributing + this Package. + + "Reasonable copying fee" is whatever you can justify on the + basis of media cost, duplication charges, time of people involved, + and so on. (You will not be required to justify it to the + Copyright Holder, but only to the computing community at large + as a market that must bear the fee.) + + "Freely Available" means that no fee is charged for the item + itself, though there may be fees involved in handling the item. + It also means that recipients of the item may redistribute it + under the same conditions they received it. + +1. You may make and give away verbatim copies of the source form of the +Standard Version of this Package without restriction, provided that you +duplicate all of the original copyright notices and associated disclaimers. + +2. You may apply bug fixes, portability fixes and other modifications +derived from the Public Domain or from the Copyright Holder. A Package +modified in such a way shall still be considered the Standard Version. + +3. You may otherwise modify your copy of this Package in any way, provided +that you insert a prominent notice in each changed file stating how and +when you changed that file, and provided that you do at least ONE of the +following: + + a) place your modifications in the Public Domain or otherwise make them + Freely Available, such as by posting said modifications to Usenet or + an equivalent medium, or placing the modifications on a major archive + site such as uunet.uu.net, or by allowing the Copyright Holder to include + your modifications in the Standard Version of the Package. + + b) use the modified Package only within your corporation or organization. + + c) rename any non-standard executables so the names do not conflict + with standard executables, which must also be provided, and provide + a separate manual page for each non-standard executable that clearly + documents how it differs from the Standard Version. + + d) make other distribution arrangements with the Copyright Holder. + +4. You may distribute the programs of this Package in object code or +executable form, provided that you do at least ONE of the following: + + a) distribute a Standard Version of the executables and library files, + together with instructions (in the manual page or equivalent) on where + to get the Standard Version. + + b) accompany the distribution with the machine-readable source of + the Package with your modifications. + + c) give non-standard executables non-standard names, and clearly + document the differences in manual pages (or equivalent), together + with instructions on where to get the Standard Version. + + d) make other distribution arrangements with the Copyright Holder. + +5. You may charge a reasonable copying fee for any distribution of this +Package. You may charge any fee you choose for support of this +Package. You may not charge a fee for this Package itself. However, +you may distribute this Package in aggregate with other (possibly +commercial) programs as part of a larger (possibly commercial) software +distribution provided that you do not advertise this Package as a +product of your own. You may embed this Package's interpreter within +an executable of yours (by linking); this shall be construed as a mere +form of aggregation, provided that the complete Standard Version of the +interpreter is so embedded. + +6. The scripts and library files supplied as input to or produced as +output from the programs of this Package do not automatically fall +under the copyright of this Package, but belong to whoever generated +them, and may be sold commercially, and may be aggregated with this +Package. If such scripts or library files are aggregated with this +Package via the so-called "undump" or "unexec" methods of producing a +binary executable image, then distribution of such an image shall +neither be construed as a distribution of this Package nor shall it +fall under the restrictions of Paragraphs 3 and 4, provided that you do +not represent such an executable image as a Standard Version of this +Package. + +7. C subroutines (or comparably compiled subroutines in other +languages) supplied by you and linked into this Package in order to +emulate subroutines and variables of the language defined by this +Package shall not be considered part of this Package, but are the +equivalent of input as in Paragraph 6, provided these subroutines do +not change the language in any way that would cause it to fail the +regression tests for the language. + +8. Aggregation of this Package with a commercial distribution is always +permitted provided that the use of this Package is embedded; that is, +when no overt attempt is made to make this Package's interfaces visible +to the end user of the commercial distribution. Such use shall not be +construed as a distribution of this Package. + +9. The name of the Copyright Holder may not be used to endorse or promote +products derived from this software without specific prior written permission. + +10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED +WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + + The End diff --git a/Changes b/Changes index a61aed9..5e94767 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,41 @@ Revision history for Perl extension NetAddr::IP +4.049 Sat Oct 15 22:15:10 PDT 2011 + Updates to Lite.pm, Util.pm, new module InetBase.pm + + In Lite v1.32 + bug68723, add capability to parse input of the form + ->new6(12345,1). This should have been there but was + missing. Thanks to Andrei Benea for spotting this. + + In Util v1.41 + add inet_pton, inet_ntop, AF_INET, AF_INET6 + + modify inet_n2dx and inet_n2ad to + recognize the new 128 bit IPv4 format + ::FFFF:FFFF:0:0 + + replace isIPv4 with a pure perl version for portablity + + split the following into NetAddr::IP::InetBase v0.01 + to provide better long term support for IPv6 + inet_aton + inet_ntoa + ipv6_aton + ipv6_n2x + ipv6_n2d + inet_any2n + inet_n2dx + inet_n2ad + inet_ntop + inet_pton + packzeros + isIPv4 + isNewIPv4 + isAnyIPv4 + AF_INET + AF_INET6 + 4.048 Sat Oct 8 01:33:44 PDT 2011 remove debug print statement from Util v1.40 diff --git a/Copying b/Copying new file mode 100644 index 0000000..a43ea21 --- /dev/null +++ b/Copying @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + 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 + + Appendix: How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + 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., 675 Mass Ave, Cambridge, MA 02139, USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/IP.pm b/IP.pm index ee3e498..935676e 100644 --- a/IP.pm +++ b/IP.pm @@ -4,8 +4,8 @@ use strict; #use diagnostics; -use NetAddr::IP::Lite 1.31 qw(Zero Zeros Ones V4mask V4net); -use NetAddr::IP::Util 1.40 qw( +use NetAddr::IP::Lite 1.32 qw(Zero Zeros Ones V4mask V4net); +use NetAddr::IP::Util 1.41 qw( sub128 inet_aton inet_any2n @@ -18,6 +18,7 @@ hasbits notcontiguous ); + use AutoLoader qw(AUTOLOAD); use vars qw( @@ -34,7 +35,7 @@ @ISA = qw(Exporter NetAddr::IP::Lite); -$VERSION = do { sprintf " %d.%03d", (q$Revision: 4.48 $ =~ /\d+/g) }; +$VERSION = do { sprintf " %d.%03d", (q$Revision: 4.49 $ =~ /\d+/g) }; =pod @@ -69,6 +70,8 @@ my $ip = new NetAddr::IP::Lite '127.0.0.1'; + or if your prefer + my $ip = NetAddr::IP::Lite->new('127.0.0.1); or from a packed IPv4 address my $ip = new_from_aton NetAddr::IP::Lite (inet_aton('127.0.0.1')); or from an octal filtered IPv4 address @@ -335,11 +338,11 @@ =cut +my $full_format = "%04X:%04X:%04X:%04X:%04X:%04X:%D.%D.%D.%D"; +my $full6_format = "%04X:%04X:%04X:%04X:%04X:%04X:%04X:%04X"; + sub import { - our $full_format = "%04X:%04X:%04X:%04X:%04X:%04X:%D.%D.%D.%D"; - our $full6_format = "%04X:%04X:%04X:%04X:%04X:%04X:%04X:%04X"; - if (grep { $_ eq ':old_storable' } @_) { @_ = grep { $_ ne ':old_storable' } @_; } else { @@ -436,6 +439,24 @@ return $rv ? @$rv : (); } +sub full($) { + if (! $_[0]->{isv6} && isIPv4($_[0]->{addr})) { + my @hex = (unpack("n8",$_[0]->{addr})); + $hex[9] = $hex[7] & 0xff; + $hex[8] = $hex[7] >> 8; + $hex[7] = $hex[6] & 0xff; + $hex[6] >>= 8; + return sprintf($full_format,@hex); + } else { + &full6; + } +} + +sub full6($) { + my @hex = (unpack("n8",$_[0]->{addr})); + return sprintf($full6_format,@hex); +} + sub DESTROY {}; 1; @@ -816,26 +837,6 @@ Returns the address part in FULL ipV6 notation -=cut - -sub full($) { - if (! $_[0]->{isv6} && isIPv4($_[0]->{addr})) { - my @hex = (unpack("n8",$_[0]->{addr})); - $hex[9] = $hex[7] & 0xff; - $hex[8] = $hex[7] >> 8; - $hex[7] = $hex[6] & 0xff; - $hex[6] >>= 8; - return sprintf($full_format,@hex); - } else { - &full6; - } -} - -sub full6($) { - my @hex = (unpack("n8",$_[0]->{addr})); - return sprintf($full6_format,@hex); -} - =item C<$me-Econtains($other)> Returns true when C<$me> completely contains C<$other>. False is @@ -1477,10 +1478,6 @@ &STORABLE_thaw; } -1; - -__END__ - =pod =back @@ -1520,13 +1517,41 @@ This software comes with the same warranty as perl itself (ie, none), so by using it you accept any and all the liability. -=head1 LICENSE +=head1 COPYRIGHT This software is (c) Luis E. Muñoz, 1999 - 2007, and (c) Michael -Robinton, 2006 - 2010. It can be used under the terms of the Perl -artistic license provided that proper credit for the work of the -authors is preserved in the form of this copyright notice and license -for this module. +Robinton, 2006 - 2011. + +All rights reserved. + +This program is free software; you can redistribute it and/or modify +it under the terms of either: + + a) the GNU General Public License as published by the Free + Software Foundation; either version 2, or (at your option) any + later version, or + + b) the "Artistic License" which comes with this distribution. + +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 either +the GNU General Public License or the Artistic License for more details. + +You should have received a copy of the Artistic License with this +distribution, in the file named "Artistic". If not, I'll be glad to provide +one. + +You should also have received a copy of the GNU General Public License +along with this program in the file named "Copying". If not, write to the + + Free Software Foundation, Inc. + 59 Temple Place, Suite 330 + Boston, MA 02111-1307, USA + +or visit their web page on the internet at: + + http://www.gnu.org/copyleft/gpl.html. =head1 SEE ALSO diff --git a/Lite/Changes b/Lite/Changes index 8f9951d..7990ca0 100644 --- a/Lite/Changes +++ b/Lite/Changes @@ -1,5 +1,38 @@ Revision history for Perl extension NetAddr::IP::Lite +1.32 Sat Oct 15 22:05:23 PDT 2011 + bug68723, add capability to parse input of the form + ->new6(12345,1). This should have been there but was + missing. Thanks to Andrei Benea for spotting this. + + In Util v1.41 + add inet_pton, inet_ntop, AF_INET, AF_INET6 + + modify inet_n2dx and inet_n2ad to + recognize the new 128 bit IPv4 format + ::FFFF:FFFF:0:0 + + replace isIPv4 with a pure perl version for portablity + + split the following into NetAddr::IP::InetBase v0.01 + to provide better long term support for IPv6 + inet_aton + inet_ntoa + ipv6_aton + ipv6_n2x + ipv6_n2d + inet_any2n + inet_n2dx + inet_n2ad + inet_ntop + inet_pton + packzeros + isIPv4 + isNewIPv4 + isAnyIPv4 + AF_INET + AF_INET6 + 1.31 Sat Oct 8 01:33:44 PDT 2011 remove debug print statement from Util.pm v1.40 diff --git a/Lite/Lite.pm b/Lite/Lite.pm index a96ebb6..6b9cc54 100644 --- a/Lite/Lite.pm +++ b/Lite/Lite.pm @@ -6,30 +6,32 @@ use strict; #use diagnostics; #use warnings; -use NetAddr::IP::Util qw( +use NetAddr::IP::InetBase qw( inet_any2n + isIPv4 + inet_n2dx + inet_aton + ipv6_aton + ipv6_n2x +); +use NetAddr::IP::Util qw( addconst sub128 ipv6to4 notcontiguous - isIPv4 shiftleft - inet_n2dx hasbits bin2bcd bcd2bin - inet_aton - inet_any2n - ipv6_aton - ipv6_n2x mask4to6 ipv4to6 naip_gethostbyname + havegethostbyname2 ); use vars qw(@ISA @EXPORT_OK $VERSION $Accept_Binary_IP $Old_nth $AUTOLOAD *Zero); -$VERSION = do { my @r = (q$Revision: 1.31 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; +$VERSION = do { my @r = (q$Revision: 1.32 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; require Exporter; @@ -63,6 +65,8 @@ ); my $ip = new NetAddr::IP::Lite '127.0.0.1'; + or if your prefer + my $ip = NetAddr::IP::Lite->new('127.0.0.1); or from a packed IPv4 address my $ip = new_from_aton NetAddr::IP::Lite (inet_aton('127.0.0.1')); or from an octal filtered IPv4 address @@ -608,18 +612,25 @@ =cut +my $lbmask = inet_aton('255.0.0.0'); +my $_p4broad = inet_any2n('255.255.255.255'); +my $_p4loop = inet_any2n('127.0.0.1'); +my $_p4mloop = inet_aton('255.0.0.0'); + $_p4mloop = mask4to6($_p4mloop); +my $_p6loop = inet_any2n('::1'); + my %fip4 = ( default => Zeros, any => Zeros, - broadcast => inet_any2n('255.255.255.255'), - loopback => inet_any2n('127.0.0.1'), + broadcast => $_p4broad, + loopback => $_p4loop, unspecified => undef, ); my %fip4m = ( default => Zeros, any => Zeros, broadcast => Ones, - loopback => mask4to6(inet_aton('255.0.0.0')), + loopback => $_p4mloop, unspecified => undef, # not applicable for ipV4 host => Ones, ); @@ -628,7 +639,7 @@ default => Zeros, any => Zeros, broadcast => undef, # not applicable for ipV6 - loopback => inet_any2n('::1'), + loopback => $_p6loop, unspecified => Zeros, ); @@ -745,6 +756,8 @@ last; } else { return undef unless $isV6; +# add for ipv6 notation "12345, 1" + $mask = lc $_[0]; } } else { $mask = lc $_[0]; @@ -758,8 +771,22 @@ # parse mask if ($mask =~ /^(\d+)$/) { my $mval = $1; - if (! $isV6 && index($ip,':') < 0) { # is ipV4 - if ($mval == 32) { # cidr 32 +# need to test here for 'dots' try running the tests that fail with $ip =~ /\D/ and print what IP is to see what's missing + if (! $isV6 && index($ip,':') < 0) { # ipV4 + my $try; + if ($ip !~ /\D/ && # binary IP address, could be ipV6 + ($try = bcd2bin($ip)) && + ! isIPv4($try) ) { + $isV6 = 1; + if ($mask < 128) { # small cidr + $mask = shiftleft(Ones,128 -$mval); # with small cidr + } else { # else binary mask + $mask = bcd2bin($mval); + } + $ip = $try; + last; + } + elsif ($mval == 32) { # cidr 32 $mask = Ones; } elsif ($mask < 32) { # small cidr @@ -767,12 +794,27 @@ } else { # is a binary mask $mask = pack('L3N',0xffffffff,0xffffffff,0xffffffff,$mval); } +# } +# elsif (! $isV6 && $ip !~ /\D/) { # is just a big number +# $ip = bcd2bin($ip); # special case +# if (isAnyIPv4($ip)) { +# $mask = shiftleft(Ones,32 -$mval); +# } else { +# $mask = shiftleft(Ones,128 -$mval); +# } } else { # is ipV6 $isV6 = 1; if ($mval == 128) { # cidr 128 $mask = Ones; } - elsif (index($ip,':') < 0) { # corner case of ipV4 with new6 + elsif ($ip !~ /\D/) { # numeric IPv6 address + if ($mask < 128) { # small cidr + $mask = shiftleft(Ones,128 -$mval); # with small cidr + } else { # else binary mask + $mask = bcd2bin($mval); + } + } + elsif (index($ip,':') < 0) { # corner case of ipV4 with new6 $mask = shiftleft(Ones,32 -$mval); } elsif ($mask < 128) { # small cidr @@ -818,7 +860,8 @@ elsif ($ip =~ /^(\d+)$/ && $hasmask && $1 >= 0 and $1 < 256) { # pure numeric $ip = sprintf("%d.0.0.0",$1); } - elsif ($ip =~ /^\d+$/ && !$hasmask) { # a big integer +# elsif ($ip =~ /^\d+$/ && !$hasmask) { # a big integer + elsif ($ip =~ /^\d+$/ ) { # a big integer $ip = bcd2bin($ip); last; } @@ -894,7 +937,7 @@ last; } # check for resolvable IPv6 hosts - elsif ($ip !~ /[^a-zA-Z0-9\.-]/ && ($tmp = naip_gethostbyname($ip))) { + elsif ($ip !~ /[^a-zA-Z0-9\.-]/ && havegethostbyname2() && ($tmp = naip_gethostbyname($ip))) { $ip = $tmp; $isV6 = 1; last; @@ -1346,18 +1389,45 @@ This software comes with the same warranty as perl itself (ie, none), so by using it you accept any and all the liability. -=head1 LICENSE +=head1 COPYRIGHT This software is (c) Luis E. Mu�oz, 1999 - 2005 - and (c) Michael Robinton, 2006 - 2010. + and (c) Michael Robinton, 2006 - 2011. -It can be used under the terms of the perl artistic license provided that -proper credit for the work of the author is preserved in the form of this -copyright notice and license for this module. +All rights reserved. + +This program is free software; you can redistribute it and/or modify +it under the terms of either: + + a) the GNU General Public License as published by the Free + Software Foundation; either version 2, or (at your option) any + later version, or + + b) the "Artistic License" which comes with this distribution. + +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 either +the GNU General Public License or the Artistic License for more details. + +You should have received a copy of the Artistic License with this +distribution, in the file named "Artistic". If not, I'll be glad to provide +one. + +You should also have received a copy of the GNU General Public License +along with this program in the file named "Copying". If not, write to the + + Free Software Foundation, Inc. + 59 Temple Place, Suite 330 + Boston, MA 02111-1307, USA + +or visit their web page on the internet at: + + http://www.gnu.org/copyleft/gpl.html. =head1 SEE ALSO -perl(1), NetAddr::IP(3), NetAddr::IP::Util(3) +NetAddr::IP(3), NetAddr::IP::Util(3), NetAddr::IP::InetBase(3) =cut diff --git a/Lite/MANIFEST b/Lite/MANIFEST index c90a453..fe92d78 100644 --- a/Lite/MANIFEST +++ b/Lite/MANIFEST @@ -5,10 +5,11 @@ Makefile.PL META.yml README -bug2742981.pl +bug2742981 t/addr.t t/aton.t t/bigints.t +t/bin_ips.t t/bits.t t/broadcast.t t/bug62521.t @@ -74,7 +75,6 @@ Util/configure Util/configure.ac Util/config.h.in -Util/GPL Util/lib/NetAddr/IP/UtilPP.pm Util/tlib/NetAddr/IP/Util_IS.pm Util/localconf.h @@ -83,6 +83,7 @@ Util/t/4to6.t Util/t/add128.t Util/t/addconst.t +Util/t/af_inet6.t Util/t/anyto6.t Util/t/badd.t Util/t/bcd2bin.t @@ -107,5 +108,18 @@ Util/typemap Util/Util.pm Util/Util.xs -Util/xs_include/inet_aton.c -Util/xs_include/miniSocket.inc +Util/lib/NetAddr/IP/InetBase.pm +Util/t/binet_n2ad.t +Util/t/binet_n2dx.t +Util/t/binet_pton.t +Util/t/bipv4_inet.t +Util/t/bipv6_any2n.t +Util/t/bipv6func.t +Util/t/bisIPv4.t +Util/t/bpackzeros.t +Util/t/inet_pton.t +Util/t/packzeros.t +Util/t/binet_ntoa.t +Util/t/ipv6_ntoa.t +Util/t/naip_gethostbyname.t +Util/t/no6_naip_gethostbyname.t diff --git a/Lite/README b/Lite/README index 575fc19..fcbae0f 100644 --- a/Lite/README +++ b/Lite/README @@ -14,6 +14,8 @@ ); my $ip = new NetAddr::IP::Lite '127.0.0.1'; + or if your prefer + my $ip = NetAddr::IP::Lite->new('127.0.0.1); or from a packed IPv4 address my $ip = new_from_aton NetAddr::IP::Lite (inet_aton('127.0.0.1')); or from an octal filtered IPv4 address @@ -459,14 +461,42 @@ This software comes with the same warranty as perl itself (ie, none), so by using it you accept any and all the liability. -LICENSE +COPYRIGHT This software is (c) Luis E. Mu�oz, 1999 - 2005 - and (c) Michael Robinton, 2006 - 2010. + and (c) Michael Robinton, 2006 - 2011. - It can be used under the terms of the perl artistic license provided - that proper credit for the work of the author is preserved in the form - of this copyright notice and license for this module. + All rights reserved. + + This program is free software; you can redistribute it and/or modify it + under the terms of either: + + a) the GNU General Public License as published by the Free + Software Foundation; either version 2, or (at your option) any + later version, or + + b) the "Artistic License" which comes with this distribution. + + 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 either the GNU + General Public License or the Artistic License for more details. + + You should have received a copy of the Artistic License with this + distribution, in the file named "Artistic". If not, I'll be glad to + provide one. + + You should also have received a copy of the GNU General Public License + along with this program in the file named "Copying". If not, write to + the + + Free Software Foundation, Inc. + 59 Temple Place, Suite 330 + Boston, MA 02111-1307, USA + + or visit their web page on the internet at: + + http://www.gnu.org/copyleft/gpl.html. SEE ALSO - perl(1), NetAddr::IP(3), NetAddr::IP::Util(3) + NetAddr::IP(3), NetAddr::IP::Util(3), NetAddr::IP::InetBase(3) diff --git a/Lite/Util/Changes b/Lite/Util/Changes index 6a157ea..a922282 100644 --- a/Lite/Util/Changes +++ b/Lite/Util/Changes @@ -1,4 +1,31 @@ -Revision history for Perl extension NetAddr::IP::Util + +1.41 Sat Oct 15 17:26:21 PDT 2011 + add inet_pton, inet_ntop, AF_INET, AF_INET6 + + modify inet_n2dx and inet_n2ad to + recognize the new 128 bit IPv4 format + ::FFFF:FFFF:0:0 + + replace isIPv4 with a pure perl version for portablity + + split the following into NetAddr::IP::InetBase v0.01 + to provide better long term support for IPv6 + inet_aton + inet_ntoa + ipv6_aton + ipv6_n2x + ipv6_n2d + inet_any2n + inet_n2dx + inet_n2ad + inet_ntop + inet_pton + packzeros + isIPv4 + isNewIPv4 + isAnyIPv4 + AF_INET + AF_INET6 1.40 Sat Oct 8 01:33:44 PDT 2011 remove debug print statement from Util.pm diff --git a/Lite/Util/GPL b/Lite/Util/GPL deleted file mode 100644 index 2ba72d5..0000000 --- a/Lite/Util/GPL +++ /dev/null @@ -1,340 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 - - Copyright (C) 1989, 1991 Free Software Foundation, Inc. - 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) 19yy - - 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - - -Also add information on how to contact you by electronic and paper mail. - -If the program is interactive, make it output a short notice like this -when it starts in an interactive mode: - - Gnomovision version 69, Copyright (C) 19yy name of author - Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, the commands you use may -be called something other than `show w' and `show c'; they could even be -mouse-clicks or menu items--whatever suits your program. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the program, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the program - `Gnomovision' (which makes passes at compilers) written by James Hacker. - - , 1 April 1989 - Ty Coon, President of Vice - -This General Public License does not permit incorporating your program into -proprietary programs. If your program is a subroutine library, you may -consider it more useful to permit linking proprietary applications with the -library. If this is what you want to do, use the GNU Library General -Public License instead of this License. diff --git a/Lite/Util/MANIFEST b/Lite/Util/MANIFEST index 870e4f5..6cf5479 100644 --- a/Lite/Util/MANIFEST +++ b/Lite/Util/MANIFEST @@ -1,27 +1,43 @@ Changes -configure -configure.ac -config.h.in -GPL -lib/NetAddr/IP/UtilPP.pm -localconf.h +MANIFEST This list of files +MANIFEST.SKIP Makefile.PL README +Util.pm +Util.xs +config.h.in +configure +configure.ac +lib/NetAddr/IP/InetBase.pm +lib/NetAddr/IP/UtilPP.pm +localconf.h t/4to6.t t/add128.t t/addconst.t +t/af_inet6.t t/anyto6.t t/badd.t t/bcd2bin.t t/bcdn2bin.t t/bin.t +t/binet_n2ad.t +t/binet_n2dx.t +t/binet_pton.t +t/bipv4_inet.t +t/binet_ntoa.t +t/bipv6_any2n.t +t/bipv6func.t +t/bisIPv4.t +t/bpackzeros.t t/comp128.t t/croak.t t/hasbits.t t/inet_4map6.t t/inet_n2ad.t t/inet_n2dx.t +t/inet_pton.t t/ipv4_inet.t +t/ipv6_ntoa.t t/ipv6_any2n.t t/ipv6func.t t/ipv6to4.t @@ -29,14 +45,9 @@ t/leftshift.t t/mode.t t/notcontiguous.t +t/packzeros.t t/simple_pack.t t/sub128.t +t/naip_gethostbyname.t +t/no6_naip_gethostbyname.t typemap -Util.pm -Util.xs -xs_include/inet_aton.c -xs_include/miniSocket.inc -Makefile.PL -MANIFEST This list of files -MANIFEST.SKIP -README diff --git a/Lite/Util/Makefile.PL b/Lite/Util/Makefile.PL index d2e8236..c13e77b 100644 --- a/Lite/Util/Makefile.PL +++ b/Lite/Util/Makefile.PL @@ -36,7 +36,8 @@ my %makeparms = ( NAME => $pkg, VERSION_FROM => $module, # finds $VERSION - depend => {$cfile => q[xs_include/miniSocket.inc localconf.h config.h localperl.h], +# depend => {$cfile => q[xs_include/miniSocket.inc localconf.h config.h localperl.h], + depend => {$cfile => q[ localconf.h config.h localperl.h ], }, # PREREQ_PM => {Test::More => 0, # }, diff --git a/Lite/Util/README b/Lite/Util/README index ab29f48..76a846e 100644 --- a/Lite/Util/README +++ b/Lite/Util/README @@ -6,6 +6,7 @@ inet_aton inet_ntoa ipv6_aton + ipv6_ntoa ipv6_n2x ipv6_n2d inet_any2n @@ -15,12 +16,15 @@ isAnyIPv4 inet_n2dx inet_n2ad + inet_pton + inet_ntop inet_4map6 ipv4to6 mask4to6 ipanyto6 maskanyto6 ipv6to4 + packzeros shiftleft addconst add128 @@ -29,23 +33,28 @@ bin2bcd bcd2bin mode + AF_INET + AF_INET6 naip_gethostbyname ); use NetAddr::IP::Util qw(:all :inet :ipv4 :ipv6 :math) - :inet => inet_aton, inet_ntoa, ipv6_aton, - ipv6_n2x, ipv6_n2d, inet_any2n, - inet_n2dx, inet_n2ad, inet_4map6, - ipv4to6, mask4to6, ipanyto6, + :inet => inet_aton, inet_ntoa, ipv6_aton + ipv6_ntoa, ipv6_n2x, ipv6_n2d, + inet_any2n, inet_n2dx, inet_n2ad, + inet_pton, inet_ntop, inet_4map6, + ipv4to6, mask4to6, ipanyto6, packzeros maskanyto6, ipv6to4, naip_gethostbyname :ipv4 => inet_aton, inet_ntoa - :ipv6 => ipv6_aton, ipv6_n2x, ipv6_n2d, - inet_any2n, inet_n2dx, inet_n2ad - inet_4map6, ipv4to6, mask4to6, ipanyto6, - maskanyto6, ipv6to4, naip_gethostbyname + :ipv6 => ipv6_aton, ipv6_ntoa, ipv6_n2x, + ipv6_n2d, inet_any2n, inet_n2dx, + inet_n2ad, inet_pton, inet_ntop, + inet_4map6, ipv4to6, mask4to6, + ipanyto6, maskanyto6, ipv6to4, + packzeros, naip_gethostbyname :math => hasbits, isIPv4, isNewIPv4, isAnyIPv4, addconst, add128, sub128, notcontiguous, @@ -54,8 +63,10 @@ $dotquad = inet_ntoa($netaddr); $netaddr = inet_aton($dotquad); $ipv6naddr = ipv6_aton($ipv6_text); + $ipv6_text = ipvt_ntoa($ipv6naddr); $hex_text = ipv6_n2x($ipv6naddr); $dec_text = ipv6_n2d($ipv6naddr); + $hex_text = packzeros($hex_text); $ipv6naddr = inet_any2n($dotquad or $ipv6_text); $ipv6naddr = inet_4map6($netaddr or $ipv6naddr); $rv = hasbits($bits128); @@ -64,6 +75,8 @@ $rv = isAnyIPv4($bits128); $dotquad or $hex_text = inet_n2dx($ipv6naddr); $dotquad or $dec_text = inet_n2ad($ipv6naddr); + $netaddr = inet_pton($AF_family,$hex_text); + $hex_text = inet_ntop($AF_family,$netaddr); $ipv6naddr = ipv4to6($netaddr); $ipv6naddr = mask4to6($netaddr); $ipv6naddr = ipanyto6($netaddr); @@ -81,6 +94,7 @@ $bits128 = bcd2bin($bcdtxt); $modetext = mode; ($name,$aliases,$addrtype,$length,@addrs)=naip_gethostbyname(NAME); + $trueif = havegethostbyname2(); NetAddr::IP::Util::lower(); NetAddr::IP::Util::upper(); @@ -142,6 +156,13 @@ input: ipv6 text returns: 128 bit RDATA string + * $ipv6_text = ipv6_ntoa($ipv6naddr); + Convert a 128 bit binary IPv6 address to compressed rfc 1884 text + representation. + + input: 128 bit RDATA string + returns: ipv6 text + * $hex_text = ipv6_n2x($ipv6addr); Takes an IPv6 RDATA string and returns an 8 segment IPv6 hex address @@ -235,8 +256,26 @@ returns: ddd.ddd.ddd.ddd or x:x:x:x:x:x:ddd.ddd.ddd.dd + * $netaddr = inet_pton($AF_family,$hex_text); + This function takes an IP address in IPv4 or IPv6 text format and + converts it into binary format. The type of IP address conversion is + controlled by the FAMILY argument. + + * $hex_text = inet_ntop($AF_family,$netaddr); + This function takes and IP address in binary format and converts it + into text format. The type of IP address conversion is controlled by + the FAMILY argument. + + NOTE: inet_ntop ALWAYS returns lowercase characters. + + * $hex_text = packzeros($hex_text); + This function optimizes and rfc 1884 IPv6 hex address to reduce the + number of long strings of zero bits as specified in rfc 1884, 2.2 + (2) by substituting :: for the first occurence of the longest string + of zeros in the address. + * $ipv6naddr = ipv4to6($netaddr); - Convert an ipv4 network address into an ipv6 network address. + Convert an ipv4 network address into an IPv6 network address. input: 32 bit network address returns: 128 bit network address @@ -342,11 +381,35 @@ * ($name,$aliases,$addrtype,$length,@addrs)=naip_gethostbyname(NAME); Replacement for Perl's gethostbyname if Socket6 is available + In ARRAY context, returns a list of five elements, the hostname or + NAME, a space seperated list of C_NAMES, AF family, length of the + address structure, and an array of one or more netaddr's + + In SCALAR context, returns the first netaddr. + This function ALWAYS returns an IPv6 address, even on IPv4 only systems. IPv4 addresses are mapped into IPv6 space in the form: ::FFFF:FFFF:d.d.d.d + This is NOT the expected result from Perl's gethostbyname2. It is + instead equivalent to: + + On an IPv4 only system: + $ipv6naddr = ipv4to6 scalar ( gethostbyname( name )); + + On a system with Socket6 and a working gethostbyname2: + $ipv6naddr = gethostbyname2( name, AF_INET6 ); + and if that fails, the IPv4 conversion above. + + For a gethostbyname2 emulator that behave like Socket6, see: the + Net::DNS::Dig manpage + + * $trueif = havegethostbyname2(); + This function returns TRUE if Socket6 has a functioning + gethostbyname2, otherwise it returns FALSE. See the comments above + about the behavior of naip_gethostbyname. + * NetAddr::IP::Util::lower(); Return IPv6 strings in lowercase. @@ -422,17 +485,19 @@ ? 1 : 0; } + # truely hard way to do $ip++ # add a constant, wrapping at netblock boundaries # to subtract the constant, negate it before calling # 'addwrap' since 'addconst' will extend the sign bits # sub addwrap { my($nip,$const) = @_; - my $mask = $nip->{addr}; + my $addr = $nip->{addr}; + my $mask = $nip->{mask}; my $bits = $nip->{bits}; my $notmask = ~ $mask; my $hibits = $addr & $mask; - my $addr = addconst($addr,$const); + $addr = addconst($addr,$const); my $wraponly = $addr & $notmask; my $newip = { addr => $hibits | $wraponly, @@ -443,10 +508,28 @@ return $newip; } + # something more useful + # increment a /24 net to the NEXT net at the boundry + + my $nextnet = 256; # for /24 + LOOP: + while (...continuing) { + your code.... + ... + my $lastip = $ip-copy(); + $ip++; + if ($ip < $lastip) { # host part wrapped? + # discard carry + (undef, $ip->{addr} = addconst($ip->{addr}, $nextnet); + } + next LOOP; + } + EXPORT_OK inet_aton inet_ntoa ipv6_aton + ipv6_ntoa ipv6_n2x ipv6_n2d inet_any2n @@ -456,12 +539,15 @@ isAnyIPv4 inet_n2dx inet_n2ad + inet_pton + inet_ntop inet_4map6 ipv4to6 mask4to6 ipanyto6 maskanyto6 ipv6to4 + packzeros shiftleft addconst add128 @@ -471,36 +557,49 @@ bcd2bin mode naip_gethostbyname + havegethostbyname2 AUTHOR Michael Robinton -ACKNOWLEDGMENTS - The following functions are used in whole or in part as include files to - Util.xs. The copyright is include in the file. - - file: function: - - miniSocket.inc inet_aton, inet_ntoa - - inet_aton, inet_ntoa are from the perl-5.8.0 release by Larry Wall, - copyright 1989-2002. inet_aton, inet_ntoa code is current through - perl-5.9.3 release. Thank you Larry for making PERL possible for all of - us. - COPYRIGHT - Copyright 2003 - 2010, Michael Robinton + Copyright 2003 - 2011, Michael Robinton - LICENSE AND WARRANTY + All rights reserved. - This software is (c) Michael Robinton. It can be used under the terms of - the perl artistic license provided that proper credit for the work of - the author is preserved in the form of this copyright notice and license - for this module. + This program is free software; you can redistribute it and/or modify it + under the terms of either: - No warranty of any kind is expressed or implied, by using it you accept - any and all the liability. + a) the GNU General Public License as published by the Free + Software Foundation; either version 2, or (at your option) any + later version, or + + b) the "Artistic License" which comes with this distribution. + + 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 either the GNU + General Public License or the Artistic License for more details. + + You should have received a copy of the Artistic License with this + distribution, in the file named "Artistic". If not, I'll be glad to + provide one. + + You should also have received a copy of the GNU General Public License + along with this program in the file named "Copying". If not, write to + the + + Free Software Foundation, Inc. + 59 Temple Place, Suite 330 + Boston, MA 02111-1307, USA + + or visit their web page on the internet at: + + http://www.gnu.org/copyleft/gpl.html. AUTHOR Michael Robinton +SEE ALSO + NetAddr::IP(3), NetAddr::IP::Lite(3), NetAddr::IP::InetBase(3) + diff --git a/Lite/Util/Util.pm b/Lite/Util/Util.pm index b17affa..774aff8 100644 --- a/Lite/Util/Util.pm +++ b/Lite/Util/Util.pm @@ -3,22 +3,31 @@ use strict; #use diagnostics; -#use lib qw(blib lib); +#use lib qw(blib/lib); use vars qw($VERSION @EXPORT_OK @ISA %EXPORT_TAGS $Mode); use AutoLoader qw(AUTOLOAD); use NetAddr::IP::Util_IS; +use NetAddr::IP::InetBase qw( + :upper + :all +); + +*NetAddr::IP::Util::upper = \&NetAddr::IP::InetBase::upper; +*NetAddr::IP::Util::lower = \&NetAddr::IP::InetBase::lower; + require DynaLoader; require Exporter; @ISA = qw(Exporter DynaLoader); -$VERSION = do { my @r = (q$Revision: 1.40 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; +$VERSION = do { my @r = (q$Revision: 1.41 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; @EXPORT_OK = qw( inet_aton inet_ntoa ipv6_aton + ipv6_ntoa ipv6_n2x ipv6_n2d inet_any2n @@ -28,6 +37,8 @@ isAnyIPv4 inet_n2dx inet_n2ad + inet_pton + inet_ntop inet_4map6 shiftleft addconst @@ -47,7 +58,11 @@ bcdn2bin simple_pack comp128 + packzeros + AF_INET + AF_INET6 naip_gethostbyname + havegethostbyname2 ); %EXPORT_TAGS = ( @@ -56,17 +71,21 @@ inet_aton inet_ntoa ipv6_aton + ipv6_ntoa ipv6_n2x ipv6_n2d inet_any2n inet_n2dx inet_n2ad + inet_pton + inet_ntop inet_4map6 ipv4to6 mask4to6 ipanyto6 maskanyto6 ipv6to4 + packzeros naip_gethostbyname )], math => [qw( @@ -88,17 +107,21 @@ )], ipv6 => [qw( ipv6_aton + ipv6_ntoa ipv6_n2x ipv6_n2d inet_any2n inet_n2dx inet_n2ad + inet_pton + inet_ntop inet_4map6 ipv4to6 mask4to6 ipanyto6 maskanyto6 ipv6to4 + packzeros naip_gethostbyname )], ); @@ -111,73 +134,24 @@ if (NetAddr::IP::Util_IS->pure || $@) { ## load the pure perl version if 'C' lib missing require NetAddr::IP::UtilPP; import NetAddr::IP::UtilPP qw( :all ); - require Socket; - import Socket qw(inet_ntoa); - *yinet_aton = \&Socket::inet_aton; +# require Socket; +# import Socket qw(inet_ntoa); +# *yinet_aton = \&Socket::inet_aton; $Mode = 'Pure Perl'; } else { $Mode = 'CC XS'; } -# allow user to choose upper or lower case -BEGIN { - use vars qw($n2x_format $n2d_format); - $n2x_format = "%X:%X:%X:%X:%X:%X:%X:%X"; - $n2d_format = "%X:%X:%X:%X:%X:%X:%D.%D.%D.%D"; -} - -sub upper { $n2x_format = uc($n2x_format); $n2d_format = uc($n2d_format); } -sub lower { $n2x_format = lc($n2x_format); $n2d_format = lc($n2d_format); } - -sub ipv6_n2x { - die "Bad arg length for 'ipv6_n2x', length is ". length($_[0]) ." should be 16" - unless length($_[0]) == 16; - return sprintf($n2x_format,unpack("n8",$_[0])); -} - -sub ipv6_n2d { - die "Bad arg length for 'ipv6_n2d', length is ". length($_[0]) ." should be 16" - unless length($_[0]) == 16; - my @hex = (unpack("n8",$_[0])); - $hex[9] = $hex[7] & 0xff; - $hex[8] = $hex[7] >> 8; - $hex[7] = $hex[6] & 0xff; - $hex[6] >>= 8; - return sprintf($n2d_format,@hex); -} - # if Socket lib is broken in some way, check for overange values # -my $overange = yinet_aton('256.1') ? 1:0; +#my $overange = yinet_aton('256.1') ? 1:0; +my $overange = gethostbyname('256.1') ? 1:0; sub mode() { $Mode }; -sub inet_aton { - if (! $overange || $_[0] =~ /[^0-9\.]/) { # hostname - return &yinet_aton; - } - my @dq = split(/\./,$_[0]); - foreach (@dq) { - return undef if $_ > 255; - } - return &yinet_aton; -} - my $_newV4compat = pack('L4',0,0,0xffffffff,0); -sub isNewIPv4 { - my $naddr = $_[0] ^ $_newV4compat; - return isIPv4($naddr); -} - -sub isAnyIPv4 { - my $naddr = $_[0]; - my $rv = isIPv4($_[0]); - return $rv if $rv; - return isNewIPv4($naddr); -} - sub inet_4map6 { my $naddr = shift; if (length($naddr) == 4) { @@ -195,10 +169,18 @@ sub DESTROY {}; +my $havegethostbyname2 = 0; + my $mygethostbyname; my $_Sock6ok = 1; # for testing gethostbyname +sub havegethostbyname2 { + return $_Sock6ok + ? $havegethostbyname2 + : 0; +} + sub import { if (grep { $_ eq ':noSock6' } @_) { $_Sock6ok = 0; @@ -215,14 +197,12 @@ use strict; use Socket; -sub DESTROY {}; +my $_v4zero = pack('L',0); +my $_zero = pack('L4',0,0,0,0); # invoke replacement subroutine for Perl's "gethostbyname" # if Socket6 is available. # -my $_v4zero = pack('L',0); -my $_zero = pack('L4',0,0,0,0); - sub _end_gethostbyname { # my ($name,$aliases,$addrtype,$length,@addrs) = @_; my @rv = @_; @@ -246,19 +226,24 @@ return @rv; } -unless (eval { require Socket6 }) { +unless ( eval { require Socket6 }) { $mygethostbyname = sub { my @tip = gethostbyname($_[0]); return &_end_gethostbyname(@tip); }; } else { - import Socket6 qw( gethostbyname2 ); - import Socket6 qw( AF_INET6 ) - unless eval { &AF_INET6 }; + import Socket6 qw( gethostbyname2 getipnodebyname ); + my $try = eval { my @try = gethostbyname2('127.0.0.1',NetAddr::IP::Util::AF_INET()); $try[4] }; + if (! $@ && $try && $try eq INADDR_LOOPBACK()) { + *_ghbn2 = \&Socket6::gethostbyname2; + $havegethostbyname2 = 1; + } else { + *_ghbn2 = sub { return () }; # use failure branch below + } $mygethostbyname = sub { my @tip; - unless ($_Sock6ok && (@tip = gethostbyname2($_[0],&AF_INET6))) { + unless ($_Sock6ok && (@tip = _ghbn2($_[0],NetAddr::IP::Util::AF_INET6())) && @tip > 1) { @tip = gethostbyname($_[0]); } return &_end_gethostbyname(@tip); @@ -290,6 +275,7 @@ inet_aton inet_ntoa ipv6_aton + ipv6_ntoa ipv6_n2x ipv6_n2d inet_any2n @@ -299,12 +285,15 @@ isAnyIPv4 inet_n2dx inet_n2ad + inet_pton + inet_ntop inet_4map6 ipv4to6 mask4to6 ipanyto6 maskanyto6 ipv6to4 + packzeros shiftleft addconst add128 @@ -313,23 +302,28 @@ bin2bcd bcd2bin mode + AF_INET + AF_INET6 naip_gethostbyname ); use NetAddr::IP::Util qw(:all :inet :ipv4 :ipv6 :math) - :inet => inet_aton, inet_ntoa, ipv6_aton, - ipv6_n2x, ipv6_n2d, inet_any2n, - inet_n2dx, inet_n2ad, inet_4map6, - ipv4to6, mask4to6, ipanyto6, + :inet => inet_aton, inet_ntoa, ipv6_aton + ipv6_ntoa, ipv6_n2x, ipv6_n2d, + inet_any2n, inet_n2dx, inet_n2ad, + inet_pton, inet_ntop, inet_4map6, + ipv4to6, mask4to6, ipanyto6, packzeros maskanyto6, ipv6to4, naip_gethostbyname :ipv4 => inet_aton, inet_ntoa - :ipv6 => ipv6_aton, ipv6_n2x, ipv6_n2d, - inet_any2n, inet_n2dx, inet_n2ad - inet_4map6, ipv4to6, mask4to6, ipanyto6, - maskanyto6, ipv6to4, naip_gethostbyname + :ipv6 => ipv6_aton, ipv6_ntoa, ipv6_n2x, + ipv6_n2d, inet_any2n, inet_n2dx, + inet_n2ad, inet_pton, inet_ntop, + inet_4map6, ipv4to6, mask4to6, + ipanyto6, maskanyto6, ipv6to4, + packzeros, naip_gethostbyname :math => hasbits, isIPv4, isNewIPv4, isAnyIPv4, addconst, add128, sub128, notcontiguous, @@ -338,8 +332,10 @@ $dotquad = inet_ntoa($netaddr); $netaddr = inet_aton($dotquad); $ipv6naddr = ipv6_aton($ipv6_text); + $ipv6_text = ipvt_ntoa($ipv6naddr); $hex_text = ipv6_n2x($ipv6naddr); $dec_text = ipv6_n2d($ipv6naddr); + $hex_text = packzeros($hex_text); $ipv6naddr = inet_any2n($dotquad or $ipv6_text); $ipv6naddr = inet_4map6($netaddr or $ipv6naddr); $rv = hasbits($bits128); @@ -348,6 +344,8 @@ $rv = isAnyIPv4($bits128); $dotquad or $hex_text = inet_n2dx($ipv6naddr); $dotquad or $dec_text = inet_n2ad($ipv6naddr); + $netaddr = inet_pton($AF_family,$hex_text); + $hex_text = inet_ntop($AF_family,$netaddr); $ipv6naddr = ipv4to6($netaddr); $ipv6naddr = mask4to6($netaddr); $ipv6naddr = ipanyto6($netaddr); @@ -365,6 +363,7 @@ $bits128 = bcd2bin($bcdtxt); $modetext = mode; ($name,$aliases,$addrtype,$length,@addrs)=naip_gethostbyname(NAME); + $trueif = havegethostbyname2(); NetAddr::IP::Util::lower(); NetAddr::IP::Util::upper(); @@ -433,39 +432,13 @@ input: ipv6 text returns: 128 bit RDATA string -=cut +=item * $ipv6_text = ipv6_ntoa($ipv6naddr); -sub ipv6_aton { - my($ipv6) = @_; - return undef unless $ipv6; - local($1,$2,$3,$4,$5); - if ($ipv6 =~ /^(.*:)(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) { # mixed hex, dot-quad - return undef if $2 > 255 || $3 > 255 || $4 > 255 || $5 > 255; - $ipv6 = sprintf("%s%X%02X:%X%02X",$1,$2,$3,$4,$5); # convert to pure hex - } - my $c; - return undef if - $ipv6 =~ /[^:0-9a-fA-F]/ || # non-hex character - (($c = $ipv6) =~ s/::/x/ && $c =~ /(?:x|:):/) || # double :: ::? - $ipv6 =~ /[0-9a-fA-F]{5,}/; # more than 4 digits - $c = $ipv6 =~ tr/:/:/; # count the colons - return undef if $c < 7 && $ipv6 !~ /::/; - if ($c > 7) { # strip leading or trailing :: - return undef unless - $ipv6 =~ s/^::/:/ || - $ipv6 =~ s/::$/:/; - return undef if --$c > 7; - } - while ($c++ < 7) { # expand compressed fields - $ipv6 =~ s/::/:::/; - } - $ipv6 .= 0 if $ipv6 =~ /:$/; - my @hex = split(/:/,$ipv6); - foreach(0..$#hex) { - $hex[$_] = hex($hex[$_] || 0); - } - pack("n8",@hex); -} +Convert a 128 bit binary IPv6 address to compressed rfc 1884 +text representation. + + input: 128 bit RDATA string + returns: ipv6 text =item * $hex_text = ipv6_n2x($ipv6addr); @@ -492,16 +465,6 @@ input: dot-quad or rfc1844 address returns: 128 bit IPv6 string -=cut - -sub inet_any2n($) { - my($addr) = @_; - $addr = '' unless $addr; - $addr = '::' . $addr - unless $addr =~ /:/; - return ipv6_aton($addr); -} - =item * $rv = hasbits($bits128); This function returns true if there are one's present in the 128 bit string @@ -570,18 +533,6 @@ returns: ddd.ddd.ddd.ddd or x:x:x:x:x:x:x:x -=cut - -sub inet_n2dx($) { - my($nadr) = @_; - if (isIPv4($nadr)) { - local $1; - ipv6_n2d($nadr) =~ /([^:]+)$/; - return $1; - } - return ipv6_n2x($nadr); -} - =item * $dotquad or $dec_text = inet_n2ad($ipv6naddr); This function B and returns the text for either a @@ -591,20 +542,29 @@ returns: ddd.ddd.ddd.ddd or x:x:x:x:x:x:ddd.ddd.ddd.dd -=cut +=item * $netaddr = inet_pton($AF_family,$hex_text); -sub inet_n2ad($) { - my($nadr) = @_; - my $addr = ipv6_n2d($nadr); - return $addr unless isIPv4($nadr); - local $1; - $addr =~ /([^:]+)$/; - return $1; -} +This function takes an IP address in IPv4 or IPv6 text format and converts it into +binary format. The type of IP address conversion is controlled by the FAMILY +argument. + +=item * $hex_text = inet_ntop($AF_family,$netaddr); + +This function takes and IP address in binary format and converts it into +text format. The type of IP address conversion is controlled by the FAMILY +argument. + +NOTE: inet_ntop ALWAYS returns lowercase characters. + +=item * $hex_text = packzeros($hex_text); + +This function optimizes and rfc 1884 IPv6 hex address to reduce the number of +long strings of zero bits as specified in rfc 1884, 2.2 (2) by substituting +B<::> for the first occurence of the longest string of zeros in the address. =item * $ipv6naddr = ipv4to6($netaddr); -Convert an ipv4 network address into an ipv6 network address. +Convert an ipv4 network address into an IPv6 network address. input: 32 bit network address returns: 128 bit network address @@ -764,11 +724,35 @@ Replacement for Perl's gethostbyname if Socket6 is available +In ARRAY context, returns a list of five elements, the hostname or NAME, +a space seperated list of C_NAMES, AF family, length of the address +structure, and an array of one or more netaddr's + +In SCALAR context, returns the first netaddr. + This function ALWAYS returns an IPv6 address, even on IPv4 only systems. IPv4 addresses are mapped into IPv6 space in the form: ::FFFF:FFFF:d.d.d.d +This is NOT the expected result from Perl's gethostbyname2. It is instead equivalent to: + + On an IPv4 only system: + $ipv6naddr = ipv4to6 scalar ( gethostbyname( name )); + + On a system with Socket6 and a working gethostbyname2: + $ipv6naddr = gethostbyname2( name, AF_INET6 ); + and if that fails, the IPv4 conversion above. + +For a gethostbyname2 emulator that behave like Socket6, see: +L + +=item * $trueif = havegethostbyname2(); + +This function returns TRUE if Socket6 has a functioning B, +otherwise it returns FALSE. See the comments above about the behavior of +B. + =item * NetAddr::IP::Util::lower(); Return IPv6 strings in lowercase. @@ -851,17 +835,19 @@ ? 1 : 0; } + # truely hard way to do $ip++ # add a constant, wrapping at netblock boundaries # to subtract the constant, negate it before calling # 'addwrap' since 'addconst' will extend the sign bits # sub addwrap { my($nip,$const) = @_; - my $mask = $nip->{addr}; + my $addr = $nip->{addr}; + my $mask = $nip->{mask}; my $bits = $nip->{bits}; my $notmask = ~ $mask; my $hibits = $addr & $mask; - my $addr = addconst($addr,$const); + $addr = addconst($addr,$const); my $wraponly = $addr & $notmask; my $newip = { addr => $hibits | $wraponly, @@ -872,11 +858,30 @@ return $newip; } + # something more useful + # increment a /24 net to the NEXT net at the boundry + + my $nextnet = 256; # for /24 + LOOP: + while (...continuing) { + your code.... + ... + my $lastip = $ip-copy(); + $ip++; + if ($ip < $lastip) { # host part wrapped? + # discard carry + (undef, $ip->{addr} = addconst($ip->{addr}, $nextnet); + } + next LOOP; + } + + =head1 EXPORT_OK inet_aton inet_ntoa ipv6_aton + ipv6_ntoa ipv6_n2x ipv6_n2d inet_any2n @@ -886,12 +891,15 @@ isAnyIPv4 inet_n2dx inet_n2ad + inet_pton + inet_ntop inet_4map6 ipv4to6 mask4to6 ipanyto6 maskanyto6 ipv6to4 + packzeros shiftleft addconst add128 @@ -901,43 +909,55 @@ bcd2bin mode naip_gethostbyname + havegethostbyname2 =head1 AUTHOR Michael Robinton -=head1 ACKNOWLEDGMENTS - -The following functions are used in whole or in part as include files to -Util.xs. The copyright is include in the file. - - file: function: - - miniSocket.inc inet_aton, inet_ntoa - -inet_aton, inet_ntoa are from the perl-5.8.0 release by Larry Wall, copyright -1989-2002. inet_aton, inet_ntoa code is current through perl-5.9.3 release. -Thank you Larry for making PERL possible for all of us. - =head1 COPYRIGHT -Copyright 2003 - 2010, Michael Robinton Emichael@bizsystems.comE +Copyright 2003 - 2011, Michael Robinton Emichael@bizsystems.comE -LICENSE AND WARRANTY +All rights reserved. -This software is (c) Michael Robinton. It can be used under the terms of -the perl artistic license provided that proper credit for the work of -the author is preserved in the form of this copyright notice and -license for this module. +This program is free software; you can redistribute it and/or modify +it under the terms of either: -No warranty of any kind is expressed or implied, by using it -you accept any and all the liability. + a) the GNU General Public License as published by the Free + Software Foundation; either version 2, or (at your option) any + later version, or + b) the "Artistic License" which comes with this distribution. + +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 either +the GNU General Public License or the Artistic License for more details. + +You should have received a copy of the Artistic License with this +distribution, in the file named "Artistic". If not, I'll be glad to provide +one. + +You should also have received a copy of the GNU General Public License +along with this program in the file named "Copying". If not, write to the + + Free Software Foundation, Inc. + 59 Temple Place, Suite 330 + Boston, MA 02111-1307, USA + +or visit their web page on the internet at: + + http://www.gnu.org/copyleft/gpl.html. =head1 AUTHOR Michael Robinton +=head1 SEE ALSO + +NetAddr::IP(3), NetAddr::IP::Lite(3), NetAddr::IP::InetBase(3) + =cut 1; diff --git a/Lite/Util/Util.xs b/Lite/Util/Util.xs index 9d56f0c..4a6b765 100644 --- a/Lite/Util/Util.xs +++ b/Lite/Util/Util.xs @@ -44,7 +44,7 @@ #endif /* workaround for OS's without inet_aton */ -#include "xs_include/inet_aton.c" +/* #include "xs_include/inet_aton.c" removed 10-12-11 */ typedef union { @@ -54,7 +54,8 @@ char * is_ipv6to4 = "ipv6to4", * is_shiftleft = "shiftleft", * is_comp128 = "comp128"; char * is_sub128 = "sub128", * is_add128 = "add128"; -char * is_hasbits = "hasbits", * is_isIPv4 = "isIPv4"; +char * is_hasbits = "hasbits"; +/* , * is_isIPv4 = "isIPv4"; */ char * is_bcd2bin = "bcd2bin", * is_simple_pack = "simple_pack", * is_bcdn2bin = "bcdn2bin"; char * is_mask4to6 = "mask4to6", * is_ipv4to6 = "ipv4to6"; char * is_maskanyto6 = "maskanyto6", * is_ipanyto6 = "ipanyto6"; @@ -468,12 +469,14 @@ return j; } +/* INCLUDE: xs_include/miniSocket.inc removed 10-12-11 */ + + + MODULE = NetAddr::IP::Util PACKAGE = NetAddr::IP::Util PROTOTYPES: ENABLE -INCLUDE: xs_include/miniSocket.inc - void comp128(s,...) SV * s @@ -601,8 +604,6 @@ int hasbits(s) SV * s -ALIAS: - NetAddr::IP::Util::isIPv4 = 1 PREINIT: unsigned char * bp; char * subname; @@ -610,19 +611,11 @@ CODE: bp = (unsigned char *) SvPV(s,len); if (len != 16) { - if (ix == 1) - subname = is_isIPv4; - else - subname = is_hasbits; + subname = is_hasbits; croak("Bad arg length for %s%s, length is %d, should be %d", "NetAddr::IP::Util::",subname,len *8,128); } - if (ix == 1) { - RETVAL = _isipv4(bp); - } - else { - RETVAL = have128(bp); - } + RETVAL = have128(bp); OUTPUT: RETVAL diff --git a/Lite/Util/lib/NetAddr/IP/InetBase.pm b/Lite/Util/lib/NetAddr/IP/InetBase.pm new file mode 100644 index 0000000..1d1fe1b --- /dev/null +++ b/Lite/Util/lib/NetAddr/IP/InetBase.pm @@ -0,0 +1,707 @@ +#!/usr/bin/perl +package NetAddr::IP::InetBase; + +use strict; +#use diagnostics; +#use lib qw(blib lib); + +use vars qw($VERSION @EXPORT_OK @ISA %EXPORT_TAGS $Mode); +use AutoLoader qw(AUTOLOAD); +require Exporter; + +@ISA = qw(Exporter); + +$VERSION = do { my @r = (q$Revision: 0.01 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; + +@EXPORT_OK = qw( + inet_aton + inet_ntoa + ipv6_aton + ipv6_ntoa + ipv6_n2x + ipv6_n2d + inet_any2n + inet_n2dx + inet_n2ad + inet_ntop + inet_pton + packzeros + isIPv4 + isNewIPv4 + isAnyIPv4 + AF_INET + AF_INET6 + fake_AF_INET6 +); + +%EXPORT_TAGS = ( + all => [@EXPORT_OK], + ipv4 => [qw( + inet_aton + inet_ntoa + )], + ipv6 => [qw( + ipv6_aton + ipv6_ntoa + ipv6_n2x + ipv6_n2d + inet_any2n + inet_n2dx + inet_n2ad + inet_pton + inet_ntop + packzeros + )], +); + +# prototypes +sub inet_ntoa; +sub ipv6_aton; +sub ipv6_ntoa; +sub inet_any2n($); +sub inet_n2dx($); +sub inet_n2ad($); +sub _inet_ntop; +sub _inet_pton; + +my $emulateAF_INET6 = 0; + +{ no warnings 'once'; + +*packzeros = \&_packzeros; + +## dynamic configuraton for IPv6 + +require Socket; + +*AF_INET = \&Socket::AF_INET; + +if (eval { AF_INET6() } ) { + *AF_INET6 = \&Socket::AF_INET6; + $emulateAF_INET6 = -1; # have it, remind below +} +if (eval{ require Socket6 } ) { + if ($emulateAF_INET6) { + import Socket6 qw( + inet_pton + inet_ntop + ); + } else { + import Socket6 qw( + inet_pton + inet_ntop + AF_INET6 + ); + } + $emulateAF_INET6 = 0; # clear, have it from elsewhere or here +} else { + unless ($emulateAF_INET6) { # unlikely at this point + require Config; # make an educated guess about AF_INET6 + my $osname = $Config::Config{osname}; + if ($osname =~ /(?:free|dragon.+)bsd/i) { # FreeBSD, DragonFlyBSD + $emulateAF_INET6 = 28; + } elsif ($osname =~ /bsd/i) { # other BSD flavors like NetBDS, OpenBSD, BSD + $emulateAF_INET6 = 24; + } elsif ($osname =~ /(?:darwin|mac)/i) { # Mac OS X + $emulateAF_INET6 = 30; + } elsif ($osname =~ /win/i) { # Windows + $emulateAF_INET6 = 23; + } elsif ($osname =~ /(?:solaris|sun)/i) { # Sun box + $emulateAF_INET6 = 26; + } else { # use linux default + $emulateAF_INET6 = 10; + } + *AF_INET6 = sub { $emulateAF_INET6; }; + } else { + $emulateAF_INET6 = 0; # clear, have it from elsewhere + } + *inet_pton = \&_inet_pton; + *inet_ntop = \&_inet_ntop; +} + +} # end no warnings 'once' + +sub fake_AF_INET6 { + return $emulateAF_INET6; +} + +# allow user to choose upper or lower case +BEGIN { + use vars qw($n2x_format $n2d_format); + $n2x_format = "%x:%x:%x:%x:%x:%x:%x:%x"; + $n2d_format = "%x:%x:%x:%x:%x:%x:%d.%d.%d.%d"; +} + +my $case = 0; # default lower case + +sub upper { $n2x_format = uc($n2x_format); $n2d_format = uc($n2d_format); $case = 1; } +sub lower { $n2x_format = lc($n2x_format); $n2d_format = lc($n2d_format); $case = 0; } + +sub ipv6_n2x { + die "Bad arg length for 'ipv6_n2x', length is ". length($_[0]) ." should be 16" + unless length($_[0]) == 16; + return sprintf($n2x_format,unpack("n8",$_[0])); +} + +sub ipv6_n2d { + die "Bad arg length for 'ipv6_n2d', length is ". length($_[0]) ." should be 16" + unless length($_[0]) == 16; + my @hex = (unpack("n8",$_[0])); + $hex[9] = $hex[7] & 0xff; + $hex[8] = $hex[7] >> 8; + $hex[7] = $hex[6] & 0xff; + $hex[6] >>= 8; + return sprintf($n2d_format,@hex); +} + +# if Socket lib is broken in some way, check for overange values +# +#my $overange = yinet_aton('256.1') ? 1:0; +my $overange = gethostbyname('256.1') ? 1:0; + +sub inet_aton { + unless (! $overange || $_[0] =~ /[^0-9\.]/) { # hostname + my @dq = split(/\./,$_[0]); + foreach (@dq) { + return undef if $_ > 255; + } + } + scalar gethostbyname($_[0]); +} + +my $_zero = pack('L4',0,0,0,0); +my $_ipv4mask = pack('L4',0xffffffff,0xffffffff,0xffffffff,0); + +sub isIPv4 { + if (length($_[0]) != 16) { + my $sub = (caller(1))[3] || (caller(0))[3]; + die "Bad arg length for $sub, length is ". (length($_[0]) *8) .", should be 128"; + } + return ($_[0] & $_ipv4mask) eq $_zero + ? 1 : 0; +} + +my $_newV4compat = pack('L4',0,0,0xffffffff,0); + +sub isNewIPv4 { + my $naddr = $_[0] ^ $_newV4compat; + return isIPv4($naddr); +} + +sub isAnyIPv4 { + my $naddr = $_[0]; + my $rv = isIPv4($_[0]); + return $rv if $rv; + return isNewIPv4($naddr); +} + +sub DESTROY {}; + +sub import { + if (grep { $_ eq ':upper' } @_) { + upper(); + @_ = grep { $_ ne ':upper' } @_; + } + NetAddr::IP::InetBase->export_to_level(1,@_); +} + +1; + +__END__ + +=head1 NAME + +NetAddr::IP::InetBase -- IPv4 and IPV6 utilities + +=head1 SYNOPSIS + + use NetAddr::IP::Base qw( + :upper + inet_aton + inet_ntoa + ipv6_aton + ipv6_ntoa + ipv6_n2x + ipv6_n2d + inet_any2n + inet_n2dx + inet_n2ad + inet_pton + inet_ntop + packzeros + isIPv4 + isNewIPv4 + isAnyIPv4 + AF_INET + AF_INET6 + fake_AF_INET6 + ); + + use NetAddr::IP::Util qw(:all :inet :ipv4 :ipv6 :math) + + :ipv4 => inet_aton, inet_ntoa + + :ipv6 => ipv6_aton, ipv6_ntoa,ipv6_n2x, ipv6_n2d, + inet_any2n, inet_n2dx, inet_n2ad + inet_pton, inet_ntop, packzeros + + $dotquad = inet_ntoa($netaddr); + $netaddr = inet_aton($dotquad); + $ipv6naddr = ipv6_aton($ipv6_text); + $ipv6_text = ipv6_ntoa($ipv6naddr); + $hex_text = ipv6_n2x($ipv6naddr); + $dec_text = ipv6_n2d($ipv6naddr); + $ipv6naddr = inet_any2n($dotquad or $ipv6_text); + $dotquad or $hex_text = inet_n2dx($ipv6naddr); + $dotquad or $dec_text = inet_n2ad($ipv6naddr); + $netaddr = inet_pton($AF_family,$text_addr); + $text_addr = inet_ntop($AF_family,$netaddr); + $hex_text = packzeros($hex_text); + $rv = isIPv4($bits128); + $rv = isNewIPv4($bits128); + $rv = isAnyIPv4($bits128); + $constant = AF_INET(); + $constant = AF_INET6(); + $trueif = fake_AF_INET6(); + + NetAddr::IP::InetBase::lower(); + NetAddr::IP::InetBase::upper(); + +=head1 INSTALLATION + +Un-tar the distribution in an appropriate directory and type: + + perl Makefile.PL + make + make test + make install + +=head1 DESCRIPTION + +B provides a suite network of conversion functions +written in pure Perl for converting both IPv4 and IPv6 addresses to +and from network address format and text format. + +The IPv6 functions support all rfc1884 formats. + + i.e. x:x:x:x:x:x:x:x:x + x:x:x:x:x:x:x:d.d.d.d + ::x:x:x + ::x:d.d.d.d + and so on... + +=over 4 + +=item * $dotquad = inet_ntoa($netaddr); + +Convert a packed IPv4 network address to a dot-quad IP address. + + input: packed network address + returns: IP address i.e. 10.4.12.123 + +=cut + +sub inet_ntoa { + die 'Bad arg length for '. __PACKAGE__ ."::inet_ntoa, length is ". length($_[0]) ." should be 4" + unless length($_[0]) == 4; + my @hex = (unpack("n2",$_[0])); + $hex[3] = $hex[1] & 0xff; + $hex[2] = $hex[1] >> 8; + $hex[1] = $hex[0] & 0xff; + $hex[0] >>= 8; + return sprintf("%d.%d.%d.%d",@hex); +} + +=item * $netaddr = inet_aton($dotquad); + +Convert a dot-quad IP address into an IPv4 packed network address. + + input: IP address i.e. 192.5.16.32 + returns: packed network address + +=item * $ipv6addr = ipv6_aton($ipv6_text); + +Takes an IPv6 address of the form described in rfc1884 +and returns a 128 bit binary RDATA string. + + input: ipv6 text + returns: 128 bit RDATA string + +=cut + +sub ipv6_aton { + my($ipv6) = @_; + return undef unless $ipv6; + local($1,$2,$3,$4,$5); + if ($ipv6 =~ /^(.*:)(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) { # mixed hex, dot-quad + return undef if $2 > 255 || $3 > 255 || $4 > 255 || $5 > 255; + $ipv6 = sprintf("%s%X%02X:%X%02X",$1,$2,$3,$4,$5); # convert to pure hex + } + my $c; + return undef if + $ipv6 =~ /[^:0-9a-fA-F]/ || # non-hex character + (($c = $ipv6) =~ s/::/x/ && $c =~ /(?:x|:):/) || # double :: ::? + $ipv6 =~ /[0-9a-fA-F]{5,}/; # more than 4 digits + $c = $ipv6 =~ tr/:/:/; # count the colons + return undef if $c < 7 && $ipv6 !~ /::/; + if ($c > 7) { # strip leading or trailing :: + return undef unless + $ipv6 =~ s/^::/:/ || + $ipv6 =~ s/::$/:/; + return undef if --$c > 7; + } + while ($c++ < 7) { # expand compressed fields + $ipv6 =~ s/::/:::/; + } + $ipv6 .= 0 if $ipv6 =~ /:$/; + my @hex = split(/:/,$ipv6); + foreach(0..$#hex) { + $hex[$_] = hex($hex[$_] || 0); + } + pack("n8",@hex); +} + +=item * $ipv6text = ipv6_ntoa($ipv6naddr); + +Convert a 128 bit binary IPv6 address to compressed rfc 1884 +text representation. + + input: 128 bit RDATA string + returns: ipv6 text + +=cut + +sub ipv6_ntoa { + return inet_ntop(AF_INET6(),$_[0]); +} + +=item * $hex_text = ipv6_n2x($ipv6addr); + +Takes an IPv6 RDATA string and returns an 8 segment IPv6 hex address + + input: 128 bit RDATA string + returns: x:x:x:x:x:x:x:x + + Note: this function does NOT compress adjacent + strings of 0:0:0:0 into the :: format + +=item * $dec_text = ipv6_n2d($ipv6addr); + +Takes an IPv6 RDATA string and returns a mixed hex - decimal IPv6 address +with the 6 uppermost chunks in hex and the lower 32 bits in dot-quad +representation. + + input: 128 bit RDATA string + returns: x:x:x:x:x:x:d.d.d.d + + Note: this function does NOT compress adjacent + strings of 0:0:0:0 into the :: format + +=item * $ipv6naddr = inet_any2n($dotquad or $ipv6_text); + +This function converts a text IPv4 or IPv6 address in text format in any +standard notation into a 128 bit IPv6 string address. It prefixes any +dot-quad address (if found) with '::' and passes it to B. + + input: dot-quad or rfc1844 address + returns: 128 bit IPv6 string + +=cut + +sub inet_any2n($) { + my($addr) = @_; + $addr = '' unless $addr; + $addr = '::' . $addr + unless $addr =~ /:/; + return ipv6_aton($addr); +} + +=item * $dotquad or $hex_text = inet_n2dx($ipv6naddr); + +This function B and returns the text for either a +dot-quad IPv4 or a hex notation IPv6 address. + + input: 128 bit IPv6 string + returns: ddd.ddd.ddd.ddd + or x:x:x:x:x:x:x:x + + Note: this function does NOT compress adjacent + strings of 0:0:0:0 into the :: format + +=cut + +sub inet_n2dx($) { + my($nadr) = @_; + if (isAnyIPv4($nadr)) { + local $1; + ipv6_n2d($nadr) =~ /([^:]+)$/; + return $1; + } + return ipv6_n2x($nadr); +} + +=item * $dotquad or $dec_text = inet_n2ad($ipv6naddr); + +This function B and returns the text for either a +dot-quad IPv4 or a hex::decimal notation IPv6 address. + + input: 128 bit IPv6 string + returns: ddd.ddd.ddd.ddd + or x:x:x:x:x:x:ddd.ddd.ddd.dd + + Note: this function does NOT compress adjacent + strings of 0:0:0:0 into the :: format + +=cut + +sub inet_n2ad($) { + my($nadr) = @_; + my $addr = ipv6_n2d($nadr); + return $addr unless isAnyIPv4($nadr); + local $1; + $addr =~ /([^:]+)$/; + return $1; +} + +=item * $netaddr = inet_pton($AF_family,$text_addr); + +This function takes an IP address in IPv4 or IPv6 text format and converts it into +binary format. The type of IP address conversion is controlled by the FAMILY +argument. + +NOTE: inet_pton, inet_ntop and AF_INET6 come from the Socket6 library if it +is present on this host. + +=cut + +sub _inet_pton { + my($af,$ip) = @_; + die 'Bad address family for '. __PACKAGE__ ."::inet_pton, got $af" + unless $af == AF_INET6() || $af == AF_INET(); + if ($af == AF_INET()) { + inet_aton($ip); + } else { + ipv6_aton($ip); + } +} + +=item * $text_addr = inet_ntop($AF_family,$netaddr); + +This function takes and IP address in binary format and converts it into +text format. The type of IP address conversion is controlled by the FAMILY +argument. + +NOTE: inet_ntop ALWAYS returns lowercase characters. + +NOTE: inet_pton, inet_ntop and AF_INET6 come from the Socket6 library if it +is present on this host. + +=cut + +sub _inet_ntop { + my($af,$naddr) = @_; + die 'Unsupported address family for '. __PACKAGE__ ."::inet_ntop, af is $af" + unless $af == AF_INET6() || $af == AF_INET(); + if ($af == AF_INET()) { + inet_ntoa($naddr); + } else { + return ($case) + ? lc packzeros(ipv6_n2x($naddr)) + : _packzeros(ipv6_n2x($naddr)); + } +} + +=item * $hex_text = packzeros($hex_text); + +This function optimizes and rfc 1884 IPv6 hex address to reduce the number of +long strings of zero bits as specified in rfc 1884, 2.2 (2) by substituting +B<::> for the first occurence of the longest string of zeros in the address. + +=cut + +sub _packzeros { + my $x6 = shift; + if ($x6 =~ /\:\:/) { # already contains :: +# then re-optimize + $x6 = ($x6 =~ /\:\d+\.\d+\.\d+\.\d+/) # ipv4 notation ? + ? ipv6_n2d(ipv6_aton($x6)) + : ipv6_n2x(ipv6_aton($x6)); + } + $x6 = ':'. lc $x6; # prefix : & always lower case + my $d = ''; + if ($x6 =~ /(.+\:)(\d+\.\d+\.\d+\.\d+)/) { # if contains dot quad + $x6 = $1; # save hex piece + $d = $2; # and dot quad piece + } + $x6 .= ':'; # suffix : + $x6 =~ s/\:0+/\:0/g; # compress strings of 0's to single '0' + $x6 =~ s/\:0([1-9a-f]+)/\:$1/g; # eliminate leading 0's in hex strings + my @x = $x6 =~ /(?:\:0)*/g; # split only strings of :0:0..." + + my $m = 0; + my $i = 0; + + for (0..$#x) { # find next longest pattern :0:0:0... + my $len = length($x[$_]); + next unless $len > $m; + $m = $len; + $i = $_; # index to first longest pattern + } + + if ($m > 2) { # there was a string of 2 or more zeros + $x6 =~ s/$x[$i]/\:/; # replace first longest :0:0:0... with "::" + unless ($i) { # if it is the first match, $i = 0 + $x6 = substr($x6,0,-1); # keep the leading ::, remove trailing ':' + } else { + $x6 = substr($x6,1,-1); # else remove leading & trailing ':' + } + $x6 .= ':' unless $x6 =~ /\:\:/; # restore ':' if match and we can't see it, implies trailing '::' + } else { # there was no match + $x6 = substr($x6,1,-1); # remove leading & trailing ':' + } + $x6 .= $d; # append digits if any + return $case + ? uc $x6 + : $x6; +} + +=item * $ipv6naddr = ipv4to6($netaddr); + +Convert an ipv4 network address into an ipv6 network address. + + input: 32 bit network address + returns: 128 bit network address + +=item * $rv = isIPv4($bits128); + +This function returns true if there are no on bits present in the IPv6 +portion of the 128 bit string and false otherwise. + + i.e. the address must be of the form - ::d.d.d.d + +Note: this is an old and deprecated ipV4 compatible ipV6 address + +=item * $rv = isNewIPv4($bits128); + +This function return true if the IPv6 128 bit string is of the form + + ::ffff::d.d.d.d + +=item * $rv = isAnyIPv4($bits128); + +This function return true if the IPv6 bit string is of the form + + ::d.d.d.d or ::ffff::d.d.d.d + +=item * NetAddr::IP::InetBase::lower(); + +Return IPv6 strings in lowercase. This is the default. + +=item * NetAddr::IP::InetBase::upper(); + +Return IPv6 strings in uppercase. + +The default may be set to uppercase when the module is loaded by invoking +the TAG :upper. i.e. + + use NetAddr::IP::InetBase qw( :upper ); + +=item * $constant = AF_INET; + +This function returns the system value for AF_INET. + +=item * $constant = AF_INET6; + +AF_INET6 is sometimes present in the Socket library and always present in the Socket6 library. When the Socket +library does not contain AF_INET6 and when Socket6 is not present, a place holder value is C based on +the underlying host operating system. See B below. + +NOTE: inet_pton, inet_ntop and AF_INET6 come from the Socket6 library if it +is present on this host. + +=item * $trueif = fake_AF_INET6; + +This function return FALSE if AF_INET6 is provided by Socket or Socket6. Otherwise, it returns the best guess +value based on name of the host operating system. + +=back + +=head1 EXPORT_OK + + :upper + inet_aton + inet_ntoa + ipv6_aton + ipv6_ntoa + ipv6_n2x + ipv6_n2d + inet_any2n + inet_n2dx + inet_n2ad + inet_pton + inet_ntop + packzeros + isIPv4 + isNewIPv4 + isAnyIPv4 + AF_INET + AF_INET6 + fake_AF_INET6 + +=head1 %EXPORT_TAGS + + :all + :ipv4 + :ipv6 + :upper + +=head1 AUTHOR + +Michael Robinton + +=head1 COPYRIGHT + +Copyright 2003 - 2011, Michael Robinton Emichael@bizsystems.comE + +All rights reserved. + +This program is free software; you can redistribute it and/or modify +it under the terms of either: + + a) the GNU General Public License as published by the Free + Software Foundation; either version 2, or (at your option) any + later version, or + + b) the "Artistic License" which comes with this distribution. + +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 either +the GNU General Public License or the Artistic License for more details. + +You should have received a copy of the Artistic License with this +distribution, in the file named "Artistic". If not, I'll be glad to provide +one. + +You should also have received a copy of the GNU General Public License +along with this program in the file named "Copying". If not, write to the + + Free Software Foundation, Inc. + 59 Temple Place, Suite 330 + Boston, MA 02111-1307, USA + +or visit their web page on the internet at: + + http://www.gnu.org/copyleft/gpl.html. + +=head1 AUTHOR + +Michael Robinton + +=head1 SEE ALSO + +NetAddr::IP(3), NetAddr::IP::Lite(3), NetAddr::IP::Util(3) + +=cut + +1; diff --git a/Lite/Util/lib/NetAddr/IP/UtilPP.pm b/Lite/Util/lib/NetAddr/IP/UtilPP.pm index ef8c958..9e7bc53 100644 --- a/Lite/Util/lib/NetAddr/IP/UtilPP.pm +++ b/Lite/Util/lib/NetAddr/IP/UtilPP.pm @@ -12,11 +12,10 @@ @ISA = qw(Exporter); -$VERSION = do { my @r = (q$Revision: 1.7 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; +$VERSION = do { my @r = (q$Revision: 1.8 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; @EXPORT_OK = qw( hasbits - isIPv4 shiftleft addconst add128 @@ -53,7 +52,6 @@ use NetAddr::IP::UtilPP qw( hasbits - isIPv4 shiftleft addconst add128 @@ -71,7 +69,6 @@ use NetAddr::IP::UtilPP qw(:all) $rv = hasbits($bits128); - $rv = isIPv4($bits128); $bitsX2 = shiftleft($bits128,$n); $carry = addconst($ipv6naddr,$signed_32con); ($carry,$ipv6naddr)=addconst($ipv6naddr,$signed_32con); @@ -122,31 +119,34 @@ $len *= 8; $should = 128 unless $should; my $sub = (caller(1))[3]; - die "Bad argument length for ".__PACKAGE__.":$sub, is $len, should be $should"; + die "Bad argument length for $sub, is $len, should be $should"; } sub hasbits { _deadlen(length($_[0])) if length($_[0]) != 16; + return 1 if vec($_[0],0,32); + return 1 if vec($_[0],1,32); + return 1 if vec($_[0],2,32); return 1 if vec($_[0],3,32); - return (isIPv4($_[0])) ? 0 : 1; + return 0; } -=item * $rv = isIPv4($bits128); - -This function returns true if there are no on bits present in the IPv6 -portion of the 128 bit string and false otherwise. - -=cut - -sub isIPv4 { - _deadlen(length($_[0])) - if length($_[0]) != 16; - return 0 if vec($_[0],0,32); - return 0 if vec($_[0],1,32); - return 0 if vec($_[0],2,32); - return 1; -} +#=item * $rv = isIPv4($bits128); +# +#This function returns true if there are no on bits present in the IPv6 +#portion of the 128 bit string and false otherwise. +# +#=cut +# +#sub xisIPv4 { +# _deadlen(length($_[0])) +# if length($_[0]) != 16; +# return 0 if vec($_[0],0,32); +# return 0 if vec($_[0],1,32); +# return 0 if vec($_[0],2,32); +# return 1; +#} =item * $bitsXn = shiftleft($bits128,$n); @@ -655,7 +655,6 @@ =head1 EXPORT_OK hasbits - isIPv4 shiftleft addconst add128 @@ -681,22 +680,38 @@ =head1 COPYRIGHT -Copyright 2006 - 2008, Michael Robinton +Copyright 2003 - 2011, Michael Robinton Emichael@bizsystems.comE + +All rights reserved. This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License (except as noted -otherwise in individuals sub modules) published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. +it under the terms of either: + + a) the GNU General Public License as published by the Free + Software Foundation; either version 2, or (at your option) any + later version, or + + b) the "Artistic License" which comes with this distribution. 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. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either +the GNU General Public License or the Artistic 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +You should have received a copy of the Artistic License with this +distribution, in the file named "Artistic". If not, I'll be glad to provide +one. + +You should also have received a copy of the GNU General Public License +along with this program in the file named "Copying". If not, write to the + + Free Software Foundation, Inc. + 59 Temple Place, Suite 330 + Boston, MA 02111-1307, USA + +or visit their web page on the internet at: + + http://www.gnu.org/copyleft/gpl.html. =head1 AUTHOR diff --git a/Lite/Util/t/af_inet6.t b/Lite/Util/t/af_inet6.t new file mode 100644 index 0000000..e07b7a0 --- /dev/null +++ b/Lite/Util/t/af_inet6.t @@ -0,0 +1,46 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..3\n"; } +END {print "not ok 1\n" unless $loaded;} + +#use diagnostics; +use Config; +use NetAddr::IP::InetBase qw( + AF_INET + AF_INET6 + fake_AF_INET6 +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +## test 2 - 13 add stuff to buffer +my $af; +print "AF_INET = 2 not found in the Socket library\nnot " + unless ($af = AF_INET()) && $af == 2; +&ok; + +my $fake = fake_AF_INET6(); +my $af_inet6 = AF_INET6(); +my $txt = $fake + ? "\n\tSocket does not have AF_INET6, Socket6 not present\n\tguessed AF_INET6 for '$Config{osname}' = $fake\n" + : "\n\tAF_INET6 = $af_inet6 derived from Socket or Socket6\n"; +print STDERR $txt; +&ok; diff --git a/Lite/Util/t/binet_n2ad.t b/Lite/Util/t/binet_n2ad.t new file mode 100644 index 0000000..1d575e0 --- /dev/null +++ b/Lite/Util/t/binet_n2ad.t @@ -0,0 +1,49 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..4\n"; } +END {print "not ok 1\n" unless $loaded;} + +use NetAddr::IP::InetBase qw( + :upper + ipv6_aton + ipv6_n2x + inet_any2n + inet_n2ad +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +my @num = qw # input expected +( a1b2:c3d4:e5d6:f7e8:08f9:190a:2a1b:3b4c A1B2:C3D4:E5D6:F7E8:8F9:190A:42.27.59.76 + 1.2.3.4 1.2.3.4 + 190A::102:304 190A:0:0:0:0:0:1.2.3.4 +); + +my $ff = ipv6_aton($num[1]); +for(my $i=0;$i<@num;$i+=2) { + my $num = $num[$i]; + my $bstr = inet_any2n($num); + my $rv = inet_n2ad($bstr); + my $exp = $num[$i +1]; + print "got: $rv\nexp: $exp\nnot " + unless $rv eq $exp; + &ok; +} diff --git a/Lite/Util/t/binet_n2dx.t b/Lite/Util/t/binet_n2dx.t new file mode 100644 index 0000000..98ebba3 --- /dev/null +++ b/Lite/Util/t/binet_n2dx.t @@ -0,0 +1,50 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..6\n"; } +END {print "not ok 1\n" unless $loaded;} + +use NetAddr::IP::InetBase qw( + :upper + ipv6_aton + inet_any2n + inet_n2dx +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +my @num = qw # input expected +( a1b2:c3d4:e5d6:f7e8:08f9:190a:2a1b:3b4c A1B2:C3D4:E5D6:F7E8:8F9:190A:2A1B:3B4C + 1.2.3.4 1.2.3.4 + A1B2:C3D4:E5D6:F7E8:08F9:190A:1.2.3.4 A1B2:C3D4:E5D6:F7E8:8F9:190A:102:304 + ::1.2.3.4 1.2.3.4 + ::FFFF:FFFF:1.2.3.4 1.2.3.4 +); + +my $ff = ipv6_aton($num[1]); +for(my $i=0;$i<@num;$i+=2) { + my $num = $num[$i]; + my $bstr = inet_any2n($num); + my $rv = inet_n2dx($bstr); + my $exp = $num[$i +1]; + print "got: $rv\nexp: $exp\nnot " + unless $rv eq $exp; + &ok; +} diff --git a/Lite/Util/t/binet_ntoa.t b/Lite/Util/t/binet_ntoa.t new file mode 100644 index 0000000..5be3763 --- /dev/null +++ b/Lite/Util/t/binet_ntoa.t @@ -0,0 +1,66 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..20\n"; } +END {print "not ok 1\n" unless $loaded;} + +#use diagnostics; +use NetAddr::IP::InetBase qw( + ipv6_ntoa + inet_pton + AF_INET6 +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +## test 2 - 19 add stuff to buffer +my @num = # in exphex +qw( + :: :: + 43:: 43:: + ::21 ::21 + ::1:2:3:4:5:6:7 0:1:2:3:4:5:6:7 + 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:0 + 1::8 1::8 + FF00::FFFF ff00::ffff + FFFF::FFFF:FFFF ffff::ffff:ffff + A1B2:C3D4:E5D6:F7E8:08F9:190A:1.2.3.4 a1b2:c3d4:e5d6:f7e8:8f9:190a:102:304 +); + +for (my $i=0;$i<@num;$i+=2) { + my $bits = inet_pton(AF_INET6(),$num[$i]); + my $len = length($bits); + print "bad len = $len, exp: 16\nnot " + unless $len == 16; # 16 bytes x 8 bits + &ok; + my $ipv6x = ipv6_ntoa($bits); + print "got: $ipv6x\nexp: $num[$i +1]\nnot " + unless $ipv6x eq $num[$i +1]; + &ok; +} + +## test 32 check bad length ntop +my $try = '1234'; +my $notempty = eval { + ipv6_ntoa($try); +}; +print "failed bad argument length test for ipv6_ntoa\nnot " + unless $@ && $@ =~ /Bad arg/; +&ok; diff --git a/Lite/Util/t/binet_pton.t b/Lite/Util/t/binet_pton.t new file mode 100644 index 0000000..cd37372 --- /dev/null +++ b/Lite/Util/t/binet_pton.t @@ -0,0 +1,96 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..32\n"; } +END {print "not ok 1\n" unless $loaded;} + +#use diagnostics; +use NetAddr::IP::InetBase qw( + :upper + :ipv6 + AF_INET + AF_INET6 +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +## test 2 - 13 add stuff to buffer +my @num = # addr +qw( + 0.0.0.0 + 255.255.255.255 + 1.2.3.4 + 10.253.230.9 +); + +foreach (@num) { + my @digs = split(/\./,$_); + my $pkd = pack('C4',@digs); + my $naddr = inet_pton(AF_INET(),$_); + my $addr = join('.',unpack('C4',$naddr)); + my $num = inet_ntop(AF_INET(),$pkd); + + print "bits do not match\nnot " + unless $naddr eq $pkd; + &ok; + + print "inet_pton: $addr, exp: $_\nnot " + unless $addr eq $_; + &ok; + + print "inet_ntop: $num, exp: $_\nnot " + unless $num eq $_; + &ok; +} + +## test 14 - 31 +@num = # in exphex +qw( + :: :: + 43:: 43:: + ::21 ::21 + ::1:2:3:4:5:6:7 0:1:2:3:4:5:6:7 + 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:0 + 1::8 1::8 + FF00::FFFF ff00::ffff + FFFF::FFFF:FFFF ffff::ffff:ffff + A1B2:C3D4:E5D6:F7E8:08F9:190A:1.2.3.4 a1b2:c3d4:e5d6:f7e8:8f9:190a:102:304 +); + +for (my $i=0;$i<@num;$i+=2) { + my $bits = inet_pton(AF_INET6(),$num[$i]); + my $len = length($bits); + print "bad len = $len, exp: 16\nnot " + unless $len == 16; # 16 bytes x 8 bits + &ok; + my $ipv6x = inet_ntop(AF_INET6(),$bits); + print "got: $ipv6x\nexp: $num[$i +1]\nnot " + unless $ipv6x eq $num[$i +1]; + &ok; +} + +## test 32 check bad length ntop +my $try = '1234'; +my $notempty = eval { + inet_ntop(AF_INET6(),$try); +}; +print "failed bad argument length test for inet_ntop\nnot " + unless $@ && $@ =~ /Bad arg/; +&ok; diff --git a/Lite/Util/t/bipv4_inet.t b/Lite/Util/t/bipv4_inet.t new file mode 100644 index 0000000..1a32ca3 --- /dev/null +++ b/Lite/Util/t/bipv4_inet.t @@ -0,0 +1,59 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..13\n"; } +END {print "not ok 1\n" unless $loaded;} + +#use diagnostics; +use NetAddr::IP::InetBase qw( + inet_ntoa + inet_aton +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +## test 2 add stuff to buffer +my @num = # addr +qw( + 0.0.0.0 + 255.255.255.255 + 1.2.3.4 + 10.253.230.9 +); + +foreach (@num) { + my @digs = split(/\./,$_); + my $pkd = pack('C4',@digs); + my $naddr = inet_aton($_); + my $addr = join('.',unpack('C4',$naddr)); + my $num = inet_ntoa($pkd); + + print "bits do not match\nnot " + unless $naddr eq $pkd; + &ok; + + print "inet_aton: $addr, exp: $_\nnot " + unless $addr eq $_; + &ok; + + print "inet_ntoa: $num, exp: $_\nnot " + unless $num eq $_; + &ok; +} diff --git a/Lite/Util/t/bipv6_any2n.t b/Lite/Util/t/bipv6_any2n.t new file mode 100644 index 0000000..bfdba4a --- /dev/null +++ b/Lite/Util/t/bipv6_any2n.t @@ -0,0 +1,48 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..4\n"; } +END {print "not ok 1\n" unless $loaded;} + +use NetAddr::IP::InetBase qw( + :upper + ipv6_aton + ipv6_n2x + inet_any2n +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +my @num = qw # input expected +( a1b2:c3d4:e5d6:f7e8:08f9:190a:2a1b:3b4c A1B2:C3D4:E5D6:F7E8:8F9:190A:2A1B:3B4C + 1.2.3.4 0:0:0:0:0:0:102:304 + A1B2:C3D4:E5D6:F7E8:08F9:190A:1.2.3.4 A1B2:C3D4:E5D6:F7E8:8F9:190A:102:304 +); + +my $ff = ipv6_aton($num[1]); +for(my $i=0;$i<@num;$i+=2) { + my $num = $num[$i]; + my $bstr = inet_any2n($num); + my $rv = ipv6_n2x($bstr); + my $exp = $num[$i +1]; + print "got: $rv\nexp: $exp\nnot " + unless $rv eq $exp; + &ok; +} diff --git a/Lite/Util/t/bipv6func.t b/Lite/Util/t/bipv6func.t new file mode 100644 index 0000000..2cff0d9 --- /dev/null +++ b/Lite/Util/t/bipv6func.t @@ -0,0 +1,76 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..27\n"; } +END {print "not ok 1\n" unless $loaded;} + +use NetAddr::IP::InetBase qw( + :upper + ipv6_aton + ipv6_n2x + ipv6_n2d +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +my @num = # in exphex expd +qw( + :: 0:0:0:0:0:0:0:0 0:0:0:0:0:0:0.0.0.0 + 43:: 43:0:0:0:0:0:0:0 43:0:0:0:0:0:0.0.0.0 + ::21 0:0:0:0:0:0:0:21 0:0:0:0:0:0:0.0.0.33 + ::1:2:3:4:5:6:7 0:1:2:3:4:5:6:7 0:1:2:3:4:5:0.6.0.7 + 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:0 1:2:3:4:5:6:0.7.0.0 + 1::8 1:0:0:0:0:0:0:8 1:0:0:0:0:0:0.0.0.8 + FF00::FFFF FF00:0:0:0:0:0:0:FFFF FF00:0:0:0:0:0:0.0.255.255 + FFFF::FFFF:FFFF FFFF:0:0:0:0:0:FFFF:FFFF FFFF:0:0:0:0:0:255.255.255.255 +); + +for (my $i=0;$i<@num;$i+=3) { + my $bits = ipv6_aton($num[$i]); + my $len = length($bits); + print "bad len = $len, exp: 16\nnot " + unless $len == 16; # 16 bytes x 8 bits + &ok; + my $ipv6x = ipv6_n2x($bits); + print "got: $ipv6x\nexp: $num[$i +1]\nnot " + unless $ipv6x eq $num[$i +1]; + &ok; + my $ipv6d = ipv6_n2d($bits); + print "got: $ipv6d\nexp: $num[$i +2]\nnot " + unless $ipv6d eq $num[$i +2]; + &ok; +} + +## test 26 check bad length n2x +my $try = '1234'; +my $notempty = eval { + ipv6_n2x($try); +}; +print "failed bad argument length test for ipv6_n2x\nnot " + unless $@ && $@ =~ /Bad arg/; +&ok; + +## test 27 check bad length n2d +$notempty = eval { + ipv6_n2d($try); +}; +print "failed bad argument length test for ipv6_n2d\nnot " + unless $@ && $@ =~ /Bad arg/; +&ok; diff --git a/Lite/Util/t/bisIPv4.t b/Lite/Util/t/bisIPv4.t new file mode 100644 index 0000000..bb51df9 --- /dev/null +++ b/Lite/Util/t/bisIPv4.t @@ -0,0 +1,187 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..421\n"; } +END {print "not ok 1\n" unless $loaded;} + +use NetAddr::IP::InetBase qw( + :upper + ipv6_aton + ipv6_n2x + isIPv4 + isNewIPv4 + isAnyIPv4 +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +my @num = qw # input +( + :: + 8000:: + 4000:: + 2000:: + 1000:: + 800:: + 400:: + 200:: + 100:: + 80:: + 40:: + 20:: + 10:: + 1:: + 0:8000:: + 0:4000:: + 0:2000:: + 0:1000:: + 0:800:: + 0:400:: + 0:200:: + 0:100:: + 0:80:: + 0:40:: + 0:20:: + 0:10:: + 0:1:: + 0:0:8000:: + 0:0:4000:: + 0:0:2000:: + 0:0:1000:: + 0:0:800:: + 0:0:400:: + 0:0:200:: + 0:0:100:: + 0:0:80:: + 0:0:40:: + 0:0:20:: + 0:0:10:: + 0:0:1:: + 0:0:0:8000:: + 0:0:0:4000:: + 0:0:0:2000:: + 0:0:0:1000:: + 0:0:0:800:: + 0:0:0:400:: + 0:0:0:200:: + 0:0:0:100:: + 0:0:0:80:: + 0:0:0:40:: + 0:0:0:20:: + 0:0:0:10:: + 0:0:0:1:: + 0:0:0:0:8000:: + 0:0:0:0:4000:: + 0:0:0:0:2000:: + 0:0:0:0:1000:: + 0:0:0:0:800:: + 0:0:0:0:400:: + 0:0:0:0:200:: + 0:0:0:0:100:: + 0:0:0:0:80:: + 0:0:0:0:40:: + 0:0:0:0:20:: + 0:0:0:0:10:: + 0:0:0:0:1:: + 0:0:0:0:0:8000:: + 0:0:0:0:0:4000:: + 0:0:0:0:0:2000:: + 0:0:0:0:0:1000:: + 0:0:0:0:0:800:: + 0:0:0:0:0:400:: + 0:0:0:0:0:200:: + 0:0:0:0:0:100:: + 0:0:0:0:0:80:: + 0:0:0:0:0:40:: + 0:0:0:0:0:20:: + 0:0:0:0:0:10:: + 0:0:0:0:0:1:: + ::8000:0 + ::4000:0 + ::2000:0 + ::1000:0 + ::800:0 + ::400:0 + ::200:0 + ::100:0 + ::80:0 + ::40:0 + ::20:0 + ::10:0 + ::1:0 + ::8000 + ::4000 + ::2000 + ::1000 + ::800 + ::400 + ::200 + ::100 + ::80 + ::40 + ::20 + ::10 + ::1 +); + +# check isIPv4 + +foreach (@num) { + my $bstr = ipv6_aton($_); + my $rv = isIPv4($bstr); + my $exp = ($_ =~ /\d::$/) ? 0:1; + print "got: $rv, exp: $exp for ", ipv6_n2x($bstr), "\nnot " + unless $rv eq $exp; + &ok; +} + +# check isAnyIPv4 +foreach (@num) { + my $bstr = ipv6_aton($_); + my $rv = isAnyIPv4($bstr); + my $exp = ($_ =~ /\d::$/) ? 0:1; + print "got: $rv, exp: $exp for ", ipv6_n2x($bstr), "\nnot " + unless $rv eq $exp; + &ok; +} + +my $compat = ipv6_aton('::FFFF:FFFF:0:0'); + +# check isAnyIPv4 with compatible high bits +foreach (@num) { + my $bstr = ipv6_aton($_); + $bstr ^= $compat; + my $rv = isAnyIPv4($bstr); + my $exp = ($_ =~ /\d::$/) ? 0:1; + print "got: $rv, exp: $exp for ", ipv6_n2x($bstr), "\nnot " + unless $rv eq $exp; + &ok; +} + +# check isNewIPv4 with compatible high bits +foreach (@num) { + my $bstr = ipv6_aton($_); + $bstr ^= $compat; + my $rv = isNewIPv4($bstr); + my $exp = ($_ =~ /\d::$/) ? 0:1; + print "got: $rv, exp: $exp for ", ipv6_n2x($bstr), "\nnot " + unless $rv eq $exp; + &ok; +} diff --git a/Lite/Util/t/bpackzeros.t b/Lite/Util/t/bpackzeros.t new file mode 100644 index 0000000..7afdd0d --- /dev/null +++ b/Lite/Util/t/bpackzeros.t @@ -0,0 +1,52 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..13\n"; } +END {print "not ok 1\n" unless $loaded;} + +use NetAddr::IP::InetBase qw( + packzeros +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +my @addr = qw # input expected +( D0:00:0000:0000:000:b00:0000:000 d0::b00:0:0 + 0d0:00:0000:0000:000:0b00:: d0::b00:0:0 + ::c3D4:E5d6:0:0:0:0 0:0:c3d4:e5d6:: + 0:0000:c3D4:e5d6:0:0:0:0 0:0:c3d4:e5d6:: + 0:0:0:0:0:0:0:0 :: + 0:0:: :: + ::0:000:0 :: + 0:0::1.2.3.4 ::1.2.3.4 + ::1.2.3.4 ::1.2.3.4 + ::01b2:c3D4:0:0:0:1.2.3.4 0:1b2:c3d4::1.2.3.4 + 0:0:0:0:a1B2:c3d4:: ::a1b2:c3d4:0:0 + 12:0:0:0:34:0:00:000 12::34:0:0:0 +); + +for(my $i=0;$i<@addr;$i+=2) { + my $addr = $addr[$i]; + my $rv = packzeros($addr); + my $exp = $addr[$i +1]; + print "got: $rv\nexp: $exp\nnot " + unless $rv eq $exp; + &ok; +} diff --git a/Lite/Util/t/inet_n2dx.t b/Lite/Util/t/inet_n2dx.t index ee60e4f..8af8287 100644 --- a/Lite/Util/t/inet_n2dx.t +++ b/Lite/Util/t/inet_n2dx.t @@ -5,7 +5,7 @@ # Change 1..1 below to 1..last_test_to_print . # (It may become useful if the test is moved to ./t subdirectory.) -BEGIN { $| = 1; print "1..4\n"; } +BEGIN { $| = 1; print "1..6\n"; } END {print "not ok 1\n" unless $loaded;} use NetAddr::IP::Util qw( @@ -34,6 +34,8 @@ ( a1b2:c3d4:e5d6:f7e8:08f9:190a:2a1b:3b4c A1B2:C3D4:E5D6:F7E8:8F9:190A:2A1B:3B4C 1.2.3.4 1.2.3.4 A1B2:C3D4:E5D6:F7E8:08F9:190A:1.2.3.4 A1B2:C3D4:E5D6:F7E8:8F9:190A:102:304 + ::1.2.3.4 1.2.3.4 + ::FFFF:FFFF:1.2.3.4 1.2.3.4 ); my $ff = ipv6_aton($num[1]); diff --git a/Lite/Util/t/inet_pton.t b/Lite/Util/t/inet_pton.t new file mode 100644 index 0000000..2058063 --- /dev/null +++ b/Lite/Util/t/inet_pton.t @@ -0,0 +1,96 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..32\n"; } +END {print "not ok 1\n" unless $loaded;} + +#use diagnostics; +use NetAddr::IP::Util qw( + inet_ntop + inet_pton + AF_INET + AF_INET6 +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +## test 2 - 13 add stuff to buffer +my @num = # addr +qw( + 0.0.0.0 + 255.255.255.255 + 1.2.3.4 + 10.253.230.9 +); + +foreach (@num) { + my @digs = split(/\./,$_); + my $pkd = pack('C4',@digs); + my $naddr = inet_pton(AF_INET(),$_); + my $addr = join('.',unpack('C4',$naddr)); + my $num = inet_ntop(AF_INET(),$pkd); + + print "bits do not match\nnot " + unless $naddr eq $pkd; + &ok; + + print "inet_pton: $addr, exp: $_\nnot " + unless $addr eq $_; + &ok; + + print "inet_ntop: $num, exp: $_\nnot " + unless $num eq $_; + &ok; +} + +## test 14 - 31 +@num = # in exphex +qw( + :: :: + 43:: 43:: + ::21 ::21 + ::1:2:3:4:5:6:7 0:1:2:3:4:5:6:7 + 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:0 + 1::8 1::8 + FF00::FFFF ff00::ffff + FFFF::FFFF:FFFF ffff::ffff:ffff + A1B2:C3D4:E5D6:F7E8:08F9:190A:1.2.3.4 a1b2:c3d4:e5d6:f7e8:8f9:190a:102:304 +); + +for (my $i=0;$i<@num;$i+=2) { + my $bits = inet_pton(AF_INET6(),$num[$i]); + my $len = length($bits); + print "bad len = $len, exp: 16\nnot " + unless $len == 16; # 16 bytes x 8 bits + &ok; + my $ipv6x = inet_ntop(AF_INET6(),$bits); + print "got: $ipv6x\nexp: $num[$i +1]\nnot " + unless $ipv6x eq $num[$i +1]; + &ok; +} + +## test 32 check bad length ntop +my $try = '1234'; +my $notempty = eval { + inet_ntop(AF_INET6(),$try); +}; +print "failed bad argument length test for inet_ntop\nnot " + unless $@ && $@ =~ /Bad arg/; +&ok; diff --git a/Lite/Util/t/ipv6_ntoa.t b/Lite/Util/t/ipv6_ntoa.t new file mode 100644 index 0000000..67cf2b1 --- /dev/null +++ b/Lite/Util/t/ipv6_ntoa.t @@ -0,0 +1,66 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..20\n"; } +END {print "not ok 1\n" unless $loaded;} + +#use diagnostics; +use NetAddr::IP::Util qw( + ipv6_ntoa + inet_pton + AF_INET6 +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +## test 2 - 19 add stuff to buffer +my @num = # in exphex +qw( + :: :: + 43:: 43:: + ::21 ::21 + ::1:2:3:4:5:6:7 0:1:2:3:4:5:6:7 + 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:0 + 1::8 1::8 + FF00::FFFF ff00::ffff + FFFF::FFFF:FFFF ffff::ffff:ffff + A1B2:C3D4:E5D6:F7E8:08F9:190A:1.2.3.4 a1b2:c3d4:e5d6:f7e8:8f9:190a:102:304 +); + +for (my $i=0;$i<@num;$i+=2) { + my $bits = inet_pton(AF_INET6(),$num[$i]); + my $len = length($bits); + print "bad len = $len, exp: 16\nnot " + unless $len == 16; # 16 bytes x 8 bits + &ok; + my $ipv6x = ipv6_ntoa($bits); + print "got: $ipv6x\nexp: $num[$i +1]\nnot " + unless $ipv6x eq $num[$i +1]; + &ok; +} + +## test 32 check bad length ntop +my $try = '1234'; +my $notempty = eval { + ipv6_ntoa($try); +}; +print "failed bad argument length test for ipv6_ntoa\nnot " + unless $@ && $@ =~ /Bad arg/; +&ok; diff --git a/Lite/Util/t/ipv6func.t b/Lite/Util/t/ipv6func.t index ddfa9c0..4ef0d9c 100644 --- a/Lite/Util/t/ipv6func.t +++ b/Lite/Util/t/ipv6func.t @@ -44,7 +44,7 @@ for (my $i=0;$i<@num;$i+=3) { my $bits = ipv6_aton($num[$i]); my $len = length($bits); - print "bad len = $len, exp: 32\nnot " + print "bad len = $len, exp: 16\nnot " unless $len == 16; # 16 bytes x 8 bits &ok; my $ipv6x = ipv6_n2x($bits); diff --git a/Lite/Util/t/naip_gethostbyname.t b/Lite/Util/t/naip_gethostbyname.t new file mode 100644 index 0000000..147254d --- /dev/null +++ b/Lite/Util/t/naip_gethostbyname.t @@ -0,0 +1,59 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..3\n"; } +END {print "not ok 1\n" unless $loaded;} + +#use diagnostics; +use NetAddr::IP::Util qw( + naip_gethostbyname + havegethostbyname2 + ipv6_n2x + inet_aton +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +my $exp = '0:0:0:0:FFFF:FFFF:7F00:1'; +my $host = '127.1'; +my $got = ipv6_n2x( scalar naip_gethostbyname($host)); +print "got: $got\nexp: $exp\nnot " + unless $got eq $exp; +&ok; + +$exp = '0:0:0:0:0:0:0:1'; +$host = $exp; + +if (havegethostbyname2()) { + $got = ipv6_n2x(scalar naip_gethostbyname($host)); + print "got: $got\nexp: $exp\nnot " + unless $got eq $exp; +} else { +print STDERR "\n\tgethostbyname2 missing from Socket6\n" + if eval{ require Socket6 }; + $got = scalar naip_gethostbyname($host); + if ($got) { + $got = eval{ inet_ntoa($got) } || + eval{ ipv6_n2x($got) }; + } + print "unexpected return value got: $got\nnot " + if $got; +} +&ok; diff --git a/Lite/Util/t/no6_naip_gethostbyname.t b/Lite/Util/t/no6_naip_gethostbyname.t new file mode 100644 index 0000000..a37f537 --- /dev/null +++ b/Lite/Util/t/no6_naip_gethostbyname.t @@ -0,0 +1,58 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..3\n"; } +END {print "not ok 1\n" unless $loaded;} + +#use diagnostics; +use NetAddr::IP::Util qw( + :noSock6 + naip_gethostbyname + havegethostbyname2 + ipv6_n2x + inet_aton +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +my $exp = '0:0:0:0:FFFF:FFFF:7F00:1'; +my $host = '127.1'; +my $got = ipv6_n2x( scalar naip_gethostbyname($host)); +print "got: $got\nexp: $exp\nnot " + unless $got eq $exp; +&ok; + +$exp = '0:0:0:0:0:0:0:1'; +$host = $exp; + +if (havegethostbyname2()) { + $got = ipv6_n2x(scalar naip_gethostbyname($host)); + print "got: $got\nexp: $exp\nnot " + unless $got eq $exp; +} else { + $got = scalar naip_gethostbyname($host); + if ($got) { + $got = eval{ inet_ntoa($got) } || + eval{ ipv6_n2x($got) }; + } + print "unexpected return value got: $got\nnot " + if $got; +} +&ok; diff --git a/Lite/Util/t/packzeros.t b/Lite/Util/t/packzeros.t new file mode 100644 index 0000000..6c89542 --- /dev/null +++ b/Lite/Util/t/packzeros.t @@ -0,0 +1,53 @@ +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl test.pl' + +######################### We start with some black magic to print on failure. +# Change 1..1 below to 1..last_test_to_print . +# (It may become useful if the test is moved to ./t subdirectory.) + +BEGIN { $| = 1; print "1..13\n"; } +END {print "not ok 1\n" unless $loaded;} + +use NetAddr::IP::Util qw( + + packzeros +); + +$loaded = 1; +print "ok 1\n"; +######################### End of black magic. + +# Insert your test code below (better if it prints "ok 13" +# (correspondingly "not ok 13") depending on the success of chunk 13 +# of the test code): + +$test = 2; + +sub ok { + print "ok $test\n"; + ++$test; +} + +my @addr = qw # input expected +( D0:00:0000:0000:000:b00:0000:000 d0::b00:0:0 + 0d0:00:0000:0000:000:0B00:: d0::b00:0:0 + ::c3D4:e5d6:0:0:0:0 0:0:c3d4:e5d6:: + 0:0000:c3D4:e5d6:0:0:0:0 0:0:c3d4:e5d6:: + 0:0:0:0:0:0:0:0 :: + 0:0:: :: + ::0:000:0 :: + 0:0::1.2.3.4 ::1.2.3.4 + ::1.2.3.4 ::1.2.3.4 + ::01b2:C3d4:0:0:0:1.2.3.4 0:1b2:c3d4::1.2.3.4 + 0:0:0:0:a1b2:c3D4:: ::a1b2:c3d4:0:0 + 12:0:0:0:34:0:00:000 12::34:0:0:0 +); + +for(my $i=0;$i<@addr;$i+=2) { + my $addr = $addr[$i]; + my $rv = packzeros($addr); + my $exp = lc $addr[$i +1]; + print "got: $rv\nexp: $exp\nnot " + unless $rv eq $exp; + &ok; +} diff --git a/Lite/Util/xs_include/inet_aton.c b/Lite/Util/xs_include/inet_aton.c deleted file mode 100644 index ec7a078..0000000 --- a/Lite/Util/xs_include/inet_aton.c +++ /dev/null @@ -1,43 +0,0 @@ -/* inet_aton.c - * - * Copyright 2006 - 2008, Michael Robinton - * - * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -*/ - -#ifndef HAVE_INET_ATON - -int -my_inet_aton(const char *cp, struct in_addr *inp) -{ -# ifdef HAVE_INET_PTON - return inet_pton(AF_INET,cp,inp); -# else -# ifdef HAVE_INET_ADDR - inp->s_addr = inet_addr(cp); - if (inp->s_addr == -1) { - if (strncmp("255.255.255.255",cp,15) == 0) - return 1; - else - return 0; - } - return 1; -# else -# error inet_aton, inet_pton, inet_addr not defined on this platform -# endif -# endif -} -#define inet_aton my_inet_aton -#endif diff --git a/Lite/Util/xs_include/miniSocket.inc b/Lite/Util/xs_include/miniSocket.inc deleted file mode 100644 index d6a8674..0000000 --- a/Lite/Util/xs_include/miniSocket.inc +++ /dev/null @@ -1,130 +0,0 @@ - - # This file is excerpted from perl-5.8.0/ext/Socket/Socket.xs and - # modified slightly so that it compiles on older versions of perl/gcc - # - # 3/28/06 version 1.78 of Socket.xs, included in perl 5.9.3 - # is 100% compatible with this version - # - # Copyright 2003 - 2006, Michael Robinton - -void -yinet_aton(host) - char * host - CODE: - { - struct in_addr ip_address; - struct hostent * phe; - int ok = - (host != NULL) && - (*host != '\0') && - inet_aton(host, &ip_address); - - if (!ok && (phe = gethostbyname(host))) { - Copy( phe->h_addr, &ip_address, phe->h_length, char ); - ok = 1; - } - - ST(0) = sv_newmortal(); - if (ok) - sv_setpvn( ST(0), (char *)&ip_address, sizeof ip_address ); - } - -void -inet_ntoa(ip_address_sv) - SV * ip_address_sv - CODE: - { - STRLEN addrlen; - struct in_addr addr; - char * addr_str; - char * ip_address; - # sigh.... these lines fail on older perl/gcc combinations - # if (DO_UTF8(ip_address_sv) && !sv_utf8_downgrade(ip_address_sv, 1)) - # croak("Wide character in Socket::inet_ntoa"); - # ip_address = SvPVbyte(ip_address_sv, addrlen); - ip_address = SvPV(ip_address_sv,addrlen); - if (addrlen == sizeof(addr) || addrlen == 4) - addr.s_addr = - (ip_address[0] & 0xFF) << 24 | - (ip_address[1] & 0xFF) << 16 | - (ip_address[2] & 0xFF) << 8 | - (ip_address[3] & 0xFF); - else - croak("Bad arg length for %s, length is %d, should be %d", - "NetAddr::IP::Util::inet_ntoa", - addrlen, sizeof(addr)); - /* We could use inet_ntoa() but that is broken - * in HP-UX + GCC + 64bitint (returns "0.0.0.0"), - * so let's use this sprintf() workaround everywhere. - * This is also more threadsafe than using inet_ntoa(). */ - Newx(addr_str, 4 * 3 + 3 + 1, char); /* IPv6? */ - sprintf(addr_str, "%d.%d.%d.%d", - ((addr.s_addr >> 24) & 0xFF), - ((addr.s_addr >> 16) & 0xFF), - ((addr.s_addr >> 8) & 0xFF), - ( addr.s_addr & 0xFF)); - ST(0) = sv_2mortal(newSVpvn(addr_str, strlen(addr_str))); - Safefree(addr_str); - } diff --git a/Lite/bug2742981 b/Lite/bug2742981 new file mode 100755 index 0000000..fa420b8 --- /dev/null +++ b/Lite/bug2742981 @@ -0,0 +1,96 @@ +#!/usr/bin/perl +use Math::BigInt; +use NetAddr::IP::Util qw( + bcd2bin + ipv6_n2x +); + +my $data = q| + got: '340282366920938963520000944000010176000' +# expected: '340282366920938463463374607431768211455' + +# Failed test 'cafe:cafe::/64 Scalar numeric ok' +# at t/v6-numeric.t line 57. +# got: '269827015721314205120005577600083392000' +# expected: '269827015721314068804783158349174669312' + +# Failed test 'cafe:cafe::/64 Array numeric ok for network' +# at t/v6-numeric.t line 58. +# got: '269827015721314205120005577600083392000' +# expected: '269827015721314068804783158349174669312' + +# Failed test 'cafe:cafe::/64 Array numeric ok for mask' +# at t/v6-numeric.t line 59. +# got: '340282366920938963520000944000010176000' +# expected: '340282366920938463444927863358058659840' + +# Failed test 'cafe:cafe::1/64 Scalar numeric ok' +# at t/v6-numeric.t line 57. +# got: '269827015721314205120005577600083392000' +# expected: '269827015721314068804783158349174669313' + +# Failed test 'cafe:cafe::1/64 Array numeric ok for network' +# at t/v6-numeric.t line 58. +# got: '269827015721314205120005577600083392000' +# expected: '269827015721314068804783158349174669313' + +# Failed test 'cafe:cafe::1/64 Array numeric ok for mask' +# at t/v6-numeric.t line 59. +# got: '340282366920938963520000944000010176000' +# expected: '340282366920938463444927863358058659840' + +# Failed test 'dead:beef::/100 Scalar numeric ok' +# at t/v6-numeric.t line 57. +# got: '295990755014136299520006003200014752000' +# expected: '295990755014133383690938178081940045824' + +# Failed test 'dead:beef::/100 Array numeric ok for network' +# at t/v6-numeric.t line 58. +# got: '295990755014136299520006003200014752000' +# expected: '295990755014133383690938178081940045824' + +# Failed test 'dead:beef::/100 Array numeric ok for mask' +# at t/v6-numeric.t line 59. +# got: '340282366920938963520000944000010176000' +# expected: '340282366920938463463374607431499776000' + +# Failed test 'dead:beef::1/100 Scalar numeric ok' +# at t/v6-numeric.t line 57. +# got: '295990755014136299520006003200014752000' +# expected: '295990755014133383690938178081940045825' + +# Failed test 'dead:beef::1/100 Array numeric ok for network' +# at t/v6-numeric.t line 58. +# got: '295990755014136299520006003200014752000' +# expected: '295990755014133383690938178081940045825' + +# Failed test 'dead:beef::1/100 Array numeric ok for mask' +# at t/v6-numeric.t line 59. +# got: '340282366920938963520000944000010176000' +# expected: '340282366920938463463374607431499776000' +|; + +my @trial = split("\n",$data); +my @data; +foreach(@trial) { + if ($_ =~ /(?:got|expected)\:\s+\'(\d+)/) { + push @data,$1; + } +} + +#for(my $i=0;$i <= $#data;$i +=2) { +# print $data[$i]," -\n"; +# print $data[$i +1]," =\n"; +# my $x = Math::BigInt->new($data[$i]); +# my $y = Math::BigInt->new($data[$i +1]); +# $x->bsub($y); +# print $x,"\n\n"; +#} + +for(my $i=0;$i <= $#data;$i +=2) { + my $x = ipv6_n2x(bcd2bin($data[$i])); + print $data[$i],"\t=> $x\n"; + my $y = ipv6_n2x(bcd2bin($data[$i +1])); + print $data[$i +1],"\t=> $y\n"; +} + diff --git a/Lite/bug2742981.pl b/Lite/bug2742981.pl deleted file mode 100755 index fa420b8..0000000 --- a/Lite/bug2742981.pl +++ /dev/null @@ -1,96 +0,0 @@ -#!/usr/bin/perl -use Math::BigInt; -use NetAddr::IP::Util qw( - bcd2bin - ipv6_n2x -); - -my $data = q| - got: '340282366920938963520000944000010176000' -# expected: '340282366920938463463374607431768211455' - -# Failed test 'cafe:cafe::/64 Scalar numeric ok' -# at t/v6-numeric.t line 57. -# got: '269827015721314205120005577600083392000' -# expected: '269827015721314068804783158349174669312' - -# Failed test 'cafe:cafe::/64 Array numeric ok for network' -# at t/v6-numeric.t line 58. -# got: '269827015721314205120005577600083392000' -# expected: '269827015721314068804783158349174669312' - -# Failed test 'cafe:cafe::/64 Array numeric ok for mask' -# at t/v6-numeric.t line 59. -# got: '340282366920938963520000944000010176000' -# expected: '340282366920938463444927863358058659840' - -# Failed test 'cafe:cafe::1/64 Scalar numeric ok' -# at t/v6-numeric.t line 57. -# got: '269827015721314205120005577600083392000' -# expected: '269827015721314068804783158349174669313' - -# Failed test 'cafe:cafe::1/64 Array numeric ok for network' -# at t/v6-numeric.t line 58. -# got: '269827015721314205120005577600083392000' -# expected: '269827015721314068804783158349174669313' - -# Failed test 'cafe:cafe::1/64 Array numeric ok for mask' -# at t/v6-numeric.t line 59. -# got: '340282366920938963520000944000010176000' -# expected: '340282366920938463444927863358058659840' - -# Failed test 'dead:beef::/100 Scalar numeric ok' -# at t/v6-numeric.t line 57. -# got: '295990755014136299520006003200014752000' -# expected: '295990755014133383690938178081940045824' - -# Failed test 'dead:beef::/100 Array numeric ok for network' -# at t/v6-numeric.t line 58. -# got: '295990755014136299520006003200014752000' -# expected: '295990755014133383690938178081940045824' - -# Failed test 'dead:beef::/100 Array numeric ok for mask' -# at t/v6-numeric.t line 59. -# got: '340282366920938963520000944000010176000' -# expected: '340282366920938463463374607431499776000' - -# Failed test 'dead:beef::1/100 Scalar numeric ok' -# at t/v6-numeric.t line 57. -# got: '295990755014136299520006003200014752000' -# expected: '295990755014133383690938178081940045825' - -# Failed test 'dead:beef::1/100 Array numeric ok for network' -# at t/v6-numeric.t line 58. -# got: '295990755014136299520006003200014752000' -# expected: '295990755014133383690938178081940045825' - -# Failed test 'dead:beef::1/100 Array numeric ok for mask' -# at t/v6-numeric.t line 59. -# got: '340282366920938963520000944000010176000' -# expected: '340282366920938463463374607431499776000' -|; - -my @trial = split("\n",$data); -my @data; -foreach(@trial) { - if ($_ =~ /(?:got|expected)\:\s+\'(\d+)/) { - push @data,$1; - } -} - -#for(my $i=0;$i <= $#data;$i +=2) { -# print $data[$i]," -\n"; -# print $data[$i +1]," =\n"; -# my $x = Math::BigInt->new($data[$i]); -# my $y = Math::BigInt->new($data[$i +1]); -# $x->bsub($y); -# print $x,"\n\n"; -#} - -for(my $i=0;$i <= $#data;$i +=2) { - my $x = ipv6_n2x(bcd2bin($data[$i])); - print $data[$i],"\t=> $x\n"; - my $y = ipv6_n2x(bcd2bin($data[$i +1])); - print $data[$i +1],"\t=> $y\n"; -} - diff --git a/Lite/t/bin_ips.t b/Lite/t/bin_ips.t new file mode 100644 index 0000000..884b2d4 --- /dev/null +++ b/Lite/t/bin_ips.t @@ -0,0 +1,102 @@ + +#use diagnostics; +use NetAddr::IP::Lite; + +$| = 1; + +sub ok() { + print 'ok ',$test++,"\n"; +} + +print "1..10\n"; + +$test = 1; + +############## test new6 + +my $exp = '0:0:0:0:0:0:0:3039/1'; + +my $ip = NetAddr::IP::Lite->new6(12345,1); +my $got = $ip->cidr(); +print "got: $got\nexp: $exp\nnot " + unless $got eq $exp; +&ok; + +$ip = NetAddr::IP::Lite->new6('12345',1); +$got = $ip->cidr(); +print "got: $got\nexp: $exp\nnot " + unless $got eq $exp; +&ok; + +$ip = NetAddr::IP::Lite->new6('12345/1'); +$got = $ip->cidr(); +print "got: $got\nexp: $exp\nnot " + unless $got eq $exp; +&ok; + + +# 2^127 170141183460469231731687303715884105728 + +$exp = '8000:0:0:0:0:0:0:0/1'; + +$ip = NetAddr::IP::Lite->new6('170141183460469231731687303715884105728/1'); +$got = $ip->cidr(); +print "got: $got\nexp: $exp\nnot " + unless $got eq $exp; +&ok; + +# 2^128 340282366920938463463374607431768211456 +# minus one + +$exp = 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF/1'; + +$ip = NetAddr::IP::Lite->new6('340282366920938463463374607431768211455/1'); +$got = $ip->cidr(); +print "got: $got\nexp: $exp\nnot " + unless $got eq $exp; +&ok; + +############# test new + +$exp = '0.0.48.57/1'; + +$ip = NetAddr::IP::Lite->new(12345,1); +$got = $ip->cidr(); +print "got: $got\nexp: $exp\nnot " + unless $got eq $exp; +&ok; + +$ip = NetAddr::IP::Lite->new('12345',1); +$got = $ip->cidr(); +print "got: $got\nexp: $exp\nnot " + unless $got eq $exp; +&ok; + +$ip = NetAddr::IP::Lite->new('12345/1'); +$got = $ip->cidr(); +print "got: $got\nexp: $exp\nnot " + unless $got eq $exp; +&ok; + + +# 2^127 170141183460469231731687303715884105728 + +$exp = '8000:0:0:0:0:0:0:0/1'; + +$ip = NetAddr::IP::Lite->new('170141183460469231731687303715884105728/1'); +$got = $ip->cidr(); +print "got: $got\nexp: $exp\nnot " + unless $got eq $exp; +&ok; + +# 2^128 340282366920938463463374607431768211456 +# minus one + +$exp = 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF/1'; + +$ip = NetAddr::IP::Lite->new('340282366920938463463374607431768211455/1'); +$got = $ip->cidr(); +print "got: $got\nexp: $exp\nnot " + unless $got eq $exp; +&ok; + diff --git a/MANIFEST b/MANIFEST index bb3f3f6..5f00bb4 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1,14 +1,54 @@ +Artistic Changes -docs/rfc1884.txt +Copying IP.pm +MANIFEST This list of files +MANIFEST.SKIP +META.yml Module meta-data (added by MakeMaker) +Makefile.PL +README +TODO +docs/rfc1884.txt +t/constants.t +t/full.t +t/full6.t +t/imhoff.t +t/loops.t +t/lower.t +t/masklen.t +t/new-store.t +t/old-store.t +t/over-arr.t +t/over-qq.t +t/relops.t +t/short.t +t/splitref.t +t/v4-coalesce.t +t/v4-compact.t +t/v4-compplus.t +t/v4-hostenum.t +t/v4-re.t +t/v4-split-bulk.t +t/v4-split-list.t +t/v4-splitplan.t +t/v4-sprefix.t +t/v4-xprefix.t +t/v6-re.t +t/v6-split-bulk.t +t/v6-splitplan.t +t/wildcard.t Lite/Changes Lite/Lite.pm -Lite/Makefile.PL +Lite/MANIFEST +Lite/MANIFEST.SKIP Lite/META.yml +Lite/Makefile.PL Lite/README +Lite/bug2742981 Lite/t/addr.t Lite/t/aton.t Lite/t/bigints.t +Lite/t/bin_ips.t Lite/t/bits.t Lite/t/broadcast.t Lite/t/bug62521.t @@ -47,9 +87,8 @@ Lite/t/v4-contains.t Lite/t/v4-last.t Lite/t/v4-new-first.t -Lite/t/v4-new_from_aton.t Lite/t/v4-new.t -Lite/t/v4_new_cis.t +Lite/t/v4-new_from_aton.t Lite/t/v4-no_octal.t Lite/t/v4-num.t Lite/t/v4-numeric.t @@ -57,40 +96,55 @@ Lite/t/v4-range.t Lite/t/v4-snew.t Lite/t/v4-wnew.t +Lite/t/v4_new_cis.t Lite/t/v6-cnew.t Lite/t/v6-contains.t Lite/t/v6-inc.t +Lite/t/v6-new-base.t Lite/t/v6-new_cis6_base.t Lite/t/v6-new_cis_base.t -Lite/t/v6-new-base.t Lite/t/v6-num.t Lite/t/v6-numeric.t Lite/t/v6-old-base.t Lite/t/version.t Lite/t/within.t Lite/Util/Changes -Lite/Util/configure -Lite/Util/configure.ac -Lite/Util/config.h.in -Lite/Util/GPL -Lite/Util/lib/NetAddr/IP/UtilPP.pm -Lite/Util/tlib/NetAddr/IP/Util_IS.pm -Lite/Util/localconf.h +Lite/Util/MANIFEST +Lite/Util/MANIFEST.SKIP Lite/Util/Makefile.PL Lite/Util/README +Lite/Util/Util.pm +Lite/Util/Util.xs +Lite/Util/config.h.in +Lite/Util/configure +Lite/Util/configure.ac +Lite/Util/lib/NetAddr/IP/InetBase.pm +Lite/Util/lib/NetAddr/IP/UtilPP.pm +Lite/Util/localconf.h Lite/Util/t/4to6.t Lite/Util/t/add128.t Lite/Util/t/addconst.t +Lite/Util/t/af_inet6.t Lite/Util/t/anyto6.t Lite/Util/t/badd.t Lite/Util/t/bcd2bin.t Lite/Util/t/bcdn2bin.t Lite/Util/t/bin.t +Lite/Util/t/binet_n2ad.t +Lite/Util/t/binet_n2dx.t +Lite/Util/t/binet_pton.t +Lite/Util/t/bipv4_inet.t +Lite/Util/t/bipv6_any2n.t +Lite/Util/t/bipv6func.t +Lite/Util/t/bisIPv4.t +Lite/Util/t/bpackzeros.t Lite/Util/t/comp128.t Lite/Util/t/croak.t Lite/Util/t/hasbits.t +Lite/Util/t/inet_4map6.t Lite/Util/t/inet_n2ad.t Lite/Util/t/inet_n2dx.t +Lite/Util/t/inet_pton.t Lite/Util/t/ipv4_inet.t Lite/Util/t/ipv6_any2n.t Lite/Util/t/ipv6func.t @@ -99,50 +153,13 @@ Lite/Util/t/leftshift.t Lite/Util/t/mode.t Lite/Util/t/notcontiguous.t +Lite/Util/t/packzeros.t Lite/Util/t/simple_pack.t Lite/Util/t/sub128.t +Lite/Util/tlib/NetAddr/IP/Util_IS.pm +Lite/Util/t/binet_ntoa.t +Lite/Util/t/ipv6_ntoa.t +Lite/Util/t/naip_gethostbyname.t +Lite/Util/t/no6_naip_gethostbyname.t Lite/Util/typemap -Lite/Util/Util.pm -Lite/Util/Util.xs -Lite/Util/xs_include/inet_aton.c -Lite/Util/xs_include/miniSocket.inc -Makefile.PL -MANIFEST This list of files -MANIFEST.SKIP -README -t/constants.t -t/full.t -t/full6.t -t/imhoff.t -t/loops.t -t/lower.t -t/masklen.t -t/new-store.t -t/old-store.t -t/over-arr.t -t/over-qq.t -t/relops.t -t/short.t -t/splitref.t -t/v4-coalesce.t -t/v4-compact.t -t/v4-compplus.t -t/v4-hostenum.t -t/v4-re.t -t/v4-split-bulk.t -t/v4-split-list.t -t/v4-splitplan.t -t/v4-sprefix.t -t/v4-xprefix.t -t/v6-re.t -t/v6-split-bulk.t -t/v6-splitplan.t -t/wildcard.t -TODO -Lite/bug2742981.pl -Lite/MANIFEST -Lite/MANIFEST.SKIP -Lite/Util/MANIFEST -Lite/Util/MANIFEST.SKIP -Lite/Util/t/inet_4map6.t -META.yml Module meta-data (added by MakeMaker) + diff --git a/META.yml b/META.yml index e055f43..b1fd991 100644 --- a/META.yml +++ b/META.yml @@ -1,10 +1,10 @@ --- #YAML:1.0 name: NetAddr-IP -version: 4.048 +version: 4.049 abstract: Manages IPv4 and IPv6 addresses and subnets license: ~ author: - - Luis E. Muñoz + - Luis E. Muñoz , Michael Robinton generated_by: ExtUtils::MakeMaker version 6.42 distribution_type: module requires: diff --git a/Makefile.PL b/Makefile.PL index 72f603e..76baad9 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -102,7 +102,7 @@ use NetAddr::IP ':aton'; -Versions before 4.08 recognized the above syntax. This behavior is +Versions before 4.008 recognized the above syntax. This behavior is fundamentally broken, is DEPRECATED and WILL BE REMOVED in version 5.0 Use this method to create new objects from 'aton's @@ -121,7 +121,7 @@ }, # e.g., Module::Name => 1.1 ($] >= 5.005 ? ## Add these new keywords supported since 5.005 (ABSTRACT_FROM => 'IP.pm', - AUTHOR => 'Luis E. Muñoz ') : ()), + AUTHOR => 'Luis E. Muñoz , Michael Robinton ') : ()), clean => { FILES => "*~ tmp*"}, );