2 * networking-posix.c: Modern posix networking code
5 * Rodrigo Kumpera (kumpera@gmail.com)
10 #include <mono/utils/networking.h>
16 #ifdef HAVE_SYS_IOCTL_H
17 #include <sys/ioctl.h>
25 #ifdef HAVE_GETIFADDRS
30 get_address_from_sockaddr (struct sockaddr *sa)
32 switch (sa->sa_family) {
34 return &((struct sockaddr_in*)sa)->sin_addr;
36 return &((struct sockaddr_in6*)sa)->sin6_addr;
41 #ifdef HAVE_GETADDRINFO
44 mono_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **result)
46 char service_name [16];
47 struct addrinfo hints, *res = NULL, *info;
48 MonoAddressEntry *cur = NULL, *prev = NULL;
49 MonoAddressInfo *addr_info;
51 memset (&hints, 0, sizeof (struct addrinfo));
54 hints.ai_family = PF_UNSPEC;
55 if (flags & MONO_HINT_IPV4)
56 hints.ai_family = PF_INET;
57 else if (flags & MONO_HINT_IPV6)
58 hints.ai_family = PF_INET6;
60 hints.ai_socktype = SOCK_STREAM;
62 if (flags & MONO_HINT_CANONICAL_NAME)
63 hints.ai_flags = AI_CANONNAME;
65 /* Some ancient libc don't define AI_ADDRCONFIG */
67 if (flags & MONO_HINT_CONFIGURED_ONLY)
68 hints.ai_flags = AI_ADDRCONFIG;
70 sprintf (service_name, "%d", port);
71 if (getaddrinfo (hostname, service_name, &hints, &info))
72 return 1; /* FIXME propagate the error */
75 *result = addr_info = g_new0 (MonoAddressInfo, 1);
78 cur = g_new0 (MonoAddressEntry, 1);
79 cur->family = res->ai_family;
80 cur->socktype = res->ai_socktype;
81 cur->protocol = res->ai_protocol;
82 if (cur->family == PF_INET) {
83 cur->address_len = sizeof (struct in_addr);
84 cur->address.v4 = ((struct sockaddr_in*)res->ai_addr)->sin_addr;
85 } else if (cur->family == PF_INET6) {
86 cur->address_len = sizeof (struct in6_addr);
87 cur->address.v6 = ((struct sockaddr_in6*)res->ai_addr)->sin6_addr;
89 g_warning ("Cannot handle address family %d", cur->family);
95 if (res->ai_canonname)
96 cur->canonical_name = g_strdup (res->ai_canonname);
101 addr_info->entries = cur;
113 #ifdef HAVE_GETPROTOBYNAME
116 fetch_protocol (const char *proto_name, int *cache, int *proto, int default_val)
119 struct protoent *pent;
121 pent = getprotobyname (proto_name);
122 *proto = pent ? pent->p_proto : default_val;
129 mono_networking_get_tcp_protocol (void)
131 static int cache, proto;
132 return fetch_protocol ("tcp", &cache, &proto, 6); //6 is SOL_TCP on linux
136 mono_networking_get_ip_protocol (void)
138 static int cache, proto;
139 return fetch_protocol ("ip", &cache, &proto, 0); //0 is SOL_IP on linux
143 mono_networking_get_ipv6_protocol (void)
145 static int cache, proto;
146 return fetch_protocol ("ipv6", &cache, &proto, 41); //41 is SOL_IPV6 on linux
151 #if defined (HAVE_SIOCGIFCONF)
153 #define IFCONF_BUFF_SIZE 1024
154 #ifndef _SIZEOF_ADDR_IFREQ
155 #define _SIZEOF_ADDR_IFREQ(ifr) (sizeof (struct ifreq))
158 #define FOREACH_IFR(IFR, IFC) \
159 for (IFR = (IFC).ifc_req; \
160 ifr < (struct ifreq*)((char*)(IFC).ifc_req + (IFC).ifc_len); \
161 ifr = (struct ifreq*)((char*)(IFR) + _SIZEOF_ADDR_IFREQ (*(IFR))))
164 mono_get_local_interfaces (int family, int *interface_count)
170 gboolean ignore_loopback = FALSE;
174 *interface_count = 0;
176 if (!mono_address_size_for_family (family))
179 fd = socket (family, SOCK_STREAM, 0);
183 memset (&ifc, 0, sizeof (ifc));
184 ifc.ifc_len = IFCONF_BUFF_SIZE;
185 ifc.ifc_buf = g_malloc (IFCONF_BUFF_SIZE); /* We can't have such huge buffers on the stack. */
186 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0)
189 FOREACH_IFR (ifr, ifc) {
192 //only return addresses of the same type as @family
193 if (ifr->ifr_addr.sa_family != family) {
194 ifr->ifr_name [0] = '\0';
198 strcpy (iflags.ifr_name, ifr->ifr_name);
200 //ignore interfaces we can't get props for
201 if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
202 ifr->ifr_name [0] = '\0';
206 //ignore interfaces that are down
207 if ((iflags.ifr_flags & IFF_UP) == 0) {
208 ifr->ifr_name [0] = '\0';
212 //If we have a non-loopback iface, don't return any loopback
213 if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
214 ignore_loopback = TRUE;
215 ifr->ifr_name [0] = 1;//1 means non-loopback
217 ifr->ifr_name [0] = 2; //2 means loopback
222 result_ptr = result = g_malloc (if_count * mono_address_size_for_family (family));
223 FOREACH_IFR (ifr, ifc) {
224 if (ifr->ifr_name [0] == '\0')
227 if (ignore_loopback && ifr->ifr_name [0] == 2) {
232 memcpy (result_ptr, get_address_from_sockaddr (&ifr->ifr_addr), mono_address_size_for_family (family));
233 result_ptr += mono_address_size_for_family (family);
235 g_assert (result_ptr <= (char*)result + if_count * mono_address_size_for_family (family));
238 *interface_count = if_count;
239 g_free (ifc.ifc_buf);
244 #elif defined(HAVE_GETIFADDRS)
247 mono_get_local_interfaces (int family, int *interface_count)
249 struct ifaddrs *ifap = NULL, *cur;
251 gboolean ignore_loopback = FALSE;
255 *interface_count = 0;
257 if (!mono_address_size_for_family (family))
260 if (getifaddrs (&ifap))
263 for (cur = ifap; cur; cur = cur->ifa_next) {
264 //ignore interfaces with no address assigned
268 //ignore interfaces that don't belong to @family
269 if (cur->ifa_addr->sa_family != family)
272 //ignore interfaces that are down
273 if ((cur->ifa_flags & IFF_UP) == 0)
276 //If we have a non-loopback iface, don't return any loopback
277 if ((cur->ifa_flags & IFF_LOOPBACK) == 0)
278 ignore_loopback = TRUE;
283 result_ptr = result = g_malloc (if_count * mono_address_size_for_family (family));
284 for (cur = ifap; cur; cur = cur->ifa_next) {
287 if (cur->ifa_addr->sa_family != family)
289 if ((cur->ifa_flags & IFF_UP) == 0)
292 //we decrement if_count because it did not on the previous loop.
293 if (ignore_loopback && (cur->ifa_flags & IFF_LOOPBACK)) {
298 memcpy (result_ptr, get_address_from_sockaddr (cur->ifa_addr), mono_address_size_for_family (family));
299 result_ptr += mono_address_size_for_family (family);
301 g_assert (result_ptr <= (char*)result + if_count * mono_address_size_for_family (family));
304 *interface_count = if_count;
310 #ifdef HAVE_GETNAMEINFO
313 mono_networking_addr_to_str (MonoAddress *address, char *buffer, socklen_t buflen)
315 MonoSocketAddress saddr;
317 mono_socket_address_init (&saddr, &len, address->family, &address->addr, 0);
319 return getnameinfo (&saddr.addr, len, buffer, buflen, NULL, 0, NI_NUMERICHOST) == 0;
325 mono_networking_addr_to_str (MonoAddress *address, char *buffer, socklen_t buflen)
327 return inet_ntop (address->family, &address->addr, buffer, buflen) != NULL;
333 // These are already defined in networking-windows.c for Windows
335 mono_networking_init (void)
341 mono_networking_shutdown (void)