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@32:679654f2d023, 2017-08-17 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Aug 17 14:21:02 2017 +0000
- Revision:
- 32:679654f2d023
- Parent:
- 15:6ca6778168b1
- Child:
- 33:714a0345e59b
Corrected issues with .local in llmnr and mdns
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 | 32:679654f2d023 | 44 | int DnsMakeFullNameFromName(int protocol, char* p, int size, char* result) |
andrewboyson | 32:679654f2d023 | 45 | { |
andrewboyson | 32:679654f2d023 | 46 | int i = 0; |
andrewboyson | 32:679654f2d023 | 47 | char c; |
andrewboyson | 32:679654f2d023 | 48 | |
andrewboyson | 32:679654f2d023 | 49 | while (i < size - 1) |
andrewboyson | 32:679654f2d023 | 50 | { |
andrewboyson | 32:679654f2d023 | 51 | c = *p++; |
andrewboyson | 32:679654f2d023 | 52 | if (!c) break; |
andrewboyson | 32:679654f2d023 | 53 | *result++ = c; |
andrewboyson | 32:679654f2d023 | 54 | i++; |
andrewboyson | 32:679654f2d023 | 55 | } |
andrewboyson | 32:679654f2d023 | 56 | if (protocol == DNS_PROTOCOL_MDNS) |
andrewboyson | 32:679654f2d023 | 57 | { |
andrewboyson | 32:679654f2d023 | 58 | p = ".local"; |
andrewboyson | 32:679654f2d023 | 59 | while (i < size - 1) |
andrewboyson | 32:679654f2d023 | 60 | { |
andrewboyson | 32:679654f2d023 | 61 | c = *p++; |
andrewboyson | 32:679654f2d023 | 62 | if (!c) break; |
andrewboyson | 32:679654f2d023 | 63 | *result++ = c; |
andrewboyson | 32:679654f2d023 | 64 | i++; |
andrewboyson | 32:679654f2d023 | 65 | } |
andrewboyson | 32:679654f2d023 | 66 | } |
andrewboyson | 32:679654f2d023 | 67 | if (protocol == DNS_PROTOCOL_UDNS && DhcpDomainName[0]) //Shouldn't do this in IPv6 as DHCP is IPv4 only |
andrewboyson | 32:679654f2d023 | 68 | { |
andrewboyson | 32:679654f2d023 | 69 | if (i < size - 1) |
andrewboyson | 32:679654f2d023 | 70 | { |
andrewboyson | 32:679654f2d023 | 71 | *result++ = '.'; |
andrewboyson | 32:679654f2d023 | 72 | i++; |
andrewboyson | 32:679654f2d023 | 73 | } |
andrewboyson | 32:679654f2d023 | 74 | p = DhcpDomainName; |
andrewboyson | 32:679654f2d023 | 75 | while (i < size - 1) |
andrewboyson | 32:679654f2d023 | 76 | { |
andrewboyson | 32:679654f2d023 | 77 | c = *p++; |
andrewboyson | 32:679654f2d023 | 78 | if (!c) break; |
andrewboyson | 32:679654f2d023 | 79 | *result++ = c; |
andrewboyson | 32:679654f2d023 | 80 | i++; |
andrewboyson | 32:679654f2d023 | 81 | } |
andrewboyson | 32:679654f2d023 | 82 | } |
andrewboyson | 32:679654f2d023 | 83 | *result = 0; //Terminate the resulting string |
andrewboyson | 32:679654f2d023 | 84 | return i; |
andrewboyson | 32:679654f2d023 | 85 | } |
andrewboyson | 32:679654f2d023 | 86 | int DnsStripNameFromFullName(int protocol, char* p, int size, char* result) |
andrewboyson | 32:679654f2d023 | 87 | { |
andrewboyson | 32:679654f2d023 | 88 | int i = 0; |
andrewboyson | 32:679654f2d023 | 89 | char c; |
andrewboyson | 32:679654f2d023 | 90 | |
andrewboyson | 32:679654f2d023 | 91 | while (i < size - 1) |
andrewboyson | 32:679654f2d023 | 92 | { |
andrewboyson | 32:679654f2d023 | 93 | c = *p++; |
andrewboyson | 32:679654f2d023 | 94 | if (c >= 'A' && c <= 'Z') c |= 0x20; //Make lower case |
andrewboyson | 32:679654f2d023 | 95 | if (c == 0) break; //End of the fqdn so stop |
andrewboyson | 32:679654f2d023 | 96 | if (c == '.') |
andrewboyson | 32:679654f2d023 | 97 | { |
andrewboyson | 32:679654f2d023 | 98 | if (protocol == DNS_PROTOCOL_UDNS) |
andrewboyson | 32:679654f2d023 | 99 | { |
andrewboyson | 32:679654f2d023 | 100 | 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 | 101 | } |
andrewboyson | 32:679654f2d023 | 102 | else |
andrewboyson | 32:679654f2d023 | 103 | { |
andrewboyson | 32:679654f2d023 | 104 | break; //Strip the domain from an LLMNR (there shouldn't be one) or MDNS (it should always be '.local') fqdn |
andrewboyson | 32:679654f2d023 | 105 | } |
andrewboyson | 32:679654f2d023 | 106 | } |
andrewboyson | 32:679654f2d023 | 107 | *result++ = c; |
andrewboyson | 32:679654f2d023 | 108 | i++; |
andrewboyson | 32:679654f2d023 | 109 | } |
andrewboyson | 32:679654f2d023 | 110 | *result = 0; //Terminate the copied string |
andrewboyson | 32:679654f2d023 | 111 | return i; |
andrewboyson | 32:679654f2d023 | 112 | } |
andrewboyson | 13:9cd54f7db57a | 113 | |
andrewboyson | 13:9cd54f7db57a | 114 | void DnsTick() |
andrewboyson | 13:9cd54f7db57a | 115 | { |
andrewboyson | 13:9cd54f7db57a | 116 | DnsQueryTick(); |
andrewboyson | 13:9cd54f7db57a | 117 | } |
andrewboyson | 13:9cd54f7db57a | 118 | |
andrewboyson | 13:9cd54f7db57a | 119 | int DnsHandlePacketReceived(int dnsProtocol, int* pSize, void* pPacket) |
andrewboyson | 13:9cd54f7db57a | 120 | { |
andrewboyson | 13:9cd54f7db57a | 121 | DnsHdrSetup(pPacket); |
andrewboyson | 13:9cd54f7db57a | 122 | DnsHdrRead(); |
andrewboyson | 13:9cd54f7db57a | 123 | int action; |
andrewboyson | 13:9cd54f7db57a | 124 | if (DnsHdrIsReply) |
andrewboyson | 13:9cd54f7db57a | 125 | { |
andrewboyson | 13:9cd54f7db57a | 126 | action = DnsReplyHandle(dnsProtocol, pSize); |
andrewboyson | 13:9cd54f7db57a | 127 | } |
andrewboyson | 13:9cd54f7db57a | 128 | else |
andrewboyson | 13:9cd54f7db57a | 129 | { |
andrewboyson | 13:9cd54f7db57a | 130 | action = DnsServerHandleQuery(dnsProtocol, pSize); |
andrewboyson | 13:9cd54f7db57a | 131 | } |
andrewboyson | 13:9cd54f7db57a | 132 | return action; |
andrewboyson | 13:9cd54f7db57a | 133 | } |
andrewboyson | 13:9cd54f7db57a | 134 | |
andrewboyson | 13:9cd54f7db57a | 135 | int DnsPollForPacketToSend(void* pPacket, int* pSize) |
andrewboyson | 13:9cd54f7db57a | 136 | { |
andrewboyson | 13:9cd54f7db57a | 137 | DnsHdrSetup(pPacket); |
andrewboyson | 13:9cd54f7db57a | 138 | return DnsQueryPoll(pSize); |
andrewboyson | 13:9cd54f7db57a | 139 | } |