gpt4 book ai didi

c++ - 是否可以检查接口(interface)是否在 pcap 中激活?

转载 作者:行者123 更新时间:2023-11-28 06:27:05 24 4
gpt4 key购买 nike

我正在使用 pcap.h 制作一个基本的数据包嗅探器。当我对调用 pcap_dispatch 的函数进行单元测试时,我给了它非激活接口(interface)和无效接口(interface)。 pcap_dispatch 返回 -3,就 pcap_dispatch 的手册页而言,它应该只返回 -2、-1 或更多,但绝不少于。

当然,我可以在 pcap_dispatch 返回后处理这个 -3 返回值,将其作为一些模糊的边缘情况处理,但我想确保它永远不会返回这样的值。

只有当我知道如何检查接口(interface)是否被激活时,我才能使用断言来做到这一点。那么,有人知道吗?


pcap_t *null_iface = NULL;
pcap_t *not_act = pcap_create( "valid", errmsg );
pcap_t *act = pcap_create( "valid", errmsg );
pcap_activate( act );
pcap_t *not_act_inv = pcap_create( "invalid", errmsg );
pcap_t *act_inv = pcap_create( "invalid", errmsg );
pcap_activate( act );
pcap_t *ifaces[ 5 ] = {
null_iface, not_act, act, not_act_inv, act_inv
};

for ( int a = 0; a < 5; a++ ) {
result = pcap_dispatch( iface[ a ], 1, handler, NULL );
// if one - got one
// elif zero - got nothing
// elif -1 - error ( doesn't fire, by the way )
// elif -2 - I manually stopped it
// else - impossible - but the assert fires
// - printing result is -3
// - for not_act, not_act_inv, and act_inv
else {
assert( false );
}
}

最佳答案

Is it possible to check if an interface is activated in pcap?

是的。您可以通过查看 pcap_activate() 的返回值来进行检查。 pcap_activate() 手册页说:

pcap_activate() returns 0 on success without warnings, PCAP_WARNING_PROMISC_NOTSUP on success on a device that doesn't support promiscuous mode if promiscuous mode was requested, PCAP_WARNING on success with any other warning, PCAP_ERROR_ACTIVATED if the handle has already been activated, PCAP_ERROR_NO_SUCH_DEVICE if the capture source specified when the handle was created doesn't exist, PCAP_ERROR_PERM_DENIED if the process doesn't have permission to open the capture source, PCAP_ERROR_RFMON_NOTSUP if monitor mode was specified but the capture source doesn't support monitor mode, PCAP_ERROR_IFACE_NOT_UP if the capture source is not up, and PCAP_ERROR if another error occurred. If PCAP_WARNING or PCAP_ERROR is returned, pcap_geterr() or pcap_perror() may be called with p as an argument to fetch or display a message describing the warning or error. If PCAP_WARNING_PROMISC_NOTSUP, PCAP_ERROR_NO_SUCH_DEVICE, or PCAP_ERROR_PERM_DENIED is returned, pcap_geterr() or pcap_perror() may be called with p as an argument to fetch or display an message giving additional details about the problem that might be useful for debugging the problem if it's unexpected.

顺便说一句,您还应该检查 pcap_create() 是否返回 NULL。

关于c++ - 是否可以检查接口(interface)是否在 pcap 中激活?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28346558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com