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:
Thu Apr 02 19:08:25 2020 +0000
Revision:
167:3ba4e3c49631
Parent:
128:79052cb4a41c
Child:
170:96c637dc3f52
Modified resolution cache ajaxs to include the index

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 116:60521b29e4c9 67 case DNS_PROTOCOL_NONE: return DNS_PROTOCOL_MDNS;
andrewboyson 32:679654f2d023 68 case DNS_PROTOCOL_MDNS: return DNS_PROTOCOL_LLMNR;
andrewboyson 116:60521b29e4c9 69 case DNS_PROTOCOL_LLMNR: return DNS_PROTOCOL_UDNS;
andrewboyson 116:60521b29e4c9 70 case DNS_PROTOCOL_UDNS: 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 13:9cd54f7db57a 85
andrewboyson 43:bc028d5a6424 86 void DnsMain()
andrewboyson 13:9cd54f7db57a 87 {
andrewboyson 43:bc028d5a6424 88 DnsQueryMain();
andrewboyson 13:9cd54f7db57a 89 }
andrewboyson 13:9cd54f7db57a 90
andrewboyson 59:e0e556c8bd46 91 int DnsHandlePacketReceived(void (*traceback)(void), int dnsProtocol, int sizeRx, void* pPacketRx, int* pSizeTx, void* pPacketTx)
andrewboyson 13:9cd54f7db57a 92 {
andrewboyson 59:e0e556c8bd46 93 DnsHdrSetup(pPacketRx, sizeRx);
andrewboyson 13:9cd54f7db57a 94 DnsHdrRead();
andrewboyson 59:e0e556c8bd46 95
andrewboyson 13:9cd54f7db57a 96 int action;
andrewboyson 13:9cd54f7db57a 97 if (DnsHdrIsReply)
andrewboyson 13:9cd54f7db57a 98 {
andrewboyson 59:e0e556c8bd46 99 action = DnsReplyHandle(traceback, dnsProtocol);
andrewboyson 13:9cd54f7db57a 100 }
andrewboyson 13:9cd54f7db57a 101 else
andrewboyson 13:9cd54f7db57a 102 {
andrewboyson 59:e0e556c8bd46 103 action = DnsServerHandleQuery(traceback, dnsProtocol, pPacketTx, pSizeTx);
andrewboyson 13:9cd54f7db57a 104 }
andrewboyson 13:9cd54f7db57a 105 return action;
andrewboyson 13:9cd54f7db57a 106 }
andrewboyson 13:9cd54f7db57a 107
andrewboyson 59:e0e556c8bd46 108 int DnsPollForPacketToSend(void* pPacketTx, int* pSize)
andrewboyson 13:9cd54f7db57a 109 {
andrewboyson 59:e0e556c8bd46 110 return DnsQueryPoll(pPacketTx, pSize);
andrewboyson 13:9cd54f7db57a 111 }