From 6bb5d4c4e20c8ee603bdcec7be6d2855c13dadba Mon Sep 17 00:00:00 2001 From: Alexis Christoforides Date: Fri, 17 Aug 2018 00:07:19 +0300 Subject: [PATCH] [runtime] Use getprotobyname_r() where needed and available Fixes https://github.com/mono/mono/issues/7087 --- configure.ac | 17 +++++++++++++++++ mono/utils/networking-posix.c | 23 ++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index c9b895d220d35..96cbe154a80a8 100644 --- a/configure.ac +++ b/configure.ac @@ -2286,6 +2286,23 @@ if test x$host_win32 = xno; then AC_MSG_RESULT(no) ]) + dnl ********************************** + dnl *** Check for getprotobyname_r *** + dnl ********************************** + AC_MSG_CHECKING(for getprotobyname_r) + AC_TRY_LINK([ + #include + #include + ], [ + getprotobyname_r(NULL, NULL, NULL, 0, NULL); + ], [ + # Yes, we have it... + AC_MSG_RESULT(yes) + AC_DEFINE(HAVE_GETPROTOBYNAME_R, 1, [Have getprotobyname_r]) + ], [ + AC_MSG_RESULT(no) + ]) + dnl ********************************** dnl *** Check for getnameinfo *** dnl ********************************** diff --git a/mono/utils/networking-posix.c b/mono/utils/networking-posix.c index 85a6eba551f2f..142f4f846fbb9 100644 --- a/mono/utils/networking-posix.c +++ b/mono/utils/networking-posix.c @@ -127,7 +127,24 @@ mono_get_address_info (const char *hostname, int port, int flags, MonoAddressInf #endif -#ifdef HAVE_GETPROTOBYNAME +#if defined(__linux__) && defined(HAVE_GETPROTOBYNAME_R) + +static int +fetch_protocol (const char *proto_name, int *cache, int *proto, int default_val) +{ + if (!*cache) { + struct protoent protoent_buf = { 0 }; + struct protoent *pent = NULL; + char buf[1024]; + + getprotobyname_r (proto_name, &protoent_buf, buf, 1024, &pent); + *proto = pent ? pent->p_proto : default_val; + *cache = 1; + } + return *proto; +} + +#elif HAVE_GETPROTOBYNAME static int fetch_protocol (const char *proto_name, int *cache, int *proto, int default_val) @@ -142,6 +159,8 @@ fetch_protocol (const char *proto_name, int *cache, int *proto, int default_val) return *proto; } +#endif + int mono_networking_get_tcp_protocol (void) { @@ -163,8 +182,6 @@ mono_networking_get_ipv6_protocol (void) return fetch_protocol ("ipv6", &cache, &proto, 41); //41 is SOL_IPV6 on linux } -#endif - #if defined (HAVE_SIOCGIFCONF) #define IFCONF_BUFF_SIZE 1024