1IPv4Addr(3) User Contributed Perl Documentation IPv4Addr(3)
2
3
4
6 Net::IPv4Addr - Perl extension for manipulating IPv4 addresses.
7
9 use Net::IPv4Addr qw( :all );
10
11 my ($ip,$cidr) = ipv4_parse( "127.0.0.1/24" );
12 my ($ip,$cidr) = ipv4_parse( "192.168.100.10 / 255.255.255.0" );
13
14 my ($net,$msk) = ipv4_network( "192.168.100.30" );
15
16 my $broadcast = ipv4_broadcast( "192.168.100.30/26" );
17
18 if ( ipv4_in_network( "192.168.100.0", $her_ip ) ) {
19 print "Welcome !";
20 }
21
22 etc.
23
25 Net::IPv4Addr provides functions for parsing IPv4 addresses both in
26 traditional address/netmask format and in the new CIDR format. There
27 are also methods for calculating the network and broadcast address and
28 also to see check if a given address is in a specific network.
29
31 All of Net::IPv4Addr functions accepts addresses in many format. The
32 parsing is very liberal.
33
34 All these addresses would be accepted:
35
36 127.0.0.1
37 192.168.001.010/24
38 192.168.10.10/255.255.255.0
39 192.168.30.10 / 21
40 10.0.0.0 / 255.0.0.0
41 255.255.0.0
42
43 Those wouldn't though:
44
45 272.135.234.0
46 192.168/16
47
48 Most functions accepts the address and netmask or masklength in the
49 same scalar value or as separate values. That is either
50
51 my($ip,$masklength) = ipv4_parse($cidr_str);
52 my($ip,$masklength) = ipv4_parse($ip_str,$msk_str);
53
55 No functions are exported by default. Either use the ":all" tag to
56 import them all or explicitly import those you need.
57
59 ipv4_parse
60 my ($ip,$msklen) = ipv4_parse($cidr_str);
61 my $cidr = ipv4_parse($ip_str,$msk_str);
62 my ($ip) = ipv4_parse($ip_str,$msk_str);
63
64 Parse an IPv4 address and in scalar context the address in CIDR
65 format and in an array context the address and the mask length.
66
67 If the parameters doesn't contains a netmask or a mask length, in
68 scalar context only the IPv4 address is returned and in an array
69 context the mask length is undefined.
70
71 If the function cannot parse its input, it croaks. Trap it using
72 "eval" if don't like that.
73
74 ipv4_network
75 my $cidr = ipv4_network($ip_str);
76 my $cidr = ipv4_network($cidr_str);
77 my ($net,$msk) = ipv4_network( $net_str, $msk_str);
78
79 In scalar context, this function returns the network in CIDR format
80 in which the address is. In array context, it returns the network
81 address and its mask length as a two elements array. If the input
82 is an host without a netmask of mask length, the default netmask is
83 assumed.
84
85 Again, the function croak if the input is invalid.
86
87 ipv4_broadcast
88 my ($broadcast) = ipv4_broadcast($ip_str);
89 my $broadcast = ipv4_broadcast($ip_str,$msk_str);
90
91 This function returns the broadcast address. If the input doesn't
92 contains a netmask or mask length, the default netmask is assumed.
93
94 This function croaks if the input is invalid.
95
96 ipv4_network
97 my $cidr = ipv4_network($net_str);
98 my $cidr = ipv4_network($cidr_sstr);
99 my ($net,$msk) = ipv4_network( $ip_str, $mask_str);
100
101 In scalar context, this function returns the network in CIDR format
102 in which the address is. In array context, it returns the network
103 address and its mask length as a two elements array. If the input
104 is an host without a netmask or mask length, the default netmask is
105 assumed.
106
107 Again, the function croak if the input is invalid.
108
109 ipv4_in_network
110 print "Yes" if ipv4_in_network( $cidr_str1, $cidr_str2);
111 print "Yes" if ipv4_in_network( $ip_str1, $mask_str1, $cidr_str2 );
112 print "Yes" if ipv4_in_network( $ip1, $mask1, $ip2, $msk2 );
113
114 This function checks if the second network is contained in the
115 first one and it implements the following semantics :
116
117 If net1 or net2 is a magic address (0.0.0.0 or 255.255.255.255)
118 than this function returns true.
119
120 If net1 is an host, net2 will be in the same net only if
121 it is the same host.
122
123 If net2 is an host, it will be contained in net1 only if
124 it is part of net1.
125
126 If net2 is only part of net1 if it is entirely contained in
127 net1.
128
129 Trap bad input with "eval" or else.
130
131 ipv4_checkip
132 if ($ip = ipv4_checkip($str) ) {
133 # Do something
134 }
135
136 Return the IPv4 address in the string or undef if the input doesn't
137 contains a valid IPv4 address.
138
139 ipv4_cidr2msk
140 my $netmask = ipv4_cidr2msk( $cidr );
141
142 Returns the netmask corresponding to the mask length given in
143 input. As usual, croaks if it doesn't like your input (in this
144 case a number between 0 and 32).
145
146 ipv4_msk2cidr
147 my $masklen = ipv4_msk2cidr( $msk );
148
149 Returns the mask length of the netmask in input. As usual, croaks
150 if it doesn't like your input.
151
153 Francis J. Lacoste <francis.lacoste@iNsu.COM>
154
156 Copyright (c) 1999, 2000 iNsu Innovations Inc. All rights reserved.
157
158 This program is free software; you can redistribute it and/or modify it
159 under the terms as perl itself.
160
162 perl(1) ipv4calc(1).
163
164
165
166perl v5.34.0 2022-01-21 IPv4Addr(3)