]>
git.ipfire.org Git - people/ms/ipfire-3.x.git/blob - pkgs/core/firewall/src/functions.ip
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2009 Michael Tremer & Christian Schmidt #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
22 ###############################################################################
23 # General IP address manipulation functions
24 # ip_encode - Encodes an IP to an integer
25 # Parameters: ip address (e.g. 192.168.0.1)
27 # ip_decode - Decodes an integer to an IP
29 # Returns : ip address
30 # ip_valid - Checks if given IP is valid
34 # General subnet functions
35 # ip_range - Enumerates members of an IP range
36 # Parameters: ip range (e.g. 192.168.0.1-192.168.0.128)
37 # Returns : several subnets/IPs
38 # ip_range_explicit - Enumerates ALL IP addresses of an IP range
39 # Parameters: ip range
40 # Returns : several IPs
41 # subnet_network - Calculates the network address of a CIDR
42 # Parameters: CIDR network (e.g. 192.168.0.0/24)
43 # Returns : Network address
44 # subnet_broadcast - Calculates the broadcast address of a CIDR
45 # Parameters: CIDR network
46 # Returns : Broadcast address
47 # ip_in_subnet - Checks if an IP is in given subnet
48 # Parameters: IP address, subnet
50 # mask_to_cidr - Converts a subnet mask to cidr type
51 # Parameters: subnet (e.g. 255.255.255.0)
52 # Returns : CIDR (e.g. 24)
55 function ip_encode
() {
60 int
=$
(( $
(( $int << 8 )) | $field ))
67 function ip_decode() {
104 first
=$
(ip_encode
${1%-*})
105 last
=$
(ip_encode
${1#*-})
107 if [ $first -gt $last ]; then
108 error
"Invalid IP address range: $1"
113 while [ $l -gt $first ]; do
119 while [ $
(( $first % $y )) -eq 0 ] && [ $l -gt $
(( $first + $y )) ]; do
126 echo $
(ip_decode
$first)$vlsm
127 first
=$
(($first + $z))
131 function ip_range_explicit
() {
136 [0-9]*.
*.
*.
*-*.
*.
*.
*)
144 first
=$
(ip_encode
${1%-*})
145 last
=$
(ip_encode
${1#*-})
147 if [ $first -gt $last ]; then
148 error
"Invalid IP address range: $1"
151 while ! [ $first -gt $last ]; do
152 echo $
(ip_decode
$first)
153 first
=$
(($first + 1))
157 function _netmask
() {
160 [ $vlsm -eq 0 ] && echo 0 ||
echo $
(( -1 << $
(( 32 - $vlsm )) ))
163 function subnet_network
() {
165 encodedaddr
=$
(ip_encode
${1%/*})
167 netmask
=$
(_netmask
$1)
169 echo $
(ip_decode $
(($encodedaddr & $netmask)))
172 function _broadcast
() {
174 x
=$
(( 32 - ${1#*/} ))
175 [ $x -eq 32 ] && echo -1 ||
echo $
(( $
(( 1 << $x )) - 1 ))
178 function subnet_broadcast
() {
180 encodedaddr
=$
(ip_encode
${1%/*})
182 netmask
=$
(_netmask
$1)
184 broadcast
=$
(_broadcast
$1)
186 echo $
(ip_decode $
(( $
(($encodedaddr & $netmask)) |
$broadcast )))
189 function ip_in_subnet
() {
191 netmask
=$
(_netmask
$2)
192 [ $
(( $
(ip_encode
$1) & $netmask)) = $
(( $
(ip_encode
${2%/*}) & $netmask )) ]
195 function mask_to_cidr
() {
201 x
=$
(( 128 << 24 )) # 0x80000000
203 while [ $(( $x & $mask )) -ne 0 ]; do
204 [ $mask -eq $x ] && mask=0 || mask=$(( $mask << 1 ))
208 if [ $(( $mask & 2147483647 )) -ne 0 ]; then # 2147483647 = 0x7fffffff
209 echo "Invalid net mask: $1" >&2
215 function ip_valid() {
220 [0-9]|[0-9][0-9]|[1-2][0-9][0-9])
221 [ $x -lt 256 ] || { IFS=$(ifs); return 1; }