-
Notifications
You must be signed in to change notification settings - Fork 16
/
npfring.c
149 lines (123 loc) · 3.04 KB
/
npfring.c
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
#include <pfring.h>
#include <sched.h>
#include "npfring.h"
#include "utils.h"
void npfring_print_version(FILE * const out)
{
uint32_t pfring_version;
pfring_version_noring(&pfring_version);
fprintf(out,
"PF_RING version: %d.%d.%d\n",
(pfring_version & 0xFFFF0000) >> 16,
(pfring_version & 0x0000FF00) >> 8,
(pfring_version & 0x000000FF));
}
int npfring_init(char const * device_name, uint32_t caplen, struct npfring * result)
{
pfring * pd = pfring_open(device_name, caplen, PF_RING_REENTRANT | PF_RING_PROMISC);
if (pd == NULL)
{
return -1;
}
pfring_set_application_name(pd, "nDPId");
logger_early(0, "PF_RING RX channels: %d", pfring_get_num_rx_channels(pd));
result->pfring_desc = pd;
int rc;
if ((rc = pfring_set_socket_mode(pd, recv_only_mode)) != 0)
{
logger_early(1, "pfring_set_sock_moode returned: %d", rc);
return -1;
}
return 0;
}
void npfring_close(struct npfring * npf)
{
if (npf->pfring_desc != NULL)
{
pfring_close(npf->pfring_desc);
npf->pfring_desc = NULL;
}
}
int npfring_set_bpf(struct npfring * npf, char const * bpf_filter)
{
char buf[BUFSIZ];
if (npf->pfring_desc == NULL)
{
return -1;
}
// pfring_set_bpf_filter expects a char*
snprintf(buf, sizeof(buf), "%s", bpf_filter);
return pfring_set_bpf_filter(npf->pfring_desc, buf);
}
int npfring_datalink(struct npfring * npf)
{
if (npf->pfring_desc != NULL)
{
return pfring_get_link_type(npf->pfring_desc);
}
return -1;
}
int npfring_enable(struct npfring * npf)
{
if (npf->pfring_desc == NULL)
{
return -1;
}
return pfring_enable_ring(npf->pfring_desc);
}
int npfring_get_selectable_fd(struct npfring * npf)
{
if (npf->pfring_desc == NULL)
{
return -1;
}
return pfring_get_selectable_fd(npf->pfring_desc);
}
int npfring_recv(struct npfring * npf, struct pcap_pkthdr * pcap_hdr)
{
int rc;
if (npf->pfring_desc == NULL || pcap_hdr == NULL)
{
return -1;
}
unsigned char * buf = &npf->pfring_buffer[0];
struct pfring_pkthdr pfring_pkthdr;
rc = pfring_recv(npf->pfring_desc, &buf, PFRING_BUFFER_SIZE, &pfring_pkthdr, 0);
if (rc > 0)
{
pcap_hdr->ts = pfring_pkthdr.ts;
pcap_hdr->caplen = pfring_pkthdr.caplen;
pcap_hdr->len = pfring_pkthdr.len;
}
else
{
pcap_hdr->ts.tv_sec = 0;
pcap_hdr->ts.tv_usec = 0;
pcap_hdr->caplen = 0;
pcap_hdr->len = 0;
}
return rc;
}
int npfring_stats(struct npfring * npf, struct npfring_stats * stats)
{
int rc;
if (npf->pfring_desc == NULL)
{
return -1;
}
pfring_stat pstats;
rc = pfring_stats(npf->pfring_desc, &pstats);
if (rc == 0)
{
stats->recv = pstats.recv;
stats->drop = pstats.drop;
stats->shunt = pstats.shunt;
}
else
{
stats->drop = 0;
stats->recv = 0;
stats->shunt = 0;
}
return rc;
}