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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/*
* Copyright (c) 2018-2024 OARC, Inc.
* All rights reserved.
*
* This file is part of dnsjit.
*
* dnsjit is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dnsjit 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 dnsjit. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "input/pcap.h"
#include "core/assert.h"
#include "core/object/pcap.h"
static core_log_t _log = LOG_T_INIT("input.pcap");
static input_pcap_t _defaults = {
LOG_T_INIT_OBJ("input.pcap"),
0, 0,
0,
CORE_OBJECT_PCAP_INIT(0),
0, 0,
0, 0
};
core_log_t* input_pcap_log()
{
return &_log;
}
void input_pcap_init(input_pcap_t* self)
{
mlassert_self();
*self = _defaults;
}
void input_pcap_destroy(input_pcap_t* self)
{
mlassert_self();
if (self->pcap) {
pcap_close(self->pcap);
}
}
int input_pcap_create(input_pcap_t* self, const char* source)
{
char errbuf[PCAP_ERRBUF_SIZE] = { 0 };
mlassert_self();
lassert(source, "source is nil");
if (self->pcap) {
lfatal("already opened");
}
if (!(self->pcap = pcap_create(source, errbuf))) {
lcritical("pcap_create(%s) error: %s", source, errbuf);
return -1;
}
return 0;
}
int input_pcap_activate(input_pcap_t* self)
{
int ret;
mlassert_self();
if (!self->pcap) {
lfatal("no PCAP opened");
}
if (!(ret = pcap_activate(self->pcap))) {
self->snaplen = pcap_snapshot(self->pcap);
self->linktype = pcap_datalink(self->pcap);
self->is_swapped = 0;
self->prod_pkt.snaplen = self->snaplen;
self->prod_pkt.linktype = self->linktype;
self->prod_pkt.is_swapped = self->is_swapped;
ldebug("pcap v%u.%u snaplen:%lu %s", pcap_major_version(self->pcap), pcap_minor_version(self->pcap), self->snaplen, self->is_swapped ? " swapped" : "");
}
return ret;
}
int input_pcap_open_offline(input_pcap_t* self, const char* file)
{
char errbuf[PCAP_ERRBUF_SIZE] = { 0 };
mlassert_self();
lassert(file, "file is nil");
if (self->pcap) {
lfatal("already opened");
}
if (!(self->pcap = pcap_open_offline(file, errbuf))) {
lcritical("pcap_open_offline(%s) error: %s", file, errbuf);
return -1;
}
self->snaplen = pcap_snapshot(self->pcap);
self->linktype = pcap_datalink(self->pcap);
self->is_swapped = pcap_is_swapped(self->pcap);
self->prod_pkt.snaplen = self->snaplen;
self->prod_pkt.linktype = self->linktype;
self->prod_pkt.is_swapped = self->is_swapped;
ldebug("pcap v%u.%u snaplen:%lu %s", pcap_major_version(self->pcap), pcap_minor_version(self->pcap), self->snaplen, self->is_swapped ? " swapped" : "");
return 0;
}
static void _handler(u_char* user, const struct pcap_pkthdr* h, const u_char* bytes)
{
input_pcap_t* self = (input_pcap_t*)user;
core_object_pcap_t pkt = CORE_OBJECT_PCAP_INIT(0);
mlassert_self();
lassert(h, "pcap_pkthdr is nil");
lassert(bytes, "bytes is nil");
self->pkts++;
pkt.snaplen = self->snaplen;
pkt.linktype = self->linktype;
pkt.ts.sec = h->ts.tv_sec;
pkt.ts.nsec = h->ts.tv_usec * 1000;
pkt.caplen = h->caplen;
pkt.len = h->len;
pkt.bytes = bytes;
pkt.is_swapped = self->is_swapped;
self->recv(self->ctx, (core_object_t*)&pkt);
}
int input_pcap_loop(input_pcap_t* self, int cnt)
{
mlassert_self();
if (!self->pcap) {
lfatal("no PCAP opened");
}
if (!self->recv) {
lfatal("no receiver set");
}
return pcap_loop(self->pcap, cnt, _handler, (void*)self);
}
int input_pcap_dispatch(input_pcap_t* self, int cnt)
{
mlassert_self();
if (!self->pcap) {
lfatal("no PCAP opened");
}
if (!self->recv) {
lfatal("no receiver set");
}
return pcap_dispatch(self->pcap, cnt, _handler, (void*)self);
}
static const core_object_t* _produce(input_pcap_t* self)
{
struct pcap_pkthdr* h;
const u_char* bytes;
int ret = 0;
mlassert_self();
while (!(ret = pcap_next_ex(self->pcap, &h, &bytes)))
;
if (ret == 1 && h && bytes) {
self->prod_pkt.ts.sec = h->ts.tv_sec;
self->prod_pkt.ts.nsec = h->ts.tv_usec * 1000;
self->prod_pkt.caplen = h->caplen;
self->prod_pkt.len = h->len;
self->prod_pkt.bytes = bytes;
return (core_object_t*)&self->prod_pkt;
}
return 0;
}
core_producer_t input_pcap_producer(input_pcap_t* self)
{
mlassert_self();
if (!self->pcap) {
lfatal("no PCAP opened");
}
return (core_producer_t)_produce;
}
|