Skip to content

Fixed the usage of int.from_bytes and int.to_bytes #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions adafruit_wiznet5k/adafruit_wiznet5k.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ def _get_rx_rcv_size(self, sock):
val_1 = self._read_snrx_rsr(sock)
if val_1 != 0:
val = self._read_snrx_rsr(sock)
return int.from_bytes(val, "b")
return int.from_bytes(val, "big")

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

def _read_snrx_rd(self, sock):
self._pbuff[0] = self._read_socket(sock, REG_SNRX_RD)[0]
Expand Down
16 changes: 8 additions & 8 deletions adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def send_dhcp_message(self, state, time_elapsed, renew=False):

# Transaction ID (xid)
self._initial_xid = htonl(self._transaction_id)
self._initial_xid = self._initial_xid.to_bytes(4, "l")
self._initial_xid = self._initial_xid.to_bytes(4, "big")
_BUFF[4:7] = self._initial_xid

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

# flags
flags = htons(0x8000)
flags = flags.to_bytes(2, "b")
flags = flags.to_bytes(2, "big")
_BUFF[10] = flags[1]
_BUFF[11] = flags[0]

Expand Down Expand Up @@ -259,7 +259,7 @@ def parse_dhcp_response(self):
if _BUFF[28:34] == 0:
return 0, 0

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

# -- Parse Packet, VARIABLE -- #
Expand Down Expand Up @@ -287,7 +287,7 @@ def parse_dhcp_response(self):
ptr += 1
opt_len = _BUFF[ptr]
ptr += 1
self._lease_time = int.from_bytes(_BUFF[ptr : ptr + opt_len], "l")
self._lease_time = int.from_bytes(_BUFF[ptr : ptr + opt_len], "big")
ptr += opt_len
elif _BUFF[ptr] == ROUTERS_ON_SUBNET:
ptr += 1
Expand All @@ -305,13 +305,13 @@ def parse_dhcp_response(self):
ptr += 1
opt_len = _BUFF[ptr]
ptr += 1
self._t1 = int.from_bytes(_BUFF[ptr : ptr + opt_len], "l")
self._t1 = int.from_bytes(_BUFF[ptr : ptr + opt_len], "big")
ptr += opt_len
elif _BUFF[ptr] == T2_VAL:
ptr += 1
opt_len = _BUFF[ptr]
ptr += 1
self._t2 = int.from_bytes(_BUFF[ptr : ptr + opt_len], "l")
self._t2 = int.from_bytes(_BUFF[ptr : ptr + opt_len], "big")
ptr += opt_len
elif _BUFF[ptr] == 0:
break
Expand Down Expand Up @@ -399,7 +399,7 @@ def _dhcp_state_machine(self):
if msg_type == DHCP_OFFER:
# Check if transaction ID matches, otherwise it may be an offer
# for another device
if htonl(self._transaction_id) == int.from_bytes(xid, "l"):
if htonl(self._transaction_id) == int.from_bytes(xid, "big"):
if self._debug:
print(
"* DHCP: Send request to {}".format(self.dhcp_server_ip)
Expand All @@ -423,7 +423,7 @@ def _dhcp_state_machine(self):
msg_type, xid = self.parse_dhcp_response()
# Check if transaction ID matches, otherwise it may be
# for another device
if htonl(self._transaction_id) == int.from_bytes(xid, "l"):
if htonl(self._transaction_id) == int.from_bytes(xid, "big"):
if msg_type == DHCP_ACK:
if self._debug:
print("* DHCP: Successful lease")
Expand Down
18 changes: 9 additions & 9 deletions adafruit_wiznet5k/adafruit_wiznet5k_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _parse_dns_response(
print("DNS Packet Received: ", self._pkt_buf)

# Validate request identifier
xid = int.from_bytes(self._pkt_buf[0:2], "l")
xid = int.from_bytes(self._pkt_buf[0:2], "big")
if not xid == self._request_id:
if self._debug:
print(
Expand All @@ -124,19 +124,19 @@ def _parse_dns_response(
)
return -1
# Validate flags
flags = int.from_bytes(self._pkt_buf[2:4], "l")
flags = int.from_bytes(self._pkt_buf[2:4], "big")
if not flags in (0x8180, 0x8580):
if self._debug:
print("* DNS ERROR: Invalid flags, ", flags)
return -1
# Number of questions
qr_count = int.from_bytes(self._pkt_buf[4:6], "l")
qr_count = int.from_bytes(self._pkt_buf[4:6], "big")
if not qr_count >= 1:
if self._debug:
print("* DNS ERROR: Question count >=1, ", qr_count)
return -1
# Number of answers
an_count = int.from_bytes(self._pkt_buf[6:8], "l")
an_count = int.from_bytes(self._pkt_buf[6:8], "big")
if self._debug:
print("* DNS Answer Count: ", an_count)
if not an_count >= 1:
Expand All @@ -156,15 +156,15 @@ def _parse_dns_response(
ptr += name_len + 1

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

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

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

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

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