3 * Modern posix networking code
6 * Rodrigo Kumpera (kumpera@gmail.com)
17 #ifdef HAVE_SYS_IOCTL_H
18 #include <sys/ioctl.h>
26 #ifdef HAVE_GETIFADDRS
30 #include <mono/utils/networking.h>
31 #include <mono/utils/mono-threads-coop.h>
34 get_address_from_sockaddr (struct sockaddr *sa)
36 switch (sa->sa_family) {
38 return &((struct sockaddr_in*)sa)->sin_addr;
40 return &((struct sockaddr_in6*)sa)->sin6_addr;
45 #ifdef HAVE_GETADDRINFO
48 mono_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **result)
50 char service_name [16];
51 struct addrinfo hints, *res = NULL, *info;
52 MonoAddressEntry *cur = NULL, *prev = NULL;
53 MonoAddressInfo *addr_info;
56 memset (&hints, 0, sizeof (struct addrinfo));
59 hints.ai_family = PF_UNSPEC;
60 if (flags & MONO_HINT_IPV4)
61 hints.ai_family = PF_INET;
62 else if (flags & MONO_HINT_IPV6)
63 hints.ai_family = PF_INET6;
65 hints.ai_socktype = SOCK_STREAM;
67 if (flags & MONO_HINT_CANONICAL_NAME)
68 hints.ai_flags = AI_CANONNAME;
70 /* Some ancient libc don't define AI_ADDRCONFIG */
72 if (flags & MONO_HINT_CONFIGURED_ONLY)
73 hints.ai_flags = AI_ADDRCONFIG;
75 sprintf (service_name, "%d", port);
78 ret = getaddrinfo (hostname, service_name, &hints, &info);
82 return 1; /* FIXME propagate the error */
85 *result = addr_info = g_new0 (MonoAddressInfo, 1);
88 cur = g_new0 (MonoAddressEntry, 1);
89 cur->family = res->ai_family;
90 cur->socktype = res->ai_socktype;
91 cur->protocol = res->ai_protocol;
92 if (cur->family == PF_INET) {
93 cur->address_len = sizeof (struct in_addr);
94 cur->address.v4 = ((struct sockaddr_in*)res->ai_addr)->sin_addr;
95 } else if (cur->family == PF_INET6) {
96 cur->address_len = sizeof (struct in6_addr);
97 cur->address.v6 = ((struct sockaddr_in6*)res->ai_addr)->sin6_addr;
99 g_warning ("Cannot handle address family %d", cur->family);
105 if (res->ai_canonname)
106 cur->canonical_name = g_strdup (res->ai_canonname);
111 addr_info->entries = cur;
123 #ifdef HAVE_GETPROTOBYNAME
126 fetch_protocol (const char *proto_name, int *cache, int *proto, int default_val)
129 struct protoent *pent;
131 pent = getprotobyname (proto_name);
132 *proto = pent ? pent->p_proto : default_val;
139 mono_networking_get_tcp_protocol (void)
141 static int cache, proto;
142 return fetch_protocol ("tcp", &cache, &proto, 6); //6 is SOL_TCP on linux
146 mono_networking_get_ip_protocol (void)
148 static int cache, proto;
149 return fetch_protocol ("ip", &cache, &proto, 0); //0 is SOL_IP on linux
153 mono_networking_get_ipv6_protocol (void)
155 static int cache, proto;
156 return fetch_protocol ("ipv6", &cache, &proto, 41); //41 is SOL_IPV6 on linux
161 #if defined (HAVE_SIOCGIFCONF)
163 #define IFCONF_BUFF_SIZE 1024
164 #ifndef _SIZEOF_ADDR_IFREQ
165 #define _SIZEOF_ADDR_IFREQ(ifr) (sizeof (struct ifreq))
168 #define FOREACH_IFR(IFR, IFC) \
169 for (IFR = (IFC).ifc_req; \
170 ifr < (struct ifreq*)((char*)(IFC).ifc_req + (IFC).ifc_len); \
171 ifr = (struct ifreq*)((char*)(IFR) + _SIZEOF_ADDR_IFREQ (*(IFR))))
174 mono_get_local_interfaces (int family, int *interface_count)
180 gboolean ignore_loopback = FALSE;
184 *interface_count = 0;
186 if (!mono_address_size_for_family (family))
189 fd = socket (family, SOCK_STREAM, 0);
193 memset (&ifc, 0, sizeof (ifc));
194 ifc.ifc_len = IFCONF_BUFF_SIZE;
195 ifc.ifc_buf = (char *)g_malloc (IFCONF_BUFF_SIZE); /* We can't have such huge buffers on the stack. */
196 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0)
199 FOREACH_IFR (ifr, ifc) {
202 //only return addresses of the same type as @family
203 if (ifr->ifr_addr.sa_family != family) {
204 ifr->ifr_name [0] = '\0';
208 strcpy (iflags.ifr_name, ifr->ifr_name);
210 //ignore interfaces we can't get props for
211 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
212 ifr->ifr_name [0] = '\0';
216 //ignore interfaces that are down
217 if ((iflags.ifr_flags & IFF_UP) == 0) {
218 ifr->ifr_name [0] = '\0';
222 //If we have a non-loopback iface, don't return any loopback
223 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
224 ignore_loopback = TRUE;
225 ifr->ifr_name [0] = 1;//1 means non-loopback
227 ifr->ifr_name [0] = 2; //2 means loopback
232 result = (char *)g_malloc (if_count * mono_address_size_for_family (family));
233 result_ptr = (char *)result;
234 FOREACH_IFR (ifr, ifc) {
235 if (ifr->ifr_name [0] == '\0')
238 if (ignore_loopback && ifr->ifr_name [0] == 2) {
243 memcpy (result_ptr, get_address_from_sockaddr (&ifr->ifr_addr), mono_address_size_for_family (family));
244 result_ptr += mono_address_size_for_family (family);
246 g_assert (result_ptr <= (char*)result + if_count * mono_address_size_for_family (family));
249 *interface_count = if_count;
250 g_free (ifc.ifc_buf);
255 #elif defined(HAVE_GETIFADDRS)
258 mono_get_local_interfaces (int family, int *interface_count)
260 struct ifaddrs *ifap = NULL, *cur;
262 gboolean ignore_loopback = FALSE;
266 *interface_count = 0;
268 if (!mono_address_size_for_family (family))
271 if (getifaddrs (&ifap))
274 for (cur = ifap; cur; cur = cur->ifa_next) {
275 //ignore interfaces with no address assigned
279 //ignore interfaces that don't belong to @family
280 if (cur->ifa_addr->sa_family != family)
283 //ignore interfaces that are down
284 if ((cur->ifa_flags & IFF_UP) == 0)
287 //If we have a non-loopback iface, don't return any loopback
288 if ((cur->ifa_flags & IFF_LOOPBACK) == 0)
289 ignore_loopback = TRUE;
294 result_ptr = result = g_malloc (if_count * mono_address_size_for_family (family));
295 for (cur = ifap; cur; cur = cur->ifa_next) {
298 if (cur->ifa_addr->sa_family != family)
300 if ((cur->ifa_flags & IFF_UP) == 0)
303 //we decrement if_count because it did not on the previous loop.
304 if (ignore_loopback && (cur->ifa_flags & IFF_LOOPBACK)) {
309 memcpy (result_ptr, get_address_from_sockaddr (cur->ifa_addr), mono_address_size_for_family (family));
310 result_ptr += mono_address_size_for_family (family);
312 g_assert (result_ptr <= (char*)result + if_count * mono_address_size_for_family (family));
315 *interface_count = if_count;
321 #ifdef HAVE_GETNAMEINFO
324 mono_networking_addr_to_str (MonoAddress *address, char *buffer, socklen_t buflen)
326 MonoSocketAddress saddr;
328 mono_socket_address_init (&saddr, &len, address->family, &address->addr, 0);
330 return getnameinfo (&saddr.addr, len, buffer, buflen, NULL, 0, NI_NUMERICHOST) == 0;
336 mono_networking_addr_to_str (MonoAddress *address, char *buffer, socklen_t buflen)
338 return inet_ntop (address->family, &address->addr, buffer, buflen) != NULL;
344 // These are already defined in networking-windows.c for Windows
346 mono_networking_init (void)
352 mono_networking_shutdown (void)