コード例 #1
0
        public unsafe void BlockSource(IPAddress groupAdress, IPAddress sourceAdress, uint interfaceIndex = 0)
        {
            if (groupAdress.AddressFamily != sourceAdress.AddressFamily)
                throw new ArgumentException("Address family must be the same");

            var adress = groupAdress.GetAddressBytes();
            var sourceadress = groupAdress.GetAddressBytes();

            if (groupAdress.AddressFamily == AddressFamily.InterNetwork)
            {
                ip_mreq_source value = new ip_mreq_source();
                value.imr_interface.s_b4 = (byte)interfaceIndex;
                fixed (byte* a = adress)
                    Unsafe.CopyBlock(value.imr_multiaddr.Address, a, (uint)adress.Length);

                fixed (byte* a = sourceadress)
                    Unsafe.CopyBlock(value.imr_multiaddr.Address, a, (uint)sourceadress.Length);

                if (SetSocketOption(IPPROTO_IP_SocketOptions.IP_BLOCK_SOURCE, (void*)&value, Marshal.SizeOf<ip_mreq_source>()) != 0)
                    WinSock.ThrowLastWSAError();
            }
            else if (groupAdress.AddressFamily == AddressFamily.InterNetworkV6)
            {

                //ipv6_mreq value = new ipv6_mreq();
                //value.ipv6mr_interface = interfaceIndex;
                //fixed (byte* a = adress)
                //    Unsafe.CopyBlock(value.ipv6mr_multiaddr.Address, a, (uint)adress.Length);

                //if (SetSocketOption(IPPROTO_IPV6_SocketOptions., (void*)&value, Marshal.SizeOf<ipv6_mreq>()) != 0)
                //    WinSock.ThrowLastWSAError();
            }
        }
コード例 #2
0
        public unsafe void UnBlockSource(IPAddress groupAdress, IPAddress sourceAdress, uint interfaceIndex = 0)
        {
            if (groupAdress.AddressFamily != sourceAdress.AddressFamily)
            {
                throw new ArgumentException("Address family must be the same");
            }

            var adress       = groupAdress.GetAddressBytes();
            var sourceadress = groupAdress.GetAddressBytes();

            if (groupAdress.AddressFamily == AddressFamily.InterNetwork)
            {
                ip_mreq_source value = new ip_mreq_source();
                value.imr_interface.s_b4 = (byte)interfaceIndex;

                fixed(byte *a = adress)
                Unsafe.CopyBlock(value.imr_multiaddr.Address, a, (uint)adress.Length);

                fixed(byte *a = sourceadress)
                Unsafe.CopyBlock(value.imr_multiaddr.Address, a, (uint)sourceadress.Length);

                if (SetSocketOption(IPPROTO_IP_SocketOptions.IP_BLOCK_SOURCE, (void *)&value, Marshal.SizeOf <ip_mreq_source>()) != 0)
                {
                    WinSock.ThrowLastWSAError();
                }
            }
            else if (groupAdress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                //ipv6_mreq value = new ipv6_mreq();
                //value.ipv6mr_interface = interfaceIndex;
                //fixed (byte* a = adress)
                //    Unsafe.CopyBlock(value.ipv6mr_multiaddr.Address, a, (uint)adress.Length);

                //if (SetSocketOption(IPPROTO_IPV6_SocketOptions., (void*)&value, Marshal.SizeOf<ipv6_mreq>()) != 0)
                //    WinSock.ThrowLastWSAError();
            }
        }