NetworkChange.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. //
  2. // System.Net.NetworkInformation.NetworkChange
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier (LinuxNetworkChange) ([email protected])
  6. // Aaron Bockover (MacNetworkChange) ([email protected])
  7. //
  8. // Copyright (c) 2006,2011 Novell, Inc. (http://www.novell.com)
  9. // Copyright (c) 2013 Xamarin, Inc. (http://www.xamarin.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Net.Sockets;
  32. using System.Runtime.InteropServices;
  33. using System.Threading;
  34. #if NETWORK_CHANGE_STANDALONE
  35. namespace NetworkInformation {
  36. public class NetworkAvailabilityEventArgs : EventArgs
  37. {
  38. public bool IsAvailable { get; set; }
  39. public NetworkAvailabilityEventArgs (bool available)
  40. {
  41. IsAvailable = available;
  42. }
  43. }
  44. public delegate void NetworkAddressChangedEventHandler (object sender, EventArgs args);
  45. public delegate void NetworkAvailabilityChangedEventHandler (object sender, NetworkAvailabilityEventArgs args);
  46. #else
  47. namespace System.Net.NetworkInformation {
  48. #endif
  49. internal interface INetworkChange : IDisposable {
  50. event NetworkAddressChangedEventHandler NetworkAddressChanged;
  51. event NetworkAvailabilityChangedEventHandler NetworkAvailabilityChanged;
  52. bool HasRegisteredEvents { get; }
  53. }
  54. public sealed class NetworkChange {
  55. static INetworkChange networkChange;
  56. public static event NetworkAddressChangedEventHandler NetworkAddressChanged {
  57. add {
  58. lock (typeof (INetworkChange)) {
  59. MaybeCreate ();
  60. if (networkChange != null)
  61. networkChange.NetworkAddressChanged += value;
  62. }
  63. }
  64. remove {
  65. lock (typeof (INetworkChange)) {
  66. if (networkChange != null) {
  67. networkChange.NetworkAddressChanged -= value;
  68. MaybeDispose ();
  69. }
  70. }
  71. }
  72. }
  73. public static event NetworkAvailabilityChangedEventHandler NetworkAvailabilityChanged {
  74. add {
  75. lock (typeof (INetworkChange)) {
  76. MaybeCreate ();
  77. if (networkChange != null)
  78. networkChange.NetworkAvailabilityChanged += value;
  79. }
  80. }
  81. remove {
  82. lock (typeof (INetworkChange)) {
  83. if (networkChange != null) {
  84. networkChange.NetworkAvailabilityChanged -= value;
  85. MaybeDispose ();
  86. }
  87. }
  88. }
  89. }
  90. static void MaybeCreate ()
  91. {
  92. if (networkChange != null)
  93. return;
  94. try {
  95. networkChange = new MacNetworkChange ();
  96. } catch {
  97. #if !NETWORK_CHANGE_STANDALONE && !MONOTOUCH
  98. networkChange = new LinuxNetworkChange ();
  99. #endif
  100. }
  101. }
  102. static void MaybeDispose ()
  103. {
  104. if (networkChange != null && networkChange.HasRegisteredEvents) {
  105. networkChange.Dispose ();
  106. networkChange = null;
  107. }
  108. }
  109. }
  110. internal sealed class MacNetworkChange : INetworkChange
  111. {
  112. const string DL_LIB = "/usr/lib/libSystem.dylib";
  113. const string CORE_SERVICES_LIB = "/System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration";
  114. const string CORE_FOUNDATION_LIB = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";
  115. [UnmanagedFunctionPointerAttribute (CallingConvention.Cdecl)]
  116. delegate void SCNetworkReachabilityCallback (IntPtr target, NetworkReachabilityFlags flags, IntPtr info);
  117. [DllImport (DL_LIB)]
  118. static extern IntPtr dlopen (string path, int mode);
  119. [DllImport (DL_LIB)]
  120. static extern IntPtr dlsym (IntPtr handle, string symbol);
  121. [DllImport (DL_LIB)]
  122. static extern int dlclose (IntPtr handle);
  123. [DllImport (CORE_FOUNDATION_LIB)]
  124. static extern void CFRelease (IntPtr handle);
  125. [DllImport (CORE_FOUNDATION_LIB)]
  126. static extern IntPtr CFRunLoopGetMain ();
  127. [DllImport (CORE_SERVICES_LIB)]
  128. static extern IntPtr SCNetworkReachabilityCreateWithAddress (IntPtr allocator, ref sockaddr_in sockaddr);
  129. [DllImport (CORE_SERVICES_LIB)]
  130. static extern bool SCNetworkReachabilityGetFlags (IntPtr reachability, out NetworkReachabilityFlags flags);
  131. [DllImport (CORE_SERVICES_LIB)]
  132. static extern bool SCNetworkReachabilitySetCallback (IntPtr reachability, SCNetworkReachabilityCallback callback, ref SCNetworkReachabilityContext context);
  133. [DllImport (CORE_SERVICES_LIB)]
  134. static extern bool SCNetworkReachabilityScheduleWithRunLoop (IntPtr reachability, IntPtr runLoop, IntPtr runLoopMode);
  135. [DllImport (CORE_SERVICES_LIB)]
  136. static extern bool SCNetworkReachabilityUnscheduleFromRunLoop (IntPtr reachability, IntPtr runLoop, IntPtr runLoopMode);
  137. [StructLayout (LayoutKind.Explicit, Size = 28)]
  138. struct sockaddr_in {
  139. [FieldOffset (0)] public byte sin_len;
  140. [FieldOffset (1)] public byte sin_family;
  141. public static sockaddr_in Create ()
  142. {
  143. return new sockaddr_in {
  144. sin_len = 28,
  145. sin_family = 2 // AF_INET
  146. };
  147. }
  148. }
  149. [StructLayout (LayoutKind.Sequential)]
  150. struct SCNetworkReachabilityContext {
  151. public IntPtr version;
  152. public IntPtr info;
  153. public IntPtr retain;
  154. public IntPtr release;
  155. public IntPtr copyDescription;
  156. }
  157. [Flags]
  158. enum NetworkReachabilityFlags {
  159. None = 0,
  160. TransientConnection = 1 << 0,
  161. Reachable = 1 << 1,
  162. ConnectionRequired = 1 << 2,
  163. ConnectionOnTraffic = 1 << 3,
  164. InterventionRequired = 1 << 4,
  165. ConnectionOnDemand = 1 << 5,
  166. IsLocalAddress = 1 << 16,
  167. IsDirect = 1 << 17,
  168. IsWWAN = 1 << 18,
  169. ConnectionAutomatic = ConnectionOnTraffic
  170. }
  171. IntPtr handle;
  172. IntPtr runLoopMode;
  173. SCNetworkReachabilityCallback callback;
  174. bool scheduledWithRunLoop;
  175. NetworkReachabilityFlags flags;
  176. event NetworkAddressChangedEventHandler networkAddressChanged;
  177. event NetworkAvailabilityChangedEventHandler networkAvailabilityChanged;
  178. public event NetworkAddressChangedEventHandler NetworkAddressChanged {
  179. add {
  180. value (null, EventArgs.Empty);
  181. networkAddressChanged += value;
  182. }
  183. remove { networkAddressChanged -= value; }
  184. }
  185. public event NetworkAvailabilityChangedEventHandler NetworkAvailabilityChanged {
  186. add {
  187. value (null, new NetworkAvailabilityEventArgs (IsAvailable));
  188. networkAvailabilityChanged += value;
  189. }
  190. remove { networkAvailabilityChanged -= value; }
  191. }
  192. bool IsAvailable {
  193. get {
  194. return (flags & NetworkReachabilityFlags.Reachable) != 0 &&
  195. (flags & NetworkReachabilityFlags.ConnectionRequired) == 0;
  196. }
  197. }
  198. public bool HasRegisteredEvents {
  199. get { return networkAddressChanged != null || networkAvailabilityChanged != null; }
  200. }
  201. public MacNetworkChange ()
  202. {
  203. var sockaddr = sockaddr_in.Create ();
  204. handle = SCNetworkReachabilityCreateWithAddress (IntPtr.Zero, ref sockaddr);
  205. if (handle == IntPtr.Zero)
  206. throw new Exception ("SCNetworkReachabilityCreateWithAddress returned NULL");
  207. callback = new SCNetworkReachabilityCallback (HandleCallback);
  208. var info = new SCNetworkReachabilityContext {
  209. info = GCHandle.ToIntPtr (GCHandle.Alloc (this))
  210. };
  211. SCNetworkReachabilitySetCallback (handle, callback, ref info);
  212. scheduledWithRunLoop =
  213. LoadRunLoopMode () &&
  214. SCNetworkReachabilityScheduleWithRunLoop (handle, CFRunLoopGetMain (), runLoopMode);
  215. SCNetworkReachabilityGetFlags (handle, out flags);
  216. }
  217. bool LoadRunLoopMode ()
  218. {
  219. var cfLibHandle = dlopen (CORE_FOUNDATION_LIB, 0);
  220. if (cfLibHandle == IntPtr.Zero)
  221. return false;
  222. try {
  223. runLoopMode = dlsym (cfLibHandle, "kCFRunLoopDefaultMode");
  224. if (runLoopMode != IntPtr.Zero) {
  225. runLoopMode = Marshal.ReadIntPtr (runLoopMode);
  226. return runLoopMode != IntPtr.Zero;
  227. }
  228. } finally {
  229. dlclose (cfLibHandle);
  230. }
  231. return false;
  232. }
  233. public void Dispose ()
  234. {
  235. lock (this) {
  236. if (handle == IntPtr.Zero)
  237. return;
  238. if (scheduledWithRunLoop)
  239. SCNetworkReachabilityUnscheduleFromRunLoop (handle, CFRunLoopGetMain (), runLoopMode);
  240. CFRelease (handle);
  241. handle = IntPtr.Zero;
  242. callback = null;
  243. flags = NetworkReachabilityFlags.None;
  244. scheduledWithRunLoop = false;
  245. }
  246. }
  247. #if MONOTOUCH
  248. [MonoTouch.MonoPInvokeCallback (typeof (SCNetworkReachabilityCallback))]
  249. #endif
  250. static void HandleCallback (IntPtr reachability, NetworkReachabilityFlags flags, IntPtr info)
  251. {
  252. if (info == IntPtr.Zero)
  253. return;
  254. var instance = GCHandle.FromIntPtr (info).Target as MacNetworkChange;
  255. if (instance == null || instance.flags == flags)
  256. return;
  257. instance.flags = flags;
  258. var addressChanged = instance.networkAddressChanged;
  259. if (addressChanged != null)
  260. addressChanged (null, EventArgs.Empty);
  261. var availabilityChanged = instance.networkAvailabilityChanged;
  262. if (availabilityChanged != null)
  263. availabilityChanged (null, new NetworkAvailabilityEventArgs (instance.IsAvailable));
  264. }
  265. }
  266. #if !NETWORK_CHANGE_STANDALONE && !MONOTOUCH
  267. internal sealed class LinuxNetworkChange : INetworkChange {
  268. [Flags]
  269. enum EventType {
  270. Availability = 1 << 0,
  271. Address = 1 << 1,
  272. }
  273. object _lock = new object ();
  274. Socket nl_sock;
  275. SocketAsyncEventArgs nl_args;
  276. EventType pending_events;
  277. Timer timer;
  278. NetworkAddressChangedEventHandler AddressChanged;
  279. NetworkAvailabilityChangedEventHandler AvailabilityChanged;
  280. public event NetworkAddressChangedEventHandler NetworkAddressChanged {
  281. add { Register (value); }
  282. remove { Unregister (value); }
  283. }
  284. public event NetworkAvailabilityChangedEventHandler NetworkAvailabilityChanged {
  285. add { Register (value); }
  286. remove { Unregister (value); }
  287. }
  288. public bool HasRegisteredEvents {
  289. get { return AddressChanged != null || AvailabilityChanged != null; }
  290. }
  291. public void Dispose ()
  292. {
  293. }
  294. //internal Socket (AddressFamily family, SocketType type, ProtocolType proto, IntPtr sock)
  295. bool EnsureSocket ()
  296. {
  297. lock (_lock) {
  298. if (nl_sock != null)
  299. return true;
  300. IntPtr fd = CreateNLSocket ();
  301. if (fd.ToInt64 () == -1)
  302. return false;
  303. nl_sock = new Socket (0, SocketType.Raw, ProtocolType.Udp, fd);
  304. nl_args = new SocketAsyncEventArgs ();
  305. nl_args.SetBuffer (new byte [8192], 0, 8192);
  306. nl_args.Completed += OnDataAvailable;
  307. nl_sock.ReceiveAsync (nl_args);
  308. }
  309. return true;
  310. }
  311. // _lock is held by the caller
  312. void MaybeCloseSocket ()
  313. {
  314. if (nl_sock == null || AvailabilityChanged != null || AddressChanged != null)
  315. return;
  316. CloseNLSocket (nl_sock.Handle);
  317. GC.SuppressFinalize (nl_sock);
  318. nl_sock = null;
  319. nl_args = null;
  320. }
  321. bool GetAvailability ()
  322. {
  323. NetworkInterface [] adapters = NetworkInterface.GetAllNetworkInterfaces ();
  324. foreach (NetworkInterface n in adapters) {
  325. // TODO: also check for a default route present?
  326. if (n.NetworkInterfaceType == NetworkInterfaceType.Loopback)
  327. continue;
  328. if (n.OperationalStatus == OperationalStatus.Up)
  329. return true;
  330. }
  331. return false;
  332. }
  333. void OnAvailabilityChanged (object unused)
  334. {
  335. NetworkAvailabilityChangedEventHandler d = AvailabilityChanged;
  336. d (null, new NetworkAvailabilityEventArgs (GetAvailability ()));
  337. }
  338. void OnAddressChanged (object unused)
  339. {
  340. NetworkAddressChangedEventHandler d = AddressChanged;
  341. d (null, EventArgs.Empty);
  342. }
  343. void OnEventDue (object unused)
  344. {
  345. EventType evts;
  346. lock (_lock) {
  347. evts = pending_events;
  348. pending_events = 0;
  349. timer.Change (-1, -1);
  350. }
  351. if ((evts & EventType.Availability) != 0)
  352. ThreadPool.QueueUserWorkItem (OnAvailabilityChanged);
  353. if ((evts & EventType.Address) != 0)
  354. ThreadPool.QueueUserWorkItem (OnAddressChanged);
  355. }
  356. void QueueEvent (EventType type)
  357. {
  358. lock (_lock) {
  359. if (timer == null)
  360. timer = new Timer (OnEventDue);
  361. if (pending_events == 0)
  362. timer.Change (150, -1);
  363. pending_events |= type;
  364. }
  365. }
  366. unsafe void OnDataAvailable (object sender, SocketAsyncEventArgs args)
  367. {
  368. EventType type;
  369. fixed (byte *ptr = args.Buffer) {
  370. type = ReadEvents (nl_sock.Handle, new IntPtr (ptr), args.BytesTransferred, 8192);
  371. }
  372. nl_sock.ReceiveAsync (nl_args);
  373. if (type != 0)
  374. QueueEvent (type);
  375. }
  376. void Register (NetworkAddressChangedEventHandler d)
  377. {
  378. EnsureSocket ();
  379. AddressChanged += d;
  380. }
  381. void Register (NetworkAvailabilityChangedEventHandler d)
  382. {
  383. EnsureSocket ();
  384. AvailabilityChanged += d;
  385. }
  386. void Unregister (NetworkAddressChangedEventHandler d)
  387. {
  388. lock (_lock) {
  389. AddressChanged -= d;
  390. MaybeCloseSocket ();
  391. }
  392. }
  393. void Unregister (NetworkAvailabilityChangedEventHandler d)
  394. {
  395. lock (_lock) {
  396. AvailabilityChanged -= d;
  397. MaybeCloseSocket ();
  398. }
  399. }
  400. #if MONOTOUCH || MONODROID
  401. const string LIBNAME = "__Internal";
  402. #else
  403. const string LIBNAME = "MonoPosixHelper";
  404. #endif
  405. [DllImport (LIBNAME, CallingConvention=CallingConvention.Cdecl)]
  406. static extern IntPtr CreateNLSocket ();
  407. [DllImport (LIBNAME, CallingConvention=CallingConvention.Cdecl)]
  408. static extern EventType ReadEvents (IntPtr sock, IntPtr buffer, int count, int size);
  409. [DllImport (LIBNAME, CallingConvention=CallingConvention.Cdecl)]
  410. static extern IntPtr CloseNLSocket (IntPtr sock);
  411. }
  412. #endif
  413. }