对于读取路由列表,可以通过 /proc/net/route文件进行读取具体如何读取可以参考,busybox中的函数。
void FAST_FUNC bb_displayroutes(int noresolve, int netstatfmt);
对于设置,需要使用 struct rtentry配合ioctrl,可以参考busybox的函数。
static NOINLINE void INET_setroute(int action, char **args);
#include <sys/socket.h>
#include <linux/route.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#define SIOCADDRT 0x890B /* add routing table entry */
#define SIOCDELRT 0x890C /* delete routing table entry */
#define SIOCRTMSG 0x890D /* call to routing system */
struct rtentry {
unsigned long rt_pad1;
struct sockaddr rt_dst; /* target address */
struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
struct sockaddr rt_genmask; /* target network mask (IP) */
unsigned short rt_flags;
short rt_pad2;
unsigned long rt_pad3;
void *rt_pad4;
short rt_metric; /* +1 for binary compatibility! */
char *rt_dev; /* forcing the device at add */
unsigned long rt_mtu; /* per route MTU/Window */
#define rt_mss rt_mtu /* Compatibility :-( */
unsigned long rt_window; /* Window clamping */
unsigned short rt_irtt; /* Initial RTT */
};
http://www.cocoachina.com/articles/105156
https://stackoverflow.com/questions/17648440/specify-interface-when-adding-default-gateway-via-siocaddrt