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.

277 lines
6.8 KiB

  1. /*++
  2. Copyright (c) 1996-2002 Microsoft Corporation
  3. Module Name:
  4. lananame.c
  5. Abstract:
  6. This module contains routines for discovering the Connectoid name
  7. associated with a netbios LANA number.
  8. Author:
  9. Charlie Wickham (charlwi) 07-May-2002
  10. --*/
  11. #define UNICODE 1
  12. #define _UNICODE 1
  13. #include <windows.h>
  14. #include <lmcons.h>
  15. #include <nb30.h>
  16. #include <msgrutil.h>
  17. #include <iphlpapi.h>
  18. #include <objbase.h>
  19. #include "clusrtl.h"
  20. #define clearncb(x) memset((char *)x,'\0',sizeof(NCB))
  21. DWORD
  22. ClRtlpGetConnectoidNameFromAdapterIndex(
  23. IN PCLRTL_NET_ADAPTER_ENUM ClusAdapterEnum,
  24. IN DWORD AdapterIndex,
  25. OUT LPWSTR * ConnectoidName
  26. )
  27. /*++
  28. Routine Description:
  29. Look through the adapter enum struct for the matching adapter index. If
  30. found, create a new buffer and copy the connetoid name into it.
  31. Arguments:
  32. ClusAdapterEnum - pointer to struct describing adapters on the node
  33. AdapterIndex - IP's (?) adapter index value
  34. ConnectoidName - address of pointer to receive address of new buffer
  35. Return Value:
  36. appropriate Win32 error code
  37. --*/
  38. {
  39. DWORD status = ERROR_NOT_FOUND;
  40. DWORD index;
  41. LPWSTR cName;
  42. PCLRTL_NET_ADAPTER_INFO adapterInfo = ClusAdapterEnum->AdapterList;
  43. for ( index = 0; index < ClusAdapterEnum->AdapterCount; ++index ) {
  44. if ( adapterInfo->Index == AdapterIndex ) {
  45. //
  46. // dup the string into a new buffer since we're going to clobber
  47. // the adapter info at some point.
  48. //
  49. cName = LocalAlloc( 0, ( wcslen( adapterInfo->ConnectoidName ) + 1 ) * sizeof(WCHAR) );
  50. if ( cName == NULL ) {
  51. return GetLastError();
  52. }
  53. wcscpy( cName, adapterInfo->ConnectoidName );
  54. *ConnectoidName = cName;
  55. return ERROR_SUCCESS;
  56. }
  57. adapterInfo = adapterInfo->Next;
  58. }
  59. return status;
  60. } // ClRtlpGetConnectoidNameFromAdapterIndex
  61. DWORD
  62. ClRtlpGetAdapterIndexFromMacAddress(
  63. PUCHAR MacAddress,
  64. DWORD MacAddrLength,
  65. PIP_ADAPTER_INFO IpAdapterInfo,
  66. DWORD * AdapterIndex
  67. )
  68. /*++
  69. Routine Description:
  70. For the specified MAC address, look through IP helper's Adatper Info
  71. struct and find the matching adapter. Copy its index into AdapterIndex.
  72. Arguments:
  73. MacAddress - pointer to buffer holding MAC address of NIC.
  74. MacAddrLength - length, in bytes, of address in MacAddress
  75. IpAdapterInfo - pointer to data returned from IP helper GetAdaptersInfo()
  76. AdapterIndex - pointer to DWORD that receives the index (if found)
  77. Return Value:
  78. appropriate Win32 error code
  79. --*/
  80. {
  81. DWORD status = ERROR_NOT_FOUND;
  82. while ( IpAdapterInfo != NULL ) {
  83. if ( IpAdapterInfo->AddressLength == MacAddrLength ) {
  84. if ( memcmp( MacAddress, IpAdapterInfo->Address, MacAddrLength ) == 0 ) {
  85. *AdapterIndex = IpAdapterInfo->Index;
  86. return ERROR_SUCCESS;
  87. }
  88. }
  89. IpAdapterInfo = IpAdapterInfo->Next;
  90. }
  91. return status;
  92. } // ClRtlpGetAdapterIndexFromMacAddress
  93. DWORD
  94. ClRtlpGetConnectoidNameFromMacAddress(
  95. PUCHAR MacAddress,
  96. DWORD MacAddrLength,
  97. LPWSTR * ConnectoidName,
  98. PCLRTL_NET_ADAPTER_ENUM ClusAdapterEnum,
  99. PIP_ADAPTER_INFO IpAdapterInfo
  100. )
  101. /*++
  102. Routine Description:
  103. Arguments:
  104. Return Value:
  105. --*/
  106. {
  107. DWORD status;
  108. DWORD adapterIndex;
  109. //
  110. // get index from IP helper using MAC address
  111. //
  112. status = ClRtlpGetAdapterIndexFromMacAddress(MacAddress,
  113. MacAddrLength,
  114. IpAdapterInfo,
  115. &adapterIndex );
  116. if ( status == ERROR_SUCCESS ) {
  117. status = ClRtlpGetConnectoidNameFromAdapterIndex( ClusAdapterEnum, adapterIndex, ConnectoidName );
  118. }
  119. return status;
  120. } // ClRtlpGetConnectoidNameFromMacAddress
  121. DWORD
  122. ClRtlGetConnectoidNameFromLANA(
  123. IN UCHAR LanaNumber,
  124. OUT LPWSTR * ConnectoidName
  125. )
  126. /*++
  127. Routine Description:
  128. Arguments:
  129. Return Value:
  130. --*/
  131. {
  132. DWORD status = ERROR_NOT_FOUND;
  133. NCB ncb;
  134. UCHAR nbStatus;
  135. WORD aStatBufferSize = (WORD)(sizeof(ADAPTER_STATUS));
  136. UCHAR astatBuffer[ sizeof(ADAPTER_STATUS) ];
  137. PADAPTER_STATUS adapterStatus = (PADAPTER_STATUS)astatBuffer;
  138. PCLRTL_NET_ADAPTER_ENUM clusAdapterEnum = NULL;
  139. PIP_ADAPTER_INFO ipAdapterInfo = NULL;
  140. DWORD ipAdapterInfoSize = 0;
  141. //
  142. // get our home grown list of adapter info. The connectoid name is in here
  143. // but we can't match it directly to a MAC address. The adapter index is
  144. // in here which we can match to a structure in IP helper API.
  145. //
  146. clusAdapterEnum = ClRtlEnumNetAdapters();
  147. if ( clusAdapterEnum == NULL ) {
  148. status = GetLastError();
  149. goto cleanup;
  150. }
  151. retry_ip_info:
  152. status = GetAdaptersInfo( ipAdapterInfo, &ipAdapterInfoSize );
  153. if ( status == ERROR_BUFFER_OVERFLOW ) {
  154. ipAdapterInfo = LocalAlloc( 0, ipAdapterInfoSize );
  155. if ( ipAdapterInfo == NULL ) {
  156. status = GetLastError();
  157. goto cleanup;
  158. }
  159. goto retry_ip_info;
  160. }
  161. else if ( status != ERROR_SUCCESS ) {
  162. goto cleanup;
  163. }
  164. //
  165. // clear the NCB and format the remote name appropriately.
  166. //
  167. clearncb(&ncb);
  168. ncb.ncb_callname[0] = '*';
  169. NetpNetBiosReset( LanaNumber );
  170. ncb.ncb_command = NCBASTAT;
  171. ncb.ncb_buffer = astatBuffer;
  172. ncb.ncb_length = aStatBufferSize;
  173. ncb.ncb_lana_num = LanaNumber;
  174. nbStatus = Netbios( &ncb );
  175. if ( nbStatus == NRC_GOODRET ) {
  176. status = ClRtlpGetConnectoidNameFromMacAddress(adapterStatus->adapter_address,
  177. sizeof( adapterStatus->adapter_address ),
  178. ConnectoidName,
  179. clusAdapterEnum,
  180. ipAdapterInfo );
  181. }
  182. else {
  183. status = NetpNetBiosStatusToApiStatus( nbStatus );
  184. }
  185. cleanup:
  186. if ( clusAdapterEnum != NULL ) {
  187. ClRtlFreeNetAdapterEnum( clusAdapterEnum );
  188. }
  189. if ( ipAdapterInfo != NULL ) {
  190. LocalFree( ipAdapterInfo );
  191. }
  192. return status;
  193. } // ClRtlGetConnectoidNameFromLANA