Handle ENETDOWN error if defined.
[mono.git] / mcs / class / System / System.Net.NetworkInformation / NetworkChange.cs
1 //
2 // System.Net.NetworkInformation.NetworkChange
3 //
4 // Author:
5 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //
7 // Copyright (c) 2006,2011 Novell, Inc. (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Net.Sockets;
30 using System.Runtime.InteropServices;
31 using System.Threading;
32
33 namespace System.Net.NetworkInformation {
34         public sealed class NetworkChange {
35                 [Flags]
36                 enum EventType {
37                         Availability = 1 << 0,
38                         Address = 1 << 1,
39                 }
40
41                 static object _lock = new object ();
42                 static Socket nl_sock;
43                 static SocketAsyncEventArgs nl_args;
44                 static EventType pending_events;
45                 static Timer timer;
46
47                 static NetworkAddressChangedEventHandler AddressChanged;
48                 static NetworkAvailabilityChangedEventHandler AvailabilityChanged;
49
50                 private NetworkChange ()
51                 {
52                 }
53
54                 public static event NetworkAddressChangedEventHandler NetworkAddressChanged {
55                         add { Register (value); }
56                         remove { Unregister (value); }
57                 }
58
59                 public static event NetworkAvailabilityChangedEventHandler NetworkAvailabilityChanged {
60                         add { Register (value); }
61                         remove { Unregister (value); }
62                 }
63
64                 //internal Socket (AddressFamily family, SocketType type, ProtocolType proto, IntPtr sock)
65
66                 static bool EnsureSocket ()
67                 {
68                         lock (_lock) {
69                                 if (nl_sock != null)
70                                         return true;
71                                 IntPtr fd = CreateNLSocket ();
72                                 if (fd.ToInt64 () == -1)
73                                         return false;
74
75                                 nl_sock = new Socket (0, SocketType.Raw, ProtocolType.Udp, fd);
76                                 nl_args = new SocketAsyncEventArgs ();
77                                 nl_args.SetBuffer (new byte [8192], 0, 8192);
78                                 nl_args.Completed += OnDataAvailable;
79                                 nl_sock.ReceiveAsync (nl_args);
80                         }
81                         return true;
82                 }
83
84                 // _lock is held by the caller
85                 static void MaybeCloseSocket ()
86                 {
87                         if (nl_sock == null || AvailabilityChanged != null || AddressChanged != null)
88                                 return;
89
90                         CloseNLSocket (nl_sock.Handle);
91                         GC.SuppressFinalize (nl_sock);
92                         nl_sock = null;
93                         nl_args = null;
94                 }
95
96                 static bool GetAvailability ()
97                 {
98                         NetworkInterface [] adapters = NetworkInterface.GetAllNetworkInterfaces ();
99                         foreach (NetworkInterface n in adapters) {
100                                 // TODO: also check for a default route present?
101                                 if (n.NetworkInterfaceType == NetworkInterfaceType.Loopback)
102                                         continue;
103                                 if (n.OperationalStatus == OperationalStatus.Up)
104                                         return true;
105                         }
106                         return false;
107                 }
108
109                 static void OnAvailabilityChanged (object unused)
110                 {
111                         NetworkAvailabilityChangedEventHandler d = AvailabilityChanged;
112                         d (null, new NetworkAvailabilityEventArgs (GetAvailability ()));
113                 }
114
115                 static void OnAddressChanged (object unused)
116                 {
117                         NetworkAddressChangedEventHandler d = AddressChanged;
118                         d (null, EventArgs.Empty);
119                 }
120
121                 static void OnEventDue (object unused)
122                 {
123                         EventType evts;
124                         lock (_lock) {
125                                 evts = pending_events;
126                                 pending_events = 0;
127                                 timer.Change (-1, -1);
128                         }
129                         if ((evts & EventType.Availability) != 0)
130                                 ThreadPool.QueueUserWorkItem (OnAvailabilityChanged);
131                         if ((evts & EventType.Address) != 0)
132                                 ThreadPool.QueueUserWorkItem (OnAddressChanged);
133                 }
134
135                 static void QueueEvent (EventType type)
136                 {
137                         lock (_lock) {
138                                 if (timer == null)
139                                         timer = new Timer (OnEventDue);
140                                 if (pending_events == 0)
141                                         timer.Change (150, -1);
142                                 pending_events |= type;
143                         }
144                 }
145
146                 unsafe static void OnDataAvailable (object sender, SocketAsyncEventArgs args)
147                 {
148                         EventType type;
149                         fixed (byte *ptr = args.Buffer) {       
150                                 type = ReadEvents (nl_sock.Handle, new IntPtr (ptr), args.BytesTransferred, 8192);
151                         }
152                         nl_sock.ReceiveAsync (nl_args);
153                         if (type != 0)
154                                 QueueEvent (type);
155                 }
156
157                 static void Register (NetworkAddressChangedEventHandler d)
158                 {
159                         EnsureSocket ();
160                         AddressChanged += d;
161                 }
162
163                 static void Register (NetworkAvailabilityChangedEventHandler d)
164                 {
165                         EnsureSocket ();
166                         AvailabilityChanged += d;
167                 }
168
169                 static void Unregister (NetworkAddressChangedEventHandler d)
170                 {
171                         lock (_lock) {
172                                 AddressChanged -= d;
173                                 MaybeCloseSocket ();
174                         }
175                 }
176
177                 static void Unregister (NetworkAvailabilityChangedEventHandler d)
178                 {
179                         lock (_lock) {
180                                 AvailabilityChanged -= d;
181                                 MaybeCloseSocket ();
182                         }
183                 }
184
185 #if MONOTOUCH || MONODROID
186                 const string LIBNAME = "__Internal";
187 #else
188                 const string LIBNAME = "MonoPosixHelper";
189 #endif
190
191                 [DllImport (LIBNAME, CallingConvention=CallingConvention.Cdecl)]
192                 static extern IntPtr CreateNLSocket ();
193
194                 [DllImport (LIBNAME, CallingConvention=CallingConvention.Cdecl)]
195                 static extern EventType ReadEvents (IntPtr sock, IntPtr buffer, int count, int size);
196
197                 [DllImport (LIBNAME, CallingConvention=CallingConvention.Cdecl)]
198                 static extern IntPtr CloseNLSocket (IntPtr sock);
199         }
200 }
201