Digital Mages

Digital Mages - Paranoid::Network::IPv4(3)

Paranoid::Network::IPv4

Section: User Contributed Perl Documentation (3)
Updated: 2022-03-08
Index  

NAME

Paranoid::Network::IPv4 - IPv4-related functions  

VERSION

$Id: lib/Paranoid/Network/IPv4.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $  

SYNOPSIS

    use Paranoid::Network::IPv4;

    @net = ipv4NetConvert($netAddr);
    $rv = ipv4NetIntersect($net1, $net2);

    use Paranoid::Network::IPv4 qw(:all);

    print "Valid IP address\n" if $netAddr =~ /^@{[ IPV4REGEX ]}$/;

    @net = ipv4NetConvert($netAddr);
    $broadcast = $net[IPV4BRDCST];

    use Paranoid::Network::IPv4 qw(:ipv4Sort);

    @nets = sort ipv4StrSort    @nets;
    @nets = sort ipv4PackedSort @nets;
    @nets = sort ipv4NumSort    @nets;

 

DESCRIPTION

This module contains a few convenience functions for working with IPv4 addresses.  

IMPORT LISTS

This module exports the following symbols by default:

    ipv4NetConvert ipv4NetIntersect

The following specialized import lists also exist:

    List        Members
    --------------------------------------------------------
    base        @defaults
    constants   MAXIPV4CIDR IPV4REGEX IPV4CIDRRGX IPV4BASE 
                IPV4BRDCST IPV4MASK
    ipv4Sort    ipv4NumSort ipv4StrSort ipv4PackedSort
    all         @base @constants @ipv4sort

 

SUBROUTINES/METHODS

 

ipv4NetConvert

    @net = ipv4NetConvert($netAddr);

This function takes an IPv4 network address in string format and converts it into an array containing the base network address, the broadcast address, and the netmask, in integer format. The network address can have the netmask in either CIDR format or dotted quads.

In the case of a single IP address, the array with only have one element, that of the IP in integer format.

Passing any argument to this function that is not a string representation of an IP address (including undef values) will cause this function to return an empty array.  

ipv4NetIntersect

    $rv = ipv4NetIntersect($net1, $net2);

This function tests whether an IP or subnet intersects with another IP or subnet. The return value is essentially boolean, but the true value can vary to indicate which is a subset of the other:

    -1: destination range encompasses target range
     0: both ranges do not intersect at all
     1: target range encompasses destination range

The function handles the same string formats as ipv4NetConvert, but will allow you to test single IPs in integer format as well.  

ipv4StrSort

    @sorted = sort ipv4StrSort @nets;

This function allows IPv4 addresses and networks to be passed in string format. Networks can be in CIDR format. Sorts in ascending order.  

ipv4PackedSort

    @sorted = sort ipv4PackedSort @nets;

This function sorts IPv4 addresses as returned by inet_aton. Sorts in ascending order.  

ipv4NumSort

    @sorted = sort ipv4NumSort @nets;

This function is rather pointless, but is included merely for completeness. Addresses are in unpacked, native integer format, such as one gets from:

    $ip = unpack 'N', inet_aton($ipAddr);

Sorts in ascending order.  

CONSTANTS

These are only imported if explicitly requested or with the :all tag.  

MAXIPV4CIDR

Simply put: 32. This is the largest CIDR notation supported in IPv4.  

IPV4REGEX

Regular expression.

You can use this for validating IP addresses as such:

    $ip =~ m#^@{[ IPV4REGEX ]}$#;

or to extract potential IPs from extraneous text:

    @ips = ( $string =~ m#(@{[ IPV4REGEX ]})#gsm);

 

IPV4CIDRRGX

Regular expression.

By default this will test a CIDR notation or dotted quad network address:

    $netaddr =~ m#@{[ IPV4CIDRRGX ]}$#s;

or extract network addresses from a string:

    @networks = ( $string =~ m#(@{[ IPV4CIDRRGX ]})#s );

 

IPV4BASE

This is the ordinal index of the base network address as returned by ipv4NetConvert.  

IPV4BRDCST

This is the ordinal index of the broadcast address as returned by ipv4NetConvert.  

IPV4MASK

This is the ordinal index of the network mask as returned by ipv4NetConvert.  

DEPENDENCIES

o
Paranoid
o
Paranoid::Debug
o
Paranoid::Network::Socket
 

BUGS AND LIMITATIONS

 

AUTHOR

Arthur Corliss (corliss@digitalmages.com)  

LICENSE AND COPYRIGHT

This software is free software. Similar to Perl, you can redistribute it and/or modify it under the terms of either:

  a)     the GNU General Public License
         <https://www.gnu.org/licenses/gpl-1.0.html> as published by the 
         Free Software Foundation <http://www.fsf.org/>; either version 1
         <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
         <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
  b)     the Artistic License 2.0
         <https://opensource.org/licenses/Artistic-2.0>,

subject to the following additional term: No trademark rights to ``Paranoid'' have been or are conveyed under any of the above licenses. However, ``Paranoid'' may be used fairly to describe this unmodified software, in good faith, but not as a trademark.

(c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com) (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)


 

Index

NAME
VERSION
SYNOPSIS
DESCRIPTION
IMPORT LISTS
SUBROUTINES/METHODS
ipv4NetConvert
ipv4NetIntersect
ipv4StrSort
ipv4PackedSort
ipv4NumSort
CONSTANTS
MAXIPV4CIDR
IPV4REGEX
IPV4CIDRRGX
IPV4BASE
IPV4BRDCST
IPV4MASK
DEPENDENCIES
BUGS AND LIMITATIONS
AUTHOR
LICENSE AND COPYRIGHT