getnameinfo: support ipv6 as well

This commit is contained in:
rofl0r 2015-12-06 13:01:56 +00:00
parent 4c3be5e1e3
commit 672bf7661d

View File

@ -423,17 +423,34 @@ int pc_getnameinfo(const struct sockaddr *sa, socklen_t salen,
INIT(); INIT();
PFUNC(); PFUNC();
char ip_buf[16];
if(!proxychains_resolver) { if(!proxychains_resolver) {
return true_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags); return true_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
} else { } else {
if(salen < sizeof(struct sockaddr_in) || SOCKFAMILY(*sa) != AF_INET) if(!salen || !(SOCKFAMILY(*sa) == AF_INET || SOCKFAMILY(*sa) == AF_INET6))
return EAI_FAMILY;
int v6 = SOCKFAMILY(*sa) == AF_INET6;
if(salen < (v6?sizeof(struct sockaddr_in6):sizeof(struct sockaddr_in)))
return EAI_FAMILY; return EAI_FAMILY;
if(hostlen) { if(hostlen) {
pc_stringfromipv4((unsigned char*) &(SOCKADDR_2(*sa)), ip_buf); unsigned char v4inv6buf[4];
if(snprintf(host, hostlen, "%s", ip_buf) >= hostlen) const void *ip = v6 ? (void*)&((struct sockaddr_in6*)sa)->sin6_addr
: (void*)&((struct sockaddr_in*)sa)->sin_addr;
unsigned scopeid = 0;
if(v6) {
if(is_v4inv6(&((struct sockaddr_in6*)sa)->sin6_addr)) {
memcpy(v4inv6buf, &((struct sockaddr_in6*)sa)->sin6_addr.s6_addr32[3], 4);
ip = v4inv6buf;
v6 = 0;
} else
scopeid = ((struct sockaddr_in6 *)sa)->sin6_scope_id;
}
if(!inet_ntop(v6?AF_INET6:AF_INET,ip,host,hostlen))
return EAI_OVERFLOW; return EAI_OVERFLOW;
if(scopeid) {
size_t l = strlen(host);
if(snprintf(host+l, hostlen-l, "%%%u", scopeid) >= hostlen-l)
return EAI_OVERFLOW;
}
} }
if(servlen) { if(servlen) {
if(snprintf(serv, servlen, "%d", ntohs(SOCKPORT(*sa))) >= servlen) if(snprintf(serv, servlen, "%d", ntohs(SOCKPORT(*sa))) >= servlen)