IT评测·应用市场-qidao123.com

标题: VC++ 获取目的IP的路由 [打印本页]

作者: 郭卫东    时间: 2025-3-8 11:07
标题: VC++ 获取目的IP的路由
GetBestRoute 函数获取到目的IP的最佳匹配路由。
第一个参数为:destination(目的IP)
第二个参数为:source(源IP)
通常不需要指定第二个source,这个一般用来匹配具体某一个网卡接口路由的,即source等于本机某个网卡的接口IP或网关。

GetBestInterface 函数是获取到目的IP的最佳网卡接口IFR_INDEX。
  1.             bool Router::GetBestRoute(uint32_t destination, uint32_t source, MIB_IPFORWARDROW& route) noexcept
  2.             {
  3.                 int err = ::GetBestRoute(destination, source, &route);
  4.                 return err == NO_ERROR;
  5.             }
  6.             bool Router::GetBestRoute(uint32_t destination, MIB_IPFORWARDROW& route) noexcept
  7.             {
  8.                 return GetBestRoute(destination, 0, route);
  9.             }
  10.             int Router::GetBestInterface(uint32_t ip) noexcept
  11.             {
  12.                 DWORD dwBestIfIndex = 0;
  13.                 int err = ::GetBestInterface(ip, &dwBestIfIndex);
  14.                 if (err != NO_ERROR)
  15.                 {
  16.                     return -1;
  17.                 }
  18.                 return dwBestIfIndex;
  19.             }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4