Linux:如何从C设置默认路由? [英] Linux : how to set default route from C?

查看:286
本文介绍了Linux:如何从C设置默认路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从C程序设置(并替换现有的)默认网络路由?如果可能,我想在没有外壳命令的情况下执行此操作(这是内存不足的嵌入式系统).还可以在不指定网关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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆