{
    "mode": "perldoc",
    "parameter": "NetAddr::IP::Util",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/NetAddr%3A%3AIP%3A%3AUtil/json",
    "generated": "2026-06-16T20:42:52Z",
    "synopsis": "use NetAddr::IP::Util qw(\ninetaton\ninetntoa\nipv6aton\nipv6ntoa\nipv6n2x\nipv6n2d\ninetany2n\nhasbits\nisIPv4\nisNewIPv4\nisAnyIPv4\ninetn2dx\ninetn2ad\ninetpton\ninetntop\ninet4map6\nipv4to6\nmask4to6\nipanyto6\nmaskanyto6\nipv6to4\npackzeros\nshiftleft\naddconst\nadd128\nsub128\nnotcontiguous\nbin2bcd\nbcd2bin\nmode\nAFINET\nAFINET6\nnaipgethostbyname\n);\nuse NetAddr::IP::Util qw(:all :inet :ipv4 :ipv6 :math)\n:inet   =>    inetaton, inetntoa, ipv6aton\nipv6ntoa, ipv6n2x, ipv6n2d,\ninetany2n, inetn2dx, inetn2ad,\ninetpton, inetntop, inet4map6,\nipv4to6, mask4to6, ipanyto6, packzeros\nmaskanyto6, ipv6to4, naipgethostbyname\n:ipv4   =>    inetaton, inetntoa\n:ipv6   =>    ipv6aton, ipv6ntoa, ipv6n2x,\nipv6n2d, inetany2n, inetn2dx,\ninetn2ad, inetpton, inetntop,\ninet4map6, ipv4to6, mask4to6,\nipanyto6, maskanyto6, ipv6to4,\npackzeros, naipgethostbyname\n:math   =>    hasbits, isIPv4, isNewIPv4, isAnyIPv4,\naddconst, add128, sub128, notcontiguous,\nbin2bcd, bcd2bin, shiftleft\n$dotquad = inetntoa($netaddr);\n$netaddr = inetaton($dotquad);\n$ipv6naddr = ipv6aton($ipv6text);\n$ipv6text = ipvtntoa($ipv6naddr);\n$hextext = ipv6n2x($ipv6naddr);\n$dectext = ipv6n2d($ipv6naddr);\n$hextext = packzeros($hextext);\n$ipv6naddr = inetany2n($dotquad or $ipv6text);\n$ipv6naddr = inet4map6($netaddr or $ipv6naddr);\n$rv = hasbits($bits128);\n$rv = isIPv4($bits128);\n$rv = isNewIPv4($bits128);\n$rv = isAnyIPv4($bits128);\n$dotquad or $hextext = inetn2dx($ipv6naddr);\n$dotquad or $dectext = inetn2ad($ipv6naddr);\n$netaddr = inetpton($AFfamily,$hextext);\n$hextext = inetntop($AFfamily,$netaddr);\n$ipv6naddr = ipv4to6($netaddr);\n$ipv6naddr = mask4to6($netaddr);\n$ipv6naddr = ipanyto6($netaddr);\n$ipv6naddr = maskanyto6($netaddr);\n$netaddr = ipv6to4($pv6naddr);\n$bitsX2 = shiftleft($bits128,$n);\n$carry = addconst($ipv6naddr,$signed32con);\n($carry,$ipv6naddr)=addconst($ipv6naddr,$signed32con);\n$carry = add128($ipv6naddr1,$ipv6naddr2);\n($carry,$ipv6naddr)=add128($ipv6naddr1,$ipv6naddr2);\n$carry = sub128($ipv6naddr1,$ipv6naddr2);\n($carry,$ipv6naddr)=sub128($ipv6naddr1,$ipv6naddr2);\n($spurious,$cidr) = notcontiguous($mask128);\n$bcdtext = bin2bcd($bits128);\n$bits128 = bcd2bin($bcdtxt);\n$modetext = mode;\n($name,$aliases,$addrtype,$length,@addrs)=naipgethostbyname(NAME);\n$trueif = havegethostbyname2();\nNetAddr::IP::Util::lower();\nNetAddr::IP::Util::upper();",
    "sections": {
        "NAME": {
            "content": "NetAddr::IP::Util -- IPv4/6 and 128 bit number utilities\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use NetAddr::IP::Util qw(\ninetaton\ninetntoa\nipv6aton\nipv6ntoa\nipv6n2x\nipv6n2d\ninetany2n\nhasbits\nisIPv4\nisNewIPv4\nisAnyIPv4\ninetn2dx\ninetn2ad\ninetpton\ninetntop\ninet4map6\nipv4to6\nmask4to6\nipanyto6\nmaskanyto6\nipv6to4\npackzeros\nshiftleft\naddconst\nadd128\nsub128\nnotcontiguous\nbin2bcd\nbcd2bin\nmode\nAFINET\nAFINET6\nnaipgethostbyname\n);\n\nuse NetAddr::IP::Util qw(:all :inet :ipv4 :ipv6 :math)\n\n:inet   =>    inetaton, inetntoa, ipv6aton\nipv6ntoa, ipv6n2x, ipv6n2d,\ninetany2n, inetn2dx, inetn2ad,\ninetpton, inetntop, inet4map6,\nipv4to6, mask4to6, ipanyto6, packzeros\nmaskanyto6, ipv6to4, naipgethostbyname\n\n:ipv4   =>    inetaton, inetntoa\n\n:ipv6   =>    ipv6aton, ipv6ntoa, ipv6n2x,\nipv6n2d, inetany2n, inetn2dx,\ninetn2ad, inetpton, inetntop,\ninet4map6, ipv4to6, mask4to6,\nipanyto6, maskanyto6, ipv6to4,\npackzeros, naipgethostbyname\n\n:math   =>    hasbits, isIPv4, isNewIPv4, isAnyIPv4,\naddconst, add128, sub128, notcontiguous,\nbin2bcd, bcd2bin, shiftleft\n\n$dotquad = inetntoa($netaddr);\n$netaddr = inetaton($dotquad);\n$ipv6naddr = ipv6aton($ipv6text);\n$ipv6text = ipvtntoa($ipv6naddr);\n$hextext = ipv6n2x($ipv6naddr);\n$dectext = ipv6n2d($ipv6naddr);\n$hextext = packzeros($hextext);\n$ipv6naddr = inetany2n($dotquad or $ipv6text);\n$ipv6naddr = inet4map6($netaddr or $ipv6naddr);\n$rv = hasbits($bits128);\n$rv = isIPv4($bits128);\n$rv = isNewIPv4($bits128);\n$rv = isAnyIPv4($bits128);\n$dotquad or $hextext = inetn2dx($ipv6naddr);\n$dotquad or $dectext = inetn2ad($ipv6naddr);\n$netaddr = inetpton($AFfamily,$hextext);\n$hextext = inetntop($AFfamily,$netaddr);\n$ipv6naddr = ipv4to6($netaddr);\n$ipv6naddr = mask4to6($netaddr);\n$ipv6naddr = ipanyto6($netaddr);\n$ipv6naddr = maskanyto6($netaddr);\n$netaddr = ipv6to4($pv6naddr);\n$bitsX2 = shiftleft($bits128,$n);\n$carry = addconst($ipv6naddr,$signed32con);\n($carry,$ipv6naddr)=addconst($ipv6naddr,$signed32con);\n$carry = add128($ipv6naddr1,$ipv6naddr2);\n($carry,$ipv6naddr)=add128($ipv6naddr1,$ipv6naddr2);\n$carry = sub128($ipv6naddr1,$ipv6naddr2);\n($carry,$ipv6naddr)=sub128($ipv6naddr1,$ipv6naddr2);\n($spurious,$cidr) = notcontiguous($mask128);\n$bcdtext = bin2bcd($bits128);\n$bits128 = bcd2bin($bcdtxt);\n$modetext = mode;\n($name,$aliases,$addrtype,$length,@addrs)=naipgethostbyname(NAME);\n$trueif = havegethostbyname2();\n\nNetAddr::IP::Util::lower();\nNetAddr::IP::Util::upper();\n",
            "subsections": []
        },
        "INSTALLATION": {
            "content": "Un-tar the distribution in an appropriate directory and type:\n\nperl Makefile.PL\nmake\nmake test\nmake install\n\nNetAddr::IP::Util installs by default with its primary functions\ncompiled using Perl's XS extensions to build a 'C' library. If you do\nnot have a 'C' compiler available or would like the slower Pure Perl\nversion for some other reason, then type:\n\nperl Makefile.PL -noxs\nmake\nmake test\nmake install\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "NetAddr::IP::Util provides a suite of tools for manipulating and\nconverting IPv4 and IPv6 addresses into 128 bit string context and back\nto text. The strings can be manipulated with Perl's logical operators:\n\nand     &\nor      |\nxor     ^\n~       compliment\n\nin the same manner as 'vec' strings.\n\nThe IPv6 functions support all rfc1884 formats.\n\ni.e.  x:x:x:x:x:x:x:x:x\nx:x:x:x:x:x:x:d.d.d.d\n::x:x:x\n::x:d.d.d.d\nand so on...\n\n*   $dotquad = inetntoa($netaddr);\n\nConvert a packed IPv4 network address to a dot-quad IP address.\n\ninput:        packed network address\nreturns:      IP address i.e. 10.4.12.123\n\n*   $netaddr = inetaton($dotquad);\n\nConvert a dot-quad IP address into an IPv4 packed network address.\n\ninput:        IP address i.e. 192.5.16.32\nreturns:      packed network address\n\n*   $ipv6addr = ipv6aton($ipv6text);\n\nTakes an IPv6 address of the form described in rfc1884 and returns a\n128 bit binary RDATA string.\n\ninput:        ipv6 text\nreturns:      128 bit RDATA string\n\n*   $ipv6text = ipv6ntoa($ipv6naddr);\n\nConvert a 128 bit binary IPv6 address to compressed rfc 1884 text\nrepresentation.\n\ninput:        128 bit RDATA string\nreturns:      ipv6 text\n\n*   $hextext = ipv6n2x($ipv6addr);\n\nTakes an IPv6 RDATA string and returns an 8 segment IPv6 hex address\n\ninput:        128 bit RDATA string\nreturns:      x:x:x:x:x:x:x:x\n\n*   $dectext = ipv6n2d($ipv6addr);\n\nTakes an IPv6 RDATA string and returns a mixed hex - decimal IPv6\naddress with the 6 uppermost chunks in hex and the lower 32 bits in\ndot-quad representation.\n\ninput:        128 bit RDATA string\nreturns:      x:x:x:x:x:x:d.d.d.d\n\n*   $ipv6naddr = inetany2n($dotquad or $ipv6text);\n\nThis function converts a text IPv4 or IPv6 address in text format in\nany standard notation into a 128 bit IPv6 string address. It\nprefixes any dot-quad address (if found) with '::' and passes it to\nipv6aton.\n\ninput:        dot-quad or rfc1844 address\nreturns:      128 bit IPv6 string\n\n*   $rv = hasbits($bits128);\n\nThis function returns true if there are one's present in the 128 bit\nstring and false if all the bits are zero.\n\ni.e.  if (hasbits($bits128)) {\n&dosomething;\n}\n\nor    if (hasbits($bits128 & $mask128) {\n&dosomething;\n}\n\nThis allows the implementation of logical functions of the form of:\n\nif ($bits128 & $mask128) {\n...\n\ninput:        128 bit IPv6 string\nreturns:      true if any bits are present\n\n*   $ipv6naddr = inet4map6($netaddr or $ipv6naddr\n\nThis function returns an ipV6 network address with the first 80 bits\nset to zero and the next 16 bits set to one, while the last 32 bits\nare filled with the ipV4 address.\n\ninput:        ipV4 netaddr\nor  ipV6 netaddr\nreturns:      ipV6 netaddr\n\nreturns: undef on error\n\nAn ipV6 network address must be in one of the two compatible ipV4\nmapped address spaces. i.e.\n\n::ffff::d.d.d.d    or    ::d.d.d.d\n\n*   $rv = isIPv4($bits128);\n\nThis function returns true if there are no on bits present in the\nIPv6 portion of the 128 bit string and false otherwise.\n\ni.e.  the address must be of the form - ::d.d.d.d\n\nNote: this is an old and deprecated ipV4 compatible ipV6 address\n\n*   $rv = isNewIPv4($bits128);\n\nThis function return true if the IPv6 128 bit string is of the form\n\n::ffff::d.d.d.d\n\n*   $rv = isAnyIPv4($bits128);\n\nThis function return true if the IPv6 bit string is of the form\n\n::d.d.d.d       or      ::ffff::d.d.d.d\n\n*   $dotquad or $hextext = inetn2dx($ipv6naddr);\n\nThis function does the right thing and returns the text for either a\ndot-quad IPv4 or a hex notation IPv6 address.\n\ninput:        128 bit IPv6 string\nreturns:      ddd.ddd.ddd.ddd\nor  x:x:x:x:x:x:x:x\n\n*   $dotquad or $dectext = inetn2ad($ipv6naddr);\n\nThis function does the right thing and returns the text for either a\ndot-quad IPv4 or a hex::decimal notation IPv6 address.\n\ninput:        128 bit IPv6 string\nreturns:      ddd.ddd.ddd.ddd\nor  x:x:x:x:x:x:ddd.ddd.ddd.dd\n\n*   $netaddr = inetpton($AFfamily,$hextext);\n\nThis function takes an IP address in IPv4 or IPv6 text format and\nconverts it into binary format. The type of IP address conversion is\ncontrolled by the FAMILY argument.\n\n*   $hextext = inetntop($AFfamily,$netaddr);\n\nThis function takes and IP address in binary format and converts it\ninto text format. The type of IP address conversion is controlled by\nthe FAMILY argument.\n\nNOTE: inetntop ALWAYS returns lowercase characters.\n\n*   $hextext = packzeros($hextext);\n\nThis function optimizes and rfc 1884 IPv6 hex address to reduce the\nnumber of long strings of zero bits as specified in rfc 1884, 2.2\n(2) by substituting :: for the first occurrence of the longest\nstring of zeros in the address.\n\n*   $ipv6naddr = ipv4to6($netaddr);\n\nConvert an ipv4 network address into an IPv6 network address.\n\ninput:        32 bit network address\nreturns:      128 bit network address\n\n*   $ipv6naddr = mask4to6($netaddr);\n\nConvert an ipv4 network address/mask into an ipv6 network mask.\n\ninput:        32 bit network/mask address\nreturns:      128 bit network/mask address\n\nNOTE: returns the high 96 bits as one's\n\n*   $ipv6naddr = ipanyto6($netaddr);\n\nSimilar to ipv4to6 except that this function takes either an IPv4 or\nIPv6 input and always returns a 128 bit IPv6 network address.\n\ninput:        32 or 128 bit network address\nreturns:      128 bit network address\n\n*   $ipv6naddr = maskanyto6($netaddr);\n\nSimilar to mask4to6 except that this function takes either an IPv4\nor IPv6 netmask and always returns a 128 bit IPv6 netmask.\n\ninput:        32 or 128 bit network mask\nreturns:      128 bit network mask\n\n*   $netaddr = ipv6to4($pv6naddr);\n\nTruncate the upper 96 bits of a 128 bit address and return the lower\n32 bits. Returns an IPv4 address as returned by inetaton.\n\ninput:        128 bit network address\nreturns:      32 bit inetaton network address\n\n*   $bitsXn = shiftleft($bits128,$n);\n\ninput:        128 bit string variable,\nnumber of shifts [optional]\nreturns:      bits X n shifts\n\nNOTE: a single shift is performed\nif $n is not specified\n\n*   addconst($ipv6naddr,$signed32con);\n\nAdd a signed constant to a 128 bit string variable.\n\ninput:        128 bit IPv6 string,\nsigned 32 bit integer\nreturns:  scalar      carry\narray       (carry, result)\n\n*   add128($ipv6naddr1,$ipv6naddr2);\n\nAdd two 128 bit string variables.\n\ninput:        128 bit string var1,\n128 bit string var2\nreturns:  scalar      carry\narray       (carry, result)\n\n*   sub128($ipv6naddr1,$ipv6naddr2);\n\nSubtract two 128 bit string variables.\n\ninput:        128 bit string var1,\n128 bit string var2\nreturns:  scalar      carry\narray       (carry, result)\n\nNote: The carry from this operation is the result of adding the\none's complement of ARG2 +1 to the ARG1. It is logically NOT borrow.\n\ni.e.    if ARG1 >= ARG2 then carry = 1\nor      if ARG1  < ARG2 then carry = 0\n\n*   ($spurious,$cidr) = notcontiguous($mask128);\n\nThis function counts the bit positions remaining in the mask when\nthe rightmost '0's are removed.\n\ninput:  128 bit netmask\nreturns true if there are spurious\nzero bits remaining in the\nmask, false if the mask is\ncontiguous one's,\n128 bit cidr number\n\n*   $bcdtext = bin2bcd($bits128);\n\nConvert a 128 bit binary string into binary coded decimal text\ndigits.\n\ninput:        128 bit string variable\nreturns:      string of bcd text digits\n\n*   $bits128 = bcd2bin($bcdtxt);\n\nConvert a bcd text string to 128 bit string variable\n\ninput:        string of bcd text digits\nreturns:      128 bit string variable\n\n*   $modetext = mode;\n\nReturns the operating mode of this module.\n\ninput:          none\nreturns:        \"Pure Perl\"\nor   \"CC XS\"\n\n*   ($name,$aliases,$addrtype,$length,@addrs)=naipgethostbyname(NAME);\n\nReplacement for Perl's gethostbyname if Socket6 is available\n\nIn ARRAY context, returns a list of five elements, the hostname or\nNAME, a space separated list of CNAMES, AF family, length of the\naddress structure, and an array of one or more netaddr's\n\nIn SCALAR context, returns the first netaddr.\n\nThis function ALWAYS returns an IPv6 address, even on IPv4 only\nsystems. IPv4 addresses are mapped into IPv6 space in the form:\n\n::FFFF:FFFF:d.d.d.d\n\nThis is NOT the expected result from Perl's gethostbyname2. It is\ninstead equivalent to:\n\nOn an IPv4 only system:\n$ipv6naddr = ipv4to6 scalar ( gethostbyname( name ));\n\nOn a system with Socket6 and a working gethostbyname2:\n$ipv6naddr = gethostbyname2( name, AFINET6 );\nand if that fails, the IPv4 conversion above.\n\nFor a gethostbyname2 emulator that behave like Socket6, see:\nNet::DNS::Dig\n\n*   $trueif = havegethostbyname2();\n\nThis function returns TRUE if Socket6 has a functioning\ngethostbyname2, otherwise it returns FALSE. See the comments above\nabout the behavior of naipgethostbyname.\n\n*   NetAddr::IP::Util::lower();\n\nReturn IPv6 strings in lowercase.\n\n*   NetAddr::IP::Util::upper();\n\nReturn IPv6 strings in uppercase. This is the default.\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "# convert any textual IP address into a 128 bit vector\n#\nsub text2vec {\nmy($anyIP,$anyMask) = @;\n\n# not IPv4 bit mask\nmy $notiv4 = ipv6aton('FFFF:FFFF:FFFF:FFFF:FFFF:FFFF::');\n\nmy $vecip   = inetany2n($anyIP);\nmy $mask    = inetany2n($anyMask);\n\n# extend mask bits for IPv4\nmy $bits = 128;     # default\nunless (hasbits($mask & $notiv4)) {\n$mask |= $notiv4;\n$bits = 32;\n}\nreturn ($vecip, $mask, $bits);\n}\n\n... alternate implementation, a little faster\n\nsub text2vec {\nmy($anyIP,$anyMask) = @;\n\n# not IPv4 bit mask\nmy $notiv4 = ipv6aton('FFFF:FFFF:FFFF:FFFF:FFFF:FFFF::');\n\nmy $vecip   = inetany2n($anyIP);\nmy $mask    = inetany2n($anyMask);\n\n# extend mask bits for IPv4\nmy $bits = 128;     # default\nif (isIPv4($mask)) {\n$mask |= $notiv4;\n$bits = 32;\n}\nreturn ($vecip, $mask, $bits);\n}\n\n\n... elsewhere\n$nip = {\naddr    => $vecip,\nmask    => $mask,\nbits    => $bits,\n};\n\n# return network and broadcast addresses from IP and Mask\n#\nsub netbroad {\nmy($nip) = shift;\nmy $notmask = ~ $nip->{mask};\nmy $bcast   = $nip->{addr} | $notmask;\nmy $network = $nip->{addr} & $nip->{mask};\nreturn ($network, $broadcast);\n}\n\n# check if address is within a network\n#\nsub within {\nmy($nip,$net) = @;\nmy $addr = $nip->{addr}\nmy($nw,$bc) = netbroad($net);\n# arg1 >= arg2, sub128 returns true\nreturn (sub128($addr,$nw) && sub128($bc,$addr))\n? 1 : 0;\n}\n\n# truly hard way to do $ip++\n# add a constant, wrapping at netblock boundaries\n# to subtract the constant, negate it before calling\n# 'addwrap' since 'addconst' will extend the sign bits\n#\nsub addwrap {\nmy($nip,$const) = @;\nmy $addr    = $nip->{addr};\nmy $mask    = $nip->{mask};\nmy $bits    = $nip->{bits};\nmy $notmask = ~ $mask;\nmy $hibits  = $addr & $mask;\n$addr = addconst($addr,$const);\nmy $wraponly = $addr & $notmask;\nmy $newip = {\naddr    => $hibits | $wraponly,\nmask    => $mask,\nbits    => $bits,\n};\n# bless $newip as appropriate\nreturn $newip;\n}\n\n# something more useful\n# increment a /24 net to the NEXT net at the boundary\n\nmy $nextnet = 256;    # for /24\nLOOP:\nwhile (...continuing) {\nyour code....\n...\nmy $lastip = $ip-copy();\n$ip++;\nif ($ip < $lastip) {        # host part wrapped?\n# discard carry\n(undef, $ip->{addr} = addconst($ip->{addr}, $nextnet);\n}\nnext LOOP;\n}\n",
            "subsections": []
        },
        "EXPORTOK": {
            "content": "inetaton\ninetntoa\nipv6aton\nipv6ntoa\nipv6n2x\nipv6n2d\ninetany2n\nhasbits\nisIPv4\nisNewIPv4\nisAnyIPv4\ninetn2dx\ninetn2ad\ninetpton\ninetntop\ninet4map6\nipv4to6\nmask4to6\nipanyto6\nmaskanyto6\nipv6to4\npackzeros\nshiftleft\naddconst\nadd128\nsub128\nnotcontiguous\nbin2bcd\nbcd2bin\nmode\nnaipgethostbyname\nhavegethostbyname2\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Michael Robinton <michael@bizsystems.com>\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright 2003 - 2014, Michael Robinton <michael@bizsystems.com>\n\nAll rights reserved.\n\nThis program is free software; you can redistribute it and/or modify it\nunder the terms of either:\n\na) the GNU General Public License as published by the Free\nSoftware Foundation; either version 2, or (at your option) any\nlater version, or\n\nb) the \"Artistic License\" which comes with this distribution.\n\nThis program is distributed in the hope that it will be useful, but\nWITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the GNU\nGeneral Public License or the Artistic License for more details.\n\nYou should have received a copy of the Artistic License with this\ndistribution, in the file named \"Artistic\". If not, I'll be glad to\nprovide one.\n\nYou should also have received a copy of the GNU General Public License\nalong with this program in the file named \"Copying\". If not, write to\nthe\n\nFree Software Foundation, Inc.\n51 Franklin Street, Fifth Floor\nBoston, MA 02110-1301 USA.\n\nor visit their web page on the internet at:\n\nhttp://www.gnu.org/copyleft/gpl.html.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "NetAddr::IP(3), NetAddr::IP::Lite(3), NetAddr::IP::InetBase(3)\n",
            "subsections": []
        }
    },
    "summary": "NetAddr::IP::Util -- IPv4/6 and 128 bit number utilities",
    "flags": [],
    "examples": [
        "# convert any textual IP address into a 128 bit vector",
        "sub text2vec {",
        "my($anyIP,$anyMask) = @;",
        "# not IPv4 bit mask",
        "my $notiv4 = ipv6aton('FFFF:FFFF:FFFF:FFFF:FFFF:FFFF::');",
        "my $vecip   = inetany2n($anyIP);",
        "my $mask    = inetany2n($anyMask);",
        "# extend mask bits for IPv4",
        "my $bits = 128;     # default",
        "unless (hasbits($mask & $notiv4)) {",
        "$mask |= $notiv4;",
        "$bits = 32;",
        "return ($vecip, $mask, $bits);",
        "... alternate implementation, a little faster",
        "sub text2vec {",
        "my($anyIP,$anyMask) = @;",
        "# not IPv4 bit mask",
        "my $notiv4 = ipv6aton('FFFF:FFFF:FFFF:FFFF:FFFF:FFFF::');",
        "my $vecip   = inetany2n($anyIP);",
        "my $mask    = inetany2n($anyMask);",
        "# extend mask bits for IPv4",
        "my $bits = 128;     # default",
        "if (isIPv4($mask)) {",
        "$mask |= $notiv4;",
        "$bits = 32;",
        "return ($vecip, $mask, $bits);",
        "... elsewhere",
        "$nip = {",
        "addr    => $vecip,",
        "mask    => $mask,",
        "bits    => $bits,",
        "};",
        "# return network and broadcast addresses from IP and Mask",
        "sub netbroad {",
        "my($nip) = shift;",
        "my $notmask = ~ $nip->{mask};",
        "my $bcast   = $nip->{addr} | $notmask;",
        "my $network = $nip->{addr} & $nip->{mask};",
        "return ($network, $broadcast);",
        "# check if address is within a network",
        "sub within {",
        "my($nip,$net) = @;",
        "my $addr = $nip->{addr}",
        "my($nw,$bc) = netbroad($net);",
        "# arg1 >= arg2, sub128 returns true",
        "return (sub128($addr,$nw) && sub128($bc,$addr))",
        "? 1 : 0;",
        "# truly 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 $addr    = $nip->{addr};",
        "my $mask    = $nip->{mask};",
        "my $bits    = $nip->{bits};",
        "my $notmask = ~ $mask;",
        "my $hibits  = $addr & $mask;",
        "$addr = addconst($addr,$const);",
        "my $wraponly = $addr & $notmask;",
        "my $newip = {",
        "addr    => $hibits | $wraponly,",
        "mask    => $mask,",
        "bits    => $bits,",
        "};",
        "# bless $newip as appropriate",
        "return $newip;",
        "# something more useful",
        "# increment a /24 net to the NEXT net at the boundary",
        "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;"
    ],
    "see_also": [
        {
            "name": "IP",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/IP/3/json"
        },
        {
            "name": "Lite",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/Lite/3/json"
        },
        {
            "name": "InetBase",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/InetBase/3/json"
        }
    ]
}