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-05-01
Revision:
13:9cd54f7db57a
Child:
15:6ca6778168b1

File content as of revision 13:9cd54f7db57a:

#include "dns.h"
#include "dnshdr.h"
#include "dnsquery.h"
#include "dnsreply.h"
#include "dnsserver.h"
#include "io.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;
        default:              snprintf(text, size, "%d", recordtype); break;
    }
}

void DnsTick()
{
    DnsQueryTick();
}

int DnsHandlePacketReceived(int dnsProtocol, int* pSize, void* pPacket)
{
    DnsHdrSetup(pPacket);
    DnsHdrRead();
    int action;
    if (DnsHdrIsReply)
    {
        Led1 = 1;
        action = DnsReplyHandle(dnsProtocol, pSize);
        Led1 = 0;
    }
    else
    {
        Led2 = 1;
        action = DnsServerHandleQuery(dnsProtocol, pSize);
        Led2 = 0;
    }
    return action;
}

int DnsPollForPacketToSend(void* pPacket, int* pSize)
{
    DnsHdrSetup(pPacket);
    return DnsQueryPoll(pSize);
}