diff --git a/mbtask/mbtask.c b/mbtask/mbtask.c index 1a1a9b89..6ee33ab0 100644 --- a/mbtask/mbtask.c +++ b/mbtask/mbtask.c @@ -85,7 +85,6 @@ extern int s_index; /* Compile nl semafore */ extern int s_newnews; /* New news semafore */ extern int s_reqindex; /* Create req index sem */ extern int s_msglink; /* Messages link sem */ -extern int pingresult[2]; /* Ping results */ int masterinit = FALSE; /* Master init needed */ int ptimer = PAUSETIME; /* Pause timer */ int tflags = FALSE; /* if nodes with Txx */ @@ -886,9 +885,6 @@ void scheduler(void) die(1); } - pingresult[1] = TRUE; - pingresult[2] = TRUE; - /* * The flag masterinit is set if a new config.data is created, this * is true if mbtask is started the very first time. Then we run @@ -1053,18 +1049,18 @@ void scheduler(void) } } + /* + * Check Pause Timer, make sure it's only checked + * once each second. Also do pingcheck. + */ + if (tm->tm_sec != oldsec) { + oldsec = tm->tm_sec; + if (ptimer) + ptimer--; + check_ping(); + } if (Processing) { - /* - * Check Pause Timer, make sure it's only checked - * once each second. - */ - if (tm->tm_sec != oldsec) { - oldsec = tm->tm_sec; - if (ptimer) - ptimer--; - } - /* * Here we run all normal operations. */ @@ -1132,7 +1128,6 @@ void scheduler(void) * These tasks run once per 20 seconds. */ oldmin = tm->tm_sec / SLOWRUN; - check_ping(); if (Processing) { @@ -1227,12 +1222,6 @@ void scheduler(void) } } } /* if (Processing) */ - - /* - * PING state changes - */ - state_ping(); - } /* if ((tm->tm_sec / SLOWRUN) != oldmin) */ } while (TRUE); diff --git a/mbtask/ping.c b/mbtask/ping.c index 48e8b7c9..4fe8fc68 100644 --- a/mbtask/ping.c +++ b/mbtask/ping.c @@ -46,10 +46,7 @@ int ping_isocket; /* Ping socket */ int icmp_errs = 0; /* ICMP error counter */ extern int internet; /* Internet is down */ extern int rescan; /* Master rescan flag */ -int pingstate = P_INIT; /* Ping state */ -int pingnr = 1; /* Ping #, 1 or 2 */ -int pingresult[2]; /* Ping results */ -char pingaddress[41]; /* Ping current address */ +int pingstate = P_BOOT; /* Ping state */ struct in_addr paddr; /* Current ping address */ @@ -306,99 +303,112 @@ int ping_receive(struct in_addr addr) void check_ping(void) { - int rc = 0; + int rc = 0; + static int pingnr, pingresult[2]; + static char pingaddress[41]; + static time_t pingtime; - /* - * If the previous pingstat is still P_SENT, then we now consider it a timeout. - */ - if (pingstate == P_SENT) { - pingresult[pingnr] = FALSE; - icmp_errs++; - if (icmp_errs < ICMP_MAX_ERRS) - tasklog('p', "ping: %s seq=%d timeout", pingaddress, p_sequence); - } - - /* - * Check internet connection with ICMP ping - */ - if (pingnr == 1) { - pingnr = 2; - if (strlen(TCFG.isp_ping2)) { - sprintf(pingaddress, "%s", TCFG.isp_ping2); - } else { - pingstate = P_NONE; - } - } else { - pingnr = 1; - if (strlen(TCFG.isp_ping1)) { - sprintf(pingaddress, "%s", TCFG.isp_ping1); - } else { - pingstate = P_NONE; - } - } - - if (inet_aton(pingaddress, &paddr)) { - rc = ping_send(paddr); - if (rc) { - if (icmp_errs++ < ICMP_MAX_ERRS) - tasklog('?', "ping: to %s rc=%d", pingaddress, rc); - pingstate = P_FAIL; - pingresult[pingnr] = FALSE; - } else { - pingstate = P_SENT; - } - } else { - if (icmp_errs++ < ICMP_MAX_ERRS) - tasklog('?', "Ping address %d is invalid \"%s\"", pingnr, pingaddress); - pingstate = P_NONE; - } - - if (pingresult[1] == FALSE && pingresult[2] == FALSE) { - icmp_errs++; - if (internet) { - tasklog('!', "Internet connection is down"); - internet = FALSE; - sem_set((char *)"scanout", TRUE); - RemoveSema((char *)"is_inet"); - rescan = TRUE; - } - } else { - if (!internet) { - tasklog('!', "Internet connection is up"); - internet = TRUE; - sem_set((char *)"scanout", TRUE); - CreateSema((char *)"is_inet"); - rescan = TRUE; - } - icmp_errs = 0; - } -} - - - -void state_ping(void) -{ - int rc; - - /* - * PING state changes - */ switch (pingstate) { - case P_NONE: pingresult[pingnr] = TRUE; + case P_BOOT: pingnr = 2; + pingstate = P_SENT; + pingresult[1] = pingresult[2] = internet = FALSE; break; - case P_SENT: /* - * Quickly eat all packets not for us, we only want our - * packets and empty results (packet still underway). - */ - while ((rc = ping_receive(paddr)) == -1); - if (!rc) { - pingstate = P_OK; - pingresult[pingnr] = TRUE; + + case P_PAUSE: // tasklog('p', "PAUSE:"); + if (time(NULL) >= pingtime) + pingstate = P_SENT; + break; + + case P_WAIT: // tasklog('p', "WAIT:"); + if (time(NULL) >= pingtime) { + pingstate = P_ERROR; + tasklog('p', "timeout"); } else { - if (rc != -6) - tasklog('p', "ping: recv %s id=%d rc=%d", pingaddress, id, rc); + /* + * Quickly eat all packets not for us, we only want our + * packets and empty results (packet still underway). + */ + while ((rc = ping_receive(paddr)) == -1); + if (!rc) { + /* + * Reply received. + */ + pingresult[pingnr] = TRUE; + if (pingresult[1] || pingresult[2]) { + if (!internet) { + tasklog('!', "Internet connection is up"); + internet = TRUE; + sem_set((char *)"scanout", TRUE); + CreateSema((char *)"is_inet"); + rescan = TRUE; + } + icmp_errs = 0; + } + pingtime = time(NULL) + 5; // 5 secs pause until next ping + pingstate = P_PAUSE; + } else { + if (rc != -6) { + tasklog('p', "ping: recv %s id=%d rc=%d", pingaddress, id, rc); + pingstate = P_ERROR; + } + } } break; + + case P_SENT: tasklog('p', "SENT:"); + pingtime = time(NULL) + 10; // 10 secs timeout for pause. + if (pingnr == 1) { + pingnr = 2; + if (strlen(TCFG.isp_ping2)) { + sprintf(pingaddress, "%s", TCFG.isp_ping2); + } else { + pingresult[2] = FALSE; + pingstate = P_PAUSE; + break; + } + } else { + pingnr = 1; + if (strlen(TCFG.isp_ping1)) { + sprintf(pingaddress, "%s", TCFG.isp_ping1); + } else { + pingresult[1] = FALSE; + pingstate = P_PAUSE; + break; + } + } + pingtime = time(NULL) + 20; // 20 secs timeout for a real ping + tasklog('p', "nr %d address %s", pingnr, pingaddress); + if (inet_aton(pingaddress, &paddr)) { + rc = ping_send(paddr); + if (rc) { + if (icmp_errs++ < ICMP_MAX_ERRS) + tasklog('?', "ping: to %s rc=%d", pingaddress, rc); + pingstate = P_ERROR; + pingresult[pingnr] = FALSE; + } else { + pingstate = P_WAIT; + } + } else { + if (icmp_errs++ < ICMP_MAX_ERRS) + tasklog('?', "Ping address %d is invalid \"%s\"", pingnr, pingaddress); + pingstate = P_PAUSE; + } + break; + + case P_ERROR: tasklog('p', "ERROR:"); + pingresult[pingnr] = FALSE; + if (pingresult[1] == FALSE && pingresult[2] == FALSE) { + icmp_errs++; + if (internet) { + tasklog('!', "Internet connection is down"); + internet = FALSE; + sem_set((char *)"scanout", TRUE); + RemoveSema((char *)"is_inet"); + rescan = TRUE; + } + } + pingstate = P_SENT; + break; } } diff --git a/mbtask/ping.h b/mbtask/ping.h index d26767fe..9fd5127e 100644 --- a/mbtask/ping.h +++ b/mbtask/ping.h @@ -4,7 +4,7 @@ #define _PING_H -typedef enum {P_INIT, P_SENT, P_FAIL, P_OK, P_ERROR, P_NONE} PINGSTATE; +typedef enum {P_BOOT, P_PAUSE, P_SENT, P_WAIT, P_ERROR} PINGSTATE; @@ -16,7 +16,6 @@ typedef enum {P_INIT, P_SENT, P_FAIL, P_OK, P_ERROR, P_NONE} PINGSTATE; #define SET_SOCKA_LEN4(socka) void check_ping(void); -void state_ping(void); void init_pingsocket(void); #endif