NameServer.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Copyright (C) 2014-2018 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using System.Text;
  12. using System.Threading;
  13. using SMBLibrary.NetBios;
  14. using Utilities;
  15. namespace SMBLibrary.Server
  16. {
  17. /// <summary>
  18. /// NetBIOS name service server
  19. /// </summary>
  20. public class NameServer
  21. {
  22. public static readonly int NetBiosNameServicePort = 137;
  23. public static readonly string WorkgroupName = "WORKGROUP";
  24. private IPAddress m_serverAddress;
  25. private IPAddress m_broadcastAddress;
  26. private UdpClient m_client;
  27. private bool m_listening;
  28. public NameServer(IPAddress serverAddress, IPAddress subnetMask)
  29. {
  30. if (serverAddress.AddressFamily != AddressFamily.InterNetwork)
  31. {
  32. throw new ArgumentException("NetBIOS name service can only supply IPv4 addresses");
  33. }
  34. if (IPAddress.Equals(serverAddress, IPAddress.Any))
  35. {
  36. // When registering a NetBIOS name, we must supply the client with a usable IPAddress.
  37. throw new ArgumentException("NetBIOS name service requires an IPAddress that is associated with a specific network interface");
  38. }
  39. m_serverAddress = serverAddress;
  40. m_broadcastAddress = GetBroadcastAddress(serverAddress, subnetMask);
  41. }
  42. public void Start()
  43. {
  44. if (!m_listening)
  45. {
  46. m_listening = true;
  47. m_client = new UdpClient(new IPEndPoint(m_serverAddress, NetBiosNameServicePort));
  48. m_client.BeginReceive(ReceiveCallback, null);
  49. ThreadStart threadStart = new ThreadStart(RegisterNetBIOSName);
  50. Thread thread = new Thread(threadStart);
  51. thread.Start();
  52. }
  53. }
  54. public void Stop()
  55. {
  56. m_listening = false;
  57. m_client.Close();
  58. }
  59. private void ReceiveCallback(IAsyncResult result)
  60. {
  61. if (!m_listening)
  62. {
  63. return;
  64. }
  65. IPEndPoint remoteEP = null;
  66. byte[] buffer;
  67. try
  68. {
  69. buffer = m_client.EndReceive(result, ref remoteEP);
  70. }
  71. catch (ObjectDisposedException)
  72. {
  73. return;
  74. }
  75. catch (SocketException)
  76. {
  77. return;
  78. }
  79. // Process buffer
  80. if (buffer.Length > NameServicePacketHeader.Length)
  81. {
  82. NameServicePacketHeader header = new NameServicePacketHeader(buffer, 0);
  83. if (header.OpCode == NameServiceOperation.QueryRequest)
  84. {
  85. NameQueryRequest request = null;
  86. try
  87. {
  88. request = new NameQueryRequest(buffer, 0);
  89. }
  90. catch
  91. {
  92. }
  93. if (request != null)
  94. {
  95. if (request.Question.Type == NameRecordType.NB)
  96. {
  97. string name = NetBiosUtils.GetNameFromMSNetBiosName(request.Question.Name);
  98. NetBiosSuffix suffix = (NetBiosSuffix)request.Question.Name[15];
  99. bool nameMatch = String.Equals(name, Environment.MachineName, StringComparison.OrdinalIgnoreCase);
  100. if (nameMatch && ((suffix == NetBiosSuffix.WorkstationService) || (suffix == NetBiosSuffix.FileServiceService)))
  101. {
  102. PositiveNameQueryResponse response = new PositiveNameQueryResponse();
  103. response.Header.TransactionID = request.Header.TransactionID;
  104. response.Resource.Name = request.Question.Name;
  105. NameFlags nameFlags = new NameFlags();
  106. response.Addresses.Add(m_serverAddress.GetAddressBytes(), nameFlags);
  107. byte[] responseBytes = response.GetBytes();
  108. m_client.Send(responseBytes, responseBytes.Length, remoteEP);
  109. }
  110. }
  111. else // NBStat
  112. {
  113. NodeStatusResponse response = new NodeStatusResponse();
  114. response.Header.TransactionID = request.Header.TransactionID;
  115. response.Resource.Name = request.Question.Name;
  116. NameFlags nameFlags = new NameFlags();
  117. string name1 = NetBiosUtils.GetMSNetBiosName(Environment.MachineName, NetBiosSuffix.WorkstationService);
  118. string name2 = NetBiosUtils.GetMSNetBiosName(Environment.MachineName, NetBiosSuffix.FileServiceService);
  119. NameFlags nameFlags3 = new NameFlags();
  120. nameFlags3.WorkGroup = true;
  121. string name3 = NetBiosUtils.GetMSNetBiosName(WorkgroupName, NetBiosSuffix.WorkstationService);
  122. response.Names.Add(name1, nameFlags);
  123. response.Names.Add(name2, nameFlags);
  124. response.Names.Add(name3, nameFlags3);
  125. byte[] responseBytes = response.GetBytes();
  126. try
  127. {
  128. m_client.Send(responseBytes, responseBytes.Length, remoteEP);
  129. }
  130. catch (ObjectDisposedException)
  131. {
  132. }
  133. }
  134. }
  135. }
  136. }
  137. try
  138. {
  139. m_client.BeginReceive(ReceiveCallback, null);
  140. }
  141. catch (ObjectDisposedException)
  142. {
  143. }
  144. catch (SocketException)
  145. {
  146. }
  147. }
  148. private void RegisterNetBIOSName()
  149. {
  150. NameRegistrationRequest request1 = new NameRegistrationRequest(Environment.MachineName, NetBiosSuffix.WorkstationService, m_serverAddress);
  151. NameRegistrationRequest request2 = new NameRegistrationRequest(Environment.MachineName, NetBiosSuffix.FileServiceService, m_serverAddress);
  152. NameRegistrationRequest request3 = new NameRegistrationRequest(WorkgroupName, NetBiosSuffix.WorkstationService, m_serverAddress);
  153. request3.NameFlags.WorkGroup = true;
  154. RegisterName(request1);
  155. RegisterName(request2);
  156. RegisterName(request3);
  157. }
  158. private void RegisterName(NameRegistrationRequest request)
  159. {
  160. byte[] packet = request.GetBytes();
  161. IPEndPoint broadcastEP = new IPEndPoint(m_broadcastAddress, NetBiosNameServicePort);
  162. for (int index = 0; index < 4; index++)
  163. {
  164. try
  165. {
  166. m_client.Send(packet, packet.Length, broadcastEP);
  167. }
  168. catch (ObjectDisposedException)
  169. {
  170. }
  171. if (index < 3)
  172. {
  173. System.Threading.Thread.Sleep(250);
  174. }
  175. }
  176. }
  177. public static IPAddress GetBroadcastAddress(IPAddress address, IPAddress subnetMask)
  178. {
  179. byte[] ipAdressBytes = address.GetAddressBytes();
  180. byte[] subnetMaskBytes = subnetMask.GetAddressBytes();
  181. byte[] broadcastAddress = new byte[ipAdressBytes.Length];
  182. for (int i = 0; i < broadcastAddress.Length; i++)
  183. {
  184. broadcastAddress[i] = (byte)(ipAdressBytes[i] | (subnetMaskBytes[i] ^ 255));
  185. }
  186. return new IPAddress(broadcastAddress);
  187. }
  188. }
  189. }