Debian Codegraph @

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