- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
因为我想用 libpcap 做一些测试和一个小的 C 程序,我试图将一个结构从 main() 传递到 got_packet()。阅读 libpcap 教程后,我发现了这一点:
The prototype for pcap_loop() is below:
int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
The last argument is useful in some applications, but many times is simply set as NULL. Suppose we have arguments of our own that we wish to send to our callback function, in addition to the arguments that pcap_loop() sends. This is where we do it. Obviously, you must typecast to a u_char pointer to ensure the results make it there correctly; as we will see later, pcap makes use of some very interesting means of passing information in the form of a u_char pointer.
因此,据此,可以使用 pcap_loop() 的参数编号 4 发送 got_packet() 中的结构。但是在尝试之后,我得到了一个错误。
这是我的(错误的)代码:
int main(int argc, char **argv)
{
/* some line of code, not important */
/* def. of the structure: */
typedef struct _configuration Configuration;
struct _configuration {
int id;
char title[255];
};
/* init. of the structure: */
Configuration conf[2] = {
{0, "foo"},
{1, "bar"}};
/* use pcap_loop with got_packet callback: */
pcap_loop(handle, num_packets, got_packet, &conf);
}
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
/* this line don't work: */
printf("test: %d\n", *args[0]->id);
}
经过一些测试后我得到了这种错误:
gcc -c got_packet.c -o got_packet.o
got_packet.c: In function ‘got_packet’:
got_packet.c:25: error: invalid type argument of ‘->’
您看到我如何编辑此代码以便在 got_packet() 函数中传递 conf(带有配置结构数组)吗?
非常感谢您的帮助。
问候
最佳答案
我重写了你的代码,现在编译没有任何错误:
#include <pcap.h>
typedef struct {
int id;
char title[255];
} Configuration;
void got_packet( Configuration args[], const struct pcap_pkthdr *header, const u_char *packet){
(void)header, (void)packet;
printf("test: %d\n", args[0].id);
}
int main(void){
Configuration conf[2] = {
{0, "foo"},
{1, "bar"}};
pcap_loop(NULL, 0, (pcap_handler)got_packet, (u_char*)conf);
}
关于c - 在 libpcap pcap_loop() 回调上传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1734507/
我的程序像这样使用 libpcap: while pcaket = pcap_next() { ... (modify the pcaket and do checksum) ... pcap_sen
我正在为一个流量非常大的网络编写监控程序(高清视频通过网络流式传输)。大多数数据包都非常大,我只想查看 header (仅限 IP 和 UDP/TCP)。当然,我想避免复制整个数据的开销。 libpc
我对原始套接字和 libcap 的使用感到有点困惑。任何人都可以简单地指出使用两者的优点。我读了几个链接,但是,它让我感到困惑。 最佳答案 libpcap 在不同的操作系统上使用不同的机制。在 Lin
我在项目中看到了这种代码: while (1) { l_numPkts = pcap_next_ex( m_pcapHandle, &header, &pkt_data); //do some
我想使用 libpcap 使用 gcc 选项 -m32 将 .c 文件编译为 32 位可执行文件机器是linux 64位 fedora 16 但是,我收到以下错误 [root@fdf source]#
我只想从我的系统中嗅探传出的“TCP-ACK”数据包。因此,我在 lib-pcap 程序中将过滤器表达式设置为: char filter_exp[] = "src host 172.16.0.1 an
我正在尝试使用交叉编译器 arm-linux-gcc 编译 libpcap。当我运行“make”时出现错误: ./pcap-linux.c:254:14: conflicting types for
我正在编写相当简单的pcap“实时”捕获引擎,但是pcap_dispatch的数据包处理回调实现应花费相对较长的时间进行处理。 pcap是否在单独的线程中运行每个“pcap_handler”回调?如果
我正在使用 lib-pcap 库来安装/卸载过滤器。 为了安装 pcap 过滤器,我使用pcap_compile 后接 pcap_setfilter,效果很好。 对于卸载过滤器,我使用 pcap_fr
pcap_next 是否显示已发送的数据包? 例如:如果我在 libpcap 监听数据包时 ping google.com,然后运行 pcap_next 两次,返回的值将都是数据包,还是其中一个为
我正在尝试使用 libpcap 打印确认号。我确实知道我会得到与我在 pcap 文件中看到的不同的确认号。我的问题是,在 pcap 文件中,编号为 10、11 和 12 的数据包具有不同的 ack 编
我是 libpcap 的新手。 我正在使用这个库来捕获数据包,下面是我编写的捕获数据包的代码。 我正在窃听的接口(interface)总是充满arp数据包,因此总是有数据包到达该接口(interfac
使用 pcap,我声明了 rtp 结构,当我尝试指向数据包的这个区域时,我发现根据我的声明,它以不同的方式工作。 我写的是: struct udphdr *udp; struct rtphdr *rt
我想拦截来自用户空间的网络流量,所以我使用了 libpcap API 来捕获 tcp/ip 数据包。现在我只是在注册的回调中捕获有关数据包 header 和有效负载的信息。我如何使用此回调来阻止流量或
有人可以给我指出一个好的起点来了解 libpcap 如何从网络驱动程序获取数据包吗?我打算用快速实现替换接收函数。 最佳答案 “接收函数”是: 标准驱动程序接收函数,与用于接收数据包并将其交给常规网络
我用库 libpcap(Ubuntu, c) 捕获了信标帧 我将 wlan 模式更改为通过以下函数调用进行监控和捕获 // 3000 is large enough number for test p
我正在使用 libpcap 捕获特定端口上的所有数据包。但是我需要分开并清楚地知道我的设备正在发送哪些数据包,以及它正在接收哪些数据包。实际上,我使用 pcap_close() 来捕获数据包,而且效果
我正在尝试用 C 解析一个 pcap 文件。我不想使用 libpcap。但出于某种原因,我无法做到。你知道我该怎么做吗?这是我的尝试: fseek(f,24,0); while(countcaplen
我遇到了以下情况:我用 pcap_open_live() 打开我的网络接口(interface)之一。然后我正在为 pcap 编译一个过滤器,只捕获指定的以太网类型(ether proto 0x123
我正在尝试编写一个 c++ 应用程序 (linux) 来捕获无线数据包以及相关的信号强度(以 dBm 为单位)。捕获部分很简单,但问题是我找不到任何关于如何获取每个数据包信号强度的文档。 它是标题的一
我是一名优秀的程序员,十分优秀!