計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第1頁(yè)
計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第2頁(yè)
計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第3頁(yè)
計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第4頁(yè)
計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第5頁(yè)
已閱讀5頁(yè),還剩33頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)實(shí)驗(yàn)報(bào)告專心-專注-專業(yè)計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)實(shí)驗(yàn)報(bào)告一、 實(shí)驗(yàn)內(nèi)容和要求1、實(shí)驗(yàn)一 數(shù)據(jù)包的捕獲與分析Wireshark是一種開源的網(wǎng)絡(luò)數(shù)據(jù)包的捕獲和分析軟件,本實(shí)驗(yàn)經(jīng)過Wireshark軟件的安裝使用,監(jiān)控局域網(wǎng)的狀態(tài),捕獲在局域網(wǎng)中傳輸?shù)臄?shù)據(jù)包,并結(jié)合在計(jì)算機(jī)網(wǎng)絡(luò)課程中學(xué)習(xí)到的理論知識(shí),對(duì)常見網(wǎng)絡(luò)協(xié)議的數(shù)據(jù)包做出分析,加深網(wǎng)絡(luò)課程知識(shí)的理解和掌握。具體內(nèi)容及要求如下:l Wireshark軟件的安裝;l Wireshark軟件的啟動(dòng),并設(shè)置網(wǎng)卡的狀態(tài)為混雜狀態(tài),使得Wireshark能夠監(jiān)控局域網(wǎng)的狀態(tài);l 啟動(dòng)數(shù)據(jù)包的捕獲,跟蹤PC之間的報(bào)文,并

2、存入文件以備重新查;l 設(shè)置過濾器過濾網(wǎng)絡(luò)報(bào)文以檢測(cè)特定數(shù)據(jù)流;l 對(duì)常見協(xié)議的數(shù)據(jù)包的報(bào)文格式進(jìn)行分析,利用協(xié)議分析軟件的統(tǒng)計(jì)工具顯示網(wǎng)絡(luò)報(bào)文的各種統(tǒng)計(jì)信息。2、實(shí)驗(yàn)二 網(wǎng)絡(luò)層實(shí)驗(yàn)Ping程序的設(shè)計(jì)與實(shí)現(xiàn)實(shí)驗(yàn)?zāi)康?本實(shí)驗(yàn)?zāi)康氖鞘箤W(xué)生掌握網(wǎng)絡(luò)層協(xié)議的原理及實(shí)現(xiàn)方法。 實(shí)驗(yàn)設(shè)計(jì)內(nèi)容本實(shí)驗(yàn)為ICMP實(shí)驗(yàn)。實(shí)驗(yàn)內(nèi)容:Ping命令實(shí)現(xiàn)的擴(kuò)充,在給定的Ping程序的基礎(chǔ)上做如下功能擴(kuò)充:-h顯示幫助信息-b允許ping一個(gè)廣播地址,只用于IPv4-t 設(shè)置ttl值,只用于IPv4-q安靜模式。不顯示每個(gè)收到的包的分析結(jié)果,只在結(jié)束時(shí),顯示匯總結(jié)果Ping命令的基本描述Ping的操作是向某些IP地址發(fā)送一

3、個(gè)ICMP Echo消息,接著該節(jié)點(diǎn)返回一個(gè)ICMP Echo replay消息。ICMP消息使用IP頭作為基本控制。二、實(shí)驗(yàn)環(huán)境實(shí)驗(yàn)一 數(shù)據(jù)包的捕獲與分析1.聯(lián)網(wǎng)計(jì)算機(jī)2.Windows 或linux 系統(tǒng)3.在P中安裝協(xié)議分析軟件(如:Wireshark)4.物理基礎(chǔ):IEEE802.3 標(biāo)準(zhǔn)的以太網(wǎng)采用的是持續(xù) CSMA 的方式,正是由于以太網(wǎng)采用這種廣播信道爭(zhēng)用的方式,使得各個(gè)站點(diǎn)能夠獲得其它站點(diǎn)發(fā)送的數(shù)據(jù)。運(yùn)用這一原理使信息捕獲系統(tǒng)能夠攔截的我們所要的信5.工作模式:1) 廣播模式(Broad Cast Model):它的物理地址(MAC)地址是 0Xffffff 的幀為廣播幀,工作

4、在廣播模式的網(wǎng)卡接收廣播幀。2) 多播傳送(MultiCast Model):多播傳送地址作為目的物理地址的幀能夠被組內(nèi)的其它主機(jī)同時(shí)接收,而組外主機(jī)卻接收不到??墒牵绻麑⒕W(wǎng)卡設(shè)置為 多播傳送模式,它能夠接收所有的多播傳送幀,而不論它是不是組內(nèi)成員。3) 直接模式(Direct Model):工作在直接模式下的網(wǎng)卡只接收目地址是自己 MAC地址的幀。4) 混雜模式(Promiscuous Model):工作在混雜模式下的網(wǎng)卡接收所有的流過網(wǎng)卡的幀,信包捕獲程序就是在這種模式下運(yùn)行的。實(shí)驗(yàn)二 網(wǎng)絡(luò)層實(shí)驗(yàn)Ping程序的設(shè)計(jì)與實(shí)現(xiàn)1. 聯(lián)網(wǎng)計(jì)算機(jī)2. Linux系統(tǒng)3. 系統(tǒng)自帶編譯環(huán)境三、 程序

5、的需求分析與邏輯框圖需求分析1、實(shí)驗(yàn)一數(shù)據(jù)包的捕獲與分析1>.在P中安裝協(xié)議分析軟件。2>.啟動(dòng)Wireshark協(xié)議分析軟件,選擇抓包菜單項(xiàng)啟動(dòng)實(shí)時(shí)監(jiān)視器,開始實(shí)時(shí)跟蹤顯示網(wǎng)絡(luò)數(shù)據(jù)報(bào)文。可根據(jù)系統(tǒng)提示修改顯示方式。3>.調(diào)出跟蹤存儲(chǔ)的歷史報(bào)文,選擇有代表性的ETHERNET,IEEE802.3,IP,ICMP,TCP,UDP報(bào)文,對(duì)照有關(guān)協(xié)議逐個(gè)分析報(bào)文各字段的含義及內(nèi)容。4>.設(shè)置過濾器屬性,如目的地址,源地址,協(xié)議類型等。如過濾不需要的網(wǎng)絡(luò)報(bào)文,過濾器允許設(shè)置第二層,第三層或第四層的協(xié)議字段。2、實(shí)驗(yàn)二網(wǎng)絡(luò)層實(shí)驗(yàn)Ping程序的設(shè)計(jì)與實(shí)現(xiàn)PING程序是我們使用的比較

6、多的用于測(cè)試網(wǎng)絡(luò)連通性的程序。PING程序給予ICMP使用ICMP的回送請(qǐng)求和回送應(yīng)答來(lái)工作。ICMP是基于IP的一個(gè)協(xié)議,ICMP包經(jīng)過IP的封裝之后傳遞。實(shí)現(xiàn)檢測(cè)網(wǎng)絡(luò)通暢及速度的ping,并擴(kuò)展以下功能:-h 顯示幫助信息-b 允許ping一個(gè)廣播地址,只用于IPv4-t 設(shè)置ttl值,只用于IPv4-q 安靜模式,不顯示每個(gè)收到的包的分析結(jié)果,只在結(jié)束時(shí),顯示匯總結(jié)果三、程序的需求分析與邏輯框圖邏輯框圖1、 總體設(shè)計(jì)程序分為兩大部分:一部分讀取收到的所有消息,并輸出ICMP Echo replay消息,另一部分每個(gè)一秒鐘發(fā)送一個(gè)Echo消息。另一部分由SIGALARM信號(hào)每秒驅(qū)動(dòng)一次。2

7、、 詳細(xì)設(shè)計(jì)1)main函數(shù)三、程序的需求分析與邏輯框圖邏輯框圖2)readloop函數(shù) 3)proc函數(shù) 4)send函數(shù) 三、程序的需求分析與邏輯框圖邏輯框圖Checksum開始定義初始化cksum (size > 1)確定cksum及size大小是if (size)計(jì)算校驗(yàn)cksum,獲得結(jié)果cksum += *(UCHAR*)buffer;否結(jié)束四、程序核心功能的實(shí)現(xiàn)機(jī)制1、實(shí)驗(yàn)一 利用協(xié)議分析軟件跟蹤局域網(wǎng)報(bào)文,實(shí)驗(yàn)內(nèi)容如下:將安裝協(xié)議分析軟件的PC接入以太網(wǎng)中,跟蹤PC之間的報(bào)文,并存入文件以備重新查。設(shè)置過濾器過濾網(wǎng)絡(luò)報(bào)文以檢測(cè)特定數(shù)據(jù)流。利用協(xié)議分析軟件的統(tǒng)計(jì)工具顯示網(wǎng)絡(luò)

8、報(bào)文的各種統(tǒng)計(jì)信息。2、實(shí)驗(yàn)二Ping命令的基本描述Ping的操作是向某些IP地址發(fā)送一個(gè)ICMP Echo消息,接著該節(jié)點(diǎn)返回一個(gè)ICMP Echo replay消息。ICMP消息使用IP頭作為基本控制。IP頭的格式如下0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |Version| IHL |Type of Service| Total Length | +-+-

9、+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Identification |Flags| Fragment Offset| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Time to Live | Protocol | Header Checksum| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source Address |

10、+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Destination Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Version=4 IHL Internet頭長(zhǎng) Type of Service = 0 Total Length IP包的總長(zhǎng)度 Identification, Flags, Fragment Offset 用于IP包分段 Time to Live IP包的存活時(shí)長(zhǎng) Protocol

11、ICMP = 1 Addresses 發(fā)送Echo消息的源地址是發(fā)送Echo reply消息的目的地址,相反,發(fā)送Echo 消息的目的地址是發(fā)送Echo reply消息的源地址。四、 程序核心功能的實(shí)現(xiàn)機(jī)制2、實(shí)驗(yàn)二Ping實(shí)際上是使用ICMP中的ECHO報(bào)文來(lái)實(shí)現(xiàn)的。Echo 或 Echo Reply 消息格式如下: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |

12、 Type | Code | Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Identifier | Sequence Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Data . +-+-+-+-+-Typeecho消息的類型為8 echo reply 的消息類型為0。Code=0Checksum為從TYPE開始到IP包結(jié)束的校驗(yàn)和Identifier 如果 code = 0,

13、 identifier用來(lái)匹配echo和echo reply消息Sequence Number 如果 code = 0, identifier用來(lái)匹配echo和echo reply消息功能描述: 收到echo 消息必須回應(yīng) echo reply 消息。 identifier 和 sequence number 可能被發(fā)送echo的主機(jī)用來(lái)匹配返回的 echo reply消息。例如: identifier 可能用于類似于TCP或UDP的 port 用來(lái)標(biāo)示一個(gè)會(huì)話, 而sequence number 會(huì)在每次發(fā)送echo請(qǐng)求后遞增。收到echo的主機(jī)或路由器返回同一個(gè)值與之匹配四、程序核心功能的

14、實(shí)現(xiàn)機(jī)制2、實(shí)驗(yàn)二1、數(shù)據(jù)結(jié)構(gòu)的描述1) IP包格式struct ip BYTE Ver_ihl; /版本號(hào)與包頭長(zhǎng)度BYTE TOS; /服務(wù)類型WORD Leng; /IP包長(zhǎng)度WORD Id; /IP包標(biāo)示,用于輔助IP包的拆裝,本實(shí)驗(yàn)不用,置零WORD Flg_offset; /偏移量,也是用于IP包的拆裝,本實(shí)驗(yàn)不用,置零BYTE TTL; /IP包的存活時(shí)間BYTE Protocol; /上一層協(xié)議,本實(shí)驗(yàn)置ICMPWORD Checksum; /包頭校驗(yàn)和,最初置零,等所有包頭都填寫正確后,計(jì)算并替換。BYTE Saddr4; /源端IP地址BYTE Daddr4; /目的端IP

15、地址BYTE Data1; /IP包數(shù)據(jù);2)ICMP包格式struct icmp BYTE Type; /ICMP類型,本實(shí)驗(yàn)用 8: ECHO 0:ECHO REPLYBYTE Code; /本實(shí)驗(yàn)置零WORD Checksum; /ICMP包校驗(yàn)和,從TYPE開始,直到最后一位用戶數(shù)據(jù),如果為字節(jié)數(shù)為奇數(shù)則補(bǔ)充一位WORD ID; /用于匹配ECHO和ECHO REPLY包WORD Seq; /用于標(biāo)記ECHO報(bào)文順序BYTE Data1; /用戶數(shù)據(jù);五、程序源代碼(核心部分)2、實(shí)驗(yàn)二PING.H#ifndef PING_H_#define PING_H_#include <n

16、etinet/in_systm.h>#include<netinet/ip.h>#include<netinet/ip_icmp.h>#include<netinet/icmp6.h>#include<netinet/ip6.h>#include<sys/types.h>/* basic system data types */#include<sys/socket.h>/* basic socket definitions */#include<sys/time.h>/* timeval for sel

17、ect() */#include<time.h>/* timespec for pselect() */#include<netinet/in.h>/* sockaddr_in and other Internet defns */#include<arpa/inet.h>/* inet(3) functions */#include<netdb.h>#include<signal.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#inc

18、lude<errno.h>#include <pwd.h>#include<unistd.h>#include<sys/un.h>/* for Unix domain sockets */#include<sys/ioctl.h>#include<net/if.h>#include <stdarg.h>#include <syslog.h>#ifdef HAVE_SOCKADDR_DL_STRUCT# include <net/if_dl.h>#endif#define BUFSIZE1

19、500#define MAXLINE 4096/* globals */char recvbufBUFSIZE;char sendbufBUFSIZE;int ttl_signal;int ttl_value;char *ttl_val;int datalen;/* #bytes of data, following ICMP header */char*host;int broad_sign=0;int nsent;/* add 1 for each sendto() */pid_t pid;/* our PID */int sockfd;int verbose;int daemon_pro

20、c; /* set nonzero by daemon_init() */* function prototypes */void proc_v4(char *, ssize_t, struct timeval *);void proc_v6(char *, ssize_t, struct timeval *);void send_v4(void);void send_v6(void);void readloop(void);void sig_alrm(int);void tv_sub(struct timeval *, struct timeval *);char * Sock_ntop_h

21、ost(const struct sockaddr *sa, socklen_t salen);struct addrinfo* host_serv(const char *host, const char *serv, int family, int socktype);static void err_doit(int errnoflag, int level, const char *fmt, va_list ap);void err_quit(const char *fmt, .);void err_sys(const char *fmt, .);struct proto void (*

22、fproc)(char *, ssize_t, struct timeval *); void (*fsend)(void); struct sockaddr *sasend;/* sockaddr for send, from getaddrinfo */ struct sockaddr *sarecv;/* sockaddr for receiving */ socklen_t salen;/* length of sockaddrs */ int icmpproto;/* IPPROTO_xxx value for ICMP */ *pr;#endif /* PING_H_ */PING

23、.C#include "ping.h" struct protoproto_v4 = proc_v4, send_v4, NULL, NULL, 0, IPPROTO_ICMP ;#ifdefIPV6struct protoproto_v6 = proc_v6, send_v6, NULL, NULL, 0, IPPROTO_ICMPV6 ;#endifintdatalen = 56;/* data that goes with ICMP echo request */double rtt_min = INFINITY, rtt_max = -INFINITY, rtt_t

24、otal = 0, rtt_sqr_total = 0;long long send_count = 0, recv_count = 0;int ttl_flag = 0, broadcast_flag = 0;int ttl = 0;struct timeval tval_start;const char *usage = "usage: ping -v -h -b -t ttl -q <hostname>n" "t-vtNormal moden" "t-btBroadcastn" "t-t ttltSet T

25、TL(0-255)n" "t-qtQuiet mode"int main(int argc, char *argv)intc;struct addrinfo*ai;opterr = 0;/* don't want getopt() writing to stderr */while ( (c = getopt(argc, argv, "vhbt:q") != -1) switch (c) case 'v':verbose+;break;case 'h': puts(usage); return 0;cas

26、e 'b': broadcast_flag = 1; break;case 't': ttl_flag = sscanf(optarg, "%d", &ttl) && ttl >= 0 && ttl < 256; break;case 'q': verbose-; break;case '?':err_quit("unrecognized option: %c", c, usage);if (optind != argc-1)err_qui

27、t(usage);host = argvoptind;pid = getpid();signal(SIGALRM, sig_alrm);signal(SIGINT, sig_int); ai = host_serv(host, NULL, 0, 0);printf("ping %s (%s): %d data bytesn", ai->ai_canonname, Sock_ntop_host(ai->ai_addr, ai->ai_addrlen), datalen);/* 4initialize according to protocol */if (a

28、i->ai_family = AF_INET) pr = &proto_v4;#ifdefIPV6 else if (ai->ai_family = AF_INET6) pr = &proto_v6;if (IN6_IS_ADDR_V4MAPPED(&(struct sockaddr_in6 *) ai->ai_addr)->sin6_addr)err_quit("cannot ping IPv4-mapped IPv6 address");#endif elseerr_quit("unknown address f

29、amily %d", ai->ai_family);pr->sasend = ai->ai_addr;pr->sarecv = calloc(1, ai->ai_addrlen);pr->salen = ai->ai_addrlen; gettimeofday(&tval_start, NULL);readloop();exit(0);void proc_v4(char *ptr, ssize_t len, struct timeval *tvrecv)inthlen1, icmplen;doublertt;struct ip*ip;

30、struct icmp*icmp;struct timeval*tvsend;ip = (struct ip *) ptr;/* start of IP header */hlen1 = ip->ip_hl << 2;/* length of IP header */icmp = (struct icmp *) (ptr + hlen1);/* start of ICMP header */if ( (icmplen = len - hlen1) < 8)err_quit("icmplen (%d) < 8", icmplen);if (icm

31、p->icmp_type = ICMP_ECHOREPLY) if (icmp->icmp_id != pid)return;/* not a response to our ECHO_REQUEST */if (icmplen < 16)err_quit("icmplen (%d) < 16", icmplen);tvsend = (struct timeval *) icmp->icmp_data;tv_sub(tvrecv, tvsend);rtt = tvrecv->tv_sec * 1000.0 + tvrecv->tv_

32、usec / 1000.0;if (rtt < rtt_min) rtt_min = rtt;if (rtt > rtt_max) rtt_max = rtt;rtt_total += rtt;rtt_sqr_total += rtt * rtt;recv_count+;if (verbose > 0)printf("%d bytes from %s: seq=%u, ttl=%d, rtt=%.3f msn",icmplen, Sock_ntop_host(pr->sarecv, pr->salen),icmp->icmp_seq, i

33、p->ip_ttl, rtt); else if (verbose > 1) printf(" %d bytes from %s: type = %d, code = %dn",icmplen, Sock_ntop_host(pr->sarecv, pr->salen),icmp->icmp_type, icmp->icmp_code);void proc_v6(char *ptr, ssize_t len, struct timeval* tvrecv)#ifdefIPV6inthlen1, icmp6len;doublertt;str

34、uct ip6_hdr*ip6;struct icmp6_hdr*icmp6;struct timeval*tvsend;ip6 = (struct ip6_hdr *) ptr;/* start of IPv6 header */hlen1 = sizeof(struct ip6_hdr);if (ip6->ip6_nxt != IPPROTO_ICMPV6)err_quit("next header not IPPROTO_ICMPV6");icmp6 = (struct icmp6_hdr *) (ptr + hlen1);if ( (icmp6len = le

35、n - hlen1) < 8)err_quit("icmp6len (%d) < 8", icmp6len);if (icmp6->icmp6_type = ICMP6_ECHO_REPLY) if (icmp6->icmp6_id != pid)return;/* not a response to our ECHO_REQUEST */if (icmp6len < 16)err_quit("icmp6len (%d) < 16", icmp6len);tvsend = (struct timeval *) (icm

36、p6 + 1);tv_sub(tvrecv, tvsend);rtt = tvrecv->tv_sec * 1000.0 + tvrecv->tv_usec / 1000.0;if (rtt < rtt_min) rtt_min = rtt;if (rtt > rtt_max) rtt_max = rtt;rtt_total += rtt;rtt_sqr_total += rtt * rtt;recv_count+;if (verbose > 0)printf("%d bytes from %s: seq=%u, hlim=%d, rtt=%.3f ms

37、n",icmp6len, Sock_ntop_host(pr->sarecv, pr->salen),icmp6->icmp6_seq, ip6->ip6_hlim, rtt); else if (verbose > 1) printf(" %d bytes from %s: type = %d, code = %dn",icmp6len, Sock_ntop_host(pr->sarecv, pr->salen),icmp6->icmp6_type, icmp6->icmp6_code);#endif/* I

38、PV6 */unsigned short in_cksum(unsigned short *addr, int len) int nleft = len; int sum = 0; unsigned short *w = addr; unsigned short answer = 0; while (nleft > 1) sum += *w+; nleft -= 2; /* 4mop up an odd byte, if necessary */ if (nleft = 1) *(unsigned char *)(&answer) = *(unsigned char *)w ;

39、sum += answer; /* 4add back carry outs from top 16 bits to low 16 bits */ sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ sum += (sum >> 16); /* add carry */ answer = sum; /* truncate to 16 bits */ return(answer);void send_v4(void)intlen;struct icmp*icmp;icmp = (struct

40、icmp *) sendbuf;icmp->icmp_type = ICMP_ECHO;icmp->icmp_code = 0;icmp->icmp_id = pid;icmp->icmp_seq = nsent+;gettimeofday(struct timeval *) icmp->icmp_data, NULL);len = 8 + datalen;/* checksum ICMP header and data */icmp->icmp_cksum = 0;icmp->icmp_cksum = in_cksum(u_short *) icmp

41、, len);sendto(sockfd, sendbuf, len, 0, pr->sasend, pr->salen);void send_v6()#ifdefIPV6intlen;struct icmp6_hdr*icmp6;icmp6 = (struct icmp6_hdr *) sendbuf;icmp6->icmp6_type = ICMP6_ECHO_REQUEST;icmp6->icmp6_code = 0;icmp6->icmp6_id = pid;icmp6->icmp6_seq = nsent+;gettimeofday(struct

42、timeval *) (icmp6 + 1), NULL);len = 8 + datalen;/* 8-byte ICMPv6 header */sendto(sockfd, sendbuf, len, 0, pr->sasend, pr->salen);#endif/* IPV6 */void readloop(void)intsize;charrecvbufBUFSIZE;socklen_tlen;ssize_tn;struct timevaltval;sockfd = socket(pr->sasend->sa_family, SOCK_RAW, pr->

43、icmpproto);setuid(getuid();/* don't need special permissions any more */size = 60 * 1024;/* OK if setsockopt fails */setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size);if (ttl_flag) setsockopt(sockfd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl) ; if (broadcast_flag) setsockopt(sockf

44、d, SOL_SOCKET, SO_BROADCAST, &broadcast_flag, sizeof(broadcast_flag) ;sig_alrm(SIGALRM);/* send first packet */for ( ; ; ) len = pr->salen;n = recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0, pr->sarecv, &len);if (n < 0) if (errno = EINTR)continue;elseerr_sys("recvfrom error"

45、);gettimeofday(&tval, NULL);(*pr->fproc)(recvbuf, n, &tval);void sig_alrm(int signo) (*pr->fsend)(); send_count+; alarm(1); return; /* probably interrupts recvfrom() */void sig_int(int signo) struct timeval tval_end; double tval_total; gettimeofday(&tval_end, NULL); tv_sub(&tva

46、l_end, &tval_start); tval_total = tval_end.tv_sec * 1000.0 + tval_end.tv_usec / 1000.0; puts("- ping statistics -"); printf("%lld packets transmitted, %lld received, %.0lf% packet loss, time %.2lfmsn", send_count, recv_count, (send_count - recv_count) * 100.0 / send_count, tv

47、al_total); double rtt_avg = rtt_total / recv_count; printf("rtt min/avg/max/mdev = %.3lf/%.3lf/%.3lf/%.3lf msn", rtt_min, rtt_avg, rtt_max, rtt_sqr_total / recv_count - rtt_avg * rtt_avg); close(sockfd); exit(0);void tv_sub(struct timeval *out, struct timeval *in)if ( (out->tv_usec -= i

48、n->tv_usec) < 0) /* out -= in */-out->tv_sec;out->tv_usec += ;out->tv_sec -= in->tv_sec;char * sock_ntop_host(const struct sockaddr *sa, socklen_t salen) static char str128; /* Unix domain is largest */ switch (sa->sa_family) case AF_INET: struct sockaddr_in *sin = (struct socka

49、ddr_in *) sa; if (inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str) = NULL) return(NULL); return(str); #ifdef IPV6 case AF_INET6: struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa; if (inet_ntop(AF_INET6, &sin6->sin6_addr, str, sizeof(str) = NULL) return(NULL); return(str); #end

50、if#ifdef HAVE_SOCKADDR_DL_STRUCT case AF_LINK: struct sockaddr_dl *sdl = (struct sockaddr_dl *) sa; if (sdl->sdl_nlen > 0) snprintf(str, sizeof(str), "%*s", sdl->sdl_nlen, &sdl->sdl_data0); else snprintf(str, sizeof(str), "AF_LINK, index=%d", sdl->sdl_index); return(str); #endif default: snprintf(str, sizeof(str), "sock_ntop_host: unknown AF_xxx: %d, len %d", sa->sa_family, salen); return(str); return (NULL);char *

溫馨提示

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

評(píng)論

0/150

提交評(píng)論