diff --git a/src/host.c b/src/host.c index 748b032f89c4f4c14b620c10a025c09d369ee6aa..0d1d5414834115e626c0e00316c9a9d2e6c8178c 100644 --- a/src/host.c +++ b/src/host.c @@ -782,12 +782,11 @@ int host_get_ipaddr_hostname(struct topologyd_private *priv, if (ret != 0) return ret; - ret = strncmp(ipv4_address, "", 24); - if (ret == 0) - return -1; - strncpy(p->ipv4addr, ipv4_address, 24); dbg("The IPv4 address is %s mac is %s device is %s\n", p->ipv4addr, macaddr_str, device); + + host_get_netlink_ip6(p->hwaddr, p); + ret = host_append_dhcpv4_info(macaddr_str, p); if (ret != 0) { strncpy(p->ipv4addr_list[0], p->ipv4addr, 24); @@ -810,10 +809,14 @@ int host_get_ipaddr_hostname(struct topologyd_private *priv, } p->active_connections = active_connections(p->ipv4addr); + ret = strncmp(p->ipv4addr, "", 24); + if (ret == 0) + return -1; + dbg("%s %d ipv4addr = [%s]\n", __func__, __LINE__, p->ipv4addr); // Append DHCPv6 information: IPv6 Address, DUID, Lease Time - if (strlen(p->hostname) > 1) - host_append_dhcpv6_info(p); + //if (strlen(p->hostname) > 1) + //host_append_dhcpv6_info(p); ret = 0; dbg("Inside %s %d mac=[%s] macinterface =[%d]\n", __func__, __LINE__, macaddr_str, p->intf_type); diff --git a/src/host_utils.c b/src/host_utils.c index d46b430cec973058ac3c6554471b32daf78e4223..172fac9c19c3f0120eb0950814bbad7f62b25ea9 100644 --- a/src/host_utils.c +++ b/src/host_utils.c @@ -296,6 +296,104 @@ int host_get_netlink_ip(uint8_t *mac_addr, char *ipv4_str, char *device) return 0; } +int host_get_netlink_ip6(uint8_t *mac_addr, struct host_node *p) +{ + struct rtnl_neigh *neigh; + struct nl_object *nobj; + struct nl_cache *cache; + uint32_t ifindex = 0; + struct nl_sock *sk; + int i, num; + int ret; + char *ifname = NULL; + + dbg("Inside %s %d\n", __func__, __LINE__); + sk = nl_socket_alloc(); + if (!sk) { + err("Unable to open nl event socket\n"); + return -1; + } + + if (nl_connect(sk, NETLINK_ROUTE) < 0) { + nl_socket_free(sk); + return -1; + } + + ret = rtnl_neigh_alloc_cache(sk, &cache); + if (ret) { + nl_socket_free(sk); + return -1; + } + + num = nl_cache_nitems(cache); + nobj = nl_cache_get_first(cache); + neigh = (struct rtnl_neigh *)nobj; + p->ipv6addr_count = 0; + + for (i = 0; i < num; i++) { + struct nl_addr *lladdr; + struct nl_addr *ipaddr; + struct ip_address ip = {0}; + uint8_t hwaddr[6] = {0}; + char addr_str[128]; + + nl_object_get((struct nl_object *) neigh); + + ifindex = rtnl_neigh_get_ifindex(neigh); + lladdr = rtnl_neigh_get_lladdr(neigh); + if (lladdr) + memcpy(hwaddr, nl_addr_get_binary_addr(lladdr), + nl_addr_get_len(lladdr)); + + if (hwaddr_is_zero(hwaddr)) { + nl_object_put((struct nl_object *) neigh); + nobj = nl_cache_get_next(nobj); + neigh = (struct rtnl_neigh *)nobj; + continue; + } + + ipaddr = rtnl_neigh_get_dst(neigh); + if (ipaddr) { + ip.family = nl_addr_get_family(ipaddr); + if (ip.family == AF_INET6) { + memcpy(&ip.addr, nl_addr_get_binary_addr(ipaddr), + nl_addr_get_len(ipaddr)); + } else { + nl_object_put((struct nl_object *) neigh); + nobj = nl_cache_get_next(nobj); + neigh = (struct rtnl_neigh *)nobj; + continue; + } + } + char *addr_s = nl_addr2str(ipaddr, addr_str, sizeof(addr_str)); + + if (addr_s == NULL) { + /* error */ + err("nl_addr2str failed\n"); + nl_object_put((struct nl_object *) neigh); + nl_cache_free(cache); + nl_socket_free(sk); + return -1; + } + + ret = memcmp(hwaddr, mac_addr, 6); + if (ret == 0) { + strncpy(p->ipv6addr[p->ipv6addr_count], addr_str, 128); + dbg("ipv6addr= [%s] mac=["MACFMT"]\n", p->ipv6addr[p->ipv6addr_count], MAC2STR(hwaddr)); + p->ipv6addr_count = p->ipv6addr_count + 1; + } + + nl_object_put((struct nl_object *) neigh); + nobj = nl_cache_get_next(nobj); + neigh = (struct rtnl_neigh *)nobj; + } + + nl_cache_free(cache); + nl_socket_free(sk); + + return 0; +} + void _E1B(uint8_t **packet_ppointer, uint8_t *memory_pointer) { *memory_pointer = **packet_ppointer; diff --git a/src/host_utils.h b/src/host_utils.h index b6741b7f11a55d27dafb665ebca56fd6ebb13a0e..8af8b9be2124c6e5a2975397647abf583edc698a 100644 --- a/src/host_utils.h +++ b/src/host_utils.h @@ -19,6 +19,7 @@ #include <libubus.h> #include <easy/easy.h> #include <linux/rtnetlink.h> +#include "host.h" #define NLMSG_NEIGH 28 #define IFLIST_REPLY_BUFFER 8192 @@ -27,6 +28,7 @@ bool host_send_arping(const char *targetIP, const char *device, int toms, int is_recv); int host_get_neigh(struct nlmsghdr *nlh, char *dev, char *dest_addr, uint8_t *mac_addr); int host_get_netlink_ip(uint8_t *mac_addr, char *ipv4_str, char *device); +int host_get_netlink_ip6(uint8_t *mac_addr, struct host_node *p); uint8_t count_tlv_present_instream_unsafe(uint8_t *tlv_stream); uint16_t get_tlv_packet_len(uint8_t *tlv_stream); uint8_t get_tlv_packet_type(uint8_t *tlv_stream); diff --git a/src/topologyd.c b/src/topologyd.c index 0255fb1fd36545fed9245aff90aae86561556432..c2282c66ee3afb8892bfb3955a08270a996e28a8 100644 --- a/src/topologyd.c +++ b/src/topologyd.c @@ -2020,8 +2020,10 @@ void changelog_copy_node_info(struct topologyd_private *t, struct node *p, int32 hn = host_node_lookup(t->host.node_htable, p->hwaddr); if (!hn) { hn = host_topo_node_add(t, p, NULL, 0); - if (!hn) + if (!hn) { err("Failed to add node in the hosts\n"); + return; + } if (hn->is_ipaddr) host_send_client_event(t, hn, 1); } @@ -2101,9 +2103,11 @@ void update_changelog_info(struct topologyd_private *priv, struct node *dest, st struct host_node *hn; hn = host_node_lookup(priv->host.node_htable, elem.nbr_macaddr); if (!hn) { - hn = host_topo_node_add(priv, elem.nbr_macaddr, NULL, 0); - if (!hn) + hn = host_topo_node_add(priv, NULL, elem.nbr_macaddr, 0); + if (!hn) { err("Failed to add non1905 node in the hosts\n"); + continue; + } if (hn->is_ipaddr) host_send_client_event(priv, hn, 1); } @@ -2377,6 +2381,7 @@ void topologyd_update_changelog_firstentry(struct topologyd_private *priv, struc struct topology_changelog elem; uint8_t node_mac[6]; int ret = 0; + struct host_node *hn = NULL; if (dest == NULL || priv == NULL) return; @@ -2395,12 +2400,13 @@ void topologyd_update_changelog_firstentry(struct topologyd_private *priv, struc //Send event topology_send_node_event(&elem, dest->ingress_ifr_name); /*Here we need to add the node in the host*/ - struct host_node *hn; hn = host_node_lookup(priv->host.node_htable, node_mac); if (!hn) { hn = host_topo_node_add(priv, NULL, node_mac, 0); - if (!hn) + if (!hn) { err("Failed to add node in the hosts\n"); + continue; + } if (hn->is_ipaddr) host_send_client_event(priv, hn, 1); }