29
29
import adafruit_wiznet5k .adafruit_wiznet5k_socket as socket
30
30
from adafruit_wiznet5k .adafruit_wiznet5k_socket import htons
31
31
32
- QUERY_FLAG = const (0x00 )
33
- OPCODE_STANDARD_QUERY = const (0x00 )
34
- RECURSION_DESIRED_FLAG = 1 << 8
32
+ _QUERY_FLAG = const (0x00 )
33
+ _OPCODE_STANDARD_QUERY = const (0x00 )
34
+ _RECURSION_DESIRED_FLAG = 1 << 8
35
35
36
- TYPE_A = const (0x0001 )
37
- CLASS_IN = const (0x0001 )
38
- DATA_LEN = const (0x0004 )
36
+ _TYPE_A = const (0x0001 )
37
+ _CLASS_IN = const (0x0001 )
38
+ _DATA_LEN = const (0x0004 )
39
39
40
40
# Return codes for gethostbyname
41
- SUCCESS = const (1 )
42
- TIMED_OUT = const (- 1 )
43
- INVALID_SERVER = const (- 2 )
44
- TRUNCATED = const (- 3 )
45
- INVALID_RESPONSE = const (- 4 )
41
+ _SUCCESS = const (1 )
42
+ _TIMED_OUT = const (- 1 )
43
+ _INVALID_SERVER = const (- 2 )
44
+ _TRUNCATED = const (- 3 )
45
+ _INVALID_RESPONSE = const (- 4 )
46
46
47
- DNS_PORT = const (0x35 ) # port used for DNS request
47
+ _DNS_PORT = const (0x35 ) # port used for DNS request
48
48
49
49
50
50
class DNS :
@@ -81,15 +81,15 @@ def gethostbyname(self, hostname: bytes) -> Union[int, bytes]:
81
81
:return Union[int, bytes] The IPv4 address if successful, -1 otherwise.
82
82
"""
83
83
if self ._dns_server is None :
84
- return INVALID_SERVER
84
+ return _INVALID_SERVER
85
85
self ._host = hostname
86
86
# build DNS request packet
87
87
self ._build_dns_header ()
88
88
self ._build_dns_question ()
89
89
90
90
# Send DNS request packet
91
- self ._sock .bind ((None , DNS_PORT ))
92
- self ._sock .connect ((self ._dns_server , DNS_PORT ))
91
+ self ._sock .bind ((None , _DNS_PORT ))
92
+ self ._sock .connect ((self ._dns_server , _DNS_PORT ))
93
93
if self ._debug :
94
94
print ("* DNS: Sending request packet..." )
95
95
self ._sock .send (self ._pkt_buf )
@@ -176,15 +176,15 @@ def _parse_dns_response(
176
176
177
177
# Validate Query is Type A
178
178
q_type = int .from_bytes (self ._pkt_buf [ptr : ptr + 2 ], "big" )
179
- if not q_type == TYPE_A :
179
+ if not q_type == _TYPE_A :
180
180
if self ._debug :
181
181
print ("* DNS ERROR: Incorrect Query Type: " , q_type )
182
182
return - 1
183
183
ptr += 2
184
184
185
185
# Validate Query is Type A
186
186
q_class = int .from_bytes (self ._pkt_buf [ptr : ptr + 2 ], "big" )
187
- if not q_class == TYPE_A :
187
+ if not q_class == _TYPE_A :
188
188
if self ._debug :
189
189
print ("* DNS ERROR: Incorrect Query Class: " , q_class )
190
190
return - 1
@@ -201,15 +201,15 @@ def _parse_dns_response(
201
201
202
202
# Validate Answer Type A
203
203
ans_type = int .from_bytes (self ._pkt_buf [ptr : ptr + 2 ], "big" )
204
- if not ans_type == TYPE_A :
204
+ if not ans_type == _TYPE_A :
205
205
if self ._debug :
206
206
print ("* DNS ERROR: Incorrect Answer Type: " , ans_type )
207
207
return - 1
208
208
ptr += 2
209
209
210
210
# Validate Answer Class IN
211
211
ans_class = int .from_bytes (self ._pkt_buf [ptr : ptr + 2 ], "big" )
212
- if not ans_class == TYPE_A :
212
+ if not ans_class == _TYPE_A :
213
213
if self ._debug :
214
214
print ("* DNS ERROR: Incorrect Answer Class: " , ans_class )
215
215
return - 1
@@ -220,7 +220,7 @@ def _parse_dns_response(
220
220
221
221
# Validate addr is IPv4
222
222
data_len = int .from_bytes (self ._pkt_buf [ptr : ptr + 2 ], "big" )
223
- if not data_len == DATA_LEN :
223
+ if not data_len == _DATA_LEN :
224
224
if self ._debug :
225
225
print ("* DNS ERROR: Unexpected Data Length: " , data_len )
226
226
return - 1
@@ -267,8 +267,8 @@ def _build_dns_question(self) -> None:
267
267
# end of the name
268
268
self ._pkt_buf .append (0x00 )
269
269
# Type A record
270
- self ._pkt_buf .append (htons (TYPE_A ) & 0xFF )
271
- self ._pkt_buf .append (htons (TYPE_A ) >> 8 )
270
+ self ._pkt_buf .append (htons (_TYPE_A ) & 0xFF )
271
+ self ._pkt_buf .append (htons (_TYPE_A ) >> 8 )
272
272
# Class IN
273
- self ._pkt_buf .append (htons (CLASS_IN ) & 0xFF )
274
- self ._pkt_buf .append (htons (CLASS_IN ) >> 8 )
273
+ self ._pkt_buf .append (htons (_CLASS_IN ) & 0xFF )
274
+ self ._pkt_buf .append (htons (_CLASS_IN ) >> 8 )
0 commit comments