Debian Codegraph @

protolib/examples/testFuncs.cpp

raw
1 #include "manetMsg.h"
2 #include "testFuncs.h"
3
4 #include <stdio.h> // for sprintf()
5 #include <stdlib.h> // for rand(), srand()
6 #include <ctype.h> // for isprint
7
8 int BuildPacket(UINT32* buffer, ManetPkt& pkt)
9 {
10 struct timeval currentTime;
11 ProtoSystemTime(currentTime);
12 srand(currentTime.tv_usec);
13
14 // 1) Test the generic pkt, msg, tlv stuff
15 PLOG(PL_INFO,"msgExample: Building a packet ...\n");
16
17 // a) Instantiate an "ManetPkt" from the stack
18 // LJT UINT32 buffer[PACKET_SIZE_MAX/4];
19 memset(buffer, 0, PACKET_SIZE_MAX/4);
20 // LJT ManetPkt pkt;
21 if (!pkt.InitIntoBuffer(buffer, PACKET_SIZE_MAX)) PLOG(PL_INFO,"ManetPkt::Init() error\n");
22
23 // Add a pkt-tlv
24 PLOG(PL_INFO," adding pkt-tlv to packet ...\n");
25 ManetTlv* tlv = pkt.AppendTlv(SMF_TLV_RTR_PRIORITY);
26
27
28 // b) Append a message to the pkt (multiple messages may go in an ManetPkt)
29 // (Note: the msg pointer returned here is invalid after another message is appended!)
30 PLOG(PL_INFO," adding message to packet ...\n");
31 ManetMsg* msg = pkt.AppendMessage();
32 if (NULL == msg)
33 {
34 PLOG(PL_INFO,"msgExample: ManetPkt::AppendMessage() error\n");
35 return -1;
36 }
37
38 // c) Set msg header fields
39 msg->SetType(SMF_HELLO);
40 ProtoAddress myAddr;
41 myAddr.ResolveFromString("192.168.1.1");
42 msg->SetOriginator(myAddr);
43
44
45 PLOG(PL_INFO," adding a msg-tlv : type:%u ...\n", SMF_TLV_RELAY_ALGORITHM);
46 // d) Append a message tlv ...
47 tlv = msg->AppendTlv(SMF_TLV_RELAY_ALGORITHM);
48 UINT8 relayAlgorithm = (UINT8)SMF_RELAY_SMPR;
49 tlv->SetValue(relayAlgorithm); // set TLV value
50
51 // Append another message tlv
52 PLOG(PL_INFO," adding another msg-tlv : type:%u ...\n", SMF_TLV_HELLO_INTERVAL);
53 tlv = msg->AppendTlv(SMF_TLV_HELLO_INTERVAL);
54 tlv->SetValue((UINT16)2); // set TLV value
55
56
57 PLOG(PL_INFO," adding an address block ...\n");
58 unsigned int numAddrs = 8;
59 // e) Add an address block
60 ManetAddrBlock* addrBlk = msg->AppendAddressBlock();
61 if (NULL == addrBlk)
62 {
63 PLOG(PL_INFO,"msgExample: ManetMsg::AppendAddressBlock() error\n");
64 return -1;
65 }
66
67 PLOG(PL_INFO," setting 2-byte address block head ...\n");
68 ProtoAddress prefix;
69 prefix.ResolveFromString("192.168.1.0");
70 if (!addrBlk->SetHead(prefix, 2))
71 {
72 PLOG(PL_INFO,"msgExample: addrBlk.SetHead() error\n");
73 return -1;
74 }
75 PLOG(PL_INFO," setting 1-byte address block tail ...\n");
76 prefix.ResolveFromString("192.168.1.1");
77 if (!addrBlk->SetTail(prefix, 1))
78 {
79 PLOG(PL_INFO,"msgExample: addrBlk.SetHead() error\n");
80 return -1;
81 }
82
83 // For testing, we add a batch of addresses w/ link status & "willingness" tlv's
84 // (Note that we build the tlv's into separate buffers to parallelize addr blk/tlv building)
85
86 // This one will be a "multi-value" tlv with different value for each address
87 UINT8 tlvType = (UINT8)SMF_TLV_LINK_STATUS;
88 UINT32 linkStatusTlvBuffer[256];
89 ManetTlv linkStatusTlv;
90 linkStatusTlv.InitIntoBuffer(tlvType, (char*)linkStatusTlvBuffer, 256*sizeof(UINT32));
91 linkStatusTlv.SetIndexRange(0, 7, true, numAddrs);
92
93 // This one will be a "single-value" tlv applied to a subset of addresses
94 tlvType = (UINT8)SMF_TLV_RELAY_WILLING;
95 UINT32 willingnessTlvBuffer[256];
96 ManetTlv willingnessTlv;
97 willingnessTlv.InitIntoBuffer(tlvType, (char*)willingnessTlvBuffer, 256*sizeof(UINT32));
98 willingnessTlv.SetIndexRange(2, 5, false, numAddrs);
99 willingnessTlv.SetValue((UINT8)2);
100
101 PLOG(PL_INFO," adding 8 addresses ...\n");
102 for (unsigned int i = 0; i < numAddrs; i++)
103 {
104 char addrString[32];
105 sprintf(addrString, "192.168.%d.1", i+2);
106 ProtoAddress neighbor;
107 neighbor.ResolveFromString(addrString);
108 addrBlk->AppendAddress(neighbor); // should check result
109 // We apply a separate tlv value to each address
110 // (Note these could be applied to a subset
111 // of the addresses if desired)
112 UINT8 linkStatus;
113 if (0 == (i & 0x01))
114 linkStatus = SMF_LINK_HEARD;
115 else
116 linkStatus = SMF_LINK_SYMMETRIC;
117 linkStatusTlv.SetValue(linkStatus, i, numAddrs);
118 }
119
120 PLOG(PL_INFO," adding addr-blk-tlvs ...\n");
121 // Copy the "pre-built" tlv's into the address block
122 if (!addrBlk->AppendTlv(linkStatusTlv))
123 {
124 PLOG(PL_INFO,"msgExample: ManetAddrBlk::AppendTlv(linkStatusTlv) error\n");
125 return -1;
126 }
127 if (!addrBlk->AppendTlv(willingnessTlv))
128 {
129 PLOG(PL_INFO,"msgExample: ManetAddrBlk::AppendTlv(willingnessTlv) error\n");
130 return -1;
131 }
132 // Add a non-indexed, single-value tlv to address block
133 tlv = addrBlk->AppendTlv(SMF_TLV_MPR_SELECT);
134 if (NULL == tlv)
135 {
136 PLOG(PL_INFO,"msgExample: ManetAddrBlk::AppendTlv() error\n");
137 return -1;
138 }
139 if (!tlv->SetValue((UINT8)1)) // set value to TRUE
140 {
141 PLOG(PL_INFO,"msgExample: ManetTlv::SetValue() error\n");
142 return -1;
143 }
144
145 // Add _another_ address block for testing purposes
146 PLOG(PL_INFO," adding another address block ...\n");
147 addrBlk = msg->AppendAddressBlock();
148 if (NULL == addrBlk)
149 {
150 PLOG(PL_INFO,"msgExample: ManetMsg::AppendAddressBlock() error\n");
151 return -1;
152 }
153 prefix.ResolveFromString("192.168.2.0");
154 PLOG(PL_INFO," setting 3-byte addr-blk head\n");
155 if (!addrBlk->SetHead(prefix, 3))
156 {
157 PLOG(PL_INFO,"msgExample: addrBlk.SetHead() error\n");
158 return -1;
159 }
160 // For testing, we add a batch of addresses
161 PLOG(PL_INFO," adding 4 addresses to addr-blk\n");
162 for (int i = 2; i < 6; i++)
163 {
164 char addrString[32];
165 sprintf(addrString, "192.168.2.%d", i);
166 ProtoAddress neighbor;
167 neighbor.ResolveFromString(addrString);
168 addrBlk->AppendAddress(neighbor);
169 }
170
171 // f) Finally, "pack" the packet to finalize structure
172 PLOG(PL_INFO," finalizing packet ...\n");
173 pkt.Pack();
174 PLOG(PL_INFO,"msgExample: Packet build completed, len:%u\n\n\n", pkt.GetLength());
175
176 return true;
177 } // BuildPacket
178 // Output buffer in INRIA PacketBB "hex dump" format
179 void MakeDump(char* buffer, unsigned int buflen)
180 {
181 PLOG(PL_INFO,"msgExample: outputting \"hex dump\" of packet ...\n");
182 for (unsigned int i = 0; i< buflen; i++)
183 {
184 printf("%02x ", (UINT8)buffer[i]);
185 if (0 == (i+1)%4) printf("\n");
186 }
187 printf("\n");
188 } // end MakeDump();
189
190 // Parse file containing PacketBB packet in INRIA PacketBB "hex dump" format
191 int ParseDump(const char* file)
192 {
193 FILE* infile = fopen(file, "r");
194 if (NULL == infile)
195 {
196 perror("msgExample: fopen() error");
197 return - 1;
198 }
199
200 UINT32 msgBuffer[1024];
201 unsigned int msgLength = 0;
202
203 unsigned int val;
204 while (1 == fscanf(infile, "%x", &val))
205 ((char*)msgBuffer)[msgLength++] = (char)val;
206
207 int result = ParseBuffer(msgBuffer, msgLength);
208
209 fclose(infile);
210 return result;
211 } // end ParseDump()
212
213 // Parse file containing PacketBB packet in INRIA PacketBB "hex dump" format
214 unsigned int ParseDump(const char* file, UINT32* msgBuffer)
215 {
216 FILE* infile = fopen(file, "r");
217 if (NULL == infile)
218 {
219 perror("msgExample: fopen() error");
220 return - 1;
221 }
222
223 //UINT32 msgBuffer[1024];
224 unsigned int msgLength = 0;
225
226 unsigned int val;
227 while (1 == fscanf(infile, "%x", &val))
228 {
229 ((char*)msgBuffer)[msgLength++] = (char)val;
230 }
231
232 if (ParseBuffer(msgBuffer, msgLength) != 0)
233 {
234 perror("testFuncs: ParseBuffer() error");
235 return -1;
236 }
237 fclose(infile);
238 return msgLength;
239 } // end ParseDump()
240
241 int ParseBuffer(UINT32* msgBuffer, unsigned int msgLength)
242 {
243 PLOG(PL_INFO,"msgExample: Parsing %u-byte packet buffer ...\n", msgLength);
244 // Parse/display Manet PacketBB Pkt
245 ManetPkt recvPkt; // "received" packet in "buffer" already ...
246 if (!recvPkt.InitFromBuffer(msgLength, msgBuffer, msgLength))
247 {
248 PLOG(PL_INFO,"msgExample: recvPkt.InitFromBuffer() error\n");
249 return -1;
250 }
251
252 // Iterate through any packet TLVs
253 ManetPkt::TlvIterator pktTlvIterator(recvPkt);
254
255 ManetTlv recvTlv;
256 while (pktTlvIterator.GetNextTlv(recvTlv))
257 {
258 PLOG(PL_INFO," found pkt-tlv, type:%u len:%lu\n", recvTlv.GetType(), recvTlv.GetTlvLength());
259 }
260
261 // Iterate through messages ...
262 ManetPkt::MsgIterator iterator(recvPkt);
263 ManetMsg recvMsg;
264
265 while (iterator.GetNextMessage(recvMsg))
266 {
267 PLOG(PL_INFO," found message, type:%d len:%d ", recvMsg.GetType(), recvMsg.GetLength());
268 if (recvMsg.HasOriginator())
269 {
270 ProtoAddress origin;
271 recvMsg.GetOriginator(origin);
272 PLOG(PL_INFO,"originator:%s", origin.GetHostString());
273 }
274 PLOG(PL_INFO,"\n");
275
276 // Iterate through any message tlv's ...
277 ManetMsg::TlvIterator iterator(recvMsg);
278 while (iterator.GetNextTlv(recvTlv))
279 {
280 PLOG(PL_INFO," got msg-tlv, type:%u len:%lu\n", recvTlv.GetType(), recvTlv.GetTlvLength());
281 }
282
283 // Iterate through any address blocks in the message
284 ManetMsg::AddrBlockIterator addrBlkIterator(recvMsg);
285 ManetAddrBlock recvAddrBlock;
286 while (addrBlkIterator.GetNextAddressBlock(recvAddrBlock))
287 {
288 PLOG(PL_INFO," got addr block len:%u w/ %u addresses (len:%u)\n",
289 recvAddrBlock.GetLength(), recvAddrBlock.GetAddressCount(), recvAddrBlock.GetAddressLength());
290 // Iterate through addresses in this block
291 unsigned int addrCount = recvAddrBlock.GetAddressCount();
292 for (unsigned int i = 0; i < addrCount; i++)
293 {
294 ProtoAddress addr;
295 if (recvAddrBlock.GetAddress(i, addr))
296 {
297 PLOG(PL_INFO," addr(%u): %s\n", i, addr.GetHostString());
298 }
299 else
300 {
301 PLOG(PL_INFO,"msgExample: ManetAddrBlock::GetAddress(%u) error\n", i);
302 return -1;
303 }
304 }
305
306 // Iterate through any tlv's associated with this block
307 ManetAddrBlock::TlvIterator iterator(recvAddrBlock);
308 while (iterator.GetNextTlv(recvTlv))
309 {
310 UINT8 start, stop;
311 if (recvTlv.HasIndex())
312 {
313 start = recvTlv.GetIndexStart();
314 stop = recvTlv.GetIndexStop(addrCount);
315
316 }
317 else
318 {
319 start = 0;
320 stop = addrCount - 1;
321 }
322 if (recvTlv.IsMultiValue())
323 {
324 PLOG(PL_INFO," found indexed, multi-value addr-blk-tlv, type:%d len:%u\n",
325 recvTlv.GetType(), recvTlv.GetLength());
326 }
327 else
328 {
329 PLOG(PL_INFO," found %sindexed single-value addr-blk-tlv, type:%d len:%u indices:%u-%u\n",
330 recvTlv.HasIndex() ? "" : "non-", recvTlv.GetType(), recvTlv.GetLength(),
331 start, stop);
332 start = stop; // to force print out of _single_ value
333 }
334 for (UINT8 index = start; index <= stop; index++)
335 {
336 switch (recvTlv.GetValueLength(addrCount))
337 {
338 case 1:
339 {
340 UINT8 val;
341 recvTlv.GetValue(val, index, addrCount);
342 PLOG(PL_INFO," value[%u] = 0x%02x\n", index, val);
343 break;
344 }
345 case 2:
346 {
347 UINT16 val;
348 recvTlv.GetValue(val, index, addrCount);
349 PLOG(PL_INFO," value[%u] = 0x%04x\n", index, val);
350 break;
351 }
352 case 4:
353 {
354 UINT32 val;
355 recvTlv.GetValue(val, index, addrCount);
356 PLOG(PL_INFO," value[%u] = 0x%08x\n", index, val);
357 break;
358 }
359 default:
360 {
361 UINT16 valueLength = recvTlv.GetValueLength(addrCount);
362 UINT8* vptr = (UINT8*)recvTlv.GetValuePtr();//valueLength, index, addrCount);
363 PLOG(PL_INFO," value[%u] = 0x\n", index);
364 for (unsigned int i = 0; i < valueLength; i++)
365 PLOG(PL_INFO,"%02x", *vptr++);
366 PLOG(PL_INFO,"\n");
367 break;
368 }
369 }
370 }
371 }
372 }
373 }
374 PLOG(PL_INFO,"msgExample: packet parsing completed.\n");
375 return 0;
376 } // end ParseBuffer()