Merge pull request #1390 from woodsb02/FreeBSDMacNetworkInterfaces
[mono.git] / mono / metadata / socket-io.c
index 145633f943585e969c37d9507460cfad1d255c52..0b75405bd0acd42aca213b10a011e745d1ecc636 100644 (file)
 
 #ifndef DISABLE_SOCKETS
 
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__FreeBSD__)
 #define __APPLE_USE_RFC_3542
 #endif
 
 #include <glib.h>
 #include <string.h>
 #include <stdlib.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#include <errno.h>
-
-#include <sys/types.h>
 #ifdef HOST_WIN32
 #include <ws2tcpip.h>
 #else
 #include <netdb.h>
 #include <arpa/inet.h>
 #endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <errno.h>
+
+#include <sys/types.h>
 
 #include <mono/metadata/object.h>
 #include <mono/io-layer/io-layer.h>
 #define AI_ADDRCONFIG 0
 #endif
 
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__FreeBSD__)
 /*
  * We remove this until we have a Darwin implementation
  * that can walk the result of struct ifconf.  The current
@@ -1216,7 +1216,7 @@ static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
                /* Need a byte for the '\0' terminator/prefix, and the first
                 * two bytes hold the SocketAddress family
                 */
-               if (len - 2 >= MONO_SIZEOF_SUNPATH) {
+               if (len - 2 >= sizeof(sock_un->sun_path)) {
                        mono_raise_exception (mono_get_exception_index_out_of_range ());
                }
                
@@ -1824,8 +1824,8 @@ static MonoObject* int_to_object (MonoDomain *domain, int val)
 
 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
 {
-       int system_level;
-       int system_name;
+       int system_level = 0;
+       int system_name = 0;
        int ret;
        int val;
        socklen_t valsize=sizeof(val);
@@ -1979,8 +1979,8 @@ void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET soc
 
 void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
 {
-       int system_level;
-       int system_name;
+       int system_level = 0;
+       int system_name = 0;
        int ret;
        guchar *buf;
        socklen_t valsize;
@@ -2051,7 +2051,7 @@ static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
 #endif /* AF_INET6 */
 #endif
 
-#if defined(__APPLE__)
+#if defined(__APPLE__) || defined(__FreeBSD__)
 
 #if defined(HAVE_GETIFADDRS) && defined(HAVE_IF_NAMETOINDEX)
 static int
@@ -2089,13 +2089,13 @@ get_local_interface_id (int family)
 }
 #endif
 
-#endif /* __APPLE__ */
+#endif /* defined(__APPLE__) || defined(__FreeBSD__) */
 
 void ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal(SOCKET sock, gint32 level, gint32 name, MonoObject *obj_val, MonoArray *byte_val, gint32 int_val, gint32 *error)
 {
        struct linger linger;
-       int system_level;
-       int system_name;
+       int system_level = 0;
+       int system_name = 0;
        int ret;
 #ifdef AF_INET6
        int sol_ip;
@@ -2187,7 +2187,7 @@ void ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal(SOCKET sock, g
                                field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
                                mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
                                
-#if defined(__APPLE__)
+#if defined(__APPLE__) || defined(__FreeBSD__)
                                /*
                                * Bug #5504:
                                *
@@ -3018,6 +3018,7 @@ MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, Mo
        hints.ai_flags = AI_CANONNAME;
 
        if (*hostname && getaddrinfo(hostname, NULL, &hints, &info) == -1) {
+               g_free (hostname);
                return(FALSE);
        }
        
@@ -3106,10 +3107,13 @@ MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, Mo
 #else
        he = _wapi_gethostbyname (hostname);
 #endif
-       g_free(hostname);
 
-       if (*hostname && he==NULL)
+       if (*hostname && he==NULL) {
+               g_free (hostname);
                return(FALSE);
+       }
+
+       g_free (hostname);
 
        return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
 }