使用libnl-3-route添加路由“无效的输入数据或参数”

时间:2014-03-07 07:59:54

标签: c++ c routes netlink

我正在尝试使用libnl-3-routing添加路由(内核2.6.32-57)。我使用文档:

  1. Netlink library
  2. Netlink routing
  3. 但是路由章节是空的......;)

    问题是“无效输入数据或参数”的内核响应。我已经使用netlink做了一些测试,如果我设置了以下属性,我可以添加一个路由:

    1. 接口索引
    2. 目的地地址
    3. 网关地址
    4. 面具

          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;
      
    5. 我在路由表中添加了信息:

      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
      

      感谢您帮助解决此问题

1 个答案:

答案 0 :(得分:0)

问题来自于功能nl_addr_build的错误使用。

替换为nl_addr_parse()并且正在运行。

nl_addr_build:解析二进制地址

nl_addr_parse:解析像“10.10.1.10”这样的char * addr