Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

340 lines
7.4 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. iphlpwrp.c
  5. Abstract:
  6. This module contains all of the code to wrap
  7. the ip public help apis for getting the list of
  8. active interfaces on a machine.
  9. Author:
  10. krishnaG
  11. Environment
  12. User Level: Win32
  13. Revision History:
  14. abhisheV 30-September-1999
  15. --*/
  16. #include "precomp.h"
  17. DWORD
  18. PaPNPGetIfTable(
  19. OUT PMIB_IFTABLE * ppMibIfTable
  20. )
  21. {
  22. DWORD dwStatus = 0;
  23. PMIB_IFTABLE pIfTable = NULL;
  24. PMIB_IFTABLE pMibIfTable = NULL;
  25. PIP_INTERFACE_INFO pInterfaceInfo = NULL;
  26. DWORD i = 0;
  27. DWORD j = 0;
  28. DWORD dwNameLen = 0;
  29. dwStatus = AllocateAndGetIfTableFromStack(
  30. &pIfTable,
  31. TRUE,
  32. GetProcessHeap(),
  33. 0,
  34. TRUE
  35. );
  36. BAIL_ON_WIN32_ERROR(dwStatus);
  37. pMibIfTable = (PMIB_IFTABLE) LocalAlloc(
  38. LPTR,
  39. sizeof(DWORD)+
  40. sizeof(MIB_IFROW) *
  41. pIfTable->dwNumEntries
  42. );
  43. if (!pMibIfTable) {
  44. dwStatus = ERROR_OUTOFMEMORY;
  45. BAIL_ON_WIN32_ERROR(dwStatus);
  46. }
  47. for (i = 0; i < pIfTable->dwNumEntries; i++) {
  48. memcpy(&(pMibIfTable->table[i]), &(pIfTable->table[i]), sizeof(MIB_IFROW));
  49. }
  50. pMibIfTable->dwNumEntries = pIfTable->dwNumEntries;
  51. // Get the corresponding Interface Information structure here.
  52. dwStatus = PaPNPGetInterfaceInformation(
  53. &pInterfaceInfo
  54. );
  55. BAIL_ON_WIN32_ERROR(dwStatus);
  56. if (!pInterfaceInfo) {
  57. dwStatus = ERROR_OUTOFMEMORY;
  58. BAIL_ON_WIN32_ERROR(dwStatus);
  59. }
  60. for (j = 0; j < pMibIfTable->dwNumEntries; j++) {
  61. for (i = 0; i < (DWORD) pInterfaceInfo->NumAdapters; i++) {
  62. if (pInterfaceInfo->Adapter[i].Index == pMibIfTable->table[j].dwIndex) {
  63. dwNameLen = wcslen((LPTSTR) &pInterfaceInfo->Adapter[i].Name);
  64. wcsncpy(
  65. (LPTSTR)&(pMibIfTable->table[j].wszName),
  66. (LPTSTR)&(pInterfaceInfo->Adapter[i].Name),
  67. dwNameLen
  68. );
  69. }
  70. }
  71. }
  72. *ppMibIfTable = pMibIfTable;
  73. cleanup:
  74. if (pIfTable) {
  75. HeapFree(GetProcessHeap(), 0, pIfTable);
  76. }
  77. if (pInterfaceInfo) {
  78. LocalFree(pInterfaceInfo);
  79. }
  80. return (dwStatus);
  81. error:
  82. if (pMibIfTable) {
  83. LocalFree(pMibIfTable);
  84. }
  85. *ppMibIfTable = NULL;
  86. goto cleanup;
  87. }
  88. DWORD
  89. PaPNPGetInterfaceInformation(
  90. OUT PIP_INTERFACE_INFO * ppInterfaceInfo
  91. )
  92. {
  93. LPBYTE pBuffer = NULL;
  94. DWORD dwBufferSize = 2048;
  95. DWORD dwStatus = 0;
  96. pBuffer = (LPBYTE) LocalAlloc(
  97. LPTR,
  98. dwBufferSize
  99. );
  100. if (!pBuffer) {
  101. return (ERROR_OUTOFMEMORY);
  102. }
  103. dwStatus = GetInterfaceInfo(
  104. (PIP_INTERFACE_INFO) pBuffer,
  105. &dwBufferSize
  106. );
  107. if (dwStatus == ERROR_INSUFFICIENT_BUFFER) {
  108. if (pBuffer) {
  109. LocalFree(pBuffer);
  110. pBuffer = NULL;
  111. }
  112. pBuffer = (LPBYTE) LocalAlloc(
  113. LPTR,
  114. dwBufferSize
  115. );
  116. if (!pBuffer) {
  117. return (ERROR_OUTOFMEMORY);
  118. }
  119. dwStatus = GetInterfaceInfo(
  120. (PIP_INTERFACE_INFO) pBuffer,
  121. &dwBufferSize
  122. );
  123. if (dwStatus) {
  124. goto error;
  125. }
  126. }
  127. else if (dwStatus) {
  128. goto error;
  129. }
  130. *ppInterfaceInfo = (PIP_INTERFACE_INFO) pBuffer;
  131. return (dwStatus);
  132. error:
  133. if (pBuffer) {
  134. LocalFree(pBuffer);
  135. }
  136. return (dwStatus);
  137. }
  138. VOID
  139. PrintMibIfTable(
  140. IN PMIB_IFTABLE pMibIfTable
  141. )
  142. {
  143. DWORD dwNumEntries = 0;
  144. DWORD i = 0;
  145. PMIB_IFROW pMibIfRow = NULL;
  146. PMIB_IFROW pCurrentMibIfRow = NULL;
  147. dwNumEntries = pMibIfTable->dwNumEntries;
  148. pMibIfRow = &(pMibIfTable->table[0]);
  149. for (i = 0; i < dwNumEntries; i++) {
  150. pCurrentMibIfRow = pMibIfRow + i;
  151. wprintf(L"Name = %s\n", pCurrentMibIfRow->wszName);
  152. wprintf(L"dwIndex = %d\n", pCurrentMibIfRow->dwIndex);
  153. printf("Description = %s\n", (pCurrentMibIfRow->bDescr));
  154. }
  155. wprintf(L"\n\n");
  156. }
  157. VOID
  158. PrintInterfaceInfo(
  159. IN PIP_INTERFACE_INFO pInterfaceInfo
  160. )
  161. {
  162. DWORD dwNumAdapters = 0;
  163. DWORD i = 0;
  164. PIP_ADAPTER_INDEX_MAP pAdapterIndexMap = NULL;
  165. PIP_ADAPTER_INDEX_MAP pCurrentAdapter = NULL;
  166. dwNumAdapters = pInterfaceInfo->NumAdapters;
  167. pAdapterIndexMap = &(pInterfaceInfo->Adapter[0]);
  168. for (i = 0; i < dwNumAdapters; i++) {
  169. pCurrentAdapter = pAdapterIndexMap + i;
  170. wprintf(
  171. L"Adapter %d Index %d Name %s\n",
  172. i,
  173. pCurrentAdapter->Index,
  174. pCurrentAdapter->Name
  175. );
  176. }
  177. }
  178. VOID
  179. PrintMibAddrTable(
  180. IN PMIB_IPADDRTABLE pMibAddrTable
  181. )
  182. {
  183. DWORD dwNumEntries = 0;
  184. DWORD i = 0;
  185. PMIB_IPADDRROW pMibAddrRow = NULL;
  186. PMIB_IPADDRROW pCurrentMibAddrRow = NULL;
  187. dwNumEntries = pMibAddrTable->dwNumEntries;
  188. pMibAddrRow = &(pMibAddrTable->table[0]);
  189. for (i = 0; i < dwNumEntries; i++) {
  190. pCurrentMibAddrRow = pMibAddrRow + i;
  191. wprintf(L"Address = %s\n", pCurrentMibAddrRow->dwAddr);
  192. wprintf(L"dwIndex = %d\n", pCurrentMibAddrRow->dwIndex);
  193. wprintf(L"dwMask = %s\n", pCurrentMibAddrRow->dwMask);
  194. }
  195. wprintf(L"\n\n");
  196. }
  197. DWORD
  198. PaPNPGetIpAddrTable(
  199. OUT PMIB_IPADDRTABLE * ppMibIpAddrTable
  200. )
  201. {
  202. PMIB_IPADDRTABLE pMibIpAddrTable = NULL;
  203. DWORD dwBufferSize = 2048;
  204. DWORD dwStatus = 0;
  205. DWORD dwNameLen = 0;
  206. pMibIpAddrTable = (PMIB_IPADDRTABLE) LocalAlloc(
  207. LPTR,
  208. dwBufferSize
  209. );
  210. if (!pMibIpAddrTable) {
  211. return (ERROR_OUTOFMEMORY);
  212. }
  213. dwStatus = GetIpAddrTable(
  214. (PMIB_IPADDRTABLE) pMibIpAddrTable,
  215. &dwBufferSize,
  216. TRUE
  217. );
  218. if (dwStatus == ERROR_INSUFFICIENT_BUFFER) {
  219. if (pMibIpAddrTable) {
  220. LocalFree(pMibIpAddrTable);
  221. pMibIpAddrTable = NULL;
  222. }
  223. pMibIpAddrTable = (PMIB_IPADDRTABLE) LocalAlloc(
  224. LPTR,
  225. dwBufferSize
  226. );
  227. if (!pMibIpAddrTable) {
  228. return (ERROR_OUTOFMEMORY);
  229. }
  230. dwStatus = GetIpAddrTable(
  231. (PMIB_IPADDRTABLE) pMibIpAddrTable,
  232. &dwBufferSize,
  233. TRUE
  234. );
  235. if (dwStatus) {
  236. goto error;
  237. }
  238. }
  239. else if (dwStatus) {
  240. goto error;
  241. }
  242. *ppMibIpAddrTable = pMibIpAddrTable;
  243. return (dwStatus);
  244. error:
  245. *ppMibIpAddrTable = NULL;
  246. if (pMibIpAddrTable) {
  247. LocalFree(pMibIpAddrTable);
  248. }
  249. return (dwStatus);
  250. }