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.

219 lines
5.9 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. wins.c
  5. Abstract:
  6. Functions to retrieve info from NetBT device driver
  7. Contents:
  8. GetWinsServers
  9. Author:
  10. Richard L Firth (rfirth) 6-Aug-1994
  11. Revision History:
  12. rfirth 6-Aug-1994
  13. Created
  14. --*/
  15. #include "precomp.h"
  16. #pragma warning(push)
  17. #pragma warning(disable:4201)
  18. #pragma warning(disable:4214)
  19. #include <nbtioctl.h>
  20. #pragma warning(pop)
  21. //
  22. // seems that if WINS addresses not specified, NetBT reports 127.0.0.0 so if
  23. // this value is returned, we won't display them
  24. //
  25. #define LOCAL_WINS_ADDRESS 0x0000007f // 127.0.0.0
  26. #define BYTE_SWAP(w) (HIBYTE(w) | (LOBYTE(w) << 8))
  27. #define WORD_SWAP(d) (BYTE_SWAP(HIWORD(d)) | (BYTE_SWAP(LOWORD(d)) << 16))
  28. /*******************************************************************************
  29. *
  30. * GetWinsServers
  31. *
  32. * Gets the primary and secondary WINS addresses for a particular adapter from
  33. * NetBT
  34. *
  35. * ENTRY AdapterInfo - pointer to ADAPTER_INFO
  36. *
  37. * EXIT AdapterInfo.PrimaryWinsServer and AdapterInfo.SecondaryWinsServer
  38. *
  39. * RETURNS TRUE if success
  40. *
  41. * ASSUMES 1.
  42. * 2. We have already got the Node Type for this adapter
  43. *
  44. ******************************************************************************/
  45. BOOL GetWinsServers(PIP_ADAPTER_INFO AdapterInfo)
  46. {
  47. HANDLE h;
  48. OBJECT_ATTRIBUTES objAttr;
  49. IO_STATUS_BLOCK iosb;
  50. STRING name;
  51. UNICODE_STRING uname;
  52. NTSTATUS status;
  53. DWORD i;
  54. tWINS_NODE_INFO winsInfo;
  55. char path[MAX_PATH];
  56. //
  57. // default the 'have WINS' status of this adapter
  58. //
  59. AdapterInfo->HaveWins = FALSE;
  60. strcpy(path, "\\Device\\NetBT_Tcpip_");
  61. strcat(path, AdapterInfo->AdapterName);
  62. RtlInitString(&name, path);
  63. status = RtlAnsiStringToUnicodeString(&uname, &name, TRUE);
  64. if (!NT_SUCCESS(status)) {
  65. DEBUG_PRINT(("GetWinsServers: RtlAnsiStringToUnicodeString(name=%s) failed, err=%d\n",
  66. name, GetLastError() ));
  67. return FALSE;
  68. }
  69. InitializeObjectAttributes(
  70. &objAttr,
  71. &uname,
  72. OBJ_CASE_INSENSITIVE,
  73. (HANDLE)NULL,
  74. (PSECURITY_DESCRIPTOR)NULL
  75. );
  76. status = NtCreateFile(&h,
  77. SYNCHRONIZE | FILE_READ_DATA | FILE_WRITE_DATA,
  78. &objAttr,
  79. &iosb,
  80. NULL,
  81. FILE_ATTRIBUTE_NORMAL,
  82. FILE_SHARE_READ | FILE_SHARE_WRITE,
  83. FILE_OPEN_IF,
  84. 0,
  85. NULL,
  86. 0
  87. );
  88. RtlFreeUnicodeString(&uname);
  89. if (!NT_SUCCESS(status)) {
  90. DEBUG_PRINT(("GetWinsServers: NtCreateFile(path=%s) failed, err=%d\n",
  91. path, GetLastError() ));
  92. return FALSE;
  93. }
  94. status = NtDeviceIoControlFile(h,
  95. NULL,
  96. NULL,
  97. NULL,
  98. &iosb,
  99. IOCTL_NETBT_GET_WINS_ADDR,
  100. NULL,
  101. 0,
  102. (PVOID)&winsInfo,
  103. sizeof(winsInfo)
  104. );
  105. if (status == STATUS_PENDING) {
  106. status = NtWaitForSingleObject(h, TRUE, NULL);
  107. if (NT_SUCCESS(status)) {
  108. status = iosb.Status;
  109. }
  110. }
  111. NtClose(h);
  112. if (!NT_SUCCESS(status)) {
  113. DEBUG_PRINT(("GetWinsServers: NtDeviceIoControlFile failed, err=%d\n",
  114. GetLastError() ));
  115. return FALSE;
  116. }
  117. //
  118. // for some reason, NetBT returns the addresses in low-byte order. We have
  119. // to swap them
  120. //
  121. for (i = 0; i < RTL_NUMBER_OF(winsInfo.AllNameServers); i++) {
  122. winsInfo.AllNameServers[i] =
  123. RtlUlongByteSwap(winsInfo.AllNameServers[i]);
  124. }
  125. DEBUG_PRINT(("GetWinsServers: Primary Address = %d.%d.%d.%d\n",
  126. ((LPBYTE)&winsInfo.AllNameServers[0])[0],
  127. ((LPBYTE)&winsInfo.AllNameServers[0])[1],
  128. ((LPBYTE)&winsInfo.AllNameServers[0])[2],
  129. ((LPBYTE)&winsInfo.AllNameServers[0])[3]
  130. ));
  131. DEBUG_PRINT(("GetWinsServers: Secondary Address = %d.%d.%d.%d\n",
  132. ((LPBYTE)&winsInfo.AllNameServers[1])[0],
  133. ((LPBYTE)&winsInfo.AllNameServers[1])[1],
  134. ((LPBYTE)&winsInfo.AllNameServers[1])[2],
  135. ((LPBYTE)&winsInfo.AllNameServers[1])[3]
  136. ));
  137. //
  138. // if we get 127.0.0.0 back then convert it to the NULL address. See
  139. // ASSUMES in function header
  140. //
  141. if (winsInfo.AllNameServers[0] == LOCAL_WINS_ADDRESS) {
  142. winsInfo.AllNameServers[0] = 0;
  143. } else {
  144. AdapterInfo->HaveWins = TRUE;
  145. }
  146. AddIpAddress(&AdapterInfo->PrimaryWinsServer,
  147. winsInfo.AllNameServers[0],
  148. 0,
  149. 0
  150. );
  151. //
  152. // same with secondary
  153. //
  154. if (winsInfo.AllNameServers[1] == LOCAL_WINS_ADDRESS) {
  155. winsInfo.AllNameServers[1] = 0;
  156. } else {
  157. AdapterInfo->HaveWins = TRUE;
  158. }
  159. AddIpAddress(&AdapterInfo->SecondaryWinsServer,
  160. winsInfo.AllNameServers[1],
  161. 0,
  162. 0
  163. );
  164. //
  165. // Append any remaining addresses.
  166. //
  167. for (i = 0; i < winsInfo.NumOtherServers; i++) {
  168. if (winsInfo.Others[i] != LOCAL_WINS_ADDRESS) {
  169. AddIpAddress(&AdapterInfo->SecondaryWinsServer,
  170. winsInfo.Others[i],
  171. 0,
  172. 0
  173. );
  174. }
  175. }
  176. return TRUE;
  177. }