Debian Codegraph @

protolib/src/sim/ns/ns234/packet.h

raw
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 * @(#) $Header: /cvsroot/nsnam/ns-2/common/packet.h,v 1.103 2009/01/15 06:23:49 tom_henderson Exp $ (LBL)
35 */
36
37 #ifndef ns_packet_h
38 #define ns_packet_h
39
40 #include <string.h>
41 #include <assert.h>
42
43 #include "config.h"
44 #include "scheduler.h"
45 #include "object.h"
46 #include "lib/bsd-list.h"
47 #include "packet-stamp.h"
48 #include "ns-process.h"
49
50 // Used by wireless routing code to attach routing agent
51 #define RT_PORT 255 /* port that all route msgs are sent to */
52
53 #define HDR_CMN(p) (hdr_cmn::access(p))
54 #define HDR_ARP(p) (hdr_arp::access(p))
55 #define HDR_MAC(p) (hdr_mac::access(p))
56 #define HDR_MAC802_11(p) ((hdr_mac802_11 *)hdr_mac::access(p))
57 #define HDR_MAC_TDMA(p) ((hdr_mac_tdma *)hdr_mac::access(p))
58 #define HDR_SMAC(p) ((hdr_smac *)hdr_mac::access(p))
59 #define HDR_LL(p) (hdr_ll::access(p))
60 #define HDR_HDLC(p) ((hdr_hdlc *)hdr_ll::access(p))
61 #define HDR_IP(p) (hdr_ip::access(p))
62 #define HDR_RTP(p) (hdr_rtp::access(p))
63 #define HDR_TCP(p) (hdr_tcp::access(p))
64 #define HDR_SCTP(p) (hdr_sctp::access(p))
65 #define HDR_SR(p) (hdr_sr::access(p))
66 #define HDR_TFRC(p) (hdr_tfrc::access(p))
67 #define HDR_TORA(p) (hdr_tora::access(p))
68 #define HDR_IMEP(p) (hdr_imep::access(p))
69 #define HDR_CDIFF(p) (hdr_cdiff::access(p)) /* chalermak's diffusion*/
70 //#define HDR_DIFF(p) (hdr_diff::access(p)) /* SCADD's diffusion ported into ns */
71 #define HDR_LMS(p) (hdr_lms::access(p))
72 #define HDR_PROTOLIBMK(p) (hdr_protolibmk::access(p))
73 /* --------------------------------------------------------------------*/
74
75 /*
76 * modified ns-2.33, adding support for dynamic libraries
77 *
78 * packet_t is changed from enum to unsigned int in order to allow
79 * dynamic definition of new packet types within dynamic libraries.
80 * Pre-defined packet types are implemented as static const.
81 *
82 */
83
84 typedef unsigned int packet_t;
85
86 static const packet_t PT_TCP = 0;
87 static const packet_t PT_UDP = 1;
88 static const packet_t PT_CBR = 2;
89 static const packet_t PT_AUDIO = 3;
90 static const packet_t PT_VIDEO = 4;
91 static const packet_t PT_ACK = 5;
92 static const packet_t PT_START = 6;
93 static const packet_t PT_STOP = 7;
94 static const packet_t PT_PRUNE = 8;
95 static const packet_t PT_GRAFT = 9;
96 static const packet_t PT_GRAFTACK = 10;
97 static const packet_t PT_JOIN = 11;
98 static const packet_t PT_ASSERT = 12;
99 static const packet_t PT_MESSAGE = 13;
100 static const packet_t PT_RTCP = 14;
101 static const packet_t PT_RTP = 15;
102 static const packet_t PT_RTPROTO_DV = 16;
103 static const packet_t PT_CtrMcast_Encap = 17;
104 static const packet_t PT_CtrMcast_Decap = 18;
105 static const packet_t PT_SRM = 19;
106 /* simple signalling messages */
107 static const packet_t PT_REQUEST = 20;
108 static const packet_t PT_ACCEPT = 21;
109 static const packet_t PT_CONFIRM = 22;
110 static const packet_t PT_TEARDOWN = 23;
111 static const packet_t PT_LIVE = 24; // packet from live network
112 static const packet_t PT_REJECT = 25;
113
114 static const packet_t PT_TELNET = 26; // not needed: telnet use TCP
115 static const packet_t PT_FTP = 27;
116 static const packet_t PT_PARETO = 28;
117 static const packet_t PT_EXP = 29;
118 static const packet_t PT_INVAL = 30;
119 static const packet_t PT_HTTP = 31;
120
121 /* new encapsulator */
122 static const packet_t PT_ENCAPSULATED = 32;
123 static const packet_t PT_MFTP = 33;
124
125 /* CMU/Monarch's extnsions */
126 static const packet_t PT_ARP = 34;
127 static const packet_t PT_MAC = 35;
128 static const packet_t PT_TORA = 36;
129 static const packet_t PT_DSR = 37;
130 static const packet_t PT_AODV = 38;
131 static const packet_t PT_IMEP = 39;
132
133 // RAP packets
134 static const packet_t PT_RAP_DATA = 40;
135 static const packet_t PT_RAP_ACK = 41;
136
137 static const packet_t PT_TFRC = 42;
138 static const packet_t PT_TFRC_ACK = 43;
139 static const packet_t PT_PING = 44;
140
141 static const packet_t PT_PBC = 45;
142 // Diffusion packets - Chalermek
143 static const packet_t PT_DIFF = 46;
144
145 // LinkState routing update packets
146 static const packet_t PT_RTPROTO_LS = 47;
147
148 // MPLS LDP header
149 static const packet_t PT_LDP = 48;
150
151 // GAF packet
152 static const packet_t PT_GAF = 49;
153
154 // ReadAudio traffic
155 static const packet_t PT_REALAUDIO = 50;
156
157 // Pushback Messages
158 static const packet_t PT_PUSHBACK = 51;
159
160 #ifdef HAVE_STL
161 // Pragmatic General Multicast
162 static const packet_t PT_PGM = 52;
163 #endif //STL
164 // LMS packets
165 static const packet_t PT_LMS = 53;
166 static const packet_t PT_LMS_SETUP = 54;
167
168 static const packet_t PT_SCTP = 55;
169 static const packet_t PT_SCTP_APP1 = 56;
170
171 // SMAC packet
172 static const packet_t PT_SMAC = 57;
173 // XCP packet
174 static const packet_t PT_XCP = 58;
175
176 // HDLC packet
177 static const packet_t PT_HDLC = 59;
178
179 // Bell Labs Traffic Trace Type (PackMime OL)
180 static const packet_t PT_BLTRACE = 60;
181
182 // AOMDV packet
183 static const packet_t PT_AOMDV = 61;
184
185 static const packet_t PT_PROTOLIBMK = 62;
186 // insert new packet types here
187 static packet_t PT_NTYPE = 63; // This MUST be the LAST one
188
189 enum packetClass
190 {
191 UNCLASSIFIED,
192 ROUTING,
193 DATApkt
194 };
195
196
197 /*
198 * ns-2.33 adding support for dynamic libraries
199 *
200 * The PacketClassifier class is needed to make
201 * p_info::data_packet(packet_t) work also with dynamically defined
202 * packet types.
203 *
204 */
205 class PacketClassifier
206 {
207 public:
208 PacketClassifier(): next_(0){}
209 virtual ~PacketClassifier() {}
210 void setNext(PacketClassifier *next){next_ = next;}
211 PacketClassifier *getNext(){return next_;}
212 packetClass classify(packet_t type)
213 {
214 packetClass c = getClass(type);
215 if(c == UNCLASSIFIED && next_)
216 c = next_->classify(type);
217 return c;
218 }
219
220 protected:
221 //return 0 if the packet is unknown
222 virtual packetClass getClass(packet_t type) = 0;
223 PacketClassifier *next_;
224 };
225
226 class p_info {
227 public:
228 p_info()
229 {
230 initName();
231 }
232 const char* name(packet_t p) const {
233 if ( p <= p_info::nPkt_ ) return name_[p];
234 return 0;
235 }
236 static bool data_packet(packet_t type) {
237 return ( (type) == PT_TCP || \
238 (type) == PT_TELNET || \
239 (type) == PT_CBR || \
240 (type) == PT_AUDIO || \
241 (type) == PT_VIDEO || \
242 (type) == PT_ACK || \
243 (type) == PT_SCTP || \
244 (type) == PT_SCTP_APP1 || \
245 (type) == PT_HDLC \
246 );
247 }
248 static packetClass classify(packet_t type) {
249 if (type == PT_DSR ||
250 type == PT_MESSAGE ||
251 type == PT_TORA ||
252 type == PT_AODV ||
253 type == PT_PROTOLIBMK )
254 return ROUTING;
255 if (type == PT_TCP ||
256 type == PT_TELNET ||
257 type == PT_CBR ||
258 type == PT_AUDIO ||
259 type == PT_VIDEO ||
260 type == PT_ACK ||
261 type == PT_SCTP ||
262 type == PT_SCTP_APP1 ||
263 type == PT_HDLC)
264 return DATApkt;
265 if (pc_)
266 return pc_->classify(type);
267 return UNCLASSIFIED;
268 }
269 static void addPacketClassifier(PacketClassifier *pc)
270 {
271 if(!pc)
272 return;
273 pc->setNext(pc_);
274 pc_ = pc;
275 }
276 static void initName()
277 {
278 if(nPkt_ >= PT_NTYPE+1)
279 return;
280 char **nameNew = new char*[PT_NTYPE+1];
281 for(unsigned int i = (unsigned int)PT_SMAC+1; i < nPkt_; i++)
282 {
283 nameNew[i] = name_[i];
284 }
285 if(!nPkt_)
286 delete [] name_;
287 name_ = nameNew;
288 nPkt_ = PT_NTYPE+1;
289
290
291 name_[PT_TCP]= "tcp";
292 name_[PT_UDP]= "udp";
293 name_[PT_CBR]= "cbr";
294 name_[PT_AUDIO]= "audio";
295 name_[PT_VIDEO]= "video";
296 name_[PT_ACK]= "ack";
297 name_[PT_START]= "start";
298 name_[PT_STOP]= "stop";
299 name_[PT_PRUNE]= "prune";
300 name_[PT_GRAFT]= "graft";
301 name_[PT_GRAFTACK]= "graftAck";
302 name_[PT_JOIN]= "join";
303 name_[PT_ASSERT]= "assert";
304 name_[PT_MESSAGE]= "message";
305 name_[PT_RTCP]= "rtcp";
306 name_[PT_RTP]= "rtp";
307 name_[PT_RTPROTO_DV]= "rtProtoDV";
308 name_[PT_CtrMcast_Encap]= "CtrMcast_Encap";
309 name_[PT_CtrMcast_Decap]= "CtrMcast_Decap";
310 name_[PT_SRM]= "SRM";
311
312 name_[PT_REQUEST]= "sa_req";
313 name_[PT_ACCEPT]= "sa_accept";
314 name_[PT_CONFIRM]= "sa_conf";
315 name_[PT_TEARDOWN]= "sa_teardown";
316 name_[PT_LIVE]= "live";
317 name_[PT_REJECT]= "sa_reject";
318
319 name_[PT_TELNET]= "telnet";
320 name_[PT_FTP]= "ftp";
321 name_[PT_PARETO]= "pareto";
322 name_[PT_EXP]= "exp";
323 name_[PT_INVAL]= "httpInval";
324 name_[PT_HTTP]= "http";
325 name_[PT_ENCAPSULATED]= "encap";
326 name_[PT_MFTP]= "mftp";
327 name_[PT_ARP]= "ARP";
328 name_[PT_MAC]= "MAC";
329 name_[PT_TORA]= "TORA";
330 name_[PT_DSR]= "DSR";
331 name_[PT_AODV]= "AODV";
332 name_[PT_IMEP]= "IMEP";
333 name_[PT_PROTOLIBMK]= "ProtolibMK";
334
335 name_[PT_RAP_DATA] = "rap_data";
336 name_[PT_RAP_ACK] = "rap_ack";
337
338 name_[PT_TFRC]= "tcpFriend";
339 name_[PT_TFRC_ACK]= "tcpFriendCtl";
340 name_[PT_PING]="ping";
341
342 name_[PT_PBC] = "PBC";
343
344 /* For diffusion : Chalermek */
345 name_[PT_DIFF] = "diffusion";
346
347 // Link state routing updates
348 name_[PT_RTPROTO_LS] = "rtProtoLS";
349
350 // MPLS LDP packets
351 name_[PT_LDP] = "LDP";
352
353 // for GAF
354 name_[PT_GAF] = "gaf";
355
356 // RealAudio packets
357 name_[PT_REALAUDIO] = "ra";
358
359 //pushback
360 name_[PT_PUSHBACK] = "pushback";
361
362 #ifdef HAVE_STL
363 // for PGM
364 name_[PT_PGM] = "PGM";
365 #endif //STL
366
367 // LMS entries
368 name_[PT_LMS]="LMS";
369 name_[PT_LMS_SETUP]="LMS_SETUP";
370
371 name_[PT_SCTP]= "sctp";
372 name_[PT_SCTP_APP1] = "sctp_app1";
373
374 // smac
375 name_[PT_SMAC]="smac";
376
377 // HDLC
378 name_[PT_HDLC]="HDLC";
379
380 // XCP
381 name_[PT_XCP]="xcp";
382
383 // Bell Labs (PackMime OL)
384 name_[PT_BLTRACE]="BellLabsTrace";
385
386 // AOMDV patch
387 name_[PT_AOMDV]= "AOMDV";
388
389 name_[PT_NTYPE]= "undefined";
390 }
391 static int addPacket(char *name);
392 static packet_t getType(const char *name)
393 {
394 for(unsigned int i = 0; i < nPkt_; i++)
395 {
396 if(strcmp(name, name_[i]) == 0)
397 return i;
398 }
399 return PT_NTYPE;
400
401 }
402 private:
403 static char** name_;
404 static unsigned int nPkt_;
405 static PacketClassifier *pc_;
406 };
407 extern p_info packet_info; /* map PT_* to string name */
408 //extern char* p_info::name_[];
409
410 #define DATA_PACKET(type) ( (type) == PT_TCP || \
411 (type) == PT_TELNET || \
412 (type) == PT_CBR || \
413 (type) == PT_AUDIO || \
414 (type) == PT_VIDEO || \
415 (type) == PT_ACK || \
416 (type) == PT_SCTP || \
417 (type) == PT_SCTP_APP1 \
418 )
419
420 //#define OFFSET(type, field) ((long) &((type *)0)->field)
421 #define OFFSET(type, field) ( (char *)&( ((type *)256)->field ) - (char *)256)
422
423 class PacketData : public AppData {
424 public:
425 PacketData(int sz) : AppData(PACKET_DATA) {
426 datalen_ = sz;
427 if (datalen_ > 0)
428 data_ = new unsigned char[datalen_];
429 else
430 data_ = NULL;
431 }
432 PacketData(PacketData& d) : AppData(d) {
433 datalen_ = d.datalen_;
434 if (datalen_ > 0) {
435 data_ = new unsigned char[datalen_];
436 memcpy(data_, d.data_, datalen_);
437 } else
438 data_ = NULL;
439 }
440 virtual ~PacketData() {
441 if (data_ != NULL)
442 delete []data_;
443 }
444 unsigned char* data() { return data_; }
445
446 virtual int size() const { return datalen_; }
447 virtual AppData* copy() { return new PacketData(*this); }
448 private:
449 unsigned char* data_;
450 int datalen_;
451 };
452
453 //Monarch ext
454 typedef void (*FailureCallback)(Packet *,void *);
455
456 class Packet : public Event {
457 private:
458 unsigned char* bits_; // header bits
459 // unsigned char* data_; // variable size buffer for 'data'
460 // unsigned int datalen_; // length of variable size buffer
461 AppData* data_; // variable size buffer for 'data'
462 static void init(Packet*); // initialize pkt hdr
463 bool fflag_;
464 protected:
465 static Packet* free_; // packet free list
466 int ref_count_; // free the pkt until count to 0
467 public:
468 Packet* next_; // for queues and the free list
469 static int hdrlen_;
470
471 Packet() : bits_(0), data_(0), ref_count_(0), next_(0) { }
472 inline unsigned char* const bits() { return (bits_); }
473 inline Packet* copy() const;
474 inline Packet* refcopy() { ++ref_count_; return this; }
475 inline int& ref_count() { return (ref_count_); }
476 static inline Packet* alloc();
477 static inline Packet* alloc(int);
478 inline void allocdata(int);
479 // dirty hack for diffusion data
480 inline void initdata() { data_ = 0;}
481 static inline void free(Packet*);
482 inline unsigned char* access(int off) const {
483 if (off < 0)
484 abort();
485 return (&bits_[off]);
486 }
487 // This is used for backward compatibility, i.e., assuming user data
488 // is PacketData and return its pointer.
489 inline unsigned char* accessdata() const {
490 if (data_ == 0)
491 return 0;
492 assert(data_->type() == PACKET_DATA);
493 return (((PacketData*)data_)->data());
494 }
495 // This is used to access application-specific data, not limited
496 // to PacketData.
497 inline AppData* userdata() const {
498 return data_;
499 }
500 inline void setdata(AppData* d) {
501 if (data_ != NULL)
502 delete data_;
503 data_ = d;
504 }
505 inline int datalen() const { return data_ ? data_->size() : 0; }
506
507 // Monarch extn
508
509 static void dump_header(Packet *p, int offset, int length);
510
511 // the pkt stamp carries all info about how/where the pkt
512 // was sent needed for a receiver to determine if it correctly
513 // receives the pkt
514 PacketStamp txinfo_;
515
516 /*
517 * According to cmu code:
518 * This flag is set by the MAC layer on an incoming packet
519 * and is cleared by the link layer. It is an ugly hack, but
520 * there's really no other way because NS always calls
521 * the recv() function of an object.
522 *
523 */
524 uint8_t incoming;
525
526 //monarch extns end;
527 };
528
529 /*
530 * static constant associations between interface special (negative)
531 * values and their c-string representations that are used from tcl
532 */
533 class iface_literal {
534 public:
535 enum iface_constant {
536 UNKN_IFACE= -1, /*
537 * iface value for locally originated packets
538 */
539 ANY_IFACE= -2 /*
540 * hashnode with iif == ANY_IFACE_
541 * matches any pkt iface (imported from TCL);
542 * this value should be different from
543 * hdr_cmn::UNKN_IFACE (packet.h)
544 */
545 };
546 iface_literal(const iface_constant i, const char * const n) :
547 value_(i), name_(n) {}
548 inline int value() const { return value_; }
549 inline const char * const name() const { return name_; }
550 private:
551 const iface_constant value_;
552 /* strings used in TCL to access those special values */
553 const char * const name_;
554 };
555
556 static const iface_literal UNKN_IFACE(iface_literal::UNKN_IFACE, "?");
557 static const iface_literal ANY_IFACE(iface_literal::ANY_IFACE, "*");
558
559 /*
560 * Note that NS_AF_* doesn't necessarily correspond with
561 * the constants used in your system (because many
562 * systems don't have NONE or ILINK).
563 */
564 enum ns_af_enum { NS_AF_NONE, NS_AF_ILINK, NS_AF_INET };
565
566 enum ModulationScheme {BPSK = 0, QPSK = 1, QAM16 = 2, QAM64 = 3};
567
568 struct hdr_cmn {
569 enum dir_t { DOWN= -1, NONE= 0, UP= 1 };
570 packet_t ptype_; // packet type (see above)
571 int size_; // simulated packet size
572 int uid_; // unique id
573 int error_; // error flag
574 int errbitcnt_; // # of corrupted bits jahn
575 int fecsize_;
576 double ts_; // timestamp: for q-delay measurement
577 int iface_; // receiving interface (label)
578 dir_t direction_; // direction: 0=none, 1=up, -1=down
579 // source routing
580 char src_rt_valid;
581 double ts_arr_; // Required by Marker of JOBS
582
583 //Monarch extn begins
584 nsaddr_t prev_hop_; // IP addr of forwarding hop
585 nsaddr_t next_hop_; // next hop for this packet
586 int addr_type_; // type of next_hop_ addr
587 nsaddr_t last_hop_; // for tracing on multi-user channels
588
589 // AOMDV patch
590 int aomdv_salvage_count_;
591
592 // called if pkt can't obtain media or isn't ack'd. not called if
593 // droped by a queue
594 FailureCallback xmit_failure_;
595 void *xmit_failure_data_;
596
597 /*
598 * MONARCH wants to know if the MAC layer is passing this back because
599 * it could not get the RTS through or because it did not receive
600 * an ACK.
601 */
602 int xmit_reason_;
603 #define XMIT_REASON_RTS 0x01
604 #define XMIT_REASON_ACK 0x02
605
606 // filled in by GOD on first transmission, used for trace analysis
607 int num_forwards_; // how many times this pkt was forwarded
608 int opt_num_forwards_; // optimal #forwards
609 // Monarch extn ends;
610
611 // tx time for this packet in sec
612 double txtime_;
613 inline double& txtime() { return(txtime_); }
614
615 static int offset_; // offset for this header
616 inline static int& offset() { return offset_; }
617 inline static hdr_cmn* access(const Packet* p) {
618 return (hdr_cmn*) p->access(offset_);
619 }
620
621 /* per-field member functions */
622 inline packet_t& ptype() { return (ptype_); }
623 inline int& size() { return (size_); }
624 inline int& uid() { return (uid_); }
625 inline int& error() { return error_; }
626 inline int& errbitcnt() {return errbitcnt_; }
627 inline int& fecsize() {return fecsize_; }
628 inline double& timestamp() { return (ts_); }
629 inline int& iface() { return (iface_); }
630 inline dir_t& direction() { return (direction_); }
631 // monarch_begin
632 inline nsaddr_t& next_hop() { return (next_hop_); }
633 inline int& addr_type() { return (addr_type_); }
634 inline int& num_forwards() { return (num_forwards_); }
635 inline int& opt_num_forwards() { return (opt_num_forwards_); }
636 //monarch_end
637
638 ModulationScheme mod_scheme_;
639 inline ModulationScheme& mod_scheme() { return (mod_scheme_); }
640 };
641
642
643 class PacketHeaderClass : public TclClass {
644 protected:
645 PacketHeaderClass(const char* classname, int hdrsize);
646 virtual int method(int argc, const char*const* argv);
647 void field_offset(const char* fieldname, int offset);
648 inline void bind_offset(int* off) { offset_ = off; }
649 inline void offset(int* off) {offset_= off;}
650 int hdrlen_; // # of bytes for this header
651 int* offset_; // offset for this header
652 public:
653 virtual void bind();
654 virtual void export_offsets();
655 TclObject* create(int argc, const char*const* argv);
656 };
657
658
659 inline void Packet::init(Packet* p)
660 {
661 bzero(p->bits_, hdrlen_);
662 }
663
664 inline Packet* Packet::alloc()
665 {
666 Packet* p = free_;
667 if (p != 0) {
668 assert(p->fflag_ == FALSE);
669 free_ = p->next_;
670 assert(p->data_ == 0);
671 p->uid_ = 0;
672 p->time_ = 0;
673 } else {
674 p = new Packet;
675 p->bits_ = new unsigned char[hdrlen_];
676 if (p == 0 || p->bits_ == 0)
677 abort();
678 }
679 init(p); // Initialize bits_[]
680 (HDR_CMN(p))->next_hop_ = -2; // -1 reserved for IP_BROADCAST
681 (HDR_CMN(p))->last_hop_ = -2; // -1 reserved for IP_BROADCAST
682 p->fflag_ = TRUE;
683 (HDR_CMN(p))->direction() = hdr_cmn::DOWN;
684 /* setting all direction of pkts to be downward as default;
685 until channel changes it to +1 (upward) */
686 p->next_ = 0;
687 return (p);
688 }
689
690 /*
691 * Allocate an n byte data buffer to an existing packet
692 *
693 * To set application-specific AppData, use Packet::setdata()
694 */
695 inline void Packet::allocdata(int n)
696 {
697 assert(data_ == 0);
698 data_ = new PacketData(n);
699 if (data_ == 0)
700 abort();
701 }
702
703 /* allocate a packet with an n byte data buffer */
704 inline Packet* Packet::alloc(int n)
705 {
706 Packet* p = alloc();
707 if (n > 0)
708 p->allocdata(n);
709 return (p);
710 }
711
712
713 inline void Packet::free(Packet* p)
714 {
715 if (p->fflag_) {
716 if (p->ref_count_ == 0) {
717 /*
718 * A packet's uid may be < 0 (out of a event queue), or
719 * == 0 (newed but never gets into the event queue.
720 */
721 assert(p->uid_ <= 0);
722 // Delete user data because we won't need it any more.
723 if (p->data_ != 0) {
724 delete p->data_;
725 p->data_ = 0;
726 }
727 init(p);
728 p->next_ = free_;
729 free_ = p;
730 p->fflag_ = FALSE;
731 } else {
732 --p->ref_count_;
733 }
734 }
735 }
736
737 inline Packet* Packet::copy() const
738 {
739
740 Packet* p = alloc();
741 memcpy(p->bits(), bits_, hdrlen_);
742 if (data_)
743 p->data_ = data_->copy();
744 p->txinfo_.init(&txinfo_);
745
746 return (p);
747 }
748
749 inline void
750 Packet::dump_header(Packet *p, int offset, int length)
751 {
752 assert(offset + length <= p->hdrlen_);
753 struct hdr_cmn *ch = HDR_CMN(p);
754
755 fprintf(stderr, "\nPacket ID: %d\n", ch->uid());
756
757 for(int i = 0; i < length ; i+=16) {
758 fprintf(stderr, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
759 p->bits_[offset + i], p->bits_[offset + i + 1],
760 p->bits_[offset + i + 2], p->bits_[offset + i + 3],
761 p->bits_[offset + i + 4], p->bits_[offset + i + 5],
762 p->bits_[offset + i + 6], p->bits_[offset + i + 7],
763 p->bits_[offset + i + 8], p->bits_[offset + i + 9],
764 p->bits_[offset + i + 10], p->bits_[offset + i + 11],
765 p->bits_[offset + i + 12], p->bits_[offset + i + 13],
766 p->bits_[offset + i + 14], p->bits_[offset + i + 15]);
767 }
768 }
769
770 #endif