Skip to content

Commit

Permalink
Fixed SockaddrToString to format as 4dotIP if unknown (#299)
Browse files Browse the repository at this point in the history
* Fixed SockaddrToString to format as 4dotIP if unknown
  • Loading branch information
ethouris authored and rndi committed Mar 15, 2018
1 parent 98e86ac commit b47cf11
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions srtcore/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}


Expand Down

0 comments on commit b47cf11

Please sign in to comment.