方法一、读取/proc/net/route文件
用sscanf函数将读取内容按格式存储在不同变量中。
route_loop(route_t *r, route_handler callback, void *arg)
{
FILE *fp;
struct route_entry entry;
char buf[BUFSIZ];
int ret = 0;
if ((fp = fopen(PROC_ROUTE_FILE, "r")) != NULL) {
char ifbuf[16];
int i, iflags, refcnt, use, metric, mss, win, irtt;
uint32_t mask;
while (fgets(buf, sizeof(buf), fp) != NULL) {
i = sscanf(buf, "%16s %X %X %X %d %d %d %X %d %d %d\n",
ifbuf, &entry.route_dst.addr_ip,
&entry.route_gw.addr_ip, &iflags, &refcnt, &use,
&metric, &mask, &mss, &win, &irtt);//获取路由表各项信息
if (i < 10 || !(iflags & RTF_UP))
continue;
entry.route_dst.addr_type = entry.route_gw.addr_type =
ADDR_TYPE_IP;
if (addr_mtob(&mask, IP_ADDR_LEN,
&entry.route_dst.addr_bits) < 0)//对mask的每一位进行是否为零的判断,IP_ADDR_LEN =4