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