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
- Committer:
- andrewboyson
- Date:
- 2017-08-17
- Revision:
- 32:679654f2d023
- Parent:
- 15:6ca6778168b1
- Child:
- 33:714a0345e59b
File content as of revision 32:679654f2d023:
#include "dns.h" #include "dnshdr.h" #include "dnsquery.h" #include "dnsreply.h" #include "dnsserver.h" #include "io.h" #include "log.h" #include "dhcp.h" void DnsProtocolToString(uint8_t protocol, int size, char* text) { switch (protocol) { case DNS_PROTOCOL_UDNS: strncpy (text, "DNS", size); break; case DNS_PROTOCOL_MDNS: strncpy (text, "MDNS", size); break; case DNS_PROTOCOL_LLMNR: strncpy (text, "LLMNR", size); break; default: snprintf(text, size, "%d", protocol); break; } } void DnsRecordTypeToString(uint8_t recordtype, int size, char* text) { switch (recordtype) { case DNS_RECORD_A: strncpy (text, "A", size); break; case DNS_RECORD_AAAA: strncpy (text, "AAAA", size); break; case DNS_RECORD_PTR: strncpy (text, "PTR", size); break; case DNS_RECORD_TXT: strncpy (text, "TXT", size); break; case DNS_RECORD_SRV: strncpy (text, "SRV", size); break; default: snprintf(text, size, "%d", recordtype); break; } } int DnsGetNextProtocol(int protocol) { switch(protocol) { case DNS_PROTOCOL_NONE: return DNS_PROTOCOL_UDNS; case DNS_PROTOCOL_UDNS: return DNS_PROTOCOL_MDNS; case DNS_PROTOCOL_MDNS: return DNS_PROTOCOL_LLMNR; case DNS_PROTOCOL_LLMNR: return DNS_PROTOCOL_NONE; default: LogTimeF("DNS invalid protocol %d\r\n", protocol); return DNS_PROTOCOL_NONE; } } int DnsMakeFullNameFromName(int protocol, char* p, int size, char* result) { int i = 0; char c; while (i < size - 1) { c = *p++; if (!c) break; *result++ = c; i++; } if (protocol == DNS_PROTOCOL_MDNS) { p = ".local"; while (i < size - 1) { c = *p++; if (!c) break; *result++ = c; i++; } } if (protocol == DNS_PROTOCOL_UDNS && DhcpDomainName[0]) //Shouldn't do this in IPv6 as DHCP is IPv4 only { if (i < size - 1) { *result++ = '.'; i++; } p = DhcpDomainName; while (i < size - 1) { c = *p++; if (!c) break; *result++ = c; i++; } } *result = 0; //Terminate the resulting string return i; } int DnsStripNameFromFullName(int protocol, char* p, int size, char* result) { int i = 0; char c; while (i < size - 1) { c = *p++; if (c >= 'A' && c <= 'Z') c |= 0x20; //Make lower case if (c == 0) break; //End of the fqdn so stop if (c == '.') { if (protocol == DNS_PROTOCOL_UDNS) { 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. } else { break; //Strip the domain from an LLMNR (there shouldn't be one) or MDNS (it should always be '.local') fqdn } } *result++ = c; i++; } *result = 0; //Terminate the copied string return i; } void DnsTick() { DnsQueryTick(); } int DnsHandlePacketReceived(int dnsProtocol, int* pSize, void* pPacket) { DnsHdrSetup(pPacket); DnsHdrRead(); int action; if (DnsHdrIsReply) { action = DnsReplyHandle(dnsProtocol, pSize); } else { action = DnsServerHandleQuery(dnsProtocol, pSize); } return action; } int DnsPollForPacketToSend(void* pPacket, int* pSize) { DnsHdrSetup(pPacket); return DnsQueryPoll(pSize); }