A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ns3::Address Class Reference

a polymophic address class More...

#include "address.h"

+ Collaboration diagram for ns3::Address:

Public Member Functions

 Address ()
 Create an invalid address.
 
 Address (const Address &address)
 Create an address from another address.
 
 Address (uint8_t type, const uint8_t *buffer, uint8_t len)
 Create an address from a type and a buffer.
 
bool CheckCompatible (uint8_t type, uint8_t len) const
 
uint32_t CopyAllFrom (const uint8_t *buffer, uint8_t len)
 
uint32_t CopyAllTo (uint8_t *buffer, uint8_t len) const
 
uint32_t CopyFrom (const uint8_t *buffer, uint8_t len)
 
uint32_t CopyTo (uint8_t buffer[MAX_SIZE]) const
 Copy the address bytes into a buffer.
 
void Deserialize (TagBuffer buffer)
 
uint8_t GetLength () const
 Get the length of the underlying address.
 
uint32_t GetSerializedSize () const
 Get the number of bytes needed to serialize the underlying Address Typically, this is GetLength () + 2.
 
bool IsInvalid () const
 
bool IsMatchingType (uint8_t type) const
 
Addressoperator= (const Address &address)
 Basic assignment operator.
 
void Serialize (TagBuffer buffer) const
 Serialize this address in host byte order to a byte buffer.
 

Static Public Member Functions

static uint8_t Register ()
 Allocate a new type id for a new type of address.
 

Static Public Attributes

static constexpr uint32_t MAX_SIZE {20}
 The maximum size of a byte buffer which can be stored in an Address instance.
 

Private Attributes

uint8_t m_data [MAX_SIZE]
 The address value.
 
uint8_t m_len
 Length of the address.
 
uint8_t m_type
 Type of the address.
 

Friends

bool operator!= (const Address &a, const Address &b)
 Not equal to operator.
 
bool operator< (const Address &a, const Address &b)
 Less than operator.
 
std::ostream & operator<< (std::ostream &os, const Address &address)
 Stream insertion operator.
 
bool operator== (const Address &a, const Address &b)
 Equal to operator.
 
std::istream & operator>> (std::istream &is, Address &address)
 Stream extraction operator.
 

Detailed Description

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 () 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:

// this class represents addresses which are 2 bytes long.
class MyAddress
{
public:
Address ConvertTo () const;
static MyAddress ConvertFrom ();
private:
static uint8_t GetType ();
};
Address MyAddress::ConvertTo () 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 ()
{
static uint8_t type = Address::Register ();
return type;
}
a polymophic address class
Definition: address.h:100
Address()
Create an invalid address.
Definition: address.cc:34
static uint8_t Register()
Allocate a new type id for a new type of address.
Definition: address.cc:146
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66

To convert a specific Address T (e.g., Ipv6Address) to and from an Address type, a class must implement three public functions:

static T ConvertFrom(const Address& address);
Address ConvertTo() const;
operator Address() const;
See also
Address Attribute

Definition at line 99 of file address.h.

Constructor & Destructor Documentation

◆ Address() [1/3]

ns3::Address::Address ( )

Create an invalid address.

Definition at line 34 of file address.cc.

References NS_LOG_FUNCTION.

◆ Address() [2/3]

ns3::Address::Address ( uint8_t  type,
const uint8_t *  buffer,
uint8_t  len 
)

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.

Parameters
typethe type of the Address to create
buffera pointer to a buffer of bytes which hold a serialized representation of the address in network byte order.
lenthe length of the buffer.

Definition at line 42 of file address.cc.

References m_data, m_len, MAX_SIZE, NS_ASSERT, and NS_LOG_FUNCTION.

◆ Address() [3/3]

ns3::Address::Address ( const Address address)

Create an address from another address.

Parameters
addressthe address to copy

Definition at line 51 of file address.cc.

References m_data, m_len, MAX_SIZE, and NS_ASSERT.

Member Function Documentation

◆ CheckCompatible()

bool ns3::Address::CheckCompatible ( uint8_t  type,
uint8_t  len 
) const
Parameters
typea type id as returned by Address::Register
lenthe length associated to this type id.
Returns
true if the type of the address stored internally is compatible with the requested type, false otherwise.
Internal:
Mac address type/length detection is discussed in Bug 1568

Definition at line 129 of file address.cc.

References m_len, m_type, MAX_SIZE, NS_ASSERT, and NS_LOG_FUNCTION.

Referenced by ns3::Inet6SocketAddress::ConvertFrom(), and ns3::Inet6SocketAddress::IsMatchingType().

+ Here is the caller graph for this function:

◆ CopyAllFrom()

uint32_t ns3::Address::CopyAllFrom ( const uint8_t *  buffer,
uint8_t  len 
)
Parameters
bufferpointer to a buffer of bytes which contain a copy of all the members of this Address class.
lenthe length of the buffer
Returns
the number of bytes copied.

The inverse of CopyAllTo().

See also
CopyAllTo

Definition at line 116 of file address.cc.

References m_data, m_len, m_type, NS_ASSERT, and NS_LOG_FUNCTION.

Referenced by ns3::PacketSocketAddress::ConvertFrom().

+ Here is the caller graph for this function:

◆ CopyAllTo()

uint32_t ns3::Address::CopyAllTo ( uint8_t *  buffer,
uint8_t  len 
) const
Parameters
bufferbuffer to copy the whole address data structure to
lenthe size of the buffer
Returns
the number of bytes copied.

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.

Definition at line 95 of file address.cc.

References m_data, m_len, m_type, NS_ASSERT, and NS_LOG_FUNCTION.

Referenced by ns3::PacketSocketAddress::ConvertTo().

+ Here is the caller graph for this function:

◆ CopyFrom()

uint32_t ns3::Address::CopyFrom ( const uint8_t *  buffer,
uint8_t  len 
)
Parameters
bufferpointer to a buffer of bytes which contain a serialized representation of the address in network byte order.
lenlength of buffer
Returns
the number of bytes copied.

Copy the address bytes from buffer into to the internal buffer of this address instance.

Definition at line 106 of file address.cc.

References m_data, m_len, MAX_SIZE, NS_ASSERT, and NS_LOG_FUNCTION.

Referenced by ns3::DhcpServer::AddStaticDhcpEntry(), ns3::Icmpv6OptionLinkLayerAddress::Deserialize(), ns3::SixLowPanMesh::Deserialize(), ns3::DhcpHeader::GetChaddr(), ns3::ReadFrom(), and ns3::DhcpClient::StartApplication().

+ Here is the caller graph for this function:

◆ CopyTo()

uint32_t ns3::Address::CopyTo ( uint8_t  buffer[MAX_SIZE]) const

◆ Deserialize()

void ns3::Address::Deserialize ( TagBuffer  buffer)
Parameters
bufferbuffer to read address from

The input address buffer is expected to be in host byte order format.

Definition at line 171 of file address.cc.

References m_data, m_len, m_type, MAX_SIZE, NS_ASSERT, NS_LOG_FUNCTION, ns3::TagBuffer::Read(), and ns3::TagBuffer::ReadU8().

Referenced by ns3::PacketSocketTag::Deserialize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetLength()

uint8_t ns3::Address::GetLength ( ) const

◆ GetSerializedSize()

uint32_t ns3::Address::GetSerializedSize ( ) const

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

Definition at line 155 of file address.cc.

References m_len, and NS_LOG_FUNCTION.

Referenced by ns3::PacketSocketTag::GetSerializedSize().

+ Here is the caller graph for this function:

◆ IsInvalid()

bool ns3::Address::IsInvalid ( ) const
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.

Definition at line 71 of file address.cc.

References m_len, m_type, and NS_LOG_FUNCTION.

Referenced by DualStackTestCase::DoRun(), ns3::BulkSendApplication::StartApplication(), ns3::OnOffApplication::StartApplication(), and ns3::Ping::StartApplication().

+ Here is the caller graph for this function:

◆ IsMatchingType()

bool ns3::Address::IsMatchingType ( uint8_t  type) const
Parameters
typea 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.

Definition at line 139 of file address.cc.

References m_type, and NS_LOG_FUNCTION.

◆ operator=()

Address & ns3::Address::operator= ( const Address address)

Basic assignment operator.

Parameters
addressthe address to copy
Returns
the address

Definition at line 60 of file address.cc.

References m_data, m_len, m_type, MAX_SIZE, and NS_ASSERT.

◆ Register()

uint8_t ns3::Address::Register ( )
static

◆ Serialize()

void ns3::Address::Serialize ( TagBuffer  buffer) const

Serialize this address in host byte order to a byte buffer.

Parameters
bufferoutput buffer that gets written with this Address

Definition at line 162 of file address.cc.

References m_data, m_len, m_type, NS_LOG_FUNCTION, ns3::TagBuffer::Write(), and ns3::TagBuffer::WriteU8().

Referenced by ns3::PacketSocketTag::Serialize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( const Address a,
const Address b 
)
friend

Not equal to operator.

Parameters
athe first operand
bthe first operand
Returns
true if the operands are not equal

Definition at line 206 of file address.cc.

◆ operator<

bool operator< ( const Address a,
const Address b 
)
friend

Less than operator.

Parameters
athe first operand
bthe first operand
Returns
true if the operand a is less than operand b

Definition at line 211 of file address.cc.

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
const Address address 
)
friend

Stream insertion operator.

Parameters
osthe stream
addressthe address
Returns
a reference to the stream

Definition at line 245 of file address.cc.

◆ operator==

bool operator== ( const Address a,
const Address b 
)
friend

Equal to operator.

Parameters
athe first operand
bthe first operand
Returns
true if the operands are equal

Definition at line 183 of file address.cc.

◆ operator>>

std::istream & operator>> ( std::istream &  is,
Address address 
)
friend

Stream extraction operator.

Parameters
isthe stream
addressthe address
Returns
a reference to the stream

Definition at line 264 of file address.cc.

Member Data Documentation

◆ m_data

uint8_t ns3::Address::m_data[MAX_SIZE]
private

The address value.

Definition at line 282 of file address.h.

Referenced by Address(), CopyAllFrom(), CopyAllTo(), CopyFrom(), CopyTo(), Deserialize(), operator=(), and Serialize().

◆ m_len

uint8_t ns3::Address::m_len
private

◆ m_type

uint8_t ns3::Address::m_type
private

Type of the address.

Definition at line 280 of file address.h.

Referenced by CheckCompatible(), CopyAllFrom(), CopyAllTo(), Deserialize(), IsInvalid(), IsMatchingType(), operator=(), and Serialize().

◆ MAX_SIZE


The documentation for this class was generated from the following files: