void route_table_mgr::create_route_val_from_info(const netlink_route_info *netlink_route_info, route_val &netlink_route_val) { char dst_addr_chr[ADDR_LEN]; inet_ntop(AF_INET, netlink_route_info->dst_addr, dst_addr_chr, ADDR_LEN); netlink_route_val.set_dst_addr(inet_addr((const char*)dst_addr_chr)); in_addr_t dst_mask = htonl(VMA_NETMASK(netlink_route_info->dst_prefixlen)); netlink_route_val.set_dst_mask(dst_mask); netlink_route_val.set_dst_pref_len(netlink_route_info->dst_prefixlen); netlink_route_val.set_protocol(netlink_route_info->protocol); netlink_route_val.set_scope(netlink_route_info->scope); netlink_route_val.set_type(netlink_route_info->type); int if_index = netlink_route_info->oif; netlink_route_val.set_if_index(if_index); char if_name[IFNAMSIZ]; if_indextoname(if_index,if_name); netlink_route_val.set_if_name(if_name); struct sockaddr_in src_addr; if (!get_ipv4_from_ifname(if_name, &src_addr)) { netlink_route_val.set_src_addr(src_addr.sin_addr.s_addr); } netlink_route_val.set_str(); if (g_vlogger_level >= VLOG_FUNC) { netlink_route_val.print_route_val(); } }
bool route_table_mgr::rt_mgr_parse_enrty(nlmsghdr *nl_header, route_val *p_rtv) { int len; struct rtmsg *rt_msg; struct rtattr *rt_attribute; // get route entry header rt_msg = (struct rtmsg *) NLMSG_DATA(nl_header); // we are only concerned about the main route table if (rt_msg->rtm_family != AF_INET || rt_msg->rtm_table != RT_TABLE_MAIN) return false; p_rtv->set_protocol(rt_msg->rtm_protocol); p_rtv->set_scope(rt_msg->rtm_scope); p_rtv->set_type(rt_msg->rtm_type); in_addr_t dst_mask = htonl(VMA_NETMASK(rt_msg->rtm_dst_len)); p_rtv->set_dst_mask(dst_mask); p_rtv->set_dst_pref_len(rt_msg->rtm_dst_len); len = RTM_PAYLOAD(nl_header); rt_attribute = (struct rtattr *) RTM_RTA(rt_msg); for (;RTA_OK(rt_attribute, len);rt_attribute=RTA_NEXT(rt_attribute,len)) { rt_mgr_parse_attr(rt_attribute, p_rtv); } p_rtv->set_state(true); if (!p_rtv->get_src_addr()) { struct sockaddr_in src_addr; char *if_name = (char *)p_rtv->get_if_name(); if (!get_ipv4_from_ifname(if_name, &src_addr)) { p_rtv->set_src_addr(src_addr.sin_addr.s_addr); } else { // Failed mapping if_name to IPv4 address // Should we log or return error also from here? } } p_rtv->set_str(); return true; }
void route_table_mgr::rt_mgr_update_source_ip() { route_val *p_val; //for route entries which still have no src ip and no gw for (int i = 0; i < m_tab.entries_num; i++) { p_val = &m_tab.value[i]; if (p_val->get_src_addr() || p_val->get_gw_addr()) continue; if (g_p_net_device_table_mgr) { //try to get src ip from net_dev list of the interface in_addr_t longest_prefix = 0; in_addr_t correct_src = 0; net_dev_lst_t* nd_lst = g_p_net_device_table_mgr->get_net_device_val_lst_from_index(p_val->get_if_index()); if (nd_lst) { net_dev_lst_t::iterator iter = nd_lst->begin(); while (iter != nd_lst->end()) { if((p_val->get_dst_addr() & (*iter)->get_netmask()) == ((*iter)->get_local_addr() & (*iter)->get_netmask())) { //found a match in routing table if(((*iter)->get_netmask() | longest_prefix) != longest_prefix){ longest_prefix = (*iter)->get_netmask(); // this is the longest prefix match correct_src = (*iter)->get_local_addr(); } } iter++; } if (correct_src) { p_val->set_src_addr(correct_src); continue; } } } // if still no src ip, get it from ioctl struct sockaddr_in src_addr; char *if_name = (char *)p_val->get_if_name(); if (!get_ipv4_from_ifname(if_name, &src_addr)) { p_val->set_src_addr(src_addr.sin_addr.s_addr); } else { // Failed mapping if_name to IPv4 address rt_mgr_logwarn("could not figure out source ip for rtv = %s", p_val->to_str()); } } //for route entries with gateway, do recursive search for src ip int num_unresolved_src = m_tab.entries_num; int prev_num_unresolved_src = 0; do { prev_num_unresolved_src = num_unresolved_src; num_unresolved_src = 0; for (int i = 0; i < m_tab.entries_num; i++) { p_val = &m_tab.value[i]; if (p_val->get_gw_addr() && !p_val->get_src_addr()) { route_val* p_val_dst; in_addr_t in_addr = p_val->get_gw_addr(); unsigned char table_id = p_val->get_table_id(); if (find_route_val(in_addr, table_id, p_val_dst)) { if (p_val_dst->get_src_addr()) { p_val->set_src_addr(p_val_dst->get_src_addr()); } else if (p_val == p_val_dst) { //gateway of the entry lead to same entry net_dev_lst_t* nd_lst = g_p_net_device_table_mgr->get_net_device_val_lst_from_index(p_val->get_if_index()); if (nd_lst) { net_dev_lst_t::iterator iter = nd_lst->begin(); while (iter != nd_lst->end()) { if(p_val->get_gw_addr() == (*iter)->get_local_addr()) { p_val->set_gw(0); p_val->set_src_addr((*iter)->get_local_addr()); break; } iter++; } } if (!p_val->get_src_addr()) num_unresolved_src++; } else { num_unresolved_src++; } // gateway and source are equal, no need of gw. if (p_val->get_src_addr() == p_val->get_gw_addr()) { p_val->set_gw(0); } } else { num_unresolved_src++; } } } } while (num_unresolved_src && prev_num_unresolved_src > num_unresolved_src); //for route entries which still have no src ip for (int i = 0; i < m_tab.entries_num; i++) { p_val = &m_tab.value[i]; if (p_val->get_src_addr()) continue; if (p_val->get_gw_addr()) { rt_mgr_logdbg("could not figure out source ip for gw address. rtv = %s", p_val->to_str()); } // if still no src ip, get it from ioctl struct sockaddr_in src_addr; char *if_name = (char *)p_val->get_if_name(); if (!get_ipv4_from_ifname(if_name, &src_addr)) { p_val->set_src_addr(src_addr.sin_addr.s_addr); } else { // Failed mapping if_name to IPv4 address rt_mgr_logwarn("could not figure out source ip for rtv = %s", p_val->to_str()); } } }