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/dnslabel.c@183:ee809769bf89, 2021-01-12 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue Jan 12 19:10:22 2021 +0000
- Revision:
- 183:ee809769bf89
- Parent:
- 182:ff48c6ea91c1
Added acheck as to whether DNS could be used for internal lookups and prevented external lookups being passed to LLMNR or MDNS.; Modified DHCP to try to solve the sky router sometimes sending spurious NAKs.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 128:79052cb4a41c | 1 | #include <stdbool.h> |
andrewboyson | 128:79052cb4a41c | 2 | #include <string.h> |
andrewboyson | 128:79052cb4a41c | 3 | |
andrewboyson | 128:79052cb4a41c | 4 | #include "dns.h" |
andrewboyson | 128:79052cb4a41c | 5 | #include "dnslabel.h" |
andrewboyson | 128:79052cb4a41c | 6 | #include "dhcp.h" |
andrewboyson | 128:79052cb4a41c | 7 | |
andrewboyson | 183:ee809769bf89 | 8 | bool DnsLabelIsExternal(char* p) |
andrewboyson | 183:ee809769bf89 | 9 | { |
andrewboyson | 183:ee809769bf89 | 10 | while (true) |
andrewboyson | 183:ee809769bf89 | 11 | { |
andrewboyson | 183:ee809769bf89 | 12 | if (*p == '\0') return false; |
andrewboyson | 183:ee809769bf89 | 13 | if (*p == '.') return true; |
andrewboyson | 183:ee809769bf89 | 14 | p++; |
andrewboyson | 183:ee809769bf89 | 15 | } |
andrewboyson | 183:ee809769bf89 | 16 | } |
andrewboyson | 183:ee809769bf89 | 17 | |
andrewboyson | 128:79052cb4a41c | 18 | void DnsLabelCopy(char* dst, char* src) |
andrewboyson | 128:79052cb4a41c | 19 | { |
andrewboyson | 128:79052cb4a41c | 20 | strncpy(dst, src, DNS_MAX_LABEL_LENGTH); //Copies up to 63 bytes |
andrewboyson | 128:79052cb4a41c | 21 | dst[DNS_MAX_LABEL_LENGTH] = 0; //dst[63] is the 64th position from 0..63 is set to null. All labels must be declared as a size of 64 |
andrewboyson | 128:79052cb4a41c | 22 | } |
andrewboyson | 128:79052cb4a41c | 23 | |
andrewboyson | 128:79052cb4a41c | 24 | bool DnsLabelIsSame(char* pA, char* pB) |
andrewboyson | 128:79052cb4a41c | 25 | { |
andrewboyson | 128:79052cb4a41c | 26 | while(true) |
andrewboyson | 128:79052cb4a41c | 27 | { |
andrewboyson | 128:79052cb4a41c | 28 | char a = *pA++; |
andrewboyson | 128:79052cb4a41c | 29 | char b = *pB++; |
andrewboyson | 128:79052cb4a41c | 30 | if (a >= 'A' && a <= 'Z') a |= 0x20; //Make lower case |
andrewboyson | 128:79052cb4a41c | 31 | if (b >= 'A' && b <= 'Z') b |= 0x20; //Make lower case |
andrewboyson | 128:79052cb4a41c | 32 | if (a != b) return false; //If different then stop and return the fact |
andrewboyson | 128:79052cb4a41c | 33 | if (!a) break; //No need to check 'b' too as it will necessarily be equal to 'a' at this point. |
andrewboyson | 128:79052cb4a41c | 34 | } |
andrewboyson | 128:79052cb4a41c | 35 | return true; //If we get here the strings must equate. |
andrewboyson | 128:79052cb4a41c | 36 | } |
andrewboyson | 128:79052cb4a41c | 37 | |
andrewboyson | 128:79052cb4a41c | 38 | int DnsLabelMakeFullNameFromName(int protocol, const char* p, int size, char* result) |
andrewboyson | 128:79052cb4a41c | 39 | { |
andrewboyson | 128:79052cb4a41c | 40 | int i = 0; |
andrewboyson | 128:79052cb4a41c | 41 | char c; |
andrewboyson | 182:ff48c6ea91c1 | 42 | bool isExternal = false; |
andrewboyson | 128:79052cb4a41c | 43 | |
andrewboyson | 128:79052cb4a41c | 44 | while (i < size - 1) |
andrewboyson | 128:79052cb4a41c | 45 | { |
andrewboyson | 128:79052cb4a41c | 46 | c = *p++; |
andrewboyson | 182:ff48c6ea91c1 | 47 | if (c == '.') isExternal = true; |
andrewboyson | 128:79052cb4a41c | 48 | if (!c) break; |
andrewboyson | 128:79052cb4a41c | 49 | *result++ = c; |
andrewboyson | 128:79052cb4a41c | 50 | i++; |
andrewboyson | 128:79052cb4a41c | 51 | } |
andrewboyson | 128:79052cb4a41c | 52 | if (protocol == DNS_PROTOCOL_MDNS) |
andrewboyson | 128:79052cb4a41c | 53 | { |
andrewboyson | 128:79052cb4a41c | 54 | p = ".local"; |
andrewboyson | 128:79052cb4a41c | 55 | while (i < size - 1) |
andrewboyson | 128:79052cb4a41c | 56 | { |
andrewboyson | 128:79052cb4a41c | 57 | c = *p++; |
andrewboyson | 128:79052cb4a41c | 58 | if (!c) break; |
andrewboyson | 128:79052cb4a41c | 59 | *result++ = c; |
andrewboyson | 128:79052cb4a41c | 60 | i++; |
andrewboyson | 128:79052cb4a41c | 61 | } |
andrewboyson | 128:79052cb4a41c | 62 | } |
andrewboyson | 182:ff48c6ea91c1 | 63 | if (protocol == DNS_PROTOCOL_UDNS && !isExternal && DhcpDomainName[0]) //Shouldn't do this in IPv6 as DHCP is IPv4 only |
andrewboyson | 128:79052cb4a41c | 64 | { |
andrewboyson | 128:79052cb4a41c | 65 | if (i < size - 1) |
andrewboyson | 128:79052cb4a41c | 66 | { |
andrewboyson | 128:79052cb4a41c | 67 | *result++ = '.'; |
andrewboyson | 128:79052cb4a41c | 68 | i++; |
andrewboyson | 128:79052cb4a41c | 69 | } |
andrewboyson | 128:79052cb4a41c | 70 | p = DhcpDomainName; |
andrewboyson | 128:79052cb4a41c | 71 | while (i < size - 1) |
andrewboyson | 128:79052cb4a41c | 72 | { |
andrewboyson | 128:79052cb4a41c | 73 | c = *p++; |
andrewboyson | 128:79052cb4a41c | 74 | if (!c) break; |
andrewboyson | 128:79052cb4a41c | 75 | *result++ = c; |
andrewboyson | 128:79052cb4a41c | 76 | i++; |
andrewboyson | 128:79052cb4a41c | 77 | } |
andrewboyson | 128:79052cb4a41c | 78 | } |
andrewboyson | 128:79052cb4a41c | 79 | *result = 0; //Terminate the resulting string |
andrewboyson | 128:79052cb4a41c | 80 | return i; |
andrewboyson | 128:79052cb4a41c | 81 | } |
andrewboyson | 128:79052cb4a41c | 82 | int DnsLabelStripNameFromFullName(int protocol, char* p, int size, char* result) |
andrewboyson | 128:79052cb4a41c | 83 | { |
andrewboyson | 128:79052cb4a41c | 84 | int i = 0; |
andrewboyson | 128:79052cb4a41c | 85 | char c; |
andrewboyson | 128:79052cb4a41c | 86 | |
andrewboyson | 128:79052cb4a41c | 87 | while (i < size - 1) |
andrewboyson | 128:79052cb4a41c | 88 | { |
andrewboyson | 128:79052cb4a41c | 89 | c = *p++; |
andrewboyson | 128:79052cb4a41c | 90 | if (c == 0) break; //End of the fqdn so stop |
andrewboyson | 128:79052cb4a41c | 91 | if (c == '.') |
andrewboyson | 128:79052cb4a41c | 92 | { |
andrewboyson | 128:79052cb4a41c | 93 | if (protocol == DNS_PROTOCOL_UDNS) |
andrewboyson | 128:79052cb4a41c | 94 | { |
andrewboyson | 128:79052cb4a41c | 95 | if (strcmp(p, DhcpDomainName) == 0) break; //Strip the domain from a UDNS fqdn if, and only if, it matches the domain given in DHCP. IPv4 only. |
andrewboyson | 128:79052cb4a41c | 96 | } |
andrewboyson | 128:79052cb4a41c | 97 | else |
andrewboyson | 128:79052cb4a41c | 98 | { |
andrewboyson | 128:79052cb4a41c | 99 | break; //Strip the domain from an LLMNR (there shouldn't be one) or MDNS (it should always be '.local') fqdn |
andrewboyson | 128:79052cb4a41c | 100 | } |
andrewboyson | 128:79052cb4a41c | 101 | } |
andrewboyson | 128:79052cb4a41c | 102 | *result++ = c; |
andrewboyson | 128:79052cb4a41c | 103 | i++; |
andrewboyson | 128:79052cb4a41c | 104 | } |
andrewboyson | 128:79052cb4a41c | 105 | *result = 0; //Terminate the copied string |
andrewboyson | 128:79052cb4a41c | 106 | return i; |
andrewboyson | 128:79052cb4a41c | 107 | } |