pcap目前是一个应用广泛的针对网络接口数据包抓取的共享库。
该系统对数据抓取提供一个高层的接口,获得指定网络的全部
数据包,包括那些目的地址不是本机的数据包。
下面的程序是一个简单的实验用例。它只是使用了pcap的一小
部分主要功能,实现对默认网络接口 数据包的抓取,并实现了
简陋的不完全的分析。它可以被视为一个残疾版或毛坯版的
tcpdump。
由于我认识上的不足和我在编程时图省事,所以不可避免的出
现一些错误和不适当的地方。
程序在Fedora 11上测试过。
编译:
g++ -g -W -Wall -Wextra -o mytest -lpcap main.cpp
执行举例(必须使用root账户):
./mytest
./mytest "tcp and dst port 80"
./mytest ""
./mytest "tcp or udp"
我在程序中只对选项做了简单处理。所以引号是必须的,
否则shell会将引号中的选项展开。
参考:
pcap(3), www.tcpdump.org上的文档
main.cpp:
============================================================================
// 2011年 03月 14日 星期一 14:24:24 CST
// author: 李小丹(Li Shao Dan) 字 殊恒(shuheng)
// K.I.S.S
// S.P.O.T
/*
*
* Copyright © 2011 李小丹(Li Shao Dan)
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. 李小丹(Li Shao Dan) makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
pcap(Packet capture library)的简单应用
最新推荐文章于 2020-12-13 09:08:12 发布
本文档介绍了一个简单的示例程序,该程序利用pcap库捕获默认网络接口的数据包,并进行了初步分析。程序已在Fedora 11上测试通过,涉及对TCP/IP协议栈中不同层次的解析,如以太网、IP、TCP和UDP。编译和执行示例中提到了命令行参数的使用。
摘要由CSDN通过智能技术生成