Skip to content

Commit 40cc29d

Browse files
author
brentru
committed
merge in newer, working dns impl.
1 parent f20cb39 commit 40cc29d

File tree

4 files changed

+266
-25
lines changed

4 files changed

+266
-25
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
from adafruit_bus_device.spi_device import SPIDevice
4949
import adafruit_wiznet5k.adafruit_wiznet5k_dhcp as dhcp
50+
import adafruit_wiznet5k.adafruit_wiznet5k_dns as dns
5051

5152

5253
__version__ = "0.0.0-auto.0"
@@ -206,14 +207,28 @@ def set_dhcp(self, response_timeout=1):
206207
_gw_addr = (_dhcp_client.gateway_ip[0], _dhcp_client.gateway_ip[1],
207208
_dhcp_client.gateway_ip[2], _dhcp_client.gateway_ip[3])
208209

209-
_dns_addr = (_dhcp_client.dns_server_ip[0], _dhcp_client.dns_server_ip[1],
210-
_dhcp_client.dns_server_ip[2], _dhcp_client.dns_server_ip[3])
211-
self.ifconfig = ((_ip, _subnet_mask, _gw_addr, _dns_addr))
210+
self._dns = (_dhcp_client.dns_server_ip[0], _dhcp_client.dns_server_ip[1],
211+
_dhcp_client.dns_server_ip[2], _dhcp_client.dns_server_ip[3])
212+
self.ifconfig = ((_ip, _subnet_mask, _gw_addr, self._dns))
212213
return 0
213-
# Reset SRC_Port
214214
self._src_port = 0
215215
return 1
216216

217+
def get_host_by_name(self, hostname):
218+
"""Convert a hostname to a packed 4-byte IP Address.
219+
Returns a 4 bytearray.
220+
"""
221+
if self._debug:
222+
print("* Get host by name")
223+
if isinstance(hostname, str):
224+
hostname = bytes(hostname, 'utf-8')
225+
self._src_port = int(time.monotonic())
226+
# Return IP assigned by DHCP
227+
_dns_client = dns.DNS(self, self._dns)
228+
ret = _dns_client.gethostbyname(hostname)
229+
self._src_port = 0
230+
return ret
231+
217232
@property
218233
def max_sockets(self):
219234
"""Returns max number of sockets supported by chip."""
@@ -289,10 +304,11 @@ def remote_port(self):
289304
def ifconfig(self):
290305
"""Returns the network configuration as a tuple."""
291306
# set subnet and gateway addresses
307+
self._pbuff = bytearray(8)
292308
for octet in range(0, 4):
293309
self._pbuff += self.read(REG_SUBR+octet, 0x04)
294310
for octet in range(0, 4):
295-
self._pbuff[3+octet] += self.read(REG_GAR+octet, 0x04)
311+
self._pbuff += self.read(REG_GAR+octet, 0x04)
296312

297313
params = (self.ip_address, self._pbuff[0:3], self._pbuff[3:7], self._dns)
298314
return params
@@ -430,7 +446,6 @@ def socket_available(self, socket_num, sock_type=SNMR_TCP):
430446
"""
431447
if self._debug:
432448
print("* socket_available called with protocol", sock_type)
433-
print("* #", socket_num)
434449
assert socket_num <= self.max_sockets, "Provided socket exceeds max_sockets."
435450

436451
if sock_type == 0x02:
@@ -490,6 +505,7 @@ def socket_connect(self, socket_num, dest, port, conn_mode=SNMR_TCP):
490505
self._read_sncr(socket_num)
491506
# wait for tcp connection establishment
492507
while self.socket_status(socket_num)[0] != SNSR_SOCK_ESTABLISHED:
508+
print(self.socket_status(socket_num)[0])
493509
if self.socket_status(socket_num)[0] == SNSR_SOCK_CLOSED:
494510
raise RuntimeError('Failed to establish connection.')
495511
time.sleep(1)
@@ -556,15 +572,18 @@ def socket_open(self, socket_num, dest, port, conn_mode=SNMR_TCP):
556572
return 1
557573

558574
def socket_close(self, socket_num):
559-
"""Closes a socket.
560-
561-
"""
562-
assert self.link_status, "Ethernet cable disconnected!"
575+
"""Closes a socket."""
563576
if self._debug:
564577
print("*** Closing socket #%d" % socket_num)
565578
self._write_sncr(socket_num, CMD_SOCK_CLOSE)
566579
self._read_sncr(socket_num)
567-
self._write_snir(socket_num, 0xFF)
580+
581+
def socket_disconnect(self, socket_num):
582+
"""Disconnect a TCP connection."""
583+
if self._debug:
584+
print("*** Disconnecting socket #%d" % socket_num)
585+
self._write_sncr(socket_num, CMD_SOCK_DISCON)
586+
self._read_sncr(socket_num)
568587

569588
def socket_read(self, socket_num, length):
570589
"""Reads data from a socket into a buffer.
@@ -673,7 +692,7 @@ def socket_write(self, socket_num, buffer):
673692
# check data was transferred correctly
674693
while(self._read_socket(socket_num, REG_SNIR)[0] & SNIR_SEND_OK) != SNIR_SEND_OK:
675694
if self.socket_status(socket_num) == SNSR_SOCK_CLOSED:
676-
self.socket_close(socket_num)
695+
#self.socket_close(socket_num)
677696
return 0
678697
time.sleep(0.01)
679698

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
DHCP_SERVER_PORT = const(67)
7373
# DHCP Lease Time, in seconds
7474
DEFAULT_LEASE_TIME = const(900)
75-
BROADCAST_SERVER_ADDR = 255, 255, 255, 255
75+
BROADCAST_SERVER_ADDR = (('255.255.255.255'))
7676

7777
# pylint: enable=bad-whitespace
7878
_BUFF = bytearray(317)
@@ -120,7 +120,7 @@ def __init__(self, eth, mac_address, timeout=1, timeout_response=1):
120120
def send_dhcp_message(self, state, time_elapsed):
121121
"""Assemble and send a DHCP message packet to a socket.
122122
:param int state: DHCP Message state.
123-
:param float time_elapsed: Number of seconds elapsed since client attempted to renew lease.
123+
:param float time_elapsed: Number of seconds elapsed since client attempted to acquire/renew a lease.
124124
125125
"""
126126
# OP
@@ -264,8 +264,10 @@ def parse_dhcp_response(self, response_timeout):
264264
self._t2 = int.from_bytes(_BUFF[263:267], 'l')
265265
# Subnet Mask
266266
self.subnet_mask = _BUFF[269:273]
267+
print(_BUFF)
268+
#time.sleep(100)
267269
# DNS Server
268-
self.dns_server_ip = _BUFF[285:289]
270+
self.dns_server_ip = _BUFF[281:285]
269271

270272
return msg_type, xid
271273

@@ -322,4 +324,6 @@ def request_dhcp_lease(self):
322324

323325
self._transaction_id += 1
324326
self._last_check_lease_ms = time.monotonic()
327+
# close the socket, we're done with it
328+
self._sock.close()
325329
return result
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
QUERY_FLAG = const(0x00)
40+
OPCODE_STANDARD_QUERY = const(0x00)
41+
RECURSION_DESIRED_FLAG = 1<<8
42+
43+
TYPE_A = const(0x0001)
44+
CLASS_IN = const(0x0001)
45+
DATA_LEN = const(0x0004)
46+
47+
# Return codes for gethostbyname
48+
SUCCESS = const(1)
49+
TIMED_OUT = const(-1)
50+
INVALID_SERVER = const(-2)
51+
TRUNCATED = const(-3)
52+
INVALID_RESPONSE = const(-4)
53+
54+
DNS_PORT = const(0x35) # port used for DNS request
55+
56+
class DNS:
57+
"""W5K DNS implementation.
58+
59+
:param iface: Network interface
60+
"""
61+
def __init__(self, iface, dns_address):
62+
self._iface = iface
63+
socket.set_interface(iface)
64+
self._sock = socket.socket(type=socket.SOCK_DGRAM)
65+
self._sock.settimeout(1)
66+
67+
self._dns_server = dns_address
68+
self._host = 0
69+
self._request_id = 0 # request identifier
70+
self._pkt_buf = bytearray()
71+
72+
def gethostbyname(self, hostname):
73+
"""Translate a host name to IPv4 address format.
74+
:param str hostname: Desired host name to connect to.
75+
76+
Returns the IPv4 address as a bytearray if successful, -1 otherwise.
77+
"""
78+
if self._dns_server is None:
79+
return INVALID_SERVER
80+
self._host = hostname
81+
# build DNS request packet
82+
self._build_dns_header()
83+
self._build_dns_question()
84+
85+
# Send DNS request packet
86+
self._sock.connect((self._dns_server, DNS_PORT))
87+
self._sock.send(self._pkt_buf)
88+
89+
# wait and retry 3 times for a response
90+
retries = 0
91+
addr = -1
92+
while (retries < 3) and (addr == -1):
93+
addr = self._parse_dns_response()
94+
retries+=1
95+
96+
self._sock.close()
97+
return addr
98+
99+
def _parse_dns_response(self):
100+
"""Receives and parses DNS query response.
101+
Returns desired hostname address if obtained, -1 otherwise.
102+
103+
"""
104+
# wait for a response
105+
start_time = time.monotonic()
106+
packet_sz = self._sock.available()
107+
while packet_sz <= 0:
108+
packet_sz = self._sock.available()
109+
if (time.monotonic() - start_time) > 1.0:
110+
# timed out!
111+
return -1
112+
time.sleep(0.05)
113+
# store packet in buffer
114+
self._pkt_buf = self._sock.recv(packet_sz)[0]
115+
116+
# Validate request identifier
117+
if not hex(int.from_bytes(self._pkt_buf[0:2], 'l')) == hex(self._request_id):
118+
return -1
119+
120+
# Validate flags
121+
if not int.from_bytes(self._pkt_buf[2:4], 'l') == 0x8180:
122+
return -1
123+
# Validate Answer RRs (>=1)
124+
an_count = int.from_bytes(self._pkt_buf[6:8], 'l')
125+
if not an_count >= 1:
126+
return -1
127+
128+
# iterate over ANCOUNT since answer may not be type A
129+
while an_count > 0:
130+
ans_type = int.from_bytes(self._pkt_buf[41:43], 'l')
131+
ans_class = int.from_bytes(self._pkt_buf[43:45], 'l')
132+
ans_len = int.from_bytes(self._pkt_buf[49:51], 'l')
133+
if ans_type == TYPE_A and ans_class == CLASS_IN:
134+
if ans_len != 4:
135+
# invalid size ret.'d
136+
return -1
137+
# return the address
138+
return self._pkt_buf[51:55]
139+
else:
140+
# not the correct answer type or class
141+
an_count+=1
142+
143+
return -1
144+
145+
def _build_dns_header(self):
146+
"""Builds DNS header."""
147+
# generate a random, 16-bit, request identifier
148+
self._request_id = getrandbits(16)
149+
150+
# ID, 16-bit identifier
151+
self._pkt_buf.append(self._request_id >> 8)
152+
self._pkt_buf.append(self._request_id & 0xFF)
153+
154+
# Flags (0x0100)
155+
self._pkt_buf.append(0x01)
156+
self._pkt_buf.append(0x00)
157+
158+
# QDCOUNT
159+
self._pkt_buf.append(0x00)
160+
self._pkt_buf.append(0x01)
161+
# ANCOUNT
162+
self._pkt_buf.append(0x00)
163+
self._pkt_buf.append(0x00)
164+
# NSCOUNT
165+
self._pkt_buf.append(0x00)
166+
self._pkt_buf.append(0x00)
167+
#ARCOUNT
168+
self._pkt_buf.append(0x00)
169+
self._pkt_buf.append(0x00)
170+
171+
def _build_dns_question(self):
172+
"""Build DNS question"""
173+
host = self._host.decode('utf-8')
174+
host = host.split(".")
175+
# write out each section of host
176+
for i, _ in enumerate(host):
177+
# append the sz of the section
178+
self._pkt_buf.append(len(host[i]))
179+
# append the section data
180+
self._pkt_buf += host[i]
181+
# end of the name
182+
self._pkt_buf.append(0x00)
183+
# Type A record
184+
self._pkt_buf.append(htons(TYPE_A) & 0xFF)
185+
self._pkt_buf.append(htons(TYPE_A) >> 8)
186+
# Class IN
187+
self._pkt_buf.append(htons(CLASS_IN) & 0xFF)
188+
self._pkt_buf.append(htons(CLASS_IN) >> 8)
189+
190+

0 commit comments

Comments
 (0)