gpt4 book ai didi

c++ - 使用 libnl-3-route "Invalid input data or parameter"添加路由

转载 作者:行者123 更新时间:2023-11-30 17:37:57 33 4
gpt4 key购买 nike

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

  1. Netlink library
  2. Netlink routing

但是路由章节是空的...;)

问题是内核响应“无效的输入数据或参数”。我已经使用 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;

我添加我的路由表以获取信息:

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

感谢您帮助解决此问题

最佳答案

问题来自于函数nl_addr_build的错误使用。

替换为 nl_addr_parse() 即可。

nl_addr_build:解析二进制地址

nl_addr_parse :解析 char* addr,如“10.10.1.10”

关于c++ - 使用 libnl-3-route "Invalid input data or parameter"添加路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22244614/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com