Debian Codegraph @

protolib/src/sim/ns/ns235/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.107 2010/05/09 22:28:41 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 /*
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 // AOMDV packet
184 static const packet_t PT_AOMDV = 61;
185
186 // PUMA packet
187 static const packet_t PT_PUMA = 62;
188
189 // DCCP packets
190 static const packet_t PT_DCCP = 63;
191 static const packet_t PT_DCCP_REQ = 64;
192 static const packet_t PT_DCCP_RESP = 65;
193 static const packet_t PT_DCCP_ACK = 66;
194 static const packet_t PT_DCCP_DATA = 67;
195 static const packet_t PT_DCCP_DATAACK = 68;
196 static const packet_t PT_DCCP_CLOSE = 69;
197 static const packet_t PT_DCCP_CLOSEREQ = 70;
198 static const packet_t PT_DCCP_RESET = 71;
199
200 // M-DART packets
201 static const packet_t PT_MDART = 72;
202
203 // NRL ProtoManetKernel packet
204 static const packet_t PT_PROTOLIBMK = 73;
205
206 // insert new packet types here
207 static packet_t PT_NTYPE = 74; // This MUST be the LAST one
208
209 enum packetClass
210 {
211 UNCLASSIFIED,
212 ROUTING,
213 DATApkt
214 };
215
216
217 /*
218 * ns-2.33 adding support for dynamic libraries
219 *
220 * The PacketClassifier class is needed to make
221 * p_info::data_packet(packet_t) work also with dynamically defined
222 * packet types.
223 *
224 */
225 class PacketClassifier
226 {
227 public:
228 PacketClassifier(): next_(0){}
229 virtual ~PacketClassifier() {}
230 void setNext(PacketClassifier *next){next_ = next;}
231 PacketClassifier *getNext(){return next_;}
232 packetClass classify(packet_t type)
233 {
234 packetClass c = getClass(type);
235 if(c == UNCLASSIFIED && next_)
236 c = next_->classify(type);
237 return c;
238 }
239
240 protected:
241 //return 0 if the packet is unknown
242 virtual packetClass getClass(packet_t type) = 0;
243 PacketClassifier *next_;
244 };
245
246 class p_info {
247 public:
248 p_info()
249 {
250 initName();
251 }
252 const char* name(packet_t p) const {
253 if ( p <= p_info::nPkt_ ) return name_[p];
254 return 0;
255 }
256 static bool data_packet(packet_t type) {
257 return ( (type) == PT_TCP || \
258 (type) == PT_TELNET || \
259 (type) == PT_CBR || \
260 (type) == PT_AUDIO || \
261 (type) == PT_VIDEO || \
262 (type) == PT_ACK || \
263 (type) == PT_SCTP || \
264 (type) == PT_SCTP_APP1 || \
265 (type) == PT_HDLC \
266 );
267 }
268 static packetClass classify(packet_t type) {
269 if (type == PT_DSR ||
270 type == PT_MESSAGE ||
271 type == PT_TORA ||
272 type == PT_PUMA ||
273 type == PT_AODV ||
274 type == PT_MDART ||
275 type == PT_PROTOLIBMK)
276 return ROUTING;
277 if (type == PT_TCP ||
278 type == PT_TELNET ||
279 type == PT_CBR ||
280 type == PT_AUDIO ||
281 type == PT_VIDEO ||
282 type == PT_ACK ||
283 type == PT_SCTP ||
284 type == PT_SCTP_APP1 ||
285 type == PT_HDLC)
286 return DATApkt;
287 if (pc_)
288 return pc_->classify(type);
289 return UNCLASSIFIED;
290 }
291 static void addPacketClassifier(PacketClassifier *pc)
292 {
293 if(!pc)
294 return;
295 pc->setNext(pc_);
296 pc_ = pc;
297 }
298 static void initName()
299 {
300 if(nPkt_ >= PT_NTYPE+1)
301 return;
302 char **nameNew = new char*[PT_NTYPE+1];
303 for(unsigned int i = (unsigned int)PT_SMAC+1; i < nPkt_; i++)
304 {
305 nameNew[i] = name_[i];
306 }
307 if(!nPkt_)
308 delete [] name_;
309 name_ = nameNew;
310 nPkt_ = PT_NTYPE+1;
311
312
313 name_[PT_TCP]= "tcp";
314 name_[PT_UDP]= "udp";
315 name_[PT_CBR]= "cbr";
316 name_[PT_AUDIO]= "audio";
317 name_[PT_VIDEO]= "video";
318 name_[PT_ACK]= "ack";
319 name_[PT_START]= "start";
320 name_[PT_STOP]= "stop";
321 name_[PT_PRUNE]= "prune";
322 name_[PT_GRAFT]= "graft";
323 name_[PT_GRAFTACK]= "graftAck";
324 name_[PT_JOIN]= "join";
325 name_[PT_ASSERT]= "assert";
326 name_[PT_MESSAGE]= "message";
327 name_[PT_RTCP]= "rtcp";
328 name_[PT_RTP]= "rtp";
329 name_[PT_RTPROTO_DV]= "rtProtoDV";
330 name_[PT_CtrMcast_Encap]= "CtrMcast_Encap";
331 name_[PT_CtrMcast_Decap]= "CtrMcast_Decap";
332 name_[PT_SRM]= "SRM";
333
334 name_[PT_REQUEST]= "sa_req";
335 name_[PT_ACCEPT]= "sa_accept";
336 name_[PT_CONFIRM]= "sa_conf";
337 name_[PT_TEARDOWN]= "sa_teardown";
338 name_[PT_LIVE]= "live";
339 name_[PT_REJECT]= "sa_reject";
340
341 name_[PT_TELNET]= "telnet";
342 name_[PT_FTP]= "ftp";
343 name_[PT_PARETO]= "pareto";
344 name_[PT_EXP]= "exp";
345 name_[PT_INVAL]= "httpInval";
346 name_[PT_HTTP]= "http";
347 name_[PT_ENCAPSULATED]= "encap";
348 name_[PT_MFTP]= "mftp";
349 name_[PT_ARP]= "ARP";
350 name_[PT_MAC]= "MAC";
351 name_[PT_TORA]= "TORA";
352 name_[PT_DSR]= "DSR";
353 name_[PT_AODV]= "AODV";
354 name_[PT_MDART]= "MDART";
355 name_[PT_IMEP]= "IMEP";
356
357 name_[PT_RAP_DATA] = "rap_data";
358 name_[PT_RAP_ACK] = "rap_ack";
359
360 name_[PT_TFRC]= "tcpFriend";
361 name_[PT_TFRC_ACK]= "tcpFriendCtl";
362 name_[PT_PING]="ping";
363
364 name_[PT_PBC] = "PBC";
365
366 /* For diffusion : Chalermek */
367 name_[PT_DIFF] = "diffusion";
368
369 // Link state routing updates
370 name_[PT_RTPROTO_LS] = "rtProtoLS";
371
372 // MPLS LDP packets
373 name_[PT_LDP] = "LDP";
374
375 // for GAF
376 name_[PT_GAF] = "gaf";
377
378 // RealAudio packets
379 name_[PT_REALAUDIO] = "ra";
380
381 //pushback
382 name_[PT_PUSHBACK] = "pushback";
383
384 #ifdef HAVE_STL
385 // for PGM
386 name_[PT_PGM] = "PGM";
387 #endif //STL
388
389 // LMS entries
390 name_[PT_LMS]="LMS";
391 name_[PT_LMS_SETUP]="LMS_SETUP";
392
393 name_[PT_SCTP]= "sctp";
394 name_[PT_SCTP_APP1] = "sctp_app1";
395
396 // smac
397 name_[PT_SMAC]="smac";
398
399 // HDLC
400 name_[PT_HDLC]="HDLC";
401
402 // XCP
403 name_[PT_XCP]="xcp";
404
405 // Bell Labs (PackMime OL)
406 name_[PT_BLTRACE]="BellLabsTrace";
407
408 // AOMDV patch
409 name_[PT_AOMDV]= "AOMDV";
410
411 // PUMA
412 name_[PT_PUMA]="PUMA";
413
414 // DCCP
415 name_[PT_DCCP]="DCCP";
416 name_[PT_DCCP_REQ]="DCCP_Request";
417 name_[PT_DCCP_RESP]="DCCP_Response";
418 name_[PT_DCCP_ACK]="DCCP_Ack";
419 name_[PT_DCCP_DATA]="DCCP_Data";
420 name_[PT_DCCP_DATAACK]="DCCP_DataAck";
421 name_[PT_DCCP_CLOSE]="DCCP_Close";
422 name_[PT_DCCP_CLOSEREQ]="DCCP_CloseReq";
423 name_[PT_DCCP_RESET]="DCCP_Reset";
424
425 // NRL ProtoManetKernel
426 name_[PT_PROTOLIBMK]= "ProtolibMK";
427
428 name_[PT_NTYPE]= "undefined";
429 }
430 static int addPacket(char *name);
431 static packet_t getType(const char *name)
432 {
433 for(unsigned int i = 0; i < nPkt_; i++)
434 {
435 if(strcmp(name, name_[i]) == 0)
436 return i;
437 }
438 return PT_NTYPE;
439
440 }
441 private:
442 static char** name_;
443 static unsigned int nPkt_;
444 static PacketClassifier *pc_;
445 };
446 extern p_info packet_info; /* map PT_* to string name */
447 //extern char* p_info::name_[];
448
449 #define DATA_PACKET(type) ( (type) == PT_TCP || \
450 (type) == PT_TELNET || \
451 (type) == PT_CBR || \
452 (type) == PT_AUDIO || \
453 (type) == PT_VIDEO || \
454 (type) == PT_ACK || \
455 (type) == PT_SCTP || \
456 (type) == PT_SCTP_APP1 \
457 )
458
459 //#define OFFSET(type, field) ((long) &((type *)0)->field)
460 #define OFFSET(type, field) ( (char *)&( ((type *)256)->field ) - (char *)256)
461
462 class PacketData : public AppData {
463 public:
464 PacketData(int sz) : AppData(PACKET_DATA) {
465 datalen_ = sz;
466 if (datalen_ > 0)
467 data_ = new unsigned char[datalen_];
468 else
469 data_ = NULL;
470 }
471 PacketData(PacketData& d) : AppData(d) {
472 datalen_ = d.datalen_;
473 if (datalen_ > 0) {
474 data_ = new unsigned char[datalen_];
475 memcpy(data_, d.data_, datalen_);
476 } else
477 data_ = NULL;
478 }
479 virtual ~PacketData() {
480 if (data_ != NULL)
481 delete []data_;
482 }
483 unsigned char* data() { return data_; }
484
485 virtual int size() const { return datalen_; }
486 virtual AppData* copy() { return new PacketData(*this); }
487 private:
488 unsigned char* data_;
489 int datalen_;
490 };
491
492 //Monarch ext
493 typedef void (*FailureCallback)(Packet *,void *);
494
495 class Packet : public Event {
496 private:
497 unsigned char* bits_; // header bits
498 // unsigned char* data_; // variable size buffer for 'data'
499 // unsigned int datalen_; // length of variable size buffer
500 AppData* data_; // variable size buffer for 'data'
501 static void init(Packet*); // initialize pkt hdr
502 bool fflag_;
503 protected:
504 static Packet* free_; // packet free list
505 int ref_count_; // free the pkt until count to 0
506 public:
507 Packet* next_; // for queues and the free list
508 static int hdrlen_;
509
510 Packet() : bits_(0), data_(0), ref_count_(0), next_(0) { }
511 inline unsigned char* bits() { return (bits_); }
512 inline Packet* copy() const;
513 inline Packet* refcopy() { ++ref_count_; return this; }
514 inline int& ref_count() { return (ref_count_); }
515 static inline Packet* alloc();
516 static inline Packet* alloc(int);
517 inline void allocdata(int);
518 // dirty hack for diffusion data
519 inline void initdata() { data_ = 0;}
520 static inline void free(Packet*);
521 inline unsigned char* access(int off) const {
522 if (off < 0)
523 abort();
524 return (&bits_[off]);
525 }
526 // This is used for backward compatibility, i.e., assuming user data
527 // is PacketData and return its pointer.
528 inline unsigned char* accessdata() const {
529 if (data_ == 0)
530 return 0;
531 assert(data_->type() == PACKET_DATA);
532 return (((PacketData*)data_)->data());
533 }
534 // This is used to access application-specific data, not limited
535 // to PacketData.
536 inline AppData* userdata() const {
537 return data_;
538 }
539 inline void setdata(AppData* d) {
540 if (data_ != NULL)
541 delete data_;
542 data_ = d;
543 }
544 inline int datalen() const { return data_ ? data_->size() : 0; }
545
546 // Monarch extn
547
548 static void dump_header(Packet *p, int offset, int length);
549
550 // the pkt stamp carries all info about how/where the pkt
551 // was sent needed for a receiver to determine if it correctly
552 // receives the pkt
553 PacketStamp txinfo_;
554
555 /*
556 * According to cmu code:
557 * This flag is set by the MAC layer on an incoming packet
558 * and is cleared by the link layer. It is an ugly hack, but
559 * there's really no other way because NS always calls
560 * the recv() function of an object.
561 *
562 */
563 uint8_t incoming;
564
565 //monarch extns end;
566 };
567
568 /*
569 * static constant associations between interface special (negative)
570 * values and their c-string representations that are used from tcl
571 */
572 class iface_literal {
573 public:
574 enum iface_constant {
575 UNKN_IFACE= -1, /*
576 * iface value for locally originated packets
577 */
578 ANY_IFACE= -2 /*
579 * hashnode with iif == ANY_IFACE_
580 * matches any pkt iface (imported from TCL);
581 * this value should be different from
582 * hdr_cmn::UNKN_IFACE (packet.h)
583 */
584 };
585 iface_literal(const iface_constant i, const char * const n) :
586 value_(i), name_(n) {}
587 inline int value() const { return value_; }
588 inline const char * name() const { return name_; }
589 private:
590 const iface_constant value_;
591 /* strings used in TCL to access those special values */
592 const char * const name_;
593 };
594
595 static const iface_literal UNKN_IFACE(iface_literal::UNKN_IFACE, "?");
596 static const iface_literal ANY_IFACE(iface_literal::ANY_IFACE, "*");
597
598 /*
599 * Note that NS_AF_* doesn't necessarily correspond with
600 * the constants used in your system (because many
601 * systems don't have NONE or ILINK).
602 */
603 enum ns_af_enum { NS_AF_NONE, NS_AF_ILINK, NS_AF_INET };
604
605 enum ModulationScheme {BPSK = 0, QPSK = 1, QAM16 = 2, QAM64 = 3};
606
607 struct hdr_cmn {
608 enum dir_t { DOWN= -1, NONE= 0, UP= 1 };
609 packet_t ptype_; // packet type (see above)
610 int size_; // simulated packet size
611 int uid_; // unique id
612 int error_; // error flag
613 int errbitcnt_; // # of corrupted bits jahn
614 int fecsize_;
615 double ts_; // timestamp: for q-delay measurement
616 int iface_; // receiving interface (label)
617 dir_t direction_; // direction: 0=none, 1=up, -1=down
618 // source routing
619 char src_rt_valid;
620 double ts_arr_; // Required by Marker of JOBS
621
622 //Monarch extn begins
623 nsaddr_t prev_hop_; // IP addr of forwarding hop
624 nsaddr_t next_hop_; // next hop for this packet
625 int addr_type_; // type of next_hop_ addr
626 nsaddr_t last_hop_; // for tracing on multi-user channels
627
628 // AOMDV patch
629 int aomdv_salvage_count_;
630
631 // called if pkt can't obtain media or isn't ack'd. not called if
632 // droped by a queue
633 FailureCallback xmit_failure_;
634 void *xmit_failure_data_;
635
636 /*
637 * MONARCH wants to know if the MAC layer is passing this back because
638 * it could not get the RTS through or because it did not receive
639 * an ACK.
640 */
641 int xmit_reason_;
642 #define XMIT_REASON_RTS 0x01
643 #define XMIT_REASON_ACK 0x02
644
645 // filled in by GOD on first transmission, used for trace analysis
646 int num_forwards_; // how many times this pkt was forwarded
647 int opt_num_forwards_; // optimal #forwards
648 // Monarch extn ends;
649
650 // tx time for this packet in sec
651 double txtime_;
652 inline double& txtime() { return(txtime_); }
653
654 static int offset_; // offset for this header
655 inline static int& offset() { return offset_; }
656 inline static hdr_cmn* access(const Packet* p) {
657 return (hdr_cmn*) p->access(offset_);
658 }
659
660 /* per-field member functions */
661 inline packet_t& ptype() { return (ptype_); }
662 inline int& size() { return (size_); }
663 inline int& uid() { return (uid_); }
664 inline int& error() { return error_; }
665 inline int& errbitcnt() {return errbitcnt_; }
666 inline int& fecsize() {return fecsize_; }
667 inline double& timestamp() { return (ts_); }
668 inline int& iface() { return (iface_); }
669 inline dir_t& direction() { return (direction_); }
670 // monarch_begin
671 inline nsaddr_t& next_hop() { return (next_hop_); }
672 inline int& addr_type() { return (addr_type_); }
673 inline int& num_forwards() { return (num_forwards_); }
674 inline int& opt_num_forwards() { return (opt_num_forwards_); }
675 //monarch_end
676
677 ModulationScheme mod_scheme_;
678 inline ModulationScheme& mod_scheme() { return (mod_scheme_); }
679 };
680
681
682 class PacketHeaderClass : public TclClass {
683 protected:
684 PacketHeaderClass(const char* classname, int hdrsize);
685 virtual int method(int argc, const char*const* argv);
686 void field_offset(const char* fieldname, int offset);
687 inline void bind_offset(int* off) { offset_ = off; }
688 inline void offset(int* off) {offset_= off;}
689 int hdrlen_; // # of bytes for this header
690 int* offset_; // offset for this header
691 public:
692 virtual void bind();
693 virtual void export_offsets();
694 TclObject* create(int argc, const char*const* argv);
695 };
696
697
698 inline void Packet::init(Packet* p)
699 {
700 bzero(p->bits_, hdrlen_);
701 }
702
703 inline Packet* Packet::alloc()
704 {
705 Packet* p = free_;
706 if (p != 0) {
707 assert(p->fflag_ == FALSE);
708 free_ = p->next_;
709 assert(p->data_ == 0);
710 p->uid_ = 0;
711 p->time_ = 0;
712 } else {
713 p = new Packet;
714 p->bits_ = new unsigned char[hdrlen_];
715 if (p == 0 || p->bits_ == 0)
716 abort();
717 }
718 init(p); // Initialize bits_[]
719 (HDR_CMN(p))->next_hop_ = -2; // -1 reserved for IP_BROADCAST
720 (HDR_CMN(p))->last_hop_ = -2; // -1 reserved for IP_BROADCAST
721 p->fflag_ = TRUE;
722 (HDR_CMN(p))->direction() = hdr_cmn::DOWN;
723 /* setting all direction of pkts to be downward as default;
724 until channel changes it to +1 (upward) */
725 p->next_ = 0;
726 return (p);
727 }
728
729 /*
730 * Allocate an n byte data buffer to an existing packet
731 *
732 * To set application-specific AppData, use Packet::setdata()
733 */
734 inline void Packet::allocdata(int n)
735 {
736 assert(data_ == 0);
737 data_ = new PacketData(n);
738 if (data_ == 0)
739 abort();
740 }
741
742 /* allocate a packet with an n byte data buffer */
743 inline Packet* Packet::alloc(int n)
744 {
745 Packet* p = alloc();
746 if (n > 0)
747 p->allocdata(n);
748 return (p);
749 }
750
751 #include "dccp/dccp_packets.h"
752
753 inline void Packet::free(Packet* p)
754 {
755 hdr_dccp *dccph;
756 if (p->fflag_) {
757 if (p->ref_count_ == 0) {
758
759 //free DCCP options on dropped packets
760 switch (HDR_CMN(p)->ptype_){
761 case PT_DCCP:
762 case PT_DCCP_REQ:
763 case PT_DCCP_RESP:
764 case PT_DCCP_ACK:
765 case PT_DCCP_DATA:
766 case PT_DCCP_DATAACK:
767 case PT_DCCP_CLOSE:
768 case PT_DCCP_CLOSEREQ:
769 case PT_DCCP_RESET:
770 dccph = hdr_dccp::access(p);
771 if (dccph->options_ != NULL){
772 delete (dccph->options_);
773 }
774 break;
775 default:
776 ;
777 }
778
779 /*
780 * A packet's uid may be < 0 (out of a event queue), or
781 * == 0 (newed but never gets into the event queue.
782 */
783 assert(p->uid_ <= 0);
784 // Delete user data because we won't need it any more.
785 if (p->data_ != 0) {
786 delete p->data_;
787 p->data_ = 0;
788 }
789 init(p);
790 p->next_ = free_;
791 free_ = p;
792 p->fflag_ = FALSE;
793 } else {
794 --p->ref_count_;
795 }
796 }
797 }
798
799 inline Packet* Packet::copy() const
800 {
801 hdr_dccp *dccph, *dccph_p;
802 Packet* p = alloc();
803 memcpy(p->bits(), bits_, hdrlen_);
804
805 //copy DCCP options_, since it is a pointer
806 switch (HDR_CMN(this)->ptype_){
807 case PT_DCCP:
808 case PT_DCCP_REQ:
809 case PT_DCCP_RESP:
810 case PT_DCCP_ACK:
811 case PT_DCCP_DATA:
812 case PT_DCCP_DATAACK:
813 case PT_DCCP_CLOSE:
814 case PT_DCCP_CLOSEREQ:
815 case PT_DCCP_RESET:
816 dccph = hdr_dccp::access(this);
817 dccph_p = hdr_dccp::access(p);
818 if (dccph->options_ != NULL)
819 dccph_p->options_ = new DCCPOptions(*dccph->options_);
820 break;
821 default:
822 ;
823 }
824
825 if (data_)
826 p->data_ = data_->copy();
827 p->txinfo_.init(&txinfo_);
828
829 return (p);
830 }
831
832 inline void
833 Packet::dump_header(Packet *p, int offset, int length)
834 {
835 assert(offset + length <= p->hdrlen_);
836 struct hdr_cmn *ch = HDR_CMN(p);
837
838 fprintf(stderr, "\nPacket ID: %d\n", ch->uid());
839
840 for(int i = 0; i < length ; i+=16) {
841 fprintf(stderr, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
842 p->bits_[offset + i], p->bits_[offset + i + 1],
843 p->bits_[offset + i + 2], p->bits_[offset + i + 3],
844 p->bits_[offset + i + 4], p->bits_[offset + i + 5],
845 p->bits_[offset + i + 6], p->bits_[offset + i + 7],
846 p->bits_[offset + i + 8], p->bits_[offset + i + 9],
847 p->bits_[offset + i + 10], p->bits_[offset + i + 11],
848 p->bits_[offset + i + 12], p->bits_[offset + i + 13],
849 p->bits_[offset + i + 14], p->bits_[offset + i + 15]);
850 }
851 }
852
853 #endif