/* Generic function for conversion string to struct prefix. */ int str2prefix(const char *str, struct prefix *p) { int ret; /* First we try to convert string to struct prefix_ipv4. */ ret = str2prefix_ipv4(str, (struct prefix_ipv4 *)p); if (ret) return ret; #ifdef HAVE_IPV6 /* Next we try to convert string to struct prefix_ipv6. */ ret = str2prefix_ipv6(str, (struct prefix_ipv6 *)p); if (ret) return ret; #endif /* HAVE_IPV6 */ return 0; }
/* IPv4 route add and delete test. */ void zebra_test_ipv4 (int command, int type, char *prefix, char *gateway, u_char distance) { struct zapi_ipv4 api; struct prefix_ipv4 p; struct in_addr gate; struct in_addr *gpnt; str2prefix_ipv4 (prefix, &p); inet_aton (gateway, &gate); gpnt = &gate; api.type = type; api.flags = 0; api.message = 0; SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); api.nexthop_num = 1; api.nexthop = &gpnt; api.ifindex_num = 0; if (distance) { SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE); api.distance = distance; } switch (command) { case ZEBRA_IPV4_ROUTE_ADD: zapi_ipv4_add (zclient, &p, &api); break; case ZEBRA_IPV4_ROUTE_DELETE: zapi_ipv4_delete (zclient, &p, &api); break; } }
/* * add_node * * Add the given prefix (passed in as a string) to the given table. */ static void add_node(struct bgp_table *table, const char *prefix_str) { struct prefix_ipv4 p; struct test_node_t *node; struct bgp_node *rn; assert(prefix_str); if (str2prefix_ipv4(prefix_str, &p) <= 0) assert(0); rn = bgp_node_get(table, (struct prefix *)&p); if (rn->info) { assert(0); return; } node = malloc(sizeof(struct test_node_t)); assert(node); node->prefix_str = strdup(prefix_str); assert(node->prefix_str); rn->info = node; }
} rip_passive_interface_apply_all (); } /* RIP enable network or interface configuration. */ DEFUN (rip_network, rip_network_cmd, "network (A.B.C.D/M|WORD)", "Enable routing on an IP network\n" "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n" "Interface name\n") { int ret; struct prefix_ipv4 p; ret = str2prefix_ipv4 (argv[0], &p); if (ret) ret = rip_enable_network_add ((struct prefix *) &p); else ret = rip_enable_if_add (argv[0]); if (ret < 0) { vty_out (vty, "There is a same network configuration %s%s", argv[0], VTY_NEWLINE); return CMD_WARNING; } return CMD_SUCCESS; }
void rt_policy_for_logic_restore_dbtable(void) { s32 i = 0; u32 count = 0; s32 sys_ret = 0; s32 sequence = 0; u32 policyid = 0; s8 *src = NULL; s8 *dest = NULL; s8 *iifname = NULL; s32 protocol; s32 minsrcport, maxsrcport, mindstport, maxdstport; s32 flag = 0; s32 tos = 0; s8 sql_select[512] = {0}; s32 iifindex; s32 sys_error_code = 0; sqlite3_res res = NULL; rtp_logicplcy_s add; struct prefix_ipv4 prefix_src; struct prefix_ipv4 prefix_dest; struct in_addr srcmask; struct in_addr dstmask; void *buffer = NULL; /*打开数据库连接*/ if (NULL == db) { db = sqlite3_open_ex(1, DATABASE_RT_POLICY_PATH); if (NULL == db) { return; } } /*查询策略个数*/ sprintf(sql_select, "select count(*) as count from tb_logic_policy;"); if ( SQLITE_OK != sqlite3_exec_query_ex(db, sql_select, &res) ) { goto lable_ret; } sqlite3_get_u32_ex(res, 0, "count", &count); sqlite3_res_free_ex(res); res = NULL; if(0 == count) { goto lable_ret; } /*按sequence排序*/ sprintf(sql_select, "select * from tb_logic_policy order by sequence;"); if ( SQLITE_OK != sqlite3_exec_query_ex(db, sql_select, &res) ) { goto lable_ret; } for (i = 0; i <(s32) count; i++) { sqlite3_get_u32_ex(res, i, "policy_id", &policyid); sqlite3_get_str_ex(res, i, "src", &src); sqlite3_get_str_ex(res, i, "dest", &dest); sqlite3_get_str_ex(res, i, "iifname", &iifname); sqlite3_get_s32_ex(res, i, "tos", &tos); sqlite3_get_s32_ex(res, i, "protocol", &protocol); sqlite3_get_s32_ex(res, i, "minsrcport", &minsrcport); sqlite3_get_s32_ex(res, i, "maxsrcport", &maxsrcport); sqlite3_get_s32_ex(res, i, "mindstport", &mindstport); sqlite3_get_s32_ex(res, i, "maxdstport", &maxdstport); sqlite3_get_s32_ex(res, i, "flag", &flag); /*将公网地址由点分十进制转换为网络二进制数*/ if (0 == str2prefix_ipv4(src, &prefix_src)) { goto lable_ret; } if (0 == str2prefix_ipv4(dest, &prefix_dest)) { goto lable_ret; } masklen2ip ((s32)prefix_src.prefixlen, &srcmask); prefix_src.prefix.s_addr &= srcmask.s_addr; masklen2ip ((s32)prefix_dest.prefixlen, &dstmask); prefix_dest.prefix.s_addr &= dstmask.s_addr; /*将接口名转换成接口索引*/ if ((!strcmp("any", iifname))||(!strcmp("Any", iifname))) { iifindex = 0; } else { if (0 != if_get_index_by_name(iifname, &iifindex)) { goto lable_ret; } } /*填充要向数据面发送的数据*/ add.id = policyid; add.src = prefix_src.prefix.s_addr; add.srcmask = srcmask.s_addr; add.dst = prefix_dest.prefix.s_addr; add.dstmask = dstmask.s_addr; add.iifindex = iifindex; add.tos = (u8)tos; add.protocol = (u8)protocol; add.srcminportnum = (u16)minsrcport; add.srcmaxportnum = (u16)maxsrcport; add.dstminportnum = (u16)mindstport; add.dstmaxportnum = (u16)maxdstport; add.flag = (u16)flag; buffer = malloc(sizeof(s32) + sizeof(rtp_logicplcy_s)); if(NULL == buffer) { goto lable_ret; } *(s32 *)buffer = sequence; memcpy(buffer + sizeof(s32), &add, sizeof(rtp_logicplcy_s)); sys_ret = conplat_syscall(MODULEID_RT_POLICY, RTP_LOGICPLCY_SYSCALL_ADD_OR_MODIFY, buffer, sizeof(s32) + sizeof(rtp_logicplcy_s), (s32*)&sys_error_code); /* 如果系统调用不成功,返回系统错误 */ if((0 != sys_error_code && 1 != sys_error_code) || 0 != sys_ret) { goto lable_ret; } cpuproxy_list_item_addmod(sequence, &add); } lable_ret: if (NULL != buffer) { free (buffer); buffer = NULL; } /*释放结果集*/ if (NULL != res) { sqlite3_res_free_ex(res); res = NULL; } return; }
/******************************************************************************* 函数名称 : rt_policy_restore_dbtable 函数功能 : 策略路由配置恢复 输入参数 : 无 输出参数 : 无 返回结果 : 无 -------------------------------------------------------------------------------- 最近一次修改记录 : 修改作者 : wangchao 修改目的 :将策略路由配置恢复移至策略路由deamon进程 修改日期 : 2010年12月30日 *******************************************************************************/ void rt_policy_restore_dbtable(s32 listflag, u32 *dcdm_needed) { s32 i = 0, j = 0; u32 count = 0; s32 sys_ret = 0; s32 sequence = 0; u32 policyid = 0; s8 *src = NULL; s8 *dest = NULL; s8 *iifname = NULL; s8 *oifname; s8 *nexthop; s8 *weight; s32 nexthop_count, protocol, netappid; u32 flag = 0; s32 minsrcport, maxsrcport, mindstport, maxdstport; s8 *tos = NULL; s8 sql_select[512] = {0}; s8 tb_name[32] = {0}; s32 iifindex; s32 sys_error_code = 0; sqlite3_res res = NULL; rt_policy_s add; struct prefix_ipv4 prefix_src = {0}; struct prefix_ipv4 prefix_dest = {0}; struct in_addr nexthop_addr[RT_POLICY_MAX_NEXTHOP]; struct in_addr srcmask = {0}; struct in_addr dstmask = {0}; u32 srcgroup = 0; u32 destgroup = 0; void *buffer = NULL; s8 oifname_str[RT_POLICY_MAX_NEXTHOP][20]; s8 nexthop_str[RT_POLICY_MAX_NEXTHOP][100]; s8 weight_str[RT_POLICY_MAX_NEXTHOP][10]; s8 *str_point[3], *line_point[3]; s32 oifindex[RT_POLICY_MAX_NEXTHOP]; s32 weightvalue[RT_POLICY_MAX_NEXTHOP]; switch (listflag) { case RT_POLICY_BEFORE_ROUTE: strcpy(tb_name, "tb_rt_policy_pre"); break; case RT_POLICY_AFTER_ROUTE: strcpy(tb_name, "tb_rt_policy_aft"); break; case RT_POLICY_LOCAL_ROUTE: strcpy(tb_name, "tb_rt_policy_loc"); break; case RT_POLICY_PRE_LOCAL_ROUTE: strcpy(tb_name, "tb_rt_policy_pre_loc"); break; } /*打开数据库连接*/ if (NULL == db) { db = sqlite3_open_ex(2, DATABASE_RT_POLICY_PATH, "/config/db/netaddr_cfg.db"); if (NULL == db) { return; } } /*查询策略个数*/ sprintf(sql_select, "select count(*) as count from %s;", tb_name); sys_error_code = sqlite3_exec_query_ex(db, sql_select, &res); if ( SQLITE_OK != sys_error_code ) { goto lable_ret; } sqlite3_get_u32_ex(res, 0, "count", &count); sqlite3_res_free_ex(res); res = NULL; if(0 == count) { goto lable_ret; } memset(sql_select, 0, 512); /*按sequence排序*/ sprintf(sql_select, "select * from %s order by sequence;", tb_name); sys_error_code = sqlite3_exec_query_ex(db, sql_select, &res); if ( SQLITE_OK != sys_error_code ) { goto lable_ret; } buffer = calloc(1, sizeof(s32) * 2 + sizeof(rt_policy_s)); if(NULL == buffer) { goto lable_ret; } /*赋值*/ for (i = 0; i <(s32) count; i++) { sqlite3_get_u32_ex(res, i, "policy_id", &policyid); sqlite3_get_str_ex(res, i, "src", &src); sqlite3_get_str_ex(res, i, "dest", &dest); sqlite3_get_str_ex(res, i, "iifname", &iifname); sqlite3_get_str_ex(res, i, "oifname", &oifname); sqlite3_get_str_ex(res, i, "nexthop", &nexthop); sqlite3_get_str_ex(res, i, "weight", &weight); sqlite3_get_str_ex(res, i, "tos", &tos); sqlite3_get_s32_ex(res, i, "nexthopcount", &nexthop_count); sqlite3_get_s32_ex(res, i, "netappid", &netappid); sqlite3_get_s32_ex(res, i, "protocol", &protocol); sqlite3_get_s32_ex(res, i, "minsrcport", &minsrcport); sqlite3_get_s32_ex(res, i, "maxsrcport", &maxsrcport); sqlite3_get_s32_ex(res, i, "mindstport", &mindstport); sqlite3_get_s32_ex(res, i, "maxdstport", &maxdstport); sqlite3_get_u32_ex(res, i, "flag", &flag); str_point[0] = oifname; str_point[1] = weight; str_point[2] = nexthop; /*解析下一跳信息*/ for(j = 0; j < nexthop_count; j++) { if(j != (nexthop_count - 1)) { line_point[0] = static_strchr(str_point[0],';'); static_strncpy(oifname_str[j], str_point[0], line_point[0] - str_point[0]); str_point[0] = line_point[0] + 1; line_point[1] = static_strchr(str_point[1],';'); static_strncpy(weight_str[j], str_point[1], line_point[1] - str_point[1]); str_point[1] = line_point[1] + 1; line_point[2] = static_strchr(str_point[2],';'); static_strncpy(nexthop_str[j], str_point[2], line_point[2] - str_point[2]); str_point[2] = line_point[2] + 1; } else { line_point[0] = static_strchr(str_point[0],'\0'); static_strncpy(oifname_str[j], str_point[0], line_point[0] - str_point[0]); line_point[1] = static_strchr(str_point[1],'\0'); static_strncpy(weight_str[j], str_point[1], line_point[1] - str_point[1]); line_point[2] = static_strchr(str_point[2],'\0'); static_strncpy(nexthop_str[j], str_point[2], line_point[2] - str_point[2]); } } for(j = 0; j < nexthop_count; j++) { sys_error_code = if_get_index_by_name(oifname_str[j], &oifindex[j]); if (sys_error_code != ERROR_SUCCESS) { goto lable_ret; } weightvalue[j] = atoi(weight_str[j]); inet_aton (nexthop_str[j], &nexthop_addr[j]); } for(; j < RT_POLICY_MAX_NEXTHOP; j++) { oifindex[j] = 0; nexthop_addr[j].s_addr = 0; weightvalue[j] = 0; } /*将公网地址由点分十进制转换为网络二进制数*/ if(flag & RT_POLICY_SRC_NETADDR_GROUP) { if (0 != netaddr_group_get_id_by_name(db, "",src, &srcgroup)) { flag &= (~RT_POLICY_SRC_NETADDR_GROUP); prefix_src.prefix.s_addr = 0; } else { prefix_src.prefix.s_addr = srcgroup; } srcmask.s_addr = 0; } else { sys_error_code = str2prefix_ipv4(src, &prefix_src); if (sys_error_code <= 0) { goto lable_ret; } masklen2ip ((s32)prefix_src.prefixlen, &srcmask); prefix_src.prefix.s_addr &= srcmask.s_addr; } if(flag & RT_POLICY_DST_NETADDR_GROUP) { if (0 != netaddr_group_get_id_by_name(db, "",dest, &destgroup)) { flag &= (~RT_POLICY_DST_NETADDR_GROUP); prefix_dest.prefix.s_addr = 0; } else { prefix_dest.prefix.s_addr = destgroup; } dstmask.s_addr = 0; } else { sys_error_code = str2prefix_ipv4(dest, &prefix_dest); if (sys_error_code <= 0) { goto lable_ret; } masklen2ip ((s32)prefix_dest.prefixlen, &dstmask); prefix_dest.prefix.s_addr &= dstmask.s_addr; } /*将接口名转换成接口索引*/ if (!strcmp("any", iifname)) { iifindex = 0; } else { sys_error_code = if_get_index_by_name(iifname, &iifindex); if (sys_error_code != 0) { goto lable_ret; } } /*填充要向数据面发送的数据*/ add.id = policyid; add.src = prefix_src.prefix.s_addr; add.srcmask = srcmask.s_addr; add.dst = prefix_dest.prefix.s_addr; add.dstmask = dstmask.s_addr; add.iifindex = iifindex; add.nexthopcount = (u8)nexthop_count; for(j = 0; j < RT_POLICY_MAX_NEXTHOP; j++) { add.oifindex[j] = oifindex[j]; add.nexthop[j] = nexthop_addr[j].s_addr; add.weight[j] = (u8)weightvalue[j]; add.nhflag[j] = 0; } add.tos = (u8)atoi(tos); add.netappid = (u32)netappid; add.protocol = (u8)protocol; add.srcminportnum = (u16)minsrcport; add.srcmaxportnum = (u16)maxsrcport; add.dstminportnum = (u16)mindstport; add.dstmaxportnum = (u16)maxdstport; add.flag = flag; memcpy(buffer, &listflag, sizeof(s32)); memcpy(buffer + sizeof(s32), &sequence, sizeof(s32)); memcpy(buffer + sizeof(s32) * 2, &add, sizeof(rt_policy_s)); sys_ret = conplat_syscall(MODULEID_RT_POLICY, RT_POLICY_SYSCALL_ADD_OR_MODIFY, buffer, 2 * sizeof(s32) + sizeof(rt_policy_s), (s32*)&sys_error_code); /* 如果系统调用不成功,返回系统错误 */ if((0 != sys_error_code && 1 != sys_error_code) || 0 != sys_ret) { goto lable_ret; } if ( (0 != netappid) && (0 == *dcdm_needed) ) { *dcdm_needed = 1; } if(RT_POLICY_BEFORE_ROUTE == listflag) { /* ignore the return value here */ rtpolicy_list_item_addmod(sequence, &add); neighindex_list_item_add(&add); } } lable_ret: if (NULL != buffer) { free (buffer); buffer = NULL; } /*释放结果集*/ if (NULL != res) { sqlite3_res_free_ex(res); res = NULL; } return; }