Changed the ping state machine

This commit is contained in:
Michiel Broek 2002-06-06 19:46:44 +00:00
parent 9bf685786e
commit edfc55fb15
3 changed files with 112 additions and 114 deletions

View File

@ -85,7 +85,6 @@ extern int s_index; /* Compile nl semafore */
extern int s_newnews; /* New news semafore */ extern int s_newnews; /* New news semafore */
extern int s_reqindex; /* Create req index sem */ extern int s_reqindex; /* Create req index sem */
extern int s_msglink; /* Messages link sem */ extern int s_msglink; /* Messages link sem */
extern int pingresult[2]; /* Ping results */
int masterinit = FALSE; /* Master init needed */ int masterinit = FALSE; /* Master init needed */
int ptimer = PAUSETIME; /* Pause timer */ int ptimer = PAUSETIME; /* Pause timer */
int tflags = FALSE; /* if nodes with Txx */ int tflags = FALSE; /* if nodes with Txx */
@ -886,9 +885,6 @@ void scheduler(void)
die(1); die(1);
} }
pingresult[1] = TRUE;
pingresult[2] = TRUE;
/* /*
* The flag masterinit is set if a new config.data is created, this * 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 * is true if mbtask is started the very first time. Then we run
@ -1053,18 +1049,18 @@ void scheduler(void)
} }
} }
if (Processing) {
/* /*
* Check Pause Timer, make sure it's only checked * Check Pause Timer, make sure it's only checked
* once each second. * once each second. Also do pingcheck.
*/ */
if (tm->tm_sec != oldsec) { if (tm->tm_sec != oldsec) {
oldsec = tm->tm_sec; oldsec = tm->tm_sec;
if (ptimer) if (ptimer)
ptimer--; ptimer--;
check_ping();
} }
if (Processing) {
/* /*
* Here we run all normal operations. * Here we run all normal operations.
*/ */
@ -1132,7 +1128,6 @@ void scheduler(void)
* These tasks run once per 20 seconds. * These tasks run once per 20 seconds.
*/ */
oldmin = tm->tm_sec / SLOWRUN; oldmin = tm->tm_sec / SLOWRUN;
check_ping();
if (Processing) { if (Processing) {
@ -1227,12 +1222,6 @@ void scheduler(void)
} }
} }
} /* if (Processing) */ } /* if (Processing) */
/*
* PING state changes
*/
state_ping();
} /* if ((tm->tm_sec / SLOWRUN) != oldmin) */ } /* if ((tm->tm_sec / SLOWRUN) != oldmin) */
} while (TRUE); } while (TRUE);

View File

@ -46,10 +46,7 @@ int ping_isocket; /* Ping socket */
int icmp_errs = 0; /* ICMP error counter */ int icmp_errs = 0; /* ICMP error counter */
extern int internet; /* Internet is down */ extern int internet; /* Internet is down */
extern int rescan; /* Master rescan flag */ extern int rescan; /* Master rescan flag */
int pingstate = P_INIT; /* Ping state */ int pingstate = P_BOOT; /* Ping state */
int pingnr = 1; /* Ping #, 1 or 2 */
int pingresult[2]; /* Ping results */
char pingaddress[41]; /* Ping current address */
struct in_addr paddr; /* Current ping address */ struct in_addr paddr; /* Current ping address */
@ -307,62 +304,37 @@ int ping_receive(struct in_addr addr)
void check_ping(void) void check_ping(void)
{ {
int rc = 0; int rc = 0;
static int pingnr, pingresult[2];
static char pingaddress[41];
static time_t pingtime;
/* switch (pingstate) {
* If the previous pingstat is still P_SENT, then we now consider it a timeout. case P_BOOT: pingnr = 2;
*/
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; pingstate = P_SENT;
} pingresult[1] = pingresult[2] = internet = FALSE;
} else { break;
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) { case P_PAUSE: // tasklog('p', "PAUSE:");
icmp_errs++; if (time(NULL) >= pingtime)
if (internet) { pingstate = P_SENT;
tasklog('!', "Internet connection is down"); break;
internet = FALSE;
sem_set((char *)"scanout", TRUE); case P_WAIT: // tasklog('p', "WAIT:");
RemoveSema((char *)"is_inet"); if (time(NULL) >= pingtime) {
rescan = TRUE; pingstate = P_ERROR;
} tasklog('p', "timeout");
} else { } else {
/*
* 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) { if (!internet) {
tasklog('!', "Internet connection is up"); tasklog('!', "Internet connection is up");
internet = TRUE; internet = TRUE;
@ -372,32 +344,70 @@ void check_ping(void)
} }
icmp_errs = 0; icmp_errs = 0;
} }
} pingtime = time(NULL) + 5; // 5 secs pause until next ping
pingstate = P_PAUSE;
void state_ping(void)
{
int rc;
/*
* PING state changes
*/
switch (pingstate) {
case P_NONE: pingresult[pingnr] = TRUE;
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;
} else { } else {
if (rc != -6) if (rc != -6) {
tasklog('p', "ping: recv %s id=%d rc=%d", pingaddress, id, rc); 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; break;
} }
} }

View File

@ -4,7 +4,7 @@
#define _PING_H #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) #define SET_SOCKA_LEN4(socka)
void check_ping(void); void check_ping(void);
void state_ping(void);
void init_pingsocket(void); void init_pingsocket(void);
#endif #endif