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 = &pAdapterInfo[ndx + 1];
584 ret = ERROR_OUTOFMEMORY;
594 /******************************************************************
595 * GetBestInterface (IPHLPAPI.@)
601 * pdwBestIfIndex [In/Out]
608 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
613 ret = ERROR_INVALID_PARAMETER;
615 MIB_IPFORWARDROW ipRow;
617 ret = GetBestRoute(dwDestAddr, 0, &ipRow);
618 if (ret == ERROR_SUCCESS)
619 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
625 /******************************************************************
626 * GetBestRoute (IPHLPAPI.@)
640 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
642 PMIB_IPFORWARDTABLE table;
646 return ERROR_INVALID_PARAMETER;
648 AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
650 DWORD ndx, matchedBits, matchedNdx = 0;
652 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
653 if ((dwDestAddr & table->table[ndx].dwForwardMask) ==
654 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
655 DWORD numShifts, mask;
657 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
658 mask && !(mask & 1); mask >>= 1, numShifts++)
660 if (numShifts > matchedBits) {
661 matchedBits = numShifts;
666 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
667 HeapFree(GetProcessHeap(), 0, table);
671 ret = ERROR_OUTOFMEMORY;
676 /******************************************************************
677 * GetFriendlyIfIndex (IPHLPAPI.@)
689 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
691 /* windows doesn't validate these, either, just makes sure the top byte is
692 cleared. I assume my ifenum module never gives an index with the top
698 /******************************************************************
699 * GetIcmpStatistics (IPHLPAPI.@)
711 DWORD WINAPI GetIcmpStatistics(PMIB_ICMP pStats)
713 return getICMPStats(pStats);
717 /******************************************************************
718 * GetIfEntry (IPHLPAPI.@)
730 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
736 return ERROR_INVALID_PARAMETER;
738 name = getInterfaceNameByIndex(pIfRow->dwIndex);
740 ret = getInterfaceEntryByName(name, pIfRow);
742 ret = getInterfaceStatsByName(name, pIfRow);
745 ret = ERROR_INVALID_DATA;
750 static int IfTableSorter(const void *a, const void *b)
755 ret = ((PMIB_IFROW)a)->dwIndex - ((PMIB_IFROW)b)->dwIndex;
762 /******************************************************************
763 * GetIfTable (IPHLPAPI.@)
777 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
782 ret = ERROR_INVALID_PARAMETER;
784 DWORD numInterfaces = getNumInterfaces();
785 ULONG size = sizeof(MIB_IFTABLE) + (numInterfaces - 1) * sizeof(MIB_IFROW);
787 if (!pIfTable || *pdwSize < size) {
789 ret = ERROR_INSUFFICIENT_BUFFER;
792 InterfaceIndexTable *table = getInterfaceIndexTable();
795 size = sizeof(MIB_IFTABLE) + (table->numIndexes - 1) *
797 if (*pdwSize < size) {
799 ret = ERROR_INSUFFICIENT_BUFFER;
804 pIfTable->dwNumEntries = 0;
805 for (ndx = 0; ndx < table->numIndexes; ndx++) {
806 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
807 GetIfEntry(&pIfTable->table[ndx]);
808 pIfTable->dwNumEntries++;
811 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
818 ret = ERROR_OUTOFMEMORY;
825 /******************************************************************
826 * GetInterfaceInfo (IPHLPAPI.@)
832 * dwOutBufLen [In/Out]
839 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
844 ret = ERROR_INVALID_PARAMETER;
846 DWORD numInterfaces = getNumInterfaces();
847 ULONG size = sizeof(IP_INTERFACE_INFO) + (numInterfaces - 1) *
848 sizeof(IP_ADAPTER_INDEX_MAP);
850 if (!pIfTable || *dwOutBufLen < size) {
852 ret = ERROR_INSUFFICIENT_BUFFER;
855 InterfaceIndexTable *table = getInterfaceIndexTable();
858 size = sizeof(IP_INTERFACE_INFO) + (table->numIndexes - 1) *
859 sizeof(IP_ADAPTER_INDEX_MAP);
860 if (*dwOutBufLen < size) {
862 ret = ERROR_INSUFFICIENT_BUFFER;
867 pIfTable->NumAdapters = 0;
868 for (ndx = 0; ndx < table->numIndexes; ndx++) {
869 const char *walker, *name;
872 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
873 name = getInterfaceNameByIndex(table->indexes[ndx]);
874 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
876 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
877 walker++, assigner++)
880 pIfTable->NumAdapters++;
887 ret = ERROR_OUTOFMEMORY;
894 static int IpAddrTableSorter(const void *a, const void *b)
899 ret = ((PMIB_IPADDRROW)a)->dwAddr - ((PMIB_IPADDRROW)b)->dwAddr;
906 /******************************************************************
907 * GetIpAddrTable (IPHLPAPI.@)
912 * pIpAddrTable [In/Out]
921 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
926 ret = ERROR_INVALID_PARAMETER;
928 DWORD numInterfaces = getNumInterfaces();
929 ULONG size = sizeof(MIB_IPADDRTABLE) + (numInterfaces - 1) *
930 sizeof(MIB_IPADDRROW);
932 if (!pIpAddrTable || *pdwSize < size) {
934 ret = ERROR_INSUFFICIENT_BUFFER;
937 InterfaceIndexTable *table = getInterfaceIndexTable();
940 size = sizeof(MIB_IPADDRTABLE) + (table->numIndexes - 1) *
941 sizeof(MIB_IPADDRROW);
942 if (*pdwSize < size) {
944 ret = ERROR_INSUFFICIENT_BUFFER;
949 pIpAddrTable->dwNumEntries = 0;
950 for (ndx = 0; ndx < table->numIndexes; ndx++) {
951 pIpAddrTable->table[ndx].dwIndex = table->indexes[ndx];
952 pIpAddrTable->table[ndx].dwAddr =
953 getInterfaceIPAddrByIndex(table->indexes[ndx]);
954 pIpAddrTable->table[ndx].dwMask =
955 getInterfaceMaskByIndex(table->indexes[ndx]);
956 pIpAddrTable->table[ndx].dwBCastAddr =
957 getInterfaceBCastAddrByIndex(table->indexes[ndx]);
958 /* FIXME: hardcoded reasm size, not sure where to get it */
959 pIpAddrTable->table[ndx].dwReasmSize = 65535;
960 pIpAddrTable->table[ndx].unused1 = 0;
961 pIpAddrTable->table[ndx].wType = 0; /* aka unused2 */
962 pIpAddrTable->dwNumEntries++;
965 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
966 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
972 ret = ERROR_OUTOFMEMORY;
979 static int IpForwardTableSorter(const void *a, const void *b)
984 PMIB_IPFORWARDROW rowA = (PMIB_IPFORWARDROW)a, rowB = (PMIB_IPFORWARDROW)b;
986 ret = rowA->dwForwardDest - rowB->dwForwardDest;
988 ret = rowA->dwForwardProto - rowB->dwForwardProto;
990 ret = rowA->dwForwardPolicy - rowB->dwForwardPolicy;
992 ret = rowA->dwForwardNextHop - rowB->dwForwardNextHop;
1002 /******************************************************************
1003 * GetIpForwardTable (IPHLPAPI.@)
1008 * pIpForwardTable [In/Out]
1017 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1022 ret = ERROR_INVALID_PARAMETER;
1024 DWORD numRoutes = getNumRoutes();
1025 ULONG sizeNeeded = sizeof(MIB_IPFORWARDTABLE) + (numRoutes - 1) *
1026 sizeof(MIB_IPFORWARDROW);
1028 if (!pIpForwardTable || *pdwSize < sizeNeeded) {
1029 *pdwSize = sizeNeeded;
1030 ret = ERROR_INSUFFICIENT_BUFFER;
1033 RouteTable *table = getRouteTable();
1035 sizeNeeded = sizeof(MIB_IPFORWARDTABLE) + (table->numRoutes - 1) *
1036 sizeof(MIB_IPFORWARDROW);
1037 if (*pdwSize < sizeNeeded) {
1038 *pdwSize = sizeNeeded;
1039 ret = ERROR_INSUFFICIENT_BUFFER;
1044 pIpForwardTable->dwNumEntries = table->numRoutes;
1045 for (ndx = 0; ndx < numRoutes; ndx++) {
1046 pIpForwardTable->table[ndx].dwForwardIfIndex =
1047 table->routes[ndx].ifIndex;
1048 pIpForwardTable->table[ndx].dwForwardDest =
1049 table->routes[ndx].dest;
1050 pIpForwardTable->table[ndx].dwForwardMask =
1051 table->routes[ndx].mask;
1052 pIpForwardTable->table[ndx].dwForwardPolicy = 0;
1053 pIpForwardTable->table[ndx].dwForwardNextHop =
1054 table->routes[ndx].gateway;
1055 /* FIXME: this type is appropriate for local interfaces; may not
1056 always be appropriate */
1057 pIpForwardTable->table[ndx].dwForwardType = MIB_IPROUTE_TYPE_DIRECT;
1058 /* FIXME: other protos might be appropriate, e.g. the default route
1059 is typically set with MIB_IPPROTO_NETMGMT instead */
1060 pIpForwardTable->table[ndx].dwForwardProto = MIB_IPPROTO_LOCAL;
1061 /* punt on age and AS */
1062 pIpForwardTable->table[ndx].dwForwardAge = 0;
1063 pIpForwardTable->table[ndx].dwForwardNextHopAS = 0;
1064 pIpForwardTable->table[ndx].dwForwardMetric1 =
1065 table->routes[ndx].metric;
1066 /* rest of the metrics are 0.. */
1067 pIpForwardTable->table[ndx].dwForwardMetric2 = 0;
1068 pIpForwardTable->table[ndx].dwForwardMetric3 = 0;
1069 pIpForwardTable->table[ndx].dwForwardMetric4 = 0;
1070 pIpForwardTable->table[ndx].dwForwardMetric5 = 0;
1073 qsort(pIpForwardTable->table, pIpForwardTable->dwNumEntries,
1074 sizeof(MIB_IPFORWARDROW), IpForwardTableSorter);
1080 ret = ERROR_OUTOFMEMORY;
1087 static int IpNetTableSorter(const void *a, const void *b)
1092 ret = ((PMIB_IPNETROW)a)->dwAddr - ((PMIB_IPNETROW)b)->dwAddr;
1099 /******************************************************************
1100 * GetIpNetTable (IPHLPAPI.@)
1105 * pIpNetTable [In/Out]
1114 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1119 ret = ERROR_INVALID_PARAMETER;
1121 DWORD numEntries = getNumArpEntries();
1122 ULONG size = sizeof(MIB_IPNETTABLE) + (numEntries - 1) *
1123 sizeof(MIB_IPNETROW);
1125 if (!pIpNetTable || *pdwSize < size) {
1127 ret = ERROR_INSUFFICIENT_BUFFER;
1130 PMIB_IPNETTABLE table = getArpTable();
1133 size = sizeof(MIB_IPNETTABLE) + (table->dwNumEntries - 1) *
1134 sizeof(MIB_IPNETROW);
1135 if (*pdwSize < size) {
1137 ret = ERROR_INSUFFICIENT_BUFFER;
1140 memcpy(pIpNetTable, table, size);
1142 qsort(pIpNetTable->table, pIpNetTable->dwNumEntries,
1143 sizeof(MIB_IPNETROW), IpNetTableSorter);
1149 ret = ERROR_OUTOFMEMORY;
1156 /******************************************************************
1157 * GetIpStatistics (IPHLPAPI.@)
1169 DWORD WINAPI GetIpStatistics(PMIB_IPSTATS pStats)
1171 return getIPStats(pStats);
1175 /******************************************************************
1176 * GetNetworkParams (IPHLPAPI.@)
1181 * pFixedInfo [In/Out]
1182 * pOutBufLen [In/Out]
1189 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1195 return ERROR_INVALID_PARAMETER;
1198 size = sizeof(FIXED_INFO) + (_res.nscount > 0 ? (_res.nscount - 1) *
1199 sizeof(IP_ADDR_STRING) : 0);
1200 if (!pFixedInfo || *pOutBufLen < size) {
1202 return ERROR_BUFFER_OVERFLOW;
1205 memset(pFixedInfo, 0, size);
1206 size = sizeof(pFixedInfo->HostName);
1207 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1208 size = sizeof(pFixedInfo->DomainName);
1209 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1210 if (_res.nscount > 0) {
1211 PIP_ADDR_STRING ptr;
1214 for (i = 0, ptr = &pFixedInfo->DnsServerList; i < _res.nscount && ptr;
1215 i++, ptr = ptr->Next) {
1216 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1217 ptr->IpAddress.String);
1218 if (i == _res.nscount - 1)
1221 ptr->Next = (PIP_ADDR_STRING)((LPBYTE)pFixedInfo + sizeof(FIXED_INFO));
1223 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1226 pFixedInfo->NodeType = HYBRID_NODETYPE;
1227 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1228 "\\SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey)
1231 DWORD size = sizeof(pFixedInfo->ScopeId);
1233 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, pFixedInfo->ScopeId, &size);
1237 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1238 I suppose could also check for a listener on port 53 to set EnableDns */
1243 /******************************************************************
1244 * GetNumberOfInterfaces (IPHLPAPI.@)
1256 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1261 ret = ERROR_INVALID_PARAMETER;
1263 *pdwNumIf = getNumInterfaces();
1270 /******************************************************************
1271 * GetPerAdapterInfo (IPHLPAPI.@)
1277 * pPerAdapterInfo [In/Out]
1278 * pOutBufLen [In/Out]
1285 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1288 /* marking Win2K+ functions not supported */
1289 return ERROR_NOT_SUPPORTED;
1293 /******************************************************************
1294 * GetRTTAndHopCount (IPHLPAPI.@)
1299 * DestIpAddress [In]
1309 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1316 /******************************************************************
1317 * GetTcpStatistics (IPHLPAPI.@)
1329 DWORD WINAPI GetTcpStatistics(PMIB_TCPSTATS pStats)
1331 return getTCPStats(pStats);
1335 static int TcpTableSorter(const void *a, const void *b)
1340 PMIB_TCPROW rowA = (PMIB_TCPROW)a, rowB = (PMIB_TCPROW)b;
1342 ret = rowA->dwLocalAddr - rowB->dwLocalAddr;
1344 ret = rowA->dwLocalPort - rowB->dwLocalPort;
1346 ret = rowA->dwRemoteAddr - rowB->dwRemoteAddr;
1348 ret = rowA->dwRemotePort - rowB->dwRemotePort;
1358 /******************************************************************
1359 * GetTcpTable (IPHLPAPI.@)
1364 * pTcpTable [In/Out]
1373 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1378 ret = ERROR_INVALID_PARAMETER;
1380 DWORD numEntries = getNumTcpEntries();
1381 ULONG size = sizeof(MIB_TCPTABLE) + (numEntries - 1) * sizeof(MIB_TCPROW);
1383 if (!pTcpTable || *pdwSize < size) {
1385 ret = ERROR_INSUFFICIENT_BUFFER;
1388 PMIB_TCPTABLE table = getTcpTable();
1391 size = sizeof(MIB_TCPTABLE) + (table->dwNumEntries - 1) *
1393 if (*pdwSize < size) {
1395 ret = ERROR_INSUFFICIENT_BUFFER;
1398 memcpy(pTcpTable, table, size);
1400 qsort(pTcpTable->table, pTcpTable->dwNumEntries,
1401 sizeof(MIB_TCPROW), TcpTableSorter);
1407 ret = ERROR_OUTOFMEMORY;
1414 /******************************************************************
1415 * GetUdpStatistics (IPHLPAPI.@)
1427 DWORD WINAPI GetUdpStatistics(PMIB_UDPSTATS pStats)
1429 return getUDPStats(pStats);
1433 static int UdpTableSorter(const void *a, const void *b)
1438 PMIB_UDPROW rowA = (PMIB_UDPROW)a, rowB = (PMIB_UDPROW)b;
1440 ret = rowA->dwLocalAddr - rowB->dwLocalAddr;
1442 ret = rowA->dwLocalPort - rowB->dwLocalPort;
1450 /******************************************************************
1451 * GetUdpTable (IPHLPAPI.@)
1456 * pUdpTable [In/Out]
1465 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1470 ret = ERROR_INVALID_PARAMETER;
1472 DWORD numEntries = getNumUdpEntries();
1473 ULONG size = sizeof(MIB_UDPTABLE) + (numEntries - 1) * sizeof(MIB_UDPROW);
1475 if (!pUdpTable || *pdwSize < size) {
1477 ret = ERROR_INSUFFICIENT_BUFFER;
1480 PMIB_UDPTABLE table = getUdpTable();
1483 size = sizeof(MIB_UDPTABLE) + (table->dwNumEntries - 1) *
1485 if (*pdwSize < size) {
1487 ret = ERROR_INSUFFICIENT_BUFFER;
1490 memcpy(pUdpTable, table, size);
1492 qsort(pUdpTable->table, pUdpTable->dwNumEntries,
1493 sizeof(MIB_UDPROW), UdpTableSorter);
1499 ret = ERROR_OUTOFMEMORY;
1506 /******************************************************************
1507 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1512 * pIPIfInfo [In/Out]
1513 * dwOutBufLen [In/Out]
1520 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1522 /* a unidirectional adapter?? not bloody likely! */
1523 return ERROR_NOT_SUPPORTED;
1527 /******************************************************************
1528 * IpReleaseAddress (IPHLPAPI.@)
1533 * AdapterInfo [In/Out]
1540 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1542 /* not a stub, never going to support this (and I never mark an adapter as
1543 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1544 return ERROR_NOT_SUPPORTED;
1548 /******************************************************************
1549 * IpRenewAddress (IPHLPAPI.@)
1554 * AdapterInfo [In/Out]
1561 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1563 /* not a stub, never going to support this (and I never mark an adapter as
1564 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1565 return ERROR_NOT_SUPPORTED;
1569 /******************************************************************
1570 * NotifyAddrChange (IPHLPAPI.@)
1576 * overlapped [In/Out]
1583 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1586 /* marking Win2K+ functions not supported */
1587 return ERROR_NOT_SUPPORTED;
1591 /******************************************************************
1592 * NotifyRouteChange (IPHLPAPI.@)
1598 * overlapped [In/Out]
1605 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1608 /* marking Win2K+ functions not supported */
1609 return ERROR_NOT_SUPPORTED;
1613 /******************************************************************
1614 * SendARP (IPHLPAPI.@)
1622 * PhyAddrLen [In/Out]
1629 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1632 /* marking Win2K+ functions not supported */
1633 return ERROR_NOT_SUPPORTED;
1637 /******************************************************************
1638 * SetIfEntry (IPHLPAPI.@)
1650 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1652 /* this is supposed to set an administratively interface up or down.
1653 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
1654 this sort of down is indistinguishable from other sorts of down (e.g. no
1657 return ERROR_NOT_SUPPORTED;
1661 /******************************************************************
1662 * SetIpForwardEntry (IPHLPAPI.@)
1674 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1676 /* this is to add a route entry, how's it distinguishable from
1677 CreateIpForwardEntry?
1678 could use SIOCADDRT, not sure I want to */
1684 /******************************************************************
1685 * SetIpNetEntry (IPHLPAPI.@)
1690 * pArpEntry [In/Out]
1697 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1699 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1705 /******************************************************************
1706 * SetIpStatistics (IPHLPAPI.@)
1718 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1725 /******************************************************************
1726 * SetIpTTL (IPHLPAPI.@)
1738 DWORD WINAPI SetIpTTL(UINT nTTL)
1740 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
1741 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
1747 /******************************************************************
1748 * SetTcpEntry (IPHLPAPI.@)
1760 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
1767 /******************************************************************
1768 * UnenableRouter (IPHLPAPI.@)
1773 * pOverlapped [In/Out]
1774 * lpdwEnableCount [In/Out]
1781 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
1784 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
1785 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
1786 marking Win2K+ functions not supported */
1787 return ERROR_NOT_SUPPORTED;