Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
Diff: eth/nr4.c
- Revision:
- 93:580fc113d9e9
- Parent:
- 66:18a10c0b6d93
--- a/eth/nr4.c Thu Nov 29 16:52:10 2018 +0000 +++ b/eth/nr4.c Sun Dec 02 18:40:35 2018 +0000 @@ -3,7 +3,7 @@ #include <string.h> #include "log.h" -#include "clock.h" +#include "mstimer.h" #include "net.h" #include "mac.h" #include "ip4addr.h" @@ -14,10 +14,10 @@ bool Nr4Trace = false; -#define NAME_MAX_LENGTH 20 -#define CACHE_TIMEOUT 3600 -#define FREEZE_TIMEOUT 1800 -#define REPLY_TIMEOUT 2 +#define NAME_MAX_LENGTH 20 +#define CACHE_TIMEOUT_MS 3600 * 1000 +#define FREEZE_TIMEOUT_MS 1800 * 1000 +#define REPLY_TIMEOUT_MS 2 * 1000 #define RECORDS_COUNT 20 @@ -30,7 +30,6 @@ #define TODO_NAME_FROM_IP 1 #define TODO_IP_FROM_NAME 2 -static uint32_t elapsed = 0; struct record { uint32_t elapsed; @@ -72,18 +71,19 @@ } static int getOldest() { - int iN = 0; - uint32_t tN = 0xFFFFFFFF; + int iOldest = 0; + uint32_t ageOldest = 0; for (int i = 0; i < RECORDS_COUNT; i++) { if (records[i].state == STATE_EMPTY) return i; //Found an empty slot so just return it - if (records[i].elapsed < tN) + uint32_t age = MsTimerCount - records[i].elapsed; + if (age >= ageOldest) { - tN = records[i].elapsed; - iN = i; + ageOldest = age; + iOldest = i; } } - return iN; //Otherwise return the oldest + return iOldest; //Otherwise return the oldest } void Nr4MakeRequestForNameFromIp(uint32_t ip) { @@ -95,7 +95,7 @@ i = getExistingIp(ip); if (i > -1) { - if (elapsed < records[i].elapsed + FREEZE_TIMEOUT) return; + if (!MsTimerHasElapsed(records[i].elapsed, FREEZE_TIMEOUT_MS)) return; if (Nr4Trace) { LogTimeF("NR - renew name of "); @@ -105,7 +105,7 @@ records[i].todo = TODO_NAME_FROM_IP; records[i].state = STATE_WANT; records[i].protocol = DnsGetNextProtocol4(DNS_PROTOCOL_NONE); - records[i].elapsed = elapsed; + records[i].elapsed = MsTimerCount; return; } @@ -121,7 +121,7 @@ records[i].todo = TODO_NAME_FROM_IP; records[i].state = STATE_WANT; records[i].protocol = DnsGetNextProtocol4(DNS_PROTOCOL_NONE); - records[i].elapsed = elapsed; + records[i].elapsed = MsTimerCount; records[i].name[0] = 0; } void Nr4MakeRequestForIpFromName(char* name) @@ -134,7 +134,7 @@ i = getExistingName(name); if (i > -1) { - if (elapsed < records[i].elapsed + FREEZE_TIMEOUT) return; + if (!MsTimerHasElapsed(records[i].elapsed, FREEZE_TIMEOUT_MS)) return; if (Nr4Trace) { LogTimeF("NR - renew IPv4 of %s\r\n", name); @@ -142,7 +142,7 @@ records[i].todo = TODO_IP_FROM_NAME; records[i].state = STATE_WANT; records[i].protocol = DnsGetNextProtocol4(DNS_PROTOCOL_NONE); - records[i].elapsed = elapsed; + records[i].elapsed = MsTimerCount; return; } @@ -156,14 +156,14 @@ records[i].todo = TODO_IP_FROM_NAME; records[i].state = STATE_WANT; records[i].protocol = DnsGetNextProtocol4(DNS_PROTOCOL_NONE); - records[i].elapsed = elapsed; + records[i].elapsed = MsTimerCount; strncpy(records[i].name, name, NAME_MAX_LENGTH); records[i].name[NAME_MAX_LENGTH - 1] = 0; } static void addIpRecord(int i, uint32_t ip, char* name, int protocol) { records[i].todo = TODO_NONE; - records[i].elapsed = elapsed; + records[i].elapsed = MsTimerCount; records[i].ip = ip; records[i].protocol = protocol; records[i].state = STATE_VALID; @@ -241,16 +241,17 @@ } void Nr4NameToIp(char* name, uint32_t* pIp) { - int newest = 0; + uint32_t newest = 0xFFFFFFFF; *pIp = 0; for (int i = 0; i < RECORDS_COUNT; i++) { if (records[i].state == STATE_EMPTY) continue; if (!records[i].ip) continue; if (!DnsHostNamesEquate(records[i].name, name)) continue; - if (records[i].elapsed > newest) + uint32_t age = MsTimerCount - records[i].elapsed; + if (age <= newest) { - newest = records[i].elapsed; + newest = age; *pIp = records[i].ip; } } @@ -281,7 +282,7 @@ if (records[i].state == STATE_EMPTY) continue; if (records[i].ip || records[i].name[0]) { - HttpAddF("%4u ", (elapsed - records[i].elapsed) / 60); + HttpAddF("%4u ", (MsTimerCount - records[i].elapsed) / 1000 / 60); int ipLen = Ip4AddressHttp(records[i].ip); HttpFillChar(' ', 40 - ipLen); @@ -299,11 +300,11 @@ } static void clearCache(struct record* pr) { - if (elapsed > pr->elapsed + CACHE_TIMEOUT) pr->state = STATE_EMPTY; + if (MsTimerHasElapsed(pr->elapsed, CACHE_TIMEOUT_MS)) pr->state = STATE_EMPTY; } static void nextProtocol(struct record* pr) { - if (pr->state == STATE_SENT && elapsed > pr->elapsed + REPLY_TIMEOUT && pr->protocol) + if (pr->state == STATE_SENT && MsTimerHasElapsed(pr->elapsed, REPLY_TIMEOUT_MS) && pr->protocol) { pr->protocol = DnsGetNextProtocol4(pr->protocol); if (pr->protocol) @@ -316,7 +317,7 @@ if (pr->todo == TODO_IP_FROM_NAME) pr->ip = 0; pr->state = STATE_VALID; } - pr->elapsed = elapsed; + pr->elapsed = MsTimerCount; } } static void queryNameFromIp(struct record* pr) @@ -353,7 +354,7 @@ if (pr->todo == TODO_IP_FROM_NAME) queryIpFromName(pr); pr->state = STATE_SENT; - pr->elapsed = elapsed; + pr->elapsed = MsTimerCount; } void Nr4Main() { @@ -366,8 +367,6 @@ clearCache (pr); nextProtocol(pr); sendRequest (pr); - - if (ClockTicked) elapsed++; } void Nr4Init() {