我正在使用pcap.h进行基本的数据包嗅探器。当我单元测试称为pcap_dispatch的函数时,我给了它非激活的接口和无效的接口。 pcap_dispatch返回-3,并且就pcap_dispatch的手册页而言,它应该只返回-2,-1或更多,但不会更少。是否可以检查一个接口是否在pcap中被激活?
当然,我可以在pcap_dispatch返回后处理这个-3的返回值,将其作为一些模糊的边缘情况处理,但是我想确保它不会返回这样的值。
只有当我知道如何检查接口是否被激活时,我才可以用assert做到这一点。那么,有人知道吗?
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);
}
}
错过了,谢谢 – user2738698 2015-02-05 19:56:09