TMP: add a ctdb snapshot of current ctdb master (git://git.samba.org/ctdb.git) to...
[obnox/samba/samba-obnox.git] / ctdb / tests / eventscripts / stubs / ip
1 #!/bin/sh
2
3 : ${FAKE_IP_STATE:=${PWD}/var/fake-ip-state}
4
5 not_implemented ()
6 {
7     echo "ip stub command: \"$1\" not implemented"
8     exit 127
9 }
10
11
12 case "$1" in
13     link)
14         case "$2" in
15             set)
16                 iface="$3"
17                 case "$4" in
18                     up)
19                         rm -f "${FAKE_IP_STATE}/interfaces-down/${iface}"
20                         ;;
21                     down)
22                         mkdir -p "${FAKE_IP_STATE}/interfaces-down"
23                         touch "${FAKE_IP_STATE}/interfaces-down/${iface}"
24                         ;;
25                     *)
26                         not_implemented "$*"
27                 esac
28                 ;;
29             *)
30                 not_implemented "$1 $2"
31         esac
32         
33         ;;
34     addr)
35         case "$2" in
36             add)
37                 shift 2
38                 local=""
39                 dev=""
40                 brd=""
41                 while [ -n "$1" ] ; do
42                     case "$1" in
43                         *.*.*.*/*)
44                             local="$1" ; shift
45                             ;;
46                         local)
47                             local="$2" ; shift 2
48                             ;;
49                         broadcast|brd)
50                             # For now assume this is always '+'.
51                             if [ "$2" != "+" ] ; then
52                                 not_implemented "addr add ... brd $2 ..."
53                             fi
54                             shift 2
55                             ;;
56                         dev)
57                             dev="$2" ; shift 2
58                             ;;
59                         *)
60                             not_implemented "addr add ... $1 ..."
61                     esac
62                 done
63                 if [ -z "$dev" ] ; then
64                     not_implemented "addr add (without dev)"
65                 fi
66                 mkdir -p "${FAKE_IP_STATE}/addresses"
67                 pf="${FAKE_IP_STATE}/addresses/${dev}-primary"
68                 sf="${FAKE_IP_STATE}/addresses/${dev}-secondary"
69                 # We could lock here... but we should be the only ones
70                 # playing around here with these stubs.
71                 if [ ! -f "$pf" ] ; then
72                     echo "$local" >"$pf"
73                 elif grep -Fq "$local" "$pf" ; then 
74                     echo "RTNETLINK answers: File exists" >&2
75                     exit 254
76                 elif [ -f "$sf" ] && grep -Fq "$local" "$sf" ; then 
77                     echo "RTNETLINK answers: File exists" >&2
78                     exit 254
79                 else
80                     echo "$local" >>"$sf"
81                 fi
82                 ;;
83             delete|del)
84                 shift 2
85                 local=""
86                 dev=""
87                 while [ -n "$1" ] ; do
88                     case "$1" in
89                         *.*.*.*/*)
90                             local="$1" ; shift
91                             ;;
92                         local)
93                             local="$2" ; shift 2
94                             ;;
95                         dev)
96                             dev="$2" ; shift 2
97                             ;;
98                         *)
99                             not_implemented "addr del ... $1 ..."
100                     esac
101                 done
102                 if [ -z "$dev" ] ; then
103                     not_implemented "addr del (without dev)"
104                 fi
105                 mkdir -p "${FAKE_IP_STATE}/addresses"
106                 pf="${FAKE_IP_STATE}/addresses/${dev}-primary"
107                 sf="${FAKE_IP_STATE}/addresses/${dev}-secondary"
108                 # We could lock here... but we should be the only ones
109                 # playing around here with these stubs.
110                 if [ ! -f "$pf" ] ; then
111                     echo "RTNETLINK answers: Cannot assign requested address" >&2
112                     exit 254
113                 elif grep -Fq "$local" "$pf" ; then
114                     # Remove primaries AND SECONDARIES.
115                     rm -f "$pf" "$sf"
116                 elif [ -f "$sf" ] && grep -Fq "$local" "$sf" ; then 
117                     grep -Fv "$local" "$sf" >"${sf}.new"
118                     mv "${sf}.new" "$sf"
119                 else
120                     echo "RTNETLINK answers: Cannot assign requested address" >&2
121                     exit 254
122                 fi
123                 ;;
124             show|list)
125                 shift 2
126                 dev=""
127                 primary=true
128                 secondary=true
129                 while [ -n "$1" ] ; do
130                     case "$1" in
131                         dev)
132                             dev="$2" ; shift 2
133                             ;;
134                         # Do stupid things and stupid things will happen!
135                         primary)
136                             primary=true ; secondary=false ; shift
137                             ;;
138                         secondary)
139                             secondary=true ; primary=false ; shift
140                             ;;
141                         *)
142                             # Assume an interface name
143                             dev="$1" ; shift 1
144                     esac
145                 done
146                 devices="$dev"
147                 if [ -z "$devices" ] ; then
148                     # No device specified?  Get all the primaries...
149                     devices=$(ls "${FAKE_IP_STATE}/addresses/"*-primary 2>/dev/null | \
150                         sed -e 's@.*/@@' -e 's@-primary$@@')
151                 fi
152                 calc_brd ()
153                 {
154                     case "${local#*/}" in
155                         24)
156                             brd="${local%.*}.255"
157                             ;;
158                         *)
159                            not_implemented "list ... fake bits other than 24: ${local#*/}"
160                     esac
161                 }
162                 show_iface()
163                 {
164                     pf="${FAKE_IP_STATE}/addresses/${dev}-primary"
165                     sf="${FAKE_IP_STATE}/addresses/${dev}-secondary"
166                     mac=$(echo $dev | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
167                     cat <<EOF
168 ${n}: ${dev}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
169     link/ether ${mac} brd ff:ff:ff:ff:ff:ff
170 EOF
171                     if $primary && [ -r "$pf" ] ; then
172                         read local <"$pf"
173                         calc_brd
174 cat <<EOF
175     inet ${local} brd ${brd} scope global ${dev}
176 EOF
177                     fi
178                     if $secondary && [ -r "$sf" ] ; then
179                         while read local ; do
180                             calc_brd
181 cat <<EOF
182     inet ${local} brd ${brd} scope global secondary ${dev}
183 EOF
184                         done <"$sf"
185                     fi
186 cat <<EOF
187        valid_lft forever preferred_lft forever
188 EOF
189
190                 }
191                 n=1
192                 for dev in $devices ; do
193                     show_iface
194                     n=$(($n + 1))
195                 done
196                 ;;
197             *)
198                 not_implemented "$1 $2"
199         esac
200         ;;
201     *)
202         not_implemented "$1"
203 esac
204
205 exit 0