[sgen] Add parallel copying infrastructure
[mono-project.git] / mono / utils / networking-fallback.c
blob992db1543ccba145540ddfa2df54d1ca2edf9895
1 /*
2 * networking-fallbacks.c: Fallback networking code that rely on old BSD apis or whatever else is available.
4 * Author:
5 * Rodrigo Kumpera (kumpera@gmail.com)
7 * (C) 2015 Xamarin
8 */
10 #include <mono/utils/networking.h>
11 #include <glib.h>
13 #ifdef HAVE_NETDB_H
14 #include <netdb.h>
15 #endif
17 #if !defined (HAVE_GETADDRINFO)
19 #if defined (HAVE_GETHOSTBYNAME) || defined (HAVE_GETHOSTBYNAME2)
21 static void
22 add_hostent (MonoAddressInfo *info, int flags, struct hostent *h)
24 MonoAddressEntry *cur, *prev = info->entries;
25 int idx = 0;
27 if (!h)
28 return;
30 if (!info->aliases)
31 info->aliases = g_strdupv (h->h_aliases);
33 while (h->h_addr_list [idx]) {
34 cur = g_new0 (MonoAddressEntry, 1);
35 if (prev)
36 prev->next = cur;
37 else
38 info->entries = cur;
40 if (flags & MONO_HINT_CANONICAL_NAME && h->h_name)
41 cur->canonical_name = g_strdup (h->h_name);
43 cur->family = h->h_addrtype;
44 cur->socktype = SOCK_STREAM;
45 cur->protocol = 0; /* Zero means the default stream protocol */
46 cur->address_len = h->h_length;
47 memcpy (&cur->address, h->h_addr_list [idx], h->h_length);
49 prev = cur;
50 ++idx;
54 int
55 mono_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **result)
57 MonoAddressInfo *addr_info;
58 addr_info = g_new0 (MonoAddressInfo, 1);
60 #ifdef HAVE_GETHOSTBYNAME2
61 if (flags & MONO_HINT_IPV6 || flags & MONO_HINT_UNSPECIFIED)
62 add_hostent (addr_info, flags, gethostbyname2 (hostname, AF_INET6));
63 if (flags & MONO_HINT_IPV4 || flags & MONO_HINT_UNSPECIFIED)
64 add_hostent (addr_info, flags, gethostbyname2 (hostname, AF_INET));
65 #else
66 add_hostent (addr_info, flags, gethostbyname (hostname))
67 #endif
69 if (!addr_info->entries) {
70 *result = NULL;
71 mono_free_address_info (addr_info);
72 return 1;
75 *result = addr_info;
76 return 0;
79 #endif /* defined (HAVE_GETHOSTBYNAME) || defined (HAVE_GETHOSTBYNAME2) */
81 #endif /* !defined (HAVE_GETADDRINFO) */