internal IPAddress MarshalIPAddress() { // Determine the address family used to create the IPAddress. AddressFamily family = (addressLength > Internals.SocketAddress.IPv4AddressSize) ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; Internals.SocketAddress sockAddress = new Internals.SocketAddress(family, addressLength); Marshal.Copy(address, sockAddress.Buffer, 0, addressLength); return sockAddress.GetIPAddress(); }
internal IPAddress MarshalIPAddress() { // Determine the address family used to create the IPAddress. AddressFamily family = (addressLength > Internals.SocketAddress.IPv4AddressSize) ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; Internals.SocketAddress sockAddress = new Internals.SocketAddress(family, addressLength); Marshal.Copy(address, sockAddress.Buffer, 0, addressLength); return(sockAddress.GetIPAddress()); }
// A socketAddress must always be the result of remoteEP.Serialize(). private Internals.SocketAddress CheckCacheRemote(ref EndPoint remoteEP, bool isOverwrite) { IPEndPoint ipSnapshot = remoteEP as IPEndPoint; if (ipSnapshot != null) { // Snapshot to avoid external tampering and malicious derivations if IPEndPoint. ipSnapshot = ipSnapshot.Snapshot(); // DualMode: Do the security check on the user input address, but return an IPEndPoint // mapped to an IPv6 address. remoteEP = RemapIPEndPoint(ipSnapshot); } // This doesn't use SnapshotAndSerialize() because we need the ipSnapshot later. Internals.SocketAddress socketAddress = CallSerializeCheckDnsEndPoint(remoteEP); // We remember the first peer with which we have communicated. Internals.SocketAddress permittedRemoteAddress = _permittedRemoteAddress; if (permittedRemoteAddress != null && permittedRemoteAddress.Equals(socketAddress)) { return permittedRemoteAddress; } // Cache only the first peer with which we communicated. if (_permittedRemoteAddress == null || isOverwrite) { _permittedRemoteAddress = socketAddress; } return socketAddress; }
// Cleans up any existing Overlapped object and related state variables. private void FreeOverlapped(bool checkForShutdown) { if (!checkForShutdown || !Environment.HasShutdownStarted) { // Free the overlapped object. if (_ptrNativeOverlapped != null && !_ptrNativeOverlapped.IsInvalid) { _ptrNativeOverlapped.Dispose(); _ptrNativeOverlapped = null; } // Free the preallocated overlapped object. This in turn will unpin // any pinned buffers. if (_preAllocatedOverlapped != null) { _preAllocatedOverlapped.Dispose(); _preAllocatedOverlapped = null; _pinState = PinState.None; _pinnedAcceptBuffer = null; _pinnedSingleBuffer = null; _pinnedSingleBufferOffset = 0; _pinnedSingleBufferCount = 0; } // Free any allocated GCHandles. if (_socketAddressGCHandle.IsAllocated) { _socketAddressGCHandle.Free(); _pinnedSocketAddress = null; } if (_wsaMessageBufferGCHandle.IsAllocated) { _wsaMessageBufferGCHandle.Free(); _ptrWSAMessageBuffer = IntPtr.Zero; } if (_wsaRecvMsgWSABufferArrayGCHandle.IsAllocated) { _wsaRecvMsgWSABufferArrayGCHandle.Free(); _ptrWSARecvMsgWSABufferArray = IntPtr.Zero; } if (_controlBufferGCHandle.IsAllocated) { _controlBufferGCHandle.Free(); _ptrControlBuffer = IntPtr.Zero; } } }
// Ensures appropriate SocketAddress buffer is pinned. private void PinSocketAddressBuffer() { // Check if already pinned. if (_pinnedSocketAddress == _socketAddress) { return; } // Unpin any existing. if (_socketAddressGCHandle.IsAllocated) { _socketAddressGCHandle.Free(); } // Pin down the new one. _socketAddressGCHandle = GCHandle.Alloc(_socketAddress.Buffer, GCHandleType.Pinned); _socketAddress.CopyAddressSizeIntoBuffer(); _ptrSocketAddressBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(_socketAddress.Buffer, 0); _ptrSocketAddressBufferSize = Marshal.UnsafeAddrOfPinnedArrayElement(_socketAddress.Buffer, _socketAddress.GetAddressSizeOffset()); _pinnedSocketAddress = _socketAddress; }