NetUtils.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using MediaBrowser.Common.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Management;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.Runtime.InteropServices;
  10. namespace MediaBrowser.Common.Net
  11. {
  12. /// <summary>
  13. /// Class NetUtils
  14. /// </summary>
  15. public static class NetUtils
  16. {
  17. /// <summary>
  18. /// Gets the machine's local ip address
  19. /// </summary>
  20. /// <returns>IPAddress.</returns>
  21. public static IPAddress GetLocalIpAddress()
  22. {
  23. var host = Dns.GetHostEntry(Dns.GetHostName());
  24. return host.AddressList.FirstOrDefault(i => i.AddressFamily == AddressFamily.InterNetwork);
  25. }
  26. /// <summary>
  27. /// Gets a random port number that is currently available
  28. /// </summary>
  29. /// <returns>System.Int32.</returns>
  30. public static int GetRandomUnusedPort()
  31. {
  32. var listener = new TcpListener(IPAddress.Any, 0);
  33. listener.Start();
  34. var port = ((IPEndPoint)listener.LocalEndpoint).Port;
  35. listener.Stop();
  36. return port;
  37. }
  38. /// <summary>
  39. /// Creates the netsh URL registration.
  40. /// </summary>
  41. /// <param name="urlPrefix">The URL prefix.</param>
  42. public static void CreateNetshUrlRegistration(string urlPrefix)
  43. {
  44. var startInfo = new ProcessStartInfo
  45. {
  46. FileName = "netsh",
  47. Arguments = string.Format("http add urlacl url={0} user=\"NT AUTHORITY\\Authenticated Users\"", urlPrefix),
  48. CreateNoWindow = true,
  49. WindowStyle = ProcessWindowStyle.Hidden,
  50. Verb = "runas",
  51. ErrorDialog = false
  52. };
  53. using (var process = Process.Start(startInfo))
  54. {
  55. process.WaitForExit();
  56. }
  57. }
  58. /// <summary>
  59. /// Adds the windows firewall rule.
  60. /// </summary>
  61. /// <param name="port">The port.</param>
  62. /// <param name="protocol">The protocol.</param>
  63. public static void AddWindowsFirewallRule(int port, NetworkProtocol protocol)
  64. {
  65. // First try to remove it so we don't end up creating duplicates
  66. RemoveWindowsFirewallRule(port, protocol);
  67. var args = string.Format("advfirewall firewall add rule name=\"Port {0}\" dir=in action=allow protocol={1} localport={0}", port, protocol);
  68. RunNetsh(args);
  69. }
  70. /// <summary>
  71. /// Removes the windows firewall rule.
  72. /// </summary>
  73. /// <param name="port">The port.</param>
  74. /// <param name="protocol">The protocol.</param>
  75. public static void RemoveWindowsFirewallRule(int port, NetworkProtocol protocol)
  76. {
  77. var args = string.Format("advfirewall firewall delete rule name=\"Port {0}\" protocol={1} localport={0}", port, protocol);
  78. RunNetsh(args);
  79. }
  80. /// <summary>
  81. /// Runs the netsh.
  82. /// </summary>
  83. /// <param name="args">The args.</param>
  84. private static void RunNetsh(string args)
  85. {
  86. var startInfo = new ProcessStartInfo
  87. {
  88. FileName = "netsh",
  89. Arguments = args,
  90. CreateNoWindow = true,
  91. WindowStyle = ProcessWindowStyle.Hidden,
  92. Verb = "runas",
  93. ErrorDialog = false
  94. };
  95. using (var process = new Process { StartInfo = startInfo })
  96. {
  97. process.Start();
  98. process.WaitForExit();
  99. }
  100. }
  101. /// <summary>
  102. /// Returns MAC Address from first Network Card in Computer
  103. /// </summary>
  104. /// <returns>[string] MAC Address</returns>
  105. public static string GetMacAddress()
  106. {
  107. var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  108. var moc = mc.GetInstances();
  109. var macAddress = String.Empty;
  110. foreach (ManagementObject mo in moc)
  111. {
  112. if (macAddress == String.Empty) // only return MAC Address from first card
  113. {
  114. try
  115. {
  116. if ((bool)mo["IPEnabled"]) macAddress = mo["MacAddress"].ToString();
  117. }
  118. catch
  119. {
  120. mo.Dispose();
  121. return "";
  122. }
  123. }
  124. mo.Dispose();
  125. }
  126. return macAddress.Replace(":", "");
  127. }
  128. /// <summary>
  129. /// Uses the DllImport : NetServerEnum with all its required parameters
  130. /// (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp
  131. /// for full details or method signature) to retrieve a list of domain SV_TYPE_WORKSTATION
  132. /// and SV_TYPE_SERVER PC's
  133. /// </summary>
  134. /// <returns>Arraylist that represents all the SV_TYPE_WORKSTATION and SV_TYPE_SERVER
  135. /// PC's in the Domain</returns>
  136. public static IEnumerable<string> GetNetworkComputers()
  137. {
  138. //local fields
  139. const int MAX_PREFERRED_LENGTH = -1;
  140. var SV_TYPE_WORKSTATION = 1;
  141. var SV_TYPE_SERVER = 2;
  142. var buffer = IntPtr.Zero;
  143. var tmpBuffer = IntPtr.Zero;
  144. var entriesRead = 0;
  145. var totalEntries = 0;
  146. var resHandle = 0;
  147. var sizeofINFO = Marshal.SizeOf(typeof(_SERVER_INFO_100));
  148. try
  149. {
  150. //call the DllImport : NetServerEnum with all its required parameters
  151. //see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp
  152. //for full details of method signature
  153. var ret = NativeMethods.NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out resHandle);
  154. //if the returned with a NERR_Success (C++ term), =0 for C#
  155. if (ret == 0)
  156. {
  157. //loop through all SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's
  158. for (var i = 0; i < totalEntries; i++)
  159. {
  160. //get pointer to, Pointer to the buffer that received the data from
  161. //the call to NetServerEnum. Must ensure to use correct size of
  162. //STRUCTURE to ensure correct location in memory is pointed to
  163. tmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));
  164. //Have now got a pointer to the list of SV_TYPE_WORKSTATION and
  165. //SV_TYPE_SERVER PC's, which is unmanaged memory
  166. //Needs to Marshal data from an unmanaged block of memory to a
  167. //managed object, again using STRUCTURE to ensure the correct data
  168. //is marshalled
  169. var svrInfo = (_SERVER_INFO_100)Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100));
  170. //add the PC names to the ArrayList
  171. if (!string.IsNullOrEmpty(svrInfo.sv100_name))
  172. {
  173. yield return svrInfo.sv100_name;
  174. }
  175. }
  176. }
  177. }
  178. finally
  179. {
  180. //The NetApiBufferFree function frees
  181. //the memory that the NetApiBufferAllocate function allocates
  182. NativeMethods.NetApiBufferFree(buffer);
  183. }
  184. }
  185. }
  186. /// <summary>
  187. /// Enum NetworkProtocol
  188. /// </summary>
  189. public enum NetworkProtocol
  190. {
  191. /// <summary>
  192. /// The TCP
  193. /// </summary>
  194. Tcp,
  195. /// <summary>
  196. /// The UDP
  197. /// </summary>
  198. Udp
  199. }
  200. }