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
udp/dns/dnsname.cpp@36:900e24b27bfb, 2017-09-25 (annotated)
- Committer:
- andrewboyson
- Date:
- Mon Sep 25 07:09:32 2017 +0000
- Revision:
- 36:900e24b27bfb
- Parent:
- 32:679654f2d023
- Child:
- 37:793b39683406
Corrected DNS NameCompare to correctly compare a shortcut name
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 13:9cd54f7db57a | 1 | #include "mbed.h" |
andrewboyson | 13:9cd54f7db57a | 2 | #include "dnshdr.h" |
andrewboyson | 13:9cd54f7db57a | 3 | #include "ctype.h" |
andrewboyson | 13:9cd54f7db57a | 4 | #include "log.h" |
andrewboyson | 13:9cd54f7db57a | 5 | |
andrewboyson | 22:914b970356f0 | 6 | #define DEBUG false |
andrewboyson | 22:914b970356f0 | 7 | |
andrewboyson | 13:9cd54f7db57a | 8 | #define MAX_PACKET_SIZE 512 |
andrewboyson | 13:9cd54f7db57a | 9 | #define MAX_DEPTH 10 |
andrewboyson | 13:9cd54f7db57a | 10 | |
andrewboyson | 13:9cd54f7db57a | 11 | static bool isOffset(char c) { return (c & 0xC0) == 0xC0; } |
andrewboyson | 13:9cd54f7db57a | 12 | static char* derefOffset(char* p) |
andrewboyson | 13:9cd54f7db57a | 13 | { |
andrewboyson | 13:9cd54f7db57a | 14 | int offset = (*p & 0x3F) << 8; //Any bits in the upper byte are added |
andrewboyson | 13:9cd54f7db57a | 15 | p++; //Move to the lower byte |
andrewboyson | 13:9cd54f7db57a | 16 | offset |= *p; //And add it |
andrewboyson | 13:9cd54f7db57a | 17 | return DnsHdrPacket + offset; //Return a pointer |
andrewboyson | 13:9cd54f7db57a | 18 | } |
andrewboyson | 13:9cd54f7db57a | 19 | |
andrewboyson | 13:9cd54f7db57a | 20 | int DnsNameLength(char* pStart) //Returns 0 on error |
andrewboyson | 13:9cd54f7db57a | 21 | { |
andrewboyson | 13:9cd54f7db57a | 22 | char* p = pStart; |
andrewboyson | 13:9cd54f7db57a | 23 | |
andrewboyson | 13:9cd54f7db57a | 24 | while (true) |
andrewboyson | 13:9cd54f7db57a | 25 | { |
andrewboyson | 13:9cd54f7db57a | 26 | if (p > DnsHdrPacket + MAX_PACKET_SIZE) |
andrewboyson | 13:9cd54f7db57a | 27 | { |
andrewboyson | 22:914b970356f0 | 28 | if (DEBUG) LogTimeF("DnsNameLength strayed out of the packet\r\n"); |
andrewboyson | 13:9cd54f7db57a | 29 | return 0; |
andrewboyson | 13:9cd54f7db57a | 30 | } |
andrewboyson | 13:9cd54f7db57a | 31 | |
andrewboyson | 13:9cd54f7db57a | 32 | int length = *p++; |
andrewboyson | 13:9cd54f7db57a | 33 | if (length == 0) //Its the last field so finish |
andrewboyson | 13:9cd54f7db57a | 34 | { |
andrewboyson | 13:9cd54f7db57a | 35 | return p - pStart; |
andrewboyson | 13:9cd54f7db57a | 36 | } |
andrewboyson | 13:9cd54f7db57a | 37 | else if (isOffset(length)) //it's a pointer so skip the length byte and return |
andrewboyson | 13:9cd54f7db57a | 38 | { |
andrewboyson | 13:9cd54f7db57a | 39 | p++; |
andrewboyson | 13:9cd54f7db57a | 40 | return p - pStart; |
andrewboyson | 13:9cd54f7db57a | 41 | } |
andrewboyson | 13:9cd54f7db57a | 42 | else //it's a length plus a field eg 3www |
andrewboyson | 13:9cd54f7db57a | 43 | { |
andrewboyson | 13:9cd54f7db57a | 44 | p += length; |
andrewboyson | 13:9cd54f7db57a | 45 | } |
andrewboyson | 13:9cd54f7db57a | 46 | } |
andrewboyson | 13:9cd54f7db57a | 47 | } |
andrewboyson | 13:9cd54f7db57a | 48 | |
andrewboyson | 13:9cd54f7db57a | 49 | int DnsNameIndexFromPointer(char* p) |
andrewboyson | 13:9cd54f7db57a | 50 | { |
andrewboyson | 13:9cd54f7db57a | 51 | int depth = 0; |
andrewboyson | 13:9cd54f7db57a | 52 | while (isOffset(*p)) //it's a pointer so jump to the new offset eg ^C0 ^0C |
andrewboyson | 13:9cd54f7db57a | 53 | { |
andrewboyson | 13:9cd54f7db57a | 54 | depth++; |
andrewboyson | 13:9cd54f7db57a | 55 | if (depth > MAX_DEPTH) |
andrewboyson | 13:9cd54f7db57a | 56 | { |
andrewboyson | 22:914b970356f0 | 57 | if (DEBUG) LogTimeF("DnsNameIndexFromPointer exceeded depth limit\r\n"); |
andrewboyson | 13:9cd54f7db57a | 58 | return -1; |
andrewboyson | 13:9cd54f7db57a | 59 | } |
andrewboyson | 13:9cd54f7db57a | 60 | p = derefOffset(p); |
andrewboyson | 13:9cd54f7db57a | 61 | } |
andrewboyson | 13:9cd54f7db57a | 62 | return p - DnsHdrPacket; |
andrewboyson | 13:9cd54f7db57a | 63 | } |
andrewboyson | 13:9cd54f7db57a | 64 | |
andrewboyson | 32:679654f2d023 | 65 | bool DnsNameCompare(char* pStart, char* pTarget) |
andrewboyson | 32:679654f2d023 | 66 | { |
andrewboyson | 32:679654f2d023 | 67 | char* p = pStart; |
andrewboyson | 32:679654f2d023 | 68 | |
andrewboyson | 32:679654f2d023 | 69 | int depth = 0; |
andrewboyson | 32:679654f2d023 | 70 | |
andrewboyson | 32:679654f2d023 | 71 | while (true) |
andrewboyson | 32:679654f2d023 | 72 | { |
andrewboyson | 32:679654f2d023 | 73 | if (p > DnsHdrPacket + MAX_PACKET_SIZE) |
andrewboyson | 32:679654f2d023 | 74 | { |
andrewboyson | 32:679654f2d023 | 75 | if (DEBUG) LogTimeF("DnsNameCompare strayed out of the packet\r\n"); |
andrewboyson | 32:679654f2d023 | 76 | return false; |
andrewboyson | 32:679654f2d023 | 77 | } |
andrewboyson | 32:679654f2d023 | 78 | |
andrewboyson | 32:679654f2d023 | 79 | int length = *p; |
andrewboyson | 32:679654f2d023 | 80 | if (length == 0) //Its the last field so should be on the target's terminating NUL |
andrewboyson | 32:679654f2d023 | 81 | { |
andrewboyson | 32:679654f2d023 | 82 | if (*pTarget) return false; |
andrewboyson | 32:679654f2d023 | 83 | return true; |
andrewboyson | 32:679654f2d023 | 84 | } |
andrewboyson | 32:679654f2d023 | 85 | else if (isOffset(length)) //it's a pointer so go up one |
andrewboyson | 32:679654f2d023 | 86 | { |
andrewboyson | 32:679654f2d023 | 87 | depth++; |
andrewboyson | 32:679654f2d023 | 88 | if (depth > MAX_DEPTH) |
andrewboyson | 32:679654f2d023 | 89 | { |
andrewboyson | 32:679654f2d023 | 90 | if (DEBUG) LogTimeF("DnsNameCompare exceeded depth limit\r\n"); |
andrewboyson | 32:679654f2d023 | 91 | return false; |
andrewboyson | 32:679654f2d023 | 92 | } |
andrewboyson | 36:900e24b27bfb | 93 | pStart = derefOffset(p); |
andrewboyson | 36:900e24b27bfb | 94 | p = pStart; |
andrewboyson | 32:679654f2d023 | 95 | } |
andrewboyson | 32:679654f2d023 | 96 | else //it's a length plus a field eg 3www |
andrewboyson | 32:679654f2d023 | 97 | { |
andrewboyson | 32:679654f2d023 | 98 | if (p > pStart) if (*pTarget++ != '.') return false; |
andrewboyson | 32:679654f2d023 | 99 | p++; |
andrewboyson | 32:679654f2d023 | 100 | for (int i = 0; i < length; i++) |
andrewboyson | 32:679654f2d023 | 101 | { |
andrewboyson | 32:679654f2d023 | 102 | if (toupper(*pTarget++) != toupper(*p++)) return false; |
andrewboyson | 32:679654f2d023 | 103 | } |
andrewboyson | 32:679654f2d023 | 104 | } |
andrewboyson | 32:679654f2d023 | 105 | } |
andrewboyson | 32:679654f2d023 | 106 | } |
andrewboyson | 32:679654f2d023 | 107 | bool DnsNameCompareIp4(char *pStart, uint32_t ip) |
andrewboyson | 32:679654f2d023 | 108 | { |
andrewboyson | 32:679654f2d023 | 109 | int field = 0; |
andrewboyson | 32:679654f2d023 | 110 | int depth = 0; |
andrewboyson | 32:679654f2d023 | 111 | char* p = pStart; |
andrewboyson | 32:679654f2d023 | 112 | while (true) |
andrewboyson | 32:679654f2d023 | 113 | { |
andrewboyson | 32:679654f2d023 | 114 | if (p > DnsHdrPacket + MAX_PACKET_SIZE) |
andrewboyson | 32:679654f2d023 | 115 | { |
andrewboyson | 32:679654f2d023 | 116 | LogTimeF("DnsNameCompareIp4 could not find name end\r\n"); |
andrewboyson | 32:679654f2d023 | 117 | return false; |
andrewboyson | 32:679654f2d023 | 118 | } |
andrewboyson | 32:679654f2d023 | 119 | |
andrewboyson | 32:679654f2d023 | 120 | int length = *p; |
andrewboyson | 32:679654f2d023 | 121 | if (length == 0) //Its the last field so return the ip if it is the correct length |
andrewboyson | 32:679654f2d023 | 122 | { |
andrewboyson | 32:679654f2d023 | 123 | if (field != 6) return false; |
andrewboyson | 32:679654f2d023 | 124 | return true; |
andrewboyson | 32:679654f2d023 | 125 | } |
andrewboyson | 32:679654f2d023 | 126 | else if (isOffset(*p)) //it's a pointer so jump to the new offset eg ^C0 ^0C |
andrewboyson | 32:679654f2d023 | 127 | { |
andrewboyson | 32:679654f2d023 | 128 | depth++; |
andrewboyson | 32:679654f2d023 | 129 | if (depth > MAX_DEPTH) |
andrewboyson | 32:679654f2d023 | 130 | { |
andrewboyson | 32:679654f2d023 | 131 | LogTimeF("DnsNameCompareIp4 exceeded depth limit\r\n"); |
andrewboyson | 32:679654f2d023 | 132 | return false; |
andrewboyson | 32:679654f2d023 | 133 | } |
andrewboyson | 32:679654f2d023 | 134 | p = derefOffset(p); |
andrewboyson | 32:679654f2d023 | 135 | } |
andrewboyson | 32:679654f2d023 | 136 | else //it's a length plus a field eg 3www |
andrewboyson | 32:679654f2d023 | 137 | { |
andrewboyson | 32:679654f2d023 | 138 | p++; // move past the length |
andrewboyson | 32:679654f2d023 | 139 | if (field <= 3) |
andrewboyson | 32:679654f2d023 | 140 | { |
andrewboyson | 32:679654f2d023 | 141 | if (length > 3) return false; // expect 90.1.168.192.in-addr.arpa |
andrewboyson | 32:679654f2d023 | 142 | int byte = 0; |
andrewboyson | 32:679654f2d023 | 143 | for (int i = 0; i < length; i++) |
andrewboyson | 32:679654f2d023 | 144 | { |
andrewboyson | 32:679654f2d023 | 145 | byte = byte * 10; |
andrewboyson | 32:679654f2d023 | 146 | if (*p > '9' || *p < '0') return false; // expect 90.1.168.192.in-addr.arpa |
andrewboyson | 32:679654f2d023 | 147 | byte += *p - '0'; |
andrewboyson | 32:679654f2d023 | 148 | p++; |
andrewboyson | 32:679654f2d023 | 149 | } |
andrewboyson | 32:679654f2d023 | 150 | if (byte != ip & 0xFF) return false; |
andrewboyson | 32:679654f2d023 | 151 | ip >>= 8; |
andrewboyson | 32:679654f2d023 | 152 | } |
andrewboyson | 32:679654f2d023 | 153 | else if (field == 4) |
andrewboyson | 32:679654f2d023 | 154 | { |
andrewboyson | 32:679654f2d023 | 155 | if (length != 7) return false; // expect 90.1.168.192.in-addr.arpa |
andrewboyson | 32:679654f2d023 | 156 | p+= length; |
andrewboyson | 32:679654f2d023 | 157 | } |
andrewboyson | 32:679654f2d023 | 158 | else if (field == 5) |
andrewboyson | 32:679654f2d023 | 159 | { |
andrewboyson | 32:679654f2d023 | 160 | if (length != 4) return false; // expect 90.1.168.192.in-addr.arpa |
andrewboyson | 32:679654f2d023 | 161 | p+= length; |
andrewboyson | 32:679654f2d023 | 162 | } |
andrewboyson | 32:679654f2d023 | 163 | else |
andrewboyson | 32:679654f2d023 | 164 | { |
andrewboyson | 32:679654f2d023 | 165 | return false; // expect 90.1.168.192.in-addr.arpa |
andrewboyson | 32:679654f2d023 | 166 | } |
andrewboyson | 32:679654f2d023 | 167 | field++; |
andrewboyson | 32:679654f2d023 | 168 | } |
andrewboyson | 32:679654f2d023 | 169 | } |
andrewboyson | 32:679654f2d023 | 170 | } |
andrewboyson | 32:679654f2d023 | 171 | bool DnsNameCompareIp6(char* pStart, char* pIp) |
andrewboyson | 32:679654f2d023 | 172 | { |
andrewboyson | 32:679654f2d023 | 173 | int field = 0; |
andrewboyson | 32:679654f2d023 | 174 | int depth = 0; |
andrewboyson | 32:679654f2d023 | 175 | char* p = pStart; |
andrewboyson | 32:679654f2d023 | 176 | while (true) |
andrewboyson | 32:679654f2d023 | 177 | { |
andrewboyson | 32:679654f2d023 | 178 | if (p > DnsHdrPacket + MAX_PACKET_SIZE) |
andrewboyson | 32:679654f2d023 | 179 | { |
andrewboyson | 32:679654f2d023 | 180 | LogTimeF("DnsNameDecodeIp6 could not find name end\r\n"); |
andrewboyson | 32:679654f2d023 | 181 | return false; |
andrewboyson | 32:679654f2d023 | 182 | } |
andrewboyson | 32:679654f2d023 | 183 | |
andrewboyson | 32:679654f2d023 | 184 | int length = *p; |
andrewboyson | 32:679654f2d023 | 185 | if (length == 0) //Its the last field so return the ip if it is the correct length |
andrewboyson | 32:679654f2d023 | 186 | { |
andrewboyson | 32:679654f2d023 | 187 | if (field != 34) return false; |
andrewboyson | 32:679654f2d023 | 188 | return true; |
andrewboyson | 32:679654f2d023 | 189 | } |
andrewboyson | 32:679654f2d023 | 190 | else if (isOffset(*p)) //it's a pointer so jump to the new offset eg ^C0 ^0C |
andrewboyson | 32:679654f2d023 | 191 | { |
andrewboyson | 32:679654f2d023 | 192 | depth++; |
andrewboyson | 32:679654f2d023 | 193 | if (depth > MAX_DEPTH) |
andrewboyson | 32:679654f2d023 | 194 | { |
andrewboyson | 32:679654f2d023 | 195 | LogTimeF("DnsNameDecodeIp6 exceeded depth limit\r\n"); |
andrewboyson | 32:679654f2d023 | 196 | return false; |
andrewboyson | 32:679654f2d023 | 197 | } |
andrewboyson | 32:679654f2d023 | 198 | p = derefOffset(p); |
andrewboyson | 32:679654f2d023 | 199 | } |
andrewboyson | 32:679654f2d023 | 200 | else //it's a length plus a field eg 3www |
andrewboyson | 32:679654f2d023 | 201 | { |
andrewboyson | 32:679654f2d023 | 202 | p++; // move past the length |
andrewboyson | 32:679654f2d023 | 203 | if (field <= 31) |
andrewboyson | 32:679654f2d023 | 204 | { |
andrewboyson | 32:679654f2d023 | 205 | if (length > 1) return false; // expect 5.8.c.c.0.1.e.f.f.f.2.3.1.1.2.0.8.7.d.0.9.0.f.1.0.7.4.0.1.0.0.2.ip6.arpa |
andrewboyson | 32:679654f2d023 | 206 | int nibble = 0; |
andrewboyson | 32:679654f2d023 | 207 | if (*p >= '0' && *p <= '9') nibble = *p - '0'; |
andrewboyson | 32:679654f2d023 | 208 | else if (*p >= 'a' && *p <= 'f') nibble = *p - 'a' + 10; |
andrewboyson | 32:679654f2d023 | 209 | else if (*p >= 'A' && *p <= 'F') nibble = *p - 'A' + 10; |
andrewboyson | 32:679654f2d023 | 210 | else return false; // expect 5.8.c.c.0.1.e.f.f.f.2.3.1.1.2.0.8.7.d.0.9.0.f.1.0.7.4.0.1.0.0.2.ip6.arpa |
andrewboyson | 32:679654f2d023 | 211 | if ((field & 1) == 0) |
andrewboyson | 32:679654f2d023 | 212 | { |
andrewboyson | 32:679654f2d023 | 213 | if (pIp[15 - (field >> 1)] & 0x0F != nibble) return false; |
andrewboyson | 32:679654f2d023 | 214 | } |
andrewboyson | 32:679654f2d023 | 215 | else |
andrewboyson | 32:679654f2d023 | 216 | { |
andrewboyson | 32:679654f2d023 | 217 | nibble <<= 4; |
andrewboyson | 32:679654f2d023 | 218 | if (pIp[15 - (field >> 1)] & 0xF0 != nibble) return false; |
andrewboyson | 32:679654f2d023 | 219 | } |
andrewboyson | 32:679654f2d023 | 220 | p += length; |
andrewboyson | 32:679654f2d023 | 221 | } |
andrewboyson | 32:679654f2d023 | 222 | else if (field == 32) |
andrewboyson | 32:679654f2d023 | 223 | { |
andrewboyson | 32:679654f2d023 | 224 | if (length != 3) return false; // expect 5.8.c.c.0.1.e.f.f.f.2.3.1.1.2.0.8.7.d.0.9.0.f.1.0.7.4.0.1.0.0.2.ip6.arpa |
andrewboyson | 32:679654f2d023 | 225 | p+= length; |
andrewboyson | 32:679654f2d023 | 226 | } |
andrewboyson | 32:679654f2d023 | 227 | else if (field == 33) |
andrewboyson | 32:679654f2d023 | 228 | { |
andrewboyson | 32:679654f2d023 | 229 | if (length != 4) return false; // expect 5.8.c.c.0.1.e.f.f.f.2.3.1.1.2.0.8.7.d.0.9.0.f.1.0.7.4.0.1.0.0.2.ip6.arpa |
andrewboyson | 32:679654f2d023 | 230 | p+= length; |
andrewboyson | 32:679654f2d023 | 231 | } |
andrewboyson | 32:679654f2d023 | 232 | else |
andrewboyson | 32:679654f2d023 | 233 | { |
andrewboyson | 32:679654f2d023 | 234 | return false; // expect 5.8.c.c.0.1.e.f.f.f.2.3.1.1.2.0.8.7.d.0.9.0.f.1.0.7.4.0.1.0.0.2.ip6.arpa |
andrewboyson | 32:679654f2d023 | 235 | } |
andrewboyson | 32:679654f2d023 | 236 | field++; |
andrewboyson | 32:679654f2d023 | 237 | } |
andrewboyson | 32:679654f2d023 | 238 | } |
andrewboyson | 32:679654f2d023 | 239 | } |
andrewboyson | 32:679654f2d023 | 240 | |
andrewboyson | 13:9cd54f7db57a | 241 | void DnsNameDecode(int offset, int lenName, char* pName) |
andrewboyson | 13:9cd54f7db57a | 242 | { |
andrewboyson | 13:9cd54f7db57a | 243 | bool nameStarted = false; |
andrewboyson | 13:9cd54f7db57a | 244 | int depth = 0; |
andrewboyson | 13:9cd54f7db57a | 245 | char* p = DnsHdrPacket + offset; |
andrewboyson | 13:9cd54f7db57a | 246 | while (true) |
andrewboyson | 13:9cd54f7db57a | 247 | { |
andrewboyson | 13:9cd54f7db57a | 248 | if (p > DnsHdrPacket + MAX_PACKET_SIZE) |
andrewboyson | 13:9cd54f7db57a | 249 | { |
andrewboyson | 13:9cd54f7db57a | 250 | *pName = 0; |
andrewboyson | 23:b641979389b2 | 251 | if (DEBUG) LogTimeF("DnsNameDecode could not find name end\r\n"); |
andrewboyson | 13:9cd54f7db57a | 252 | return; |
andrewboyson | 13:9cd54f7db57a | 253 | } |
andrewboyson | 13:9cd54f7db57a | 254 | |
andrewboyson | 13:9cd54f7db57a | 255 | int length = *p; |
andrewboyson | 13:9cd54f7db57a | 256 | if (length == 0) //Its the last field so terminate the string |
andrewboyson | 13:9cd54f7db57a | 257 | { |
andrewboyson | 13:9cd54f7db57a | 258 | *pName = 0; |
andrewboyson | 13:9cd54f7db57a | 259 | return; |
andrewboyson | 13:9cd54f7db57a | 260 | } |
andrewboyson | 13:9cd54f7db57a | 261 | else if (isOffset(*p)) //it's a pointer so jump to the new offset eg ^C0 ^0C |
andrewboyson | 13:9cd54f7db57a | 262 | { |
andrewboyson | 13:9cd54f7db57a | 263 | depth++; |
andrewboyson | 13:9cd54f7db57a | 264 | if (depth > MAX_DEPTH) |
andrewboyson | 13:9cd54f7db57a | 265 | { |
andrewboyson | 13:9cd54f7db57a | 266 | LogTimeF("DnsNameDecode exceeded depth limit\r\n"); |
andrewboyson | 13:9cd54f7db57a | 267 | *pName = 0; |
andrewboyson | 13:9cd54f7db57a | 268 | return; |
andrewboyson | 13:9cd54f7db57a | 269 | } |
andrewboyson | 13:9cd54f7db57a | 270 | p = derefOffset(p); |
andrewboyson | 13:9cd54f7db57a | 271 | } |
andrewboyson | 13:9cd54f7db57a | 272 | else //it's a length plus a field eg 3www |
andrewboyson | 13:9cd54f7db57a | 273 | { |
andrewboyson | 13:9cd54f7db57a | 274 | if (pName) |
andrewboyson | 13:9cd54f7db57a | 275 | { |
andrewboyson | 13:9cd54f7db57a | 276 | if (length + 1 > lenName) //Leave room to terminate the string |
andrewboyson | 13:9cd54f7db57a | 277 | { |
andrewboyson | 13:9cd54f7db57a | 278 | *pName = 0; |
andrewboyson | 13:9cd54f7db57a | 279 | LogTimeF("DnsNameDecode overran name buffer\r\n"); |
andrewboyson | 13:9cd54f7db57a | 280 | return; |
andrewboyson | 13:9cd54f7db57a | 281 | } |
andrewboyson | 13:9cd54f7db57a | 282 | lenName -= length + 1; //name chunk plus a '.' |
andrewboyson | 13:9cd54f7db57a | 283 | if (nameStarted) *pName++ = '.'; |
andrewboyson | 13:9cd54f7db57a | 284 | p++; |
andrewboyson | 13:9cd54f7db57a | 285 | for (int i = 0; i < length; i++) *pName++ = *p++; |
andrewboyson | 13:9cd54f7db57a | 286 | nameStarted = true; |
andrewboyson | 13:9cd54f7db57a | 287 | } |
andrewboyson | 13:9cd54f7db57a | 288 | else |
andrewboyson | 13:9cd54f7db57a | 289 | { |
andrewboyson | 13:9cd54f7db57a | 290 | p += length + 1; |
andrewboyson | 13:9cd54f7db57a | 291 | } |
andrewboyson | 13:9cd54f7db57a | 292 | } |
andrewboyson | 13:9cd54f7db57a | 293 | } |
andrewboyson | 13:9cd54f7db57a | 294 | } |
andrewboyson | 13:9cd54f7db57a | 295 | void DnsNameDecodeIp4(int offset, uint32_t* pIp) |
andrewboyson | 13:9cd54f7db57a | 296 | { |
andrewboyson | 13:9cd54f7db57a | 297 | int field = 0; |
andrewboyson | 13:9cd54f7db57a | 298 | int depth = 0; |
andrewboyson | 13:9cd54f7db57a | 299 | char* p = DnsHdrPacket + offset; |
andrewboyson | 13:9cd54f7db57a | 300 | while (true) |
andrewboyson | 13:9cd54f7db57a | 301 | { |
andrewboyson | 13:9cd54f7db57a | 302 | if (p > DnsHdrPacket + MAX_PACKET_SIZE) |
andrewboyson | 13:9cd54f7db57a | 303 | { |
andrewboyson | 13:9cd54f7db57a | 304 | *pIp = 0; |
andrewboyson | 13:9cd54f7db57a | 305 | LogTimeF("DnsNameDecodeIp4 could not find name end\r\n"); |
andrewboyson | 13:9cd54f7db57a | 306 | return; |
andrewboyson | 13:9cd54f7db57a | 307 | } |
andrewboyson | 13:9cd54f7db57a | 308 | |
andrewboyson | 13:9cd54f7db57a | 309 | int length = *p; |
andrewboyson | 13:9cd54f7db57a | 310 | if (length == 0) //Its the last field so return the ip if it is the correct length |
andrewboyson | 13:9cd54f7db57a | 311 | { |
andrewboyson | 13:9cd54f7db57a | 312 | if (field != 6) *pIp = 0; |
andrewboyson | 13:9cd54f7db57a | 313 | return; |
andrewboyson | 13:9cd54f7db57a | 314 | } |
andrewboyson | 13:9cd54f7db57a | 315 | else if (isOffset(*p)) //it's a pointer so jump to the new offset eg ^C0 ^0C |
andrewboyson | 13:9cd54f7db57a | 316 | { |
andrewboyson | 13:9cd54f7db57a | 317 | depth++; |
andrewboyson | 13:9cd54f7db57a | 318 | if (depth > MAX_DEPTH) |
andrewboyson | 13:9cd54f7db57a | 319 | { |
andrewboyson | 13:9cd54f7db57a | 320 | LogTimeF("DnsNameDecodeIp4 exceeded depth limit\r\n"); |
andrewboyson | 13:9cd54f7db57a | 321 | *pIp = 0; |
andrewboyson | 13:9cd54f7db57a | 322 | return; |
andrewboyson | 13:9cd54f7db57a | 323 | } |
andrewboyson | 13:9cd54f7db57a | 324 | p = derefOffset(p); |
andrewboyson | 13:9cd54f7db57a | 325 | } |
andrewboyson | 13:9cd54f7db57a | 326 | else //it's a length plus a field eg 3www |
andrewboyson | 13:9cd54f7db57a | 327 | { |
andrewboyson | 13:9cd54f7db57a | 328 | p++; // move past the length |
andrewboyson | 13:9cd54f7db57a | 329 | if (field <= 3) |
andrewboyson | 13:9cd54f7db57a | 330 | { |
andrewboyson | 13:9cd54f7db57a | 331 | if (length > 3) {*pIp = 0; return; } // expect 90.1.168.192.in-addr.arpa |
andrewboyson | 13:9cd54f7db57a | 332 | int byte = 0; |
andrewboyson | 13:9cd54f7db57a | 333 | for (int i = 0; i < length; i++) |
andrewboyson | 13:9cd54f7db57a | 334 | { |
andrewboyson | 13:9cd54f7db57a | 335 | byte = byte * 10; |
andrewboyson | 13:9cd54f7db57a | 336 | if (*p > '9' || *p < '0') {*pIp = 0; return; } // expect 90.1.168.192.in-addr.arpa |
andrewboyson | 13:9cd54f7db57a | 337 | byte += *p - '0'; |
andrewboyson | 13:9cd54f7db57a | 338 | p++; |
andrewboyson | 13:9cd54f7db57a | 339 | } |
andrewboyson | 13:9cd54f7db57a | 340 | *pIp <<= 8; |
andrewboyson | 13:9cd54f7db57a | 341 | *pIp |= byte; |
andrewboyson | 13:9cd54f7db57a | 342 | } |
andrewboyson | 13:9cd54f7db57a | 343 | else if (field == 4) |
andrewboyson | 13:9cd54f7db57a | 344 | { |
andrewboyson | 13:9cd54f7db57a | 345 | if (length != 7) {*pIp = 0; return; } // expect 90.1.168.192.in-addr.arpa |
andrewboyson | 13:9cd54f7db57a | 346 | p+= length; |
andrewboyson | 13:9cd54f7db57a | 347 | } |
andrewboyson | 13:9cd54f7db57a | 348 | else if (field == 5) |
andrewboyson | 13:9cd54f7db57a | 349 | { |
andrewboyson | 13:9cd54f7db57a | 350 | if (length != 4) {*pIp = 0; return; } // expect 90.1.168.192.in-addr.arpa |
andrewboyson | 13:9cd54f7db57a | 351 | p+= length; |
andrewboyson | 13:9cd54f7db57a | 352 | } |
andrewboyson | 13:9cd54f7db57a | 353 | else |
andrewboyson | 13:9cd54f7db57a | 354 | { |
andrewboyson | 13:9cd54f7db57a | 355 | *pIp = 0; return; // expect 90.1.168.192.in-addr.arpa |
andrewboyson | 13:9cd54f7db57a | 356 | } |
andrewboyson | 13:9cd54f7db57a | 357 | field++; |
andrewboyson | 13:9cd54f7db57a | 358 | } |
andrewboyson | 13:9cd54f7db57a | 359 | } |
andrewboyson | 13:9cd54f7db57a | 360 | } |
andrewboyson | 13:9cd54f7db57a | 361 | void DnsNameDecodeIp6(int offset, char* pIp) |
andrewboyson | 13:9cd54f7db57a | 362 | { |
andrewboyson | 13:9cd54f7db57a | 363 | int field = 0; |
andrewboyson | 13:9cd54f7db57a | 364 | int depth = 0; |
andrewboyson | 13:9cd54f7db57a | 365 | char* p = DnsHdrPacket + offset; |
andrewboyson | 13:9cd54f7db57a | 366 | while (true) |
andrewboyson | 13:9cd54f7db57a | 367 | { |
andrewboyson | 13:9cd54f7db57a | 368 | if (p > DnsHdrPacket + MAX_PACKET_SIZE) |
andrewboyson | 13:9cd54f7db57a | 369 | { |
andrewboyson | 13:9cd54f7db57a | 370 | pIp[0] = 0; |
andrewboyson | 13:9cd54f7db57a | 371 | LogTimeF("DnsNameDecodeIp6 could not find name end\r\n"); |
andrewboyson | 13:9cd54f7db57a | 372 | return; |
andrewboyson | 13:9cd54f7db57a | 373 | } |
andrewboyson | 13:9cd54f7db57a | 374 | |
andrewboyson | 13:9cd54f7db57a | 375 | int length = *p; |
andrewboyson | 13:9cd54f7db57a | 376 | if (length == 0) //Its the last field so return the ip if it is the correct length |
andrewboyson | 13:9cd54f7db57a | 377 | { |
andrewboyson | 13:9cd54f7db57a | 378 | if (field != 34) pIp[0] = 0; |
andrewboyson | 13:9cd54f7db57a | 379 | return; |
andrewboyson | 13:9cd54f7db57a | 380 | } |
andrewboyson | 13:9cd54f7db57a | 381 | else if (isOffset(*p)) //it's a pointer so jump to the new offset eg ^C0 ^0C |
andrewboyson | 13:9cd54f7db57a | 382 | { |
andrewboyson | 13:9cd54f7db57a | 383 | depth++; |
andrewboyson | 13:9cd54f7db57a | 384 | if (depth > MAX_DEPTH) |
andrewboyson | 13:9cd54f7db57a | 385 | { |
andrewboyson | 13:9cd54f7db57a | 386 | LogTimeF("DnsNameDecodeIp6 exceeded depth limit\r\n"); |
andrewboyson | 13:9cd54f7db57a | 387 | *pIp = 0; |
andrewboyson | 13:9cd54f7db57a | 388 | return; |
andrewboyson | 13:9cd54f7db57a | 389 | } |
andrewboyson | 13:9cd54f7db57a | 390 | p = derefOffset(p); |
andrewboyson | 13:9cd54f7db57a | 391 | } |
andrewboyson | 13:9cd54f7db57a | 392 | else //it's a length plus a field eg 3www |
andrewboyson | 13:9cd54f7db57a | 393 | { |
andrewboyson | 13:9cd54f7db57a | 394 | p++; // move past the length |
andrewboyson | 13:9cd54f7db57a | 395 | if (field <= 31) |
andrewboyson | 13:9cd54f7db57a | 396 | { |
andrewboyson | 13:9cd54f7db57a | 397 | if (length > 1) {pIp[0] = 0; return; } // expect 5.8.c.c.0.1.e.f.f.f.2.3.1.1.2.0.8.7.d.0.9.0.f.1.0.7.4.0.1.0.0.2.ip6.arpa |
andrewboyson | 13:9cd54f7db57a | 398 | int nibble = 0; |
andrewboyson | 13:9cd54f7db57a | 399 | if (*p >= '0' && *p <= '9') nibble = *p - '0'; |
andrewboyson | 13:9cd54f7db57a | 400 | else if (*p >= 'a' && *p <= 'f') nibble = *p - 'a' + 10; |
andrewboyson | 13:9cd54f7db57a | 401 | else if (*p >= 'A' && *p <= 'F') nibble = *p - 'A' + 10; |
andrewboyson | 13:9cd54f7db57a | 402 | else {pIp[0] = 0; return; } // expect 5.8.c.c.0.1.e.f.f.f.2.3.1.1.2.0.8.7.d.0.9.0.f.1.0.7.4.0.1.0.0.2.ip6.arpa |
andrewboyson | 13:9cd54f7db57a | 403 | if ((field & 1) == 0) |
andrewboyson | 13:9cd54f7db57a | 404 | { |
andrewboyson | 13:9cd54f7db57a | 405 | pIp[15 - (field >> 1)] = nibble; |
andrewboyson | 13:9cd54f7db57a | 406 | } |
andrewboyson | 13:9cd54f7db57a | 407 | else |
andrewboyson | 13:9cd54f7db57a | 408 | { |
andrewboyson | 13:9cd54f7db57a | 409 | nibble <<= 4; |
andrewboyson | 13:9cd54f7db57a | 410 | pIp[15 - (field >> 1)] |= nibble; |
andrewboyson | 13:9cd54f7db57a | 411 | } |
andrewboyson | 13:9cd54f7db57a | 412 | p += length; |
andrewboyson | 13:9cd54f7db57a | 413 | } |
andrewboyson | 13:9cd54f7db57a | 414 | else if (field == 32) |
andrewboyson | 13:9cd54f7db57a | 415 | { |
andrewboyson | 13:9cd54f7db57a | 416 | if (length != 3) {pIp[0] = 0; return; } // expect 5.8.c.c.0.1.e.f.f.f.2.3.1.1.2.0.8.7.d.0.9.0.f.1.0.7.4.0.1.0.0.2.ip6.arpa |
andrewboyson | 13:9cd54f7db57a | 417 | p+= length; |
andrewboyson | 13:9cd54f7db57a | 418 | } |
andrewboyson | 13:9cd54f7db57a | 419 | else if (field == 33) |
andrewboyson | 13:9cd54f7db57a | 420 | { |
andrewboyson | 13:9cd54f7db57a | 421 | if (length != 4) {pIp[0] = 0; return; } // expect 5.8.c.c.0.1.e.f.f.f.2.3.1.1.2.0.8.7.d.0.9.0.f.1.0.7.4.0.1.0.0.2.ip6.arpa |
andrewboyson | 13:9cd54f7db57a | 422 | p+= length; |
andrewboyson | 13:9cd54f7db57a | 423 | } |
andrewboyson | 13:9cd54f7db57a | 424 | else |
andrewboyson | 13:9cd54f7db57a | 425 | { |
andrewboyson | 13:9cd54f7db57a | 426 | pIp[0] = 0; return; // expect 5.8.c.c.0.1.e.f.f.f.2.3.1.1.2.0.8.7.d.0.9.0.f.1.0.7.4.0.1.0.0.2.ip6.arpa |
andrewboyson | 13:9cd54f7db57a | 427 | } |
andrewboyson | 13:9cd54f7db57a | 428 | field++; |
andrewboyson | 13:9cd54f7db57a | 429 | } |
andrewboyson | 13:9cd54f7db57a | 430 | } |
andrewboyson | 13:9cd54f7db57a | 431 | } |
andrewboyson | 13:9cd54f7db57a | 432 | void DnsNameEncodeIp4(uint32_t ip, char** pp) |
andrewboyson | 13:9cd54f7db57a | 433 | { |
andrewboyson | 13:9cd54f7db57a | 434 | char* p = *pp; //Get a convenient pointer to the next byte |
andrewboyson | 13:9cd54f7db57a | 435 | |
andrewboyson | 13:9cd54f7db57a | 436 | char* pLen = p; |
andrewboyson | 13:9cd54f7db57a | 437 | p++; |
andrewboyson | 13:9cd54f7db57a | 438 | |
andrewboyson | 13:9cd54f7db57a | 439 | *pLen = sprintf(p, "%d", (ip & 0xff000000) >> 24); |
andrewboyson | 13:9cd54f7db57a | 440 | p += *pLen; |
andrewboyson | 13:9cd54f7db57a | 441 | pLen = p; |
andrewboyson | 13:9cd54f7db57a | 442 | p++; |
andrewboyson | 13:9cd54f7db57a | 443 | |
andrewboyson | 13:9cd54f7db57a | 444 | *pLen = sprintf(p, "%d", (ip & 0x00ff0000) >> 16); |
andrewboyson | 13:9cd54f7db57a | 445 | p += *pLen; |
andrewboyson | 13:9cd54f7db57a | 446 | pLen = p; |
andrewboyson | 13:9cd54f7db57a | 447 | p++; |
andrewboyson | 13:9cd54f7db57a | 448 | |
andrewboyson | 13:9cd54f7db57a | 449 | *pLen = sprintf(p, "%d", (ip & 0x0000ff00) >> 8); |
andrewboyson | 13:9cd54f7db57a | 450 | p += *pLen; |
andrewboyson | 13:9cd54f7db57a | 451 | pLen = p; |
andrewboyson | 13:9cd54f7db57a | 452 | p++; |
andrewboyson | 13:9cd54f7db57a | 453 | |
andrewboyson | 13:9cd54f7db57a | 454 | *pLen = sprintf(p, "%d", (ip & 0x000000ff) >> 0); |
andrewboyson | 13:9cd54f7db57a | 455 | p += *pLen; |
andrewboyson | 13:9cd54f7db57a | 456 | pLen = p; |
andrewboyson | 13:9cd54f7db57a | 457 | p++; |
andrewboyson | 13:9cd54f7db57a | 458 | |
andrewboyson | 13:9cd54f7db57a | 459 | *pLen = sprintf(p, "in-addr"); |
andrewboyson | 13:9cd54f7db57a | 460 | p += *pLen; |
andrewboyson | 13:9cd54f7db57a | 461 | pLen = p; |
andrewboyson | 13:9cd54f7db57a | 462 | p++; |
andrewboyson | 13:9cd54f7db57a | 463 | |
andrewboyson | 13:9cd54f7db57a | 464 | *pLen = sprintf(p, "arpa"); |
andrewboyson | 13:9cd54f7db57a | 465 | p += *pLen; |
andrewboyson | 13:9cd54f7db57a | 466 | pLen = p; |
andrewboyson | 13:9cd54f7db57a | 467 | p++; |
andrewboyson | 13:9cd54f7db57a | 468 | |
andrewboyson | 13:9cd54f7db57a | 469 | *pLen = 0; |
andrewboyson | 13:9cd54f7db57a | 470 | |
andrewboyson | 13:9cd54f7db57a | 471 | *pp = p; //Save the convenient pointer back into the pointer to pointer |
andrewboyson | 13:9cd54f7db57a | 472 | |
andrewboyson | 13:9cd54f7db57a | 473 | } |
andrewboyson | 13:9cd54f7db57a | 474 | |
andrewboyson | 13:9cd54f7db57a | 475 | void DnsNameEncodeIp6(char* ip, char** pp) |
andrewboyson | 13:9cd54f7db57a | 476 | { |
andrewboyson | 13:9cd54f7db57a | 477 | char* p = *pp; //Get a convenient pointer to the next byte |
andrewboyson | 13:9cd54f7db57a | 478 | |
andrewboyson | 13:9cd54f7db57a | 479 | ip += 16; |
andrewboyson | 13:9cd54f7db57a | 480 | for (int i = 0; i < 16; i++) |
andrewboyson | 13:9cd54f7db57a | 481 | { |
andrewboyson | 13:9cd54f7db57a | 482 | ip--; |
andrewboyson | 13:9cd54f7db57a | 483 | |
andrewboyson | 13:9cd54f7db57a | 484 | int v; |
andrewboyson | 13:9cd54f7db57a | 485 | |
andrewboyson | 13:9cd54f7db57a | 486 | *p++ = 1; |
andrewboyson | 13:9cd54f7db57a | 487 | v = *ip & 0x0F; |
andrewboyson | 13:9cd54f7db57a | 488 | *p++ = v < 10 ? v + '0' : v - 10 + 'a'; |
andrewboyson | 13:9cd54f7db57a | 489 | |
andrewboyson | 13:9cd54f7db57a | 490 | *p++ = 1; |
andrewboyson | 13:9cd54f7db57a | 491 | v = *ip >> 4; |
andrewboyson | 13:9cd54f7db57a | 492 | *p++ = v < 10 ? v + '0' : v - 10 + 'a'; |
andrewboyson | 13:9cd54f7db57a | 493 | } |
andrewboyson | 13:9cd54f7db57a | 494 | char* pLen = p; |
andrewboyson | 13:9cd54f7db57a | 495 | p++; |
andrewboyson | 13:9cd54f7db57a | 496 | |
andrewboyson | 13:9cd54f7db57a | 497 | *pLen = sprintf(p, "ip6"); |
andrewboyson | 13:9cd54f7db57a | 498 | p += *pLen; |
andrewboyson | 13:9cd54f7db57a | 499 | pLen = p; |
andrewboyson | 13:9cd54f7db57a | 500 | p++; |
andrewboyson | 13:9cd54f7db57a | 501 | |
andrewboyson | 13:9cd54f7db57a | 502 | *pLen = sprintf(p, "arpa"); |
andrewboyson | 13:9cd54f7db57a | 503 | p += *pLen; |
andrewboyson | 13:9cd54f7db57a | 504 | pLen = p; |
andrewboyson | 13:9cd54f7db57a | 505 | p++; |
andrewboyson | 13:9cd54f7db57a | 506 | |
andrewboyson | 13:9cd54f7db57a | 507 | *pLen = 0; |
andrewboyson | 13:9cd54f7db57a | 508 | |
andrewboyson | 13:9cd54f7db57a | 509 | *pp = p; //Save the convenient pointer back into the pointer to pointer |
andrewboyson | 13:9cd54f7db57a | 510 | |
andrewboyson | 13:9cd54f7db57a | 511 | } |
andrewboyson | 13:9cd54f7db57a | 512 | |
andrewboyson | 13:9cd54f7db57a | 513 | void DnsNameEncode(char* pName, char** pp) |
andrewboyson | 13:9cd54f7db57a | 514 | { |
andrewboyson | 13:9cd54f7db57a | 515 | char* p = *pp; //Get a convenient pointer to the next byte |
andrewboyson | 13:9cd54f7db57a | 516 | |
andrewboyson | 13:9cd54f7db57a | 517 | char* pLen = p; //Record the position of the first length byte |
andrewboyson | 13:9cd54f7db57a | 518 | p++; //Move to the start of the first field |
andrewboyson | 13:9cd54f7db57a | 519 | |
andrewboyson | 13:9cd54f7db57a | 520 | while (true) |
andrewboyson | 13:9cd54f7db57a | 521 | { |
andrewboyson | 13:9cd54f7db57a | 522 | char c = *pName; |
andrewboyson | 13:9cd54f7db57a | 523 | switch (c) |
andrewboyson | 13:9cd54f7db57a | 524 | { |
andrewboyson | 13:9cd54f7db57a | 525 | case 0: |
andrewboyson | 13:9cd54f7db57a | 526 | *pLen = p - pLen - 1; |
andrewboyson | 13:9cd54f7db57a | 527 | *p = 0; |
andrewboyson | 13:9cd54f7db57a | 528 | p++; |
andrewboyson | 13:9cd54f7db57a | 529 | *pp = p; //Save the convenient pointer back into the pointer to pointer |
andrewboyson | 13:9cd54f7db57a | 530 | return; |
andrewboyson | 13:9cd54f7db57a | 531 | case '.': |
andrewboyson | 13:9cd54f7db57a | 532 | *pLen = p - pLen - 1; |
andrewboyson | 13:9cd54f7db57a | 533 | pLen = p; |
andrewboyson | 13:9cd54f7db57a | 534 | p++; |
andrewboyson | 13:9cd54f7db57a | 535 | break; |
andrewboyson | 13:9cd54f7db57a | 536 | default: |
andrewboyson | 13:9cd54f7db57a | 537 | *p = c; |
andrewboyson | 13:9cd54f7db57a | 538 | p++; |
andrewboyson | 13:9cd54f7db57a | 539 | break; |
andrewboyson | 13:9cd54f7db57a | 540 | } |
andrewboyson | 13:9cd54f7db57a | 541 | pName++; |
andrewboyson | 13:9cd54f7db57a | 542 | } |
andrewboyson | 13:9cd54f7db57a | 543 | } |