diff --git a/srtcore/api.h b/srtcore/api.h index bcdb7f042..a065f0362 100644 --- a/srtcore/api.h +++ b/srtcore/api.h @@ -267,30 +267,34 @@ friend class CRendezvousQueue; // Debug support inline std::string SockaddrToString(const sockaddr* sadr) { - void* addr = + void* addr = sadr->sa_family == AF_INET ? - (void*)&((sockaddr_in*)sadr)->sin_addr + (void*)&((sockaddr_in*)sadr)->sin_addr : sadr->sa_family == AF_INET6 ? - (void*)&((sockaddr_in6*)sadr)->sin6_addr + (void*)&((sockaddr_in6*)sadr)->sin6_addr : 0; - // (cast to (void*) is required because otherwise the 2-3 arguments - // of ?: operator would have different types, which isn't allowed in C++. + // (cast to (void*) is required because otherwise the 2-3 arguments + // of ?: operator would have different types, which isn't allowed in C++. if ( !addr ) return "unknown:0"; - std::ostringstream output; - char hostbuf[1024]; - if (!getnameinfo(sadr, sizeof(*sadr), hostbuf, 1024, NULL, 0, NI_NAMEREQD)) - { - output << hostbuf; - } - else - { - output << "unknown"; - } - - output << ":" << ntohs(((sockaddr_in*)sadr)->sin_port); // TRICK: sin_port and sin6_port have the same offset and size - return output.str(); + std::ostringstream output; + char hostbuf[1024]; + if (!getnameinfo(sadr, sizeof(*sadr), hostbuf, 1024, NULL, 0, NI_NAMEREQD)) + { + output << hostbuf; + } + else + { + if (inet_ntop(sadr->sa_family, addr, hostbuf, 1024) == NULL) + { + strcpy(hostbuf, "unknown"); + } + output << hostbuf; + } + + output << ":" << ntohs(((sockaddr_in*)sadr)->sin_port); // TRICK: sin_port and sin6_port have the same offset and size + return output.str(); }