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_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)
}
}
if (Processing) {
/*
* Check Pause Timer, make sure it's only checked
* once each second.
* once each second. Also do pingcheck.
*/
if (tm->tm_sec != oldsec) {
oldsec = tm->tm_sec;
if (ptimer)
ptimer--;
check_ping();
}
if (Processing) {
/*
* 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);

View File

@ -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 */
@ -307,62 +304,37 @@ int ping_receive(struct in_addr addr)
void check_ping(void)
{
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 {
switch (pingstate) {
case P_BOOT: pingnr = 2;
pingstate = P_SENT;
}
} else {
if (icmp_errs++ < ICMP_MAX_ERRS)
tasklog('?', "Ping address %d is invalid \"%s\"", pingnr, pingaddress);
pingstate = P_NONE;
}
pingresult[1] = pingresult[2] = internet = FALSE;
break;
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;
}
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 {
/*
* 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;
@ -372,32 +344,70 @@ void check_ping(void)
}
icmp_errs = 0;
}
}
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;
pingtime = time(NULL) + 5; // 5 secs pause until next ping
pingstate = P_PAUSE;
} else {
if (rc != -6)
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;
}
}

View File

@ -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