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

Committer:
andrewboyson
Date:
Sat Nov 17 15:49:03 2018 +0000
Revision:
83:08c983006a6e
Parent:
61:aad055f1b0d1
Child:
116:60521b29e4c9
Added "send requests via IPv4" toggles for DNS, NTP and TFTP.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 61:aad055f1b0d1 1 #include <stdint.h>
andrewboyson 61:aad055f1b0d1 2 #include <stdbool.h>
andrewboyson 61:aad055f1b0d1 3 #include <string.h>
andrewboyson 61:aad055f1b0d1 4 #include <stdio.h>
andrewboyson 61:aad055f1b0d1 5
andrewboyson 13:9cd54f7db57a 6 #include "dns.h"
andrewboyson 13:9cd54f7db57a 7 #include "dnshdr.h"
andrewboyson 13:9cd54f7db57a 8 #include "dnsquery.h"
andrewboyson 13:9cd54f7db57a 9 #include "dnsreply.h"
andrewboyson 13:9cd54f7db57a 10 #include "dnsserver.h"
andrewboyson 32:679654f2d023 11 #include "log.h"
andrewboyson 32:679654f2d023 12 #include "dhcp.h"
andrewboyson 13:9cd54f7db57a 13
andrewboyson 83:08c983006a6e 14
andrewboyson 83:08c983006a6e 15 bool DnsSendRequestsViaIp4 = false;
andrewboyson 83:08c983006a6e 16
andrewboyson 47:73af5c0b0dc2 17 void DnsProtocolString(uint8_t protocol, int size, char* text)
andrewboyson 13:9cd54f7db57a 18 {
andrewboyson 13:9cd54f7db57a 19 switch (protocol)
andrewboyson 13:9cd54f7db57a 20 {
andrewboyson 13:9cd54f7db57a 21 case DNS_PROTOCOL_UDNS: strncpy (text, "DNS", size); break;
andrewboyson 13:9cd54f7db57a 22 case DNS_PROTOCOL_MDNS: strncpy (text, "MDNS", size); break;
andrewboyson 13:9cd54f7db57a 23 case DNS_PROTOCOL_LLMNR: strncpy (text, "LLMNR", size); break;
andrewboyson 13:9cd54f7db57a 24 default: snprintf(text, size, "%d", protocol); break;
andrewboyson 13:9cd54f7db57a 25 }
andrewboyson 13:9cd54f7db57a 26 }
andrewboyson 13:9cd54f7db57a 27
andrewboyson 47:73af5c0b0dc2 28 void DnsRecordTypeString(uint8_t recordtype, int size, char* text)
andrewboyson 13:9cd54f7db57a 29 {
andrewboyson 13:9cd54f7db57a 30 switch (recordtype)
andrewboyson 13:9cd54f7db57a 31 {
andrewboyson 13:9cd54f7db57a 32 case DNS_RECORD_A: strncpy (text, "A", size); break;
andrewboyson 13:9cd54f7db57a 33 case DNS_RECORD_AAAA: strncpy (text, "AAAA", size); break;
andrewboyson 13:9cd54f7db57a 34 case DNS_RECORD_PTR: strncpy (text, "PTR", size); break;
andrewboyson 15:6ca6778168b1 35 case DNS_RECORD_TXT: strncpy (text, "TXT", size); break;
andrewboyson 15:6ca6778168b1 36 case DNS_RECORD_SRV: strncpy (text, "SRV", size); break;
andrewboyson 13:9cd54f7db57a 37 default: snprintf(text, size, "%d", recordtype); break;
andrewboyson 13:9cd54f7db57a 38 }
andrewboyson 13:9cd54f7db57a 39 }
andrewboyson 47:73af5c0b0dc2 40 void DnsProtocolLog(uint8_t protocol)
andrewboyson 47:73af5c0b0dc2 41 {
andrewboyson 47:73af5c0b0dc2 42 switch (protocol)
andrewboyson 47:73af5c0b0dc2 43 {
andrewboyson 47:73af5c0b0dc2 44 case DNS_PROTOCOL_UDNS: Log ("DNS "); break;
andrewboyson 47:73af5c0b0dc2 45 case DNS_PROTOCOL_MDNS: Log ("MDNS "); break;
andrewboyson 47:73af5c0b0dc2 46 case DNS_PROTOCOL_LLMNR: Log ("LLMNR"); break;
andrewboyson 47:73af5c0b0dc2 47 default: LogF("%d", protocol); break;
andrewboyson 47:73af5c0b0dc2 48 }
andrewboyson 47:73af5c0b0dc2 49 }
andrewboyson 47:73af5c0b0dc2 50
andrewboyson 47:73af5c0b0dc2 51 void DnsRecordTypeLog(uint8_t recordtype)
andrewboyson 47:73af5c0b0dc2 52 {
andrewboyson 47:73af5c0b0dc2 53 switch (recordtype)
andrewboyson 47:73af5c0b0dc2 54 {
andrewboyson 47:73af5c0b0dc2 55 case DNS_RECORD_A: Log ("A" ); break;
andrewboyson 47:73af5c0b0dc2 56 case DNS_RECORD_AAAA: Log ("AAAA"); break;
andrewboyson 47:73af5c0b0dc2 57 case DNS_RECORD_PTR: Log ("PTR" ); break;
andrewboyson 47:73af5c0b0dc2 58 case DNS_RECORD_TXT: Log ("TXT" ); break;
andrewboyson 47:73af5c0b0dc2 59 case DNS_RECORD_SRV: Log ("SRV" ); break;
andrewboyson 47:73af5c0b0dc2 60 default: LogF("%d", recordtype); break;
andrewboyson 47:73af5c0b0dc2 61 }
andrewboyson 47:73af5c0b0dc2 62 }
andrewboyson 41:db727df5f98b 63 int DnsGetNextProtocol4(int protocol)
andrewboyson 32:679654f2d023 64 {
andrewboyson 32:679654f2d023 65 switch(protocol)
andrewboyson 32:679654f2d023 66 {
andrewboyson 32:679654f2d023 67 case DNS_PROTOCOL_NONE: return DNS_PROTOCOL_UDNS;
andrewboyson 32:679654f2d023 68 case DNS_PROTOCOL_UDNS: return DNS_PROTOCOL_MDNS;
andrewboyson 32:679654f2d023 69 case DNS_PROTOCOL_MDNS: return DNS_PROTOCOL_LLMNR;
andrewboyson 32:679654f2d023 70 case DNS_PROTOCOL_LLMNR: return DNS_PROTOCOL_NONE;
andrewboyson 32:679654f2d023 71 default: LogTimeF("DNS invalid protocol %d\r\n", protocol); return DNS_PROTOCOL_NONE;
andrewboyson 32:679654f2d023 72 }
andrewboyson 32:679654f2d023 73 }
andrewboyson 41:db727df5f98b 74 int DnsGetNextProtocol6(int protocol)
andrewboyson 41:db727df5f98b 75 {
andrewboyson 41:db727df5f98b 76 switch(protocol)
andrewboyson 41:db727df5f98b 77 {
andrewboyson 41:db727df5f98b 78 case DNS_PROTOCOL_NONE: return DNS_PROTOCOL_MDNS;
andrewboyson 41:db727df5f98b 79 case DNS_PROTOCOL_MDNS: return DNS_PROTOCOL_LLMNR;
andrewboyson 41:db727df5f98b 80 case DNS_PROTOCOL_LLMNR: return DNS_PROTOCOL_UDNS;
andrewboyson 41:db727df5f98b 81 case DNS_PROTOCOL_UDNS: return DNS_PROTOCOL_NONE;
andrewboyson 41:db727df5f98b 82 default: LogTimeF("DNS invalid protocol %d\r\n", protocol); return DNS_PROTOCOL_NONE;
andrewboyson 41:db727df5f98b 83 }
andrewboyson 41:db727df5f98b 84 }
andrewboyson 33:714a0345e59b 85 bool DnsHostNamesEquate(char* pA, char* pB)
andrewboyson 33:714a0345e59b 86 {
andrewboyson 33:714a0345e59b 87 while(true)
andrewboyson 33:714a0345e59b 88 {
andrewboyson 33:714a0345e59b 89 char a = *pA++;
andrewboyson 33:714a0345e59b 90 char b = *pB++;
andrewboyson 33:714a0345e59b 91 if (a >= 'A' && a <= 'Z') a |= 0x20; //Make lower case
andrewboyson 33:714a0345e59b 92 if (b >= 'A' && b <= 'Z') b |= 0x20; //Make lower case
andrewboyson 33:714a0345e59b 93 if (a != b) return false; //If different then stop and return the fact
andrewboyson 33:714a0345e59b 94 if (!a) break; //No need to check 'b' too as it will necessarily be equal to 'a' at this point.
andrewboyson 33:714a0345e59b 95 }
andrewboyson 33:714a0345e59b 96 return true; //If we get here the strings must equate.
andrewboyson 33:714a0345e59b 97 }
andrewboyson 33:714a0345e59b 98
andrewboyson 59:e0e556c8bd46 99 int DnsMakeFullNameFromName(int protocol, const char* p, int size, char* result)
andrewboyson 32:679654f2d023 100 {
andrewboyson 32:679654f2d023 101 int i = 0;
andrewboyson 32:679654f2d023 102 char c;
andrewboyson 32:679654f2d023 103
andrewboyson 32:679654f2d023 104 while (i < size - 1)
andrewboyson 32:679654f2d023 105 {
andrewboyson 32:679654f2d023 106 c = *p++;
andrewboyson 32:679654f2d023 107 if (!c) break;
andrewboyson 32:679654f2d023 108 *result++ = c;
andrewboyson 32:679654f2d023 109 i++;
andrewboyson 32:679654f2d023 110 }
andrewboyson 32:679654f2d023 111 if (protocol == DNS_PROTOCOL_MDNS)
andrewboyson 32:679654f2d023 112 {
andrewboyson 32:679654f2d023 113 p = ".local";
andrewboyson 32:679654f2d023 114 while (i < size - 1)
andrewboyson 32:679654f2d023 115 {
andrewboyson 32:679654f2d023 116 c = *p++;
andrewboyson 32:679654f2d023 117 if (!c) break;
andrewboyson 32:679654f2d023 118 *result++ = c;
andrewboyson 32:679654f2d023 119 i++;
andrewboyson 32:679654f2d023 120 }
andrewboyson 32:679654f2d023 121 }
andrewboyson 32:679654f2d023 122 if (protocol == DNS_PROTOCOL_UDNS && DhcpDomainName[0]) //Shouldn't do this in IPv6 as DHCP is IPv4 only
andrewboyson 32:679654f2d023 123 {
andrewboyson 32:679654f2d023 124 if (i < size - 1)
andrewboyson 32:679654f2d023 125 {
andrewboyson 32:679654f2d023 126 *result++ = '.';
andrewboyson 32:679654f2d023 127 i++;
andrewboyson 32:679654f2d023 128 }
andrewboyson 32:679654f2d023 129 p = DhcpDomainName;
andrewboyson 32:679654f2d023 130 while (i < size - 1)
andrewboyson 32:679654f2d023 131 {
andrewboyson 32:679654f2d023 132 c = *p++;
andrewboyson 32:679654f2d023 133 if (!c) break;
andrewboyson 32:679654f2d023 134 *result++ = c;
andrewboyson 32:679654f2d023 135 i++;
andrewboyson 32:679654f2d023 136 }
andrewboyson 32:679654f2d023 137 }
andrewboyson 32:679654f2d023 138 *result = 0; //Terminate the resulting string
andrewboyson 32:679654f2d023 139 return i;
andrewboyson 32:679654f2d023 140 }
andrewboyson 32:679654f2d023 141 int DnsStripNameFromFullName(int protocol, char* p, int size, char* result)
andrewboyson 32:679654f2d023 142 {
andrewboyson 32:679654f2d023 143 int i = 0;
andrewboyson 32:679654f2d023 144 char c;
andrewboyson 32:679654f2d023 145
andrewboyson 32:679654f2d023 146 while (i < size - 1)
andrewboyson 32:679654f2d023 147 {
andrewboyson 32:679654f2d023 148 c = *p++;
andrewboyson 32:679654f2d023 149 if (c == 0) break; //End of the fqdn so stop
andrewboyson 32:679654f2d023 150 if (c == '.')
andrewboyson 32:679654f2d023 151 {
andrewboyson 32:679654f2d023 152 if (protocol == DNS_PROTOCOL_UDNS)
andrewboyson 32:679654f2d023 153 {
andrewboyson 32:679654f2d023 154 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 155 }
andrewboyson 32:679654f2d023 156 else
andrewboyson 32:679654f2d023 157 {
andrewboyson 32:679654f2d023 158 break; //Strip the domain from an LLMNR (there shouldn't be one) or MDNS (it should always be '.local') fqdn
andrewboyson 32:679654f2d023 159 }
andrewboyson 32:679654f2d023 160 }
andrewboyson 32:679654f2d023 161 *result++ = c;
andrewboyson 32:679654f2d023 162 i++;
andrewboyson 32:679654f2d023 163 }
andrewboyson 32:679654f2d023 164 *result = 0; //Terminate the copied string
andrewboyson 32:679654f2d023 165 return i;
andrewboyson 32:679654f2d023 166 }
andrewboyson 13:9cd54f7db57a 167
andrewboyson 43:bc028d5a6424 168 void DnsMain()
andrewboyson 13:9cd54f7db57a 169 {
andrewboyson 43:bc028d5a6424 170 DnsQueryMain();
andrewboyson 13:9cd54f7db57a 171 }
andrewboyson 13:9cd54f7db57a 172
andrewboyson 59:e0e556c8bd46 173 int DnsHandlePacketReceived(void (*traceback)(void), int dnsProtocol, int sizeRx, void* pPacketRx, int* pSizeTx, void* pPacketTx)
andrewboyson 13:9cd54f7db57a 174 {
andrewboyson 59:e0e556c8bd46 175 DnsHdrSetup(pPacketRx, sizeRx);
andrewboyson 13:9cd54f7db57a 176 DnsHdrRead();
andrewboyson 59:e0e556c8bd46 177
andrewboyson 13:9cd54f7db57a 178 int action;
andrewboyson 13:9cd54f7db57a 179 if (DnsHdrIsReply)
andrewboyson 13:9cd54f7db57a 180 {
andrewboyson 59:e0e556c8bd46 181 action = DnsReplyHandle(traceback, dnsProtocol);
andrewboyson 13:9cd54f7db57a 182 }
andrewboyson 13:9cd54f7db57a 183 else
andrewboyson 13:9cd54f7db57a 184 {
andrewboyson 59:e0e556c8bd46 185 action = DnsServerHandleQuery(traceback, dnsProtocol, pPacketTx, pSizeTx);
andrewboyson 13:9cd54f7db57a 186 }
andrewboyson 13:9cd54f7db57a 187 return action;
andrewboyson 13:9cd54f7db57a 188 }
andrewboyson 13:9cd54f7db57a 189
andrewboyson 59:e0e556c8bd46 190 int DnsPollForPacketToSend(void* pPacketTx, int* pSize)
andrewboyson 13:9cd54f7db57a 191 {
andrewboyson 59:e0e556c8bd46 192 return DnsQueryPoll(pPacketTx, pSize);
andrewboyson 13:9cd54f7db57a 193 }