1 // System.Net.Sockets.Socket.cs
4 // Phillip Pearson (pp@myelin.co.nz)
5 // Dick Porter <dick@ximian.com>
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Sridhar Kulkarni (sridharkulkarni@gmail.com)
8 // Brian Nickel (brian.nickel@gmail.com)
10 // Copyright (C) 2001, 2002 Phillip Pearson and Ximian, Inc.
11 // http://www.myelin.co.nz
12 // (c) 2004-2011 Novell, Inc. (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 using System.Collections;
39 using System.Collections.Generic;
40 using System.Runtime.CompilerServices;
41 using System.Runtime.InteropServices;
42 using System.Threading;
44 using System.Security;
48 using System.Net.Configuration;
49 using System.Net.NetworkInformation;
52 namespace System.Net.Sockets {
54 public partial class Socket : IDisposable {
55 [StructLayout (LayoutKind.Sequential)]
61 // Used by the runtime
62 internal enum SocketOperation {
79 [StructLayout (LayoutKind.Sequential)]
80 internal sealed class SocketAsyncResult: IAsyncResult
82 /* Same structure in the runtime */
84 Keep this in sync with MonoSocketAsyncResult in
85 metadata/socket-io.h and ProcessAsyncReader
86 in System.Diagnostics/Process.cs.
92 AsyncCallback callback; // used from the runtime
93 WaitHandle waithandle;
95 Exception delayedException;
97 public EndPoint EndPoint; // Connect,ReceiveFrom,SendTo
98 public byte [] Buffer; // Receive,ReceiveFrom,Send,SendTo
99 public int Offset; // Receive,ReceiveFrom,Send,SendTo
100 public int Size; // Receive,ReceiveFrom,Send,SendTo
101 public SocketFlags SockFlags; // Receive,ReceiveFrom,Send,SendTo
102 public Socket AcceptSocket; // AcceptReceive
103 public IPAddress[] Addresses; // Connect
104 public int Port; // Connect
105 public IList<ArraySegment<byte>> Buffers; // Receive, Send
106 public bool ReuseSocket; // Disconnect
114 public bool blocking;
116 public SocketOperation operation;
118 public int EndCalled;
120 // These fields are not in MonoSocketAsyncResult
121 public Worker Worker;
122 public int CurrentAddress; // Connect
124 public SocketAsyncResult ()
128 public void Init (Socket sock, object state, AsyncCallback callback, SocketOperation operation)
132 this.blocking = sock.blocking;
133 this.handle = sock.socket;
135 this.blocking = true;
136 this.handle = IntPtr.Zero;
139 this.callback = callback;
140 GC.KeepAlive (this.callback);
141 this.operation = operation;
142 SockFlags = SocketFlags.None;
143 if (waithandle != null)
144 ((ManualResetEvent) waithandle).Reset ();
146 delayedException = null;
161 completed_sync = false;
170 public void DoMConnectCallback ()
172 if (callback == null)
174 ThreadPool.UnsafeQueueUserWorkItem (_ => { callback (this); }, null);
177 public void Dispose ()
179 Init (null, null, null, 0);
180 if (waithandle != null) {
186 public SocketAsyncResult (Socket sock, object state, AsyncCallback callback, SocketOperation operation)
189 this.blocking = sock.blocking;
190 this.handle = sock.socket;
192 this.callback = callback;
193 GC.KeepAlive (this.callback);
194 this.operation = operation;
195 SockFlags = SocketFlags.None;
196 Worker = new Worker (this);
199 public void CheckIfThrowDelayedException ()
201 if (delayedException != null) {
202 Sock.connected = false;
203 throw delayedException;
207 Sock.connected = false;
208 throw new SocketException (error);
212 void CompleteAllOnDispose (Queue queue)
214 object [] pending = queue.ToArray ();
218 for (int i = 0; i < pending.Length; i++) {
219 Worker worker = (Worker) pending [i];
220 SocketAsyncResult ares = worker.result;
221 cb = new WaitCallback (ares.CompleteDisposed);
222 ThreadPool.UnsafeQueueUserWorkItem (cb, null);
226 void CompleteDisposed (object unused)
231 public void Complete ()
233 if (operation != SocketOperation.Receive && Sock.disposed)
234 delayedException = new ObjectDisposedException (Sock.GetType ().ToString ());
239 if (operation == SocketOperation.Receive ||
240 operation == SocketOperation.ReceiveFrom ||
241 operation == SocketOperation.ReceiveGeneric ||
242 operation == SocketOperation.Accept) {
244 } else if (operation == SocketOperation.Send ||
245 operation == SocketOperation.SendTo ||
246 operation == SocketOperation.SendGeneric) {
252 Worker worker = null;
253 SocketAsyncCall sac = null;
255 // queue.Count will only be 0 if the socket is closed while receive/send/accept
256 // operation(s) are pending and at least one call to this method is
257 // waiting on the lock while another one calls CompleteAllOnDispose()
259 queue.Dequeue (); // remove ourselves
260 if (queue.Count > 0) {
261 worker = (Worker) queue.Peek ();
262 if (!Sock.disposed) {
263 sac = Worker.Dispatcher;
265 CompleteAllOnDispose (queue);
271 Socket.socket_pool_queue (sac, worker.result);
273 // IMPORTANT: 'callback', if any is scheduled from unmanaged code
276 public void Complete (bool synch)
278 completed_sync = synch;
282 public void Complete (int total)
288 public void Complete (Exception e, bool synch)
290 completed_sync = synch;
291 delayedException = e;
295 public void Complete (Exception e)
297 delayedException = e;
301 public void Complete (Socket s)
307 public void Complete (Socket s, int total)
314 public object AsyncState {
320 public WaitHandle AsyncWaitHandle {
323 if (waithandle == null)
324 waithandle = new ManualResetEvent (completed);
334 public bool CompletedSynchronously {
336 return(completed_sync);
340 public bool IsCompleted {
347 if (waithandle != null && value) {
348 ((ManualResetEvent) waithandle).Set ();
354 public Socket Socket {
361 get { return total; }
362 set { total = value; }
365 public SocketError ErrorCode {
367 SocketException ex = delayedException as SocketException;
369 return(ex.SocketErrorCode);
372 return((SocketError)error);
374 return(SocketError.Success);
379 internal sealed class Worker
381 public SocketAsyncResult result;
382 SocketAsyncEventArgs args;
384 public Worker (SocketAsyncEventArgs args)
387 result = new SocketAsyncResult ();
388 result.Worker = this;
391 public Worker (SocketAsyncResult ares)
396 public void Dispose ()
398 if (result != null) {
405 public static SocketAsyncCall Dispatcher = new SocketAsyncCall (DispatcherCB);
407 static void DispatcherCB (SocketAsyncResult sar)
409 SocketOperation op = sar.operation;
410 if (op == Socket.SocketOperation.Receive || op == Socket.SocketOperation.ReceiveGeneric ||
411 op == Socket.SocketOperation.RecvJustCallback)
412 sar.Worker.Receive ();
413 else if (op == Socket.SocketOperation.Send || op == Socket.SocketOperation.SendGeneric ||
414 op == Socket.SocketOperation.SendJustCallback)
416 else if (op == Socket.SocketOperation.ReceiveFrom)
417 sar.Worker.ReceiveFrom ();
418 else if (op == Socket.SocketOperation.SendTo)
419 sar.Worker.SendTo ();
420 else if (op == Socket.SocketOperation.Connect)
421 sar.Worker.Connect ();
422 else if (op == Socket.SocketOperation.Accept)
423 sar.Worker.Accept ();
424 else if (op == Socket.SocketOperation.AcceptReceive)
425 sar.Worker.AcceptReceive ();
426 else if (op == Socket.SocketOperation.Disconnect)
427 sar.Worker.Disconnect ();
429 // SendPackets and ReceiveMessageFrom are not implemented yet
431 else if (op == Socket.SocketOperation.ReceiveMessageFrom)
432 async_op = SocketAsyncOperation.ReceiveMessageFrom;
433 else if (op == Socket.SocketOperation.SendPackets)
434 async_op = SocketAsyncOperation.SendPackets;
437 throw new NotImplementedException (String.Format ("Operation {0} is not implemented", op));
440 /* This is called when reusing a SocketAsyncEventArgs */
441 public void Init (Socket sock, SocketAsyncEventArgs args, SocketOperation op)
443 result.Init (sock, args, SocketAsyncEventArgs.Dispatcher, op);
444 result.Worker = this;
445 SocketAsyncOperation async_op;
448 // -SocketOperation.AcceptReceive not used in SocketAsyncEventArgs
449 // -SendPackets and ReceiveMessageFrom are not implemented yet
450 if (op == Socket.SocketOperation.Connect)
451 async_op = SocketAsyncOperation.Connect;
452 else if (op == Socket.SocketOperation.Accept)
453 async_op = SocketAsyncOperation.Accept;
454 else if (op == Socket.SocketOperation.Disconnect)
455 async_op = SocketAsyncOperation.Disconnect;
456 else if (op == Socket.SocketOperation.Receive || op == Socket.SocketOperation.ReceiveGeneric)
457 async_op = SocketAsyncOperation.Receive;
458 else if (op == Socket.SocketOperation.ReceiveFrom)
459 async_op = SocketAsyncOperation.ReceiveFrom;
461 else if (op == Socket.SocketOperation.ReceiveMessageFrom)
462 async_op = SocketAsyncOperation.ReceiveMessageFrom;
464 else if (op == Socket.SocketOperation.Send || op == Socket.SocketOperation.SendGeneric)
465 async_op = SocketAsyncOperation.Send;
467 else if (op == Socket.SocketOperation.SendPackets)
468 async_op = SocketAsyncOperation.SendPackets;
470 else if (op == Socket.SocketOperation.SendTo)
471 async_op = SocketAsyncOperation.SendTo;
473 throw new NotImplementedException (String.Format ("Operation {0} is not implemented", op));
475 args.SetLastOperation (async_op);
476 args.SocketError = SocketError.Success;
477 args.BytesTransferred = 0;
480 public void Accept ()
482 Socket acc_socket = null;
484 if (args != null && args.AcceptSocket != null) {
485 result.Sock.Accept (args.AcceptSocket);
486 acc_socket = args.AcceptSocket;
488 acc_socket = result.Sock.Accept ();
490 args.AcceptSocket = acc_socket;
492 } catch (Exception e) {
497 result.Complete (acc_socket);
500 /* only used in 2.0 profile and newer, but
501 * leave in older profiles to keep interface
502 * to runtime consistent
504 public void AcceptReceive ()
506 Socket acc_socket = null;
508 if (result.AcceptSocket == null) {
509 acc_socket = result.Sock.Accept ();
511 acc_socket = result.AcceptSocket;
512 result.Sock.Accept (acc_socket);
514 } catch (Exception e) {
519 /* It seems the MS runtime
520 * special-cases 0-length requested
521 * receive data. See bug 464201.
524 if (result.Size > 0) {
527 total = acc_socket.Receive_nochecks (result.Buffer,
533 result.Complete (new SocketException ((int) error));
536 } catch (Exception e) {
542 result.Complete (acc_socket, total);
545 public void Connect ()
547 if (result.EndPoint == null) {
548 result.Complete (new SocketException ((int)SocketError.AddressNotAvailable));
552 SocketAsyncResult mconnect = result.AsyncState as SocketAsyncResult;
553 bool is_mconnect = (mconnect != null && mconnect.Addresses != null);
556 EndPoint ep = result.EndPoint;
557 error_code = (int) result.Sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
558 if (error_code == 0) {
561 result.Sock.seed_endpoint = ep;
562 result.Sock.connected = true;
563 result.Sock.isbound = true;
564 result.Sock.connect_in_progress = false;
568 result.DoMConnectCallback ();
573 result.Sock.connect_in_progress = false;
574 result.Complete (new SocketException (error_code));
578 if (mconnect.CurrentAddress >= mconnect.Addresses.Length) {
579 mconnect.Complete (new SocketException (error_code));
581 mconnect.DoMConnectCallback ();
584 mconnect.Sock.BeginMConnect (mconnect);
585 } catch (Exception e) {
586 result.Sock.connect_in_progress = false;
591 result.DoMConnectCallback ();
596 /* Also only used in 2.0 profile and newer */
597 public void Disconnect ()
601 result.ReuseSocket = args.DisconnectReuseSocket;
602 result.Sock.Disconnect (result.ReuseSocket);
603 } catch (Exception e) {
610 public void Receive ()
612 if (result.operation == SocketOperation.ReceiveGeneric) {
616 // Actual recv() done in the runtime
620 public void ReceiveFrom ()
624 total = result.Sock.ReceiveFrom_nochecks (result.Buffer,
628 ref result.EndPoint);
629 } catch (Exception e) {
634 result.Complete (total);
637 public void ReceiveGeneric ()
641 total = result.Sock.Receive (result.Buffers, result.SockFlags);
642 } catch (Exception e) {
646 result.Complete (total);
651 void UpdateSendValues (int last_sent)
653 if (result.error == 0) {
654 send_so_far += last_sent;
655 result.Offset += last_sent;
656 result.Size -= last_sent;
662 if (result.operation == SocketOperation.SendGeneric) {
666 // Actual send() done in the runtime
667 if (result.error == 0) {
668 UpdateSendValues (result.Total);
669 if (result.Sock.disposed) {
674 if (result.Size > 0) {
675 Socket.socket_pool_queue (Worker.Dispatcher, result);
676 return; // Have to finish writing everything. See bug #74475.
678 result.Total = send_so_far;
684 public void SendTo ()
688 total = result.Sock.SendTo_nochecks (result.Buffer,
694 UpdateSendValues (total);
695 if (result.Size > 0) {
696 Socket.socket_pool_queue (Worker.Dispatcher, result);
697 return; // Have to finish writing everything. See bug #74475.
699 result.Total = send_so_far;
701 } catch (Exception e) {
710 public void SendGeneric ()
714 total = result.Sock.Send (result.Buffers, result.SockFlags);
715 } catch (Exception e) {
719 result.Complete (total);
723 private Queue readQ = new Queue (2);
724 private Queue writeQ = new Queue (2);
726 internal delegate void SocketAsyncCall (SocketAsyncResult sar);
729 * These two fields are looked up by name by the runtime, don't change
730 * their name without also updating the runtime code.
732 private static int ipv4Supported = -1, ipv6Supported = -1;
737 // initialize ipv4Supported and ipv6Supported
738 CheckProtocolSupport ();
741 internal static void CheckProtocolSupport ()
743 if(ipv4Supported == -1) {
745 Socket tmp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
754 if (ipv6Supported == -1) {
755 // We need to put a try/catch around ConfigurationManager methods as will always throw an exception
756 // when run in a mono embedded application. This occurs as embedded applications do not have a setup
757 // for application config. The exception is not thrown when called from a normal .NET application.
759 // We, then, need to guard calls to the ConfigurationManager. If the config is not found or throws an
760 // exception, will fall through to the existing Socket / API directly below in the code.
762 // Also note that catching ConfigurationErrorsException specifically would require library dependency
763 // System.Configuration, and wanted to avoid that.
765 #if CONFIGURATION_DEP
767 SettingsSection config;
768 config = (SettingsSection) System.Configuration.ConfigurationManager.GetSection ("system.net/settings");
770 ipv6Supported = config.Ipv6.Enabled ? -1 : 0;
776 NetConfig config = System.Configuration.ConfigurationSettings.GetConfig("system.net/settings") as NetConfig;
778 ipv6Supported = config.ipv6Enabled ? -1 : 0;
784 if (ipv6Supported != 0) {
786 Socket tmp = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
797 public static bool SupportsIPv4 {
799 CheckProtocolSupport();
800 return ipv4Supported == 1;
804 [ObsoleteAttribute ("Use OSSupportsIPv6 instead")]
805 public static bool SupportsIPv6 {
807 CheckProtocolSupport();
808 return ipv6Supported == 1;
812 public static bool OSSupportsIPv4 {
814 CheckProtocolSupport();
815 return ipv4Supported == 1;
820 public static bool OSSupportsIPv6 {
822 CheckProtocolSupport();
823 return ipv6Supported == 1;
827 public static bool OSSupportsIPv6 {
829 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces ();
831 foreach (NetworkInterface adapter in nics) {
832 if (adapter.Supports (NetworkInterfaceComponent.IPv6))
840 /* the field "socket" is looked up by name by the runtime */
841 private IntPtr socket;
842 private AddressFamily address_family;
843 private SocketType socket_type;
844 private ProtocolType protocol_type;
845 internal bool blocking=true;
846 List<Thread> blocking_threads;
847 private bool isbound;
848 /* When true, the socket was connected at the time of
849 * the last IO operation
851 private bool connected;
852 /* true if we called Close_internal */
854 internal bool disposed;
855 bool connect_in_progress;
858 * This EndPoint is used when creating new endpoints. Because
859 * there are many types of EndPoints possible,
860 * seed_endpoint.Create(addr) is used for creating new ones.
861 * As such, this value is set on Bind, SentTo, ReceiveFrom,
864 internal EndPoint seed_endpoint = null;
866 void RegisterForBlockingSyscall ()
868 while (blocking_threads == null) {
869 //In the rare event this CAS fail, there's a good chance other thread won, so we're kosher.
870 //In the VERY rare event of all CAS fail together, we pay the full price of of failure.
871 Interlocked.CompareExchange (ref blocking_threads, new List<Thread> (), null);
877 /* We must use a finally block here to make this atomic. */
878 lock (blocking_threads) {
879 blocking_threads.Add (Thread.CurrentThread);
884 /* This must be called from a finally block! */
885 void UnRegisterForBlockingSyscall ()
887 //If this NRE, we're in deep problems because Register Must have
888 lock (blocking_threads) {
889 blocking_threads.Remove (Thread.CurrentThread);
893 void AbortRegisteredThreads () {
894 if (blocking_threads == null)
897 lock (blocking_threads) {
898 foreach (var t in blocking_threads)
899 cancel_blocking_socket_operation (t);
900 blocking_threads.Clear ();
904 // Creates a new system socket, returning the handle
905 [MethodImplAttribute(MethodImplOptions.InternalCall)]
906 private extern IntPtr Socket_internal(AddressFamily family,
911 public Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
913 #if NET_2_1 && !MOBILE
914 switch (addressFamily) {
915 case AddressFamily.InterNetwork: // ok
916 case AddressFamily.InterNetworkV6: // ok
917 case AddressFamily.Unknown: // SocketException will be thrown later (with right error #)
919 // case AddressFamily.Unspecified:
921 throw new ArgumentException ("addressFamily");
924 switch (socketType) {
925 case SocketType.Stream: // ok
926 case SocketType.Unknown: // SocketException will be thrown later (with right error #)
929 throw new ArgumentException ("socketType");
932 switch (protocolType) {
933 case ProtocolType.Tcp: // ok
934 case ProtocolType.Unspecified: // ok
935 case ProtocolType.Unknown: // SocketException will be thrown later (with right error #)
938 throw new ArgumentException ("protocolType");
941 address_family = addressFamily;
942 socket_type = socketType;
943 protocol_type = protocolType;
947 socket = Socket_internal (addressFamily, socketType, protocolType, out error);
949 throw new SocketException (error);
950 #if !NET_2_1 || MOBILE
961 public AddressFamily AddressFamily {
962 get { return address_family; }
965 [MethodImplAttribute(MethodImplOptions.InternalCall)]
966 private extern static void Blocking_internal(IntPtr socket,
970 public bool Blocking {
975 if (disposed && closed)
976 throw new ObjectDisposedException (GetType ().ToString ());
980 Blocking_internal (socket, value, out error);
983 throw new SocketException (error);
989 public bool Connected {
990 get { return connected; }
991 internal set { connected = value; }
994 public ProtocolType ProtocolType {
995 get { return protocol_type; }
998 public bool NoDelay {
1000 if (disposed && closed)
1001 throw new ObjectDisposedException (GetType ().ToString ());
1005 return (int)(GetSocketOption (
1006 SocketOptionLevel.Tcp,
1007 SocketOptionName.NoDelay)) != 0;
1011 if (disposed && closed)
1012 throw new ObjectDisposedException (GetType ().ToString ());
1017 SocketOptionLevel.Tcp,
1018 SocketOptionName.NoDelay, value ? 1 : 0);
1022 public int ReceiveBufferSize {
1024 if (disposed && closed) {
1025 throw new ObjectDisposedException (GetType ().ToString ());
1027 return((int)GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer));
1030 if (disposed && closed) {
1031 throw new ObjectDisposedException (GetType ().ToString ());
1034 throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
1037 SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, value);
1041 public int SendBufferSize {
1043 if (disposed && closed) {
1044 throw new ObjectDisposedException (GetType ().ToString ());
1046 return((int)GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendBuffer));
1049 if (disposed && closed) {
1050 throw new ObjectDisposedException (GetType ().ToString ());
1053 throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
1056 SetSocketOption (SocketOptionLevel.Socket,
1057 SocketOptionName.SendBuffer,
1064 if (disposed && closed) {
1065 throw new ObjectDisposedException (GetType ().ToString ());
1070 if (address_family == AddressFamily.InterNetwork) {
1071 ttl_val = (short)((int)GetSocketOption (SocketOptionLevel.IP, SocketOptionName.IpTimeToLive));
1072 } else if (address_family == AddressFamily.InterNetworkV6) {
1073 ttl_val = (short)((int)GetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.HopLimit));
1075 throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
1081 if (disposed && closed) {
1082 throw new ObjectDisposedException (GetType ().ToString ());
1085 throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
1088 if (address_family == AddressFamily.InterNetwork) {
1089 SetSocketOption (SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, value);
1090 } else if (address_family == AddressFamily.InterNetworkV6) {
1091 SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.HopLimit, value);
1093 throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
1098 // Returns the remote endpoint details in addr and port
1099 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1100 private extern static SocketAddress RemoteEndPoint_internal(IntPtr socket, int family, out int error);
1102 public EndPoint RemoteEndPoint {
1104 if (disposed && closed)
1105 throw new ObjectDisposedException (GetType ().ToString ());
1108 * If the seed EndPoint is null, Connect, Bind,
1109 * etc has not yet been called. MS returns null
1112 if (!connected || seed_endpoint == null)
1117 sa=RemoteEndPoint_internal(socket, (int) address_family, out error);
1120 throw new SocketException (error);
1122 return seed_endpoint.Create (sa);
1126 void Linger (IntPtr handle)
1128 if (!connected || linger_timeout <= 0)
1131 // We don't want to receive any more data
1133 Shutdown_internal (handle, SocketShutdown.Receive, out error);
1137 int seconds = linger_timeout / 1000;
1138 int ms = linger_timeout % 1000;
1140 // If the other end closes, this will return 'true' with 'Available' == 0
1141 Poll_internal (handle, SelectMode.SelectRead, ms * 1000, out error);
1147 LingerOption linger = new LingerOption (true, seconds);
1148 SetSocketOption_internal (handle, SocketOptionLevel.Socket, SocketOptionName.Linger, linger, null, 0, out error);
1149 /* Not needed, we're closing upon return */
1154 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1155 static extern void cancel_blocking_socket_operation (Thread thread);
1157 protected virtual void Dispose (bool disposing)
1163 bool was_connected = connected;
1165 if ((int) socket != -1) {
1169 socket = (IntPtr) (-1);
1171 AbortRegisteredThreads ();
1175 //DateTime start = DateTime.UtcNow;
1176 Close_internal (x, out error);
1177 //Console.WriteLine ("Time spent in Close_internal: {0}ms", (DateTime.UtcNow - start).TotalMilliseconds);
1179 throw new SocketException (error);
1184 public void Dispose ()
1186 void IDisposable.Dispose ()
1190 GC.SuppressFinalize (this);
1193 // Closes the socket
1194 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1195 private extern static void Close_internal(IntPtr socket, out int error);
1197 public void Close ()
1200 ((IDisposable) this).Dispose ();
1203 public void Close (int timeout)
1205 linger_timeout = timeout;
1206 ((IDisposable) this).Dispose ();
1209 // Connects to the remote address
1210 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1211 private extern static void Connect_internal(IntPtr sock,
1215 public void Connect (EndPoint remoteEP)
1217 SocketAddress serial = null;
1219 if (disposed && closed)
1220 throw new ObjectDisposedException (GetType ().ToString ());
1222 if (remoteEP == null)
1223 throw new ArgumentNullException ("remoteEP");
1225 IPEndPoint ep = remoteEP as IPEndPoint;
1226 if (ep != null && socket_type != SocketType.Dgram) /* Dgram uses Any to 'disconnect' */
1227 if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any))
1228 throw new SocketException ((int) SocketError.AddressNotAvailable);
1231 throw new InvalidOperationException ();
1232 serial = remoteEP.Serialize ();
1237 RegisterForBlockingSyscall ();
1238 Connect_internal (socket, serial, out error);
1240 UnRegisterForBlockingSyscall ();
1243 if (error == 0 || error == 10035)
1244 seed_endpoint = remoteEP; // Keep the ep around for non-blocking sockets
1248 error = SOCKET_CLOSED;
1249 throw new SocketException (error);
1252 if (socket_type == SocketType.Dgram && ep != null && (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)))
1259 public bool ReceiveAsync (SocketAsyncEventArgs e)
1261 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
1262 if (disposed && closed)
1263 throw new ObjectDisposedException (GetType ().ToString ());
1265 // LAME SPEC: the ArgumentException is never thrown, instead an NRE is
1266 // thrown when e.Buffer and e.BufferList are null (works fine when one is
1267 // set to a valid object)
1268 if (e.Buffer == null && e.BufferList == null)
1269 throw new NullReferenceException ("Either e.Buffer or e.BufferList must be valid buffers.");
1272 SocketOperation op = (e.Buffer != null) ? SocketOperation.Receive : SocketOperation.ReceiveGeneric;
1273 e.Worker.Init (this, e, op);
1274 SocketAsyncResult res = e.Worker.result;
1275 if (e.Buffer != null) {
1276 res.Buffer = e.Buffer;
1277 res.Offset = e.Offset;
1280 res.Buffers = e.BufferList;
1282 res.SockFlags = e.SocketFlags;
1285 readQ.Enqueue (e.Worker);
1286 count = readQ.Count;
1289 // Receive takes care of ReceiveGeneric
1290 socket_pool_queue (Worker.Dispatcher, res);
1296 public bool SendAsync (SocketAsyncEventArgs e)
1298 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
1299 if (disposed && closed)
1300 throw new ObjectDisposedException (GetType ().ToString ());
1301 if (e.Buffer == null && e.BufferList == null)
1302 throw new NullReferenceException ("Either e.Buffer or e.BufferList must be valid buffers.");
1305 SocketOperation op = (e.Buffer != null) ? SocketOperation.Send : SocketOperation.SendGeneric;
1306 e.Worker.Init (this, e, op);
1307 SocketAsyncResult res = e.Worker.result;
1308 if (e.Buffer != null) {
1309 res.Buffer = e.Buffer;
1310 res.Offset = e.Offset;
1313 res.Buffers = e.BufferList;
1315 res.SockFlags = e.SocketFlags;
1318 writeQ.Enqueue (e.Worker);
1319 count = writeQ.Count;
1322 // Send takes care of SendGeneric
1323 socket_pool_queue (Worker.Dispatcher, res);
1328 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1329 extern static bool Poll_internal (IntPtr socket, SelectMode mode, int timeout, out int error);
1331 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1332 private extern static int Receive_internal(IntPtr sock,
1339 internal int Receive_nochecks (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error)
1342 int ret = Receive_internal (socket, buf, offset, size, flags, out nativeError);
1343 error = (SocketError) nativeError;
1344 if (error != SocketError.Success && error != SocketError.WouldBlock && error != SocketError.InProgress) {
1354 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1355 private extern static void GetSocketOption_obj_internal(IntPtr socket,
1356 SocketOptionLevel level, SocketOptionName name, out object obj_val,
1359 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1360 private extern static int Send_internal(IntPtr sock,
1361 byte[] buf, int offset,
1366 internal int Send_nochecks (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error)
1369 error = SocketError.Success;
1375 int ret = Send_internal (socket, buf, offset, size, flags, out nativeError);
1377 error = (SocketError)nativeError;
1379 if (error != SocketError.Success && error != SocketError.WouldBlock && error != SocketError.InProgress) {
1389 public object GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName)
1391 if (disposed && closed)
1392 throw new ObjectDisposedException (GetType ().ToString ());
1397 GetSocketOption_obj_internal (socket, optionLevel, optionName, out obj_val,
1400 throw new SocketException (error);
1402 if (optionName == SocketOptionName.Linger) {
1403 return((LingerOption)obj_val);
1404 } else if (optionName == SocketOptionName.AddMembership ||
1405 optionName == SocketOptionName.DropMembership) {
1406 return((MulticastOption)obj_val);
1407 } else if (obj_val is int) {
1408 return((int)obj_val);
1414 [MethodImplAttribute (MethodImplOptions.InternalCall)]
1415 private extern static void Shutdown_internal (IntPtr socket, SocketShutdown how, out int error);
1417 public void Shutdown (SocketShutdown how)
1419 if (disposed && closed)
1420 throw new ObjectDisposedException (GetType ().ToString ());
1423 throw new SocketException (10057); // Not connected
1427 Shutdown_internal (socket, how, out error);
1429 throw new SocketException (error);
1432 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1433 private extern static void SetSocketOption_internal (IntPtr socket, SocketOptionLevel level,
1434 SocketOptionName name, object obj_val,
1435 byte [] byte_val, int int_val,
1438 public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
1440 if (disposed && closed)
1441 throw new ObjectDisposedException (GetType ().ToString ());
1445 SetSocketOption_internal (socket, optionLevel, optionName, null,
1446 null, optionValue, out error);
1449 throw new SocketException (error);
1452 private void ThrowIfUpd ()
1454 #if !NET_2_1 || MOBILE
1455 if (protocol_type == ProtocolType.Udp)
1456 throw new SocketException ((int)SocketError.ProtocolOption);
1461 IAsyncResult BeginConnect(EndPoint end_point, AsyncCallback callback, object state)
1463 if (disposed && closed)
1464 throw new ObjectDisposedException (GetType ().ToString ());
1466 if (end_point == null)
1467 throw new ArgumentNullException ("end_point");
1469 SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Connect);
1470 req.EndPoint = end_point;
1472 // Bug #75154: Connect() should not succeed for .Any addresses.
1473 if (end_point is IPEndPoint) {
1474 IPEndPoint ep = (IPEndPoint) end_point;
1475 if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)) {
1476 req.Complete (new SocketException ((int) SocketError.AddressNotAvailable), true);
1482 if (connect_in_progress) {
1483 // This could happen when multiple IPs are used
1484 // Calling connect() again will reset the connection attempt and cause
1485 // an error. Better to just close the socket and move on.
1486 connect_in_progress = false;
1487 Close_internal (socket, out error);
1488 socket = Socket_internal (address_family, socket_type, protocol_type, out error);
1490 throw new SocketException (error);
1492 bool blk = blocking;
1495 SocketAddress serial = end_point.Serialize ();
1496 Connect_internal (socket, serial, out error);
1503 req.Complete (true);
1507 if (error != (int) SocketError.InProgress && error != (int) SocketError.WouldBlock) {
1511 req.Complete (new SocketException (error), true);
1518 connect_in_progress = true;
1519 socket_pool_queue (Worker.Dispatcher, req);
1524 IAsyncResult BeginConnect (IPAddress[] addresses, int port, AsyncCallback callback, object state)
1527 if (disposed && closed)
1528 throw new ObjectDisposedException (GetType ().ToString ());
1530 if (addresses == null)
1531 throw new ArgumentNullException ("addresses");
1533 if (addresses.Length == 0)
1534 throw new ArgumentException ("Empty addresses list");
1536 if (this.AddressFamily != AddressFamily.InterNetwork &&
1537 this.AddressFamily != AddressFamily.InterNetworkV6)
1538 throw new NotSupportedException ("This method is only valid for addresses in the InterNetwork or InterNetworkV6 families");
1540 if (port <= 0 || port > 65535)
1541 throw new ArgumentOutOfRangeException ("port", "Must be > 0 and < 65536");
1543 throw new InvalidOperationException ();
1545 SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Connect);
1546 req.Addresses = addresses;
1549 return BeginMConnect (req);
1552 IAsyncResult BeginMConnect (SocketAsyncResult req)
1554 IAsyncResult ares = null;
1555 Exception exc = null;
1556 for (int i = req.CurrentAddress; i < req.Addresses.Length; i++) {
1557 IPAddress addr = req.Addresses [i];
1558 IPEndPoint ep = new IPEndPoint (addr, req.Port);
1560 req.CurrentAddress++;
1561 ares = BeginConnect (ep, null, req);
1562 if (ares.IsCompleted && ares.CompletedSynchronously) {
1563 ((SocketAsyncResult) ares).CheckIfThrowDelayedException ();
1564 req.DoMConnectCallback ();
1567 } catch (Exception e) {
1579 // Returns false when it is ok to use RemoteEndPoint
1580 // true when addresses must be used (and addresses could be null/empty)
1581 bool GetCheckedIPs (SocketAsyncEventArgs e, out IPAddress [] addresses)
1585 // Connect to the first address that match the host name, like:
1586 // http://blogs.msdn.com/ncl/archive/2009/07/20/new-ncl-features-in-net-4-0-beta-2.aspx
1587 // while skipping entries that do not match the address family
1588 DnsEndPoint dep = (e.RemoteEndPoint as DnsEndPoint);
1590 addresses = Dns.GetHostAddresses (dep.Host);
1593 e.ConnectByNameError = null;
1597 return false; // < NET_4_0 -> use remote endpoint
1601 bool ConnectAsyncReal (SocketAsyncEventArgs e)
1603 bool use_remoteep = true;
1605 IPAddress [] addresses = null;
1606 use_remoteep = !GetCheckedIPs (e, out addresses);
1609 Worker w = e.Worker;
1610 w.Init (this, e, SocketOperation.Connect);
1611 SocketAsyncResult result = w.result;
1612 IAsyncResult ares = null;
1615 result.EndPoint = e.RemoteEndPoint;
1616 ares = BeginConnect (e.RemoteEndPoint, SocketAsyncEventArgs.Dispatcher, e);
1621 DnsEndPoint dep = (e.RemoteEndPoint as DnsEndPoint);
1622 result.Addresses = addresses;
1623 result.Port = dep.Port;
1625 ares = BeginConnect (addresses, dep.Port, SocketAsyncEventArgs.Dispatcher, e);
1628 if (ares.IsCompleted && ares.CompletedSynchronously) {
1629 ((SocketAsyncResult) ares).CheckIfThrowDelayedException ();
1632 } catch (Exception exc) {
1633 result.Complete (exc, true);
1639 public bool ConnectAsync (SocketAsyncEventArgs e)
1641 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
1642 if (disposed && closed)
1643 throw new ObjectDisposedException (GetType ().ToString ());
1645 throw new InvalidOperationException ("You may not perform this operation after calling the Listen method.");
1646 if (e.RemoteEndPoint == null)
1647 throw new ArgumentNullException ("remoteEP");
1649 return ConnectAsyncReal (e);
1652 [MethodImplAttribute (MethodImplOptions.InternalCall)]
1653 private extern static int Receive_internal (IntPtr sock, WSABUF[] bufarray, SocketFlags flags, out int error);
1655 int Receive (IList<ArraySegment<byte>> buffers)
1659 ret = Receive (buffers, SocketFlags.None, out error);
1660 if (error != SocketError.Success) {
1661 throw new SocketException ((int)error);
1666 [CLSCompliant (false)]
1668 int Receive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags)
1672 ret = Receive (buffers, socketFlags, out error);
1673 if (error != SocketError.Success) {
1674 throw new SocketException ((int)error);
1679 [CLSCompliant (false)]
1681 int Receive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
1683 if (disposed && closed)
1684 throw new ObjectDisposedException (GetType ().ToString ());
1686 if (buffers == null ||
1687 buffers.Count == 0) {
1688 throw new ArgumentNullException ("buffers");
1691 int numsegments = buffers.Count;
1695 /* Only example I can find of sending a byte
1696 * array reference directly into an internal
1698 * System.Runtime.Remoting/System.Runtime.Remoting.Channels.Ipc.Win32/NamedPipeSocket.cs,
1699 * so taking a lead from that...
1701 WSABUF[] bufarray = new WSABUF[numsegments];
1702 GCHandle[] gch = new GCHandle[numsegments];
1704 for(int i = 0; i < numsegments; i++) {
1705 ArraySegment<byte> segment = buffers[i];
1707 if (segment.Offset < 0 || segment.Count < 0 ||
1708 segment.Count > segment.Array.Length - segment.Offset)
1709 throw new ArgumentOutOfRangeException ("segment");
1711 gch[i] = GCHandle.Alloc (segment.Array, GCHandleType.Pinned);
1712 bufarray[i].len = segment.Count;
1713 bufarray[i].buf = Marshal.UnsafeAddrOfPinnedArrayElement (segment.Array, segment.Offset);
1717 ret = Receive_internal (socket, bufarray,
1721 for(int i = 0; i < numsegments; i++) {
1722 if (gch[i].IsAllocated) {
1728 errorCode = (SocketError)nativeError;
1732 [MethodImplAttribute (MethodImplOptions.InternalCall)]
1733 private extern static int Send_internal (IntPtr sock, WSABUF[] bufarray, SocketFlags flags, out int error);
1735 int Send (IList<ArraySegment<byte>> buffers)
1739 ret = Send (buffers, SocketFlags.None, out error);
1740 if (error != SocketError.Success) {
1741 throw new SocketException ((int)error);
1747 int Send (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags)
1751 ret = Send (buffers, socketFlags, out error);
1752 if (error != SocketError.Success) {
1753 throw new SocketException ((int)error);
1758 [CLSCompliant (false)]
1760 int Send (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
1762 if (disposed && closed)
1763 throw new ObjectDisposedException (GetType ().ToString ());
1764 if (buffers == null)
1765 throw new ArgumentNullException ("buffers");
1766 if (buffers.Count == 0)
1767 throw new ArgumentException ("Buffer is empty", "buffers");
1768 int numsegments = buffers.Count;
1772 WSABUF[] bufarray = new WSABUF[numsegments];
1773 GCHandle[] gch = new GCHandle[numsegments];
1774 for(int i = 0; i < numsegments; i++) {
1775 ArraySegment<byte> segment = buffers[i];
1777 if (segment.Offset < 0 || segment.Count < 0 ||
1778 segment.Count > segment.Array.Length - segment.Offset)
1779 throw new ArgumentOutOfRangeException ("segment");
1781 gch[i] = GCHandle.Alloc (segment.Array, GCHandleType.Pinned);
1782 bufarray[i].len = segment.Count;
1783 bufarray[i].buf = Marshal.UnsafeAddrOfPinnedArrayElement (segment.Array, segment.Offset);
1787 ret = Send_internal (socket, bufarray, socketFlags, out nativeError);
1789 for(int i = 0; i < numsegments; i++) {
1790 if (gch[i].IsAllocated) {
1795 errorCode = (SocketError)nativeError;
1799 Exception InvalidAsyncOp (string method)
1801 return new InvalidOperationException (method + " can only be called once per asynchronous operation");
1805 int EndReceive (IAsyncResult result)
1808 int bytesReceived = EndReceive (result, out error);
1809 if (error != SocketError.Success) {
1810 if (error != SocketError.WouldBlock && error != SocketError.InProgress)
1812 throw new SocketException ((int)error);
1814 return bytesReceived;
1818 int EndReceive (IAsyncResult asyncResult, out SocketError errorCode)
1820 if (disposed && closed)
1821 throw new ObjectDisposedException (GetType ().ToString ());
1823 if (asyncResult == null)
1824 throw new ArgumentNullException ("asyncResult");
1826 SocketAsyncResult req = asyncResult as SocketAsyncResult;
1828 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
1830 if (Interlocked.CompareExchange (ref req.EndCalled, 1, 0) == 1)
1831 throw InvalidAsyncOp ("EndReceive");
1832 if (!asyncResult.IsCompleted)
1833 asyncResult.AsyncWaitHandle.WaitOne ();
1835 errorCode = req.ErrorCode;
1836 // If no socket error occurred, call CheckIfThrowDelayedException in case there are other
1837 // kinds of exceptions that should be thrown.
1838 if (errorCode == SocketError.Success)
1839 req.CheckIfThrowDelayedException();
1845 int EndSend (IAsyncResult result)
1848 int bytesSent = EndSend (result, out error);
1849 if (error != SocketError.Success) {
1850 if (error != SocketError.WouldBlock && error != SocketError.InProgress)
1852 throw new SocketException ((int)error);
1858 int EndSend (IAsyncResult asyncResult, out SocketError errorCode)
1860 if (disposed && closed)
1861 throw new ObjectDisposedException (GetType ().ToString ());
1862 if (asyncResult == null)
1863 throw new ArgumentNullException ("asyncResult");
1865 SocketAsyncResult req = asyncResult as SocketAsyncResult;
1867 throw new ArgumentException ("Invalid IAsyncResult", "result");
1869 if (Interlocked.CompareExchange (ref req.EndCalled, 1, 0) == 1)
1870 throw InvalidAsyncOp ("EndSend");
1871 if (!asyncResult.IsCompleted)
1872 asyncResult.AsyncWaitHandle.WaitOne ();
1874 errorCode = req.ErrorCode;
1875 // If no socket error occurred, call CheckIfThrowDelayedException in case there are other
1876 // kinds of exceptions that should be thrown.
1877 if (errorCode == SocketError.Success)
1878 req.CheckIfThrowDelayedException ();
1883 // Used by Udpclient
1885 int EndReceiveFrom(IAsyncResult result, ref EndPoint end_point)
1887 if (disposed && closed)
1888 throw new ObjectDisposedException (GetType ().ToString ());
1891 throw new ArgumentNullException ("result");
1893 if (end_point == null)
1894 throw new ArgumentNullException ("remote_end");
1896 SocketAsyncResult req = result as SocketAsyncResult;
1898 throw new ArgumentException ("Invalid IAsyncResult", "result");
1900 if (Interlocked.CompareExchange (ref req.EndCalled, 1, 0) == 1)
1901 throw InvalidAsyncOp ("EndReceiveFrom");
1902 if (!result.IsCompleted)
1903 result.AsyncWaitHandle.WaitOne();
1905 req.CheckIfThrowDelayedException();
1906 end_point = req.EndPoint;
1910 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1911 static extern void socket_pool_queue (SocketAsyncCall d, SocketAsyncResult r);