FWR-610P 添加和删除一条路由规则源代码

项目: FWR-610P

添加和删除一条路由规则源代码

 

route_add("lo", 0,"127.0.0.0", "0.0.0.0", "255.0.0.0");

 

route_add(char *name, int metric, char *dst, char *gateway,char *genmask)

{

         returnroute_manip(SIOCADDRT, name, metric, dst,gateway, genmask);

}

 

int

route_del(char *name, int metric,char *dst, char *gateway, char *genmask)

{

         returnroute_manip(SIOCDELRT, name, metric, dst,gateway, genmask);

}

 

route_manip(int cmd, char *name, int metric, char *dst, char*gateway, char *genmask)

{

         ints;

         struct rtentryrt;

 

         /*Open a raw socket to the kernel */

         if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)

                   gotoerr;

 

         /*Fill in rtentry */

         memset(&rt,0, sizeof(rt));

         if(dst)

                   inet_aton(dst,&sin_addr(&rt.rt_dst));

         if(gateway)

                   inet_aton(gateway,&sin_addr(&rt.rt_gateway));

         if(genmask)

                   inet_aton(genmask,&sin_addr(&rt.rt_genmask));

         rt.rt_metric= metric;

         rt.rt_flags= RTF_UP;

         if(sin_addr(&rt.rt_gateway).s_addr)

                   rt.rt_flags|= RTF_GATEWAY;

         if(sin_addr(&rt.rt_genmask).s_addr == INADDR_BROADCAST)

                   rt.rt_flags|= RTF_HOST;

         rt.rt_dev= name;

 

         /*Force address family to AF_INET */

         rt.rt_dst.sa_family= AF_INET;

         rt.rt_gateway.sa_family= AF_INET;

         rt.rt_genmask.sa_family= AF_INET;

                  

         if(ioctl(s,cmd, &rt) < 0)

                   gotoerr;

 

         close(s);

         return0;

 

 err:

         close(s);

         perror(name);

         returnerrno;

}

 

 

struct rtentry

{

         unsignedlong  rt_pad1;

         structsockaddr        rt_dst;               /* target address              */

         structsockaddr        rt_gateway;     /* gateway addr (RTF_GATEWAY)   */

         structsockaddr        rt_genmask;    /* target network mask (IP)    */

         unsignedshort         rt_flags;

         short                  rt_pad2;

         unsignedlong  rt_pad3;

         void           *rt_pad4;

         short                  rt_metric;         /* +1 for binary compatibility!          */

         char__user      *rt_dev;   /* forcing the device at add     */

         unsignedlong  rt_mtu;             /* per route MTU/Window     */

#ifndef __KERNEL__

#define rt_mss         rt_mtu                        /*Compatibility :-(            */

#endif

         unsignedlong  rt_window;      /* Window clamping                */

         unsignedshort         rt_irtt;      /* Initial RTT                       */

};

 

1.      /* Routing table calls. */ // 路由操作命令

#define SIOCADDRT        0x890B              /*add routing table entry        */

#define SIOCDELRT       0x890C              /* delete routing table entry   */

#define SIOCRTMSG         0x890D              /*call to routing system */

 

2.       

#define sin_addr(s) (((struct sockaddr_in*)(s))->sin_addr)

 

3.       

#ifndef RTF_UP

/* Keep this in sync with/usr/src/linux/include/linux/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  */

#ifndef RTF_MSS

#define RTF_MSS         RTF_MTU   /* Compatibility :-(           */

#endif

#define RTF_WINDOW      0x0080    /*per route window clamping    */

#define RTF_IRTT        0x0100         /*Initial round trip time      */

#define RTF_REJECT      0x0200         /*Reject route                 */

#endif

 

 

ioctl函数,后续分析

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值