From: Josh Date: Wed, 10 Dec 2014 18:41:05 +0000 (-0600) Subject: privatizing codec Buffer's data field X-Git-Tag: 3.0.0-233~1089^2~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=af875778b1ecb0c156368fc0ddd4f39f4c5b0b2d;p=thirdparty%2Fsnort3.git privatizing codec Buffer's data field --- diff --git a/src/codecs/ip/cd_ipv4.cc b/src/codecs/ip/cd_ipv4.cc index ba4f6d532..1b097cd7c 100644 --- a/src/codecs/ip/cd_ipv4.cc +++ b/src/codecs/ip/cd_ipv4.cc @@ -679,7 +679,7 @@ bool Ipv4Codec::encode(const uint8_t* const raw_in, const uint16_t /*raw_len*/, const ip::IP4Hdr* const ip4h_in = reinterpret_cast(raw_in); - ip::IP4Hdr* const ip4h_out = reinterpret_cast(buf.base); + ip::IP4Hdr* const ip4h_out = reinterpret_cast(buf.data()); /* IPv4 encoded header is hardcoded 20 bytes */ ip4h_out->ip_verhl = 0x45; diff --git a/src/codecs/ip/cd_ipv6.cc b/src/codecs/ip/cd_ipv6.cc index 88f6d8194..f5b3ec1a5 100644 --- a/src/codecs/ip/cd_ipv6.cc +++ b/src/codecs/ip/cd_ipv6.cc @@ -588,7 +588,7 @@ bool Ipv6Codec::encode(const uint8_t* const raw_in, const uint16_t /*raw_len*/, return false; const ip::IP6Hdr* const hi = reinterpret_cast(raw_in); - ip::IP6Hdr* const ipvh_out = reinterpret_cast(buf.base); + ip::IP6Hdr* const ipvh_out = reinterpret_cast(buf.data()); diff --git a/src/codecs/ip/cd_tcp.cc b/src/codecs/ip/cd_tcp.cc index 733be3eba..0912f7aef 100644 --- a/src/codecs/ip/cd_tcp.cc +++ b/src/codecs/ip/cd_tcp.cc @@ -608,7 +608,7 @@ bool TcpCodec::encode(const uint8_t* const raw_in, const uint16_t /*raw_len*/, if (!buf.allocate(tcp::TCP_MIN_HEADER_LEN)) return false; - tcp::TCPHdr* tcph_out = reinterpret_cast(buf.base); + tcp::TCPHdr* tcph_out = reinterpret_cast(buf.data()); const int ctl = (hi->th_flags & TH_SYN) ? 1 : 0; if ( forward(enc.flags) ) diff --git a/src/codecs/ip/cd_udp.cc b/src/codecs/ip/cd_udp.cc index 9658be6f8..c453ce233 100644 --- a/src/codecs/ip/cd_udp.cc +++ b/src/codecs/ip/cd_udp.cc @@ -379,7 +379,7 @@ bool UdpCodec::encode(const uint8_t* const raw_in, const uint16_t /*raw_len*/, return false; const udp::UDPHdr* const hi = reinterpret_cast(raw_in); - udp::UDPHdr* const udph_out = reinterpret_cast(buf.base); + udp::UDPHdr* const udph_out = reinterpret_cast(buf.data()); if ( forward(enc.flags) ) { diff --git a/src/codecs/link/cd_pppoe.cc b/src/codecs/link/cd_pppoe.cc index 5ab4741d2..87be00e55 100644 --- a/src/codecs/link/cd_pppoe.cc +++ b/src/codecs/link/cd_pppoe.cc @@ -267,8 +267,8 @@ static inline bool pppoepkt_encode(const uint8_t* const raw_in, if (!buf.allocate(raw_len)) return false; - memcpy(buf.base, raw_in, raw_len); - PPPoEHdr* const ppph = reinterpret_cast(buf.base); + memcpy(buf.data(), raw_in, raw_len); + PPPoEHdr* const ppph = reinterpret_cast(buf.data()); ppph->length = htons((uint16_t)buf.size()); return true; diff --git a/src/codecs/misc/cd_gtp.cc b/src/codecs/misc/cd_gtp.cc index fbe5424ca..041d7cb18 100644 --- a/src/codecs/misc/cd_gtp.cc +++ b/src/codecs/misc/cd_gtp.cc @@ -269,7 +269,7 @@ bool GtpCodec::encode(const uint8_t* const raw_in, const uint16_t raw_len, if (buf.allocate(raw_len)) return false; - GTPHdr* const gtph = reinterpret_cast(buf.base); + GTPHdr* const gtph = reinterpret_cast(buf.data()); memcpy(gtph, raw_in, raw_len); return update_GTP_length(gtph, buf.size()); } diff --git a/src/codecs/root/cd_eth.cc b/src/codecs/root/cd_eth.cc index d784beb3a..56a11b9f7 100644 --- a/src/codecs/root/cd_eth.cc +++ b/src/codecs/root/cd_eth.cc @@ -187,7 +187,7 @@ bool EthCodec::encode(const uint8_t* const raw_in, const uint16_t /*raw_len*/, if (!buf.allocate(sizeof(*ho))) return false; - ho = reinterpret_cast(buf.base); + ho = reinterpret_cast(buf.data()); if (enc.ethertype_set()) ho->ether_type = enc.next_ethertype; diff --git a/src/framework/codec.h b/src/framework/codec.h index 1054e9bcc..2f8a575a1 100644 --- a/src/framework/codec.h +++ b/src/framework/codec.h @@ -147,13 +147,7 @@ struct EncState // * base+size-1 is last byte of packet (in) / buffer (out) struct Buffer { - uint8_t* base; /* start of data */ /* FIXIT-L J - make this private. Ppl should be to access, not manipulate */ - uint32_t off; /* offset into data */ -private: - uint32_t end; /* end of data */ - const uint32_t max_len; /* size of allocation */ public: - /* Logic behind 'buf + size + 1' -- we're encoding the * packet from the inside out. So, whenever we add * data, 'allocating' N bytes means moving the pointer @@ -164,11 +158,14 @@ public: */ Buffer(uint8_t* buf, uint32_t size) : base(buf + size + 1), - off(0), end(0), - max_len(size) + max_len(size), + off(0) { } + inline uint8_t* data() const + { return base; } + uint32_t size() const { return end; } @@ -188,6 +185,15 @@ public: end = 0; off = 0; } + + +private: + uint8_t* base; /* start of data */ + uint32_t end; /* end of data */ + const uint32_t max_len; /* size of allocation */ + +public: + uint32_t off; /* offset into data */ }; @@ -355,7 +361,7 @@ public: * be set. * * NOTE: all funtions MUST call the Buffer.allocate() function before - * memory. + * manipulating memory. */ virtual bool encode(const uint8_t* const /*raw_in */, const uint16_t /*raw_len*/, diff --git a/src/protocols/packet_manager.cc b/src/protocols/packet_manager.cc index a7d5b6c10..84078edb6 100644 --- a/src/protocols/packet_manager.cc +++ b/src/protocols/packet_manager.cc @@ -445,7 +445,7 @@ const uint8_t* PacketManager::encode_response( if (!buf.allocate(payload_len)) return nullptr; - memcpy(buf.base, payload, payload_len); + memcpy(buf.data(), payload, payload_len); flags |= ENC_FLAG_PAY; } flags |= ENC_FLAG_FIN; @@ -457,7 +457,7 @@ const uint8_t* PacketManager::encode_response( if (!buf.allocate(payload_len)) return nullptr; - memcpy(buf.base, payload, payload_len); + memcpy(buf.data(), payload, payload_len); flags |= ENC_FLAG_PAY; } flags |= ENC_FLAG_PSH; @@ -474,7 +474,7 @@ const uint8_t* PacketManager::encode_response( if (encode(p, flags, p->num_layers-1, ENC_PROTO_UNSET , buf)) { len = buf.size(); - return buf.base + buf.off; + return buf.data() + buf.off; } len = 0; @@ -497,7 +497,7 @@ const uint8_t* PacketManager::encode_reject( UnreachResponse type, if (!buf.allocate(icmp::ICMP_UNREACH_DATA_LEN)) return nullptr; - memcpy(buf.base, p->layers[inner_ip_index+1].start, icmp::ICMP_UNREACH_DATA_LEN); + memcpy(buf.data(), p->layers[inner_ip_index+1].start, icmp::ICMP_UNREACH_DATA_LEN); const ip::IP4Hdr* const ip4h = @@ -506,14 +506,14 @@ const uint8_t* PacketManager::encode_reject( UnreachResponse type, if (!buf.allocate(ip_len)) return nullptr; - memcpy(buf.base, ip4h, ip_len); + memcpy(buf.data(), ip4h, ip_len); // If this returns false, we're down pig creek. if (!buf.allocate(sizeof(icmp::Icmp4Base))) return nullptr; - icmp::Icmp4Base* const icmph = reinterpret_cast(buf.base); + icmp::Icmp4Base* const icmph = reinterpret_cast(buf.data()); icmph->type = icmp::IcmpType::DEST_UNREACH; icmph->csum = 0; icmph->opt32 = 0; @@ -536,13 +536,13 @@ const uint8_t* PacketManager::encode_reject( UnreachResponse type, icmph->code = icmp::IcmpCode::PORT_UNREACH; } - icmph->csum = checksum::icmp_cksum((uint16_t *)buf.base, buf.size()); + icmph->csum = checksum::icmp_cksum((uint16_t *)buf.data(), buf.size()); if (encode(p, flags, p->num_layers-1, IPPROTO_ID_ICMPV4, buf)) { len = buf.size(); - return buf.base + buf.off; + return buf.data() + buf.off; } len = 0; @@ -559,19 +559,19 @@ const uint8_t* PacketManager::encode_reject( UnreachResponse type, if (!buf.allocate(icmp::ICMP_UNREACH_DATA_LEN)) return nullptr; - memcpy(buf.base, p->layers[inner_ip_index+1].start, icmp::ICMP_UNREACH_DATA_LEN); + memcpy(buf.data(), p->layers[inner_ip_index+1].start, icmp::ICMP_UNREACH_DATA_LEN); // copy original ip header if (!buf.allocate(ip::IP6_HEADER_LEN)) return nullptr; const ip::IP6Hdr* const ip6h = p->ptrs.ip_api.get_ip6h(); - memcpy(buf.base, ip6h, ip::IP6_HEADER_LEN); + memcpy(buf.data(), ip6h, ip::IP6_HEADER_LEN); if (!buf.allocate(sizeof(icmp::Icmp6Hdr))) return nullptr; - icmp::Icmp6Hdr* const icmph = reinterpret_cast(buf.base); + icmp::Icmp6Hdr* const icmph = reinterpret_cast(buf.data()); icmph->type = icmp::Icmp6Types::UNREACH; icmph->csum = 0; icmph->opt32 = 0; @@ -604,13 +604,13 @@ const uint8_t* PacketManager::encode_reject( UnreachResponse type, ps6.protocol = IPPROTO_ICMPV6; ps6.len = htons((uint16_t)(ip_len)); - icmph->csum = checksum::icmp_cksum((uint16_t *)buf.base, ip_len, &ps6); + icmph->csum = checksum::icmp_cksum((uint16_t *)buf.data(), ip_len, &ps6); if (encode(p, flags, p->num_layers-1, IPPROTO_ICMPV6, buf)) { len = buf.size(); - return buf.base + buf.off; + return buf.data() + buf.off; } len = 0;