#!/bin/sh /etc/rc.common # Copyright (C) 2012 Inteno START=98 STOP=40 wait_connection_acs() { i=0 result=1 until [ \( $result -eq 0 \) -o \( $i -eq 20 \) ];do echo -e -n "" | nc $1 $2 result=$? if [ $result -eq 1 ];then i=`expr $i + 1` sleep 5 fi done } check_connection_request_url() { cpe_port=`uci get cwmp.cpe.port` if [ "_$cpe_port" = "_" ];then cpe_port=7547 fi default_acs="http://192.168.1.1:8080/openacs/acs" acs_dhcp_discovery=`uci -q get cwmp.acs.dhcp_discovery` url=`uci -q get cwmp.acs.url` if [ \( "_$acs_dhcp_discovery" = "_enable" \) -o \( "_$url" = "_" \) ];then url=`uci -P /var/state -q get provisioning.iup.tr069url` elif [ "_$url" != "_" ];then url=`uci -q get cwmp.acs.url` else url=$default_acs fi dest=`echo $url|sed 's/http:\/\///g'|cut -f1 -d \/|cut -f1 -d:` port=`echo $url|sed 's/http:\/\///g'|cut -f1 -d \/|cut -f2 -d:` if [ "_$port" = "_" ];then port=80 fi isIP=`echo "$dest"|grep -v -e [a-zA-Z]` if [ "_$isIP" != "_" ];then list=`netstat -t -n|grep $dest:$port|sed 's/\ \+/-/g'|sed 's/-$//g'` wait_connection_acs "$dest" "$port" list2=`netstat -t -n|grep $dest:$port|sed 's/\ \+/-/g'|sed 's/-$//g'` else i=0 result=1 until [ \( $result -eq 0 \) -o \( $i -eq 20 \) ];do nslookup $dest > /dev/null result=$? if [ $result -eq 1 ];then i=`expr $i + 1` sleep 5 fi done if [ $result -eq 1 ];then exit 1 fi number_entries=`nslookup $dest|wc -l` n=`expr $number_entries - 4` if [ $n -lt 0 ];then exit 1 fi for addr in `nslookup $dest|tail -$n|grep Address|cut -f2 -d:|sed 's/^ //g'|cut -f1 -d " "`;do list=$list`netstat -t -n|grep $addr:$port|sed 's/\ \+/-/g'|sed 's/-$//g'` done for addr in `nslookup $dest|tail -$n|grep Address|cut -f2 -d:|sed 's/^ //g'|cut -f1 -d " "`;do wait_connection_acs "$addr" "$port" list2=$list2`netstat -t -n|grep $addr:$port|sed 's/\ \+/-/g'|sed 's/-$//g'` done fi for line2 in $list2;do found=0 for line in $list;do if [ "_$line" = "_$line2" ];then found=1 break fi done if [ $found -eq 1 ];then continue fi ip=`echo $line2|cut -f4 -d -|cut -f1 -d:|uniq` break done if [ "_$ip" = "_" ];then exit 1 fi old_cr_url=`uci get -q -P /etc/cwmpd/.iccu cwmp.cpe.iccu_url` if [ $? -eq 0 ];then cr_url="http://$ip:$cpe_port" if [ "_$cr_url" != "_$old_cr_url" ];then rm -rf /etc/cwmpd/.iccu uci set -q -P /etc/cwmpd/.iccu cwmp.cpe.iccu_url=$cr_url ubus call tr069 notify '{"parameter": "InternetGatewayDevice.ManagementServer.ConnectionRequestURL", "value": "'$cr_url'", "type": "xsd:string"}' if [ $? -eq 1 ];then uci set -q -P /etc/cwmpd/.iccu cwmp.cpe.iccu_change_state=yes else uci set -q -P /etc/cwmpd/.iccu cwmp.cpe.iccu_change_state=no fi uci commit fi else rm -rf /etc/cwmpd/.iccu uci set -q -P /etc/cwmpd/.iccu cwmp.cpe.iccu_url=http://$ip:$cpe_port uci set -q -P /etc/cwmpd/.iccu cwmp.cpe.iccu_change_state=no uci commit fi } boot() { check_connection_request_url } start() { check_connection_request_url } restart() { start }