Repository navigation
libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo - #20691
Conversation
3db5424 to
4ad088a
Compare
|
4ad088a to
7264b61
Compare
|
@Swatantra-66 please fix the conflict. |
7264b61 to
110a9c8
Compare
|
@xiaoxiang781216 Rebased onto latest master and resolved the conflict. |
110a9c8 to
8ea0073
Compare
8ea0073 to
d9d3448
Compare
d9d3448 to
fee3630
Compare
fee3630 to
1804549
Compare
|
please fix: |
When getaddrinfo() is called with hints->ai_socktype set to SOCK_STREAM or SOCK_DGRAM and hints->ai_protocol set to 0, standard networking implementations (KAME, BSD, glibc, musl) populate ai_protocol with IPPROTO_TCP or IPPROTO_UDP respectively. Previously, NuttX left ai_protocol as 0, which broke applications and runtimes (such as WAMR) that validate or rely on standard ai_protocol values. Furthermore, POSIX requires that when both ai_socktype and ai_protocol are unspecified (0), getaddrinfo() returns all supported transports (e.g., both SOCK_STREAM/IPPROTO_TCP and SOCK_DGRAM/IPPROTO_UDP) for each resolved address. This patch: 1. Normalizes ai_protocol to IPPROTO_TCP for SOCK_STREAM and IPPROTO_UDP for SOCK_DGRAM when ai_protocol is 0 under Internet address families (AF_INET, AF_INET6, AF_UNSPEC). 2. Validates compatibility between ai_socktype and ai_protocol, returning EAI_SOCKTYPE for incompatible combinations. 3. Preserves explicit non-zero ai_protocol values without overwriting them during service resolution. 4. Returns separate records for all supported transports (both TCP and UDP) when ai_socktype and ai_protocol are 0. 5. Rejects SOCK_RAW with EAI_SERVICE when resolving named services. Resolves apache#16693 Signed-off-by: Swatantra Yadav <[email protected]>
1804549 to
57c1659
Compare
|
Fixed. Refactored Inline addrinfo node construction into This reduces the |
|
let's ignore ci error which is fixed by #20960 instead wasting ci resource by retrigger. |
|
Should we have an apps PR that sets |
casaroli
left a comment
There was a problem hiding this comment.
Should we mention the behavioral changes in the commit message?
ai_canonnameis set only withAI_CANONNAMEhostname == NULLwith no result returnsEAI_NONAME, notEAI_MEMORY.- A not enabled family, returns
EAI_FAMILY
Yes, That makes complete sense. I can open the companion nuttx-apps PR |
@casaroli thank you for approval, I have updated the PR description with these 3 points so they are documented. Since @xiaoxiang781216 noted earlier to avoid retriggering CI by pushing, I updated the PR summary instead of amending the commit to save runner resources. |
|
@casaroli I have opened the companion apps PR here: apache/nuttx-apps#3913 |
Summary
Resolves #16693.
When
getaddrinfo()is called withhints->ai_socktypeset toSOCK_STREAMorSOCK_DGRAMandhints->ai_protocolset to0, standard networking implementations (KAME, BSD, glibc, musl) populateai_protocolwithIPPROTO_TCPorIPPROTO_UDPrespectively.Previously, NuttX left
ai_protocolas0, which broke applications and runtimes (such as WAMR - WebAssembly Micro Runtime) that validate or rely on standardai_protocolvalues.Furthermore, POSIX requires that when both
ai_socktypeandai_protocolare unspecified (0),getaddrinfo()returns all supported transports (e.g., bothSOCK_STREAM/IPPROTO_TCPandSOCK_DGRAM/IPPROTO_UDP) for each resolved address so that callers can choose their transport.Key Changes:
ai_protocoltoIPPROTO_TCPforSOCK_STREAMandIPPROTO_UDPforSOCK_DGRAMwhenai_protocol == 0under Internet address families (AF_INET,AF_INET6,AF_UNSPEC).ai_socktypefor Internet families (accepting0,SOCK_STREAM,SOCK_DGRAM,SOCK_RAW) and returnsEAI_SOCKTYPEfor incompatible type/protocol combinations without regressing non-Internet families (AF_LOCAL,AF_RPMSG) that supportSOCK_SEQPACKET.ai_protocolvalues during service resolution, returningEAI_SERVICEif an incompatible named service is queried rather than inferring TCP over the caller's explicit protocol.ai_socktype == 0andai_protocol == 0, querying both protocols viagetservbyname_r()for named services.SOCK_RAWwithEAI_SERVICEwhen resolving named services.port = transports[0].portafter named service lookups to ensure local, RPMSG, and VSOCK consumers receive the resolved port.ai_canonnamestrictly whenAI_CANONNAMEis specified inhints->ai_flagsper POSIX specification, avoiding unwanted string assignments when not explicitly requested.EAI_NONAME(instead ofEAI_MEMORY) whenhostname == NULLyields no matching address or transport.EAI_FAMILYwhen the resolved address family is not supported or enabled in the current configuration.Impact
ai_protocolvalues.EAI_NONAME,EAI_FAMILY, andAI_CANONNAME).nxstyleerrors.Testing
hints.ai_socktype = SOCK_STREAM, hints.ai_protocol = 0-> returnsIPPROTO_TCPhints.ai_socktype = SOCK_DGRAM, hints.ai_protocol = 0-> returnsIPPROTO_UDPhints.ai_socktype = 0, hints.ai_protocol = 0-> returns both TCP and UDP transportshints.ai_socktype = SOCK_STREAM, hints.ai_protocol = IPPROTO_UDP-> returnsEAI_SOCKTYPEhints.ai_socktype = SOCK_RAW, servname = "http"-> returnsEAI_SERVICEAF_LOCALwithSOCK_SEQPACKET-> succeeds withoutEAI_SOCKTYPEhints.ai_flags = 0->ai_canonnameremainsNULLhints.ai_flags = AI_CANONNAME->ai_canonnameis populatedhostname == NULLwith no matching result -> returnsEAI_NONAME