Пример #1
1
 protected static int SetSocketOption(Socket socket, SocketOptionLevel level, SocketOptionName name, int value)
 {
     if (((int)socket.GetSocketOption(level, name)) == value)
         return value;
     socket.SetSocketOption(level, name, value);
     return (int)socket.GetSocketOption(level, name);
 }
Пример #2
0
 protected void SetSocketOption(SocketOptionLevel level, SocketOptionName name, int option)
 {
     try
     {
         m_sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, option);
     }
     catch { }
 }
Пример #3
0
		protected static bool SetSocketOption(Socket socket, SocketOptionLevel level, SocketOptionName name, bool value) {
			if (((int)socket.GetSocketOption(level, name)) == (value ? 1 : 0))
				return value;
			socket.SetSocketOption(level, name, value);
			if (((int)socket.GetSocketOption(level, name)) != 1) {
				return false;
			}
			return true;
		}
Пример #4
0
		protected void SetSocketOption(SocketOptionLevel level, SocketOptionName name, int val)
		{
			try
			{
				sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, val);
			}
			catch
			{
			}
		}
Пример #5
0
 internal static unsafe Error SetSockOpt(SafeHandle socket, SocketOptionLevel optionLevel, SocketOptionName optionName, byte* optionValue, int optionLen)
 {
     bool release = false;
     try
     {
         socket.DangerousAddRef(ref release);
         return DangerousSetSockOpt((int)socket.DangerousGetHandle(), optionLevel, optionName, optionValue, optionLen);
     }
     finally
     {
         if (release)
         {
             socket.DangerousRelease();
         }
     }
 }
Пример #6
0
        public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, bool optionValue)
        {

            if (optionName == SocketOptionName.ReuseAddress)
            {
                if (this.InternalServerSocket != null)
                {
                    try
                    {
                        //Console.WriteLine("setReuseAddress... " + new { optionValue });

                        this.InternalServerSocket.setReuseAddress(optionValue);
                    }
                    catch
                    {
                        throw;
                    }
                }
            }
        }
Пример #7
0
 public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
 {
     this.socket.SetSocketOption(optionLevel, optionName, optionValue);
 }
 public override object GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName)
 {
     return(m_socket.GetSocketOption(optionLevel, optionName));
 }
Пример #9
0
		public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
		{
			ThrowIfDisposedAndClosed ();

			int error;
			SetSocketOption_internal (safe_handle, optionLevel, optionName, null, null, optionValue, out error);

			if (error != 0) {
				throw new SocketException (error);
			}
		}
Пример #10
0
 // Sets the specified option to the specified value.
 public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, bool optionValue)
 {
     SetSocketOption(optionLevel, optionName, (optionValue ? 1 : 0));
 }
Пример #11
0
 private void CheckSetOptionPermissions(SocketOptionLevel optionLevel, SocketOptionName optionName)
 {
     // Freely allow only the options listed below.
     if (!(optionLevel == SocketOptionLevel.Tcp &&
           (optionName == SocketOptionName.NoDelay ||
            optionName == SocketOptionName.BsdUrgent ||
            optionName == SocketOptionName.Expedited))
           &&
           !(optionLevel == SocketOptionLevel.Udp &&
             (optionName == SocketOptionName.NoChecksum ||
              optionName == SocketOptionName.ChecksumCoverage))
           &&
           !(optionLevel == SocketOptionLevel.Socket &&
           (optionName == SocketOptionName.KeepAlive ||
            optionName == SocketOptionName.Linger ||
            optionName == SocketOptionName.DontLinger ||
            optionName == SocketOptionName.SendBuffer ||
            optionName == SocketOptionName.ReceiveBuffer ||
            optionName == SocketOptionName.SendTimeout ||
            optionName == SocketOptionName.ExclusiveAddressUse ||
            optionName == SocketOptionName.ReceiveTimeout))
           &&
           !(optionLevel == SocketOptionLevel.IPv6 &&
             optionName == (SocketOptionName)23)) // IPv6 protection level.
     {
     }
 }
Пример #12
0
 /// <summary>See <see cref="T:System.Net.Sockets.Socket.GetSocketOption(SocketOptionLevel, SocketOptionName, int)" />.</summary>
 public byte[] GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionLength)
 {
     return(client.Client.GetSocketOption(optionLevel, optionName, optionLength));
 }
Пример #13
0
 /// <summary>See <see cref="T:System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel, SocketOptionName, object)" />.</summary>
 public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, object optionValue)
 {
     client.Client.SetSocketOption(optionLevel, optionName, optionValue);
 }
Пример #14
0
 /// <summary>See <see cref="T:System.Net.Sockets.Socket.GetSocketOption(SocketOptionLevel, SocketOptionName)" />.</summary>
 public object GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName)
 {
     return(client.Client.GetSocketOption(optionLevel, optionName));
 }
Пример #15
0
 /// <summary>See <see cref="T:System.Net.Sockets.Socket.GetSocketOption(SocketOptionLevel, SocketOptionName, byte[])" />.</summary>
 public void GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue)
 {
     client.Client.GetSocketOption(optionLevel, optionName, optionValue);
 }
 public override void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
 {
     m_socket.SetSocketOption(optionLevel, optionName, optionValue);
 }
 public override byte[] GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionLength)
 {
     return(m_socket.GetSocketOption(optionLevel, optionName, optionLength));
 }
 public override void GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue)
 {
     m_socket.GetSocketOption(optionLevel, optionName, optionValue);
 }
Пример #19
0
		public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, bool optionValue)
		{
			if (disposed && closed)
				throw new ObjectDisposedException (GetType ().ToString ());

			int error;
			int int_val = (optionValue) ? 1 : 0;
			SetSocketOption_internal (socket, optionLevel, optionName, null, null, int_val, out error);
			if (error != 0) {
				if (error == (int) SocketError.InvalidArgument)
					throw new ArgumentException ();
				throw new SocketException (error);
			}
		}
Пример #20
0
        /// <summary>
        /// Begins listening for clients.
        /// </summary>
        /// <param name="port">Port to listen for clients on.</param>
        /// <param name="ipv6">If set to true, use a dual-stack socket to allow IPv4/6 connections. Otherwise use IPv4-only socket.</param>
        public void Listen(ushort port, bool ipv6)
        {
            this.Port = port;
            try
            {
                if (!Listening)
                {
                    if (_handle != null)
                    {
                        _handle.Close();
                        _handle = null;
                    }

                    if (Socket.OSSupportsIPv6 && ipv6)
                    {
                        _handle = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                        // fix for mono compatibility, SocketOptionName.IPv6Only
                        SocketOptionName ipv6only = (SocketOptionName)27;
                        _handle.SetSocketOption(SocketOptionLevel.IPv6, ipv6only, 0);
                        _handle.Bind(new IPEndPoint(IPAddress.IPv6Any, port));
                    }
                    else
                    {
                        _handle = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        _handle.Bind(new IPEndPoint(IPAddress.Any, port));
                    }
                    _handle.Listen(1000);

                    ProcessingDisconnect = false;

                    OnServerState(true);

                    if (_item != null)
                    {
                        _item.Dispose();
                        _item = null;
                    }

                    _item            = new SocketAsyncEventArgs();
                    _item.Completed += AcceptClient;

                    if (!_handle.AcceptAsync(_item))
                    {
                        AcceptClient(null, _item);
                    }
                }
            }
            catch (SocketException ex)
            {
                if (ex.ErrorCode == 10048)
                {
                    MessageBox.Show("The port is already in use.", "Listen Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show(
                        string.Format(
                            "An unexpected socket error occurred: {0}\n\nError Code: {1}\n\nPlease report this as fast as possible here:\n{2}/issues",
                            ex.Message, ex.ErrorCode, Settings.RepositoryURL), "Unexpected Listen Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                Disconnect();
            }
            catch (Exception)
            {
                Disconnect();
            }
        }
Пример #21
0
 // Sets the specified option to the specified value.
 public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
 {
     if (CleanedUp)
     {
         throw new ObjectDisposedException(this.GetType().FullName);
     }
     CheckSetOptionPermissions(optionLevel, optionName);
     GlobalLog.Print("Socket#" + Logging.HashString(this) + "::SetSocketOption(): optionLevel:" + optionLevel.ToString() + " optionName:" + optionName.ToString() + " optionValue:" + optionValue.ToString());
     SetSocketOption(optionLevel, optionName, optionValue, false);
 }
        public Object GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName)
        {
            Contract.Ensures(Contract.Result <System.Object>() != null);

            return(default(Object));
        }
Пример #23
0
        // Gets the value of a socket option.
        public object GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName)
        {
            if (CleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (optionLevel == SocketOptionLevel.Socket && optionName == SocketOptionName.Linger)
            {
                return GetLingerOpt();
            }
            else if (optionLevel == SocketOptionLevel.IP && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership))
            {
                return GetMulticastOpt(optionName);
            }
            else if (optionLevel == SocketOptionLevel.IPv6 && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership))
            {
                // Handle IPv6 case
                return GetIPv6MulticastOpt(optionName);
            }

            int optionValue = 0;

            // This can throw ObjectDisposedException.
            SocketError errorCode = SocketPal.GetSockOpt(
                _handle,
                optionLevel,
                optionName,
                out optionValue);

            GlobalLog.Print("Socket#" + Logging.HashString(this) + "::GetSocketOption() Interop.Winsock.getsockopt returns errorCode:" + errorCode);

            // Throw an appropriate SocketException if the native call fails.
            if (errorCode != SocketError.Success)
            {
                // Update the internal state of this socket according to the error before throwing.
                SocketException socketException = new SocketException((int)errorCode);
                UpdateStatusAfterSocketError(socketException);
                if (s_loggingEnabled)
                {
                    Logging.Exception(Logging.Sockets, this, "GetSocketOption", socketException);
                }
                throw socketException;
            }

            return optionValue;
        }
 public void GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue)
 {
 }
Пример #25
0
        // IPv6 getsockopt for JOIN / LEAVE multicast group.
        private IPv6MulticastOption GetIPv6MulticastOpt(SocketOptionName optionName)
        {
            IPv6MulticastOption multicastOption;
            SocketError errorCode = SocketPal.GetIPv6MulticastOption(_handle, optionName, out multicastOption);

            GlobalLog.Print("Socket#" + Logging.HashString(this) + "::getIPv6MulticastOpt() Interop.Winsock.getsockopt returns errorCode:" + errorCode);

            // Throw an appropriate SocketException if the native call fails.
            if (errorCode != SocketError.Success)
            {
                // Update the internal state of this socket according to the error before throwing.
                SocketException socketException = new SocketException((int)errorCode);
                UpdateStatusAfterSocketError(socketException);
                if (s_loggingEnabled)
                {
                    Logging.Exception(Logging.Sockets, this, "getIPv6MulticastOpt", socketException);
                }
                throw socketException;
            }

            return multicastOption;
        }
 public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, bool optionValue)
 {
 }
Пример #27
0
		public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, object optionValue)
		{
			ThrowIfDisposedAndClosed ();

			// NOTE: if a null is passed, the byte[] overload is used instead...
			if (optionValue == null)
				throw new ArgumentNullException("optionValue");

			int error;

			if (optionLevel == SocketOptionLevel.Socket && optionName == SocketOptionName.Linger) {
				LingerOption linger = optionValue as LingerOption;
				if (linger == null)
					throw new ArgumentException ("A 'LingerOption' value must be specified.", "optionValue");
				SetSocketOption_internal (safe_handle, optionLevel, optionName, linger, null, 0, out error);
			} else if (optionLevel == SocketOptionLevel.IP && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)) {
				MulticastOption multicast = optionValue as MulticastOption;
				if (multicast == null)
					throw new ArgumentException ("A 'MulticastOption' value must be specified.", "optionValue");
				SetSocketOption_internal (safe_handle, optionLevel, optionName, multicast, null, 0, out error);
			} else if (optionLevel == SocketOptionLevel.IPv6 && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)) {
				IPv6MulticastOption multicast = optionValue as IPv6MulticastOption;
				if (multicast == null)
					throw new ArgumentException ("A 'IPv6MulticastOption' value must be specified.", "optionValue");
				SetSocketOption_internal (safe_handle, optionLevel, optionName, multicast, null, 0, out error);
			} else {
				throw new ArgumentException ("Invalid value specified.", "optionValue");
			}

			if (error != 0) {
				if (error == (int) SocketError.InvalidArgument)
					throw new ArgumentException ();
				throw new SocketException (error);
			}
		}
Пример #28
0
 internal static void SetSockOptSysCall(Socket socket, SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
 {
     unsafe
     {
         ThrowOnError(SetSockOptSysCall(socket.Handle, (int)optionLevel, (int)optionName, (byte *)&optionValue, sizeof(int)));
     }
 }
Пример #29
0
		extern static void SetSocketOption_internal (IntPtr socket, SocketOptionLevel level, SocketOptionName name, object obj_val, byte [] byte_val, int int_val, out int error);
Пример #30
0
 /// <summary>
 /// Sets the specified option to the specified value.
 /// </summary>
 /// <param name="optionLevel"></param>
 /// <param name="optionName"></param>
 /// <param name="optionValue"></param>
 public void SetSocketOption(SocketOptionLevel optionLevel,
                             SocketOptionName optionName,
                             object optionValue)
 {
     m_sock.SetSocketOption(optionLevel, optionName, optionValue);
 }
Пример #31
0
        private void SetOption <T>(ref T location, ref bool initialized, SocketOptionLevel level, SocketOptionName name, T value)
        {
            // Used in the setter for each shadow property.

            // Store the option on to the client socket.  If the client socket isn't yet set, we still want to set
            // the property onto a socket so we can get validation performed by the underlying system on the option being
            // set... so, albeit being a bit expensive, we create a temporary socket we can use for validation purposes.
            Socket s = _clientSocket ?? CreateSocket();

            try
            {
                if (typeof(T) == typeof(int))
                {
                    s.SetSocketOption(level, name, (int)(object)value);
                }
                else
                {
                    Debug.Assert(typeof(T) == typeof(LingerOption), $"Unexpected type: {typeof(T)}");
                    s.SetSocketOption(level, name, value);
                }
            }
            finally
            {
                if (s != _clientSocket)
                {
                    s.Dispose();
                }
            }

            // Then if it was successful, store it into the shadow.
            location    = value;
            initialized = true;
        }
Пример #32
0
        private LinuxError HandleSetSocketOption(ServiceCtx context, BsdSocket socket, SocketOptionName optionName, long optionValuePosition, long optionValueSize)
        {
            try
            {
                switch (optionName)
                {
                case SocketOptionName.Broadcast:
                case SocketOptionName.DontLinger:
                case SocketOptionName.Debug:
                case SocketOptionName.Error:
                case SocketOptionName.KeepAlive:
                case SocketOptionName.OutOfBandInline:
                case SocketOptionName.ReceiveBuffer:
                case SocketOptionName.ReceiveTimeout:
                case SocketOptionName.SendBuffer:
                case SocketOptionName.SendTimeout:
                case SocketOptionName.Type:
                case SocketOptionName.ReuseAddress:
                    socket.Handle.SetSocketOption(SocketOptionLevel.Socket, optionName, context.Memory.Read <int>((ulong)optionValuePosition));

                    return(LinuxError.SUCCESS);

                case (SocketOptionName)0x200:
                    socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, context.Memory.Read <int>((ulong)optionValuePosition));

                    return(LinuxError.SUCCESS);

                case SocketOptionName.Linger:
                    socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger,
                                                  new LingerOption(context.Memory.Read <int>((ulong)optionValuePosition) != 0, context.Memory.Read <int>((ulong)optionValuePosition + 4)));

                    return(LinuxError.SUCCESS);

                default:
                    Logger.Warning?.Print(LogClass.ServiceBsd, $"Unsupported SetSockOpt OptionName: {optionName}");

                    return(LinuxError.EOPNOTSUPP);
                }
            }
            catch (SocketException exception)
            {
                return(ConvertError((WsaError)exception.ErrorCode));
            }
        }
Пример #33
0
        private T GetOption <T>(ref T location, ref bool initialized, SocketOptionLevel level, SocketOptionName name)
        {
            // Used in the getter for each shadow property.

            // If we already have our client socket set up, just get its value for the option.
            // Otherwise, return our shadow property.  If that shadow hasn't yet been initialized,
            // first initialize it by creating a temporary socket from which we can obtain a default value.

            if (_clientSocket != null)
            {
                return((T)_clientSocket.GetSocketOption(level, name));
            }

            if (!initialized)
            {
                using (Socket s = CreateSocket())
                {
                    location = (T)s.GetSocketOption(level, name);
                }
                initialized = true;
            }

            return(location);
        }
Пример #34
0
        public static unsafe SocketError SetSockOpt(SafeCloseSocket handle, SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
        {
            SocketError errorCode = Interop.Winsock.setsockopt(
                handle,
                optionLevel,
                optionName,
                ref optionValue,
                sizeof(int));

            return(errorCode == SocketError.SocketError ? GetLastSocketError() : SocketError.Success);
        }
Пример #35
0
		public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, byte [] optionValue)
		{
			if (disposed && closed)
				throw new ObjectDisposedException (GetType ().ToString ());

			// I'd throw an ArgumentNullException, but this is what MS does.
			if (optionValue == null)
				throw new SocketException ((int) SocketError.Fault,
					"Error trying to dereference an invalid pointer");
			
			int error;

			SetSocketOption_internal (socket, optionLevel, optionName, null,
						 optionValue, 0, out error);

			if (error != 0) {
				if (error == (int) SocketError.InvalidArgument)
					throw new ArgumentException ();
				throw new SocketException (error);
			}
		}
Пример #36
0
        public static SocketError SetSockOpt(SafeCloseSocket handle, SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue)
        {
            SocketError errorCode = Interop.Winsock.setsockopt(
                handle,
                optionLevel,
                optionName,
                optionValue,
                optionValue != null ? optionValue.Length : 0);

            return(errorCode == SocketError.SocketError ? GetLastSocketError() : SocketError.Success);
        }
Пример #37
0
 private int numericOption(SocketOptionLevel optionLevel, SocketOptionName optionName) {
     return (int)Client.GetSocketOption(optionLevel, optionName);
 }
Пример #38
0
        public static SocketError GetSockOpt(SafeCloseSocket handle, SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue, ref int optionLength)
        {
            SocketError errorCode = Interop.Winsock.getsockopt(
                handle,
                optionLevel,
                optionName,
                optionValue,
                ref optionLength);

            return(errorCode == SocketError.SocketError ? GetLastSocketError() : SocketError.Success);
        }
Пример #39
0
        public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue)
        {
            if (CleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            CheckSetOptionPermissions(optionLevel, optionName);

            GlobalLog.Print("Socket#" + Logging.HashString(this) + "::SetSocketOption(): optionLevel:" + optionLevel.ToString() + " optionName:" + optionName.ToString() + " optionValue:" + optionValue.ToString());

            // This can throw ObjectDisposedException.
            SocketError errorCode = SocketPal.SetSockOpt(_handle, optionLevel, optionName, optionValue);

            GlobalLog.Print("Socket#" + Logging.HashString(this) + "::SetSocketOption() Interop.Winsock.setsockopt returns errorCode:" + errorCode);

            // Throw an appropriate SocketException if the native call fails.
            if (errorCode != SocketError.Success)
            {
                // Update the internal state of this socket according to the error before throwing.
                SocketException socketException = new SocketException((int)errorCode);
                UpdateStatusAfterSocketError(socketException);
                if (s_loggingEnabled)
                {
                    Logging.Exception(Logging.Sockets, this, "SetSocketOption", socketException);
                }
                throw socketException;
            }
        }
Пример #40
0
 public abstract void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue);
Пример #41
0
        // Sets the specified option to the specified value.
        public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, object optionValue)
        {
            if (CleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            // Validate input parameters.
            if (optionValue == null)
            {
                throw new ArgumentNullException("optionValue");
            }

            CheckSetOptionPermissions(optionLevel, optionName);

            GlobalLog.Print("Socket#" + Logging.HashString(this) + "::SetSocketOption(): optionLevel:" + optionLevel.ToString() + " optionName:" + optionName.ToString() + " optionValue:" + optionValue.ToString());

            if (optionLevel == SocketOptionLevel.Socket && optionName == SocketOptionName.Linger)
            {
                LingerOption lingerOption = optionValue as LingerOption;
                if (lingerOption == null)
                {
                    throw new ArgumentException(SR.Format(SR.net_sockets_invalid_optionValue, "LingerOption"), "optionValue");
                }
                if (lingerOption.LingerTime < 0 || lingerOption.LingerTime > (int)UInt16.MaxValue)
                {
                    throw new ArgumentException(SR.Format(SR.ArgumentOutOfRange_Bounds_Lower_Upper, 0, (int)UInt16.MaxValue), "optionValue.LingerTime");
                }
                SetLingerOption(lingerOption);
            }
            else if (optionLevel == SocketOptionLevel.IP && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership))
            {
                MulticastOption multicastOption = optionValue as MulticastOption;
                if (multicastOption == null)
                {
                    throw new ArgumentException(SR.Format(SR.net_sockets_invalid_optionValue, "MulticastOption"), "optionValue");
                }
                SetMulticastOption(optionName, multicastOption);
            }
            else if (optionLevel == SocketOptionLevel.IPv6 && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership))
            {
                // IPv6 Changes: Handle IPv6 Multicast Add / Drop
                IPv6MulticastOption multicastOption = optionValue as IPv6MulticastOption;
                if (multicastOption == null)
                {
                    throw new ArgumentException(SR.Format(SR.net_sockets_invalid_optionValue, "IPv6MulticastOption"), "optionValue");
                }
                SetIPv6MulticastOption(optionName, multicastOption);
            }
            else
            {
                throw new ArgumentException(SR.net_sockets_invalid_optionValue_all, "optionValue");
            }
        }
Пример #42
0
		public void GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, byte [] optionValue)
		{
			ThrowIfDisposedAndClosed ();

			if (optionValue == null)
				throw new SocketException ((int) SocketError.Fault, "Error trying to dereference an invalid pointer");

			int error;
			GetSocketOption_arr_internal (safe_handle, optionLevel, optionName, ref optionValue, out error);

			if (error != 0)
				throw new SocketException (error);
		}
Пример #43
0
        public byte[] GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionLength)
        {
            if (CleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            byte[] optionValue = new byte[optionLength];
            int realOptionLength = optionLength;

            // This can throw ObjectDisposedException.
            SocketError errorCode = SocketPal.GetSockOpt(
                _handle,
                optionLevel,
                optionName,
                optionValue,
                ref realOptionLength);

            GlobalLog.Print("Socket#" + Logging.HashString(this) + "::GetSocketOption() Interop.Winsock.getsockopt returns errorCode:" + errorCode);

            // Throw an appropriate SocketException if the native call fails.
            if (errorCode != SocketError.Success)
            {
                // Update the internal state of this socket according to the error before throwing.
                SocketException socketException = new SocketException((int)errorCode);
                UpdateStatusAfterSocketError(socketException);
                if (s_loggingEnabled)
                {
                    Logging.Exception(Logging.Sockets, this, "GetSocketOption", socketException);
                }
                throw socketException;
            }

            if (optionLength != realOptionLength)
            {
                byte[] newOptionValue = new byte[realOptionLength];
                Buffer.BlockCopy(optionValue, 0, newOptionValue, 0, realOptionLength);
                optionValue = newOptionValue;
            }

            return optionValue;
        }
Пример #44
0
		public object GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName)
		{
			ThrowIfDisposedAndClosed ();

			int error;
			object obj_val;
			GetSocketOption_obj_internal (safe_handle, optionLevel, optionName, out obj_val, out error);

			if (error != 0)
				throw new SocketException (error);

			if (optionName == SocketOptionName.Linger)
				return (LingerOption) obj_val;
			else if (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)
				return (MulticastOption) obj_val;
			else if (obj_val is int)
				return (int) obj_val;
			else
				return obj_val;
		}
Пример #45
0
        internal unsafe void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue, bool silent)
        {
            GlobalLog.Print("Socket#" + Logging.HashString(this) + "::SetSocketOption() optionLevel:" + optionLevel + " optionName:" + optionName + " optionValue:" + optionValue + " silent:" + silent);
            if (silent && (CleanedUp || _handle.IsInvalid))
            {
                GlobalLog.Print("Socket#" + Logging.HashString(this) + "::SetSocketOption() skipping the call");
                return;
            }
            SocketError errorCode = SocketError.Success;
            try
            {
                errorCode = SocketPal.SetSockOpt(_handle, optionLevel, optionName, optionValue);

                GlobalLog.Print("Socket#" + Logging.HashString(this) + "::SetSocketOption() Interop.Winsock.setsockopt returns errorCode:" + errorCode);
            }
            catch
            {
                if (silent && _handle.IsInvalid)
                {
                    return;
                }
                throw;
            }

            // Keep the internal state in sync if the user manually resets this.
            if (optionName == SocketOptionName.PacketInformation && optionValue == 0 &&
                errorCode == SocketError.Success)
            {
                _receivingPacketInformation = false;
            }

            if (silent)
            {
                return;
            }

            // Throw an appropriate SocketException if the native call fails.
            if (errorCode != SocketError.Success)
            {
                // Update the internal state of this socket according to the error before throwing.
                SocketException socketException = new SocketException((int)errorCode);
                UpdateStatusAfterSocketError(socketException);
                if (s_loggingEnabled)
                {
                    Logging.Exception(Logging.Sockets, this, "SetSocketOption", socketException);
                }
                throw socketException;
            }
        }
Пример #46
0
		extern static void GetSocketOption_obj_internal(IntPtr socket, SocketOptionLevel level, SocketOptionName name, out object obj_val, out int error);
Пример #47
0
 public abstract Object GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName);
Пример #48
0
 public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, bool optionValue)
 {
     SetSocketOption(optionLevel, optionName, optionValue ? 1 : 0);
 }
Пример #49
0
		public byte [] GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, int length)
		{
			ThrowIfDisposedAndClosed ();

			int error;
			byte[] byte_val = new byte [length];
			GetSocketOption_arr_internal (safe_handle, optionLevel, optionName, ref byte_val, out error);

			if (error != 0)
				throw new SocketException (error);

			return byte_val;
		}
Пример #50
0
 public abstract void GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue);
Пример #51
0
		extern static void GetSocketOption_arr_internal(IntPtr socket, SocketOptionLevel level, SocketOptionName name, ref byte[] byte_val, out int error);
Пример #52
0
 public abstract byte[] GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionLength);
Пример #53
0
		public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, byte [] optionValue)
		{
			ThrowIfDisposedAndClosed ();

			// I'd throw an ArgumentNullException, but this is what MS does.
			if (optionValue == null)
				throw new SocketException ((int) SocketError.Fault, "Error trying to dereference an invalid pointer");

			int error;
			SetSocketOption_internal (safe_handle, optionLevel, optionName, null, optionValue, 0, out error);

			if (error != 0) {
				if (error == (int) SocketError.InvalidArgument)
					throw new ArgumentException ();
				throw new SocketException (error);
			}
		}
Пример #54
0
        public static unsafe SocketError SetSockOpt(SafeSocketHandle handle, SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
        {
            SocketError errorCode;

            if (optionLevel == SocketOptionLevel.Tcp &&
                (optionName == SocketOptionName.TcpKeepAliveTime || optionName == SocketOptionName.TcpKeepAliveInterval) &&
                IOControlKeepAlive.IsNeeded)
            {
                errorCode = IOControlKeepAlive.Set(handle, optionName, optionValue);
            }
            else
            {
                errorCode = Interop.Winsock.setsockopt(
                    handle,
                    optionLevel,
                    optionName,
                    ref optionValue,
                    sizeof(int));
            }
            return(errorCode == SocketError.SocketError ? GetLastSocketError() : SocketError.Success);
        }
Пример #55
0
		public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, bool optionValue)
		{
			ThrowIfDisposedAndClosed ();

			int error;
			int int_val = optionValue ? 1 : 0;
			SetSocketOption_internal (safe_handle, optionLevel, optionName, null, null, int_val, out error);

			if (error != 0) {
				if (error == (int) SocketError.InvalidArgument)
					throw new ArgumentException ();
				throw new SocketException (error);
			}
		}
Пример #56
0
        public static unsafe SocketError GetSockOpt(SafeSocketHandle handle, SocketOptionLevel optionLevel, SocketOptionName optionName, out int optionValue)
        {
            if (optionLevel == SocketOptionLevel.Tcp &&
                (optionName == SocketOptionName.TcpKeepAliveTime || optionName == SocketOptionName.TcpKeepAliveInterval) &&
                IOControlKeepAlive.IsNeeded)
            {
                optionValue = IOControlKeepAlive.Get(handle, optionName);
                return(SocketError.Success);
            }

            int         optionLength   = sizeof(int);
            int         tmpOptionValue = 0;
            SocketError errorCode      = Interop.Winsock.getsockopt(
                handle,
                optionLevel,
                optionName,
                (byte *)&tmpOptionValue,
                ref optionLength);

            optionValue = tmpOptionValue;
            return(errorCode == SocketError.SocketError ? GetLastSocketError() : SocketError.Success);
        }
Пример #57
0
		static void SetSocketOption_internal (SafeSocketHandle safeHandle, SocketOptionLevel level, SocketOptionName name, object obj_val, byte [] byte_val, int int_val, out int error)
		{
			bool release = false;
			try {
				safeHandle.DangerousAddRef (ref release);
				SetSocketOption_internal (safeHandle.DangerousGetHandle (), level, name, obj_val, byte_val, int_val, out error);
			} finally {
				if (release)
					safeHandle.DangerousRelease ();
			}
		}
Пример #58
0
        public static unsafe SocketError GetSockOpt(SafeSocketHandle handle, SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue, ref int optionLength)
        {
            if (optionLevel == SocketOptionLevel.Tcp &&
                (optionName == SocketOptionName.TcpKeepAliveTime || optionName == SocketOptionName.TcpKeepAliveInterval) &&
                IOControlKeepAlive.IsNeeded)
            {
                return(IOControlKeepAlive.Get(handle, optionName, optionValue, ref optionLength));
            }

            fixed(byte *optionValuePtr = optionValue)
            {
                SocketError errorCode = Interop.Winsock.getsockopt(
                    handle,
                    optionLevel,
                    optionName,
                    optionValuePtr,
                    ref optionLength);

                return(errorCode == SocketError.SocketError ? GetLastSocketError() : SocketError.Success);
            }
        }
Пример #59
0
 internal void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, object optionValue)
 {
     internalSocket.SetSocketOption(optionLevel, optionName, optionValue);
 }
Пример #60
0
 public static extern SocketError setsockopt([In] IntPtr socketHandle, [In] SocketOptionLevel optionLevel, [In] SocketOptionName optionName, [In] ref int optionValue, [In] int optionLength);