Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
udp/dns/dnshdr.cpp@35:93c39d260a83, 2017-09-22 (annotated)
- Committer:
- andrewboyson
- Date:
- Fri Sep 22 13:55:56 2017 +0000
- Revision:
- 35:93c39d260a83
- Parent:
- 33:714a0345e59b
- Child:
- 37:793b39683406
Added name resolution and address resolution
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 13:9cd54f7db57a | 1 | #include "mbed.h" |
andrewboyson | 13:9cd54f7db57a | 2 | #include "dns.h" |
andrewboyson | 13:9cd54f7db57a | 3 | #include "net.h" |
andrewboyson | 13:9cd54f7db57a | 4 | #include "log.h" |
andrewboyson | 14:e75a59c1123d | 5 | #include "net.h" |
andrewboyson | 14:e75a59c1123d | 6 | #include "eth.h" |
andrewboyson | 14:e75a59c1123d | 7 | #include "ip4.h" |
andrewboyson | 14:e75a59c1123d | 8 | #include "ip6.h" |
andrewboyson | 14:e75a59c1123d | 9 | #include "udp.h" |
andrewboyson | 13:9cd54f7db57a | 10 | |
andrewboyson | 13:9cd54f7db57a | 11 | #define HEADER_LENGTH 12 |
andrewboyson | 13:9cd54f7db57a | 12 | |
andrewboyson | 13:9cd54f7db57a | 13 | __packed struct header |
andrewboyson | 13:9cd54f7db57a | 14 | { |
andrewboyson | 13:9cd54f7db57a | 15 | uint16_t id; |
andrewboyson | 13:9cd54f7db57a | 16 | uint8_t uflags; |
andrewboyson | 13:9cd54f7db57a | 17 | uint8_t lflags; |
andrewboyson | 13:9cd54f7db57a | 18 | |
andrewboyson | 13:9cd54f7db57a | 19 | uint16_t qdcount; |
andrewboyson | 13:9cd54f7db57a | 20 | uint16_t ancount; |
andrewboyson | 13:9cd54f7db57a | 21 | uint16_t nscount; |
andrewboyson | 13:9cd54f7db57a | 22 | uint16_t arcount; |
andrewboyson | 13:9cd54f7db57a | 23 | }; |
andrewboyson | 13:9cd54f7db57a | 24 | |
andrewboyson | 13:9cd54f7db57a | 25 | uint16_t DnsHdrId; |
andrewboyson | 13:9cd54f7db57a | 26 | |
andrewboyson | 13:9cd54f7db57a | 27 | bool DnsHdrIsReply; |
andrewboyson | 13:9cd54f7db57a | 28 | bool DnsHdrIsAuthoritative; |
andrewboyson | 13:9cd54f7db57a | 29 | bool DnsHdrIsRecursiveQuery; |
andrewboyson | 13:9cd54f7db57a | 30 | |
andrewboyson | 13:9cd54f7db57a | 31 | uint16_t DnsHdrQdcount; |
andrewboyson | 13:9cd54f7db57a | 32 | uint16_t DnsHdrAncount; |
andrewboyson | 13:9cd54f7db57a | 33 | uint16_t DnsHdrNscount; |
andrewboyson | 13:9cd54f7db57a | 34 | uint16_t DnsHdrArcount; |
andrewboyson | 13:9cd54f7db57a | 35 | |
andrewboyson | 13:9cd54f7db57a | 36 | char* DnsHdrPacket; |
andrewboyson | 13:9cd54f7db57a | 37 | char* DnsHdrData; |
andrewboyson | 13:9cd54f7db57a | 38 | |
andrewboyson | 13:9cd54f7db57a | 39 | void DnsHdrSetup(void* pPacket) |
andrewboyson | 13:9cd54f7db57a | 40 | { |
andrewboyson | 13:9cd54f7db57a | 41 | DnsHdrPacket = (char*)pPacket; |
andrewboyson | 13:9cd54f7db57a | 42 | DnsHdrData = DnsHdrPacket + HEADER_LENGTH; |
andrewboyson | 13:9cd54f7db57a | 43 | } |
andrewboyson | 13:9cd54f7db57a | 44 | |
andrewboyson | 13:9cd54f7db57a | 45 | void DnsHdrRead() |
andrewboyson | 13:9cd54f7db57a | 46 | { |
andrewboyson | 13:9cd54f7db57a | 47 | struct header* pHeader = (header*)DnsHdrPacket; |
andrewboyson | 13:9cd54f7db57a | 48 | |
andrewboyson | 13:9cd54f7db57a | 49 | DnsHdrId = NetToHost16(pHeader->id); |
andrewboyson | 13:9cd54f7db57a | 50 | |
andrewboyson | 13:9cd54f7db57a | 51 | uint8_t uflags = pHeader->uflags; |
andrewboyson | 13:9cd54f7db57a | 52 | DnsHdrIsReply = uflags & 0x80; |
andrewboyson | 13:9cd54f7db57a | 53 | DnsHdrIsAuthoritative = uflags & 0x04; |
andrewboyson | 33:714a0345e59b | 54 | DnsHdrIsRecursiveQuery = uflags & 0x01; |
andrewboyson | 13:9cd54f7db57a | 55 | |
andrewboyson | 13:9cd54f7db57a | 56 | DnsHdrQdcount = NetToHost16(pHeader->qdcount); |
andrewboyson | 13:9cd54f7db57a | 57 | DnsHdrAncount = NetToHost16(pHeader->ancount); |
andrewboyson | 13:9cd54f7db57a | 58 | DnsHdrNscount = NetToHost16(pHeader->nscount); |
andrewboyson | 13:9cd54f7db57a | 59 | DnsHdrArcount = NetToHost16(pHeader->arcount); |
andrewboyson | 13:9cd54f7db57a | 60 | |
andrewboyson | 13:9cd54f7db57a | 61 | } |
andrewboyson | 13:9cd54f7db57a | 62 | void DnsHdrWrite() |
andrewboyson | 13:9cd54f7db57a | 63 | { |
andrewboyson | 13:9cd54f7db57a | 64 | struct header* pHeader = (header*)DnsHdrPacket; |
andrewboyson | 13:9cd54f7db57a | 65 | |
andrewboyson | 13:9cd54f7db57a | 66 | pHeader->id = NetToHost16(DnsHdrId); |
andrewboyson | 13:9cd54f7db57a | 67 | |
andrewboyson | 13:9cd54f7db57a | 68 | uint8_t uflags = 0; |
andrewboyson | 13:9cd54f7db57a | 69 | uint8_t lflags = 0; |
andrewboyson | 13:9cd54f7db57a | 70 | if (DnsHdrIsReply) uflags |= 0x80; |
andrewboyson | 13:9cd54f7db57a | 71 | if (DnsHdrIsAuthoritative) uflags |= 0x04; |
andrewboyson | 13:9cd54f7db57a | 72 | if (DnsHdrIsRecursiveQuery) uflags |= 0x01; |
andrewboyson | 13:9cd54f7db57a | 73 | pHeader->uflags = uflags; |
andrewboyson | 13:9cd54f7db57a | 74 | pHeader->lflags = lflags; |
andrewboyson | 13:9cd54f7db57a | 75 | |
andrewboyson | 13:9cd54f7db57a | 76 | pHeader->qdcount = NetToHost16(DnsHdrQdcount); |
andrewboyson | 13:9cd54f7db57a | 77 | pHeader->ancount = NetToHost16(DnsHdrAncount); |
andrewboyson | 13:9cd54f7db57a | 78 | pHeader->nscount = NetToHost16(DnsHdrNscount); |
andrewboyson | 13:9cd54f7db57a | 79 | pHeader->arcount = NetToHost16(DnsHdrArcount); |
andrewboyson | 13:9cd54f7db57a | 80 | } |
andrewboyson | 13:9cd54f7db57a | 81 | void DnsHdrLog(char* title, int protocol) |
andrewboyson | 13:9cd54f7db57a | 82 | { |
andrewboyson | 14:e75a59c1123d | 83 | char text[100]; |
andrewboyson | 15:6ca6778168b1 | 84 | DnsProtocolToString(protocol, sizeof(text), text); |
andrewboyson | 15:6ca6778168b1 | 85 | LogTimeF("%s %s\r\n", text, title); |
andrewboyson | 14:e75a59c1123d | 86 | if (EthProtocol == IPV6) |
andrewboyson | 14:e75a59c1123d | 87 | { |
andrewboyson | 35:93c39d260a83 | 88 | Log(" IP: src "); Ip6LogHeaderSrc(); Log(" dst "); Ip6LogHeaderDst(); Log("\r\n"); |
andrewboyson | 14:e75a59c1123d | 89 | } |
andrewboyson | 14:e75a59c1123d | 90 | else if (EthProtocol == IPV4) |
andrewboyson | 14:e75a59c1123d | 91 | { |
andrewboyson | 35:93c39d260a83 | 92 | Log(" IP: src "); Ip4LogHeaderSrc(); Log(" dst "); Ip4LogHeaderDst(); Log("\r\n"); |
andrewboyson | 14:e75a59c1123d | 93 | } |
andrewboyson | 14:e75a59c1123d | 94 | else |
andrewboyson | 14:e75a59c1123d | 95 | { |
andrewboyson | 14:e75a59c1123d | 96 | LogF(" Unknown eth protocol %04hX\r\n", EthProtocol); |
andrewboyson | 14:e75a59c1123d | 97 | } |
andrewboyson | 15:6ca6778168b1 | 98 | LogF(" Port: src %5hu; dst %5hu\r\n", UdpSrcPort, UdpDstPort); |
andrewboyson | 13:9cd54f7db57a | 99 | LogF(" Ident %hd\r\n", DnsHdrId); |
andrewboyson | 15:6ca6778168b1 | 100 | if (DnsHdrIsReply) |
andrewboyson | 15:6ca6778168b1 | 101 | { |
andrewboyson | 15:6ca6778168b1 | 102 | if (DnsHdrIsAuthoritative) LogF(" Authoritative reply\r\n"); |
andrewboyson | 15:6ca6778168b1 | 103 | else LogF(" Non authoritative reply\r\n"); |
andrewboyson | 15:6ca6778168b1 | 104 | } |
andrewboyson | 15:6ca6778168b1 | 105 | else |
andrewboyson | 15:6ca6778168b1 | 106 | { |
andrewboyson | 15:6ca6778168b1 | 107 | if (DnsHdrIsRecursiveQuery) LogF(" Recursive query\r\n"); |
andrewboyson | 15:6ca6778168b1 | 108 | else LogF(" Non recursive query\r\n"); |
andrewboyson | 15:6ca6778168b1 | 109 | } |
andrewboyson | 14:e75a59c1123d | 110 | LogF(" qd, an, ns, ar %hu, %hu, %hu, %hu\r\n", DnsHdrQdcount, DnsHdrAncount, DnsHdrNscount, DnsHdrArcount); |
andrewboyson | 13:9cd54f7db57a | 111 | } |
andrewboyson | 13:9cd54f7db57a | 112 |