]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
statistics framework added. codec api changed.
authorJosh <jrosenba@cisco.com>
Wed, 23 Apr 2014 16:57:36 +0000 (12:57 -0400)
committerJosh <jrosenba@cisco.com>
Wed, 23 Apr 2014 16:57:36 +0000 (12:57 -0400)
20 files changed:
src/codecs/CMakeLists.txt
src/codecs/basic/cd_esp.cc
src/codecs/basic/cd_eth.cc
src/codecs/basic/cd_ipv4.cc
src/codecs/cd_stats.cc [new file with mode: 0644]
src/codecs/cd_stats.h [new file with mode: 0644]
src/codecs/plugins/cd_ah.cc
src/codecs/plugins/cd_arp.cc
src/codecs/plugins/cd_erspan2.cc
src/codecs/plugins/cd_erspan3.cc
src/codecs/plugins/cd_ethloopback.cc
src/codecs/plugins/cd_gre.cc
src/codecs/plugins/cd_mpls.cc
src/codecs/plugins/cd_pppencap.cc
src/codecs/plugins/cd_pppoepkt.cc
src/codecs/plugins/cd_transbridge.cc
src/codecs/plugins/cd_vlan.cc
src/framework/codec.h
src/managers/packet_manager.cc
src/managers/packet_manager.h

index c90bfc9737c0f7dfedcbbaeef860d8de976a338f..a041b438a0ae90f2499e7287af866b13d05ee5aa 100644 (file)
@@ -29,6 +29,8 @@ add_library( codecs STATIC
     decode_module.cc
     codec_api.h
     codec_api.cc
+    cd_stats.h
+    cd_stats.cc
 )
 
 
index cea7def6ad6e6c37e0163bada320c91d9b53e2ee..9579b6b309e7d65f762ba6affe00669ab22cfeb3 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
 /*
 ** Copyright (C) 2002-2013 Sourcefire, Inc.
 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
-#if 0
-
-#ifdef HAVE_DUMBNET_H
-#include <dumbnet.h>
-#else
-#include <dnet.h>
-#endif
-#endif
 
 #include "framework/codec.h"
 #include "codecs/codec_events.h"
@@ -53,8 +43,6 @@ public:
 
     virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, 
         Packet *, uint16_t &p_hdr_len, int &next_prot_id);
-
-    virtual void get_protocol_ids(std::vector<uint16_t>&);
     
 };
 
@@ -154,9 +142,7 @@ bool EspCodec::decode(const uint8_t *raw_pkt, const uint32_t len,
     return true;
 }
 
-
-
-void EspCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
 {
     v.push_back(ESP_PROT_ID);
 }
@@ -196,6 +182,8 @@ static const CodecApi esp_api =
     NULL, // tterm
     ctor, // ctor
     dtor, // dtor
+    NULL, // get_dlt()
+    get_protocol_ids,
     sum, // sum
     stats  // stats
 };
index 4ea76e65fe1da146ad1e90b48620a60fe7f74255..cfd61001b39dd5e5aca894ed8342ad7bc00914ec 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
 /*
 ** Copyright (C) 2002-2013 Sourcefire, Inc.
 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
@@ -52,8 +50,6 @@ public:
     virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, 
         Packet *p, uint16_t &p_hdr_len, int &next_prot_id);
 
-    virtual void get_protocol_ids(std::vector<uint16_t>&);
-    virtual void get_data_link_type(std::vector<int>&);
     // DELETE
     #include "codecs/sf_protocols.h"
     virtual inline PROTO_ID get_proto_id() { return PROTO_ETH; };
@@ -63,7 +59,6 @@ public:
 } // anonymous namespace
 
 
-
 //--------------------------------------------------------------------
 // decode.c::Ethernet
 //--------------------------------------------------------------------
@@ -209,14 +204,11 @@ void Eth_Format (EncodeFlags f, const Packet* p, Packet* c, Layer* lyr)
 #endif
 
 
-void EthCodec::get_data_link_type(std::vector<int>&v)
+static void get_data_link_type(std::vector<int>&v)
 {
     v.push_back(DLT_EN10MB);
 }
 
-void EthCodec::get_protocol_ids(std::vector<uint16_t>& v)
-{
-}
 
 static Codec* ctor()
 {
@@ -239,8 +231,10 @@ static const CodecApi eth_api =
     NULL, // tterm
     ctor, // ctor
     dtor, // dtor
+    get_data_link_type,
+    NULL,
+    NULL,
     NULL,
-    NULL
 };
 
 const BaseApi* cd_eth = &eth_api.base;
index 2aa1d791e4063f22eda322be58c6e75a87858e2e..67a6ec47fc6eabfc1dd1f5c218521b7ff09f1c03 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
 /*
 ** Copyright (C) 2002-2013 Sourcefire, Inc.
 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
diff --git a/src/codecs/cd_stats.cc b/src/codecs/cd_stats.cc
new file mode 100644 (file)
index 0000000..dd498d9
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+** Copyright (C) 2002-2013 Sourcefire, Inc.
+** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License Version 2 as
+** published by the Free Software Foundation.  You may not use, modify or
+** distribute this program under any other version of the GNU General
+** Public License.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+
+#include "cd_stats.h"
+#include "src/thread.h"
+
+namespace codec_statistics
+{
+
+static THREAD_LOCAL ProtType curr_type;
+
+
+void set_state(ProtType ct)
+{
+    curr_type = ct;
+}
+
+ProtType get_state(){
+    return curr_type;
+}
+
+} //namespace codec_statistics
diff --git a/src/codecs/cd_stats.h b/src/codecs/cd_stats.h
new file mode 100644 (file)
index 0000000..f6826f7
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+** Copyright (C) 2002-2013 Sourcefire, Inc.
+** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License Version 2 as
+** published by the Free Software Foundation.  You may not use, modify or
+** distribute this program under any other version of the GNU General
+** Public License.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+
+#ifndef CD_STATS_H
+#define CD_STATS_H
+
+namespace codec_statistics
+{
+
+enum class ProtType{
+    PROT_IPV4,
+    PROT_IPV6,
+    PROT_IPV6_EXT,
+    PROT_ICMP4,
+    PROT_ICMP6,
+    PROT_UDP,
+    PROT_TCP,
+    PROT_GRE,
+};
+
+void set_state(ProtType);
+ProtType get_state();
+
+}; // namespace codec_statistics
+
+#endif
index 99408e28885d99b7b20e4707f851a5807ae72efc..c7e14f610aa8c747c575b18ae6e101e07d1d512b 100644 (file)
@@ -43,7 +43,6 @@ public:
 
     virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, 
         Packet *, uint16_t &p_hdr_len, int &next_prot_id);
-    virtual void get_protocol_ids(std::vector<uint16_t>&);
 
 
     // DELETE from here and below
@@ -76,7 +75,7 @@ bool AhCodec::decode(const uint8_t *raw_pkt, const uint32_t len,
 
 
 
-void AhCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
 {
     v.push_back(AH_PROT_ID);
 }
@@ -116,6 +115,8 @@ static const CodecApi ah_api =
     NULL, // tterm
     ctor, // ctor
     dtor, // dtor
+    nullptr, 
+    get_protocol_ids,
     sum, // sum
     stats  // stats
 };
index 130720dd6d6f45a76d895e672a9e577fb037d7ee..2c38ce179a5fa2b6750f1346a5272874b3d1ec7e 100644 (file)
@@ -42,7 +42,6 @@ public:
 
     virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, 
         Packet *, uint16_t &p_hdr_len, int &next_prot_id);
-    virtual void get_protocol_ids(std::vector<uint16_t>&);
     
 
     // DELETE from here and below
@@ -101,7 +100,7 @@ bool ArpCodec::decode(const uint8_t *raw_pkt, const uint32_t len,
 
 
 
-void ArpCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
 {
     v.push_back(ETHERNET_TYPE_ARP);
     v.push_back(ETHERNET_TYPE_REVARP);
@@ -142,6 +141,8 @@ static const CodecApi arp_api =
     NULL, // tterm
     ctor, // ctor
     dtor, // dtor
+    nullptr, // get_dlt
+    get_protocol_ids,
     sum, // sum
     stats  // stats
 };
index 5e310f334c55b0191a3edada51f1d9e6fbf1db1f..eda44193ad19c72385718c2fb183dbad4f25239b 100644 (file)
@@ -38,8 +38,6 @@ public:
 
     virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, 
         Packet *, uint16_t &p_hdr_len, int &next_prot_id);
-
-    virtual void get_protocol_ids(std::vector<uint16_t>&);
     
     // DELETE from here and below
     #include "codecs/sf_protocols.h"
@@ -111,7 +109,7 @@ bool Erspan2Codec::decode(const uint8_t *raw_pkt, const uint32_t len,
 
 
 
-void Erspan2Codec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
 {
     v.push_back(ETHERTYPE_ERSPAN_TYPE2);
 }
@@ -151,6 +149,8 @@ static const CodecApi erspan2_api =
     NULL, // tterm
     ctor, // ctor
     dtor, // dtor
+    nullptr,
+    get_protocol_ids,
     sum, // sum
     stats  // stats
 };
index 9df6c0bcf39bda38a1079e52697ef0bcb6e9ed47..cb0b6da0dd6f3561d1a8f88417f8aa104686046e 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
 /*
 ** Copyright (C) 2002-2013 Sourcefire, Inc.
 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
@@ -40,8 +38,6 @@ public:
 
     virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, 
         Packet *, uint16_t &p_hdr_len, int &next_prot_id);
-
-    virtual void get_protocol_ids(std::vector<uint16_t>&);
     
     // DELETE from here and below
     #include "codecs/sf_protocols.h"
@@ -114,7 +110,7 @@ bool Erspan3Codec::decode(const uint8_t *raw_pkt, const uint32_t len,
 }
 
 
-void Erspan3Codec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
 {
     v.push_back(ETHERTYPE_ERSPAN_TYPE3);
 }
@@ -154,6 +150,8 @@ static const CodecApi erspan3_api =
     NULL, // tterm
     ctor, // ctor
     dtor, // dtor
+    nullptr, // get_dlt
+    get_protocol_ids,
     sum, // sum
     stats  // stats
 };
index c160238df39aa7676e35b573f061edd681c5861b..aa15a4834ac3bc181f26855bab5369511fd85850 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
 /*
 ** Copyright (C) 2002-2013 Sourcefire, Inc.
 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
@@ -38,8 +36,6 @@ public:
     virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, 
         Packet *, uint16_t &p_hdr_len, int &next_prot_id);
 
-    virtual void get_protocol_ids(std::vector<uint16_t>&);
-    virtual void get_data_link_type(std::vector<int>&){};
     
 };
 
@@ -69,7 +65,7 @@ bool EthLoopbackCodec::decode(const uint8_t *raw_pkt, const uint32_t len,
 //-------------------------------------------------------------------------
 
 
-void EthLoopbackCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
 {
     v.push_back(ETHERNET_TYPE_LOOP);
 }
@@ -109,6 +105,8 @@ static const CodecApi ethloopback_api =
     NULL, // tterm
     ctor, // ctor
     dtor, // dtor
+    nullptr, // get_dlt
+    get_protocol_ids,
     sum, // sum
     stats  // stats
 };
index 87634d3ce886c6d1a17ebf0d6b4dc41ac3d75a1f..49fab38267436241e0022ebcf6dea0942968fe62 100644 (file)
@@ -41,10 +41,6 @@ public:
     virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, 
         Packet *, uint16_t &p_hdr_len, int &next_prot_id);
 
-    virtual void get_protocol_ids(std::vector<uint16_t>&);
-    virtual void get_data_link_type(std::vector<int>&){};
-
-
     // DELETE from here and below
     #include "codecs/sf_protocols.h"
     virtual inline PROTO_ID get_proto_id() { return PROTO_GRE; };
@@ -253,7 +249,7 @@ void GRE_Format (EncodeFlags, const Packet*, Packet* c, Layer* lyr)
 
 
 
-void GreCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
 {
     v.push_back(GRE_PROT_ID);
 }
@@ -293,6 +289,8 @@ static const CodecApi gre_api =
     NULL, // tterm
     ctor, // ctor
     dtor, // dtor
+    nullptr,
+    get_protocol_ids,
     sum, // sum
     stats  // stats
 };
index 281b1fbed57859f10049a39a410528a3ab6983f0..49bbbeae755afa52ab1e3f853c95e937faa00780 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
 /*
 ** Copyright (C) 2002-2013 Sourcefire, Inc.
 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
@@ -45,9 +43,7 @@ public:
 
 
     virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, 
-        Packet *, uint16_t &p_hdr_len, int &next_prot_id);
-    virtual void get_protocol_ids(std::vector<uint16_t>&);
-    
+        Packet *, uint16_t &p_hdr_len, int &next_prot_id);    
 
     // DELETE from here and below
     #include "codecs/sf_protocols.h"
@@ -258,7 +254,7 @@ static int checkMplsHdr(
 }
 
 
-void MplsCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
 {
     v.push_back(ETHERNET_TYPE_MPLS_UNICAST);
     v.push_back(ETHERNET_TYPE_MPLS_MULTICAST);
@@ -299,6 +295,8 @@ static const CodecApi mpls_api =
     NULL, // tterm
     ctor, // ctor
     dtor, // dtor
+    nullptr, // get_dlt
+    get_protocol_ids,
     sum, // sum
     stats  // stats
 };
index 775446578711da80e3f564d1a809f769940dcd95..1c57fec31a64f259c43dd35aeca9e8e8bdd8a922 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
 /*
 ** Copyright (C) 2002-2013 Sourcefire, Inc.
 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
@@ -45,8 +43,6 @@ public:
 
     virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, 
         Packet *, uint16_t &p_hdr_len, int &next_prot_id);
-    virtual void get_protocol_ids(std::vector<uint16_t>&);
-
 
     // DELETE from here and below
     #include "codecs/sf_protocols.h"
@@ -173,10 +169,7 @@ bool PppEncap::decode(const uint8_t *raw_pkt, const uint32_t len,
     return true;
 }
 
-
-
-
-void PppEncap::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
 {
     v.push_back(ETHERTYPE_PPP);
 }
@@ -216,6 +209,8 @@ static const CodecApi pppencap_api =
     NULL, // tterm
     ctor, // ctor
     dtor, // dtor
+    nullptr, // get_dlt
+    get_protocol_ids,
     sum, // sum
     stats  // stats
 };
index b2a4dfea61c11478050d6eca78ba411d0298288a..80479a7ca37a2ae7c99124d2136cda6a457a494d 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
 /*
 ** Copyright (C) 2002-2013 Sourcefire, Inc.
 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
@@ -39,7 +37,6 @@ public:
 
     virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, 
         Packet *, uint16_t &p_hdr_len, int &next_prot_id);
-    virtual void get_protocol_ids(std::vector<uint16_t>&);
     
     // DELETE from here and below
     #include "codecs/sf_protocols.h"
@@ -286,7 +283,7 @@ EncStatus PPPoE_Encode (EncState* enc, Buffer* in, Buffer* out)
 }
 #endif
 
-void PPPoEPkt::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
 {
     v.push_back(ETHERNET_TYPE_PPPoE_DISC);
     v.push_back(ETHERNET_TYPE_PPPoE_SESS);
@@ -327,6 +324,8 @@ static const CodecApi pppoe_api =
     NULL, // tterm
     ctor, // ctor
     dtor, // dtor
+    nullptr,
+    get_protocol_ids,
     sum, // sum
     stats  // stats
 };
index f71820aa71dddbb2976145987708a3eb9e9cb90a..2d7c84f1e4109405ce59dcd5f07bee949cab54f0 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
 /*
 ** Copyright (C) 2002-2013 Sourcefire, Inc.
 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
@@ -46,8 +44,6 @@ public:
 
     virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, 
         Packet *, uint16_t &p_hdr_len, int &next_prot_id);
-
-    virtual void get_protocol_ids(std::vector<uint16_t>&);
     
 };
 
@@ -97,7 +93,7 @@ bool TransbridgeCodec::decode(const uint8_t *raw_pkt, const uint32_t len,
 
 
 
-void TransbridgeCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
 {
     v.push_back(ETHERTYPE_TRANS_ETHER_BRIDGING); // defined in ethertypes.h"
 }
@@ -137,6 +133,8 @@ static const CodecApi transbridge_api =
     NULL, // tterm
     ctor, // ctor
     dtor, // dtor
+    nullptr,
+    get_protocol_ids,
     sum, // sum
     stats  // stats
 };
index 5555183921924f9ad37186735f96be9dd32cf3e6..c9cebfae380af76225d928ec7b6e0b3236ce28c1 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
 /*
 ** Copyright (C) 2002-2013 Sourcefire, Inc.
 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
@@ -54,9 +52,6 @@ public:
     virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, 
         Packet *, uint16_t &p_hdr_len, int &next_prot_id);
 
-    virtual void get_protocol_ids(std::vector<uint16_t>&);
-    virtual void get_data_link_type(std::vector<int>&){};
-
     
     // DELETE from here and below
     #include "codecs/sf_protocols.h"
@@ -176,7 +171,7 @@ void VLAN_Format (EncodeFlags, const Packet*, Packet* c, Layer* lyr)
 
 
 
-void VlanCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
 {
     v.push_back(ETHERNET_TYPE_8021Q);
 }
@@ -216,6 +211,8 @@ static const CodecApi vlan_api =
     NULL, // tterm
     ctor, // ctor
     dtor, // dtor
+    NULL, // get_dlt
+    get_protocol_ids,
     sum, // sum
     stats  // stats
 };
index 8abecb9f98e24741efbce2c696ce68bb3de6cded..0ce33b686a6de78c307b97f755488638567f5ddb 100644 (file)
@@ -55,8 +55,6 @@ public:
 
     // do nothing unless methods overridden.
     // ONE OF THESE METHODS MUST BE IMPLEMENTED!!
-    virtual void get_protocol_ids(std::vector<uint16_t>&){};
-    virtual void get_data_link_type(std::vector<int>&){};
     virtual inline const char* get_name(){return name; };
 
 
@@ -80,10 +78,9 @@ typedef Codec* (*cd_new_f)();
 
 typedef void (*cd_del_f)(Codec *);
 typedef void (*cd_aux_f)();
-typedef void (*cd_get_protos)(std::vector<uint16_t>&);
-typedef void (*cd_get_dlt)(std::vector<int>&);
 typedef bool (*decode_f)(const uint8_t *, const uint32_t, Packet *, uint16_t &, uint16_t &);
-
+typedef void (*cd_dlt_f)(std::vector<int>&v);
+typedef void (*cd_prot_id_f)(std::vector<uint16_t>&);
 
     // add every protocol id, included IP protocols and 
     // ethertypes, to the passed in vector
@@ -107,6 +104,9 @@ struct CodecApi
     cd_new_f ctor;   // get eval optional instance data
     cd_del_f dtor;   // clean up instance data
 
+    cd_dlt_f dlt;   // get the data link type
+    cd_prot_id_f proto_id;  // get the protocol ids
+
     cd_aux_f sum;
     cd_aux_f stats;
 };
index 8a9e7091a10f54e46d13d42137ded3e80f7df680..caf88ae2c8f9e87fe75a6911a607cac2a2fa2f8d 100644 (file)
@@ -1,5 +1,24 @@
+/*
+** Copyright (C) 2013-2013 Sourcefire, Inc.
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License Version 2 as
+** published by the Free Software Foundation.  You may not use, modify or
+** distribute this program under any other version of the GNU General
+** Public License.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
 
 #include <list>
+#include <vector>
 using namespace std;
 
 #include "packet_manager.h"
@@ -11,33 +30,37 @@ using namespace std;
 
 #include "protocols/packet.h"
 #include "protocols/undefined_protocols.h"
+#include "time/profiler.h"
 
-#if 0
-#include "codecs/decode.h"
-#include "codecs/essential/root_eth.h"
-#include "codecs/root/root_raw4.h"
-#include "codecs/root/root_raw6.h"
-#include "codecs/root/root_null.h"
-#endif
 
-#include "time/profiler.h"
+namespace
+{
+struct CdGenPegs{
+    PegCount total_processed = 0;
+    PegCount other_codecs = 0;
+    PegCount discards = 0;
+};
+
+std::vector<const char*> CdGenPegNames =
+{
+    "total",
+    "other",
+    "discards"
+};
 
+} // anonymous namespace
 
-//static list<const CodecApi*> s_codecs;
-//static THREAD_LOCAL decode_f grinder;
 
 #ifdef PERF_PROFILING
 THREAD_LOCAL PreprocStats decodePerfStats;
 #endif
 
+static const uint16_t max_protocol_id = 65535;
+static std::array<Codec*, max_protocol_id> s_protocols;
+static list<const CodecApi*> s_codecs;
+static THREAD_LOCAL CdGenPegs pkt_cnt;
+static CdGenPegs gpkt_cnt;
 
-//namespace
-//{
-    static const uint16_t max_protocol_id = 65535;
-    static std::array<Codec*, max_protocol_id> s_protocols;
-    static list<const CodecApi*> s_codecs;
-
-//} // namespace
 
 //-------------------------------------------------------------------------
 // plugins
@@ -67,6 +90,12 @@ void PacketManager::add_plugin(const CodecApi* api)
 
 void PacketManager::release_plugins()
 {
+    for ( auto* p : s_codecs )
+    {
+        p->gterm();
+        p->tterm();
+//        p->dtor();
+    }
     s_codecs.clear();
 }
 
@@ -97,72 +126,33 @@ void PacketManager::decode(
     p->pkt = pkt;
     len = pkthdr->caplen;
     curr_prot_id = GRINDER_ID;
+    pkt_cnt.total_processed++;
 
-    // The boolean check in this order so 
-    while(curr_prot_id  >= 0 && 
-            curr_prot_id < max_protocol_id &&
-            s_protocols[curr_prot_id] != 0 &&
-            s_protocols[curr_prot_id]->decode(pkt, len, p, p_hdr_len, next_prot_id))
+    // loop until the protocol id is no longer valid
+    while(curr_prot_id  >= 0 && curr_prot_id < max_protocol_id)
     {
+        if (s_protocols[curr_prot_id] == 0)
+        {
+            pkt_cnt.other_codecs++;
+            break;
+        }
+        else if( !s_protocols[curr_prot_id]->decode(pkt, len, p, p_hdr_len, next_prot_id))
+        {
+            pkt_cnt.discards++;
+            break;
+        }           
 
-
-        // if we have succesfully decoded this layer, push the layer
         PacketClass::PushLayer(p, s_protocols[curr_prot_id], pkt, p_hdr_len);
         curr_prot_id = next_prot_id;
         len -= p_hdr_len;
         pkt += p_hdr_len;
-
     }
 
     p->dsize = len;
     p->data = pkt;
-
     PREPROC_PROFILE_END(decodePerfStats);
 }
 
-#if 0
-const CodecApi *PacketManager::get_data_link_type(int dlt)
-{
-    vector<int> dlt_vec;
-
-    for ( auto* p : s_codecs )
-    {
-            dlt_vec.clear();
-//            p->get_dlt(dlt_vec);
-
-            for (auto *it = dlt_vec.begin(); it != dlt_vec.end(); ++it)
-            {
-                if (*it == dlt)
-                    return p;
-            }
-    }
-
-    return nullptr;
-}
-
-void PacketManager::set_grinder(void)
-{
-    const char* slink = NULL;
-    const char* extra = NULL;
-
-    // initialize values
-
-    int dlt = DAQ_GetBaseProtocol();
-    const CodecApi *cd_api = get_data_link_type(dlt);
-
-    if(cd_api != nullptr)
-    {
-        grinder = cd_api->ctor();
-
-        if ( !ScReadMode() || ScPcapShow() )
-            LogMessage("Decoding %s\n", slink);        
-    }
-
-
-    FatalError("%s(%d) Could not find codec for Data Link Type %d.\n",
-                     __FILE__, __LINE__, dlt);
-}
-#endif
 
 void PacketManager::set_grinder(void)
 {
@@ -188,7 +178,7 @@ void PacketManager::set_grinder(void)
 
 
         proto.clear();
-        cd->get_protocol_ids(proto);
+        p->proto_id(proto);
         for (auto proto_id : proto)
         {
             if(s_protocols[proto_id] != NULL)
@@ -203,7 +193,7 @@ void PacketManager::set_grinder(void)
 
 
         dlt.clear();
-        cd->get_data_link_type(dlt);
+        p->dlt(dlt);
         // set the grinder if the data link types match
         for (auto curr_dlt : dlt )
         {
@@ -222,21 +212,55 @@ void PacketManager::set_grinder(void)
 
         if (!codec_registered)
             WarningMessage("The Codec %s is never used\n", cd->get_name());
-        // ERRRO:  If multiple correct grinders found.
     }
-
-
-
-//        FatalError("Codec installation checking!!");
 }
 
 
 void PacketManager::dump_stats()
 {
-//    for ( auto* cd : s_codecs )
-//        cd->sum();
+    sum_stats((PegCount*)&gpkt_cnt, (PegCount*)&pkt_cnt, array_size(CdGenPegNames));
+
+    for ( auto* cd : s_codecs )
+        if (cd->sum != nullptr)
+            cd->sum();
+
+    std::vector<const char*> pegNames(CdGenPegNames);
+    std::vector<PegCount> pegs;
+    pegs.push_back(gpkt_cnt.total_processed);
+    pegs.push_back(gpkt_cnt.other_codecs);
+    pegs.push_back(gpkt_cnt.discards);
+
+    // using two temporary vectors to ensure codecs cannot
+    // see any other codecs statistics
+    std::vector<const char*> tmpNames;
+    std::vector<PegCount> tmpPegs;
+
+    for ( auto* cd : s_codecs )
+    {
+        if (cd->stats != nullptr)
+        {
+            tmpPegs.clear();
+            tmpNames.clear();
+//            cd->stats(tmpPegs, tmpNames);
+            if (tmpNames.size() == tmpPegs.size())
+            {
+                pegs.insert(pegs.end(), tmpPegs.begin(), tmpPegs.end());
+                pegNames.insert(pegNames.end(), tmpNames.begin(), tmpNames.end());
+            } 
+            else
+            {
+                WarningMessage("The %s Codecs stats function returned a "
+                    "different %d PegCounts and %d PegNames.  the two "
+                    "values must be equal\n",
+                    cd->base.name, pegs.size(), pegNames.size());
+            }
+        }
+    }
+    show_percent_stats(&pegs[0], &pegNames[0], pegNames.size(),
+        "codecs");
 }
 
+
 bool PacketManager::has_codec(uint16_t cd_id)
 {
     return s_protocols[cd_id] != 0;
index bc3668ffa07c2f8965c0867e0649310ac043eeec..8a4626f1df438121cd2d3ab8ea68c00ccae94dfc 100644 (file)
@@ -23,6 +23,7 @@
 #include "snort_types.h"
 #include "framework/codec.h"
 #include "time/profiler.h"
+#include "utils/stats.h"
 
 #include <array>
 #include <list>
@@ -50,15 +51,12 @@ public:
     static void init_codecs();
     static void dump_stats();
 
-
     static bool has_codec(uint16_t);
 
 //    static void encode_update(Packet *);
 //    static void encode_format(Packet *);
 
 private:
-    static const CodecApi *get_data_link_type(int dlt);
-
 
 };