Commit b7556dcf authored by Anjan Chanda's avatar Anjan Chanda Committed by Jakob Olsson
Browse files

misc cleanup and refactor

parent 5763d405
Pipeline #35679 passed with stages
in 1 minute and 24 seconds
......@@ -5,11 +5,12 @@ include:
stages:
- static_code_analysis
- functional_test
- functional_api_test
- functional_api_test
variables:
DEBUG: 'TRUE'
SOURCE_FOLDER: "src"
RUN_CPPCHECK: "cppcheck --enable=all --error-exitcode=1 --suppress=unusedFunction --suppress=unreadVariable --suppress=variableScope --suppress=syntaxError ."
run_functional_test:
stage: functional_test
......
......@@ -16,10 +16,13 @@
#include <libubus.h>
#include <libubox/utils.h>
#include <easy/easy.h>
#include "timer.h"
#include "cmdu_ackq.h"
#include "host_config.h"
#include "topologyd.h"
static int timeradd_msecs(struct timeval *a, unsigned long msecs,
struct timeval *res)
{
......
......@@ -27,6 +27,7 @@
#include <uci.h>
#include "debug.h"
#include "host_config.h"
#include "topologyd.h"
#include "config.h"
......
......@@ -9,27 +9,32 @@
#include <stdbool.h>
#include <pthread.h>
#include <json-c/json.h>
#include <libubox/blobmsg.h>
#include <libubox/blobmsg_json.h>
#include <libubox/uloop.h>
#include <libubus.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <json-c/json.h>
#include <libubox/blobmsg.h>
#include <libubox/blobmsg_json.h>
#include <libubox/uloop.h>
#include <libubus.h>
#include <uci.h>
#include <easy/easy.h>
#include "host.h"
#include "host_config.h"
#include "debug.h"
#include "host_config.h"
#include "topologyd.h"
#include "host_nodes.h"
#include "config.h"
#include "json_utils.h"
#include "host_utils.h"
#include "host.h"
extern const char *ubus_socket;
......@@ -89,7 +94,7 @@ static void uobjx_lookup(struct ubus_context *ctx, struct ubus_object_data *obj,
p = strstr(obj->path, token);
if (p != NULL) {
strcpy(str->ifname[str->count], obj->path);
strncpy(str->ifname[str->count], obj->path, 16);
str->count = str->count + 1;
}
}
......@@ -135,7 +140,6 @@ void host_dump_node_nbr(struct topologyd_private *priv, struct blob_buf *b)
char mac_str[18] = { 0x0 };
void *nodes_array;
struct host_node *p;
int i;
void *table1 = NULL;
struct tm *info;
time_t tmp_t;
......@@ -255,7 +259,9 @@ struct host_node *host_check_get_node(struct topologyd_private *priv, uint8_t *m
}
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 *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 };
......@@ -278,9 +284,9 @@ struct host_node *host_topo_node_add(struct topologyd_private *priv, struct node
return NULL;
p->is_ieee1905_node = 1;
// TODO review the if case
if (p->intf_type != HOST_TYPE_WIFI || (p->intf_type == HOST_TYPE_WIFI && is_wifi))
if (p->intf_type != HOST_TYPE_WIFI || is_wifi)
host_change_active_state(priv, p, 1);
host_get_wifi_data(priv, p);
} else if (mac_addr != NULL) {
......@@ -291,7 +297,7 @@ struct host_node *host_topo_node_add(struct topologyd_private *priv, struct node
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))
if (p->intf_type != HOST_TYPE_WIFI || is_wifi)
host_change_active_state(priv, p, 1);
}
......@@ -300,14 +306,20 @@ struct host_node *host_topo_node_add(struct topologyd_private *priv, struct node
if (ret != 0)
err("Unable to fetch interface type\n");
} else {
config_set_host_option("hosts", "host", "macaddr", mac_str, "interface_type", "wifi", priv->host_global_cfg.persistent);
config_set_host_option("hosts", "host", "macaddr",
mac_str, "interface_type",
"wifi",
priv->host_global_cfg.persistent);
p->intf_type = HOST_TYPE_WIFI;
}
config_set_host_option("hosts", "host", "macaddr", mac_str, "interface_type",
(p->intf_type == HOST_TYPE_WIFI ? "wifi" : "eth"), priv->host_global_cfg.persistent);
config_set_host_option("hosts", "host", "macaddr", mac_str,
"interface_type",
(p->intf_type == HOST_TYPE_WIFI ? "wifi" : "eth"),
priv->host_global_cfg.persistent);
}
bool has_ip = p->is_ipaddr;
//bool has_ip = p->is_ipaddr;
ret = host_get_ipaddr_hostname(priv, mac_str, p);
if (ret != 0) {
......@@ -322,12 +334,14 @@ struct host_node *host_topo_node_add(struct topologyd_private *priv, struct node
if (p && p->al_node)
host_topo_node_add(priv, p->al_node, NULL, is_wifi);
host_topo_node_add(priv, NULL, p->al_node->hwaddr, is_wifi);
return p;
}
struct host_node *host_topo_node_del(struct topologyd_private *priv, struct node *node, uint8_t *mac_addr, uint8_t is_wifi)
struct host_node *host_topo_node_del(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 };
......@@ -370,12 +384,15 @@ struct host_node *host_topo_node_del(struct topologyd_private *priv, struct node
}
}
if (p && is_wifi) {
config_set_host_option("hosts", "host", "macaddr", mac_str, "interface_type", "wifi", priv->host_global_cfg.persistent);
config_set_host_option("hosts", "host", "macaddr", mac_str,
"interface_type",
"wifi",
priv->host_global_cfg.persistent);
p->intf_type = HOST_TYPE_WIFI;
}
if (p && p->al_node)
host_topo_node_del(priv, p->al_node, NULL, is_wifi);
host_topo_node_del(priv, NULL, p->al_node->hwaddr, is_wifi);
if (p) {
p->is_event = 0;
......@@ -406,7 +423,7 @@ int host_append_dhcpv4_info(char *mac_addr, struct host_node *p)
if (leases != NULL) {
while (fgets(line, sizeof(line), leases) != NULL) {
remove_newline(line);
if (sscanf(line, "%ld %24s %24s %64s %256s", &leasetime, macaddr, ipaddr, hostname, clid) == 5) {
if (sscanf(line, "%ld %23s %23s %63s %255s", &leasetime, macaddr, ipaddr, hostname, clid) == 5) {
dbg("%s %d ipv4addr = [%s]\n", __func__, __LINE__, ipaddr);
if (strcasecmp(mac_addr, macaddr) == 0) {
if (found == 0) {
......@@ -450,7 +467,7 @@ void host_append_dhcpv6_info(struct host_node *p)
while (fgets(line, sizeof(line), hosts6) != NULL) {
remove_newline(line);
if (sscanf(line, "%64s %128s", ip6addr, hostname)) {
if (sscanf(line, "%127s %63s", ip6addr, hostname)) {
if (strcasecmp(p->hostname, hostname) == 0) {
dbg("ipv6 address %s %d\n", __func__, __LINE__);
strncpy(p->ipv6addr[p->ipv6addr_count], ip6addr, 128);
......@@ -540,7 +557,7 @@ void host_get_network(struct topologyd_private *priv, struct host_node *p)
for (j = 0; j < ipv4_len; j++) {
struct in_addr ip, snet, host, rslt;
struct json_object *jobj_ptr, *iface;
char *ipaddr;
const char *ipaddr;
int netmask;
uint32_t mask = 0;
......@@ -680,7 +697,7 @@ static void get_ethernet_interface(struct host_node *host, char *bridge, int por
struct dirent *ent;
FILE *pfile;
char path_brif[256];
char path_portno[512];
char path_portno[1024];
unsigned int pno = 0;
dbg("Inside %s %d\n", __func__, __LINE__);
......@@ -697,7 +714,7 @@ static void get_ethernet_interface(struct host_node *host, char *bridge, int por
if (ent->d_name[0] == '.')
continue;
snprintf(path_portno, 512, "/%s/%s/port_no", path_brif, ent->d_name);
snprintf(path_portno, 1024, "/%s/%s/port_no", path_brif, ent->d_name);
pfile = fopen(path_portno, "r");
if (pfile != NULL) {
......@@ -800,7 +817,8 @@ int host_get_ipaddr_hostname(struct topologyd_private *priv,
host_get_network(priv, p);
get_bridge_info(p, device);
if (p->intf_type == HOST_TYPE_WIFI && p->is_ieee1905_node || (p->intf_type == HOST_TYPE_WIFI && !p->is_in_assoclist)) {
if ((p->intf_type == HOST_TYPE_WIFI && p->is_ieee1905_node) ||
(p->intf_type == HOST_TYPE_WIFI && !p->is_in_assoclist)) {
strncpy(p->device, device, 16);
get_ethernet_interface(p, device, p->port_no);
if (p->is_device_mac == 1) {
......@@ -854,7 +872,7 @@ bool host_is_in_assoclist(struct topologyd_private *priv, uint8_t *hwaddr)
len = json_object_array_length(sta_array);
for (i = 0; i < len; i++) {
struct json_object *sta_obj, *sta_obj_macaddr, *jobj_ptr, *stat_obj;
struct json_object *sta_obj, *sta_obj_macaddr;
unsigned char macaddr[6] = {0};
sta_obj = json_object_array_get_idx(sta_array, i);
......@@ -1171,9 +1189,11 @@ int host_get_neigh_status(uint8_t *mac_addr)
return -1;
hwaddr_ntoa(mac_addr, macaddr_str);
snprintf(cmd, 256, NEIGH_CMD, macaddr_str);
snprintf(cmd, 256,
"ip neigh show nud reachable|awk '{print $5}'|grep %s",
macaddr_str);
fp = popen(cmd, "r");
fp = popen(cmd, "r"); /* flawfinder: ignore */
if (fp == NULL) {
err("unable to execute command");
return -1;
......@@ -1502,10 +1522,11 @@ int topology_update_client_assoc_event(struct topologyd_private *priv, struct tl
}
int topology_update_host_assoc_client(struct topologyd_private *priv,
struct tlv_assoc_client *tlv)
struct tlv_assoc_client *tlv)
{
struct host_node *hn = NULL;
int i, offset = 0;
uint8_t *pos = (uint8_t *) tlv;
if (tlv == NULL || priv == NULL)
return -1;
......@@ -1519,10 +1540,10 @@ int topology_update_host_assoc_client(struct topologyd_private *priv,
int j;
dbg("\t\tbssid: " MACFMT "\n",
MAC2STR(&tlv[offset]));
MAC2STR(&pos[offset]));
offset += 6;
num_client = BUF_GET_BE16(tlv[offset]);
num_client = BUF_GET_BE16(pos[offset]);
offset += 2;
dbg("\t\tassoc_clients_nr: %u\n", num_client);
......@@ -1531,25 +1552,25 @@ int topology_update_host_assoc_client(struct topologyd_private *priv,
uint8_t client_addr[6];
dbg("\t\t\tclient_addr is: " MACFMT "\n",
MAC2STR(&tlv[offset]));
memcpy(client_addr, &tlv[offset], 6);
MAC2STR(&pos[offset]));
memcpy(client_addr, &pos[offset], 6);
//Here look at the host node of this assoc client and update the time
hn = host_node_lookup(priv->host.node_htable, &tlv[offset]);
hn = host_node_lookup(priv->host.node_htable, &pos[offset]);
if (!hn) {
hn = host_topo_node_add(priv, NULL, &tlv[offset], 1);
hn = host_topo_node_add(priv, NULL, &pos[offset], 1);
if (hn->is_ipaddr)
host_send_client_event(priv, hn, 1);
} else {
if (hn->active == 0) {
hn = host_topo_node_add(priv, NULL, &tlv[offset], 1);
hn = host_topo_node_add(priv, NULL, &pos[offset], 1);
if (hn->is_ipaddr)
host_send_client_event(priv, hn, 1);
}
}
hn->age_time = time(NULL);
offset += 6;
conntime = BUF_GET_BE16(tlv[offset]);
conntime = BUF_GET_BE16(pos[offset]);
offset += 2;
dbg("\t\t\tuptime: 0x%04x\n", conntime);
}
......@@ -1658,6 +1679,7 @@ void host_send_topology_query_all(struct topologyd_private *priv)
}
}
#if 0
static void host_mid_response_cb(struct ubus_request *req,
int mtype, struct blob_attr *msg)
{
......@@ -1683,6 +1705,7 @@ static void host_mid_response_cb(struct ubus_request *req,
return;
}
#endif
struct cmdu_buff *host_topology_query_map_tlv(void)
{
......@@ -1738,27 +1761,6 @@ int host_send_mid_topology_query(struct topologyd_private *p, uint8_t *dst_mac)
return 0;
}
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)
{
......@@ -1824,25 +1826,50 @@ void host_wifi_sta_event_handler(void *c, struct blob_attr *msg)
}
}
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);
}
bool is_local_mapagent_available(void)
{
bool enable = false;
struct stat sb;
int ret = 0;
dbg("Inside %s access %d\n", __func__, access("/etc/config/mapagent", F_OK|R_OK));
if (!access("/etc/config/mapagent", F_OK|R_OK)) {
ret = host_config_mapagent_get(&enable);
if (ret == 0) {
if (enable == true)
return true;
}
dbg("Inside %s ===>\n", __func__);
ret = stat("/etc/config/mapagent", &sb);
if (ret)
return false;
ret = host_config_mapagent_get(&enable);
if (ret == 0) {
if (enable == true)
return true;
}
return false;
}
bool host_create_file(void)
{
if (!access("/var/state/hosts", F_OK|R_OK))
if (!access("/var/state/hosts", F_OK|R_OK)) /* flawfinder: ignore */
return true;
else {
FILE *fptr = NULL;
......
......@@ -154,4 +154,18 @@ int topology_update_client_assoc_event(struct topologyd_private *priv, struct tl
int topology_update_host_assoc_client(struct topologyd_private *priv, struct tlv_assoc_client *p);
bool host_is_in_assoclist(struct topologyd_private *priv, uint8_t *hwaddr);
void host_change_active_state(struct topologyd_private *priv, struct host_node *p, uint8_t state);
int host_copy_node_data(struct host_node *src_host, struct host_node *dest_host,
struct topologyd_private *priv);
void host_send_client_event(struct topologyd_private *priv, struct host_node *p, uint8_t state);
bool host_create_file(void);
bool is_local_mapagent_available(void);
int host_get_wifi_data(struct topologyd_private *priv, struct host_node *p);
void host_send_topology_query_all(struct topologyd_private *priv);
#endif /* HOSTD_H */
......@@ -28,8 +28,9 @@
#include <uci.h>
#include "debug.h"
#include "host.h"
#include "host_config.h"
#include "topologyd.h"
#include "host.h"
struct uci_section *config_get_section(struct uci_context *ctx,
struct uci_package *pkg, const char *type, const char *key,
......@@ -64,9 +65,6 @@ int config_del_section(const char *config, const char *type, const char *value,
struct uci_section *section;
struct uci_ptr ptr = {0};
int rv = -1;
static const char s[2] = ":";
char *token;
char mac_name[18] = { 0 };
dbg("Inside %s %d the value is %s\n", __func__, __LINE__, value);
......@@ -109,20 +107,20 @@ struct uci_section *config_add_section(struct uci_context *ctx,
struct uci_ptr ptr = {0};
int rv = -1;
char name_section[50] = { 0 };
dbg("Inside %s %d\n", __func__, __LINE__);
static const char s[2] = ":";
char *token;
char mac_name[18] = { 0 };
strcpy(mac_name, value);
dbg("Inside %s %d\n", __func__, __LINE__);
strncpy(mac_name, value, 18);
/* get the first token */
token = strtok(mac_name, s);
/* walk through other tokens */
while (token != NULL) {
strcat(name_section, token);
snprintf(name_section + strlen(name_section),
sizeof(name_section), "%s", token);
token = strtok(NULL, s);
}
......
......@@ -31,4 +31,8 @@ int config_add_default_host_mac(const char *config, const char *type,
bool config_set_host_option(char *package_name,
char *section_type, char *search_key, char *search_val,
char *option, char *value, bool persistent);
bool config_get_al_ifname(char *ifname);
#endif /* HOSTD_CONFIG_H */
......@@ -13,10 +13,14 @@
#include <libubox/list.h>
#include <libubox/uloop.h>
#include <libubus.h>
#include <uci.h>
#include <easy/easy.h>
#include "debug.h"
#include "host_config.h"
#include "topologyd.h"
#include "host.h"
#include "mdns_avahi.h"
......
#ifndef NODES_H
#define NODES_H
#ifndef HOST_NODES_H
#define HOST_NODES_H
#include <stdint.h>
#include <easy/easy.h>
struct host_node *host_node_create(uint8_t *hwaddr);
void host_node_free(struct host_node *n);
......@@ -13,4 +10,4 @@ int host_node_del(struct hlist_head *table, uint8_t *hwaddr);
struct host_node *host_node_lookup(struct hlist_head *table, uint8_t *hwaddr);
void host_node_print_all(struct hlist_head *table);
#endif /* NODES_H */
#endif /* HOST_NODES_H */
......@@ -18,12 +18,55 @@
* along with this program; If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#include "host_utils.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <net/if_arp.h>
#include <netinet/ether.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <libubus.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink/route/rtnl.h>
#include <netlink/route/neighbour.h>
#include <netlink/route/addr.h>
#include <netlink/route/link.h>
#include <netlink/genl/ctrl.h>
#include <netlink/genl/genl.h>
#include <netlink/attr.h>
#include <uci.h>
#include <easy/easy.h>
#include "debug.h"
#include "host_config.h"
#include "topologyd.h"
#include "host.h"
#include "host_utils.h"
#ifndef TLV_TYPE_END_OF_MESSAGE
#define TLV_TYPE_END_OF_MESSAGE 0x00
#endif
struct in_addr src;
struct in_addr dst;
......@@ -38,8 +81,8 @@ struct nl_req_s {
struct rtgenmsg gen;
};
static int
send_pack(struct in_addr *src_addr, struct in_addr *dst_addr, struct sockaddr_ll *ME, struct sockaddr_ll *HE)
static int send_pack(struct in_addr *src_addr, struct in_addr *dst_addr,
struct sockaddr_ll *ME, struct sockaddr_ll *HE)
{
int err;
unsigned char buf[256];
......@@ -62,8 +105,7 @@ send_pack(struct in_addr *src_addr, struct in_addr *dst_addr, struct sockaddr_ll
return err;
}
static bool
recv_pack(char *buf, int len, struct sockaddr_ll *FROM)
static bool recv_pack(char *buf, int len, struct sockaddr_ll *FROM)
{
struct arphdr *ah = (struct arphdr *) buf;
unsigned char *p = (unsigned char *) (ah + 1);
......@@ -104,8 +146,7 @@ recv_pack(char *buf, int len, struct sockaddr_ll *FROM)
}
bool
host_send_arping(const char *targetIP, const char *device, int toms, int is_recv)
bool host_send_arping(const char *targetIP, const char *device, int toms, int is_recv)
{
struct sockaddr_in saddr;
struct ifreq ifr;
......@@ -280,7 +321,9 @@ int host_get_netlink_ip(uint8_t *mac_addr, char *ipv4_str, char *device)
nl_socket_free(sk);
return -1;
}
dbg("intfname = [%s] ipv4addr= [%s] mac=["MACFMT"]\n", device, ipv4_str, MAC2STR(hwaddr));
dbg("intfname = [%s] ipv4addr= [%s] mac=["MACFMT"]\n",
device, ipv4_str, MAC2STR(hwaddr));
nl_object_put((struct nl_object *) neigh);
break;
}
......@@ -301,11 +344,10 @@ 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;