久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

linux - libpcap抓包結果不完整?

瀏覽:154日期:2024-07-03 18:58:02

問題描述

在ubuntu14.04下使用libpcap抓包,我想得到一段使用http傳輸的html,但是得到的結果和同樣情況下wireshark獲得的數據不一致。

目前代碼如下:

#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('錯誤: 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;}

服務器有一個簡單的html,我用瀏覽器訪問服務器http://123.206.7.47/test.html時,wireshark(同樣bpf)抓到這樣10個數據包:

linux - libpcap抓包結果不完整?

我的程序使用調試器看到的卻是這樣的,這個圖對應上圖第四個數據包(大小為474):

linux - libpcap抓包結果不完整?

為什么unsigned char * packet出現incomplete sequence?還有為什么tcp_data這么短?

是我代碼里libpcap少了什么配置還是其他的原因?

還有一點補充是我訪問其他網站時,偶爾能捕捉到完整的HTTP請求,但是在我訪問的那個網頁上就不行。

問題解答

回答1:

已經解決了。直接按caplen讀char就行了,printf('%s')輸出不全似乎是因為某個二進制數據是0被截斷。

相關文章:
主站蜘蛛池模板: 安西县| 新兴县| 永寿县| 奉节县| 杂多县| 繁峙县| 鄂托克旗| 永嘉县| 崇礼县| 甘谷县| 寿光市| 塔河县| 普格县| 武冈市| 涟水县| 阿拉善左旗| 安远县| 格尔木市| 石屏县| 昌图县| 安仁县| 聂拉木县| 开平市| 屏东县| 黔东| 上犹县| 姜堰市| 唐河县| 福泉市| 甘孜县| 察隅县| 嘉鱼县| 遂平县| 库伦旗| 南开区| 营山县| 滕州市| 新晃| 新野县| 鹤庆县| 赣榆县|