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:
Fri Jan 19 19:47:37 2018 +0000
Revision:
66:18a10c0b6d93
Parent:
65:37acccf2752f
Child:
93:580fc113d9e9
Updated following clock.

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 66:18a10c0b6d93 6 #include "clock.h"
andrewboyson 60:1d8c7a1e7483 7 #include "net.h"
andrewboyson 60:1d8c7a1e7483 8 #include "mac.h"
andrewboyson 60:1d8c7a1e7483 9 #include "ip6addr.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 Nr6Trace = false;
andrewboyson 60:1d8c7a1e7483 16
andrewboyson 60:1d8c7a1e7483 17 #define NAME_MAX_LENGTH 20
andrewboyson 60:1d8c7a1e7483 18 #define CACHE_TIMEOUT 3600
andrewboyson 60:1d8c7a1e7483 19 #define FREEZE_TIMEOUT 1800
andrewboyson 60:1d8c7a1e7483 20 #define REPLY_TIMEOUT 2
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 static uint32_t elapsed = 0;
andrewboyson 60:1d8c7a1e7483 34 struct record
andrewboyson 60:1d8c7a1e7483 35 {
andrewboyson 60:1d8c7a1e7483 36 uint32_t elapsed;
andrewboyson 60:1d8c7a1e7483 37 char ip[16];
andrewboyson 60:1d8c7a1e7483 38 uint8_t todo;
andrewboyson 60:1d8c7a1e7483 39 uint8_t state;
andrewboyson 60:1d8c7a1e7483 40 uint8_t protocol;
andrewboyson 60:1d8c7a1e7483 41 char name[NAME_MAX_LENGTH];
andrewboyson 60:1d8c7a1e7483 42 };
andrewboyson 60:1d8c7a1e7483 43 static struct record records[RECORDS_COUNT];
andrewboyson 60:1d8c7a1e7483 44
andrewboyson 60:1d8c7a1e7483 45 static int getExistingIp(char* ip)
andrewboyson 60:1d8c7a1e7483 46 {
andrewboyson 60:1d8c7a1e7483 47 for (int i = 0; i < RECORDS_COUNT; i++)
andrewboyson 60:1d8c7a1e7483 48 {
andrewboyson 60:1d8c7a1e7483 49 if (records[i].state == STATE_EMPTY) continue;
andrewboyson 60:1d8c7a1e7483 50 if (Ip6AddressIsSame(records[i].ip, ip)) return i;
andrewboyson 60:1d8c7a1e7483 51 }
andrewboyson 60:1d8c7a1e7483 52 return -1;
andrewboyson 60:1d8c7a1e7483 53 }
andrewboyson 60:1d8c7a1e7483 54 static int getExistingName(char* name)
andrewboyson 60:1d8c7a1e7483 55 {
andrewboyson 60:1d8c7a1e7483 56 for (int i = 0; i < RECORDS_COUNT; i++)
andrewboyson 60:1d8c7a1e7483 57 {
andrewboyson 60:1d8c7a1e7483 58 if (records[i].state == STATE_EMPTY) continue;
andrewboyson 60:1d8c7a1e7483 59 if (DnsHostNamesEquate(records[i].name, name)) return i;
andrewboyson 60:1d8c7a1e7483 60 }
andrewboyson 60:1d8c7a1e7483 61 return -1;
andrewboyson 60:1d8c7a1e7483 62 }
andrewboyson 60:1d8c7a1e7483 63 static int getNameOnly(char* name)
andrewboyson 60:1d8c7a1e7483 64 {
andrewboyson 60:1d8c7a1e7483 65 for (int i = 0; i < RECORDS_COUNT; i++)
andrewboyson 60:1d8c7a1e7483 66 {
andrewboyson 60:1d8c7a1e7483 67 if (records[i].state == STATE_EMPTY) continue;
andrewboyson 60:1d8c7a1e7483 68 if (!Ip6AddressIsEmpty(records[i].ip)) continue;
andrewboyson 60:1d8c7a1e7483 69 if (DnsHostNamesEquate(records[i].name, name)) return i;
andrewboyson 60:1d8c7a1e7483 70 }
andrewboyson 60:1d8c7a1e7483 71 return -1;
andrewboyson 60:1d8c7a1e7483 72 }
andrewboyson 60:1d8c7a1e7483 73 static int getOldest()
andrewboyson 60:1d8c7a1e7483 74 {
andrewboyson 60:1d8c7a1e7483 75 int iN = 0;
andrewboyson 60:1d8c7a1e7483 76 uint32_t tN = 0xFFFFFFFF;
andrewboyson 60:1d8c7a1e7483 77 for (int i = 0; i < RECORDS_COUNT; i++)
andrewboyson 60:1d8c7a1e7483 78 {
andrewboyson 60:1d8c7a1e7483 79 if (records[i].state == STATE_EMPTY) return i; //Found an empty slot so just return it
andrewboyson 60:1d8c7a1e7483 80 if (records[i].elapsed < tN)
andrewboyson 60:1d8c7a1e7483 81 {
andrewboyson 60:1d8c7a1e7483 82 tN = records[i].elapsed;
andrewboyson 60:1d8c7a1e7483 83 iN = i;
andrewboyson 60:1d8c7a1e7483 84 }
andrewboyson 60:1d8c7a1e7483 85 }
andrewboyson 60:1d8c7a1e7483 86 return iN; //Otherwise return the oldest
andrewboyson 60:1d8c7a1e7483 87 }
andrewboyson 60:1d8c7a1e7483 88 void Nr6MakeRequestForNameFromIp(char* ip)
andrewboyson 60:1d8c7a1e7483 89 {
andrewboyson 60:1d8c7a1e7483 90 //Don't treat non ips
andrewboyson 60:1d8c7a1e7483 91 if (!ip[0]) 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 60:1d8c7a1e7483 98 if (elapsed < records[i].elapsed + FREEZE_TIMEOUT) return;
andrewboyson 60:1d8c7a1e7483 99 if (Nr6Trace)
andrewboyson 60:1d8c7a1e7483 100 {
andrewboyson 60:1d8c7a1e7483 101 LogTimeF("NR - renew name of ");
andrewboyson 60:1d8c7a1e7483 102 Ip6AddressLog(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 = DnsGetNextProtocol6(DNS_PROTOCOL_NONE);
andrewboyson 60:1d8c7a1e7483 108 records[i].elapsed = elapsed;
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 (Nr6Trace)
andrewboyson 60:1d8c7a1e7483 114 {
andrewboyson 60:1d8c7a1e7483 115 LogTimeF("NR - request name of ");
andrewboyson 60:1d8c7a1e7483 116 Ip6AddressLog(ip);
andrewboyson 60:1d8c7a1e7483 117 Log("\r\n");
andrewboyson 60:1d8c7a1e7483 118 }
andrewboyson 60:1d8c7a1e7483 119 i = getOldest();
andrewboyson 60:1d8c7a1e7483 120 Ip6AddressCopy(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 = DnsGetNextProtocol6(DNS_PROTOCOL_NONE);
andrewboyson 60:1d8c7a1e7483 124 records[i].elapsed = elapsed;
andrewboyson 60:1d8c7a1e7483 125 records[i].name[0] = 0;
andrewboyson 60:1d8c7a1e7483 126 }
andrewboyson 60:1d8c7a1e7483 127 void Nr6MakeRequestForIpFromName(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 60:1d8c7a1e7483 137 if (elapsed < records[i].elapsed + FREEZE_TIMEOUT) return;
andrewboyson 60:1d8c7a1e7483 138 if (Nr6Trace)
andrewboyson 60:1d8c7a1e7483 139 {
andrewboyson 60:1d8c7a1e7483 140 LogTimeF("NR - renew IPv6 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 = DnsGetNextProtocol6(DNS_PROTOCOL_NONE);
andrewboyson 60:1d8c7a1e7483 145 records[i].elapsed = elapsed;
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 (Nr6Trace)
andrewboyson 60:1d8c7a1e7483 151 {
andrewboyson 60:1d8c7a1e7483 152 LogTimeF("NR - request IPv6 of %s\r\n", name);
andrewboyson 60:1d8c7a1e7483 153 }
andrewboyson 60:1d8c7a1e7483 154 i = getOldest();
andrewboyson 60:1d8c7a1e7483 155 records[i].ip[0] = 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 = DnsGetNextProtocol6(DNS_PROTOCOL_NONE);
andrewboyson 60:1d8c7a1e7483 159 records[i].elapsed = elapsed;
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, char* ip, char* name, int protocol)
andrewboyson 60:1d8c7a1e7483 164 {
andrewboyson 60:1d8c7a1e7483 165 records[i].todo = TODO_NONE;
andrewboyson 60:1d8c7a1e7483 166 records[i].elapsed = elapsed;
andrewboyson 60:1d8c7a1e7483 167 Ip6AddressCopy(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 Nr6AddIpRecord(char* 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 (Nr6Trace)
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 Ip6AddressLog(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 (Nr6Trace) 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 (Nr6Trace)
andrewboyson 60:1d8c7a1e7483 206 {
andrewboyson 60:1d8c7a1e7483 207 LogTimeF("NR - add ip for name ");
andrewboyson 60:1d8c7a1e7483 208 Ip6AddressLog(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 (Nr6Trace)
andrewboyson 60:1d8c7a1e7483 220 {
andrewboyson 60:1d8c7a1e7483 221 LogTimeF("NR - add ip for name %s ");
andrewboyson 60:1d8c7a1e7483 222 Ip6AddressLog(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 Nr6IpToName(char* 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 (Ip6AddressIsSame(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 Nr6NameToIp(char* name, char* ip)
andrewboyson 60:1d8c7a1e7483 243 {
andrewboyson 60:1d8c7a1e7483 244 int newest = 0;
andrewboyson 60:1d8c7a1e7483 245 Ip6AddressClear(ip);
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(Ip6AddressIsEmpty(records[i].ip)) continue;
andrewboyson 60:1d8c7a1e7483 250 if (!DnsHostNamesEquate(records[i].name, name)) continue;
andrewboyson 60:1d8c7a1e7483 251 if (records[i].elapsed > newest)
andrewboyson 60:1d8c7a1e7483 252 {
andrewboyson 60:1d8c7a1e7483 253 newest = records[i].elapsed;
andrewboyson 60:1d8c7a1e7483 254 Ip6AddressCopy(ip, records[i].ip);
andrewboyson 60:1d8c7a1e7483 255 }
andrewboyson 60:1d8c7a1e7483 256 }
andrewboyson 60:1d8c7a1e7483 257 }
andrewboyson 60:1d8c7a1e7483 258 static char letterFromStateAndProtocol(uint8_t dnsState, uint8_t protocol)
andrewboyson 60:1d8c7a1e7483 259 {
andrewboyson 60:1d8c7a1e7483 260 switch (dnsState)
andrewboyson 60:1d8c7a1e7483 261 {
andrewboyson 60:1d8c7a1e7483 262 case STATE_WANT:
andrewboyson 60:1d8c7a1e7483 263 case STATE_SENT: return '>';
andrewboyson 60:1d8c7a1e7483 264
andrewboyson 60:1d8c7a1e7483 265 case STATE_VALID:
andrewboyson 60:1d8c7a1e7483 266 switch (protocol)
andrewboyson 60:1d8c7a1e7483 267 {
andrewboyson 60:1d8c7a1e7483 268 case DNS_PROTOCOL_UDNS: return 'd';
andrewboyson 60:1d8c7a1e7483 269 case DNS_PROTOCOL_MDNS: return 'm';
andrewboyson 60:1d8c7a1e7483 270 case DNS_PROTOCOL_LLMNR: return 'l';
andrewboyson 60:1d8c7a1e7483 271 case DNS_PROTOCOL_NONE: return '-';
andrewboyson 60:1d8c7a1e7483 272 default: return '?';
andrewboyson 60:1d8c7a1e7483 273 }
andrewboyson 60:1d8c7a1e7483 274 default: return '~';
andrewboyson 60:1d8c7a1e7483 275 }
andrewboyson 60:1d8c7a1e7483 276 }
andrewboyson 60:1d8c7a1e7483 277 void Nr6SendHttp()
andrewboyson 60:1d8c7a1e7483 278 {
andrewboyson 60:1d8c7a1e7483 279 for (int i = 0; i < RECORDS_COUNT; i++)
andrewboyson 60:1d8c7a1e7483 280 {
andrewboyson 60:1d8c7a1e7483 281 if (records[i].state == STATE_EMPTY) continue;
andrewboyson 60:1d8c7a1e7483 282 if (!Ip6AddressIsEmpty(records[i].ip) || records[i].name[0])
andrewboyson 60:1d8c7a1e7483 283 {
andrewboyson 60:1d8c7a1e7483 284 HttpAddF("%4u ", (elapsed - records[i].elapsed) / 60);
andrewboyson 60:1d8c7a1e7483 285
andrewboyson 60:1d8c7a1e7483 286 int ipLen = Ip6AddressHttp(records[i].ip);
andrewboyson 60:1d8c7a1e7483 287 HttpFillChar(' ', 40 - ipLen);
andrewboyson 60:1d8c7a1e7483 288
andrewboyson 60:1d8c7a1e7483 289 HttpAddChar(letterFromStateAndProtocol(records[i].state, records[i].protocol));
andrewboyson 60:1d8c7a1e7483 290
andrewboyson 60:1d8c7a1e7483 291 HttpAddChar(' ');
andrewboyson 60:1d8c7a1e7483 292
andrewboyson 60:1d8c7a1e7483 293 HttpAddText(records[i].name);
andrewboyson 60:1d8c7a1e7483 294
andrewboyson 60:1d8c7a1e7483 295 HttpAddChar('\r');
andrewboyson 60:1d8c7a1e7483 296 HttpAddChar('\n');
andrewboyson 60:1d8c7a1e7483 297 }
andrewboyson 60:1d8c7a1e7483 298 }
andrewboyson 60:1d8c7a1e7483 299 }
andrewboyson 60:1d8c7a1e7483 300 static void clearCache(struct record* pr)
andrewboyson 60:1d8c7a1e7483 301 {
andrewboyson 60:1d8c7a1e7483 302 if (elapsed > pr->elapsed + CACHE_TIMEOUT) pr->state = STATE_EMPTY;
andrewboyson 60:1d8c7a1e7483 303 }
andrewboyson 60:1d8c7a1e7483 304 static void nextProtocol(struct record* pr)
andrewboyson 60:1d8c7a1e7483 305 {
andrewboyson 60:1d8c7a1e7483 306 if (pr->state == STATE_SENT && elapsed > pr->elapsed + REPLY_TIMEOUT && pr->protocol)
andrewboyson 60:1d8c7a1e7483 307 {
andrewboyson 60:1d8c7a1e7483 308 pr->protocol = DnsGetNextProtocol6(pr->protocol);
andrewboyson 60:1d8c7a1e7483 309 if (pr->protocol)
andrewboyson 60:1d8c7a1e7483 310 {
andrewboyson 60:1d8c7a1e7483 311 pr->state = STATE_WANT;
andrewboyson 60:1d8c7a1e7483 312 }
andrewboyson 60:1d8c7a1e7483 313 else
andrewboyson 60:1d8c7a1e7483 314 {
andrewboyson 60:1d8c7a1e7483 315 if (pr->todo == TODO_NAME_FROM_IP) pr->name[0] = 0;
andrewboyson 60:1d8c7a1e7483 316 if (pr->todo == TODO_IP_FROM_NAME) Ip6AddressClear(pr->ip);
andrewboyson 60:1d8c7a1e7483 317 pr->state = STATE_VALID;
andrewboyson 60:1d8c7a1e7483 318 }
andrewboyson 60:1d8c7a1e7483 319 pr->elapsed = elapsed;
andrewboyson 60:1d8c7a1e7483 320 }
andrewboyson 60:1d8c7a1e7483 321 }
andrewboyson 60:1d8c7a1e7483 322 static void queryNameFromIp(struct record* pr)
andrewboyson 60:1d8c7a1e7483 323 {
andrewboyson 60:1d8c7a1e7483 324 if (Nr6Trace)
andrewboyson 60:1d8c7a1e7483 325 {
andrewboyson 60:1d8c7a1e7483 326 LogTime("NR - send ");
andrewboyson 60:1d8c7a1e7483 327 DnsProtocolLog(pr->protocol);
andrewboyson 60:1d8c7a1e7483 328 Log(" request for name from IP6 ");
andrewboyson 60:1d8c7a1e7483 329 Ip6AddressLog(pr->ip);
andrewboyson 60:1d8c7a1e7483 330 Log("\r\n");
andrewboyson 60:1d8c7a1e7483 331 }
andrewboyson 60:1d8c7a1e7483 332 DnsQueryNameFromIp6(pr->ip, pr->protocol);
andrewboyson 60:1d8c7a1e7483 333 }
andrewboyson 60:1d8c7a1e7483 334 static void queryIpFromName(struct record* pr)
andrewboyson 60:1d8c7a1e7483 335 {
andrewboyson 60:1d8c7a1e7483 336 if (Nr6Trace)
andrewboyson 60:1d8c7a1e7483 337 {
andrewboyson 60:1d8c7a1e7483 338 LogTime("NR - send ");
andrewboyson 60:1d8c7a1e7483 339 DnsProtocolLog(pr->protocol);
andrewboyson 60:1d8c7a1e7483 340 Log(" request for IP6 from name '");
andrewboyson 60:1d8c7a1e7483 341 Log(pr->name);
andrewboyson 60:1d8c7a1e7483 342 Log("'\r\n");
andrewboyson 60:1d8c7a1e7483 343 }
andrewboyson 60:1d8c7a1e7483 344 DnsQueryIp6FromName(pr->name, pr->protocol);
andrewboyson 60:1d8c7a1e7483 345 }
andrewboyson 60:1d8c7a1e7483 346 static void sendRequest(struct record* pr)
andrewboyson 60:1d8c7a1e7483 347 {
andrewboyson 60:1d8c7a1e7483 348 if ( DnsQueryIsBusy ) return;
andrewboyson 60:1d8c7a1e7483 349 if ( pr->state != STATE_WANT) return;
andrewboyson 60:1d8c7a1e7483 350 if (!pr->protocol ) return;
andrewboyson 60:1d8c7a1e7483 351
andrewboyson 60:1d8c7a1e7483 352 if (pr->todo == TODO_NAME_FROM_IP) queryNameFromIp(pr);
andrewboyson 60:1d8c7a1e7483 353 if (pr->todo == TODO_IP_FROM_NAME) queryIpFromName(pr);
andrewboyson 60:1d8c7a1e7483 354
andrewboyson 60:1d8c7a1e7483 355 pr->state = STATE_SENT;
andrewboyson 60:1d8c7a1e7483 356 pr->elapsed = elapsed;
andrewboyson 60:1d8c7a1e7483 357 }
andrewboyson 60:1d8c7a1e7483 358 void Nr6Main()
andrewboyson 60:1d8c7a1e7483 359 {
andrewboyson 60:1d8c7a1e7483 360 static int i = -1;
andrewboyson 60:1d8c7a1e7483 361 i++;
andrewboyson 60:1d8c7a1e7483 362 if (i >= RECORDS_COUNT) i = 0;
andrewboyson 60:1d8c7a1e7483 363
andrewboyson 60:1d8c7a1e7483 364 struct record* pr = &records[i];
andrewboyson 60:1d8c7a1e7483 365
andrewboyson 60:1d8c7a1e7483 366 clearCache (pr);
andrewboyson 60:1d8c7a1e7483 367 nextProtocol(pr);
andrewboyson 60:1d8c7a1e7483 368 sendRequest (pr);
andrewboyson 60:1d8c7a1e7483 369
andrewboyson 66:18a10c0b6d93 370 if (ClockTicked) elapsed++;
andrewboyson 60:1d8c7a1e7483 371 }
andrewboyson 60:1d8c7a1e7483 372 void Nr6Init()
andrewboyson 60:1d8c7a1e7483 373 {
andrewboyson 60:1d8c7a1e7483 374 for (int i = 0; i < RECORDS_COUNT; i++) records[i].state = STATE_EMPTY;
andrewboyson 60:1d8c7a1e7483 375 }