EAFNOSUPPORT
Linux / POSIXERRORCommonNetworkHIGH confidence
Address Family Not Supported by Protocol
Production Risk
Common during IPv4/IPv6 migration; ensure socket and address families match.
What this means
EAFNOSUPPORT (errno 97) is returned when a socket address of the wrong address family is passed to an operation, or when the kernel does not support IPv6.
Why it happens
- 1Connecting an AF_INET socket to an AF_INET6 address
- 2IPv6 not compiled into the kernel
- 3Passing an AF_INET6 sockaddr to a bind() on an AF_INET socket
How to reproduce
Connecting an IPv4 socket to an IPv6 address.
trigger — this will error
trigger — this will error
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in6 addr6 = { .sin6_family = AF_INET6, ... };
// Wrong address family for this socket
connect(sockfd, (struct sockaddr*)&addr6, sizeof(addr6));
// Returns -1, errno = EAFNOSUPPORTexpected output
connect: Address family not supported by protocol (EAFNOSUPPORT)
Fix
Match socket and address address families
WHEN When EAFNOSUPPORT is returned on connect/bind
Match socket and address address families
// Create IPv6 socket for IPv6 addresses
int sockfd = socket(AF_INET6, SOCK_STREAM, 0);
struct sockaddr_in6 addr6 = {
.sin6_family = AF_INET6,
.sin6_port = htons(80),
};
inet_pton(AF_INET6, "::1", &addr6.sin6_addr);
connect(sockfd, (struct sockaddr*)&addr6, sizeof(addr6));Why this works
The socket address family (AF_INET vs AF_INET6) must match the socket created.
Sources
Official documentation ↗
Linux Programmer Manual connect(2)
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev