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:
Tue Nov 28 17:05:46 2017 +0000
Revision:
57:e0fb648acf48
Parent:
49:1a6336f2b3f9
Child:
59:e0e556c8bd46
Added TFTP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 13:9cd54f7db57a 1 #include "mbed.h"
andrewboyson 13:9cd54f7db57a 2 #include "log.h"
andrewboyson 43:bc028d5a6424 3 #include "clock.h"
andrewboyson 13:9cd54f7db57a 4 #include "net.h"
andrewboyson 37:793b39683406 5 #include "action.h"
andrewboyson 49:1a6336f2b3f9 6 #include "ip4addr.h"
andrewboyson 49:1a6336f2b3f9 7 #include "ip6addr.h"
andrewboyson 13:9cd54f7db57a 8 #include "dhcp.h"
andrewboyson 13:9cd54f7db57a 9 #include "dns.h"
andrewboyson 13:9cd54f7db57a 10 #include "udp.h"
andrewboyson 13:9cd54f7db57a 11 #include "slaac.h"
andrewboyson 13:9cd54f7db57a 12 #include "dnshdr.h"
andrewboyson 13:9cd54f7db57a 13 #include "dnsname.h"
andrewboyson 13:9cd54f7db57a 14
andrewboyson 37:793b39683406 15 bool DnsQueryTrace = false;
andrewboyson 13:9cd54f7db57a 16
andrewboyson 13:9cd54f7db57a 17 #define TIME_OUT_SENT 3
andrewboyson 13:9cd54f7db57a 18
andrewboyson 13:9cd54f7db57a 19 #define MDNS_UNICAST false
andrewboyson 13:9cd54f7db57a 20
andrewboyson 35:93c39d260a83 21 char DnsQueryName[DNS_MAX_LABEL_LENGTH+1];
andrewboyson 13:9cd54f7db57a 22 uint32_t DnsQueryIp4 = 0;
andrewboyson 13:9cd54f7db57a 23 char DnsQueryIp6[16];
andrewboyson 13:9cd54f7db57a 24
andrewboyson 30:e34173b7585c 25 char DnsQueryRecordType = DNS_RECORD_NONE;
andrewboyson 30:e34173b7585c 26 int DnsQueryProtocol = DNS_PROTOCOL_NONE;
andrewboyson 30:e34173b7585c 27 bool DnsQueryIsBusy = false;
andrewboyson 13:9cd54f7db57a 28
andrewboyson 13:9cd54f7db57a 29 static uint32_t started = 0;
andrewboyson 13:9cd54f7db57a 30 static uint32_t elapsed = 0;
andrewboyson 13:9cd54f7db57a 31 static void reap()
andrewboyson 13:9cd54f7db57a 32 {
andrewboyson 13:9cd54f7db57a 33 if (!DnsQueryIsBusy) return;
andrewboyson 13:9cd54f7db57a 34
andrewboyson 13:9cd54f7db57a 35 if (elapsed - started >= TIME_OUT_SENT)
andrewboyson 13:9cd54f7db57a 36 {
andrewboyson 13:9cd54f7db57a 37 LogTimeF("DNS reaped ongoing request for ");
andrewboyson 13:9cd54f7db57a 38 if (DnsQueryName[0]) LogF("name %s", DnsQueryName);
andrewboyson 13:9cd54f7db57a 39 if (DnsQueryIp4)
andrewboyson 13:9cd54f7db57a 40 {
andrewboyson 47:73af5c0b0dc2 41 Log("ip4 "); Ip4AddressLog(DnsQueryIp4);
andrewboyson 13:9cd54f7db57a 42 }
andrewboyson 13:9cd54f7db57a 43 if (DnsQueryIp6[0])
andrewboyson 13:9cd54f7db57a 44 {
andrewboyson 47:73af5c0b0dc2 45 Log("ip6 "); Ip6AddressLog(DnsQueryIp6);
andrewboyson 13:9cd54f7db57a 46 }
andrewboyson 13:9cd54f7db57a 47 LogF("\r\n");
andrewboyson 13:9cd54f7db57a 48
andrewboyson 13:9cd54f7db57a 49 DnsQueryName[0] = 0;
andrewboyson 13:9cd54f7db57a 50 DnsQueryIp4 = 0;
andrewboyson 13:9cd54f7db57a 51 DnsQueryIp6[0] = 0;
andrewboyson 13:9cd54f7db57a 52 DnsQueryIsBusy = false;
andrewboyson 13:9cd54f7db57a 53 started = 0;
andrewboyson 13:9cd54f7db57a 54 DnsQueryProtocol = DNS_PROTOCOL_NONE;
andrewboyson 30:e34173b7585c 55 DnsQueryRecordType = DNS_RECORD_NONE;
andrewboyson 13:9cd54f7db57a 56 }
andrewboyson 13:9cd54f7db57a 57 }
andrewboyson 43:bc028d5a6424 58 void DnsQueryMain()
andrewboyson 13:9cd54f7db57a 59 {
andrewboyson 43:bc028d5a6424 60 if (ClockTicked)
andrewboyson 43:bc028d5a6424 61 {
andrewboyson 43:bc028d5a6424 62 elapsed++;
andrewboyson 43:bc028d5a6424 63 reap();
andrewboyson 43:bc028d5a6424 64 }
andrewboyson 13:9cd54f7db57a 65 }
andrewboyson 13:9cd54f7db57a 66 void DnsQueryIp4FromName(char * name, int protocol)
andrewboyson 13:9cd54f7db57a 67 {
andrewboyson 32:679654f2d023 68 DnsMakeFullNameFromName(protocol, name, sizeof(DnsQueryName), DnsQueryName);
andrewboyson 13:9cd54f7db57a 69 DnsQueryIp4 = 0;
andrewboyson 13:9cd54f7db57a 70 DnsQueryIp6[0] = 0;
andrewboyson 13:9cd54f7db57a 71 DnsQueryIsBusy = true;
andrewboyson 13:9cd54f7db57a 72 started = elapsed;
andrewboyson 13:9cd54f7db57a 73 DnsQueryProtocol = protocol;
andrewboyson 30:e34173b7585c 74 DnsQueryRecordType = DNS_RECORD_A;
andrewboyson 13:9cd54f7db57a 75 }
andrewboyson 13:9cd54f7db57a 76 void DnsQueryIp6FromName(char * name, int protocol)
andrewboyson 13:9cd54f7db57a 77 {
andrewboyson 32:679654f2d023 78 DnsMakeFullNameFromName(protocol, name, sizeof(DnsQueryName), DnsQueryName);
andrewboyson 13:9cd54f7db57a 79 DnsQueryIp4 = 0;
andrewboyson 13:9cd54f7db57a 80 DnsQueryIp6[0] = 0;
andrewboyson 13:9cd54f7db57a 81 DnsQueryIsBusy = true;
andrewboyson 13:9cd54f7db57a 82 started = elapsed;
andrewboyson 13:9cd54f7db57a 83 DnsQueryProtocol = protocol;
andrewboyson 30:e34173b7585c 84 DnsQueryRecordType = DNS_RECORD_AAAA;
andrewboyson 13:9cd54f7db57a 85 }
andrewboyson 13:9cd54f7db57a 86 void DnsQueryNameFromIp4(uint32_t ip, int protocol)
andrewboyson 13:9cd54f7db57a 87 {
andrewboyson 13:9cd54f7db57a 88 DnsQueryName[0] = 0;
andrewboyson 13:9cd54f7db57a 89 DnsQueryIp4 = ip;
andrewboyson 13:9cd54f7db57a 90 DnsQueryIp6[0] = 0;
andrewboyson 13:9cd54f7db57a 91 DnsQueryIsBusy = true;
andrewboyson 13:9cd54f7db57a 92 started = elapsed;
andrewboyson 13:9cd54f7db57a 93 DnsQueryProtocol = protocol;
andrewboyson 30:e34173b7585c 94 DnsQueryRecordType = DNS_RECORD_PTR;
andrewboyson 13:9cd54f7db57a 95 }
andrewboyson 13:9cd54f7db57a 96 void DnsQueryNameFromIp6(char* ip, int protocol)
andrewboyson 13:9cd54f7db57a 97 {
andrewboyson 13:9cd54f7db57a 98 DnsQueryName[0] = 0;
andrewboyson 13:9cd54f7db57a 99 DnsQueryIp4 = 0;
andrewboyson 49:1a6336f2b3f9 100 Ip6AddressCopy(DnsQueryIp6, ip);
andrewboyson 13:9cd54f7db57a 101 DnsQueryIsBusy = true;
andrewboyson 13:9cd54f7db57a 102 started = elapsed;
andrewboyson 13:9cd54f7db57a 103 DnsQueryProtocol = protocol;
andrewboyson 30:e34173b7585c 104 DnsQueryRecordType = DNS_RECORD_PTR;
andrewboyson 30:e34173b7585c 105 }
andrewboyson 30:e34173b7585c 106 static void logQuery()
andrewboyson 30:e34173b7585c 107 {
andrewboyson 43:bc028d5a6424 108 if (NetTraceNewLine) Log("\r\n");
andrewboyson 47:73af5c0b0dc2 109 LogTimeF("DnsQuery sent ");
andrewboyson 47:73af5c0b0dc2 110 DnsProtocolLog(DnsQueryProtocol);
andrewboyson 47:73af5c0b0dc2 111 Log(" request for ");
andrewboyson 47:73af5c0b0dc2 112 DnsRecordTypeLog(DnsQueryRecordType);
andrewboyson 47:73af5c0b0dc2 113 Log(" ");
andrewboyson 30:e34173b7585c 114 if (DnsQueryIp4) //Reverse
andrewboyson 30:e34173b7585c 115 {
andrewboyson 47:73af5c0b0dc2 116 Ip4AddressLog(DnsQueryIp4);
andrewboyson 30:e34173b7585c 117 }
andrewboyson 30:e34173b7585c 118 else if (DnsQueryIp6[0])
andrewboyson 30:e34173b7585c 119 {
andrewboyson 47:73af5c0b0dc2 120 Ip6AddressLog(DnsQueryIp6);
andrewboyson 30:e34173b7585c 121 }
andrewboyson 30:e34173b7585c 122 else //Forward
andrewboyson 30:e34173b7585c 123 {
andrewboyson 47:73af5c0b0dc2 124 Log(DnsQueryName);
andrewboyson 30:e34173b7585c 125 }
andrewboyson 47:73af5c0b0dc2 126 Log("\r\n");
andrewboyson 13:9cd54f7db57a 127 }
andrewboyson 13:9cd54f7db57a 128 int DnsQueryPoll(int* pSize)
andrewboyson 13:9cd54f7db57a 129 {
andrewboyson 13:9cd54f7db57a 130 if (!DnsQueryIsBusy) return DO_NOTHING;
andrewboyson 13:9cd54f7db57a 131 if (DnsQueryProtocol == DNS_PROTOCOL_UDNS && DhcpLocalIp == 0) return DO_NOTHING;
andrewboyson 13:9cd54f7db57a 132
andrewboyson 57:e0fb648acf48 133 NetTraceHostCheckIp6(DnsQueryIp6);
andrewboyson 57:e0fb648acf48 134
andrewboyson 57:e0fb648acf48 135 if (DnsQueryTrace || NetTraceHostGetMatched()) logQuery();
andrewboyson 30:e34173b7585c 136
andrewboyson 13:9cd54f7db57a 137 static uint16_t id = 0;
andrewboyson 13:9cd54f7db57a 138 DnsHdrId = ++id;
andrewboyson 13:9cd54f7db57a 139 DnsHdrIsReply = false;
andrewboyson 13:9cd54f7db57a 140 DnsHdrIsRecursiveQuery = false;
andrewboyson 13:9cd54f7db57a 141
andrewboyson 13:9cd54f7db57a 142 DnsHdrQdcount = 1;
andrewboyson 13:9cd54f7db57a 143 DnsHdrAncount = 0;
andrewboyson 13:9cd54f7db57a 144 DnsHdrNscount = 0;
andrewboyson 13:9cd54f7db57a 145 DnsHdrArcount = 0;
andrewboyson 13:9cd54f7db57a 146
andrewboyson 13:9cd54f7db57a 147 DnsHdrWrite();
andrewboyson 13:9cd54f7db57a 148 char* p = DnsHdrData;
andrewboyson 13:9cd54f7db57a 149
andrewboyson 30:e34173b7585c 150 if (DnsQueryIp4 ) DnsNameEncodeIp4(DnsQueryIp4, &p);
andrewboyson 30:e34173b7585c 151 else if (DnsQueryIp6[0]) DnsNameEncodeIp6(DnsQueryIp6, &p);
andrewboyson 37:793b39683406 152 else DnsNameEncodePtr(DnsQueryName, &p);
andrewboyson 30:e34173b7585c 153
andrewboyson 30:e34173b7585c 154 *p++ = 0;
andrewboyson 30:e34173b7585c 155 *p++ = DnsQueryRecordType;
andrewboyson 13:9cd54f7db57a 156 *p++ = DnsQueryProtocol == DNS_PROTOCOL_MDNS && MDNS_UNICAST ? 0x80 : 0; //Set the 15th bit (UNICAST_RESPONSE) to 1 if MDNS
andrewboyson 13:9cd54f7db57a 157 *p++ = 1; //QCLASS_IN = 1 - internet
andrewboyson 13:9cd54f7db57a 158
andrewboyson 13:9cd54f7db57a 159 *pSize = p - DnsHdrPacket;
andrewboyson 13:9cd54f7db57a 160
andrewboyson 13:9cd54f7db57a 161 DnsQueryIsBusy = false;
andrewboyson 38:cc8945857a0d 162
andrewboyson 57:e0fb648acf48 163 if (DnsQueryTrace || NetTraceHostGetMatched()) DnsHdrLog(DnsQueryProtocol);
andrewboyson 13:9cd54f7db57a 164
andrewboyson 37:793b39683406 165 int dest = DO_NOTHING;
andrewboyson 37:793b39683406 166
andrewboyson 13:9cd54f7db57a 167 switch (DnsQueryProtocol)
andrewboyson 13:9cd54f7db57a 168 {
andrewboyson 37:793b39683406 169 case DNS_PROTOCOL_UDNS: dest = UNICAST_DNS; break; //IPv6 ==> NdpDnsServer; IPv4 ==> DhcpDnsServer
andrewboyson 37:793b39683406 170 case DNS_PROTOCOL_MDNS: dest = MULTICAST_MDNS; break;
andrewboyson 37:793b39683406 171 case DNS_PROTOCOL_LLMNR: dest = MULTICAST_LLMNR; break;
andrewboyson 13:9cd54f7db57a 172 default:
andrewboyson 13:9cd54f7db57a 173 LogTimeF("DNS unknown query protocol %d\r\n", DnsQueryProtocol);
andrewboyson 13:9cd54f7db57a 174 return DO_NOTHING;
andrewboyson 13:9cd54f7db57a 175 }
andrewboyson 37:793b39683406 176
andrewboyson 57:e0fb648acf48 177 return ActionMakeFromDestAndTrace(dest, DnsQueryTrace || NetTraceHostGetMatched());
andrewboyson 13:9cd54f7db57a 178 }