Connect () returns an "invalid argument" with ipv6 address

I have this simple client-server application pair. The code is pretty simple, I only use new, recommended methods like getaddinfo etc., and everything works fine for ipv4. Even for ipv6 loopback (:: 1) it works. Problems start when it comes to some other ipv6 addresses ... I have two machines on the network, everything works fine when I pass their ipv4 addresses, but when I give the address of my ipv6 client, I get an error message when connection: invalid argument, hey, don't I know that? I do! When I try to ping6 this ipv6 address, I get the same error:

connect: Invalid argument

But there is a way to overcome this block - you need to choose an interface with the -I switch, and since then everything has been working smoothly. But how can I do the same in my client application? What should I do? My client code is as follows:

struct addrinfo hints; struct addrinfo *server; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; int status; if((status = getaddrinfo(argv[1], argv[2], &hints, &server) != 0)) { perror("getaddrinfo error"); return 1; } int sock_fd; struct addrinfo *ptr; for(ptr=server;ptr!=NULL;ptr=ptr->ai_next) { if( (sock_fd = socket(ptr->ai_family,ptr->ai_socktype,ptr->ai_protocol)) == -1) { perror("socket error"); continue; } if( connect(sock_fd, ptr->ai_addr,ptr->ai_addrlen) == -1 ) { perror("connect error"); continue; } break; } 
+6
source share
3 answers

Addresses starting with ff... are multicast addresses. Connecting a stream to a multicast address does not work.

Addresses starting with fe80... are local local links that have an interface identifier associated with them. Try to find the sockaddr returned from getaddrinfo , is the scope field full?

+5
source

You need to specify the interface for ping IPv6 (i.e. -I eth0):

 ping6 -I eth0 fe80::208:54ff:fe34:22ae 

Using local local links for ping IP traffic requires determining which device should send / receive a packet - each device has a local link address.

Trying without this will result in an error message like:

 --> # ping6 fe80::208:54ff:fe34:22ae connect: Invalid argument 

In this case, you need to additionally specify the interface, as shown here:

 --> # ping6 -I eth0 fe80::208:54ff:fe34:22ae PING fe80::208:54ff:fe34:22ae(fe80::208:54ff:fe34:22ae) from fe80::208:54ff:fe34:22ae eth0: 56 data bytes 64 bytes from fe80::208:54ff:fe34:22ae: icmp_seq=0 ttl=64 time=0.027 ms 64 bytes from fe80::208:54ff:fe34:22ae: icmp_seq=1 ttl=64 time=0.030 ms 64 bytes from fe80::208:54ff:fe34:22ae: icmp_seq=2 ttl=64 time=0.036 ms 

One of these approaches that you must follow in your APP client application.

+6
source

My recommendation is that you enable IP6 in the interface / network connection, also release ip4 if you still have an error.

In my Linux Box, this also happened when I had an ip4 interface and my application tried to use the ip4 interface with ip6 settings. The same should be for windows.

If something is unclear to ask.

0
source

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


All Articles