reinstate an initscript for the backend so that /etc/iproute2/rt_tables gets cleaned...
[planetlab-umts-tools.git] / umts_functions
1 #!/bin/bash
2
3
4 STROK="remote IP address"
5 GCOMP="comgt"
6 GCOM="/usr/bin/"${GCOMP}
7 LOGF="/tmp/umtslogs"
8 PPPD_PIDFILE="/var/run/umts_pppd.pid"
9 DESTS_FILE="/tmp/umts_dest_file"
10 FILE_TEMP_NID="/tmp/umts_temp_nid"
11 FILE_UMTS_INT="/tmp/umts_dev"
12 LOCK_DIR="/tmp/umts_lock_d"
13 FILE_LOCK_SLIVER="/tmp/umts_locking_sliver"
14 PPP_INT="ppp0"
15 PPPD="pppd"
16 CHAT_SCRIPTS="/etc/chatscripts"
17 UMTS_CONF="/etc/umts.conf"
18
19
20
21 function get_umts_dev(){
22         echo "/dev/umts_modem"
23 }
24
25 function init_umts(){
26         #local found=1
27         
28         if [ -e $LOCK_DIR ]; then
29                 rmdir $LOCK_DIR
30         fi
31
32         # the routing table is now managed by sliceip
33         #if ! grep "umts_table" /etc/iproute2/rt_tables > /dev/null 2>&1; then
34         #       echo "20 umts_table" >> /etc/iproute2/rt_tables
35         #fi
36
37         return 0
38
39 }
40
41
42 function get_temp_nid(){
43         cat $FILE_TEMP_NID
44 }
45
46
47 #called when the connection is started
48 function conn_on(){
49         local sliver=$1
50
51         #DESTS_FILE contains added destinations
52         rm -f $DESTS_FILE 
53
54         cat $LOGF | grep "local"
55         return 0
56
57 }
58
59 #called when the connection is terminated to remove the rules
60 #on the destinations reachable thorugh the UMTS device
61 function conn_off(){
62         local sliver=$1
63         local ppp_addr=$2
64
65 # Not needed anymore as the killing of the PPP connection
66 # makes the rules pointing to it automatically disappear.
67
68 #       for i in `cat $DESTS_FILE`; do
69 #               del_destination $i $sliver > /dev/null 2>&1
70 #       done
71
72         rm $DESTS_FILE >/dev/null 2>&1
73 }
74
75 function start_umts(){
76     local sliver=$1
77     local sliver_nid=`get_nid $sliver`
78
79     if ! lock $sliver; then
80         return 1
81     fi  
82
83     if status_umts; then
84          echo "Already connected"
85          return 0; 
86     fi
87     
88     echo "Starting GCOM..."
89     if ! $GCOM -d `get_umts_dev`; then
90         unlock $sliver
91         return 1
92     fi
93
94     rm -f $LOGF
95
96     if [ -e $UMTS_CONF ]; then
97         . $UMTS_CONF
98     fi
99  
100     if ! [[ $APN ]]; then
101         APN="web.omnitel.it"
102     fi
103     if ! [[ $NUM ]]; then
104         NUM="*99***1#"
105     fi
106     
107
108     exec /usr/sbin/pppd nodetach `get_umts_dev` 460800 \
109                 0.0.0.0:0.0.0.0 \
110                 connect "/usr/sbin/chat -v                     \
111                 TIMEOUT         6                              \
112                 ABORT           '\nBUSY\r'                     \
113                 ABORT           '\nNO ANSWER\r'                \
114                 ABORT           '\nRINGING\r\n\r\nRINGING\r'   \
115                 ''              ATZ     OK 'ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0' OK  \
116                 'AT+CGDCONT=1,\"IP\",\"$APN\"' OK \
117                 ATD$NUM   CONNECT ''" > $LOGF &
118
119
120     echo $! > $PPPD_PIDFILE
121
122     sleep 5s;  #waiting for the interface to establish the link
123     if grep "$STROK" $LOGF >/dev/null 2>&1; then
124         conn_on $sliver $sliver_nid
125     else
126         #second try
127         sleep 5s;
128         if grep "$STROK" $LOGF >/dev/null 2>&1; then
129             conn_on $sliver $sliver_nid
130         else
131             stop_umts $sliver
132             return 1
133         fi
134     fi
135 }
136
137
138 # stop the UMTS connection by killing the pppd daemon and
139 # then by calling conn_off
140 function stop_umts(){
141     local sliver=$1
142     local sliver_nid=`get_nid $sliver`
143     local ppp_addr
144
145     if ! [ -e $PPPD_PIDFILE ]; then 
146         echo "Disconnected"
147         return 0;
148     fi
149
150     ppp_addr=`get_ppp_address`
151
152     if ! check_who_locked $sliver; then
153         return 1
154     fi
155
156     PID=`cat $PPPD_PIDFILE`;
157
158     if [ -d /proc/$PID ] &&  grep $PPPD /proc/$PID/cmdline >/dev/null 2>&1; then        
159         kill $PID;      
160         touch $DESTS_FILE
161         sleep 2s;
162     fi
163
164     if status_umts; then
165         return 1;
166     else
167         conn_off $sliver $ppp_addr
168         cat $LOGF | grep "time"
169         rm $PPPD_PIDFILE
170         unlock $sliver;
171         return 0
172     fi
173 }
174
175 # check the status of the umts connection by looking at then
176 # state of pppd daemon
177 function status_umts(){
178     local sliver=$1
179         
180     if ! [ -e $PPPD_PIDFILE ]; then return 1; fi
181     PID=`cat $PPPD_PIDFILE`
182
183     if [ -d /proc/$PID ] &&  grep $PPPD /proc/$PID/cmdline >/dev/null 2>&1; then
184         return 0;
185     else 
186         return 1;
187     fi
188 }
189
190 #add the ppp interface to the slice - not used at the moment
191 function add_interface(){
192     local nid=`get_nid $1`
193     $NADDRESS --add --nid $nid --ip $PPP_INT >>$LOGFILE 2>&1;
194 }
195
196 #remove the ppp interface from the slice - not used at the moment
197 function del_interface(){
198     local nid=`get_nid $1`
199     #$NADDRESS --remove --nid $NID --ip $PPP_INT >>$LOGFILE 2>&1;
200 }
201
202 #get slice network id
203 function get_nid(){
204     id -u ${1}
205 }
206
207
208 # check if sliceip is activated for the slice
209 function check_sliceip(){
210   
211   if ! [ -e /vsys/sliceip ]; then
212     echo "Sliceip is not installed. Exiting."
213     return 1
214   fi
215   
216 }
217
218 # Deliver a command to sliceip (basically to set the destinations to be
219 # reached through the UMTS device)
220 function sliceip_cmd(){
221
222   local command=$2
223   local sliver=$1
224   
225   echo "$command" | /vsys/sliceip $sliver
226   
227 }
228
229 # Add a destination to be reached through the UMTS device
230 function add_destination(){
231         local dest="$1"
232         local sliver=$2
233
234         if [[ ! $dest ]]; then return 1; fi
235
236         if ! status_umts; then
237                 return 1;
238         fi
239
240         if ! check_who_locked $sliver; then
241                 return 1;
242         fi
243
244         sliceip_cmd $sliver "route add $dest dev ${PPP_INT}"    
245           
246 }
247
248 # Delete a destination that was previously reached through the UMTS device.
249 function del_destination(){
250         local dest="$1"
251         local sliver=$2
252         
253         if [[ ! $dest ]]; then return 1; fi
254
255         if ! check_who_locked $sliver; then
256                 return 1;
257         fi
258
259         sliceip_cmd $sliver "route del $dest dev ${PPP_INT}";
260
261 }
262
263
264 function get_ppp_address(){
265         ifconfig $PPP_INT | grep inet\ addr | cut -d ":" -f 2 | cut -d " " -f 1
266 }
267
268
269 function check_who_locked(){
270         local sliver=$1
271         local sliver_nid=`get_nid $sliver`
272         local ret=0
273
274         if [ -e $LOCK_DIR ]; then
275                 if [[ `cat $FILE_LOCK_SLIVER` != $sliver_nid ]]; then
276                         echo "Interface in use by another slice.";
277                         ret=1
278                 fi
279         else
280                 ret=1
281         fi
282
283         return $ret
284
285 }
286
287 # kill the gcom daemon
288 function kill_gcom(){
289         
290         killall $GCOMP
291         sleep 3
292         if ! ps -C $GCOMP >/dev/null 2>&1; then
293                 echo "$GCOMP terminated."       
294         else 
295                 echo "$GCOMP still alive. Try kill -9"
296                 sleep 2
297
298                 killall -9 $GCOMP
299         
300                 if ! ps -C $GCOMP >/dev/null 2>&1; then
301                         echo "$GCOMP terminated."
302                 else            
303                         echo "Couldn't stop $GCOMP. Please contact administrators for assistance."      
304                 fi
305         fi
306 }
307
308 # lock the umts connection to a specific sliver.
309 # only one sliver at a given time can use the UMTS device
310 function lock(){
311         local sliver=$1
312 #       local sliver_nid=`get_nid $sliver`
313         local ret=0
314
315         if [ -e $LOCK_DIR ]; then
316                 if ! check_who_locked $sliver; then
317                         ret=1
318                 fi
319         else 
320                 if mkdir $LOCK_DIR >/dev/null 2>&1; then
321                         echo "$sliver_nid" > $FILE_LOCK_SLIVER
322                 else
323                         ret=1
324                 fi
325         fi
326
327         return $ret
328 }
329
330 function unlock(){
331         local sliver=$1
332         local sliver_nid=`get_nid $sliver`
333         local ret=0
334
335         if ! [ -e $LOCK_DIR ]; then
336                 ret=1
337         else 
338                 if ! check_who_locked $sliver; then
339                         ret=1
340                 else
341                         if ! rmdir $LOCK_DIR; then
342                                 ret=1
343                         fi
344                 fi      
345         fi
346
347         return $ret
348 }
349
350
351
352
353 # checks an ip addresse for validity
354 function valid_dotted_quad(){
355     oldIFS=$IFS
356     IFS=.
357     set -f
358     set -- $1
359     if [ $# -eq 4 ]
360     then
361       for seg
362       do
363         case $seg in
364             ""|*[!0-9]*) return 1; break ;; ## Segment empty or non-numeric char
365             *) [ $seg -gt 255 ] && return 2 ;;
366         esac
367       done
368     else
369       return 3 ## Not 4 segments
370     fi
371     IFS=$oldIFS
372     set +f
373     return 0;
374 }
375