diff --git a/src/host.c b/src/host.c index 6036f02b3a7438c9ffb586a8c511c2f1a3b87bbb..c93d21bea9f8aa7f16ca6f643750bbc17bb45baf 100644 --- a/src/host.c +++ b/src/host.c @@ -228,14 +228,43 @@ void host_dump_node_nbr(struct topologyd_private *priv, struct blob_buf *b) uci_free_context(ctx); } +struct host_node *host_check_get_node(struct topologyd_private *priv, uint8_t *mac, bool *send_ev) +{ + char mac_str[18] = { 0 }; + struct host_node *p = NULL; + + if (!memcmp(mac, priv->ieee1905_macaddr, 6)) + return NULL; + + /* Add node if not present in host. + * + * In case the node is present, update the node's values + * and the timestamp. + */ + + p = host_node_lookup(priv->host.node_htable, mac); + if (!p) { + p = host_node_add(priv->host.node_htable, mac); + if (p == NULL) + return NULL; + *send_ev = true; + } + + dbg("%s %d ||||||||| setting node "MACFMT" to active\n", __func__, __LINE__, MAC2STR(p->hwaddr)); + + //here we need to write this info in the config file + hwaddr_ntoa(mac, mac_str); + config_add_default_host_mac("hosts", "host", mac_str); + return p; + +} + struct host_node *host_topo_node_add(struct topologyd_private *priv, struct node *node, uint8_t *mac_addr, uint8_t is_wifi) { struct host_node *p = NULL; char mac_str[18] = { 0 }; - time_t tmp_t; - struct tm *info; - char str_tm[32] = {0}; int ret = 0; + bool send_ev = false; dbg("Inside %s %d\n", __func__, __LINE__); @@ -248,89 +277,26 @@ struct host_node *host_topo_node_add(struct topologyd_private *priv, struct node } if (node != NULL) { - bool send_ev = false; - - if (!memcmp(node->hwaddr, priv->ieee1905_macaddr, 6)) + p = host_check_get_node(priv, node->hwaddr, &send_ev); + if (p == NULL) return NULL; - - /* Add node if not present in host. - * - * In case the node is present, update the node's values - * and the timestamp. - */ - p = host_node_lookup(priv->host.node_htable, node->hwaddr); - if (!p) { - p = host_node_add(priv->host.node_htable, node->hwaddr); - if (p == NULL) - return NULL; - send_ev = true; - } p->is_ieee1905_node = 1; - dbg("%s %d ||||||||| setting node "MACFMT" to active\n", __func__, __LINE__, MAC2STR(p->hwaddr)); - - //here we need to write this info in the config file - hwaddr_ntoa(node->hwaddr, mac_str); - config_add_default_host_mac("hosts", "host", mac_str); // TODO review the if case if (p->intf_type != HOST_TYPE_WIFI || (p->intf_type == HOST_TYPE_WIFI && is_wifi)) host_change_active_state(priv, p, 1); - p->active_last_change = time(NULL); - tmp_t = p->active_last_change; - info = localtime(&tmp_t); - strftime(str_tm, sizeof(str_tm), "%Y-%m-%dT%H:%M:%SZ", info); - config_set_host_option("hosts", "host", "macaddr", mac_str, "active_last_change", str_tm); host_get_wifi_data(priv, p); - bool has_ip = p->is_ipaddr; - - ret = host_get_ipaddr_hostname(priv, mac_str, p); - if (ret != 0) { - err("Unable to fetch ip adddress and host name\n"); - p->is_ipaddr = 0; - } else { - p->is_ipaddr = 1; - if (!has_ip && p->intf_type == HOST_TYPE_WIFI && !p->is_in_assoclist) { - if (!host_get_arping_status(p->ipv4addr)) { - host_send_client_event(priv, p, 1); - host_change_active_state(priv, p, 1); - } else - host_change_active_state(priv, p, 0); - } - } - - if (send_ev && p->active && !is_wifi && p->is_ipaddr) - host_send_client_event(priv, p, 1); } else if (mac_addr != NULL) { - bool send_ev = false; - - if (!memcmp(mac_addr, priv->ieee1905_macaddr, 6)) { - //Here the node is the selfnode - //self node does not get added in the hosts - return 0; - } - - p = host_node_lookup(priv->host.node_htable, mac_addr); - if (!p) { - p = host_node_add(priv->host.node_htable, mac_addr); - if (p == NULL) - return NULL; - send_ev = true; - } - p->is_ieee1905_node = 0; + p = host_check_get_node(priv, mac_addr, &send_ev); + if (p == NULL) + return NULL; hwaddr_ntoa(mac_addr, mac_str); - //here we need to write this info in the config file - config_add_default_host_mac("hosts", "host", mac_str); + p->is_ieee1905_node = 0; if (p->active != 1) { dbg("%s %d ||||||||| setting node %s to active\n", __func__, __LINE__, mac_str); if (p->intf_type != HOST_TYPE_WIFI || (p->intf_type == HOST_TYPE_WIFI && is_wifi)) host_change_active_state(priv, p, 1); - p->active_last_change = time(NULL); - tmp_t = p->active_last_change; - info = localtime(&tmp_t); - strftime(str_tm, sizeof(str_tm), "%Y-%m-%dT%H:%M:%SZ", info); - config_set_host_option("hosts", "host", "macaddr", mac_str, "active_last_change", str_tm); - //Here Adding the ipv4 address and hostname and ipv6 address } if (is_wifi == 0 && p->intf_type != HOST_TYPE_WIFI) { @@ -342,34 +308,30 @@ struct host_node *host_topo_node_add(struct topologyd_private *priv, struct node p->intf_type = HOST_TYPE_WIFI; } - if (p->intf_type == HOST_TYPE_WIFI) - config_set_host_option("hosts", "host", "macaddr", mac_str, "interface_type", "wifi"); - else/* if (p->intf_type == HOST_TYPE_ETHER) */{ - config_set_host_option("hosts", "host", "macaddr", mac_str, "interface_type", "eth"); - } - - - bool has_ip = p->is_ipaddr; + config_set_host_option("hosts", "host", "macaddr", mac_str, "interface_type", + (p->intf_type == HOST_TYPE_WIFI ? "wifi" : "eth")); + } + bool has_ip = p->is_ipaddr; - ret = host_get_ipaddr_hostname(priv, mac_str, p); - if (ret != 0) { - err("Unable to fetch ip adddress and host name\n"); - p->is_ipaddr = 0; - } else { - p->is_ipaddr = 1; - if (!has_ip && p->intf_type == HOST_TYPE_WIFI && !p->is_in_assoclist) { - if (!host_get_arping_status(p->ipv4addr)) { - //host_send_client_event(priv, p, 1); - host_change_active_state(priv, p, 1); - } else - host_change_active_state(priv, p, 0); - } + ret = host_get_ipaddr_hostname(priv, mac_str, p); + if (ret != 0) { + err("Unable to fetch ip adddress and host name\n"); + p->is_ipaddr = 0; + } else { + p->is_ipaddr = 1; + if (!has_ip && p->intf_type == HOST_TYPE_WIFI && !p->is_in_assoclist) { + if (!host_get_arping_status(p->ipv4addr)) { + if (p->is_ieee1905_node == 1) + host_send_client_event(priv, p, 1); + host_change_active_state(priv, p, 1); + } else + host_change_active_state(priv, p, 0); } + } - if (send_ev && p->active && !is_wifi && p->is_ipaddr) - host_send_client_event(priv, p, 1); + if (send_ev && p->active && !is_wifi && p->is_ipaddr) + host_send_client_event(priv, p, 1); - } if (p && p->al_node) host_topo_node_add(priv, p->al_node, NULL, is_wifi); @@ -619,7 +581,7 @@ void host_get_network(struct topologyd_private *priv, struct host_node *p) } } -static void host_get_network_dump(struct topologyd_private *priv) +void host_get_network_dump(struct topologyd_private *priv) { struct blob_buf bb = {0}; int ret; @@ -1040,81 +1002,6 @@ static int host_invoke_wifi_nodes(struct topologyd_private *priv, } -void host_event_handler(struct ubus_context *ctx, - struct ubus_event_handler *ev, const char *type, - struct blob_attr *msg) -{ - char *str; - - struct topologyd_private *priv = - container_of(ev, struct topologyd_private, wifi_evh); - - if (!msg) - return; - - str = blobmsg_format_json(msg, true); - if (!str) - return; - - info("[ &Host = %p ] Received [event = %s] [val = %s]\n", - priv, type, str); - host_wifi_sta_event_handler(priv, msg); - free(str); -} - -void host_wifi_sta_event_handler(void *c, struct blob_attr *msg) -{ - return; - - struct topologyd_private *p = (struct topologyd_private *)c; - char ifname[16] = {0}, event[16] = {0}; - struct blob_attr *tb[3]; - static const struct blobmsg_policy ev_attr[3] = { - [0] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING }, - [1] = { .name = "event", .type = BLOBMSG_TYPE_STRING }, - [2] = { .name = "data", .type = BLOBMSG_TYPE_TABLE }, - }; - bool add = false, del = false; - - blobmsg_parse(ev_attr, 3, tb, blob_data(msg), blob_len(msg)); - - if (!tb[0] || !tb[1] || !tb[2]) - return; - - strncpy(ifname, blobmsg_data(tb[0]), sizeof(ifname) - 1); - strncpy(event, blobmsg_data(tb[1]), sizeof(event) - 1); - - add = !strcmp(event, "connected"); - del = !strcmp(event, "disconnected"); - - if (add || del) { - struct blob_attr *data[1]; - static const struct blobmsg_policy data_attr[1] = { - [0] = { .name = "macaddr", .type = BLOBMSG_TYPE_STRING }, - }; - - char mac_str[18] = {0}; - uint8_t mac[6] = {0}; - - blobmsg_parse(data_attr, 1, data, blobmsg_data(tb[2]), - blobmsg_data_len(tb[2])); - - if (!data[0]) - return; - - strncpy(mac_str, blobmsg_data(data[0]), sizeof(mac_str) - 1); - - if (!hwaddr_aton(mac_str, mac)) - return; - - if (add) - host_topo_node_add(p, NULL, mac, 1); - - else if (del) - host_topo_node_del(p, NULL, mac, 1); - } -} - int host_get_interface_type(struct topologyd_private *priv) { struct json_object *arr; @@ -1226,7 +1113,7 @@ int host_get_neigh_status(uint8_t *mac_addr) dbg("Inside %s %d\n", __func__, __LINE__); if (mac_addr == NULL) - return NULL; + return -1; hwaddr_ntoa(mac_addr, macaddr_str); snprintf(cmd, 256, NEIGH_CMD, macaddr_str); @@ -1258,9 +1145,9 @@ int host_get_neigh_status(uint8_t *mac_addr) } /* -{ "topology.host": {"action":"connect","macaddr":"fe:ce:90:d7:5e:64","ipaddr":"192.168.1.157","network":"lan"} } -{ "topology.host": {"action":"disconnect","macaddr":"fe:ce:90:d7:5e:64","ipaddr":"192.168.1.157","network":"lan"} } -*/ + *{ "topology.host": {"action":"connect","macaddr":"fe:ce:90:d7:5e:64","ipaddr":"192.168.1.157","network":"lan"} } + *{ "topology.host": {"action":"disconnect","macaddr":"fe:ce:90:d7:5e:64","ipaddr":"192.168.1.157","network":"lan"} } + */ void host_send_client_event(struct topologyd_private *priv, struct host_node *p, uint8_t state) { @@ -1323,7 +1210,6 @@ void host_run_status_check(struct topologyd_private *priv) if (!priv) return; host_get_interface_type(priv); - host_get_network_dump(priv); for (i = 0; i < NODE_HTABLE_SIZE; i++) { if (hlist_empty(&priv->host.node_htable[i])) @@ -1545,10 +1431,7 @@ void host_set_interface_type(struct host_node *p) return; hwaddr_ntoa(p->hwaddr, mac_str); - if (p->intf_type == HOST_TYPE_WIFI) - config_set_host_option("hosts", "host", "macaddr", mac_str, "interface_type", "wifi"); - else - config_set_host_option("hosts", "host", "macaddr", mac_str, "interface_type", "eth"); + config_set_host_option("hosts", "host", "macaddr", mac_str, "interface_type", + (p->intf_type == HOST_TYPE_WIFI ? "wifi" : "eth")); config_set_host_option("hosts", "host", "macaddr", mac_str, "active", "1"); - } diff --git a/src/host.h b/src/host.h index aa04d24b39fa4d9a853161af158f860034de003c..4c3115f3368a7da51cbaf791dcf61b00f2594632 100644 --- a/src/host.h +++ b/src/host.h @@ -17,7 +17,6 @@ #define HOST_NODE_MAX 500 #define HOST_MAX_IPV6ADDR 20 #define HOST_MAX_IPV4ADDR 20 -#define WIFI_EVENTS "wifi.sta" #define HOST_REFRESH_TIMER 15 #define NEIGH_CMD "ip neigh show nud reachable|awk '{print $5}'|grep %s" #define ARPING_CMD "arping -I br-lan -c 1 -w 1 -D %s| grep Received| awk '{print $2}'" @@ -97,9 +96,6 @@ int host_get_ipaddr_hostname(struct topologyd_private *priv, char *macaddr_str, struct host_node *p); void host_append_dhcpv6_info(struct host_node *p); int host_append_dhcpv4_info(char *mac_addr, struct host_node *p); -void host_event_handler(struct ubus_context *ctx, - struct ubus_event_handler *ev, const char *type, - struct blob_attr *msg); int host_add_netlink(int *fd); void host_netlink_cb(struct uloop_fd *fd, unsigned int events); void host_run_status_check(struct topologyd_private *priv); @@ -111,4 +107,5 @@ void host_agent_event_handler(struct ubus_context *ctx, void host_set_interface_type(struct host_node *p); int host_get_arping_status(char *ipaddr_str); void host_arp_table_refresh(struct uloop_timeout *t); +void host_get_network_dump(struct topologyd_private *priv); #endif /* HOSTD_H */ diff --git a/src/host_utils.c b/src/host_utils.c index 87c9fab82d7f1e3cdfa72f7adcfb60fa7ded9e6a..79d546188d977f3abeb46b15f5afc38177cedc69 100644 --- a/src/host_utils.c +++ b/src/host_utils.c @@ -157,8 +157,10 @@ host_send_arping(const char *targetIP, const char *device, int toms, int is_recv send_pack(&src, &dst, &me, &he); /*Here we are just sending the arp packet and returning*/ - if (is_recv != 1) + if (is_recv != 1) { + close(sock_fd); return 1; + } socklen_t alen = sizeof(from); bool connected = false; @@ -256,6 +258,8 @@ int host_get_netlink_ip(uint8_t *mac_addr, char *ipv4_str, char *device) if (addr_s == NULL) { /* error */ err("nl_addr2str failed\n"); + nl_cache_free(cache); + nl_socket_free(sk); return -1; } @@ -263,8 +267,11 @@ int host_get_netlink_ip(uint8_t *mac_addr, char *ipv4_str, char *device) if (ret == 0) { strncpy(ipv4_str, addr_str, 24); ifname = if_indextoname(ifindex, device); - if (ifname == NULL) + if (ifname == NULL) { + nl_cache_free(cache); + nl_socket_free(sk); return -1; + } dbg("intfname = [%s] ipv4addr= [%s] mac=["MACFMT"]\n", device, ipv4_str, MAC2STR(hwaddr)); break; } diff --git a/src/topologyd.c b/src/topologyd.c index df0d359777acbad635e159143ea8b7ee970a77e3..f153979422ff3ea1420efdd3161e5e320383a70b 100644 --- a/src/topologyd.c +++ b/src/topologyd.c @@ -22,15 +22,10 @@ #include "config.h" #include "json_utils.h" -static int signal_pending; extern const char *ubus_socket; uint32_t topology_log_max; static bool g_registrar_freq_band[3]; -static void topologyd_sighandler(int sig) -{ - signal_pending = sig; -} static void ieee1905_info_response_cb(struct ubus_request *req, int mtype, struct blob_attr *msg) @@ -1320,22 +1315,29 @@ static void topologyd_start_heartbeat(struct uloop_timeout *t) { struct topologyd_private *priv = container_of(t, struct topologyd_private, heartbeat); + sigset_t waiting_mask; - if (!signal_pending) - uloop_timeout_set(&priv->heartbeat, 1 * 1000); + sigpending(&waiting_mask); - switch (signal_pending) { - case SIGTERM: - case SIGINT: - //topologyd_exit(priv); + if (sigismember(&waiting_mask, SIGINT)) { + dbg("|%s:%d| Received SIGINT, terminating\n", __func__, __LINE__); exit(0); - break; - case SIGHUP: - //topologyd_reload(priv); - break; - default: - break; } + + if (sigismember(&waiting_mask, SIGTERM)) { + dbg("|%s:%d| Received SIGSTP, terminating\n", __func__, __LINE__); + exit(0); + } + + if (sigismember(&waiting_mask, SIGHUP)) { + + dbg("|%s:%d| Received SIGHUP, reload network config\n", __func__, __LINE__); + + signal(SIGHUP, SIG_IGN); + //Here reload the network interface in the hosts + host_get_network_dump(priv); + } + uloop_timeout_set(&priv->heartbeat, 1 * 1000); } static int topologyd_run(struct topologyd_private *priv) @@ -1430,10 +1432,14 @@ int topologyd_start(void) { struct topologyd_private *priv; - signal_pending = 0; - set_sighandler(SIGINT, topologyd_sighandler); - set_sighandler(SIGTERM, topologyd_sighandler); - set_sighandler(SIGHUP, topologyd_sighandler); + sigset_t base_mask; + + sigemptyset(&base_mask); + sigaddset(&base_mask, SIGINT); + sigaddset(&base_mask, SIGTERM); + sigaddset(&base_mask, SIGHUP); + + sigprocmask(SIG_SETMASK, &base_mask, NULL); set_sighandler(SIGPIPE, SIG_IGN); priv = calloc(1, sizeof(*priv)); @@ -1467,11 +1473,6 @@ int topologyd_start(void) priv->ev.cb = topologyd_event_handler; ubus_register_event_handler(priv->ctx, &priv->ev, IEEE1905_EVENTS); - //Here add the event hadler for the host - //wifi connection events - priv->wifi_evh.cb = host_event_handler; - ubus_register_event_handler(priv->ctx, &priv->wifi_evh, WIFI_EVENTS); - //Here we are adding the event for topology notification //message extention priv->agent_evh.cb = host_agent_event_handler; @@ -1499,6 +1500,7 @@ int topologyd_start(void) /*Here we need to fill the host structures*/ config_init_host_data("hosts", "host", priv); + host_get_network_dump(priv); if (!priv->config.enabled) { info("topologyd not enabled.\n"); diff --git a/src/topologyd.h b/src/topologyd.h index 9f996e6ab9477411a212a8e78900fd0fd308aafc..8adbcf7c919bc299e689dd5cc75142c14b02f1ca 100644 --- a/src/topologyd.h +++ b/src/topologyd.h @@ -215,7 +215,6 @@ struct topologyd_private { enum { INCOMPLETE, AVAILABLE } status; struct ubus_context *ctx; struct ubus_event_handler ev; - struct ubus_event_handler wifi_evh; struct ubus_event_handler agent_evh; struct uloop_fd uloop_fd; struct uloop_timeout heartbeat;