GetIPProtocolInformation() static private method

static private GetIPProtocolInformation ( AddressFamily addressFamily, Internals socketAddress, bool &isIPv4, bool &isIPv6 ) : void
addressFamily AddressFamily
socketAddress Internals
isIPv4 bool
isIPv6 bool
return void
コード例 #1
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;
        }
コード例 #2
0
        internal unsafe SocketError DoOperationReceiveMessageFrom(Socket socket, SafeCloseSocket handle, out int bytesTransferred)
        {
            bool isIPv4, isIPv6;

            Socket.GetIPProtocolInformation(socket.AddressFamily, _socketAddress, out isIPv4, out isIPv6);

            bytesTransferred = 0;
            return(handle.AsyncContext.ReceiveMessageFromAsync(_buffer, _offset, _count, _socketFlags, _socketAddress.Buffer, _socketAddress.Size, isIPv4, isIPv6, ReceiveMessageFromCompletionCallback));
        }
コード例 #3
0
        internal unsafe SocketError DoOperationReceiveMessageFrom(Socket socket, SafeCloseSocket handle)
        {
            bool isIPv4, isIPv6;

            Socket.GetIPProtocolInformation(socket.AddressFamily, _socketAddress, out isIPv4, out isIPv6);

            int                 socketAddressSize = _socketAddress.Size;
            int                 bytesReceived;
            SocketFlags         receivedFlags;
            IPPacketInformation ipPacketInformation;
            SocketError         socketError = handle.AsyncContext.ReceiveMessageFromAsync(_buffer, _bufferListInternal, _offset, _count, _socketFlags, _socketAddress.Buffer, ref socketAddressSize, isIPv4, isIPv6, out bytesReceived, out receivedFlags, out ipPacketInformation, ReceiveMessageFromCompletionCallback);

            if (socketError != SocketError.IOPending)
            {
                CompleteReceiveMessageFromOperation(bytesReceived, _socketAddress.Buffer, socketAddressSize, receivedFlags, ipPacketInformation, socketError);
                FinishOperationSync(socketError, bytesReceived, receivedFlags);
            }
            return(socketError);
        }
コード例 #4
0
        public static unsafe SocketError ReceiveMessageFrom(Socket socket, SafeCloseSocket handle, byte[] buffer, int offset, int size, ref SocketFlags socketFlags, Internals.SocketAddress socketAddress, out Internals.SocketAddress receiveAddress, out IPPacketInformation ipPacketInformation, out int bytesTransferred)
        {
            bool ipv4, ipv6;

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

            bytesTransferred    = 0;
            receiveAddress      = socketAddress;
            ipPacketInformation = default(IPPacketInformation);

            fixed(byte *ptrBuffer = buffer)
            fixed(byte *ptrSocketAddress = socketAddress.Buffer)
            {
                Interop.Winsock.WSAMsg wsaMsg;
                wsaMsg.socketAddress = (IntPtr)ptrSocketAddress;
                wsaMsg.addressLength = (uint)socketAddress.Size;
                wsaMsg.flags         = socketFlags;

                WSABuffer wsaBuffer;

                wsaBuffer.Length  = size;
                wsaBuffer.Pointer = (IntPtr)(ptrBuffer + offset);
                wsaMsg.buffers    = (IntPtr)(&wsaBuffer);
                wsaMsg.count      = 1;

                if (ipv4)
                {
                    Interop.Winsock.ControlData controlBuffer;
                    wsaMsg.controlBuffer.Pointer = (IntPtr)(&controlBuffer);
                    wsaMsg.controlBuffer.Length  = sizeof(Interop.Winsock.ControlData);

                    if (socket.WSARecvMsgBlocking(
                            handle.DangerousGetHandle(),
                            (IntPtr)(&wsaMsg),
                            out bytesTransferred,
                            IntPtr.Zero,
                            IntPtr.Zero) == SocketError.SocketError)
                    {
                        return(GetLastSocketError());
                    }

                    ipPacketInformation = GetIPPacketInformation(&controlBuffer);
                }
                else if (ipv6)
                {
                    Interop.Winsock.ControlDataIPv6 controlBuffer;
                    wsaMsg.controlBuffer.Pointer = (IntPtr)(&controlBuffer);
                    wsaMsg.controlBuffer.Length  = sizeof(Interop.Winsock.ControlDataIPv6);

                    if (socket.WSARecvMsgBlocking(
                            handle.DangerousGetHandle(),
                            (IntPtr)(&wsaMsg),
                            out bytesTransferred,
                            IntPtr.Zero,
                            IntPtr.Zero) == SocketError.SocketError)
                    {
                        return(GetLastSocketError());
                    }

                    ipPacketInformation = GetIPPacketInformation(&controlBuffer);
                }
                else
                {
                    wsaMsg.controlBuffer.Pointer = IntPtr.Zero;
                    wsaMsg.controlBuffer.Length  = 0;

                    if (socket.WSARecvMsgBlocking(
                            handle.DangerousGetHandle(),
                            (IntPtr)(&wsaMsg),
                            out bytesTransferred,
                            IntPtr.Zero,
                            IntPtr.Zero) == SocketError.SocketError)
                    {
                        return(GetLastSocketError());
                    }
                }

                socketFlags = wsaMsg.flags;
            }

            return(SocketError.Success);
        }