📄 packet.hh
字号:
* @pre On aligned targets, @a i must be evenly divisible by 2. * * Affects annotation bytes [@a i, @a i+1]. */ uint16_t anno_u16(int i) const { assert(i >= 0 && i < anno_size - 1);#if !HAVE_INDIFFERENT_ALIGNMENT assert(i % 2 == 0);#endif return *reinterpret_cast<const uint16_t *>(xanno()->c + i); } /** @brief Set 16-bit annotation at offset @a i. * @param i annotation offset in bytes * @param v value * @pre 0 <= @a i < @link Packet::anno_size anno_size @endlink - 1 * @pre On aligned targets, @a i must be evenly divisible by 2. * * Affects annotation bytes [@a i, @a i+1]. */ void set_anno_u16(int i, uint16_t v) { assert(i >= 0 && i < anno_size - 1);#if !HAVE_INDIFFERENT_ALIGNMENT assert(i % 2 == 0);#endif *reinterpret_cast<uint16_t *>(xanno()->c + i) = v; } /** @brief Return 16-bit annotation at offset @a i. * @pre 0 <= @a i < @link Packet::anno_size anno_size @endlink - 1 * @pre On aligned targets, @a i must be evenly divisible by 2. * * Affects annotation bytes [@a i, @a i+1]. */ int16_t anno_s16(int i) const { assert(i >= 0 && i < anno_size - 1);#if !HAVE_INDIFFERENT_ALIGNMENT assert(i % 2 == 0);#endif return *reinterpret_cast<const int16_t *>(xanno()->c + i); } /** @brief Set 16-bit annotation at offset @a i. * @param i annotation offset in bytes * @param v value * @pre 0 <= @a i < @link Packet::anno_size anno_size @endlink - 1 * @pre On aligned targets, @a i must be evenly divisible by 2. * * Affects annotation bytes [@a i, @a i+1]. */ void set_anno_s16(int i, int16_t v) { assert(i >= 0 && i < anno_size - 1);#if !HAVE_INDIFFERENT_ALIGNMENT assert(i % 2 == 0);#endif *reinterpret_cast<int16_t *>(xanno()->c + i) = v; } /** @brief Return 32-bit annotation at offset @a i. * @pre 0 <= @a i < @link Packet::anno_size anno_size @endlink - 3 * @pre On aligned targets, @a i must be evenly divisible by 4. * * Affects user annotation bytes [@a i, @a i+3]. */ uint32_t anno_u32(int i) const { assert(i >= 0 && i < anno_size - 3);#if !HAVE_INDIFFERENT_ALIGNMENT assert(i % 4 == 0);#endif return *reinterpret_cast<const uint32_t *>(xanno()->c + i); } /** @brief Set 32-bit annotation at offset @a i. * @param i annotation offset in bytes * @param v value * @pre 0 <= @a i < @link Packet::anno_size anno_size @endlink - 3 * @pre On aligned targets, @a i must be evenly divisible by 4. * * Affects user annotation bytes [@a i, @a i+3]. */ void set_anno_u32(int i, uint32_t v) { assert(i >= 0 && i < anno_size - 3);#if !HAVE_INDIFFERENT_ALIGNMENT assert(i % 4 == 0);#endif *reinterpret_cast<uint32_t *>(xanno()->c + i) = v; } /** @brief Return 32-bit annotation at offset @a i. * @pre 0 <= @a i < @link Packet::anno_size anno_size @endlink - 3 * * Affects user annotation bytes [4*@a i, 4*@a i+3]. */ int32_t anno_s32(int i) const { assert(i >= 0 && i < anno_size - 3);#if !HAVE_INDIFFERENT_ALIGNMENT assert(i % 4 == 0);#endif return *reinterpret_cast<const int32_t *>(xanno()->c + i); } /** @brief Set 32-bit annotation at offset @a i. * @param i annotation offset in bytes * @param v value * @pre 0 <= @a i < @link Packet::anno_size anno_size @endlink - 3 * @pre On aligned targets, @a i must be evenly divisible by 4. * * Affects user annotation bytes [@a i, @a i+3]. */ void set_anno_s32(int i, int32_t v) { assert(i >= 0 && i < anno_size - 3);#if !HAVE_INDIFFERENT_ALIGNMENT assert(i % 4 == 0);#endif *reinterpret_cast<int32_t *>(xanno()->c + i) = v; }#if HAVE_INT64_TYPES /** @brief Return 64-bit annotation at offset @a i. * @pre 0 <= @a i < @link Packet::anno_size anno_size @endlink - 7 * @pre On aligned targets, @a i must be aligned properly for uint64_t. * * Affects user annotation bytes [@a i, @a i+7]. */ uint64_t anno_u64(int i) const { assert(i >= 0 && i < anno_size - 7);#if !HAVE_INDIFFERENT_ALIGNMENT assert(i % __alignof__(uint64_t) == 0);#endif return *reinterpret_cast<const uint64_t *>(xanno()->c + i); } /** @brief Set 64-bit annotation at offset @a i. * @param i annotation offset in bytes * @param v value * @pre 0 <= @a i < @link Packet::anno_size anno_size @endlink - 7 * @pre On aligned targets, @a i must be aligned properly for uint64_t. * * Affects user annotation bytes [@a i, @a i+7]. */ void set_anno_u64(int i, uint64_t v) { assert(i >= 0 && i < anno_size - 7);#if !HAVE_INDIFFERENT_ALIGNMENT assert(i % __alignof__(uint64_t) == 0);#endif *reinterpret_cast<uint64_t *>(xanno()->c + i) = v; }#endif inline void clear_annotations(bool all = true); inline void copy_annotations(const Packet *); //@} /** @cond never */ enum { DEFAULT_HEADROOM = default_headroom, MIN_BUFFER_LENGTH = min_buffer_length, addr_anno_offset = 0, addr_anno_size = 16, user_anno_offset = 16, user_anno_size = 32, ADDR_ANNO_SIZE = addr_anno_size, USER_ANNO_SIZE = user_anno_size, USER_ANNO_U16_SIZE = USER_ANNO_SIZE / 2, USER_ANNO_U32_SIZE = USER_ANNO_SIZE / 4, USER_ANNO_U64_SIZE = USER_ANNO_SIZE / 8 } CLICK_DEPRECATED; inline const unsigned char *buffer_data() const CLICK_DEPRECATED; inline void *addr_anno() CLICK_DEPRECATED; inline const void *addr_anno() const CLICK_DEPRECATED; inline void *user_anno() CLICK_DEPRECATED; inline const void *user_anno() const CLICK_DEPRECATED; inline uint8_t *user_anno_u8() CLICK_DEPRECATED; inline const uint8_t *user_anno_u8() const CLICK_DEPRECATED; inline uint32_t *user_anno_u32() CLICK_DEPRECATED; inline const uint32_t *user_anno_u32() const CLICK_DEPRECATED; inline uint8_t user_anno_u8(int i) const CLICK_DEPRECATED; inline void set_user_anno_u8(int i, uint8_t v) CLICK_DEPRECATED; inline uint16_t user_anno_u16(int i) const CLICK_DEPRECATED; inline void set_user_anno_u16(int i, uint16_t v) CLICK_DEPRECATED; inline uint32_t user_anno_u32(int i) const CLICK_DEPRECATED; inline void set_user_anno_u32(int i, uint32_t v) CLICK_DEPRECATED; inline int32_t user_anno_s32(int i) const CLICK_DEPRECATED; inline void set_user_anno_s32(int i, int32_t v) CLICK_DEPRECATED;#if HAVE_INT64_TYPES inline uint64_t user_anno_u64(int i) const CLICK_DEPRECATED; inline void set_user_anno_u64(int i, uint64_t v) CLICK_DEPRECATED;#endif inline const uint8_t *all_user_anno() const CLICK_DEPRECATED; inline uint8_t *all_user_anno() CLICK_DEPRECATED; inline const uint32_t *all_user_anno_u() const CLICK_DEPRECATED; inline uint32_t *all_user_anno_u() CLICK_DEPRECATED; inline uint8_t user_anno_c(int) const CLICK_DEPRECATED; inline void set_user_anno_c(int, uint8_t) CLICK_DEPRECATED; inline int16_t user_anno_s(int) const CLICK_DEPRECATED; inline void set_user_anno_s(int, int16_t) CLICK_DEPRECATED; inline uint16_t user_anno_us(int) const CLICK_DEPRECATED; inline void set_user_anno_us(int, uint16_t) CLICK_DEPRECATED; inline int32_t user_anno_i(int) const CLICK_DEPRECATED; inline void set_user_anno_i(int, int32_t) CLICK_DEPRECATED; inline uint32_t user_anno_u(int) const CLICK_DEPRECATED; inline void set_user_anno_u(int, uint32_t) CLICK_DEPRECATED; /** @endcond never */ private: // Anno must fit in sk_buff's char cb[48]. /** @cond never */ union Anno { char c[anno_size]; uint8_t u8[anno_size]; uint16_t u16[anno_size / 2]; uint32_t u32[anno_size / 4];#if HAVE_INT64_TYPES uint64_t u64[anno_size / 8];#endif // allocations: see packet_anno.hh }; /** @endcond never */#if !CLICK_LINUXMODULE // User-space and BSD kernel module implementations. atomic_uint32_t _use_count; Packet *_data_packet; /* mimic Linux sk_buff */ unsigned char *_head; /* start of allocated buffer */ unsigned char *_data; /* where the packet starts */ unsigned char *_tail; /* one beyond end of packet */ unsigned char *_end; /* one beyond end of allocated buffer */# if CLICK_USERLEVEL void (*_destructor)(unsigned char *, size_t);# endif unsigned char _cb[48]; unsigned char *_mac; unsigned char *_nh; unsigned char *_h; PacketType _pkt_type; Timestamp _timestamp;# if CLICK_BSDMODULE struct mbuf *_m;# endif Packet *_next; Packet *_prev;# if CLICK_NS SimPacketinfoWrapper _sim_packetinfo;# endif#endif Packet(); Packet(const Packet &); ~Packet(); Packet &operator=(const Packet &);#if !CLICK_LINUXMODULE Packet(int, int, int) { } static WritablePacket *make(int, int, int); bool alloc_data(uint32_t, uint32_t, uint32_t);#endif#if CLICK_BSDMODULE static void assimilate_mbuf(Packet *p); void assimilate_mbuf();#endif inline void shift_header_annotations(const unsigned char *old_head, int32_t extra_headroom); WritablePacket *expensive_uniqueify(int32_t extra_headroom, int32_t extra_tailroom, bool free_on_failure); WritablePacket *expensive_push(uint32_t nbytes); WritablePacket *expensive_put(uint32_t nbytes); friend class WritablePacket;};class WritablePacket : public Packet { public: inline unsigned char *data() const; inline unsigned char *end_data() const; inline unsigned char *buffer() const; inline unsigned char *end_buffer() const; inline unsigned char *mac_header() const; inline click_ether *ether_header() const; inline unsigned char *network_header() const; inline click_ip *ip_header() const; inline click_ip6 *ip6_header() const; inline unsigned char *transport_header() const; inline click_icmp *icmp_header() const; inline click_tcp *tcp_header() const; inline click_udp *udp_header() const; /** @cond never */ inline unsigned char *buffer_data() const CLICK_DEPRECATED; /** @endcond never */ private: WritablePacket() { } WritablePacket(const Packet &) { } ~WritablePacket() { } friend class Packet;};/** @brief Return the packet's data pointer. * * This is the pointer to the first byte of packet data. */inline const unsigned char *Packet::data() const{#if CLICK_LINUXMODULE return skb()->data;#else return _data;#endif}/** @brief Return the packet's end data pointer. * * The result points at the byte following the packet data. * @invariant end_data() == data() + length() */inline const unsigned char *Packet::end_data() const{#if CLICK_LINUXMODULE# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) return skb_tail_pointer(skb());# else return skb()->tail;# endif#else return _tail;#endif}/** @brief Return a pointer to the packet's data buffer. * * The result points at the packet's headroom, not its data. * @invariant buffer() == data() - headroom() */inline const unsigned char *Packet::buffer() const{#if CLICK_LINUXMODULE return skb()->head;#else return _head;#endif}/** @brief Return the packet's end data buffer pointer. * * The result points past the packet's tailroom. * @invariant end_buffer() == end_data() + tailroom() */inline const unsigned char *Packet::end_buffer() const{#if CLICK_LINUXMODULE# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) return skb_end_pointer(skb());# else return skb()->end;# endif#else return _end;#endif}/** @brief Return the packet's length. */inline uint32_tPacket::length() const{#if CLICK_LINUXMODULE return skb()->len;#else return _tail - _data;#endif}/** @brief Return the packet's headroom. * * The headroom is the amount of space available in the current packet buffer * before data(). A push() operation is cheap if the packet's unshared and * the length pushed is less than headroom(). */inline uint32_tPacket::headroom() const{ return data() - buffer();}/** @brief Return the packet's tailroom. * * The tailroom is the amount of space available in the current packet buffer * following end_data(). A put() operation is cheap if the packet's unshared * and the length put is less than tailroom(). */inline uint32_tPacket::tailroom() const{ return end_buffer() - end_data();}/** @brief Return the packet's buffer length. * @invariant buffer_length() == headroom() + length() + tailroom() * @invariant buffer() + buffer_length() == end_buffer() */inline uint32_tPacket::buffer_length() const{ return end_buffer() - buffer();}inline Packet *Packet::next() const{#if CLICK_LINUXMODULE return (Packet *)(skb()->next);#else return _next;#endif}inline Packet *&Packet::next(){#if CLICK_LINUXMODULE return (Packet *&)(skb()->next);#else return _next;#endif}inline voidPacket::set_next(Packet *p){#if CLICK_LINUXMODULE skb()->next = p->skb();#else _next = p;#endif}inline Packet *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -