简体   繁体   English

针对IPv4和IPv6地址的connect()行为

[英]Behaviour of connect() for IPv4 and IPv6 addresses

I create a socket with following params: 我用以下参数创建一个套接字:

hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo(serverName, port, &hints, &res);
sFd = socket (res_node->ai_family, SOCK_STREAM, 0);

I then make connect calls for IPv4 and Ipv6 server addresses which may or may not be present. 然后,我对可能存在或可能不存在的IPv4和Ipv6服务器地址进行连接调用。 When the server is not reachable, it has different behavior in case of IPv4 and IPv6 addresses. 当服务器不可访问时,对于IPv4和IPv6地址,它具有不同的行为。

In case of v4 it gets stuck for a very long time (some internal default timeout) before returning error. 对于v4,在返回错误之前,它会卡住很长时间(某些内部默认超时)。 However, in case of IPv6 the behavior is changed. 但是,在使用IPv6的情况下,行为已更改。 For some addresses the call returns error immediately eg address like 1111::22, whereas for some it takes longer eg fec0:60:69bc:94:211:25ff:fec4:6 but still lot lesser than IPv4 timeout. 对于某些地址,呼叫会立即返回错误,例如1111 :: 22之类的地址,而对于某些地址,它会花费更长的时间,例如fec0:60:69bc:94:211:25ff:fec4:6,但仍比IPv4超时小得多。

Can anyone explain the difference in behavior? 谁能解释一下行为上的差异?

When the route lookup to the particular target host (either protocol) yields unreachable/prohibited/reject/etc., connect() can immediately fail. 当到特定目标主机(任一协议)的路由查找产生不可达/被禁止/拒绝等情况时,connect()可能立即失败。 Otherwise it has to send probes (ARP/NDISC, and potentially L4 packets like TCP SYN/SCTP INIT). 否则,它必须发送探测(ARP / NDISC,以及可能的L4数据包,例如TCP SYN / SCTP INIT)。

Route lookup example of a system without a route to global IPv6 unicast: 没有到全局IPv6单播路由的系统的路由查找示例:

$ ip r g 2a01::1
unreachable 2a01::1 from :: dev lo  table unspec  proto kernel  src fe80::224:beff:fec2:7f16  metric -1  error -101 hoplimit 255

Note the difference to an IPv6-connected host: 请注意与连接IPv6的主机的区别:

$ ip r g 2a01::1
2a01::1 from :: via fe80::2d0:3ff:fe93:d123 dev eth0  src 2001:527:500:770f:19e:84ff:fe9e:878  metric 0 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM