rbeede
rbeede

Reputation: 303

Portable way in c++ to get the routing table

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.

Upvotes: 1

Views: 3928

Answers (2)

Juan
Juan

Reputation: 1371

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

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

Upvotes: 0

Wes Hardaker
Wes Hardaker

Reputation: 22262

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.

Upvotes: 5

Related Questions