]> git.ipfire.org Git - people/arne_f/network.git/blame - functions.ipv4
ipv[46]-static: Sum up some functions.
[people/arne_f/network.git] / functions.ipv4
CommitLineData
05c234a8
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
21
e617226b
MT
22IP_SUPPORTED_PROTOCOLS="${IP_SUPPORTED_PROTOCOLS} ipv4"
23
05c234a8
MT
24function ipv4_is_valid() {
25 local address=${1}
26
27 assert isset address
28
38f61548
MT
29 # Cut the prefix if there is one given
30 local prefix=$(ip_get_prefix ${address})
31 address=$(ip_split_prefix ${address})
32
33 # If address is larger than 15 characters it cannot be an IPv4 address
34 [ ${#address} -gt 15 ] && return ${EXIT_ERROR}
35
36 # Check for a valid IPv4 prefix if provided
37 if [ -n "${prefix}" ]; then
38 if [ ${prefix} -lt 4 ] || [ ${prefix} -gt 30 ]; then
39 return ${EXIT_ERROR}
40 fi
41 fi
05c234a8
MT
42
43 local IFS="."
44 local octet
45 local count
46 for octet in ${address}; do
47 if [ ${octet} -ge 0 ] && [ ${octet} -le 255 ]; then
48 count=$(( ${count} + 1 ))
49 continue
50 fi
51
52 # If we get here the address was not valid
53 break
54 done
55
56 if [ ${count} -eq 4 ]; then
57 return ${EXIT_OK}
58 fi
59
60 return ${EXIT_ERROR}
61}
62
63function ipv4_detect_duplicate() {
64 local device=${1}
65 local address=${2}
66
67 assert isset address
68 assert isset device
69 assert device_exists ${device}
70
71 if ! arping -q -c 2 -w 3 -D -I ${device} ${address}; then
72 log DEBUG "Detected duplicate address '${address}' on device '${device}'."
9eebfc55 73 return ${EXIT_OK}
05c234a8
MT
74 fi
75
9eebfc55 76 return ${EXIT_ERROR}
05c234a8
MT
77}
78
79function ipv4_update_neighbours() {
80 local device=${1}
81 local address=${2}
82
83 arping -q -A -c 1 -I ${device} ${address}
84 ( sleep 2; arping -q -U -c 1 -I ${device} ${address} ) >/dev/null 2>&1 </dev/null &
85}