• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:45:09 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我是iOS初学者,毕业设计是开发一个可以在iOS上抓包的app。

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

这是我的代码:

-(IBAction)captureid)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

如果有人对此有类似的经验或知道如何解决,如果您可以通过 [email protected] 与我联系,我将不胜感激。



Best Answer-推荐答案


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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap