-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
unix/modsocket: Accept a host+port array for socket.connect. #17289
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
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #17289 +/- ##
=======================================
Coverage 98.54% 98.54%
=======================================
Files 169 169
Lines 21896 21897 +1
=======================================
+ Hits 21578 21579 +1
Misses 318 318 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Code size report:
|
dcdd59a
to
0223a78
Compare
Are IPv6 connections blocked in the CI environment? Unless I'm missing something the IPv6 tests should succeed (they do from my LAN at least). Edit: Indeed they are: actions/runner-images#668 :( I'll rewrite the tests to acknowledge this fact. |
0223a78
to
c02c290
Compare
Interesting, |
fe48e57
to
32e83f1
Compare
This commit lets socket.connect accept an array containing a hostname string and a port number as the address to connect to, if the socket family is either AF_INET or AF_INET6. This brings the behaviour of socket.connect in line with the other embedded ports' versions that use LWIP, and also with CPython - even though CPython can only accept a tuple as its host+port argument. The existing behaviour of accepting a serialised sockaddr structure as the connection address is still present, maintaining compatibility with existing code. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
32e83f1
to
354d67a
Compare
Thanks for the patch. There's a bit of history here why this isn't supported. And it's documented: https://docs.micropython.org/en/latest/library/socket.html#socket-address-format-s . In particular, quoting that page:
You'll see there's actually a So that's some background... not sure how to proceed with this PR at this stage. |
Thanks for the explanation! I've never really used ffi nor micropython-lib when dealing with the Unix port, I usually develop directly on device myself and use Unix just to validate some things that would take longer to test elsewhere. From what I've seen (and I hope I didn't miss anything) there are only two ports that have sockets support and can't natively handle a host+port tuple in So, I wonder whether adding at least dotted quad support to Zephyr (and maybe to Unix without ffi to make it easier for interactive use) would make sense at this point, to actually make things more compatible across ports for simpler cases. If that's not something that can help, no worries, I'll close the PR :) On the other hand, maybe the sockaddr-only unit test cases can still be saved. |
Summary
This PR lets
socket.connect
accept an array containing a hostname string and a port number as the address to connect to, if the socket family is eitherAF_INET
orAF_INET6
. The existing behaviour of accepting a serialised sockaddr structure as the connection address is still present, maintaining compatibility with existing code.This brings the behaviour of socket.connect almost in line with the other embedded ports' versions that use LWIP, and somewhat with CPython as well. The differences are as follows:
AF_INET
/AF_INET6
netutils_parse_inet_addr
inshared/netutils/netutils.c
as invoked byextmod/modsocket.c
), but is supported in CPython.Hostname resolution is performed internally via a call to
socket.getaddrinfo
, and in case of multiple compatible entries mapped to a single hostname the first one returned bygetaddrinfo(2)
will be used.Testing
This has been tested manually as such (numeric addresses are what www.google.com resolves to me from here):
with the output:
I seeextmod/ssl_basic.py
usestest.example.com
for its tests - if a test file is required I may use that host myself as well.Edit: a test file with ipv4/ipv6 connection tests using both host+port pair and a pre-resolved sockaddr buffer have been added.
Trade-offs and Alternatives
There is a slight footprint increase for the Unix port but, considering the average computing power and storage facilities of the average environment the Unix port runs in, the quality of life improvements, and increased compatibility should be enough benefits to make these code changes useful.