A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-interface-container.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@cutebugs.net>
7 */
8
9#ifndef IPV4_INTERFACE_CONTAINER_H
10#define IPV4_INTERFACE_CONTAINER_H
11
12#include "ns3/ipv4-address.h"
13#include "ns3/ipv4.h"
14
15#include <stdint.h>
16#include <vector>
17
18namespace ns3
19{
20
21/**
22 * @ingroup ipv4
23 *
24 * @brief holds a vector of std::pair of Ptr<Ipv4> and interface index.
25 *
26 * Typically ns-3 Ipv4Interfaces are installed on devices using an Ipv4 address
27 * helper. The helper's Assign() method takes a NetDeviceContainer which holds
28 * some number of Ptr<NetDevice>. For each of the NetDevices in the
29 * NetDeviceContainer the helper will find the associated Ptr<Node> and
30 * Ptr<Ipv4>. It makes sure that an interface exists on the node for the
31 * device and then adds an Ipv4Address according to the address helper settings
32 * (incrementing the Ipv4Address somehow as it goes). The helper then converts
33 * the Ptr<Ipv4> and the interface index to a std::pair and adds them to a
34 * container -- a container of this type.
35 *
36 * The point is then to be able to implicitly associate an index into the
37 * original NetDeviceContainer (that identifies a particular net device) with
38 * an identical index into the Ipv4InterfaceContainer that has a std::pair with
39 * the Ptr<Ipv4> and interface index you need to play with the interface.
40 *
41 * @see Ipv4AddressHelper
42 * @see Ipv4
43 */
45{
46 public:
47 /**
48 * @brief Container Const Iterator for pairs of Ipv4 smart pointer / Interface Index.
49 */
50 typedef std::vector<std::pair<Ptr<Ipv4>, uint32_t>>::const_iterator Iterator;
51
52 /**
53 * Create an empty Ipv4InterfaceContainer.
54 */
56
57 /**
58 * Concatenate the entries in the other container with ours.
59 * @param other container
60 */
61 void Add(const Ipv4InterfaceContainer& other);
62
63 /**
64 * @brief Get an iterator which refers to the first pair in the
65 * container.
66 *
67 * Pairs can be retrieved from the container in two ways. First,
68 * directly by an index into the container, and second, using an iterator.
69 * This method is used in the iterator method and is typically used in a
70 * for-loop to run through the pairs
71 *
72 * @code
73 * Ipv4InterfaceContainer::Iterator i;
74 * for (i = container.Begin (); i != container.End (); ++i)
75 * {
76 * std::pair<Ptr<Ipv4>, uint32_t> pair = *i;
77 * method (pair.first, pair.second); // use the pair
78 * }
79 * @endcode
80 *
81 * @returns an iterator which refers to the first pair in the container.
82 */
83 Iterator Begin() const;
84
85 /**
86 * @brief Get an iterator which indicates past-the-last Node in the
87 * container.
88 *
89 * Nodes can be retrieved from the container in two ways. First,
90 * directly by an index into the container, and second, using an iterator.
91 * This method is used in the iterator method and is typically used in a
92 * for-loop to run through the Nodes
93 *
94 * @code
95 * NodeContainer::Iterator i;
96 * for (i = container.Begin (); i != container.End (); ++i)
97 * {
98 * std::pair<Ptr<Ipv4>, uint32_t> pair = *i;
99 * method (pair.first, pair.second); // use the pair
100 * }
101 * @endcode
102 *
103 * @returns an iterator which indicates an ending condition for a loop.
104 */
105 Iterator End() const;
106
107 /**
108 * @returns the number of Ptr<Ipv4> and interface pairs stored in this
109 * Ipv4InterfaceContainer.
110 *
111 * Pairs can be retrieved from the container in two ways. First,
112 * directly by an index into the container, and second, using an iterator.
113 * This method is used in the direct method and is typically used to
114 * define an ending condition in a for-loop that runs through the stored
115 * Nodes
116 *
117 * @code
118 * uint32_t nNodes = container.GetN ();
119 * for (uint32_t i = 0 i < nNodes; ++i)
120 * {
121 * std::pair<Ptr<Ipv4>, uint32_t> pair = container.Get (i);
122 * method (pair.first, pair.second); // use the pair
123 * }
124 * @endcode
125 *
126 * @returns the number of Ptr<Node> stored in this container.
127 */
128 uint32_t GetN() const;
129
130 /**
131 * @param i index of ipInterfacePair in container
132 * @param j interface address index (if interface has multiple addresses)
133 * @returns the IPv4 address of the j'th address of the interface
134 * corresponding to index i.
135 *
136 * If the second parameter is omitted, the zeroth indexed address of
137 * the interface is returned. Unless IP aliasing is being used on
138 * the interface, the second parameter may typically be omitted.
139 */
140 Ipv4Address GetAddress(uint32_t i, uint32_t j = 0) const;
141
142 /**
143 * @brief Set a metric for the given interface
144 * @param i Interface index
145 * @param metric the interface metric
146 */
147 void SetMetric(uint32_t i, uint16_t metric);
148
149 /**
150 * Manually add an entry to the container consisting of the individual parts
151 * of an entry std::pair.
152 *
153 * @param ipv4 pointer to Ipv4 object
154 * @param interface interface index of the Ipv4Interface to add to the container
155 */
156 void Add(Ptr<Ipv4> ipv4, uint32_t interface);
157
158 /**
159 * Manually add an entry to the container consisting of a previously composed
160 * entry std::pair.
161 *
162 * @param ipInterfacePair the pair of a pointer to Ipv4 object and interface index of the
163 * Ipv4Interface to add to the container
164 */
165 void Add(std::pair<Ptr<Ipv4>, uint32_t> ipInterfacePair);
166
167 /**
168 * Manually add an entry to the container consisting of the individual parts
169 * of an entry std::pair.
170 *
171 * @param ipv4Name std:string referring to the saved name of an Ipv4 Object that
172 * has been previously named using the Object Name Service.
173 * @param interface interface index of the Ipv4Interface to add to the container
174 */
175 void Add(std::string ipv4Name, uint32_t interface);
176
177 /**
178 * Get the std::pair of an Ptr<Ipv4> and interface stored at the location
179 * specified by the index.
180 *
181 * @param i the index of the container entry to retrieve.
182 * @return the std::pair of a Ptr<Ipv4> and an interface index
183 *
184 * @note The returned Ptr<Ipv4> cannot be used directly to fetch the
185 * Ipv4Interface using the returned index (the GetInterface () method
186 * is provided in class Ipv4L3Protocol, and not class Ipv4). An
187 * example usage is provided below.
188 *
189 * @code
190 * Ipv4InterfaceContainer c;
191 * ...
192 * std::pair<Ptr<Ipv4>, uint32_t> returnValue = c.Get (0);
193 * Ptr<Ipv4> ipv4 = returnValue.first;
194 * uint32_t index = returnValue.second;
195 * Ptr<Ipv4Interface> iface = DynamicCast<Ipv4L3Protocol> (ipv4)->GetInterface (index);
196 * @endcode
197 */
198 std::pair<Ptr<Ipv4>, uint32_t> Get(uint32_t i) const;
199
200 private:
201 /**
202 * @brief Container for pairs of Ipv4 smart pointer / Interface Index.
203 */
204 typedef std::vector<std::pair<Ptr<Ipv4>, uint32_t>> InterfaceVector;
205
206 /**
207 * @brief List of IPv4 stack and interfaces index.
208 */
210};
211
212} // namespace ns3
213
214#endif /* IPV4_INTERFACE_CONTAINER_H */
Ipv4 addresses are stored in host order in this class.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
std::vector< std::pair< Ptr< Ipv4 >, uint32_t > >::const_iterator Iterator
Container Const Iterator for pairs of Ipv4 smart pointer / Interface Index.
std::vector< std::pair< Ptr< Ipv4 >, uint32_t > > InterfaceVector
Container for pairs of Ipv4 smart pointer / Interface Index.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
std::pair< Ptr< Ipv4 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr<Ipv4> and interface stored at the location specified by the index.
void SetMetric(uint32_t i, uint16_t metric)
Set a metric for the given interface.
InterfaceVector m_interfaces
List of IPv4 stack and interfaces index.
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Iterator Begin() const
Get an iterator which refers to the first pair in the container.
Ipv4InterfaceContainer()
Create an empty Ipv4InterfaceContainer.
Smart pointer class similar to boost::intrusive_ptr.
Every class exported by the ns3 library is enclosed in the ns3 namespace.