internal static unsafe SocketError CreateSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, out SafeSocketHandle socket)
        {
            SocketError errorCode;

            socket = CreateSocket(InnerSafeCloseSocket.CreateSocket(addressFamily, socketType, protocolType, out errorCode));
            return(errorCode);
        }
Exemplo n.º 2
0
            public static InnerSafeCloseSocket CreateSocket(int fileDescriptor)
            {
                var res = new InnerSafeCloseSocket();

                res.SetHandle((IntPtr)fileDescriptor);
                return(res);
            }
Exemplo n.º 3
0
 internal static SafeCloseSocket Accept(
     SafeCloseSocket socketHandle,
     byte[] socketAddress,
     ref int socketAddressSize)
 {
     return(CreateSocket(InnerSafeCloseSocket.Accept(socketHandle, socketAddress, ref socketAddressSize)));
 }
Exemplo n.º 4
0
        protected override bool ReleaseHandle()
        {
            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Print(
                    "SafeCloseSocket#" + LoggingHash.HashString(this) + "::ReleaseHandle() m_InnerSocket=" +
                    _innerSocket == null ? "null" : LoggingHash.HashString(_innerSocket));
            }

            _released = true;
            InnerSafeCloseSocket innerSocket = _innerSocket == null ? null : Interlocked.Exchange <InnerSafeCloseSocket>(ref _innerSocket, null);

            if (innerSocket != null)
            {
#if DEBUG
                // On AppDomain unload we may still have pending Overlapped operations.
                // ThreadPoolBoundHandle should handle this scenario by canceling them.
                innerSocket.LogRemainingOperations();
#endif

                innerSocket.DangerousRelease();
            }

            InnerReleaseHandle();

            return(true);
        }
Exemplo n.º 5
0
        protected static void CreateSocket(InnerSafeCloseSocket socket, SafeCloseSocket target)
        {
            if (socket != null && socket.IsInvalid)
            {
                target.SetHandleAsInvalid();
                return;
            }

            bool b = false;

            try
            {
                socket.DangerousAddRef(ref b);
            }
            catch
            {
                if (b)
                {
                    socket.DangerousRelease();
                    b = false;
                }
            }
            finally
            {
                if (b)
                {
                    target.SetInnerSocket(socket);
                    socket.Dispose();
                }
                else
                {
                    target.SetHandleAsInvalid();
                }
            }
        }
        private static SafeCloseSocket CreateSocket(InnerSafeCloseSocket socket)
        {
            SafeCloseSocket ret = new SafeCloseSocket();

            CreateSocket(socket, ret);
            return(ret);
        }
Exemplo n.º 7
0
            public static unsafe InnerSafeCloseSocket CreateSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
            {
                int af   = SocketPal.GetPlatformAddressFamily(addressFamily);
                int sock = SocketPal.GetPlatformSocketType(socketType);
                int pt   = (int)protocolType;

                int fd = Interop.libc.socket(af, sock, pt);

                if (fd != -1)
                {
                    // The socket was created successfully; make it non-blocking and enable
                    // IPV6_V6ONLY by default for AF_INET6 sockets.
                    int err = Interop.Sys.Fcntl.SetIsNonBlocking(fd, 1);
                    if (err != 0)
                    {
                        Interop.Sys.Close(fd);
                        fd = -1;
                    }
                    else if (addressFamily == AddressFamily.InterNetworkV6)
                    {
                        int on = 1;
                        err = Interop.libc.setsockopt(fd, Interop.libc.IPPROTO_IPV6, Interop.libc.IPV6_V6ONLY, &on, (uint)sizeof(int));
                        if (err != 0)
                        {
                            Interop.Sys.Close(fd);
                            fd = -1;
                        }
                    }
                }

                var res = new InnerSafeCloseSocket();

                res.SetHandle((IntPtr)fd);
                return(res);
            }
        internal static unsafe SocketError Accept(SafeSocketHandle socketHandle, byte[] socketAddress, ref int socketAddressSize, out SafeSocketHandle socket)
        {
            SocketError errorCode;

            socket = CreateSocket(InnerSafeCloseSocket.Accept(socketHandle, socketAddress, ref socketAddressSize, out errorCode));
            return(errorCode);
        }
        protected override bool ReleaseHandle()
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(this, $"_innerSocket={_innerSocket}");
            }

            _released = true;
            InnerSafeCloseSocket innerSocket = _innerSocket == null ? null : Interlocked.Exchange <InnerSafeCloseSocket>(ref _innerSocket, null);

            if (innerSocket != null)
            {
#if DEBUG
                // On AppDomain unload we may still have pending Overlapped operations.
                // ThreadPoolBoundHandle should handle this scenario by canceling them.
                innerSocket.LogRemainingOperations();
#endif

                innerSocket.DangerousRelease();
            }

            InnerReleaseHandle();

            return(true);
        }
            internal static InnerSafeCloseSocket CreateSocket(IntPtr fileDescriptor)
            {
                var res = new InnerSafeCloseSocket();

                res.SetHandle(fileDescriptor);
                return(res);
            }
Exemplo n.º 11
0
        private void SetInnerSocket(InnerSafeCloseSocket socket)
        {
            _innerSocket = socket;
            SetHandle(socket.DangerousGetHandle());
#if DEBUG
            _innerSocketCopy = socket;
#endif
        }
Exemplo n.º 12
0
        private void SetInnerSocket(InnerSafeCloseSocket socket)
        {
            _innerSocket = socket;
            SetHandle(socket.DangerousGetHandle());
#if DEBUG
            _innerSocketCopy = socket;
#endif
        }
            internal static InnerSafeCloseSocket Accept(SafeCloseSocket socketHandle, byte[] socketAddress, ref int socketAddressSize)
            {
                InnerSafeCloseSocket result = UnsafeNclNativeMethods.SafeNetHandles.accept(socketHandle.DangerousGetHandle(), socketAddress, ref socketAddressSize);

                if (result.IsInvalid)
                {
                    result.SetHandleAsInvalid();
                }
                return(result);
            }
Exemplo n.º 14
0
        private static SafeCloseSocket CreateSocket(InnerSafeCloseSocket socket)
        {
            SafeCloseSocket ret = new SafeCloseSocket();

            CreateSocket(socket, ret);

            GlobalLog.Print("SafeCloseSocket#" + Logging.HashString(ret) + "::CreateSocket()");

            return(ret);
        }
Exemplo n.º 15
0
            internal static InnerSafeCloseSocket CreateWSASocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
            {
                InnerSafeCloseSocket result = Interop.Winsock.WSASocketW(addressFamily, socketType, protocolType, IntPtr.Zero, 0, Interop.Winsock.SocketConstructorFlags.WSA_FLAG_OVERLAPPED);

                if (result.IsInvalid)
                {
                    result.SetHandleAsInvalid();
                }
                return(result);
            }
Exemplo n.º 16
0
            internal static InnerSafeCloseSocket Accept(SafeCloseSocket socketHandle, byte[] socketAddress, ref int socketAddressSize)
            {
                InnerSafeCloseSocket result = Interop.Winsock.accept(socketHandle.DangerousGetHandle(), socketAddress, ref socketAddressSize);

                if (result.IsInvalid)
                {
                    result.SetHandleAsInvalid();
                }
                return(result);
            }
            internal unsafe static InnerSafeCloseSocket CreateWSASocket(byte *pinnedBuffer)
            {
                //-1 is the value for FROM_PROTOCOL_INFO
                InnerSafeCloseSocket result = UnsafeNclNativeMethods.OSSOCK.WSASocket((AddressFamily)(-1), (SocketType)(-1), (ProtocolType)(-1), pinnedBuffer, 0, SocketConstructorFlags.WSA_FLAG_OVERLAPPED);

                if (result.IsInvalid)
                {
                    result.SetHandleAsInvalid();
                }
                return(result);
            }
        protected override bool ReleaseHandle()
        {
            this.m_Released = true;
            InnerSafeCloseSocket socket = (this.m_InnerSocket == null) ? null : Interlocked.Exchange <InnerSafeCloseSocket>(ref this.m_InnerSocket, null);

            if (socket != null)
            {
                socket.DangerousRelease();
            }
            return(true);
        }
        protected override bool ReleaseHandle()
        {
            m_Released = true;
            InnerSafeCloseSocket innerSocket = m_InnerSocket == null ? null : Interlocked.Exchange <InnerSafeCloseSocket>(ref m_InnerSocket, null);

            if (innerSocket != null)
            {
                innerSocket.DangerousRelease();
            }
            return(true);
        }
Exemplo n.º 20
0
            internal unsafe static InnerSafeCloseSocket CreateWSASocket(byte *pinnedBuffer)
            {
                // NOTE: -1 is the value for FROM_PROTOCOL_INFO.
                InnerSafeCloseSocket result = Interop.Winsock.WSASocketW((AddressFamily)(-1), (SocketType)(-1), (ProtocolType)(-1), pinnedBuffer, 0, Interop.Winsock.SocketConstructorFlags.WSA_FLAG_OVERLAPPED);

                if (result.IsInvalid)
                {
                    result.SetHandleAsInvalid();
                }
                return(result);
            }
Exemplo n.º 21
0
        internal void CloseAsIs(bool abortive)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(this, $"_innerSocket={_innerSocket}");
            }

#if DEBUG
            // If this throws it could be very bad.
            try
            {
#endif
            InnerSafeCloseSocket innerSocket = _innerSocket == null ? null : Interlocked.Exchange <InnerSafeCloseSocket>(ref _innerSocket, null);

            Dispose();
            if (innerSocket != null)
            {
                bool canceledOperations = false;

                // Wait until it's safe.
                SpinWait sw = new SpinWait();
                while (!_released)
                {
                    // The socket was not released due to the SafeHandle being used.
                    // Try to make those on-going calls return.
                    // On Linux, TryUnblockSocket will unblock current operations but it doesn't prevent
                    // a new one from starting. So we must call TryUnblockSocket multiple times.
                    canceledOperations |= innerSocket.TryUnblockSocket(abortive, _hasShutdownSend);
                    sw.SpinOnce();
                }

                canceledOperations |= DoReleaseHandle();

                // In case we cancel operations, switch to an abortive close.
                // Unless the user requested a normal close using Socket.Shutdown.
                if (canceledOperations && !_hasShutdownSend)
                {
                    abortive = true;
                }

                innerSocket.Close(abortive);
            }
#if DEBUG
        }

        catch (Exception exception) when(!ExceptionCheck.IsFatal(exception))
        {
            NetEventSource.Fail(this, $"handle:{handle}, error:{exception}");
            throw;
        }
#endif
        }
Exemplo n.º 22
0
        private static SafeCloseSocket CreateSocket(InnerSafeCloseSocket socket)
        {
            SafeCloseSocket ret = new SafeCloseSocket();

            CreateSocket(socket, ret);

            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Print("SafeCloseSocket#" + LoggingHash.HashString(ret) + "::CreateSocket()");
            }

            return(ret);
        }
Exemplo n.º 23
0
        private static SafeSocketHandle CreateSocket(InnerSafeCloseSocket socket)
        {
            SafeSocketHandle ret = new SafeSocketHandle();

            CreateSocket(socket, ret);

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(null, ret);
            }

            return(ret);
        }
        internal static SafeCloseSocketAndEvent CreateWSASocketWithEvent(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, bool autoReset, bool signaled)
        {
            SafeCloseSocketAndEvent result = new SafeCloseSocketAndEvent();

            CreateSocket(InnerSafeCloseSocket.CreateWSASocket(addressFamily, socketType, protocolType), result);
            if (result.IsInvalid)
            {
                throw new SocketException();
            }

            result._waitHandle = new AutoResetEvent(false);
            CompleteInitialization(result);
            return(result);
        }
Exemplo n.º 25
0
        internal void CloseAsIs()
        {
            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Print(
                    "SafeCloseSocket#" + LoggingHash.HashString(this) + "::CloseAsIs() m_InnerSocket=" +
                    _innerSocket == null ? "null" : LoggingHash.HashString(_innerSocket));
            }

#if DEBUG
            // If this throws it could be very bad.
            try
            {
#endif
            InnerSafeCloseSocket innerSocket = _innerSocket == null ? null : Interlocked.Exchange <InnerSafeCloseSocket>(ref _innerSocket, null);

            Dispose();
            if (innerSocket != null)
            {
                // Wait until it's safe.
                SpinWait sw = new SpinWait();
                while (!_released)
                {
                    sw.SpinOnce();
                }

                // Now free it with blocking.
                innerSocket.BlockingRelease();
            }

            InnerReleaseHandle();
#if DEBUG
        }

        catch (Exception exception)
        {
            if (!ExceptionCheck.IsFatal(exception))
            {
                if (GlobalLog.IsEnabled)
                {
                    GlobalLog.Assert("SafeCloseSocket::CloseAsIs(handle:" + handle.ToString("x") + ")", exception.Message);
                }
                Debug.Fail("SafeCloseSocket::CloseAsIs(handle:" + handle.ToString("x") + ")", exception.Message);
            }
            throw;
        }
#endif
        }
Exemplo n.º 26
0
 internal void AddRef()
 {
     try
     {
         // The inner socket can be closed by CloseAsIs and when SafeHandle runs ReleaseHandle.
         InnerSafeCloseSocket innerSocket = Volatile.Read(ref _innerSocket);
         if (innerSocket != null)
         {
             innerSocket.AddRef();
         }
     }
     catch (Exception e)
     {
         Debug.Fail("SafeSocketHandle.AddRef after inner socket disposed." + e);
     }
 }
Exemplo n.º 27
0
 public void Release()
 {
     try
     {
         // The inner socket can be closed by CloseAsIs and when SafeHandle runs ReleaseHandle.
         InnerSafeCloseSocket innerSocket = Volatile.Read(ref _innerSocket);
         if (innerSocket != null)
         {
             innerSocket.Release();
         }
     }
     catch (Exception e)
     {
         Debug.Assert(false, "SafeCloseSocket.Release after inner socket disposed." + e);
     }
 }
Exemplo n.º 28
0
            public static unsafe InnerSafeCloseSocket Accept(SafeCloseSocket socketHandle, byte[] socketAddress, ref int socketAddressLen, out SocketError errorCode)
            {
                int acceptedFd;

                if (!socketHandle.IsNonBlocking)
                {
                    errorCode = socketHandle.AsyncContext.Accept(socketAddress, ref socketAddressLen, -1, out acceptedFd);
                }
                else
                {
                    SocketPal.TryCompleteAccept(socketHandle, socketAddress, ref socketAddressLen, out acceptedFd, out errorCode);
                }

                var res = new InnerSafeCloseSocket();

                res.SetHandle((IntPtr)acceptedFd);
                return(res);
            }
Exemplo n.º 29
0
            public static unsafe InnerSafeCloseSocket CreateSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, out SocketError errorCode)
            {
                int fd;

                Interop.Error error = Interop.Sys.Socket(addressFamily, socketType, protocolType, &fd);
                if (error == Interop.Error.SUCCESS)
                {
                    Debug.Assert(fd != -1);

                    errorCode = SocketError.Success;

                    // The socket was created successfully; make it non-blocking and enable
                    // IPV6_V6ONLY by default for AF_INET6 sockets.
                    int err = Interop.Sys.Fcntl.SetIsNonBlocking((IntPtr)fd, 1);
                    if (err != 0)
                    {
                        Interop.Sys.Close((IntPtr)fd);
                        fd        = -1;
                        errorCode = SocketError.SocketError;
                    }
                    else if (addressFamily == AddressFamily.InterNetworkV6)
                    {
                        int on = 1;
                        error = Interop.Sys.SetSockOpt(fd, SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, (byte *)&on, sizeof(int));
                        if (error != Interop.Error.SUCCESS)
                        {
                            Interop.Sys.Close((IntPtr)fd);
                            fd        = -1;
                            errorCode = SocketPal.GetSocketErrorForErrorCode(error);
                        }
                    }
                }
                else
                {
                    Debug.Assert(fd == -1);

                    errorCode = SocketPal.GetSocketErrorForErrorCode(error);
                }

                var res = new InnerSafeCloseSocket();

                res.SetHandle((IntPtr)fd);
                return(res);
            }
Exemplo n.º 30
0
        internal void CloseAsIs(bool abortive)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(this, $"_innerSocket={_innerSocket}");
            }

#if DEBUG
            // If this throws it could be very bad.
            try
            {
#endif
            InnerSafeCloseSocket innerSocket = _innerSocket == null ? null : Interlocked.Exchange <InnerSafeCloseSocket>(ref _innerSocket, null);

            Dispose();
            if (innerSocket != null)
            {
                // Wait until it's safe.
                SpinWait sw = new SpinWait();
                while (!_released)
                {
                    // The socket was not released due to the SafeHandle being used.
                    // Try to make those on-going calls return.
                    // On Linux, TryUnblockSocket will unblock current operations but it doesn't prevent
                    // a new one from starting. So we must call TryUnblockSocket multiple times.
                    abortive |= innerSocket.TryUnblockSocket();
                    sw.SpinOnce();
                }

                abortive |= InnerReleaseHandle();

                innerSocket.Close(abortive);
            }
#if DEBUG
        }

        catch (Exception exception) when(!ExceptionCheck.IsFatal(exception))
        {
            NetEventSource.Fail(this, $"handle:{handle}, error:{exception}");
            throw;
        }
#endif
        }
 internal void CloseAsIs()
 {
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
     }
     finally
     {
         InnerSafeCloseSocket socket = (this.m_InnerSocket == null) ? null : Interlocked.Exchange <InnerSafeCloseSocket>(ref this.m_InnerSocket, null);
         base.Close();
         if (socket != null)
         {
             while (!this.m_Released)
             {
                 Thread.SpinWait(1);
             }
             socket.BlockingRelease();
         }
     }
 }
 protected static void CreateSocket(InnerSafeCloseSocket socket, SafeCloseSocket target)
 {
     if ((socket != null) && socket.IsInvalid)
     {
         target.SetHandleAsInvalid();
     }
     else
     {
         bool success = false;
         RuntimeHelpers.PrepareConstrainedRegions();
         try
         {
             socket.DangerousAddRef(ref success);
         }
         catch
         {
             if (success)
             {
                 socket.DangerousRelease();
                 success = false;
             }
         }
         finally
         {
             if (success)
             {
                 target.SetInnerSocket(socket);
                 socket.Close();
             }
             else
             {
                 target.SetHandleAsInvalid();
             }
         }
     }
 }
Exemplo n.º 33
0
 public static InnerSafeCloseSocket CreateSocket(IntPtr fileDescriptor)
 {
     var res = new InnerSafeCloseSocket();
     res.SetHandle(fileDescriptor);
     return res;
 }
Exemplo n.º 34
0
            public static unsafe InnerSafeCloseSocket CreateSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
            {
                int af = SocketPal.GetPlatformAddressFamily(addressFamily);
                int sock = SocketPal.GetPlatformSocketType(socketType);
                int pt = (int)protocolType;

                int fd = Interop.libc.socket(af, sock, pt);
                if (fd != -1)
                {
                    // The socket was created successfully; make it non-blocking and enable
                    // IPV6_V6ONLY by default for AF_INET6 sockets.
                    int err = Interop.Sys.Fcntl.SetIsNonBlocking(fd, 1);
                    if (err != 0)
                    {
                        Interop.Sys.Close(fd);
                        fd = -1;
                    }
                    else if (addressFamily == AddressFamily.InterNetworkV6)
                    {
                        int on = 1;
                        Interop.Error error = Interop.Sys.SetSockOpt(fd, SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, (byte*)&on, sizeof(int));
                        if (error != Interop.Error.SUCCESS)
                        {
                            Interop.Sys.Close(fd);
                            fd = -1;
                        }
                    }
                }

                var res = new InnerSafeCloseSocket();
                res.SetHandle((IntPtr)fd);
                return res;
            }
Exemplo n.º 35
0
        private static SafeCloseSocket CreateSocket(InnerSafeCloseSocket socket)
        {
            SafeCloseSocket ret = new SafeCloseSocket();
            CreateSocket(socket, ret);

            GlobalLog.Print("SafeCloseSocket#" + Logging.HashString(ret) + "::CreateSocket()");

            return ret;
        }
Exemplo n.º 36
0
            public static unsafe InnerSafeCloseSocket CreateSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
            {
                int af = SocketPal.GetPlatformAddressFamily(addressFamily);
                int sock = SocketPal.GetPlatformSocketType(socketType);
                int pt = (int)protocolType;

                int fd = Interop.libc.socket(af, sock, pt);
                if (fd != -1)
                {
                    // The socket was created successfully; make it non-blocking and enable
                    // IPV6_V6ONLY by default for AF_INET6 sockets.
                    int err = Interop.Sys.Fcntl.SetIsNonBlocking(fd, 1);
                    if (err != 0)
                    {
                        Interop.Sys.Close(fd);
                        fd = -1;
                    }

                    if (addressFamily == AddressFamily.InterNetworkV6)
                    {
                        int on = 1;
                        err = Interop.libc.setsockopt(fd, Interop.libc.IPPROTO_IPV6, Interop.libc.IPV6_V6ONLY, &on, (uint)sizeof(int));
                        if (err != 0)
                        {
                            Interop.Sys.Close(fd);
                            fd = -1;
                        }
                    }
                }

                var res = new InnerSafeCloseSocket();
                res.SetHandle((IntPtr)fd);
                return res;
            }
Exemplo n.º 37
0
        private static SafeCloseSocket CreateSocket(InnerSafeCloseSocket socket)
        {
            SafeCloseSocket ret = new SafeCloseSocket();
            CreateSocket(socket, ret);

            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Print("SafeCloseSocket#" + LoggingHash.HashString(ret) + "::CreateSocket()");
            }

            return ret;
        }
Exemplo n.º 38
0
            public static unsafe InnerSafeCloseSocket Accept(SafeCloseSocket socketHandle, byte[] socketAddress, ref int socketAddressLen)
            {
                int acceptedFd;
                if (!socketHandle.IsNonBlocking)
                {
                    socketHandle.AsyncContext.Accept(socketAddress, ref socketAddressLen, -1, out acceptedFd);
                }
                else
                {
                    SocketError unused;
                    SocketPal.TryCompleteAccept(socketHandle.FileDescriptor, socketAddress, ref socketAddressLen, out acceptedFd, out unused);
                }

                var res = new InnerSafeCloseSocket();
                res.SetHandle((IntPtr)acceptedFd);
                return res;
            }
Exemplo n.º 39
0
        protected static void CreateSocket(InnerSafeCloseSocket socket, SafeCloseSocket target)
        {
            if (socket != null && socket.IsInvalid)
            {
                target.SetHandleAsInvalid();
                return;
            }

            bool b = false;
            try
            {
                socket.DangerousAddRef(ref b);
            }
            catch
            {
                if (b)
                {
                    socket.DangerousRelease();
                    b = false;
                }
            }
            finally
            {
                if (b)
                {
                    target.SetInnerSocket(socket);
                    socket.Dispose();
                }
                else
                {
                    target.SetHandleAsInvalid();
                }
            }
        }
Exemplo n.º 40
0
        private static SafeCloseSocket CreateSocket(InnerSafeCloseSocket socket)
        {
            SafeCloseSocket ret = new SafeCloseSocket();
            CreateSocket(socket, ret);

            if (NetEventSource.IsEnabled) NetEventSource.Info(null, ret);

            return ret;
        }
Exemplo n.º 41
0
            public static unsafe InnerSafeCloseSocket CreateSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, out SocketError errorCode)
            {
                int fd;
                Interop.Error error = Interop.Sys.Socket(addressFamily, socketType, protocolType, &fd);
                if (error == Interop.Error.SUCCESS)
                {
                    Debug.Assert(fd != -1);

                    errorCode = SocketError.Success;

                    // The socket was created successfully; make it non-blocking and enable
                    // IPV6_V6ONLY by default for AF_INET6 sockets.
                    int err = Interop.Sys.Fcntl.SetIsNonBlocking(fd, 1);
                    if (err != 0)
                    {
                        Interop.Sys.Close(fd);
                        fd = -1;
                        errorCode = SocketError.SocketError;
                    }
                    else if (addressFamily == AddressFamily.InterNetworkV6)
                    {
                        int on = 1;
                        error = Interop.Sys.SetSockOpt(fd, SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, (byte*)&on, sizeof(int));
                        if (error != Interop.Error.SUCCESS)
                        {
                            Interop.Sys.Close(fd);
                            fd = -1;
                            errorCode = SocketPal.GetSocketErrorForErrorCode(error);
                        }
                    }
                }
                else
                {
                    errorCode = SocketPal.GetSocketErrorForErrorCode(error);
                }

                var res = new InnerSafeCloseSocket();
                res.SetHandle((IntPtr)fd);
                return res;
            }
Exemplo n.º 42
0
            public static unsafe InnerSafeCloseSocket CreateSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, out SocketError errorCode)
            {
                IntPtr fd;
                Interop.Error error = Interop.Sys.Socket(addressFamily, socketType, protocolType, &fd);
                if (error == Interop.Error.SUCCESS)
                {
                    Debug.Assert(fd != (IntPtr)(-1), "fd should not be -1");

                    errorCode = SocketError.Success;

                    // The socket was created successfully; enable IPV6_V6ONLY by default for AF_INET6 sockets.
                    if (addressFamily == AddressFamily.InterNetworkV6)
                    {
                        int on = 1;
                        error = Interop.Sys.SetSockOpt(fd, SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, (byte*)&on, sizeof(int));
                        if (error != Interop.Error.SUCCESS)
                        {
                            Interop.Sys.Close(fd);
                            fd = (IntPtr)(-1);
                            errorCode = SocketPal.GetSocketErrorForErrorCode(error);
                        }
                    }
                }
                else
                {
                    Debug.Assert(fd == (IntPtr)(-1), $"Unexpected fd: {fd}");

                    errorCode = SocketPal.GetSocketErrorForErrorCode(error);
                }

                var res = new InnerSafeCloseSocket();
                res.SetHandle(fd);
                return res;
            }
Exemplo n.º 43
0
 private static SafeCloseSocket CreateSocket(InnerSafeCloseSocket socket)
 {
     SafeCloseSocket ret = new SafeCloseSocket();
     CreateSocket(socket, ret);
     return ret;
 }
Exemplo n.º 44
0
            public static unsafe InnerSafeCloseSocket Accept(SafeCloseSocket socketHandle, byte[] socketAddress, ref int socketAddressLen, out SocketError errorCode)
            {
                IntPtr acceptedFd;
                if (!socketHandle.IsNonBlocking)
                {
                    errorCode = socketHandle.AsyncContext.Accept(socketAddress, ref socketAddressLen, -1, out acceptedFd);
                }
                else
                {
                    SocketPal.TryCompleteAccept(socketHandle, socketAddress, ref socketAddressLen, out acceptedFd, out errorCode);
                }

                var res = new InnerSafeCloseSocket();
                res.SetHandle(acceptedFd);
                return res;
            }
 private void SetInnerSocket(InnerSafeCloseSocket socket)
 {
     this.m_InnerSocket = socket;
     base.SetHandle(socket.DangerousGetHandle());
 }