Ejemplo n.º 1
0
 internal static unsafe extern SocketError WSARecv(
     IntPtr socketHandle,
     WSABuffer *buffer,
     int bufferCount,
     out int bytesTransferred,
     ref SocketFlags socketFlags,
     NativeOverlapped *overlapped,
     IntPtr completionRoutine);
Ejemplo n.º 2
0
 internal static extern unsafe SocketError WSASend(
     SafeHandle socketHandle,
     WSABuffer *buffers,
     int bufferCount,
     out int bytesTransferred,
     SocketFlags socketFlags,
     NativeOverlapped *overlapped,
     IntPtr completionRoutine);
Ejemplo n.º 3
0
 public static unsafe extern uint WSASend(
     [In] IntPtr socket,
     [In] WSABuffer* lpBuffers,
     [In] int buffersCount,
     [Out] out int numberOfBytesSent,
     [In] int dwFlags,
     [In] NativeOverlapped* lpOverlapped,
     [In] IntPtr lpCompletionRoutine);
Ejemplo n.º 4
0
 internal static extern Int32 WSARecv(
     IntPtr socketHandle,
     WSABuffer *buffer,
     Int32 bufferCount,
     out Int32 bytesTransferred,
     ref SocketFlags socketFlags,
     NativeOverlapped *overlapped,
     IntPtr completionRoutine);
Ejemplo n.º 5
0
        // SetUnmanagedStructures
        //
        // Fills in overlapped Structures used in an async overlapped Winsock call.
        // These calls are outside the runtime and are unmanaged code, so we need
        // to prepare specific structures and ints that lie in unmanaged memory
        // since the overlapped calls may complete asynchronously.
        internal void SetUnmanagedStructures(byte[] buffer, int offset, int size, Internals.SocketAddress socketAddress, SocketFlags socketFlags)
        {
            _messageBuffer  = new byte[s_wsaMsgSize];
            _wsaBufferArray = new byte[s_wsaBufferSize];

            bool ipv4, ipv6;

            Socket.GetIPProtocolInformation(((Socket)AsyncObject).AddressFamily, socketAddress, out ipv4, out ipv6);

            // Prepare control buffer.
            if (ipv4)
            {
                _controlBuffer = new byte[s_controlDataSize];
            }
            else if (ipv6)
            {
                _controlBuffer = new byte[s_controlDataIPv6Size];
            }

            // Pin buffers.
            object[] objectsToPin = new object[(_controlBuffer != null) ? 5 : 4];
            objectsToPin[0] = buffer;
            objectsToPin[1] = _messageBuffer;
            objectsToPin[2] = _wsaBufferArray;

            // Prepare socketaddress buffer.
            _socketAddress = socketAddress;
            _socketAddress.CopyAddressSizeIntoBuffer();
            objectsToPin[3] = _socketAddress.Buffer;

            if (_controlBuffer != null)
            {
                objectsToPin[4] = _controlBuffer;
            }

            base.SetUnmanagedStructures(objectsToPin);

            // Prepare data buffer.
            _wsaBuffer          = (WSABuffer *)Marshal.UnsafeAddrOfPinnedArrayElement(_wsaBufferArray, 0);
            _wsaBuffer->Length  = size;
            _wsaBuffer->Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset);


            // Setup structure.
            _message = (Interop.Winsock.WSAMsg *)Marshal.UnsafeAddrOfPinnedArrayElement(_messageBuffer, 0);
            _message->socketAddress = Marshal.UnsafeAddrOfPinnedArrayElement(_socketAddress.Buffer, 0);
            _message->addressLength = (uint)_socketAddress.Size;
            _message->buffers       = Marshal.UnsafeAddrOfPinnedArrayElement(_wsaBufferArray, 0);
            _message->count         = 1;

            if (_controlBuffer != null)
            {
                _message->controlBuffer.Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(_controlBuffer, 0);
                _message->controlBuffer.Length  = _controlBuffer.Length;
            }

            _message->flags = socketFlags;
        }
        //
        // SetUnmanagedStructures -
        // Fills in Overlapped Structures used in an Async Overlapped Winsock call
        //   these calls are outside the runtime and are unmanaged code, so we need
        //   to prepare specific structures and ints that lie in unmanaged memory
        //   since the Overlapped calls can be Async
        //

        internal void SetUnmanagedStructures(byte[] buffer, int offset, int size, SocketAddress socketAddress, SocketFlags socketFlags)
        {
            //ipv4 or ipv6?
            bool ipv4 = ((Socket)(AsyncObject)).AddressFamily == AddressFamily.InterNetwork;
            bool ipv6 = ((Socket)(AsyncObject)).AddressFamily == AddressFamily.InterNetworkV6;

            m_MessageBuffer  = new byte[s_WSAMsgSize];
            m_WSABufferArray = new byte[s_WSABufferSize];

            //prepare control buffer
            if (ipv4)
            {
                m_ControlBuffer = new byte[s_ControlDataSize];
            }
            else if (ipv6)
            {
                m_ControlBuffer = new byte[s_ControlDataIPv6Size];
            }

            //pin buffers
            object[] objectsToPin = new object[(m_ControlBuffer != null)?5:4];
            objectsToPin[0] = buffer;
            objectsToPin[1] = m_MessageBuffer;
            objectsToPin[2] = m_WSABufferArray;

            //prepare socketaddress buffer
            m_SocketAddress = socketAddress;
            m_SocketAddress.CopyAddressSizeIntoBuffer();
            objectsToPin[3] = m_SocketAddress.m_Buffer;

            if (m_ControlBuffer != null)
            {
                objectsToPin[4] = m_ControlBuffer;
            }

            base.SetUnmanagedStructures(objectsToPin);

            //prepare data buffer
            m_WSABuffer          = (WSABuffer *)Marshal.UnsafeAddrOfPinnedArrayElement(m_WSABufferArray, 0);
            m_WSABuffer->Length  = size;
            m_WSABuffer->Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset);


            //setup structure
            m_Message = (UnsafeNclNativeMethods.OSSOCK.WSAMsg *)Marshal.UnsafeAddrOfPinnedArrayElement(m_MessageBuffer, 0);
            m_Message->socketAddress = Marshal.UnsafeAddrOfPinnedArrayElement(m_SocketAddress.m_Buffer, 0);
            m_Message->addressLength = (uint)m_SocketAddress.Size;
            m_Message->buffers       = Marshal.UnsafeAddrOfPinnedArrayElement(m_WSABufferArray, 0);
            m_Message->count         = 1;

            if (m_ControlBuffer != null)
            {
                m_Message->controlBuffer.Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(m_ControlBuffer, 0);
                m_Message->controlBuffer.Length  = m_ControlBuffer.Length;
            }

            m_Message->flags = socketFlags;
        }
        // SetUnmanagedStructures
        //
        // Fills in overlapped Structures used in an async overlapped Winsock call.
        // These calls are outside the runtime and are unmanaged code, so we need
        // to prepare specific structures and ints that lie in unmanaged memory
        // since the overlapped calls may complete asynchronously.
        internal void SetUnmanagedStructures(byte[] buffer, int offset, int size, Internals.SocketAddress socketAddress, SocketFlags socketFlags)
        {
            _messageBuffer = new byte[s_wsaMsgSize];
            _wsaBufferArray = new byte[s_wsaBufferSize];

            bool ipv4, ipv6;
            Socket.GetIPProtocolInformation(((Socket)AsyncObject).AddressFamily, socketAddress, out ipv4, out ipv6);

            // Prepare control buffer.
            if (ipv4)
            {
                _controlBuffer = new byte[s_controlDataSize];
            }
            else if (ipv6)
            {
                _controlBuffer = new byte[s_controlDataIPv6Size];
            }

            // Pin buffers.
            object[] objectsToPin = new object[(_controlBuffer != null) ? 5 : 4];
            objectsToPin[0] = buffer;
            objectsToPin[1] = _messageBuffer;
            objectsToPin[2] = _wsaBufferArray;

            // Prepare socketaddress buffer.
            _socketAddress = socketAddress;
            _socketAddress.CopyAddressSizeIntoBuffer();
            objectsToPin[3] = _socketAddress.Buffer;

            if (_controlBuffer != null)
            {
                objectsToPin[4] = _controlBuffer;
            }

            base.SetUnmanagedStructures(objectsToPin);

            // Prepare data buffer.
            _wsaBuffer = (WSABuffer*)Marshal.UnsafeAddrOfPinnedArrayElement(_wsaBufferArray, 0);
            _wsaBuffer->Length = size;
            _wsaBuffer->Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset);


            // Setup structure.
            _message = (Interop.Winsock.WSAMsg*)Marshal.UnsafeAddrOfPinnedArrayElement(_messageBuffer, 0);
            _message->socketAddress = Marshal.UnsafeAddrOfPinnedArrayElement(_socketAddress.Buffer, 0);
            _message->addressLength = (uint)_socketAddress.Size;
            _message->buffers = Marshal.UnsafeAddrOfPinnedArrayElement(_wsaBufferArray, 0);
            _message->count = 1;

            if (_controlBuffer != null)
            {
                _message->controlBuffer.Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(_controlBuffer, 0);
                _message->controlBuffer.Length = _controlBuffer.Length;
            }

            _message->flags = socketFlags;
        }
Ejemplo n.º 8
0
 private static extern unsafe SocketError WSASendTo(
     SafeHandle socketHandle,
     WSABuffer *buffers,
     int bufferCount,
     out int bytesTransferred,
     SocketFlags socketFlags,
     IntPtr socketAddress,
     int socketAddressSize,
     NativeOverlapped *overlapped,
     IntPtr completionRoutine);
Ejemplo n.º 9
0
 private static extern unsafe SocketError WSARecvFrom(
     SafeHandle socketHandle,
     WSABuffer *buffers,
     int bufferCount,
     out int bytesTransferred,
     ref SocketFlags socketFlags,
     IntPtr socketAddressPointer,
     IntPtr socketAddressSizePointer,
     NativeOverlapped *overlapped,
     IntPtr completionRoutine);
Ejemplo n.º 10
0
 internal static unsafe extern SocketError WSARecvFrom(
     SafeCloseSocket socketHandle,
     WSABuffer *buffers,
     int bufferCount,
     out int bytesTransferred,
     ref SocketFlags socketFlags,
     IntPtr socketAddressPointer,
     IntPtr socketAddressSizePointer,
     SafeNativeOverlapped overlapped,
     IntPtr completionRoutine);
Ejemplo n.º 11
0
        private Int32 MyWsaRecv(
            IntPtr socketHandle,
            WSABuffer *buffer,
            Int32 bufferCount,
            out Int32 bytesTransferred,
            ref SocketFlags socketFlags,
            NativeOverlapped *overlapped,
            IntPtr completionRoutine)
        {
            // this.HookClient.Log("WSARecv");

            bytesTransferred = 0;
            return(0);
            // return WSARecv(socketHandle, buffer, bufferCount, out bytesTransferred, ref socketFlags, overlapped, completionRoutine);
        }
Ejemplo n.º 12
0
        private unsafe SocketError Detour_WsaSend(
            IntPtr socketHandle,
            WSABuffer *buffers,
            int bufferCount,
            out int bytesTransferred,
            SocketFlags socketFlags,
            NativeOverlapped *overlapped,
            IntPtr completionRoutine)
        {
            SocketHook This = (SocketHook)HookRuntimeInfo.Callback;

            if (This != null)
            {
                // Increment WSASend send count
                This._wsaSendBufferCount++;
            }
            return(Interop.Winsock.WSASend(socketHandle, buffers, bufferCount, out bytesTransferred, socketFlags, overlapped, completionRoutine));
        }
Ejemplo n.º 13
0
        internal unsafe void SetUnmanagedStructures(byte[] buffer, int offset, int size, System.Net.SocketAddress socketAddress, SocketFlags socketFlags)
        {
            bool flag  = ((Socket)base.AsyncObject).AddressFamily == AddressFamily.InterNetwork;
            bool flag2 = ((Socket)base.AsyncObject).AddressFamily == AddressFamily.InterNetworkV6;

            this.m_MessageBuffer  = new byte[s_WSAMsgSize];
            this.m_WSABufferArray = new byte[s_WSABufferSize];
            if (flag)
            {
                this.m_ControlBuffer = new byte[s_ControlDataSize];
            }
            else if (flag2)
            {
                this.m_ControlBuffer = new byte[s_ControlDataIPv6Size];
            }
            object[] objectsToPin = new object[(this.m_ControlBuffer != null) ? 5 : 4];
            objectsToPin[0]      = buffer;
            objectsToPin[1]      = this.m_MessageBuffer;
            objectsToPin[2]      = this.m_WSABufferArray;
            this.m_SocketAddress = socketAddress;
            this.m_SocketAddress.CopyAddressSizeIntoBuffer();
            objectsToPin[3] = this.m_SocketAddress.m_Buffer;
            if (this.m_ControlBuffer != null)
            {
                objectsToPin[4] = this.m_ControlBuffer;
            }
            base.SetUnmanagedStructures(objectsToPin);
            this.m_WSABuffer             = (WSABuffer *)Marshal.UnsafeAddrOfPinnedArrayElement(this.m_WSABufferArray, 0);
            this.m_WSABuffer.Length      = size;
            this.m_WSABuffer.Pointer     = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset);
            this.m_Message               = (UnsafeNclNativeMethods.OSSOCK.WSAMsg *)Marshal.UnsafeAddrOfPinnedArrayElement(this.m_MessageBuffer, 0);
            this.m_Message.socketAddress = Marshal.UnsafeAddrOfPinnedArrayElement(this.m_SocketAddress.m_Buffer, 0);
            this.m_Message.addressLength = (uint)this.m_SocketAddress.Size;
            this.m_Message.buffers       = Marshal.UnsafeAddrOfPinnedArrayElement(this.m_WSABufferArray, 0);
            this.m_Message.count         = 1;
            if (this.m_ControlBuffer != null)
            {
                this.m_Message.controlBuffer.Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(this.m_ControlBuffer, 0);
                this.m_Message.controlBuffer.Length  = this.m_ControlBuffer.Length;
            }
            this.m_Message.flags = socketFlags;
        }
Ejemplo n.º 14
0
 internal static unsafe extern int WSARecv
 (
     IntPtr handle, WSABuffer *buffers, int bufferCount, out int bytesTransferred,
     ref int socketFlags,
     NativeOverlapped *nativeOverlapped,
     IntPtr completionRoutine);
 internal unsafe void SetUnmanagedStructures(byte[] buffer, int offset, int size, System.Net.SocketAddress socketAddress, SocketFlags socketFlags)
 {
     bool flag = ((Socket) base.AsyncObject).AddressFamily == AddressFamily.InterNetwork;
     bool flag2 = ((Socket) base.AsyncObject).AddressFamily == AddressFamily.InterNetworkV6;
     this.m_MessageBuffer = new byte[s_WSAMsgSize];
     this.m_WSABufferArray = new byte[s_WSABufferSize];
     if (flag)
     {
         this.m_ControlBuffer = new byte[s_ControlDataSize];
     }
     else if (flag2)
     {
         this.m_ControlBuffer = new byte[s_ControlDataIPv6Size];
     }
     object[] objectsToPin = new object[(this.m_ControlBuffer != null) ? 5 : 4];
     objectsToPin[0] = buffer;
     objectsToPin[1] = this.m_MessageBuffer;
     objectsToPin[2] = this.m_WSABufferArray;
     this.m_SocketAddress = socketAddress;
     this.m_SocketAddress.CopyAddressSizeIntoBuffer();
     objectsToPin[3] = this.m_SocketAddress.m_Buffer;
     if (this.m_ControlBuffer != null)
     {
         objectsToPin[4] = this.m_ControlBuffer;
     }
     base.SetUnmanagedStructures(objectsToPin);
     this.m_WSABuffer = (WSABuffer*) Marshal.UnsafeAddrOfPinnedArrayElement(this.m_WSABufferArray, 0);
     this.m_WSABuffer.Length = size;
     this.m_WSABuffer.Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset);
     this.m_Message = (UnsafeNclNativeMethods.OSSOCK.WSAMsg*) Marshal.UnsafeAddrOfPinnedArrayElement(this.m_MessageBuffer, 0);
     this.m_Message.socketAddress = Marshal.UnsafeAddrOfPinnedArrayElement(this.m_SocketAddress.m_Buffer, 0);
     this.m_Message.addressLength = (uint) this.m_SocketAddress.Size;
     this.m_Message.buffers = Marshal.UnsafeAddrOfPinnedArrayElement(this.m_WSABufferArray, 0);
     this.m_Message.count = 1;
     if (this.m_ControlBuffer != null)
     {
         this.m_Message.controlBuffer.Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(this.m_ControlBuffer, 0);
         this.m_Message.controlBuffer.Length = this.m_ControlBuffer.Length;
     }
     this.m_Message.flags = socketFlags;
 }
        //
        // SetUnmanagedStructures -
        // Fills in Overlapped Structures used in an Async Overlapped Winsock call
        //   these calls are outside the runtime and are unmanaged code, so we need
        //   to prepare specific structures and ints that lie in unmanaged memory
        //   since the Overlapped calls can be Async
        //

        internal void SetUnmanagedStructures(byte[] buffer, int offset, int size, SocketAddress socketAddress, SocketFlags socketFlags)
        {
            m_MessageBuffer = new byte[s_WSAMsgSize];
            m_WSABufferArray = new byte[s_WSABufferSize];

            //ipv4 or ipv6?
            IPAddress ipAddress = (socketAddress.Family == AddressFamily.InterNetworkV6 
                ? socketAddress.GetIPAddress() : null);
            bool ipv4 = (((Socket)AsyncObject).AddressFamily == AddressFamily.InterNetwork
                || (ipAddress != null && ipAddress.IsIPv4MappedToIPv6)); // DualMode
            bool ipv6 = ((Socket)AsyncObject).AddressFamily == AddressFamily.InterNetworkV6;

            //prepare control buffer
            if (ipv4) {
                m_ControlBuffer = new byte[s_ControlDataSize];
            }
            else if (ipv6) {
                m_ControlBuffer = new byte[s_ControlDataIPv6Size];
            }

            //pin buffers
            object[] objectsToPin = new object[(m_ControlBuffer != null)?5:4];
            objectsToPin[0] = buffer;
            objectsToPin[1] = m_MessageBuffer;
            objectsToPin[2] = m_WSABufferArray;
            
            //prepare socketaddress buffer
            m_SocketAddress = socketAddress;
            m_SocketAddress.CopyAddressSizeIntoBuffer();
            objectsToPin[3] = m_SocketAddress.m_Buffer;
                
            if(m_ControlBuffer != null){
                objectsToPin[4] = m_ControlBuffer;
            }

            base.SetUnmanagedStructures(objectsToPin);

            //prepare data buffer
            m_WSABuffer = (WSABuffer*) Marshal.UnsafeAddrOfPinnedArrayElement(m_WSABufferArray, 0);
            m_WSABuffer->Length = size;
            m_WSABuffer->Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset);


            //setup structure
            m_Message = (UnsafeNclNativeMethods.OSSOCK.WSAMsg*) Marshal.UnsafeAddrOfPinnedArrayElement(m_MessageBuffer, 0);
            m_Message->socketAddress = Marshal.UnsafeAddrOfPinnedArrayElement(m_SocketAddress.m_Buffer,0);
            m_Message->addressLength = (uint)m_SocketAddress.Size;
            m_Message->buffers = Marshal.UnsafeAddrOfPinnedArrayElement(m_WSABufferArray,0);
            m_Message->count = 1;

            if(m_ControlBuffer != null){
                m_Message->controlBuffer.Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(m_ControlBuffer, 0);
                m_Message->controlBuffer.Length = m_ControlBuffer.Length;
            }

            m_Message->flags = socketFlags;
        }