2 * iphlpapi dll implementation
4 * Copyright (C) 2003 Juan Lang
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
26 #ifdef HAVE_NETINET_IN_H
27 # include <netinet/in.h>
29 #ifdef HAVE_ARPA_NAMESER_H
30 # include <arpa/nameser.h>
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
47 /******************************************************************
48 * AddIPAddress (IPHLPAPI.@)
57 * NTEInstance [In/Out]
64 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
67 /* marking Win2K+ functions not supported */
68 return ERROR_NOT_SUPPORTED;
72 /******************************************************************
73 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
78 * ppIfTable [Out] -- pointer into which the MIB_IFTABLE is
79 * allocated and returned.
80 * bOrder [In] -- passed to GetIfTable to order the table
81 * heap [In] -- heap from which the table is allocated
82 * flags [In] -- flags to HeapAlloc
84 * RETURNS -- ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
85 * GetIfTable returns otherwise
88 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
89 BOOL bOrder, HANDLE heap, DWORD flags)
94 ret = ERROR_INVALID_PARAMETER;
98 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
99 if (ret == ERROR_INSUFFICIENT_BUFFER) {
100 *ppIfTable = (PMIB_IFTABLE)HeapAlloc(heap, flags, dwSize);
101 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
108 /******************************************************************
109 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
114 * ppIpAddrTable [Out]
115 * bOrder [In] -- passed to GetIpAddrTable to order the table
116 * heap [In] -- heap from which the table is allocated
117 * flags [In] -- flags to HeapAlloc
124 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
125 BOOL bOrder, HANDLE heap, DWORD flags)
130 ret = ERROR_INVALID_PARAMETER;
134 ret = GetIpAddrTable(*ppIpAddrTable, &dwSize, bOrder);
135 if (ret == ERROR_INSUFFICIENT_BUFFER) {
136 *ppIpAddrTable = (PMIB_IPADDRTABLE)HeapAlloc(heap, flags, dwSize);
137 ret = GetIpAddrTable(*ppIpAddrTable, &dwSize, bOrder);
144 /******************************************************************
145 * AllocateAndGetIpForwardTableFromStack (IPHLPAPI.@)
148 * ppIpForwardTable [Out] -- pointer into which the MIB_IPFORWARDTABLE is
149 * allocated and returned.
150 * bOrder [In] -- passed to GetIfTable to order the table
151 * heap [In] -- heap from which the table is allocated
152 * flags [In] -- flags to HeapAlloc
154 * RETURNS -- ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
155 * GetIpForwardTable returns otherwise
158 DWORD WINAPI AllocateAndGetIpForwardTableFromStack(PMIB_IPFORWARDTABLE *
159 ppIpForwardTable, BOOL bOrder, HANDLE heap, DWORD flags)
163 if (!ppIpForwardTable)
164 ret = ERROR_INVALID_PARAMETER;
168 ret = GetIpForwardTable(*ppIpForwardTable, &dwSize, bOrder);
169 if (ret == ERROR_INSUFFICIENT_BUFFER) {
170 *ppIpForwardTable = (PMIB_IPFORWARDTABLE)HeapAlloc(heap, flags, dwSize);
171 ret = GetIpForwardTable(*ppIpForwardTable, &dwSize, bOrder);
178 /******************************************************************
179 * AllocateAndGetIpNetTableFromStack (IPHLPAPI.@)
185 * bOrder [In] -- passed to GetIpNetTable to order the table
186 * heap [In] -- heap from which the table is allocated
187 * flags [In] -- flags to HeapAlloc
194 DWORD WINAPI AllocateAndGetIpNetTableFromStack(PMIB_IPNETTABLE *ppIpNetTable,
195 BOOL bOrder, HANDLE heap, DWORD flags)
200 ret = ERROR_INVALID_PARAMETER;
204 ret = GetIpNetTable(*ppIpNetTable, &dwSize, bOrder);
205 if (ret == ERROR_INSUFFICIENT_BUFFER) {
206 *ppIpNetTable = (PMIB_IPNETTABLE)HeapAlloc(heap, flags, dwSize);
207 ret = GetIpNetTable(*ppIpNetTable, &dwSize, bOrder);
214 /******************************************************************
215 * AllocateAndGetTcpTableFromStack (IPHLPAPI.@)
221 * bOrder [In] -- passed to GetTcpTable to order the table
222 * heap [In] -- heap from which the table is allocated
223 * flags [In] -- flags to HeapAlloc
230 DWORD WINAPI AllocateAndGetTcpTableFromStack(PMIB_TCPTABLE *ppTcpTable,
231 BOOL bOrder, HANDLE heap, DWORD flags)
236 ret = ERROR_INVALID_PARAMETER;
240 ret = GetTcpTable(*ppTcpTable, &dwSize, bOrder);
241 if (ret == ERROR_INSUFFICIENT_BUFFER) {
242 *ppTcpTable = (PMIB_TCPTABLE)HeapAlloc(heap, flags, dwSize);
243 ret = GetTcpTable(*ppTcpTable, &dwSize, bOrder);
250 /******************************************************************
251 * AllocateAndGetUdpTableFromStack (IPHLPAPI.@)
257 * bOrder [In] -- passed to GetUdpTable to order the table
258 * heap [In] -- heap from which the table is allocated
259 * flags [In] -- flags to HeapAlloc
266 DWORD WINAPI AllocateAndGetUdpTableFromStack(PMIB_UDPTABLE *ppUdpTable,
267 BOOL bOrder, HANDLE heap, DWORD flags)
272 ret = ERROR_INVALID_PARAMETER;
276 ret = GetUdpTable(*ppUdpTable, &dwSize, bOrder);
277 if (ret == ERROR_INSUFFICIENT_BUFFER) {
278 *ppUdpTable = (PMIB_UDPTABLE)HeapAlloc(heap, flags, dwSize);
279 ret = GetUdpTable(*ppUdpTable, &dwSize, bOrder);
286 /******************************************************************
287 * CreateIpForwardEntry (IPHLPAPI.@)
299 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
301 /* could use SIOCADDRT, not sure I want to */
307 /******************************************************************
308 * CreateIpNetEntry (IPHLPAPI.@)
320 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
322 /* could use SIOCSARP on systems that support it, not sure I want to */
328 /******************************************************************
329 * CreateProxyArpEntry (IPHLPAPI.@)
343 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
346 /* marking Win2K+ functions not supported */
347 return ERROR_NOT_SUPPORTED;
351 /******************************************************************
352 * DeleteIPAddress (IPHLPAPI.@)
364 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
367 /* marking Win2K+ functions not supported */
368 return ERROR_NOT_SUPPORTED;
372 /******************************************************************
373 * DeleteIpForwardEntry (IPHLPAPI.@)
385 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
387 /* could use SIOCDELRT, not sure I want to */
393 /******************************************************************
394 * DeleteIpNetEntry (IPHLPAPI.@)
406 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
408 /* could use SIOCDARP on systems that support it, not sure I want to */
414 /******************************************************************
415 * DeleteProxyArpEntry (IPHLPAPI.@)
429 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
432 /* marking Win2K+ functions not supported */
433 return ERROR_NOT_SUPPORTED;
437 /******************************************************************
438 * EnableRouter (IPHLPAPI.@)
444 * pOverlapped [In/Out]
451 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
454 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
455 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
456 marking Win2K+ functions not supported */
457 return ERROR_NOT_SUPPORTED;
461 /******************************************************************
462 * FlushIpNetTable (IPHLPAPI.@)
474 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
477 /* this flushes the arp cache of the given index
478 marking Win2K+ functions not supported */
479 return ERROR_NOT_SUPPORTED;
483 /******************************************************************
484 * GetAdapterIndex (IPHLPAPI.@)
489 * AdapterName [In/Out]
497 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
500 /* marking Win2K+ functions not supported */
501 return ERROR_NOT_SUPPORTED;
505 /******************************************************************
506 * GetAdaptersInfo (IPHLPAPI.@)
511 * pAdapterInfo [In/Out]
512 * pOutBufLen [In/Out]
519 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
524 ret = ERROR_INVALID_PARAMETER;
526 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
528 if (numNonLoopbackInterfaces > 0) {
529 /* this calculation assumes only one address in the IP_ADDR_STRING lists.
530 that's okay, because:
531 - we don't get multiple addresses per adapter anyway
532 - we don't know about per-adapter gateways
533 - we don't know about DHCP or WINS (and these must be single anyway) */
534 ULONG size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
536 if (!pAdapterInfo || *pOutBufLen < size) {
538 ret = ERROR_BUFFER_OVERFLOW;
541 InterfaceIndexTable *table = getNonLoopbackInterfaceIndexTable();
544 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
545 if (*pOutBufLen < size) {
547 ret = ERROR_INSUFFICIENT_BUFFER;
552 memset(pAdapterInfo, 0, size);
553 for (ndx = 0; ndx < table->numIndexes; ndx++) {
554 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
555 DWORD addrLen = sizeof(ptr->Address), type;
557 /* on Win98 this is left empty, but whatever */
558 strncpy(ptr->AdapterName,
559 getInterfaceNameByIndex(table->indexes[ndx]),
560 sizeof(ptr->AdapterName));
561 ptr->AdapterName[MAX_ADAPTER_NAME_LENGTH] = '\0';
562 getInterfacePhysicalByIndex(table->indexes[ndx], &addrLen,
563 ptr->Address, &type);
564 /* MS defines address length and type as UINT in some places and
565 DWORD in others, **sigh**. Don't want to assume that PUINT and
566 PDWORD are equiv (64-bit?) */
567 ptr->AddressLength = addrLen;
569 ptr->Index = table->indexes[ndx];
570 toIPAddressString(getInterfaceIPAddrByIndex(table->indexes[ndx]),
571 ptr->IpAddressList.IpAddress.String);
572 toIPAddressString(getInterfaceMaskByIndex(table->indexes[ndx]),
573 ptr->IpAddressList.IpMask.String);
574 if (ndx < table->numIndexes + 1)
575 ptr->Next = (ndx == table->numIndexes - 1) ? NULL : &pAdapterInfo[ndx + 1];
582 ret = ERROR_OUTOFMEMORY;
592 /******************************************************************
593 * GetBestInterface (IPHLPAPI.@)
599 * pdwBestIfIndex [In/Out]
606 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
611 ret = ERROR_INVALID_PARAMETER;
613 MIB_IPFORWARDROW ipRow;
615 ret = GetBestRoute(dwDestAddr, 0, &ipRow);
616 if (ret == ERROR_SUCCESS)
617 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
623 /******************************************************************
624 * GetBestRoute (IPHLPAPI.@)
638 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
640 PMIB_IPFORWARDTABLE table;
644 return ERROR_INVALID_PARAMETER;
646 AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
648 DWORD ndx, matchedBits, matchedNdx = 0;
650 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
651 if ((dwDestAddr & table->table[ndx].dwForwardMask) ==
652 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
653 DWORD numShifts, mask;
655 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
656 mask && !(mask & 1); mask >>= 1, numShifts++)
658 if (numShifts > matchedBits) {
659 matchedBits = numShifts;
664 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
665 HeapFree(GetProcessHeap(), 0, table);
669 ret = ERROR_OUTOFMEMORY;
674 /******************************************************************
675 * GetFriendlyIfIndex (IPHLPAPI.@)
687 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
689 /* windows doesn't validate these, either, just makes sure the top byte is
690 cleared. I assume my ifenum module never gives an index with the top
696 /******************************************************************
697 * GetIcmpStatistics (IPHLPAPI.@)
709 DWORD WINAPI GetIcmpStatistics(PMIB_ICMP pStats)
711 return getICMPStats(pStats);
715 /******************************************************************
716 * GetIfEntry (IPHLPAPI.@)
728 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
734 return ERROR_INVALID_PARAMETER;
736 name = getInterfaceNameByIndex(pIfRow->dwIndex);
738 ret = getInterfaceEntryByName(name, pIfRow);
740 ret = getInterfaceStatsByName(name, pIfRow);
743 ret = ERROR_INVALID_DATA;
748 static int IfTableSorter(const void *a, const void *b)
753 ret = ((PMIB_IFROW)a)->dwIndex - ((PMIB_IFROW)b)->dwIndex;
760 /******************************************************************
761 * GetIfTable (IPHLPAPI.@)
775 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
780 ret = ERROR_INVALID_PARAMETER;
782 DWORD numInterfaces = getNumInterfaces();
783 ULONG size = sizeof(MIB_IFTABLE) + (numInterfaces - 1) * sizeof(MIB_IFROW);
785 if (!pIfTable || *pdwSize < size) {
787 ret = ERROR_INSUFFICIENT_BUFFER;
790 InterfaceIndexTable *table = getInterfaceIndexTable();
793 size = sizeof(MIB_IFTABLE) + (table->numIndexes - 1) *
795 if (*pdwSize < size) {
797 ret = ERROR_INSUFFICIENT_BUFFER;
802 pIfTable->dwNumEntries = 0;
803 for (ndx = 0; ndx < table->numIndexes; ndx++) {
804 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
805 GetIfEntry(&pIfTable->table[ndx]);
806 pIfTable->dwNumEntries++;
809 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
816 ret = ERROR_OUTOFMEMORY;
823 /******************************************************************
824 * GetInterfaceInfo (IPHLPAPI.@)
830 * dwOutBufLen [In/Out]
837 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
842 ret = ERROR_INVALID_PARAMETER;
844 DWORD numInterfaces = getNumInterfaces();
845 ULONG size = sizeof(IP_INTERFACE_INFO) + (numInterfaces - 1) *
846 sizeof(IP_ADAPTER_INDEX_MAP);
848 if (!pIfTable || *dwOutBufLen < size) {
850 ret = ERROR_INSUFFICIENT_BUFFER;
853 InterfaceIndexTable *table = getInterfaceIndexTable();
856 size = sizeof(IP_INTERFACE_INFO) + (table->numIndexes - 1) *
857 sizeof(IP_ADAPTER_INDEX_MAP);
858 if (*dwOutBufLen < size) {
860 ret = ERROR_INSUFFICIENT_BUFFER;
865 pIfTable->NumAdapters = 0;
866 for (ndx = 0; ndx < table->numIndexes; ndx++) {
867 const char *walker, *name;
870 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
871 name = getInterfaceNameByIndex(table->indexes[ndx]);
872 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
874 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
875 walker++, assigner++)
878 pIfTable->NumAdapters++;
885 ret = ERROR_OUTOFMEMORY;
892 static int IpAddrTableSorter(const void *a, const void *b)
897 ret = ((PMIB_IPADDRROW)a)->dwAddr - ((PMIB_IPADDRROW)b)->dwAddr;
904 /******************************************************************
905 * GetIpAddrTable (IPHLPAPI.@)
910 * pIpAddrTable [In/Out]
919 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
924 ret = ERROR_INVALID_PARAMETER;
926 DWORD numInterfaces = getNumInterfaces();
927 ULONG size = sizeof(MIB_IPADDRTABLE) + (numInterfaces - 1) *
928 sizeof(MIB_IPADDRROW);
930 if (!pIpAddrTable || *pdwSize < size) {
932 ret = ERROR_INSUFFICIENT_BUFFER;
935 InterfaceIndexTable *table = getInterfaceIndexTable();
938 size = sizeof(MIB_IPADDRTABLE) + (table->numIndexes - 1) *
939 sizeof(MIB_IPADDRROW);
940 if (*pdwSize < size) {
942 ret = ERROR_INSUFFICIENT_BUFFER;
947 pIpAddrTable->dwNumEntries = 0;
948 for (ndx = 0; ndx < table->numIndexes; ndx++) {
949 pIpAddrTable->table[ndx].dwIndex = table->indexes[ndx];
950 pIpAddrTable->table[ndx].dwAddr =
951 getInterfaceIPAddrByIndex(table->indexes[ndx]);
952 pIpAddrTable->table[ndx].dwMask =
953 getInterfaceMaskByIndex(table->indexes[ndx]);
954 pIpAddrTable->table[ndx].dwBCastAddr =
955 getInterfaceBCastAddrByIndex(table->indexes[ndx]);
956 /* FIXME: hardcoded reasm size, not sure where to get it */
957 pIpAddrTable->table[ndx].dwReasmSize = 65535;
958 pIpAddrTable->table[ndx].unused1 = 0;
959 pIpAddrTable->table[ndx].wType = 0; /* aka unused2 */
960 pIpAddrTable->dwNumEntries++;
963 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
964 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
970 ret = ERROR_OUTOFMEMORY;
977 static int IpForwardTableSorter(const void *a, const void *b)
982 PMIB_IPFORWARDROW rowA = (PMIB_IPFORWARDROW)a, rowB = (PMIB_IPFORWARDROW)b;
984 ret = rowA->dwForwardDest - rowB->dwForwardDest;
986 ret = rowA->dwForwardProto - rowB->dwForwardProto;
988 ret = rowA->dwForwardPolicy - rowB->dwForwardPolicy;
990 ret = rowA->dwForwardNextHop - rowB->dwForwardNextHop;
1000 /******************************************************************
1001 * GetIpForwardTable (IPHLPAPI.@)
1006 * pIpForwardTable [In/Out]
1015 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1020 ret = ERROR_INVALID_PARAMETER;
1022 DWORD numRoutes = getNumRoutes();
1023 ULONG sizeNeeded = sizeof(MIB_IPFORWARDTABLE) + (numRoutes - 1) *
1024 sizeof(MIB_IPFORWARDROW);
1026 if (!pIpForwardTable || *pdwSize < sizeNeeded) {
1027 *pdwSize = sizeNeeded;
1028 ret = ERROR_INSUFFICIENT_BUFFER;
1031 RouteTable *table = getRouteTable();
1033 sizeNeeded = sizeof(MIB_IPFORWARDTABLE) + (table->numRoutes - 1) *
1034 sizeof(MIB_IPFORWARDROW);
1035 if (*pdwSize < sizeNeeded) {
1036 *pdwSize = sizeNeeded;
1037 ret = ERROR_INSUFFICIENT_BUFFER;
1042 pIpForwardTable->dwNumEntries = table->numRoutes;
1043 for (ndx = 0; ndx < numRoutes; ndx++) {
1044 pIpForwardTable->table[ndx].dwForwardIfIndex =
1045 table->routes[ndx].ifIndex;
1046 pIpForwardTable->table[ndx].dwForwardDest =
1047 table->routes[ndx].dest;
1048 pIpForwardTable->table[ndx].dwForwardMask =
1049 table->routes[ndx].mask;
1050 pIpForwardTable->table[ndx].dwForwardPolicy = 0;
1051 pIpForwardTable->table[ndx].dwForwardNextHop =
1052 table->routes[ndx].gateway;
1053 /* FIXME: this type is appropriate for local interfaces; may not
1054 always be appropriate */
1055 pIpForwardTable->table[ndx].dwForwardType = MIB_IPROUTE_TYPE_DIRECT;
1056 /* FIXME: other protos might be appropriate, e.g. the default route
1057 is typically set with MIB_IPPROTO_NETMGMT instead */
1058 pIpForwardTable->table[ndx].dwForwardProto = MIB_IPPROTO_LOCAL;
1059 /* punt on age and AS */
1060 pIpForwardTable->table[ndx].dwForwardAge = 0;
1061 pIpForwardTable->table[ndx].dwForwardNextHopAS = 0;
1062 pIpForwardTable->table[ndx].dwForwardMetric1 =
1063 table->routes[ndx].metric;
1064 /* rest of the metrics are 0.. */
1065 pIpForwardTable->table[ndx].dwForwardMetric2 = 0;
1066 pIpForwardTable->table[ndx].dwForwardMetric3 = 0;
1067 pIpForwardTable->table[ndx].dwForwardMetric4 = 0;
1068 pIpForwardTable->table[ndx].dwForwardMetric5 = 0;
1071 qsort(pIpForwardTable->table, pIpForwardTable->dwNumEntries,
1072 sizeof(MIB_IPFORWARDROW), IpForwardTableSorter);
1078 ret = ERROR_OUTOFMEMORY;
1085 static int IpNetTableSorter(const void *a, const void *b)
1090 ret = ((PMIB_IPNETROW)a)->dwAddr - ((PMIB_IPNETROW)b)->dwAddr;
1097 /******************************************************************
1098 * GetIpNetTable (IPHLPAPI.@)
1103 * pIpNetTable [In/Out]
1112 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1117 ret = ERROR_INVALID_PARAMETER;
1119 DWORD numEntries = getNumArpEntries();
1120 ULONG size = sizeof(MIB_IPNETTABLE) + (numEntries - 1) *
1121 sizeof(MIB_IPNETROW);
1123 if (!pIpNetTable || *pdwSize < size) {
1125 ret = ERROR_INSUFFICIENT_BUFFER;
1128 PMIB_IPNETTABLE table = getArpTable();
1131 size = sizeof(MIB_IPNETTABLE) + (table->dwNumEntries - 1) *
1132 sizeof(MIB_IPNETROW);
1133 if (*pdwSize < size) {
1135 ret = ERROR_INSUFFICIENT_BUFFER;
1138 memcpy(pIpNetTable, table, size);
1140 qsort(pIpNetTable->table, pIpNetTable->dwNumEntries,
1141 sizeof(MIB_IPNETROW), IpNetTableSorter);
1147 ret = ERROR_OUTOFMEMORY;
1154 /******************************************************************
1155 * GetIpStatistics (IPHLPAPI.@)
1167 DWORD WINAPI GetIpStatistics(PMIB_IPSTATS pStats)
1169 return getIPStats(pStats);
1173 /******************************************************************
1174 * GetNetworkParams (IPHLPAPI.@)
1179 * pFixedInfo [In/Out]
1180 * pOutBufLen [In/Out]
1187 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1193 return ERROR_INVALID_PARAMETER;
1196 size = sizeof(FIXED_INFO) + (_res.nscount > 0 ? (_res.nscount - 1) *
1197 sizeof(IP_ADDR_STRING) : 0);
1198 if (!pFixedInfo || *pOutBufLen < size) {
1200 return ERROR_BUFFER_OVERFLOW;
1203 memset(pFixedInfo, 0, size);
1204 size = sizeof(pFixedInfo->HostName);
1205 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1206 size = sizeof(pFixedInfo->DomainName);
1207 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1208 if (_res.nscount > 0) {
1209 PIP_ADDR_STRING ptr;
1212 for (i = 0, ptr = &pFixedInfo->DnsServerList; i < _res.nscount && ptr;
1213 i++, ptr = ptr->Next) {
1214 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1215 ptr->IpAddress.String);
1216 if (i == _res.nscount - 1)
1219 ptr->Next = (PIP_ADDR_STRING)((LPBYTE)pFixedInfo + sizeof(FIXED_INFO));
1221 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1224 pFixedInfo->NodeType = HYBRID_NODETYPE;
1225 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1226 "\\SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey)
1229 DWORD size = sizeof(pFixedInfo->ScopeId);
1231 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, pFixedInfo->ScopeId, &size);
1235 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1236 I suppose could also check for a listener on port 53 to set EnableDns */
1241 /******************************************************************
1242 * GetNumberOfInterfaces (IPHLPAPI.@)
1254 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1259 ret = ERROR_INVALID_PARAMETER;
1261 *pdwNumIf = getNumInterfaces();
1268 /******************************************************************
1269 * GetPerAdapterInfo (IPHLPAPI.@)
1275 * pPerAdapterInfo [In/Out]
1276 * pOutBufLen [In/Out]
1283 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1286 /* marking Win2K+ functions not supported */
1287 return ERROR_NOT_SUPPORTED;
1291 /******************************************************************
1292 * GetRTTAndHopCount (IPHLPAPI.@)
1297 * DestIpAddress [In]
1307 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1314 /******************************************************************
1315 * GetTcpStatistics (IPHLPAPI.@)
1327 DWORD WINAPI GetTcpStatistics(PMIB_TCPSTATS pStats)
1329 return getTCPStats(pStats);
1333 static int TcpTableSorter(const void *a, const void *b)
1338 PMIB_TCPROW rowA = (PMIB_TCPROW)a, rowB = (PMIB_TCPROW)b;
1340 ret = rowA->dwLocalAddr - rowB->dwLocalAddr;
1342 ret = rowA->dwLocalPort - rowB->dwLocalPort;
1344 ret = rowA->dwRemoteAddr - rowB->dwRemoteAddr;
1346 ret = rowA->dwRemotePort - rowB->dwRemotePort;
1356 /******************************************************************
1357 * GetTcpTable (IPHLPAPI.@)
1362 * pTcpTable [In/Out]
1371 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1376 ret = ERROR_INVALID_PARAMETER;
1378 DWORD numEntries = getNumTcpEntries();
1379 ULONG size = sizeof(MIB_TCPTABLE) + (numEntries - 1) * sizeof(MIB_TCPROW);
1381 if (!pTcpTable || *pdwSize < size) {
1383 ret = ERROR_INSUFFICIENT_BUFFER;
1386 PMIB_TCPTABLE table = getTcpTable();
1389 size = sizeof(MIB_TCPTABLE) + (table->dwNumEntries - 1) *
1391 if (*pdwSize < size) {
1393 ret = ERROR_INSUFFICIENT_BUFFER;
1396 memcpy(pTcpTable, table, size);
1398 qsort(pTcpTable->table, pTcpTable->dwNumEntries,
1399 sizeof(MIB_TCPROW), TcpTableSorter);
1405 ret = ERROR_OUTOFMEMORY;
1412 /******************************************************************
1413 * GetUdpStatistics (IPHLPAPI.@)
1425 DWORD WINAPI GetUdpStatistics(PMIB_UDPSTATS pStats)
1427 return getUDPStats(pStats);
1431 static int UdpTableSorter(const void *a, const void *b)
1436 PMIB_UDPROW rowA = (PMIB_UDPROW)a, rowB = (PMIB_UDPROW)b;
1438 ret = rowA->dwLocalAddr - rowB->dwLocalAddr;
1440 ret = rowA->dwLocalPort - rowB->dwLocalPort;
1448 /******************************************************************
1449 * GetUdpTable (IPHLPAPI.@)
1454 * pUdpTable [In/Out]
1463 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1468 ret = ERROR_INVALID_PARAMETER;
1470 DWORD numEntries = getNumUdpEntries();
1471 ULONG size = sizeof(MIB_UDPTABLE) + (numEntries - 1) * sizeof(MIB_UDPROW);
1473 if (!pUdpTable || *pdwSize < size) {
1475 ret = ERROR_INSUFFICIENT_BUFFER;
1478 PMIB_UDPTABLE table = getUdpTable();
1481 size = sizeof(MIB_UDPTABLE) + (table->dwNumEntries - 1) *
1483 if (*pdwSize < size) {
1485 ret = ERROR_INSUFFICIENT_BUFFER;
1488 memcpy(pUdpTable, table, size);
1490 qsort(pUdpTable->table, pUdpTable->dwNumEntries,
1491 sizeof(MIB_UDPROW), UdpTableSorter);
1497 ret = ERROR_OUTOFMEMORY;
1504 /******************************************************************
1505 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1510 * pIPIfInfo [In/Out]
1511 * dwOutBufLen [In/Out]
1518 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1520 /* a unidirectional adapter?? not bloody likely! */
1521 return ERROR_NOT_SUPPORTED;
1525 /******************************************************************
1526 * IpReleaseAddress (IPHLPAPI.@)
1531 * AdapterInfo [In/Out]
1538 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1540 /* not a stub, never going to support this (and I never mark an adapter as
1541 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1542 return ERROR_NOT_SUPPORTED;
1546 /******************************************************************
1547 * IpRenewAddress (IPHLPAPI.@)
1552 * AdapterInfo [In/Out]
1559 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1561 /* not a stub, never going to support this (and I never mark an adapter as
1562 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1563 return ERROR_NOT_SUPPORTED;
1567 /******************************************************************
1568 * NotifyAddrChange (IPHLPAPI.@)
1574 * overlapped [In/Out]
1581 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1584 /* marking Win2K+ functions not supported */
1585 return ERROR_NOT_SUPPORTED;
1589 /******************************************************************
1590 * NotifyRouteChange (IPHLPAPI.@)
1596 * overlapped [In/Out]
1603 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1606 /* marking Win2K+ functions not supported */
1607 return ERROR_NOT_SUPPORTED;
1611 /******************************************************************
1612 * SendARP (IPHLPAPI.@)
1620 * PhyAddrLen [In/Out]
1627 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1630 /* marking Win2K+ functions not supported */
1631 return ERROR_NOT_SUPPORTED;
1635 /******************************************************************
1636 * SetIfEntry (IPHLPAPI.@)
1648 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1650 /* this is supposed to set an administratively interface up or down.
1651 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
1652 this sort of down is indistinguishable from other sorts of down (e.g. no
1655 return ERROR_NOT_SUPPORTED;
1659 /******************************************************************
1660 * SetIpForwardEntry (IPHLPAPI.@)
1672 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1674 /* this is to add a route entry, how's it distinguishable from
1675 CreateIpForwardEntry?
1676 could use SIOCADDRT, not sure I want to */
1682 /******************************************************************
1683 * SetIpNetEntry (IPHLPAPI.@)
1688 * pArpEntry [In/Out]
1695 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1697 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1703 /******************************************************************
1704 * SetIpStatistics (IPHLPAPI.@)
1716 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1723 /******************************************************************
1724 * SetIpTTL (IPHLPAPI.@)
1736 DWORD WINAPI SetIpTTL(UINT nTTL)
1738 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
1739 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
1745 /******************************************************************
1746 * SetTcpEntry (IPHLPAPI.@)
1758 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
1765 /******************************************************************
1766 * UnenableRouter (IPHLPAPI.@)
1771 * pOverlapped [In/Out]
1772 * lpdwEnableCount [In/Out]
1779 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
1782 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
1783 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
1784 marking Win2K+ functions not supported */
1785 return ERROR_NOT_SUPPORTED;