// $Id$
//
-// Copyright (C) 2007
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+// Copyright (C) 2007
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
// Stefan Bund <g0dil@berlios.de>
//
// This program is free software; you can redistribute it and/or modify
#include <iostream>
#include <string>
#include <boost/cstdint.hpp>
-#include <boost/function.hpp>
#include <boost/array.hpp>
#include <boost/operators.hpp>
-#include "Utils/SafeBool.hh"
+#include "../../../Utils/safe_bool.hh"
+#include "../../../Utils/Tags.hh"
+#include "../AddressExceptions.hh"
//#include "INet4Address.mpp"
///////////////////////////////hh.p////////////////////////////////////////
namespace senf {
- /** \brief IpV4 Internet address
+ /** \brief IPv4 Internet address
INet4Address represents a simple IP address. It is modelled as a fixed-size
container/sequence of 4 bytes.
+ The following statements all create the same INet4 address <code>211.194.177.160</code>
+ \code
+ // Used to construct constant INet4 addresses
+ INet4Address(0xD3C2B1A0)
+
+ // Construct an INet4 address from it's string representation. All the standard address
+ // representations are supported
+ INet4Address::from_string("211.194.177.160")
+ INet4Address::from_string("211.12759456")
+
+ // Construct an INet4 address from raw data. 'from_data' takes an arbitrary iterator (e.g. a
+ // pointer) as argument. Here we use a fixed array but normally you will need this to build
+ // an INet4 address in a packet parser
+ char rawBytes[] = { 0xD3, 0xC2, 0xB1, 0xA0 };
+ INet4Address::from_data(rawBytes)
+
+ // Construct an INet4 address from the standard POSIX representation: a 32-bit integer in
+ // network byte oder. This is used to interface with POSIX routines
+ struct sockaddr_in saddr = ...;
+ INet4Address::from_inaddr(saddr.sin_addr.s_addr)
+ \endcode
+
+ Since INet4Address is based on \c boost::array, you can access the raw data bytes of the
+ address (in network byte order) using \c begin(), \c end() or \c operator[]
+ \code
+ INet4Address ina = ...;
+ Packet::iterator i = ...;
+ std::copy(ina.begin(), ina.end(), i); // Copies 4 bytes
+ \endcode
+
\see CheckINet4Network \n INet4Network
\implementation We awkwardly need to use static named constructors (<tt>from_</tt> members)
therefore not be distinguished from initialization with a string literal. Therefore we
need to disambiguate using the named constructors.
- \todo Add additional classes for CIDR addresses and networks and network math.
\ingroup addr_group
*/
class INet4Address
: public boost::array<boost::uint8_t,4>,
- public ComparableSafeBool<INet4Address>
+ public comparable_safe_bool<INet4Address>
{
public:
///////////////////////////////////////////////////////////////////////////
typedef uint32_t address_type; ///< Address representation as number in host byte order
typedef uint32_t inaddr_type; ///< Legacy address representation in network byte order
- typedef boost::function<void (INet4Address const &)> Callback;
- ///< Callback for asynchronous from_string call
static INet4Address const None; ///< The empty (0) address
static INet4Address const Loopback; ///< The loopback (127.0.0.1) address
static INet4Address const Broadcast; ////< The global broadcast (255.255.255.255) address
- enum NoInit_t { noinit };
-
///////////////////////////////////////////////////////////////////////////
///\name Structors and default members
///@{
INet4Address(); ///< Construct an empty address
- explicit INet4Address(NoInit_t); ///< Construct uninitialized (!) address
+ explicit INet4Address(senf::NoInit_t); ///< Construct uninitialized (!) address
explicit INet4Address(address_type value);
///< Construct an address constant
\attention This call may block if \a s represents a
hostname which must be looked up via some network
protocol like DNS or NIS
- \throws SyntaxException if the address cannot be
+ \throws AddressSyntaxException if the address cannot be
converted for some reason
\param[in] s Address literal or hostname */
- static void from_string(std::string const & s, Callback const & cb);
- ///< Convert string to address (async/non-blocking)
- /**< This member works like
- from_string(std::string const &). However unlike
- from_string(std::string const &), this call will not
- block. Instead it will call \a cb passing the
- INet4Address instance as soon as the address has been
- resolved (which may be immediate if the address
- represents an IP literal). \par
- On error, the address passed to \a cb will be empty.
- \param[in] s Address literal or hostname
- \param[in] cb Callback to pass the address to
- \fixme Implement */
-
template <class InputIterator>
static INet4Address from_data(InputIterator i);
///< Construct address from 4 bytes of raw data
////@}
- struct SyntaxException : public std::exception
- { virtual char const * what() const throw() { return "invalid INet4 address syntax"; } };
-
private:
enum InAddr_t { IsInAddr };
INet4Address(inaddr_type addr, InAddr_t);
static bool match(INet4Address const & addr);
};
- /** \brief IpV4 network prefix
+ /** \brief IPv4 network prefix
- This class represents an IpV4 network prefix in CIDR notation.
+ This class represents an IPv4 network prefix in CIDR notation.
*/
class INet4Network
: public boost::equality_comparable<INet4Network>,
- public ComparableSafeBool<INet4Network>
+ public comparable_safe_bool<INet4Network>
{
public:
///////////////////////////////////////////////////////////////////////////