IPv6 Binding Errors

I have a server implementation where I need 2 separate sockets - 1 IPv4 socket socket listening on a specific IPv4 address and server X port, and IPv6 socket listening on a specific IPv6 address and the same server port X. IPv4 and IPv6 addresses are on the same interface.

memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(v4addr); sin.sin_port = htons(tcp_port); 

I use evconnlistener_new_bind to create an ipv4 socket and bind to it. For an IPv6 listener, the code is as follows.

  memset(&sin6, 0, sizeof(sin6)); sin6.sin6_family = AF_INET6; memcpy(sin6.sin6_addr.s6_addr, v6addr_bytes, IPV6_ADDR_LEN); sin6.sin6_port = htons(tcp_port); fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); evutil_make_socket_nonblocking(fd) setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&on, sizeof(on)) setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&on, sizeof(on)) evutil_make_listen_socket_reuseable(fd) /* Libevent call to set SO_REUSEADDR */ evutil_make_socket_nonblocking(fd) /* Libevent call to set fd non-blocking */ bind(fd, (const struct sockaddr *)&sin6, sizeof(sin6)) 

When I bind my fd to a specific ipv6 address, I see interrupt bindings with interruptions.

bind v6 failed sin6 3ffe :: a00: 513 - errno 99 - Unable to assign the requested address

I tried to connect to gdb, but every time I connect to gdb, the binding is successful.

I am not sure why I see this problem. Can anybody help?

+6
source share
1 answer

By default, when a socket is bound to a TCP port, the port remains reserved for one minute when the socket is closed - this is called the TCP state TIME_WAIT . TIME_WAIT avoids some race conditions that could lead to data corruption, but it is usually safe to ignore TIME_WAIT on the server side.

This is done by setting the socket SO_REUSEADDR :

 int one = 1; rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) 
0
source

Source: https://habr.com/ru/post/979110/


All Articles