aboutsummaryrefslogtreecommitdiff
path: root/cvs/cvsweb.cgi/code/nel/src/net/inet_address.cpp?rev=1.38&content-type=text/x-cvsweb-markup&sortby=date/index.html
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2018-08-11 20:21:34 +0200
committerneodarz <neodarz@neodarz.net>2018-08-11 20:21:34 +0200
commit0ea5fc66924303d1bf73ba283a383e2aadee02f2 (patch)
tree2568e71a7ccc44ec23b8bb3f0ff97fb6bf2ed709 /cvs/cvsweb.cgi/code/nel/src/net/inet_address.cpp?rev=1.38&content-type=text/x-cvsweb-markup&sortby=date/index.html
downloadnevrax-website-self-hostable-0ea5fc66924303d1bf73ba283a383e2aadee02f2.tar.xz
nevrax-website-self-hostable-0ea5fc66924303d1bf73ba283a383e2aadee02f2.zip
Initial commit
Diffstat (limited to 'cvs/cvsweb.cgi/code/nel/src/net/inet_address.cpp?rev=1.38&content-type=text/x-cvsweb-markup&sortby=date/index.html')
-rw-r--r--cvs/cvsweb.cgi/code/nel/src/net/inet_address.cpp?rev=1.38&content-type=text/x-cvsweb-markup&sortby=date/index.html452
1 files changed, 452 insertions, 0 deletions
diff --git a/cvs/cvsweb.cgi/code/nel/src/net/inet_address.cpp?rev=1.38&content-type=text/x-cvsweb-markup&sortby=date/index.html b/cvs/cvsweb.cgi/code/nel/src/net/inet_address.cpp?rev=1.38&content-type=text/x-cvsweb-markup&sortby=date/index.html
new file mode 100644
index 00000000..4986879c
--- /dev/null
+++ b/cvs/cvsweb.cgi/code/nel/src/net/inet_address.cpp?rev=1.38&content-type=text/x-cvsweb-markup&sortby=date/index.html
@@ -0,0 +1,452 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML>
+<HEAD><style> A { color:black }</style>
+<!-- hennerik CVSweb $Revision: 1.93 $ -->
+<TITLE>code/nel/src/net/inet_address.cpp - view - 1.38</TITLE></HEAD>
+<BODY BGCOLOR="#eeeeee">
+<table width="100%" border=0 cellspacing=0 cellpadding=1 bgcolor="#aaaaaa"><tr valign=bottom><td><a href="inet_address.cpp?sortby=date"><IMG SRC="http://www.nevrax.org/inc/img/picto-up.gif" ALT="[BACK]" BORDER="0" WIDTH="14" HEIGHT="13"></a> <b>Return to <A HREF="inet_address.cpp?sortby=date">inet_address.cpp</A>
+ CVS log</b> <IMG SRC="http://www.nevrax.org/inc/img/picto-news.gif" ALT="[TXT]" BORDER="0" WIDTH="13" HEIGHT="15"></td><td align=right><IMG SRC="http://www.nevrax.org/inc/img/picto-dir.gif" ALT="[DIR]" BORDER="0" WIDTH="15" HEIGHT="13"> <b>Up to <a href="/cvs/cvsweb.cgi/?sortby=date">Nevrax</a> / <a href="/cvs/cvsweb.cgi/code/?sortby=date">code</a> / <a href="/cvs/cvsweb.cgi/code/nel/?sortby=date">nel</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/?sortby=date">src</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/net/?sortby=date">net</a></b></td></tr></table><HR noshade><table width="100%"><tr><td bgcolor="#ffffff">File: <a href="/cvs/cvsweb.cgi/?sortby=date">Nevrax</a> / <a href="/cvs/cvsweb.cgi/code/?sortby=date">code</a> / <a href="/cvs/cvsweb.cgi/code/nel/?sortby=date">nel</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/?sortby=date">src</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/net/?sortby=date">net</a> / <a href="/cvs/cvsweb.cgi/code/nel/src/net/inet_address.cpp?sortby=date">inet_address.cpp</a>&nbsp;(<A HREF="/cvs/cvsweb.cgi/~checkout~/code/nel/src/net/inet_address.cpp?rev=1.38&amp;sortby=date" target="cvs_checkout" onClick="window.open('/cvs/cvsweb.cgi/~checkout~/code/nel/src/net/inet_address.cpp?rev=1.38','cvs_checkout','resizeable,scrollbars');"><b>download</b></A>)<BR>
+Revision <B>1.38</B>, <i>Fri Dec 28 10:17:21 2001 UTC</i> (7 months ago) by <i>lecroart</i>
+<BR>Branch: <b>MAIN</b>
+<BR>CVS Tags: <b>georges_v2, HEAD</b><BR>Changes since <b>1.37: +3 -6
+ lines</b><PRE>
+ADDED: support of precompilated header on visual
+</PRE>
+</td></tr></table><HR noshade><PRE>/** \file inet_address.cpp
+ * Class CInetAddress (IP address + port)
+ *
+ * $Id: inet_address.cpp,v 1.38 2001/12/28 10:17:21 lecroart Exp $
+ */
+
+/* Copyright, 2000 Nevrax Ltd.
+ *
+ * This file is part of NEVRAX NEL.
+ * NEVRAX NEL is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+
+ * NEVRAX NEL is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with NEVRAX NEL; see the file COPYING. If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+#include &quot;stdnet.h&quot;
+
+#include &quot;nel/net/inet_address.h&quot;
+#include &quot;nel/net/sock.h&quot;
+
+#ifdef NL_OS_WINDOWS
+
+#include &lt;winsock2.h&gt;
+
+#elif defined NL_OS_UNIX
+
+#include &lt;unistd.h&gt;
+#include &lt;sys/socket.h&gt;
+#include &lt;arpa/inet.h&gt;
+#include &lt;netinet/in.h&gt;
+#include &lt;netdb.h&gt;
+
+#define WSAGetLastError() 0
+#define SOCKET_ERROR -1
+
+#endif
+
+
+using namespace std;
+
+
+namespace NLNET
+{
+
+bool CInetAddress::RetrieveNames = true;
+
+
+/*
+ * Constructor
+ */
+CInetAddress::CInetAddress() :
+ &nbsp;_Valid( false )
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;init();
+ &nbsp; &nbsp; &nbsp; &nbsp;_SockAddr-&gt;sin_port = 0; // same as htons(0)
+ &nbsp; &nbsp; &nbsp; &nbsp;memset( &amp;_SockAddr-&gt;sin_addr, 0, sizeof(in_addr) ); // same as htonl(INADDR_ANY)
+}
+
+
+/*
+ * Constructor with ip address, port=0
+ */
+CInetAddress::CInetAddress( const in_addr *ip )
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;init();
+ &nbsp; &nbsp; &nbsp; &nbsp;_SockAddr-&gt;sin_port = 0;
+ &nbsp; &nbsp; &nbsp; &nbsp;memcpy( &amp;_SockAddr-&gt;sin_addr, ip, sizeof(in_addr) );
+}
+
+
+/*
+ * Alternate constructor (calls setByName())
+ */
+CInetAddress::CInetAddress( const std::string&amp; hostName, uint16 port ) :
+ &nbsp; &nbsp; &nbsp; &nbsp;_Valid( false )
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;init();
+ &nbsp; &nbsp; &nbsp; &nbsp;setPort( port );
+ &nbsp; &nbsp; &nbsp; &nbsp;setByName( hostName );
+}
+
+
+/*
+ * Alternate constructor (calls setNameAndPort())
+ */
+CInetAddress::CInetAddress( const std::string&amp; hostNameAndPort ) :
+ &nbsp; &nbsp; &nbsp; &nbsp;_Valid( false )
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;init();
+ &nbsp; &nbsp; &nbsp; &nbsp;setNameAndPort( hostNameAndPort );
+}
+
+
+/*
+ * Copy constructor
+ */
+CInetAddress::CInetAddress( const CInetAddress&amp; other ) :
+ &nbsp; &nbsp; &nbsp; &nbsp;_Valid( false )
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;init();
+ &nbsp; &nbsp; &nbsp; &nbsp;_HostName = other._HostName;
+ &nbsp; &nbsp; &nbsp; &nbsp;memcpy( _SockAddr, other._SockAddr, sizeof( *_SockAddr ) );
+ &nbsp; &nbsp; &nbsp; &nbsp;_Valid = other._Valid;
+}
+
+
+/*
+ * Assignment operator
+ */
+CInetAddress&amp; CInetAddress::operator=( const CInetAddress&amp; other )
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;_HostName = other._HostName;
+ &nbsp; &nbsp; &nbsp; &nbsp;memcpy( _SockAddr, other._SockAddr, sizeof( *_SockAddr ) );
+ &nbsp; &nbsp; &nbsp; &nbsp;_Valid = other._Valid;
+ &nbsp; &nbsp; &nbsp; &nbsp;return *this;
+}
+
+
+/*
+ * Comparison == operator
+ */
+bool operator==( const CInetAddress&amp; a1, const CInetAddress&amp; a2 )
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;// Compares the sockaddr structure except the last 8 bytes equal to zero.
+ &nbsp; &nbsp; &nbsp; &nbsp;return ( memcmp( a1._SockAddr, a2._SockAddr, sizeof(sockaddr_in)-8 ) == 0 );
+}
+
+
+/*
+ * Comparison &lt; operator
+ */
+bool operator&lt;( const CInetAddress&amp; a1, const CInetAddress&amp; a2 )
+{
+#ifdef NL_OS_WINDOWS
+ &nbsp; &nbsp; &nbsp; &nbsp;if ( a1._SockAddr-&gt;sin_addr.S_un.S_addr == a2._SockAddr-&gt;sin_addr.S_un.S_addr )
+ &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return ( a1.port() &lt; a2.port() );
+ &nbsp; &nbsp; &nbsp; &nbsp;}
+ &nbsp; &nbsp; &nbsp; &nbsp;else
+ &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return ( a1._SockAddr-&gt;sin_addr.S_un.S_addr &lt; a2._SockAddr-&gt;sin_addr.S_un.S_addr );
+ &nbsp; &nbsp; &nbsp; &nbsp;}
+#elif defined NL_OS_UNIX
+ &nbsp; &nbsp; &nbsp; &nbsp;if ( a1._SockAddr-&gt;sin_addr.s_addr == a2._SockAddr-&gt;sin_addr.s_addr )
+ &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return ( a1.port() &lt; a2.port() );
+ &nbsp; &nbsp; &nbsp; &nbsp;}
+ &nbsp; &nbsp; &nbsp; &nbsp;else
+ &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return ( a1._SockAddr-&gt;sin_addr.s_addr &lt; a2._SockAddr-&gt;sin_addr.s_addr );
+ &nbsp; &nbsp; &nbsp; &nbsp;}
+#endif &nbsp;
+}
+
+
+/*
+ * Constructor contents
+ */
+void CInetAddress::init()
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;CSock::initNetwork(); /// \todo cado: handle exception (because init() is called within a constructor)
+ &nbsp; &nbsp; &nbsp; &nbsp;
+ &nbsp; &nbsp; &nbsp; &nbsp;_SockAddr = new sockaddr_in;
+ &nbsp; &nbsp; &nbsp; &nbsp;_SockAddr-&gt;sin_family = AF_INET;
+ &nbsp; &nbsp; &nbsp; &nbsp;memset( _SockAddr-&gt;sin_zero, 0, 8 );
+}
+
+
+/*
+ * Destructor
+ */
+CInetAddress::~CInetAddress()
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;delete _SockAddr;
+ &nbsp; &nbsp; &nbsp; &nbsp;// _Valid = false;
+}
+
+
+/*
+ * Sets hostname and port (ex: www.nevrax.com:80)
+ */
+void CInetAddress::setNameAndPort( const std::string&amp; hostNameAndPort )
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;int pos = hostNameAndPort.find_first_of (':');
+ &nbsp; &nbsp; &nbsp; &nbsp;if (pos != string::npos)
+ &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setPort( atoi(hostNameAndPort.substr(pos + 1).c_str()) );
+ &nbsp; &nbsp; &nbsp; &nbsp;}
+ &nbsp; &nbsp; &nbsp; &nbsp;else
+ &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setPort( 0 );
+ &nbsp; &nbsp; &nbsp; &nbsp;}
+
+ &nbsp; &nbsp; &nbsp; &nbsp;// if pos == -1, it will copy all the string
+ &nbsp; &nbsp; &nbsp; &nbsp;setByName( hostNameAndPort.substr (0, pos) );
+}
+
+
+/*
+ * Resolves a name
+ */
+CInetAddress&amp; CInetAddress::setByName( const std::string&amp; hostName )
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;// Try to convert directly for addresses such as a.b.c.d
+ &nbsp; &nbsp; &nbsp; &nbsp;in_addr iaddr;
+#ifdef NL_OS_WINDOWS
+ &nbsp; &nbsp; &nbsp; &nbsp;iaddr.S_un.S_addr = inet_addr( hostName.c_str() );
+ &nbsp; &nbsp; &nbsp; &nbsp;if ( iaddr.S_un.S_addr == INADDR_NONE )
+#elif defined NL_OS_UNIX
+ &nbsp; &nbsp; &nbsp; &nbsp;iaddr.s_addr = inet_addr( hostName.c_str() );
+ &nbsp; &nbsp; &nbsp; &nbsp;if ( iaddr.s_addr == INADDR_NONE )
+#endif
+ &nbsp; &nbsp; &nbsp; &nbsp;{
+
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Otherwise use the traditional DNS look-up
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hostent *phostent = gethostbyname( hostName.c_str() );
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( phostent == NULL )
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_Valid = false;
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nldebug( &quot;LNETL0: Network error: resolution of hostname '%s' failed&quot;, hostName.c_str() );
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// return *this;
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw ESocket( (string(&quot;Hostname resolution failed for &quot;)+hostName).c_str() );
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_HostName = string( phostent-&gt;h_name );
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memcpy( &amp;_SockAddr-&gt;sin_addr, phostent-&gt;h_addr, sizeof(in_addr) );
+ &nbsp; &nbsp; &nbsp; &nbsp;}
+ &nbsp; &nbsp; &nbsp; &nbsp;else
+ &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_HostName = hostName;
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memcpy( &amp;_SockAddr-&gt;sin_addr, &amp;iaddr, sizeof(iaddr) );
+ &nbsp; &nbsp; &nbsp; &nbsp;}
+ &nbsp; &nbsp; &nbsp; &nbsp;_Valid = true;
+ &nbsp; &nbsp; &nbsp; &nbsp;return *this;
+}
+
+
+/*
+ * Sets port
+ */
+void CInetAddress::setPort( uint16 port )
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;_SockAddr-&gt;sin_port = htons( port );
+
+}
+
+
+/* Sets internal socket address directly (contents is copied).
+ * It also retrieves the host name if CInetAddress::RetrieveNames is true.
+ */
+void CInetAddress::setSockAddr( const sockaddr_in* saddr )
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;memcpy( _SockAddr, saddr, sizeof(*saddr) );
+
+ &nbsp; &nbsp; &nbsp; &nbsp;// Get host name
+ &nbsp; &nbsp; &nbsp; &nbsp;// Warning: when it can't find it, it take more than 4 seconds
+ &nbsp; &nbsp; &nbsp; &nbsp;if ( CInetAddress::RetrieveNames )
+ &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hostent *phostent = gethostbyaddr( (char*)&amp;saddr-&gt;sin_addr.s_addr, 4, &nbsp;AF_INET );
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( phostent == NULL )
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_HostName = ipAddress();
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_HostName = string( phostent-&gt;h_name );
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
+ &nbsp; &nbsp; &nbsp; &nbsp;}
+ &nbsp; &nbsp; &nbsp; &nbsp;_Valid = true;
+}
+
+
+/*
+ * Returns if object (address and port) is valid
+ */
+bool CInetAddress::isValid() const
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;return ( _Valid &amp;&amp; _SockAddr-&gt;sin_port!=0 ); // same as ntohs(0)
+}
+
+
+/*
+ * Returns internal socket address (read only)
+ */
+const sockaddr_in *CInetAddress::sockAddr() const
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;return _SockAddr;
+}
+
+
+/*
+ * Returns internal IP address
+ */
+uint32 CInetAddress::internalIPAddress() const
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;return _SockAddr-&gt;sin_addr.s_addr;
+}
+
+
+/*
+ * Returns readable IP address. (ex: &quot;195.68.21.195&quot;)
+ */
+string CInetAddress::ipAddress() const
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;/*stringstream ss; // or use inet_ntoa
+ &nbsp; &nbsp; &nbsp; &nbsp;ss &lt;&lt; inet_ntoa ( _SockAddr-&gt;sin_addr );
+ &nbsp; &nbsp; &nbsp; &nbsp;return ss.str();*/
+ &nbsp; &nbsp; &nbsp; &nbsp;return string( inet_ntoa( _SockAddr-&gt;sin_addr ) );
+}
+
+
+/*
+ * Returns host name. (ex: &quot;www.nevrax.org&quot;)
+ */
+const string&amp; CInetAddress::hostName() const
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;return _HostName;
+}
+
+
+/*
+ * Returns port
+ */
+uint16 CInetAddress::port() const
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;return ntohs( _SockAddr-&gt;sin_port );
+}
+
+
+/*
+ * Returns hostname and port as a string. (ex: &quot;www.nevrax.org:80 (195.68.21.195)&quot;)
+ */
+std::string CInetAddress::asString() const
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;stringstream ss;
+ &nbsp; &nbsp; &nbsp; &nbsp;ss &lt;&lt; hostName() &lt;&lt; &quot;:&quot; &lt;&lt; port() &lt;&lt; &quot; (&quot; &lt;&lt; ipAddress() &lt;&lt; &quot;)&quot;;
+ &nbsp; &nbsp; &nbsp; &nbsp;return ss.str();
+}
+
+
+/*
+ * Returns IP address and port as a string. (ex: &quot;195.68.21.195:80&quot;)
+ */
+std::string CInetAddress::asIPString() const
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;stringstream ss;
+ &nbsp; &nbsp; &nbsp; &nbsp;ss &lt;&lt; ipAddress() &lt;&lt; &quot;:&quot; &lt;&lt; port();
+ &nbsp; &nbsp; &nbsp; &nbsp;return ss.str();
+}
+
+
+/*
+ * Serialize
+ */
+void CInetAddress::serial( NLMISC::IStream&amp; s )
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;NLMISC::CMemStream *ms = dynamic_cast&lt;NLMISC::CMemStream*&gt;(&amp;s);
+ &nbsp; &nbsp; &nbsp; &nbsp;if ( ms &amp;&amp; ms-&gt;stringMode() )
+ &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// String stream
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string addrs;
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( ms-&gt;isReading() )
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ms-&gt;serial( addrs );
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setNameAndPort( addrs );
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;addrs = asIPString();
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ms-&gt;serial( addrs );
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
+ &nbsp; &nbsp; &nbsp; &nbsp;}
+ &nbsp; &nbsp; &nbsp; &nbsp;else
+ &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Binary stream
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s.serialBuffer( (uint8*)_SockAddr, sizeof(*_SockAddr) ); // this is possible only because the contents of _SockAddr is platform-independant !
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s.serial( _Valid );
+ &nbsp; &nbsp; &nbsp; &nbsp;}
+
+ &nbsp; &nbsp; &nbsp; &nbsp;// Common
+ &nbsp; &nbsp; &nbsp; &nbsp;s.serial( _Valid );
+}
+
+
+/*
+ * Creates a CInetAddress object with local host address, port=0
+ */
+CInetAddress CInetAddress::localHost()
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;const uint maxlength = 80;
+ &nbsp; &nbsp; &nbsp; &nbsp;char localhost [maxlength];
+ &nbsp; &nbsp; &nbsp; &nbsp;if ( gethostname( localhost, maxlength ) != 0 )
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw ESocket( &quot;Unable to get local hostname&quot; );
+ &nbsp; &nbsp; &nbsp; &nbsp;CInetAddress localaddr = CInetAddress( string(localhost) );
+ &nbsp; &nbsp; &nbsp; &nbsp;nlassert( localaddr.ipAddress() != &quot;127.0.0.1&quot; );
+ &nbsp; &nbsp; &nbsp; &nbsp;return localaddr;
+}
+
+
+/* Returns the list of the local host addresses (with port=0)
+ * (especially useful if the host is multihomed)
+ */
+std::vector&lt;CInetAddress&gt; CInetAddress::localAddresses()
+{
+ &nbsp; &nbsp; &nbsp; &nbsp;// 1. Get local host name
+ &nbsp; &nbsp; &nbsp; &nbsp;const uint maxlength = 80;
+ &nbsp; &nbsp; &nbsp; &nbsp;char localhost [maxlength];
+ &nbsp; &nbsp; &nbsp; &nbsp;if ( gethostname( localhost, maxlength ) == SOCKET_ERROR )
+ &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw ESocket( &quot;Unable to get local hostname&quot; );
+ &nbsp; &nbsp; &nbsp; &nbsp;}
+
+ &nbsp; &nbsp; &nbsp; &nbsp;// 2. Get address list
+ &nbsp; &nbsp;hostent *phostent = gethostbyname( localhost );
+ &nbsp; &nbsp;if ( phostent == NULL )
+ &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw ESocket( (string(&quot;Hostname resolution failed for &quot;)+string(localhost)).c_str() );
+ &nbsp; &nbsp; &nbsp; &nbsp;}
+ &nbsp; &nbsp; &nbsp; &nbsp;uint i;
+ &nbsp; &nbsp; &nbsp; &nbsp;vector&lt;CInetAddress&gt; vect;
+ &nbsp; &nbsp;for ( i=0; phostent-&gt;h_addr_list[i]!=0; ++i )
+ &nbsp; &nbsp; &nbsp; &nbsp;{
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vect.push_back( CInetAddress( (const in_addr*)(phostent-&gt;h_addr_list[i]) ) );
+ &nbsp; &nbsp;}
+ &nbsp; &nbsp; &nbsp; &nbsp;return vect;
+}
+
+
+} // NLNET
+</PRE> \ No newline at end of file