我希望将连接套接字的端口和IP收集到网络内核扩展(NKE)中的套接字过滤器。
我试图在sf_bind, sf_connect_in, sf_connect_out
收回struct sflt_filter
的功能。
在sf_bind(void *cookie,socket_t so,const struct sockaddr *to)
回调。它为to
param指定“套接字的本地地址将绑定到”所以我使用了代码:
printf("Local port:<%u> and IP<%04X>",
ntohs(((struct sockaddr_in *)to)->sin_port),
ntohs(((struct sockaddr_in*)to)->sin_addr.s_addr));
有时IP和端口都是空的,有时两者中的一个出现。
在我尝试过的相同和其他回调代码中:
unsigned char szAddrStr[256];
struct sockaddr_in addrLcl;
sock_getsockname(so, (struct sockaddr *)&addrLcl, sizeof(addrLcl));
inet_ntop(AF_INET, &addrLcl.sin_addr, (char *)szAddrStr, sizeof(szAddrStr));
printf("IP String <%s> Port Hex:<%X>", szAddrStr, ntohs(addrLcl.sin_port));
但是这段代码总是让IP和端口为空。
有谁有任何想法?还是另一种方式来获得它?提前致谢。
关于你的第一个代码片段:如果没有完整的回调代码示例,很难说出什么是错误的,但是在尝试打印sin_port和sin_addr作为端口和ip之前,你应该检查sa_family字段“to”。
我认为您要寻找的值是AF_INET和AF_INET6。
关于你的第二个代码片段:你应该再次检查sa_family;
此外,在绑定回调中在“so”上调用sock_getsockname可能是错误的,因为在相关操作之前调用“回调”并且您不会为“so”填充sockaddr。
有tcplognke样本(它曾经是苹果开发者的资源,但他们并不擅长关心它们),你应该查看它的例子。
有一个关于Apple计划制作NKE技术deprecated soon的信息,所以要小心使用它:)