linux - libpcap抓包結(jié)果不完整?
問題描述
在ubuntu14.04下使用libpcap抓包,我想得到一段使用http傳輸?shù)膆tml,但是得到的結(jié)果和同樣情況下wireshark獲得的數(shù)據(jù)不一致。
目前代碼如下:
#include <pcap.h>#include <time.h>#include <stdlib.h>#include <stdio.h>#include <linux/if_ether.h>#include <linux/ip.h>#include <linux/tcp.h>#include <string.h>#include <netinet/in.h>void getPacket(u_char * arg, const struct pcap_pkthdr * pkthdr, const u_char * packet){ bpf_u_int32 caplen = pkthdr->caplen; bpf_u_int32 len = pkthdr->len; int * id = (int *)arg; struct iphdr *ip_header = (struct iphdr *)(packet + ETH_HLEN); struct tcphdr *tcp_header = (struct tcphdr *)(packet + ETH_HLEN + sizeof(struct iphdr)); const u_char *tcp_data = packet + ETH_HLEN + sizeof(struct iphdr) + sizeof(struct tcphdr); printf('%snn', tcp_data);}int main(){ char errBuf[PCAP_ERRBUF_SIZE]; pcap_t * device = pcap_open_live('wlan0', 65535, 1, 0, errBuf); if(!device) {printf('錯(cuò)誤: pcap_open_live(): %sn', errBuf);exit(1); } struct bpf_program filter; pcap_compile(device, &filter, 'tcp port 80 and host 123.206.7.47', 1, 0); pcap_setfilter(device, &filter); int id = 0; pcap_loop(device, -1, getPacket, (u_char*)&id); pcap_close(device); return 0;}
服務(wù)器有一個(gè)簡(jiǎn)單的html,我用瀏覽器訪問服務(wù)器http://123.206.7.47/test.html時(shí),wireshark(同樣bpf)抓到這樣10個(gè)數(shù)據(jù)包:
我的程序使用調(diào)試器看到的卻是這樣的,這個(gè)圖對(duì)應(yīng)上圖第四個(gè)數(shù)據(jù)包(大小為474):
為什么unsigned char * packet出現(xiàn)incomplete sequence?還有為什么tcp_data這么短?
是我代碼里libpcap少了什么配置還是其他的原因?
還有一點(diǎn)補(bǔ)充是我訪問其他網(wǎng)站時(shí),偶爾能捕捉到完整的HTTP請(qǐng)求,但是在我訪問的那個(gè)網(wǎng)頁上就不行。
問題解答
回答1:已經(jīng)解決了。直接按caplen讀char就行了,printf('%s')輸出不全似乎是因?yàn)槟硞€(gè)二進(jìn)制數(shù)據(jù)是0被截?cái)唷?/p>
相關(guān)文章:
1. android - 分享到微信,如何快速轉(zhuǎn)換成字節(jié)數(shù)組2. javascript - 能否讓vue-cli的express修改express重啟服務(wù)3. 解決Android webview設(shè)置cookie和cookie丟失的問題4. angular.js - Beego 與 AngularJS的模板格式?jīng)_突,該怎么解決?5. javascript - vue2.0中,$refs對(duì)象為什么用駝峰的方式獲取不到屬性?6. node.js - npm一直提示proxy有問題7. html5 - 有人做過防微信app界面的H5 demo嗎?8. javascript - 有沒有iOS微信中可以在背景播放視頻的方法?9. 最新版本的微信web開發(fā)者工具必須要APPID,會(huì)提供測(cè)試號(hào),但是像你一樣tabBar配置的話不會(huì)顯示首頁與日志,難道我要下載跟你一樣的版本?10. Navicat for mysql 中以json格式儲(chǔ)存的數(shù)據(jù)存在大量反斜杠,如何去除?
