|  | 
|  | 1 | +/** | 
|  | 2 | + * \file | 
|  | 3 | + * Portable networking functions | 
|  | 4 | + * | 
|  | 5 | + * Author: | 
|  | 6 | + *	Rodrigo Kumpera ([email protected]) | 
|  | 7 | + * | 
|  | 8 | + * (C) 2015 Xamarin | 
|  | 9 | + */ | 
|  | 10 | + | 
|  | 11 | +#include <mono/utils/mono-threads-coop.h> | 
|  | 12 | +#include <glib.h> | 
|  | 13 | + | 
|  | 14 | +#ifdef HAVE_NETDB_H | 
|  | 15 | +#include <netdb.h> | 
|  | 16 | +#endif | 
|  | 17 | +#ifdef HAVE_SYS_IOCTL_H | 
|  | 18 | +#include <sys/ioctl.h> | 
|  | 19 | +#endif | 
|  | 20 | +#ifdef HAVE_SYS_SOCKET_H | 
|  | 21 | +#include <sys/socket.h> | 
|  | 22 | +#endif | 
|  | 23 | +#ifdef HAVE_NET_IF_H | 
|  | 24 | +#include <net/if.h> | 
|  | 25 | +#endif | 
|  | 26 | +#ifdef HAVE_UNISTD_H | 
|  | 27 | +#include <unistd.h> | 
|  | 28 | +#endif | 
|  | 29 | + | 
|  | 30 | +#include "debugger-networking.h" | 
|  | 31 | + | 
|  | 32 | + | 
|  | 33 | +void | 
|  | 34 | +mono_debugger_networking_init (void) | 
|  | 35 | +{ | 
|  | 36 | +#ifdef HOST_WIN32 | 
|  | 37 | +	WSADATA wsadata; | 
|  | 38 | +	int err; | 
|  | 39 | + | 
|  | 40 | +	err = WSAStartup (2 /* 2.0 */, &wsadata); | 
|  | 41 | +	if(err) | 
|  | 42 | +		g_error ("%s: Couldn't initialise networking", __func__); | 
|  | 43 | +#endif | 
|  | 44 | +} | 
|  | 45 | + | 
|  | 46 | +void | 
|  | 47 | +mono_debugger_networking_shutdown (void) | 
|  | 48 | +{ | 
|  | 49 | +#ifdef HOST_WIN32 | 
|  | 50 | +	WSACleanup (); | 
|  | 51 | +#endif | 
|  | 52 | +} | 
|  | 53 | + | 
|  | 54 | + | 
|  | 55 | +/* port in host order, address in network order */ | 
|  | 56 | +void | 
|  | 57 | +mono_debugger_socket_address_init (MonoSocketAddress *sa, socklen_t *len, int family, const void *address, int port) | 
|  | 58 | +{ | 
|  | 59 | +	memset (sa, 0, sizeof (MonoSocketAddress)); | 
|  | 60 | +	if (family == AF_INET) { | 
|  | 61 | +		*len = sizeof (struct sockaddr_in); | 
|  | 62 | + | 
|  | 63 | +		sa->v4.sin_family = AF_INET; | 
|  | 64 | +		sa->v4.sin_addr = *(struct in_addr*)address; | 
|  | 65 | +		sa->v4.sin_port = htons (GINT_TO_UINT16 (port)); | 
|  | 66 | +#if HAVE_SOCKADDR_IN_SIN_LEN | 
|  | 67 | +		sa->v4.sin_len = sizeof (*len); | 
|  | 68 | +#endif | 
|  | 69 | +#ifdef HAVE_STRUCT_SOCKADDR_IN6 | 
|  | 70 | +	} else if (family == AF_INET6) { | 
|  | 71 | +		*len = sizeof (struct sockaddr_in6); | 
|  | 72 | + | 
|  | 73 | +		sa->v6.sin6_family = AF_INET6; | 
|  | 74 | +		sa->v6.sin6_addr = *(struct in6_addr*)address; | 
|  | 75 | +		sa->v6.sin6_port = htons (GINT_TO_UINT16 (port)); | 
|  | 76 | +#if HAVE_SOCKADDR_IN6_SIN_LEN | 
|  | 77 | +		sa->v6.sin6_len = sizeof (*len); | 
|  | 78 | +#endif | 
|  | 79 | +#endif | 
|  | 80 | +	} else { | 
|  | 81 | +		g_error ("Cannot handle address family %d", family); | 
|  | 82 | +	} | 
|  | 83 | +} | 
|  | 84 | + | 
|  | 85 | + | 
|  | 86 | +void | 
|  | 87 | +mono_debugger_free_address_info (MonoAddressInfo *ai) | 
|  | 88 | +{ | 
|  | 89 | +	MonoAddressEntry *cur = ai->entries, *next; | 
|  | 90 | +	while (cur) { | 
|  | 91 | +		next = cur->next; | 
|  | 92 | +		g_free ((void*)cur->canonical_name); | 
|  | 93 | +		g_free (cur); | 
|  | 94 | +		cur = next; | 
|  | 95 | +	} | 
|  | 96 | +	g_strfreev (ai->aliases); | 
|  | 97 | +	g_free (ai); | 
|  | 98 | +} | 
|  | 99 | + | 
|  | 100 | + | 
|  | 101 | +#if !defined (HAVE_GETADDRINFO) && (defined (HAVE_GETHOSTBYNAME) || defined (HAVE_GETHOSTBYNAME2)) | 
|  | 102 | +static void | 
|  | 103 | +add_hostent (MonoAddressInfo *info, int flags, struct hostent *h) | 
|  | 104 | +{ | 
|  | 105 | +	MonoAddressEntry *cur, *prev = info->entries; | 
|  | 106 | +	int idx = 0; | 
|  | 107 | + | 
|  | 108 | +	if (!h) | 
|  | 109 | +		return; | 
|  | 110 | + | 
|  | 111 | +	if (!info->aliases) | 
|  | 112 | +		info->aliases = g_strdupv (h->h_aliases); | 
|  | 113 | + | 
|  | 114 | +	while (h->h_addr_list [idx]) { | 
|  | 115 | +		cur = g_new0 (MonoAddressEntry, 1); | 
|  | 116 | +		if (prev) | 
|  | 117 | +			prev->next = cur; | 
|  | 118 | +		else | 
|  | 119 | +			info->entries = cur; | 
|  | 120 | + | 
|  | 121 | +		if (flags & MONO_HINT_CANONICAL_NAME && h->h_name) | 
|  | 122 | +			cur->canonical_name = g_strdup (h->h_name); | 
|  | 123 | + | 
|  | 124 | +		cur->family = h->h_addrtype; | 
|  | 125 | +		cur->socktype = SOCK_STREAM; | 
|  | 126 | +		cur->protocol = 0; /* Zero means the default stream protocol */ | 
|  | 127 | +		cur->address_len = h->h_length; | 
|  | 128 | +		memcpy (&cur->address, h->h_addr_list [idx], h->h_length); | 
|  | 129 | + | 
|  | 130 | +		prev = cur; | 
|  | 131 | +		++idx; | 
|  | 132 | +	} | 
|  | 133 | +} | 
|  | 134 | +#endif /* !defined (HAVE_GETADDRINFO) && (defined (HAVE_GETHOSTBYNAME) || defined (HAVE_GETHOSTBYNAME2)) */ | 
|  | 135 | + | 
|  | 136 | + | 
|  | 137 | +int | 
|  | 138 | +mono_debugger_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **result) | 
|  | 139 | +{ | 
|  | 140 | +#if defined (HAVE_GETADDRINFO) /* modern posix networking code */ | 
|  | 141 | +	char service_name [16]; | 
|  | 142 | +	struct addrinfo hints, *res = NULL, *info; | 
|  | 143 | +	MonoAddressEntry *cur = NULL, *prev = NULL; | 
|  | 144 | +	MonoAddressInfo *addr_info; | 
|  | 145 | +	int ret; | 
|  | 146 | + | 
|  | 147 | +	memset (&hints, 0, sizeof (struct addrinfo)); | 
|  | 148 | +	*result = NULL; | 
|  | 149 | + | 
|  | 150 | +	hints.ai_family = PF_UNSPEC; | 
|  | 151 | +	if (flags & MONO_HINT_IPV4) | 
|  | 152 | +		hints.ai_family = PF_INET; | 
|  | 153 | +	else if (flags & MONO_HINT_IPV6) | 
|  | 154 | +		hints.ai_family = PF_INET6; | 
|  | 155 | + | 
|  | 156 | +	hints.ai_socktype = SOCK_STREAM; | 
|  | 157 | + | 
|  | 158 | +	if (flags & MONO_HINT_CANONICAL_NAME) | 
|  | 159 | +		hints.ai_flags = AI_CANONNAME; | 
|  | 160 | +	if (flags & MONO_HINT_NUMERIC_HOST) | 
|  | 161 | +		hints.ai_flags |= AI_NUMERICHOST; | 
|  | 162 | + | 
|  | 163 | +/* Some ancient libc don't define AI_ADDRCONFIG */ | 
|  | 164 | +#ifdef AI_ADDRCONFIG | 
|  | 165 | +	if (flags & MONO_HINT_CONFIGURED_ONLY) | 
|  | 166 | +		hints.ai_flags |= AI_ADDRCONFIG; | 
|  | 167 | +#endif | 
|  | 168 | +	sprintf (service_name, "%d", port); | 
|  | 169 | + | 
|  | 170 | +	MONO_ENTER_GC_SAFE; | 
|  | 171 | +	ret = getaddrinfo (hostname, service_name, &hints, &info); | 
|  | 172 | +	MONO_EXIT_GC_SAFE; | 
|  | 173 | + | 
|  | 174 | +	if (ret) | 
|  | 175 | +		return 1; /* FIXME propagate the error */ | 
|  | 176 | + | 
|  | 177 | +	res = info; | 
|  | 178 | +	*result = addr_info = g_new0 (MonoAddressInfo, 1); | 
|  | 179 | + | 
|  | 180 | +	while (res) { | 
|  | 181 | +		cur = g_new0 (MonoAddressEntry, 1); | 
|  | 182 | +		cur->family = res->ai_family; | 
|  | 183 | +		cur->socktype = res->ai_socktype; | 
|  | 184 | +		cur->protocol = res->ai_protocol; | 
|  | 185 | +		if (cur->family == PF_INET) { | 
|  | 186 | +			cur->address_len = sizeof (struct in_addr); | 
|  | 187 | +			cur->address.v4 = ((struct sockaddr_in*)res->ai_addr)->sin_addr; | 
|  | 188 | +#ifdef HAVE_STRUCT_SOCKADDR_IN6 | 
|  | 189 | +		} else if (cur->family == PF_INET6) { | 
|  | 190 | +			cur->address_len = sizeof (struct in6_addr); | 
|  | 191 | +			cur->address.v6 = ((struct sockaddr_in6*)res->ai_addr)->sin6_addr; | 
|  | 192 | +#endif | 
|  | 193 | +		} else { | 
|  | 194 | +			g_warning ("Cannot handle address family %d", cur->family); | 
|  | 195 | +			res = res->ai_next; | 
|  | 196 | +			g_free (cur); | 
|  | 197 | +			continue; | 
|  | 198 | +		} | 
|  | 199 | + | 
|  | 200 | +		if (res->ai_canonname) | 
|  | 201 | +			cur->canonical_name = g_strdup (res->ai_canonname); | 
|  | 202 | + | 
|  | 203 | +		if (prev) | 
|  | 204 | +			prev->next = cur; | 
|  | 205 | +		else | 
|  | 206 | +			addr_info->entries = cur; | 
|  | 207 | + | 
|  | 208 | +		prev = cur; | 
|  | 209 | +		res = res->ai_next; | 
|  | 210 | +	} | 
|  | 211 | + | 
|  | 212 | +	freeaddrinfo (info); | 
|  | 213 | +	return 0; | 
|  | 214 | + | 
|  | 215 | +#elif defined (HAVE_GETHOSTBYNAME) || defined (HAVE_GETHOSTBYNAME2) /* fallback networking code that relies on old BSD apis or whatever else is available */ | 
|  | 216 | +	MonoAddressInfo *addr_info; | 
|  | 217 | +	addr_info = g_new0 (MonoAddressInfo, 1); | 
|  | 218 | + | 
|  | 219 | +#ifdef HAVE_GETHOSTBYNAME2 | 
|  | 220 | +	if (flags & MONO_HINT_IPV6 || flags & MONO_HINT_UNSPECIFIED) | 
|  | 221 | +		add_hostent (addr_info, flags, gethostbyname2 (hostname, AF_INET6)); | 
|  | 222 | +	if (flags & MONO_HINT_IPV4 || flags & MONO_HINT_UNSPECIFIED) | 
|  | 223 | +		add_hostent (addr_info, flags, gethostbyname2 (hostname, AF_INET)); | 
|  | 224 | +#else | 
|  | 225 | +	add_hostent (addr_info, flags, gethostbyname (hostname)) | 
|  | 226 | +#endif | 
|  | 227 | + | 
|  | 228 | +	if (!addr_info->entries) { | 
|  | 229 | +		*result = NULL; | 
|  | 230 | +		mono_debugger_free_address_info (addr_info); | 
|  | 231 | +		return 1; | 
|  | 232 | +	} | 
|  | 233 | + | 
|  | 234 | +	*result = addr_info; | 
|  | 235 | +	return 0; | 
|  | 236 | + | 
|  | 237 | +#else | 
|  | 238 | +	g_error ("No networking implementation available"); | 
|  | 239 | +	return 1; | 
|  | 240 | +#endif /* defined (HAVE_GETADDRINFO) */ | 
|  | 241 | +} | 
0 commit comments