A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Wed Dec 05 18:30:37 2018 +0000
Revision:
94:e2973a2c488e
Parent:
93:580fc113d9e9
Fixed bug - incorrect MSS being sent from a polled sync: expected 1440 but had -60. Traced to buffer datalength in EthPollForPacketToSend being set to zero instead of being calculated from the buffer length - headersize.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 61:aad055f1b0d1 1 #include <stdint.h>
andrewboyson 61:aad055f1b0d1 2 #include <stdbool.h>
andrewboyson 61:aad055f1b0d1 3 #include <string.h>
andrewboyson 61:aad055f1b0d1 4
andrewboyson 60:1d8c7a1e7483 5 #include "log.h"
andrewboyson 93:580fc113d9e9 6 #include "mstimer.h"
andrewboyson 60:1d8c7a1e7483 7 #include "net.h"
andrewboyson 60:1d8c7a1e7483 8 #include "mac.h"
andrewboyson 60:1d8c7a1e7483 9 #include "ip4addr.h"
andrewboyson 60:1d8c7a1e7483 10 #include "dns.h"
andrewboyson 60:1d8c7a1e7483 11 #include "dhcp.h"
andrewboyson 60:1d8c7a1e7483 12 #include "dnsquery.h"
andrewboyson 60:1d8c7a1e7483 13 #include "http.h"
andrewboyson 60:1d8c7a1e7483 14
andrewboyson 60:1d8c7a1e7483 15 bool Nr4Trace = false;
andrewboyson 60:1d8c7a1e7483 16
andrewboyson 93:580fc113d9e9 17 #define NAME_MAX_LENGTH 20
andrewboyson 93:580fc113d9e9 18 #define CACHE_TIMEOUT_MS 3600 * 1000
andrewboyson 93:580fc113d9e9 19 #define FREEZE_TIMEOUT_MS 1800 * 1000
andrewboyson 93:580fc113d9e9 20 #define REPLY_TIMEOUT_MS 2 * 1000
andrewboyson 60:1d8c7a1e7483 21
andrewboyson 60:1d8c7a1e7483 22 #define RECORDS_COUNT 20
andrewboyson 60:1d8c7a1e7483 23
andrewboyson 60:1d8c7a1e7483 24 #define STATE_EMPTY 0
andrewboyson 60:1d8c7a1e7483 25 #define STATE_WANT 1
andrewboyson 60:1d8c7a1e7483 26 #define STATE_SENT 2
andrewboyson 60:1d8c7a1e7483 27 #define STATE_VALID 3
andrewboyson 60:1d8c7a1e7483 28
andrewboyson 60:1d8c7a1e7483 29 #define TODO_NONE 0
andrewboyson 60:1d8c7a1e7483 30 #define TODO_NAME_FROM_IP 1
andrewboyson 60:1d8c7a1e7483 31 #define TODO_IP_FROM_NAME 2
andrewboyson 60:1d8c7a1e7483 32
andrewboyson 60:1d8c7a1e7483 33 struct record
andrewboyson 60:1d8c7a1e7483 34 {
andrewboyson 60:1d8c7a1e7483 35 uint32_t elapsed;
andrewboyson 60:1d8c7a1e7483 36 uint32_t ip;
andrewboyson 60:1d8c7a1e7483 37 uint8_t todo;
andrewboyson 60:1d8c7a1e7483 38 uint8_t state;
andrewboyson 60:1d8c7a1e7483 39 uint8_t protocol;
andrewboyson 60:1d8c7a1e7483 40 char name[NAME_MAX_LENGTH];
andrewboyson 60:1d8c7a1e7483 41 };
andrewboyson 60:1d8c7a1e7483 42 static struct record records[RECORDS_COUNT];
andrewboyson 60:1d8c7a1e7483 43
andrewboyson 60:1d8c7a1e7483 44 static int getExistingIp(uint32_t ip)
andrewboyson 60:1d8c7a1e7483 45 {
andrewboyson 60:1d8c7a1e7483 46 for (int i = 0; i < RECORDS_COUNT; i++)
andrewboyson 60:1d8c7a1e7483 47 {
andrewboyson 60:1d8c7a1e7483 48 if (records[i].state == STATE_EMPTY) continue;
andrewboyson 60:1d8c7a1e7483 49 if (records[i].ip == ip) return i;
andrewboyson 60:1d8c7a1e7483 50 }
andrewboyson 60:1d8c7a1e7483 51 return -1;
andrewboyson 60:1d8c7a1e7483 52 }
andrewboyson 60:1d8c7a1e7483 53 static int getExistingName(char* name)
andrewboyson 60:1d8c7a1e7483 54 {
andrewboyson 60:1d8c7a1e7483 55 for (int i = 0; i < RECORDS_COUNT; i++)
andrewboyson 60:1d8c7a1e7483 56 {
andrewboyson 60:1d8c7a1e7483 57 if (records[i].state == STATE_EMPTY) continue;
andrewboyson 60:1d8c7a1e7483 58 if (DnsHostNamesEquate(records[i].name, name)) return i;
andrewboyson 60:1d8c7a1e7483 59 }
andrewboyson 60:1d8c7a1e7483 60 return -1;
andrewboyson 60:1d8c7a1e7483 61 }
andrewboyson 60:1d8c7a1e7483 62 static int getNameOnly(char* name)
andrewboyson 60:1d8c7a1e7483 63 {
andrewboyson 60:1d8c7a1e7483 64 for (int i = 0; i < RECORDS_COUNT; i++)
andrewboyson 60:1d8c7a1e7483 65 {
andrewboyson 60:1d8c7a1e7483 66 if (records[i].state == STATE_EMPTY) continue;
andrewboyson 60:1d8c7a1e7483 67 if (records[i].ip) continue;
andrewboyson 60:1d8c7a1e7483 68 if (DnsHostNamesEquate(records[i].name, name)) return i;
andrewboyson 60:1d8c7a1e7483 69 }
andrewboyson 60:1d8c7a1e7483 70 return -1;
andrewboyson 60:1d8c7a1e7483 71 }
andrewboyson 60:1d8c7a1e7483 72 static int getOldest()
andrewboyson 60:1d8c7a1e7483 73 {
andrewboyson 93:580fc113d9e9 74 int iOldest = 0;
andrewboyson 93:580fc113d9e9 75 uint32_t ageOldest = 0;
andrewboyson 60:1d8c7a1e7483 76 for (int i = 0; i < RECORDS_COUNT; i++)
andrewboyson 60:1d8c7a1e7483 77 {
andrewboyson 60:1d8c7a1e7483 78 if (records[i].state == STATE_EMPTY) return i; //Found an empty slot so just return it
andrewboyson 93:580fc113d9e9 79 uint32_t age = MsTimerCount - records[i].elapsed;
andrewboyson 93:580fc113d9e9 80 if (age >= ageOldest)
andrewboyson 60:1d8c7a1e7483 81 {
andrewboyson 93:580fc113d9e9 82 ageOldest = age;
andrewboyson 93:580fc113d9e9 83 iOldest = i;
andrewboyson 60:1d8c7a1e7483 84 }
andrewboyson 60:1d8c7a1e7483 85 }
andrewboyson 93:580fc113d9e9 86 return iOldest; //Otherwise return the oldest
andrewboyson 60:1d8c7a1e7483 87 }
andrewboyson 60:1d8c7a1e7483 88 void Nr4MakeRequestForNameFromIp(uint32_t ip)
andrewboyson 60:1d8c7a1e7483 89 {
andrewboyson 60:1d8c7a1e7483 90 //Don't treat non ips
andrewboyson 60:1d8c7a1e7483 91 if (!ip) return;
andrewboyson 60:1d8c7a1e7483 92 int i;
andrewboyson 60:1d8c7a1e7483 93
andrewboyson 60:1d8c7a1e7483 94 //If a record already exists then request an update
andrewboyson 60:1d8c7a1e7483 95 i = getExistingIp(ip);
andrewboyson 60:1d8c7a1e7483 96 if (i > -1)
andrewboyson 60:1d8c7a1e7483 97 {
andrewboyson 93:580fc113d9e9 98 if (!MsTimerHasElapsed(records[i].elapsed, FREEZE_TIMEOUT_MS)) return;
andrewboyson 60:1d8c7a1e7483 99 if (Nr4Trace)
andrewboyson 60:1d8c7a1e7483 100 {
andrewboyson 60:1d8c7a1e7483 101 LogTimeF("NR - renew name of ");
andrewboyson 60:1d8c7a1e7483 102 Ip4AddressLog(ip);
andrewboyson 60:1d8c7a1e7483 103 Log("\r\n");
andrewboyson 60:1d8c7a1e7483 104 }
andrewboyson 60:1d8c7a1e7483 105 records[i].todo = TODO_NAME_FROM_IP;
andrewboyson 60:1d8c7a1e7483 106 records[i].state = STATE_WANT;
andrewboyson 60:1d8c7a1e7483 107 records[i].protocol = DnsGetNextProtocol4(DNS_PROTOCOL_NONE);
andrewboyson 93:580fc113d9e9 108 records[i].elapsed = MsTimerCount;
andrewboyson 60:1d8c7a1e7483 109 return;
andrewboyson 60:1d8c7a1e7483 110 }
andrewboyson 60:1d8c7a1e7483 111
andrewboyson 60:1d8c7a1e7483 112 //If a record does not exist then find the first empty slot and add the IP and date
andrewboyson 60:1d8c7a1e7483 113 if (Nr4Trace)
andrewboyson 60:1d8c7a1e7483 114 {
andrewboyson 60:1d8c7a1e7483 115 LogTimeF("NR - request name of ");
andrewboyson 60:1d8c7a1e7483 116 Ip4AddressLog(ip);
andrewboyson 60:1d8c7a1e7483 117 Log("\r\n");
andrewboyson 60:1d8c7a1e7483 118 }
andrewboyson 60:1d8c7a1e7483 119 i = getOldest();
andrewboyson 60:1d8c7a1e7483 120 records[i].ip = ip;
andrewboyson 60:1d8c7a1e7483 121 records[i].todo = TODO_NAME_FROM_IP;
andrewboyson 60:1d8c7a1e7483 122 records[i].state = STATE_WANT;
andrewboyson 60:1d8c7a1e7483 123 records[i].protocol = DnsGetNextProtocol4(DNS_PROTOCOL_NONE);
andrewboyson 93:580fc113d9e9 124 records[i].elapsed = MsTimerCount;
andrewboyson 60:1d8c7a1e7483 125 records[i].name[0] = 0;
andrewboyson 60:1d8c7a1e7483 126 }
andrewboyson 60:1d8c7a1e7483 127 void Nr4MakeRequestForIpFromName(char* name)
andrewboyson 60:1d8c7a1e7483 128 {
andrewboyson 60:1d8c7a1e7483 129 //Don't treat non names
andrewboyson 60:1d8c7a1e7483 130 if (!name[0]) return;
andrewboyson 60:1d8c7a1e7483 131 int i;
andrewboyson 60:1d8c7a1e7483 132
andrewboyson 60:1d8c7a1e7483 133 //If a record already exists then request an update
andrewboyson 60:1d8c7a1e7483 134 i = getExistingName(name);
andrewboyson 60:1d8c7a1e7483 135 if (i > -1)
andrewboyson 60:1d8c7a1e7483 136 {
andrewboyson 93:580fc113d9e9 137 if (!MsTimerHasElapsed(records[i].elapsed, FREEZE_TIMEOUT_MS)) return;
andrewboyson 60:1d8c7a1e7483 138 if (Nr4Trace)
andrewboyson 60:1d8c7a1e7483 139 {
andrewboyson 60:1d8c7a1e7483 140 LogTimeF("NR - renew IPv4 of %s\r\n", name);
andrewboyson 60:1d8c7a1e7483 141 }
andrewboyson 60:1d8c7a1e7483 142 records[i].todo = TODO_IP_FROM_NAME;
andrewboyson 60:1d8c7a1e7483 143 records[i].state = STATE_WANT;
andrewboyson 60:1d8c7a1e7483 144 records[i].protocol = DnsGetNextProtocol4(DNS_PROTOCOL_NONE);
andrewboyson 93:580fc113d9e9 145 records[i].elapsed = MsTimerCount;
andrewboyson 60:1d8c7a1e7483 146 return;
andrewboyson 60:1d8c7a1e7483 147 }
andrewboyson 60:1d8c7a1e7483 148
andrewboyson 60:1d8c7a1e7483 149 //If a record does not exist then find the first empty slot and add the name and date
andrewboyson 60:1d8c7a1e7483 150 if (Nr4Trace)
andrewboyson 60:1d8c7a1e7483 151 {
andrewboyson 60:1d8c7a1e7483 152 LogTimeF("NR - request IPv4 of %s\r\n", name);
andrewboyson 60:1d8c7a1e7483 153 }
andrewboyson 60:1d8c7a1e7483 154 i = getOldest();
andrewboyson 60:1d8c7a1e7483 155 records[i].ip = 0;
andrewboyson 60:1d8c7a1e7483 156 records[i].todo = TODO_IP_FROM_NAME;
andrewboyson 60:1d8c7a1e7483 157 records[i].state = STATE_WANT;
andrewboyson 60:1d8c7a1e7483 158 records[i].protocol = DnsGetNextProtocol4(DNS_PROTOCOL_NONE);
andrewboyson 93:580fc113d9e9 159 records[i].elapsed = MsTimerCount;
andrewboyson 60:1d8c7a1e7483 160 strncpy(records[i].name, name, NAME_MAX_LENGTH);
andrewboyson 60:1d8c7a1e7483 161 records[i].name[NAME_MAX_LENGTH - 1] = 0;
andrewboyson 60:1d8c7a1e7483 162 }
andrewboyson 60:1d8c7a1e7483 163 static void addIpRecord(int i, uint32_t ip, char* name, int protocol)
andrewboyson 60:1d8c7a1e7483 164 {
andrewboyson 60:1d8c7a1e7483 165 records[i].todo = TODO_NONE;
andrewboyson 93:580fc113d9e9 166 records[i].elapsed = MsTimerCount;
andrewboyson 60:1d8c7a1e7483 167 records[i].ip = ip;
andrewboyson 60:1d8c7a1e7483 168 records[i].protocol = protocol;
andrewboyson 60:1d8c7a1e7483 169 records[i].state = STATE_VALID;
andrewboyson 60:1d8c7a1e7483 170 strncpy(records[i].name, name, NAME_MAX_LENGTH);
andrewboyson 60:1d8c7a1e7483 171 records[i].name[NAME_MAX_LENGTH - 1] = 0;
andrewboyson 60:1d8c7a1e7483 172 }
andrewboyson 60:1d8c7a1e7483 173 void Nr4AddIpRecord(uint32_t ip, char* name, int protocol)
andrewboyson 60:1d8c7a1e7483 174 {
andrewboyson 60:1d8c7a1e7483 175 int i;
andrewboyson 60:1d8c7a1e7483 176
andrewboyson 60:1d8c7a1e7483 177 //Get existing ip and, if found, add it then clear any name only entries
andrewboyson 60:1d8c7a1e7483 178 i = getExistingIp(ip);
andrewboyson 60:1d8c7a1e7483 179 if (i >= 0)
andrewboyson 60:1d8c7a1e7483 180 {
andrewboyson 60:1d8c7a1e7483 181 if (Nr4Trace)
andrewboyson 60:1d8c7a1e7483 182 {
andrewboyson 60:1d8c7a1e7483 183 if (DnsHostNamesEquate(name, records[i].name)) LogTimeF("NR - confirm existing ");
andrewboyson 60:1d8c7a1e7483 184 else LogTimeF("NR - replace name for existing ip ");
andrewboyson 60:1d8c7a1e7483 185 Ip4AddressLog(ip);
andrewboyson 60:1d8c7a1e7483 186 Log(" == '");
andrewboyson 60:1d8c7a1e7483 187 Log(name);
andrewboyson 60:1d8c7a1e7483 188 Log("'\r\n");
andrewboyson 60:1d8c7a1e7483 189 }
andrewboyson 60:1d8c7a1e7483 190 addIpRecord(i, ip, name, protocol);
andrewboyson 60:1d8c7a1e7483 191
andrewboyson 60:1d8c7a1e7483 192 i = getNameOnly(name);
andrewboyson 60:1d8c7a1e7483 193 if (i >= 0)
andrewboyson 60:1d8c7a1e7483 194 {
andrewboyson 60:1d8c7a1e7483 195 if (Nr4Trace) LogTimeF("NR - clear name '%s' with no ip\r\n", name);
andrewboyson 60:1d8c7a1e7483 196 records[i].state = STATE_EMPTY;
andrewboyson 60:1d8c7a1e7483 197 }
andrewboyson 60:1d8c7a1e7483 198 return;
andrewboyson 60:1d8c7a1e7483 199 }
andrewboyson 60:1d8c7a1e7483 200
andrewboyson 60:1d8c7a1e7483 201 //Get name only entry and, if found, add it
andrewboyson 60:1d8c7a1e7483 202 i = getNameOnly(name);
andrewboyson 60:1d8c7a1e7483 203 if (i >= 0)
andrewboyson 60:1d8c7a1e7483 204 {
andrewboyson 60:1d8c7a1e7483 205 if (Nr4Trace)
andrewboyson 60:1d8c7a1e7483 206 {
andrewboyson 60:1d8c7a1e7483 207 LogTimeF("NR - add ip for name ");
andrewboyson 60:1d8c7a1e7483 208 Ip4AddressLog(ip);
andrewboyson 60:1d8c7a1e7483 209 Log(" == '");
andrewboyson 60:1d8c7a1e7483 210 Log(name);
andrewboyson 60:1d8c7a1e7483 211 Log("'\r\n");
andrewboyson 60:1d8c7a1e7483 212 }
andrewboyson 60:1d8c7a1e7483 213 addIpRecord(i, ip, name, protocol);
andrewboyson 60:1d8c7a1e7483 214 return;
andrewboyson 60:1d8c7a1e7483 215 }
andrewboyson 60:1d8c7a1e7483 216
andrewboyson 60:1d8c7a1e7483 217 //No other entry exists so just add it to the next available space
andrewboyson 60:1d8c7a1e7483 218 i = getOldest();
andrewboyson 60:1d8c7a1e7483 219 if (Nr4Trace)
andrewboyson 60:1d8c7a1e7483 220 {
andrewboyson 60:1d8c7a1e7483 221 LogTimeF("NR - add ip for name ");
andrewboyson 60:1d8c7a1e7483 222 Ip4AddressLog(ip);
andrewboyson 60:1d8c7a1e7483 223 Log(" == '");
andrewboyson 60:1d8c7a1e7483 224 Log(name);
andrewboyson 60:1d8c7a1e7483 225 Log("'\r\n");
andrewboyson 60:1d8c7a1e7483 226 }
andrewboyson 60:1d8c7a1e7483 227 addIpRecord(i, ip, name, protocol);
andrewboyson 60:1d8c7a1e7483 228 }
andrewboyson 60:1d8c7a1e7483 229 void Nr4IpToName(uint32_t ip, char* name)
andrewboyson 60:1d8c7a1e7483 230 {
andrewboyson 60:1d8c7a1e7483 231 for (int i = 0; i < RECORDS_COUNT; i++)
andrewboyson 60:1d8c7a1e7483 232 {
andrewboyson 60:1d8c7a1e7483 233 if (records[i].state == STATE_EMPTY) continue;
andrewboyson 60:1d8c7a1e7483 234 if (records[i].ip == ip)
andrewboyson 60:1d8c7a1e7483 235 {
andrewboyson 60:1d8c7a1e7483 236 strcpy(name, records[i].name);
andrewboyson 60:1d8c7a1e7483 237 return;
andrewboyson 60:1d8c7a1e7483 238 }
andrewboyson 60:1d8c7a1e7483 239 }
andrewboyson 60:1d8c7a1e7483 240 name[0] = 0;
andrewboyson 60:1d8c7a1e7483 241 }
andrewboyson 60:1d8c7a1e7483 242 void Nr4NameToIp(char* name, uint32_t* pIp)
andrewboyson 60:1d8c7a1e7483 243 {
andrewboyson 93:580fc113d9e9 244 uint32_t newest = 0xFFFFFFFF;
andrewboyson 60:1d8c7a1e7483 245 *pIp = 0;
andrewboyson 60:1d8c7a1e7483 246 for (int i = 0; i < RECORDS_COUNT; i++)
andrewboyson 60:1d8c7a1e7483 247 {
andrewboyson 60:1d8c7a1e7483 248 if (records[i].state == STATE_EMPTY) continue;
andrewboyson 60:1d8c7a1e7483 249 if (!records[i].ip) continue;
andrewboyson 60:1d8c7a1e7483 250 if (!DnsHostNamesEquate(records[i].name, name)) continue;
andrewboyson 93:580fc113d9e9 251 uint32_t age = MsTimerCount - records[i].elapsed;
andrewboyson 93:580fc113d9e9 252 if (age <= newest)
andrewboyson 60:1d8c7a1e7483 253 {
andrewboyson 93:580fc113d9e9 254 newest = age;
andrewboyson 60:1d8c7a1e7483 255 *pIp = records[i].ip;
andrewboyson 60:1d8c7a1e7483 256 }
andrewboyson 60:1d8c7a1e7483 257 }
andrewboyson 60:1d8c7a1e7483 258 }
andrewboyson 60:1d8c7a1e7483 259 static char letterFromStateAndProtocol(uint8_t dnsState, uint8_t protocol)
andrewboyson 60:1d8c7a1e7483 260 {
andrewboyson 60:1d8c7a1e7483 261 switch (dnsState)
andrewboyson 60:1d8c7a1e7483 262 {
andrewboyson 60:1d8c7a1e7483 263 case STATE_SENT:
andrewboyson 60:1d8c7a1e7483 264 case STATE_WANT: return '>';
andrewboyson 60:1d8c7a1e7483 265
andrewboyson 60:1d8c7a1e7483 266 case STATE_VALID:
andrewboyson 60:1d8c7a1e7483 267 switch (protocol)
andrewboyson 60:1d8c7a1e7483 268 {
andrewboyson 60:1d8c7a1e7483 269 case DNS_PROTOCOL_UDNS: return 'd';
andrewboyson 60:1d8c7a1e7483 270 case DNS_PROTOCOL_MDNS: return 'm';
andrewboyson 60:1d8c7a1e7483 271 case DNS_PROTOCOL_LLMNR: return 'l';
andrewboyson 60:1d8c7a1e7483 272 case DNS_PROTOCOL_NONE: return '-';
andrewboyson 60:1d8c7a1e7483 273 default: return '?';
andrewboyson 60:1d8c7a1e7483 274 }
andrewboyson 60:1d8c7a1e7483 275 default: return '~';
andrewboyson 60:1d8c7a1e7483 276 }
andrewboyson 60:1d8c7a1e7483 277 }
andrewboyson 60:1d8c7a1e7483 278 void Nr4SendHttp()
andrewboyson 60:1d8c7a1e7483 279 {
andrewboyson 60:1d8c7a1e7483 280 for (int i = 0; i < RECORDS_COUNT; i++)
andrewboyson 60:1d8c7a1e7483 281 {
andrewboyson 60:1d8c7a1e7483 282 if (records[i].state == STATE_EMPTY) continue;
andrewboyson 60:1d8c7a1e7483 283 if (records[i].ip || records[i].name[0])
andrewboyson 60:1d8c7a1e7483 284 {
andrewboyson 93:580fc113d9e9 285 HttpAddF("%4u ", (MsTimerCount - records[i].elapsed) / 1000 / 60);
andrewboyson 60:1d8c7a1e7483 286
andrewboyson 60:1d8c7a1e7483 287 int ipLen = Ip4AddressHttp(records[i].ip);
andrewboyson 60:1d8c7a1e7483 288 HttpFillChar(' ', 40 - ipLen);
andrewboyson 60:1d8c7a1e7483 289
andrewboyson 60:1d8c7a1e7483 290 HttpAddChar(letterFromStateAndProtocol(records[i].state, records[i].protocol));
andrewboyson 60:1d8c7a1e7483 291
andrewboyson 60:1d8c7a1e7483 292 HttpAddChar(' ');
andrewboyson 60:1d8c7a1e7483 293
andrewboyson 60:1d8c7a1e7483 294 HttpAddText(records[i].name);
andrewboyson 60:1d8c7a1e7483 295
andrewboyson 60:1d8c7a1e7483 296 HttpAddChar('\r');
andrewboyson 60:1d8c7a1e7483 297 HttpAddChar('\n');
andrewboyson 60:1d8c7a1e7483 298 }
andrewboyson 60:1d8c7a1e7483 299 }
andrewboyson 60:1d8c7a1e7483 300 }
andrewboyson 60:1d8c7a1e7483 301 static void clearCache(struct record* pr)
andrewboyson 60:1d8c7a1e7483 302 {
andrewboyson 93:580fc113d9e9 303 if (MsTimerHasElapsed(pr->elapsed, CACHE_TIMEOUT_MS)) pr->state = STATE_EMPTY;
andrewboyson 60:1d8c7a1e7483 304 }
andrewboyson 60:1d8c7a1e7483 305 static void nextProtocol(struct record* pr)
andrewboyson 60:1d8c7a1e7483 306 {
andrewboyson 93:580fc113d9e9 307 if (pr->state == STATE_SENT && MsTimerHasElapsed(pr->elapsed, REPLY_TIMEOUT_MS) && pr->protocol)
andrewboyson 60:1d8c7a1e7483 308 {
andrewboyson 60:1d8c7a1e7483 309 pr->protocol = DnsGetNextProtocol4(pr->protocol);
andrewboyson 60:1d8c7a1e7483 310 if (pr->protocol)
andrewboyson 60:1d8c7a1e7483 311 {
andrewboyson 60:1d8c7a1e7483 312 pr->state = STATE_WANT;
andrewboyson 60:1d8c7a1e7483 313 }
andrewboyson 60:1d8c7a1e7483 314 else
andrewboyson 60:1d8c7a1e7483 315 {
andrewboyson 60:1d8c7a1e7483 316 if (pr->todo == TODO_NAME_FROM_IP) pr->name[0] = 0;
andrewboyson 60:1d8c7a1e7483 317 if (pr->todo == TODO_IP_FROM_NAME) pr->ip = 0;
andrewboyson 60:1d8c7a1e7483 318 pr->state = STATE_VALID;
andrewboyson 60:1d8c7a1e7483 319 }
andrewboyson 93:580fc113d9e9 320 pr->elapsed = MsTimerCount;
andrewboyson 60:1d8c7a1e7483 321 }
andrewboyson 60:1d8c7a1e7483 322 }
andrewboyson 60:1d8c7a1e7483 323 static void queryNameFromIp(struct record* pr)
andrewboyson 60:1d8c7a1e7483 324 {
andrewboyson 60:1d8c7a1e7483 325 if (Nr4Trace)
andrewboyson 60:1d8c7a1e7483 326 {
andrewboyson 60:1d8c7a1e7483 327 LogTime("NR - send ");
andrewboyson 60:1d8c7a1e7483 328 DnsProtocolLog(pr->protocol);
andrewboyson 60:1d8c7a1e7483 329 Log(" request for name from IP4 ");
andrewboyson 60:1d8c7a1e7483 330 Ip4AddressLog(pr->ip);
andrewboyson 60:1d8c7a1e7483 331 Log("\r\n");
andrewboyson 60:1d8c7a1e7483 332 }
andrewboyson 60:1d8c7a1e7483 333 DnsQueryNameFromIp4(pr->ip, pr->protocol);
andrewboyson 60:1d8c7a1e7483 334 }
andrewboyson 60:1d8c7a1e7483 335 static void queryIpFromName(struct record* pr)
andrewboyson 60:1d8c7a1e7483 336 {
andrewboyson 60:1d8c7a1e7483 337 if (Nr4Trace)
andrewboyson 60:1d8c7a1e7483 338 {
andrewboyson 60:1d8c7a1e7483 339 LogTime("NR - send ");
andrewboyson 60:1d8c7a1e7483 340 DnsProtocolLog(pr->protocol);
andrewboyson 60:1d8c7a1e7483 341 Log(" request for IP4 from name '");
andrewboyson 60:1d8c7a1e7483 342 Log(pr->name);
andrewboyson 60:1d8c7a1e7483 343 Log("'\r\n");
andrewboyson 60:1d8c7a1e7483 344 }
andrewboyson 60:1d8c7a1e7483 345 DnsQueryIp4FromName(pr->name, pr->protocol);
andrewboyson 60:1d8c7a1e7483 346 }
andrewboyson 60:1d8c7a1e7483 347 static void sendRequest(struct record* pr)
andrewboyson 60:1d8c7a1e7483 348 {
andrewboyson 60:1d8c7a1e7483 349 if ( DnsQueryIsBusy ) return;
andrewboyson 60:1d8c7a1e7483 350 if ( pr->state != STATE_WANT) return;
andrewboyson 60:1d8c7a1e7483 351 if (!pr->protocol ) return;
andrewboyson 60:1d8c7a1e7483 352
andrewboyson 60:1d8c7a1e7483 353 if (pr->todo == TODO_NAME_FROM_IP) queryNameFromIp(pr);
andrewboyson 60:1d8c7a1e7483 354 if (pr->todo == TODO_IP_FROM_NAME) queryIpFromName(pr);
andrewboyson 60:1d8c7a1e7483 355
andrewboyson 60:1d8c7a1e7483 356 pr->state = STATE_SENT;
andrewboyson 93:580fc113d9e9 357 pr->elapsed = MsTimerCount;
andrewboyson 60:1d8c7a1e7483 358 }
andrewboyson 60:1d8c7a1e7483 359 void Nr4Main()
andrewboyson 60:1d8c7a1e7483 360 {
andrewboyson 60:1d8c7a1e7483 361 static int i = -1;
andrewboyson 60:1d8c7a1e7483 362 i++;
andrewboyson 60:1d8c7a1e7483 363 if (i >= RECORDS_COUNT) i = 0;
andrewboyson 60:1d8c7a1e7483 364
andrewboyson 60:1d8c7a1e7483 365 struct record* pr = &records[i];
andrewboyson 60:1d8c7a1e7483 366
andrewboyson 60:1d8c7a1e7483 367 clearCache (pr);
andrewboyson 60:1d8c7a1e7483 368 nextProtocol(pr);
andrewboyson 60:1d8c7a1e7483 369 sendRequest (pr);
andrewboyson 60:1d8c7a1e7483 370 }
andrewboyson 60:1d8c7a1e7483 371 void Nr4Init()
andrewboyson 60:1d8c7a1e7483 372 {
andrewboyson 60:1d8c7a1e7483 373 for (int i = 0; i < RECORDS_COUNT; i++) records[i].state = STATE_EMPTY;
andrewboyson 60:1d8c7a1e7483 374 }