/[vps]/baselayout-vserver/trunk/lib/rcscripts/net.modules.d/apipa
Gentoo

Contents of /baselayout-vserver/trunk/lib/rcscripts/net.modules.d/apipa

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations) (download)
Sat Sep 3 16:10:27 2005 UTC (9 years ago) by hollow
File size: 2446 byte(s)
import initial baselayout sources (1.12.0_pre8)
1 #!/bin/bash
2 # Copyright (c) 2004-2005 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 # Contributed by Roy Marples (uberlord@gentoo.org)
6
7 # char* apipa_provides(void)
8 #
9 # Returns a string to change module definition for starting up
10 apipa_provides() {
11 echo "apipa"
12 }
13
14 # void apipa_depend(void)
15 #
16 # Sets up the dependancies for the module
17 apipa_depend() {
18 after system dhcp vlan
19 }
20
21 # bool apipa_check_installed(void)
22 #
23 # Returns 1 if ifenslave is installed, otherwise 0
24 apipa_check_installed() {
25 [[ -x /sbin/arping || -x /usr/sbin/arping2 ]] && return 0
26 if ${1:-false}; then
27 eerror "For Automatic Private IP Addressing (APIPA) support"
28 eerror "emerge net-misc/iputils or net-analyzer/arping"
29 fi
30 return 1
31 }
32
33 # bool apipa_check_depends(void)
34 #
35 # Checks to see if we have the needed functions
36 apipa_check_depends() {
37 local f
38
39 for f in interface_exists interface_up; do
40 [[ $( type -t "${f}" ) == "function" ]] && continue
41 eerror "apipa: missing required function ${f}\n"
42 return 1
43 done
44
45 return 0
46 }
47
48 # bool address_exists(char *interface, char *address)
49 #
50 # Returns 0 if the address on the interface responds to an arping
51 # 1 if not - packets defaults to 1
52 # If neither arping (net-misc/iputils) or arping2 (net-analyzer/arping)
53 # is installed then we return 1
54 address_exists() {
55 local iface="$1" address="${2%%/*}" i
56
57 # We only handle IPv4 addresses
58 [[ ${address} != *.*.*.* ]] && return 1
59
60 # We need to bring the interface up to test
61 interface_up "${iface}"
62
63 if [[ -x /sbin/arping ]]; then
64 /sbin/arping -q -c 2 -w 3 -D -f -I "${iface}" "${address}" \
65 &>/dev/null || return 0
66 elif [[ -x /usr/sbin/arping2 ]]; then
67 for (( i=0; i<3; i++ )); do
68 /usr/sbin/arping2 -0 -c 1 -i "${iface}" "${address}" \
69 &>/dev/null && return 0
70 done
71 fi
72 return 1
73 }
74
75 # bool apipa_start(char *iface)
76 #
77 # Tries to locate an address in the 169.254.0.0 netmask 169.254.255.255 range
78 apipa_start() {
79 local iface="$1" i1 i2 addr i=0
80
81 interface_exists "${iface}" true || return 1
82
83 ebegin "Searching for free addresses"
84 interface_up "${iface}"
85
86 while [[ ${i} -lt 64516 ]]; do
87 (( i1=${RANDOM}%255 ))
88 (( i2=${RANDOM}%255 ))
89
90 addr="169.254.${i1}.${i2}"
91 if ! address_exists "${iface}" "${addr}" ; then
92 config[config_counter]="${addr}/16 broadcast 169.254.255.255"
93 (( config_counter-- ))
94 eend 0
95 return 0
96 fi
97
98 (( i++ ))
99 done
100
101 eend 1 "No free address found!"
102 return 1
103 }
104
105 # vim:ts=4

  ViewVC Help
Powered by ViewVC 1.1.20