I assume this is how to determine if the incoming connection is localhost if you have a peer connection in struct sockaddr_in6 obtained from getpeername , for example:
struct sockaddr_in6 peer; socklen_t len = sizeof(peer); getpeername( sock, (struct sockaddr *) &peer, &len);
From there, you can add your own struct sockaddr_in6 with the address localhost ::1 and compare the memory for equality:
struct sockaddr_in6 localhost; memset(localhost.sin6_addr.s6_addr, 0, 16); localhost.sin6_addr.s6_addr[15] = 1; if( memcmp(peer.sin6_addr.s6_addr, localhost.sin6_addr.s6_addr, 16) == 0) printf("localhost!\n");
Or you can create an array of bytes that matches the localhost address:
static const unsigned char localhost_bytes[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; if( memcmp(peer.sin6_addr.s6_addr, localhost_bytes, 16) == 0) printf("localhost!\n");
And watch out for the displayed local IPv4 host, ::ffff:127.0.0.1 . You may also need to check this out.
static const unsigned char mapped_ipv4_localhost[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x7f, 0, 0, 1 }; if( memcmp(peer.sin6_addr.s6_addr, mapped_ipv4_localhost, 16) == 0) printf("mapped IPv4 localhost!\n");