[utils] Small header sanitization. networking.h now include config.h
[mono.git] / mono / utils / networking.c
1 /*
2  * networking.c: Portable networking functions
3  *
4  * Author:
5  *      Rodrigo Kumpera (kumpera@gmail.com)
6  *
7  * (C) 2015 Xamarin
8  */
9
10 #include <mono/utils/networking.h>
11 #include <glib.h>
12
13 void
14 mono_free_address_info (MonoAddressInfo *ai)
15 {
16         MonoAddressEntry *cur = ai->entries, *next;
17         while (cur) {
18                 next = cur->next;
19                 g_free ((void*)cur->canonical_name);
20                 g_free (cur);
21                 cur = next;
22         }
23         g_strfreev (ai->aliases);
24         g_free (ai);
25 }
26
27
28 /* port in host order, address in network order */
29 void
30 mono_socket_address_init (MonoSocketAddress *sa, socklen_t *len, int family, const void *address, int port)
31 {
32         memset (sa, 0, sizeof (MonoSocketAddress));
33         if (family == AF_INET) {
34                 *len = sizeof (struct sockaddr_in);
35
36                 sa->v4.sin_family = family;
37                 sa->v4.sin_addr = *(struct in_addr*)address;
38                 sa->v4.sin_port = htons (port);
39 #if HAVE_SOCKADDR_IN_SIN_LEN
40                 sa->v4.sin_len = sizeof (*len);
41 #endif
42         } else if (family == AF_INET6) {
43                 *len = sizeof (struct sockaddr_in6);
44
45                 sa->v6.sin6_family = family;
46                 sa->v6.sin6_addr = *(struct in6_addr*)address;
47                 sa->v6.sin6_port = htons (port);
48 #if HAVE_SOCKADDR_IN6_SIN_LEN
49                 sa->v6.sin6_len = sizeof (*len);
50 #endif
51         } else {
52                 g_error ("Cannot handle address family %d", family);
53         }
54 }