changeset 339:6efe3cbe7df5

Socket: provide a function to retrieve the address using getsockname
author David Demelier <markand@malikania.fr>
date Fri, 13 Mar 2015 16:57:54 +0100
parents 54dda199b2ef
children 1dc2e1adf247
files C++/modules/Socket/Socket.cpp C++/modules/Socket/Socket.h
diffstat 2 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/C++/modules/Socket/Socket.cpp	Wed Mar 11 09:45:28 2015 +0100
+++ b/C++/modules/Socket/Socket.cpp	Fri Mar 13 16:57:54 2015 +0100
@@ -119,6 +119,22 @@
 	m_state = SocketState::Opened;
 }
 
+SocketAddress Socket::address() const
+{
+#if defined(_WIN32)
+	int length;
+#else
+	socklen_t length;
+#endif
+
+	sockaddr_storage ss;
+
+	if (getsockname(m_handle, (sockaddr *)&ss, &length) == Error)
+		throw SocketError(SocketError::System, "getsockname");
+
+	return SocketAddress(ss, length);
+}
+
 void Socket::bind(const SocketAddress &address)
 {
 	const auto &sa = address.address();
--- a/C++/modules/Socket/Socket.h	Wed Mar 11 09:45:28 2015 +0100
+++ b/C++/modules/Socket/Socket.h	Fri Mar 13 16:57:54 2015 +0100
@@ -296,6 +296,14 @@
 	virtual ~Socket() = default;
 
 	/**
+	 * Get the local name. This is a wrapper of getsockname().
+	 *
+	 * @return the address
+	 * @throw SocketError on failures
+	 */
+	SocketAddress address() const;
+
+	/**
 	 * Set an option for the socket.
 	 *
 	 * @param level the setting level