mono_native_tls_get_value that does not change LastError. (#15568)
[mono-project.git] / mono / utils / networking.c
blob6f643dfd692325115e2c101820730fd805506742
1 /**
2 * \file
3 * Portable networking functions
5 * Author:
6 * Rodrigo Kumpera (kumpera@gmail.com)
8 * (C) 2015 Xamarin
9 */
11 #include <mono/utils/networking.h>
12 #include <glib.h>
14 int
15 mono_address_size_for_family (int family)
17 switch (family) {
18 case AF_INET:
19 return sizeof (struct in_addr);
20 #ifdef HAVE_STRUCT_SOCKADDR_IN6
21 case AF_INET6:
22 return sizeof (struct in6_addr);
23 #endif
25 return 0;
29 void
30 mono_free_address_info (MonoAddressInfo *ai)
32 MonoAddressEntry *cur = ai->entries, *next;
33 while (cur) {
34 next = cur->next;
35 g_free ((void*)cur->canonical_name);
36 g_free (cur);
37 cur = next;
39 g_strfreev (ai->aliases);
40 g_free (ai);
44 /* port in host order, address in network order */
45 void
46 mono_socket_address_init (MonoSocketAddress *sa, socklen_t *len, int family, const void *address, int port)
48 memset (sa, 0, sizeof (MonoSocketAddress));
49 if (family == AF_INET) {
50 *len = sizeof (struct sockaddr_in);
52 sa->v4.sin_family = family;
53 sa->v4.sin_addr = *(struct in_addr*)address;
54 sa->v4.sin_port = htons (port);
55 #if HAVE_SOCKADDR_IN_SIN_LEN
56 sa->v4.sin_len = sizeof (*len);
57 #endif
58 #ifdef HAVE_STRUCT_SOCKADDR_IN6
59 } else if (family == AF_INET6) {
60 *len = sizeof (struct sockaddr_in6);
62 sa->v6.sin6_family = family;
63 sa->v6.sin6_addr = *(struct in6_addr*)address;
64 sa->v6.sin6_port = htons (port);
65 #if HAVE_SOCKADDR_IN6_SIN_LEN
66 sa->v6.sin6_len = sizeof (*len);
67 #endif
68 #endif
69 } else {
70 g_error ("Cannot handle address family %d", family);
74 void
75 mono_address_init (MonoAddress *out_addr, int family, void *in_addr)
77 memset (out_addr, 0, sizeof (MonoAddress));
78 out_addr->family = family;
79 memcpy (&out_addr->addr, in_addr, mono_address_size_for_family (family));