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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8
9#ifndef ADDRESS_H
10#define ADDRESS_H
11
12#include "tag-buffer.h"
13
14#include "ns3/attribute-helper.h"
15#include "ns3/attribute.h"
16
17#include <ostream>
18#include <stdint.h>
19#include <unordered_map>
20
21namespace ns3
22{
23
24/**
25 * @ingroup network
26 * @defgroup address Address
27 *
28 * Network Address abstractions, including MAC, IPv4 and IPv6.
29 */
30
31/**
32 * @ingroup address
33 * @brief a polymophic address class
34 *
35 * This class is very similar in design and spirit to the BSD sockaddr
36 * structure: they are both used to hold multiple types of addresses
37 * together with the type of the address.
38 *
39 * A new address class defined by a user needs to:
40 * - allocate a type id with Address::Register
41 * - provide a method to convert his new address to an Address
42 * instance. This method is typically a member method named ConvertTo:
43 * Address MyAddress::ConvertTo () const;
44 * - provide a method to convert an Address instance back to
45 * an instance of his new address type. This method is typically
46 * a static member method of his address class named ConvertFrom:
47 * static MyAddress MyAddress::ConvertFrom (const Address &address);
48 * - the ConvertFrom method is expected to check that the type of the
49 * input Address instance is compatible with its own type.
50 *
51 * Typical code to create a new class type looks like:
52 * @code
53 * // this class represents addresses which are 2 bytes long.
54 * class MyAddress
55 * {
56 * public:
57 * Address ConvertTo () const;
58 * static MyAddress ConvertFrom ();
59 * private:
60 * static uint8_t GetType ();
61 * };
62 *
63 * Address MyAddress::ConvertTo () const
64 * {
65 * return Address (GetType (), m_buffer, 2);
66 * }
67 *
68 * MyAddress MyAddress::ConvertFrom (const Address &address)
69 * {
70 * MyAddress ad;
71 * NS_ASSERT (address.CheckCompatible (GetType (), 2));
72 * address.CopyTo (ad.m_buffer, 2);
73 * return ad;
74 * }
75 *
76 * uint8_t MyAddress::GetType ()
77 * {
78 * static uint8_t type = Address::Register ("AddressType", 2);
79 * return type;
80 * }
81 * @endcode
82 *
83 * To convert a specific Address T (e.g., Ipv6Address) to and from an Address type,
84 * a class must implement three public functions:
85 * @code
86 * static T ConvertFrom(const Address& address);
87 * Address ConvertTo() const;
88 * operator Address() const;
89 * @endcode
90 *
91 * Furthermore, the specific address must call the static function
92 * ``Address::Register(kind, length)``. This is typically done in a static function like
93 * ``GetType``.
94 *
95 * The kind/length pair is used to set the address type when this is not known a-priori.
96 * The typical use-case is when you decode a header where the address kind is known, but its
97 * length is specified in the header itself. The concrete example is the ARP or NDP headers,
98 * where you have a MAC address with a variable length.
99 * In these cases the code will be (assuming the address is a MAC address):
100 * @code
101 * Address addr;
102 * addr.SetType("MacAddress", addressLen);
103 * ReadFrom(i, addr, addressLen);
104 * @endcode
105 *
106 * It is forbidden to have two specific addresses sharing the same kind and length.
107 *
108 * @see attribute_Address
109 */
111{
112 private:
113 /**
114 * Key for the address registry: kind (string) / type (uint8_t)
115 */
116 using KindType = std::pair<std::string, uint8_t>;
117
118 /**
119 * Structure necessary to hash KindType, used as the key for the m_typeRegistry map
120 */
121 struct KeyHash
122 {
123 /**
124 * Functional operator for (string, uint8_t) hash computation.
125 * @param k the key to hash
126 * @return The key hash
127 */
128 std::size_t operator()(const KindType& k) const
129 {
130 return std::hash<std::string>()(k.first) ^ (std::hash<uint8_t>()(k.second));
131 }
132 };
133
134 /**
135 * Type of the address registry.
136 */
137 using KindTypeRegistry = std::unordered_map<KindType, uint8_t, Address::KeyHash>;
138
139 /**
140 * Container of allocated address types.
141 *
142 * The data is ordered by KindType (a {kind, length} pair), and sores the address type.
143 */
145
146 /// Unassigned Address type is reserved. Defined for clarity.
147 static constexpr uint8_t UNASSIGNED_TYPE{0};
148
149 public:
150 /**
151 * The maximum size of a byte buffer which
152 * can be stored in an Address instance.
153 */
154 static constexpr uint32_t MAX_SIZE{20};
155
156 /**
157 * Create an invalid address
158 */
159 Address();
160 /**
161 * @brief Create an address from a type and a buffer.
162 *
163 * This constructor is typically invoked from the conversion
164 * functions of various address types when they have to
165 * convert themselves to an Address instance.
166 *
167 * @param type the type of the Address to create
168 * @param buffer a pointer to a buffer of bytes which hold
169 * a serialized representation of the address in network
170 * byte order.
171 * @param len the length of the buffer.
172 */
173 Address(uint8_t type, const uint8_t* buffer, uint8_t len);
174 /**
175 * @brief Create an address from another address.
176 * @param address the address to copy
177 */
178 Address(const Address& address);
179 /**
180 * @brief Basic assignment operator.
181 * @param address the address to copy
182 * @return the address
183 */
184 Address& operator=(const Address& address);
185
186 /**
187 * Set the address type.
188 *
189 * Works only if the type is not yet set.
190 * @see Register()
191 *
192 * @param length address length
193 * @param kind address kind
194 */
195 void SetType(const std::string& kind, uint8_t length);
196 /**
197 * @return true if this address is invalid, false otherwise.
198 *
199 * An address is invalid if and only if it was created
200 * through the default constructor and it was never
201 * re-initialized.
202 */
203 bool IsInvalid() const;
204 /**
205 * @brief Get the length of the underlying address.
206 * @return the length of the underlying address.
207 */
208 uint8_t GetLength() const;
209 /**
210 * @brief Copy the address bytes into a buffer.
211 * @param buffer buffer to copy the address bytes to.
212 * @return the number of bytes copied.
213 */
214 uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const;
215 /**
216 * @param buffer buffer to copy the whole address data structure to
217 * @param len the size of the buffer
218 * @return the number of bytes copied.
219 *
220 * Copies the type to buffer[0], the length of the address internal buffer
221 * to buffer[1] and copies the internal buffer starting at buffer[2]. len
222 * must be at least the size of the internal buffer plus a byte for the type
223 * and a byte for the length.
224 */
225 uint32_t CopyAllTo(uint8_t* buffer, uint8_t len) const;
226 /**
227 * @param buffer pointer to a buffer of bytes which contain
228 * a serialized representation of the address in network
229 * byte order.
230 * @param len length of buffer
231 * @return the number of bytes copied.
232 *
233 * Copy the address bytes from buffer into to the internal buffer of this
234 * address instance.
235 */
236 uint32_t CopyFrom(const uint8_t* buffer, uint8_t len);
237 /**
238 * @param buffer pointer to a buffer of bytes which contain
239 * a copy of all the members of this Address class.
240 * @param len the length of the buffer
241 * @return the number of bytes copied.
242 *
243 * The inverse of CopyAllTo().
244 *
245 * @see CopyAllTo
246 */
247 uint32_t CopyAllFrom(const uint8_t* buffer, uint8_t len);
248 /**
249 * @param type a type id as returned by Address::Register
250 * @param len the length associated to this type id.
251 *
252 * @return true if the type of the address stored internally
253 * is compatible with the requested type, false otherwise.
254 */
255 bool CheckCompatible(uint8_t type, uint8_t len) const;
256 /**
257 * @param type a type id as returned by Address::Register
258 * @return true if the type of the address stored internally
259 * is compatible with the requested type, false otherwise.
260 *
261 * This method checks that the types are _exactly_ equal.
262 * This method is really used only by the PacketSocketAddress
263 * and there is little point in using it otherwise so,
264 * you have been warned: DO NOT USE THIS METHOD.
265 */
266 bool IsMatchingType(uint8_t type) const;
267 /**
268 * Allocate a new type id for a new type of address.
269 *
270 * Each address-like class that needs to be converted to/from an
271 * ``Address`` needs to register itself once. This is typically done
272 * in the ``GetType`` function.
273 *
274 * The address kind and length are typically used during the buffer
275 * deserialization, where the exact address type is unknown, and only its
276 * kind and length are known, e.g., if you parse an ARP reply.
277 *
278 * It is not allowed to have two different addresses with the same
279 * kind and length.
280 *
281 * @param length address length
282 * @param kind address kind, such as "MacAddress"
283 * @return a new type id.
284 */
285 static uint8_t Register(const std::string& kind, uint8_t length);
286 /**
287 * Get the number of bytes needed to serialize the underlying Address
288 * Typically, this is GetLength () + 2
289 *
290 * @return the number of bytes required for an Address in serialized form
291 */
293 /**
294 * Serialize this address in host byte order to a byte buffer
295 *
296 * @param buffer output buffer that gets written with this Address
297 */
298 void Serialize(TagBuffer buffer) const;
299 /**
300 * @param buffer buffer to read address from
301 *
302 * The input address buffer is expected to be in host byte order format.
303 */
304 void Deserialize(TagBuffer buffer);
305
306 private:
307 /**
308 * @brief Equal to operator.
309 *
310 * @param a the first operand
311 * @param b the first operand
312 * @return true if the operands are equal
313 */
314 friend bool operator==(const Address& a, const Address& b);
315
316 /**
317 * @brief Not equal to operator.
318 *
319 * @param a the first operand
320 * @param b the first operand
321 * @return true if the operands are not equal
322 */
323 friend bool operator!=(const Address& a, const Address& b);
324
325 /**
326 * @brief Less than operator.
327 *
328 * @param a the first operand
329 * @param b the first operand
330 * @return true if the operand a is less than operand b
331 */
332 friend bool operator<(const Address& a, const Address& b);
333
334 /**
335 * @brief Stream insertion operator.
336 *
337 * @param os the stream
338 * @param address the address
339 * @return a reference to the stream
340 */
341 friend std::ostream& operator<<(std::ostream& os, const Address& address);
342
343 /**
344 * @brief Stream extraction operator.
345 *
346 * @param is the stream
347 * @param address the address
348 * @return a reference to the stream
349 */
350 friend std::istream& operator>>(std::istream& is, Address& address);
351
352 uint8_t m_type; //!< Type of the address
353 uint8_t m_len; //!< Length of the address
354 uint8_t m_data[MAX_SIZE]; //!< The address value
355};
356
358
359bool operator==(const Address& a, const Address& b);
360bool operator!=(const Address& a, const Address& b);
361bool operator<(const Address& a, const Address& b);
362std::ostream& operator<<(std::ostream& os, const Address& address);
363std::istream& operator>>(std::istream& is, Address& address);
364
365} // namespace ns3
366
367#endif /* ADDRESS_H */
a polymophic address class
Definition address.h:111
friend std::istream & operator>>(std::istream &is, Address &address)
Stream extraction operator.
Definition address.cc:273
uint32_t GetSerializedSize() const
Get the number of bytes needed to serialize the underlying Address Typically, this is GetLength () + ...
Definition address.cc:168
void Serialize(TagBuffer buffer) const
Serialize this address in host byte order to a byte buffer.
Definition address.cc:175
static constexpr uint8_t UNASSIGNED_TYPE
Unassigned Address type is reserved. Defined for clarity.
Definition address.h:147
uint32_t CopyFrom(const uint8_t *buffer, uint8_t len)
Definition address.cc:116
friend std::ostream & operator<<(std::ostream &os, const Address &address)
Stream insertion operator.
Definition address.cc:250
static constexpr uint32_t MAX_SIZE
The maximum size of a byte buffer which can be stored in an Address instance.
Definition address.h:154
bool IsInvalid() const
Definition address.cc:81
std::pair< std::string, uint8_t > KindType
Key for the address registry: kind (string) / type (uint8_t).
Definition address.h:116
uint8_t m_len
Length of the address.
Definition address.h:353
uint8_t m_data[MAX_SIZE]
The address value.
Definition address.h:354
void SetType(const std::string &kind, uint8_t length)
Set the address type.
Definition address.cc:63
uint8_t m_type
Type of the address.
Definition address.h:352
Address & operator=(const Address &address)
Basic assignment operator.
Definition address.cc:52
std::unordered_map< KindType, uint8_t, Address::KeyHash > KindTypeRegistry
Type of the address registry.
Definition address.h:137
static KindTypeRegistry m_typeRegistry
Container of allocated address types.
Definition address.h:144
Address()
Create an invalid address.
Definition address.cc:25
friend bool operator!=(const Address &a, const Address &b)
Not equal to operator.
Definition address.cc:210
uint32_t CopyAllFrom(const uint8_t *buffer, uint8_t len)
Definition address.cc:128
bool CheckCompatible(uint8_t type, uint8_t len) const
Definition address.cc:141
uint8_t GetLength() const
Get the length of the underlying address.
Definition address.cc:88
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
Definition address.cc:96
void Deserialize(TagBuffer buffer)
Definition address.cc:184
friend bool operator<(const Address &a, const Address &b)
Less than operator.
Definition address.cc:216
static uint8_t Register(const std::string &kind, uint8_t length)
Allocate a new type id for a new type of address.
Definition address.cc:156
bool IsMatchingType(uint8_t type) const
Definition address.cc:149
friend bool operator==(const Address &a, const Address &b)
Equal to operator.
Definition address.cc:196
uint32_t CopyAllTo(uint8_t *buffer, uint8_t len) const
Definition address.cc:105
read and write tag data
Definition tag-buffer.h:41
#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:658
bool operator==(const EventId &a, const EventId &b)
Definition event-id.h:146
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
std::istream & operator>>(std::istream &is, Angles &a)
Definition angles.cc:172
bool operator<(const EventId &a, const EventId &b)
Definition event-id.h:159
Structure necessary to hash KindType, used as the key for the m_typeRegistry map.
Definition address.h:122
std::size_t operator()(const KindType &k) const
Functional operator for (string, uint8_t) hash computation.
Definition address.h:128