本文来源http://blog.csdn.net/gt945/article/details/45315911
其实网上已经有类似代码,但是还是自己重新写了一遍,实践出真知
/*
* NetMonitor.c
*
* Created on: 2015年4月25日
* Author: tao
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <asm/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/route.h>
#define BUFLEN 20480
#define t_assert(x) { \
if(!(x)) {err = -__LINE__;goto error;} \
}
/*Ctrl + C 退出*/
static volatile int keepRunning = 1;
void intHandler(int dummy)
{
keepRunning = 0;
}
/**
* 解析RTA,并存入tb
*/
void parse_rtattr(struct rtattr **tb, int max, struct rtattr *