Exemple #1
0
        internal SocketAddress(IPAddress ipAddress)
            : this(ipAddress.AddressFamily,
                   ((ipAddress.AddressFamily == AddressFamily.InterNetwork) ? IPv4AddressSize : IPv6AddressSize))
        {
            // No Port.
            SocketAddressPal.SetPort(Buffer, 0);

            if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                Span <byte> addressBytes = stackalloc byte[IPAddressParserStatics.IPv6AddressBytes];
                ipAddress.TryWriteBytes(addressBytes, out int bytesWritten);
                Debug.Assert(bytesWritten == IPAddressParserStatics.IPv6AddressBytes);

                SocketAddressPal.SetIPv6Address(Buffer, addressBytes, (uint)ipAddress.ScopeId);
            }
            else
            {
#pragma warning disable CS0618 // using Obsolete Address API because it's the more efficient option in this case
                uint address = unchecked ((uint)ipAddress.Address);
#pragma warning restore CS0618

                Debug.Assert(ipAddress.AddressFamily == AddressFamily.InterNetwork);
                SocketAddressPal.SetIPv4Address(Buffer, address);
            }
        }
Exemple #2
0
        internal SocketAddress(IPAddress ipAddress)
            : this(ipAddress.AddressFamily,
                   ((ipAddress.AddressFamily == AddressFamily.InterNetwork) ? IPv4AddressSize : IPv6AddressSize))
        {
            // No Port.
            SocketAddressPal.SetPort(Buffer, 0);

            if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                Span <byte> addressBytes;
                unsafe
                {
                    // TODO https://github.com/dotnet/roslyn/issues/17287: Clean up once we can stackalloc into a span
                    byte *mem = stackalloc byte[IPAddressParserStatics.IPv6AddressBytes];
                    addressBytes = new Span <byte>(mem, IPAddressParserStatics.IPv6AddressBytes);
                }
                ipAddress.TryWriteBytes(addressBytes, out int bytesWritten);
                Debug.Assert(bytesWritten == IPAddressParserStatics.IPv6AddressBytes);

                SocketAddressPal.SetIPv6Address(Buffer, addressBytes, (uint)ipAddress.ScopeId);
            }
            else
            {
#pragma warning disable CS0618 // using Obsolete Address API because it's the more efficient option in this case
                uint address = unchecked ((uint)ipAddress.Address);
#pragma warning restore CS0618

                Debug.Assert(ipAddress.AddressFamily == AddressFamily.InterNetwork);
                SocketAddressPal.SetIPv4Address(Buffer, address);
            }
        }
Exemple #3
0
        internal SocketAddress(IPAddress ipAddress)
            : this(ipAddress.AddressFamily,
                   ((ipAddress.AddressFamily == AddressFamily.InterNetwork) ? IPv4AddressSize : IPv6AddressSize))
        {
            // No Port.
            SocketAddressPal.SetPort(Buffer, 0);

            if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                SocketAddressPal.SetIPv6Address(Buffer, ipAddress.GetAddressBytes(), (uint)ipAddress.ScopeId);
            }
            else
            {
#if SYSTEM_NET_PRIMITIVES_DLL
                uint address = unchecked ((uint)ipAddress.Address);
#else
                byte[] ipAddressBytes = ipAddress.GetAddressBytes();
                Debug.Assert(ipAddressBytes.Length == 4);
                uint address = ipAddressBytes.NetworkBytesToNetworkUInt32(0);
#endif

                Debug.Assert(ipAddress.AddressFamily == AddressFamily.InterNetwork);
                SocketAddressPal.SetIPv4Address(Buffer, address);
            }
        }
Exemple #4
0
        public void IPv6Address_Get_Set_Throws()
        {
            uint unused;
            var  buffer = new byte[4];

            SocketAddressPal.SetAddressFamily(buffer, AddressFamily.InterNetworkV6);
            Assert.ThrowsAny <Exception>(() => SocketAddressPal.SetIPv6Address(buffer, new byte[16], 0));
            Assert.ThrowsAny <Exception>(() => SocketAddressPal.GetIPv6Address(buffer, new byte[16], out unused));
        }
Exemple #5
0
        public void IPv6Address_Get_Set(byte[] address, uint scope)
        {
            var buffer = new byte[SocketAddressPal.IPv6AddressSize];

            SocketAddressPal.SetAddressFamily(buffer, AddressFamily.InterNetworkV6);
            SocketAddressPal.SetIPv6Address(buffer, address, scope);

            var  actualAddress = new byte[address.Length];
            uint actualScope;

            SocketAddressPal.GetIPv6Address(buffer, actualAddress, out actualScope);

            for (var i = 0; i < actualAddress.Length; i++)
            {
                Assert.Equal(address[i], actualAddress[i]);
            }

            Assert.Equal(scope, actualScope);
        }
Exemple #6
0
        internal SocketAddress(IPAddress ipAddress)
            : this(ipAddress.AddressFamily,
                   ((ipAddress.AddressFamily == AddressFamily.InterNetwork) ? IPv4AddressSize : IPv6AddressSize))
        {
            // No Port.
            SocketAddressPal.SetPort(Buffer, 0);

            if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                SocketAddressPal.SetIPv6Address(Buffer, ipAddress.GetAddressBytes(), (uint)ipAddress.ScopeId);
            }
            else
            {
#pragma warning disable CS0618 // using Obsolete Address API because it's the more efficient option in this case
                uint address = unchecked ((uint)ipAddress.Address);
#pragma warning restore CS0618

                Debug.Assert(ipAddress.AddressFamily == AddressFamily.InterNetwork);
                SocketAddressPal.SetIPv4Address(Buffer, address);
            }
        }