获取到达数据包的IP地址

发布于 2024-07-21 14:57:30 字数 572 浏览 4 评论 0原文

创建一个输入套接字

因此,我正在使用CFSocketCreateWithSocketSignature (NULL, &signature, kCFSocketDataCallBack, receiveData, &socket_context);

在 receiveData 函数(正确调用)中,我尝试使用CFDataRef address 参数来查找此“包裹”的发件人地址。

发送方 PC 的 IP 地址为 192.168.1.2。

我正在使用

<代码> 字符缓冲区[INET_ADDRSTRLEN]; NSLog([NSString stringWithFormat:@"incoming connection from: %s", inet_ntop(AF_INET, address, buffer, INET_ADDRSTRLEN)]);

但是我总是从日志中获取 192.6.105.48 。 是什么赋予了? 我真的不太喜欢 Cocoa/C 中的网络,所以非常感谢任何帮助/解释。

提前致谢!

So I'm creating an input socket using

CFSocketCreateWithSocketSignature (NULL, &signature, kCFSocketDataCallBack, receiveData, &socket_context);

Within the receiveData function (that gets called properly) I'm trying to use the CFDataRef address parameter to find out the sender address of the this "package".

The IP address of the sender PC is at 192.168.1.2.

I'm using


char buffer[INET_ADDRSTRLEN];
NSLog([NSString stringWithFormat:@"incoming connection from: %s", inet_ntop(AF_INET, address, buffer, INET_ADDRSTRLEN)]);

However I always get the 192.6.105.48 out of the log. What gives? I'm really not big on networking in Cocoa/C so any help / explanation is very much appreciated.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江心雾 2024-07-28 14:57:30

这是我为我的一个项目实现的 NSData 类别。 使用 CFDataRef 和 NSData 之间的免费桥,您可以使用以下类。

@implementation NSData (Additions)

- (int)port
{
    int port;
    struct sockaddr *addr;

    addr = (struct sockaddr *)[self bytes];
    if(addr->sa_family == AF_INET)
        // IPv4 family
        port = ntohs(((struct sockaddr_in *)addr)->sin_port);
    else if(addr->sa_family == AF_INET6)
        // IPv6 family
        port = ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
    else
        // The family is neither IPv4 nor IPv6. Can't handle.
        port = 0;

    return port;
}


- (NSString *)host
{
    struct sockaddr *addr = (struct sockaddr *)[self bytes];
    if(addr->sa_family == AF_INET) {
        char *address = 
          inet_ntoa(((struct sockaddr_in *)addr)->sin_addr);
        if (address)
            return [NSString stringWithCString: address];
    }
    else if(addr->sa_family == AF_INET6) {
        struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
        char straddr[INET6_ADDRSTRLEN];
        inet_ntop(AF_INET6, &(addr6->sin6_addr), straddr, 
            sizeof(straddr));
        return [NSString stringWithCString: straddr];
    }
    return nil;
}

@end

Here is a Category class of NSData that I've implemented for one of my projects. Using the toll-free bridge between CFDataRef and NSData you can use the following class.

@implementation NSData (Additions)

- (int)port
{
    int port;
    struct sockaddr *addr;

    addr = (struct sockaddr *)[self bytes];
    if(addr->sa_family == AF_INET)
        // IPv4 family
        port = ntohs(((struct sockaddr_in *)addr)->sin_port);
    else if(addr->sa_family == AF_INET6)
        // IPv6 family
        port = ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
    else
        // The family is neither IPv4 nor IPv6. Can't handle.
        port = 0;

    return port;
}


- (NSString *)host
{
    struct sockaddr *addr = (struct sockaddr *)[self bytes];
    if(addr->sa_family == AF_INET) {
        char *address = 
          inet_ntoa(((struct sockaddr_in *)addr)->sin_addr);
        if (address)
            return [NSString stringWithCString: address];
    }
    else if(addr->sa_family == AF_INET6) {
        struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
        char straddr[INET6_ADDRSTRLEN];
        inet_ntop(AF_INET6, &(addr6->sin6_addr), straddr, 
            sizeof(straddr));
        return [NSString stringWithCString: straddr];
    }
    return nil;
}

@end
想你只要分分秒秒 2024-07-28 14:57:30

网络流量可能在到达接收端的途中经过 NAT/伪装。 您为发送方 PC 提供的 IP 地址位于 RFC 1918 专用/未路由网络之一中,而您看到的 IP 地址位于路由网络块上。

It could be that the network-traffic is NAT/masqueraded on the way to the receiving end. The IP address you've given for the sender PC is in one of the RFC 1918 private/unrouted networks, whereas the IP address you're seeing is on a routed network-block.

俏︾媚 2024-07-28 14:57:30

好吧,除非出现编程错误,假设 Mac 平台,检查两台机器上 ifconfig 的输出,并使用 route get 从两台机器检查路由; 一直到 tcpdump

顺便说一句,拥有 BSD 层在 Mac 上确实有好处 - 如果您不知道如何使用工具,只需 man 它,例如 man tcpdump

Well, barring programming mistakes and assuming Mac platform, check output of ifconfig on both machines, check the routing with route get <IP> from both machines; and all the way down to tcpdump.

By the way, having BSD layer really pays off on Mac - if you don't know how to use a tool just man it, like man tcpdump.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文