can I read/write the routing table in C without using system() command?

白昼怎懂夜的黑 提交于 2019-12-08 17:39:53

问题


I have code written in C. I want to get using C code information that is stored in routing table. Is it possible?


回答1:


You can open a netlink socket and send route update messages. There is an article about how to do this.




回答2:


You can also run "strace route add ..." to see how the route command does it.

On my system, it uses ioctl with SIOCADDRT. A little searching turns up some sample code.

Oddly, the best documentation I have found is from IBM's AS400 man pages.

If you just want to read the routing table, you can open and read /proc/net/route. (Again, strace shows that this is how the route command does it.) The hex numbers are 32-bit IP addresses in machine-endian form. Try cat /proc/net/route.




回答3:


If you want to query the routing table only, try to read the file /proc/net/route. It works in my system.



来源:https://stackoverflow.com/questions/6358431/can-i-read-write-the-routing-table-in-c-without-using-system-command

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