Exemplo n.º 1
0
        internal IPAddress MarshalIPAddress()
        {
            // Determine the address family used to create the IPAddress
            AddressFamily family = (addressLength > SocketAddress.IPv4AddressSize)
                ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork;
            SocketAddress sockAddress = new SocketAddress(family, addressLength);

            Marshal.Copy(address, sockAddress.m_Buffer, 0, addressLength);

            return(sockAddress.GetIPAddress());
        }
        //
        // 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;
        }