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. |
26 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
27 |
#include <netdb.h> |
#include <netdb.h> |
28 |
#include <pthread.h> |
#include <pthread.h> |
29 |
|
#include <pcap.h> |
30 |
|
|
31 |
#include "registry.h" |
#include "registry.h" |
32 |
#include "net.h" |
#include "net.h" |
70 |
if (direction == NETIO_FILTER_DIR_RX) { |
if (direction == NETIO_FILTER_DIR_RX) { |
71 |
nio->rx_filter_data = NULL; |
nio->rx_filter_data = NULL; |
72 |
nio->rx_filter = pf; |
nio->rx_filter = pf; |
73 |
} else { |
} else if (direction == NETIO_FILTER_DIR_TX) { |
74 |
nio->tx_filter_data = NULL; |
nio->tx_filter_data = NULL; |
75 |
nio->tx_filter = pf; |
nio->tx_filter = pf; |
76 |
|
} else { |
77 |
|
nio->both_filter_data = NULL; |
78 |
|
nio->both_filter = pf; |
79 |
} |
} |
|
|
|
80 |
return(0); |
return(0); |
81 |
} |
} |
82 |
|
|
89 |
if (direction == NETIO_FILTER_DIR_RX) { |
if (direction == NETIO_FILTER_DIR_RX) { |
90 |
opt = &nio->rx_filter_data; |
opt = &nio->rx_filter_data; |
91 |
pf = nio->rx_filter; |
pf = nio->rx_filter; |
92 |
} else { |
} else if (direction == NETIO_FILTER_DIR_TX) { |
93 |
opt = &nio->tx_filter_data; |
opt = &nio->tx_filter_data; |
94 |
pf = nio->tx_filter; |
pf = nio->tx_filter; |
95 |
|
} else { |
96 |
|
opt = &nio->both_filter_data; |
97 |
|
pf = nio->both_filter; |
98 |
} |
} |
99 |
|
|
100 |
if (!pf) |
if (!pf) |
113 |
if (direction == NETIO_FILTER_DIR_RX) { |
if (direction == NETIO_FILTER_DIR_RX) { |
114 |
opt = &nio->rx_filter_data; |
opt = &nio->rx_filter_data; |
115 |
pf = nio->rx_filter; |
pf = nio->rx_filter; |
116 |
} else { |
} else if (direction == NETIO_FILTER_DIR_TX) { |
117 |
opt = &nio->tx_filter_data; |
opt = &nio->tx_filter_data; |
118 |
pf = nio->tx_filter; |
pf = nio->tx_filter; |
119 |
|
} else { |
120 |
|
opt = &nio->both_filter_data; |
121 |
|
pf = nio->both_filter; |
122 |
} |
} |
123 |
|
|
124 |
if (!pf) |
if (!pf) |
128 |
} |
} |
129 |
|
|
130 |
/* ======================================================================== */ |
/* ======================================================================== */ |
131 |
|
/* Packet Capture ("capture") */ |
132 |
|
/* GFA */ |
133 |
|
/* ======================================================================== */ |
134 |
|
|
135 |
|
/* Free resources used by filter */ |
136 |
|
static void pf_capture_free(netio_desc_t *nio,void **opt) |
137 |
|
{ |
138 |
|
struct netio_filter_capture *c = *opt; |
139 |
|
|
140 |
|
if (c != NULL) { |
141 |
|
printf("NIO %s: ending packet capture.\n",nio->name); |
142 |
|
|
143 |
|
/* Close dumper */ |
144 |
|
if (c->dumper) |
145 |
|
pcap_dump_close(c->dumper); |
146 |
|
|
147 |
|
/* Close PCAP descriptor */ |
148 |
|
if (c->desc) |
149 |
|
pcap_close(c->desc); |
150 |
|
|
151 |
|
free(c); |
152 |
|
*opt = NULL; |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
|
/* Setup filter resources */ |
157 |
|
static int pf_capture_setup(netio_desc_t *nio,void **opt, |
158 |
|
int argc,char *argv[]) |
159 |
|
{ |
160 |
|
struct netio_filter_capture *c; |
161 |
|
int link_type; |
162 |
|
|
163 |
|
/* We must have a link type and a filename */ |
164 |
|
if (argc != 2) |
165 |
|
return(-1); |
166 |
|
|
167 |
|
/* Free resources if something has already been done */ |
168 |
|
pf_capture_free(nio,opt); |
169 |
|
|
170 |
|
/* Allocate structure to hold PCAP info */ |
171 |
|
if (!(c = malloc(sizeof(*c)))) |
172 |
|
return(-1); |
173 |
|
|
174 |
|
if ((link_type = pcap_datalink_name_to_val(argv[0])) == -1) { |
175 |
|
fprintf(stderr,"NIO %s: unknown link type %s, assuming Ethernet.\n", |
176 |
|
nio->name,argv[0]); |
177 |
|
link_type = DLT_EN10MB; |
178 |
|
} |
179 |
|
|
180 |
|
/* Open a dead pcap descriptor */ |
181 |
|
if (!(c->desc = pcap_open_dead(link_type,8192))) { |
182 |
|
fprintf(stderr,"NIO %s: pcap_open_dead failure\n",nio->name); |
183 |
|
goto pcap_open_err; |
184 |
|
} |
185 |
|
|
186 |
|
/* Open the output file */ |
187 |
|
if (!(c->dumper = pcap_dump_open(c->desc,argv[1]))) { |
188 |
|
fprintf(stderr,"NIO %s: pcap_dump_open failure (file %s)\n", |
189 |
|
nio->name,argv[0]); |
190 |
|
goto pcap_dump_err; |
191 |
|
} |
192 |
|
|
193 |
|
printf("NIO %s: capturing to file '%s'\n",nio->name,argv[1]); |
194 |
|
*opt = c; |
195 |
|
return(0); |
196 |
|
|
197 |
|
pcap_dump_err: |
198 |
|
pcap_close(c->desc); |
199 |
|
pcap_open_err: |
200 |
|
free(c); |
201 |
|
return(-1); |
202 |
|
} |
203 |
|
|
204 |
|
/* Packet handler: write packets to a file in CAP format */ |
205 |
|
static int pf_capture_pkt_handler(netio_desc_t *nio,void *pkt,size_t len, |
206 |
|
void *opt) |
207 |
|
{ |
208 |
|
struct netio_filter_capture *c = opt; |
209 |
|
struct pcap_pkthdr pkt_hdr; |
210 |
|
|
211 |
|
if (c != NULL) { |
212 |
|
gettimeofday(&pkt_hdr.ts,0); |
213 |
|
pkt_hdr.caplen = len; |
214 |
|
pkt_hdr.len = len; |
215 |
|
|
216 |
|
pcap_dump((u_char *)c->dumper,&pkt_hdr,pkt); |
217 |
|
pcap_dump_flush(c->dumper); |
218 |
|
} |
219 |
|
|
220 |
|
return(NETIO_FILTER_ACTION_PASS); |
221 |
|
} |
222 |
|
|
223 |
|
/* Packet capture */ |
224 |
|
static netio_pktfilter_t pf_capture_def = { |
225 |
|
"capture", |
226 |
|
pf_capture_setup, |
227 |
|
pf_capture_free, |
228 |
|
pf_capture_pkt_handler, |
229 |
|
NULL, |
230 |
|
}; |
231 |
|
|
232 |
|
/* ======================================================================== */ |
233 |
/* Frequency Dropping ("freq_drop"). */ |
/* Frequency Dropping ("freq_drop"). */ |
234 |
/* ======================================================================== */ |
/* ======================================================================== */ |
235 |
|
|
309 |
void netio_filter_load_all(void) |
void netio_filter_load_all(void) |
310 |
{ |
{ |
311 |
netio_filter_add(&pf_freqdrop_def); |
netio_filter_add(&pf_freqdrop_def); |
312 |
|
netio_filter_add(&pf_capture_def); |
313 |
} |
} |