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.c@170:96c637dc3f52, 2020-12-09 (annotated)
- Committer:
- andrewboyson
- Date:
- Wed Dec 09 18:11:05 2020 +0000
- Revision:
- 170:96c637dc3f52
- Parent:
- 128:79052cb4a41c
- Child:
- 171:f708d6776752
Tidied up mdns messages
Who changed what in which revision?
User | Revision | Line number | New 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 | 170:96c637dc3f52 | 63 | int DnsGetNextProtocol(int protocol) |
andrewboyson | 41:db727df5f98b | 64 | { |
andrewboyson | 41:db727df5f98b | 65 | switch(protocol) |
andrewboyson | 41:db727df5f98b | 66 | { |
andrewboyson | 41:db727df5f98b | 67 | case DNS_PROTOCOL_NONE: return DNS_PROTOCOL_MDNS; |
andrewboyson | 41:db727df5f98b | 68 | case DNS_PROTOCOL_MDNS: return DNS_PROTOCOL_LLMNR; |
andrewboyson | 41:db727df5f98b | 69 | case DNS_PROTOCOL_LLMNR: return DNS_PROTOCOL_UDNS; |
andrewboyson | 41:db727df5f98b | 70 | case DNS_PROTOCOL_UDNS: return DNS_PROTOCOL_NONE; |
andrewboyson | 41:db727df5f98b | 71 | default: LogTimeF("DNS invalid protocol %d\r\n", protocol); return DNS_PROTOCOL_NONE; |
andrewboyson | 41:db727df5f98b | 72 | } |
andrewboyson | 41:db727df5f98b | 73 | } |
andrewboyson | 13:9cd54f7db57a | 74 | |
andrewboyson | 43:bc028d5a6424 | 75 | void DnsMain() |
andrewboyson | 13:9cd54f7db57a | 76 | { |
andrewboyson | 43:bc028d5a6424 | 77 | DnsQueryMain(); |
andrewboyson | 13:9cd54f7db57a | 78 | } |
andrewboyson | 13:9cd54f7db57a | 79 | |
andrewboyson | 59:e0e556c8bd46 | 80 | int DnsHandlePacketReceived(void (*traceback)(void), int dnsProtocol, int sizeRx, void* pPacketRx, int* pSizeTx, void* pPacketTx) |
andrewboyson | 13:9cd54f7db57a | 81 | { |
andrewboyson | 59:e0e556c8bd46 | 82 | DnsHdrSetup(pPacketRx, sizeRx); |
andrewboyson | 13:9cd54f7db57a | 83 | DnsHdrRead(); |
andrewboyson | 59:e0e556c8bd46 | 84 | |
andrewboyson | 13:9cd54f7db57a | 85 | int action; |
andrewboyson | 13:9cd54f7db57a | 86 | if (DnsHdrIsReply) |
andrewboyson | 13:9cd54f7db57a | 87 | { |
andrewboyson | 59:e0e556c8bd46 | 88 | action = DnsReplyHandle(traceback, dnsProtocol); |
andrewboyson | 13:9cd54f7db57a | 89 | } |
andrewboyson | 13:9cd54f7db57a | 90 | else |
andrewboyson | 13:9cd54f7db57a | 91 | { |
andrewboyson | 59:e0e556c8bd46 | 92 | action = DnsServerHandleQuery(traceback, dnsProtocol, pPacketTx, pSizeTx); |
andrewboyson | 13:9cd54f7db57a | 93 | } |
andrewboyson | 13:9cd54f7db57a | 94 | return action; |
andrewboyson | 13:9cd54f7db57a | 95 | } |
andrewboyson | 13:9cd54f7db57a | 96 | |
andrewboyson | 59:e0e556c8bd46 | 97 | int DnsPollForPacketToSend(void* pPacketTx, int* pSize) |
andrewboyson | 13:9cd54f7db57a | 98 | { |
andrewboyson | 59:e0e556c8bd46 | 99 | return DnsQueryPoll(pPacketTx, pSize); |
andrewboyson | 13:9cd54f7db57a | 100 | } |