網(wǎng)絡(luò)通信程序設(shè)計(jì)實(shí)驗(yàn)二路由跟蹤_第1頁
網(wǎng)絡(luò)通信程序設(shè)計(jì)實(shí)驗(yàn)二路由跟蹤_第2頁
網(wǎng)絡(luò)通信程序設(shè)計(jì)實(shí)驗(yàn)二路由跟蹤_第3頁
網(wǎng)絡(luò)通信程序設(shè)計(jì)實(shí)驗(yàn)二路由跟蹤_第4頁
網(wǎng)絡(luò)通信程序設(shè)計(jì)實(shí)驗(yàn)二路由跟蹤_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、 實(shí) 驗(yàn) 報(bào) 告 課程名稱 網(wǎng)絡(luò)通信程序設(shè)計(jì) 實(shí)驗(yàn)儀器 計(jì)算機(jī) 實(shí)驗(yàn)名稱 路由跟蹤 系 別 _計(jì)算機(jī)學(xué)院_ 專 業(yè) _ 班級/學(xué)號_ _學(xué)生姓名 實(shí)驗(yàn)日期 2014年4月6日 成 績 _ 指導(dǎo)教師 焦健 實(shí)驗(yàn)二 路由跟蹤實(shí)驗(yàn)一、實(shí)驗(yàn)?zāi)康?學(xué)習(xí)分析程序功能結(jié)構(gòu)。熟悉icmp協(xié)議的工作原理和路由跟蹤的原理。掌握vc6.0下程序調(diào)試、運(yùn)行的基本方法。二、實(shí)驗(yàn)原理路由跟蹤的實(shí)現(xiàn)就是巧妙地利用了icmp報(bào)文的ttl超時(shí)報(bào)文。其實(shí)現(xiàn)過程如下:源主機(jī)先向目的主機(jī)發(fā)送一個(gè)回應(yīng)請求報(bào)文(類型8),ttl值設(shè)為1,第一個(gè)路由器收到后將ttl減1,這樣ttl變?yōu)?,分組被廢除。同時(shí)路由器向源主機(jī)發(fā)送一個(gè)ttl超時(shí)報(bào)

2、文(類型為11),報(bào)文的ip包頭中的源ip地址就是第一個(gè)路由器的地址,源主機(jī)就可以通過對該報(bào)文進(jìn)行分析,得到第一個(gè)路由器的地址。接著發(fā)送ttl等于2的報(bào)文得到第二個(gè)路由器地址,再發(fā)ttl等于3的報(bào)文。如此下去直到收到目的主機(jī)的回應(yīng)應(yīng)答報(bào)文(類型為0)或目的不可達(dá)報(bào)文(類型為3),或者到了最大跳數(shù)(要檢測路由器個(gè)數(shù)的最大值)??梢钥吹剑瑢tl的設(shè)置是實(shí)現(xiàn)跟蹤的關(guān)鍵,使用函數(shù)setsockopt(m_sock, ipproto_ip, ip_ttl,(lpstr)&ttl,sizeof(int) 可以對其進(jìn)行設(shè)置,m_sock是所創(chuàng)建的套接字,ip_ttl說明是進(jìn)行ttl設(shè)置,ttl即是

3、要設(shè)置的ttl值,為一個(gè)整形數(shù)值 。其實(shí)現(xiàn)流程如圖1所示:圖 1路由跟蹤流程圖三、實(shí)驗(yàn)內(nèi)容 1、按照附錄內(nèi)容給routetrace程序添加代碼,增加注釋,調(diào)試程序通過。源代碼:routetrace.cpp/ routetrace.cpp : defines the class behaviors for the application.#include "stdafx.h"#include "routetrace.h"#include "routetracedlg.h"#ifdef _debug#define new debug_ne

4、w#undef this_filestatic char this_file = _file_;#endif/ croutetraceappbegin_message_map(croutetraceapp, cwinapp)/afx_msg_map(croutetraceapp)/ note - the classwizard will add and remove mapping macros here./ do not edit what you see in these blocks of generated code!/afx_msgon_command(id_help, cwinap

5、p:onhelp)end_message_map()/ croutetraceapp constructioncroutetraceapp:croutetraceapp()/ todo: add construction code here,/ place all significant initialization in initinstance/ the one and only croutetraceapp objectcroutetraceapp theapp;/ croutetraceapp initializationbool croutetraceapp:initinstance

6、()afxenablecontrolcontainer();/ standard initialization/ if you are not using these features and wish to reduce the size/ of your final executable, you should remove from the following/ the specific initialization routines you do not need.#ifdef _afxdllenable3dcontrols();/ call this when using mfc i

7、n a shared dll#elseenable3dcontrolsstatic();/ call this when linking to mfc statically#endifcroutetracedlg dlg;m_pmainwnd = &dlg;int nresponse = dlg.domodal();if (nresponse = idok) / todo: place code here to handle when the dialog is/ dismissed with okelse if (nresponse = idcancel)/ todo: place

8、code here to handle when the dialog is/ dismissed with cancel/ since the dialog has been closed, return false so that we exit the/ application, rather than start the application's message pump.return false;源代碼:icmp.cpp / icmp.cpp: implementation of the cicmp class.#include "stdafx.h"#i

9、nclude "routetrace.h"#include "icmp.h"#include "ws2tcpip.h"#ifdef _debug#undef this_filestatic char this_file=_file_;#define new debug_new#endif/ construction/destruction/cicmp:cicmp()winsock = 0;m_pip = null;m_picmp = null;m_pip = (ip_head *)new bytemax_packet;m_picmp

10、= (icmp_head *)new bytemax_packet;cicmp:cicmp()delete m_pip;delete m_picmp;bool cicmp:initialize()wsadata wsadata;if( wsastartup(makeword(2, 1),&wsadata) ) afxmessagebox("wsastartup初始化失敗!");return false;winsock= wsasocket (af_inet, /建立socket sock_raw, ipproto_icmp, null, 0,0);if(!winso

11、ck)afxmessagebox( "socket創(chuàng)建失敗!");return false;int timeout =5000;setsockopt(winsock,sol_socket,so_rcvtimeo,(char *)&timeout, /設(shè)置接收超時(shí)sizeof(timeout);timeout = 5000;setsockopt(winsock,sol_socket,so_sndtimeo,(char *)&timeout, /設(shè)置發(fā)送超時(shí)sizeof(timeout);return true;void cicmp:uninitialize()

12、 /釋放socketif(winsock)closesocket(winsock);wsacleanup();ushort cicmp:checksum(ushort *buffer, int size) /計(jì)算校驗(yàn)和 unsigned long cksum = 0; while(size > 1) cksum+=*buffer+;size -=sizeof(ushort); if(size ) cksum += *(uchar*)buffer; cksum = (cksum >> 16) + (cksum & 0xffff); cksum += (cksum >

13、;>16); return (ushort)(cksum);bool cicmp:sendicmppack(char *paddr)sockaddr_in sockaddr;memset(void *)&sockaddr,0,sizeof(sockaddr);sockaddr.sin_family = af_inet;sockaddr.sin_port = 0;sockaddr.sin_addr.s_un.s_addr=inet_addr(paddr);return sendicmppack(&sockaddr);/-設(shè)置ttl-int cicmp:setttl(int

14、ttl)int nret=setsockopt(winsock, ipproto_ip, ip_ttl,(lpstr)&ttl,sizeof(int); if(nret=socket_error) cstring ttlerr;ttlerr.format("設(shè)置 ttl 錯(cuò)誤!");afxmessagebox(ttlerr);return 0;return 1;/-發(fā)送-bool cicmp:sendicmppack(sockaddr_in *paddr)/填充icmp數(shù)據(jù)各項(xiàng)int state;char *p_data;m_picmp->type = icm

15、p_echo;m_picmp->code = 0;m_picmp->id = (ushort)getcurrentprocessid();m_picmp->number = 0;m_picmp->time = gettickcount();m_picmp->cksum = 0;/填充數(shù)據(jù)p_data = (char *)m_picmp + sizeof(icmp_head);memset(char *)p_data,'0',def_packet);/檢查和m_picmp->cksum = checksum(ushort *)m_picmp,d

16、ef_packet+sizeof(icmp_head);/發(fā)送數(shù)據(jù)state = sendto(winsock,(char *)m_picmp,def_packet+sizeof(icmp_head),null,(struct sockaddr *)paddr,sizeof(sockaddr);if(state = socket_error) if(getlasterror()=wsaetimedout)m_strinfo = "連接超時(shí)!(發(fā)送)"elsem_strinfo="出現(xiàn)未知發(fā)送錯(cuò)誤!"return false;if(state <de

17、f_packet) m_strinfo = "發(fā)送數(shù)據(jù)錯(cuò)誤!"return false;memcpy(void *)&m_sockaddr,(void *)paddr,sizeof(sockaddr_in);return true;/-接收數(shù)據(jù)-bool cicmp:recvicmppack()int state;int len = sizeof(sockaddr_in);char * addr; struct hostent *lphostent = null; int maxfd = 1; / 監(jiān)視的最大的文件描述符值+1 fd_set readfdset; /

18、 設(shè)置文件描述符 struct timeval timeout; addr = inet_ntoa(m_sockaddr.sin_addr); fd_zero(&readfdset); fd_set(winsock, &readfdset); maxfd = max(maxfd, winsock) + 1; timeout.tv_sec = 10; / 設(shè)置響應(yīng)時(shí)間限制 timeout.tv_usec = 0; int e = :select(maxfd, &readfdset, null, null, &timeout); /獲取多路復(fù)用套接字的響應(yīng)結(jié)果 if

19、(e <= 0) routeaddr="*"routestate=1;routestate="超時(shí)未知" return false; if(!fd_isset(winsock, &readfdset) return false;state=recvfrom(winsock,(char*)m_pip,max_packet,0,(struct sockaddr*)&m_sockaddr,&len);if (state = socket_error) if (wsagetlasterror() = wsaetimedout)m_s

20、trinfo.format("接收超時(shí),路由跟蹤失敗!");routestate=0;routestate="路由跟蹤失敗!"elsem_strinfo = "未知接收錯(cuò)誤!"return false;/分析數(shù)據(jù)int ipheadlen;ipheadlen = m_pip->headlen * 4 ;if (state < (ipheadlen+min_packet)m_strinfo = "目的地址的響應(yīng)數(shù)據(jù)不正確"return false;icmp_head * p_icmprev;p_icmprev = (icmp_head*)(char *)m_pip + ipheadlen); switch (p_icmprev->type)case icmp_echoreply: /收到正?;仫@m_strinfo.format("接收到%s %d字節(jié)響應(yīng)數(shù)據(jù),響應(yīng)時(shí)間:%dms.",inet_ntoa(m_sockaddr.sin_addr),len,gettickcount()-p_icmprev->time);routeaddr=addr;routestate=0; routestate="到達(dá)目的主機(jī)!" return tr

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論