]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - config/cfgroot/network-functions.pl
core94: Ship changes from dma branch
[people/pmueller/ipfire-2.x.git] / config / cfgroot / network-functions.pl
CommitLineData
4e9a2b57
MT
1#!/usr/bin/perl -w
2############################################################################
3# #
4# This file is part of the IPFire Firewall. #
5# #
6# IPFire is free software; you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation; either version 2 of the License, or #
9# (at your option) any later version. #
10# #
11# IPFire is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with IPFire; if not, write to the Free Software #
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
19# #
20# Copyright (C) 2014 IPFire Team <info@ipfire.org>. #
21# #
22############################################################################
23
24package Network;
25
5428eeee
MT
26require "/var/ipfire/general-functions.pl";
27
4e9a2b57
MT
28use Socket;
29
30my %PREFIX2NETMASK = (
31 32 => "255.255.255.255",
32 31 => "255.255.255.254",
33 30 => "255.255.255.252",
34 29 => "255.255.255.248",
35 28 => "255.255.255.240",
36 27 => "255.255.255.224",
37 26 => "255.255.255.192",
38 25 => "255.255.255.128",
39 24 => "255.255.255.0",
40 23 => "255.255.254.0",
41 22 => "255.255.252.0",
42 21 => "255.255.248.0",
43 20 => "255.255.240.0",
44 19 => "255.255.224.0",
45 18 => "255.255.192.0",
46 17 => "255.255.128.0",
47 16 => "255.255.0.0",
48 15 => "255.254.0.0",
49 14 => "255.252.0.0",
50 13 => "255.248.0.0",
51 12 => "255.240.0.0",
52 11 => "255.224.0.0",
53 10 => "255.192.0.0",
54 9 => "255.128.0.0",
55 8 => "255.0.0.0",
56 7 => "254.0.0.0",
57 6 => "252.0.0.0",
58 5 => "248.0.0.0",
59 4 => "240.0.0.0",
60 3 => "224.0.0.0",
61 2 => "192.0.0.0",
62 1 => "128.0.0.0",
63 0 => "0.0.0.0"
64);
65
66my %NETMASK2PREFIX = reverse(%PREFIX2NETMASK);
67
68# Takes an IP address in dotted decimal notation and
69# returns a 32 bit integer representing that IP addresss.
70# Will return undef for invalid inputs.
71sub ip2bin($) {
72 my $address = shift;
73
74 # This function returns undef for undefined input.
75 if (!defined $address) {
76 return undef;
77 }
78
79 my $address_bin = &Socket::inet_pton(AF_INET, $address);
80 if ($address_bin) {
81 $address_bin = unpack('N', $address_bin);
82 }
83
84 return $address_bin;
85}
86
87# Does the reverse of ip2bin().
88# Will return undef for invalid inputs.
89sub bin2ip($) {
90 my $address_bin = shift;
91
92 # This function returns undef for undefined input.
93 if (!defined $address_bin) {
94 return undef;
95 }
96
97 my $address = pack('N', $address_bin);
98 if ($address) {
99 $address = &Socket::inet_ntop(AF_INET, $address);
100 }
101
102 return $address;
103}
104
105# Takes a network in either a.b.c.d/a.b.c.d or a.b.c.d/e notation
106# and will return an 32 bit integer representing the start
107# address and an other one representing the network mask.
108sub network2bin($) {
109 my $network = shift;
110
111 my ($address, $netmask) = split(/\//, $network, 2);
112
113 if (&check_prefix($netmask)) {
114 $netmask = &convert_prefix2netmask($netmask);
115 }
116
117 my $address_bin = &ip2bin($address);
118 my $netmask_bin = &ip2bin($netmask);
119
120 my $network_start = $address_bin & $netmask_bin;
121
122 return ($network_start, $netmask_bin);
123}
124
125# Returns True for all valid IP addresses
126sub check_ip_address($) {
127 my $address = shift;
128
129 # Normalise the IP address and compare the result with
130 # the input - which should obviously the same.
131 my $normalised_address = &_normalise_ip_address($address);
132
133 return ((defined $normalised_address) && ($address eq $normalised_address));
134}
135
136# Returns True for all valid prefixes.
137sub check_prefix($) {
138 my $prefix = shift;
139
140 return (exists $PREFIX2NETMASK{$prefix});
141}
142
143# Returns True for all valid subnet masks.
144sub check_netmask($) {
145 my $netmask = shift;
146
147 return (exists $NETMASK2PREFIX{$netmask});
148}
149
150# Returns True for all valid inputs like a.b.c.d/a.b.c.d.
151sub check_ip_address_and_netmask($$) {
152 my $network = shift;
153
154 my ($address, $netmask) = split(/\//, $network, 2);
155
156 # Check if the IP address is fine.
157 #
158 my $result = &check_ip_address($address);
159 unless ($result) {
160 return $result;
161 }
162
163 return &check_netmask($netmask);
164}
165
166# For internal use only. Will take an IP address and
167# return it in a normalised style. Like 8.8.8.010 -> 8.8.8.8.
168sub _normalise_ip_address($) {
169 my $address = shift;
170
171 my $address_bin = &ip2bin($address);
172 if (!defined $address_bin) {
173 return undef;
174 }
175
176 return &bin2ip($address_bin);
177}
178
179# Returns the prefix for the given subnet mask.
180sub convert_netmask2prefix($) {
181 my $netmask = shift;
182
183 if (exists $NETMASK2PREFIX{$netmask}) {
184 return $NETMASK2PREFIX{$netmask};
185 }
186
187 return undef;
188}
189
190# Returns the subnet mask for the given prefix.
191sub convert_prefix2netmask($) {
192 my $prefix = shift;
193
194 if (exists $PREFIX2NETMASK{$prefix}) {
195 return $PREFIX2NETMASK{$prefix};
196 }
197
198 return undef;
199}
200
201# Takes an IP address and an offset and
202# will return the offset'th IP address.
203sub find_next_ip_address($$) {
204 my $address = shift;
205 my $offset = shift;
206
207 my $address_bin = &ip2bin($address);
208 $address_bin += $offset;
209
210 return &bin2ip($address_bin);
211}
212
213# Returns the network address of the given network.
214sub get_netaddress($) {
215 my $network = shift;
216 my ($network_bin, $netmask_bin) = &network2bin($network);
217
218 if (defined $network_bin) {
219 return &bin2ip($network_bin);
220 }
221
222 return undef;
223}
224
225# Returns the broadcast of the given network.
226sub get_broadcast($) {
227 my $network = shift;
228 my ($network_bin, $netmask_bin) = &network2bin($network);
229
230 return &bin2ip($network_bin ^ ~$netmask_bin);
231}
232
233# Returns True if $address is in $network.
234sub ip_address_in_network($$) {
235 my $address = shift;
236 my $network = shift;
237
238 my $address_bin = &ip2bin($address);
239 return undef unless (defined $address_bin);
240
241 my ($network_bin, $netmask_bin) = &network2bin($network);
242
243 # Find end address
244 my $broadcast_bin = $network_bin ^ ~$netmask_bin;
245
246 return (($address_bin ge $network_bin) && ($address_bin le $broadcast_bin));
247}
248
5428eeee
MT
249sub setup_upstream_proxy() {
250 my %proxysettings = ();
251 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
252
253 if ($proxysettings{'UPSTREAM_PROXY'}) {
254 my $credentials = "";
255
256 if ($proxysettings{'UPSTREAM_USER'}) {
257 $credentials = $proxysettings{'UPSTREAM_USER'};
258
259 if ($proxysettings{'UPSTREAM_PASSWORD'}) {
260 $credentials .= ":" . $proxysettings{'UPSTREAM_PASSWORD'};
261 }
262
263 $credentials .= "@";
264 }
265
266 my $proxy = "http://" . $credentials . $proxysettings{'UPSTREAM_PROXY'};
267
268 $ENV{'http_proxy'} = $proxy;
269 $ENV{'https_proxy'} = $proxy;
270 $ENV{'ftp_proxy'} = $proxy;
271 }
272}
273
4e9a2b57
MT
2741;
275
276# Remove the next line to enable the testsuite
277__END__
278
279sub assert($) {
280 my $ret = shift;
281
282 if ($ret) {
283 return;
284 }
285
286 print "ASSERTION ERROR";
287 exit(1);
288}
289
290sub testsuite() {
291 my $result;
292
293 my $address1 = &ip2bin("8.8.8.8");
294 assert($address1 == 134744072);
295
296 my $address2 = &bin2ip($address1);
297 assert($address2 eq "8.8.8.8");
298
299 # Check if valid IP addresses are correctly recognised.
300 foreach my $address ("1.2.3.4", "192.168.180.1", "127.0.0.1") {
301 if (!&check_ip_address($address)) {
302 print "$address is not correctly recognised as a valid IP address!\n";
303 exit 1;
304 };
305 }
306
307 # Check if invalid IP addresses are correctly found.
308 foreach my $address ("456.2.3.4", "192.768.180.1", "127.1", "1", "a.b.c.d", "1.2.3.4.5", "1.2.3.4/12") {
309 if (&check_ip_address($address)) {
310 print "$address is recognised as a valid IP address!\n";
311 exit 1;
312 };
313 }
314
315 $result = &check_ip_address_and_netmask("192.168.180.0/255.255.255.0");
316 assert($result);
317
318 $result = &convert_netmask2prefix("255.255.254.0");
319 assert($result == 23);
320
321 $result = &convert_prefix2netmask(8);
322 assert($result eq "255.0.0.0");
323
324 $result = &find_next_ip_address("1.2.3.4", 2);
325 assert($result eq "1.2.3.6");
326
327 $result = &ip_address_in_network("10.0.1.4", "10.0.0.0/8");
328 assert($result);
329
330 return 0;
331}
332
333&testsuite();