Skip to content

Commit 6697a42

Browse files
committed
Implement ICMP plugin
1 parent 86e7af1 commit 6697a42

File tree

2 files changed

+32
-44
lines changed

2 files changed

+32
-44
lines changed

process/icmp.cpp

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545

4646
#include "icmp.hpp"
4747

48+
#include "../input/headers.hpp"
49+
4850
namespace ipxp {
4951

5052
int RecordExtICMP::REGISTERED_ID = -1;
@@ -56,49 +58,27 @@ __attribute__((constructor)) static void register_this_plugin()
5658
RecordExtICMP::REGISTERED_ID = register_extension();
5759
}
5860

59-
ICMPPlugin::ICMPPlugin()
60-
{
61-
}
62-
63-
ICMPPlugin::~ICMPPlugin()
64-
{
65-
}
66-
67-
void ICMPPlugin::init(const char *params)
68-
{
69-
}
70-
71-
void ICMPPlugin::close()
72-
{
73-
}
74-
7561
ProcessPlugin *ICMPPlugin::copy()
7662
{
7763
return new ICMPPlugin(*this);
7864
}
7965

80-
int ICMPPlugin::pre_create(Packet &pkt)
81-
{
82-
return 0;
83-
}
84-
8566
int ICMPPlugin::post_create(Flow &rec, const Packet &pkt)
8667
{
87-
return 0;
88-
}
68+
if (pkt.ip_proto == IPPROTO_ICMP ||
69+
pkt.ip_proto == IPPROTO_ICMPV6) {
70+
if (pkt.payload_len < 2)
71+
return 0;
8972

90-
int ICMPPlugin::pre_update(Flow &rec, Packet &pkt)
91-
{
92-
return 0;
93-
}
73+
auto ext = new RecordExtICMP();
9474

95-
int ICMPPlugin::post_update(Flow &rec, const Packet &pkt)
96-
{
97-
return 0;
98-
}
75+
// the type and code are the first two bytes, type on MSB and code on LSB
76+
// in the network byte order
77+
ext->type_code = *reinterpret_cast<const uint16_t *>(pkt.payload);
9978

100-
void ICMPPlugin::pre_export(Flow &rec)
101-
{
79+
rec.add_extension(ext);
80+
}
81+
return 0;
10282
}
10383

10484
}

process/icmp.hpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,19 @@
5050
#include "fields.h"
5151
#endif
5252

53+
#include <sstream>
54+
5355
#include <ipfixprobe/process.hpp>
5456
#include <ipfixprobe/flowifc.hpp>
5557
#include <ipfixprobe/packet.hpp>
5658
#include <ipfixprobe/ipfix-elements.hpp>
5759

5860
namespace ipxp {
5961

60-
#define ICMP_UNIREC_TEMPLATE "ICMP_TYPE,ICMP_CODE"
62+
#define ICMP_UNIREC_TEMPLATE "L4_ICMP_TYPE_CODE"
6163

6264
UR_FIELDS (
63-
uint8 ICMP_TYPE,
64-
uint8 ICMP_CODE
65+
uint16 L4_ICMP_TYPE_CODE
6566
)
6667

6768
/**
@@ -70,8 +71,11 @@ UR_FIELDS (
7071
struct RecordExtICMP : public RecordExt {
7172
static int REGISTERED_ID;
7273

74+
uint16_t type_code;
75+
7376
RecordExtICMP() : RecordExt(REGISTERED_ID)
7477
{
78+
type_code = 0;
7579
}
7680

7781
#ifdef WITH_NEMEA
@@ -98,6 +102,18 @@ struct RecordExtICMP : public RecordExt {
98102
};
99103
return ipfix_template;
100104
}
105+
106+
std::string get_text() const
107+
{
108+
// type is on the first byte, code is on the second byte
109+
auto *type_code = reinterpret_cast<const uint8_t *>(&this->type_code);
110+
111+
std::ostringstream out;
112+
out << "type=\"" << (int)type_code[0] << '"'
113+
<< ",code=\"" << (int)type_code[1] << '"';
114+
115+
return out.str();
116+
}
101117
};
102118

103119
/**
@@ -106,20 +122,12 @@ struct RecordExtICMP : public RecordExt {
106122
class ICMPPlugin : public ProcessPlugin
107123
{
108124
public:
109-
ICMPPlugin();
110-
~ICMPPlugin();
111-
void init(const char *params);
112-
void close();
113125
OptionsParser *get_parser() const { return new OptionsParser("icmp", "Parse ICMP traffic"); }
114126
std::string get_name() const { return "icmp"; }
115127
RecordExt *get_ext() const { return new RecordExtICMP(); }
116128
ProcessPlugin *copy();
117129

118-
int pre_create(Packet &pkt);
119130
int post_create(Flow &rec, const Packet &pkt);
120-
int pre_update(Flow &rec, Packet &pkt);
121-
int post_update(Flow &rec, const Packet &pkt);
122-
void pre_export(Flow &rec);
123131
};
124132

125133
}

0 commit comments

Comments
 (0)