C++操作Linux路由表哦.docx

上传人:聆听****声音 文档编号:18941488 上传时间:2024-03-23 格式:DOCX 页数:6 大小:43.12KB
下载 相关 举报
C++操作Linux路由表哦.docx_第1页
第1页 / 共6页
C++操作Linux路由表哦.docx_第2页
第2页 / 共6页
C++操作Linux路由表哦.docx_第3页
第3页 / 共6页
C++操作Linux路由表哦.docx_第4页
第4页 / 共6页
C++操作Linux路由表哦.docx_第5页
第5页 / 共6页
C++操作Linux路由表哦.docx_第6页
第6页 / 共6页
亲,该文档总共6页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C++操作Linux路由表哦.docx

《C++操作Linux路由表哦.docx》由会员分享,可在线阅读,更多相关《C++操作Linux路由表哦.docx(6页珍藏版)》请在冰点文库上搜索。

C++操作Linux路由表哦.docx

你也可以查看/proc/net/route文件。

添加NullRoutes

Nullroutes简单的忽略和它相匹配的包。

使用它可以有效的屏蔽某个有问题的IP。

例如:

$sudorouteadd123.123.123.123reject

现在你不能和IP(123.123.123.123) 发送、接收数据包。

移除NullRoutes

$routedel123.123.123.123reject

在操作路由表时要格外小心;如果你不小心移除了和SSH相关的远程主机路由,你只能通过访问物理主机解决。

使用C/C++更改路由表

要使用程序添加nullroute,你需要使用ioctl,控制参数SIOCADDRT:

#include

#include

#include

#include

booladdNullRoute(longhost)

{

//createthecontrolsocket.

intfd=socket(PF_INET,SOCK_DGRAM,IPPROTO_IP);

structrtentryroute;

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

//setthegatewayto0.

structsockaddr_in*addr=(structsockaddr_in*)&route.rt_gateway;

addr->sin_family=AF_INET;

addr->sin_addr.s_addr=0;

//setthehostwearerejecting.

addr=(structsockaddr_in*)&route.rt_dst;

addr->sin_family=AF_INET;

addr->sin_addr.s_addr=htonl(host);

//Setthemask.Inthiscaseweareusing255.255.255.255,toblockasingle

//IP.ButyoucouldusealessrestrictivemasktoblockarangeofIPs.

//ToblockandentireCblockyouwoulduse255.255.255.0,or0x00FFFFFFF

addr=(structsockaddr_in*)&route.rt_genmask;

addr->sin_family=AF_INET;

addr->sin_addr.s_addr=0xFFFFFFFF;

//Theseflagsmean:

thisrouteiscreated"up",oractive

//Theblockedentityisa"host"asopposedtoa"gateway"

//Thepacketsshouldberejected.OnBSDthereisaflagRTF_BLACKHOLE

//thatcausespacketstobedroppedsilently.WewouldusethatifLinux

//hadit.RTF_REJECTwillcausethenetworkinterfacetosignalthatthe

//packetsarebeingactivelyrejected.

route.rt_flags=RTF_UP|RTF_HOST|RTF_REJECT;

route.rt_metric=0;

//thisiswherethemagichappens..

if(ioctl(fd,SIOCADDRT,&route))

{

close(fd);

returnfalse;

}

//remembertoclosethesocketlestyouleakhandles.

close(fd);

returntrue;

}

注意:

重启系统更改失效。

上面代码的反操作,移除route:

booldelNullRoute(longhost)

{

intfd=socket(PF_INET,SOCK_DGRAM,IPPROTO_IP);

structrtentryroute;

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

structsockaddr_in*addr=(structsockaddr_in*)&route.rt_gateway;

addr->sin_family=AF_INET;

addr->sin_addr.s_addr=0;

addr=(structsockaddr_in*)&route.rt_dst;

addr->sin_family=AF_INET;

addr->sin_addr.s_addr=htonl(host);

addr=(structsockaddr_in*)&route.rt_genmask;

addr->sin_family=AF_INET;

addr->sin_addr.s_addr=0xFFFFFFFF;

route.rt_flags=RTF_UP|RTF_HOST|RTF_REJECT;

route.rt_metric=0;

//thistimewearedeletingtheroute:

if(ioctl(fd,SIOCDELRT,&route))

{

close(fd);

returnfalse;

}

close(fd);

returntrue;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2