diff --git a/src/mono/mono/component/CMakeLists.txt b/src/mono/mono/component/CMakeLists.txt index a47a3802a8736b..5516122acf9906 100644 --- a/src/mono/mono/component/CMakeLists.txt +++ b/src/mono/mono/component/CMakeLists.txt @@ -24,6 +24,8 @@ list(APPEND components set(${MONO_DEBUGGER_COMPONENT_NAME}-sources ${MONO_COMPONENT_PATH}/debugger.c ${MONO_COMPONENT_PATH}/debugger.h + ${MONO_COMPONENT_PATH}/debugger-networking.c + ${MONO_COMPONENT_PATH}/debugger-networking.h ${MONO_COMPONENT_PATH}/debugger-agent.c ${MONO_COMPONENT_PATH}/debugger-agent.h ${MONO_COMPONENT_PATH}/debugger-engine.c diff --git a/src/mono/mono/component/debugger-agent.c b/src/mono/mono/component/debugger-agent.c index 9c0ed65cd2179a..4d931284f02afb 100644 --- a/src/mono/mono/component/debugger-agent.c +++ b/src/mono/mono/component/debugger-agent.c @@ -81,7 +81,6 @@ #include #include #include -#include #include #include #include @@ -89,6 +88,7 @@ #include #include "debugger-agent.h" +#include "debugger-networking.h" #include #include #include @@ -1047,7 +1047,7 @@ socket_transport_connect (const char *address) listen_fd = INVALID_SOCKET; MONO_ENTER_GC_UNSAFE; - mono_networking_init(); + mono_debugger_networking_init (); MONO_EXIT_GC_UNSAFE; if (host) { @@ -1060,7 +1060,7 @@ socket_transport_connect (const char *address) for (size_t i = 0; i < sizeof(hints) / sizeof(int); i++) { /* Obtain address(es) matching host/port */ MONO_ENTER_GC_UNSAFE; - s = mono_get_address_info (host, port, hints[i], &result); + s = mono_debugger_get_address_info (host, port, hints[i], &result); MONO_EXIT_GC_UNSAFE; if (s == 0) break; @@ -1111,7 +1111,7 @@ socket_transport_connect (const char *address) int n = 1; MONO_ENTER_GC_UNSAFE; - mono_socket_address_init (&sockaddr, &sock_len, rp->family, &rp->address, port); + mono_debugger_socket_address_init (&sockaddr, &sock_len, rp->family, &rp->address, port); MONO_EXIT_GC_UNSAFE; sfd = socket (rp->family, rp->socktype, rp->protocol); @@ -1133,7 +1133,7 @@ socket_transport_connect (const char *address) } MONO_ENTER_GC_UNSAFE; - mono_free_address_info (result); + mono_debugger_free_address_info (result); MONO_EXIT_GC_UNSAFE; } @@ -1176,7 +1176,7 @@ socket_transport_connect (const char *address) socklen_t sock_len; MONO_ENTER_GC_UNSAFE; - mono_socket_address_init (&sockaddr, &sock_len, rp->family, &rp->address, port); + mono_debugger_socket_address_init (&sockaddr, &sock_len, rp->family, &rp->address, port); MONO_EXIT_GC_UNSAFE; sfd = socket (rp->family, rp->socktype, @@ -1213,7 +1213,7 @@ socket_transport_connect (const char *address) conn_fd = sfd; MONO_ENTER_GC_UNSAFE; - mono_free_address_info (result); + mono_debugger_free_address_info (result); MONO_EXIT_GC_UNSAFE; } diff --git a/src/mono/mono/component/debugger-networking.c b/src/mono/mono/component/debugger-networking.c new file mode 100644 index 00000000000000..510a40bba2c7bd --- /dev/null +++ b/src/mono/mono/component/debugger-networking.c @@ -0,0 +1,241 @@ +/** + * \file + * Portable networking functions + * + * Author: + * Rodrigo Kumpera (kumpera@gmail.com) + * + * (C) 2015 Xamarin + */ + +#include +#include + +#ifdef HAVE_NETDB_H +#include +#endif +#ifdef HAVE_SYS_IOCTL_H +#include +#endif +#ifdef HAVE_SYS_SOCKET_H +#include +#endif +#ifdef HAVE_NET_IF_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif + +#include "debugger-networking.h" + + +void +mono_debugger_networking_init (void) +{ +#ifdef HOST_WIN32 + WSADATA wsadata; + int err; + + err = WSAStartup (2 /* 2.0 */, &wsadata); + if(err) + g_error ("%s: Couldn't initialise networking", __func__); +#endif +} + +void +mono_debugger_networking_shutdown (void) +{ +#ifdef HOST_WIN32 + WSACleanup (); +#endif +} + + +/* port in host order, address in network order */ +void +mono_debugger_socket_address_init (MonoSocketAddress *sa, socklen_t *len, int family, const void *address, int port) +{ + memset (sa, 0, sizeof (MonoSocketAddress)); + if (family == AF_INET) { + *len = sizeof (struct sockaddr_in); + + sa->v4.sin_family = AF_INET; + sa->v4.sin_addr = *(struct in_addr*)address; + sa->v4.sin_port = htons (GINT_TO_UINT16 (port)); +#if HAVE_SOCKADDR_IN_SIN_LEN + sa->v4.sin_len = sizeof (*len); +#endif +#ifdef HAVE_STRUCT_SOCKADDR_IN6 + } else if (family == AF_INET6) { + *len = sizeof (struct sockaddr_in6); + + sa->v6.sin6_family = AF_INET6; + sa->v6.sin6_addr = *(struct in6_addr*)address; + sa->v6.sin6_port = htons (GINT_TO_UINT16 (port)); +#if HAVE_SOCKADDR_IN6_SIN_LEN + sa->v6.sin6_len = sizeof (*len); +#endif +#endif + } else { + g_error ("Cannot handle address family %d", family); + } +} + + +void +mono_debugger_free_address_info (MonoAddressInfo *ai) +{ + MonoAddressEntry *cur = ai->entries, *next; + while (cur) { + next = cur->next; + g_free ((void*)cur->canonical_name); + g_free (cur); + cur = next; + } + g_strfreev (ai->aliases); + g_free (ai); +} + + +#if !defined (HAVE_GETADDRINFO) && (defined (HAVE_GETHOSTBYNAME) || defined (HAVE_GETHOSTBYNAME2)) +static void +add_hostent (MonoAddressInfo *info, int flags, struct hostent *h) +{ + MonoAddressEntry *cur, *prev = info->entries; + int idx = 0; + + if (!h) + return; + + if (!info->aliases) + info->aliases = g_strdupv (h->h_aliases); + + while (h->h_addr_list [idx]) { + cur = g_new0 (MonoAddressEntry, 1); + if (prev) + prev->next = cur; + else + info->entries = cur; + + if (flags & MONO_HINT_CANONICAL_NAME && h->h_name) + cur->canonical_name = g_strdup (h->h_name); + + cur->family = h->h_addrtype; + cur->socktype = SOCK_STREAM; + cur->protocol = 0; /* Zero means the default stream protocol */ + cur->address_len = h->h_length; + memcpy (&cur->address, h->h_addr_list [idx], h->h_length); + + prev = cur; + ++idx; + } +} +#endif /* !defined (HAVE_GETADDRINFO) && (defined (HAVE_GETHOSTBYNAME) || defined (HAVE_GETHOSTBYNAME2)) */ + + +int +mono_debugger_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **result) +{ +#if defined (HAVE_GETADDRINFO) /* modern posix networking code */ + char service_name [16]; + struct addrinfo hints, *res = NULL, *info; + MonoAddressEntry *cur = NULL, *prev = NULL; + MonoAddressInfo *addr_info; + int ret; + + memset (&hints, 0, sizeof (struct addrinfo)); + *result = NULL; + + hints.ai_family = PF_UNSPEC; + if (flags & MONO_HINT_IPV4) + hints.ai_family = PF_INET; + else if (flags & MONO_HINT_IPV6) + hints.ai_family = PF_INET6; + + hints.ai_socktype = SOCK_STREAM; + + if (flags & MONO_HINT_CANONICAL_NAME) + hints.ai_flags = AI_CANONNAME; + if (flags & MONO_HINT_NUMERIC_HOST) + hints.ai_flags |= AI_NUMERICHOST; + +/* Some ancient libc don't define AI_ADDRCONFIG */ +#ifdef AI_ADDRCONFIG + if (flags & MONO_HINT_CONFIGURED_ONLY) + hints.ai_flags |= AI_ADDRCONFIG; +#endif + sprintf (service_name, "%d", port); + + MONO_ENTER_GC_SAFE; + ret = getaddrinfo (hostname, service_name, &hints, &info); + MONO_EXIT_GC_SAFE; + + if (ret) + return 1; /* FIXME propagate the error */ + + res = info; + *result = addr_info = g_new0 (MonoAddressInfo, 1); + + while (res) { + cur = g_new0 (MonoAddressEntry, 1); + cur->family = res->ai_family; + cur->socktype = res->ai_socktype; + cur->protocol = res->ai_protocol; + if (cur->family == PF_INET) { + cur->address_len = sizeof (struct in_addr); + cur->address.v4 = ((struct sockaddr_in*)res->ai_addr)->sin_addr; +#ifdef HAVE_STRUCT_SOCKADDR_IN6 + } else if (cur->family == PF_INET6) { + cur->address_len = sizeof (struct in6_addr); + cur->address.v6 = ((struct sockaddr_in6*)res->ai_addr)->sin6_addr; +#endif + } else { + g_warning ("Cannot handle address family %d", cur->family); + res = res->ai_next; + g_free (cur); + continue; + } + + if (res->ai_canonname) + cur->canonical_name = g_strdup (res->ai_canonname); + + if (prev) + prev->next = cur; + else + addr_info->entries = cur; + + prev = cur; + res = res->ai_next; + } + + freeaddrinfo (info); + return 0; + +#elif defined (HAVE_GETHOSTBYNAME) || defined (HAVE_GETHOSTBYNAME2) /* fallback networking code that relies on old BSD apis or whatever else is available */ + MonoAddressInfo *addr_info; + addr_info = g_new0 (MonoAddressInfo, 1); + +#ifdef HAVE_GETHOSTBYNAME2 + if (flags & MONO_HINT_IPV6 || flags & MONO_HINT_UNSPECIFIED) + add_hostent (addr_info, flags, gethostbyname2 (hostname, AF_INET6)); + if (flags & MONO_HINT_IPV4 || flags & MONO_HINT_UNSPECIFIED) + add_hostent (addr_info, flags, gethostbyname2 (hostname, AF_INET)); +#else + add_hostent (addr_info, flags, gethostbyname (hostname)) +#endif + + if (!addr_info->entries) { + *result = NULL; + mono_debugger_free_address_info (addr_info); + return 1; + } + + *result = addr_info; + return 0; + +#else + g_error ("No networking implementation available"); + return 1; +#endif /* defined (HAVE_GETADDRINFO) */ +} diff --git a/src/mono/mono/utils/networking.h b/src/mono/mono/component/debugger-networking.h similarity index 70% rename from src/mono/mono/utils/networking.h rename to src/mono/mono/component/debugger-networking.h index 0c3d24a265c8c7..27c98bc819db39 100644 --- a/src/mono/mono/utils/networking.h +++ b/src/mono/mono/component/debugger-networking.h @@ -9,8 +9,8 @@ */ -#ifndef __MONO_NETWORKING_H__ -#define __MONO_NETWORKING_H__ +#ifndef __MONO_DEBUGGER_NETWORKING_H__ +#define __MONO_DEBUGGER_NETWORKING_H__ #include #include @@ -77,18 +77,14 @@ typedef union { } MonoSocketAddress; /* This only supports IPV4 / IPV6 and tcp */ -MONO_COMPONENT_API int mono_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **res); +int mono_debugger_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **res); -MONO_COMPONENT_API void mono_free_address_info (MonoAddressInfo *ai); +void mono_debugger_free_address_info (MonoAddressInfo *ai); -MONO_COMPONENT_API void mono_socket_address_init (MonoSocketAddress *sa, socklen_t *len, int family, const void *address, int port); +void mono_debugger_socket_address_init (MonoSocketAddress *sa, socklen_t *len, int family, const void *address, int port); -#ifndef HAVE_INET_PTON -int inet_pton (int family, const char *address, void *inaddrp); -#endif - -MONO_COMPONENT_API void mono_networking_init (void); -void mono_networking_shutdown (void); +void mono_debugger_networking_init (void); +void mono_debugger_networking_shutdown (void); #endif diff --git a/src/mono/mono/utils/CMakeLists.txt b/src/mono/mono/utils/CMakeLists.txt index 5d6544b36083b8..a0148d5c14cfbf 100644 --- a/src/mono/mono/utils/CMakeLists.txt +++ b/src/mono/mono/utils/CMakeLists.txt @@ -166,12 +166,6 @@ set(utils_common_sources mono-conc-hashtable.c json.h json.c - networking.c - networking-posix.c - networking-fallback.c - networking-missing.c - networking-windows.c - networking.h mono-rand.c mono-rand-windows.c mono-rand.h diff --git a/src/mono/mono/utils/networking-fallback.c b/src/mono/mono/utils/networking-fallback.c deleted file mode 100644 index 0c3b47adaf340c..00000000000000 --- a/src/mono/mono/utils/networking-fallback.c +++ /dev/null @@ -1,85 +0,0 @@ -/** - * \file - * Fallback networking code that rely on old BSD apis or whatever else is available. - * - * Author: - * Rodrigo Kumpera (kumpera@gmail.com) - * - * (C) 2015 Xamarin - */ - -#include -#include -#include - -#ifdef HAVE_NETDB_H -#include -#endif - -#if !defined (HAVE_GETADDRINFO) - -#if defined (HAVE_GETHOSTBYNAME) || defined (HAVE_GETHOSTBYNAME2) - -static void -add_hostent (MonoAddressInfo *info, int flags, struct hostent *h) -{ - MonoAddressEntry *cur, *prev = info->entries; - int idx = 0; - - if (!h) - return; - - if (!info->aliases) - info->aliases = g_strdupv (h->h_aliases); - - while (h->h_addr_list [idx]) { - cur = g_new0 (MonoAddressEntry, 1); - if (prev) - prev->next = cur; - else - info->entries = cur; - - if (flags & MONO_HINT_CANONICAL_NAME && h->h_name) - cur->canonical_name = g_strdup (h->h_name); - - cur->family = h->h_addrtype; - cur->socktype = SOCK_STREAM; - cur->protocol = 0; /* Zero means the default stream protocol */ - cur->address_len = h->h_length; - memcpy (&cur->address, h->h_addr_list [idx], h->h_length); - - prev = cur; - ++idx; - } -} - -int -mono_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **result) -{ - MonoAddressInfo *addr_info; - addr_info = g_new0 (MonoAddressInfo, 1); - -#ifdef HAVE_GETHOSTBYNAME2 - if (flags & MONO_HINT_IPV6 || flags & MONO_HINT_UNSPECIFIED) - add_hostent (addr_info, flags, gethostbyname2 (hostname, AF_INET6)); - if (flags & MONO_HINT_IPV4 || flags & MONO_HINT_UNSPECIFIED) - add_hostent (addr_info, flags, gethostbyname2 (hostname, AF_INET)); -#else - add_hostent (addr_info, flags, gethostbyname (hostname)) -#endif - - if (!addr_info->entries) { - *result = NULL; - mono_free_address_info (addr_info); - return 1; - } - - *result = addr_info; - return 0; -} - -#endif /* defined (HAVE_GETHOSTBYNAME) || defined (HAVE_GETHOSTBYNAME2) */ -#else /* !defined (HAVE_GETADDRINFO) */ - -MONO_EMPTY_SOURCE_FILE (networking_fallback); -#endif /* !defined (HAVE_GETADDRINFO) */ diff --git a/src/mono/mono/utils/networking-missing.c b/src/mono/mono/utils/networking-missing.c deleted file mode 100644 index 1f16b4121d7fec..00000000000000 --- a/src/mono/mono/utils/networking-missing.c +++ /dev/null @@ -1,62 +0,0 @@ -/** - * \file - * Implements missing standard socket functions. - * - * Author: - * Rodrigo Kumpera (kumpera@gmail.com) - * - * (C) 2015 Xamarin - */ - -#include -#include -#include - -#ifdef HAVE_NETDB_H -#include -#endif - -//wasm does have inet_pton even though autoconf fails to find -#if !defined (HAVE_INET_PTON) && !defined (HOST_WASM) - -int -inet_pton (int family, const char *address, void *inaddrp) -{ - if (family == AF_INET) { -#ifdef HAVE_INET_ATON - struct in_addr inaddr; - - if (!inet_aton (address, &inaddr)) - return 0; - - memcpy (inaddrp, &inaddr, sizeof (struct in_addr)); - return 1; -#else - /* assume the system has inet_addr(), if it doesn't - have that we're pretty much screwed... */ - guint32 inaddr; - - if (!strcmp (address, "255.255.255.255")) { - /* special-case hack */ - inaddr = 0xffffffff; - } else { - inaddr = inet_addr (address); -#ifndef INADDR_NONE -#define INADDR_NONE ((in_addr_t) -1) -#endif - if (inaddr == INADDR_NONE) - return 0; - } - - memcpy (inaddrp, &inaddr, sizeof (guint32)); - return 1; -#endif /* HAVE_INET_ATON */ - } - - return -1; -} - -#else /* !HAVE_INET_PTON */ - -MONO_EMPTY_SOURCE_FILE (networking_missing); -#endif /* !HAVE_INET_PTON */ diff --git a/src/mono/mono/utils/networking-posix.c b/src/mono/mono/utils/networking-posix.c deleted file mode 100644 index f9905573da7ce9..00000000000000 --- a/src/mono/mono/utils/networking-posix.c +++ /dev/null @@ -1,128 +0,0 @@ -/** - * \file - * Modern posix networking code - * - * Author: - * Rodrigo Kumpera (kumpera@gmail.com) - * - * (C) 2015 Xamarin - */ - -#include -#include - -#ifdef HAVE_NETDB_H -#include -#endif -#ifdef HAVE_SYS_IOCTL_H -#include -#endif -#ifdef HAVE_SYS_SOCKET_H -#include -#endif -#ifdef HAVE_NET_IF_H -#include -#endif -#ifdef HAVE_UNISTD_H -#include -#endif - -#include -#include - -#ifdef HAVE_GETADDRINFO - -int -mono_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **result) -{ - char service_name [16]; - struct addrinfo hints, *res = NULL, *info; - MonoAddressEntry *cur = NULL, *prev = NULL; - MonoAddressInfo *addr_info; - int ret; - - memset (&hints, 0, sizeof (struct addrinfo)); - *result = NULL; - - hints.ai_family = PF_UNSPEC; - if (flags & MONO_HINT_IPV4) - hints.ai_family = PF_INET; - else if (flags & MONO_HINT_IPV6) - hints.ai_family = PF_INET6; - - hints.ai_socktype = SOCK_STREAM; - - if (flags & MONO_HINT_CANONICAL_NAME) - hints.ai_flags = AI_CANONNAME; - if (flags & MONO_HINT_NUMERIC_HOST) - hints.ai_flags |= AI_NUMERICHOST; - -/* Some ancient libc don't define AI_ADDRCONFIG */ -#ifdef AI_ADDRCONFIG - if (flags & MONO_HINT_CONFIGURED_ONLY) - hints.ai_flags |= AI_ADDRCONFIG; -#endif - sprintf (service_name, "%d", port); - - MONO_ENTER_GC_SAFE; - ret = getaddrinfo (hostname, service_name, &hints, &info); - MONO_EXIT_GC_SAFE; - - if (ret) - return 1; /* FIXME propagate the error */ - - res = info; - *result = addr_info = g_new0 (MonoAddressInfo, 1); - - while (res) { - cur = g_new0 (MonoAddressEntry, 1); - cur->family = res->ai_family; - cur->socktype = res->ai_socktype; - cur->protocol = res->ai_protocol; - if (cur->family == PF_INET) { - cur->address_len = sizeof (struct in_addr); - cur->address.v4 = ((struct sockaddr_in*)res->ai_addr)->sin_addr; -#ifdef HAVE_STRUCT_SOCKADDR_IN6 - } else if (cur->family == PF_INET6) { - cur->address_len = sizeof (struct in6_addr); - cur->address.v6 = ((struct sockaddr_in6*)res->ai_addr)->sin6_addr; -#endif - } else { - g_warning ("Cannot handle address family %d", cur->family); - res = res->ai_next; - g_free (cur); - continue; - } - - if (res->ai_canonname) - cur->canonical_name = g_strdup (res->ai_canonname); - - if (prev) - prev->next = cur; - else - addr_info->entries = cur; - - prev = cur; - res = res->ai_next; - } - - freeaddrinfo (info); - return 0; -} - -#endif - -#ifndef _WIN32 -// These are already defined in networking-windows.c for Windows -void -mono_networking_init (void) -{ - //nothing really -} - -void -mono_networking_shutdown (void) -{ - //nothing really -} -#endif diff --git a/src/mono/mono/utils/networking-windows.c b/src/mono/mono/utils/networking-windows.c deleted file mode 100644 index bd445e5ee973c4..00000000000000 --- a/src/mono/mono/utils/networking-windows.c +++ /dev/null @@ -1,34 +0,0 @@ -/** - * \file - * Windows-specific networking implementations - * - * Author: - * Alexander Köplinger (alex.koeplinger@outlook.com) - */ - -#include - -#if defined(HOST_WIN32) - -void -mono_networking_init (void) -{ - WSADATA wsadata; - int err; - - err = WSAStartup (2 /* 2.0 */, &wsadata); - if(err) - g_error ("%s: Couldn't initialise networking", __func__); -} - -void -mono_networking_shutdown (void) -{ - WSACleanup (); -} - -#else - -MONO_EMPTY_SOURCE_FILE (networking_windows); - -#endif /* defined(HOST_WIN32) */ diff --git a/src/mono/mono/utils/networking.c b/src/mono/mono/utils/networking.c deleted file mode 100644 index 30dfd48101293e..00000000000000 --- a/src/mono/mono/utils/networking.c +++ /dev/null @@ -1,58 +0,0 @@ -/** - * \file - * Portable networking functions - * - * Author: - * Rodrigo Kumpera (kumpera@gmail.com) - * - * (C) 2015 Xamarin - */ - -#include -#include - -void -mono_free_address_info (MonoAddressInfo *ai) -{ - MonoAddressEntry *cur = ai->entries, *next; - while (cur) { - next = cur->next; - g_free ((void*)cur->canonical_name); - g_free (cur); - cur = next; - } - g_strfreev (ai->aliases); - g_free (ai); -} - - -/* port in host order, address in network order */ -void -mono_socket_address_init (MonoSocketAddress *sa, socklen_t *len, int family, const void *address, int port) -{ - memset (sa, 0, sizeof (MonoSocketAddress)); - if (family == AF_INET) { - *len = sizeof (struct sockaddr_in); - - sa->v4.sin_family = AF_INET; - sa->v4.sin_addr = *(struct in_addr*)address; - sa->v4.sin_port = htons (GINT_TO_UINT16 (port)); -#if HAVE_SOCKADDR_IN_SIN_LEN - sa->v4.sin_len = sizeof (*len); -#endif -#ifdef HAVE_STRUCT_SOCKADDR_IN6 - } else if (family == AF_INET6) { - *len = sizeof (struct sockaddr_in6); - - sa->v6.sin6_family = AF_INET6; - sa->v6.sin6_addr = *(struct in6_addr*)address; - sa->v6.sin6_port = htons (GINT_TO_UINT16 (port)); -#if HAVE_SOCKADDR_IN6_SIN_LEN - sa->v6.sin6_len = sizeof (*len); -#endif -#endif - } else { - g_error ("Cannot handle address family %d", family); - } -} -