NetworkManager.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Model.IO;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Model.Net;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Runtime.InteropServices;
  11. using Emby.Common.Implementations.Networking;
  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 List<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. IntPtr buffer = IntPtr.Zero;
  94. IntPtr 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. var returnList = new List<string>();
  100. try
  101. {
  102. //call the DllImport : NetServerEnum with all its required parameters
  103. //see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp
  104. //for full details of method signature
  105. var ret = NativeMethods.NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out resHandle);
  106. //if the returned with a NERR_Success (C++ term), =0 for C#
  107. if (ret == 0)
  108. {
  109. //loop through all SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's
  110. for (var i = 0; i < totalEntries; i++)
  111. {
  112. //get pointer to, Pointer to the buffer that received the data from
  113. //the call to NetServerEnum. Must ensure to use correct size of
  114. //STRUCTURE to ensure correct location in memory is pointed to
  115. tmpBuffer = new IntPtr((Int64)buffer + (i * sizeofINFO));
  116. //Have now got a pointer to the list of SV_TYPE_WORKSTATION and
  117. //SV_TYPE_SERVER PC's, which is unmanaged memory
  118. //Needs to Marshal data from an unmanaged block of memory to a
  119. //managed object, again using STRUCTURE to ensure the correct data
  120. //is marshalled
  121. var svrInfo = (_SERVER_INFO_100)Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100));
  122. //add the PC names to the ArrayList
  123. if (!string.IsNullOrEmpty(svrInfo.sv100_name))
  124. {
  125. returnList.Add(svrInfo.sv100_name);
  126. }
  127. }
  128. }
  129. }
  130. finally
  131. {
  132. //The NetApiBufferFree function frees
  133. //the memory that the NetApiBufferAllocate function allocates
  134. NativeMethods.NetApiBufferFree(buffer);
  135. }
  136. return returnList;
  137. }
  138. /// <summary>
  139. /// Gets available devices within the domain
  140. /// </summary>
  141. /// <returns>PC's in the Domain</returns>
  142. public IEnumerable<FileSystemEntryInfo> GetNetworkDevices()
  143. {
  144. return GetNetworkDevicesInternal().Select(c => new FileSystemEntryInfo
  145. {
  146. Name = c,
  147. Path = NetworkPrefix + c,
  148. Type = FileSystemEntryType.NetworkComputer
  149. });
  150. }
  151. /// <summary>
  152. /// Generates a self signed certificate at the locatation specified by <paramref name="certificatePath"/>.
  153. /// </summary>
  154. /// <param name="certificatePath">The path to generate the certificate.</param>
  155. /// <param name="hostname">The common name for the certificate.</param>
  156. public void GenerateSelfSignedSslCertificate(string certificatePath, string hostname)
  157. {
  158. CertificateGenerator.CreateSelfSignCertificatePfx(certificatePath, hostname, Logger);
  159. }
  160. /// <summary>
  161. /// Gets the network prefix.
  162. /// </summary>
  163. /// <value>The network prefix.</value>
  164. private string NetworkPrefix
  165. {
  166. get
  167. {
  168. var separator = Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture);
  169. return separator + separator;
  170. }
  171. }
  172. }
  173. }