void add_addr_to_list(struct nl_object *obj, void *data) { char buf[INET6_ADDRSTRLEN+5]; std::list<vaddress> *list = static_cast<std::list<vaddress>*>(data); struct nl_addr *naddr = rtnl_addr_get_local((struct rtnl_addr *) obj); int ifindex = 0; int scope = rtnl_addr_get_scope((struct rtnl_addr *) obj); if (scope == rtnl_str2scope("link")) ifindex = rtnl_addr_get_ifindex((struct rtnl_addr *) obj); if (naddr) { int family = nl_addr_get_family(naddr); nl_addr2str( naddr, buf, sizeof( buf ) ); vaddress vaddr(vaddress::Family(family), vaddress::strip_netmask(std::string(buf)), ifindex, false); list->push_back( vaddr ); } struct nl_addr *baddr = rtnl_addr_get_broadcast((struct rtnl_addr *) obj); if (baddr) { int family = nl_addr_get_family(baddr); nl_addr2str( baddr, buf, sizeof( buf ) ); vaddress vaddr(vaddress::Family(family), vaddress::strip_netmask(std::string(buf)), ifindex, true); list->push_back( vaddr ); } }
/** * libnl callback function. Does the real parsing of a record returned by NETLINK. This function * parses ADDRESS related packets * * @param obj Pointer to a struct nl_object response * @param arg Pointer to a struct etherinfo element where the parse result will be saved */ static void callback_nl_address(struct nl_object *obj, void *arg) { struct etherinfo *ethi = (struct etherinfo *) arg; struct nl_addr *addr; char ip_str[66]; int family; if( ethi == NULL ) { return; } addr = rtnl_addr_get_local((struct rtnl_addr *)obj); family = nl_addr_get_family(addr); switch( family ) { case AF_INET: case AF_INET6: memset(&ip_str, 0, 66); inet_ntop(family, nl_addr_get_binary_addr(addr), (char *)&ip_str, 64); if( family == AF_INET ) { struct nl_addr *brdcst = rtnl_addr_get_broadcast((struct rtnl_addr *)obj); char brdcst_str[66]; SET_STR_VALUE(ethi->ipv4_address, ip_str); ethi->ipv4_netmask = rtnl_addr_get_prefixlen((struct rtnl_addr*) obj); if( brdcst ) { memset(&brdcst_str, 0, 66); inet_ntop(family, nl_addr_get_binary_addr(brdcst), (char *)&brdcst_str, 64); SET_STR_VALUE(ethi->ipv4_broadcast, brdcst_str); } } else { ethi->ipv6_addresses = etherinfo_add_ipv6(ethi->ipv6_addresses, ip_str, rtnl_addr_get_prefixlen((struct rtnl_addr*) obj), rtnl_addr_get_scope((struct rtnl_addr*) obj)); } return; default: return; } }
TError TNlLink::WaitAddress(int timeout_s) { struct nl_cache *cache; int ret; L() << "Wait for autoconf at " << GetDesc() << std::endl; ret = rtnl_addr_alloc_cache(GetSock(), &cache); if (ret < 0) return Nl->Error(ret, "Cannot allocate addr cache"); do { for (auto obj = nl_cache_get_first(cache); obj; obj = nl_cache_get_next(obj)) { auto addr = (struct rtnl_addr *)obj; if (!rtnl_addr_get_local(addr) || rtnl_addr_get_ifindex(addr) != GetIndex() || rtnl_addr_get_family(addr) != AF_INET6 || rtnl_addr_get_scope(addr) >= RT_SCOPE_LINK || (rtnl_addr_get_flags(addr) & (IFA_F_TENTATIVE | IFA_F_DEPRECATED))) continue; L() << "Got " << TNlAddr(rtnl_addr_get_local(addr)).Format() << " at " << GetDesc() << std::endl; nl_cache_free(cache); return TError::Success(); } usleep(1000000); ret = nl_cache_refill(GetSock(), cache); if (ret < 0) return Nl->Error(ret, "Cannot refill address cache"); } while (--timeout_s > 0); nl_cache_free(cache); return TError(EError::Unknown, "Network autoconf timeout"); }
static void env_dump(struct nl_object *obj, void *arg) { struct nl_dump_params *p = arg; struct rtnl_addr *addr = (struct rtnl_addr *) obj; struct nl_cache *link_cache; struct nl_addr *a; static int index = 0; char buf[128], pfx[32], *s; snprintf(pfx, sizeof(pfx), "ADDR%d", index++); nl_dump_line(p, "%s_FAMILY=%s\n", pfx, nl_af2str(rtnl_addr_get_family(addr), buf, sizeof(buf))); nl_dump_line(p, "%s_LOCAL=%s\n", pfx, nl_addr2str(rtnl_addr_get_local(addr), buf, sizeof(buf))); nl_dump_line(p, "%s_IFINDEX=%u\n", pfx, rtnl_addr_get_ifindex(addr)); link_cache = nl_cache_mngt_require_safe("route/link"); if (link_cache) nl_dump_line(p, "%s_IFNAME=%s\n", pfx, rtnl_link_i2name(link_cache, rtnl_addr_get_ifindex(addr), buf, sizeof(buf))); if ((a = rtnl_addr_get_peer(addr))) nl_dump_line(p, "%s_PEER=%s\n", pfx, nl_addr2str(a, buf, sizeof(buf))); if ((a = rtnl_addr_get_broadcast(addr))) nl_dump_line(p, "%s_BROADCAST=%s\n", pfx, nl_addr2str(a, buf, sizeof(buf))); nl_dump_line(p, "%s_SCOPE=%s\n", pfx, rtnl_scope2str(rtnl_addr_get_scope(addr), buf, sizeof(buf))); if ((s = rtnl_addr_get_label(addr))) nl_dump_line(p, "%s_LABEL=%s\n", pfx, s); rtnl_addr_flags2str(rtnl_addr_get_flags(addr), buf, sizeof(buf)); if (buf[0]) nl_dump_line(p, "%s_FLAGS=%s\n", pfx, buf); nl_dump_line(p, "%s_CACHEINFO_VALID=%u\n", pfx, rtnl_addr_get_valid_lifetime(addr)); if (link_cache) nl_cache_put(link_cache); #if 0 if (addr->ce_mask & ADDR_ATTR_CACHEINFO) { struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo; nl_dump_line(p, "ADDR_CACHEINFO_PREFERRED=%u\n", ci->aci_prefered); nl_dump_line(p, "ADDR_CACHEINFO_CREATED=%u\n", ci->aci_cstamp); nl_dump_line(p, "ADDR_CACHEINFO_LASTUPDATE=%u\n", ci->aci_tstamp); } #endif }