Skip to content
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

_internet_on: try IPv4, if not acceptable — try IPv6 #4151

Merged

Conversation

vazhnov
Copy link
Contributor

@vazhnov vazhnov commented Jan 6, 2022

Hello!

Thank you for this great project!

I use IPv6-only workstation with Gentoo GNU/Linux. To connect to IPv4-only resources, I use public services NAT64 + DNS64.

All the software works fine, except PlatformIO:

 % pio update --dry-run

Platform Manager
================
Platform gd32v
--------
Checking platformio/gd32v                     1.2.1                              [Off-line]
Checking platformio/framework-gd32vf103-sdk   1.0.0 @ ~1.0.0                     [Off-line]
Checking platformio/tool-openocd-gd32v        0.1.1 @ ~0.1.1                     [Off-line]
Checking platformio/tool-gd32vflash           0.1.0 @ ~0.1.0                     [Off-line]
Checking platformio/toolchain-gd32v           9.2.0 @ ~9.2.0                     [Off-line]
Checking platformio/tool-dfuutil              1.9.200310 @ ~1.9.200310           [Off-line]


Library Manager
===============
Library Storage: /home/user/.platformio/lib
Failed to check for PlatformIO upgrades. Please check your Internet connection.

I found function _internet_on() uses IPv4-only code s = socket.socket(socket.AF_INET, socket.SOCK_STREAM). The next command, s.connect((host, 80)), raises an error "[Errno 101] Network is unreachable" in my OS.

Then I found official documentation https://docs.python.org/3/library/socket.html, there are examples "… The next two examples are identical to the above two, but support both IPv4 and IPv6. …". I found these examples a bit complicated to integrate into PlatformIO (but you can create your own, better solution, based on official Python examples).

I created a patch, which works fine: if IPv4 is not available, try IPv6. Here is an example:

 % pio --version
PlatformIO Core, version 5.2.4

 % pio update

Platform Manager
================
Platform gd32v
--------
Updating platformio/gd32v                     1.2.1                              [Up-to-date]
Updating platformio/framework-gd32vf103-sdk   1.0.0 @ ~1.0.0                     [Up-to-date]
Updating platformio/tool-openocd-gd32v        0.1.1 @ ~0.1.1                     [Up-to-date]
Updating platformio/tool-gd32vflash           0.1.0 @ ~0.1.0                     [Up-to-date]
Updating platformio/toolchain-gd32v           9.2.0 @ ~9.2.0                     [Up-to-date]
Updating platformio/tool-dfuutil              1.9.200310 @ ~1.9.200310           [Up-to-date]


Library Manager
===============
Library Storage: /home/user/.platformio/lib

P.S.: maybe it is better to try first IPv6, and then IPv4, so IPv4 users will have good error message. But right now, as I understand, PlatformIO doesn't show error message, so this is doesn't matter.

@CLAassistant
Copy link

CLAassistant commented Jan 6, 2022

CLA assistant check
All committers have signed the CLA.

@vazhnov
Copy link
Contributor Author

vazhnov commented Jan 6, 2022

This looks even better: socket.create_connection(address[, timeout[, source_address]]):

Connect to a TCP service listening on the internet address (a 2-tuple (host, port)), and return the socket object. This is a higher-level function than socket.connect(): if host is a non-numeric hostname, it will try to resolve it for both AF_INET and AF_INET6, and then try to connect to all possible addresses in turn until a connection succeeds. This makes it easy to write clients that are compatible to both IPv4 and IPv6.

…h one universal `socket.create_connection`
@vazhnov
Copy link
Contributor Author

vazhnov commented Jan 8, 2022

Code rewrited with socket.create_connection.
It works for me.

Thanks to Wrocław Hackerspace for this advice!

@ivankravets ivankravets self-requested a review January 8, 2022 12:39
@ivankravets ivankravets merged commit 5658e7f into platformio:develop Jan 8, 2022
@ivankravets
Copy link
Member

Thanks for the valuable PR! 🙏

@ivankravets ivankravets added this to the 5.2.5 milestone Jan 8, 2022
ivankravets added a commit that referenced this pull request Jan 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants