Skip to content

Commit 5f501fd

Browse files
authored
Merge pull request #64 from alexsporn/fix/endiannes
Fixed the usage of int.from_bytes and int.to_bytes
2 parents c1cc170 + 89bf29a commit 5f501fd

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ def _get_rx_rcv_size(self, sock):
859859
val_1 = self._read_snrx_rsr(sock)
860860
if val_1 != 0:
861861
val = self._read_snrx_rsr(sock)
862-
return int.from_bytes(val, "b")
862+
return int.from_bytes(val, "big")
863863

864864
def _get_tx_free_size(self, sock):
865865
"""Get free size of sock's tx buffer block."""
@@ -869,7 +869,7 @@ def _get_tx_free_size(self, sock):
869869
val_1 = self._read_sntx_fsr(sock)
870870
if val_1 != 0:
871871
val = self._read_sntx_fsr(sock)
872-
return int.from_bytes(val, "b")
872+
return int.from_bytes(val, "big")
873873

874874
def _read_snrx_rd(self, sock):
875875
self._pbuff[0] = self._read_socket(sock, REG_SNRX_RD)[0]

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def send_dhcp_message(self, state, time_elapsed, renew=False):
152152

153153
# Transaction ID (xid)
154154
self._initial_xid = htonl(self._transaction_id)
155-
self._initial_xid = self._initial_xid.to_bytes(4, "l")
155+
self._initial_xid = self._initial_xid.to_bytes(4, "big")
156156
_BUFF[4:7] = self._initial_xid
157157

158158
# seconds elapsed
@@ -161,7 +161,7 @@ def send_dhcp_message(self, state, time_elapsed, renew=False):
161161

162162
# flags
163163
flags = htons(0x8000)
164-
flags = flags.to_bytes(2, "b")
164+
flags = flags.to_bytes(2, "big")
165165
_BUFF[10] = flags[1]
166166
_BUFF[11] = flags[0]
167167

@@ -259,7 +259,7 @@ def parse_dhcp_response(self):
259259
if _BUFF[28:34] == 0:
260260
return 0, 0
261261

262-
if int.from_bytes(_BUFF[235:240], "l") != MAGIC_COOKIE:
262+
if int.from_bytes(_BUFF[235:240], "big") != MAGIC_COOKIE:
263263
return 0, 0
264264

265265
# -- Parse Packet, VARIABLE -- #
@@ -287,7 +287,7 @@ def parse_dhcp_response(self):
287287
ptr += 1
288288
opt_len = _BUFF[ptr]
289289
ptr += 1
290-
self._lease_time = int.from_bytes(_BUFF[ptr : ptr + opt_len], "l")
290+
self._lease_time = int.from_bytes(_BUFF[ptr : ptr + opt_len], "big")
291291
ptr += opt_len
292292
elif _BUFF[ptr] == ROUTERS_ON_SUBNET:
293293
ptr += 1
@@ -305,13 +305,13 @@ def parse_dhcp_response(self):
305305
ptr += 1
306306
opt_len = _BUFF[ptr]
307307
ptr += 1
308-
self._t1 = int.from_bytes(_BUFF[ptr : ptr + opt_len], "l")
308+
self._t1 = int.from_bytes(_BUFF[ptr : ptr + opt_len], "big")
309309
ptr += opt_len
310310
elif _BUFF[ptr] == T2_VAL:
311311
ptr += 1
312312
opt_len = _BUFF[ptr]
313313
ptr += 1
314-
self._t2 = int.from_bytes(_BUFF[ptr : ptr + opt_len], "l")
314+
self._t2 = int.from_bytes(_BUFF[ptr : ptr + opt_len], "big")
315315
ptr += opt_len
316316
elif _BUFF[ptr] == 0:
317317
break
@@ -399,7 +399,7 @@ def _dhcp_state_machine(self):
399399
if msg_type == DHCP_OFFER:
400400
# Check if transaction ID matches, otherwise it may be an offer
401401
# for another device
402-
if htonl(self._transaction_id) == int.from_bytes(xid, "l"):
402+
if htonl(self._transaction_id) == int.from_bytes(xid, "big"):
403403
if self._debug:
404404
print(
405405
"* DHCP: Send request to {}".format(self.dhcp_server_ip)
@@ -423,7 +423,7 @@ def _dhcp_state_machine(self):
423423
msg_type, xid = self.parse_dhcp_response()
424424
# Check if transaction ID matches, otherwise it may be
425425
# for another device
426-
if htonl(self._transaction_id) == int.from_bytes(xid, "l"):
426+
if htonl(self._transaction_id) == int.from_bytes(xid, "big"):
427427
if msg_type == DHCP_ACK:
428428
if self._debug:
429429
print("* DHCP: Successful lease")

adafruit_wiznet5k/adafruit_wiznet5k_dns.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def _parse_dns_response(
113113
print("DNS Packet Received: ", self._pkt_buf)
114114

115115
# Validate request identifier
116-
xid = int.from_bytes(self._pkt_buf[0:2], "l")
116+
xid = int.from_bytes(self._pkt_buf[0:2], "big")
117117
if not xid == self._request_id:
118118
if self._debug:
119119
print(
@@ -124,19 +124,19 @@ def _parse_dns_response(
124124
)
125125
return -1
126126
# Validate flags
127-
flags = int.from_bytes(self._pkt_buf[2:4], "l")
127+
flags = int.from_bytes(self._pkt_buf[2:4], "big")
128128
if not flags in (0x8180, 0x8580):
129129
if self._debug:
130130
print("* DNS ERROR: Invalid flags, ", flags)
131131
return -1
132132
# Number of questions
133-
qr_count = int.from_bytes(self._pkt_buf[4:6], "l")
133+
qr_count = int.from_bytes(self._pkt_buf[4:6], "big")
134134
if not qr_count >= 1:
135135
if self._debug:
136136
print("* DNS ERROR: Question count >=1, ", qr_count)
137137
return -1
138138
# Number of answers
139-
an_count = int.from_bytes(self._pkt_buf[6:8], "l")
139+
an_count = int.from_bytes(self._pkt_buf[6:8], "big")
140140
if self._debug:
141141
print("* DNS Answer Count: ", an_count)
142142
if not an_count >= 1:
@@ -156,15 +156,15 @@ def _parse_dns_response(
156156
ptr += name_len + 1
157157

158158
# Validate Query is Type A
159-
q_type = int.from_bytes(self._pkt_buf[ptr : ptr + 2], "l")
159+
q_type = int.from_bytes(self._pkt_buf[ptr : ptr + 2], "big")
160160
if not q_type == TYPE_A:
161161
if self._debug:
162162
print("* DNS ERROR: Incorrect Query Type: ", q_type)
163163
return -1
164164
ptr += 2
165165

166166
# Validate Query is Type A
167-
q_class = int.from_bytes(self._pkt_buf[ptr : ptr + 2], "l")
167+
q_class = int.from_bytes(self._pkt_buf[ptr : ptr + 2], "big")
168168
if not q_class == TYPE_A:
169169
if self._debug:
170170
print("* DNS ERROR: Incorrect Query Class: ", q_class)
@@ -181,15 +181,15 @@ def _parse_dns_response(
181181
ptr += 1
182182

183183
# Validate Answer Type A
184-
ans_type = int.from_bytes(self._pkt_buf[ptr : ptr + 2], "l")
184+
ans_type = int.from_bytes(self._pkt_buf[ptr : ptr + 2], "big")
185185
if not ans_type == TYPE_A:
186186
if self._debug:
187187
print("* DNS ERROR: Incorrect Answer Type: ", ans_type)
188188
return -1
189189
ptr += 2
190190

191191
# Validate Answer Class IN
192-
ans_class = int.from_bytes(self._pkt_buf[ptr : ptr + 2], "l")
192+
ans_class = int.from_bytes(self._pkt_buf[ptr : ptr + 2], "big")
193193
if not ans_class == TYPE_A:
194194
if self._debug:
195195
print("* DNS ERROR: Incorrect Answer Class: ", ans_class)
@@ -200,7 +200,7 @@ def _parse_dns_response(
200200
ptr += 4
201201

202202
# Validate addr is IPv4
203-
data_len = int.from_bytes(self._pkt_buf[ptr : ptr + 2], "l")
203+
data_len = int.from_bytes(self._pkt_buf[ptr : ptr + 2], "big")
204204
if not data_len == DATA_LEN:
205205
if self._debug:
206206
print("* DNS ERROR: Unexpected Data Length: ", data_len)

0 commit comments

Comments
 (0)