Permalink
Browse files

Adds endpoint interrogation support.

  • Loading branch information...
Andre Louis Caron
Andre Louis Caron committed Feb 7, 2012
1 parent 509688f commit 1c5f55247faea7b1f36ebe61a185bdf1157e447c
Showing with 61 additions and 4 deletions.
  1. +47 −3 code/w32.net.ipv4/EndPoint.cpp
  2. +14 −1 code/w32.net.ipv4/EndPoint.hpp
@@ -5,6 +5,7 @@
#include <w32.net.ipv4/EndPoint.hpp>
#include <w32/Error.hpp>
+#include <w32.net./StreamSocket.hpp>
#include <iostream>
namespace w32 { namespace net { namespace ipv4 {
@@ -15,10 +16,9 @@ namespace w32 { namespace net { namespace ipv4 {
myData.sin_family = AF_INET;
}
- EndPoint::EndPoint ( const Data& address )
+ EndPoint::EndPoint ( const Data& data )
{
- ::ZeroMemory(&myData, sizeof(myData));
- myData.sin_family = AF_INET;
+ ::CopyMemory(&myData, &data, sizeof(myData));
}
EndPoint::EndPoint ( uint32 address, uint16 port )
@@ -92,6 +92,50 @@ namespace w32 { namespace net { namespace ipv4 {
myData.sin_addr.S_un.S_un_b.s_b4));
}
+ EndPoint& EndPoint::operator= ( const Data& data )
+ {
+ ::CopyMemory(&myData, &data, sizeof(myData));
+ return (*this);
+ }
+
+ EndPoint host ( const StreamSocket& socket )
+ {
+ const ::SOCKET handle = socket.handle();
+
+ ::sockaddr_in endpoint;
+ ::ZeroMemory(&endpoint, sizeof(endpoint));
+ endpoint.sin_family = AF_INET;
+
+ int size = sizeof(endpoint);
+ const int result = ::getsockname
+ (handle, reinterpret_cast<::sockaddr*>(&endpoint), &size);
+ if (result == SOCKET_ERROR)
+ {
+ const int error = ::WSAGetLastError();
+ UNCHECKED_WIN32C_ERROR(getsockname, error);
+ }
+ return (EndPoint(endpoint));
+ }
+
+ EndPoint peer ( const StreamSocket& socket )
+ {
+ const ::SOCKET handle = socket.handle();
+
+ ::sockaddr_in endpoint;
+ ::ZeroMemory(&endpoint, sizeof(endpoint));
+ endpoint.sin_family = AF_INET;
+
+ int size = sizeof(endpoint);
+ const int result = ::getpeername
+ (handle, reinterpret_cast<::sockaddr*>(&endpoint), &size);
+ if (result == SOCKET_ERROR)
+ {
+ const int error = ::WSAGetLastError();
+ UNCHECKED_WIN32C_ERROR(getsockname, error);
+ }
+ return (EndPoint(endpoint));
+ }
+
std::istream& operator>> ( std::istream& stream, EndPoint& value )
{
// parse the host.
@@ -12,6 +12,12 @@
#include <w32.net.ipv4/Address.hpp>
#include <iosfwd>
+namespace w32 { namespace net {
+
+ class StreamSocket;
+
+} }
+
namespace w32 { namespace net { namespace ipv4 {
/*!
@@ -31,7 +37,7 @@ namespace w32 { namespace net { namespace ipv4 {
/* construction. */
public:
EndPoint ();
- EndPoint ( const Data& address );
+ EndPoint ( const Data& data );
EndPoint ( uint32 address, uint16 port );
EndPoint ( uint8 b1, uint8 b2, uint8 b3, uint8 b4, uint16 port );
EndPoint ( const Address& address, uint16 port );
@@ -46,8 +52,15 @@ namespace w32 { namespace net { namespace ipv4 {
const Data& data () const;
uint16 port () const;
Address address () const;
+
+ /* operators. */
+ public:
+ EndPoint& operator= ( const Data& data );
};
+ EndPoint host ( const StreamSocket& socket );
+ EndPoint peer ( const StreamSocket& socket );
+
std::istream& operator>>
( std::istream& in, EndPoint& value );

0 comments on commit 1c5f552

Please sign in to comment.