diff --git a/src/hostmngr.c b/src/hostmngr.c index eb24ae45d9f35b5b23ac8337e779790979995251..76b1ac0a27df93a2b0da0a8ce098daf625fcbb19 100644 --- a/src/hostmngr.c +++ b/src/hostmngr.c @@ -457,6 +457,21 @@ void hostmngr_update_hostname_of_neighbors(struct hostmngr_private *priv, e->leasetime = h->leasetime; e->ipv4_type_dhcp = 1; + if (!neigh_is_ip_known(e, &e->ipv4)) { + struct ip_address_entry *new; + char newip[64] = {0}; + + inet_ntop(e->ipv4.family, &e->ipv4.addr, newip, sizeof(newip)); + dbg("%s: Adding new ipaddress %s to entry " MACFMT"\n", + __func__, newip, MAC2STR(e->macaddr)); + + new = calloc(1, sizeof(*new)); + if (new) { + memcpy(&new->ip, &e->ipv4, sizeof(e->ipv4)); + list_add_tail(&new->list, &e->iplist); + } + } + ipn = neigh_ip_entry_lookup2(priv, &e->ipv4); if (ipn) { ipn->neigh = e; @@ -507,6 +522,22 @@ int hostmngr_get_neigh_hostname(struct neigh_queue *q, uint8_t *macaddr) e->ipv4_type_dhcp = 1; e->ipv4_type_static = 0; + if (!neigh_is_ip_known(e, &e->ipv4)) { + struct ip_address_entry *new; + char newip[64] = {0}; + + inet_ntop(e->ipv4.family, &e->ipv4.addr, newip, sizeof(newip)); + dbg("%s: Adding new ipaddress %s to entry " MACFMT"\n", + __func__, newip, MAC2STR(e->macaddr)); + + new = calloc(1, sizeof(*new)); + if (new) { + memcpy(&new->ip, &e->ipv4, sizeof(e->ipv4)); + list_add_tail(&new->list, &e->iplist); + } + } + + ipn = neigh_ip_entry_lookup2(priv, &e->ipv4); if (ipn) { ipn->neigh = e; diff --git a/src/neigh.c b/src/neigh.c index d18a3d8209bb4daefcaad62da69580211e5c0a87..98346e5ea11dc0a11e102b6beb050977d612d8d5 100644 --- a/src/neigh.c +++ b/src/neigh.c @@ -1046,6 +1046,18 @@ bool is_neigh_1905_slave(void *nq, uint8_t *macaddr) return is1905_slave; } +bool neigh_is_ip_known(struct neigh_entry *e, struct ip_address *ip) +{ + struct ip_address_entry *ipaddr = NULL; + + list_for_each_entry(ipaddr, &e->iplist, list) { + if (ipaddr_equal(&ipaddr->ip, ip)) + return true; + } + + return false; +} + int neigh_is_wifi_type(void *priv, uint8_t *macaddr) { struct hostmngr_private *p = (struct hostmngr_private *)priv; @@ -1136,7 +1148,6 @@ struct neigh_entry *neigh_enqueue(void *nq, uint8_t *macaddr, uint16_t state, e = neigh_lookup(nq, macaddr); if (e) { struct ip_address_entry *ipaddr; - bool ipknown = false; dbg("[%jd.%jd] Neigh " MACFMT " changed. ifname = %s, IP-family = %s, state 0x%04x -> 0x%04x\n", (uintmax_t)tsp.tv_sec, (uintmax_t)tsp.tv_usec, MAC2STR(macaddr), @@ -1189,23 +1200,7 @@ struct neigh_entry *neigh_enqueue(void *nq, uint8_t *macaddr, uint16_t state, } if (ip) { - /* can get overridden by dhcp lease entry */ - if (ip->family == AF_INET) { - hostmngr_get_neigh_hostname(q, e->macaddr); - - if (!ipaddr_equal(&e->ipv4, ip) && (e->ipv4_type_static || strlen(e->hostname))) { - memcpy(&e->ipv4, ip, sizeof(*ip)); - e->event_pending = 0; - hostmngr_host_event(priv, HOST_EVENT_CONNECT, e); - } - } - - list_for_each_entry(ipaddr, &e->iplist, list) { - if (ipaddr_equal(&ipaddr->ip, ip)) - ipknown = true; - } - - if (!ipknown) { + if (!neigh_is_ip_known(e, ip)) { struct ip_address_entry *new; char newip[64] = {0}; @@ -1255,7 +1250,6 @@ struct neigh_entry *neigh_enqueue(void *nq, uint8_t *macaddr, uint16_t state, if (e) { int idx = neigh_hash(macaddr); struct ip_address_entry *ipaddr; - bool ipknown = false; hlist_add_head(&e->hlist, &q->table[idx]); @@ -1294,13 +1288,7 @@ struct neigh_entry *neigh_enqueue(void *nq, uint8_t *macaddr, uint16_t state, if (ip->family == AF_INET) memcpy(&e->ipv4, ip, sizeof(*ip)); - - list_for_each_entry(ipaddr, &e->iplist, list) { - if (ipaddr_equal(&ipaddr->ip, ip)) - ipknown = true; - } - - if (!ipknown) { + if (!neigh_is_ip_known(e, ip)) { struct ip_address_entry *new; dbg("Adding new ipaddress to entry " MACFMT"\n", diff --git a/src/neigh.h b/src/neigh.h index 2291f939870d9f514b9cb1a876cd41a5d35a5a03..2ebdabc8719c9cf7883207d35e0dbb2b6df914ae 100644 --- a/src/neigh.h +++ b/src/neigh.h @@ -196,6 +196,7 @@ int neigh_set_1905_linkaddr(void *nq, uint8_t *aladdr, uint8_t *linkaddr); bool is_neigh_1905(void *q, uint8_t *macaddr); bool is_neigh_1905_slave(void *q, uint8_t *macaddr); +bool neigh_is_ip_known(struct neigh_entry *e, struct ip_address *ip); int neigh_is_wifi_type(void *priv, uint8_t *macaddr); struct neigh_entry *neigh_lookup(void *q, uint8_t *macaddr); diff --git a/src/netlink.c b/src/netlink.c index 6c7e50717e0022d1ab273795bdb2518a41bdeda5..30bece27029941afb23c978d7519e234df7fbf01 100644 --- a/src/netlink.c +++ b/src/netlink.c @@ -192,22 +192,6 @@ static int hostmngr_handle_neigh_tbl_change(struct hostmngr_private *priv, bool return 0; } - eh = neigh_history_lookup(&priv->neigh_q, macaddr); - if (eh && eh->type == NEIGH_TYPE_WIFI && eh->is1905 == false) { - struct neigh_entry *e; - - e = neigh_lookup(&priv->neigh_q, macaddr); - if (!e) { - dbg("Skip enqueue WiFi neigh through neigh table change\n"); - return 0; - } - - if (e->event_pending && !ipaddr_is_zero(&e->ipv4) && strlen(e->hostname)) { - e->event_pending = 0; - hostmngr_host_event(priv, HOST_EVENT_CONNECT, e); - } - } - new = neigh_enqueue(&priv->neigh_q, macaddr, state, ifname, NEIGH_TYPE_UNKNOWN, ip, NEIGH_AGEOUT_DEFAULT, NULL); @@ -225,6 +209,21 @@ static int hostmngr_handle_neigh_tbl_change(struct hostmngr_private *priv, bool } + eh = neigh_history_lookup(&priv->neigh_q, macaddr); + if (eh && eh->type == NEIGH_TYPE_WIFI && eh->is1905 == false) { + struct neigh_entry *e; + + e = neigh_lookup(&priv->neigh_q, macaddr); + if (!e) { + dbg("Skip enqueue WiFi neigh through neigh table change\n"); + return 0; + } + + if (e->event_pending && !ipaddr_is_zero(&e->ipv4) && strlen(e->hostname)) { + e->event_pending = 0; + hostmngr_host_event(priv, HOST_EVENT_CONNECT, e); + } + } #if 0 //def NEIGH_DEBUG if (priv->neigh_q.pending_cnt > 0) {