Portable way in c++ to get the routing table

我们两清 提交于 2019-12-11 04:33:09

问题


I need to get the routing table for a machine in a portable fashion. I'm using C++ and the Boost library is already available if that helps.

No calling external commands like "route" because that isn't portable and the external command may not be available.


回答1:


Certainly calling route isn't advisable as forking and executing is rarely the way.

Unfortunately, I can tell you [as the founder of the Net-SNMP project, where we report routes for many many platforms in C], there is no platform independent way. You'll need to learn how each one stores its information and how it's accessed. EG in linux, you can get it from /proc. In other OSes you can get it through API calls. You're not likely to be happy to find out that what you want doesn't exist. And then, you'll even find some platforms that may differ between IPv4 and IPv6!

You are, however, freely available to go dig down into the Net-SNMP code and look for all the ways it's done there. See agent/mibgroup/ip-forward-mib/data_access/ and agent/mibgroup/mibII/var_route.c for starters.




回答2:


You could look into a third party portability library, like libdnet (BSD license).

https://code.google.com/p/libdnet/



来源:https://stackoverflow.com/questions/9486786/portable-way-in-c-to-get-the-routing-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!