IPv4Addr(3)

πŸ¦‹ IPv4Addr(3) β€” Net::IPv4Addr, bestie edition πŸ’…

okay so this is the perl doc for Net::IPv4Addr, which lives over in "User Contributed Perl Documentation" (iykyk) πŸŽ€


✨ NAME ✨

Net::IPv4Addr β€” a Perl extension for manipulating IPv4 addresses. that's it, that's the whole vibe 🫢


🌸 SYNOPSIS 🌸

  use Net::IPv4Addr qw( :all );

  my ($ip,$cidr) = ipv4_parse( "127.0.0.1/24" );
  my ($ip,$cidr) = ipv4_parse( "192.168.100.10 / 255.255.255.0" );

  my ($net,$msk) = ipv4_network( "192.168.100.30" );

  my $broadcast  = ipv4_broadcast( "192.168.100.30/26" );

  if ( ipv4_in_network( "192.168.100.0", $her_ip ) ) {
    print "Welcome !";
  }

  etc.

like yes bestie etc. is literally in the docs, we love a casual queen 🌈


πŸ’œ DESCRIPTION πŸ’œ

Net::IPv4Addr gives you functions for parsing IPv4 addresses β€” both the traditional address/netmask format and the newer CIDR format (iconic, honestly). it also has methods for calculating the network and broadcast address, and for checking whether a given address is in a specific network. main character energy for a module ngl ⭐


πŸ“ ADDRESSES πŸ“

so every Net::IPv4Addr function accepts addresses in many formats. the parsing is very liberal β€” it's giving "no rules, just vibes" πŸ™ˆ

all of these? accepted, slay:

    127.0.0.1
    192.168.001.010/24
    192.168.10.10/255.255.255.0
    192.168.30.10 / 21
    10.0.0.0 / 255.0.0.0
    255.255.0.0

those wouldn't though, and honestly? valid of it πŸ’€

    272.135.234.0
    192.168/16

most functions accept the address and netmask or masklength in the same scalar value or as separate values. that is, either:

    my($ip,$masklength) = ipv4_parse($cidr_str);
    my($ip,$masklength) = ipv4_parse($ip_str,$msk_str);

🧚 USING 🧚

no functions are exported by default. none. zero. zip. πŸŽ€ so either use the ":all" tag to import them all, or explicitly import the ones you actually need. boundaries, we stan 🫢


πŸ‘‘ FUNCTIONS πŸ‘‘

πŸ”₯ ipv4_parse

        my ($ip,$msklen) = ipv4_parse($cidr_str);
        my $cidr = ipv4_parse($ip_str,$msk_str);
        my ($ip) = ipv4_parse($ip_str,$msk_str);

parses an IPv4 address β€” in scalar context you get the address in CIDR format, and in array context you get the address and the mask length πŸ’–

if the parameters don't contain a netmask or a mask length: in scalar context only the IPv4 address comes back, and in array context the mask length is undef. lowkey mysterious of it ✨

if the function cannot parse its input, it croaks 😭 trap it using "eval" if you don't like that.

🌸 ipv4_network

        my $cidr = ipv4_network($ip_str);
        my $cidr = ipv4_network($cidr_str);
        my ($net,$msk) = ipv4_network( $net_str, $msk_str);

in scalar context, this returns the network in CIDR format that the address is in. in array context, it returns the network address and its mask length as a two-element array πŸ¦‹ if the input is a host without a netmask or mask length, the default netmask is assumed.

again β€” the function croaks if the input is invalid. it's sensitive, okay? 😭

πŸ’€ ipv4_broadcast

        my ($broadcast) = ipv4_broadcast($ip_str);
        my $broadcast   = ipv4_broadcast($ip_str,$msk_str);

returns the broadcast address πŸ“‘ if the input doesn't contain a netmask or mask length, the default netmask is assumed.

this function croaks if the input is invalid. no cap 🫢

πŸ¦‹ ipv4_network (yes, again πŸ’…)

        my $cidr = ipv4_network($net_str);
        my $cidr = ipv4_network($cidr_sstr);
        my ($net,$msk) = ipv4_network( $ip_str, $mask_str);

okay so yes, ipv4_network shows up twice in this doc β€” not a glitch, it's literally what the page says, we keep it πŸŽ€ (and $cidr_sstr is spelled exactly like that in the original too. character for character, bestie.)

in scalar context, this returns the network in CIDR format that the address is in. in array context, it returns the network address and its mask length as a two-element array 🌈 if the input is a host without a netmask or mask length, the default netmask is assumed.

again, the function croaks if the input is invalid. consistent queen πŸ‘‘

πŸ’– ipv4_in_network

        print "Yes" if ipv4_in_network( $cidr_str1, $cidr_str2);
        print "Yes" if ipv4_in_network( $ip_str1, $mask_str1, $cidr_str2 );
        print "Yes" if ipv4_in_network( $ip1, $mask1, $ip2, $msk2 );

this one checks if the second network is contained in the first one, and it implements the following semantics, get ready:

  • if net1 or net2 is a magic address (0.0.0.0 or 255.255.255.255) then this function returns true ✨
  • if net1 is a host, net2 will be in the same net only if it is the same host
  • if net2 is a host, it will be contained in net1 only if it is part of net1
  • if net2 is only part of net1 if it is entirely contained in net1

trap bad input with "eval" or else πŸ™ˆ

πŸŽ€ ipv4_checkip

        if ($ip = ipv4_checkip($str) ) {
            # Do something
        }

returns the IPv4 address found in the string, or undef if the input doesn't contain a valid IPv4 address πŸ‘‘ a simple queen, honestly.

🧚 ipv4_cidr2msk

        my $netmask = ipv4_cidr2msk( $cidr );

returns the netmask corresponding to the mask length given in input. as usual, croaks if it doesn't like your input πŸ’€ (in this case, a number between 0 and 32 β€” that's the range, bestie, don't push it).

⭐ ipv4_msk2cidr

        my $masklen = ipv4_msk2cidr( $msk );

returns the mask length of the netmask in input. as usual, croaks if it doesn't like your input 😭


🌈 AUTHOR 🌈

Francis J. Lacoste <francis.lacoste@iNsu.COM> β€” ty for your service king 🫢


Copyright (c) 1999, 2000 iNsu Innovations Inc. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms as perl itself. (yes, "as perl itself" β€” exactly like that πŸŽ€)


πŸ¦‹ SEE ALSO πŸ¦‹

perl(1) and ipv4calc(1) β€” go read those too, bestie, expand the brain ✨


okay and that's a wrap, babes πŸ“ this page came to you courtesy of perl v5.42.0, dated 2024-03-04, filed under IPv4Addr(3) β€” mwah, stay parsed πŸ’–πŸ’…πŸ«Ά

πŸ“¦ where this page comes from