Source: internetaddress.h


Annotated List
Files
Globals
Hierarchy
Index
// ##########################################################################
// ####                                                                  ####
// ####                      RTP Audio Server Project                    ####
// ####                    ============================                  ####
// ####                                                                  ####
// #### Internet Address                                                 ####
// ####                                                                  ####
// #### Version 1.00  --  February 04, 2001                              ####
// ####                                                                  ####
// #### Copyright (C) 1999  Thomas Dreibholz                             ####
// ####               2000  Universität Bonn, Abt. IV                    ####
// ####               2001  EMail: Dreibholz@bigfoot.com                 ####
// ####                     WWW:   http://www.bigfoot.com/~dreibholz     ####
// ####                                                                  ####
// ##########################################################################


#ifndef INTERNETADDRESS_H
#define INTERNETADDRESS_H


#include "system.h"
#include "strings.h"
#include "socketaddress.h"
#include "portableaddress.h"


#include <netinet/in.h>
#include <resolv.h>


namespace Coral {


/**
  * This class manages an internet address.
  *
  * @short   Socket Address
  * @author  Thomas Dreibholz (Dreibholz@bigfoot.com)
  * @version 1.0
  */            
class InternetAddress : virtual public SocketAddress
{
   // ====== Constructors/Destructor ========================================
   public:
   /**
     * Constructor for an empty internet address.
     */
   InternetAddress();

   /**
     * Constructor for an internet address from an internet address.
     *
     * @param address Internet address.
     */
   InternetAddress(const InternetAddress& address);

   /**
     * Constructor for a internet address given by a string.
     * Examples: "gaffel:7500", "12.34.56.78:7500", "3ffe:4711::0!7500", "odin:7500", "ipv6-odin:7500".
     *
     * @param address Address string.
     */
   InternetAddress(const String& address);      

   /**
     * Constructor for a internet address given by host name and port.
     *
     * @param hostName Host name.
     * @param port Port number.
     */
   InternetAddress(const String& hostName,
                   const card16 port);

   /**
     * Constructor for INADDR_ANY address with given port.
     *
     * @param port Port number.
     */
   InternetAddress(const card16 port);

   /**
     * Constructor for a internet address from the system's sockaddr structure. The
     * sockaddr structure may be sockaddr_in (IPv4) or sockaddr_in6 (IPv6).
     *
     * @param address sockaddr.
     * @param length Length of sockaddr (sizeof(sockaddr_in) or sizeof(sockaddr_in6)).
     */
   InternetAddress(sockaddr* address, socklen_t length);

   /**
     * Destructor.
     */
   ~InternetAddress();


   // ====== Initialization =================================================
   /**
     * Reset internet address.
     */
   void reset();

   /**
     * Initialize internet address from internet address.
     */
   void init(const InternetAddress& address);

   /**
     * Initialize internet address with INADDR_ANY and given port.
     *
     * @param port Port number.
     */
   void init(const card16 port);

   /**
     * Initialize internet address with given host name and port.
     *
     * @param hostName Host name.
     * @param port Port number.
     */    
   void init(const String& hostName, const card16 port);


   // ====== Operators ======================================================
   /**
     * Implementation of = operator.
     */
   inline InternetAddress& operator=(const InternetAddress& source);


   // ====== Address manipulation ===========================================
   /**
     * Get port of address.
     */
   inline card16 getPort() const;

   /**
     * Set port of address.
     */
   inline void setPort(const card16 port);

   // ====== Print format ===================================================
   /**
     * setPrintFormat() printing formats.
     */
   enum PrintFormat {
      PF_Address  = (1 << 0),
      PF_Hostname = (1 << 1),
      PF_Full     = PF_Address | PF_Hostname
   };

   /**
     * Get printing format.
     *
     * @return Print format.
     */
   inline PrintFormat getPrintFormat() const;

   /**
     * Set printing format.
     *
     * @param format Print format.
     */
   inline void setPrintFormat(const PrintFormat format);

   
   // ====== Address functions ==============================================
   /**
     * isValid() implementation of SocketAddress.
     *
     * @see SocketAddress#isValid
     */
   bool isValid() const;

   /**
     * Check, if the address is null.
     *
     * @return true, if the address is not null; false otherwise.
     */
   inline bool isNull() const;

   /**
     * Get address string.
     *
     * @return Address string.
     */
   String getAddressString() const;

   /**
     * Check, if internet address is an IPv4 or IPv4-mapped address.
     *
     * @return true, if address is an IPv4 or IPv4-mapped address; false otherwise.
     */
   inline bool isIPv4() const;

   /**
     * Check, if internet address is a real (not IPv4-mapped) IPv6 address.
     * Addresses which return true here can be used with labeled flows by
     * class Socket.
     *
     * @return true, if address is real IPv6; false otherwise.
     */
   inline bool isIPv6() const;


   // ====== Get local address for a connection =============================
   /**
     * Get the local host address. The parameter peer gives the address of
     * the other host.
     *
     * @param address Reference to SocketAddress to write address to.
     * @param peer Address of peer.
     * @return true, if call was successful; false otherwise.
     *
     * Examples:
     * localhost => localhost address (127.0.0.1 or ::1).
     * ethernet-host => ethernet interface address.
     * internet-address => dynamic-ip address set by pppd.
     */
   static InternetAddress getLocalAddress(const InternetAddress& peer);

   
   // ====== Get/set system sockaddr structure ==============================
   /**
     * getSystemAddress() implementation of SocketAddress
     *
     * @see SocketAddress#getSystemAddress
     */            
   cardinal getSystemAddress(sockaddr*       buffer,
                             const socklen_t length,
                             const cardinal  type) const;

   /**
     * setSystemAddress() implementation of SocketAddress.
     *
     * @see SocketAddress#setSystemAddress
     */
   bool setSystemAddress(sockaddr* address, const socklen_t length);


   // ====== IPv6 support functions ========================================
   /**
     * Wrapper for system's gethostbyname() function. This version does
     * support IPv6 addresses even if the system itself does not support IPv6.
     * IPv6 addresses are then converted to IPv4 if possible (IPv4-mapped IPv6).
     *
     * @param name Host name.
     * @param myadr Storage space to save a IPv6 address (16 bytes).
     * @param length Length of the address saved in myaddr or 0 in case of failure.
     */
   static cardinal getHostByName(const String& name, card16* myadr);

   /**
     * Check, if IPv6 support is available.
     *
     * @return true, if IPv6 support is available; false otherwise.
     */
   static inline bool hasIPv6();

    /**
      * Static variable which shows the availability of IPv6. Setting this
      * variable to false on an IPv6 system simulates an IPv4-only system.
      *
      * Do *not* change this variable if Socket or InternetAddress objects
      * are already in use!!!
      */
   static bool UseIPv6;
    

    // ====== Comparision operators =========================================
   /**
     * Implementation of == operator.
     */
   int operator==(const InternetAddress& address) const;

   /**
     * Implementation of != operator.
     */
   inline int operator!=(const InternetAddress& address) const;

   /**
     * Implementation of < operator.
     */
   int operator<(const InternetAddress& address) const;

   /**
     * Implementation of <= operator.
     */
   inline int operator<=(const InternetAddress& address) const;

   /**
     * Implementation of > operator.
     */
   int operator>(const InternetAddress& address) const;

   /**
     * Implementation of >= operator.
     */
   inline int operator>=(const InternetAddress& address) const;


   // ====== Conversion from and to PortableAddress =========================
   /**
     * Get PortableAddress from InternetAddress.
     *
     * @return PortableAddress.
     */
   inline PortableAddress getPortableAddress() const;

   /**
     * Constructor for InternetAddress from PortableAddress.
     *
     * @param address PortableAddress.
     */
   InternetAddress(const PortableAddress& address);

   /**
     * Initialize InternetAddress from PortableAddress.
     *
     * @param address PortableAddress.
     */
   void init(const PortableAddress& address);


   // ====== Private data ===================================================
   private:
   static bool checkIPv6();


   private:
   /**
     * Host address in network byte order. IPv4 addresses are converted to
     * IPv4-mapped IPv6 addresses.
     */
   card16 Host[8];

   /**
     * Port number.
     */
   card16 Port;

   /**
     * Print format.
     */
   PrintFormat Format;

   /**
     * Is address valid?
     */
   bool Valid;
};


}


#include "internetaddress.icc"


#endif

Generated by: viper@odin on Sun Feb 4 18:54:51 2001, using kdoc 2.0a22.