[esp-tls] Add addr_family option to esp_tls_cfg_t (IDFGH-9620) #10967
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Presently, esp-tls effectively does not support IPV6, as reported here: #9920
Basically: if DNS is able to return A records, esp-tls will always try to use them even if ipv4 connectivity is not available. However, if an A lookup fails, an AAAA lookup is attempted next, and IPV6 can work.
This behavior comes from passing
AF_UNSPEC
togetaddrinfo()
here, which causes it to prefer IPV4 over IPV6 here. here is the spot in LwIP where an AAAA lookup is done only after an A lookup fails.I see two options to improve this:
tcp_connect()
to make a decision about which address family to connect to. Perhaps if the first call toconnect()
returnsEHOSTUNREACH
, do a second lookup passingAF_INET6
, and then make a second connection attempt if an AAAA record is returned.esp_tls_cfg_t
that allows the user to explicitly specify whether to passAF_UNSPEC
,AF_INET
, orAF_INET6
togetaddrinfo()
.In this PR, I have implemented option 2. Here is how I am using it in the application I am currently working on:
One might argue that instead of adding a field to
esp_tls_cfg_t
, we should add an argument to theesp_tls_conn_new*()
functions. The documentation foresp_tls_conn_new_sync()
suggests that thecfg
field can be NULL if "If you wish to open non-TLS connection", though I'm not sure it actually handles a NULL input safely. And that is what theis_plain_tcp
field is for anyways. My reason for preferring to add a cfg field is that it is backwards compatible: If theesp_tls_cfg_t
is zero initialized, the field defaults toESP_TLS_AF_UNSPEC
, which gives exactly the same behavior as before.