NetworkManager.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using MediaBrowser.Common.Implementations.Networking;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Model.IO;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Model.Net;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Runtime.InteropServices;
  12. namespace MediaBrowser.ServerApplication.Networking
  13. {
  14. /// <summary>
  15. /// Class NetUtils
  16. /// </summary>
  17. public class NetworkManager : BaseNetworkManager, INetworkManager
  18. {
  19. public NetworkManager(ILogger logger)
  20. : base(logger)
  21. {
  22. }
  23. /// <summary>
  24. /// Gets the network shares.
  25. /// </summary>
  26. /// <param name="path">The path.</param>
  27. /// <returns>IEnumerable{NetworkShare}.</returns>
  28. public IEnumerable<NetworkShare> GetNetworkShares(string path)
  29. {
  30. return new ShareCollection(path).OfType<Share>().Select(ToNetworkShare);
  31. }
  32. /// <summary>
  33. /// To the network share.
  34. /// </summary>
  35. /// <param name="share">The share.</param>
  36. /// <returns>NetworkShare.</returns>
  37. private NetworkShare ToNetworkShare(Share share)
  38. {
  39. return new NetworkShare
  40. {
  41. Name = share.NetName,
  42. Path = share.Path,
  43. Remark = share.Remark,
  44. Server = share.Server,
  45. ShareType = ToNetworkShareType(share.ShareType)
  46. };
  47. }
  48. /// <summary>
  49. /// To the type of the network share.
  50. /// </summary>
  51. /// <param name="shareType">Type of the share.</param>
  52. /// <returns>NetworkShareType.</returns>
  53. /// <exception cref="System.ArgumentException">Unknown share type</exception>
  54. private NetworkShareType ToNetworkShareType(ShareType shareType)
  55. {
  56. if (shareType.HasFlag(ShareType.Special))
  57. {
  58. return NetworkShareType.Special;
  59. }
  60. if (shareType.HasFlag(ShareType.Device))
  61. {
  62. return NetworkShareType.Device;
  63. }
  64. if (shareType.HasFlag(ShareType.Disk))
  65. {
  66. return NetworkShareType.Disk;
  67. }
  68. if (shareType.HasFlag(ShareType.IPC))
  69. {
  70. return NetworkShareType.Ipc;
  71. }
  72. if (shareType.HasFlag(ShareType.Printer))
  73. {
  74. return NetworkShareType.Printer;
  75. }
  76. throw new ArgumentException("Unknown share type");
  77. }
  78. /// <summary>
  79. /// Uses the DllImport : NetServerEnum with all its required parameters
  80. /// (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp
  81. /// for full details or method signature) to retrieve a list of domain SV_TYPE_WORKSTATION
  82. /// and SV_TYPE_SERVER PC's
  83. /// </summary>
  84. /// <returns>Arraylist that represents all the SV_TYPE_WORKSTATION and SV_TYPE_SERVER
  85. /// PC's in the Domain</returns>
  86. private IEnumerable<string> GetNetworkDevicesInternal()
  87. {
  88. //local fields
  89. const int MAX_PREFERRED_LENGTH = -1;
  90. var SV_TYPE_WORKSTATION = 1;
  91. var SV_TYPE_SERVER = 2;
  92. var buffer = IntPtr.Zero;
  93. var tmpBuffer = IntPtr.Zero;
  94. var entriesRead = 0;
  95. var totalEntries = 0;
  96. var resHandle = 0;
  97. var sizeofINFO = Marshal.SizeOf(typeof(_SERVER_INFO_100));
  98. try
  99. {
  100. //call the DllImport : NetServerEnum with all its required parameters
  101. //see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp
  102. //for full details of method signature
  103. var ret = NativeMethods.NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out resHandle);
  104. //if the returned with a NERR_Success (C++ term), =0 for C#
  105. if (ret == 0)
  106. {
  107. //loop through all SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's
  108. for (var i = 0; i < totalEntries; i++)
  109. {
  110. //get pointer to, Pointer to the buffer that received the data from
  111. //the call to NetServerEnum. Must ensure to use correct size of
  112. //STRUCTURE to ensure correct location in memory is pointed to
  113. tmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));
  114. //Have now got a pointer to the list of SV_TYPE_WORKSTATION and
  115. //SV_TYPE_SERVER PC's, which is unmanaged memory
  116. //Needs to Marshal data from an unmanaged block of memory to a
  117. //managed object, again using STRUCTURE to ensure the correct data
  118. //is marshalled
  119. var svrInfo = (_SERVER_INFO_100)Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100));
  120. //add the PC names to the ArrayList
  121. if (!string.IsNullOrEmpty(svrInfo.sv100_name))
  122. {
  123. yield return svrInfo.sv100_name;
  124. }
  125. }
  126. }
  127. }
  128. finally
  129. {
  130. //The NetApiBufferFree function frees
  131. //the memory that the NetApiBufferAllocate function allocates
  132. NativeMethods.NetApiBufferFree(buffer);
  133. }
  134. }
  135. /// <summary>
  136. /// Gets available devices within the domain
  137. /// </summary>
  138. /// <returns>PC's in the Domain</returns>
  139. public IEnumerable<FileSystemEntryInfo> GetNetworkDevices()
  140. {
  141. return GetNetworkDevicesInternal().Select(c => new FileSystemEntryInfo
  142. {
  143. Name = c,
  144. Path = NetworkPrefix + c,
  145. Type = FileSystemEntryType.NetworkComputer
  146. });
  147. }
  148. /// <summary>
  149. /// Generates a self signed certificate at the locatation specified by <paramref name="certificatePath"/>.
  150. /// </summary>
  151. /// <param name="certificatePath">The path to generate the certificate.</param>
  152. /// <param name="hostname">The common name for the certificate.</param>
  153. public void GenerateSelfSignedSslCertificate(string certificatePath, string hostname)
  154. {
  155. CertificateGenerator.CreateSelfSignCertificatePfx(certificatePath, hostname, Logger);
  156. }
  157. /// <summary>
  158. /// Gets the network prefix.
  159. /// </summary>
  160. /// <value>The network prefix.</value>
  161. private string NetworkPrefix
  162. {
  163. get
  164. {
  165. var separator = Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture);
  166. return separator + separator;
  167. }
  168. }
  169. }
  170. }