NetworkManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Model.Net;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.NetworkInformation;
  9. using System.Net.Sockets;
  10. using System.Runtime.InteropServices;
  11. namespace MediaBrowser.Common.Implementations.NetworkManagement
  12. {
  13. /// <summary>
  14. /// Class NetUtils
  15. /// </summary>
  16. public class NetworkManager : INetworkManager
  17. {
  18. /// <summary>
  19. /// Gets the machine's local ip address
  20. /// </summary>
  21. /// <returns>IPAddress.</returns>
  22. public IEnumerable<string> GetLocalIpAddresses()
  23. {
  24. var host = Dns.GetHostEntry(Dns.GetHostName());
  25. // Reverse them because the last one is usually the correct one
  26. // It's not fool-proof so ultimately the consumer will have to examine them and decide
  27. return host.AddressList
  28. .Where(i => i.AddressFamily == AddressFamily.InterNetwork)
  29. .Select(i => i.ToString())
  30. .Reverse();
  31. }
  32. /// <summary>
  33. /// Gets a random port number that is currently available
  34. /// </summary>
  35. /// <returns>System.Int32.</returns>
  36. public int GetRandomUnusedPort()
  37. {
  38. var listener = new TcpListener(IPAddress.Any, 0);
  39. listener.Start();
  40. var port = ((IPEndPoint)listener.LocalEndpoint).Port;
  41. listener.Stop();
  42. return port;
  43. }
  44. /// <summary>
  45. /// Returns MAC Address from first Network Card in Computer
  46. /// </summary>
  47. /// <returns>[string] MAC Address</returns>
  48. public string GetMacAddress()
  49. {
  50. return NetworkInterface.GetAllNetworkInterfaces()
  51. .Where(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback)
  52. .Select(i => BitConverter.ToString(i.GetPhysicalAddress().GetAddressBytes()))
  53. .FirstOrDefault();
  54. }
  55. /// <summary>
  56. /// Gets the network shares.
  57. /// </summary>
  58. /// <param name="path">The path.</param>
  59. /// <returns>IEnumerable{NetworkShare}.</returns>
  60. public IEnumerable<NetworkShare> GetNetworkShares(string path)
  61. {
  62. return new ShareCollection(path).OfType<Share>().Select(ToNetworkShare);
  63. }
  64. /// <summary>
  65. /// To the network share.
  66. /// </summary>
  67. /// <param name="share">The share.</param>
  68. /// <returns>NetworkShare.</returns>
  69. private NetworkShare ToNetworkShare(Share share)
  70. {
  71. return new NetworkShare
  72. {
  73. Name = share.NetName,
  74. Path = share.Path,
  75. Remark = share.Remark,
  76. Server = share.Server,
  77. ShareType = ToNetworkShareType(share.ShareType)
  78. };
  79. }
  80. /// <summary>
  81. /// To the type of the network share.
  82. /// </summary>
  83. /// <param name="shareType">Type of the share.</param>
  84. /// <returns>NetworkShareType.</returns>
  85. /// <exception cref="System.ArgumentException">Unknown share type</exception>
  86. private NetworkShareType ToNetworkShareType(ShareType shareType)
  87. {
  88. if (shareType.HasFlag(ShareType.Special))
  89. {
  90. return NetworkShareType.Special;
  91. }
  92. if (shareType.HasFlag(ShareType.Device))
  93. {
  94. return NetworkShareType.Device;
  95. }
  96. if (shareType.HasFlag(ShareType.Disk))
  97. {
  98. return NetworkShareType.Disk;
  99. }
  100. if (shareType.HasFlag(ShareType.IPC))
  101. {
  102. return NetworkShareType.Ipc;
  103. }
  104. if (shareType.HasFlag(ShareType.Printer))
  105. {
  106. return NetworkShareType.Printer;
  107. }
  108. throw new ArgumentException("Unknown share type");
  109. }
  110. /// <summary>
  111. /// Parses the specified endpointstring.
  112. /// </summary>
  113. /// <param name="endpointstring">The endpointstring.</param>
  114. /// <returns>IPEndPoint.</returns>
  115. public IPEndPoint Parse(string endpointstring)
  116. {
  117. return Parse(endpointstring, -1);
  118. }
  119. /// <summary>
  120. /// Parses the specified endpointstring.
  121. /// </summary>
  122. /// <param name="endpointstring">The endpointstring.</param>
  123. /// <param name="defaultport">The defaultport.</param>
  124. /// <returns>IPEndPoint.</returns>
  125. /// <exception cref="System.ArgumentException">Endpoint descriptor may not be empty.</exception>
  126. /// <exception cref="System.FormatException"></exception>
  127. private static IPEndPoint Parse(string endpointstring, int defaultport)
  128. {
  129. if (string.IsNullOrEmpty(endpointstring)
  130. || endpointstring.Trim().Length == 0)
  131. {
  132. throw new ArgumentException("Endpoint descriptor may not be empty.");
  133. }
  134. if (defaultport != -1 &&
  135. (defaultport < IPEndPoint.MinPort
  136. || defaultport > IPEndPoint.MaxPort))
  137. {
  138. throw new ArgumentException(string.Format("Invalid default port '{0}'", defaultport));
  139. }
  140. string[] values = endpointstring.Split(new char[] { ':' });
  141. IPAddress ipaddy;
  142. int port = -1;
  143. //check if we have an IPv6 or ports
  144. if (values.Length <= 2) // ipv4 or hostname
  145. {
  146. port = values.Length == 1 ? defaultport : GetPort(values[1]);
  147. //try to use the address as IPv4, otherwise get hostname
  148. if (!IPAddress.TryParse(values[0], out ipaddy))
  149. ipaddy = GetIPfromHost(values[0]);
  150. }
  151. else if (values.Length > 2) //ipv6
  152. {
  153. //could [a:b:c]:d
  154. if (values[0].StartsWith("[") && values[values.Length - 2].EndsWith("]"))
  155. {
  156. string ipaddressstring = string.Join(":", values.Take(values.Length - 1).ToArray());
  157. ipaddy = IPAddress.Parse(ipaddressstring);
  158. port = GetPort(values[values.Length - 1]);
  159. }
  160. else //[a:b:c] or a:b:c
  161. {
  162. ipaddy = IPAddress.Parse(endpointstring);
  163. port = defaultport;
  164. }
  165. }
  166. else
  167. {
  168. throw new FormatException(string.Format("Invalid endpoint ipaddress '{0}'", endpointstring));
  169. }
  170. if (port == -1)
  171. throw new ArgumentException(string.Format("No port specified: '{0}'", endpointstring));
  172. return new IPEndPoint(ipaddy, port);
  173. }
  174. protected static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  175. /// <summary>
  176. /// Gets the port.
  177. /// </summary>
  178. /// <param name="p">The p.</param>
  179. /// <returns>System.Int32.</returns>
  180. /// <exception cref="System.FormatException"></exception>
  181. private static int GetPort(string p)
  182. {
  183. int port;
  184. if (!int.TryParse(p, out port)
  185. || port < IPEndPoint.MinPort
  186. || port > IPEndPoint.MaxPort)
  187. {
  188. throw new FormatException(string.Format("Invalid end point port '{0}'", p));
  189. }
  190. return port;
  191. }
  192. /// <summary>
  193. /// Gets the I pfrom host.
  194. /// </summary>
  195. /// <param name="p">The p.</param>
  196. /// <returns>IPAddress.</returns>
  197. /// <exception cref="System.ArgumentException"></exception>
  198. private static IPAddress GetIPfromHost(string p)
  199. {
  200. var hosts = Dns.GetHostAddresses(p);
  201. if (hosts == null || hosts.Length == 0)
  202. throw new ArgumentException(string.Format("Host not found: {0}", p));
  203. return hosts[0];
  204. }
  205. /// <summary>
  206. /// Uses the DllImport : NetServerEnum with all its required parameters
  207. /// (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp
  208. /// for full details or method signature) to retrieve a list of domain SV_TYPE_WORKSTATION
  209. /// and SV_TYPE_SERVER PC's
  210. /// </summary>
  211. /// <returns>Arraylist that represents all the SV_TYPE_WORKSTATION and SV_TYPE_SERVER
  212. /// PC's in the Domain</returns>
  213. public IEnumerable<string> GetNetworkDevices()
  214. {
  215. //local fields
  216. const int MAX_PREFERRED_LENGTH = -1;
  217. var SV_TYPE_WORKSTATION = 1;
  218. var SV_TYPE_SERVER = 2;
  219. var buffer = IntPtr.Zero;
  220. var tmpBuffer = IntPtr.Zero;
  221. var entriesRead = 0;
  222. var totalEntries = 0;
  223. var resHandle = 0;
  224. var sizeofINFO = Marshal.SizeOf(typeof(_SERVER_INFO_100));
  225. try
  226. {
  227. //call the DllImport : NetServerEnum with all its required parameters
  228. //see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp
  229. //for full details of method signature
  230. var ret = NativeMethods.NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out resHandle);
  231. //if the returned with a NERR_Success (C++ term), =0 for C#
  232. if (ret == 0)
  233. {
  234. //loop through all SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's
  235. for (var i = 0; i < totalEntries; i++)
  236. {
  237. //get pointer to, Pointer to the buffer that received the data from
  238. //the call to NetServerEnum. Must ensure to use correct size of
  239. //STRUCTURE to ensure correct location in memory is pointed to
  240. tmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));
  241. //Have now got a pointer to the list of SV_TYPE_WORKSTATION and
  242. //SV_TYPE_SERVER PC's, which is unmanaged memory
  243. //Needs to Marshal data from an unmanaged block of memory to a
  244. //managed object, again using STRUCTURE to ensure the correct data
  245. //is marshalled
  246. var svrInfo = (_SERVER_INFO_100)Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100));
  247. //add the PC names to the ArrayList
  248. if (!string.IsNullOrEmpty(svrInfo.sv100_name))
  249. {
  250. yield return svrInfo.sv100_name;
  251. }
  252. }
  253. }
  254. }
  255. finally
  256. {
  257. //The NetApiBufferFree function frees
  258. //the memory that the NetApiBufferAllocate function allocates
  259. NativeMethods.NetApiBufferFree(buffer);
  260. }
  261. }
  262. }
  263. }