예제 #1
0
        public static Neighbour[] GetArpCache(nl_sock *socket)
        {
            nl_cache *cache;

            LibNLRoute3.rtnl_neigh_alloc_cache(socket, &cache);

            int count = LibNL3.nl_cache_nitems(cache);

            if (count == 0)
            {
                return(null);
            }

            Neighbour[] neighbours = new Neighbour[count];

            nl_object *current = LibNL3.nl_cache_get_first(cache);

            for (int i = 0; i < count; i++)
            {
                neighbours[i] = new Neighbour(current);
                current       = LibNL3.nl_cache_get_next(current);
            }

            return(neighbours);
        }
예제 #2
0
        public static NetworkInterface[] RequestAllNetworkInterfaces(nl_sock *socket)
        {
            nl_cache *cache;
            int       x;

            if ((x = LibNLRoute3.rtnl_link_alloc_cache(socket, AddressFamily.INET, &cache)) < 0)
            {
                throw new NetlinkSocketException(x);
            }
            //Check that the number of items is not 0
            int count = LibNL3.nl_cache_nitems(cache);

            if (count == 0)
            {
                return(null);
            }

            NetworkInterface[] interfaces = new NetworkInterface[count];

            nl_object *current = LibNL3.nl_cache_get_first(cache);

            for (int i = 0; i < count; i++)
            {
                interfaces[i] = new NetworkInterface(current);
                current       = LibNL3.nl_cache_get_next(current);
            }

            return(interfaces);
        }
예제 #3
0
        public unsafe Neighbour(rtnl_neigh *neighbour)
        {
            nl_addr *l2Addr = LibNLRoute3.rtnl_neigh_get_lladdr(neighbour);
            byte *   mac    = (byte *)LibNL3.nl_addr_get_binary_addr(l2Addr);

            byte[] macBytes = new byte[6];
            for (int i = 0; i < 6; i++)
            {
                macBytes[i] = mac[i];
            }
            Layer2Address = new PhysicalAddress(macBytes);


            nl_addr *l3Addr        = LibNLRoute3.rtnl_neigh_get_dst(neighbour);
            uint     addressLength = LibNL3.nl_addr_get_len(l3Addr);

            byte[] bytes = new byte[addressLength];
            for (uint i = 0; i < addressLength; i++)
            {
                bytes[i] = ((byte *)LibNL3.nl_addr_get_binary_addr(l3Addr))[i];
            }
            Layer3Address = new IPAddress(bytes);

            VlanId = (ushort)LibNLRoute3.rtnl_neigh_get_vlan(neighbour);
            Family = LibNLRoute3.rtnl_neigh_get_family(neighbour);
        }
예제 #4
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);
        }
예제 #5
0
        public static NetworkInterface RequestInterface(nl_sock *socket, String name)
        {
            nl_cache *cache;
            int       x;

            if ((x = LibNLRoute3.rtnl_link_alloc_cache(socket, AddressFamily.INET, &cache)) < 0)
            {
                throw new NetlinkSocketException(x);
            }
            //Check that the number of items is not 0
            int count = LibNL3.nl_cache_nitems(cache);

            if (count == 0)
            {
                return(null);
            }


            byte[] nameBytes = Util.StringToNativeBytes(name);
            int    index;

            fixed(byte *n = nameBytes)
            {
                index = LibNLRoute3.rtnl_link_name2i(cache, n);
            }

            NetworkInterface networkInterface = new NetworkInterface(LibNLRoute3.rtnl_link_get(cache, index));

            return(networkInterface);
        }
예제 #6
0
        public static IpAddress[] RequestAllAddresses(nl_sock *socket)
        {
            nl_cache *cache;

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

            if (count == 0)
            {
                return(null);
            }

            IpAddress[] addresses = new IpAddress[count];

            nl_object *current = LibNL3.nl_cache_get_first(cache);

            for (int i = 0; i < count; i++)
            {
                addresses[i] = new IpAddress(current);
                current      = LibNL3.nl_cache_get_next(current);
            }

            return(addresses.ToArray());
        }
예제 #7
0
        public static Route[] RequestAllRoutes(nl_sock *socket, bool ipv6)
        {
            nl_cache *cache;

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

            if (count == 0)
            {
                return(null);
            }

            Route[] routes = new Route[count];

            nl_object *current = LibNL3.nl_cache_get_first(cache);

            for (int i = 0; i < count; i++)
            {
                routes[i] = new Route(current);
                current   = LibNL3.nl_cache_get_next(current);
            }

            return(routes);
        }
예제 #8
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);
        }
예제 #9
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);
        }
예제 #10
0
        public static int AddIpAddress(nl_sock *socket, IpAddress ipAddress)
        {
            rtnl_addr *addr = LibNLRoute3.rtnl_addr_alloc();

            LibNLRoute3.rtnl_addr_set_ifindex(addr, ipAddress.Nic);
            LibNLRoute3.rtnl_addr_set_scope(addr, 0);

            nl_addr *nlAddr;

            byte[] addressBytes = ipAddress.Address.GetAddressBytes();
            fixed(byte *a = addressBytes)
            {
                nlAddr = LibNL3.nl_addr_build(ipAddress.Family, a, ipAddress.Size());
            }

            LibNL3.nl_addr_set_prefixlen(nlAddr, ipAddress.Netmask);
            LibNLRoute3.rtnl_addr_set_local(addr, nlAddr);

            return(LibNLRoute3.rtnl_addr_add(socket, addr, NLMessageFlag.REQUEST | NLMessageFlag.ATOMIC));
        }
예제 #11
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);
        }
예제 #12
0
        public static int AddRoute(nl_sock *socket, Route route)
        {
            rtnl_route *nlRoute = LibNLRoute3.rtnl_route_alloc();

            LibNLRoute3.rtnl_route_set_family(nlRoute,
                                              (byte)(route.Family == AddressFamily.INET
                    ? System.Net.Sockets.AddressFamily.InterNetwork
                    : System.Net.Sockets.AddressFamily.InterNetworkV6));
            LibNLRoute3.rtnl_route_set_table(nlRoute, (uint)RoutingTable.MAIN);

            nl_addr *nlDestination;

            byte[] destinationBytes = route.Destination.NetworkAddress.GetAddressBytes();

            fixed(byte *d = destinationBytes)
            nlDestination = LibNL3.nl_addr_build(route.Family, d, (uint)destinationBytes.Length);

            LibNL3.nl_addr_set_prefixlen(nlDestination, (int)route.Destination.NetmaskLength);
            LibNLRoute3.rtnl_route_set_dst(nlRoute, nlDestination);

            nl_addr *nlGateway;

            byte[] gatewayBytes = route.Gateway.GetAddressBytes();

            fixed(byte *g = gatewayBytes)
            nlGateway = LibNL3.nl_addr_build(route.Family, g, (uint)gatewayBytes.Length);

            LibNL3.nl_addr_set_prefixlen(nlGateway, route.Family == AddressFamily.INET ? 32 : 128);

            rtnl_nexthop *rtnlNextHop = LibNLRoute3.rtnl_route_nh_alloc();

            LibNLRoute3.rtnl_route_nh_set_gateway(rtnlNextHop, nlGateway);
            LibNLRoute3.rtnl_route_nh_set_ifindex(rtnlNextHop, route.Nic);
            LibNLRoute3.rtnl_route_add_nexthop(nlRoute, rtnlNextHop);

            LibNLRoute3.rtnl_route_set_protocol(nlRoute, (byte)route.Protocol);

            return(LibNLRoute3.rtnl_route_add(socket, nlRoute, NLMessageFlag.REQUEST | NLMessageFlag.ATOMIC));
        }
예제 #13
0
        public static Route RequestRoute(nl_sock *socket, Subnet destinationSubnet)
        {
            nl_cache *cache;
            var       family = AddressFamily.Convert(destinationSubnet.NetworkAddress.AddressFamily);

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

            if (count == 0)
            {
                return(null);
            }

            nl_addr *destinationNetwork;

            fixed(byte *bytes = destinationSubnet.NetworkAddress.GetAddressBytes())
            {
                destinationNetwork = LibNL3.nl_addr_build(family, bytes, AddressFamily.Size(family));
            }

            nl_object *current = LibNL3.nl_cache_get_first(cache);

            for (int i = 0; i < count; i++)
            {
                nl_addr *dAddr = LibNLRoute3.rtnl_route_get_dst((rtnl_route *)current);
                if (LibNL3.nl_addr_get_prefixlen(dAddr) == destinationSubnet.NetmaskLength &&
                    LibNL3.nl_addr_cmp_prefix(dAddr, destinationNetwork) == 0)
                {
                    return(new Route(current));
                }

                current = LibNL3.nl_cache_get_next(current);
            }

            return(null);
        }
        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));
        }
예제 #15
0
 public NetlinkSocket()
 {
     _sockFd = LibNL3.nl_socket_alloc();
     LibNL3.nl_connect(_sockFd, 0);
 }
예제 #16
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);
        }