diff options
-rw-r--r-- | include/Socket.h | 5 | ||||
-rw-r--r-- | lib/Socket.cc | 44 |
2 files changed, 47 insertions, 2 deletions
diff --git a/include/Socket.h b/include/Socket.h index 1b77bde..88a4abd 100644 --- a/include/Socket.h +++ b/include/Socket.h @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: Socket.h,v 1.19 2007-05-30 11:57:08 pixel Exp $ */ +/* $Id: Socket.h,v 1.20 2007-06-17 11:14:17 pixel Exp $ */ #ifndef __SOCKET_H__ #define __SOCKET_H__ @@ -44,6 +44,9 @@ class Socket : public Handle { virtual bool CanWrite(); virtual String GetName(); int GetPort(); + Uint32 GetAddr(); + int GetDistantPort(); + Uint32 GetDistantAddr(); void CloseWrite(); void CloseRead(); bool FinalizeConnect(); diff --git a/lib/Socket.cc b/lib/Socket.cc index 890457f..7c0dba1 100644 --- a/lib/Socket.cc +++ b/lib/Socket.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: Socket.cc,v 1.24 2007-05-30 11:57:10 pixel Exp $ */ +/* $Id: Socket.cc,v 1.25 2007-06-17 11:14:18 pixel Exp $ */ #ifdef _WIN32 #include <winsock2.h> @@ -212,6 +212,48 @@ int Socket::GetPort() { return r; } +Uint32 Socket::GetAddr() { + uint32_t r; + struct sockaddr_in localsocketaddr; + socklen_t locallen = sizeof(localsocketaddr); + + if (getsockname(GetHandle(), (struct sockaddr *) &localsocketaddr, &locallen)) { + return -1; + } + + r = ntohl(localsocketaddr.sin_addr.s_addr); + + return r; +} + +int Socket::GetDistantPort() { + int r; + struct sockaddr_in distantsockaddr; + socklen_t distantlen = sizeof(distantsockaddr); + + if (getpeername(GetHandle(), (struct sockaddr *) &distantsockaddr, &distantlen)) { + return -1; + } + + r = ntohs(distantsockaddr.sin_port); + + return r; +} + +Uint32 Socket::GetDistantAddr() { + uint32_t r; + struct sockaddr_in distantsockaddr; + socklen_t distantlen = sizeof(distantsockaddr); + + if (getpeername(GetHandle(), (struct sockaddr *) &distantsockaddr, &distantlen)) { + return -1; + } + + r = ntohl(distantsockaddr.sin_addr.s_addr); + + return r; +} + bool Socket::FinalizeConnect() { int r; socklen_t l = sizeof(r); |