Files
unison/src/node/address.h

239 lines
7.5 KiB
C
Raw Normal View History

2008-10-02 15:22:17 -07:00
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2007 INRIA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
2007-07-26 14:03:41 +02:00
#ifndef ADDRESS_H
#define ADDRESS_H
#include <stdint.h>
#include <ostream>
2008-02-20 21:45:42 +01:00
#include "ns3/attribute.h"
2008-02-21 18:54:02 +01:00
#include "ns3/attribute-helper.h"
2008-05-28 13:03:29 -07:00
#include "ns3/tag-buffer.h"
2007-07-26 14:03:41 +02:00
namespace ns3 {
2007-07-27 09:44:24 +02:00
/**
2008-05-30 10:36:02 -07:00
* \ingroup node
* \defgroup address Address
*/
/**
* \ingroup address
2007-07-27 09:44:24 +02:00
* \brief a polymophic address class
*
* This class is very similar in design and spirit to the BSD sockaddr
* structure: they are both used to hold multiple types of addresses
* together with the type of the address.
*
* A new address class defined by a user needs to:
* - allocate a type id with Address::Register
* - provide a method to convert his new address to an Address
* instance. This method is typically a member method named ConvertTo:
* Address MyAddress::ConvertTo (void) const;
* - provide a method to convert an Address instance back to
* an instance of his new address type. This method is typically
* a static member method of his address class named ConvertFrom:
* static MyAddress MyAddress::ConvertFrom (const Address &address);
* - the ConvertFrom method is expected to check that the type of the
* input Address instance is compatible with its own type.
*
* Typical code to create a new class type looks like:
* \code
* // this class represents addresses which are 2 bytes long.
* class MyAddress
* {
* public:
* Address ConvertTo (void) const;
* static MyAddress ConvertFrom (void);
* private:
* static uint8_t GetType (void);
* };
*
* Address MyAddress::ConvertTo (void) const
* {
* return Address (GetType (), m_buffer, 2);
* }
* MyAddress MyAddress::ConvertFrom (const Address &address)
* {
* MyAddress ad;
* NS_ASSERT (address.CheckCompatible (GetType (), 2));
* address.CopyTo (ad.m_buffer, 2);
* return ad;
* }
* uint8_t MyAddress::GetType (void)
* {
* static uint8_t type = Address::Register ();
* return type;
* }
* \endcode
*/
2007-07-26 14:03:41 +02:00
class Address
{
public:
2008-09-15 06:10:53 -07:00
/**
* The maximum size of a byte buffer which
* can be stored in an Address instance.
*/
2008-07-02 13:23:23 -07:00
enum MaxSize_e {
2009-06-03 08:49:40 +02:00
MAX_SIZE = 20
2007-07-26 14:47:05 +02:00
};
2007-07-27 09:44:24 +02:00
/**
* Create an invalid address
*/
2007-07-26 14:47:05 +02:00
Address ();
2007-07-27 09:44:24 +02:00
/**
* \param type the type of the Address to create
* \param buffer a pointer to a buffer of bytes which hold
* a serialized representation of the address in network
* byte order.
* \param len the length of the buffer.
*
* Create an address from a type and a buffer. This constructor
* is typically invoked from the conversion functions of various
* address types when they have to convert themselves to an
* Address instance.
*/
2007-07-26 14:03:41 +02:00
Address (uint8_t type, const uint8_t *buffer, uint8_t len);
Address (const Address & address);
Address &operator = (const Address &address);
2007-08-01 09:01:54 +02:00
/**
* \returns true if this address is invalid, false otherwise.
*
* An address is invalid if and only if it was created
* through the default constructor and it was never
* re-initialized.
*/
2007-08-01 09:14:31 +02:00
bool IsInvalid (void) const;
2007-07-27 09:44:24 +02:00
/**
* \returns the length of the underlying address.
*/
2007-07-26 14:03:41 +02:00
uint8_t GetLength (void) const;
2007-07-27 09:44:24 +02:00
/**
* \param buffer buffer to copy the address bytes to.
* \returns the number of bytes copied.
2007-07-27 09:44:24 +02:00
*/
uint32_t CopyTo (uint8_t buffer[MAX_SIZE]) const;
/**
* \param buffer buffer to copy the whole address data structure to
* \param len the size of the buffer
* \returns the number of bytes copied.
2008-10-02 15:22:17 -07:00
*
* Copies the type to buffer[0], the length of the address internal buffer
* to buffer[1] and copies the internal buffer starting at buffer[2]. len
* must be at least the size of the internal buffer plus a byte for the type
* and a byte for the length.
*/
uint32_t CopyAllTo (uint8_t *buffer, uint8_t len) const;
2007-07-27 09:44:24 +02:00
/**
* \param buffer pointer to a buffer of bytes which contain
* a serialized representation of the address in network
* byte order.
* \param len length of buffer
* \returns the number of bytes copied.
2007-07-27 09:44:24 +02:00
*
2008-10-02 15:22:17 -07:00
* Copy the address bytes from buffer into to the internal buffer of this
* address instance.
2007-07-27 09:44:24 +02:00
*/
uint32_t CopyFrom (const uint8_t *buffer, uint8_t len);
/**
* \param buffer pointer to a buffer of bytes which contain
* a copy of all the members of this Address class.
* \param len the length of the buffer
* \returns the number of bytes copied.
2008-10-02 15:22:17 -07:00
*
* The inverse of CopyAllTo().
*
* \see CopyAllTo
*/
uint32_t CopyAllFrom (const uint8_t *buffer, uint8_t len);
2007-07-27 09:44:24 +02:00
/**
* \param type a type id as returned by Address::Register
* \param len the length associated to this type id.
*
* \returns true if the type of the address stored internally
* is compatible with the requested type, false otherwise.
*/
2007-07-26 14:47:05 +02:00
bool CheckCompatible (uint8_t type, uint8_t len) const;
/**
* \param type a type id as returned by Address::Register
* \returns true if the type of the address stored internally
* is compatible with the requested type, false otherwise.
*
* This method checks that the types are _exactly_ equal.
* This method is really used only by the PacketSocketAddress
* and there is little point in using it otherwise so,
* you have been warned: DO NOT USE THIS METHOD.
*/
bool IsMatchingType (uint8_t type) const;
2007-07-27 09:44:24 +02:00
/**
* Allocate a new type id for a new type of address.
* \returns a new type id.
*/
2007-07-26 14:03:41 +02:00
static uint8_t Register (void);
/**
* Get the number of bytes needed to serialize the underlying Address
* Typically, this is GetLength () + 2
*
* \returns the number of bytes required for an Address in serialized form
*/
uint32_t GetSerializedSize (void) const;
/**
* Serialize this address in host byte order to a byte buffer
*
2008-05-30 10:35:25 -07:00
* \param buffer output buffer that gets written with this Address
*/
2008-05-28 13:03:29 -07:00
void Serialize (TagBuffer buffer) const;
/**
2008-05-30 10:35:25 -07:00
* \param buffer buffer to read address from
*
* The input address buffer is expected to be in host byte order format.
*/
2008-05-28 13:03:29 -07:00
void Deserialize (TagBuffer buffer);
2008-02-14 00:36:39 +01:00
2007-07-26 14:03:41 +02:00
private:
friend bool operator == (const Address &a, const Address &b);
friend bool operator < (const Address &a, const Address &b);
friend std::ostream& operator<< (std::ostream& os, const Address & address);
2008-04-11 16:52:12 -07:00
friend std::istream& operator>> (std::istream& is, Address & address);
2007-07-26 14:03:41 +02:00
uint8_t m_type;
uint8_t m_len;
uint8_t m_data[MAX_SIZE];
};
/**
* \class ns3::AddressValue
* \brief hold objects of type ns3::Address
*/
ATTRIBUTE_HELPER_HEADER (Address);
2008-02-15 05:38:29 +01:00
2007-07-26 14:03:41 +02:00
bool operator == (const Address &a, const Address &b);
bool operator != (const Address &a, const Address &b);
bool operator < (const Address &a, const Address &b);
std::ostream& operator<< (std::ostream& os, const Address & address);
2008-02-14 00:36:39 +01:00
std::istream& operator>> (std::istream& is, Address & address);
2007-07-26 14:03:41 +02:00
} // namespace ns3
#endif /* ADDRESS_H */