Restructure of mono.sln and build properties to better fix static/dynamic library...
[mono.git] / support / nl.c
index 756df906a6909464ff5b21e7a091b2947d4ddf4a..88babdae391c1b41ad995c9f3c48c697ac0c87ec 100644 (file)
@@ -7,7 +7,10 @@
  * Copyright (c) Novell, Inc. 2011
  */
 
+#ifdef HAVE_CONFIG_H
 #include <config.h>
+#endif
+
 #include "nl.h"
 
 #if defined(HAVE_LINUX_NETLINK_H) && defined(HAVE_LINUX_RTNETLINK_H)
 #include <linux/rtnetlink.h>
 
 #undef NL_DEBUG
+#define NL_DEBUG_STMT(a) do { } while (0)
 #define NL_DEBUG_PRINT(...)
+
 /*
 #define NL_DEBUG 1
+#define NL_DEBUG_STMT(a) do { a } while (0)
 #define NL_DEBUG_PRINT(...) g_message(__VA_ARGS__)
 */
 
+
+#ifdef AF_INET6
+#define ADDR_BYTE_LENGTH 16
+#define ADDR_STR_LENGTH INET6_ADDRSTRLEN
+#else
+#define ADDR_LENGTH     4
+#define ADDR_STR_LENGTH INET_ADDRSTRLEN
+#endif
+
 enum event_type {
        EVT_NONE = 0,
 #define EVT_NONE 0
@@ -158,14 +173,12 @@ CreateNLSocket (void)
        sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
 
        ret = fcntl (sock, F_GETFL, 0);
-#ifdef O_NONBLOCK
        if (ret != -1) {
                ret |= O_NONBLOCK;
                ret = fcntl (sock, F_SETFL, ret);
                if (ret < 0)
                        return -1;
        }
-#endif
 
        memset (&sa, 0, sizeof (sa));
        if (sock < 0)
@@ -208,24 +221,19 @@ ReadEvents (gpointer sock, gpointer buffer, gint32 count, gint32 size)
                int addr_length;
                int msg_type;
                int table;
+#ifdef NL_DEBUG
                int protocol;
                int scope;
+#endif
                int rtm_type;
                gboolean have_dst;
                gboolean have_src;
                gboolean have_pref_src;
                gboolean have_gw;
-#ifdef AF_INET6
-               char dst [16];
-               char src [16];
-               char pref_src [16];
-               char gw [16];
-#else
-               char dst [4];
-               char src [4];
-               char pref_src [4];
-               char gw [4];
-#endif
+               char dst [ADDR_BYTE_LENGTH];
+               char src [ADDR_BYTE_LENGTH];
+               char pref_src [ADDR_BYTE_LENGTH];
+               char gw [ADDR_BYTE_LENGTH];
 
                msg_type = nlp->nlmsg_type;
                NL_DEBUG_PRINT ("TYPE: %d %s", msg_type, FIND_RT_TYPE_NAME (msg_type));
@@ -235,13 +243,19 @@ ReadEvents (gpointer sock, gpointer buffer, gint32 count, gint32 size)
                rtp = (struct rtmsg *) NLMSG_DATA (nlp);
                family = rtp->rtm_family;
 #ifdef AF_INET6
-               addr_length = (family == AF_INET) ? 4 : 16;
+               if (family != AF_INET && family != AF_INET6) {
 #else
-               addr_length = 4;
+               if (family != AF_INET) {
 #endif
+                       continue;
+               }
+
+               addr_length = (family == AF_INET) ? 4 : 16;
                table = rtp->rtm_table;
+#ifdef NL_DEBUG
                protocol = rtp->rtm_protocol;
                scope = rtp->rtm_scope;
+#endif
                rtm_type = rtp->rtm_type;
                NL_DEBUG_PRINT ("\tRTMSG table: %d %s", table, FIND_RT_TABLE_NAME (table));
                if (table != RT_TABLE_MAIN && table != RT_TABLE_LOCAL)
@@ -266,13 +280,7 @@ ReadEvents (gpointer sock, gpointer buffer, gint32 count, gint32 size)
                for(; RTA_OK (rtap, rtl); rtap = RTA_NEXT(rtap, rtl)) {
                        char *data;
 #ifdef NL_DEBUG
-#ifdef AF_INET6
-                       char ip [INET6_ADDRSTRLEN];
-                       int ip_length = INET6_ADDRSTRLEN;
-#else
-                       char ip [INET_ADDRSTRLEN];
-                       int ip_length = INET_ADDRSTRLEN;
-#endif
+                       char ip [ADDR_STR_LENGTH];
 #endif
 
                        NL_DEBUG_PRINT ("\tAttribute: %d %d (%s)", rtap->rta_len, rtap->rta_type, FIND_RTM_ATTRS_NAME (rtap->rta_type));
@@ -281,38 +289,38 @@ ReadEvents (gpointer sock, gpointer buffer, gint32 count, gint32 size)
                        case RTA_DST:
                                have_dst = TRUE;
                                memcpy (dst, data, addr_length);
-#ifdef NL_DEBUG
-                               *ip = 0;
-                               inet_ntop (family, RTA_DATA (rtap), ip, ip_length);
-                               NL_DEBUG_PRINT ("\t\tDst: %s", ip);
-#endif
+                               NL_DEBUG_STMT (
+                                       *ip = 0;
+                                       inet_ntop (family, RTA_DATA (rtap), ip, sizeof (ip));
+                                       NL_DEBUG_PRINT ("\t\tDst: %s", ip);
+                               );
                                break;
                        case RTA_PREFSRC:
                                have_pref_src = TRUE;
                                memcpy (pref_src, data, addr_length);
-#ifdef NL_DEBUG
-                               *ip = 0;
-                               inet_ntop (family, RTA_DATA (rtap), ip, ip_length);
-                               NL_DEBUG_PRINT ("\t\tPref. Src.: %s", ip);
-#endif
+                               NL_DEBUG_STMT (
+                                       *ip = 0;
+                                       inet_ntop (family, RTA_DATA (rtap), ip, sizeof (ip));
+                                       NL_DEBUG_PRINT ("\t\tPref. Src.: %s", ip);
+                               );
                                break;
                        case RTA_SRC:
                                have_src = TRUE;
                                memcpy (src, data, addr_length);
-#ifdef NL_DEBUG
-                               *ip = 0;
-                               inet_ntop (family, RTA_DATA (rtap), ip, ip_length);
-                               NL_DEBUG_PRINT ("\tSrc: %s", ip);
-#endif
+                               NL_DEBUG_STMT (
+                                       *ip = 0;
+                                       inet_ntop (family, RTA_DATA (rtap), ip, sizeof (ip));
+                                       NL_DEBUG_PRINT ("\tSrc: %s", ip);
+                               );
                                break;
                        case RTA_GATEWAY:
                                have_gw = TRUE;
                                memcpy (gw, data, addr_length);
-#ifdef NL_DEBUG
-                               *ip = 0;
-                               inet_ntop (family, RTA_DATA (rtap), ip, ip_length);
-                               NL_DEBUG_PRINT ("\t\tGateway: %s", ip);
-#endif
+                               NL_DEBUG_STMT (
+                                       *ip = 0;
+                                       inet_ntop (family, RTA_DATA (rtap), ip, sizeof (ip));
+                                       NL_DEBUG_PRINT ("\t\tGateway: %s", ip);
+                               );
                                break;
                        default:
                                break;
@@ -369,6 +377,12 @@ ReadEvents (gpointer sock, gpointer buffer, gint32 count, gint32 size)
        return 0;
 }
 
+int
+CreateNLSocket (void)
+{
+       return -1;
+}
+
 int
 CloseNLSocket (gpointer sock)
 {