forked from CESNET/ipfixprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndp.cpp
174 lines (153 loc) · 4.9 KB
/
ndp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* \file ndp.cpp
* \brief Packet reader using NDP library for high speed capture.
* \author Tomas Benes <[email protected]>
* \author Jiri Havranek <[email protected]>
* \date 2021
*/
/*
* Copyright (C) 2020-2021 CESNET
*
* LICENSE TERMS
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Company nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* ALTERNATIVELY, provided that this notice is retained in full, this
* product may be distributed under the terms of the GNU General Public
* License (GPL) version 2 or later, in which case the provisions
* of the GPL apply INSTEAD OF those given above.
*
* This software is provided ``as is'', and any express or implied
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose are disclaimed.
* In no event shall the company or contributors be liable for any
* direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute
* goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether
* in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even
* if advised of the possibility of such damage.
*
*/
#include <config.h>
#ifdef HAVE_NDP
#include <cstdio>
#include <cstring>
#include <iostream>
#include "ndp.h"
#include "parser.h"
void packet_ndp_handler(parser_opt_t *opt, const struct ndp_packet *ndp_packet, const struct ndp_header *ndp_header)
{
struct timeval ts;
ts.tv_sec = ndp_header->timestamp_sec;
ts.tv_usec = ndp_header->timestamp_nsec / 1000;
parse_packet(opt, ts, ndp_packet->data, ndp_packet->data_length, ndp_packet->data_length);
}
NdpPacketReader::NdpPacketReader() : print_pcap_stats(false)
{
processed = 0;
parsed = 0;
}
NdpPacketReader::NdpPacketReader(const options_t &options)
{
processed = 0;
parsed = 0;
print_pcap_stats = options.print_pcap_stats;
}
NdpPacketReader::~NdpPacketReader()
{
this->close();
}
/**
* \brief Open pcap file for reading.
* \param [in] file Input file name.
* \param [in] parse_every_pkt Try to parse every captured packet.
* \return 0 on success, non 0 on failure + error_msg is filled with error message
*/
int NdpPacketReader::open_file(const string &file, bool parse_every_pkt)
{
error_msg = "Pcap Not suported in this mode";
return 1;
}
/**
* \brief Initialize network interface for reading.
* \param [in] interface Interface name.
* \param [in] snaplen Snapshot length to be set on pcap handle.
* \param [in] parse_every_pkt Try to parse every captured packet.
* \return 0 on success, non 0 on failure + error_msg is filled with error message
*/
int NdpPacketReader::init_interface(const string &interface, int snaplen, bool parse_every_pkt)
{
int res;
res = ndpReader.init_interface(interface);
error_msg = ndpReader.error_msg;
parse_all = parse_every_pkt;
return res;
}
/**
* \brief Install BPF filter to pcap handle.
* \param [in] filter_str String containing program.
* \return 0 on success, non 0 on failure.
*/
int NdpPacketReader::set_filter(const string &filter_str)
{
error_msg = "Filters not supported";
return 1;
}
void NdpPacketReader::printStats()
{
ndpReader.print_stats();
}
/**
* \brief Close opened file or interface.
*/
void NdpPacketReader::close()
{
ndpReader.close();
}
int NdpPacketReader::get_pkt(PacketBlock &packets)
{
int ret = -1;
if (print_pcap_stats) {
//print_stats();
}
struct ndp_packet *ndp_packet;
struct ndp_header *ndp_header;
parser_opt_t opt = {&packets, false, parse_all, 0};
size_t read_pkts = 0;
for (unsigned i = 0; i < packets.size; i++) {
ret = ndpReader.get_pkt(&ndp_packet, &ndp_header);
if (ret == 0) {
if (opt.pkts->cnt) {
break;
}
return 3;
} else if (ret < 0) {
// Error occured.
error_msg = ndpReader.error_msg;
return ret;
}
read_pkts++;
packet_ndp_handler(&opt, ndp_packet, ndp_header);
}
processed += read_pkts;
parsed += opt.pkts->cnt;
if (opt.pkts->cnt) {
// Packets are valid and ready to be process by flow_cache.
return 2;
}
return 1;
}
#endif /* HAVE_NDP */