|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# (c) Copyright 2009-2010 MCQN Ltd |
| 4 | +# Modified by Brent Rubell for Adafruit Industries, 2020 |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in |
| 14 | +# all copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +# THE SOFTWARE. |
| 23 | +""" |
| 24 | +`adafruit_wiznet5k_dns` |
| 25 | +================================================================================ |
| 26 | +
|
| 27 | +Pure-Python implementation of the Arduino DNS client for WIZnet 5k-based |
| 28 | +ethernet modules. |
| 29 | +
|
| 30 | +* Author(s): MCQN Ltd, Brent Rubell |
| 31 | +
|
| 32 | +""" |
| 33 | +import time |
| 34 | +from random import getrandbits |
| 35 | +from micropython import const |
| 36 | +import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket |
| 37 | +from adafruit_wiznet5k.adafruit_wiznet5k_socket import htons |
| 38 | + |
| 39 | +# pylint: disable=bad-whitespace |
| 40 | + |
| 41 | +QUERY_FLAG = const(0x00) |
| 42 | +OPCODE_STANDARD_QUERY = const(0x00) |
| 43 | +RECURSION_DESIRED_FLAG = 1<<8 |
| 44 | + |
| 45 | +TYPE_A = const(0x0001) |
| 46 | +CLASS_IN = const(0x0001) |
| 47 | +DATA_LEN = const(0x0004) |
| 48 | + |
| 49 | +# Return codes for gethostbyname |
| 50 | +SUCCESS = const(1) |
| 51 | +TIMED_OUT = const(-1) |
| 52 | +INVALID_SERVER = const(-2) |
| 53 | +TRUNCATED = const(-3) |
| 54 | +INVALID_RESPONSE = const(-4) |
| 55 | + |
| 56 | +DNS_PORT = const(0x35) # port used for DNS request |
| 57 | +# pylint: enable=bad-whitespace |
| 58 | + |
| 59 | +class DNS: |
| 60 | + """W5K DNS implementation. |
| 61 | +
|
| 62 | + :param iface: Network interface |
| 63 | + """ |
| 64 | + def __init__(self, iface, dns_address): |
| 65 | + self._iface = iface |
| 66 | + socket.set_interface(iface) |
| 67 | + self._sock = socket.socket(type=socket.SOCK_DGRAM) |
| 68 | + self._sock.settimeout(1) |
| 69 | + |
| 70 | + self._dns_server = dns_address |
| 71 | + self._host = 0 |
| 72 | + self._request_id = 0 # request identifier |
| 73 | + self._pkt_buf = bytearray() |
| 74 | + |
| 75 | + def gethostbyname(self, hostname): |
| 76 | + """Translate a host name to IPv4 address format. |
| 77 | + :param str hostname: Desired host name to connect to. |
| 78 | +
|
| 79 | + Returns the IPv4 address as a bytearray if successful, -1 otherwise. |
| 80 | + """ |
| 81 | + if self._dns_server is None: |
| 82 | + return INVALID_SERVER |
| 83 | + self._host = hostname |
| 84 | + # build DNS request packet |
| 85 | + self._build_dns_header() |
| 86 | + self._build_dns_question() |
| 87 | + |
| 88 | + # Send DNS request packet |
| 89 | + self._sock.connect((self._dns_server, DNS_PORT)) |
| 90 | + self._sock.send(self._pkt_buf) |
| 91 | + |
| 92 | + # wait and retry 3 times for a response |
| 93 | + retries = 0 |
| 94 | + addr = -1 |
| 95 | + while (retries < 3) and (addr == -1): |
| 96 | + addr = self._parse_dns_response() |
| 97 | + retries += 1 |
| 98 | + |
| 99 | + self._sock.close() |
| 100 | + return addr |
| 101 | + |
| 102 | + def _parse_dns_response(self): |
| 103 | + """Receives and parses DNS query response. |
| 104 | + Returns desired hostname address if obtained, -1 otherwise. |
| 105 | +
|
| 106 | + """ |
| 107 | + # wait for a response |
| 108 | + start_time = time.monotonic() |
| 109 | + packet_sz = self._sock.available() |
| 110 | + while packet_sz <= 0: |
| 111 | + packet_sz = self._sock.available() |
| 112 | + if (time.monotonic() - start_time) > 1.0: |
| 113 | + # timed out! |
| 114 | + return -1 |
| 115 | + time.sleep(0.05) |
| 116 | + # store packet in buffer |
| 117 | + self._pkt_buf = self._sock.recv(packet_sz)[0] |
| 118 | + |
| 119 | + # Validate request identifier |
| 120 | + if not int.from_bytes(self._pkt_buf[0:2], 'l') == self._request_id: |
| 121 | + return -1 |
| 122 | + # Validate flags |
| 123 | + if not int.from_bytes(self._pkt_buf[2:4], 'l') == 0x8180: |
| 124 | + return -1 |
| 125 | + # Validate Answer RRs (>=1) |
| 126 | + an_count = int.from_bytes(self._pkt_buf[6:8], 'l') |
| 127 | + if not an_count >= 1: |
| 128 | + return -1 |
| 129 | + |
| 130 | + # iterate over ANCOUNT since answer may not be type A |
| 131 | + while an_count > 0: |
| 132 | + ans_type = int.from_bytes(self._pkt_buf[41:43], 'l') |
| 133 | + ans_class = int.from_bytes(self._pkt_buf[43:45], 'l') |
| 134 | + ans_len = int.from_bytes(self._pkt_buf[49:51], 'l') |
| 135 | + if ans_type == TYPE_A and ans_class == CLASS_IN: |
| 136 | + if ans_len != 4: |
| 137 | + # invalid size ret.'d |
| 138 | + return -1 |
| 139 | + # return the address |
| 140 | + return self._pkt_buf[51:55] |
| 141 | + # not the correct answer type or class |
| 142 | + an_count += 1 |
| 143 | + |
| 144 | + def _build_dns_header(self): |
| 145 | + """Builds DNS header.""" |
| 146 | + # generate a random, 16-bit, request identifier |
| 147 | + self._request_id = getrandbits(16) |
| 148 | + |
| 149 | + # ID, 16-bit identifier |
| 150 | + self._pkt_buf.append(self._request_id >> 8) |
| 151 | + self._pkt_buf.append(self._request_id & 0xFF) |
| 152 | + |
| 153 | + # Flags (0x0100) |
| 154 | + self._pkt_buf.append(0x01) |
| 155 | + self._pkt_buf.append(0x00) |
| 156 | + |
| 157 | + # QDCOUNT |
| 158 | + self._pkt_buf.append(0x00) |
| 159 | + self._pkt_buf.append(0x01) |
| 160 | + # ANCOUNT |
| 161 | + self._pkt_buf.append(0x00) |
| 162 | + self._pkt_buf.append(0x00) |
| 163 | + # NSCOUNT |
| 164 | + self._pkt_buf.append(0x00) |
| 165 | + self._pkt_buf.append(0x00) |
| 166 | + #ARCOUNT |
| 167 | + self._pkt_buf.append(0x00) |
| 168 | + self._pkt_buf.append(0x00) |
| 169 | + |
| 170 | + def _build_dns_question(self): |
| 171 | + """Build DNS question""" |
| 172 | + host = self._host.decode('utf-8') |
| 173 | + host = host.split(".") |
| 174 | + # write out each section of host |
| 175 | + for i, _ in enumerate(host): |
| 176 | + # append the sz of the section |
| 177 | + self._pkt_buf.append(len(host[i])) |
| 178 | + # append the section data |
| 179 | + self._pkt_buf += host[i] |
| 180 | + # end of the name |
| 181 | + self._pkt_buf.append(0x00) |
| 182 | + # Type A record |
| 183 | + self._pkt_buf.append(htons(TYPE_A) & 0xFF) |
| 184 | + self._pkt_buf.append(htons(TYPE_A) >> 8) |
| 185 | + # Class IN |
| 186 | + self._pkt_buf.append(htons(CLASS_IN) & 0xFF) |
| 187 | + self._pkt_buf.append(htons(CLASS_IN) >> 8) |
0 commit comments