Linux:如何从 C 设置默认路由? [英] Linux : how to set default route from C?
问题描述
如何从 C 程序设置(并替换现有的)默认网络路由?如果可能的话,我想在没有 shell 命令的情况下进行(这是一个低内存嵌入式系统).也可以不指定网关IP地址就设置默认路由吗?在我的应用程序中,我想将 ppp0 或 eth0 设为默认路由,具体取决于电缆是否插入 eth0.
How can I set (and replace the existing) default network route from a C program? I'd like to do it without shell commands if possible (this is a low memory embedded system). Also can you set the default route without specifying the gateway IP address? In my application I want to make either ppp0 or eth0 the default route, depending on whether the cable is plugged into eth0 or not.
谢谢,弗雷德
推荐答案
你可以strace
你想要模仿的route
命令.这为您提供了对更改路由有用的相关系统调用.
You could strace
the route
command you are wanting to mimic. This gives you the relevant syscalls useful to change routing.
您可能对 proc(5) 感兴趣界面,例如它的 /proc/net/route
伪文件.
You may be interested by the proc(5) interface, e.g. its /proc/net/route
pseudo-file.
另见ip(7).
这篇关于Linux:如何从 C 设置默认路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!