A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
address.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19
20#ifndef ADDRESS_H
21#define ADDRESS_H
22
23#include "tag-buffer.h"
24
25#include "ns3/attribute-helper.h"
26#include "ns3/attribute.h"
27
28#include <ostream>
29#include <stdint.h>
30
31namespace ns3
32{
33
34/**
35 * \ingroup network
36 * \defgroup address Address
37 *
38 * Network Address abstractions, including MAC, IPv4 and IPv6.
39 */
40/**
41 * \ingroup address
42 * \brief a polymophic address class
43 *
44 * This class is very similar in design and spirit to the BSD sockaddr
45 * structure: they are both used to hold multiple types of addresses
46 * together with the type of the address.
47 *
48 * A new address class defined by a user needs to:
49 * - allocate a type id with Address::Register
50 * - provide a method to convert his new address to an Address
51 * instance. This method is typically a member method named ConvertTo:
52 * Address MyAddress::ConvertTo () const;
53 * - provide a method to convert an Address instance back to
54 * an instance of his new address type. This method is typically
55 * a static member method of his address class named ConvertFrom:
56 * static MyAddress MyAddress::ConvertFrom (const Address &address);
57 * - the ConvertFrom method is expected to check that the type of the
58 * input Address instance is compatible with its own type.
59 *
60 * Typical code to create a new class type looks like:
61 * \code
62 * // this class represents addresses which are 2 bytes long.
63 * class MyAddress
64 * {
65 * public:
66 * Address ConvertTo () const;
67 * static MyAddress ConvertFrom ();
68 * private:
69 * static uint8_t GetType ();
70 * };
71 *
72 * Address MyAddress::ConvertTo () const
73 * {
74 * return Address (GetType (), m_buffer, 2);
75 * }
76 * MyAddress MyAddress::ConvertFrom (const Address &address)
77 * {
78 * MyAddress ad;
79 * NS_ASSERT (address.CheckCompatible (GetType (), 2));
80 * address.CopyTo (ad.m_buffer, 2);
81 * return ad;
82 * }
83 * uint8_t MyAddress::GetType ()
84 * {
85 * static uint8_t type = Address::Register ();
86 * return type;
87 * }
88 * \endcode
89 *
90 * To convert a specific Address T (e.g., Ipv6Address) to and from an Address type,
91 * a class must implement three public functions:
92 * \code
93 * static T ConvertFrom(const Address& address);
94 * Address ConvertTo() const;
95 * operator Address() const;
96 * \endcode
97 *
98 * \see attribute_Address
99 */
101{
102 public:
103 /**
104 * The maximum size of a byte buffer which
105 * can be stored in an Address instance.
106 */
107 static constexpr uint32_t MAX_SIZE{20};
108
109 /**
110 * Create an invalid address
111 */
112 Address();
113 /**
114 * \brief Create an address from a type and a buffer.
115 *
116 * This constructor is typically invoked from the conversion
117 * functions of various address types when they have to
118 * convert themselves to an Address instance.
119 *
120 * \param type the type of the Address to create
121 * \param buffer a pointer to a buffer of bytes which hold
122 * a serialized representation of the address in network
123 * byte order.
124 * \param len the length of the buffer.
125 */
126 Address(uint8_t type, const uint8_t* buffer, uint8_t len);
127 /**
128 * \brief Create an address from another address.
129 * \param address the address to copy
130 */
131 Address(const Address& address);
132 /**
133 * \brief Basic assignment operator.
134 * \param address the address to copy
135 * \returns the address
136 */
137 Address& operator=(const Address& address);
138
139 /**
140 * \returns true if this address is invalid, false otherwise.
141 *
142 * An address is invalid if and only if it was created
143 * through the default constructor and it was never
144 * re-initialized.
145 */
146 bool IsInvalid() const;
147 /**
148 * \brief Get the length of the underlying address.
149 * \returns the length of the underlying address.
150 */
151 uint8_t GetLength() const;
152 /**
153 * \brief Copy the address bytes into a buffer.
154 * \param buffer buffer to copy the address bytes to.
155 * \returns the number of bytes copied.
156 */
157 uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const;
158 /**
159 * \param buffer buffer to copy the whole address data structure to
160 * \param len the size of the buffer
161 * \returns the number of bytes copied.
162 *
163 * Copies the type to buffer[0], the length of the address internal buffer
164 * to buffer[1] and copies the internal buffer starting at buffer[2]. len
165 * must be at least the size of the internal buffer plus a byte for the type
166 * and a byte for the length.
167 */
168 uint32_t CopyAllTo(uint8_t* buffer, uint8_t len) const;
169 /**
170 * \param buffer pointer to a buffer of bytes which contain
171 * a serialized representation of the address in network
172 * byte order.
173 * \param len length of buffer
174 * \returns the number of bytes copied.
175 *
176 * Copy the address bytes from buffer into to the internal buffer of this
177 * address instance.
178 */
179 uint32_t CopyFrom(const uint8_t* buffer, uint8_t len);
180 /**
181 * \param buffer pointer to a buffer of bytes which contain
182 * a copy of all the members of this Address class.
183 * \param len the length of the buffer
184 * \returns the number of bytes copied.
185 *
186 * The inverse of CopyAllTo().
187 *
188 * \see CopyAllTo
189 */
190 uint32_t CopyAllFrom(const uint8_t* buffer, uint8_t len);
191 /**
192 * \param type a type id as returned by Address::Register
193 * \param len the length associated to this type id.
194 *
195 * \returns true if the type of the address stored internally
196 * is compatible with the requested type, false otherwise.
197 */
198 bool CheckCompatible(uint8_t type, uint8_t len) const;
199 /**
200 * \param type a type id as returned by Address::Register
201 * \returns true if the type of the address stored internally
202 * is compatible with the requested type, false otherwise.
203 *
204 * This method checks that the types are _exactly_ equal.
205 * This method is really used only by the PacketSocketAddress
206 * and there is little point in using it otherwise so,
207 * you have been warned: DO NOT USE THIS METHOD.
208 */
209 bool IsMatchingType(uint8_t type) const;
210 /**
211 * Allocate a new type id for a new type of address.
212 * \returns a new type id.
213 */
214 static uint8_t Register();
215 /**
216 * Get the number of bytes needed to serialize the underlying Address
217 * Typically, this is GetLength () + 2
218 *
219 * \returns the number of bytes required for an Address in serialized form
220 */
222 /**
223 * Serialize this address in host byte order to a byte buffer
224 *
225 * \param buffer output buffer that gets written with this Address
226 */
227 void Serialize(TagBuffer buffer) const;
228 /**
229 * \param buffer buffer to read address from
230 *
231 * The input address buffer is expected to be in host byte order format.
232 */
233 void Deserialize(TagBuffer buffer);
234
235 private:
236 /**
237 * \brief Equal to operator.
238 *
239 * \param a the first operand
240 * \param b the first operand
241 * \returns true if the operands are equal
242 */
243 friend bool operator==(const Address& a, const Address& b);
244
245 /**
246 * \brief Not equal to operator.
247 *
248 * \param a the first operand
249 * \param b the first operand
250 * \returns true if the operands are not equal
251 */
252 friend bool operator!=(const Address& a, const Address& b);
253
254 /**
255 * \brief Less than operator.
256 *
257 * \param a the first operand
258 * \param b the first operand
259 * \returns true if the operand a is less than operand b
260 */
261 friend bool operator<(const Address& a, const Address& b);
262
263 /**
264 * \brief Stream insertion operator.
265 *
266 * \param os the stream
267 * \param address the address
268 * \returns a reference to the stream
269 */
270 friend std::ostream& operator<<(std::ostream& os, const Address& address);
271
272 /**
273 * \brief Stream extraction operator.
274 *
275 * \param is the stream
276 * \param address the address
277 * \returns a reference to the stream
278 */
279 friend std::istream& operator>>(std::istream& is, Address& address);
280
281 uint8_t m_type; //!< Type of the address
282 uint8_t m_len; //!< Length of the address
283 uint8_t m_data[MAX_SIZE]; //!< The address value
284};
285
287
288bool operator==(const Address& a, const Address& b);
289bool operator!=(const Address& a, const Address& b);
290bool operator<(const Address& a, const Address& b);
291std::ostream& operator<<(std::ostream& os, const Address& address);
292std::istream& operator>>(std::istream& is, Address& address);
293
294} // namespace ns3
295
296#endif /* ADDRESS_H */
a polymophic address class
Definition: address.h:101
friend std::istream & operator>>(std::istream &is, Address &address)
Stream extraction operator.
Definition: address.cc:268
uint32_t GetSerializedSize() const
Get the number of bytes needed to serialize the underlying Address Typically, this is GetLength () + ...
Definition: address.cc:155
void Serialize(TagBuffer buffer) const
Serialize this address in host byte order to a byte buffer.
Definition: address.cc:162
uint32_t CopyFrom(const uint8_t *buffer, uint8_t len)
Definition: address.cc:106
friend std::ostream & operator<<(std::ostream &os, const Address &address)
Stream insertion operator.
Definition: address.cc:246
static constexpr uint32_t MAX_SIZE
The maximum size of a byte buffer which can be stored in an Address instance.
Definition: address.h:107
bool IsInvalid() const
Definition: address.cc:71
uint8_t m_len
Length of the address.
Definition: address.h:282
uint8_t m_data[MAX_SIZE]
The address value.
Definition: address.h:283
uint8_t m_type
Type of the address.
Definition: address.h:281
Address & operator=(const Address &address)
Basic assignment operator.
Definition: address.cc:60
Address()
Create an invalid address.
Definition: address.cc:34
friend bool operator!=(const Address &a, const Address &b)
Not equal to operator.
Definition: address.cc:206
uint32_t CopyAllFrom(const uint8_t *buffer, uint8_t len)
Definition: address.cc:116
bool CheckCompatible(uint8_t type, uint8_t len) const
Definition: address.cc:129
static uint8_t Register()
Allocate a new type id for a new type of address.
Definition: address.cc:146
uint8_t GetLength() const
Get the length of the underlying address.
Definition: address.cc:78
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
Definition: address.cc:86
void Deserialize(TagBuffer buffer)
Definition: address.cc:171
friend bool operator<(const Address &a, const Address &b)
Less than operator.
Definition: address.cc:212
bool IsMatchingType(uint8_t type) const
Definition: address.cc:139
friend bool operator==(const Address &a, const Address &b)
Equal to operator.
Definition: address.cc:183
uint32_t CopyAllTo(uint8_t *buffer, uint8_t len) const
Definition: address.cc:95
read and write tag data
Definition: tag-buffer.h:52
#define ATTRIBUTE_HELPER_HEADER(type)
Declare the attribute value, accessor and checkers for class type
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
Definition: callback.h:678
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:157
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:170