Some code improvements:

- others/watchdog: cosmetics
 - wanuptime/wanuptime.c: changes needed to work with watchdog (thanks @tsynik)
 - shared/misc.c: added more debug logging + cosmetics (thanks @tsynik)
 - rc/dhcp.c: added more debug logging + cosmetics (thanks @tsynik)
 - rc/ppp.c: added more debug logging + cosmetics (thanks @tsynik)
arm-ng
pedro 9 years ago committed by kille72
parent 8d1b899937
commit ce661da872

@ -50,20 +50,20 @@ watchdogRun() {
METRIC=`expr $PREFNR + 100`
fi
$DEBUG $PREFIX metric $METRIC iface $IFACE uptime $ISUP
$DEBUG ""$PREFIX" metric "$METRIC" iface "$IFACE" uptime "$ISUP""
ADD=0
ISGW=`ip route | grep $IFACE | grep -v "link" | wc -l`
$DEBUG "ISGW=$ISGW, WEIGHT=$WEIGHT"
$DEBUG "ISGW="$ISGW", WEIGHT="$WEIGHT""
if [ "$ISGW" == "0" -a "$WEIGHT" -gt 0 -a "$ISUP" -gt 0 ]; then
if [ "$ISGW" -eq 0 -a "$WEIGHT" -gt 0 -a "$ISUP" -gt 0 ]; then
#failback required default gateway to check is indeed connection back to live
#so we have to add temporary gateway
GW=`ip route | grep $IFACE | grep -v "kernel" | awk {' printf $1'}`
if [ ! $GW ]; then # no GW found
$DEBUG "no gw found for $IFACE"
$DEBUG "no gw found for "$IFACE""
else
$DEBUG "add test gw $GW for $IFACE"
$DEBUG "add test gw "$GW" for "$IFACE""
if [ -n "$GW" -a ! "$GW" == "0.0.0.0" ]; then
route add default gw $GW metric $METRIC
ADD=1
@ -73,33 +73,33 @@ watchdogRun() {
fi
if [ "$WEIGHT" -gt 0 -a "$ISUP" -gt 0 ]; then
if [ "$MTD" == "1" ]; then
ckping
if [ "$MTD" -eq 1 ]; then
$DEBUG "run ping-test"
elif [ "$MTD" == "2" ]; then
cktracert
ckping
elif [ "$MTD" -eq 2 ]; then
$DEBUG "run tracert-test"
cktracert
else
ckwget
$DEBUG "run wget-test"
ckwget
fi
fi
#remove temporary added gateway
if [ "$ADD" == "1" ]; then
if [ "$ADD" -eq 1 ]; then
route del default gw $GW
$DEBUG "del test gw $GW"
$DEBUG "del test gw "$GW""
fi
if [ "$RESULT" == "0" -a "$WEIGHT" == "0" ]; then
if [ "$RESULT" -eq 0 -a "$WEIGHT" -eq 0 ]; then
#failover does not have default gateway in route table
#so we can`t check connection to outside. Check only if interface exist
ckfailover
$DEBUG "run failover-test"
fi
if [ "$RESULT" == "0" ]; then #wan is down
$DEBUG "result=0, restart $PREFIX"
if [ "$RESULT" -eq 0 ]; then #wan is down
$DEBUG "result=0, restart "$PREFIX""
logger -t watchdog[$PID] "Connection $PREFIX DOWN - Reconnecting ..."
echo "0" > /tmp/state_$PREFIX
@ -119,7 +119,7 @@ watchdogRun() {
else
echo "1" > /tmp/state_$PREFIX
$DEBUG "result=1, $PREFIX"
$DEBUG "result=1, "$PREFIX""
fi
fi
done
@ -136,25 +136,25 @@ cktracert() {
if [ "$RXBYTES2" -gt "$RXBYTES1" ]; then
RESULT=1
fi
$DEBUG tracert for $IFACE: RX2=$RXBYTES2 RX1=$RXBYTES1
$DEBUG "tracert for "$IFACE": RX2="$RXBYTES2" RX1="$RXBYTES1""
}
ckping() {
OK=0
for HOST in $HOSTLIST; do
$DEBUG "$IFACE - $HOST"
$DEBUG ""$IFACE" - "$HOST""
TEST=`ping -I $IFACE -c 4 $HOST | grep "received" | cut -d "," -f2 | cut -d " " -f2`
$DEBUG "$IFACE = $TEST"
$DEBUG ""$IFACE" = "$TEST""
if [ "$TEST" -gt 0 ]; then # "0" means 100% loss - not receive any package
OK=`expr $OK + 1`
$DEBUG "ping ok=$OK"
$DEBUG "ping ok="$OK""
fi
done
if [ "$OK" -gt 0 ]; then
RESULT=1
$DEBUG "ping for $IFACE: OK=1"
$DEBUG "ping for "$IFACE": OK=1"
fi
}
@ -179,7 +179,7 @@ ckwget() {
RESULT=1
fi
done
$DEBUG "wget OK=$RESULT"
$DEBUG "wget OK="$RESULT""
}
ckfailover() {
@ -198,7 +198,7 @@ ckfailover() {
fi
else
RESULT=1
$DEBUG "failover=1, tle $TYPE"
$DEBUG "failover=1, lte $TYPE"
fi
else
RESULT=1

@ -203,6 +203,14 @@ static int bound(char *ifname, int renew, char *prefix)
TRACE_PT("wan_6rd=%s\n", nvram_safe_get("wan_6rd"));
#endif
mwanlog(LOG_DEBUG, "*** bound, %s_ipaddr=%s", prefix, nvram_safe_get(strcat_r(prefix, "_ipaddr", tmp)));
mwanlog(LOG_DEBUG, "*** bound, %s_netmask=%s", prefix, netmask);
mwanlog(LOG_DEBUG, "*** bound, %s_gateway=%s", prefix, nvram_safe_get(strcat_r(prefix, "_gateway", tmp)));
mwanlog(LOG_DEBUG, "*** bound, %s_get_dns=%s", prefix, nvram_safe_get(strcat_r(prefix, "_get_dns", tmp)));
mwanlog(LOG_DEBUG, "*** bound, %s_routes1=%s", prefix, nvram_safe_get(strcat_r(prefix, "_routes1", tmp)));
mwanlog(LOG_DEBUG, "*** bound, %s_routes2=%s", prefix, nvram_safe_get(strcat_r(prefix, "_routes2", tmp)));
mwanlog(LOG_DEBUG, "*** bound, do ifconfig ...");
ifconfig(ifname, IFUP, "0.0.0.0", NULL);
ifconfig(ifname, IFUP, nvram_safe_get(strcat_r(prefix, "_ipaddr", tmp)), netmask);
@ -211,8 +219,9 @@ static int bound(char *ifname, int renew, char *prefix)
preset_wan(ifname, gw, netmask, prefix);
/* clear dns from the resolv.conf */
/* clear / set dns in the resolv.conf */
nvram_set(strcat_r(prefix, "_get_dns", tmp), renew ? dns : "");
mwanlog(LOG_DEBUG, "*** bound, clear dns from resolv.conf (if not renew)");
switch (wan_proto) {
case WP_PPTP:
@ -224,6 +233,7 @@ static int bound(char *ifname, int renew, char *prefix)
}
}
else {
mwanlog(LOG_DEBUG,"OUT bound: to start_wan_done, ifname=%s prefix=%s", ifname, prefix);
start_wan_done(ifname,prefix);
}

@ -92,6 +92,7 @@ int ipup_main(int argc, char **argv)
if ((p = getenv("IPREMOTE"))) {
nvram_set(strcat_r(prefix, "_gateway_get", tmp), p);
mwanlog(LOG_DEBUG,"### ipup_main: set %s_gateway_get=%s", prefix, p);
TRACE_PT("IPREMOTE=%s\n", p);
}
@ -172,10 +173,17 @@ int ipdown_main(int argc, char **argv)
nvram_set(strcat_r(prefix, "_get_dns", tmp),"");
dns_to_resolv();
if (proto == WP_L2TP) {
route_del(nvram_safe_get(strcat_r(prefix, "_ifname", tmp)), 0, nvram_safe_get(strcat_r(prefix, "_l2tp_server_ip", tmp)),
nvram_safe_get(strcat_r(prefix, "_gateway", tmp)), "255.255.255.255"); // fixed routing problem in Israel by kanki
}
if (proto == WP_L2TP) {
route_del(nvram_safe_get(strcat_r(prefix, "_ifname", tmp)), 0, nvram_safe_get(strcat_r(prefix, "_l2tp_server_ip", tmp)),
nvram_safe_get(strcat_r(prefix, "_gateway", tmp)), "255.255.255.255"); // fixed routing problem in Israel by kanki
mwanlog(LOG_DEBUG,"### ipdown_main: route_del(%s,0,%s,%s,255.255.255.255)", nvram_safe_get(strcat_r(prefix, "_ifname", tmp)), nvram_safe_get(strcat_r(prefix, "_l2tp_server_ip", tmp)), nvram_safe_get(strcat_r(prefix, "_gateway", tmp)));
}
if (proto == WP_PPTP) {
route_del(nvram_safe_get(strcat_r(prefix, "_ifname", tmp)), 0, nvram_safe_get(strcat_r(prefix, "_pptp_server_ip", tmp)),
nvram_safe_get(strcat_r(prefix, "_gateway", tmp)), "255.255.255.255");
mwanlog(LOG_DEBUG,"### ipdown_main: route_del(%s,0,%s,%s,255.255.255.255)", nvram_safe_get(strcat_r(prefix, "_ifname", tmp)), nvram_safe_get(strcat_r(prefix, "_pptp_server_ip", tmp)), nvram_safe_get(strcat_r(prefix, "_gateway", tmp)));
}
// Restore the default gateway for WAN interface
nvram_set(strcat_r(prefix, "_gateway_get", tmp), nvram_safe_get(strcat_r(prefix, "_gateway", tmp)));
@ -183,6 +191,7 @@ int ipdown_main(int argc, char **argv)
// Set default route to gateway if specified
route_del(nvram_safe_get(strcat_r(prefix, "_ifname", tmp)), 0, "0.0.0.0", nvram_safe_get(strcat_r(prefix, "_gateway", tmp)), "0.0.0.0");
route_add(nvram_safe_get(strcat_r(prefix, "_ifname", tmp)), 0, "0.0.0.0", nvram_safe_get(strcat_r(prefix, "_gateway", tmp)), "0.0.0.0");
mwanlog(LOG_DEBUG,"### ipdown_main: route_add(%s,0,0.0.0.0,%s,0.0.0.0)", nvram_safe_get(strcat_r(prefix, "_ifname", tmp)), nvram_safe_get(strcat_r(prefix, "_gateway", tmp)));
}
if (nvram_get_int(strcat_r(prefix, "_ppp_demand", tmp))) {

@ -261,9 +261,9 @@ int wan_led(int *mode) // mode: 0 - OFF, 1 - ON
int model;
if (mode) {
mwanlog(LOG_DEBUG, "wan_led: led(LED_WHITE,ON)");
mwanlog(LOG_DEBUG, "### wan_led: led(LED_WHITE,ON)");
} else {
mwanlog(LOG_DEBUG, "wan_led: led(LED_WHITE,OFF)");
mwanlog(LOG_DEBUG, "### wan_led: led(LED_WHITE,OFF)");
}
model = get_model();
@ -358,11 +358,11 @@ int wan_led_off(char *prefix) // off WAN LED only if no other WAN active
}
if (count > 0) {
mwanlog(LOG_DEBUG, "OUT wan_led_off: %s, active WANs count:%d, stay on", prefix, count);
mwanlog(LOG_DEBUG, "### OUT wan_led_off: %s, active WANs count:%d, stay on", prefix, count);
return count; // do not LED OFF
}
else {
mwanlog(LOG_DEBUG, "OUT wan_led_off: %s, no other active WANs, turn off led", prefix);
mwanlog(LOG_DEBUG, "### OUT wan_led_off: %s, no other active WANs, turn off led", prefix);
return wan_led(LED_OFF); // LED OFF
}
}
@ -416,11 +416,11 @@ int check_wanup(char *prefix)
name = psname(atoi(buf1), buf2, sizeof(buf2));
memset(pppd_name, 0, 256);
sprintf(pppd_name, "pppd%s", prefix);
//mwanlog(LOG_INFO, "### check_wanup: pppd name=%s, psname=%s", pppd_name, name);
mwanlog(LOG_DEBUG, "### check_wanup: pppd name=%s, psname=%s", pppd_name, name);
if (strcmp(name, pppd_name) == 0) up = 1;
if (proto == WP_L2TP) {
sprintf(pppd_name, "pppd");
//mwanlog(LOG_INFO, "### check_wanup: L2TP pppd name=%s, psname=%s", pppd_name, name);
mwanlog(LOG_DEBUG, "### check_wanup: L2TP pppd name=%s, psname=%s", pppd_name, name);
if (strcmp(name, pppd_name) == 0) up = 1;
}
}
@ -437,11 +437,12 @@ int check_wanup(char *prefix)
}
}
else if (!nvram_match(strcat_r(prefix, "_ipaddr", tmp), "0.0.0.0")) {
mwanlog(LOG_DEBUG, "### check_wanup: %s have IP, assume ON", prefix);
up = 1;
}
else {
_x_dprintf("%s: default !up\n", __FUNCTION__);
return up; // don't turn off LED
return up; // don't turn off WAN LED
}
if ((up) && ((f = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)) {

@ -2,13 +2,14 @@
#include <sys/sysinfo.h>
#include <time.h>
int main(int argc, char **argv)
int main(int argc, char *argv[])
{
char prefix[] = "wanXX";
if(argc > 0){
strcpy(prefix, argv[0]); }
else{
strcpy(prefix, "wan"); }
char prefix[] = "wanXX_";
if (argc > 1)
strcpy(prefix, argv[1]);
else
strcpy(prefix, "wan");
struct sysinfo si;
time_t uptime;
@ -17,10 +18,11 @@ int main(int argc, char **argv)
memset(wantime_file, 0, 128);
sprintf(wantime_file, "/var/lib/misc/%s_time", prefix);
sysinfo(&si);
if (check_wanup(prefix) && (f_read(wantime_file, &uptime, sizeof(time_t)) == sizeof(uptime))) {
if (sysinfo(&si) == -1)
return 1;
if (check_wanup(prefix) && f_read(wantime_file, &uptime, sizeof(time_t)) == sizeof(uptime))
printf("%ld\n",si.uptime - uptime);
}
else
printf("0\n");

Loading…
Cancel
Save