一个有趣的网络程序TraceRoute:记录数据包传送路径上的路由器IP

在大多数操作系统上都附带一个网络程序叫TraceRoute,它的作用是追踪数据包发送到指定对象前,在传送路径上经过了几个路由器转发,下图是用TraceRoute程序追踪从我这台主机发送数据包到百度服务器时所经过的各个路由器的ip:

屏幕快照 2019-02-21 下午5.38.48.png

其中14.215.177.38是域名www.baidu.com对应的服务器ip,从显示上看,数据包从我当前电脑发出,经过7个路由器后才能到达百度服务器,本节我们就看看traceroute应用程序的实现原理。

整个互联网其实是由一个个子网组成的,每个子网相当于一个孤岛,每个孤岛对应一个路由器,两个孤岛间的路由器如果相互连通,那么就相当于在孤岛上架起一座桥梁,于是两座孤岛就可以相互连通,整个互联网就是无数个孤岛通过路由器连接起来的一个巨大整体:

屏幕快照 2019-02-21 下午5.42.43.png

如上图当我们想把数据发送到远端服务器时,数据包从我们所在的“孤岛”通过路由器跳转到下一个孤岛,如果接收目标没有在进入的新孤岛,那么第二个孤岛的路由器会将数据包通过它的路由器提交到第三个孤岛,如此一直传递直到数据包抵达接收目标所在的孤岛,然后对应孤岛的路由器将数据包分发给接收目标。

在IP数据包头中有一个字段,用来记录数据包可以跳转的孤岛数量:
屏幕快照 2018-12-21 下午5.37.41.png

上面显示的是IP数据包头的格式,其中有一个字段是Time To Live,简称TTL,它规定了该数据包可以跳转的孤岛数量,数据包每跳转一个孤岛,该字段的值就减1,如果当该字段的值减到0数据包还没有抵达目标所在的孤岛,那么该孤岛对应的路由器就会向数据包的发送者发出一个由ICMP协议封装的数据包叫ICMP Time Exceeded Message,该数据包的格式如下:

屏幕快照 2019-02-21 下午5.50.39.png

其中type取值11,code取值为0.

traceroute就是利用这个特性来检测数据包发送路径上所经过的路由器。首先它构造一个UDP数据包发送给接收目标,并在数据包的IP报头里将TTL字段设置成1,于是数据包发送给第一个孤岛时,对方回发一个Time Exceeded消息,通过该消息的IP报头它就可以知道第一个孤岛路由器的ip,接着它再次构造同样的UDP数据包只是把其中IP报头的TTL字段改成2,于是同理获得第二个孤岛对应路由器的IP,如此反复进行,直到再也没有Time Exceeded消息返回为止,于是它便可以得知数据包发送给指定目标时,路径上经过了多少路由器转发。

我们看看如何动手实现traceroute程序功能,在这里我们需要使用一个新协议包头,也就是UDP包头,它的具体原理我们在以后的课程中会详细研究,现在我们只需要知道它的包头格式即可:

DDU ICMP controls consist of 3 controls : TDDUOSPING - A non blocking PING protocolTDDUOSTRACEROUTE - A non blocking TraceRoute ProtocolTDDUOSDNS - A non blocking ARP/RARP controlAbout TDDUOSDNS :Host names are resolved to IP addresses, and IP address can be resolved to host names. If you enter an IP into the ADDRESS property, the HOST property will be completed, and the OnHostLookup event will occur. If you enter an IP or a HOST name into the HOST property, the ADDRESS property will be completed, and the OnAddressLookup event will occure. The runtime property OK can be queried to see if the lookup was successful. To reduce lookup overhead, an IP/Hostname cache can be used by setting the UseIPCache property to true. The cache is only persistent for the current session, and maynot be saved between sessions. You can use FlushIPCache method to clear the cache.When combined with the other controls in this package, the delay normally associated with connecting a socket can be avoided. Each control must have a unique TDDUOSDNS control.About the Demo :The demo project shows how to use each control. The TDDUOSDNS control is shown both in combination with other controls, and used independantly. DNS not an ICMP protocol, but it is included to prevent the windows application from blocking.Blocking is when your windows program stops responding to messages, waiting for a call to return. Normally, all calls blocks for a very short time, and is not noticable - but when you have to wait for 5 or 10 seconds or even longer, the application can appear to be crashed. By using threads, callback messages, and events, I have illustrated how it is possible to avoid blocking in an application.These controls form a custom set of TCP/IP controls I have been developing over the past year. The ICMP controls were written over the period of a week, and I consulted several references for information. I would like to thank Quinn Shute for his excellent book "Windows Socket Network Programming&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值