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.

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