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); }
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 extern nl_addr *nl_addr_clone(nl_addr *addr);
public static extern char *nl_addr2str(nl_addr *addr, char *buf, uint size);
public static extern void *nl_addr_get_binary_addr(nl_addr *addr);
public static extern void nl_addr_set_family(nl_addr *addr, int family);
public static extern void nl_addr_set_prefixlen(nl_addr *addr, int prefixlen);
public static extern uint nl_addr_get_prefixlen(nl_addr *addr);
public static extern void nl_addr_put(nl_addr *addr);
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); }
public static extern nl_addr *nl_addr_get(nl_addr *addr);
public static extern int nl_addr_set_binary_addr(nl_addr *addr, void *buf, uint len);
public static extern int nl_addr_shared(nl_addr *addr);
public static extern int nl_addr_resolve(nl_addr *addr, char *host, uint hostlen);
public static extern int nl_addr_iszero(nl_addr *addr);
public static extern int nl_addr_guess_family(nl_addr *addr);
public static extern int nl_addr_cmp(nl_addr *a, nl_addr *b);
public static extern int nl_addr_cmp_prefix(nl_addr *a, nl_addr *b);