]> git.ipfire.org Git - people/stevee/network.git/blame - functions.ipv4
network: Some more general IP functions.
[people/stevee/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
22function ipv4_split_prefix() {
23 ip_split_prefix $@
24}
25
26function ipv4_is_valid() {
27 local address=${1}
28
29 assert isset address
30
31 # Cut the /24 if there is one given
32 address=$(ipv4_split_prefix ${address})
33
34 local IFS="."
35 local octet
36 local count
37 for octet in ${address}; do
38 if [ ${octet} -ge 0 ] && [ ${octet} -le 255 ]; then
39 count=$(( ${count} + 1 ))
40 continue
41 fi
42
43 # If we get here the address was not valid
44 break
45 done
46
47 if [ ${count} -eq 4 ]; then
48 return ${EXIT_OK}
49 fi
50
51 return ${EXIT_ERROR}
52}
53
54function ipv4_detect_duplicate() {
55 local device=${1}
56 local address=${2}
57
58 assert isset address
59 assert isset device
60 assert device_exists ${device}
61
62 if ! arping -q -c 2 -w 3 -D -I ${device} ${address}; then
63 log DEBUG "Detected duplicate address '${address}' on device '${device}'."
64 return ${EXIT_ERROR}
65 fi
66
67 return ${EXIT_OK}
68}
69
70function ipv4_update_neighbours() {
71 local device=${1}
72 local address=${2}
73
74 arping -q -A -c 1 -I ${device} ${address}
75 ( sleep 2; arping -q -U -c 1 -I ${device} ${address} ) >/dev/null 2>&1 </dev/null &
76}