From be4bd4636d8e7e06ad5728ff1b398fbd0ab74d93 Mon Sep 17 00:00:00 2001
From: Julian Smart <julian@anthemion.co.uk>
Date: Sun, 11 Jan 2004 15:50:10 +0000
Subject: [PATCH] Applied patch [ 837515 ] wxIPaddress + docs patch (Ray
 Gilbert)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@25123 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/category.tex |   1 +
 docs/latex/wx/classes.tex  |   1 +
 docs/latex/wx/ipaddr.tex   | 146 +++++++++++++++++++++++++++++++++++++
 docs/latex/wx/ipvaddr.tex  |   2 +-
 docs/latex/wx/sckaddr.tex  |   1 +
 include/wx/sckaddr.h       |  92 ++++++++++++++++++-----
 src/common/sckaddr.cpp     |  92 ++++++++++++++++++-----
 7 files changed, 298 insertions(+), 37 deletions(-)
 create mode 100644 docs/latex/wx/ipaddr.tex

diff --git a/docs/latex/wx/category.tex b/docs/latex/wx/category.tex
index 4b5f68693c..5cc2615cc3 100644
--- a/docs/latex/wx/category.tex
+++ b/docs/latex/wx/category.tex
@@ -358,6 +358,7 @@ wxWindows provides its own classes for socket based networking.
 \begin{twocollist}\itemsep=0pt
 \twocolitem{\helpref{wxDialUpManager}{wxdialupmanager}}{Provides functions to check the status of network connection and to establish one}
 \twocolitem{\helpref{wxIPV4address}{wxipv4address}}{Represents an Internet address}
+\twocolitem{\helpref{wxIPaddress}{wxipaddress}}{Represents an Internet address}
 \twocolitem{\helpref{wxSocketBase}{wxsocketbase}}{Represents a socket base object}
 \twocolitem{\helpref{wxSocketClient}{wxsocketclient}}{Represents a socket client}
 \twocolitem{\helpref{wxSocketServer}{wxsocketserver}}{Represents a socket server}
diff --git a/docs/latex/wx/classes.tex b/docs/latex/wx/classes.tex
index 0e6433dafc..b432699b23 100644
--- a/docs/latex/wx/classes.tex
+++ b/docs/latex/wx/classes.tex
@@ -172,6 +172,7 @@
 \input ilayout.tex
 \input indlgevt.tex
 \input inputstr.tex
+\input ipaddr.tex
 \input ipvaddr.tex
 \input joystick.tex
 \input joyevent.tex
diff --git a/docs/latex/wx/ipaddr.tex b/docs/latex/wx/ipaddr.tex
new file mode 100644
index 0000000000..1e99a048be
--- /dev/null
+++ b/docs/latex/wx/ipaddr.tex
@@ -0,0 +1,146 @@
+% ----------------------------------------------------------------------------
+% CLASS: wxIPaddress
+% ----------------------------------------------------------------------------
+\section{\class{wxIPaddress}}\label{wxipaddress}
+
+wxIPaddress is an abstract base class for all internet protocol address 
+objects. Currently, only \helpref{wxIPV4address}{wxipv4address} 
+is implemented. An experimental implementation for IPV6, wxIPV6address,
+is being developed.
+
+\wxheading{Derived from}
+
+\helpref{wxSockAddress}{wxsockaddress}
+
+\wxheading{Include files}
+
+<wx/socket.h>
+
+% ----------------------------------------------------------------------------
+% MEMBERS
+% ----------------------------------------------------------------------------
+\latexignore{\rtfignore{\wxheading{Members}}}
+
+%
+% Hostname
+%
+
+\membersection{wxIPaddress::Hostname}
+
+\func{virtual bool}{Hostname}{\param{const wxString\&}{ hostname}}
+
+Set the address to {\it hostname}, which can be a host name
+or an IP-style address in a format dependent on implementation.
+
+\wxheading{Return value}
+
+Returns true on success, false if something goes wrong
+(invalid hostname or invalid IP address).
+
+%
+% Hostname
+%
+
+\membersection{wxIPaddress::Hostname}
+
+\func{virtual wxString}{Hostname}{\void}
+
+Returns the hostname which matches the IP address.
+
+%
+% IPAddress
+%
+
+\membersection{wxIPaddress::IPAddress}
+
+\func{virtual wxString}{IPAddress}{\void}
+
+Returns a wxString containing the IP address.
+
+%
+% Service
+%
+
+\membersection{wxIPaddress::Service}
+
+\func{virtual bool}{Service}{\param{const wxString\&}{ service}}
+
+Set the port to that corresponding to the specified {\it service}.
+
+\wxheading{Return value}
+
+Returns true on success, false if something goes wrong
+(invalid service).
+
+%
+% Service
+%
+
+\membersection{wxIPaddress::Service}
+
+\func{virtual bool}{Service}{\param{unsigned short}{ service}}
+
+Set the port to that corresponding to the specified {\it service}.
+
+\wxheading{Return value}
+
+Returns true on success, false if something goes wrong
+(invalid service).
+
+%
+% Service
+%
+
+\membersection{wxIPaddress::Service}
+
+\func{virtual unsigned short}{Service}{\void}
+
+Returns the current service.
+
+%
+% AnyAddress
+%
+
+\membersection{wxIPaddress::AnyAddress}\label{wxIPaddressanyaddress}
+
+\func{virtual bool}{AnyAddress}{\void}
+
+Internally, this is the same as setting the IP address
+to {\bf INADDR\_ANY}.
+
+On IPV4 implementations, 0.0.0.0
+
+On IPV6 implementations, ::
+
+\wxheading{Return value}
+
+Returns true on success, false if something went wrong.
+
+%
+% LocalHost
+%
+
+\membersection{wxIPaddress::LocalHost}\label{wxIPaddresslocalhost}
+
+\func{virtual bool}{LocalHost}{\void}
+
+Set address to localhost. 
+
+On IPV4 implementations, 127.0.0.1
+
+On IPV6 implementations, ::1
+
+\wxheading{Return value}
+
+Returns true on success, false if something went wrong.
+
+\membersection{wxIPaddress::LocalHost}\label{wxIPaddresslocalhost}
+
+\func{virtual bool}{IsLocalHost}{\void}
+
+Determines if current address is set to localhost. 
+
+\wxheading{Return value}
+
+Returns true if address is localhost, false if internet address.
+
diff --git a/docs/latex/wx/ipvaddr.tex b/docs/latex/wx/ipvaddr.tex
index d78ac3ffb4..fef4c521a7 100644
--- a/docs/latex/wx/ipvaddr.tex
+++ b/docs/latex/wx/ipvaddr.tex
@@ -5,7 +5,7 @@
 
 \wxheading{Derived from}
 
-\helpref{wxSockAddress}{wxsockaddress}
+\helpref{wxIPaddress}{wxipaddress}
 
 \wxheading{Include files}
 
diff --git a/docs/latex/wx/sckaddr.tex b/docs/latex/wx/sckaddr.tex
index 7ab64c2b7f..07c38fbe29 100644
--- a/docs/latex/wx/sckaddr.tex
+++ b/docs/latex/wx/sckaddr.tex
@@ -16,6 +16,7 @@ You are unlikely to need to use this class: only wxSocketBase uses it.
 \wxheading{See also}
 
 \helpref{wxSocketBase}{wxsocketbase}
+\helpref{wxIPaddress}{wxipaddress}
 \helpref{wxIPV4address}{wxipv4address}
 
 % ----------------------------------------------------------------------------
diff --git a/include/wx/sckaddr.h b/include/wx/sckaddr.h
index bd37455fcc..bc472331c9 100644
--- a/include/wx/sckaddr.h
+++ b/include/wx/sckaddr.h
@@ -52,24 +52,58 @@ private:
   void Init();
 };
 
-class WXDLLIMPEXP_NET wxIPV4address : public wxSockAddress {
+// Interface to an IP address (either IPV4 or IPV6)
+class WXDLLIMPEXP_NET wxIPaddress : public wxSockAddress {
+  DECLARE_ABSTRACT_CLASS(wxIPaddress)
+public:
+  wxIPaddress();
+  wxIPaddress(const wxIPaddress& other);
+  virtual ~wxIPaddress();
+
+  virtual bool Hostname(const wxString& name) = 0;
+  virtual bool Service(const wxString& name) = 0;
+  virtual bool Service(unsigned short port) = 0;
+
+  virtual bool LocalHost() = 0;
+  virtual bool IsLocalHost() const = 0;
+
+  virtual bool AnyAddress() = 0;
+
+  virtual wxString IPAddress() const = 0;
+
+  virtual wxString Hostname() const = 0;
+  virtual unsigned short Service() const = 0;
+};
+
+class WXDLLIMPEXP_NET wxIPV4address : public wxIPaddress {
   DECLARE_DYNAMIC_CLASS(wxIPV4address)
 public:
   wxIPV4address();
   wxIPV4address(const wxIPV4address& other);
   virtual ~wxIPV4address();
 
-  bool Hostname(const wxString& name);
+  // IPV4 name formats
+  //
+  //                    hostname
+  // dot format         a.b.c.d
+  virtual bool Hostname(const wxString& name);
   bool Hostname(unsigned long addr);
-  bool Service(const wxString& name);
-  bool Service(unsigned short port);
-  bool LocalHost();
-  bool AnyAddress();
+  virtual bool Service(const wxString& name);
+  virtual bool Service(unsigned short port);
+
+  // localhost (127.0.0.1)
+  virtual bool LocalHost();
+  virtual bool IsLocalHost() const;
 
-  wxString Hostname();
+  // any (0.0.0.0)
+  virtual bool AnyAddress();
+
+  virtual wxString Hostname() const;
   wxString OrigHostname() { return m_origHostname; }
-  unsigned short Service();
-  wxString IPAddress() const;
+  virtual unsigned short Service() const;
+
+  // a.b.c.d
+  virtual wxString IPAddress() const;
 
   virtual int Type() { return wxSockAddress::IPV4; }
   virtual wxSockAddress *Clone() const;
@@ -80,8 +114,12 @@ private:
   wxString m_origHostname;
 };
 
-#ifdef ENABLE_IPV6
-class WXDLLIMPEXP_NET wxIPV6address : public wxSockAddress {
+#if wxUSE_IPV6
+
+// Experimental Only:
+//
+// IPV6 has not yet been implemented in socket layer
+class WXDLLIMPEXP_NET wxIPV6address : public wxIPaddress {
   DECLARE_DYNAMIC_CLASS(wxIPV6address)
 private:
   struct sockaddr_in6 *m_addr;
@@ -90,19 +128,37 @@ public:
   wxIPV6address(const wxIPV6address& other);
   virtual ~wxIPV6address();
 
-  bool Hostname(const wxString& name);
+  // IPV6 name formats
+  //
+  //                          hostname
+  //                          3ffe:ffff:0100:f101:0210:a4ff:fee3:9566 
+  // compact (base85)         Itu&-ZQ82s>J%s99FJXT
+  // compressed format        ::1
+  // ipv4 mapped              ::ffff:1.2.3.4
+  virtual bool Hostname(const wxString& name);
+
   bool Hostname(unsigned char addr[16]);
-  bool Service(const wxString& name);
-  bool Service(unsigned short port);
-  bool LocalHost();
+  virtual bool Service(const wxString& name);
+  virtual bool Service(unsigned short port);
+
+  // localhost (0000:0000:0000:0000:0000:0000:0000:0001 (::1))
+  virtual bool LocalHost();
+  virtual bool IsLocalHost() const;
 
-  wxString Hostname() const;
-  unsigned short Service() const;
+  // any (0000:0000:0000:0000:0000:0000:0000:0000 (::))
+  virtual bool AnyAddress();
+
+  // 3ffe:ffff:0100:f101:0210:a4ff:fee3:9566 
+  virtual wxString IPAddress() const;
+
+  virtual wxString Hostname() const;
+  virtual unsigned short Service() const;
 
   virtual int Type() { return wxSockAddress::IPV6; }
   virtual wxSockAddress *Clone() const { return new wxIPV6address(*this); }
 };
-#endif
+
+#endif // wxUSE_IPV6
 
 #if defined(__UNIX__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
 #include <sys/socket.h>
diff --git a/src/common/sckaddr.cpp b/src/common/sckaddr.cpp
index 8f667937dc..3ac7e22d9f 100644
--- a/src/common/sckaddr.cpp
+++ b/src/common/sckaddr.cpp
@@ -42,16 +42,17 @@
 #include "wx/sckaddr.h"
 
 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
-IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxSockAddress)
-#ifdef ENABLE_IPV6
-IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxSockAddress)
+IMPLEMENT_ABSTRACT_CLASS(wxIPaddress, wxSockAddress)
+IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxIPaddress)
+#if wxUSE_IPV6
+IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxIPaddress)
 #endif
 #if defined(__UNIX__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
 #endif
 
 // ---------------------------------------------------------------------------
-// wxIPV4address
+// wxSockAddress
 // ---------------------------------------------------------------------------
 
 void wxSockAddress::Init()
@@ -101,16 +102,35 @@ void wxSockAddress::Clear()
   m_address = GAddress_new();
 }
 
+// ---------------------------------------------------------------------------
+// wxIPaddress
+// ---------------------------------------------------------------------------
+
+wxIPaddress::wxIPaddress()
+            : wxSockAddress()
+{
+}
+
+wxIPaddress::wxIPaddress(const wxIPaddress& other)
+            : wxSockAddress(other)
+{
+}
+
+wxIPaddress::~wxIPaddress()
+{
+}
+
 // ---------------------------------------------------------------------------
 // wxIPV4address
 // ---------------------------------------------------------------------------
 
 wxIPV4address::wxIPV4address()
+             : wxIPaddress()
 {
 }
 
 wxIPV4address::wxIPV4address(const wxIPV4address& other)
-             : wxSockAddress(other)
+             : wxIPaddress(other)
 {
 }
 
@@ -155,12 +175,17 @@ bool wxIPV4address::LocalHost()
   return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
 }
 
+bool wxIPV4address::IsLocalHost() const
+{
+  return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
+}
+
 bool wxIPV4address::AnyAddress()
 {
   return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
 }
 
-wxString wxIPV4address::Hostname()
+wxString wxIPV4address::Hostname() const
 {
    char hostname[1024];
 
@@ -169,7 +194,7 @@ wxString wxIPV4address::Hostname()
    return wxString::FromAscii(hostname);
 }
 
-unsigned short wxIPV4address::Service()
+unsigned short wxIPV4address::Service() const
 {
   return GAddress_INET_GetPort(m_address);
 }
@@ -199,18 +224,18 @@ bool wxIPV4address::operator==(wxIPV4address& addr)
 	return false;
 }
 
-#if 0
+#if wxUSE_IPV6
 // ---------------------------------------------------------------------------
 // wxIPV6address
 // ---------------------------------------------------------------------------
 
 wxIPV6address::wxIPV6address()
-  : wxSockAddress()
+  : wxIPaddress()
 {
 }
 
 wxIPV6address::wxIPV6address(const wxIPV6address& other)
-             : wxSockAddress(other)
+             : wxIPaddress(other)
 {
 }
 
@@ -220,17 +245,22 @@ wxIPV6address::~wxIPV6address()
 
 bool wxIPV6address::Hostname(const wxString& name)
 {
-  return (GAddress_INET_SetHostName(m_address, name.fn_str()) == GSOCK_NOERROR);
+  if (name == wxT(""))
+  {
+    wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
+    return FALSE;
+  }
+  return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
 }
 
-bool wxIPV6address::Hostname(unsigned char addr[16])
+bool wxIPV6address::Hostname(unsigned char[16] WXUNUSED(addr))
 {
   return TRUE;
 }
 
-bool wxIPV6address::Service(const char *name)
+bool wxIPV6address::Service(const wxString& name)
 {
-  return (GAddress_INET_SetPortName(m_address, name.fn_str()) == GSOCK_NOERROR);
+  return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
 }
 
 bool wxIPV6address::Service(unsigned short port)
@@ -243,17 +273,43 @@ bool wxIPV6address::LocalHost()
   return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
 }
 
-const wxString& wxIPV6address::Hostname()
+bool wxIPV6address::IsLocalHost() const
+{
+  return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
+}
+
+bool wxIPV6address::AnyAddress()
+{
+  return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
+}
+
+wxString wxIPV6address::IPAddress() const
+{	
+	unsigned long raw =  GAddress_INET_GetHostAddress(m_address);
+	return wxString::Format(
+		_T("%u.%u.%u.%u"),
+		(unsigned char)(raw & 0xff),
+		(unsigned char)((raw>>8) & 0xff),
+		(unsigned char)((raw>>16) & 0xff),
+		(unsigned char)((raw>>24) & 0xff)
+		);
+}
+
+wxString wxIPV6address::Hostname() const
 {
-  return wxString(GAddress_INET_GetHostName(m_address));
+   char hostname[1024];
+
+   hostname[0] = 0;
+   GAddress_INET_GetHostName(m_address, hostname, 1024);
+   return wxString::FromAscii(hostname);
 }
 
-unsigned short wxIPV6address::Service()
+unsigned short wxIPV6address::Service() const
 {
   return GAddress_INET_GetPort(m_address);
 }
 
-#endif // 0
+#endif // wxUSE_IPV6
 
 #if defined(__UNIX__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
 
-- 
2.50.0

