1 |
/* |
/* |
2 |
* Cisco 7200 (Predator) simulation platform. |
* Cisco router simulation platform. |
3 |
* Copyright (c) 2005,2006 Christophe Fillot (cf@utc.fr) |
* Copyright (c) 2005,2006 Christophe Fillot (cf@utc.fr) |
4 |
* |
* |
5 |
* NetIO Filtering. |
* NetIO Filtering. |
27 |
#include <netdb.h> |
#include <netdb.h> |
28 |
#include <pthread.h> |
#include <pthread.h> |
29 |
|
|
30 |
|
#ifdef GEN_ETH |
31 |
|
#include <pcap.h> |
32 |
|
#endif |
33 |
|
|
34 |
#include "registry.h" |
#include "registry.h" |
35 |
#include "net.h" |
#include "net.h" |
36 |
#include "net_io.h" |
#include "net_io.h" |
73 |
if (direction == NETIO_FILTER_DIR_RX) { |
if (direction == NETIO_FILTER_DIR_RX) { |
74 |
nio->rx_filter_data = NULL; |
nio->rx_filter_data = NULL; |
75 |
nio->rx_filter = pf; |
nio->rx_filter = pf; |
76 |
} else { |
} else if (direction == NETIO_FILTER_DIR_TX) { |
77 |
nio->tx_filter_data = NULL; |
nio->tx_filter_data = NULL; |
78 |
nio->tx_filter = pf; |
nio->tx_filter = pf; |
79 |
|
} else { |
80 |
|
nio->both_filter_data = NULL; |
81 |
|
nio->both_filter = pf; |
82 |
} |
} |
|
|
|
83 |
return(0); |
return(0); |
84 |
} |
} |
85 |
|
|
92 |
if (direction == NETIO_FILTER_DIR_RX) { |
if (direction == NETIO_FILTER_DIR_RX) { |
93 |
opt = &nio->rx_filter_data; |
opt = &nio->rx_filter_data; |
94 |
pf = nio->rx_filter; |
pf = nio->rx_filter; |
95 |
} else { |
} else if (direction == NETIO_FILTER_DIR_TX) { |
96 |
opt = &nio->tx_filter_data; |
opt = &nio->tx_filter_data; |
97 |
pf = nio->tx_filter; |
pf = nio->tx_filter; |
98 |
|
} else { |
99 |
|
opt = &nio->both_filter_data; |
100 |
|
pf = nio->both_filter; |
101 |
} |
} |
102 |
|
|
103 |
if (!pf) |
if (!pf) |
116 |
if (direction == NETIO_FILTER_DIR_RX) { |
if (direction == NETIO_FILTER_DIR_RX) { |
117 |
opt = &nio->rx_filter_data; |
opt = &nio->rx_filter_data; |
118 |
pf = nio->rx_filter; |
pf = nio->rx_filter; |
119 |
} else { |
} else if (direction == NETIO_FILTER_DIR_TX) { |
120 |
opt = &nio->tx_filter_data; |
opt = &nio->tx_filter_data; |
121 |
pf = nio->tx_filter; |
pf = nio->tx_filter; |
122 |
|
} else { |
123 |
|
opt = &nio->both_filter_data; |
124 |
|
pf = nio->both_filter; |
125 |
} |
} |
126 |
|
|
127 |
if (!pf) |
if (!pf) |
131 |
} |
} |
132 |
|
|
133 |
/* ======================================================================== */ |
/* ======================================================================== */ |
134 |
|
/* Packet Capture ("capture") */ |
135 |
|
/* GFA */ |
136 |
|
/* ======================================================================== */ |
137 |
|
#ifdef GEN_ETH |
138 |
|
|
139 |
|
/* Free resources used by filter */ |
140 |
|
static void pf_capture_free(netio_desc_t *nio,void **opt) |
141 |
|
{ |
142 |
|
struct netio_filter_capture *c = *opt; |
143 |
|
|
144 |
|
if (c != NULL) { |
145 |
|
printf("NIO %s: ending packet capture.\n",nio->name); |
146 |
|
|
147 |
|
/* Close dumper */ |
148 |
|
if (c->dumper) |
149 |
|
pcap_dump_close(c->dumper); |
150 |
|
|
151 |
|
/* Close PCAP descriptor */ |
152 |
|
if (c->desc) |
153 |
|
pcap_close(c->desc); |
154 |
|
|
155 |
|
free(c); |
156 |
|
*opt = NULL; |
157 |
|
} |
158 |
|
} |
159 |
|
|
160 |
|
/* Setup filter resources */ |
161 |
|
static int pf_capture_setup(netio_desc_t *nio,void **opt, |
162 |
|
int argc,char *argv[]) |
163 |
|
{ |
164 |
|
struct netio_filter_capture *c; |
165 |
|
int link_type; |
166 |
|
|
167 |
|
/* We must have a link type and a filename */ |
168 |
|
if (argc != 2) |
169 |
|
return(-1); |
170 |
|
|
171 |
|
/* Free resources if something has already been done */ |
172 |
|
pf_capture_free(nio,opt); |
173 |
|
|
174 |
|
/* Allocate structure to hold PCAP info */ |
175 |
|
if (!(c = malloc(sizeof(*c)))) |
176 |
|
return(-1); |
177 |
|
|
178 |
|
if ((link_type = pcap_datalink_name_to_val(argv[0])) == -1) { |
179 |
|
fprintf(stderr,"NIO %s: unknown link type %s, assuming Ethernet.\n", |
180 |
|
nio->name,argv[0]); |
181 |
|
link_type = DLT_EN10MB; |
182 |
|
} |
183 |
|
|
184 |
|
/* Open a dead pcap descriptor */ |
185 |
|
if (!(c->desc = pcap_open_dead(link_type,8192))) { |
186 |
|
fprintf(stderr,"NIO %s: pcap_open_dead failure\n",nio->name); |
187 |
|
goto pcap_open_err; |
188 |
|
} |
189 |
|
|
190 |
|
/* Open the output file */ |
191 |
|
if (!(c->dumper = pcap_dump_open(c->desc,argv[1]))) { |
192 |
|
fprintf(stderr,"NIO %s: pcap_dump_open failure (file %s)\n", |
193 |
|
nio->name,argv[0]); |
194 |
|
goto pcap_dump_err; |
195 |
|
} |
196 |
|
|
197 |
|
printf("NIO %s: capturing to file '%s'\n",nio->name,argv[1]); |
198 |
|
*opt = c; |
199 |
|
return(0); |
200 |
|
|
201 |
|
pcap_dump_err: |
202 |
|
pcap_close(c->desc); |
203 |
|
pcap_open_err: |
204 |
|
free(c); |
205 |
|
return(-1); |
206 |
|
} |
207 |
|
|
208 |
|
/* Packet handler: write packets to a file in CAP format */ |
209 |
|
static int pf_capture_pkt_handler(netio_desc_t *nio,void *pkt,size_t len, |
210 |
|
void *opt) |
211 |
|
{ |
212 |
|
struct netio_filter_capture *c = opt; |
213 |
|
struct pcap_pkthdr pkt_hdr; |
214 |
|
|
215 |
|
if (c != NULL) { |
216 |
|
gettimeofday(&pkt_hdr.ts,0); |
217 |
|
pkt_hdr.caplen = len; |
218 |
|
pkt_hdr.len = len; |
219 |
|
|
220 |
|
pcap_dump((u_char *)c->dumper,&pkt_hdr,pkt); |
221 |
|
pcap_dump_flush(c->dumper); |
222 |
|
} |
223 |
|
|
224 |
|
return(NETIO_FILTER_ACTION_PASS); |
225 |
|
} |
226 |
|
|
227 |
|
/* Packet capture */ |
228 |
|
static netio_pktfilter_t pf_capture_def = { |
229 |
|
"capture", |
230 |
|
pf_capture_setup, |
231 |
|
pf_capture_free, |
232 |
|
pf_capture_pkt_handler, |
233 |
|
NULL, |
234 |
|
}; |
235 |
|
|
236 |
|
#endif |
237 |
|
|
238 |
|
/* ======================================================================== */ |
239 |
/* Frequency Dropping ("freq_drop"). */ |
/* Frequency Dropping ("freq_drop"). */ |
240 |
/* ======================================================================== */ |
/* ======================================================================== */ |
241 |
|
|
315 |
void netio_filter_load_all(void) |
void netio_filter_load_all(void) |
316 |
{ |
{ |
317 |
netio_filter_add(&pf_freqdrop_def); |
netio_filter_add(&pf_freqdrop_def); |
318 |
|
#ifdef GEN_ETH |
319 |
|
netio_filter_add(&pf_capture_def); |
320 |
|
#endif |
321 |
} |
} |