2 * networking.c: Portable networking functions
5 * Rodrigo Kumpera (kumpera@gmail.com)
10 #include <mono/utils/networking.h>
14 mono_address_size_for_family (int family)
18 return sizeof (struct in_addr);
20 return sizeof (struct in6_addr);
27 mono_free_address_info (MonoAddressInfo *ai)
29 MonoAddressEntry *cur = ai->entries, *next;
32 g_free ((void*)cur->canonical_name);
36 g_strfreev (ai->aliases);
41 /* port in host order, address in network order */
43 mono_socket_address_init (MonoSocketAddress *sa, socklen_t *len, int family, const void *address, int port)
45 memset (sa, 0, sizeof (MonoSocketAddress));
46 if (family == AF_INET) {
47 *len = sizeof (struct sockaddr_in);
49 sa->v4.sin_family = family;
50 sa->v4.sin_addr = *(struct in_addr*)address;
51 sa->v4.sin_port = htons (port);
52 #if HAVE_SOCKADDR_IN_SIN_LEN
53 sa->v4.sin_len = sizeof (*len);
55 } else if (family == AF_INET6) {
56 *len = sizeof (struct sockaddr_in6);
58 sa->v6.sin6_family = family;
59 sa->v6.sin6_addr = *(struct in6_addr*)address;
60 sa->v6.sin6_port = htons (port);
61 #if HAVE_SOCKADDR_IN6_SIN_LEN
62 sa->v6.sin6_len = sizeof (*len);
65 g_error ("Cannot handle address family %d", family);
70 mono_address_init (MonoAddress *out_addr, int family, void *in_addr)
72 memset (out_addr, 0, sizeof (MonoAddress));
73 out_addr->family = family;
74 memcpy (&out_addr->addr, in_addr, mono_address_size_for_family (family));