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@13:9cd54f7db57a, 2017-05-01 (annotated)
- Committer:
- andrewboyson
- Date:
- Mon May 01 18:20:55 2017 +0000
- Revision:
- 13:9cd54f7db57a
- Child:
- 15:6ca6778168b1
Added ability to read DNS queries with encoded IP addresses
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 13:9cd54f7db57a | 1 | #include "dns.h" |
andrewboyson | 13:9cd54f7db57a | 2 | #include "dnshdr.h" |
andrewboyson | 13:9cd54f7db57a | 3 | #include "dnsquery.h" |
andrewboyson | 13:9cd54f7db57a | 4 | #include "dnsreply.h" |
andrewboyson | 13:9cd54f7db57a | 5 | #include "dnsserver.h" |
andrewboyson | 13:9cd54f7db57a | 6 | #include "io.h" |
andrewboyson | 13:9cd54f7db57a | 7 | |
andrewboyson | 13:9cd54f7db57a | 8 | void DnsProtocolToString(uint8_t protocol, int size, char* text) |
andrewboyson | 13:9cd54f7db57a | 9 | { |
andrewboyson | 13:9cd54f7db57a | 10 | switch (protocol) |
andrewboyson | 13:9cd54f7db57a | 11 | { |
andrewboyson | 13:9cd54f7db57a | 12 | case DNS_PROTOCOL_UDNS: strncpy (text, "DNS", size); break; |
andrewboyson | 13:9cd54f7db57a | 13 | case DNS_PROTOCOL_MDNS: strncpy (text, "MDNS", size); break; |
andrewboyson | 13:9cd54f7db57a | 14 | case DNS_PROTOCOL_LLMNR: strncpy (text, "LLMNR", size); break; |
andrewboyson | 13:9cd54f7db57a | 15 | default: snprintf(text, size, "%d", protocol); break; |
andrewboyson | 13:9cd54f7db57a | 16 | } |
andrewboyson | 13:9cd54f7db57a | 17 | } |
andrewboyson | 13:9cd54f7db57a | 18 | |
andrewboyson | 13:9cd54f7db57a | 19 | void DnsRecordTypeToString(uint8_t recordtype, int size, char* text) |
andrewboyson | 13:9cd54f7db57a | 20 | { |
andrewboyson | 13:9cd54f7db57a | 21 | switch (recordtype) |
andrewboyson | 13:9cd54f7db57a | 22 | { |
andrewboyson | 13:9cd54f7db57a | 23 | case DNS_RECORD_A: strncpy (text, "A", size); break; |
andrewboyson | 13:9cd54f7db57a | 24 | case DNS_RECORD_AAAA: strncpy (text, "AAAA", size); break; |
andrewboyson | 13:9cd54f7db57a | 25 | case DNS_RECORD_PTR: strncpy (text, "PTR", size); break; |
andrewboyson | 13:9cd54f7db57a | 26 | default: snprintf(text, size, "%d", recordtype); break; |
andrewboyson | 13:9cd54f7db57a | 27 | } |
andrewboyson | 13:9cd54f7db57a | 28 | } |
andrewboyson | 13:9cd54f7db57a | 29 | |
andrewboyson | 13:9cd54f7db57a | 30 | void DnsTick() |
andrewboyson | 13:9cd54f7db57a | 31 | { |
andrewboyson | 13:9cd54f7db57a | 32 | DnsQueryTick(); |
andrewboyson | 13:9cd54f7db57a | 33 | } |
andrewboyson | 13:9cd54f7db57a | 34 | |
andrewboyson | 13:9cd54f7db57a | 35 | int DnsHandlePacketReceived(int dnsProtocol, int* pSize, void* pPacket) |
andrewboyson | 13:9cd54f7db57a | 36 | { |
andrewboyson | 13:9cd54f7db57a | 37 | DnsHdrSetup(pPacket); |
andrewboyson | 13:9cd54f7db57a | 38 | DnsHdrRead(); |
andrewboyson | 13:9cd54f7db57a | 39 | int action; |
andrewboyson | 13:9cd54f7db57a | 40 | if (DnsHdrIsReply) |
andrewboyson | 13:9cd54f7db57a | 41 | { |
andrewboyson | 13:9cd54f7db57a | 42 | Led1 = 1; |
andrewboyson | 13:9cd54f7db57a | 43 | action = DnsReplyHandle(dnsProtocol, pSize); |
andrewboyson | 13:9cd54f7db57a | 44 | Led1 = 0; |
andrewboyson | 13:9cd54f7db57a | 45 | } |
andrewboyson | 13:9cd54f7db57a | 46 | else |
andrewboyson | 13:9cd54f7db57a | 47 | { |
andrewboyson | 13:9cd54f7db57a | 48 | Led2 = 1; |
andrewboyson | 13:9cd54f7db57a | 49 | action = DnsServerHandleQuery(dnsProtocol, pSize); |
andrewboyson | 13:9cd54f7db57a | 50 | Led2 = 0; |
andrewboyson | 13:9cd54f7db57a | 51 | } |
andrewboyson | 13:9cd54f7db57a | 52 | return action; |
andrewboyson | 13:9cd54f7db57a | 53 | } |
andrewboyson | 13:9cd54f7db57a | 54 | |
andrewboyson | 13:9cd54f7db57a | 55 | int DnsPollForPacketToSend(void* pPacket, int* pSize) |
andrewboyson | 13:9cd54f7db57a | 56 | { |
andrewboyson | 13:9cd54f7db57a | 57 | DnsHdrSetup(pPacket); |
andrewboyson | 13:9cd54f7db57a | 58 | return DnsQueryPoll(pSize); |
andrewboyson | 13:9cd54f7db57a | 59 | } |