X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=pppd%2Fplugins%2Frp-pppoe%2Fdiscovery.c;fp=pppd%2Fplugins%2Frp-pppoe%2Fdiscovery.c;h=04877cb8295f767d90c989142da799a14c3fc4ec;hb=1817d83e51a411044e730ba89ebdb0480e1c8cd4;hp=a856490f96fb7ba887ae2e8dda16a91eae358769;hpb=c44ae5e6a7338c96eb463881fe709b2dfaffe568;p=ppp.git diff --git a/pppd/plugins/rp-pppoe/discovery.c b/pppd/plugins/rp-pppoe/discovery.c index a856490..04877cb 100644 --- a/pppd/plugins/rp-pppoe/discovery.c +++ b/pppd/plugins/rp-pppoe/discovery.c @@ -40,6 +40,30 @@ static char const RCSID[] = #include +/* Calculate time remaining until *exp, return 0 if now >= *exp */ +static int time_left(struct timeval *diff, struct timeval *exp) +{ + struct timeval now; + + if (gettimeofday(&now, NULL) < 0) { + error("gettimeofday: %m"); + return 0; + } + + if (now.tv_sec > exp->tv_sec + || (now.tv_sec == exp->tv_sec && now.tv_usec >= exp->tv_usec)) + return 0; + + diff->tv_sec = exp->tv_sec - now.tv_sec; + diff->tv_usec = exp->tv_usec - now.tv_usec; + if (diff->tv_usec < 0) { + diff->tv_usec += 1000000; + --diff->tv_sec; + } + + return 1; +} + /********************************************************************** *%FUNCTION: parseForHostUniq *%ARGUMENTS: @@ -323,6 +347,8 @@ waitForPADO(PPPoEConnection *conn, int timeout) fd_set readable; int r; struct timeval tv; + struct timeval expire_at; + PPPoEPacket packet; int len; @@ -335,10 +361,16 @@ waitForPADO(PPPoEConnection *conn, int timeout) conn->seenMaxPayload = 0; conn->error = 0; + if (gettimeofday(&expire_at, NULL) < 0) { + error("gettimeofday (waitForPADO): %m"); + return; + } + expire_at.tv_sec += timeout; + do { if (BPF_BUFFER_IS_EMPTY) { - tv.tv_sec = timeout; - tv.tv_usec = 0; + if (!time_left(&tv, &expire_at)) + return; /* Timed out */ FD_ZERO(&readable); FD_SET(conn->discoverySocket, &readable); @@ -351,7 +383,8 @@ waitForPADO(PPPoEConnection *conn, int timeout) error("select (waitForPADO): %m"); return; } - if (r == 0) return; /* Timed out */ + if (r == 0) + return; /* Timed out */ } /* Get the packet */ @@ -507,14 +540,22 @@ waitForPADS(PPPoEConnection *conn, int timeout) fd_set readable; int r; struct timeval tv; + struct timeval expire_at; + PPPoEPacket packet; int len; + if (gettimeofday(&expire_at, NULL) < 0) { + error("gettimeofday (waitForPADS): %m"); + return; + } + expire_at.tv_sec += timeout; + conn->error = 0; do { if (BPF_BUFFER_IS_EMPTY) { - tv.tv_sec = timeout; - tv.tv_usec = 0; + if (!time_left(&tv, &expire_at)) + return; /* Timed out */ FD_ZERO(&readable); FD_SET(conn->discoverySocket, &readable); @@ -527,7 +568,8 @@ waitForPADS(PPPoEConnection *conn, int timeout) error("select (waitForPADS): %m"); return; } - if (r == 0) return; + if (r == 0) + return; /* Timed out */ } /* Get the packet */ @@ -590,9 +632,6 @@ discovery(PPPoEConnection *conn) int padrAttempts = 0; int timeout = conn->discoveryTimeout; - conn->discoverySocket = - openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth); - do { padiAttempts++; if (padiAttempts > MAX_PADI_ATTEMPTS) {