Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / metadata / mono-route.c
index 4b5ca588056df60fcb60404b58fee9d8bd18fd79..c90af3cdfe33b8b89d1ce23f65a14e42046ff636 100644 (file)
@@ -1,13 +1,15 @@
-/*
- * mono-route.c: Read the network routing tables using sysctl(3) calls
- *               Required for Unix-like systems that don't have Linux's /proc/net/route
+/**
+ * \file
+ * Read the network routing tables using sysctl(3) calls
+ * Required for Unix-like systems that don't have Linux's /proc/net/route
  *
  * Author:
  *   Ben Woods (woodsb02@gmail.com)
  */
 
-#if defined(PLATFORM_MACOSX) || defined(PLATFORM_BSD)
+#include <config.h>
 
+#if defined(HOST_DARWIN) || defined(HOST_BSD)
 #include <sys/socket.h>
 #include <net/if.h>
 #include <net/if_dl.h>
@@ -21,6 +23,7 @@
 
 extern MonoBoolean ves_icall_System_Net_NetworkInformation_MacOsIPInterfaceProperties_ParseRouteInfo_internal(MonoString *iface, MonoArray **gw_addr_list)
 {
+       MonoError error;
        size_t needed;
        in_addr_t in;
        int mib[6];
@@ -31,7 +34,10 @@ extern MonoBoolean ves_icall_System_Net_NetworkInformation_MacOsIPInterfacePrope
 
        MonoDomain *domain = mono_domain_get ();
 
-       ifacename = mono_string_to_utf8(iface);
+       ifacename = mono_string_to_utf8_checked(iface, &error);
+       if (mono_error_set_pending_exception (&error))
+               return FALSE;
+
        if ((ifindex = if_nametoindex(ifacename)) == 0)
                return FALSE;
        g_free(ifacename);
@@ -49,12 +55,14 @@ extern MonoBoolean ves_icall_System_Net_NetworkInformation_MacOsIPInterfacePrope
                return FALSE;
 
        // Allocate suffcient memory for available data based on the previous sysctl call
-       if ((buf = malloc(needed)) == NULL)
+       if ((buf = g_malloc (needed)) == NULL)
                return FALSE;
 
        // Second sysctl call to retrieve data into appropriately sized buffer
-       if (sysctl(mib, G_N_ELEMENTS(mib), buf, &needed, NULL, 0) < 0)
+       if (sysctl(mib, G_N_ELEMENTS(mib), buf, &needed, NULL, 0) < 0) {
+               g_free (buf);
                return FALSE;
+       }
 
        lim = buf + needed;
        for (next = buf; next < lim; next += rtm->rtm_msglen) {
@@ -68,7 +76,9 @@ extern MonoBoolean ves_icall_System_Net_NetworkInformation_MacOsIPInterfacePrope
                num_gws++;
        }
 
-       *gw_addr_list = mono_array_new(domain, mono_get_string_class (), num_gws);
+       *gw_addr_list = mono_array_new_checked (domain, mono_get_string_class (), num_gws, &error);
+       if (!is_ok (&error))
+               goto leave;
 
        for (next = buf; next < lim; next += rtm->rtm_msglen) {
                rtm = (struct rt_msghdr *)next;
@@ -94,12 +104,15 @@ extern MonoBoolean ves_icall_System_Net_NetworkInformation_MacOsIPInterfacePrope
                        // snprintf output truncated
                        continue;
 
-               addr_string = mono_string_new (domain, addr);
+               addr_string = mono_string_new_checked (domain, addr, &error);
+               if (!is_ok (&error))
+                       goto leave;
                mono_array_setref (*gw_addr_list, gwnum, addr_string);
                gwnum++;
        }
-       free(buf);
-       return TRUE;
+leave:
+       g_free (buf);
+       return is_ok (&error);
 }
 
 in_addr_t gateway_from_rtm(struct rt_msghdr *rtm)
@@ -121,4 +134,4 @@ in_addr_t gateway_from_rtm(struct rt_msghdr *rtm)
        return 0;
 }
 
-#endif /* #if defined(PLATFORM_MACOSX) || defined(PLATFORM_BSD) */
+#endif /* #if defined(HOST_DARWIN) || defined(HOST_BSD) */