private static unsafe int ReceiveMessageFrom(SafeCloseSocket socket, SocketFlags flags, byte[] buffer, int offset, int count, byte[] socketAddress, ref int socketAddressLen, bool isIPv4, bool isIPv6, out SocketFlags receivedFlags, out IPPacketInformation ipPacketInformation, out Interop.Error errno)
{
Debug.Assert(socketAddress != null, "Expected non-null socketAddress");
int cmsgBufferLen = Interop.Sys.GetControlMessageBufferSize(isIPv4, isIPv6);
var cmsgBuffer = stackalloc byte[cmsgBufferLen];
int sockAddrLen = socketAddressLen;
Interop.Sys.MessageHeader messageHeader;
long received;
fixed (byte* rawSocketAddress = socketAddress)
fixed (byte* b = buffer)
{
var sockAddr = (byte*)rawSocketAddress;
var iov = new Interop.Sys.IOVector {
Base = &b[offset],
Count = (UIntPtr)count
};
messageHeader = new Interop.Sys.MessageHeader {
SocketAddress = sockAddr,
SocketAddressLen = sockAddrLen,
IOVectors = &iov,
IOVectorCount = 1,
ControlBuffer = cmsgBuffer,
ControlBufferLen = cmsgBufferLen
};
errno = Interop.Sys.ReceiveMessage(socket, &messageHeader, flags, &received);
receivedFlags = messageHeader.Flags;
sockAddrLen = messageHeader.SocketAddressLen;
}
ipPacketInformation = GetIPPacketInformation(&messageHeader, isIPv4, isIPv6);
if (errno != Interop.Error.SUCCESS)
{
return -1;
}
socketAddressLen = sockAddrLen;
return checked((int)received);
}