Leaked source code of windows server 2003
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.

249 lines
6.8 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: K K E N E T . C P P
  7. //
  8. // Contents: Ethernet address function
  9. //
  10. // Notes:
  11. //
  12. // Author: kumarp
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "kkutils.h"
  18. #include "ndispnp.h"
  19. #include "ntddndis.h" // This defines the IOCTL constants.
  20. extern const WCHAR c_szDevice[];
  21. HRESULT HrGetNetCardAddr(IN PCWSTR pszDriver, OUT ULONGLONG* pqwNetCardAddr)
  22. {
  23. AssertValidReadPtr(pszDriver);
  24. AssertValidWritePtr(pqwNetCardAddr);
  25. DefineFunctionName("HrGetNetCardAddr");
  26. HRESULT hr = S_OK;
  27. // Form the device name in form "\Device\{GUID}"
  28. tstring strDeviceName = c_szDevice;
  29. strDeviceName.append(pszDriver);
  30. UNICODE_STRING ustrDevice;
  31. ::RtlInitUnicodeString(&ustrDevice, strDeviceName.c_str());
  32. UINT uiRet;
  33. UCHAR MacAddr[6];
  34. UCHAR PMacAddr[6];
  35. UCHAR VendorId[3];
  36. ULONGLONG qw = 0;
  37. uiRet = NdisQueryHwAddress(&ustrDevice, MacAddr, PMacAddr, VendorId);
  38. if (uiRet)
  39. {
  40. for (int i=0; i<=4; i++)
  41. {
  42. qw |= MacAddr[i];
  43. qw <<= 8;
  44. }
  45. qw |= MacAddr[i];
  46. }
  47. else
  48. {
  49. hr = HrFromLastWin32Error();
  50. }
  51. *pqwNetCardAddr = qw;
  52. TraceError(__FUNCNAME__, hr);
  53. return hr;
  54. }
  55. //+---------------------------------------------------------------------------
  56. //
  57. // Function: HrGetNetCardAddrOld
  58. //
  59. // Purpose: Get mac address of a netcard without using NdisQueryHwAddress
  60. //
  61. // Arguments:
  62. // pszDriver [in] name (on NT3.51/4) or guid (on NT5) of driver
  63. // pqwNetCardAddr [out] pointer to result
  64. //
  65. // Returns: S_OK on success, otherwise an error code
  66. //
  67. // Author: kumarp 11-February-99
  68. //
  69. // Notes:
  70. //
  71. #define DEVICE_PREFIX L"\\\\.\\"
  72. HRESULT HrGetNetCardAddrOld(IN PCWSTR pszDriver, OUT ULONGLONG* pqwNetCardAddr)
  73. {
  74. DefineFunctionName("HrGetNetCardAddrOld");
  75. AssertValidReadPtr(pszDriver);
  76. *pqwNetCardAddr = 0;
  77. WCHAR LinkName[512];
  78. WCHAR DeviceName[80];
  79. WCHAR szMACFileName[80];
  80. WCHAR OidData[4096];
  81. BOOL fCreatedDevice = FALSE;
  82. DWORD ReturnedCount;
  83. HANDLE hMAC;
  84. HRESULT hr = S_OK;
  85. NDIS_OID OidCode[] =
  86. {
  87. OID_802_3_PERMANENT_ADDRESS, // Ethernet
  88. OID_802_5_PERMANENT_ADDRESS, // TokenRing
  89. OID_FDDI_LONG_PERMANENT_ADDR, // FDDI
  90. };
  91. //
  92. // Check to see if the DOS name for the MAC driver already exists.
  93. // Its not created automatically in version 3.1 but may be later.
  94. //
  95. TraceTag (ttidDefault, "Attempting to get address of %S", pszDriver);
  96. if (QueryDosDevice(pszDriver, LinkName, celems(LinkName)) == 0)
  97. {
  98. if (ERROR_FILE_NOT_FOUND == GetLastError())
  99. {
  100. wcscpy(DeviceName, L"\\Device\\");
  101. wcscat(DeviceName, pszDriver);
  102. //
  103. // It doesn't exist so create it.
  104. //
  105. if (DefineDosDevice( DDD_RAW_TARGET_PATH, pszDriver, DeviceName))
  106. {
  107. fCreatedDevice = TRUE;
  108. }
  109. else
  110. {
  111. TraceLastWin32Error("DefineDosDevice returned an error creating the device");
  112. hr = HrFromLastWin32Error();
  113. }
  114. }
  115. else
  116. {
  117. TraceLastWin32Error("QueryDosDevice returned an error");
  118. hr = HrFromLastWin32Error();
  119. }
  120. }
  121. if (S_OK == hr)
  122. {
  123. //
  124. // Construct a device name to pass to CreateFile
  125. //
  126. wcscpy(szMACFileName, DEVICE_PREFIX);
  127. wcscat(szMACFileName, pszDriver);
  128. hMAC = CreateFile(
  129. szMACFileName,
  130. GENERIC_READ,
  131. FILE_SHARE_READ | FILE_SHARE_WRITE,
  132. NULL,
  133. OPEN_EXISTING,
  134. 0,
  135. INVALID_HANDLE_VALUE
  136. );
  137. if (hMAC != INVALID_HANDLE_VALUE)
  138. {
  139. DWORD count = 0;
  140. DWORD ReturnedCount = 0;
  141. //
  142. // We successfully opened the driver, format the IOCTL to pass the
  143. // driver.
  144. //
  145. while ((0 == ReturnedCount) && (count < celems (OidCode)))
  146. {
  147. if (DeviceIoControl(
  148. hMAC,
  149. IOCTL_NDIS_QUERY_GLOBAL_STATS,
  150. &OidCode[count],
  151. sizeof(OidCode[count]),
  152. OidData,
  153. sizeof(OidData),
  154. &ReturnedCount,
  155. NULL
  156. ))
  157. {
  158. TraceTag (ttidDefault, "OID %lX succeeded", OidCode[count]);
  159. if (ReturnedCount == 6)
  160. {
  161. *pqwNetCardAddr = (ULONGLONG) 0;
  162. WORD wAddrLen=6;
  163. for (int i=0; i<wAddrLen; i++)
  164. {
  165. *(((BYTE*) pqwNetCardAddr)+i) = *(((BYTE*) OidData)+(wAddrLen-i-1));
  166. }
  167. hr = S_OK;
  168. }
  169. else
  170. {
  171. TraceLastWin32Error("DeviceIoControl returned an invalid count");
  172. hr = HrFromLastWin32Error();
  173. }
  174. }
  175. else
  176. {
  177. hr = HrFromLastWin32Error();
  178. }
  179. count++;
  180. }
  181. }
  182. else
  183. {
  184. TraceLastWin32Error("CreateFile returned an error");
  185. hr = HrFromLastWin32Error();
  186. }
  187. }
  188. if (fCreatedDevice)
  189. {
  190. //
  191. // The MAC driver wasn't visible in the Win32 name space so we created
  192. // a link. Now we have to delete it.
  193. //
  194. if (!DefineDosDevice(
  195. DDD_RAW_TARGET_PATH | DDD_REMOVE_DEFINITION |
  196. DDD_EXACT_MATCH_ON_REMOVE,
  197. pszDriver,
  198. DeviceName)
  199. )
  200. {
  201. TraceLastWin32Error("DefineDosDevice returned an error removing the device");
  202. }
  203. }
  204. TraceFunctionError(hr);
  205. return hr;
  206. }
  207. #ifdef DBG
  208. void PrintNetCardAddr(IN PCWSTR pszDriver)
  209. {
  210. ULONGLONG qwNetCardAddr=0;
  211. HRESULT hr = HrGetNetCardAddr(pszDriver, &qwNetCardAddr);
  212. wprintf(L"Netcard address for %s: 0x%012.12I64x", pszDriver, qwNetCardAddr);
  213. TraceError("dafile.main", hr);
  214. }
  215. #endif // DBG