C:从 TCP 和 UDP 套接字获取主机名?
我不太熟悉连接协议。
我正在使用以下代码来检查 connect() 以便获取主机名:
#ifndef NI_MAXHOST
#define NI_MAXHOST 1025
#endif
int error;
char hostname[NI_MAXHOST] = "";
error = getnameinfo(serv_addr, addrlen, hostname, NI_MAXHOST, NULL, 0, 0);
if (error !=0) {
ALogTCP(@"coudldn't resolve hostname or internal connect");
[pool release];
return orig__connect(sockfd, serv_addr, addrlen);
}
if (error == 0) {
ALogTCP(@"hostname: %s", hostname);
NSString *hostFirst = [NSString stringWithCString:hostname];
}
如果我挂接到 sendto() 中,我可以使用“相同”代码来获取主机名(以便我可以检查 UDP)吗?
提前致谢。
I'm not really familiar with the connection protocols.
I'm using the following code to examine connect() so I can get the hostname:
#ifndef NI_MAXHOST
#define NI_MAXHOST 1025
#endif
int error;
char hostname[NI_MAXHOST] = "";
error = getnameinfo(serv_addr, addrlen, hostname, NI_MAXHOST, NULL, 0, 0);
if (error !=0) {
ALogTCP(@"coudldn't resolve hostname or internal connect");
[pool release];
return orig__connect(sockfd, serv_addr, addrlen);
}
if (error == 0) {
ALogTCP(@"hostname: %s", hostname);
NSString *hostFirst = [NSString stringWithCString:hostname];
}
can I use the "same" code to get the hostname if I hook into sendto() (so I can examine UDP)?
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够这样做,因为正如您在示例代码中使用的那样,
sendto()
被传递了一个struct sockaddr
。当您“挂钩”
sendto()
时,我假设您将使用仅影响您自己的源代码的宏来执行此操作,而不是像中间驱动程序之类的东西。DNS 名称解析取决于发送 UDP 数据包,因此如果您要在足够低的级别“挂钩”
sendto()
,您的解决方案将无限递归,因为它会查找传出 DNS 查找数据包的主机名...You should be able to do so because
sendto()
is passed astruct sockaddr
just as you are using in your example code.When you "hook"
sendto()
I assume you will do so with a macro which affects only your own source code -- as opposed to something like an intermediating driver.The DNS name resolution depends on sending UDP packets, so if you were to "hook"
sendto()
at a low enough level, your solution would recurse infinitely as it looked up the hostnames for outgoing DNS lookup packets...