Example #1
0
        public static int SetNetworkInterfaceState(nl_sock *socket, int index, bool up)
        {
            nl_cache *cache;
            int       x;

            if ((x = LibNLRoute3.rtnl_link_alloc_cache(socket, AddressFamily.INET, &cache)) < 0)
            {
                throw new NetlinkSocketException(x);
            }
            rtnl_link *original = LibNLRoute3.rtnl_link_get(cache, index);

            rtnl_link *changed = (rtnl_link *)LibNL3.nl_object_clone((nl_object *)original);

            if (up)
            {
                LibNLRoute3.rtnl_link_set_flags(changed, NLInterfaceFlags.UP);
            }
            else
            {
                LibNLRoute3.rtnl_link_unset_flags(changed, NLInterfaceFlags.UP);
            }

            int r = LibNLRoute3.rtnl_link_change(socket, original, changed, NLMessageFlag.REQUEST);

            return(r == -10 ? 0 : r);
        }
Example #2
0
        public static int RemoveInterface(nl_sock *socket, NetworkInterface networkInterface)
        {
            rtnl_link *nlLink = LibNLRoute3.rtnl_link_vlan_alloc();

            LibNLRoute3.rtnl_link_set_ifindex(nlLink, networkInterface.Index);
            LibNLRoute3.rtnl_link_set_link(nlLink, networkInterface.ParentInterfaceIndex);
            fixed(char *name = networkInterface.InterfaceName)
            {
                LibNLRoute3.rtnl_link_set_name(nlLink, name);
            }

            if (networkInterface.InterfaceType == InterfaceType.VLAN)
            {
                LibNLRoute3.rtnl_link_vlan_set_id(nlLink, ((Vlan)(networkInterface.InterfaceInformation)).VlanId);
            }

            return(LibNLRoute3.rtnl_link_delete(socket, nlLink));
        }
Example #3
0
        public static int SetInterfaceVlanId(nl_sock *socket, int nicIndex, ushort vlanId)
        {
            nl_cache *cache;
            int       x;

            if ((x = LibNLRoute3.rtnl_link_alloc_cache(socket, AddressFamily.INET, &cache)) < 0)
            {
                throw new NetlinkSocketException(x);
            }
            rtnl_link *original = LibNLRoute3.rtnl_link_get(cache, nicIndex);

            rtnl_link *changed = (rtnl_link *)LibNL3.nl_object_clone((nl_object *)original);

            LibNLRoute3.rtnl_link_vlan_set_id(changed, vlanId);

            int r = LibNLRoute3.rtnl_link_change(socket, original, changed, NLMessageFlag.REQUEST);

            return(r == -10 ? 0 : r);
        }
Example #4
0
        public static int SetNetworkInterfaceMaximumTransmissionUnit(nl_sock *socket, int networkInterfaceIndex,
                                                                     uint mtu)
        {
            nl_cache *cache;
            int       x;

            if ((x = LibNLRoute3.rtnl_link_alloc_cache(socket, AddressFamily.INET, &cache)) < 0)
            {
                throw new NetlinkSocketException(x);
            }
            rtnl_link *original = LibNLRoute3.rtnl_link_get(cache, networkInterfaceIndex);

            rtnl_link *changed = (rtnl_link *)LibNL3.nl_object_clone((nl_object *)original);

            LibNLRoute3.rtnl_link_set_mtu(changed, mtu);

            int r = LibNLRoute3.rtnl_link_change(socket, original, changed, NLMessageFlag.REQUEST);

            return(r == -10 ? 0 : r);
        }
Example #5
0
        public static int SetNetworkInterfaceName(nl_sock *socket, int networkInterfaceIndex, string name)
        {
            nl_cache *cache;
            int       x;

            if ((x = LibNLRoute3.rtnl_link_alloc_cache(socket, AddressFamily.INET, &cache)) < 0)
            {
                throw new NetlinkSocketException(x);
            }
            rtnl_link *original = LibNLRoute3.rtnl_link_get(cache, networkInterfaceIndex);

            rtnl_link *changed = (rtnl_link *)LibNL3.nl_object_clone((nl_object *)original);
            var        bytes   = Util.StringToNativeBytes(name);

            fixed(byte *n = bytes)
            LibNLRoute3.rtnl_link_set_name(changed, (char *)n);

            int r = LibNLRoute3.rtnl_link_change(socket, original, changed, NLMessageFlag.REQUEST);

            return(r == -10 ? 0 : r);
        }
        public static InterfaceStatistics GetInterfaceStatistics(nl_sock *socket, int interfaceIndex)
        {
            nl_cache *cache;

            LibNLRoute3.rtnl_link_alloc_cache(socket, AddressFamily.INET, &cache);
            //Check that the number of items is not 0
            int count = LibNL3.nl_cache_nitems(cache);

            if (count == 0)
            {
                return(null);
            }
            rtnl_link *link = LibNLRoute3.rtnl_link_get(cache, interfaceIndex);

            rtnl_link_stat_id_t[] values = (rtnl_link_stat_id_t[])Enum.GetValues(typeof(rtnl_link_stat_id_t));

            ulong[] statistics = new ulong[values.Length - 1];
            for (int i = 0; i < values.Length - 1; i++)
            {
                statistics[i] = LibNLRoute3.rtnl_link_get_stat(link, values[i]);
            }
            return(new InterfaceStatistics(statistics));
        }
Example #7
0
        public static int AddInterface(nl_sock *socket, NetworkInterface networkInterface)
        {
            rtnl_link *nlLink = LibNLRoute3.rtnl_link_vlan_alloc();

            LibNLRoute3.rtnl_link_set_link(nlLink, networkInterface.ParentInterfaceIndex);
            var nameBytes = Util.StringToNativeBytes(networkInterface.InterfaceName);

            fixed(byte *name = nameBytes)
            {
                LibNLRoute3.rtnl_link_set_name(nlLink, (char *)name);
            }

            if (networkInterface.InterfaceType == InterfaceType.VLAN)
            {
                LibNLRoute3.rtnl_link_vlan_set_id(nlLink, ((Vlan)(networkInterface.InterfaceInformation)).VlanId);
            }

            if (networkInterface.MaximumTransmissionUnit != 0)
            {
                LibNLRoute3.rtnl_link_set_mtu(nlLink, networkInterface.MaximumTransmissionUnit);
            }

            return(LibNLRoute3.rtnl_link_add(socket, nlLink, NLMessageFlag.REQUEST | NLMessageFlag.ATOMIC));
        }
Example #8
0
        public unsafe NetworkInterface(rtnl_link *link)
        {
            Index = LibNLRoute3.rtnl_link_get_ifindex(link);
            ParentInterfaceIndex = LibNLRoute3.rtnl_link_get_link(link);
            InterfaceName        = Util.NativeToManagedString((sbyte *)LibNLRoute3.rtnl_link_get_name(link));

            sbyte *typeStringNative = (sbyte *)LibNLRoute3.rtnl_link_get_type(link);
            string type             = Util.NativeToManagedString(typeStringNative);

            switch (type)
            {
            case "":
                InterfaceType = InterfaceType.PHYSICAL;
                break;

            case "vlan":
                InterfaceType = InterfaceType.VLAN;
                break;

            case "bridge":
                InterfaceType = InterfaceType.BRIDGE;
                break;

            case "veth":
                InterfaceType = InterfaceType.VETH;
                break;

            default: throw new NotSupportedException("Interface type " + type + " not supported");
            }

            nl_addr *hwAddr = LibNLRoute3.rtnl_link_get_addr(link);

            if (hwAddr != null)
            {
                byte * mac      = (byte *)LibNL3.nl_addr_get_binary_addr(hwAddr);
                byte[] macBytes = new byte[6];
                for (int i = 0; i < 6; i++)
                {
                    macBytes[i] = mac[i];
                }

                HardwareAddress = new PhysicalAddress(macBytes);
            }

            var linkFlags = LibNLRoute3.rtnl_link_get_flags(link);

            IsUp                    = (linkFlags & NLInterfaceFlags.UP) != 0;
            IsLowerLayerUp          = (linkFlags & NLInterfaceFlags.LOWER_UP) != 0;
            IsBroadcastInterface    = (linkFlags & NLInterfaceFlags.BROADCAST) != 0;
            IsLoopbackInterface     = (linkFlags & NLInterfaceFlags.LOOPBACK) != 0;
            IsPointToPointInterface = (linkFlags & NLInterfaceFlags.POINTOPOINT) != 0;
            IsNbmaInterface         = !(IsBroadcastInterface || IsLoopbackInterface || IsPointToPointInterface);
            IsPromiscuousInterface  = (linkFlags & NLInterfaceFlags.PROMISC) != 0;

            if (InterfaceType == InterfaceType.PHYSICAL && IsLoopbackInterface)
            {
                InterfaceType = InterfaceType.LOOPBACK;
            }

            if (InterfaceType == InterfaceType.VLAN)
            {
                var info = new Vlan((ushort)LibNLRoute3.rtnl_link_vlan_get_id(link));
                InterfaceInformation = info;
            }

            MaximumTransmissionUnit = LibNLRoute3.rtnl_link_get_mtu(link);
        }