NetworkManager.cs 7.1 KB

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