Mercurial > code
changeset 491:c03f84bdabe9
Socket: add ip() in Ip class
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 18 Nov 2015 14:31:22 +0100 |
parents | d2b2071a9b85 |
children | b5b2bb02412a |
files | modules/sockets/sockets.h |
diffstat | 1 files changed, 27 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/modules/sockets/sockets.h Sun Nov 15 13:30:28 2015 +0100 +++ b/modules/sockets/sockets.h Wed Nov 18 14:31:22 2015 +0100 @@ -1833,6 +1833,33 @@ return ntohs(m_sin.sin_port); } + + /** + * Get the IP address in textual form. + * + * @return the address + * @throw Error on errors + */ + std::string ip() const + { + std::string result; + + if (m_domain == AF_INET6) { + result.resize(INET_ADDRSTRLEN); + + if (!inet_ntop(AF_INET6, &m_sin6.sin6_addr, &result[0], INET_ADDRSTRLEN)) { + throw Error{Error::System, "inet_ntop"}; + } + } else { + result.resize(INET6_ADDRSTRLEN); + + if (!inet_ntop(AF_INET, &m_sin.sin_addr, &result[0], INET6_ADDRSTRLEN)) { + throw Error{Error::System, "inet_ntop"}; + } + } + + return result; + } }; #if !defined(_WIN32)