[packages] olsrd: init: reduce code duplication by reusing already_in_schema()
[feed/routing.git] / files / olsrd.init
index 6d08b69b2e93cebdb735a9ff7c74d4d67986d8cf..1172623023adf60a01fb3412152d9fa02c369e83 100644 (file)
@@ -225,6 +225,7 @@ config_update_schema() {
 }
 
 config_write_options() {
+       local funcname="config_write_options"
        unset IFS
        local schema="$1"
        local cfg="$2"
@@ -232,6 +233,7 @@ config_write_options() {
        local write_func="$3"
        [ -z "$write_func" ] && output_func=echo
        local write_param="$4"
+
        local schema_entry
        local option
        local option_length
@@ -243,20 +245,108 @@ config_write_options() {
        local list_value
        local i
        local position
+       local speed
+       local list_speed_vars="HelloInterval HelloValidityTime TcInterval TcValidityTime MidInterval MidValidityTime HnaInterval HnaValidityTime"
+
+       get_value_for_entry()
+       {
+               local schema_entry="$1"
 
-       for schema_entry in $schema; do
                default="${schema_entry#*[=]}"
                [ "$default" = "$schema_entry" ] && default=
                option="${schema_entry%%[=]*}"
-               IFS=':'
-               set -- $option
-               unset IFS
+
+               IFS=':'; set -- $option; unset IFS
                option="$1"
                option_type="$2"
-               validate_varname "$option" || continue
-               [ -z "$option_type" ] || validate_varname "$option_type" || continue
-               [ "$option_type" = internal ] && continue
+
+               validate_varname "$option" || return 1
+               [ -z "$option_type" ] || validate_varname "$option_type" || return 1
+               [ "$option_type" = internal ] && return 1
+
                config_get value "$cfg" "$option"
+               [ "$option" = "speed" ] && return 1
+
+               return 0
+       }
+
+       already_in_schema()
+       {
+               case " $schema " in
+                       *" $1 "*)
+                               return 0
+                       ;;
+                       *)
+                               return 1
+                       ;;
+               esac
+       }
+
+       already_in_schema "speed" && {
+               get_value_for_entry "speed"
+
+               if [ 2>/dev/null $value -gt 0 -a $value -le 20 ]; then
+                       speed="$value"
+               else
+                       log "$funcname() Warning: invalid speed-value: '$value' - allowed integers: 1...20, fallback to 6"
+                       speed=6
+               fi
+
+               for schema_entry in $list_speed_vars; do {
+                       already_in_schema "$schema_entry" || schema="$schema $schema_entry"
+               } done
+       }
+
+       for schema_entry in $schema; do
+               if [ -n "$speed" ]; then                # like sven-ola freifunk firmware fff-1.7.4
+                       case "$schema_entry" in
+                               HelloInterval)
+                                       value="$(( $speed / 2 + 1 )).0"
+                               ;;
+                               HelloValidityTime)
+                                       value="$(( $speed * 25 )).0"
+                               ;;
+                               TcInterval)     # todo: not fisheye? -> $(( $speed * 2 ))
+                                       value=$(( $speed / 2 ))
+                                       [ $value -eq 0 ] && value=1
+                                       value="$value.0"
+                               ;;
+                               TcValidityTime)
+                                       value="$(( $speed * 100 )).0"
+                               ;;
+                               MidInterval)
+                                       value="$(( $speed * 5 )).0"
+                               ;;
+                               MidValidityTime)
+                                       value="$(( $speed * 100 )).0"
+                               ;;
+                               HnaInterval)
+                                       value="$(( $speed * 2 )).0"
+                               ;;
+                               HnaValidityTime)
+                                       value="$(( $speed * 25 )).0"
+                               ;;
+                               *)
+                                       get_value_for_entry "$schema_entry" || continue
+                               ;;
+                       esac
+
+                       is_speed_var()
+                       {
+                               case " $list_speed_vars " in
+                                       *" $1 "*)
+                                               return 0
+                                       ;;
+                                       *)
+                                               return 1
+                                       ;;
+                               esac
+                       }
+
+                       is_speed_var "$schema_entry" && option="$schema_entry"
+               else
+                       get_value_for_entry "$schema_entry" || continue
+               fi
 
                if [ -z "$value" ]; then
                        IFS='+'
@@ -481,6 +571,7 @@ olsrd_write_interface() {
 
        ifnames=
        config_get interfaces "$cfg" interface
+
        for interface in $interfaces; do
                if validate_varname "$interface"; then
                        if network_get_device IFNAME "$interface"; then
@@ -558,6 +649,37 @@ olsrd_write_config() {
        return 0
 }
 
+get_wan_ifnames()
+{
+       local wanifnames word catch_next
+
+       which ip >/dev/null || return 1
+
+       set -- $( ip route list exact 0.0.0.0/0 table all )
+       for word in $*; do
+               case "$word" in
+                       dev)
+                               catch_next="true"
+                       ;;
+                       *)
+                               [ -n "$catch_next" ] && {
+                                       case "$wanifnames" in
+                                               *" $word "*)
+                                               ;;
+                                               *)
+                                                       wanifnames="$wanifnames $word "
+                                               ;;
+                                       esac
+
+                                       catch_next=
+                               }
+                       ;;
+               esac
+       done
+
+       echo "$wanifnames"
+}
+
 olsrd_setup_smartgw_rules() {
        local funcname="olsrd_setup_smartgw_rules"
        # Check if ipip is installed
@@ -566,10 +688,12 @@ olsrd_setup_smartgw_rules() {
                return 1
        }
 
-       wanifnames=$(ip r l e 0/0 t all | sed -e 's/^.* dev //' |cut -d " " -f 1 | sort | uniq)
-       nowan=0
+       local wanifnames="$( get_wan_ifnames )"
+
        if [ -z "$wanifnames" ]; then
                nowan=1
+       else
+               nowan=0
        fi
 
        IP4T=$(which iptables)