diff options
author | emaste <emaste@FreeBSD.org> | 2015-02-09 01:44:09 +0000 |
---|---|---|
committer | emaste <emaste@FreeBSD.org> | 2015-02-09 01:44:09 +0000 |
commit | d61b076ede88b56f3372a55e7d1eac6a9d717120 (patch) | |
tree | a8f4b3abea3e6937e60728991c736e6e3d322fc1 /source/Host/common/SocketAddress.cpp | |
parent | 0c2019f4ca6b2dc6d710f6bb16a0e3ed10271531 (diff) | |
download | FreeBSD-src-d61b076ede88b56f3372a55e7d1eac6a9d717120.zip FreeBSD-src-d61b076ede88b56f3372a55e7d1eac6a9d717120.tar.gz |
Import LLDB as of upstream SVN 228549 (git 39760838)
Diffstat (limited to 'source/Host/common/SocketAddress.cpp')
-rw-r--r-- | source/Host/common/SocketAddress.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/source/Host/common/SocketAddress.cpp b/source/Host/common/SocketAddress.cpp index 6231631..fd7fbac 100644 --- a/source/Host/common/SocketAddress.cpp +++ b/source/Host/common/SocketAddress.cpp @@ -21,6 +21,58 @@ // Other libraries and framework includes // Project includes +// WindowsXP needs an inet_ntop implementation +#ifdef _WIN32 + +#ifndef INET6_ADDRSTRLEN // might not be defined in older Windows SDKs +#define INET6_ADDRSTRLEN 46 +#endif + +// TODO: implement shortened form "::" for runs of zeros +const char* inet_ntop(int af, const void * src, + char * dst, socklen_t size) +{ + if (size==0) + { + return nullptr; + } + + switch (af) + { + case AF_INET: + { + { + const char* formatted = inet_ntoa(*static_cast<const in_addr*>(src)); + if (formatted && strlen(formatted) < size) + { + strncpy(dst, formatted, size); + return dst; + } + } + return nullptr; + case AF_INET6: + { + char tmp[INET6_ADDRSTRLEN] = {0}; + const uint16_t* src16 = static_cast<const uint16_t*>(src); + int full_size = _snprintf(tmp, sizeof(tmp), + "%x:%x:%x:%x:%x:%x:%x:%x", + ntohs(src16[0]), ntohs(src16[1]), ntohs(src16[2]), ntohs(src16[3]), + ntohs(src16[4]), ntohs(src16[5]), ntohs(src16[6]), ntohs(src16[7]) + ); + if (full_size < static_cast<int>(size)) + { + strncpy(dst,tmp,size); + return dst; + } + return nullptr; + } + } + } + return nullptr; +} +#endif + + using namespace lldb_private; //---------------------------------------------------------------------- @@ -124,6 +176,26 @@ SocketAddress::SetFamily (sa_family_t family) #endif } +std::string +SocketAddress::GetIPAddress () const +{ + char str[INET6_ADDRSTRLEN] = {0}; + switch (GetFamily()) + { + case AF_INET: + if (inet_ntop(GetFamily(), &m_socket_addr.sa_ipv4.sin_addr, str, sizeof(str))) + { + return str; + } + case AF_INET6: + if (inet_ntop(GetFamily(), &m_socket_addr.sa_ipv6.sin6_addr, str, sizeof(str))) + { + return str; + } + } + return ""; +} + uint16_t SocketAddress::GetPort () const { |