getaddrinfo:如果指定了节点名,将以哪种方式忽略AI_PASSIVE? [英] getaddrinfo: in what way is AI_PASSIVE ignored if the nodename is specified?

查看:396
本文介绍了getaddrinfo:如果指定了节点名,将以哪种方式忽略AI_PASSIVE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

引用 getaddrinfo 的规范:

如果指定了AI_PASSIVE标志,则返回的地址信息应适用于绑定套接字,以接受指定服务的传入连接.在这种情况下,如果nodename参数为null,则对于IPv4地址,套接字地址结构的IP地址部分应设置为INADDR_ANY,对于IPv6地址应设置为IN6ADDR_ANY_INIT.

If the AI_PASSIVE flag is specified, the returned address information shall be suitable for use in binding a socket for accepting incoming connections for the specified service. In this case, if the nodename argument is null, then the IP address portion of the socket address structure shall be set to INADDR_ANY for an IPv4 address or IN6ADDR_ANY_INIT for an IPv6 address.

这很有道理.如果指定AI_PASSIVE,则可以在返回的地址上bind()/listen()/accept().如果nodename为空,则返回的地址将绑定到所有网络接口,因此您可以使用计算机的任何IP地址(例如,以太网LAN IP,Wi-Fi LAN IP,环回地址等).摘要:在服务器上使用AI_PASSIVE(用于TCP套接字).

This makes sense. If you specify AI_PASSIVE, you can bind()/listen()/accept() on the returned address. If nodename is null, the returned address binds to all network interfaces so you can use any of the computer's IP addresses (e.g. ethernet LAN IP, Wi-Fi LAN IP, loopback address, etc.). Summary: use AI_PASSIVE on the server (for a TCP socket).

继续:

如果未指定AI_PASSIVE标志,则返回的地址信息应适合于调用connect()(用于连接模式协议)或调用connect()sendto()sendmsg()(用于无连接协议).在这种情况下,如果nodename参数为null,则套接字地址结构的IP地址部分应设置为环回地址.

If the AI_PASSIVE flag is not specified, the returned address information shall be suitable for a call to connect() (for a connection-mode protocol) or for a call to connect(), sendto(), or sendmsg() (for a connectionless protocol). In this case, if the nodename argument is null, then the IP address portion of the socket address structure shall be set to the loopback address.

这也很有意义.如果不指定AI_PASSIVE,则可以在返回的地址上connect()/sendto()/sendmsg().如果nodename为null,则获得回送地址.摘要:请勿在客户端(对于TCP套接字)上使用AI_PASSIVE.

This also makes sense. If you don't specify AI_PASSIVE, you can connect()/sendto()/sendmsg() on the returned address. If nodename is null, you get the loopback address. Summary: don't use AI_PASSIVE on the client (for a TCP socket).

继续:

如果nodename参数不为null,则应忽略AI_PASSIVE标志.

The AI_PASSIVE flag shall be ignored if the nodename argument is not null.

嗯? 忽略 AI_PASSIVE标志甚至意味着什么?从前两个引号开始,听起来像AI_PASSIVE仅用于确定您是否获得适合服务器/客户端使用的地址,在这两种情况下,听起来像都是空的nodename只是为您提供了一个INADDR_ANY地址(用于服务器)或回送地址(用于客户端). 忽略 AI_PASSIVE标志有什么用?

Huh? What does it even mean to ignore the AI_PASSIVE flag? From the first two quotes, it sounds like AI_PASSIVE is only used to determine whether you get addresses suitable for server/client use, and in both of those cases it sounds like a null nodename just gives you a INADDR_ANY address (for server) or loopback address (for client). What good does it do to ignore the AI_PASSIVE flag?

最后一个引用的真正含义是什么?它如何影响前两个引号?

What does this last quote really mean? How does it affect the first two quotes?

推荐答案

gettaddrinfo(2)的Linux手册页对它的描述略有不同(并且更加一致):

The Linux manpage for gettaddrinfo(2) describes it slightly differently (and a bit more consistently):

如果在 hints.ai_flags 中指定了 AI_PASSIVE 标志,并且 node 为NULL,则返回的套接字地址将适合绑定(2个),该套接字将接受(2个)连接.返回的套接字地址将包含通配符地址" [...].如果 node 不为NULL,则将忽略 AI_PASSIVE 标志.

If the AI_PASSIVE flag is specified in hints.ai_flags, and node is NULL, then the returned socket addresses will be suitable for bind(2)ing a socket that will accept(2) connections. The returned socket address will contain the "wildcard address" [...]. If node is not NULL, then the AI_PASSIVE flag is ignored.

如果未在 hints.ai_flags 中设置 AI_PASSIVE 标志,则返回的套接字地址将适用于 connect (2 ),发送到(2)或 sendmsg (2).如果 node 为NULL,则网络地址将设置为回送接口地址[...].

If the AI_PASSIVE flag is not set in hints.ai_flags, then the returned socket addresses will be suitable for use with connect(2), sendto(2), or sendmsg(2). If node is NULL, then the network address will be set to the loopback interface address [...].

当然,除了Linux手册外,Linux手册页没有权威性,但我将其作为广泛使用的解释来提供.特别要注意的是,它指定仅当AI_PASSIVE指定为并且 node 为NULL时,才获得服务器套接字.对于忽略" AI_PASSIVE意味着什么,仍然存在一些歧义,但是我认为这仍然指向Linux的解释:如果 node 不是,您总是会获得适合于客户端套接字的地址信息.空.

Of course, the Linux manpage is not authoritative other than for Linux, but I offer it as a widely-used interpretation. In particular, note that it specifies that you get a server socket only if AI_PASSIVE is given and node is NULL. That still leaves some ambiguity as to what it means to "ignore" AI_PASSIVE, but I think it nevertheless points to the interpretation taken by Linux: you always get address info suitable for a client socket if node is not NULL.

您也许可以这样进行合理化:应忽略AI_PASSIVE标志"应解释为如果设置了AI_PASSIVE标志,则应忽略该事实".即,应将其视为未设置标志的方式进行处理.我认为,该语句与设置AI_PASSIVE时的行为描述在同一段落中(在官方文档中,而不仅仅是Linux联机帮助页中)都在同一段落中显示,这进一步证明了这一点.

You could perhaps rationalize it this way: "The AI_PASSIVE flag shall be ignored" should be interpreted as "if the AI_PASSIVE flag is set then that fact shall be ignored". That is, it shall be handled as if the flag were not set. I think that's additionally supported by the fact that this statement occurs in the same paragraph with the description of the behavior when AI_PASSIVE is set (in the official docs too, not just the Linux manpage).

这篇关于getaddrinfo:如果指定了节点名,将以哪种方式忽略AI_PASSIVE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆