📄 packet.h
字号:
inline void initdata() { data_ = 0;} static inline void free(Packet*); inline unsigned char* access(int off) const { if (off < 0) abort(); return (&bits_[off]); } // This is used for backward compatibility, i.e., assuming user data // is PacketData and return its pointer. inline unsigned char* accessdata() const { if (data_ == 0) return 0; assert(data_->type() == PACKET_DATA); return (((PacketData*)data_)->data()); } // This is used to access application-specific data, not limited // to PacketData. inline AppData* userdata() const { return data_; } inline void setdata(AppData* d) { if (data_ != NULL) delete data_; data_ = d; } inline int datalen() const { return data_ ? data_->size() : 0; } // Monarch extn static void dump_header(Packet *p, int offset, int length); // the pkt stamp carries all info about how/where the pkt // was sent needed for a receiver to determine if it correctly // receives the pkt PacketStamp txinfo_; /* * According to cmu code: * This flag is set by the MAC layer on an incoming packet * and is cleared by the link layer. It is an ugly hack, but * there's really no other way because NS always calls * the recv() function of an object. * */ u_int8_t incoming; //monarch extns end;#ifdef MIT_uAMPS static void PrintRcHeader(Packet *p, char *layer);#endif};/* * static constant associations between interface special (negative) * values and their c-string representations that are used from tcl */class iface_literal {public: enum iface_constant { UNKN_IFACE= -1, /* * iface value for locally originated packets */ ANY_IFACE= -2 /* * hashnode with iif == ANY_IFACE_ * matches any pkt iface (imported from TCL); * this value should be different from * hdr_cmn::UNKN_IFACE (packet.h) */ }; iface_literal(const iface_constant i, const char * const n) : value_(i), name_(n) {} inline int value() const { return value_; } inline const char * const name() const { return name_; }private: const iface_constant value_; /* strings used in TCL to access those special values */ const char * const name_; };static const iface_literal UNKN_IFACE(iface_literal::UNKN_IFACE, "?");static const iface_literal ANY_IFACE(iface_literal::ANY_IFACE, "*");/* * Note that NS_AF_* doesn't necessarily correspond with * the constants used in your system (because many * systems don't have NONE or ILINK). */enum ns_af_enum { NS_AF_NONE, NS_AF_ILINK, NS_AF_INET };struct hdr_cmn { enum dir_t { DOWN= -1, NONE= 0, UP= 1 }; packet_t ptype_; // packet type (see above) int size_; // simulated packet size int uid_; // unique id int error_; // error flag int errbitcnt_; // # of corrupted bits jahn int fecsize_; double ts_; // timestamp: for q-delay measurement int iface_; // receiving interface (label) dir_t direction_; // direction: 0=none, 1=up, -1=down // source routing char src_rt_valid; double ts_arr_; // Required by Marker of JOBS //Monarch extn begins nsaddr_t prev_hop_; // IP addr of forwarding hop nsaddr_t next_hop_; // next hop for this packet int addr_type_; // type of next_hop_ addr nsaddr_t last_hop_; // for tracing on multi-user channels // called if pkt can't obtain media or isn't ack'd. not called if // droped by a queue FailureCallback xmit_failure_; void *xmit_failure_data_; /* * MONARCH wants to know if the MAC layer is passing this back because * it could not get the RTS through or because it did not receive * an ACK. */ int xmit_reason_;#define XMIT_REASON_RTS 0x01#define XMIT_REASON_ACK 0x02 // filled in by GOD on first transmission, used for trace analysis int num_forwards_; // how many times this pkt was forwarded int opt_num_forwards_; // optimal #forwards // Monarch extn ends; // tx time for this packet in sec double txtime_; inline double& txtime() { return(txtime_); } static int offset_; // offset for this header inline static int& offset() { return offset_; } inline static hdr_cmn* access(const Packet* p) { return (hdr_cmn*) p->access(offset_); } /* per-field member functions */ inline packet_t& ptype() { return (ptype_); } inline int& size() { return (size_); } inline int& uid() { return (uid_); } inline int& error() { return error_; } inline int& errbitcnt() {return errbitcnt_; } inline int& fecsize() {return fecsize_; } inline double& timestamp() { return (ts_); } inline int& iface() { return (iface_); } inline dir_t& direction() { return (direction_); } // monarch_begin inline nsaddr_t& next_hop() { return (next_hop_); } inline int& addr_type() { return (addr_type_); } inline int& num_forwards() { return (num_forwards_); } inline int& opt_num_forwards() { return (opt_num_forwards_); } //monarch_end};#ifdef MIT_uAMPSstruct hdr_rca { int msg_type_; char meta_[1000]; int meta_size_; float dist_to_dest_; int dist_est_; int rca_src_; int rca_mac_dst_; int rca_link_dst_; int code_; static int offset_; // offset for this header inline static int& offset() { return offset_; } inline static hdr_rca* access(Packet* p) { return (hdr_rca*) p->access(offset_); } /* per-field member functions */ inline int& msg_type() { return (msg_type_); } inline int& meta_size() { return (meta_size_); } inline float& get_dist() { return (dist_to_dest_); } inline int& dist_est() { return (dist_est_); } inline void set_meta(const char* data) { meta_size_ = strlen(data); if (meta_size_ > maxmetasize()) { printf("Error: Meta size %d too large (max = %d).\n", meta_size_, maxmetasize()); exit(1); } memcpy(meta_, data, meta_size_+1); } inline char* const meta() { return (meta_); } inline int maxmetasize() { return (sizeof(meta_)); } inline int& rca_src() { return (rca_src_); } inline int& rca_mac_dst() { return (rca_mac_dst_); } inline int& rca_link_dst() { return (rca_link_dst_); } inline int& get_code() { return (code_); }};#endifclass PacketHeaderClass : public TclClass {protected: PacketHeaderClass(const char* classname, int hdrsize); virtual int method(int argc, const char*const* argv); void field_offset(const char* fieldname, int offset); inline void bind_offset(int* off) { offset_ = off; } inline void offset(int* off) {offset_= off;} int hdrlen_; // # of bytes for this header int* offset_; // offset for this headerpublic: virtual void bind(); virtual void export_offsets(); TclObject* create(int argc, const char*const* argv);};inline void Packet::init(Packet* p){ bzero(p->bits_, hdrlen_);}inline Packet* Packet::alloc(){ Packet* p = free_; if (p != 0) { assert(p->fflag_ == FALSE); free_ = p->next_; assert(p->data_ == 0);#ifdef MIT_uAMPS hdr_rca* rca_hdr = HDR_RCA(p); rca_hdr->meta_size_ = 0;#endif p->uid_ = 0; p->time_ = 0; } else { p = new Packet; p->bits_ = new unsigned char[hdrlen_]; if (p == 0 || p->bits_ == 0) abort(); } init(p); // Initialize bits_[] (HDR_CMN(p))->next_hop_ = -2; // -1 reserved for IP_BROADCAST (HDR_CMN(p))->last_hop_ = -2; // -1 reserved for IP_BROADCAST p->fflag_ = TRUE; (HDR_CMN(p))->direction() = hdr_cmn::DOWN; /* setting all direction of pkts to be downward as default; until channel changes it to +1 (upward) */ p->next_ = 0; return (p);}#ifdef MIT_uAMPSinline void Packet::PrintRcHeader(Packet *p, char *layer){ hdr_cmn *hdr = HDR_CMN(p); hdr_rca *rca_hdr = HDR_RCA(p); printf("%s Layer received: Type=%d data_size=%d\n\tMeta = %s\n\tSource = %x\n\tTarget = %x\n\tLink_target = %x\n",layer,rca_hdr->msg_type(), hdr->size(), rca_hdr->meta(),rca_hdr->rca_src(), rca_hdr->rca_mac_dst(), rca_hdr->rca_link_dst());}#endif/* * Allocate an n byte data buffer to an existing packet * * To set application-specific AppData, use Packet::setdata() */inline void Packet::allocdata(int n){ assert(data_ == 0); data_ = new PacketData(n); if (data_ == 0) abort();}/* allocate a packet with an n byte data buffer */inline Packet* Packet::alloc(int n){ Packet* p = alloc(); if (n > 0) p->allocdata(n); return (p);}inline void Packet::free(Packet* p){ if (p->fflag_) { if (p->ref_count_ == 0) { /* * A packet's uid may be < 0 (out of a event queue), or * == 0 (newed but never gets into the event queue. */ assert(p->uid_ <= 0); // Delete user data because we won't need it any more. if (p->data_ != 0) { delete p->data_; p->data_ = 0; } init(p); p->next_ = free_; free_ = p; p->fflag_ = FALSE; } else { --p->ref_count_; } }}inline Packet* Packet::copy() const{ Packet* p = alloc(); memcpy(p->bits(), bits_, hdrlen_); if (data_) p->data_ = data_->copy(); p->txinfo_.init(&txinfo_);#ifdef MIT_uAMPS hdr_rca* ch = HDR_RCA(this); hdr_rca* new_ch = HDR_RCA(p); if (ch->meta_size_) { new_ch->meta_size_ = ch->meta_size_; memcpy(new_ch->meta_, ch->meta_, ch->meta_size_+1); }#endif return (p);}inline voidPacket::dump_header(Packet *p, int offset, int length){ assert(offset + length <= p->hdrlen_); struct hdr_cmn *ch = HDR_CMN(p); fprintf(stderr, "\nPacket ID: %d\n", ch->uid()); for(int i = 0; i < length ; i+=16) { fprintf(stderr, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", p->bits_[offset + i], p->bits_[offset + i + 1], p->bits_[offset + i + 2], p->bits_[offset + i + 3], p->bits_[offset + i + 4], p->bits_[offset + i + 5], p->bits_[offset + i + 6], p->bits_[offset + i + 7], p->bits_[offset + i + 8], p->bits_[offset + i + 9], p->bits_[offset + i + 10], p->bits_[offset + i + 11], p->bits_[offset + i + 12], p->bits_[offset + i + 13], p->bits_[offset + i + 14], p->bits_[offset + i + 15]); }}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -