如何使用NSHost获取外部IP地址?

时间:2021-01-27 07:09:33

I'm trying to find my external IP address, but I get local ones only, behind the NAT.

我正在尝试找到我的外部IP地址,但我只在NAT后面获得本地IP地址。

NSArray *addresses = [[NSHost currentHost] addresses];

Is there a way to print out the public address? Using NSHost is it a good idea?

有没有办法打印公共地址?使用NSHost是一个好主意吗?

3 个解决方案

#1


There may not always be a reliable way to get at your public IP, but the DNSService API in OS X will use UPnP and/or the NAT port mapping protocol to get the public IP (amongst other things). The code illustrating how to use the C API would be a bit large (50-60 lines) to show here, but there's some Apple sample code which implements a nice ObjC wrapper around the functionality, and even offers a pair of functions to directly return UInt32 and NSString representations of the public IP address.

可能并不总是有一种可靠的方式来获取您的公共IP,但OS X中的DNSService API将使用UPnP和/或NAT端口映射协议来获取公共IP(以及其他内容)。说明如何使用C API的代码有点大(50-60行),但是有一些Apple示例代码实现了一个很好的ObjC包装功能,甚至提供了一对函数来直接返回公共IP地址的UInt32和NSString表示。

The relevant code itself is located here, but you're probably best off just downloading the zip file and including PortMapper.h and PortMapper.m in your project and using them directly. Then all you'd need to do is:

相关代码本身位于此处,但您可能最好只下载zip文件并在项目中包含PortMapper.h和PortMapper.m并直接使用它们。然后你需要做的就是:

NSString * publicAddressString = [PortMapper findPublicAddress];

#2


If you ever get to look at a network topology chart for a major organization, do so. It's enlightening. The whole point of NAT, firewalls and all that other "black magic" is to allow the network to manage addresses (including protecting you) without your knowledge.

如果您要查看主要组织的网络拓扑图,请执行此操作。这很有启发性。 NAT,防火墙以及所有其他“黑魔法”的全部意义在于让网络在您不知情的情况下管理地址(包括保护您)。

There are only three ways in which I've been able to reliably (and reliable is a relative concept here) get the external-facing IP address of a server.

我只能通过三种方式可靠地(并且可靠的是这里的相对概念)获取服务器的面向外部的IP地址。

The first is to as the network gods themselves (and make sure you refer to them as gods when asking, this will assist you in getting the information). Sometimes (not always), it's a simple mapping of the top bytes of your IP address whilst retaining the low-order bytes. Sometimes it's more complex, but still follows rules that you can use. Just keep in mind these rules can change at any time.

第一个是作为网络神灵自己(并确保在询问时将它们称为神,这将有助于您获取信息)。有时(并非总是),它是IP地址的顶部字节的简单映射,同时保留低位字节。有时它更复杂,但仍然遵循您可以使用的规则。请记住,这些规则可以随时更改。

The second is to have a box outside of your network which you can query and it, in turn, can let you know your IP address.

第二种是在网络外部有一个框,您可以查询它,反过来,它可以让您知道您的IP地址。

The third is to specifically attach to an outside DNS server (not your corporate one) to retrieve the information.

第三种是专门附加到外部DNS服务器(不是您的公司服务器)来检索信息。

Of course, you should question the need to know your external IP address. The whole point of DNS is to avoid having to worry too much about IP addresses and just refer to machines by the domain names.

当然,您应该质疑是否需要知道您的外部IP地址。 DNS的重点是避免过多担心IP地址,只需通过域名引用机器即可。

#3


There is no reliable way to get your public IP. Depending on your network topology, you may not be able to depend on the outgoing address for a number of reasons: IP address pools, dynamic routes, multiple layers of NAT, proxy servers, etc.

没有可靠的方法来获取您的公共IP。根据您的网络拓扑,您可能由于多种原因而无法依赖传出地址:IP地址池,动态路由,多层NAT,代理服务器等。

#1


There may not always be a reliable way to get at your public IP, but the DNSService API in OS X will use UPnP and/or the NAT port mapping protocol to get the public IP (amongst other things). The code illustrating how to use the C API would be a bit large (50-60 lines) to show here, but there's some Apple sample code which implements a nice ObjC wrapper around the functionality, and even offers a pair of functions to directly return UInt32 and NSString representations of the public IP address.

可能并不总是有一种可靠的方式来获取您的公共IP,但OS X中的DNSService API将使用UPnP和/或NAT端口映射协议来获取公共IP(以及其他内容)。说明如何使用C API的代码有点大(50-60行),但是有一些Apple示例代码实现了一个很好的ObjC包装功能,甚至提供了一对函数来直接返回公共IP地址的UInt32和NSString表示。

The relevant code itself is located here, but you're probably best off just downloading the zip file and including PortMapper.h and PortMapper.m in your project and using them directly. Then all you'd need to do is:

相关代码本身位于此处,但您可能最好只下载zip文件并在项目中包含PortMapper.h和PortMapper.m并直接使用它们。然后你需要做的就是:

NSString * publicAddressString = [PortMapper findPublicAddress];

#2


If you ever get to look at a network topology chart for a major organization, do so. It's enlightening. The whole point of NAT, firewalls and all that other "black magic" is to allow the network to manage addresses (including protecting you) without your knowledge.

如果您要查看主要组织的网络拓扑图,请执行此操作。这很有启发性。 NAT,防火墙以及所有其他“黑魔法”的全部意义在于让网络在您不知情的情况下管理地址(包括保护您)。

There are only three ways in which I've been able to reliably (and reliable is a relative concept here) get the external-facing IP address of a server.

我只能通过三种方式可靠地(并且可靠的是这里的相对概念)获取服务器的面向外部的IP地址。

The first is to as the network gods themselves (and make sure you refer to them as gods when asking, this will assist you in getting the information). Sometimes (not always), it's a simple mapping of the top bytes of your IP address whilst retaining the low-order bytes. Sometimes it's more complex, but still follows rules that you can use. Just keep in mind these rules can change at any time.

第一个是作为网络神灵自己(并确保在询问时将它们称为神,这将有助于您获取信息)。有时(并非总是),它是IP地址的顶部字节的简单映射,同时保留低位字节。有时它更复杂,但仍然遵循您可以使用的规则。请记住,这些规则可以随时更改。

The second is to have a box outside of your network which you can query and it, in turn, can let you know your IP address.

第二种是在网络外部有一个框,您可以查询它,反过来,它可以让您知道您的IP地址。

The third is to specifically attach to an outside DNS server (not your corporate one) to retrieve the information.

第三种是专门附加到外部DNS服务器(不是您的公司服务器)来检索信息。

Of course, you should question the need to know your external IP address. The whole point of DNS is to avoid having to worry too much about IP addresses and just refer to machines by the domain names.

当然,您应该质疑是否需要知道您的外部IP地址。 DNS的重点是避免过多担心IP地址,只需通过域名引用机器即可。

#3


There is no reliable way to get your public IP. Depending on your network topology, you may not be able to depend on the outgoing address for a number of reasons: IP address pools, dynamic routes, multiple layers of NAT, proxy servers, etc.

没有可靠的方法来获取您的公共IP。根据您的网络拓扑,您可能由于多种原因而无法依赖传出地址:IP地址池,动态路由,多层NAT,代理服务器等。