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