3 * Portable networking functions
6 * Rodrigo Kumpera (kumpera@gmail.com)
11 #include <mono/utils/networking.h>
15 mono_address_size_for_family (int family
)
19 return sizeof (struct in_addr
);
20 #ifdef HAVE_STRUCT_SOCKADDR_IN6
22 return sizeof (struct in6_addr
);
30 mono_free_address_info (MonoAddressInfo
*ai
)
32 MonoAddressEntry
*cur
= ai
->entries
, *next
;
35 g_free ((void*)cur
->canonical_name
);
39 g_strfreev (ai
->aliases
);
44 /* port in host order, address in network order */
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
);
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
);
70 g_error ("Cannot handle address family %d", family
);
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
));