1 /* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
2 /*
3 * Copyright (c) 1997 Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the Computer Systems
17 * Engineering Group at Lawrence Berkeley Laboratory.
18 * 4. Neither the name of the University nor of the Laboratory may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * Ported from CMU/Monarch's code, appropriate copyright applies.
35 * nov'98 -Padma.
36 *
37 * $Header: /cvsroot/nsnam/ns-2/trace/cmu-trace.cc,v 1.98 2011/10/02 22:32:35 tom_henderson Exp $
38 */
39
40 #include <packet.h>
41 #include <ip.h>
42 #include <tcp.h>
43 #include <sctp.h>
44 #include <rtp.h>
45 #include <arp.h>
46 #include <dsr/hdr_sr.h> // DSR
47 #include <mac.h>
48 #include <mac-802_11.h>
49 #include <smac.h>
50 #include <address.h>
51 #include <tora/tora_packet.h> //TORA
52 #include <imep/imep_spec.h> // IMEP
53 #include <aodv/aodv_packet.h> //AODV
54 #include <aomdv/aomdv_packet.h>
55 #include <mdart/mdart_packet.h>
56 #include <nsProtoManetKernel.h>
57 #include <mdart/mdart_function.h>
58 #include <cmu-trace.h>
59 #include <mobilenode.h>
60 #include <simulator.h>
61 //<zheng: add for 802.15.4>
62 #include "wpan/p802_15_4pkt.h"
63 #include "wpan/p802_15_4trace.h"
64 #include "wpan/p802_15_4nam.h"
65 //</zheng: add for 802.15.4>
66
67 #include "diffusion/diff_header.h" // DIFFUSION -- Chalermek
68
69
PacketTracer()70 PacketTracer::PacketTracer() : next_(0)
71 {
72 }
setNext(PacketTracer * next)73 void PacketTracer::setNext(PacketTracer *next)
74 {
75 next_ = next;
76 }
77
~PacketTracer()78 PacketTracer::~PacketTracer()
79 {
80 }
81
getNext()82 PacketTracer *PacketTracer::getNext()
83 {
84 return next_;
85 }
86
format_unknow(Packet * p,int offset,BaseTrace * pt,int newtrace)87 int PacketTracer::format_unknow(Packet *p, int offset, BaseTrace *pt, int newtrace)
88 {
89 return (format(p, offset, pt, newtrace) || (next_ && next_->format_unknow(p, offset, pt, newtrace)));
90 }
91
92 PacketTracer *CMUTrace::pktTrc_ = 0;
93
addPacketTracer(PacketTracer * pt)94 void CMUTrace::addPacketTracer(PacketTracer *pt)
95 {
96 if(!pt)
97 return;
98 pt->setNext(pktTrc_);
99 pktTrc_ = pt;
100 }
101
102
103 //#define LOG_POSITION
104
105 //extern char* pt_names[];
106
107 static class CMUTraceClass : public TclClass {
108 public:
CMUTraceClass()109 CMUTraceClass() : TclClass("CMUTrace") { }
create(int,const char * const * argv)110 TclObject* create(int, const char*const* argv) {
111 return (new CMUTrace(argv[4], *argv[5]));
112 }
113 } cmutrace_class;
114
115
116 //<zheng: ns 2.27 removed the following part, but we need it to control the broadcast radius>
117 double CMUTrace::bradius = 0.0;
118 double CMUTrace::radius_scaling_factor_ = 0.0;
119 double CMUTrace::duration_scaling_factor_ = 0.0;
120 //</zheng>
121
CMUTrace(const char * s,char t)122 CMUTrace::CMUTrace(const char *s, char t) : Trace(t)
123 {
124 bzero(tracename, sizeof(tracename));
125 strncpy(tracename, s, MAX_ID_LEN);
126
127 if(strcmp(tracename, "RTR") == 0) {
128 tracetype = TR_ROUTER;
129 }
130 else if(strcmp(tracename, "TRP") == 0) {
131 tracetype = TR_ROUTER;
132 }
133 else if(strcmp(tracename, "PHY") == 0) {
134 tracetype = TR_PHY;
135 }
136 else if(strcmp(tracename, "MAC") == 0) {
137 tracetype = TR_MAC;
138 }
139 else if(strcmp(tracename, "IFQ") == 0) {
140 tracetype = TR_IFQ;
141 }
142 else if(strcmp(tracename, "AGT") == 0) {
143 tracetype = TR_AGENT;
144 }
145 else {
146 fprintf(stderr, "CMU Trace Initialized with invalid type\n");
147 exit(1);
148 }
149 // change wrt Mike's code
150 // assert(type_ == DROP || type_ == SEND || type_ == RECV);
151 assert(type_ == DROP || type_ == SEND || type_ == RECV
152 || ((type_ == EOT) && (tracetype == TR_MAC)));
153
154
155
156 newtrace_ = 0;
157 for (int i=0 ; i < MAX_NODE ; i++)
158 nodeColor[i] = 3 ;
159 node_ = 0;
160 }
161
162 void
format_mac_common(Packet * p,const char * why,int offset)163 CMUTrace::format_mac_common(Packet *p, const char *why, int offset)
164 {
165 struct hdr_cmn *ch = HDR_CMN(p);
166 struct hdr_ip *ih = HDR_IP(p);
167 struct hdr_mac802_11 *mh;
168 struct hdr_smac *sh;
169 char mactype[SMALL_LEN];
170
171 strcpy(mactype, Simulator::instance().macType());
172 if (strcmp (mactype, "Mac/SMAC") == 0)
173 sh = HDR_SMAC(p);
174 else
175 mh = HDR_MAC802_11(p);
176
177 double x = 0.0, y = 0.0, z = 0.0;
178
179 char op = (char) type_;
180 Node* thisnode = Node::get_node_by_address(src_);
181 double energy = -1;
182 if (thisnode) {
183 if (thisnode->energy_model()) {
184 energy = thisnode->energy_model()->energy();
185 }
186 }
187
188 // hack the IP address to convert pkt format to hostid format
189 // for now until port ids are removed from IP address. -Padma.
190
191 int src = Address::instance().get_nodeaddr(ih->saddr());
192
193 if(tracetype == TR_ROUTER && type_ == SEND) {
194 if(src_ != src)
195 op = FWRD;
196 }
197
198 // use tagged format if appropriate
199 if (pt_->tagged()) {
200 int next_hop = -1 ;
201 Node* nextnode = Node::get_node_by_address(ch->next_hop_);
202 if (nextnode) next_hop = nextnode->nodeid();
203
204 node_->getLoc(&x, &y, &z);
205
206 if (op == DROP) op = 'd';
207 if (op == SEND) op = '+';
208 if (op == FWRD) op = 'h';
209
210 sprintf(pt_->buffer() + offset,
211 "%c "TIME_FORMAT" -s %d -d %d -p %s -k %3s -i %d "
212 "-N:loc {%.2f %.2f %.2f} -N:en %f ",
213
214 op, // event type
215 Scheduler::instance().clock(), // time
216 src_, // this node
217 next_hop, // next hop
218 packet_info.name(ch->ptype()), // packet type
219 tracename, // trace level
220 ch->uid(), // event id
221 x, y, z, // location
222 energy); // energy
223
224 offset = strlen(pt_->buffer());
225 if (strcmp (mactype, "Mac/SMAC") == 0) {
226 format_smac(p, offset);
227 } else {
228 format_mac(p, offset);
229 }
230 return;
231 }
232
233
234 // Use new ns trace format to replace the old cmu trace format)
235 if (newtrace_) {
236
237 node_->getLoc(&x, &y, &z);
238 // consistence
239 if ( op == DROP ) { op = 'd';}
240
241 // basic trace infomation + basic exenstion
242
243 sprintf(pt_->buffer() + offset,
244 "%c -t %.9f -Hs %d -Hd %d -Ni %d -Nx %.2f -Ny %.2f -Nz %.2f -Ne %f -Nl %3s -Nw %s ",
245 op, // event type
246 Scheduler::instance().clock(), // time
247 src_, // this node
248 ch->next_hop_, // next hop
249 src_, // this node
250 x, // x coordinate
251 y, // y coordinate
252 z, // z coordinate
253 energy, // energy, -1 = not existing
254 tracename, // trace level
255 why); // reason
256
257 // mac layer extension
258
259 offset = strlen(pt_->buffer());
260 if (strcmp(mactype, "Mac/SMAC") == 0) {
261 format_smac(p, offset);
262 } else {
263 format_mac(p, offset);
264 }
265 return;
266 }
267
268
269 #ifdef LOG_POSITION
270 x = 0.0, y = 0.0, z = 0.0;
271 node_->getLoc(&x, &y, &z);
272 #endif
273 sprintf(pt_->buffer() + offset,
274 #ifdef LOG_POSITION
275 "%c %.9f %d (%6.2f %6.2f) %3s %4s %d %s %d ",
276 #else
277 "%c %.9f _%d_ %3s %4s %d %s %d",
278 #endif
279 op,
280 Scheduler::instance().clock(),
281 src_, // this node
282 #ifdef LOG_POSITION
283 x,
284 y,
285 #endif
286 tracename,
287 why,
288
289 ch->uid(), // identifier for this event
290
291 ((ch->ptype() == PT_MAC) ? (
292 (mh->dh_fc.fc_type == MAC_Type_Control) ? (
293 (mh->dh_fc.fc_subtype == MAC_Subtype_RTS) ? "RTS" :
294 (mh->dh_fc.fc_subtype == MAC_Subtype_CTS) ? "CTS" :
295 (mh->dh_fc.fc_subtype == MAC_Subtype_ACK) ? "ACK":
296 //<zheng: add for 802.15.4>
297 (mh->dh_fc.fc_subtype == MAC_Subtype_Beacon) ? "BCN" : //Beacon
298 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoReq) ? "CM1" : //CMD: Association request
299 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoRsp) ? "CM2" : //CMD: Association response
300 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DAssNtf) ? "CM3" : //CMD: Disassociation notification
301 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DataReq) ? "CM4" : //CMD: Data request
302 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_PIDCNtf) ? "CM5" : //CMD: PAN ID conflict notification
303 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_OrphNtf) ? "CM6" : //CMD: Orphan notification
304 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_BconReq) ? "CM7" : //CMD: Beacon request
305 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_CoorRea) ? "CM8" : //CMD: Coordinator realignment
306 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_GTSReq) ? "CM9" : //CMD: GTS request
307 "UNKN") :
308 (mh->dh_fc.fc_type == MAC_Type_Management) ? (
309 (mh->dh_fc.fc_subtype == MAC_Subtype_80211_Beacon) ? "BCN" :
310 (mh->dh_fc.fc_subtype == MAC_Subtype_AssocReq) ? "ACRQ" :
311 (mh->dh_fc.fc_subtype == MAC_Subtype_AssocRep) ? "ACRP" :
312 (mh->dh_fc.fc_subtype == MAC_Subtype_Auth) ? "AUTH" :
313 (mh->dh_fc.fc_subtype == MAC_Subtype_ProbeReq) ? "PRRQ" :
314 (mh->dh_fc.fc_subtype == MAC_Subtype_ProbeRep) ? "PRRP" :
315 "UNKN") :
316 "UNKN") :
317 (ch->ptype() == PT_SMAC) ? (
318 (sh->type == RTS_PKT) ? "RTS" :
319 (sh->type == CTS_PKT) ? "CTS" :
320 (sh->type == ACK_PKT) ? "ACK" :
321 (sh->type == SYNC_PKT) ? "SYNC" :
322 "UNKN") :
323 packet_info.name(ch->ptype())),
324 ch->size());
325
326 offset = strlen(pt_->buffer());
327
328 if(tracetype == TR_PHY) {
329 format_phy(p, offset);
330 offset = strlen(pt_->buffer());
331 return;
332 }
333
334 if (strncmp (mactype, "Mac/SMAC", 8) == 0) {
335 format_smac(p, offset);
336 } else {
337 format_mac(p, offset);
338 }
339
340 offset = strlen(pt_->buffer());
341
342 if (thisnode) {
343 if (thisnode->energy_model()) {
344 // log detailed energy consumption
345 // total energy and breakdown in idle, sleep, transmit and receive modes
346 sprintf(pt_->buffer() + offset,
347 "[energy %f ei %.3f es %.3f et %.3f er %.3f] ",
348 thisnode->energy_model()->energy(),
349 thisnode->energy_model()->ei(),
350 thisnode->energy_model()->es(),
351 thisnode->energy_model()->et(),
352 thisnode->energy_model()->er());
353 }
354 }
355 }
356
357 void
format_phy(Packet *,int offset)358 CMUTrace::format_phy(Packet *, int offset)
359 {
360 sprintf(pt_->buffer() + offset, " ");
361 }
362
363
364 void
format_mac(Packet * p,int offset)365 CMUTrace::format_mac(Packet *p, int offset)
366 {
367 struct hdr_mac802_11 *mh = HDR_MAC802_11(p);
368 struct hdr_cmn *ch = HDR_CMN(p);
369 // This function assumes in some places that mh->dh_body points
370 // to an ethertype, which may not be true and causes some portability
371 // problems, so we zero the printing of this field in some cases
372 bool print_ether_type = true;
373 if ( (ch->ptype() == PT_MAC) &&
374 ( (mh->dh_fc.fc_type == MAC_Type_Control) ||
375 (mh->dh_fc.fc_type == MAC_Type_Management))) {
376 print_ether_type = false;
377 }
378
379 if (pt_->tagged()) {
380 sprintf(pt_->buffer() + offset,
381 "-M:dur %x -M:s %x -M:d %x -M:t %x ",
382 mh->dh_duration, // MAC: duration
383
384 // change wrt Mike's code
385 //ETHER_ADDR(mh->dh_da), // MAC: source
386 //ETHER_ADDR(mh->dh_sa), // MAC: destination
387 ETHER_ADDR(mh->dh_ra), // MAC: source
388 ETHER_ADDR(mh->dh_ta), // MAC: destination
389
390
391 print_ether_type ? GET_ETHER_TYPE(mh->dh_body) : 0); // MAC: type
392 } else if (newtrace_) {
393 sprintf(pt_->buffer() + offset,
394 "-Ma %x -Md %x -Ms %x -Mt %x ",
395 mh->dh_duration,
396
397 // change wrt Mike's code
398 //ETHER_ADDR(mh->dh_da),
399 //ETHER_ADDR(mh->dh_sa),
400
401 ETHER_ADDR(mh->dh_ra),
402 ETHER_ADDR(mh->dh_ta),
403
404 print_ether_type ? GET_ETHER_TYPE(mh->dh_body) : 0);
405 } else {
406 sprintf(pt_->buffer() + offset,
407 " [%x %x %x %x] ",
408 //*((u_int16_t*) &mh->dh_fc),
409 mh->dh_duration,
410
411 // change wrt Mike's code
412 //ETHER_ADDR(mh->dh_da),
413 //ETHER_ADDR(mh->dh_sa),
414 ETHER_ADDR(mh->dh_ra),
415 ETHER_ADDR(mh->dh_ta),
416 print_ether_type ? GET_ETHER_TYPE(mh->dh_body) : 0);
417 }
418 }
419
420 void
format_smac(Packet * p,int offset)421 CMUTrace::format_smac(Packet *p, int offset)
422 {
423 struct hdr_smac *sh = HDR_SMAC(p);
424 sprintf(pt_->buffer() + offset,
425 " [%.2f %d %d] ",
426 sh->duration,
427 sh->dstAddr,
428 sh->srcAddr);
429 }
430
431
432 void
format_ip(Packet * p,int offset)433 CMUTrace::format_ip(Packet *p, int offset)
434 {
435 struct hdr_cmn *ch = HDR_CMN(p);
436 struct hdr_ip *ih = HDR_IP(p);
437
438 // hack the IP address to convert pkt format to hostid format
439 // for now until port ids are removed from IP address. -Padma.
440 int src = Address::instance().get_nodeaddr(ih->saddr());
441 int dst = Address::instance().get_nodeaddr(ih->daddr());
442
443 if (pt_->tagged()) {
444 sprintf(pt_->buffer() + offset,
445 "-IP:s %d -IP:sp %d -IP:d %d -IP:dp %d -p %s -e %d "
446 "-c %d -i %d -IP:ttl %d ",
447 src, // packet src
448 ih->sport(), // src port
449 dst, // packet dest
450 ih->dport(), // dst port
451 packet_info.name(ch->ptype()), // packet type
452 ch->size(), // packet size
453 ih->flowid(), // flow id
454 ch->uid(), // unique id
455 ih->ttl_ // ttl
456 );
457 } else if (newtrace_) {
458 sprintf(pt_->buffer() + offset,
459 "-Is %d.%d -Id %d.%d -It %s -Il %d -If %d -Ii %d -Iv %d ",
460 src, // packet src
461 ih->sport(), // src port
462 dst, // packet dest
463 ih->dport(), // dst port
464 packet_info.name(ch->ptype()), // packet type
465 ch->size(), // packet size
466 ih->flowid(), // flow id
467 ch->uid(), // unique id
468 ih->ttl_); // ttl
469 } else {
470 sprintf(pt_->buffer() + offset, "------- [%d:%d %d:%d %d %d] ",
471 src, ih->sport(),
472 dst, ih->dport(),
473 ih->ttl_, (ch->next_hop_ < 0) ? 0 : ch->next_hop_);
474 }
475 }
476
477 // Note: HDLC format (format_hdlc()) has moved to satellite tracing
478
479 void
format_arp(Packet * p,int offset)480 CMUTrace::format_arp(Packet *p, int offset)
481 {
482 struct hdr_arp *ah = HDR_ARP(p);
483
484 if (pt_->tagged()) {
485 sprintf(pt_->buffer() + offset,
486 "-arp:op %s -arp:ms %d -arp:s %d -arp:md %d -arp:d %d ",
487 ah->arp_op == ARPOP_REQUEST ? "REQUEST" : "REPLY",
488 ah->arp_sha,
489 ah->arp_spa,
490 ah->arp_tha,
491 ah->arp_tpa);
492 } else if (newtrace_) {
493 sprintf(pt_->buffer() + offset,
494 "-P arp -Po %s -Pms %d -Ps %d -Pmd %d -Pd %d ",
495 ah->arp_op == ARPOP_REQUEST ? "REQUEST" : "REPLY",
496 ah->arp_sha,
497 ah->arp_spa,
498 ah->arp_tha,
499 ah->arp_tpa);
500 } else {
501
502 sprintf(pt_->buffer() + offset,
503 "------- [%s %d/%d %d/%d]",
504 ah->arp_op == ARPOP_REQUEST ? "REQUEST" : "REPLY",
505 ah->arp_sha,
506 ah->arp_spa,
507 ah->arp_tha,
508 ah->arp_tpa);
509 }
510 }
511
512 void
format_dsr(Packet * p,int offset)513 CMUTrace::format_dsr(Packet *p, int offset)
514 {
515 hdr_sr *srh = hdr_sr::access(p);
516 int last_err_index = 0;
517 int last_reply_index = 0;
518
519 if (srh->num_route_errors() > 1) {
520 last_err_index = srh->num_route_errors() - 1;
521 }
522 if (srh->route_reply_len() > 1) {
523 last_reply_index = srh->route_reply_len() - 1;
524 }
525
526 if (pt_->tagged()) {
527 sprintf(pt_->buffer() + offset,
528 "-dsr:h %d -dsr:q %d -dsr:s %d -dsr:p %d -dsr:n %d "
529 "-dsr:l %d -dsr:e {%d %d} -dsr:w %d -dsr:m %d -dsr:c %d "
530 "-dsr:b {%d %d} ",
531 srh->num_addrs(),
532 srh->route_request(),
533 srh->rtreq_seq(),
534 srh->route_reply(),
535 srh->rtreq_seq(),
536 srh->route_reply_len(),
537 srh->reply_addrs()[0].addr,
538 srh->reply_addrs()[last_reply_index].addr,
539 srh->route_error(),
540 srh->num_route_errors(),
541 srh->down_links()[last_err_index].tell_addr,
542 srh->down_links()[last_err_index].from_addr,
543 srh->down_links()[last_err_index].to_addr);
544 return;
545 } else if (newtrace_) {
546 sprintf(pt_->buffer() + offset,
547 "-P dsr -Ph %d -Pq %d -Ps %d -Pp %d -Pn %d -Pl %d -Pe %d->%d -Pw %d -Pm %d -Pc %d -Pb %d->%d ",
548 srh->num_addrs(), // how many nodes travered
549
550 srh->route_request(),
551 srh->rtreq_seq(),
552
553 srh->route_reply(),
554 srh->rtreq_seq(),
555 srh->route_reply_len(),
556 // the dest of the src route
557 srh->reply_addrs()[0].addr,
558 srh->reply_addrs()[last_reply_index].addr,
559
560 srh->route_error(),
561 srh->num_route_errors(),
562 srh->down_links()[last_err_index].tell_addr,
563 srh->down_links()[last_err_index].from_addr,
564 srh->down_links()[last_err_index].to_addr);
565
566 return;
567 }
568 sprintf(pt_->buffer() + offset,
569 "%d [%d %d] [%d %d %d %d->%d] [%d %d %d %d->%d]",
570 srh->num_addrs(),
571
572 srh->route_request(),
573 srh->rtreq_seq(),
574
575 srh->route_reply(),
576 srh->rtreq_seq(),
577 srh->route_reply_len(),
578 // the dest of the src route
579 srh->reply_addrs()[0].addr,
580 srh->reply_addrs()[last_reply_index].addr,
581
582 srh->route_error(),
583 srh->num_route_errors(),
584 srh->down_links()[last_err_index].tell_addr,
585 srh->down_links()[last_err_index].from_addr,
586 srh->down_links()[last_err_index].to_addr);
587 }
588
589 void
format_msg(Packet *,int)590 CMUTrace::format_msg(Packet *, int)
591 {
592 }
593
594 void
format_tcp(Packet * p,int offset)595 CMUTrace::format_tcp(Packet *p, int offset)
596 {
597 struct hdr_cmn *ch = HDR_CMN(p);
598 struct hdr_tcp *th = HDR_TCP(p);
599
600 if (pt_->tagged()) {
601 sprintf(pt_->buffer() + offset,
602 "-tcp:s %d -tcp:a %d -tcp:f %d -tcp:o %d ",
603 th->seqno_,
604 th->ackno_,
605 ch->num_forwards(),
606 ch->opt_num_forwards());
607 } else if (newtrace_) {
608 sprintf(pt_->buffer() + offset,
609 "-Pn tcp -Ps %d -Pa %d -Pf %d -Po %d ",
610 th->seqno_,
611 th->ackno_,
612 ch->num_forwards(),
613 ch->opt_num_forwards());
614
615 } else {
616 sprintf(pt_->buffer() + offset,
617 "[%d %d] %d %d",
618 th->seqno_,
619 th->ackno_,
620 ch->num_forwards(),
621 ch->opt_num_forwards());
622 }
623 }
624
625 /* Armando L. Caro Jr. <acaro@@cis,udel,edu> 6/5/2002
626 * (with help from Florina Almenrez <florina@@it,uc3m,es>)
627 */
628 void
format_sctp(Packet * p,int offset)629 CMUTrace::format_sctp(Packet* p,int offset)
630 {
631 struct hdr_cmn *ch = HDR_CMN(p);
632 struct hdr_sctp *sh = HDR_SCTP(p);
633 //struct hdr_ip *ih = HDR_IP(p);
634 char cChunkType;
635
636 for(u_int i = 0; i < sh->NumChunks(); i++) {
637 switch(sh->SctpTrace()[i].eType) {
638 case SCTP_CHUNK_INIT:
639 case SCTP_CHUNK_INIT_ACK:
640 case SCTP_CHUNK_COOKIE_ECHO:
641 case SCTP_CHUNK_COOKIE_ACK:
642 cChunkType = 'I'; // connection initialization
643 break;
644
645 case SCTP_CHUNK_DATA:
646 cChunkType = 'D';
647 break;
648
649 case SCTP_CHUNK_SACK:
650 cChunkType = 'S';
651 break;
652
653 case SCTP_CHUNK_FORWARD_TSN:
654 cChunkType = 'R';
655 break;
656
657 case SCTP_CHUNK_HB:
658 cChunkType = 'H';
659 break;
660
661 case SCTP_CHUNK_HB_ACK:
662 cChunkType = 'B';
663 break;
664 default:
665 // quiet compiler
666 cChunkType = ' ';
667 assert (false);
668 break;
669 }
670
671 if( newtrace_ ) {
672 sprintf(pt_->buffer() + offset,
673 "-Pn sctp -Pnc %d -Pct %c "
674 "-Ptsn %d -Psid %d -Pssn %d "
675 "-Pf %d -Po %d ",
676 sh->NumChunks(),
677 cChunkType,
678 sh->SctpTrace()[i].uiTsn,
679 sh->SctpTrace()[i].usStreamId,
680 sh->SctpTrace()[i].usStreamSeqNum,
681 ch->num_forwards(),
682 ch->opt_num_forwards());
683 }
684 else {
685 sprintf(pt_->buffer() + offset,
686 "[%d %c %d %d %d] %d %d",
687 sh->NumChunks(),
688 cChunkType,
689 sh->SctpTrace()[i].uiTsn,
690 sh->SctpTrace()[i].usStreamId,
691 sh->SctpTrace()[i].usStreamSeqNum,
692 ch->num_forwards(),
693 ch->opt_num_forwards());
694 }
695 }
696 }
697
698 void
format_rtp(Packet * p,int offset)699 CMUTrace::format_rtp(Packet *p, int offset)
700 {
701 struct hdr_cmn *ch = HDR_CMN(p);
702 struct hdr_rtp *rh = HDR_RTP(p);
703 struct hdr_ip *ih = HDR_IP(p);
704 Node* thisnode = Node::get_node_by_address(src_);
705
706 //hacking, needs to change later,
707 int dst = Address::instance().get_nodeaddr(ih->daddr());
708
709 if (dst == src_){
710 // I just received a cbr data packet
711 if (thisnode->energy_model() &&
712 thisnode->energy_model()->powersavingflag()) {
713 thisnode->energy_model()->set_node_state(EnergyModel::INROUTE);
714 }
715 }
716
717 if (pt_->tagged()) {
718 sprintf(pt_->buffer() + offset,
719 "-cbr:s %d -cbr:f %d -cbr:o %d ",
720 rh->seqno_,
721 ch->num_forwards(),
722 ch->opt_num_forwards());
723 } else if (newtrace_) {
724 sprintf(pt_->buffer() + offset,
725 "-Pn cbr -Pi %d -Pf %d -Po %d ",
726 rh->seqno_,
727 ch->num_forwards(),
728 ch->opt_num_forwards());
729 } else {
730 sprintf(pt_->buffer() + offset,
731 "[%d] %d %d",
732 rh->seqno_,
733 ch->num_forwards(),
734 ch->opt_num_forwards());
735 }
736 }
737
738 void
format_imep(Packet * p,int offset)739 CMUTrace::format_imep(Packet *p, int offset)
740 {
741 struct hdr_imep *im = HDR_IMEP(p);
742
743 #define U_INT16_T(x) *((u_int16_t*) &(x))
744
745 if (pt_->tagged()) {
746 sprintf(pt_->buffer() + offset,
747 "-imep:a %c -imep:h %c -imep:o %c -imep:l %04x ",
748 (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
749 (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
750 (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
751 U_INT16_T(im->imep_length));
752 } else if (newtrace_) {
753 sprintf(pt_->buffer() + offset,
754 "-P imep -Pa %c -Ph %c -Po %c -Pl 0x%04x ] ",
755 (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
756 (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
757 (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
758 U_INT16_T(im->imep_length));
759 } else {
760 sprintf(pt_->buffer() + offset,
761 "[%c %c %c 0x%04x] ",
762 (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
763 (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
764 (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
765 U_INT16_T(im->imep_length));
766 }
767 #undef U_INT16_T
768 }
769
770
771 void
format_tora(Packet * p,int offset)772 CMUTrace::format_tora(Packet *p, int offset)
773 {
774 struct hdr_tora *th = HDR_TORA(p);
775 struct hdr_tora_qry *qh = HDR_TORA_QRY(p);
776 struct hdr_tora_upd *uh = HDR_TORA_UPD(p);
777 struct hdr_tora_clr *ch = HDR_TORA_CLR(p);
778
779 switch(th->th_type) {
780
781 case TORATYPE_QRY:
782
783 if (pt_->tagged()) {
784 sprintf(pt_->buffer() + offset,
785 "-tora:t %x -tora:d %d -tora:c QUERY",
786 qh->tq_type, qh->tq_dst);
787 } else if (newtrace_) {
788 sprintf(pt_->buffer() + offset,
789 "-P tora -Pt 0x%x -Pd %d -Pc QUERY ",
790 qh->tq_type, qh->tq_dst);
791
792 } else {
793
794 sprintf(pt_->buffer() + offset, "[0x%x %d] (QUERY)",
795 qh->tq_type, qh->tq_dst);
796 }
797 break;
798
799 case TORATYPE_UPD:
800
801 if (pt_->tagged()) {
802 sprintf(pt_->buffer() + offset,
803 "-tora:t %x -tora:d %d -tora:a %f -tora:o %d "
804 "-tora:r %d -tora:e %d -tora:i %d -tora:c UPDATE",
805 uh->tu_type,
806 uh->tu_dst,
807 uh->tu_tau,
808 uh->tu_oid,
809 uh->tu_r,
810 uh->tu_delta,
811 uh->tu_id);
812 } else if (newtrace_) {
813 sprintf(pt_->buffer() + offset,
814 "-P tora -Pt 0x%x -Pd %d (%f %d %d %d %d) -Pc UPDATE ",
815 uh->tu_type,
816 uh->tu_dst,
817 uh->tu_tau,
818 uh->tu_oid,
819 uh->tu_r,
820 uh->tu_delta,
821 uh->tu_id);
822 } else {
823 sprintf(pt_->buffer() + offset,
824 "-Pt 0x%x -Pd %d -Pa %f -Po %d -Pr %d -Pe %d -Pi %d -Pc UPDATE ",
825 uh->tu_type,
826 uh->tu_dst,
827 uh->tu_tau,
828 uh->tu_oid,
829 uh->tu_r,
830 uh->tu_delta,
831 uh->tu_id);
832 }
833 break;
834
835 case TORATYPE_CLR:
836 if (pt_->tagged()) {
837 sprintf(pt_->buffer() + offset,
838 "-tora:t %x -tora:d %d -tora:a %f -tora:o %d "
839 "-tora:c CLEAR ",
840 ch->tc_type,
841 ch->tc_dst,
842 ch->tc_tau,
843 ch->tc_oid);
844 } else if (newtrace_) {
845 sprintf(pt_->buffer() + offset,
846 "-P tora -Pt 0x%x -Pd %d -Pa %f -Po %d -Pc CLEAR ",
847 ch->tc_type,
848 ch->tc_dst,
849 ch->tc_tau,
850 ch->tc_oid);
851 } else {
852 sprintf(pt_->buffer() + offset, "[0x%x %d %f %d] (CLEAR)",
853 ch->tc_type,
854 ch->tc_dst,
855 ch->tc_tau,
856 ch->tc_oid);
857 }
858 break;
859 }
860 }
861
862 void
format_aodv(Packet * p,int offset)863 CMUTrace::format_aodv(Packet *p, int offset)
864 {
865 struct hdr_aodv *ah = HDR_AODV(p);
866 struct hdr_aodv_request *rq = HDR_AODV_REQUEST(p);
867 struct hdr_aodv_reply *rp = HDR_AODV_REPLY(p);
868
869
870 switch(ah->ah_type) {
871 case AODVTYPE_RREQ:
872
873 if (pt_->tagged()) {
874 sprintf(pt_->buffer() + offset,
875 "-aodv:t %x -aodv:h %d -aodv:b %d -aodv:d %d "
876 "-aodv:ds %d -aodv:s %d -aodv:ss %d "
877 "-aodv:c REQUEST ",
878 rq->rq_type,
879 rq->rq_hop_count,
880 rq->rq_bcast_id,
881 rq->rq_dst,
882 rq->rq_dst_seqno,
883 rq->rq_src,
884 rq->rq_src_seqno);
885 } else if (newtrace_) {
886
887 sprintf(pt_->buffer() + offset,
888 "-P aodv -Pt 0x%x -Ph %d -Pb %d -Pd %d -Pds %d -Ps %d -Pss %d -Pc REQUEST ",
889 rq->rq_type,
890 rq->rq_hop_count,
891 rq->rq_bcast_id,
892 rq->rq_dst,
893 rq->rq_dst_seqno,
894 rq->rq_src,
895 rq->rq_src_seqno);
896
897
898 } else {
899
900 sprintf(pt_->buffer() + offset,
901 "[0x%x %d %d [%d %d] [%d %d]] (REQUEST)",
902 rq->rq_type,
903 rq->rq_hop_count,
904 rq->rq_bcast_id,
905 rq->rq_dst,
906 rq->rq_dst_seqno,
907 rq->rq_src,
908 rq->rq_src_seqno);
909 }
910 break;
911
912 case AODVTYPE_RREP:
913 case AODVTYPE_HELLO:
914 case AODVTYPE_RERR:
915
916 if (pt_->tagged()) {
917 sprintf(pt_->buffer() + offset,
918 "-aodv:t %x -aodv:h %d -aodv:d %d -adov:ds %d "
919 "-aodv:l %f -aodv:c %s ",
920 rp->rp_type,
921 rp->rp_hop_count,
922 rp->rp_dst,
923 rp->rp_dst_seqno,
924 rp->rp_lifetime,
925 rp->rp_type == AODVTYPE_RREP ? "REPLY" :
926 (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
927 "HELLO"));
928 } else if (newtrace_) {
929
930 sprintf(pt_->buffer() + offset,
931 "-P aodv -Pt 0x%x -Ph %d -Pd %d -Pds %d -Pl %f -Pc %s ",
932 rp->rp_type,
933 rp->rp_hop_count,
934 rp->rp_dst,
935 rp->rp_dst_seqno,
936 rp->rp_lifetime,
937 rp->rp_type == AODVTYPE_RREP ? "REPLY" :
938 (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
939 "HELLO"));
940 } else {
941
942 sprintf(pt_->buffer() + offset,
943 "[0x%x %d [%d %d] %f] (%s)",
944 rp->rp_type,
945 rp->rp_hop_count,
946 rp->rp_dst,
947 rp->rp_dst_seqno,
948 rp->rp_lifetime,
949 rp->rp_type == AODVTYPE_RREP ? "REPLY" :
950 (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
951 "HELLO"));
952 }
953 break;
954
955 default:
956 #ifdef WIN32
957 fprintf(stderr,
958 "CMUTrace::format_aodv: invalid AODV packet type\n");
959 #else
960 fprintf(stderr,
961 "%s: invalid AODV packet type\n", __FUNCTION__);
962 #endif
963 abort();
964 }
965 }
966
967 // AOMDV patch
968 void
format_aomdv(Packet * p,int offset)969 CMUTrace::format_aomdv(Packet *p, int offset)
970 {
971 struct hdr_aomdv *ah = HDR_AOMDV(p);
972 struct hdr_aomdv_request *rq = HDR_AOMDV_REQUEST(p);
973 struct hdr_aomdv_reply *rp = HDR_AOMDV_REPLY(p);
974
975
976 switch(ah->ah_type) {
977 case AOMDVTYPE_RREQ:
978
979 if (pt_->tagged()) {
980 sprintf(pt_->buffer() + offset,
981 "-aomdv:t %x -aomdv:h %d -aomdv:b %d -aomdv:d %d "
982 "-aomdv:ds %d -aomdv:s %d -aomdv:ss %d "
983 "-aomdv:c REQUEST ",
984 rq->rq_type,
985 rq->rq_hop_count,
986 rq->rq_bcast_id,
987 rq->rq_dst,
988 rq->rq_dst_seqno,
989 rq->rq_src,
990 rq->rq_src_seqno);
991 } else if (newtrace_) {
992
993 sprintf(pt_->buffer() + offset,
994 "-P aomdv -Pt 0x%x -Ph %d -Pb %d -Pd %d -Pds %d -Ps %d -Pss %d -Pc REQUEST ",
995 rq->rq_type,
996 rq->rq_hop_count,
997 rq->rq_bcast_id,
998 rq->rq_dst,
999 rq->rq_dst_seqno,
1000 rq->rq_src,
1001 rq->rq_src_seqno);
1002
1003
1004 } else {
1005
1006 sprintf(pt_->buffer() + offset,
1007 "[0x%x %d %d [%d %d] [%d %d]] (REQUEST)",
1008 rq->rq_type,
1009 rq->rq_hop_count,
1010 rq->rq_bcast_id,
1011 rq->rq_dst,
1012 rq->rq_dst_seqno,
1013 rq->rq_src,
1014 rq->rq_src_seqno);
1015 }
1016 break;
1017
1018 case AOMDVTYPE_RREP:
1019 case AOMDVTYPE_HELLO:
1020 case AOMDVTYPE_RERR:
1021
1022 if (pt_->tagged()) {
1023 sprintf(pt_->buffer() + offset,
1024 "-aomdv:t %x -aomdv:h %d -aomdv:d %d -admov:ds %d "
1025 "-aomdv:l %f -aomdv:c %s ",
1026 rp->rp_type,
1027 rp->rp_hop_count,
1028 rp->rp_dst,
1029 rp->rp_dst_seqno,
1030 rp->rp_lifetime,
1031 rp->rp_type == AOMDVTYPE_RREP ? "REPLY" :
1032 (rp->rp_type == AOMDVTYPE_RERR ? "ERROR" :
1033 "HELLO"));
1034 } else if (newtrace_) {
1035
1036 sprintf(pt_->buffer() + offset,
1037 "-P aomdv -Pt 0x%x -Ph %d -Pd %d -Pds %d -Pl %f -Pc %s ",
1038 rp->rp_type,
1039 rp->rp_hop_count,
1040 rp->rp_dst,
1041 rp->rp_dst_seqno,
1042 rp->rp_lifetime,
1043 rp->rp_type == AOMDVTYPE_RREP ? "REPLY" :
1044 (rp->rp_type == AOMDVTYPE_RERR ? "ERROR" :
1045 "HELLO"));
1046 } else {
1047 sprintf(pt_->buffer() + offset,
1048 "[0x%x %d [%d %d] %f] (%s) [%d %d]",
1049 rp->rp_type,
1050 rp->rp_hop_count,
1051 rp->rp_dst,
1052 rp->rp_dst_seqno,
1053 rp->rp_lifetime,
1054 rp->rp_type == AODVTYPE_RREP ? "REPLY" :
1055 (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
1056 "HELLO"),
1057 rp->rp_bcast_id,
1058 rp->rp_first_hop
1059 );
1060 }
1061 break;
1062
1063 default:
1064 #ifdef WIN32
1065 fprintf(stderr,
1066 "CMUTrace::format_aomdv: invalid AODV packet type\n");
1067 #else
1068 fprintf(stderr,
1069 "%s: invalid AOMDV packet type\n", __FUNCTION__);
1070 #endif
1071 abort();
1072 }
1073 }
1074
1075 void
format_mdart(Packet * p,int offset)1076 CMUTrace::format_mdart(Packet *p, int offset)
1077 {
1078 struct hdr_mdart *rh = HDR_MDART(p);
1079 struct hdr_mdart_hello *rhHello = HDR_MDART_HELLO(p);
1080 struct hdr_mdart_darq *rhDarq = HDR_MDART_DARQ(p);
1081 struct hdr_mdart_darp *rhDarp = HDR_MDART_DARP(p);
1082 struct hdr_mdart_daup *rhDaup = HDR_MDART_DAUP(p);
1083 // struct hdr_mdart_encp *rhEncp = HDR_MDART_ENCP(p);
1084 switch(rh->type_) {
1085 case MDART_TYPE_HELLO:
1086 {
1087 bitset<ADDR_SIZE> tempSetSrcAdd_ (rhHello->srcAdd_);
1088 string tempSrcAdd_ = tempSetSrcAdd_.to_string();
1089 bitset<ADDR_SIZE> tempSetDstAdd_ (rhHello->dstAdd_);
1090 string tempDstAdd_ = tempSetDstAdd_.to_string();
1091 if (pt_->tagged()) {
1092 sprintf(pt_->buffer() + offset, "-mdart:t %x -mdart:dAdd %d -mdart:sAdd %d -mdart:sId %d", rhHello->type_, rhHello->dstAdd_, rhHello->srcAdd_, rhHello->srcId_);
1093 } else if (newtrace_) {
1094 sprintf(pt_->buffer() + offset, "-type HELLO -srcId %d -srcAdd %s dstAdd %s -seqNum %d", rhHello->srcId_, tempSrcAdd_.c_str(), tempDstAdd_.c_str(), rhHello->seqNum_);
1095 } else {
1096 sprintf(pt_->buffer() + offset, "[0x%x [%d %d] [%d]]", rhHello->type_, rhHello->dstAdd_, rhHello->srcAdd_, rhHello->srcId_);
1097 }
1098 break;
1099 }
1100 case MDART_TYPE_DARQ:
1101 {
1102 bitset<ADDR_SIZE> tempSetSrcAdd_ (rhDarq->srcAdd_);
1103 string tempSrcAdd_ = tempSetSrcAdd_.to_string();
1104 bitset<ADDR_SIZE> tempSetForAdd_ (rhDarq->forAdd_);
1105 string tempForAdd_ = tempSetForAdd_.to_string();
1106 bitset<ADDR_SIZE> tempSetDstAdd_ (rhDarq->dstAdd_);
1107 string tempDstAdd_ = tempSetDstAdd_.to_string();
1108 if (pt_->tagged()) {
1109 sprintf(pt_->buffer() + offset, "-mdart:t %x -mdart:dstAdd %d -mdart:srcAdd %d -mdart:dstId %d -mdart:srcId %d -mdart:forAdd %d -mdart:forId %d -mdart:rId %d -mdart:pId %u -mdart:c DARQ", rhDarq->type_, rhDarq->dstAdd_, rhDarq->srcAdd_, rhDarq->dstId_, rhDarq->srcId_, rhDarq->forAdd_, rhDarq->forId_, rhDarq->reqId_, rhDarq->seqNum_);//, rhDarq->reqpktId_);
1110 } else if (newtrace_) {
1111 sprintf(pt_->buffer() + offset, "-type DARQ -srcId %d -srcAdd %s -forId %d -forAdd %s -dstId %d dstAdd %s -reqId %d -seqNum %d", rhDarq->srcId_, tempSrcAdd_.c_str(), rhDarq->forId_, tempForAdd_.c_str(), rhDarq->dstId_, tempDstAdd_.c_str(), rhDarq->reqId_, rhDarq->seqNum_);
1112 } else {
1113 sprintf(pt_->buffer() + offset, "[0x%x [%d %d] [%d %d] [%d %d] [%d] %u] (DARQ)", rhDarq->type_, rhDarq->dstAdd_, rhDarq->srcAdd_, rhDarq->dstId_, rhDarq->srcId_, rhDarq->forAdd_, rhDarq->forId_, rhDarq->reqId_, rhDarq->seqNum_);//, rhDarq->reqpktId_);
1114 }
1115 break;
1116 }
1117 case MDART_TYPE_DARP:
1118 {
1119 bitset<ADDR_SIZE> tempSetSrcAdd_ (rhDarp->srcAdd_);
1120 string tempSrcAdd_ = tempSetSrcAdd_.to_string();
1121 bitset<ADDR_SIZE> tempSetForAdd_ (rhDarp->forAdd_);
1122 string tempForAdd_ = tempSetForAdd_.to_string();
1123 bitset<ADDR_SIZE> tempSetDstAdd_ (rhDarp->dstAdd_);
1124 string tempDstAdd_ = tempSetDstAdd_.to_string();
1125 bitset<ADDR_SIZE> tempSetReqAdd_ (rhDarp->reqAdd_);
1126 string tempReqAdd_ = tempSetReqAdd_.to_string();
1127 if (pt_->tagged()) {
1128 sprintf(pt_->buffer() + offset, "-mdart:t %x -mdart:dAdd %d -mdart:sAdd %d -mdart:dId %d -mdart:sId %d -mdart:fAdd %d -mdart:fId %d -mdart:rAdd %d -mdart:rId %d -mdart:pAId %d mdart:c DARP", rhDarp->type_, rhDarp->dstAdd_, rhDarp->srcAdd_, rhDarp->dstId_, rhDarp->srcId_, rhDarp->forAdd_, rhDarp->forId_, rhDarp->reqAdd_, rhDarp->reqId_, rhDarp->seqNum_);//, rhDarp->reqpktId_);
1129 } else if (newtrace_) {
1130 sprintf(pt_->buffer() + offset, "-type DARP -srcId %d -srcAdd %s -forId %d -forAdd %s -dstId %d dstAdd %s -reqId %d -reqAdd %s -seqNum %d", rhDarp->srcId_, tempSrcAdd_.c_str(), rhDarp->forId_, tempForAdd_.c_str(), rhDarp->dstId_, tempDstAdd_.c_str(), rhDarp->reqId_, tempReqAdd_.c_str(), rhDarp->seqNum_);
1131 } else {
1132 sprintf(pt_->buffer() + offset, "[0x%x [%d %d] [%d %d] [%d %d] [%d %d] %u] (DARP)", rhDarp->type_, rhDarp->dstAdd_, rhDarp->srcAdd_, rhDarp->dstId_, rhDarp->srcId_, rhDarp->forAdd_, rhDarp->forId_, rhDarp->reqAdd_, rhDarp->reqId_, rhDarp->seqNum_);//, rhDarp->reqpktId_);
1133 }
1134 break;
1135 }
1136 case MDART_TYPE_DAUP:
1137 {
1138 bitset<ADDR_SIZE> tempSetSrcAdd_ (rhDaup->srcAdd_);
1139 string tempSrcAdd_ = tempSetSrcAdd_.to_string();
1140 bitset<ADDR_SIZE> tempSetForAdd_ (rhDaup->forAdd_);
1141 string tempForAdd_ = tempSetForAdd_.to_string();
1142 bitset<ADDR_SIZE> tempSetDstAdd_ (rhDaup->dstAdd_);
1143 string tempDstAdd_ = tempSetDstAdd_.to_string();
1144 if (pt_->tagged()) {
1145 sprintf(pt_->buffer() + offset, "-mdart:t %x -mdart:dAdd %d -mdart:sAdd %d -mdart:dId %d -mdart:sId %d -mdart:fAdd %d -mdart:fId %d -mdart:pAId %u -mdart:c DAUP", rhDaup->type_, rhDaup->dstAdd_, rhDaup->srcAdd_, rhDaup->dstId_, rhDaup->srcId_, rhDaup->forAdd_, rhDaup->forId_, rhDaup->seqNum_);
1146 } else if (newtrace_) {
1147 sprintf(pt_->buffer() + offset, "-type DAUP -srcId %d -srcAdd %s -forId %d -forAdd %s -dstId %d dstAdd %s -seqNum %d", rhDaup->srcId_, tempSrcAdd_.c_str(), rhDaup->forId_, tempForAdd_.c_str(), rhDaup->dstId_, tempDstAdd_.c_str(), rhDaup->seqNum_);
1148 } else {
1149 sprintf(pt_->buffer() + offset, "[0x%x [%d %d] [%d %d] [%d %d] %u] (DAUP)", rhDaup->type_, rhDaup->dstAdd_, rhDaup->srcAdd_, rhDaup->dstId_, rhDaup->srcId_, rhDaup->forAdd_, rhDaup->forId_, rhDaup->seqNum_);
1150 }
1151 break;
1152 }
1153 case MDART_TYPE_DABR:
1154 {
1155 bitset<ADDR_SIZE> tempSetSrcAdd_ (rhDarq->srcAdd_);
1156 string tempSrcAdd_ = tempSetSrcAdd_.to_string();
1157 bitset<ADDR_SIZE> tempSetDstAdd_ (rhDarq->dstAdd_);
1158 string tempDstAdd_ = tempSetDstAdd_.to_string();
1159 if (pt_->tagged()) {
1160 sprintf(pt_->buffer() + offset, "-mdart:t %x -mdart:dAdd %d -mdart:sAdd %d -mdart:dId %d -mdart:sId %d -mdart:fAdd %d -mdart:fId %d -mdart:pAId %u -mdart:c DAUP", rhDaup->type_, rhDaup->dstAdd_, rhDaup->srcAdd_, rhDaup->dstId_, rhDaup->srcId_, rhDaup->forAdd_, rhDaup->forId_, rhDaup->seqNum_);
1161 } else if (newtrace_) {
1162 sprintf(pt_->buffer() + offset, "-type DABR -srcId %d -srcAdd %s dstAdd %s", rhDarq->srcId_, tempSrcAdd_.c_str(), tempDstAdd_.c_str());
1163 } else {
1164 sprintf(pt_->buffer() + offset, "[0x%x [%d %d] [%d %d] [%d %d] %u] (DAUP)", rhDaup->type_, rhDaup->dstAdd_, rhDaup->srcAdd_, rhDaup->dstId_, rhDaup->srcId_, rhDaup->forAdd_, rhDaup->forId_, rhDaup->seqNum_);
1165 }
1166 break;
1167 }
1168 }
1169 }
1170
1171 void
format_protolibmk(Packet * p,int offset)1172 CMUTrace::format_protolibmk(Packet *p, int offset) {return;}
1173
1174 void
nam_format(Packet * p,int offset)1175 CMUTrace::nam_format(Packet *p, int offset)
1176 {
1177 Node* srcnode = 0 ;
1178 Node* nextnode = 0 ;
1179 struct hdr_cmn *ch = HDR_CMN(p);
1180 struct hdr_ip *ih = HDR_IP(p);
1181 char op = (char) type_;
1182 char colors[32];
1183 int next_hop = -1 ;
1184
1185 // change wrt Mike's code
1186 assert(type_ != EOT);
1187
1188
1189
1190 //<zheng: add for 802.15.4>
1191
1192 //Actually we only need to handle MAC layer for nam (but should display dropping for other layers)
1193 //if (strcmp(tracename,"MAC") != 0)
1194 //if ((op != 'D')&&(op != 'd'))
1195 // return;
1196
1197 struct hdr_mac802_11 *mh = HDR_MAC802_11(p);
1198 char ptype[11];
1199 strcpy(ptype,
1200 ((ch->ptype() == PT_MAC) ? (
1201 (mh->dh_fc.fc_subtype == MAC_Subtype_RTS) ? "RTS" :
1202 (mh->dh_fc.fc_subtype == MAC_Subtype_CTS) ? "CTS" :
1203 (mh->dh_fc.fc_subtype == MAC_Subtype_ACK) ? "ACK" :
1204 (mh->dh_fc.fc_subtype == MAC_Subtype_Beacon) ? "BCN" : //Beacon
1205 (mh->dh_fc.fc_subtype == MAC_Subtype_AssocReq) ? "ACRQ" :
1206 (mh->dh_fc.fc_subtype == MAC_Subtype_AssocRep) ? "ACRP" :
1207 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoReq) ? "CM1" : //CMD: Association request
1208 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoRsp) ? "CM2" : //CMD: Association response
1209 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DAssNtf) ? "CM3" : //CMD: Disassociation notification
1210 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DataReq) ? "CM4" : //CMD: Data request
1211 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_PIDCNtf) ? "CM5" : //CMD: PAN ID conflict notification
1212 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_OrphNtf) ? "CM6" : //CMD: Orphan notification
1213 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_BconReq) ? "CM7" : //CMD: Beacon request
1214 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_CoorRea) ? "CM8" : //CMD: Coordinator realignment
1215 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_GTSReq) ? "CM9" : //CMD: GTS request
1216 "UNKN"
1217 ) : packet_info.name(ch->ptype())));
1218 //</zheng: add for 802.15.4>
1219 int dst = Address::instance().get_nodeaddr(ih->daddr());
1220
1221 nextnode = Node::get_node_by_address(ch->next_hop_);
1222 if (nextnode) next_hop = nextnode->nodeid();
1223
1224 srcnode = Node::get_node_by_address(src_);
1225
1226 double energy = -1;
1227 double initenergy = -1;
1228
1229 //default value for changing node color with respect to energy depletion
1230 double l1 = 0.5;
1231 double l2 = 0.2;
1232
1233 if (srcnode) {
1234 if (srcnode->energy_model()) {
1235 energy = srcnode->energy_model()->energy();
1236 initenergy = srcnode->energy_model()->initialenergy();
1237 l1 = srcnode->energy_model()->level1();
1238 l2 = srcnode->energy_model()->level2();
1239 }
1240 }
1241
1242 int energyLevel = 0 ;
1243 double energyLeft = (double)(energy/initenergy) ;
1244
1245 if ((energyLeft <= 1 ) && (energyLeft >= l1 )) energyLevel = 3;
1246 if ((energyLeft >= l2 ) && (energyLeft < l1 )) energyLevel = 2;
1247 if ((energyLeft > 0 ) && (energyLeft < l2 )) energyLevel = 1;
1248
1249 if (energyLevel == 0)
1250 strcpy(colors,"-c black -o red");
1251 else if (energyLevel == 1)
1252 strcpy(colors,"-c red -o yellow");
1253 else if (energyLevel == 2)
1254 strcpy(colors,"-c yellow -o green");
1255 else if (energyLevel == 3)
1256 strcpy(colors,"-c green -o black");
1257
1258 // A simple hack for scadds demo (fernandez's visit) -- Chalermek
1259 int pkt_color = 0;
1260 if (ch->ptype()==PT_DIFF) {
1261 hdr_cdiff *dfh= HDR_CDIFF(p);
1262 if (dfh->mess_type != DATA) {
1263 pkt_color = 1;
1264 }
1265 }
1266
1267 //<zheng: add for 802.15.4>
1268 if (Nam802_15_4::Nam_Status)
1269 {
1270 ATTRIBUTELINK *attr;
1271 int t_src,t_dst;
1272 if (ch->ptype() == PT_MAC)
1273 {
1274 t_src = p802_15_4macSA(p);
1275 t_dst = p802_15_4macDA(p);;
1276 }
1277 else
1278 {
1279 t_src = HDR_IP(p)->saddr();
1280 t_dst = HDR_IP(p)->daddr();
1281 }
1282 attr = findAttrLink(HDR_CMN(p)->ptype(),t_src,t_dst);
1283 if (attr == NULL)
1284 attr = findAttrLink(HDR_CMN(p)->ptype());
1285 if (attr != NULL)
1286 HDR_LRWPAN(p)->attribute = attr->attribute;
1287 else
1288 HDR_LRWPAN(p)->attribute = 0;
1289 if (HDR_LRWPAN(p)->attribute >= 32)
1290 pkt_color = HDR_LRWPAN(p)->attribute;
1291 }
1292 //</zheng: add for 802.15.4>
1293
1294 // convert to nam format
1295 if (op == 's') op = 'h' ;
1296 if (op == 'D') op = 'd' ;
1297 if (op == 'h') {
1298 sprintf(pt_->nbuffer(),
1299 "+ -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s ",
1300 Scheduler::instance().clock(),
1301 src_, // this node
1302 next_hop,
1303 ptype, //<zheng: modify for 802.15.4>packet_info.name(ch->ptype()),
1304 ch->size(),
1305 pkt_color,
1306 ch->uid(),
1307 tracename);
1308
1309 offset = strlen(pt_->nbuffer());
1310 pt_->namdump();
1311 sprintf(pt_->nbuffer() ,
1312 "- -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s",
1313 Scheduler::instance().clock(),
1314 src_, // this node
1315 next_hop,
1316 ptype, //<zheng: modify for 802.15.4>packet_info.name(ch->ptype()),
1317 ch->size(),
1318 pkt_color,
1319 ch->uid(),
1320 tracename);
1321
1322 offset = strlen(pt_->nbuffer());
1323 pt_->namdump();
1324 }
1325
1326 // if nodes are too far from each other
1327 // nam won't dump SEND event 'cuz it's
1328 // gonna be dropped later anyway
1329 // this value 250 is pre-calculated by using
1330 // two-ray ground refelction model with fixed
1331 // transmission power 3.652e-10
1332 // if ((type_ == SEND) && (distance > 250 )) return ;
1333
1334 if(tracetype == TR_ROUTER && type_ == RECV && dst != -1 ) return ;
1335 if(type_ == RECV && dst == -1 )dst = src_ ; //broadcasting event
1336
1337 if (energy != -1) { //energy model being turned on
1338 if (src_ >= MAX_NODE) {
1339 fprintf (stderr, "node id must be < %d\n",
1340 MAX_NODE);
1341 exit(0);
1342 }
1343 if (nodeColor[src_] != energyLevel ) { //only dump it when node
1344 sprintf(pt_->nbuffer() , //color change
1345 "n -t %.9f -s %d -S COLOR %s",
1346 Scheduler::instance().clock(),
1347 src_, // this node
1348 colors);
1349 offset = strlen(pt_->nbuffer());
1350 pt_->namdump();
1351 nodeColor[src_] = energyLevel ;
1352 }
1353 }
1354
1355 sprintf(pt_->nbuffer() ,
1356 "%c -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s",
1357 op,
1358 Scheduler::instance().clock(),
1359 src_, // this node
1360 next_hop,
1361 ptype, //<zheng: modify for 802.15.4>packet_info.name(ch->ptype()),
1362 ch->size(),
1363 pkt_color,
1364 ch->uid(),
1365 tracename);
1366
1367 //<zheng: ns 2.27 removed the following part, but we need it to control the broadcast radius>
1368 if (Nam802_15_4::Nam_Status)
1369 {
1370 if ((strcmp(tracename, "AGT") != 0) || ((u_int32_t)(ih->daddr()) == IP_BROADCAST)) //<zheng: add: next_hop info not available at agent level>
1371 //(doesn't really matter -- seems agent level has no effect on nam)
1372 if (next_hop == -1 && op == 'h') {
1373 // print extra fields for broadcast packets
1374
1375 // bradius is calculated assuming 2-ray ground reflectlon
1376 // model using default settings of Phy/WirelessPhy and
1377 // Antenna/OmniAntenna
1378 if (bradius == 0.0) calculate_broadcast_parameters();
1379
1380 double radius = bradius*radius_scaling_factor_;
1381
1382 // duration is calculated based on the radius and
1383 // the speed of light (299792458 m/s)
1384 double duration = (bradius/299792458.0)*duration_scaling_factor_;
1385 //<zheng: add -- the duration in 802.15.4 could be very small and rounded to 0.0>
1386 if (Nam802_15_4::Nam_Status)
1387 if (duration < 0.000000001)
1388 duration = 0.000000001;
1389 //</zheng: add>
1390 sprintf(pt_->nbuffer() + strlen(pt_->nbuffer()),
1391 " -R %.2f -D %.2f",
1392 radius,
1393 duration);
1394 }
1395 }
1396 //</zheng>
1397
1398 offset = strlen(pt_->nbuffer());
1399 pt_->namdump();
1400 }
1401
format(Packet * p,const char * why)1402 void CMUTrace::format(Packet* p, const char *why)
1403 {
1404 hdr_cmn *ch = HDR_CMN(p);
1405 int offset = 0;
1406
1407 /*
1408 * Log the MAC Header
1409 */
1410 format_mac_common(p, why, offset);
1411
1412 if (pt_->namchannel())
1413 nam_format(p, offset);
1414 offset = strlen(pt_->buffer());
1415 switch(ch->ptype()) {
1416 case PT_MAC:
1417 case PT_SMAC:
1418 break;
1419 case PT_ARP:
1420 format_arp(p, offset);
1421 break;
1422 default:
1423 format_ip(p, offset);
1424 offset = strlen(pt_->buffer());
1425 switch(ch->ptype()) {
1426 case PT_AODV:
1427 format_aodv(p, offset);
1428 break;
1429 // AOMDV patch
1430 case PT_AOMDV:
1431 format_aomdv(p, offset);
1432 break;
1433 case PT_TORA:
1434 format_tora(p, offset);
1435 break;
1436 case PT_IMEP:
1437 format_imep(p, offset);
1438 break;
1439 case PT_DSR:
1440 format_dsr(p, offset);
1441 break;
1442 case PT_MESSAGE:
1443 case PT_UDP:
1444 format_msg(p, offset);
1445 break;
1446 case PT_TCP:
1447 case PT_ACK:
1448 format_tcp(p, offset);
1449 break;
1450 case PT_SCTP:
1451 /* Armando L. Caro Jr. <acaro@@cis,udel,edu> 6/5/2002
1452 */
1453 format_sctp(p, offset);
1454 break;
1455 case PT_CBR:
1456 format_rtp(p, offset);
1457 break;
1458 case PT_DIFF:
1459 break;
1460 case PT_GAF:
1461 case PT_PING:
1462 break;
1463 default:
1464
1465 if(pktTrc_ && pktTrc_->format_unknow(p, offset, pt_, newtrace_))
1466 break;
1467
1468 /*<zheng: del -- there are many more new packet types added, like PT_EXP (poisson traffic belongs to this type)>
1469 fprintf(stderr, "%s - invalid packet type (%s).\n",
1470 __PRETTY_FUNCTION__, packet_info.name(ch->ptype()));
1471 exit(1);
1472 </zheng: del>*/
1473 break; //zheng: add
1474 }
1475 }
1476 }
1477
1478 int
command(int argc,const char * const * argv)1479 CMUTrace::command(int argc, const char*const* argv)
1480 {
1481
1482 if(argc == 3) {
1483 if(strcmp(argv[1], "node") == 0) {
1484 node_ = (MobileNode*) TclObject::lookup(argv[2]);
1485 if(node_ == 0)
1486 return TCL_ERROR;
1487 return TCL_OK;
1488 }
1489 if (strcmp(argv[1], "newtrace") == 0) {
1490 newtrace_ = atoi(argv[2]);
1491 return TCL_OK;
1492 }
1493 }
1494 return Trace::command(argc, argv);
1495 }
1496
1497 /*ARGSUSED*/
1498 void
recv(Packet * p,Handler * h)1499 CMUTrace::recv(Packet *p, Handler *h)
1500 {
1501 if (!node_energy()) {
1502 Packet::free(p);
1503 return;
1504 }
1505 assert(initialized());
1506 /*
1507 * Agent Trace "stamp" the packet with the optimal route on
1508 * sending.
1509 */
1510 if (tracetype == TR_AGENT && type_ == SEND) {
1511 God::instance()->stampPacket(p);
1512 }
1513 #if 0
1514 /*
1515 * When the originator of a packet drops the packet, it may or may
1516 * not have been stamped by GOD. Stamp it before logging the
1517 * information.
1518 */
1519 if(src_ == src && type_ == DROP) {
1520 God::instance()->stampPacket(p);
1521 }
1522 #endif
1523 format(p, "---");
1524 pt_->dump();
1525 //namdump();
1526 if(target_ == 0)
1527 Packet::free(p);
1528 else
1529 send(p, h);
1530 }
1531
1532 void
recv(Packet * p,const char * why)1533 CMUTrace::recv(Packet *p, const char* why)
1534 {
1535 assert(initialized() && type_ == DROP);
1536 if (!node_energy()) {
1537 Packet::free(p);
1538 return;
1539 }
1540 #if 0
1541 /*
1542 * When the originator of a packet drops the packet, it may or may
1543 * not have been stamped by GOD. Stamp it before logging the
1544 * information.
1545 */
1546 if(src_ == ih->saddr()) {
1547 God::instance()->stampPacket(p);
1548 }
1549 #endif
1550 format(p, why);
1551 pt_->dump();
1552 //namdump();
1553 Packet::free(p);
1554 }
1555
node_energy()1556 int CMUTrace::node_energy()
1557 {
1558 Node* thisnode = Node::get_node_by_address(src_);
1559 double energy = 1;
1560 if (thisnode) {
1561 if (thisnode->energy_model()) {
1562 energy = thisnode->energy_model()->energy();
1563 }
1564 }
1565 if (energy > 0) return 1;
1566 return 0;
1567 }
1568
1569 //<zheng: ns 2.27 removed the following part, but we need it to control the broadcast radius>
calculate_broadcast_parameters()1570 void CMUTrace::calculate_broadcast_parameters() {
1571 // Calculate the maximum distance at which a packet can be received
1572 // based on the two-ray reflection model using the current default
1573 // values for Phy/WirelessPhy and Antenna/OmniAntenna.
1574
1575 double P_t, P_r, G_t, G_r, h, L;
1576 Tcl& tcl = Tcl::instance();
1577
1578 tcl.evalc("Phy/WirelessPhy set Pt_");
1579 P_t = atof(tcl.result());
1580 tcl.evalc("Phy/WirelessPhy set RXThresh_");
1581 P_r = atof(tcl.result());
1582 tcl.evalc("Phy/WirelessPhy set L_");
1583 L = atof(tcl.result());
1584 tcl.evalc("Antenna/OmniAntenna set Gt_");
1585 G_t = atof(tcl.result());
1586 tcl.evalc("Antenna/OmniAntenna set Gr_");
1587 G_r = atof(tcl.result());
1588 tcl.evalc("Antenna/OmniAntenna set Z_");
1589 h = atof(tcl.result());
1590 bradius = pow(P_t*G_r*G_t*pow(h,4.0)/(P_r*L), 0.25);
1591 //<zheng: add for 802.15.4>
1592 //the above calculation is not accurate for short distance
1593 double PI,freq,lambda,crossover_dist;
1594 PI = 3.14159265359;
1595 tcl.evalc("Phy/WirelessPhy set freq_");
1596 freq = atof(tcl.result());
1597 lambda = 3.0e8/freq;
1598 crossover_dist = (4 * PI * h * h) / lambda;
1599 if (bradius < crossover_dist) //need re-calculation
1600 bradius = pow(P_t * G_r * G_t * pow(lambda, 2.0)/(P_r * L), 0.5)/(4 * PI);
1601 //</zheng: add for 802.15.4>
1602
1603 // Also get the scaling factors
1604 tcl.evalc("CMUTrace set radius_scaling_factor_");
1605 radius_scaling_factor_ = atof(tcl.result());
1606 tcl.evalc("CMUTrace set duration_scaling_factor_");
1607 duration_scaling_factor_ = atof(tcl.result());
1608 }
1609 //</zheng>
1610