A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sixlowpan-nd-test-utils.cc
Go to the documentation of this file.
1/*
2 * Utility implementations for sixlowpan ND tests.
3 */
4
6
7#include <algorithm>
8#include <iomanip>
9#include <regex>
10#include <sstream>
11#include <vector>
12
13namespace ns3
14{
15
16// ---------------------------------------------------------------------------
17// Address formatting helpers
18// Node indices are 1-based (i=1 is the first LN, with link-layer addr 0x02).
19// ---------------------------------------------------------------------------
20
21/**
22 * @brief Global unicast address for leaf node index i.
23 * @param i node index (1-based)
24 * @return address string "2001::200:ff:fe00:<i+1>"
25 */
26static std::string
28{
29 std::ostringstream oss;
30 oss << "2001::200:ff:fe00:" << std::hex << (i + 1);
31 return oss.str();
32}
33
34/**
35 * @brief Link-local address for leaf node index i.
36 * @param i node index (1-based)
37 * @return address string "fe80::200:ff:fe00:<i+1>"
38 */
39static std::string
41{
42 std::ostringstream oss;
43 oss << "fe80::200:ff:fe00:" << std::hex << (i + 1);
44 return oss.str();
45}
46
47/**
48 * @brief Link-layer address for leaf node index i.
49 * @param i node index (1-based)
50 * @return address string "03-06-00:00:00:00:00:<i+1>"
51 */
52static std::string
54{
55 std::ostringstream oss;
56 oss << "03-06-00:00:00:00:00:" << std::setfill('0') << std::setw(2) << std::hex << (i + 1);
57 return oss.str();
58}
59
60/**
61 * @brief Format a node/time/protocol header line as printed by ns-3 routing helpers.
62 * @param nodeId the node identifier
63 * @param time the simulation time
64 * @param protocol the protocol name string
65 * @return the formatted header string
66 */
67static std::string
68NodeHeader(uint32_t nodeId, Time time, const std::string& protocol)
69{
70 std::ostringstream oss;
71 oss << "Node: " << nodeId << ", Time: +" << time.GetSeconds() << "s"
72 << ", Local time: +" << time.GetSeconds() << "s, " << protocol;
73 return oss.str();
74}
75
76std::string
78{
79 std::ostringstream oss;
80 oss << std::resetiosflags(std::ios::adjustfield) << std::setiosflags(std::ios::left);
81
82 for (uint32_t nodeId = 0; nodeId < numNodes; ++nodeId)
83 {
84 oss << NodeHeader(nodeId, time, "Ipv6ListRouting table") << std::endl;
85
86 // ---- Static routing (priority 0) ----
87 oss << " Priority: 0 Protocol: ns3::Ipv6StaticRouting" << std::endl;
88 oss << NodeHeader(nodeId, time, "Ipv6StaticRouting table") << std::endl;
89 oss << "Destination Next Hop Flag Met Ref Use If"
90 << std::endl;
91
92 oss << std::setw(31) << "::1/128" << std::setw(27) << "::" << std::setw(5) << "UH"
93 << std::setw(4) << "0"
94 << "- - 0" << std::endl;
95 oss << std::setw(31) << "fe80::/64" << std::setw(27) << "::" << std::setw(5) << "U"
96 << std::setw(4) << "0"
97 << "- - 1" << std::endl;
98
99 if (nodeId == 0)
100 {
101 for (uint32_t i = 1; i < numNodes; ++i)
102 {
103 oss << std::setw(31) << (GlobalAddr(i) + "/128") << std::setw(27)
104 << LinkLocalAddr(i) << std::setw(5) << "UH" << std::setw(4) << "0"
105 << "- - 1" << std::endl;
106 }
107 }
108 else
109 {
110 oss << std::setw(31) << "::/0" << std::setw(27) << "fe80::200:ff:fe00:1" << std::setw(5)
111 << "UG" << std::setw(4) << "0"
112 << "- - 1" << std::endl;
113 }
114
115 oss << std::endl;
116
117 // ---- Global routing (priority -10, empty) ----
118 oss << " Priority: -10 Protocol: ns3::Ipv6GlobalRouting" << std::endl;
119 oss << NodeHeader(nodeId, time, "Ipv6GlobalRouting table") << std::endl;
120 oss << "Destination Next Hop Flag Met Ref Use Iface"
121 << std::endl
122 << std::endl;
123 }
124
125 return oss.str();
126}
127
128std::string
129SortRoutingTableString(std::string routingTable)
130{
131 std::istringstream iss(routingTable);
132 std::ostringstream oss;
133 std::string line;
134
135 while (std::getline(iss, line))
136 {
137 if (line.find("Node: 0") != std::string::npos)
138 {
139 oss << line << std::endl;
140 std::getline(iss, line);
141 oss << line << std::endl;
142
143 std::vector<std::string> standardRoutes;
144 std::vector<std::pair<int, std::string>> hostRoutes;
145
146 while (std::getline(iss, line) && !line.empty())
147 {
148 if (line.find("2001::200:ff:fe00:") != std::string::npos)
149 {
150 std::regex hexPattern("2001::200:ff:fe00:([0-9a-f]+)/128");
151 std::smatch match;
152
153 if (std::regex_search(line, match, hexPattern))
154 {
155 int hexValue = std::stoi(match[1].str(), nullptr, 16);
156 hostRoutes.emplace_back(hexValue, line);
157 }
158 }
159 else
160 {
161 standardRoutes.push_back(line);
162 }
163 }
164
165 for (const auto& route : standardRoutes)
166 {
167 oss << route << std::endl;
168 }
169
170 std::sort(hostRoutes.begin(),
171 hostRoutes.end(),
172 [](const std::pair<int, std::string>& a,
173 const std::pair<int, std::string>& b) { return a.first < b.first; });
174
175 for (const auto& route : hostRoutes)
176 {
177 oss << route.second << std::endl;
178 }
179
180 oss << std::endl;
181 }
182 else
183 {
184 oss << line << std::endl;
185 if (line.find("Node:") != std::string::npos)
186 {
187 while (std::getline(iss, line))
188 {
189 oss << line << std::endl;
190 if (line.empty())
191 {
192 break;
193 }
194 }
195 }
196 }
197 }
198
199 return oss.str();
200}
201
202std::string
203NormalizeNdiscCacheStates(const std::string& ndiscOutput)
204{
205 std::string result = ndiscOutput;
206 result = std::regex_replace(result, std::regex("STALE"), "REACHABLE");
207 return result;
208}
209
210std::string
212{
213 std::ostringstream oss;
214
215 for (uint32_t nodeId = 0; nodeId < numNodes; ++nodeId)
216 {
217 oss << "NDISC Cache of node " << std::dec << nodeId << " at time +" << time.GetSeconds()
218 << "s" << std::endl;
219
220 if (nodeId == 0)
221 {
222 for (uint32_t i = 1; i < numNodes; ++i)
223 {
224 oss << GlobalAddr(i) << " dev 2 lladdr " << LLAddr(i) << " REACHABLE" << std::endl;
225 }
226 for (uint32_t i = 1; i < numNodes; ++i)
227 {
228 oss << LinkLocalAddr(i) << " dev 2 lladdr " << LLAddr(i) << " REACHABLE"
229 << std::endl;
230 }
231 }
232 else
233 {
234 oss << "fe80::200:ff:fe00:1 dev 2 lladdr 03-06-00:00:00:00:00:01 REACHABLE"
235 << std::endl;
236 }
237 }
238
239 return oss.str();
240}
241
242std::string
244{
245 std::ostringstream oss;
246
247 for (uint32_t nodeId = 0; nodeId < numNodes; ++nodeId)
248 {
249 oss << "6LoWPAN-ND Binding Table of node " << std::dec << nodeId << " at time +"
250 << time.GetSeconds() << "s" << std::endl;
251
252 if (nodeId == 0)
253 {
254 oss << "Interface 1:" << std::endl;
255 // Global addresses sort numerically before link-local (2001:: < fe80::),
256 // so emit all globals first, then all link-locals, both in node order.
257 for (uint32_t i = 1; i < numNodes; ++i)
258 {
259 oss << GlobalAddr(i) << " REACHABLE" << std::endl;
260 }
261 for (uint32_t i = 1; i < numNodes; ++i)
262 {
263 oss << LinkLocalAddr(i) << " REACHABLE" << std::endl;
264 }
265 }
266 }
267
268 return oss.str();
269}
270
271} // namespace ns3
return result
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static std::string LLAddr(uint32_t i)
Link-layer address for leaf node index i.
std::string SortRoutingTableString(std::string routingTable)
Sort the host-route entries in node 0's routing table block numerically.
std::string GenerateNdiscCacheOutput(uint32_t numNodes, Time time)
Generate expected NDisc cache output for a star topology.
static std::string GlobalAddr(uint32_t i)
Global unicast address for leaf node index i.
static std::string NodeHeader(uint32_t nodeId, Time time, const std::string &protocol)
Format a node/time/protocol header line as printed by ns-3 routing helpers.
std::string GenerateBindingTableOutput(uint32_t numNodes, Time time)
Generate expected binding table output for a star topology.
static std::string LinkLocalAddr(uint32_t i)
Link-local address for leaf node index i.
std::string NormalizeNdiscCacheStates(const std::string &ndiscOutput)
Replace STALE with REACHABLE in an NDisc cache output string.
std::string GenerateRoutingTableOutput(uint32_t numNodes, Time time)
Generate expected routing table output for a star topology.