| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755 |
- // System.Net.Sockets.Socket.cs
- //
- // Authors:
- // Phillip Pearson ([email protected])
- // Dick Porter <[email protected]>
- // Gonzalo Paniagua Javier ([email protected])
- // Sridhar Kulkarni ([email protected])
- // Brian Nickel ([email protected])
- //
- // Copyright (C) 2001, 2002 Phillip Pearson and Ximian, Inc.
- // http://www.myelin.co.nz
- // (c) 2004-2006 Novell, Inc. (http://www.novell.com)
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Net;
- using System.Collections;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Threading;
- using System.Reflection;
- using System.IO;
- using System.Net.Configuration;
- using System.Security;
- using System.Text;
- #if NET_2_0
- using System.Collections.Generic;
- using System.Net.NetworkInformation;
- #if !NET_2_1
- using System.Timers;
- #endif
- #endif
- namespace System.Net.Sockets {
- public partial class Socket : IDisposable {
- /*
- * These two fields are looked up by name by the runtime, don't change
- * their name without also updating the runtime code.
- */
- private static int ipv4Supported = -1, ipv6Supported = -1;
- static Socket ()
- {
- // initialize ipv4Supported and ipv6Supported
- CheckProtocolSupport ();
- }
- internal static void CheckProtocolSupport ()
- {
- if(ipv4Supported == -1) {
- try {
- Socket tmp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- tmp.Close();
- ipv4Supported = 1;
- } catch {
- ipv4Supported = 0;
- }
- }
- if (ipv6Supported == -1) {
- #if !NET_2_1
- #if NET_2_0 && CONFIGURATION_DEP
- SettingsSection config;
- config = (SettingsSection) System.Configuration.ConfigurationManager.GetSection ("system.net/settings");
- if (config != null)
- ipv6Supported = config.Ipv6.Enabled ? -1 : 0;
- #else
- NetConfig config = System.Configuration.ConfigurationSettings.GetConfig("system.net/settings") as NetConfig;
- if (config != null)
- ipv6Supported = config.ipv6Enabled ? -1 : 0;
- #endif
- #endif
- if (ipv6Supported != 0) {
- try {
- Socket tmp = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- tmp.Close();
- ipv6Supported = 1;
- } catch {
- ipv6Supported = 0;
- }
- }
- }
- }
- public static bool SupportsIPv4 {
- get {
- CheckProtocolSupport();
- return ipv4Supported == 1;
- }
- }
- #if NET_2_0
- [ObsoleteAttribute ("Use OSSupportsIPv6 instead")]
- #endif
- public static bool SupportsIPv6 {
- get {
- CheckProtocolSupport();
- return ipv6Supported == 1;
- }
- }
- #if NET_2_1
- public static bool OSSupportsIPv4 {
- get {
- CheckProtocolSupport();
- return ipv4Supported == 1;
- }
- }
- #endif
- #if NET_2_1
- public static bool OSSupportsIPv6 {
- get {
- CheckProtocolSupport();
- return ipv6Supported == 1;
- }
- }
- #elif NET_2_0
- public static bool OSSupportsIPv6 {
- get {
- NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces ();
-
- foreach (NetworkInterface adapter in nics) {
- if (adapter.Supports (NetworkInterfaceComponent.IPv6))
- return true;
- }
- return false;
- }
- }
- #endif
- /* the field "socket" is looked up by name by the runtime */
- private IntPtr socket;
- private AddressFamily address_family;
- private SocketType socket_type;
- private ProtocolType protocol_type;
- internal bool blocking=true;
- Thread blocking_thread;
- #if NET_2_0
- private bool isbound;
- #endif
- /* When true, the socket was connected at the time of
- * the last IO operation
- */
- private bool connected;
- /* true if we called Close_internal */
- private bool closed;
- internal bool disposed;
- /*
- * This EndPoint is used when creating new endpoints. Because
- * there are many types of EndPoints possible,
- * seed_endpoint.Create(addr) is used for creating new ones.
- * As such, this value is set on Bind, SentTo, ReceiveFrom,
- * Connect, etc.
- */
- internal EndPoint seed_endpoint = null;
- #if !TARGET_JVM
- // Creates a new system socket, returning the handle
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern IntPtr Socket_internal(AddressFamily family,
- SocketType type,
- ProtocolType proto,
- out int error);
- #endif
-
- public Socket(AddressFamily family, SocketType type, ProtocolType proto)
- {
- #if NET_2_1
- if (family == AddressFamily.Unspecified)
- throw new ArgumentException ("family");
- #endif
- address_family=family;
- socket_type=type;
- protocol_type=proto;
-
- int error;
-
- socket = Socket_internal (family, type, proto, out error);
- if (error != 0)
- throw new SocketException (error);
- #if !NET_2_1
- SocketDefaults ();
- #endif
- }
- ~Socket ()
- {
- Dispose (false);
- }
- public AddressFamily AddressFamily {
- get { return address_family; }
- }
- #if !TARGET_JVM
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void Blocking_internal(IntPtr socket,
- bool block,
- out int error);
- #endif
- public bool Blocking {
- get {
- return(blocking);
- }
- set {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- int error;
-
- Blocking_internal (socket, value, out error);
- if (error != 0)
- throw new SocketException (error);
-
- blocking=value;
- }
- }
- public bool Connected {
- get { return connected; }
- internal set { connected = value; }
- }
- public ProtocolType ProtocolType {
- get { return protocol_type; }
- }
- #if NET_2_0
- public bool NoDelay {
- get {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- ThrowIfUpd ();
- return (int)(GetSocketOption (
- SocketOptionLevel.Tcp,
- SocketOptionName.NoDelay)) != 0;
- }
- set {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- ThrowIfUpd ();
- SetSocketOption (
- SocketOptionLevel.Tcp,
- SocketOptionName.NoDelay, value ? 1 : 0);
- }
- }
- public int ReceiveBufferSize {
- get {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- return((int)GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer));
- }
- set {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- if (value < 0) {
- throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
- }
-
- SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, value);
- }
- }
- public int SendBufferSize {
- get {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- return((int)GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendBuffer));
- }
- set {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- if (value < 0) {
- throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
- }
-
- SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.SendBuffer,
- value);
- }
- }
- public short Ttl {
- get {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
-
- short ttl_val;
-
- if (address_family == AddressFamily.InterNetwork) {
- ttl_val = (short)((int)GetSocketOption (SocketOptionLevel.IP, SocketOptionName.IpTimeToLive));
- } else if (address_family == AddressFamily.InterNetworkV6) {
- ttl_val = (short)((int)GetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.HopLimit));
- } else {
- throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
- }
-
- return(ttl_val);
- }
- set {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
-
- if (address_family == AddressFamily.InterNetwork) {
- SetSocketOption (SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, value);
- } else if (address_family == AddressFamily.InterNetworkV6) {
- SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.HopLimit, value);
- } else {
- throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
- }
- }
- }
- #endif
- // Returns the remote endpoint details in addr and port
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static SocketAddress RemoteEndPoint_internal(IntPtr socket, out int error);
- public EndPoint RemoteEndPoint {
- get {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
-
- /*
- * If the seed EndPoint is null, Connect, Bind,
- * etc has not yet been called. MS returns null
- * in this case.
- */
- if (seed_endpoint == null)
- return null;
-
- SocketAddress sa;
- int error;
-
- sa=RemoteEndPoint_internal(socket, out error);
- if (error != 0)
- throw new SocketException (error);
- return seed_endpoint.Create (sa);
- }
- }
- protected virtual void Dispose (bool explicitDisposing)
- {
- if (disposed)
- return;
- disposed = true;
- connected = false;
- if ((int) socket != -1) {
- int error;
- closed = true;
- IntPtr x = socket;
- socket = (IntPtr) (-1);
- Close_internal (x, out error);
- if (blocking_thread != null) {
- blocking_thread.Abort ();
- blocking_thread = null;
- }
- if (error != 0)
- throw new SocketException (error);
- }
- }
- #if NET_2_1
- public void Dispose ()
- #else
- void IDisposable.Dispose ()
- #endif
- {
- Dispose (true);
- GC.SuppressFinalize (this);
- }
- // Closes the socket
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void Close_internal(IntPtr socket, out int error);
- public void Close ()
- {
- ((IDisposable) this).Dispose ();
- }
- // Connects to the remote address
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void Connect_internal(IntPtr sock,
- SocketAddress sa,
- out int error);
- public void Connect (EndPoint remote_end)
- {
- SocketAddress serial = null;
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (remote_end == null)
- throw new ArgumentNullException("remote_end");
- if (remote_end is IPEndPoint) {
- IPEndPoint ep = (IPEndPoint) remote_end;
- if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any))
- throw new SocketException ((int) SocketError.AddressNotAvailable);
- }
- #if NET_2_1
- if (protocol_type != ProtocolType.Tcp)
- throw new SocketException ((int) SocketError.AccessDenied);
- DnsEndPoint dep = (remote_end as DnsEndPoint);
- if (dep != null)
- serial = dep.AsIPEndPoint ().Serialize ();
- else
- serial = remote_end.Serialize ();
- #elif NET_2_0
- /* TODO: check this for the 1.1 profile too */
- if (islistening)
- throw new InvalidOperationException ();
- serial = remote_end.Serialize ();
- #else
- serial = remote_end.Serialize ();
- #endif
- int error = 0;
- blocking_thread = Thread.CurrentThread;
- try {
- Connect_internal (socket, serial, out error);
- } catch (ThreadAbortException) {
- if (disposed) {
- Thread.ResetAbort ();
- error = (int) SocketError.Interrupted;
- }
- } finally {
- blocking_thread = null;
- }
- if (error != 0)
- throw new SocketException (error);
- connected=true;
- #if NET_2_0
- isbound = true;
- #endif
-
- seed_endpoint = remote_end;
- }
- #if NET_2_0
- public bool ReceiveAsync (SocketAsyncEventArgs e)
- {
- // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
- //
- // LAME SPEC: the ArgumentException is never thrown, instead an NRE is
- // thrown when e.Buffer and e.BufferList are null (works fine when one is
- // set to a valid object)
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- // We do not support recv into multiple buffers yet
- if (e.BufferList != null)
- throw new NotSupportedException ("Mono doesn't support using BufferList at this point.");
-
- e.DoOperation (SocketAsyncOperation.Receive, this);
- // We always return true for now
- return true;
- }
- public bool SendAsync (SocketAsyncEventArgs e)
- {
- // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
-
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (e.Buffer == null && e.BufferList == null)
- throw new ArgumentException ("Either e.Buffer or e.BufferList must be valid buffers.");
- e.DoOperation (SocketAsyncOperation.Send, this);
- // We always return true for now
- return true;
- }
- #endif
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- extern static bool Poll_internal (IntPtr socket, SelectMode mode, int timeout, out int error);
- /* This overload is needed as the async Connect method
- * also needs to check the socket error status, but
- * getsockopt(..., SO_ERROR) clears the error.
- */
- internal bool Poll (int time_us, SelectMode mode, out int socket_error)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (mode != SelectMode.SelectRead &&
- mode != SelectMode.SelectWrite &&
- mode != SelectMode.SelectError)
- throw new NotSupportedException ("'mode' parameter is not valid.");
- int error;
- bool result = Poll_internal (socket, mode, time_us, out error);
- if (error != 0)
- throw new SocketException (error);
- socket_error = (int)GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
-
- if (mode == SelectMode.SelectWrite && result) {
- /* Update the connected state; for
- * non-blocking Connect()s this is
- * when we can find out that the
- * connect succeeded.
- */
- if (socket_error == 0) {
- connected = true;
- }
- }
-
- return result;
- }
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static int Receive_internal(IntPtr sock,
- byte[] buffer,
- int offset,
- int count,
- SocketFlags flags,
- out int error);
- internal int Receive_nochecks (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error)
- {
- int nativeError;
- int ret = Receive_internal (socket, buf, offset, size, flags, out nativeError);
- error = (SocketError) nativeError;
- if (error != SocketError.Success && error != SocketError.WouldBlock && error != SocketError.InProgress)
- connected = false;
- else
- connected = true;
-
- return ret;
- }
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void GetSocketOption_obj_internal(IntPtr socket,
- SocketOptionLevel level, SocketOptionName name, out object obj_val,
- out int error);
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static int Send_internal(IntPtr sock,
- byte[] buf, int offset,
- int count,
- SocketFlags flags,
- out int error);
- internal int Send_nochecks (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error)
- {
- if (size == 0) {
- error = SocketError.Success;
- return 0;
- }
- int nativeError;
- int ret = Send_internal (socket, buf, offset, size, flags, out nativeError);
- error = (SocketError)nativeError;
- if (error != SocketError.Success && error != SocketError.WouldBlock && error != SocketError.InProgress)
- connected = false;
- else
- connected = true;
- return ret;
- }
- public object GetSocketOption (SocketOptionLevel level, SocketOptionName name)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- object obj_val;
- int error;
-
- GetSocketOption_obj_internal(socket, level, name, out obj_val,
- out error);
- if (error != 0)
- throw new SocketException (error);
-
- if (name == SocketOptionName.Linger) {
- return((LingerOption)obj_val);
- } else if (name==SocketOptionName.AddMembership ||
- name==SocketOptionName.DropMembership) {
- return((MulticastOption)obj_val);
- } else if (obj_val is int) {
- return((int)obj_val);
- } else {
- return(obj_val);
- }
- }
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- private extern static void Shutdown_internal (IntPtr socket, SocketShutdown how, out int error);
-
- public void Shutdown (SocketShutdown how)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- int error;
-
- Shutdown_internal (socket, how, out error);
- if (error != 0)
- throw new SocketException (error);
- }
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void SetSocketOption_internal (IntPtr socket, SocketOptionLevel level,
- SocketOptionName name, object obj_val,
- byte [] byte_val, int int_val,
- out int error);
- public void SetSocketOption (SocketOptionLevel level, SocketOptionName name, int opt_value)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- int error;
-
- SetSocketOption_internal(socket, level, name, null,
- null, opt_value, out error);
- if (error != 0)
- throw new SocketException (error);
- }
- private void ThrowIfUpd ()
- {
- #if !NET_2_1
- if (protocol_type == ProtocolType.Udp)
- throw new SocketException ((int)SocketError.ProtocolOption);
- #endif
- }
- #if NET_2_1
- static MethodInfo check_socket_policy;
- static void CheckConnect (SocketAsyncEventArgs e, bool checkPolicy)
- {
- // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
- if (e.RemoteEndPoint == null)
- throw new ArgumentNullException ("remoteEP");
- if (e.BufferList != null)
- throw new ArgumentException ("Multiple buffers cannot be used with this method.");
- if (!checkPolicy)
- return;
- e.SocketError = SocketError.AccessDenied;
- if (check_socket_policy == null) {
- Type type = Type.GetType ("System.Windows.Browser.Net.CrossDomainPolicyManager, System.Windows.Browser, Version=2.0.5.0, Culture=Neutral, PublicKeyToken=7cec85d7bea7798e");
- check_socket_policy = type.GetMethod ("CheckEndPoint");
- if (check_socket_policy == null)
- throw new SecurityException ();
- }
- if ((bool) check_socket_policy.Invoke (null, new object [1] { e.RemoteEndPoint }))
- e.SocketError = SocketError.Success;
- }
- // only _directly_ used (with false) to download the socket policy
- internal bool ConnectAsync (SocketAsyncEventArgs e, bool checkPolicy)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- CheckConnect (e, checkPolicy);
- e.DoOperation (SocketAsyncOperation.Connect, this);
- // We always return true for now
- return true;
- }
- public bool ConnectAsync (SocketAsyncEventArgs e)
- {
- return ConnectAsync (e, true);
- }
- public static bool ConnectAsync (SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e)
- {
- CheckConnect (e, true);
- Socket s = new Socket (AddressFamily.InterNetwork, socketType, protocolType);
- e.DoOperation (SocketAsyncOperation.Connect, s);
- // We always return true for now
- return true;
- }
- public static void CancelConnectAsync (SocketAsyncEventArgs e)
- {
- if (e == null)
- throw new ArgumentNullException ("e");
- Socket s = e.ConnectSocket;
- if ((s != null) && (s.blocking_thread != null))
- s.blocking_thread.Abort ();
- }
- #endif
- }
- }
|