Chinaunix首页 | 论坛 | 博客
  • 博客访问: 120765
  • 博文数量: 7
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-05 12:45
文章分类
文章存档

2008年(7)

我的朋友

分类: LINUX

2008-05-14 14:21:42

linux里面可以从/proc/net/route文件中得到系统的路由信息,以前写过一个程序查找路由表就是通过读这个文件来获得的,不过当时没有考虑每条路由的flags,这里就留下一下bug.其每条路由定义如下(route.h):

#define    RTF_UP        0x0001      /* route usable                */
#define    RTF_GATEWAY    0x0002     /* destination is a gateway    */
#define    RTF_HOST    0x0004        /* host entry (net otherwise)  */
#define    RTF_REINSTATE    0x0008   /* reinstate route after tmout */
#define    RTF_DYNAMIC    0x0010     /* created dyn. (by redirect)  */
#define    RTF_MODIFIED    0x0020    /* modified dyn. (by redirect) */
#define    RTF_MTU        0x0040     /* specific MTU for this route */
#define    RTF_MSS        RTF_MTU    /* Compatibility :-(           */
#define    RTF_WINDOW    0x0080      /* per route window clamping   */
#define    RTF_IRTT    0x0100        /* Initial round trip time     */
#define    RTF_REJECT    0x0200      /* Reject route                */


现在发现,即使你用route命令去删除一条路由,这条路由仍然存在于/proc/net/route文件中,只是系统把这个路由的flags中的RTF_UP去除了,也就是表明其不可用的,所以以前写的程序是有BUG的。
by the way, 可以通过ioctl函数对系统路由表进行操作,对request参数可以为SIOCADDRT(增加路由)和SIOCDELRT(删除路径),而其实际的路由表示为结构struct rtentry,其定义如下(route.h):

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     */
#ifndef __KERNEL__
#define rt_mss    rt_mtu            /* Compatibility :-( */
#endif
    unsigned long    rt_window;    /* Window clamping         */
    unsigned short    rt_irtt;    /* Initial RTT            */
};

阅读(2614) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~