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) evutil_make_socket_nonblocking(fd) 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?