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

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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

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

3、長(如1分鐘)為單位分成若干個時間片,計算網(wǎng)絡連接在每一個時間片內的相關統(tǒng)計量。并在上述統(tǒng)計數(shù)據(jù)的基礎上分析不同應用如WEB、DNS、在線視頻等服務的流量特征。注意,可根據(jù)實際的流量分析需要自己定義相關的統(tǒng)計量。三、主要設備及環(huán)境硬件設備:(1)臺式計算機或筆記本計算機(含網(wǎng)絡適配器)軟件設備:(2)Windows操作系統(tǒng)(3)網(wǎng)絡數(shù)據(jù)包捕獲函數(shù)包,Windows平臺為winpcap(4)編程語言選用C/C+。(5)編程環(huán)境為codeblocks設計與步驟定義 mac,以太網(wǎng)幀,IPv4 首部,TCP 首部, UDP 首部與一些用于設置時間的結構體, 回調函數(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) + 首部長度 (4 bits) u_char tos; / 服務類型(Type of service) u_short tlen; / 總長(Total length) u_short identification; / 標識(Identification) u_short flags_fo; / 標志位(Flags) (3 bits) + 段偏移量(Fragment offset) (13 bits) u_char ttl; / 存活時間(Time to live) u_char proto;

6、/ 協(xié)議(Protocol) u_short crc; / 首部校驗和(Header checksum) struct in_addr saddr; / 源地址(Source address) struct in_addr daddr; / 目的地址(Destination address) u_int op_pad; / 選項與填充(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; /序列號 u_int32_t cnumber; /確認號 u_short reserve:6, /保留位 tlen:4, /報頭長度 /后6位標志位 fin : 1, /關閉連接標志 syn : 1, /請求連接標志 rst : 1, /重置連接標志 psh : 1, /接收方盡快將數(shù)據(jù)放到應用層標志 ack : 1, /確認序號標志 urg : 1, /緊急指針標志 ece : 1, /擁塞標志位 cwr : 1; /擁塞標志位 u_short window; /窗口 u_short csum; /校驗和 u_short urgent;

8、/緊急 u_int op_pad; / 選項與填充(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ù)包長度(Datagram length) u_short crc; / 校驗和(Checksum) udp_header;/*用于設置時間的結構體*/typedef struct argument pcap_t *adhandl

9、e; int time;argument;void crawl_time(void *time_c);/* 回調函數(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、獲得設備名,檢索機器所連接的所有網(wǎng)絡適配器,并在屏幕中顯示適配器的名稱和詳細信息,用戶可以輸入適配器編號選擇指定的適配器用來捕獲,代碼與結果顯示如下:*/ if (pcap_createsrcstr(source, PCAP_SRC_IFLOCAL, NULL, NULL, NULL, errbuf) = -1) printf(%sn, errbuf);exit(-1); /* 獲得設備列表 */ 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(輸入有誤,請重新輸入!n); 截圖如下:3. /*選擇過濾協(xié)議*/ int chang1 = 0; printf(n過濾協(xié)議如下:); printf(nt1、MAC); printf(nt2、IP); printf(nt3、IP and TCP); printf(nt4、IP and UDP); printf(請選擇:); scanf(%d,&chang1); while(chang1 4) printf(輸入有誤,請重新輸入: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; /*輸入讀取時間*/ while(time = 0) printf(n讀取時間(s):); scanf(%d,&time); if(time = 0) printf(輸入有誤,請重新輸入!n); 4. /*

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

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

22、p_datalink(adhandle) != DLT_EN10MB) fprintf(stderr,nThis program works only on Ethernet networks.n); /* 釋放設備列表 */ pcap_freealldevs(alldevs); return -1; /*dump文件,打開指定文件存儲捕獲的數(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) /* 獲得接口第一個地址的掩碼 */ netmask=(struct sockaddr_in *)(d-addresses-netmask)-sin_addr.S_un.S_addr; else /* 如果接口沒有地址,那么我們假設一個C類的掩碼 */ netmask=0 xffffff; /編譯過濾器 if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) 0 ) fprintf(stderr,nUnable to compile the packet filter. Check the syntax

24、.n); /* 釋放設備列表 */ pcap_freealldevs(alldevs); return -1; /設置過濾器 if (pcap_setfilter(adhandle, &fcode)description); /* 釋放設備列表 */ pcap_freealldevs(alldevs);8. /*設置抓取時間*/ 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正在寫入文件.n); /* 開始捕捉 并存儲dump文件*/ pcap_loop(adhandle, 0, packet_handler_mac,dumpfp); /*關閉設備*/ pcap_close(adhandle); printf(文件已寫入!n); fclose(file_out);結果如下:9. pcap_t *fp;/文件指針 int res; if ( pcap_createsrcstr(source, / 源字符串 PC

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

27、超時時間 NULL, / 遠程機器驗證 errbuf / 錯誤緩沖池 ) ) = NULL) fprintf(stderr,nUnable to open the file %s.n, source); return -1; HashTable Htcp;/基于TCP協(xié)議的哈希鏈表InitHashTable(&Htcp);/進行初始化HashTable Hudp;/基于UDP協(xié)議的哈希鏈表InitHashTable(&Hudp);/進行初始化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個數(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, 目的地址與本地地址對比,判斷為上傳數(shù)據(jù)包n); upload_udp_data_a

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

最新文檔

評論

0/150

提交評論