@@ -441,9 +441,25 @@ static int anetCreateSocket(char *err, int domain, int type, int protocol, int f
441441 return s ;
442442}
443443
444+ /* XXX: Until glibc 2.41, getaddrinfo with hints.ai_protocol of IPPROTO_MPTCP leads error.
445+ * Use hints.ai_protocol IPPROTO_IP (0) or IPPROTO_TCP (6) to resolve address and overwrite
446+ * it when MPTCP is enabled.
447+ * Ref: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/testing/selftests/net/mptcp/mptcp_connect.c
448+ * https://sourceware.org/git/?p=glibc.git;a=commit;h=a8e9022e0f829d44a818c642fc85b3bfbd26a514
449+ */
450+ static int anetTcpGetProtocol (int is_mptcp_enabled ) {
451+ #ifdef IPPROTO_MPTCP
452+ return is_mptcp_enabled ? IPPROTO_MPTCP : IPPROTO_TCP ;
453+ #else
454+ assert (!is_mptcp_enabled );
455+ return IPPROTO_TCP ;
456+ #endif
457+ }
458+
444459#define ANET_CONNECT_NONE 0
445460#define ANET_CONNECT_NONBLOCK 1
446461#define ANET_CONNECT_BE_BINDING 2 /* Best effort binding. */
462+ #define ANET_CONNECT_MPTCP 4
447463static int anetTcpGenericConnect (char * err , const char * addr , int port , const char * source_addr , int flags ) {
448464 int s = ANET_ERR , rv ;
449465 char portstr [6 ]; /* strlen("65535") + 1; */
@@ -466,9 +482,10 @@ static int anetTcpGenericConnect(char *err, const char *addr, int port, const ch
466482 * Make sure connection-intensive things like the benchmark tool
467483 * will be able to close/open sockets a zillion of times.
468484 */
485+ int ai_protocol = anetTcpGetProtocol (flags & ANET_CONNECT_MPTCP );
469486 int sockflags = ANET_SOCKET_CLOEXEC | ANET_SOCKET_REUSEADDR ;
470487 if (flags & ANET_CONNECT_NONBLOCK ) sockflags |= ANET_SOCKET_NONBLOCK ;
471- if ((s = anetCreateSocket (err , p -> ai_family , p -> ai_socktype , p -> ai_protocol , sockflags )) == ANET_ERR ) continue ;
488+ if ((s = anetCreateSocket (err , p -> ai_family , p -> ai_socktype , ai_protocol , sockflags )) == ANET_ERR ) continue ;
472489 if (source_addr ) {
473490 int bound = 0 ;
474491 /* Using getaddrinfo saves us from self-determining IPv4 vs IPv6 */
@@ -528,8 +545,10 @@ int anetTcpNonBlockConnect(char *err, const char *addr, int port) {
528545 return anetTcpGenericConnect (err , addr , port , NULL , ANET_CONNECT_NONBLOCK );
529546}
530547
531- int anetTcpNonBlockBestEffortBindConnect (char * err , const char * addr , int port , const char * source_addr ) {
532- return anetTcpGenericConnect (err , addr , port , source_addr , ANET_CONNECT_NONBLOCK | ANET_CONNECT_BE_BINDING );
548+ int anetTcpNonBlockBestEffortBindConnect (char * err , const char * addr , int port , const char * source_addr , int mptcp ) {
549+ int flags = ANET_CONNECT_NONBLOCK | ANET_CONNECT_BE_BINDING ;
550+ if (mptcp ) flags |= ANET_CONNECT_MPTCP ;
551+ return anetTcpGenericConnect (err , addr , port , source_addr , flags );
533552}
534553
535554static int anetListen (char * err , int s , struct sockaddr * sa , socklen_t len , int backlog , mode_t perm , char * group ) {
@@ -574,21 +593,6 @@ static int anetV6Only(char *err, int s) {
574593 return ANET_OK ;
575594}
576595
577- /* XXX: Until glibc 2.41, getaddrinfo with hints.ai_protocol of IPPROTO_MPTCP leads error.
578- * Use hints.ai_protocol IPPROTO_IP (0) or IPPROTO_TCP (6) to resolve address and overwrite
579- * it when MPTCP is enabled.
580- * Ref: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/testing/selftests/net/mptcp/mptcp_connect.c
581- * https://sourceware.org/git/?p=glibc.git;a=commit;h=a8e9022e0f829d44a818c642fc85b3bfbd26a514
582- */
583- static int anetTcpGetProtocol (int is_mptcp_enabled ) {
584- #ifdef IPPROTO_MPTCP
585- return is_mptcp_enabled ? IPPROTO_MPTCP : IPPROTO_TCP ;
586- #else
587- assert (!is_mptcp_enabled );
588- return IPPROTO_TCP ;
589- #endif
590- }
591-
592596static int _anetTcpServer (char * err , int port , char * bindaddr , int af , int backlog , int mptcp ) {
593597 int s = -1 , rv ;
594598 char _port [6 ]; /* strlen("65535") */
0 commit comments