diff options
author | charan makkina <charan795m@gmail.com> | 2019-04-30 17:40:53 +0530 |
---|---|---|
committer | charan makkina <charan795m@gmail.com> | 2019-05-20 18:14:40 +0530 |
commit | a826fe833d3f2a8fe2673fa05811fe1a22baf045 (patch) | |
tree | da11a17c46ca9b8a002a52a290628574fa3f5eda /src/nSocket/nstack_rd | |
parent | 3e6bf7b64eea418c59959c18750261b815b2892c (diff) |
Feature: 19.04 part 1
Change-Id: Ibba924b8deca1f246b9dcb12d89d085b6fd33046
Signed-off-by: charan makkina <charan795m@gmail.com>
Diffstat (limited to 'src/nSocket/nstack_rd')
-rw-r--r-- | src/nSocket/nstack_rd/nstack_rd.c | 776 | ||||
-rw-r--r-- | src/nSocket/nstack_rd/nstack_rd_init.c | 171 | ||||
-rw-r--r-- | src/nSocket/nstack_rd/nstack_rd_ip.c | 387 | ||||
-rw-r--r-- | src/nSocket/nstack_rd/nstack_rd_ip.h | 16 | ||||
-rw-r--r-- | src/nSocket/nstack_rd/nstack_rd_ip6.c | 261 | ||||
-rw-r--r-- | src/nSocket/nstack_rd/nstack_rd_ip6.h | 25 | ||||
-rw-r--r-- | src/nSocket/nstack_rd/nstack_rd_priv.h | 101 | ||||
-rw-r--r-- | src/nSocket/nstack_rd/nstack_rd_proto.c | 251 | ||||
-rw-r--r-- | src/nSocket/nstack_rd/nstack_rd_proto.h | 25 | ||||
-rw-r--r-- | src/nSocket/nstack_rd/nstack_rd_type.c | 229 | ||||
-rw-r--r-- | src/nSocket/nstack_rd/nstack_rd_type.h | 42 |
11 files changed, 1457 insertions, 827 deletions
diff --git a/src/nSocket/nstack_rd/nstack_rd.c b/src/nSocket/nstack_rd/nstack_rd.c index 650f165..0c7d127 100644 --- a/src/nSocket/nstack_rd/nstack_rd.c +++ b/src/nSocket/nstack_rd/nstack_rd.c @@ -15,58 +15,72 @@ */ #include <stdlib.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> #include "nstack_rd.h" -#include "nstack_rd_init.h" #include "nstack_rd_priv.h" #include "nstack_rd_ip.h" +#include "nstack_rd_ip6.h" +#include "nstack_rd_type.h" #include "nstack_rd_proto.h" #include "nstack_log.h" -#include "nstack_info_parse.h" #include "nstack_securec.h" +#include <arpa/inet.h> -typedef struct __rd_data_defaut_ip -{ - char ip[RD_IP_STR_MAX_LEN]; - char stackname[RD_PLANE_NAMELEN]; - int masklent; -} rd_data_defaut_ip; +extern rd_route_table *g_rd_table_handle[]; -typedef struct __rd_data_defaut_protocol -{ - unsigned int proto_type; - char stackname[RD_PLANE_NAMELEN]; -} rd_data_defaut_protocol; +rd_local_data *g_rd_local_data = NULL; rd_data_proc g_rd_cpy[RD_DATA_TYPE_MAX] = { - { - nstack_rd_ipdata_cpy, - nstack_rd_ip_item_insert, - nstack_rd_ip_item_age, - nstack_rd_ip_item_find, - nstack_rd_ip_spec, - nstack_rd_ip_default, - }, - { - nstack_rd_proto_cpy, - nstack_rd_proto_item_insert, - nstack_rd_proto_item_age, - nstack_rd_proto_item_find, - nstack_rd_proto_spec, - nstack_rd_proto_default, - }, + { + nstack_rd_ip_data_cpy, + nstack_rd_ip_item_insert, + nstack_rd_ip_item_age, + nstack_rd_ip_item_clean, + nstack_rd_ip_item_find, + nstack_rd_ip_spec, + }, + { + nstack_rd_ip6_data_cpy, + nstack_rd_ip6_item_insert, + nstack_rd_ip6_item_age, + nstack_rd_ip6_item_clean, + nstack_rd_ip6_item_find, + nstack_rd_ip6_spec, + }, + { + nstack_rd_type_data_cpy, + nstack_rd_type_item_insert, + nstack_rd_type_item_age, + nstack_rd_type_item_clean, + nstack_rd_type_item_find, + nstack_rd_type_spec, + }, + { + nstack_rd_proto_data_cpy, + nstack_rd_proto_item_insert, + nstack_rd_proto_item_age, + nstack_rd_proto_item_clean, + nstack_rd_proto_item_find, + nstack_rd_proto_spec, + } }; -rd_data_defaut_ip g_default_ip_config[] = { - {{"127.0.0.1"}, {RD_KERNEL}, 32}, - {{"0.0.0.0"}, {RD_KERNEL}, 32}, -}; +int nstack_choose_highest_prio() +{ + int i; + int ret = 0; + int highest = 0x7FFFFFFF; + nstack_rd_stack_info *stack_info = g_rd_local_data->pstack_info; -rd_data_defaut_protocol g_default_protcol[] = { - {0xf001, {RD_LWIP}}, -}; + for (i = 0; i < NSTACK_NUM; i++) + { + if (stack_info[i].priority < highest) + { + highest = stack_info[i].priority; + ret = i; + } + } + return ret; +} /***************************************************************************** * Prototype : nstack_rd_get_stackid @@ -78,256 +92,414 @@ rd_data_defaut_protocol g_default_protcol[] = { * Calls : * Called By : *****************************************************************************/ -int -nstack_rd_get_stackid (nstack_rd_key * pkey, int *stackid) +int nstack_rd_get_stackid(nstack_rd_key * pkey, rd_data_item * item) { - int type = 0; - int ret = NSTACK_RD_SUCCESS; - rd_data_item item; - if ((!pkey) || (!stackid) || (pkey->type >= RD_DATA_TYPE_MAX)) + int accumulate = 0; + int icnt = 0; + int type = 0; + int ret = NSTACK_RD_SUCCESS; + int rdtbl_ver = 0; + if ((!pkey) || (!item) || (pkey->type >= RD_DATA_TYPE_MAX)) { - NSSOC_LOGERR ("input get stackid fail]addr=%p,stackid=%p,addr->type=%d", - pkey, stackid, !pkey ? RD_DATA_TYPE_MAX : pkey->type); - return NSTACK_RD_FAIL; + NSSOC_LOGERR("input get stackid fail]addr=%p,item=%p,addr->type=%d", + pkey, item, !pkey ? RD_DATA_TYPE_MAX : pkey->type); + return NSTACK_RD_FAIL; } - int retVal = MEMSET_S (&item, sizeof (item), 0, sizeof (item)); - if (EOK != retVal) + /*add return value check */ + int retVal = + memset_s(item, sizeof(rd_data_item), 0, sizeof(rd_data_item)); + if (EOK != retVal) { - NSSOC_LOGERR ("MEMSET_S failed]retVal=%d", retVal); - return NSTACK_RD_FAIL; + NSSOC_LOGERR("memset_s failed]retVal=%d", retVal); + return NSTACK_RD_FAIL; } - type = pkey->type; + item->stack_id = -1; + type = pkey->type; - /*specfic key find, for ip example: stack-x was chose if the key is multicast ip */ - if (g_rd_cpy[type].rd_item_spec) + /*specfic key find, for ip example: stackpool was chose if the key is multicast ip */ + if (g_rd_cpy[type].rd_item_spec) { - ret = g_rd_cpy[type].rd_item_spec ((void *) pkey); - if (ret >= 0) + ret = g_rd_cpy[type].rd_item_spec((void *) pkey); + if (ret >= 0) { - *stackid = ret; - return NSTACK_RD_SUCCESS; + item->stack_id = ret; + return NSTACK_RD_SUCCESS; } } - /*search the list */ - ret = - g_rd_cpy[type].rd_item_find (NSTACK_RD_LIST (type), (void *) pkey, &item); - if (NSTACK_RD_SUCCESS == ret) + /*check ver */ + for (icnt = 0; icnt < NSTACK_NUM; icnt++) { - NSSOC_LOGDBG ("item type=%d stackid=%d was found", pkey->type, - item.stack_id); - *stackid = item.stack_id; - return NSTACK_RD_SUCCESS; + retVal = + g_rd_local_data->rdtbl_ver_get_fun(&rdtbl_ver, + g_rd_table_handle[icnt]); + if (retVal) + { + NSSOC_LOGWAR("stackid=%d get rd table ver failed!", + g_rd_local_data->pstack_info->stack_id); + if (++accumulate < NSTACK_NUM) + { + continue; + } + NSSOC_LOGERR("rd table ver get failed"); + return NSTACK_RD_FAIL; + } + if (g_rd_local_data->rdlocal_ver[icnt] != rdtbl_ver) + { + NSSOC_LOGINF + ("RD table ver unmatch]new rd tlbver:%d,local rd tblver:%d,resync now", + rdtbl_ver, g_rd_local_data->rdlocal_ver[icnt]); + ret = nstack_rd_sys(); + if (ret != NSTACK_RD_SUCCESS) + { + item->stack_id = nstack_choose_highest_prio(); + NSSOC_LOGERR + ("sync RD INF failed, choose highest priority stack, stackid=%d", + item->stack_id); + return NSTACK_RD_SUCCESS; + } + } } - if (g_rd_cpy[type].rd_item_default) + + /*search the list */ + ret = + g_rd_cpy[type].rd_item_find(NSTACK_RD_LIST(type), (void *) pkey, + item); + if (NSTACK_RD_SUCCESS == ret) { - *stackid = g_rd_cpy[type].rd_item_default ((void *) pkey); + NSSOC_LOGDBG("item type=%d stackid=%d was found", pkey->type, + item->stack_id); + return NSTACK_RD_SUCCESS; } - else + item->stack_id = nstack_choose_highest_prio(); + NSSOC_LOGINF + ("item type=%d was not found, choose highest priority stack by default, stackid=%d", + pkey->type, item->stack_id); + return NSTACK_RD_SUCCESS; +} + +static char *nstack_rd_parse_ip(char *pos, unsigned int *ip) +{ + char buf[16]; + char *p; + int len, ret; + + if (!pos) + return NULL; + + p = strchr(pos, '/'); + if (!p) + return NULL; + + len = p - pos; + if (len >= 16) + return NULL; + + ret = memcpy_s(buf, sizeof(buf), pos, len); + if (EOK != ret) { - *stackid = -1; + return NULL; } - NSSOC_LOGINF ("item type=%d was not found, return default=%d", pkey->type, - *stackid); - return NSTACK_RD_SUCCESS; + + buf[len] = 0; + + ret = inet_pton(AF_INET, buf, ip); + if (ret == 1) + return p + 1; + + return NULL; + } -/***************************************************************************** -* Prototype : nstack_rd_sys_default -* Description : sys default rd info, -* Input : None -* Output : None -* Return Value : int -* Calls : -* Called By : -******************************************************************************/ -void -nstack_rd_sys_default () +static char *nstack_rd_parse_ip6(char *pos, unsigned int ip[4]) +{ + char buf[46]; + char *p; + int len, ret; + + p = strchr(pos, '/'); + if (!p) + return NULL; + + len = p - pos; + if (len >= 46) + return NULL; + + (void) memcpy_s(buf, sizeof(buf), pos, len); + buf[len] = 0; + + ret = inet_pton(AF_INET6, buf, ip); + if (ret == 1) + return p + 1; + + return NULL; +} + +static char *nstack_rd_parse_stackid(char *pos, int *stack_id) { - rd_data_item item; - rd_data_defaut_ip *pdata = NULL; - rd_data_defaut_protocol *pprotodata = NULL; - int icnt = 0, iindex = 0; - int stack_num = 0; + size_t n = 0; + int i; - stack_num = g_rd_local_data->stack_num; + while (pos[n] != ':' && pos[n] != 0) + n++; + if (n == 0 || n >= RD_PLANE_NAMELEN) + return NULL; - /*get the ip default route */ - for (icnt = 0; - icnt < sizeof (g_default_ip_config) / sizeof (rd_data_defaut_ip); - icnt++) + for (i = 0; i < g_rd_local_data->stack_num; ++i) { - pdata = &g_default_ip_config[icnt]; - for (iindex = 0; iindex < stack_num; iindex++) + /* params are not NULL */ + if (0 == strncmp(pos, g_rd_local_data->pstack_info[i].name, n)) { - if (0 == - strcmp (g_rd_local_data->pstack_info[iindex].name, - pdata->stackname)) - { - item.stack_id = g_rd_local_data->pstack_info[iindex].stack_id; - break; - } + *stack_id = g_rd_local_data->pstack_info[i].stack_id; + return pos + n; } - if (iindex >= stack_num) + } + + return NULL; +} + +static void nstack_rd_sys_load_default() +{ + char *env, *pos; + + env = getenv("NSTACK_RD"); /*this func can be used */ + + if (!env || !env[0]) + return; + + pos = env; + while (*pos) + { + rd_data_item item; + char *pos6 = pos; + + pos = nstack_rd_parse_ip(pos, &item.ipdata.addr); + if (pos) { - NSSOC_LOGINF - ("default stack name:%s was not fount, ip:%s msklen:%d was dropped", - pdata->stackname, pdata->ip, pdata->masklent); - continue; + item.type = RD_DATA_TYPE_IP; + item.agetime = NSTACK_RD_AGETIME_MAX; + item.ipdata.resev[0] = 0; + item.ipdata.resev[1] = 0; + item.ipdata.masklen = (unsigned int) strtoul(pos, &pos, 10); + if (item.ipdata.masklen > 32 || *pos++ != '=') + { + NSSOC_LOGERR("nstack rd sys config error '%s'", env); + return; + } } - item.type = RD_DATA_TYPE_IP; - item.ipdata.addr = ntohl (inet_addr (pdata->ip)); - item.ipdata.masklen = pdata->masklent; - item.ipdata.resev[0] = 0; - item.ipdata.resev[1] = 0; - item.agetime = NSTACK_RD_AGETIME_MAX; - /*insert to the list */ - g_rd_cpy[RD_DATA_TYPE_IP].rd_item_inset (NSTACK_RD_LIST - (RD_DATA_TYPE_IP), &item); - } - - /*get the protocol default route */ - (void) MEMSET_S (&item, sizeof (item), 0, sizeof (item)); - for (icnt = 0; - icnt < sizeof (g_default_protcol) / sizeof (rd_data_defaut_protocol); - icnt++) - { - pprotodata = &g_default_protcol[icnt]; - for (iindex = 0; iindex < stack_num; iindex++) + else if (NULL != + (pos = nstack_rd_parse_ip6(pos6, item.ip6data.addr.addr32))) { - if (0 == - strcmp (g_rd_local_data->pstack_info[iindex].name, - pprotodata->stackname)) + item.type = RD_DATA_TYPE_IP6; + item.agetime = NSTACK_RD_AGETIME_MAX; + item.ip6data.masklen = (unsigned int) strtoul(pos, &pos, 10); + if (item.ip6data.masklen > 128 || *pos++ != '=') { - item.stack_id = g_rd_local_data->pstack_info[iindex].stack_id; - break; + NSSOC_LOGERR("nstack rd sys config error '%s'", env); + return; } } - if (iindex >= stack_num) + else { - NSSOC_LOGINF - ("default stack name:%s was not fount, protocoltype:%d was dropped", - pprotodata->stackname, pprotodata->proto_type); - continue; + NSSOC_LOGERR("nstack rd sys config error '%s'", env); + return; } - item.type = RD_DATA_TYPE_PROTO; - item.proto_type = pprotodata->proto_type; - /*insert to the list */ - g_rd_cpy[RD_DATA_TYPE_PROTO].rd_item_inset (NSTACK_RD_LIST - (RD_DATA_TYPE_PROTO), + + pos = nstack_rd_parse_stackid(pos, &item.stack_id); + if (!pos) + { + NSSOC_LOGERR("nstack rd sys config error '%s'", env); + return; + } + + (void) g_rd_cpy[item.type].rd_item_insert(NSTACK_RD_LIST(item.type), &item); - } - return; -} -/***************************************************************************** -* Prototype : nstack_rd_save -* Description : save the rd data -* Input : None -* Output : None -* Return Value : int -* Calls : -* Called By : -*****************************************************************************/ -void -nstack_rd_save (rd_route_data * rd_data, int num) -{ - int icnt = 0; - int iindex = 0; - int stack_num = 0; - rd_data_item item; - rd_data_type type = RD_DATA_TYPE_MAX; + if (item.type == RD_DATA_TYPE_IP6) + { + char buf[46]; + NSSOC_LOGINF("insert one RD %d=%s/%u", item.stack_id, + inet_ntop(AF_INET6, &item.ip6data.addr, buf, + sizeof(buf)), item.ip6data.masklen); + } + else + { + NSSOC_LOGINF("insert one RD %d:%u.%u.%u.%u/%u", item.stack_id, + item.ipdata.addr >> 24, + (item.ipdata.addr >> 16) & 255, + (item.ipdata.addr >> 8) & 255, + item.ipdata.addr & 255, item.ipdata.masklen); + } - int retVal = MEMSET_S (&item, sizeof (item), 0, sizeof (item)); - if (EOK != retVal) - { - NSSOC_LOGERR ("MEMSET_S failed]retVal=%d", retVal); - return; + if (*pos == ':') + pos++; } +} - stack_num = g_rd_local_data->stack_num; - - for (iindex = 0; iindex < num; iindex++) +static void nstack_rd_sys_clean() +{ + int type; + for (type = 0; type < RD_DATA_TYPE_MAX; type++) { - if (rd_data[iindex].type >= RD_DATA_TYPE_MAX) + if (!hlist_empty(&(NSTACK_RD_LIST(type)->headlist))) { - NSSOC_LOGERR ("rd data type=%d unkown", rd_data[iindex].type); - continue; + g_rd_cpy[type].rd_item_clean(NSTACK_RD_LIST(type)); } + } +} - type = rd_data[iindex].type; +static int nstack_rd_ip_get(rd_route_data ** data, int *num, int *ver, + rd_route_table * handle) +{ + rd_route_data *pdata = NULL; + rd_route_node *pnode = NULL; + size_t size = 0; + int icnt = 0; + int idex = 0; + int ret; + int rdver = 0; - if (NSTACK_RD_SUCCESS == - g_rd_cpy[type].rd_item_cpy ((void *) &item, - (void *) &rd_data[iindex])) + if (!handle || !data || !num || !ver) + { + NSSOC_LOGERR("nstack rd mng not inited or input err"); + return -1; + } + dmm_spin_lock_with_pid(&handle->rd_lock); + size = sizeof(rd_route_data) * handle->size; + pdata = (rd_route_data *) malloc(size); + if (!pdata) + { + dmm_spin_unlock(&handle->rd_lock); + NSSOC_LOGERR("rd route data malloc fail"); + return -1; + } + ret = memset_s(pdata, size, 0, size); + if (EOK != ret) + { + dmm_spin_unlock(&handle->rd_lock); + NSSOC_LOGERR("memset_s failed]ret=%d", ret); + free(pdata); + return -1; + } + for (icnt = 0; icnt < handle->size; icnt++) + { + pnode = &(handle->node[icnt]); + if (RD_NODE_USING == pnode->flag) { - item.agetime = NSTACK_RD_AGETIME_MAX; - for (icnt = 0; icnt < stack_num; icnt++) - { - if (0 == - strcmp (g_rd_local_data->pstack_info[icnt].name, - rd_data[iindex].stack_name)) - { - item.stack_id = g_rd_local_data->pstack_info[icnt].stack_id; - break; - } - } - if (icnt >= stack_num) - { - NSSOC_LOGINF - ("plane name:%s was not fount, protocoltype:%d was dropped", - rd_data[iindex].stack_name); - continue; - } - /*insert to the list */ - g_rd_cpy[type].rd_item_inset (NSTACK_RD_LIST (type), &item); - continue; + pdata[idex] = pnode->data; + idex++; } + } + rdver = handle->rdtbl_ver; + dmm_spin_unlock(&handle->rd_lock); + /*if no data fetched , just return fail */ + if (idex == 0) + { + free(pdata); + return -1; + } + *data = pdata; + *num = idex; + *ver = rdver; + return 0; +} - NSSOC_LOGERR ("rd data type=%d cpy fail", rd_data[iindex].type); +static int nstack_rd_tblver_get(int *ver, rd_route_table * handle) +{ + if (!handle || !ver) + { + NSSOC_LOGERR("nstack rd mng not inited or input err"); + return -1; } - return; + *ver = handle->rdtbl_ver; + return 0; } -/***************************************************************************** -* Prototype : nstack_rd_data_get -* Description : rd data get, -* Input : None -* Output : None -* Return Value : int -* Calls : -* Called By : -*****************************************************************************/ -int -nstack_rd_data_get (nstack_get_route_data pfun) +int nstack_rd_init(nstack_rd_stack_info * pstack, int num) { - rd_route_data *rd_data = NULL; - int iret = NSTACK_RD_FAIL; - int inum = 0; + int icnt = 0; + nstack_rd_stack_info *ptemstack = NULL; + int *rd_ver = NULL; - /*get rd config */ - if (pfun && (NSTACK_RD_SUCCESS == pfun (&rd_data, &inum))) + if (!pstack) { - if (inum > 0) - { - nstack_rd_save (rd_data, inum); - iret = NSTACK_RD_SUCCESS; - } - else - { - NSSOC_LOGDBG ("no rd data got"); - } - if (rd_data) - { - free (rd_data); - rd_data = NULL; - } + NSSOC_LOGERR("input err pstack:%p", pstack); + return NSTACK_RD_FAIL; + } + g_rd_local_data = (rd_local_data *) malloc(sizeof(rd_local_data)); + if (!g_rd_local_data) + { + NSSOC_LOGERR("g_rd_local_data alloc fail"); + return NSTACK_RD_FAIL; + } + + /*add return value check */ + if (EOK != + memset_s((void *) g_rd_local_data, sizeof(rd_local_data), 0, + sizeof(rd_local_data))) + { + NSSOC_LOGERR("memset_s fail"); + goto ERR; + } + + g_rd_local_data->sys_fun = nstack_rd_ip_get; + g_rd_local_data->rdtbl_ver_get_fun = nstack_rd_tblver_get; + + ptemstack = + (nstack_rd_stack_info *) malloc(sizeof(nstack_rd_stack_info) * num); + if (!ptemstack) + { + NSSOC_LOGERR("rd stack info malloc fail"); + goto ERR; + } + + if (EOK != + memcpy_s(ptemstack, sizeof(nstack_rd_stack_info) * num, pstack, + sizeof(nstack_rd_stack_info) * num)) + { + NSSOC_LOGERR("memcpy_s failed!"); + goto ERR; + } + + g_rd_local_data->pstack_info = ptemstack; + g_rd_local_data->stack_num = num; + + rd_ver = (int *) malloc(sizeof(int) * NSTACK_NUM); /*this function is necessary */ + if (!rd_ver) + { + NSSOC_LOGERR("rd_ver alloc failed!"); + goto ERR; + } + if (EOK != + memset_s((void *) rd_ver, sizeof(int) * NSTACK_NUM, 0, + sizeof(int) * NSTACK_NUM)) + { + NSSOC_LOGERR("memset_s failed!"); + goto ERR; + } + g_rd_local_data->rdlocal_ver = rd_ver; + + for (icnt = 0; icnt < RD_DATA_TYPE_MAX; icnt++) + { + INIT_HLIST_HEAD(&(g_rd_local_data->route_list[icnt].headlist)); } - else + return NSTACK_RD_SUCCESS; + + ERR: + if (g_rd_local_data) + { + free(g_rd_local_data); + g_rd_local_data = NULL; + } + if (ptemstack) { - NSSOC_LOGERR ("nstack rd sys rd info fail"); + free(ptemstack); } - return iret; + if (rd_ver) + { + free(rd_ver); /*this function is necessary */ + } + return NSTACK_RD_FAIL; } /***************************************************************************** @@ -339,33 +511,92 @@ nstack_rd_data_get (nstack_get_route_data pfun) * Calls : * Called By : *****************************************************************************/ -int -nstack_rd_sys () +int nstack_rd_sys() { - int iret = NSTACK_RD_FAIL; - int icnt = 0; - if (!g_rd_local_data) + rd_route_data *rd_data = NULL; + nstack_rd_stack_info *prdstack = NULL; + int icnt = 0; + int inum = 0; + int iver = 0; + int iret = 0; + int iindex = 0; + rd_data_item item; + rd_data_type type = RD_DATA_TYPE_MAX; + + if (!g_rd_local_data) { - NSSOC_LOGERR ("rd have not been inited"); - return NSTACK_RD_FAIL; + NSSOC_LOGERR("rd have not been inited"); + return NSTACK_RD_FAIL; } + /*add return value check */ + int retVal = memset_s(&item, sizeof(item), 0, sizeof(item)); + if (EOK != retVal) + { + NSSOC_LOGERR("memset_s failed]retVal=%d", retVal); + return NSTACK_RD_FAIL; + } + nstack_rd_sys_clean(); - /*insert default rd info */ - nstack_rd_sys_default (); + nstack_rd_sys_load_default(); - /*get from config file */ - for (icnt = 0; icnt < g_rd_local_data->fun_num; icnt++) + prdstack = g_rd_local_data->pstack_info; + for (icnt = 0; icnt < NSTACK_NUM; icnt++) { - if (NSTACK_RD_SUCCESS == - nstack_rd_data_get (g_rd_local_data->sys_fun[icnt])) + if (!g_rd_table_handle[icnt]) { - iret = NSTACK_RD_SUCCESS; + continue; } - } + /*get from rd table */ + iret = + g_rd_local_data->sys_fun(&rd_data, &inum, &iver, + g_rd_table_handle[icnt]); + if (NSTACK_RD_SUCCESS != iret) + { + NSSOC_LOGERR("nstack rd sys rd info stack fail] stack=%s", + prdstack[icnt].name); + return NSTACK_RD_FAIL; + } + NSSOC_LOGINF + ("nstack rd sync sucess] stack=%s, rdtable ver:%d, rdtable size:%d", + prdstack[icnt].name, iver, inum); - /*age after sys */ - nstack_rd_age (); - return iret; + g_rd_local_data->rdlocal_ver[icnt] = iver; + if (inum <= 0) + { + NSSOC_LOGDBG("no rd data got"); + if (rd_data) + { + free(rd_data); /*this function is necessary */ + rd_data = NULL; + } + continue; + } + for (iindex = 0; iindex < inum; iindex++) + { + if (rd_data[iindex].type >= RD_DATA_TYPE_MAX) + { + NSSOC_LOGERR("rd data type=%d unkown", rd_data[iindex].type); + continue; + } + type = rd_data[iindex].type; + if (NSTACK_RD_SUCCESS == + g_rd_cpy[type].rd_item_copy((void *) &item, + (void *) &rd_data[iindex])) + { + item.agetime = NSTACK_RD_AGETIME_MAX; + item.stack_id = prdstack[icnt].stack_id; + /*insert to the list */ + g_rd_cpy[type].rd_item_insert(NSTACK_RD_LIST(type), &item); /*do not need return value */ + continue; + } + NSSOC_LOGERR("rd data type=%d cpy fail", rd_data[iindex].type); + } + free(rd_data); /*this function is necessary */ + rd_data = NULL; + } + /*age after sys */ + nstack_rd_age(); /*do not need return value */ + return NSTACK_RD_SUCCESS; } /***************************************************************************** @@ -378,13 +609,42 @@ nstack_rd_sys () * Calls : * Called By : *****************************************************************************/ -int -nstack_rd_age () +int nstack_rd_age() { - int icnt = 0; - for (icnt = 0; icnt < RD_DATA_TYPE_MAX; icnt++) + int icnt = 0; + for (icnt = 0; icnt < RD_DATA_TYPE_MAX; icnt++) { - (void) g_rd_cpy[icnt].rd_item_age (NSTACK_RD_LIST (icnt)); + if (g_rd_cpy[icnt].rd_item_age) + (void) g_rd_cpy[icnt].rd_item_age(NSTACK_RD_LIST(icnt)); } - return NSTACK_RD_SUCCESS; + return NSTACK_RD_SUCCESS; +} + +int nstack_rd_match_pre(int domain, int type, int protocol, + rd_data_item * item) +{ + int ret = -1; + nstack_rd_key key = { 0 }; + + key.type = RD_DATA_TYPE_TYPE; + key.socket_type = type; + ret = + g_rd_cpy[key.type].rd_item_find(NSTACK_RD_LIST(key.type), + (void *) &key, (void *) item); + if (ret == NSTACK_RD_SUCCESS) + { + return item->stack_id; + } + + key.type = RD_DATA_TYPE_PROTO; + key.proto = protocol; + ret = + g_rd_cpy[key.type].rd_item_find(NSTACK_RD_LIST(key.type), + (void *) &key, (void *) item); + if (ret == NSTACK_RD_SUCCESS) + { + return item->stack_id; + } + + return -1; } diff --git a/src/nSocket/nstack_rd/nstack_rd_init.c b/src/nSocket/nstack_rd/nstack_rd_init.c deleted file mode 100644 index 09186ae..0000000 --- a/src/nSocket/nstack_rd/nstack_rd_init.c +++ /dev/null @@ -1,171 +0,0 @@ -/* -* -* Copyright (c) 2018 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at: -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include <stdlib.h> -#include "nstack_rd.h" -#include "nstack_rd_data.h" -#include "nstack_rd_init.h" -#include "nstack_rd_priv.h" -#include "nstack_log.h" -#include "nstack_securec.h" - -#define NSTACK_RD_ERR_CHECK_GOTO(ret, desc, lab) \ -if (NSTACK_RD_SUCCESS != (ret))\ -{ \ - NSSOC_LOGERR("%s fail, return:%d", desc, ret); \ - goto lab; \ -} - -#define NSTACK_RD_POINT_CHECK_GOTO(ptr, desc, lab) \ -if (!ptr)\ -{ \ - NSSOC_LOGERR("%s fail", desc); \ - goto lab; \ -} - -rd_local_data *g_rd_local_data = NULL; - -/***************************************************************************** -* Prototype : nstack_rd_init -* Description : nstack rd init -* Input : nstack_stack_info *pstack -* int num -* nstack_get_route_data pfun -* Output : None -* Return Value : int -* Calls : -* Called By : -* pstack : 1. priority 0: when router not fund, the first will be chose -* 2. if have many same 0 priority, fist input will be chose, -* 3. if none 0 priority, the last input will be choose -*****************************************************************************/ -int -nstack_rd_init (nstack_stack_info * pstack, int num, - nstack_get_route_data * pfun, int fun_num) -{ - int icnt = 0; - int hindex = 0; - int tindex = num - 1; - int iindex = 0; - int ret = NSTACK_RD_SUCCESS; - nstack_rd_stack_info *ptemstack = NULL; - - if ((!pstack) || (!pfun)) - { - NSSOC_LOGERR ("input err pstack:%p, pfun:%p", pstack, pfun); - return NSTACK_RD_FAIL; - } - g_rd_local_data = (rd_local_data *) malloc (sizeof (rd_local_data)); - if (!g_rd_local_data) - { - NSSOC_LOGERR ("g_rd_local_data alloc fail"); - return NSTACK_RD_FAIL; - } - - if (EOK != - MEMSET_S ((void *) g_rd_local_data, sizeof (rd_local_data), 0, - sizeof (rd_local_data))) - { - NSSOC_LOGERR ("MEMSET_S fail"); - goto ERR; - } - for (icnt = 0; icnt < fun_num; icnt++) - { - g_rd_local_data->sys_fun[icnt] = pfun[icnt]; - } - g_rd_local_data->fun_num = fun_num; - ptemstack = - (nstack_rd_stack_info *) malloc (sizeof (nstack_rd_stack_info) * num); - NSTACK_RD_POINT_CHECK_GOTO (ptemstack, "rd stack info malloc fail", ERR); - - /*save stack info in priority order */ - for (icnt = 0; icnt < num; icnt++) - { - if (0 == pstack[icnt].priority) - { - iindex = hindex; - hindex++; - } - else - { - iindex = tindex; - tindex--; - } - - /* modify destMax from RD_PLANE_NAMELEN to STACK_NAME_MAX */ - ret = - STRCPY_S (ptemstack[iindex].name, STACK_NAME_MAX, pstack[icnt].name); - if (ret != EOK) - { - NSSOC_LOGERR ("STRCPY_S failed"); - goto ERR; - } - - ptemstack[iindex].priority = pstack[icnt].priority; - ptemstack[iindex].stack_id = pstack[icnt].stack_id; - NSSOC_LOGDBG - ("nstack rd init]stackname=%s,priority=%d,stackid=%d was added", - ptemstack[iindex].name, - ptemstack[iindex].priority, ptemstack[iindex].stack_id); - } - - g_rd_local_data->pstack_info = ptemstack; - g_rd_local_data->stack_num = num; - - for (icnt = 0; icnt < RD_DATA_TYPE_MAX; icnt++) - { - INIT_HLIST_HEAD (&(g_rd_local_data->route_list[icnt].headlist)); - } - return NSTACK_RD_SUCCESS; - -ERR: - if (g_rd_local_data) - { - free (g_rd_local_data); - g_rd_local_data = NULL; - } - if (ptemstack) - { - free (ptemstack); - } - return NSTACK_RD_FAIL; -} - -/***************************************************************************** -* Prototype : nstack_get_stackid_byname -* Description : get stack ip by stack name -* Input : char *name -* Output : None -* Return Value : int -* Calls : -* Called By : -*****************************************************************************/ -int -nstack_get_stackid_byname (char *name) -{ - int stacknum = g_rd_local_data->stack_num; - int iindex = 0; - nstack_rd_stack_info *pstack = NULL; - for (iindex = 0; iindex < stacknum; iindex++) - { - pstack = &(g_rd_local_data->pstack_info[iindex]); - if (0 == strcmp (pstack->name, name)) - { - return pstack->stack_id; - } - } - return -1; -} diff --git a/src/nSocket/nstack_rd/nstack_rd_ip.c b/src/nSocket/nstack_rd/nstack_rd_ip.c index 82035d1..d769720 100644 --- a/src/nSocket/nstack_rd/nstack_rd_ip.c +++ b/src/nSocket/nstack_rd/nstack_rd_ip.c @@ -18,157 +18,158 @@ #include <arpa/inet.h> #include "nstack_rd_data.h" #include "nstack_rd.h" -#include "nstack_rd_init.h" #include "nstack_rd_priv.h" #include "nstack_rd_ip.h" #include "nstack_log.h" #include "nstack_securec.h" +/* Optimize log */ #include "nstack_ip_addr.h" -#define NSTACK_IP_MLSTACKID RD_LWIP - #define PP_HTONL(x) ((((x) & 0xff) << 24) | \ (((x) & 0xff00) << 8) | \ (((x) & 0xff0000UL) >> 8) | \ (((x) & 0xff000000UL) >> 24)) -#define rd_ismulticast(addr)(((unsigned int)(addr) & 0xf0000000UL) == 0xe0000000UL) +#define rd_ismulticast(addr)(((ntohl(addr)) & 0xf0000000UL) == 0xe0000000UL) int g_multi_stackid = -1; /*copy rd data*/ -int -nstack_rd_ipdata_cpy (void *destdata, void *srcdata) +int nstack_rd_ip_data_cpy(void *destdata, void *srcdata) { - rd_data_item *pitem = (rd_data_item *) destdata; - rd_route_data *pdata = (rd_route_data *) srcdata; - - pitem->type = pdata->type; - pitem->ipdata.addr = pdata->ipdata.addr; - pitem->ipdata.masklen = pdata->ipdata.masklen; - pitem->ipdata.resev[0] = pdata->ipdata.resev[0]; - pitem->ipdata.resev[1] = pdata->ipdata.resev[1]; - return NSTACK_RD_SUCCESS; + rd_data_item *pitem = (rd_data_item *) destdata; + rd_route_data *pdata = (rd_route_data *) srcdata; + + pitem->type = pdata->type; + pitem->ipdata.addr = pdata->ipdata.addr; + pitem->ipdata.masklen = pdata->ipdata.masklen; + pitem->ipdata.resev[0] = pdata->ipdata.resev[0]; + pitem->ipdata.resev[1] = pdata->ipdata.resev[1]; + return NSTACK_RD_SUCCESS; } /* * Add an ip segment to the list and sort it in descending order of ip mask length * If the list already exists in the same list of ip side, then stack_id update - *ip is local byteorder + *ip is network byte order */ -int -nstack_rd_ip_item_insert (nstack_rd_list * hlist, void *rditem) +/*vars are used in macro*/ +int nstack_rd_ip_item_insert(nstack_rd_list * hlist, void *rditem) { - nstack_rd_node *pdatanode = NULL; - nstack_rd_node *tempdata = NULL; - struct hlist_node *tempnode = NULL; - struct hlist_node *tem = NULL; - - unsigned int tempip_addr = 0; - unsigned int tempip_masklen = 0; - rd_data_item *pitem = (rd_data_item *) rditem; - unsigned int ip_addr = pitem->ipdata.addr; - unsigned int ip_masklen = pitem->ipdata.masklen; - unsigned int ip_maskv = MASK_V (ip_addr, ip_masklen); - - ip_masklen = pitem->ipdata.masklen; - NSSOC_LOGDBG ("stackid:%d, ipaddr:%u.%u.%u.%u masklen:0x%x was inserted", - pitem->stack_id, ip4_addr4_16 (&pitem->ipdata.addr), - ip4_addr3_16 (&pitem->ipdata.addr), - ip4_addr2_16 (&pitem->ipdata.addr), - ip4_addr1_16 (&pitem->ipdata.addr), pitem->ipdata.masklen); - - pdatanode = (nstack_rd_node *) malloc (sizeof (nstack_rd_node)); - if (!pdatanode) + nstack_rd_node *pdatanode = NULL; + nstack_rd_node *tempdata = NULL; + struct hlist_node *tempnode = NULL; + struct hlist_node *tem = NULL; + unsigned int ip_addr = 0; + unsigned int ip_masklen = 0; + unsigned int ip_maskv = 0; + unsigned int tempip_addr = 0; + unsigned int tempip_masklen = 0; + rd_data_item *pitem = (rd_data_item *) rditem; + + ip_masklen = pitem->ipdata.masklen; + ip_addr = pitem->ipdata.addr; + ip_maskv = MASK_V(ip_addr, ip_masklen); + + /* Optimize log */ + NSSOC_LOGDBG("stackid:%d, ipaddr:*.*.%u.%u masklen:0x%x was inserted", + pitem->stack_id, FUZZY_IP_VAR(&pitem->ipdata.addr), + pitem->ipdata.masklen); + + pdatanode = (nstack_rd_node *) malloc(sizeof(nstack_rd_node)); /*this function is necessary */ + if (!pdatanode) { - NSSOC_LOGERR ("nstack rd item malloc fail"); - return NSTACK_RD_FAIL; + NSSOC_LOGERR("nstack rd item malloc fail"); + return NSTACK_RD_FAIL; } - int retVal = - MEMSET_S (pdatanode, sizeof (nstack_rd_node), 0, sizeof (nstack_rd_node)); - if (EOK != retVal) + /* add return value check */ + int retVal = memset_s(pdatanode, sizeof(nstack_rd_node), 0, + sizeof(nstack_rd_node)); + if (EOK != retVal) { - NSSOC_LOGERR ("MEMSET_S failed]retVal=%d", retVal); - free (pdatanode); - return NSTACK_RD_FAIL; + NSSOC_LOGERR("memset_s failed]retVal=%d", retVal); + free(pdatanode); /*this function is necessary */ + return NSTACK_RD_FAIL; } - INIT_HLIST_NODE (&pdatanode->rdnode); - NSTACK_RD_IP_ITEM_COPY (&(pdatanode->item), pitem); + INIT_HLIST_NODE(&pdatanode->rdnode); + NSTACK_RD_IP_ITEM_COPY(&(pdatanode->item), pitem); - if (hlist_empty (&(hlist->headlist))) + if (hlist_empty(&(hlist->headlist))) { - hlist_add_head (&(pdatanode->rdnode), &(hlist->headlist)); - return NSTACK_RD_SUCCESS; + hlist_add_head(&(pdatanode->rdnode), &(hlist->headlist)); + + return NSTACK_RD_SUCCESS; + } - hlist_for_each_entry (tempdata, tempnode, &(hlist->headlist), rdnode) - { - tem = tempnode; - tempip_addr = tempdata->item.ipdata.addr; - tempip_masklen = tempdata->item.ipdata.masklen; - if (ip_masklen < tempip_masklen) - { - continue; - } - - /*if already exist, just return success */ - if (ip_maskv == MASK_V (tempip_addr, tempip_masklen)) - { - NSSOC_LOGDBG - ("insert ip:%u.%u.%u.%u, mask:0x%x, stack_id:%d, exist orgid:%d", - ip4_addr4_16 (&pitem->ipdata.addr), - ip4_addr3_16 (&pitem->ipdata.addr), - ip4_addr2_16 (&pitem->ipdata.addr), - ip4_addr1_16 (&pitem->ipdata.addr), pitem->ipdata.masklen, - pitem->stack_id, tempdata->item.stack_id); - - tempdata->item.stack_id = pitem->stack_id; - tempdata->item.agetime = NSTACK_RD_AGETIME_MAX; - free (pdatanode); + + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { + tem = tempnode; + tempip_addr = tempdata->item.ipdata.addr; + tempip_masklen = tempdata->item.ipdata.masklen; + if (ip_masklen < tempip_masklen) + { + continue; + } + + /*if already exist, just return success */ + if (ip_maskv == MASK_V(tempip_addr, tempip_masklen)) + { + /* Optimize log */ + NSSOC_LOGDBG + ("insert ip=*.*.%u.%u, mask=0x%x, stack_id=%d, exist orgid=%d", + FUZZY_IP_VAR(&pitem->ipdata.addr), pitem->ipdata.masklen, + pitem->stack_id, tempdata->item.stack_id); + /* Optimize log */ + + tempdata->item.stack_id = pitem->stack_id; + tempdata->item.agetime = NSTACK_RD_AGETIME_MAX; + free(pdatanode); /*this function is necessary */ + return NSTACK_RD_SUCCESS; + } + hlist_add_before(&(pdatanode->rdnode), tempnode); + return NSTACK_RD_SUCCESS; - } - hlist_add_before (&(pdatanode->rdnode), tempnode); + + } + hlist_add_after(tem, &(pdatanode->rdnode)); + return NSTACK_RD_SUCCESS; - } - hlist_add_after (tem, &(pdatanode->rdnode)); - return NSTACK_RD_SUCCESS; + } /* *find stackid by ip - *input ip must netorder + *input ip must be network order */ -int -nstack_rd_ip_item_find (nstack_rd_list * hlist, void *rdkey, void *outitem) +int nstack_rd_ip_item_find(nstack_rd_list * hlist, void *rdkey, void *outitem) { - struct hlist_node *tempnode = NULL; - nstack_rd_node *tempdata = NULL; - unsigned int tempip_addr = 0; - unsigned int tempip_masklen = 0; - nstack_rd_key *key = (nstack_rd_key *) rdkey; - rd_data_item *pitem = (rd_data_item *) outitem; - unsigned int ip_addr = 0; - /*need to convert to local order */ - ip_addr = ntohl (key->ip_addr); - - hlist_for_each_entry (tempdata, tempnode, &(hlist->headlist), rdnode) - { - tempip_addr = tempdata->item.ipdata.addr; - tempip_masklen = tempdata->item.ipdata.masklen; - /*if already exist, just return success */ - if (MASK_V (ip_addr, tempip_masklen) == - MASK_V (tempip_addr, tempip_masklen)) - { - NSTACK_RD_IP_ITEM_COPY (pitem, &(tempdata->item)); - return NSTACK_RD_SUCCESS; - } - } + struct hlist_node *tempnode = NULL; + nstack_rd_node *tempdata = NULL; + unsigned int tempip_addr = 0; + unsigned int tempip_masklen = 0; + nstack_rd_key *key = (nstack_rd_key *) rdkey; + rd_data_item *pitem = (rd_data_item *) outitem; + unsigned int ip_addr = key->ip_addr; - NSSOC_LOGDBG ("ip=%u.%u.%u.%u item not found", ip4_addr4_16 (&ip_addr), - ip4_addr3_16 (&ip_addr), - ip4_addr2_16 (&ip_addr), ip4_addr1_16 (&ip_addr)); + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { + tempip_addr = tempdata->item.ipdata.addr; + tempip_masklen = tempdata->item.ipdata.masklen; + /*if already exist, just return success */ + if (MASK_V(ip_addr, tempip_masklen) == + MASK_V(tempip_addr, tempip_masklen)) + { + NSTACK_RD_IP_ITEM_COPY(pitem, &(tempdata->item)); + return NSTACK_RD_SUCCESS; + } + } - return NSTACK_RD_FAIL; + /* Optimize log */ + NSSOC_LOGDBG("ip=*.*.%u.%u item not found", FUZZY_IP_VAR(&ip_addr)); + + return NSTACK_RD_FAIL; } /***************************************************************************** @@ -180,86 +181,130 @@ nstack_rd_ip_item_find (nstack_rd_list * hlist, void *rdkey, void *outitem) * Calls : * Called By : *****************************************************************************/ -int -nstack_rd_ip_item_age (nstack_rd_list * hlist) +int nstack_rd_ip_item_age(nstack_rd_list * hlist) +{ + struct hlist_node *tempnode = NULL; + nstack_rd_node *tempdata = NULL; + nstack_rd_node *prevdata = NULL; + struct hlist_node *prevnode = NULL; + NSSOC_LOGINF("nstack rd ip age begin"); + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { + /*if agetime equal 0, remove it */ + if (tempdata->item.agetime <= 0) + { + if (prevdata) + { + /* Optimize log */ + NSSOC_LOGDBG + ("stackid=%d, addrip=*.*.%u.%u, masklen=0x%x was aged", + tempdata->item.stack_id, + FUZZY_IP_VAR(&tempdata->item.ipdata.addr), + tempdata->item.ipdata.masklen); + + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ + } + prevdata = tempdata; + prevnode = tempnode; + } + else + { + tempdata->item.agetime--; + } + } + if (prevdata) + { + if (tempdata) + { + /* Optimize log */ + NSSOC_LOGDBG + ("stackid:%d, addrip:*.*.%u.%u, masklen:0x%x was last aged", + tempdata->item.stack_id, + FUZZY_IP_VAR(&tempdata->item.ipdata.addr), + tempdata->item.ipdata.masklen); + } + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ + } + NSSOC_LOGINF("nstack rd ip age end"); + return NSTACK_RD_SUCCESS; +} + +void nstack_rd_ip_item_clean(nstack_rd_list * hlist) { - struct hlist_node *tempnode = NULL; - nstack_rd_node *tempdata = NULL; - nstack_rd_node *prevdata = NULL; - struct hlist_node *prevnode = NULL; - NSSOC_LOGINF ("nstack rd ip age begin"); - hlist_for_each_entry (tempdata, tempnode, &(hlist->headlist), rdnode) - { - /*if agetime equal 0, remove it */ - if (tempdata->item.agetime <= 0) - { + struct hlist_node *tempnode = NULL; + nstack_rd_node *tempdata = NULL; + nstack_rd_node *prevdata = NULL; + struct hlist_node *prevnode = NULL; + NSSOC_LOGINF("nstack rd ip item clean begin"); + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { if (prevdata) - { + { NSSOC_LOGDBG - ("stackid:%d, addrip:%u.%u.%u.%u, masklen:0x%x was aged", - tempdata->item.stack_id, - ip4_addr4_16 (&tempdata->item.ipdata.addr), - ip4_addr3_16 (&tempdata->item.ipdata.addr), - ip4_addr2_16 (&tempdata->item.ipdata.addr), - ip4_addr1_16 (&tempdata->item.ipdata.addr), - tempdata->item.ipdata.masklen); - - hlist_del_init (prevnode); - free (prevdata); - } + ("stackid=%d, addrip=*.*.%u.%u, masklen=0x%x was aged", + tempdata->item.stack_id, + FUZZY_IP_VAR(&tempdata->item.ipdata.addr), + tempdata->item.ipdata.masklen); + + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ + } + prevdata = tempdata; prevnode = tempnode; - } - else - { - tempdata->item.agetime--; - } - } - if (prevdata) + } + if (prevdata) { - if (tempdata) + if (tempdata) { - NSSOC_LOGDBG - ("stackid:%d, addrip:%u.%u.%u.%u, masklen:0x%x was last aged", - tempdata->item.stack_id, - ip4_addr4_16 (&tempdata->item.ipdata.addr), - ip4_addr3_16 (&tempdata->item.ipdata.addr), - ip4_addr2_16 (&tempdata->item.ipdata.addr), - ip4_addr1_16 (&tempdata->item.ipdata.addr), - tempdata->item.ipdata.masklen); + NSSOC_LOGDBG + ("stackid:%d, addrip:*.*.%u.%u, masklen:0x%x was last aged", + tempdata->item.stack_id, + FUZZY_IP_VAR(&tempdata->item.ipdata.addr), + tempdata->item.ipdata.masklen); } - hlist_del_init (prevnode); - free (prevdata); + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ } - NSSOC_LOGINF ("nstack rd ip age end"); - return NSTACK_RD_SUCCESS; + NSSOC_LOGINF("nstack rd ip item clean end"); +} + +static int nstack_get_stackid_byname(char *name) +{ + int stacknum = g_rd_local_data->stack_num; + int iindex = 0; + nstack_rd_stack_info *pstack = NULL; + for (iindex = 0; iindex < stacknum; iindex++) + { + pstack = &(g_rd_local_data->pstack_info[iindex]); + if (0 == strcmp(pstack->name, name)) + { + return pstack->stack_id; + } + } + return -1; } /* *find stackid by spec ip(multicast ip) - *input ip must netorder + *input ip must be network order */ -int -nstack_rd_ip_spec (void *rdkey) +int nstack_rd_ip_spec(void *rdkey) { - nstack_rd_key *key = (nstack_rd_key *) rdkey; - unsigned int ip_addr = 0; + nstack_rd_key *key = (nstack_rd_key *) rdkey; + unsigned int ip_addr = 0; - ip_addr = ntohl (key->ip_addr); + ip_addr = (key->ip_addr); - if (rd_ismulticast (ip_addr)) + if (rd_ismulticast(ip_addr)) { - if (-1 == g_multi_stackid) + if (-1 == g_multi_stackid) { - g_multi_stackid = nstack_get_stackid_byname (NSTACK_IP_MLSTACKID); + g_multi_stackid = nstack_get_stackid_byname(RD_STACKPOOL_NAME); } - return g_multi_stackid; + return g_multi_stackid; } - return -1; -} - -int -nstack_rd_ip_default (void *rdkey) -{ - return NSTACK_GET_STACK (0); + return -1; } diff --git a/src/nSocket/nstack_rd/nstack_rd_ip.h b/src/nSocket/nstack_rd/nstack_rd_ip.h index d64956d..b3abee3 100644 --- a/src/nSocket/nstack_rd/nstack_rd_ip.h +++ b/src/nSocket/nstack_rd/nstack_rd_ip.h @@ -17,6 +17,8 @@ #ifndef __NSTACK_RD_IP_H #define __NSTACK_RD_IP_H +#include "nstack_rd_priv.h" + #define NSTACK_RD_IP_ITEM_COPY(destitem, srcitem){ \ (destitem)->agetime = (srcitem)->agetime; \ (destitem)->stack_id = (srcitem)->stack_id; \ @@ -27,12 +29,12 @@ (destitem)->ipdata.resev[1] = (srcitem)->ipdata.resev[1]; \ } -int nstack_rd_ipdata_cpy (void *destdata, void *srcdata); -int nstack_rd_ip_item_insert (nstack_rd_list * hlist, void *rditem); -int nstack_rd_ip_item_find (nstack_rd_list * hlist, void *rdkey, - void *outitem); -int nstack_rd_ip_item_age (nstack_rd_list * hlist); -int nstack_rd_ip_spec (void *rdkey); -int nstack_rd_ip_default (void *rdkey); +int nstack_rd_ip_data_cpy(void *destdata, void *srcdata); +int nstack_rd_ip_item_insert(nstack_rd_list * hlist, void *rditem); +int nstack_rd_ip_item_find(nstack_rd_list * hlist, void *rdkey, + void *outitem); +int nstack_rd_ip_item_age(nstack_rd_list * hlist); +void nstack_rd_ip_item_clean(nstack_rd_list * hlist); +int nstack_rd_ip_spec(void *rdkey); #endif diff --git a/src/nSocket/nstack_rd/nstack_rd_ip6.c b/src/nSocket/nstack_rd/nstack_rd_ip6.c new file mode 100644 index 0000000..dde201d --- /dev/null +++ b/src/nSocket/nstack_rd/nstack_rd_ip6.c @@ -0,0 +1,261 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include <stdlib.h> +#include <arpa/inet.h> +#include "nstack_rd_data.h" +#include "nstack_rd.h" +#include "nstack_rd_priv.h" +#include "nstack_rd_ip6.h" +#include "nstack_log.h" +#include "nstack_securec.h" +#include "nstack_ip_addr.h" + +inline static const char *ip6_ntoa(const ip6_addr_t * addr) +{ +#define IP6_NTOA_NUM (1 << 3) +#define IP6_NTOA_LEN 46 + static __thread char ip6_ntoa_buf[IP6_NTOA_NUM][IP6_NTOA_LEN]; + static __thread unsigned int ip6_ntoa_index = 0; + + char *buf = ip6_ntoa_buf[(ip6_ntoa_index++) & (IP6_NTOA_NUM - 1)]; + return inet_ntop(AF_INET, addr, buf, IP6_NTOA_LEN); +} + +int nstack_rd_ip6_data_cpy(void *destdata, void *srcdata) +{ + rd_data_item *pitem = (rd_data_item *) destdata; + rd_route_data *pdata = (rd_route_data *) srcdata; + + pitem->type = pdata->type; + pitem->ip6data = pdata->ip6data; + return NSTACK_RD_SUCCESS; +} + +/* + * Add an ip segment to the list and sort it in descending order of ip mask length + * If the list already exists in the same list of ip side, then stack_id update + * ip is network byte order + */ +/*vars are used in macro*/ +int nstack_rd_ip6_item_insert(nstack_rd_list * hlist, void *rditem) +{ + nstack_rd_node *pdatanode = NULL; + nstack_rd_node *tempdata = NULL; + struct hlist_node *tempnode = NULL; + struct hlist_node *tem = NULL; + rd_data_item *pitem = (rd_data_item *) rditem; + char buf[52]; + + NSSOC_LOGDBG("stackid:%d, ip6addr:%s masklen:0x%x was inserted", + pitem->stack_id, inet_ntop(AF_INET6, &pitem->ip6data.addr, + buf, sizeof(buf)), + pitem->ip6data.masklen); + + pdatanode = (nstack_rd_node *) malloc(sizeof(nstack_rd_node)); /*this function is necessary */ + if (!pdatanode) + { + NSSOC_LOGERR("nstack rd item malloc fail"); + return NSTACK_RD_FAIL; + } + + int retVal = memset_s(pdatanode, sizeof(nstack_rd_node), 0, + sizeof(nstack_rd_node)); + if (EOK != retVal) + { + NSSOC_LOGERR("memset_s failed]retVal=%d", retVal); + free(pdatanode); /*this function is necessary */ + return NSTACK_RD_FAIL; + } + + INIT_HLIST_NODE(&pdatanode->rdnode); + pdatanode->item = *pitem; + + if (hlist_empty(&(hlist->headlist))) + { + hlist_add_head(&(pdatanode->rdnode), &(hlist->headlist)); + + return NSTACK_RD_SUCCESS; + + } + + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { + tem = tempnode; + if (pitem->ip6data.masklen < tempdata->item.ip6data.masklen) + { + continue; + } + + /*if already exist, just return success */ + if (pitem->ip6data.masklen == tempdata->item.ip6data.masklen && + ip6_addr_match(&pitem->ip6data.addr, + &tempdata->item.ip6data.addr, + pitem->ip6data.masklen)) + { + NSSOC_LOGDBG + ("insert ip6:%s, mask:0x%x, stack_id:%d, exist orgid:%d", + inet_ntop(AF_INET6, &pitem->ip6data.addr, buf, + sizeof(buf)), pitem->ip6data.masklen, + pitem->stack_id, tempdata->item.stack_id); + + tempdata->item.stack_id = pitem->stack_id; + tempdata->item.agetime = NSTACK_RD_AGETIME_MAX; + free(pdatanode); /*this function is necessary */ + return NSTACK_RD_SUCCESS; + } + hlist_add_before(&(pdatanode->rdnode), tempnode); + + return NSTACK_RD_SUCCESS; + + } + hlist_add_after(tem, &(pdatanode->rdnode)); + + return NSTACK_RD_SUCCESS; + +} + +int nstack_rd_ip6_item_age(nstack_rd_list * hlist) +{ + struct hlist_node *tempnode = NULL; + nstack_rd_node *tempdata = NULL; + nstack_rd_node *prevdata = NULL; + struct hlist_node *prevnode = NULL; + char buf[46]; + + NSSOC_LOGINF("nstack rd ip age begin"); + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { + /*if agetime equal 0, remove it */ + if (tempdata->item.agetime <= 0) + { + if (prevdata) + { + NSSOC_LOGDBG("stackid:%d, addrip6:%s, masklen:0x%x was aged", + tempdata->item.stack_id, + inet_ntop(AF_INET6, + &tempdata->item.ip6data.addr, buf, + sizeof(buf)), + tempdata->item.ip6data.masklen); + + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ + } + prevdata = tempdata; + prevnode = tempnode; + } + else + { + tempdata->item.agetime--; + } + } + if (prevdata) + { + if (tempdata) + { + NSSOC_LOGDBG + ("stackid:%d, addrip6:%s, masklen:0x%x was last aged", + tempdata->item.stack_id, inet_ntop(AF_INET6, + &tempdata->item. + ip6data.addr, buf, + sizeof(buf)), + tempdata->item.ip6data.masklen); + } + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ + } + + NSSOC_LOGINF("nstack rd ip age end"); + return NSTACK_RD_SUCCESS; +} + +void nstack_rd_ip6_item_clean(nstack_rd_list * hlist) +{ + struct hlist_node *tempnode = NULL; + nstack_rd_node *tempdata = NULL; + nstack_rd_node *prevdata = NULL; + struct hlist_node *prevnode = NULL; + char buf[46]; + + NSSOC_LOGINF("nstack rd ip clean begin"); + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { + if (prevdata) + { + NSSOC_LOGDBG("stackid:%d, addrip6:%s, masklen:0x%x was aged", + tempdata->item.stack_id, + inet_ntop(AF_INET6, &tempdata->item.ip6data.addr, + buf, sizeof(buf)), + tempdata->item.ip6data.masklen); + + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ + } + + prevdata = tempdata; + prevnode = tempnode; + } + if (prevdata) + { + if (tempdata) + { + NSSOC_LOGDBG + ("stackid:%d, addrip6:%s, masklen:0x%x was last aged", + tempdata->item.stack_id, inet_ntop(AF_INET6, + &tempdata->item. + ip6data.addr, buf, + sizeof(buf)), + tempdata->item.ip6data.masklen); + } + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ + } + + NSSOC_LOGINF("nstack rd ip clean end"); +} + +int nstack_rd_ip6_item_find(nstack_rd_list * hlist, void *rdkey, + void *outitem) +{ + struct hlist_node *tempnode = NULL; + nstack_rd_node *tempdata = NULL; + nstack_rd_key *key = (nstack_rd_key *) rdkey; + char buf[46]; + + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { + rd_data_item *tempitem = &tempdata->item; + + /*if already exist, just return success */ + if (ip6_addr_match + ((struct ip6_addr *) &key->in6_addr, &tempitem->ip6data.addr, + tempitem->ip6data.masklen)) + { + *(rd_data_item *) outitem = *tempitem; + return NSTACK_RD_SUCCESS; + } + } + + NSSOC_LOGDBG("ip6=%s item not found", + inet_ntop(AF_INET6, &key->in6_addr, buf, sizeof(buf))); + + return NSTACK_RD_FAIL; +} + +int nstack_rd_ip6_spec(void *rdkey) +{ + return -1; +} diff --git a/src/nSocket/nstack_rd/nstack_rd_ip6.h b/src/nSocket/nstack_rd/nstack_rd_ip6.h new file mode 100644 index 0000000..2934bb3 --- /dev/null +++ b/src/nSocket/nstack_rd/nstack_rd_ip6.h @@ -0,0 +1,25 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#define __NSTACK_RD_IP6_H + +int nstack_rd_ip6_data_cpy(void *destdata, void *srcdata); +int nstack_rd_ip6_item_insert(nstack_rd_list * hlist, void *rditem); +int nstack_rd_ip6_item_find(nstack_rd_list * hlist, void *rdkey, + void *outitem); +int nstack_rd_ip6_item_age(nstack_rd_list * hlist); +void nstack_rd_ip6_item_clean(nstack_rd_list * hlist); +int nstack_rd_ip6_spec(void *rdkey); diff --git a/src/nSocket/nstack_rd/nstack_rd_priv.h b/src/nSocket/nstack_rd/nstack_rd_priv.h deleted file mode 100644 index 7179364..0000000 --- a/src/nSocket/nstack_rd/nstack_rd_priv.h +++ /dev/null @@ -1,101 +0,0 @@ -/* -* -* Copyright (c) 2018 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at: -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#ifndef __NSTACK_RD_PRIV_H -#define __NSTACK_RD_PRIV_H -#include "list.h" - -#define NSTACK_RD_SUCCESS (0) -#define NSTACK_RD_FAIL (-1) -#define NSTACK_RD_ITEM_MAX (1024) - -#define NSTACK_RD_AGETIME_MAX (1) -#define NSTACK_SYS_FUN_MAX (16) - -#define RD_LWIP "lwip" -#define RD_KERNEL "kernel" - -#define NSTACK_RD_INDEX_BYIP(ip) (((ip) & 0xff) \ - + (((ip) >> 8)&0xff) \ - + (((ip) >> 16)&0xff) \ - + (((ip) >> 24)&0xff)) - -/*route data*/ -typedef struct __rd_data_item -{ - /*route info type , for example base on ip */ - rd_data_type type; - int stack_id; - int agetime; - union - { - rd_ip_data ipdata; - unsigned int proto_type; - }; -} rd_data_item; - -/*stack rd node*/ -typedef struct __nstack_rd_node -{ - struct hlist_node rdnode; - rd_data_item item; -} nstack_rd_node; - -typedef struct __nstack_rd_list -{ - struct hlist_head headlist; -} nstack_rd_list; - -typedef struct __nstack_rd_stack_info -{ - /*stack name */ - char name[STACK_NAME_MAX]; - /*stack id */ - int stack_id; - /*when route info not found, high priority stack was chose, same priority chose fist input one */ - int priority; /*0: highest: route info not found choose first */ -} nstack_rd_stack_info; - -/*rd local data*/ -typedef struct __rd_local_data -{ - nstack_rd_stack_info *pstack_info; - int stack_num; - nstack_rd_list route_list[RD_DATA_TYPE_MAX]; /*route table */ - nstack_get_route_data sys_fun[NSTACK_SYS_FUN_MAX]; /*rd data sys proc function list */ - int fun_num; -} rd_local_data; - -typedef struct __rd_data_proc -{ - int (*rd_item_cpy) (void *destdata, void *srcdata); - int (*rd_item_inset) (nstack_rd_list * hlist, void *rditem); - int (*rd_item_age) (nstack_rd_list * hlist); - int (*rd_item_find) (nstack_rd_list * hlist, void *rdkey, void *outitem); - int (*rd_item_spec) (void *rdkey); - int (*rd_item_default) (void *rdkey); -} rd_data_proc; - -extern rd_local_data *g_rd_local_data; -extern rd_data_proc g_rd_cpy[RD_DATA_TYPE_MAX]; - -#define NSTACK_NUM (g_rd_local_data->stack_num) -#define NSTACK_RD_LIST(type) (&(g_rd_local_data->route_list[(type)])) -#define NSTACK_GET_STACK(idx) ((g_rd_local_data->pstack_info)[idx].stack_id) - -int nstack_get_stackid_byname (char *name); - -#endif diff --git a/src/nSocket/nstack_rd/nstack_rd_proto.c b/src/nSocket/nstack_rd/nstack_rd_proto.c index 81246c0..e1c58e2 100644 --- a/src/nSocket/nstack_rd/nstack_rd_proto.c +++ b/src/nSocket/nstack_rd/nstack_rd_proto.c @@ -18,107 +18,111 @@ #include <arpa/inet.h> #include "nstack_rd_data.h" #include "nstack_rd.h" -#include "nstack_rd_init.h" #include "nstack_rd_priv.h" #include "nstack_rd_proto.h" #include "nstack_log.h" #include "nstack_securec.h" -#include "common_mem_common.h" /*copy rd data*/ -int -nstack_rd_proto_cpy (void *destdata, void *srcdata) +int nstack_rd_proto_data_cpy(void *destdata, void *srcdata) { - rd_data_item *pitem = (rd_data_item *) destdata; - rd_route_data *pdata = (rd_route_data *) srcdata; - pitem->type = pdata->type; - pitem->proto_type = pdata->proto_type; - return NSTACK_RD_SUCCESS; + rd_data_item *pitem = (rd_data_item *) destdata; + rd_route_data *pdata = (rd_route_data *) srcdata; + + pitem->type = pdata->type; + pitem->proto_data.value = pdata->proto_data.value; + pitem->proto_data.attr = pdata->proto_data.attr; + return NSTACK_RD_SUCCESS; } /* * Add an ip segment to the list and sort it in descending order of ip mask length * If the list already exists in the same list of ip side, then stack_id update - *ip is local byteorder + *ip is network byte order */ -int -nstack_rd_proto_item_insert (nstack_rd_list * hlist, void *rditem) +/*vars are used in macro*/ +int nstack_rd_proto_item_insert(nstack_rd_list * hlist, void *rditem) { - nstack_rd_node *pdatanode = NULL; - nstack_rd_node *tempdata = NULL; - struct hlist_node *tempnode = NULL; - rd_data_item *pitem = (rd_data_item *) rditem; + nstack_rd_node *pdatanode = NULL; + nstack_rd_node *tempdata = NULL; + struct hlist_node *tempnode = NULL; + rd_data_item *pitem = (rd_data_item *) rditem; - NSSOC_LOGDBG ("stackid:%d, protocol type:%d was inserted", pitem->stack_id, - pitem->proto_type); + NSSOC_LOGDBG("stackid:%d, proto value:%u proto attr:%u was inserted", + pitem->stack_id, pitem->proto_data.value, + pitem->proto_data.attr); - pdatanode = (nstack_rd_node *) malloc (sizeof (nstack_rd_node)); - if (!pdatanode) + pdatanode = (nstack_rd_node *) malloc(sizeof(nstack_rd_node)); /*this function is necessary */ + if (!pdatanode) { - NSSOC_LOGERR ("nstack rd item malloc fail"); - return NSTACK_RD_FAIL; + NSSOC_LOGERR("nstack rd item malloc fail"); + return NSTACK_RD_FAIL; } - int retVal = - MEMSET_S (pdatanode, sizeof (nstack_rd_node), 0, sizeof (nstack_rd_node)); - if (EOK != retVal) + /* add return value check */ + int retVal = memset_s(pdatanode, sizeof(nstack_rd_node), 0, + sizeof(nstack_rd_node)); + if (EOK != retVal) { - NSSOC_LOGERR ("MEMSET_S failed]retVal=%d", retVal); - free (pdatanode); - return NSTACK_RD_FAIL; + NSSOC_LOGERR("memset_s failed]retVal=%d", retVal); + free(pdatanode); /*this function is necessary */ + return NSTACK_RD_FAIL; } - INIT_HLIST_NODE (&pdatanode->rdnode); - NSTACK_RD_PROTO_ITEM_COPY (&(pdatanode->item), pitem); - if (hlist_empty (&(hlist->headlist))) + INIT_HLIST_NODE(&pdatanode->rdnode); + NSTACK_RD_PROTO_ITEM_COPY(&(pdatanode->item), pitem); + + if (hlist_empty(&(hlist->headlist))) { - hlist_add_head (&(pdatanode->rdnode), &(hlist->headlist)); + hlist_add_head(&(pdatanode->rdnode), &(hlist->headlist)); - return NSTACK_RD_SUCCESS; + return NSTACK_RD_SUCCESS; } - hlist_for_each_entry (tempdata, tempnode, &(hlist->headlist), rdnode) - { - if (tempdata->item.proto_type == pitem->proto_type) - - { - tempdata->item.stack_id = pitem->stack_id; - tempdata->item.agetime = NSTACK_RD_AGETIME_MAX; - free (pdatanode); - return NSTACK_RD_SUCCESS; - } - } - hlist_add_head (&(pdatanode->rdnode), &(hlist->headlist)); + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { + if (tempdata->item.proto_data.value == pitem->proto_data.value) + { + NSSOC_LOGDBG + ("find duplicate node, proto value:%u, old stack_id:%d, new stack_id:%d", + pitem->proto_data.value, tempdata->item.stack_id, + pitem->stack_id); + tempdata->item.stack_id = pitem->stack_id; + tempdata->item.agetime = NSTACK_RD_AGETIME_MAX; + free(pdatanode); /*this function is necessary */ + return NSTACK_RD_SUCCESS; + } + } + hlist_add_head(&(pdatanode->rdnode), &(hlist->headlist)); - return NSTACK_RD_SUCCESS; + return NSTACK_RD_SUCCESS; } /* *find stackid by ip - *input ip must netorder + *input ip must be network order */ -int -nstack_rd_proto_item_find (nstack_rd_list * hlist, void *rdkey, void *outitem) +int nstack_rd_proto_item_find(nstack_rd_list * hlist, void *rdkey, + void *outitem) { - struct hlist_node *tempnode = NULL; - nstack_rd_node *tempdata = NULL; - nstack_rd_key *key = (nstack_rd_key *) rdkey; - rd_data_item *pitem = (rd_data_item *) outitem; - hlist_for_each_entry (tempdata, tempnode, &(hlist->headlist), rdnode) - { - - /*if already exist, just return success */ - if (tempdata->item.proto_type == key->proto_type) - - { - NSTACK_RD_PROTO_ITEM_COPY (pitem, &(tempdata->item)); - return NSTACK_RD_SUCCESS; - } - } + struct hlist_node *tempnode = NULL; + nstack_rd_node *tempdata = NULL; + nstack_rd_key *key = (nstack_rd_key *) rdkey; + rd_data_item *pitem = (rd_data_item *) outitem; - NSSOC_LOGDBG ("protocol type item not found", key->proto_type); + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { + if (tempdata->item.proto_data.value == key->proto) + { + NSTACK_RD_PROTO_ITEM_COPY(pitem, &(tempdata->item)); + return NSTACK_RD_SUCCESS; + } + } - return NSTACK_RD_FAIL; + /* Optimize log */ + NSSOC_LOGDBG("proto:%u item not found", key->proto); + + return NSTACK_RD_FAIL; } /***************************************************************************** @@ -130,61 +134,92 @@ nstack_rd_proto_item_find (nstack_rd_list * hlist, void *rdkey, void *outitem) * Calls : * Called By : *****************************************************************************/ -int -nstack_rd_proto_item_age (nstack_rd_list * hlist) +int nstack_rd_proto_item_age(nstack_rd_list * hlist) { - struct hlist_node *tempnode = NULL; - nstack_rd_node *tempdata = NULL; - nstack_rd_node *prevdata = NULL; - struct hlist_node *prevnode = NULL; - NSSOC_LOGINF ("nstack rd ip age begin"); - hlist_for_each_entry (tempdata, tempnode, &(hlist->headlist), rdnode) - { - /*if agetime equal 0, remove it */ - if (tempdata->item.agetime <= 0) - { + struct hlist_node *tempnode = NULL; + nstack_rd_node *tempdata = NULL; + nstack_rd_node *prevdata = NULL; + struct hlist_node *prevnode = NULL; + NSSOC_LOGINF("nstack rd proto age begin"); + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { + /*if agetime equal 0, remove it */ + if (tempdata->item.agetime <= 0) + { + if (prevdata) + { + /* Optimize log */ + NSSOC_LOGDBG("stackid=%d, proto:%u was aged", + tempdata->item.stack_id, + tempdata->item.proto_data.value); + + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ + } + prevdata = tempdata; + prevnode = tempnode; + } + else + { + tempdata->item.agetime--; + } + } + if (prevdata) + { + if (tempdata) + { + /* Optimize log */ + NSSOC_LOGDBG("stackid:%d, proto:%u was last aged", + tempdata->item.stack_id, + tempdata->item.proto_data.value); + } + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ + } + NSSOC_LOGINF("nstack rd proto age end"); + return NSTACK_RD_SUCCESS; +} + +void nstack_rd_proto_item_clean(nstack_rd_list * hlist) +{ + struct hlist_node *tempnode = NULL; + nstack_rd_node *tempdata = NULL; + nstack_rd_node *prevdata = NULL; + struct hlist_node *prevnode = NULL; + NSSOC_LOGINF("nstack rd proto item clean begin"); + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { if (prevdata) - { - NSSOC_LOGDBG ("stackid:%d, protocol type was aged", - tempdata->item.stack_id, tempdata->item.proto_type); + { + NSSOC_LOGDBG("stackid=%d, proto:%u was cleaned", + tempdata->item.stack_id, + tempdata->item.proto_data.value); + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ + } - hlist_del_init (prevnode); - free (prevdata); - } prevdata = tempdata; prevnode = tempnode; - } - else - { - tempdata->item.agetime--; - } - } - if (prevdata) + } + if (prevdata) { - if (tempdata) + if (tempdata) { - NSSOC_LOGDBG ("stackid:%d, protocol type was aged", - tempdata->item.stack_id, tempdata->item.proto_type); + NSSOC_LOGDBG("stackid=%d, proto:%u was last cleaned", + tempdata->item.stack_id, + tempdata->item.proto_data.value); } - hlist_del_init (prevnode); - free (prevdata); + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ } - NSSOC_LOGINF ("nstack rd ip age end"); - return NSTACK_RD_SUCCESS; + NSSOC_LOGINF("nstack rd proto item clean end"); } /* *find stackid by spec ip(multicast ip) - *input ip must netorder + *input ip must be network order */ -int -nstack_rd_proto_spec (void *rdkey) -{ - return -1; -} - -int -nstack_rd_proto_default (void *rdkey) +int nstack_rd_proto_spec(void *rdkey) { - return -1; + return -1; } diff --git a/src/nSocket/nstack_rd/nstack_rd_proto.h b/src/nSocket/nstack_rd/nstack_rd_proto.h index f3b0311..e8a573e 100644 --- a/src/nSocket/nstack_rd/nstack_rd_proto.h +++ b/src/nSocket/nstack_rd/nstack_rd_proto.h @@ -16,20 +16,23 @@ #ifndef __NSTACK_RD_PROTO_H #define __NSTACK_RD_PROTO_H -/* *INDENT-OFF* */ + +#include "nstack_rd_priv.h" + #define NSTACK_RD_PROTO_ITEM_COPY(destitem, srcitem){ \ (destitem)->agetime = (srcitem)->agetime; \ (destitem)->stack_id = (srcitem)->stack_id; \ (destitem)->type = (srcitem)->type; \ - (destitem)->proto_type = (srcitem)->proto_type; \ + (destitem)->proto_data.value = (srcitem)->proto_data.value; \ + (destitem)->proto_data.attr = (srcitem)->proto_data.attr; \ } -/* *INDENT-ON* */ -int nstack_rd_proto_cpy (void *destdata, void *srcdata); -int nstack_rd_proto_item_insert (nstack_rd_list * hlist, void *rditem); -int nstack_rd_proto_item_find (nstack_rd_list * hlist, void *rdkey, - void *outitem); -int nstack_rd_proto_item_age (nstack_rd_list * hlist); -int nstack_rd_proto_spec (void *rdkey); -int nstack_rd_proto_default (void *rdkey); -#endif /* */ +int nstack_rd_proto_data_cpy(void *destdata, void *srcdata); +int nstack_rd_proto_item_insert(nstack_rd_list * hlist, void *rditem); +int nstack_rd_proto_item_find(nstack_rd_list * hlist, void *rdkey, + void *outitem); +int nstack_rd_proto_item_age(nstack_rd_list * hlist); +void nstack_rd_proto_item_clean(nstack_rd_list * hlist); +int nstack_rd_proto_spec(void *rdkey); + +#endif diff --git a/src/nSocket/nstack_rd/nstack_rd_type.c b/src/nSocket/nstack_rd/nstack_rd_type.c new file mode 100644 index 0000000..c5f440e --- /dev/null +++ b/src/nSocket/nstack_rd/nstack_rd_type.c @@ -0,0 +1,229 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include <stdlib.h> +#include <arpa/inet.h> +#include "nstack_rd_data.h" +#include "nstack_rd.h" +#include "nstack_rd_priv.h" +#include "nstack_rd_type.h" +#include "nstack_log.h" +#include "nstack_securec.h" + +/*copy rd data*/ +int nstack_rd_type_data_cpy(void *destdata, void *srcdata) +{ + rd_data_item *pitem = (rd_data_item *) destdata; + rd_route_data *pdata = (rd_route_data *) srcdata; + + pitem->type = pdata->type; + pitem->type_data.value = pdata->type_data.value; + pitem->type_data.attr = pdata->type_data.attr; + pitem->type_data.reserved[0] = pdata->type_data.reserved[0]; + pitem->type_data.reserved[1] = pdata->type_data.reserved[1]; + pitem->type_data.reserved[2] = pdata->type_data.reserved[2]; + pitem->type_data.reserved[3] = pdata->type_data.reserved[3]; + return NSTACK_RD_SUCCESS; +} + +/* + * Add an ip segment to the list and sort it in descending order of ip mask length + * If the list already exists in the same list of ip side, then stack_id update + *ip is network byte order + */ +/*vars are used in macro*/ +int nstack_rd_type_item_insert(nstack_rd_list * hlist, void *rditem) +{ + nstack_rd_node *pdatanode = NULL; + nstack_rd_node *tempdata = NULL; + struct hlist_node *tempnode = NULL; + rd_data_item *pitem = (rd_data_item *) rditem; + + NSSOC_LOGDBG("stackid:%d, type value:%u type attr:%u was inserted", + pitem->stack_id, pitem->type_data.value, + pitem->type_data.attr); + + pdatanode = (nstack_rd_node *) malloc(sizeof(nstack_rd_node)); /*this function is necessary */ + if (!pdatanode) + { + NSSOC_LOGERR("nstack rd item malloc fail"); + return NSTACK_RD_FAIL; + } + /* add return value check */ + int retVal = memset_s(pdatanode, sizeof(nstack_rd_node), 0, + sizeof(nstack_rd_node)); + if (EOK != retVal) + { + NSSOC_LOGERR("memset_s failed]retVal=%d", retVal); + free(pdatanode); /*this function is necessary */ + return NSTACK_RD_FAIL; + } + INIT_HLIST_NODE(&pdatanode->rdnode); + NSTACK_RD_TYPE_ITEM_COPY(&(pdatanode->item), pitem); + + if (hlist_empty(&(hlist->headlist))) + { + hlist_add_head(&(pdatanode->rdnode), &(hlist->headlist)); + + return NSTACK_RD_SUCCESS; + + } + + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { + if (tempdata->item.type_data.value == pitem->type_data.value) + { + NSSOC_LOGDBG + ("find duplicate node, type value:%u, old stack_id:%d, new stack_id:%d", + pitem->type_data.value, tempdata->item.stack_id, + pitem->stack_id); + tempdata->item.stack_id = pitem->stack_id; + tempdata->item.agetime = NSTACK_RD_AGETIME_MAX; + free(pdatanode); /*this function is necessary */ + return NSTACK_RD_SUCCESS; + } + } + hlist_add_head(&(pdatanode->rdnode), &(hlist->headlist)); + + return NSTACK_RD_SUCCESS; + +} + +/* + *find stackid by ip + *input ip must be network order + */ +int nstack_rd_type_item_find(nstack_rd_list * hlist, void *rdkey, + void *outitem) +{ + struct hlist_node *tempnode = NULL; + nstack_rd_node *tempdata = NULL; + nstack_rd_key *key = (nstack_rd_key *) rdkey; + rd_data_item *pitem = (rd_data_item *) outitem; + + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { + if (tempdata->item.type_data.value == key->socket_type) + { + NSTACK_RD_TYPE_ITEM_COPY(pitem, &(tempdata->item)); + return NSTACK_RD_SUCCESS; + } + } + + /* Optimize log */ + NSSOC_LOGDBG("socket type:%u item not found", key->socket_type); + + return NSTACK_RD_FAIL; +} + +/***************************************************************************** +* Prototype : nstack_rd_ip_item_age +* Description : delete the ip item that have not been add again for one time +* Input : nstack_rd_list *hlist +* Output : None +* Return Value : int +* Calls : +* Called By : +*****************************************************************************/ +int nstack_rd_type_item_age(nstack_rd_list * hlist) +{ + struct hlist_node *tempnode = NULL; + nstack_rd_node *tempdata = NULL; + nstack_rd_node *prevdata = NULL; + struct hlist_node *prevnode = NULL; + NSSOC_LOGINF("nstack rd type age begin"); + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { + /*if agetime equal 0, remove it */ + if (tempdata->item.agetime <= 0) + { + if (prevdata) + { + /* Optimize log */ + NSSOC_LOGDBG("stackid=%d, type:%u was aged", + tempdata->item.stack_id, + tempdata->item.type_data.value); + + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ + } + prevdata = tempdata; + prevnode = tempnode; + } + else + { + tempdata->item.agetime--; + } + } + if (prevdata) + { + if (tempdata) + { + /* Optimize log */ + NSSOC_LOGDBG("stackid:%d, type:%u was last aged", + tempdata->item.stack_id, + tempdata->item.type_data.value); + } + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ + } + NSSOC_LOGINF("nstack rd type age end"); + return NSTACK_RD_SUCCESS; +} + +void nstack_rd_type_item_clean(nstack_rd_list * hlist) +{ + struct hlist_node *tempnode = NULL; + nstack_rd_node *tempdata = NULL; + nstack_rd_node *prevdata = NULL; + struct hlist_node *prevnode = NULL; + NSSOC_LOGINF("nstack rd type item clean begin"); + hlist_for_each_entry(tempdata, tempnode, &(hlist->headlist), rdnode) + { + if (prevdata) + { + NSSOC_LOGDBG("stackid=%d, type:%u was cleaned", + tempdata->item.stack_id, + tempdata->item.type_data.value); + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ + } + + prevdata = tempdata; + prevnode = tempnode; + } + if (prevdata) + { + if (tempdata) + { + NSSOC_LOGDBG("stackid=%d, type:%u was last cleaned", + tempdata->item.stack_id, + tempdata->item.type_data.value); + } + hlist_del_init(prevnode); + free(prevdata); /*this function is necessary */ + } + NSSOC_LOGINF("nstack rd type item clean end"); +} + +/* + *find stackid by spec ip(multicast ip) + *input ip must be network order + */ +int nstack_rd_type_spec(void *rdkey) +{ + return -1; +} diff --git a/src/nSocket/nstack_rd/nstack_rd_type.h b/src/nSocket/nstack_rd/nstack_rd_type.h new file mode 100644 index 0000000..d50fa87 --- /dev/null +++ b/src/nSocket/nstack_rd/nstack_rd_type.h @@ -0,0 +1,42 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef __NSTACK_RD_TYPE_H +#define __NSTACK_RD_TYPE_H + +#include "nstack_rd_priv.h" + +#define NSTACK_RD_TYPE_ITEM_COPY(destitem, srcitem){ \ + (destitem)->agetime = (srcitem)->agetime; \ + (destitem)->stack_id = (srcitem)->stack_id; \ + (destitem)->type = (srcitem)->type; \ + (destitem)->type_data.value = (srcitem)->type_data.value; \ + (destitem)->type_data.attr = (srcitem)->type_data.attr; \ + (destitem)->type_data.reserved[0] = (srcitem)->type_data.reserved[0]; \ + (destitem)->type_data.reserved[1] = (srcitem)->type_data.reserved[1]; \ + (destitem)->type_data.reserved[2] = (srcitem)->type_data.reserved[2]; \ + (destitem)->type_data.reserved[3] = (srcitem)->type_data.reserved[3]; \ +} + +int nstack_rd_type_data_cpy(void *destdata, void *srcdata); +int nstack_rd_type_item_insert(nstack_rd_list * hlist, void *rditem); +int nstack_rd_type_item_find(nstack_rd_list * hlist, void *rdkey, + void *outitem); +int nstack_rd_type_item_age(nstack_rd_list * hlist); +void nstack_rd_type_item_clean(nstack_rd_list * hlist); +int nstack_rd_type_spec(void *rdkey); + +#endif |