| 1 |
/* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */ |
| 2 |
/* |
| 3 |
* Copyright (c) 1994-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 |
|
| 35 |
#ifndef lint |
| 36 |
static const char rcsid[] = |
| 37 |
"@(#) $Header: /cvsroot/nsnam/ns-2/common/packet.cc,v 1.19 2008/02/18 03:39:02 tom_henderson Exp $ (LBL)"; |
| 38 |
#endif |
| 39 |
|
| 40 |
#include "packet.h" |
| 41 |
#include "flags.h" |
| 42 |
|
| 43 |
p_info packet_info; |
| 44 |
char** p_info::name_; |
| 45 |
unsigned int p_info::nPkt_ = 0; |
| 46 |
PacketClassifier *p_info::pc_ = 0; |
| 47 |
|
| 48 |
int p_info::addPacket(char *name) |
| 49 |
{ |
| 50 |
if(!nPkt_) |
| 51 |
initName(); |
| 52 |
|
| 53 |
int newID = nPkt_-1; |
| 54 |
PT_NTYPE = nPkt_; |
| 55 |
initName(); |
| 56 |
name_[newID] = name; |
| 57 |
return newID; |
| 58 |
} |
| 59 |
|
| 60 |
int Packet::hdrlen_ = 0; // size of a packet's header |
| 61 |
Packet* Packet::free_; // free list |
| 62 |
int hdr_cmn::offset_; // static offset of common header |
| 63 |
int hdr_flags::offset_; // static offset of flags header |
| 64 |
|
| 65 |
|
| 66 |
PacketHeaderClass::PacketHeaderClass(const char* classname, int hdrlen) : |
| 67 |
TclClass(classname), hdrlen_(hdrlen), offset_(0) |
| 68 |
{ |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
TclObject* PacketHeaderClass::create(int, const char*const*) |
| 73 |
{ |
| 74 |
return (0); |
| 75 |
} |
| 76 |
|
| 77 |
void PacketHeaderClass::bind() |
| 78 |
{ |
| 79 |
TclClass::bind(); |
| 80 |
Tcl& tcl = Tcl::instance(); |
| 81 |
tcl.evalf("%s set hdrlen_ %d", classname_, hdrlen_); |
| 82 |
export_offsets(); |
| 83 |
add_method("offset"); |
| 84 |
} |
| 85 |
|
| 86 |
void PacketHeaderClass::export_offsets() |
| 87 |
{ |
| 88 |
} |
| 89 |
|
| 90 |
void PacketHeaderClass::field_offset(const char* fieldname, int offset) |
| 91 |
{ |
| 92 |
Tcl& tcl = Tcl::instance(); |
| 93 |
tcl.evalf("%s set offset_(%s) %d", classname_, fieldname, offset); |
| 94 |
} |
| 95 |
|
| 96 |
int PacketHeaderClass::method(int ac, const char*const* av) |
| 97 |
{ |
| 98 |
Tcl& tcl = Tcl::instance(); |
| 99 |
int argc = ac - 2; |
| 100 |
const char*const* argv = av + 2; |
| 101 |
if (argc == 3) { |
| 102 |
if (strcmp(argv[1], "offset") == 0) { |
| 103 |
if (offset_) { |
| 104 |
*offset_ = atoi(argv[2]); |
| 105 |
return TCL_OK; |
| 106 |
} |
| 107 |
tcl.resultf("Warning: cannot set offset_ for %s", |
| 108 |
classname_); |
| 109 |
return TCL_OK; |
| 110 |
} |
| 111 |
} |
| 112 |
else if (argc == 2) { |
| 113 |
if (strcmp(argv[1], "offset") == 0) { |
| 114 |
if (offset_) { |
| 115 |
tcl.resultf("%d", *offset_); |
| 116 |
return TCL_OK; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
return TclClass::method(ac, av); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
class CommonHeaderClass : public PacketHeaderClass { |
| 125 |
public: |
| 126 |
CommonHeaderClass() : PacketHeaderClass("PacketHeader/Common", |
| 127 |
sizeof(hdr_cmn)) { |
| 128 |
bind_offset(&hdr_cmn::offset_); |
| 129 |
} |
| 130 |
void export_offsets() { |
| 131 |
field_offset("ptype_", OFFSET(hdr_cmn, ptype_)); |
| 132 |
field_offset("size_", OFFSET(hdr_cmn, size_)); |
| 133 |
field_offset("uid_", OFFSET(hdr_cmn, uid_)); |
| 134 |
field_offset("error_", OFFSET(hdr_cmn, error_)); |
| 135 |
}; |
| 136 |
} class_cmnhdr; |
| 137 |
|
| 138 |
class FlagsHeaderClass : public PacketHeaderClass { |
| 139 |
public: |
| 140 |
FlagsHeaderClass() : PacketHeaderClass("PacketHeader/Flags", |
| 141 |
sizeof(hdr_flags)) { |
| 142 |
bind_offset(&hdr_flags::offset_); |
| 143 |
} |
| 144 |
} class_flagshdr; |
| 145 |
|
| 146 |
|
| 147 |
/* manages active packet header types */ |
| 148 |
class PacketHeaderManager : public TclObject { |
| 149 |
public: |
| 150 |
PacketHeaderManager() { |
| 151 |
bind("hdrlen_", &Packet::hdrlen_); |
| 152 |
} |
| 153 |
}; |
| 154 |
|
| 155 |
static class PacketHeaderManagerClass : public TclClass { |
| 156 |
public: |
| 157 |
PacketHeaderManagerClass() : TclClass("PacketHeaderManager") {} |
| 158 |
TclObject* create(int, const char*const*) { |
| 159 |
return (new PacketHeaderManager); |
| 160 |
} |
| 161 |
} class_packethdr_mgr; |