我正在尝试使用libnl-3-routing添加路由(内核2.6.32-57)。我使用文档:
但是路由章节是空的......;)
问题是“无效输入数据或参数”的内核响应。我已经使用netlink做了一些测试,如果我设置了以下属性,我可以添加一个路由:
面具
int ret = 0;
// Create the route.
struct rtnl_route* rulesRoute = rtnl_route_alloc();
rtnl_route_set_iif(rulesRoute, AF_INET); // IPV4
// Set parameters.
rtnl_route_set_scope(rulesRoute, RT_SCOPE_UNIVERSE);
rtnl_route_set_table(rulesRoute, RT_TABLE_MAIN);
rtnl_route_set_protocol(rulesRoute, RTPROT_STATIC);
uint8_t maskTest = 16;
rtnl_route_set_scope(rulesRoute, maskTest);
// Set the destination.
char destinationAddr[] = "0.0.0.0";
nl_addr* dstAddr = nl_addr_build(AF_INET, destinationAddr, 8);
ret = rtnl_route_set_dst(rulesRoute, dstAddr);
if (ret != 0)
std::cout << "Error in setting destination route: " << nl_geterror(ret) << std::endl;
// Set the next hop.
struct rtnl_nexthop* route_nexthop = rtnl_route_nh_alloc();
char gatewayAddr[] = "10.110.0.240";
nl_addr* gatewAddr = nl_addr_build(AF_INET, gatewayAddr, 12);
rtnl_route_nh_set_gateway(route_nexthop, gatewAddr);
rtnl_route_nh_set_ifindex(route_nexthop, 2);
rtnl_route_add_nexthop(rulesRoute, route_nexthop);
ret = rtnl_route_add(m_nlSocket, rulesRoute, 0);
if (ret != 0)
std::cout << "Kernel response:" << nl_geterror(ret) << std::endl;
我在路由表中添加了信息:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.110.0.0 0.0.0.0 255.255.255.0 U 1 0 0 eth3
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eth3
0.0.0.0 10.110.0.1 0.0.0.0 UG 0 0 0 eth3
感谢您帮助解决此问题
答案 0 :(得分:0)
问题来自于功能nl_addr_build的错误使用。
替换为nl_addr_parse()并且正在运行。
nl_addr_build:解析二进制地址
nl_addr_parse:解析像“10.10.1.10”这样的char * addr