Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

InetAddress.h

00001 #ifndef _EHNET_InetAddress_H_  // -*-c++-*-
00002 
00003 #ifdef __GNUG__
00004 #  pragma interface
00005 #endif
00006 
00007 /*
00008  * Copyright 1999-2002 Eric M. Hopper <hopper@omnifarious.org>
00009  * 
00010  *     This program is free software; you can redistribute it and/or modify it
00011  *     under the terms of the GNU Lesser General Public License as published
00012  *     by the Free Software Foundation; either version 2 of the License, or
00013  *     (at your option) any later version.
00014  * 
00015  *     This program is distributed in the hope that it will be useful, but
00016  *     WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *     Lesser General Public License for more details.
00019  * 
00020  *     You should have received a copy of the GNU Lesser General Public
00021  *     License along with this program; if not, write to the Free Software
00022  *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00023  */
00024 
00025 /* $Header: /home/hopper/src/cvs/C++/EHnet++/EHnet++/InetAddress.h,v 1.9 2002/09/10 12:07:47 hopper Exp $ */
00026 
00027  // For log, see ../ChangeLog
00028 
00029  // $Revision: 1.9 $
00030 
00031  // Revision 1.3  1996/02/12 03:01:53  hopper
00032  // Added links to my ClassIdent system.
00033  //
00034  // Revision 1.2  1996/02/12 00:32:55  hopper
00035  // Fixed to use the new C++ standard library string class instead of all the
00036  // 'NetString' silliness.
00037  //
00038  // Revision 1.1.1.1  1995/07/23 17:45:30  hopper
00039  // Imported sources
00040  //
00041  // Revision 0.2.0.4.1.1  1995/07/23 03:21:38  hopper
00042  // Added #include <string.h> so it would compile correctly used libg++ 2.7.0.
00043  // libg++ 2.7.0 is in error.
00044  //
00045  // Revision 0.2.0.4  1994/08/12 17:07:12  hopper
00046  // Changed to use NetString class. The NetString class handles all library
00047  // dependencies.
00048  //
00049  // Revision 0.2.0.3  1994/06/16  02:59:39  hopper
00050  // Added #pragma interface for GNU stuff.
00051  //
00052  // Revision 0.2.0.2  1994/05/08  18:34:52  hopper
00053  // Changed to work better with Rogue Wave classes.
00054  //
00055  // Revision 0.2.0.1  1994/05/08  18:06:21  hopper
00056  // Head of WinterFire branch. Changed all instances of String with
00057  // RWCString.
00058  //
00059  // Revision 0.2  1994/05/08  16:11:06  hopper
00060  // Shifted declarations and definitions to work better with new libraries.
00061  //
00062  // Revision 0.1  1994/05/03  03:25:21  hopper
00063  // Initial revision
00064  //
00065 
00066 #ifndef _EHNET_SocketAddress_H_
00067 #   include <EHnet++/SocketAddress.h>
00068 #endif
00069 #include <LCore/GenTypes.h>
00070 
00071 #include <string>
00072 #include <sys/types.h>
00073 #include <netinet/in.h>
00074 
00075 #define _EHNET_InetAddress_H_
00076 
00077 namespace strmod {
00078 namespace ehnet {
00079 
00080 /** \class InetAddress InetAddress.h EHnet++/InetAddress.h
00081  * An IPV4 TCP or UDP address.
00082  *
00083  * This class represents an IPV4 TCP or UDP address.  It doesn't represent a
00084  * straight IPV4 address because it has a port number.
00085  *
00086  * This class has the nasty habit of doing blocking DNS lookups at random times.
00087  * This may cause a several second pause while they're going on.
00088  */
00089 class InetAddress : public SocketAddress {
00090  private:
00091    typedef lcore::U2Byte U2Byte;
00092    typedef lcore::U4Byte U4Byte;
00093    typedef lcore::ClassIdent ClassIdent;
00094  public:
00095    static const NET_ClassIdent identifier;
00096 
00097    /** Create an InetAddress from a hostname/dotted-quad and port #
00098     * @param h_name A host name (i.e. omnifarious.omnifarious.org) , or dotted-quad (i.e. 208.42.65.33).
00099     * @param prt A TCP or UDP port number.
00100     */
00101    InetAddress(const ::std::string &h_name, U2Byte prt);
00102    /** Create an InetAddress pointing an INADDR_ANY and a port #
00103     * @param port A TCP or UDP port number
00104     */
00105    InetAddress(U2Byte port);
00106    //! Create a copy of another InetAddress
00107    InetAddress(const InetAddress &b);
00108    /** Create an InetAddress from the associated sockaddr_in structure
00109     *
00110     * Useful for addresses you get back from accept(2) or getpeername(2)
00111     */
00112    InetAddress(const ::sockaddr_in &iadr);
00113    //! No pointers or anything, so not much to destroy
00114    virtual ~InetAddress()                               { }
00115 
00116    virtual int AreYouA(const ClassIdent &cid) const {
00117       return((identifier == cid) || SocketAddress::AreYouA(cid));
00118    }
00119 
00120    virtual ::sockaddr *SockAddr()         { return (::sockaddr *)(&inaddr); }
00121    InetAddress *Copy() const              { return (InetAddress *)MakeCopy(); }
00122    virtual int AddressSize() const        { return sizeof(::sockaddr_in); }
00123    virtual ::std::string AsString();
00124 
00125    //! Get the host name associated with this address, possibly the result of a reverse DNS lookup
00126    ::std::string GetHostname() const                    { return(hostname); }
00127    //! Get the host name associated with this address, possibly the result of a reverse DNS lookup
00128    ::std::string GetHostname(bool forcelookup);
00129    //! Get the port number
00130    U2Byte GetPort() const                               { return port; }
00131 
00132    //! Only necessary because of the DNS lookups.
00133    const InetAddress &operator =(const InetAddress &b);
00134    /** Copy an InetAddress from a sockaddr_in structure.
00135     *
00136     * Useful for addresses you get back from accept(2) or getpeername(2).
00137     */
00138    const InetAddress &operator =(const ::sockaddr_in &iadr);
00139 
00140  protected:
00141    virtual const lcore::ClassIdent *i_GetIdent() const  { return &identifier; }
00142 
00143    inline virtual SocketAddress *MakeCopy() const;
00144 
00145    //! Mark this address as invalid
00146    void InvalidateAddress();
00147    //! Parse out a dotted-quad
00148    static bool ParseNumeric(const char *numeric_addr, U4Byte &num);
00149    //! Lookup an IPV4 address from a hostname - returns false on failure.
00150    static bool NameToIaddr(const char *name_addr, U4Byte &num);
00151    //! Lookup a hostname from an IPV4 address.
00152    static ::std::string IaddrToName(const ::sockaddr_in &inaddr);
00153 
00154  private:
00155    ::std::string hostname;
00156    U2Byte port;
00157    ::sockaddr_in inaddr;
00158 
00159    static bool AsciiToQInum(const char *s, int &i,
00160                             unsigned long &num, bool endsindot);
00161    static ::std::string ToDec(U2Byte num);
00162 };
00163 
00164 //--------------------------------inline functions-----------------------------
00165 
00166 inline SocketAddress *InetAddress::MakeCopy() const
00167 {
00168    return(new InetAddress(*this));
00169 }
00170 
00171 } // end namespace ehnet
00172 } // end namespace strmod
00173 
00174 #endif

Generated on Wed Jan 29 00:32:44 2003 for libNet by doxygen1.3-rc1