在 POSIX C 中获取自己的外部 IP 地址

发布于 2024-07-21 07:55:19 字数 179 浏览 8 评论 0原文

我希望获得自己的 IP 地址,以便将该信息发布到对等网络中。 在 POSIX/C 中,我们有 getaddrinfo(NULL, ...),但这似乎总是返回 INADDR_ANYINADDR_LOOPBACK,这对于我。

有什么建议么?

I'm looking to obtain my own IP address in order to publish that information in to a Peer-to-Peer network. In POSIX/C we have getaddrinfo(NULL, ...), but this always seems to returns INADDR_ANY or INADDR_LOOPBACK, which is useless to me.

Any suggestions?

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

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

发布评论

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

评论(6

素年丶 2024-07-28 07:55:19

问题之一是,即使您获得了本地 IP 地址,互联网上其他地方看到的 IP 地址也可能会有所不同,因为 NAT 不幸的是,这很常见。 使用 IPv6 可能会抑制这个问题,但现在它可能不是您的选择。

要获取公共 IP 地址,您需要为此目的制定协议,例如 STUNRFC 5389)。 有公共 STUN 服务器并且已经存在C 语言的 STUN 客户端

所有的点对点程序都有类似的问题,所以了解其他人在做什么,我强烈建议阅读 RFC 5128,“跨网络地址转换器 (NAT) 的点对点 (P2P) 通信状态”

One of the problems is that, even if you get the local IP address, the IP address seen by the rest of the Internet may be different, because of the kludge of NAT which is unfortunately very common. Using IPv6 probably will suppress this problem but it may not be an option for you, now.

To get the public IP address, you will need a protocol made for that purpose such as STUN (RFC 5389). There are public STUN servers and already existing STUN clients in C.

All the peer-to-peer programs have similar issues, so learn what others are doing, I strongly recommend reading RFC 5128, "State of Peer-to-Peer (P2P) Communication across Network Address Translators (NATs)".

¢好甜 2024-07-28 07:55:19

您所说的公共地址是指应用程序的同行可以与您联系的地址。 唯一完全证明的方法是连接到已知的“外部”对等点并让它将您的地址发送回给您。

By public address, you're saying, an address that your application's peers can contact you with. The only full-proof way is to connect to a known "external" peer and have it send your address back to you.

你如我软肋 2024-07-28 07:55:19

我并不是想让事情变得更复杂,但我应该指出一台计算机可以有多个 IP 地址。 通常每个网卡都会有一个。

更具体的询问方式是“如何找到连接到 foo.com.org 时用作源地址的 IP 地址”我喜欢 bortzmeyer 的回答:)

I'm not trying to make things more complicated though I should point out that a single computer can have several IP addresses. typically each network card will have one.

a more specific way to ask would be "how do I find the IP address that will be used as the source address when I connect to foo.com.org" I like bortzmeyer's answer for this :)

为你拒绝所有暧昧 2024-07-28 07:55:19

您是否检查了返回结构的 ai_next 字段?

getaddrinfo() 返回地址链接列表。

Did you check ai_next field of returned structure?

getaddrinfo() returns linked list of addresses.

友欢 2024-07-28 07:55:19

创建一个 UDP 套接字,bind() 到 INADDR_ANY,connect() 到互联网上的某个地址,然后在套接字上使用 getsockname() 来找出它被分配的地址。

另一个好的解决方案(如果网络协议支持的话)就是询问您的引导伙伴他们认为您的 IP 是什么。 这会自动处理 NAT。

Create a UDP socket, bind() to INADDR_ANY, connect() to some address on the internet, then use getsockname() on the socket to find out what address it was assigned.

Another good solution, if the network's protocol supports it, is to just ask your bootstrapping peers what they think your IP is. This takes care of NATs automatically.

傲世九天 2024-07-28 07:55:19

如果您知道主机的名称,则可以使用 gethostbyname 来获取 IP 地址。

If you know the name your host, you can use gethostbyname to get the ip address.

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