From: zhenwei pi <pizhen...@bytedance.com>

mptcp_connect.c is a startup tutorial of MPTCP programming, however
there is a lack of ai_protocol(IPPROTO_MPTCP) usage. Add comment for
getaddrinfo MPTCP support.

This patch first uses IPPROTO_MPTCP to get addrinfo, and if glibc
version is too old, it falls back to using IPPROTO_TCP.

Co-developed-by: Geliang Tang <geli...@kernel.org>
Signed-off-by: Geliang Tang <geli...@kernel.org>
Signed-off-by: zhenwei pi <pizhen...@bytedance.com>
Reviewed-by: Matthieu Baerts (NGI0) <matt...@kernel.org>
Signed-off-by: Matthieu Baerts (NGI0) <matt...@kernel.org>
---
Notes:
- a previous version has already been sent to Netdev. ChangeLog:
  - show how to use IPPROTO_MPTCP with getaddrinfo, not only a comment.
  - Link to this version:
    https://lore.kernel.org/20250407085122.1203489-1-pizhen...@bytedance.com
---
 tools/testing/selftests/net/mptcp/mptcp_connect.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/net/mptcp/mptcp_connect.c 
b/tools/testing/selftests/net/mptcp/mptcp_connect.c
index 
c83a8b47bbdfa5fcf1462e2b2949b41fd32c9b14..ac1349c4b9e5404c95935eb38b08a15d774eb1d9
 100644
--- a/tools/testing/selftests/net/mptcp/mptcp_connect.c
+++ b/tools/testing/selftests/net/mptcp/mptcp_connect.c
@@ -180,13 +180,26 @@ static void xgetnameinfo(const struct sockaddr *addr, 
socklen_t addrlen,
 }
 
 static void xgetaddrinfo(const char *node, const char *service,
-                        const struct addrinfo *hints,
+                        struct addrinfo *hints,
                         struct addrinfo **res)
 {
+again:
        int err = getaddrinfo(node, service, hints, res);
 
        if (err) {
-               const char *errstr = getxinfo_strerr(err);
+               const char *errstr;
+
+               /* glibc starts to support MPTCP since v2.42.
+                * For older versions, use IPPROTO_TCP to resolve,
+                * and use TCP/MPTCP to create socket.
+                * Link: 
https://sourceware.org/git/?p=glibc.git;a=commit;h=a8e9022e0f82
+                */
+               if (err == EAI_SOCKTYPE) {
+                       hints->ai_protocol = IPPROTO_TCP;
+                       goto again;
+               }
+
+               errstr = getxinfo_strerr(err);
 
                fprintf(stderr, "Fatal: getaddrinfo(%s:%s): %s\n",
                        node ? node : "", service ? service : "", errstr);
@@ -292,7 +305,7 @@ static int sock_listen_mptcp(const char * const listenaddr,
 {
        int sock = -1;
        struct addrinfo hints = {
-               .ai_protocol = IPPROTO_TCP,
+               .ai_protocol = IPPROTO_MPTCP,
                .ai_socktype = SOCK_STREAM,
                .ai_flags = AI_PASSIVE | AI_NUMERICHOST
        };
@@ -356,7 +369,7 @@ static int sock_connect_mptcp(const char * const remoteaddr,
                              int infd, struct wstate *winfo)
 {
        struct addrinfo hints = {
-               .ai_protocol = IPPROTO_TCP,
+               .ai_protocol = IPPROTO_MPTCP,
                .ai_socktype = SOCK_STREAM,
        };
        struct addrinfo *a, *addr;

-- 
2.48.1


Reply via email to