網(wǎng)絡(luò)流量在線分析系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)_第1頁(yè)
網(wǎng)絡(luò)流量在線分析系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)_第2頁(yè)
網(wǎng)絡(luò)流量在線分析系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)_第3頁(yè)
網(wǎng)絡(luò)流量在線分析系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)_第4頁(yè)
網(wǎng)絡(luò)流量在線分析系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)_第5頁(yè)
已閱讀5頁(yè),還剩25頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)專心-專注-專業(yè)精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)綜合實(shí)訓(xùn)報(bào)告題目:網(wǎng)絡(luò)流量在線分析系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)華中農(nóng)業(yè)大學(xué)正方教務(wù)系統(tǒng)王楓指導(dǎo)老師:王建勇 信息學(xué)院計(jì)算機(jī)科學(xué)系目 錄一、 實(shí)訓(xùn)目的3二、 實(shí)訓(xùn)內(nèi)容3 三、 主要設(shè)備及環(huán)境4 四、 設(shè)計(jì)與步驟5 五、 整理與小結(jié)17 六、 參考文獻(xiàn)18一、實(shí)訓(xùn)目的設(shè)計(jì)并實(shí)現(xiàn)一個(gè)網(wǎng)絡(luò)流量的分析系統(tǒng)。該系統(tǒng)具有以下功能:(1)實(shí)時(shí)抓取網(wǎng)絡(luò)數(shù)據(jù)。(2)網(wǎng)絡(luò)協(xié)議分析與顯示。(3)將網(wǎng)絡(luò)數(shù)據(jù)包聚合成數(shù)據(jù)流,以源IP、目的IP、源端口、目的端口及協(xié)議等五元組的形式存儲(chǔ)。(4)計(jì)算并顯示固定時(shí)間

2、間隔內(nèi)網(wǎng)絡(luò)連接(雙向流)的統(tǒng)計(jì)量(如上行與下行的數(shù)據(jù)包數(shù)目,上行與下行的數(shù)據(jù)量大小等)。在這些統(tǒng)計(jì)數(shù)據(jù)的基礎(chǔ)上分析不同網(wǎng)絡(luò)應(yīng)用的流量特征。二、實(shí)訓(xùn)內(nèi)容 (1)能夠?qū)崟r(shí)抓取網(wǎng)絡(luò)中的數(shù)據(jù)包。并實(shí)時(shí)顯示在程序界面上。用戶可自定義過(guò)濾條件以抓取所需要的數(shù)據(jù)包。(2)分析各個(gè)網(wǎng)絡(luò)協(xié)議格式,能夠顯示各協(xié)議字段的實(shí)際意義。例如,能夠通過(guò)該程序反映TCP三次握手的實(shí)現(xiàn)過(guò)程。(3)采用Hash鏈表的形式將網(wǎng)絡(luò)數(shù)據(jù)以連接(雙向流)的形式存儲(chǔ)。(4)計(jì)算并顯示固定時(shí)間間隔內(nèi)網(wǎng)絡(luò)連接(雙向流)的統(tǒng)計(jì)量(如上行與下行的數(shù)據(jù)包數(shù)目,上行與下行的數(shù)據(jù)量大小等)。例如,抓取一段時(shí)間(如30分鐘)的網(wǎng)絡(luò)流量,將該段時(shí)間以固定時(shí)

3、長(zhǎng)(如1分鐘)為單位分成若干個(gè)時(shí)間片,計(jì)算網(wǎng)絡(luò)連接在每一個(gè)時(shí)間片內(nèi)的相關(guān)統(tǒng)計(jì)量。并在上述統(tǒng)計(jì)數(shù)據(jù)的基礎(chǔ)上分析不同應(yīng)用如WEB、DNS、在線視頻等服務(wù)的流量特征。注意,可根據(jù)實(shí)際的流量分析需要自己定義相關(guān)的統(tǒng)計(jì)量。三、主要設(shè)備及環(huán)境硬件設(shè)備:(1)臺(tái)式計(jì)算機(jī)或筆記本計(jì)算機(jī)(含網(wǎng)絡(luò)適配器)軟件設(shè)備:(2)Windows操作系統(tǒng)(3)網(wǎng)絡(luò)數(shù)據(jù)包捕獲函數(shù)包,Windows平臺(tái)為winpcap(4)編程語(yǔ)言選用C/C+。(5)編程環(huán)境為codeblocks設(shè)計(jì)與步驟定義 mac,以太網(wǎng)幀,IPv4 首部,TCP 首部, UDP 首部與一些用于設(shè)置時(shí)間的結(jié)構(gòu)體, 回調(diào)函數(shù)原型 包括哈希表的插入,搜索,初始

4、化。* 6字節(jié)的mac地址 */typedef struct mac_address u_char byte1; u_char byte2; u_char byte3; u_char byte4; u_char byte5; u_char byte6; mac_address;/* 以太網(wǎng)幀 */typedef struct ethernet_header mac_address daddr; /目的MAC地址 mac_address saddr; /源MAC地址 u_short etherType /以太網(wǎng)幀類型 ethernet_header;/* IPv4 首部 */typedef st

5、ruct ip_header u_char ver:4,ihl:4; / 版本 (4 bits) + 首部長(zhǎng)度 (4 bits) u_char tos; / 服務(wù)類型(Type of service) u_short tlen; / 總長(zhǎng)(Total length) u_short identification; / 標(biāo)識(shí)(Identification) u_short flags_fo; / 標(biāo)志位(Flags) (3 bits) + 段偏移量(Fragment offset) (13 bits) u_char ttl; / 存活時(shí)間(Time to live) u_char proto;

6、/ 協(xié)議(Protocol) u_short crc; / 首部校驗(yàn)和(Header checksum) struct in_addr saddr; / 源地址(Source address) struct in_addr daddr; / 目的地址(Destination address) u_int op_pad; / 選項(xiàng)與填充(Option + Padding) ip_header;/* TCP 首部*/typedef struct tcp_header u_short sport; / 源端口(Source port) u_short dport; / 目的端口(Destinatio

7、n port) u_int32_t snumber; /序列號(hào) u_int32_t cnumber; /確認(rèn)號(hào) u_short reserve:6, /保留位 tlen:4, /報(bào)頭長(zhǎng)度 /后6位標(biāo)志位 fin : 1, /關(guān)閉連接標(biāo)志 syn : 1, /請(qǐng)求連接標(biāo)志 rst : 1, /重置連接標(biāo)志 psh : 1, /接收方盡快將數(shù)據(jù)放到應(yīng)用層標(biāo)志 ack : 1, /確認(rèn)序號(hào)標(biāo)志 urg : 1, /緊急指針標(biāo)志 ece : 1, /擁塞標(biāo)志位 cwr : 1; /擁塞標(biāo)志位 u_short window; /窗口 u_short csum; /校驗(yàn)和 u_short urgent;

8、/緊急 u_int op_pad; / 選項(xiàng)與填充(Option + Padding) tcp_header;/* UDP 首部*/typedef struct udp_header u_short sport; / 源端口(Source port) u_short dport; / 目的端口(Destination port) u_short len; / UDP數(shù)據(jù)包長(zhǎng)度(Datagram length) u_short crc; / 校驗(yàn)和(Checksum) udp_header;/*用于設(shè)置時(shí)間的結(jié)構(gòu)體*/typedef struct argument pcap_t *adhandl

9、e; int time;argument;void crawl_time(void *time_c);/* 回調(diào)函數(shù)原型 */void packet_handler_mac(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);void packet_handler_ip(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);void packet_handler_tcp(u_char *param, const

10、 struct pcap_pkthdr *header, const u_char *pkt_data);void packet_handler_udp(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);void InitHashTable(HashTable* H)/初始化哈希表 int i; H - count = MAXSIZE; H - Table = ( HashNode* )malloc( ( H-count ) * sizeof( HashNode ) ); for(i = 0;i c

11、ount; i+) H-Tablei.ip_source_address; H-Tablei.ip_dest_address; H-Tablei.source_port = 0; H-Tablei.dest_port = 0; H-Tablei.sum = NULLKEY; H-Tablei.next = NULL; int Hash(int key) return key % MAXSIZE;int InsertHashTable(HashTable *H,struct in_addr source_address,struct in_addr dest_address,u_int16_t

12、s_port,u_int16_t d_port,int key)/插入哈希表 int addr; addr = Hash(key); if(H-Tableaddr.sum != key & H -Tableaddr.sum != NULLKEY) HashNode *hashnode = (HashNode *)malloc(sizeof(HashNode); hashnode-next = H-Tableaddr.next; hashnode-ip_source_address = source_address; hashnode-ip_dest_address = dest_address

13、; hashnode-sum = key; hashnode-source_port = s_port; hashnode-dest_port = d_port; H-Tableaddr.next = hashnode; addr+; return addr; else if(H-Tableaddr.sum = NULLKEY) H-Tableaddr.sum = key; return addr; bool SerchHashTable(HashTable *H,struct in_addr source_address,struct in_addr dest_address,u_int16

14、_t s_port,u_int16_t d_port,int key)/搜索哈希表 int addr; addr = Hash(key); if(H-Tableaddr.sum = key & (inet_ntoa(H-Tableaddr.ip_source_address) = inet_ntoa(source_address) & (inet_ntoa(H-Tableaddr.ip_dest_address) = inet_ntoa(dest_address) & (H-Tableaddr.source_port = s_port) & (H-Tableaddr.dest_port = d

15、_port) return true; HashNode *p = H-Tableaddr.next; while(p != NULL) if(p-sum = key) & (inet_ntoa(p-ip_source_address) = inet_ntoa(source_address) & (inet_ntoa(p-ip_dest_address) = inet_ntoa(dest_address) & (p-source_port = s_port) &(p-dest_port = d_port) return true; else p-next; return false; 2./*

16、獲得設(shè)備名,檢索機(jī)器所連接的所有網(wǎng)絡(luò)適配器,并在屏幕中顯示適配器的名稱和詳細(xì)信息,用戶可以輸入適配器編號(hào)選擇指定的適配器用來(lái)捕獲,代碼與結(jié)果顯示如下:*/ if (pcap_createsrcstr(source, PCAP_SRC_IFLOCAL, NULL, NULL, NULL, errbuf) = -1) printf(%sn, errbuf);exit(-1); /* 獲得設(shè)備列表 */ if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) = -1) fprintf(stderr,Error in p

17、cap_findalldevs: %sn, errbuf); exit(1); /* 打印列表 */ for(d=alldevs; d; d=d-next) printf(%d. %s, +i, d-name); if (d-description) printf( (%s)n, d-description); else printf( (No description available)n); if(i=0) printf(nNo interfaces found! Make sure WinPcap is installed.n); return -1; while(inum i) pri

18、ntf(Enter the interface number (1-%d):,i); scanf(%d, &inum); if(inum i) printf(輸入有誤,請(qǐng)重新輸入!n); 截圖如下:3. /*選擇過(guò)濾協(xié)議*/ int chang1 = 0; printf(n過(guò)濾協(xié)議如下:); printf(nt1、MAC); printf(nt2、IP); printf(nt3、IP and TCP); printf(nt4、IP and UDP); printf(請(qǐng)選擇:); scanf(%d,&chang1); while(chang1 4) printf(輸入有誤,請(qǐng)重新輸入:n); s

19、canf(%d,&chang1); switch(chang1) case 1: strcpy(packet_filter,t0); break; case 2: strcpy(packet_filter,t1); break; case 3: strcpy(packet_filter,t2); break; case 4: strcpy(packet_filter,t3); break; /*輸入讀取時(shí)間*/ while(time = 0) printf(n讀取時(shí)間(s):); scanf(%d,&time); if(time = 0) printf(輸入有誤,請(qǐng)重新輸入!n); 4. /*

20、 跳轉(zhuǎn)到已選設(shè)備 */ for(d=alldevs, i=0; inext, i+);5. /* 打開(kāi)適配器 */ if (adhandle= pcap_open(d-name, / 設(shè)備名 65536, / 要捕捉的數(shù)據(jù)包的部分 / 65535保證能捕獲到不同數(shù)據(jù)鏈路層上的每個(gè)數(shù)據(jù)包的全部?jī)?nèi)容 PCAP_OPENFLAG_PROMISCUOUS, / 混雜模式 1000, / 讀取超時(shí)時(shí)間 NULL, / 遠(yuǎn)程機(jī)器驗(yàn)證 errbuf / 錯(cuò)誤緩沖池 ) = NULL) fprintf(stderr,nUnable to open the adapter. %s is not supporte

21、d by WinPcapn, d-name); /* 釋放設(shè)備列表 */ pcap_freealldevs(alldevs); return -1; 6. /* 檢查數(shù)據(jù)鏈路層,為了簡(jiǎn)單,我們只考慮以太網(wǎng) */ if(pcap_datalink(adhandle) != DLT_EN10MB) fprintf(stderr,nThis program works only on Ethernet networks.n); /* 釋放設(shè)備列表 */ pcap_freealldevs(alldevs); return -1;7. /* 檢查數(shù)據(jù)鏈路層,為了簡(jiǎn)單,我們只考慮以太網(wǎng) */ if(pca

22、p_datalink(adhandle) != DLT_EN10MB) fprintf(stderr,nThis program works only on Ethernet networks.n); /* 釋放設(shè)備列表 */ pcap_freealldevs(alldevs); return -1; /*dump文件,打開(kāi)指定文件存儲(chǔ)捕獲的數(shù)據(jù)包:*/ dumpfp = pcap_dump_open(adhandle, Output);if( dumpfp = NULL) printf(Error on opening output filen);exit(-1); if(d-address

23、es != NULL) /* 獲得接口第一個(gè)地址的掩碼 */ netmask=(struct sockaddr_in *)(d-addresses-netmask)-sin_addr.S_un.S_addr; else /* 如果接口沒(méi)有地址,那么我們假設(shè)一個(gè)C類的掩碼 */ netmask=0 xffffff; /編譯過(guò)濾器 if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) 0 ) fprintf(stderr,nUnable to compile the packet filter. Check the syntax

24、.n); /* 釋放設(shè)備列表 */ pcap_freealldevs(alldevs); return -1; /設(shè)置過(guò)濾器 if (pcap_setfilter(adhandle, &fcode)description); /* 釋放設(shè)備列表 */ pcap_freealldevs(alldevs);8. /*設(shè)置抓取時(shí)間*/ pthread_t time_pt; argument time_c; time_c.adhandle = adhandle; time_c.time = time * 1000; if(pthread_create(&time_pt,NULL,crawl_time,

25、&time_c) != 0) fprintf(stderr,n pthread_create is error.n); return -1; printf(n正在寫(xiě)入文件.n); /* 開(kāi)始捕捉 并存儲(chǔ)dump文件*/ pcap_loop(adhandle, 0, packet_handler_mac,dumpfp); /*關(guān)閉設(shè)備*/ pcap_close(adhandle); printf(文件已寫(xiě)入!n); fclose(file_out);結(jié)果如下:9. pcap_t *fp;/文件指針 int res; if ( pcap_createsrcstr(source, / 源字符串 PC

26、AP_SRC_FILE, / 我們要打開(kāi)的文件 NULL, / 遠(yuǎn)程主機(jī) NULL, / 遠(yuǎn)程主機(jī)端口 Output, / 我們要打開(kāi)的文件名 errbuf / 錯(cuò)誤緩沖區(qū) ) != 0) fprintf(stderr,nError creating a source stringn); return -1; /* 打開(kāi)捕獲文件 */ if ( (fp= pcap_open(source, / 設(shè)備名 65536, / 要捕捉的數(shù)據(jù)包的部分 / 65535保證能捕獲到不同數(shù)據(jù)鏈路層上的每個(gè)數(shù)據(jù)包的全部?jī)?nèi)容 PCAP_OPENFLAG_PROMISCUOUS, / 混雜模式 1000, / 讀取

27、超時(shí)時(shí)間 NULL, / 遠(yuǎn)程機(jī)器驗(yàn)證 errbuf / 錯(cuò)誤緩沖池 ) ) = NULL) fprintf(stderr,nUnable to open the file %s.n, source); return -1; HashTable Htcp;/基于TCP協(xié)議的哈希鏈表InitHashTable(&Htcp);/進(jìn)行初始化HashTable Hudp;/基于UDP協(xié)議的哈希鏈表InitHashTable(&Hudp);/進(jìn)行初始化FILE *data_out = fopen(dataout.txt,w+); /* 從文件獲取數(shù)據(jù)包 */ while(res = pcap_next_

28、ex(fp, &header, &pkt_data) = 0) packages_number+; int tcp_s = 0; int udp_s = 0;struct ip_header *ih0;ih0 = (struct ip_header *)(pkt_data + 14);int ip_len = ih0-ihl * 4;struct tcp_header *th0;th0 = (struct tcp_header *)(pkt_data + 14 + ip_len);struct udp_header *uh0;uh0 = (struct udp_header *)(pkt_da

29、ta + 14 + ip_len); data_amount += ih0-tlen;if (ih0-proto = 17)/*UDP協(xié)議*/ udp_data_amount += ih0-tlen; udp_packages_number+;int sum = inet_addr(inet_ntoa(ih0-saddr);udp_s = InsertHashTable(&Hudp, ih0-saddr, ih0-daddr, uh0-sport,uh0-dport,sum); fprintf(data_out, 這是第%d個(gè)數(shù)據(jù)包,packages_number);fprintf(data_

30、out, 該數(shù)據(jù)包為UDP協(xié)議:n);fprintf(data_out, 源IP地址:%sn, inet_ntoa(ih0-saddr);fprintf(data_out, 目的地址:%sn, inet_ntoa(ih0-daddr);fprintf(data_out, 源端口:%dn, uh0-sport);fprintf(data_out, 目的端口:%dn, uh0-dport);if(strcmp(inet_ntoa(ih0-saddr),local_ip) = 0) fprintf(data_out, 目的地址與本地地址對(duì)比,判斷為上傳數(shù)據(jù)包n); upload_udp_data_a

31、mount += ih0-tlen; upload_udp_packages_number+; else fprintf(data_out, 目的地址與本地地址對(duì)比,判斷為下載數(shù)據(jù)包n); download_udp_data_amount += ih0-tlen; download_udp_packages_number+; fprintf(data_out,+nn);elseif (ih0-proto = 6)/*TCP協(xié)議*/ tcp_data_amount += ih0-tlen; tcp_packages_number+; int sum = inet_addr(inet_ntoa(i

32、h0-saddr); tcp_s = InsertHashTable(&Htcp, ih0-saddr, ih0-daddr, th0-sport,th0-dport, sum); fprintf(data_out, 這是第%d個(gè)數(shù)據(jù)包,packages_number);fprintf(data_out, 該數(shù)據(jù)包為T(mén)CP協(xié)議:n);fprintf(data_out, 源IP地址:%sn, inet_ntoa(ih0-saddr);fprintf(data_out, 目的地址:%sn, inet_ntoa(ih0-daddr);fprintf(data_out, 源端口:%dn, th0-sp

33、ort);fprintf(data_out, 目的端口:%dn, th0-dport);if(strcmp(inet_ntoa(ih0-saddr),local_ip) = 0) fprintf(data_out, 目的地址與本地地址對(duì)比,判斷為上傳數(shù)據(jù)包nn); upload_tcp_data_amount += ih0-tlen; upload_tcp_packages_number+; else fprintf(data_out, 目的地址與本地地址對(duì)比,判斷為下載數(shù)據(jù)包n); download_tcp_data_amount += ih0-tlen; download_tcp_pack

34、ages_number+; fprintf(data_out,+n); if (res = -1) printf(Error reading the packets: %sn, pcap_geterr(fp); fclose(data_out); printf(n-統(tǒng)計(jì)量-n); printf( 數(shù)據(jù)包總數(shù)量: %dn,packages_number); printf( tcp包數(shù)量: %dn,tcp_packages_number); printf( 上傳tcp包數(shù)量: %dn,upload_tcp_packages_number); printf( 下載tcp包數(shù)量: %dn,download_tcp_packages_number); printf( udp包數(shù)量: %dn,udp_packages_number); printf( 上傳udp包數(shù)量: %dn,upload_udp_packages_number); printf( 下載udp包數(shù)量: %dn,download_udp_packages_number); printf( 總數(shù)據(jù)量: %ldn,data_amount); printf( tcp數(shù)據(jù)量: %ldn,tcp_data_amount); printf( 上傳tcp數(shù)據(jù)量: %ldn,upload_

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 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)論