Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 4
0
        public void Port_Get_Set_Throws()
        {
            var buffer = new byte[1];

            Assert.ThrowsAny <Exception>(() => SocketAddressPal.SetPort(buffer, 0));
            Assert.ThrowsAny <Exception>(() => SocketAddressPal.GetPort(buffer));
        }
Ejemplo n.º 5
0
        public void Port_Get_Set_Throws_IPv6()
        {
            var buffer = new byte[2];

            SocketAddressPal.SetAddressFamily(buffer, AddressFamily.InterNetworkV6);
            Assert.ThrowsAny <Exception>(() => SocketAddressPal.SetPort(buffer, 0));
            Assert.ThrowsAny <Exception>(() => SocketAddressPal.GetPort(buffer));
        }
Ejemplo n.º 6
0
        public void Port_Get_Set_IPv6(ushort port)
        {
            var buffer = new byte[SocketAddressPal.IPv6AddressSize];

            SocketAddressPal.SetAddressFamily(buffer, AddressFamily.InterNetworkV6);
            SocketAddressPal.SetPort(buffer, port);
            Assert.Equal(port, SocketAddressPal.GetPort(buffer));
        }
Ejemplo n.º 7
0
        public void IPv4AddressAndPort_Get_Set(uint address, ushort port)
        {
            var buffer = new byte[SocketAddressPal.IPv4AddressSize];

            SocketAddressPal.SetAddressFamily(buffer, AddressFamily.InterNetwork);
            SocketAddressPal.SetIPv4Address(buffer, address);
            SocketAddressPal.SetPort(buffer, port);

            Assert.Equal(address, SocketAddressPal.GetIPv4Address(buffer));
            Assert.Equal(port, SocketAddressPal.GetPort(buffer));
        }
Ejemplo n.º 8
0
        public void IPv6AddressAndPort_Get_Set(byte[] address, uint scope, ushort port)
        {
            var buffer = new byte[SocketAddressPal.IPv6AddressSize];

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

            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);
            Assert.Equal(port, SocketAddressPal.GetPort(buffer));
        }
Ejemplo n.º 9
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);
            }
        }
Ejemplo n.º 10
0
 internal SocketAddress(IPAddress ipaddress, int port)
     : this(ipaddress)
 {
     SocketAddressPal.SetPort(Buffer, unchecked ((ushort)port));
 }