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

Conversation

alexsporn
Copy link
Contributor

  • Fixed the usage of int.from_bytes and int.to_bytes to pass the proper byteorder value. The correct values are "big" and "little" not "b" and "l"
  • Fixed DHCP and DNS using little-endian instead of big-endian for parsing bytes. Network order of bytes is big endian, so DHCP was not working and DNS could not match the request ids because it was using "l" instead of "big"

Not sure how this was working before. Maybe older versions ignored the wrong parameters and used big-endian by default. Without this fix I could not get it to work on my board, I was always getting a ValueError: Invalid byteorder when doing DHCP.

This was tested with:

Adafruit CircuitPython 8.0.0-alpha.1-75-gec73ab841 on 2022-07-28; W5500-EVB-Pico with rp2040
Board ID:wiznet_w5500_evb_pico

… byteorder value

Fixed DHCP and DNS using little-endian instead of big-endian for parsing bytes
@FoamyGuy
Copy link
Contributor

Thanks for the fix.

This does seem to match the CPython docs: https://docs.python.org/3/library/stdtypes.html#int.to_bytes

CircuitPython docs don't show an example. They do mention being compatible with CPython, but it's not clear to me if it means the entire API or just the positionality of the arguments: https://docs.circuitpython.org/en/latest/docs/library/builtins.html?highlight=int.to_bytes#int.to_bytes

I am also perplexed about how this could have been working in the past. Perhaps at some point the core had a different API than CPython but was adjusted to match somewhere along the way and this wasn't updated? Or perhaps this never did work, but isn't needed in all cases so no one noticed till now.

I'll try to test this out on hardware tomorrow.

@tekktrik tekktrik requested a review from a team August 1, 2022 14:09
@FoamyGuy
Copy link
Contributor

FoamyGuy commented Aug 1, 2022

My testing was performed on a Feather RP2040 with Ethernet Featherwing connected circuitpython versions 7.2.3 and 8.0.0-alpha.1

I am getting this exception raised when I try to test the simpletest script with the branch from this PR:

Traceback (most recent call last):
  File "code.py", line 23, in <module>
  File "/lib/adafruit_wiznet5k/adafruit_wiznet5k.py", line 202, in __init__
  File "/lib/adafruit_wiznet5k/adafruit_wiznet5k.py", line 222, in set_dhcp
  File "/lib/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py", line 490, in request_dhcp_lease
  File "/lib/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py", line 426, in _dhcp_state_machine
TypeError: object with buffer protocol required

Adding some additional debugging prints it seems that xid is already an int when it gets to this line:

if htonl(self._transaction_id) == int.from_bytes(xid, "l"):

Which then raises the above error. Stepping back through the code a bit further into parse_dhcp_response() I think in my case this logic is happening:

xid = _BUFF[4:8]
if bytes(xid) < self._initial_xid:
print("f")
return 0, 0

I'm getting the single "f" printed from this, but I don't know what it is supposed to indicate or in what situation it would be expected to arise.

Honestly though I'm not sure if my results are due to any of the changes in this PR. I am getting basically the same errors when I try to run the current version of the driver library also. Perhaps my router is sending something "weird" that this device or current driver can't handle

Here is the full output of the simpletest with debug=True in the driver initialization:

code.py output:
Wiznet5k WebClient Test
* Initializing DHCP
*** Get socket
Allocated socket #0
* Listening on port=68, ip=0.0.0.0
*** Opening socket 0
* Opening W5k Socket, protocol=2
* w5k socket connect, protocol=2, port=67, ip=255.255.255.255
*** Opening socket 0
* Opening W5k Socket, protocol=2
* DHCP: Send discover to (255, 255, 255, 255)
* socket_available called on socket 0, protocol 2
Bytes avail. on sock:  864
         * Processing 8 bytes of data
* DHCP: Parsing OFFER
* socket_available called on socket 0, protocol 2
Bytes avail. on sock:  856
         * Processing 300 bytes of data
DHCP Response:  b"\x02\x01\x06\x00\xfc\xa6\xfcP\x00\x00\x80\x00\x00\x00\x00\x00\xc0\xa8\x01s\xc0\xa8\x01\x01\x00\x00\x00\x00\xde\xad\xbe\xef\xfe\xed\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00c\x82Sc5\x01\x026\x04\xc0\xa8\x01\x013\x04\x00\x01Q\x80:\x04\x00\x00\xa8\xc0;\x04\x00\x01'P\x01\x04\xff\xff\xff\x00\x1c\x04\xc0\xa8\x01\xff\x03\x04\xc0\xa8\x01\x01\x06\x04\xc0\xa8\x01\x01\xff\x00\x00\x00\x00\x00\x00\x00\x00"
Msg Type: 2
Subnet Mask: (255, 255, 255, 0)
DHCP Server IP: (192, 168, 1, 1)
DNS Server IP: (192, 168, 1, 1)                  
Gateway IP: (192, 168, 1, 1)
Local IP: (192, 168, 1, 115)
T1: 43200
T2: 75600
Lease Time: 86400
* DHCP: Send request to (192, 168, 1, 1)
* socket_available called on socket 0, protocol 2
Bytes avail. on sock:  1420
         * Processing 8 bytes of data
* DHCP: Parsing ACK
* socket_available called on socket 0, protocol 2
Bytes avail. on sock:  1412
         * Processing 548 bytes of data
DHCP Response:  b'\x02\x01\x06\x00\xfc\xa6\xfcP\x00\x00\x80\x00\x00\x00\x00\x00\xc0\xa8\x01s\x00\x00\x00\x00\x00\x00\x00\x00\xde\xad\xbe\xef\xfe\xed\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00c\x82Sc5\x01\x026\x04\xc0\xa8\x01\x013\x04\x00\x01Q \x01\x04\xff\xff\xff\x00\x03\x04\xc0\xa8\x01\x01\x06\x08\xc0\xa8\x01\x01\xc0\xa8\x01\x01\x1a\x02\x05\xdc\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
f
Traceback (most recent call last):
  File "code.py", line 23, in <module>
  File "/lib/adafruit_wiznet5k/adafruit_wiznet5k.py", line 202, in __init__
  File "/lib/adafruit_wiznet5k/adafruit_wiznet5k.py", line 222, in set_dhcp
  File "/lib/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py", line 490, in request_dhcp_lease
  File "/lib/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py", line 426, in _dhcp_state_machine
TypeError: object with buffer protocol required

Code done running.

@jepler
Copy link
Contributor

jepler commented Aug 1, 2022

The DHCP server is SUPPOSED to send a response with the same 4-byte XID field as the request from the DHCP client. The if is trying to detect that the DHCP response didn't correspond to the request.

The return value in the non-error case from the function seems to be (int, bytes-of-length-4) but in the error cases it is (int, int).

At first glance, I don't see why the changes in this PR would lead to this problem. did you test with the released version or with the tip of main before this PR?

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Aug 1, 2022

I see, thank you. I guess in my case the DHCP server could be returning something different for some reason.

I added some additional prints and saw these values for xid on one run:

xid: b"\xc2\x96\x1e'" initial_xid: b"\xc3\x96\x1e'"

Which are similar, but do not match. It seems the values do change from run to run as well. Aftrl ctrl-C + ctrl-D it's two different values but similarly the xid one has the first hex byte 1 lower than the initial xid.

did you test with the released version or with the tip of main before this PR?

I did test with the released version installed via circup and do get the same exceptions raised by that version. I do think it's likely unrelated to these changes. But perhaps an opportunity to change the return types to the ones expected by the code that receives these values back and/or ideally raise a more descriptive error about the xid mismatch if that is a fatal error that we can't continue after.

@anecdata
Copy link
Member

anecdata commented Aug 1, 2022

fwiw, haven't tried PR yet, but I get the "f" return value intermittently on 5100. Not sure about 5500, there are none in my scrollback. But it's rare, and recovers.

Also, not to muddy the waters, but note that WIZnet DHCP behavior can vary based on LAN config:
#53

Copy link
Member

@anecdata anecdata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing on Adafruit CircuitPython 8.0.0-beta.0 on 2022-08-18; W5500-EVB-Pico with rp2040 confirms ValueError: Invalid byteorder without the PR code, and good operation with the PR code.

Testing on Adafruit CircuitPython 7.3.3 on 2022-08-29; Adafruit Feather RP2040 with rp2040 (and as old as 7.2.5) with Adafruit Ethernet FeatherWing (W5500) gives good results with the released v1.12.11 library, but ValueError: Invalid byteorder with 8.0.0-beta.0.

Similarly, Adafruit CircuitPython 7.3.3 on 2022-08-29; W5100S-EVB-Pico with rp2040 is fine, but Adafruit CircuitPython 8.0.0-beta.0 on 2022-08-18; W5100S-EVB-Pico with rp2040 gets ValueError: Invalid byteorder with the released v1.12.11 library.

It seems to be related to CP 8, though no issues or PRs stand out to me. CP 7.3.0 updated micropython to 1.18 and pico-sdk to 1.3.1, but there was nothing obvious to me in those and 7.3.3 runs fine.

Addendum:
The PR change appears to be backward-compatible also. The W5500-EVB-Pico runs fine with Adafruit CircuitPython 7.3.3 on 2022-08-29; W5100S-EVB-Pico with rp2040 and the PR-modified WIZnet library. Same backward-compatible results on the FeatherWing.

I'm inclined to approve the PR unless there are objections. We should still try to figure out where / why the apparent core change occurred.

Addendum: This seems relevant: adafruit/circuitpython@ac9cb93 (PR# adafruit/circuitpython#6562) ...If I read that right, in CP 7, unless the parameter is exactly "little", it will be treated as "big", so "b", "l", etc. get treated as big, and that's why it worked before. Testing a few conversions in the 7.x REPL bears this out. This commit was tagged 8.0.0-beta.0.

@anecdata anecdata merged commit 5f501fd into adafruit:main Sep 22, 2022
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Sep 23, 2022
Updating https://github.com/adafruit/Adafruit_CircuitPython_MCP9600 to 1.3.6 from 1.3.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_MCP9600#21 from kattni/doc-and-comment-fix

Updating https://github.com/adafruit/Adafruit_CircuitPython_TCA8418 to 1.0.3 from 1.0.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_TCA8418#8 from adafruit/3x4_oled
  > Merge pull request adafruit/Adafruit_CircuitPython_TCA8418#7 from adafruit/3x4_oled

Updating https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k to 1.12.12 from 1.12.11:
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#64 from alexsporn/fix/endiannes

Updating https://github.com/adafruit/Adafruit_CircuitPython_BLE_Beacon to 1.0.1 from 1.0.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_BLE_Beacon#1 from tekktrik/main

Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 5.5.1 from 5.5.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#123 from rtwfroody/eagain2

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Added the following libraries: Adafruit_CircuitPython_BLE_Beacon
@alexsporn alexsporn deleted the fix/endiannes branch February 21, 2023 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants