lede-packages-rs

git clone git://archive.git.mtrnord.blog/MTRNord/lede-packages-rs.git
Log | Files | Refs | README | LICENSE

aiccu.sh (3667B)


      1 #!/bin/sh
      2 # aiccu.sh - AICCU proto
      3 # Copyright (c) 2014 OpenWrt.org
      4 
      5 [ -n "$INCLUDE_ONLY" ] || {
      6 	. /lib/functions.sh
      7 	. /lib/functions/network.sh
      8 	. ../netifd-proto.sh
      9 	init_proto "$@"
     10 }
     11 
     12 proto_aiccu_setup() {
     13 	local cfg="$1"
     14 	local iface="$2"
     15 	local link="aiccu-$cfg"
     16 
     17 	local username password protocol server ip6prefix tunnelid requiretls defaultroute nat heartbeat verbose sourcerouting ip6addr ntpsynctimeout
     18 	json_get_vars username password protocol server ip6prefix tunnelid requiretls defaultroute nat heartbeat verbose sourcerouting ip6addr ntpsynctimeout
     19 
     20 	[ -z "$username" -o -z "$password" ] && {
     21 		proto_notify_error "$cfg" "MISSING_USERNAME_OR_PASSWORD"
     22 		proto_block_restart "$cfg"
     23 		return
     24 	}
     25 
     26 	( proto_add_host_dependency "$cfg" 0.0.0.0 )
     27 
     28 	CFGFILE="/var/etc/${link}.conf"
     29 	PIDFILE="/var/run/${link}.pid"
     30 	NTPSTRATUMFILE="/var/run/aiccu_ntp_stratum"
     31 	mkdir -p /var/run /var/etc
     32 
     33 	echo "username $username" > "$CFGFILE"
     34 	echo "password $password" >> "$CFGFILE"
     35 	echo "ipv6_interface $link" >> "$CFGFILE"
     36 	[ -n "$server" ] && echo "server $server" >> "$CFGFILE"
     37 	[ -n "$protocol" ] && echo "protocol $protocol" >> "$CFGFILE"
     38 	[ -n "$tunnelid" ] && echo "tunnel_id $tunnelid" >> "$CFGFILE"
     39 	[ "$requiretls" == 1 ] && echo "requiretls true" >> "$CFGFILE"
     40 	[ "$nat" == 1 ] && echo "behindnat true" >> "$CFGFILE"
     41 	[ "$heartbeat" == 1 ] && echo "makebeats true" >> "$CFGFILE"
     42 	[ "$verbose" == 1 ] && echo "verbose true" >> "$CFGFILE"
     43 	echo "defaultroute false" >> "$CFGFILE"
     44 	echo "daemonize true" >> "$CFGFILE"
     45 	echo "pidfile $PIDFILE" >> "$CFGFILE"
     46 
     47 	# By default, wait at most 90 seconds for NTP sync
     48 	[ -z "$ntpsynctimeout" ] && ntpsynctimeout=90
     49 	for i in $(seq 1 $ntpsynctimeout); do
     50 		[ -f "$NTPSTRATUMFILE" ] && \
     51 		[ "$(cat $NTPSTRATUMFILE)" -lt 16 ] && \
     52 		echo "NTP synced, stratum $(cat $NTPSTRATUMFILE)" && break
     53 		[ "$(( $i % 10 ))" -eq 0 ] && echo "Waiting ${i} secs for NTP sync..."
     54 		sleep 1
     55 	done
     56 
     57 	aiccu start "$CFGFILE"
     58 
     59 	[ "$?" -ne 0 ] && {
     60 		proto_notify_error "$cfg" "AICCU_FAILED_SEE_LOG"
     61 		proto_block_restart "$cfg"
     62 		return
     63 	}
     64 
     65 	proto_init_update "$link" 1
     66 
     67 	local source=""
     68 	[ "$sourcerouting" != "0" ] && source="::/128"
     69 	[ "$defaultroute" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$source"
     70 
     71 	[ -n "$ip6addr" ] && {
     72 		local local6="${ip6addr%%/*}"
     73 		local mask6="${ip6addr##*/}"
     74 		[[ "$local6" = "$mask6" ]] && mask6=
     75 		proto_add_ipv6_address "$local6" "$mask6"
     76 		[ "$defaultroute" != "0" -a "$sourcerouting" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$local6/$mask6"
     77 	}
     78 
     79 	[ -n "$ip6prefix" ] && {
     80 		proto_add_ipv6_prefix "$ip6prefix"
     81 		[ "$defaultroute" != "0" -a "$sourcerouting" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$ip6prefix"
     82 	}
     83 
     84 	proto_send_update "$cfg"
     85 
     86 }
     87 
     88 proto_aiccu_teardown() {
     89 	local cfg="$1"
     90 	local link="aiccu-$cfg"
     91 	CFGFILE="/var/etc/${link}.conf"
     92 	PIDFILE="/var/run/${link}.pid"
     93 	[ -f "$CFGFILE" -a -f "$PIDFILE" ] && {
     94 		local pid="$(cat "$PIDFILE")"
     95 		[ -d /proc/$pid -a $(cat /proc/$pid/comm) = "aiccu" ] && \
     96 		aiccu stop "$CFGFILE"
     97 	}
     98 }
     99 
    100 proto_aiccu_init_config() {
    101 	no_device=1
    102 	available=1
    103 	proto_config_add_string "username"
    104 	proto_config_add_string "password"
    105 	proto_config_add_string "protocol"
    106 	proto_config_add_string "server"
    107 	proto_config_add_string "ip6addr:ip6addr"
    108 	proto_config_add_string "ip6prefix:ip6addr"
    109 	proto_config_add_string "tunnelid"
    110 	proto_config_add_boolean "requiretls"
    111 	proto_config_add_boolean "defaultroute"
    112 	proto_config_add_boolean "sourcerouting"
    113 	proto_config_add_boolean "nat"
    114 	proto_config_add_boolean "heartbeat"
    115 	proto_config_add_boolean "verbose"
    116 	proto_config_add_int "ntpsynctimeout"
    117 }
    118 
    119 [ -n "$INCLUDE_ONLY" ] || {
    120 	add_protocol aiccu
    121 }