Skip to content

Implement DHCP lease maintenance with renew / rebind. #36

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 5 commits into from
May 6, 2021
Merged

Implement DHCP lease maintenance with renew / rebind. #36

merged 5 commits into from
May 6, 2021

Conversation

xorbit
Copy link
Contributor

@xorbit xorbit commented Apr 29, 2021

After initializing everything, the DHCP lease can now be maintained from the main loop by calling eth.maintain_dhcp_lease(). An example main loop for a WSGI web server + DHCP maintenance can look like this:

while True:
    # Our main loop where we have the server poll for incoming requests
    wsgiServer.update_poll()
    # Maintain DHCP lease
    eth.maintain_dhcp_lease()
  • Now can use socket.bind to set source port to be consistent with CPython socket behavior.
  • DHCP object is now persistent in WIZNET5K because we want to keep it around to automatically renew and rebind.
  • Added maintain_dhcp_lease to WIZNET5K to properly maintain the DHCP lease with renew / rebind behavior.
  • Implement correct DHCP transaction ID behavior so we don't accidentally grab an IP that was supposed to be assigned to another device.
  • Now keeps track of automatically assigned source ports to prevent duplication.
  • Renamed WIZNET5K._src_port to WIZNET5K.src_port because it's externally accessed from socket.connect.
  • Removed setting source port for DNS lookup, moved to bind before socket connect.
  • WIZNET5K.get_socket now only returns fully closed sockets. Previous behavior caused problems with stray old data from previous connections confusing new connections when the socket wasn't fully closed yet.
  • Improved WSGI server in how it deals with socket removal and allocation.
  • Fixed some bugs and comments.

- Now can use `socket.bind` to set source port to be consistent with
  CPython socket behavior.
- DHCP object is now persistent in `WIZNET5K` because we want to keep
  it around to automatically renew and rebind.
- Added `maintain_dhcp_lease` to `WIZNET5K` to properly maintain the
  DHCP lease with renew / rebind behavior.
- Implement correct DHCP transaction ID behavior so we don't
  accidentally grab an IP that was supposed to be assigned to another
  device.
- Now keeps track of automatically assigned source ports to prevent
  duplication.
- Renamed `WIZNET5K._src_port` to `WIZNET5K.src_port` because it's
  externally accessed from `socket.connect`.
- Removed setting source port for DNS lookup, moved to `bind` before
  socket connect.
- `WIZNET5K.get_socket` now only returns fully closed sockets.
  Previous behavior caused problems with stray old data from previous
  connections confusing new connections when the socket wasn't fully
  closed yet.
- Improved WSGI server in how it deals with socket removal and
  allocation.
- Fixed some bugs and comments.
@xorbit xorbit mentioned this pull request Apr 29, 2021
@ladyada ladyada requested a review from brentru April 29, 2021 22:56
If `socket_listen` was called, and a connection was made right
away, it was possible that the state of the socket would change
to `SNSR_SOCK_ESTABLISHED` before the check ever saw it enter
`SNSR_SOCK_LISTEN`, and the code would be stuck in an endless
loop.

This fixes the issue by accepting both `SNSR_SOCK_LISTEN` and
`SNSR_SOCK_ESTABLISHED` as valid states to continue.
@brentru
Copy link
Member

brentru commented May 3, 2021

@xorbit Great work and thank you for the PR, I'll review and test on hardware this week.

README example now shows `maintain_dhcp_lease` function and a
full detailed example showing other features was added to the
`examples` directory.
Copy link
Member

@brentru brentru left a comment

Choose a reason for hiding this comment

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

@xorbit Thank you for the PR, I tested the WSGI and BTC handler locally with an Ethernet FeatherWing and it works great!

xorbit added 2 commits May 5, 2021 11:10
Had to catch exception when creating I2C device for boards that
have no I2C pull-ups.
@brentru brentru merged commit 5dc4aeb into adafruit:master May 6, 2021
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request May 11, 2021
Updating https://github.com/adafruit/Adafruit_CircuitPython_AS7341 to 1.1.2 from 1.1.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_AS7341#17 from jposada202020/replacing_named_tuple

Updating https://github.com/adafruit/Adafruit_CircuitPython_BMP3XX to 1.3.6 from 1.3.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_BMP3XX#17 from jposada202020/adding_waitingtime_as_option

Updating https://github.com/adafruit/Adafruit_CircuitPython_DPS310 to 1.2.5 from 1.2.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_DPS310#13 from jposada202020/adding_readme_note

Updating https://github.com/adafruit/Adafruit_CircuitPython_LSM9DS1 to 2.1.9 from 2.1.8:
  > Merge pull request adafruit/Adafruit_CircuitPython_LSM9DS1#30 from jposada202020/returning_correct_units_gyro

Updating https://github.com/adafruit/Adafruit_CircuitPython_MPU6050 to 1.1.8 from 1.1.7:
  > Merge pull request adafruit/Adafruit_CircuitPython_MPU6050#17 from jposada202020/correcting_returning_units_gyro

Updating https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k to 1.10.0 from 1.9.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#36 from xorbit/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_AdafruitIO to 5.3.0 from 5.2.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_AdafruitIO#70 from rodrigoargumedo/create-multiple-records

Updating https://github.com/adafruit/Adafruit_CircuitPython_MatrixPortal to 3.0.1 from 3.0.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_MatrixPortal#78 from flavio-fernandes/fix.issue.77
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.

2 participants