Skip to content

Conversation

@greezybacon
Copy link
Contributor

@greezybacon greezybacon commented Jan 19, 2025

Summary

For IPv6 UDP sockets, the peer address was previously defined as only the first four bytes of the IP address. For IPv6 addresses, this resulted in an incorrect address expressed as IPv4. For instance, receiving a packet via ::recvfrom from 'fe80::87:e7ff:fe48:629a' is returned as having a peer address of '254.128.0.0'

Testing

I have tested this with a UDP-based IPv6 service using ::sendto and ::recvfrom, and the address are now returned correctly to Python.

The underlying sockets function works correctly if provided the correct IPv6 addresses, so this doesn't affect the underlying socket interface. Here is an example echo service which doesn't work with the current implementation:

s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s.bind(('::', 4000))
while True:
    msg, addr = s.recvfrom(512)
    # NOTE: `addr` is incorrect
    s.sendto(msg, addr)

Trade-offs and Alternatives

This uses slightly more memory for IPv4 UDP socket servers as memory is allocated based on the largest supported address family. However, it's only a few bytes per function call.

@greezybacon greezybacon force-pushed the fix/lwip-ipv6-peer-addr branch from 25e3209 to b349300 Compare January 19, 2025 22:02
@codecov
Copy link

codecov bot commented Jan 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.58%. Comparing base (03fe9c5) to head (f29bd5a).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #16613   +/-   ##
=======================================
  Coverage   98.58%   98.58%           
=======================================
  Files         167      167           
  Lines       21590    21590           
=======================================
  Hits        21285    21285           
  Misses        305      305           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-actions
Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:   +16 +0.002% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label Jan 21, 2025
@dpgeorge dpgeorge added this to the release-1.25.0 milestone Jan 21, 2025
@dpgeorge
Copy link
Member

Thanks for the fix, this is definitely needed.

I think the #include "shared/netutils/netutils.h" line can now be removed as well.

@greezybacon greezybacon force-pushed the fix/lwip-ipv6-peer-addr branch 2 times, most recently from 30e48f2 to f96f8b6 Compare January 28, 2025 15:31
For IPv6 connections, the peer address was previously defined as only the
first four bytes of the IP address. For IPv6 addresses, this resulted in an
incorrect IPv4 address. For instance, receiving a packet via `::recvfrom`
from `'fe80::87:e7ff:fe48:629a'` is returned as having a peer address of
`'254.128.0.0'`

Signed-off-by: Jared Hancock <jared.hancock@centeredsolutions.com>
@dpgeorge dpgeorge force-pushed the fix/lwip-ipv6-peer-addr branch from f96f8b6 to f29bd5a Compare January 28, 2025 23:50
@dpgeorge dpgeorge merged commit f29bd5a into micropython:master Jan 29, 2025
60 checks passed
@dpgeorge
Copy link
Member

Thanks for updating, now merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extmod Relates to extmod/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants