【问题标题】:Understanding a function. pcap and BPF理解一个函数。 pcap 和 BPF
【发布时间】:2012-07-26 07:49:57
【问题描述】:

在我尝试制作一个可以接收确认并创建连接的 TCP 程序时,我在一个示例程序中遇到了这个函数:

void *pth_capture_run(void *arg)
{
pcap_t *pd;
char *filter = "dst host 172.17.14.90 and ip";
char *dev = "fxp0";
char errbuf[PCAP_ERRBUF_SIZE];
bpf_u_int32 netp;
bpf_u_int32 maskp;
struct bpf_program  fprog;                  /* Filter Program   */
int dl = 0, dl_len = 0;

if ((pd = pcap_open_live(dev, 1514, 1, 500, errbuf)) == NULL) {
    fprintf(stderr, "cannot open device %s: %s\n", dev, errbuf);
    exit(1);
}

pcap_lookupnet(dev, &netp, &maskp, errbuf);
pcap_compile(pd, &fprog, filter, 0, netp);
if (pcap_setfilter(pd, &fprog) == -1) {
    fprintf(stderr, "cannot set pcap filter %s: %s\n", filter, errbuf);
    exit(1);
}
pcap_freecode(&fprog);
dl = pcap_datalink(pd);

switch(dl) {
    case 1:
        dl_len = 14;
        break;
    default:
        dl_len = 14;
        break;
}

if (pcap_loop(pd, -1, raw_packet_receiver, (u_char *)dl_len) < 0) {
    fprintf(stderr, "cannot get raw packet: %s\n", pcap_geterr(pd));
    exit(1);
}
}

现在我收集到这个函数会打开一个 BPF 设备并设置数据包过滤器,然后等待数据包到达。然而,作为网络编程的新手,我对各种 pcap 函数并不熟悉,并且对于这个函数简单来说是做什么以及它是如何做的仍然有点不清楚。我已经完成了这个教程:http://yuba.stanford.edu/~casado/pcap/section1.html 但我还是有点困惑。你们中的任何人都可以用更简单的术语解释一下吗?谢谢!

【问题讨论】:

  • 究竟哪一部分让你感到困惑?
  • @RedX 说实话大部分都很好,但尤其是前两个 if 语句和介于两者之间的部分。我知道我在这里有点笼统,但正如我所说,这对我来说是全新的。

标签: c tcp network-programming pcap


【解决方案1】:

您似乎已经弄清楚了该功能,至少在逻辑上,对于第一个 if 语句,它会尝试打开您正在收听的设备并在失败时打印错误,第二个设置 pcap编译后过滤。

如果您想了解更多关于特定函数及其参数的信息,您应该看看手册页,例如

人 pcap_open_live

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2015-02-01
    • 1970-01-01
    • 2019-07-16
    • 2011-06-01
    • 2014-06-06
    • 1970-01-01
    相关资源
    最近更新 更多