CVS: winex/dlls/iphlpapi ipstats.c,1.4,1.5

[email protected]
Newsgroups gmane.comp.emulators.winex.cvs
Message-ID <[email protected]>
Subject: winex/dlls/iphlpapi ipstats.c,1.4,1.5Update of /var/lib/cvsd/cvsroot/winex/dlls/iphlpapi
In directory agravaine:/tmp/cvs-serv15478/dlls/iphlpapi

Modified Files:
	ipstats.c 
Log Message:

- add error messages when unimplemented functions are called on MacOS X instead of just failing silently
- implement retrieving the interface list on MacOS X



Index: ipstats.c
===================================================================
RCS file: /var/lib/cvsd/cvsroot/winex/dlls/iphlpapi/ipstats.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- ipstats.c	30 Mar 2007 20:11:02 -0000	1.4
+++ ipstats.c	30 Mar 2007 20:18:03 -0000	1.5
@@ -20,6 +20,7 @@
 
 #include "config.h"
 #include "wine/port.h"
+#include "wine/debug.h"
 
 #include <stdarg.h>
 #include <stdio.h>
@@ -51,6 +52,14 @@
 #include <netinet/tcp_fsm.h>
 #endif
 
+#ifdef __APPLE__
+#include <sys/sysctl.h>
+
+#define ROUNDUP(a) \
+	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
+#define ADVANCE(x, n) (x += ROUNDUP(((struct sockaddr *)n)->sa_len))
+#endif
+
 #include "windef.h"
 #include "winbase.h"
 #include "iprtrmib.h"
@@ -71,6 +80,8 @@
 #define TCPS_CLOSING     11
 #endif
 
+WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
+
 DWORD getInterfaceStatsByName(const char *name, PMIB_IFROW entry)
 {
   FILE *fp;
@@ -80,6 +91,10 @@
   if (!entry)
     return ERROR_INVALID_PARAMETER;
 
+#ifdef __APPLE__
+  ERR ("unimplemented!\n");
+#endif
+
   /* get interface stats from /proc/net/dev, no error if can't
      no inUnknownProtos, outNUcastPkts, outQLen */
   fp = fopen("/proc/net/dev", "r");
@@ -162,6 +177,10 @@
   if (!stats)
     return ERROR_INVALID_PARAMETER;
 
+#ifdef __APPLE__
+  ERR ("unimplemented!\n");
+#endif
+
   memset(stats, 0, sizeof(MIB_ICMP));
   /* get most of these stats from /proc/net/snmp, no error if can't */
   fp = fopen("/proc/net/snmp", "r");
@@ -289,6 +308,10 @@
   if (!stats)
     return ERROR_INVALID_PARAMETER;
 
+#ifdef __APPLE__
+  ERR ("unimplemented!\n");
+#endif
+
   memset(stats, 0, sizeof(MIB_IPSTATS));
   stats->dwNumIf = stats->dwNumAddr = getNumInterfaces();
   stats->dwNumRoutes = getNumRoutes();
@@ -400,6 +423,10 @@
   if (!stats)
     return ERROR_INVALID_PARAMETER;
 
+#ifdef __APPLE__
+  ERR ("unimplemented!\n");
+#endif
+
   memset(stats, 0, sizeof(MIB_TCPSTATS));
 
   /* get from /proc/net/snmp, no error if can't */
@@ -490,6 +517,10 @@
   if (!stats)
     return ERROR_INVALID_PARAMETER;
 
+#ifdef __APPLE__
+  ERR ("unimplemented!\n");
+#endif
+
   memset(stats, 0, sizeof(MIB_UDPSTATS));
 
   /* get from /proc/net/snmp, no error if can't */
@@ -561,7 +592,58 @@
 
 DWORD getNumRoutes(void)
 {
-  return getNumWithOneHeader("/proc/net/route");
+#ifdef __APPLE__
+   int mib[6] = {CTL_NET, PF_ROUTE, 0, PF_INET, NET_RT_DUMP, 0};
+   size_t needed;
+   char *buf, *lim, *next;
+   struct rt_msghdr *rtm;
+   DWORD RouteCount = 0;
+
+   if (sysctl (mib, 6, NULL, &needed, NULL, 0) < 0)
+   {
+      ERR ("sysctl 1 failed!\n");
+      return 0;
+   }
+
+   buf = HeapAlloc (GetProcessHeap (), 0, needed);
+   if (!buf)
+   {
+      ERR ("HeapAlloc of %ld failed!\n", needed);
+      return 0;
+   }
+
+   if (sysctl (mib, 6, buf, &needed, NULL, 0) < 0)
+   {
+      ERR ("sysctl 2 failed!\n");
+      HeapFree (GetProcessHeap (), 0, buf);
+      return 0;
+   }
+
+   lim = buf + needed;
+   for (next = buf; next < lim; next += rtm->rtm_msglen)
+   {
+      rtm = (struct rt_msghdr *)next;
+
+      if (rtm->rtm_type != RTM_GET)
+      {
+         WARN ("Got unexpected message type 0x%x!\n",
+               rtm->rtm_type);
+         continue;
+      }
+
+      /* Ignore all entries except for gateway routes which aren't
+         multicast */
+      if (!(rtm->rtm_flags & RTF_GATEWAY) || (rtm->rtm_flags & RTF_MULTICAST))
+         continue;
+
+      RouteCount++;
+   }
+
+   HeapFree (GetProcessHeap (), 0, buf);
+   return RouteCount;
+#else
+   return getNumWithOneHeader("/proc/net/route");
+#endif
 }
 
 DWORD getRouteTable(PMIB_IPFORWARDTABLE *ppIpForwardTable, HANDLE heap,
@@ -577,6 +659,122 @@
      sizeof(MIB_IPFORWARDTABLE) + (numRoutes - 1) * sizeof(MIB_IPFORWARDROW));
 
     if (table) {
+#ifdef __APPLE__
+       int mib[6] = {CTL_NET, PF_ROUTE, 0, PF_INET, NET_RT_DUMP, 0};
+       size_t needed;
+       char *buf, *lim, *next, *addrPtr;
+       struct rt_msghdr *rtm;
+
+       if (sysctl (mib, 6, NULL, &needed, NULL, 0) < 0)
+       {
+          ERR ("sysctl 1 failed!\n");
+          HeapFree (GetProcessHeap (), 0, table);
+          return NO_ERROR;
+       }
+
+       buf = HeapAlloc (GetProcessHeap (), 0, needed);
+       if (!buf)
+       {
+          ERR ("HeapAlloc of %ld failed!\n", needed);
+          HeapFree (GetProcessHeap (), 0, table);
+          return ERROR_OUTOFMEMORY;
+       }
+
+       if (sysctl (mib, 6, buf, &needed, NULL, 0) < 0)
+       {
+          ERR ("sysctl 2 failed!\n");
+          HeapFree (GetProcessHeap (), 0, table);
+          HeapFree (GetProcessHeap (), 0, buf);
+          return NO_ERROR;
+       }
+
+       *ppIpForwardTable = table;
+       table->dwNumEntries = 0;
+
+       lim = buf + needed;
+       for (next = buf; next < lim; next += rtm->rtm_msglen)
+       {
+          int i;
+
+          rtm = (struct rt_msghdr *)next;
+
+          if (rtm->rtm_type != RTM_GET)
+          {
+             WARN ("Got unexpected message type 0x%x!\n",
+                   rtm->rtm_type);
+             continue;
+          }
+
+          /* Ignore all entries except for gateway routes which aren't
+             multicast */
+          if (!(rtm->rtm_flags & RTF_GATEWAY) ||
+              (rtm->rtm_flags & RTF_MULTICAST))
+             continue;
+
+          memset (&table->table[table->dwNumEntries], 0,
+                  sizeof (MIB_IPFORWARDROW));
+          table->table[table->dwNumEntries].dwForwardIfIndex = rtm->rtm_index;
+          table->table[table->dwNumEntries].dwForwardType =
+             MIB_IPROUTE_TYPE_INDIRECT;
+          table->table[table->dwNumEntries].dwForwardMetric1 =
+             rtm->rtm_rmx.rmx_hopcount;
+          table->table[table->dwNumEntries].dwForwardProto =
+             MIB_IPPROTO_LOCAL;
+
+          addrPtr = (char *)(rtm + 1);
+
+          for (i = 1; i; i <<= 1)
+          {
+             struct sockaddr *sa;
+             DWORD addr;
+
+             if (!(i & rtm->rtm_addrs))
+                continue;
+
+             sa = (struct sockaddr *)addrPtr;
+             ADVANCE (addrPtr, sa);
+
+             /* default routes are encoded by length-zero sockaddr */
+             if (sa->sa_len == 0)
+                addr = 0;
+             else if (sa->sa_family != AF_INET)
+             {
+                ERR ("Received unsupported sockaddr family 0x%x\n",
+                     sa->sa_family);
+                addr = 0;
+             }
+             else
+             {
+                struct sockaddr_in *sin = (struct sockaddr_in *)sa;
+
+                addr = sin->sin_addr.s_addr;
+             }
+
+             switch (i)
+             {
+                case RTA_DST:
+                   table->table[table->dwNumEntries].dwForwardDest = addr;
+                   break;
+
+                case RTA_GATEWAY:
+                   table->table[table->dwNumEntries].dwForwardNextHop = addr;
+                   break;
+
+                case RTA_NETMASK:
+                   table->table[table->dwNumEntries].dwForwardMask = addr;
+                   break;
+
+                default:
+                   ERR ("Unexpected address type 0x%x\n", i);
+             }
+          }
+
+          table->dwNumEntries++;
+       }
+
+       HeapFree (GetProcessHeap (), 0, buf);
+       ret = NO_ERROR;
+#else
       FILE *fp;
 
       ret = NO_ERROR;
@@ -656,6 +854,7 @@
         }
         fclose(fp);
       }
+#endif
     }
     else
       ret = ERROR_OUTOFMEMORY;
@@ -665,6 +864,10 @@
 
 DWORD getNumArpEntries(void)
 {
+#ifdef __APPLE__
+  ERR ("unimplemented!\n");
+#endif
+
   return getNumWithOneHeader("/proc/net/arp");
 }
 
@@ -672,6 +875,10 @@
 {
   DWORD ret;
 
+#ifdef __APPLE__
+  ERR ("unimplemented!\n");
+#endif
+
   if (!ppIpNetTable)
     ret = ERROR_INVALID_PARAMETER;
   else {
@@ -758,6 +965,10 @@
 
 DWORD getNumUdpEntries(void)
 {
+#ifdef __APPLE__
+  ERR ("unimplemented!\n");
+#endif
+
   return getNumWithOneHeader("/proc/net/udp");
 }
 
@@ -765,6 +976,10 @@
 {
   DWORD ret;
 
+#ifdef __APPLE__
+  ERR ("unimplemented!\n");
+#endif
+
   if (!ppUdpTable)
     ret = ERROR_INVALID_PARAMETER;
   else {
@@ -821,6 +1036,10 @@
 
 DWORD getNumTcpEntries(void)
 {
+#ifdef __APPLE__
+  ERR ("unimplemented!\n");
+#endif
+
   return getNumWithOneHeader("/proc/net/tcp");
 }
 
@@ -828,6 +1047,10 @@
 {
   DWORD ret;
 
+#ifdef __APPLE__
+  ERR ("unimplemented!\n");
+#endif
+
   if (!ppTcpTable)
     ret = ERROR_INVALID_PARAMETER;
   else {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.