iphone - iOS 上的 libpcap,pcap_next() 总是返回 null

标签 iphone ios pcap libpcap

我是iOS初学者,毕业设计是在iOS上开发一款抓包应用。

我使用 libpcap 库。我的 iPhone 是 JB,我已经可以以 root 身份运行应用程序。更具体地说,我可以获得我的 net_interface :en0,但我无法捕获任何数据包。pcap_next() 始终返回 null。

这是我的代码:

-(IBAction)capture:(id)sender{
    char error_content[PCAP_ERRBUF_SIZE];
    char *net_interface=NULL;
    net_interface=pcap_lookupdev(error_content);
    NSString *devstr = [[NSString alloc] initWithUTF8String:net_interface];
    text1.text=devstr;

    pcap_t *pcap_handle;
    pcap_handle = pcap_open_live(net_interface, BUFSIZ, 0, 2, error_content);

    struct pcap_pkthdr packet_capture;
    const u_char *packet_flag;
    packet_flag= pcap_next(pcap_handle, &packet_capture);
    if (!packet_flag) {
        text2.text=@"capture failed";
    }
    else{
       NSString *length =[[NSString alloc]initWithFormat:@"the length of packet is         %d",packet_capture.len];
       text2.text=length;
       [length release];
    }
       pcap_close(pcap_handle);
    }
@end

如果有人对此有类似的经验或知道如何解决它,请通过 liangweidarth@gmail.com 与我联系,我将不胜感激。

最佳答案

packet_flag= pcap_next(pcap_handle, &packet_capture);
if (!packet_flag) {
    text2.text=@"capture failed";
}

引用 pcap_next() 手册页:

pcap_next() returns a pointer to the packet data on success, and returns NULL if an error occured, or if no packets were read from a live capture (if, for example, they were discarded because they didn't pass the packet filter, or if, on platforms that support a read timeout that starts before any packets arrive, the timeout expires before any packets arrive, or if the file descriptor for the capture device is in non-blocking mode and no packets were available to be read), or if no more packets are available in a ``savefile.'' Unfortunately, there is no way to determine whether an error occured or not.

iOS 与 OS X 一样,构建于 4.4-Lite 衍生操作系统之上,并使用 BPF; BPF 是一个支持在任何数据包到达之前开始的读取超时的数据包,假设您将 2 指定为 pcap_open_live() 的超时参数,则超时为 2 毫秒,因此,如果没有数据包到达在您调用 pcap_next() 后的 2 毫秒内,pcap_next() 将返回 NULL。

您使用 pcap_loop() 做出了正确的选择。 pcap_next() 不是一个很好的 API; pcap_next_ex() 更好,pcap_dispatch()pcap_loop() 也是。

关于iphone - iOS 上的 libpcap,pcap_next() 总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16228553/

相关文章:

iphone - 使用单点触控的推送通知服务

ios - 如何在不检查 SignerIdentity 的情况下检测应用程序是否已被破解?

iphone - UITableView 不显示数组中的数据

ios - 具有动态高度的 UICollectionViewCell 使用 NSURL 进行图像下载

ios - 适用于iOS的室内地图导航SDK

ios - 在 iOS 模拟器中测试 SMS

创建 pcap 文件

iphone - 为 TWTweetComposeViewController 设置完成处理程序时崩溃

linux - libpcap 到达间隔时间和调度程序

pcap - pcap魔数(Magic Number)0xc3d4a1b2是什么?