C++ Linux getpeername IP family
Posted
by
gln
on Stack Overflow
See other posts from Stack Overflow
or by gln
Published on 2011-01-09T08:34:04Z
Indexed on
2011/01/09
10:53 UTC
Read the original article
Hit count: 288
hi
In my Linux C++ application I'm using getpeername in order to get the peer IP. my problem is: when I enable the IPv6 on my machine the IP I got from the peer is with family IF_INET6 although it is IPv4.
code:
int GetSockPeerIP( int sock)
{
struct sockaddr_storage ss;
struct socklen_t salen = sizeof(ss);
struct sockaddr *sa;
memset(&ss,0,salen);
sa = (sockaddr *)&ss;
if(getpeername(sock,sa,&salen) != 0)
{
return -1;
}
char * ip=NULL:
if(sa->sa_family == AF_INET)
{
ip = inet_ntoa((struct sockaddr_in *)sa)->sin_addr);
}
else
{
//ip = how to convert IPv6 to char IP?
}
return 0;
}
how can I fix it?
thanks1
© Stack Overflow or respective owner