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/dns.cpp@33:714a0345e59b, 2017-08-23 (annotated)
- Committer:
- andrewboyson
- Date:
- Wed Aug 23 18:02:42 2017 +0000
- Revision:
- 33:714a0345e59b
- Parent:
- 32:679654f2d023
- Child:
- 37:793b39683406
Made DNS names case insensitive
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 13:9cd54f7db57a | 1 | #include "dns.h" |
andrewboyson | 13:9cd54f7db57a | 2 | #include "dnshdr.h" |
andrewboyson | 13:9cd54f7db57a | 3 | #include "dnsquery.h" |
andrewboyson | 13:9cd54f7db57a | 4 | #include "dnsreply.h" |
andrewboyson | 13:9cd54f7db57a | 5 | #include "dnsserver.h" |
andrewboyson | 13:9cd54f7db57a | 6 | #include "io.h" |
andrewboyson | 32:679654f2d023 | 7 | #include "log.h" |
andrewboyson | 32:679654f2d023 | 8 | #include "dhcp.h" |
andrewboyson | 13:9cd54f7db57a | 9 | |
andrewboyson | 13:9cd54f7db57a | 10 | void DnsProtocolToString(uint8_t protocol, int size, char* text) |
andrewboyson | 13:9cd54f7db57a | 11 | { |
andrewboyson | 13:9cd54f7db57a | 12 | switch (protocol) |
andrewboyson | 13:9cd54f7db57a | 13 | { |
andrewboyson | 13:9cd54f7db57a | 14 | case DNS_PROTOCOL_UDNS: strncpy (text, "DNS", size); break; |
andrewboyson | 13:9cd54f7db57a | 15 | case DNS_PROTOCOL_MDNS: strncpy (text, "MDNS", size); break; |
andrewboyson | 13:9cd54f7db57a | 16 | case DNS_PROTOCOL_LLMNR: strncpy (text, "LLMNR", size); break; |
andrewboyson | 13:9cd54f7db57a | 17 | default: snprintf(text, size, "%d", protocol); break; |
andrewboyson | 13:9cd54f7db57a | 18 | } |
andrewboyson | 13:9cd54f7db57a | 19 | } |
andrewboyson | 13:9cd54f7db57a | 20 | |
andrewboyson | 13:9cd54f7db57a | 21 | void DnsRecordTypeToString(uint8_t recordtype, int size, char* text) |
andrewboyson | 13:9cd54f7db57a | 22 | { |
andrewboyson | 13:9cd54f7db57a | 23 | switch (recordtype) |
andrewboyson | 13:9cd54f7db57a | 24 | { |
andrewboyson | 13:9cd54f7db57a | 25 | case DNS_RECORD_A: strncpy (text, "A", size); break; |
andrewboyson | 13:9cd54f7db57a | 26 | case DNS_RECORD_AAAA: strncpy (text, "AAAA", size); break; |
andrewboyson | 13:9cd54f7db57a | 27 | case DNS_RECORD_PTR: strncpy (text, "PTR", size); break; |
andrewboyson | 15:6ca6778168b1 | 28 | case DNS_RECORD_TXT: strncpy (text, "TXT", size); break; |
andrewboyson | 15:6ca6778168b1 | 29 | case DNS_RECORD_SRV: strncpy (text, "SRV", size); break; |
andrewboyson | 13:9cd54f7db57a | 30 | default: snprintf(text, size, "%d", recordtype); break; |
andrewboyson | 13:9cd54f7db57a | 31 | } |
andrewboyson | 13:9cd54f7db57a | 32 | } |
andrewboyson | 32:679654f2d023 | 33 | int DnsGetNextProtocol(int protocol) |
andrewboyson | 32:679654f2d023 | 34 | { |
andrewboyson | 32:679654f2d023 | 35 | switch(protocol) |
andrewboyson | 32:679654f2d023 | 36 | { |
andrewboyson | 32:679654f2d023 | 37 | case DNS_PROTOCOL_NONE: return DNS_PROTOCOL_UDNS; |
andrewboyson | 32:679654f2d023 | 38 | case DNS_PROTOCOL_UDNS: return DNS_PROTOCOL_MDNS; |
andrewboyson | 32:679654f2d023 | 39 | case DNS_PROTOCOL_MDNS: return DNS_PROTOCOL_LLMNR; |
andrewboyson | 32:679654f2d023 | 40 | case DNS_PROTOCOL_LLMNR: return DNS_PROTOCOL_NONE; |
andrewboyson | 32:679654f2d023 | 41 | default: LogTimeF("DNS invalid protocol %d\r\n", protocol); return DNS_PROTOCOL_NONE; |
andrewboyson | 32:679654f2d023 | 42 | } |
andrewboyson | 32:679654f2d023 | 43 | } |
andrewboyson | 33:714a0345e59b | 44 | bool DnsHostNamesEquate(char* pA, char* pB) |
andrewboyson | 33:714a0345e59b | 45 | { |
andrewboyson | 33:714a0345e59b | 46 | while(true) |
andrewboyson | 33:714a0345e59b | 47 | { |
andrewboyson | 33:714a0345e59b | 48 | char a = *pA++; |
andrewboyson | 33:714a0345e59b | 49 | char b = *pB++; |
andrewboyson | 33:714a0345e59b | 50 | if (a >= 'A' && a <= 'Z') a |= 0x20; //Make lower case |
andrewboyson | 33:714a0345e59b | 51 | if (b >= 'A' && b <= 'Z') b |= 0x20; //Make lower case |
andrewboyson | 33:714a0345e59b | 52 | if (a != b) return false; //If different then stop and return the fact |
andrewboyson | 33:714a0345e59b | 53 | if (!a) break; //No need to check 'b' too as it will necessarily be equal to 'a' at this point. |
andrewboyson | 33:714a0345e59b | 54 | } |
andrewboyson | 33:714a0345e59b | 55 | return true; //If we get here the strings must equate. |
andrewboyson | 33:714a0345e59b | 56 | } |
andrewboyson | 33:714a0345e59b | 57 | |
andrewboyson | 32:679654f2d023 | 58 | int DnsMakeFullNameFromName(int protocol, char* p, int size, char* result) |
andrewboyson | 32:679654f2d023 | 59 | { |
andrewboyson | 32:679654f2d023 | 60 | int i = 0; |
andrewboyson | 32:679654f2d023 | 61 | char c; |
andrewboyson | 32:679654f2d023 | 62 | |
andrewboyson | 32:679654f2d023 | 63 | while (i < size - 1) |
andrewboyson | 32:679654f2d023 | 64 | { |
andrewboyson | 32:679654f2d023 | 65 | c = *p++; |
andrewboyson | 32:679654f2d023 | 66 | if (!c) break; |
andrewboyson | 32:679654f2d023 | 67 | *result++ = c; |
andrewboyson | 32:679654f2d023 | 68 | i++; |
andrewboyson | 32:679654f2d023 | 69 | } |
andrewboyson | 32:679654f2d023 | 70 | if (protocol == DNS_PROTOCOL_MDNS) |
andrewboyson | 32:679654f2d023 | 71 | { |
andrewboyson | 32:679654f2d023 | 72 | p = ".local"; |
andrewboyson | 32:679654f2d023 | 73 | while (i < size - 1) |
andrewboyson | 32:679654f2d023 | 74 | { |
andrewboyson | 32:679654f2d023 | 75 | c = *p++; |
andrewboyson | 32:679654f2d023 | 76 | if (!c) break; |
andrewboyson | 32:679654f2d023 | 77 | *result++ = c; |
andrewboyson | 32:679654f2d023 | 78 | i++; |
andrewboyson | 32:679654f2d023 | 79 | } |
andrewboyson | 32:679654f2d023 | 80 | } |
andrewboyson | 32:679654f2d023 | 81 | if (protocol == DNS_PROTOCOL_UDNS && DhcpDomainName[0]) //Shouldn't do this in IPv6 as DHCP is IPv4 only |
andrewboyson | 32:679654f2d023 | 82 | { |
andrewboyson | 32:679654f2d023 | 83 | if (i < size - 1) |
andrewboyson | 32:679654f2d023 | 84 | { |
andrewboyson | 32:679654f2d023 | 85 | *result++ = '.'; |
andrewboyson | 32:679654f2d023 | 86 | i++; |
andrewboyson | 32:679654f2d023 | 87 | } |
andrewboyson | 32:679654f2d023 | 88 | p = DhcpDomainName; |
andrewboyson | 32:679654f2d023 | 89 | while (i < size - 1) |
andrewboyson | 32:679654f2d023 | 90 | { |
andrewboyson | 32:679654f2d023 | 91 | c = *p++; |
andrewboyson | 32:679654f2d023 | 92 | if (!c) break; |
andrewboyson | 32:679654f2d023 | 93 | *result++ = c; |
andrewboyson | 32:679654f2d023 | 94 | i++; |
andrewboyson | 32:679654f2d023 | 95 | } |
andrewboyson | 32:679654f2d023 | 96 | } |
andrewboyson | 32:679654f2d023 | 97 | *result = 0; //Terminate the resulting string |
andrewboyson | 32:679654f2d023 | 98 | return i; |
andrewboyson | 32:679654f2d023 | 99 | } |
andrewboyson | 32:679654f2d023 | 100 | int DnsStripNameFromFullName(int protocol, char* p, int size, char* result) |
andrewboyson | 32:679654f2d023 | 101 | { |
andrewboyson | 32:679654f2d023 | 102 | int i = 0; |
andrewboyson | 32:679654f2d023 | 103 | char c; |
andrewboyson | 32:679654f2d023 | 104 | |
andrewboyson | 32:679654f2d023 | 105 | while (i < size - 1) |
andrewboyson | 32:679654f2d023 | 106 | { |
andrewboyson | 32:679654f2d023 | 107 | c = *p++; |
andrewboyson | 32:679654f2d023 | 108 | if (c == 0) break; //End of the fqdn so stop |
andrewboyson | 32:679654f2d023 | 109 | if (c == '.') |
andrewboyson | 32:679654f2d023 | 110 | { |
andrewboyson | 32:679654f2d023 | 111 | if (protocol == DNS_PROTOCOL_UDNS) |
andrewboyson | 32:679654f2d023 | 112 | { |
andrewboyson | 32:679654f2d023 | 113 | 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 | 32:679654f2d023 | 114 | } |
andrewboyson | 32:679654f2d023 | 115 | else |
andrewboyson | 32:679654f2d023 | 116 | { |
andrewboyson | 32:679654f2d023 | 117 | break; //Strip the domain from an LLMNR (there shouldn't be one) or MDNS (it should always be '.local') fqdn |
andrewboyson | 32:679654f2d023 | 118 | } |
andrewboyson | 32:679654f2d023 | 119 | } |
andrewboyson | 32:679654f2d023 | 120 | *result++ = c; |
andrewboyson | 32:679654f2d023 | 121 | i++; |
andrewboyson | 32:679654f2d023 | 122 | } |
andrewboyson | 32:679654f2d023 | 123 | *result = 0; //Terminate the copied string |
andrewboyson | 32:679654f2d023 | 124 | return i; |
andrewboyson | 32:679654f2d023 | 125 | } |
andrewboyson | 13:9cd54f7db57a | 126 | |
andrewboyson | 13:9cd54f7db57a | 127 | void DnsTick() |
andrewboyson | 13:9cd54f7db57a | 128 | { |
andrewboyson | 13:9cd54f7db57a | 129 | DnsQueryTick(); |
andrewboyson | 13:9cd54f7db57a | 130 | } |
andrewboyson | 13:9cd54f7db57a | 131 | |
andrewboyson | 13:9cd54f7db57a | 132 | int DnsHandlePacketReceived(int dnsProtocol, int* pSize, void* pPacket) |
andrewboyson | 13:9cd54f7db57a | 133 | { |
andrewboyson | 13:9cd54f7db57a | 134 | DnsHdrSetup(pPacket); |
andrewboyson | 13:9cd54f7db57a | 135 | DnsHdrRead(); |
andrewboyson | 13:9cd54f7db57a | 136 | int action; |
andrewboyson | 13:9cd54f7db57a | 137 | if (DnsHdrIsReply) |
andrewboyson | 13:9cd54f7db57a | 138 | { |
andrewboyson | 13:9cd54f7db57a | 139 | action = DnsReplyHandle(dnsProtocol, pSize); |
andrewboyson | 13:9cd54f7db57a | 140 | } |
andrewboyson | 13:9cd54f7db57a | 141 | else |
andrewboyson | 13:9cd54f7db57a | 142 | { |
andrewboyson | 13:9cd54f7db57a | 143 | action = DnsServerHandleQuery(dnsProtocol, pSize); |
andrewboyson | 13:9cd54f7db57a | 144 | } |
andrewboyson | 13:9cd54f7db57a | 145 | return action; |
andrewboyson | 13:9cd54f7db57a | 146 | } |
andrewboyson | 13:9cd54f7db57a | 147 | |
andrewboyson | 13:9cd54f7db57a | 148 | int DnsPollForPacketToSend(void* pPacket, int* pSize) |
andrewboyson | 13:9cd54f7db57a | 149 | { |
andrewboyson | 13:9cd54f7db57a | 150 | DnsHdrSetup(pPacket); |
andrewboyson | 13:9cd54f7db57a | 151 | return DnsQueryPoll(pSize); |
andrewboyson | 13:9cd54f7db57a | 152 | } |