NetworkManager.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using MediaBrowser.Model.IO;
  2. using MediaBrowser.Model.Logging;
  3. using MediaBrowser.Model.Net;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Runtime.InteropServices;
  10. namespace MediaBrowser.ServerApplication.Networking
  11. {
  12. /// <summary>
  13. /// Class NetUtils
  14. /// </summary>
  15. public class NetworkManager : Emby.Common.Implementations.Networking.NetworkManager
  16. {
  17. public NetworkManager(ILogger logger)
  18. : base(logger)
  19. {
  20. }
  21. /// <summary>
  22. /// Gets the network shares.
  23. /// </summary>
  24. /// <param name="path">The path.</param>
  25. /// <returns>IEnumerable{NetworkShare}.</returns>
  26. public override IEnumerable<NetworkShare> GetNetworkShares(string path)
  27. {
  28. Logger.Info("Getting network shares from {0}", path);
  29. return new ShareCollection(path).OfType<Share>().Select(ToNetworkShare);
  30. }
  31. /// <summary>
  32. /// To the network share.
  33. /// </summary>
  34. /// <param name="share">The share.</param>
  35. /// <returns>NetworkShare.</returns>
  36. private NetworkShare ToNetworkShare(Share share)
  37. {
  38. return new NetworkShare
  39. {
  40. Name = share.NetName,
  41. Path = share.Path,
  42. Remark = share.Remark,
  43. Server = share.Server,
  44. ShareType = ToNetworkShareType(share.ShareType)
  45. };
  46. }
  47. /// <summary>
  48. /// To the type of the network share.
  49. /// </summary>
  50. /// <param name="shareType">Type of the share.</param>
  51. /// <returns>NetworkShareType.</returns>
  52. /// <exception cref="System.ArgumentException">Unknown share type</exception>
  53. private NetworkShareType ToNetworkShareType(ShareType shareType)
  54. {
  55. if (shareType.HasFlag(ShareType.Special))
  56. {
  57. return NetworkShareType.Special;
  58. }
  59. if (shareType.HasFlag(ShareType.Device))
  60. {
  61. return NetworkShareType.Device;
  62. }
  63. if (shareType.HasFlag(ShareType.Disk))
  64. {
  65. return NetworkShareType.Disk;
  66. }
  67. if (shareType.HasFlag(ShareType.IPC))
  68. {
  69. return NetworkShareType.Ipc;
  70. }
  71. if (shareType.HasFlag(ShareType.Printer))
  72. {
  73. return NetworkShareType.Printer;
  74. }
  75. throw new ArgumentException("Unknown share type");
  76. }
  77. /// <summary>
  78. /// Uses the DllImport : NetServerEnum with all its required parameters
  79. /// (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp
  80. /// for full details or method signature) to retrieve a list of domain SV_TYPE_WORKSTATION
  81. /// and SV_TYPE_SERVER PC's
  82. /// </summary>
  83. /// <returns>Arraylist that represents all the SV_TYPE_WORKSTATION and SV_TYPE_SERVER
  84. /// PC's in the Domain</returns>
  85. private List<string> GetNetworkDevicesInternal()
  86. {
  87. //local fields
  88. const int MAX_PREFERRED_LENGTH = -1;
  89. var SV_TYPE_WORKSTATION = 1;
  90. var SV_TYPE_SERVER = 2;
  91. IntPtr buffer = IntPtr.Zero;
  92. IntPtr tmpBuffer = IntPtr.Zero;
  93. var entriesRead = 0;
  94. var totalEntries = 0;
  95. var resHandle = 0;
  96. var sizeofINFO = Marshal.SizeOf(typeof(_SERVER_INFO_100));
  97. var returnList = new List<string>();
  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((Int64)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. returnList.Add(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. return returnList;
  135. }
  136. /// <summary>
  137. /// Gets available devices within the domain
  138. /// </summary>
  139. /// <returns>PC's in the Domain</returns>
  140. public override 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. /// Gets the network prefix.
  151. /// </summary>
  152. /// <value>The network prefix.</value>
  153. private string NetworkPrefix
  154. {
  155. get
  156. {
  157. var separator = Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture);
  158. return separator + separator;
  159. }
  160. }
  161. }
  162. }