/src/libpcap-1.9.1/pcap.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998 |
3 | | * The Regents of the University of California. All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions |
7 | | * are met: |
8 | | * 1. Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * 2. Redistributions in binary form must reproduce the above copyright |
11 | | * notice, this list of conditions and the following disclaimer in the |
12 | | * documentation and/or other materials provided with the distribution. |
13 | | * 3. All advertising materials mentioning features or use of this software |
14 | | * must display the following acknowledgement: |
15 | | * This product includes software developed by the Computer Systems |
16 | | * Engineering Group at Lawrence Berkeley Laboratory. |
17 | | * 4. Neither the name of the University nor of the Laboratory may be used |
18 | | * to endorse or promote products derived from this software without |
19 | | * specific prior written permission. |
20 | | * |
21 | | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
22 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
25 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
27 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
28 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
29 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
30 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
31 | | * SUCH DAMAGE. |
32 | | */ |
33 | | |
34 | | #ifdef HAVE_CONFIG_H |
35 | | #include <config.h> |
36 | | #endif |
37 | | |
38 | | #include <pcap-types.h> |
39 | | #ifndef _WIN32 |
40 | | #include <sys/param.h> |
41 | | #ifndef MSDOS |
42 | | #include <sys/file.h> |
43 | | #endif |
44 | | #include <sys/ioctl.h> |
45 | | #include <sys/socket.h> |
46 | | #ifdef HAVE_SYS_SOCKIO_H |
47 | | #include <sys/sockio.h> |
48 | | #endif |
49 | | |
50 | | struct mbuf; /* Squelch compiler warnings on some platforms for */ |
51 | | struct rtentry; /* declarations in <net/if.h> */ |
52 | | #include <net/if.h> |
53 | | #include <netinet/in.h> |
54 | | #endif /* _WIN32 */ |
55 | | |
56 | | #include <ctype.h> |
57 | | #include <stdio.h> |
58 | | #include <stdlib.h> |
59 | | #include <string.h> |
60 | | #if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__MINGW32__) |
61 | | #include <unistd.h> |
62 | | #endif |
63 | | #include <fcntl.h> |
64 | | #include <errno.h> |
65 | | #ifdef HAVE_LIMITS_H |
66 | | #include <limits.h> |
67 | | #else |
68 | | #define INT_MAX 2147483647 |
69 | | #endif |
70 | | |
71 | | #ifdef HAVE_OS_PROTO_H |
72 | | #include "os-proto.h" |
73 | | #endif |
74 | | |
75 | | #ifdef MSDOS |
76 | | #include "pcap-dos.h" |
77 | | #endif |
78 | | |
79 | | #include "pcap-int.h" |
80 | | |
81 | | #include "optimize.h" |
82 | | |
83 | | #ifdef HAVE_DAG_API |
84 | | #include "pcap-dag.h" |
85 | | #endif /* HAVE_DAG_API */ |
86 | | |
87 | | #ifdef HAVE_SEPTEL_API |
88 | | #include "pcap-septel.h" |
89 | | #endif /* HAVE_SEPTEL_API */ |
90 | | |
91 | | #ifdef HAVE_SNF_API |
92 | | #include "pcap-snf.h" |
93 | | #endif /* HAVE_SNF_API */ |
94 | | |
95 | | #ifdef HAVE_TC_API |
96 | | #include "pcap-tc.h" |
97 | | #endif /* HAVE_TC_API */ |
98 | | |
99 | | #ifdef PCAP_SUPPORT_USB |
100 | | #include "pcap-usb-linux.h" |
101 | | #endif |
102 | | |
103 | | #ifdef PCAP_SUPPORT_BT |
104 | | #include "pcap-bt-linux.h" |
105 | | #endif |
106 | | |
107 | | #ifdef PCAP_SUPPORT_BT_MONITOR |
108 | | #include "pcap-bt-monitor-linux.h" |
109 | | #endif |
110 | | |
111 | | #ifdef PCAP_SUPPORT_NETFILTER |
112 | | #include "pcap-netfilter-linux.h" |
113 | | #endif |
114 | | |
115 | | #ifdef PCAP_SUPPORT_NETMAP |
116 | | #include "pcap-netmap.h" |
117 | | #endif |
118 | | |
119 | | #ifdef PCAP_SUPPORT_DBUS |
120 | | #include "pcap-dbus.h" |
121 | | #endif |
122 | | |
123 | | #ifdef PCAP_SUPPORT_RDMASNIFF |
124 | | #include "pcap-rdmasniff.h" |
125 | | #endif |
126 | | |
127 | | #ifdef _WIN32 |
128 | | /* |
129 | | * DllMain(), required when built as a Windows DLL. |
130 | | */ |
131 | | BOOL WINAPI DllMain( |
132 | | HANDLE hinstDLL, |
133 | | DWORD dwReason, |
134 | | LPVOID lpvReserved |
135 | | ) |
136 | | { |
137 | | return (TRUE); |
138 | | } |
139 | | |
140 | | /* |
141 | | * Start WinSock. |
142 | | * Exported in case some applications using WinPcap/Npcap called it, |
143 | | * even though it wasn't exported. |
144 | | */ |
145 | | int |
146 | | wsockinit(void) |
147 | | { |
148 | | WORD wVersionRequested; |
149 | | WSADATA wsaData; |
150 | | static int err = -1; |
151 | | static int done = 0; |
152 | | |
153 | | if (done) |
154 | | return (err); |
155 | | |
156 | | wVersionRequested = MAKEWORD( 1, 1); |
157 | | err = WSAStartup( wVersionRequested, &wsaData ); |
158 | | atexit ((void(*)(void))WSACleanup); |
159 | | done = 1; |
160 | | |
161 | | if ( err != 0 ) |
162 | | err = -1; |
163 | | return (err); |
164 | | } |
165 | | |
166 | | /* |
167 | | * This is the exported function; new programs should call this. |
168 | | */ |
169 | | int |
170 | | pcap_wsockinit(void) |
171 | | { |
172 | | return (wsockinit()); |
173 | | } |
174 | | #endif /* _WIN32 */ |
175 | | |
176 | | /* |
177 | | * String containing the library version. |
178 | | * Not explicitly exported via a header file - the right API to use |
179 | | * is pcap_lib_version() - but some programs included it, so we |
180 | | * provide it. |
181 | | * |
182 | | * We declare it here, right before defining it, to squelch any |
183 | | * warnings we might get from compilers about the lack of a |
184 | | * declaration. |
185 | | */ |
186 | | PCAP_API char pcap_version[]; |
187 | | PCAP_API_DEF char pcap_version[] = PACKAGE_VERSION; |
188 | | |
189 | | static void |
190 | | pcap_set_not_initialized_message(pcap_t *pcap) |
191 | 0 | { |
192 | 0 | if (pcap->activated) { |
193 | | /* A module probably forgot to set the function pointer */ |
194 | 0 | (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf), |
195 | 0 | "This operation isn't properly handled by that device"); |
196 | 0 | return; |
197 | 0 | } |
198 | | /* in case the caller doesn't check for PCAP_ERROR_NOT_ACTIVATED */ |
199 | 0 | (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf), |
200 | 0 | "This handle hasn't been activated yet"); |
201 | 0 | } |
202 | | |
203 | | static int |
204 | | pcap_read_not_initialized(pcap_t *pcap, int cnt _U_, pcap_handler callback _U_, |
205 | | u_char *user _U_) |
206 | 0 | { |
207 | 0 | pcap_set_not_initialized_message(pcap); |
208 | | /* this means 'not initialized' */ |
209 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
210 | 0 | } |
211 | | |
212 | | static int |
213 | | pcap_inject_not_initialized(pcap_t *pcap, const void * buf _U_, size_t size _U_) |
214 | 0 | { |
215 | 0 | pcap_set_not_initialized_message(pcap); |
216 | | /* this means 'not initialized' */ |
217 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
218 | 0 | } |
219 | | |
220 | | static int |
221 | | pcap_setfilter_not_initialized(pcap_t *pcap, struct bpf_program *fp _U_) |
222 | 0 | { |
223 | 0 | pcap_set_not_initialized_message(pcap); |
224 | | /* this means 'not initialized' */ |
225 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
226 | 0 | } |
227 | | |
228 | | static int |
229 | | pcap_setdirection_not_initialized(pcap_t *pcap, pcap_direction_t d _U_) |
230 | 0 | { |
231 | 0 | pcap_set_not_initialized_message(pcap); |
232 | | /* this means 'not initialized' */ |
233 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
234 | 0 | } |
235 | | |
236 | | static int |
237 | | pcap_set_datalink_not_initialized(pcap_t *pcap, int dlt _U_) |
238 | 0 | { |
239 | 0 | pcap_set_not_initialized_message(pcap); |
240 | | /* this means 'not initialized' */ |
241 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
242 | 0 | } |
243 | | |
244 | | static int |
245 | | pcap_getnonblock_not_initialized(pcap_t *pcap) |
246 | 0 | { |
247 | 0 | pcap_set_not_initialized_message(pcap); |
248 | | /* this means 'not initialized' */ |
249 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
250 | 0 | } |
251 | | |
252 | | static int |
253 | | pcap_stats_not_initialized(pcap_t *pcap, struct pcap_stat *ps _U_) |
254 | 0 | { |
255 | 0 | pcap_set_not_initialized_message(pcap); |
256 | | /* this means 'not initialized' */ |
257 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
258 | 0 | } |
259 | | |
260 | | #ifdef _WIN32 |
261 | | struct pcap_stat * |
262 | | pcap_stats_ex_not_initialized(pcap_t *pcap, int *pcap_stat_size _U_) |
263 | | { |
264 | | pcap_set_not_initialized_message(pcap); |
265 | | return (NULL); |
266 | | } |
267 | | |
268 | | static int |
269 | | pcap_setbuff_not_initialized(pcap_t *pcap, int dim _U_) |
270 | | { |
271 | | pcap_set_not_initialized_message(pcap); |
272 | | /* this means 'not initialized' */ |
273 | | return (PCAP_ERROR_NOT_ACTIVATED); |
274 | | } |
275 | | |
276 | | static int |
277 | | pcap_setmode_not_initialized(pcap_t *pcap, int mode _U_) |
278 | | { |
279 | | pcap_set_not_initialized_message(pcap); |
280 | | /* this means 'not initialized' */ |
281 | | return (PCAP_ERROR_NOT_ACTIVATED); |
282 | | } |
283 | | |
284 | | static int |
285 | | pcap_setmintocopy_not_initialized(pcap_t *pcap, int size _U_) |
286 | | { |
287 | | pcap_set_not_initialized_message(pcap); |
288 | | /* this means 'not initialized' */ |
289 | | return (PCAP_ERROR_NOT_ACTIVATED); |
290 | | } |
291 | | |
292 | | static HANDLE |
293 | | pcap_getevent_not_initialized(pcap_t *pcap) |
294 | | { |
295 | | pcap_set_not_initialized_message(pcap); |
296 | | return (INVALID_HANDLE_VALUE); |
297 | | } |
298 | | |
299 | | static int |
300 | | pcap_oid_get_request_not_initialized(pcap_t *pcap, bpf_u_int32 oid _U_, |
301 | | void *data _U_, size_t *lenp _U_) |
302 | | { |
303 | | pcap_set_not_initialized_message(pcap); |
304 | | return (PCAP_ERROR_NOT_ACTIVATED); |
305 | | } |
306 | | |
307 | | static int |
308 | | pcap_oid_set_request_not_initialized(pcap_t *pcap, bpf_u_int32 oid _U_, |
309 | | const void *data _U_, size_t *lenp _U_) |
310 | | { |
311 | | pcap_set_not_initialized_message(pcap); |
312 | | return (PCAP_ERROR_NOT_ACTIVATED); |
313 | | } |
314 | | |
315 | | static u_int |
316 | | pcap_sendqueue_transmit_not_initialized(pcap_t *pcap, pcap_send_queue* queue, int sync) |
317 | | { |
318 | | pcap_set_not_initialized_message(pcap); |
319 | | return (0); |
320 | | } |
321 | | |
322 | | static int |
323 | | pcap_setuserbuffer_not_initialized(pcap_t *pcap, int size _U_) |
324 | | { |
325 | | pcap_set_not_initialized_message(pcap); |
326 | | return (PCAP_ERROR_NOT_ACTIVATED); |
327 | | } |
328 | | |
329 | | static int |
330 | | pcap_live_dump_not_initialized(pcap_t *pcap, char *filename _U_, int maxsize _U_, |
331 | | int maxpacks _U_) |
332 | | { |
333 | | pcap_set_not_initialized_message(pcap); |
334 | | return (PCAP_ERROR_NOT_ACTIVATED); |
335 | | } |
336 | | |
337 | | static int |
338 | | pcap_live_dump_ended_not_initialized(pcap_t *pcap, int sync _U_) |
339 | | { |
340 | | pcap_set_not_initialized_message(pcap); |
341 | | return (PCAP_ERROR_NOT_ACTIVATED); |
342 | | } |
343 | | |
344 | | static PAirpcapHandle |
345 | | pcap_get_airpcap_handle_not_initialized(pcap_t *pcap) |
346 | | { |
347 | | pcap_set_not_initialized_message(pcap); |
348 | | return (NULL); |
349 | | } |
350 | | #endif |
351 | | |
352 | | /* |
353 | | * Returns 1 if rfmon mode can be set on the pcap_t, 0 if it can't, |
354 | | * a PCAP_ERROR value on an error. |
355 | | */ |
356 | | int |
357 | | pcap_can_set_rfmon(pcap_t *p) |
358 | 0 | { |
359 | 0 | return (p->can_set_rfmon_op(p)); |
360 | 0 | } |
361 | | |
362 | | /* |
363 | | * For systems where rfmon mode is never supported. |
364 | | */ |
365 | | static int |
366 | | pcap_cant_set_rfmon(pcap_t *p _U_) |
367 | 0 | { |
368 | 0 | return (0); |
369 | 0 | } |
370 | | |
371 | | /* |
372 | | * Sets *tstamp_typesp to point to an array 1 or more supported time stamp |
373 | | * types; the return value is the number of supported time stamp types. |
374 | | * The list should be freed by a call to pcap_free_tstamp_types() when |
375 | | * you're done with it. |
376 | | * |
377 | | * A return value of 0 means "you don't get a choice of time stamp type", |
378 | | * in which case *tstamp_typesp is set to null. |
379 | | * |
380 | | * PCAP_ERROR is returned on error. |
381 | | */ |
382 | | int |
383 | | pcap_list_tstamp_types(pcap_t *p, int **tstamp_typesp) |
384 | 0 | { |
385 | 0 | if (p->tstamp_type_count == 0) { |
386 | | /* |
387 | | * We don't support multiple time stamp types. |
388 | | * That means the only type we support is PCAP_TSTAMP_HOST; |
389 | | * set up a list containing only that type. |
390 | | */ |
391 | 0 | *tstamp_typesp = (int*)malloc(sizeof(**tstamp_typesp)); |
392 | 0 | if (*tstamp_typesp == NULL) { |
393 | 0 | pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), |
394 | 0 | errno, "malloc"); |
395 | 0 | return (PCAP_ERROR); |
396 | 0 | } |
397 | 0 | **tstamp_typesp = PCAP_TSTAMP_HOST; |
398 | 0 | return (1); |
399 | 0 | } else { |
400 | 0 | *tstamp_typesp = (int*)calloc(sizeof(**tstamp_typesp), |
401 | 0 | p->tstamp_type_count); |
402 | 0 | if (*tstamp_typesp == NULL) { |
403 | 0 | pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), |
404 | 0 | errno, "malloc"); |
405 | 0 | return (PCAP_ERROR); |
406 | 0 | } |
407 | 0 | (void)memcpy(*tstamp_typesp, p->tstamp_type_list, |
408 | 0 | sizeof(**tstamp_typesp) * p->tstamp_type_count); |
409 | 0 | return (p->tstamp_type_count); |
410 | 0 | } |
411 | 0 | } |
412 | | |
413 | | /* |
414 | | * In Windows, you might have a library built with one version of the |
415 | | * C runtime library and an application built with another version of |
416 | | * the C runtime library, which means that the library might use one |
417 | | * version of malloc() and free() and the application might use another |
418 | | * version of malloc() and free(). If so, that means something |
419 | | * allocated by the library cannot be freed by the application, so we |
420 | | * need to have a pcap_free_tstamp_types() routine to free up the list |
421 | | * allocated by pcap_list_tstamp_types(), even though it's just a wrapper |
422 | | * around free(). |
423 | | */ |
424 | | void |
425 | | pcap_free_tstamp_types(int *tstamp_type_list) |
426 | 0 | { |
427 | 0 | free(tstamp_type_list); |
428 | 0 | } |
429 | | |
430 | | /* |
431 | | * Default one-shot callback; overridden for capture types where the |
432 | | * packet data cannot be guaranteed to be available after the callback |
433 | | * returns, so that a copy must be made. |
434 | | */ |
435 | | void |
436 | | pcap_oneshot(u_char *user, const struct pcap_pkthdr *h, const u_char *pkt) |
437 | 170k | { |
438 | 170k | struct oneshot_userdata *sp = (struct oneshot_userdata *)user; |
439 | | |
440 | 170k | *sp->hdr = *h; |
441 | 170k | *sp->pkt = pkt; |
442 | 170k | } |
443 | | |
444 | | const u_char * |
445 | | pcap_next(pcap_t *p, struct pcap_pkthdr *h) |
446 | 0 | { |
447 | 0 | struct oneshot_userdata s; |
448 | 0 | const u_char *pkt; |
449 | |
|
450 | 0 | s.hdr = h; |
451 | 0 | s.pkt = &pkt; |
452 | 0 | s.pd = p; |
453 | 0 | if (pcap_dispatch(p, 1, p->oneshot_callback, (u_char *)&s) <= 0) |
454 | 0 | return (0); |
455 | 0 | return (pkt); |
456 | 0 | } |
457 | | |
458 | | int |
459 | | pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, |
460 | | const u_char **pkt_data) |
461 | 173k | { |
462 | 173k | struct oneshot_userdata s; |
463 | | |
464 | 173k | s.hdr = &p->pcap_header; |
465 | 173k | s.pkt = pkt_data; |
466 | 173k | s.pd = p; |
467 | | |
468 | | /* Saves a pointer to the packet headers */ |
469 | 173k | *pkt_header= &p->pcap_header; |
470 | | |
471 | 173k | if (p->rfile != NULL) { |
472 | 173k | int status; |
473 | | |
474 | | /* We are on an offline capture */ |
475 | 173k | status = pcap_offline_read(p, 1, p->oneshot_callback, |
476 | 173k | (u_char *)&s); |
477 | | |
478 | | /* |
479 | | * Return codes for pcap_offline_read() are: |
480 | | * - 0: EOF |
481 | | * - -1: error |
482 | | * - >1: OK |
483 | | * The first one ('0') conflicts with the return code of |
484 | | * 0 from pcap_read() meaning "no packets arrived before |
485 | | * the timeout expired", so we map it to -2 so you can |
486 | | * distinguish between an EOF from a savefile and a |
487 | | * "no packets arrived before the timeout expired, try |
488 | | * again" from a live capture. |
489 | | */ |
490 | 173k | if (status == 0) |
491 | 484 | return (-2); |
492 | 173k | else |
493 | 173k | return (status); |
494 | 173k | } |
495 | | |
496 | | /* |
497 | | * Return codes for pcap_read() are: |
498 | | * - 0: timeout |
499 | | * - -1: error |
500 | | * - -2: loop was broken out of with pcap_breakloop() |
501 | | * - >1: OK |
502 | | * The first one ('0') conflicts with the return code of 0 from |
503 | | * pcap_offline_read() meaning "end of file". |
504 | | */ |
505 | 0 | return (p->read_op(p, 1, p->oneshot_callback, (u_char *)&s)); |
506 | 173k | } |
507 | | |
508 | | /* |
509 | | * Implementation of a pcap_if_list_t. |
510 | | */ |
511 | | struct pcap_if_list { |
512 | | pcap_if_t *beginning; |
513 | | }; |
514 | | |
515 | | static struct capture_source_type { |
516 | | int (*findalldevs_op)(pcap_if_list_t *, char *); |
517 | | pcap_t *(*create_op)(const char *, char *, int *); |
518 | | } capture_source_types[] = { |
519 | | #ifdef HAVE_DAG_API |
520 | | { dag_findalldevs, dag_create }, |
521 | | #endif |
522 | | #ifdef HAVE_SEPTEL_API |
523 | | { septel_findalldevs, septel_create }, |
524 | | #endif |
525 | | #ifdef HAVE_SNF_API |
526 | | { snf_findalldevs, snf_create }, |
527 | | #endif |
528 | | #ifdef HAVE_TC_API |
529 | | { TcFindAllDevs, TcCreate }, |
530 | | #endif |
531 | | #ifdef PCAP_SUPPORT_BT |
532 | | { bt_findalldevs, bt_create }, |
533 | | #endif |
534 | | #ifdef PCAP_SUPPORT_BT_MONITOR |
535 | | { bt_monitor_findalldevs, bt_monitor_create }, |
536 | | #endif |
537 | | #ifdef PCAP_SUPPORT_USB |
538 | | { usb_findalldevs, usb_create }, |
539 | | #endif |
540 | | #ifdef PCAP_SUPPORT_NETFILTER |
541 | | { netfilter_findalldevs, netfilter_create }, |
542 | | #endif |
543 | | #ifdef PCAP_SUPPORT_NETMAP |
544 | | { pcap_netmap_findalldevs, pcap_netmap_create }, |
545 | | #endif |
546 | | #ifdef PCAP_SUPPORT_DBUS |
547 | | { dbus_findalldevs, dbus_create }, |
548 | | #endif |
549 | | #ifdef PCAP_SUPPORT_RDMASNIFF |
550 | | { rdmasniff_findalldevs, rdmasniff_create }, |
551 | | #endif |
552 | | { NULL, NULL } |
553 | | }; |
554 | | |
555 | | /* |
556 | | * Get a list of all capture sources that are up and that we can open. |
557 | | * Returns -1 on error, 0 otherwise. |
558 | | * The list, as returned through "alldevsp", may be null if no interfaces |
559 | | * were up and could be opened. |
560 | | */ |
561 | | int |
562 | | pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf) |
563 | 0 | { |
564 | 0 | size_t i; |
565 | 0 | pcap_if_list_t devlist; |
566 | | |
567 | | /* |
568 | | * Find all the local network interfaces on which we |
569 | | * can capture. |
570 | | */ |
571 | 0 | devlist.beginning = NULL; |
572 | 0 | if (pcap_platform_finddevs(&devlist, errbuf) == -1) { |
573 | | /* |
574 | | * Failed - free all of the entries we were given |
575 | | * before we failed. |
576 | | */ |
577 | 0 | if (devlist.beginning != NULL) |
578 | 0 | pcap_freealldevs(devlist.beginning); |
579 | 0 | *alldevsp = NULL; |
580 | 0 | return (-1); |
581 | 0 | } |
582 | | |
583 | | /* |
584 | | * Ask each of the non-local-network-interface capture |
585 | | * source types what interfaces they have. |
586 | | */ |
587 | 0 | for (i = 0; capture_source_types[i].findalldevs_op != NULL; i++) { |
588 | 0 | if (capture_source_types[i].findalldevs_op(&devlist, errbuf) == -1) { |
589 | | /* |
590 | | * We had an error; free the list we've been |
591 | | * constructing. |
592 | | */ |
593 | 0 | if (devlist.beginning != NULL) |
594 | 0 | pcap_freealldevs(devlist.beginning); |
595 | 0 | *alldevsp = NULL; |
596 | 0 | return (-1); |
597 | 0 | } |
598 | 0 | } |
599 | | |
600 | | /* |
601 | | * Return the first entry of the list of all devices. |
602 | | */ |
603 | 0 | *alldevsp = devlist.beginning; |
604 | 0 | return (0); |
605 | 0 | } |
606 | | |
607 | | static struct sockaddr * |
608 | | dup_sockaddr(struct sockaddr *sa, size_t sa_length) |
609 | 0 | { |
610 | 0 | struct sockaddr *newsa; |
611 | |
|
612 | 0 | if ((newsa = malloc(sa_length)) == NULL) |
613 | 0 | return (NULL); |
614 | 0 | return (memcpy(newsa, sa, sa_length)); |
615 | 0 | } |
616 | | |
617 | | /* |
618 | | * Construct a "figure of merit" for an interface, for use when sorting |
619 | | * the list of interfaces, in which interfaces that are up are superior |
620 | | * to interfaces that aren't up, interfaces that are up and running are |
621 | | * superior to interfaces that are up but not running, and non-loopback |
622 | | * interfaces that are up and running are superior to loopback interfaces, |
623 | | * and interfaces with the same flags have a figure of merit that's higher |
624 | | * the lower the instance number. |
625 | | * |
626 | | * The goal is to try to put the interfaces most likely to be useful for |
627 | | * capture at the beginning of the list. |
628 | | * |
629 | | * The figure of merit, which is lower the "better" the interface is, |
630 | | * has the uppermost bit set if the interface isn't running, the bit |
631 | | * below that set if the interface isn't up, the bit below that set |
632 | | * if the interface is a loopback interface, and the interface index |
633 | | * in the 29 bits below that. (Yes, we assume u_int is 32 bits.) |
634 | | */ |
635 | | static u_int |
636 | | get_figure_of_merit(pcap_if_t *dev) |
637 | 0 | { |
638 | 0 | const char *cp; |
639 | 0 | u_int n; |
640 | |
|
641 | 0 | if (strcmp(dev->name, "any") == 0) { |
642 | | /* |
643 | | * Give the "any" device an artificially high instance |
644 | | * number, so it shows up after all other non-loopback |
645 | | * interfaces. |
646 | | */ |
647 | 0 | n = 0x1FFFFFFF; /* 29 all-1 bits */ |
648 | 0 | } else { |
649 | | /* |
650 | | * A number at the end of the device name string is |
651 | | * assumed to be an instance number. Add 1 to the |
652 | | * instance number, and use 0 for "no instance |
653 | | * number", so we don't put "no instance number" |
654 | | * devices and "instance 0" devices together. |
655 | | */ |
656 | 0 | cp = dev->name + strlen(dev->name) - 1; |
657 | 0 | while (cp-1 >= dev->name && *(cp-1) >= '0' && *(cp-1) <= '9') |
658 | 0 | cp--; |
659 | 0 | if (*cp >= '0' && *cp <= '9') |
660 | 0 | n = atoi(cp) + 1; |
661 | 0 | else |
662 | 0 | n = 0; |
663 | 0 | } |
664 | 0 | if (!(dev->flags & PCAP_IF_RUNNING)) |
665 | 0 | n |= 0x80000000; |
666 | 0 | if (!(dev->flags & PCAP_IF_UP)) |
667 | 0 | n |= 0x40000000; |
668 | | |
669 | | /* |
670 | | * Give non-wireless interfaces that aren't disconnected a better |
671 | | * figure of merit than interfaces that are disconnected, as |
672 | | * "disconnected" should indicate that the interface isn't |
673 | | * plugged into a network and thus won't give you any traffic. |
674 | | * |
675 | | * For wireless interfaces, it means "associated with a network", |
676 | | * which we presume not to necessarily prevent capture, as you |
677 | | * might run the adapter in some flavor of monitor mode. |
678 | | */ |
679 | 0 | if (!(dev->flags & PCAP_IF_WIRELESS) && |
680 | 0 | (dev->flags & PCAP_IF_CONNECTION_STATUS) == PCAP_IF_CONNECTION_STATUS_DISCONNECTED) |
681 | 0 | n |= 0x20000000; |
682 | | |
683 | | /* |
684 | | * Sort loopback devices after non-loopback devices, *except* for |
685 | | * disconnected devices. |
686 | | */ |
687 | 0 | if (dev->flags & PCAP_IF_LOOPBACK) |
688 | 0 | n |= 0x10000000; |
689 | |
|
690 | 0 | return (n); |
691 | 0 | } |
692 | | |
693 | | #ifndef _WIN32 |
694 | | /* |
695 | | * Try to get a description for a given device. |
696 | | * Returns a mallocated description if it could and NULL if it couldn't. |
697 | | * |
698 | | * XXX - on FreeBSDs that support it, should it get the sysctl named |
699 | | * "dev.{adapter family name}.{adapter unit}.%desc" to get a description |
700 | | * of the adapter? Note that "dev.an.0.%desc" is "Aironet PC4500/PC4800" |
701 | | * with my Cisco 350 card, so the name isn't entirely descriptive. The |
702 | | * "dev.an.0.%pnpinfo" has a better description, although one might argue |
703 | | * that the problem is really a driver bug - if it can find out that it's |
704 | | * a Cisco 340 or 350, rather than an old Aironet card, it should use |
705 | | * that in the description. |
706 | | * |
707 | | * Do NetBSD, DragonflyBSD, or OpenBSD support this as well? FreeBSD |
708 | | * and OpenBSD let you get a description, but it's not generated by the OS, |
709 | | * it's set with another ioctl that ifconfig supports; we use that to get |
710 | | * a description in FreeBSD and OpenBSD, but if there is no such |
711 | | * description available, it still might be nice to get some description |
712 | | * string based on the device type or something such as that. |
713 | | * |
714 | | * In macOS, the System Configuration framework can apparently return |
715 | | * names in 10.4 and later. |
716 | | * |
717 | | * It also appears that freedesktop.org's HAL offers an "info.product" |
718 | | * string, but the HAL specification says it "should not be used in any |
719 | | * UI" and "subsystem/capability specific properties" should be used |
720 | | * instead and, in any case, I think HAL is being deprecated in |
721 | | * favor of other stuff such as DeviceKit. DeviceKit doesn't appear |
722 | | * to have any obvious product information for devices, but maybe |
723 | | * I haven't looked hard enough. |
724 | | * |
725 | | * Using the System Configuration framework, or HAL, or DeviceKit, or |
726 | | * whatever, would require that libpcap applications be linked with |
727 | | * the frameworks/libraries in question. That shouldn't be a problem |
728 | | * for programs linking with the shared version of libpcap (unless |
729 | | * you're running on AIX - which I think is the only UN*X that doesn't |
730 | | * support linking a shared library with other libraries on which it |
731 | | * depends, and having an executable linked only with the first shared |
732 | | * library automatically pick up the other libraries when started - |
733 | | * and using HAL or whatever). Programs linked with the static |
734 | | * version of libpcap would have to use pcap-config with the --static |
735 | | * flag in order to get the right linker flags in order to pick up |
736 | | * the additional libraries/frameworks; those programs need that anyway |
737 | | * for libpcap 1.1 and beyond on Linux, as, by default, it requires |
738 | | * -lnl. |
739 | | * |
740 | | * Do any other UN*Xes, or desktop environments support getting a |
741 | | * description? |
742 | | */ |
743 | | static char * |
744 | | #ifdef SIOCGIFDESCR |
745 | | get_if_description(const char *name) |
746 | | { |
747 | | char *description = NULL; |
748 | | int s; |
749 | | struct ifreq ifrdesc; |
750 | | #ifndef IFDESCRSIZE |
751 | | size_t descrlen = 64; |
752 | | #else |
753 | | size_t descrlen = IFDESCRSIZE; |
754 | | #endif /* IFDESCRSIZE */ |
755 | | |
756 | | /* |
757 | | * Get the description for the interface. |
758 | | */ |
759 | | memset(&ifrdesc, 0, sizeof ifrdesc); |
760 | | pcap_strlcpy(ifrdesc.ifr_name, name, sizeof ifrdesc.ifr_name); |
761 | | s = socket(AF_INET, SOCK_DGRAM, 0); |
762 | | if (s >= 0) { |
763 | | #ifdef __FreeBSD__ |
764 | | /* |
765 | | * On FreeBSD, if the buffer isn't big enough for the |
766 | | * description, the ioctl succeeds, but the description |
767 | | * isn't copied, ifr_buffer.length is set to the description |
768 | | * length, and ifr_buffer.buffer is set to NULL. |
769 | | */ |
770 | | for (;;) { |
771 | | free(description); |
772 | | if ((description = malloc(descrlen)) != NULL) { |
773 | | ifrdesc.ifr_buffer.buffer = description; |
774 | | ifrdesc.ifr_buffer.length = descrlen; |
775 | | if (ioctl(s, SIOCGIFDESCR, &ifrdesc) == 0) { |
776 | | if (ifrdesc.ifr_buffer.buffer == |
777 | | description) |
778 | | break; |
779 | | else |
780 | | descrlen = ifrdesc.ifr_buffer.length; |
781 | | } else { |
782 | | /* |
783 | | * Failed to get interface description. |
784 | | */ |
785 | | free(description); |
786 | | description = NULL; |
787 | | break; |
788 | | } |
789 | | } else |
790 | | break; |
791 | | } |
792 | | #else /* __FreeBSD__ */ |
793 | | /* |
794 | | * The only other OS that currently supports |
795 | | * SIOCGIFDESCR is OpenBSD, and it has no way |
796 | | * to get the description length - it's clamped |
797 | | * to a maximum of IFDESCRSIZE. |
798 | | */ |
799 | | if ((description = malloc(descrlen)) != NULL) { |
800 | | ifrdesc.ifr_data = (caddr_t)description; |
801 | | if (ioctl(s, SIOCGIFDESCR, &ifrdesc) != 0) { |
802 | | /* |
803 | | * Failed to get interface description. |
804 | | */ |
805 | | free(description); |
806 | | description = NULL; |
807 | | } |
808 | | } |
809 | | #endif /* __FreeBSD__ */ |
810 | | close(s); |
811 | | if (description != NULL && description[0] == '\0') { |
812 | | /* |
813 | | * Description is empty, so discard it. |
814 | | */ |
815 | | free(description); |
816 | | description = NULL; |
817 | | } |
818 | | } |
819 | | |
820 | | #ifdef __FreeBSD__ |
821 | | /* |
822 | | * For FreeBSD, if we didn't get a description, and this is |
823 | | * a device with a name of the form usbusN, label it as a USB |
824 | | * bus. |
825 | | */ |
826 | | if (description == NULL) { |
827 | | if (strncmp(name, "usbus", 5) == 0) { |
828 | | /* |
829 | | * OK, it begins with "usbus". |
830 | | */ |
831 | | long busnum; |
832 | | char *p; |
833 | | |
834 | | errno = 0; |
835 | | busnum = strtol(name + 5, &p, 10); |
836 | | if (errno == 0 && p != name + 5 && *p == '\0' && |
837 | | busnum >= 0 && busnum <= INT_MAX) { |
838 | | /* |
839 | | * OK, it's a valid number that's not |
840 | | * bigger than INT_MAX. Construct |
841 | | * a description from it. |
842 | | * (If that fails, we don't worry about |
843 | | * it, we just return NULL.) |
844 | | */ |
845 | | if (pcap_asprintf(&description, |
846 | | "USB bus number %ld", busnum) == -1) { |
847 | | /* Failed. */ |
848 | | description = NULL; |
849 | | } |
850 | | } |
851 | | } |
852 | | } |
853 | | #endif |
854 | | return (description); |
855 | | #else /* SIOCGIFDESCR */ |
856 | | get_if_description(const char *name _U_) |
857 | 0 | { |
858 | 0 | return (NULL); |
859 | 0 | #endif /* SIOCGIFDESCR */ |
860 | 0 | } |
861 | | |
862 | | /* |
863 | | * Look for a given device in the specified list of devices. |
864 | | * |
865 | | * If we find it, return a pointer to its entry. |
866 | | * |
867 | | * If we don't find it, attempt to add an entry for it, with the specified |
868 | | * IFF_ flags and description, and, if that succeeds, return a pointer to |
869 | | * the new entry, otherwise return NULL and set errbuf to an error message. |
870 | | */ |
871 | | pcap_if_t * |
872 | | find_or_add_if(pcap_if_list_t *devlistp, const char *name, |
873 | | bpf_u_int32 if_flags, get_if_flags_func get_flags_func, char *errbuf) |
874 | 0 | { |
875 | 0 | bpf_u_int32 pcap_flags; |
876 | | |
877 | | /* |
878 | | * Convert IFF_ flags to pcap flags. |
879 | | */ |
880 | 0 | pcap_flags = 0; |
881 | 0 | #ifdef IFF_LOOPBACK |
882 | 0 | if (if_flags & IFF_LOOPBACK) |
883 | 0 | pcap_flags |= PCAP_IF_LOOPBACK; |
884 | | #else |
885 | | /* |
886 | | * We don't have IFF_LOOPBACK, so look at the device name to |
887 | | * see if it looks like a loopback device. |
888 | | */ |
889 | | if (name[0] == 'l' && name[1] == 'o' && |
890 | | (isdigit((unsigned char)(name[2])) || name[2] == '\0') |
891 | | pcap_flags |= PCAP_IF_LOOPBACK; |
892 | | #endif |
893 | 0 | #ifdef IFF_UP |
894 | 0 | if (if_flags & IFF_UP) |
895 | 0 | pcap_flags |= PCAP_IF_UP; |
896 | 0 | #endif |
897 | 0 | #ifdef IFF_RUNNING |
898 | 0 | if (if_flags & IFF_RUNNING) |
899 | 0 | pcap_flags |= PCAP_IF_RUNNING; |
900 | 0 | #endif |
901 | | |
902 | | /* |
903 | | * Attempt to find an entry for this device; if we don't find one, |
904 | | * attempt to add one. |
905 | | */ |
906 | 0 | return (find_or_add_dev(devlistp, name, pcap_flags, |
907 | 0 | get_flags_func, get_if_description(name), errbuf)); |
908 | 0 | } |
909 | | |
910 | | /* |
911 | | * Look for a given device in the specified list of devices. |
912 | | * |
913 | | * If we find it, then, if the specified address isn't null, add it to |
914 | | * the list of addresses for the device and return 0. |
915 | | * |
916 | | * If we don't find it, attempt to add an entry for it, with the specified |
917 | | * IFF_ flags and description, and, if that succeeds, add the specified |
918 | | * address to its list of addresses if that address is non-null, and |
919 | | * return 0, otherwise return -1 and set errbuf to an error message. |
920 | | * |
921 | | * (We can get called with a null address because we might get a list |
922 | | * of interface name/address combinations from the underlying OS, with |
923 | | * the address being absent in some cases, rather than a list of |
924 | | * interfaces with each interface having a list of addresses, so this |
925 | | * call may be the only call made to add to the list, and we want to |
926 | | * add interfaces even if they have no addresses.) |
927 | | */ |
928 | | int |
929 | | add_addr_to_if(pcap_if_list_t *devlistp, const char *name, |
930 | | bpf_u_int32 if_flags, get_if_flags_func get_flags_func, |
931 | | struct sockaddr *addr, size_t addr_size, |
932 | | struct sockaddr *netmask, size_t netmask_size, |
933 | | struct sockaddr *broadaddr, size_t broadaddr_size, |
934 | | struct sockaddr *dstaddr, size_t dstaddr_size, |
935 | | char *errbuf) |
936 | 0 | { |
937 | 0 | pcap_if_t *curdev; |
938 | | |
939 | | /* |
940 | | * Check whether the device exists and, if not, add it. |
941 | | */ |
942 | 0 | curdev = find_or_add_if(devlistp, name, if_flags, get_flags_func, |
943 | 0 | errbuf); |
944 | 0 | if (curdev == NULL) { |
945 | | /* |
946 | | * Error - give up. |
947 | | */ |
948 | 0 | return (-1); |
949 | 0 | } |
950 | | |
951 | 0 | if (addr == NULL) { |
952 | | /* |
953 | | * There's no address to add; this entry just meant |
954 | | * "here's a new interface". |
955 | | */ |
956 | 0 | return (0); |
957 | 0 | } |
958 | | |
959 | | /* |
960 | | * "curdev" is an entry for this interface, and we have an |
961 | | * address for it; add an entry for that address to the |
962 | | * interface's list of addresses. |
963 | | */ |
964 | 0 | return (add_addr_to_dev(curdev, addr, addr_size, netmask, |
965 | 0 | netmask_size, broadaddr, broadaddr_size, dstaddr, |
966 | 0 | dstaddr_size, errbuf)); |
967 | 0 | } |
968 | | #endif /* _WIN32 */ |
969 | | |
970 | | /* |
971 | | * Add an entry to the list of addresses for an interface. |
972 | | * "curdev" is the entry for that interface. |
973 | | */ |
974 | | int |
975 | | add_addr_to_dev(pcap_if_t *curdev, |
976 | | struct sockaddr *addr, size_t addr_size, |
977 | | struct sockaddr *netmask, size_t netmask_size, |
978 | | struct sockaddr *broadaddr, size_t broadaddr_size, |
979 | | struct sockaddr *dstaddr, size_t dstaddr_size, |
980 | | char *errbuf) |
981 | 0 | { |
982 | 0 | pcap_addr_t *curaddr, *prevaddr, *nextaddr; |
983 | | |
984 | | /* |
985 | | * Allocate the new entry and fill it in. |
986 | | */ |
987 | 0 | curaddr = (pcap_addr_t *)malloc(sizeof(pcap_addr_t)); |
988 | 0 | if (curaddr == NULL) { |
989 | 0 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
990 | 0 | errno, "malloc"); |
991 | 0 | return (-1); |
992 | 0 | } |
993 | | |
994 | 0 | curaddr->next = NULL; |
995 | 0 | if (addr != NULL && addr_size != 0) { |
996 | 0 | curaddr->addr = (struct sockaddr *)dup_sockaddr(addr, addr_size); |
997 | 0 | if (curaddr->addr == NULL) { |
998 | 0 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
999 | 0 | errno, "malloc"); |
1000 | 0 | free(curaddr); |
1001 | 0 | return (-1); |
1002 | 0 | } |
1003 | 0 | } else |
1004 | 0 | curaddr->addr = NULL; |
1005 | | |
1006 | 0 | if (netmask != NULL && netmask_size != 0) { |
1007 | 0 | curaddr->netmask = (struct sockaddr *)dup_sockaddr(netmask, netmask_size); |
1008 | 0 | if (curaddr->netmask == NULL) { |
1009 | 0 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
1010 | 0 | errno, "malloc"); |
1011 | 0 | if (curaddr->addr != NULL) |
1012 | 0 | free(curaddr->addr); |
1013 | 0 | free(curaddr); |
1014 | 0 | return (-1); |
1015 | 0 | } |
1016 | 0 | } else |
1017 | 0 | curaddr->netmask = NULL; |
1018 | | |
1019 | 0 | if (broadaddr != NULL && broadaddr_size != 0) { |
1020 | 0 | curaddr->broadaddr = (struct sockaddr *)dup_sockaddr(broadaddr, broadaddr_size); |
1021 | 0 | if (curaddr->broadaddr == NULL) { |
1022 | 0 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
1023 | 0 | errno, "malloc"); |
1024 | 0 | if (curaddr->netmask != NULL) |
1025 | 0 | free(curaddr->netmask); |
1026 | 0 | if (curaddr->addr != NULL) |
1027 | 0 | free(curaddr->addr); |
1028 | 0 | free(curaddr); |
1029 | 0 | return (-1); |
1030 | 0 | } |
1031 | 0 | } else |
1032 | 0 | curaddr->broadaddr = NULL; |
1033 | | |
1034 | 0 | if (dstaddr != NULL && dstaddr_size != 0) { |
1035 | 0 | curaddr->dstaddr = (struct sockaddr *)dup_sockaddr(dstaddr, dstaddr_size); |
1036 | 0 | if (curaddr->dstaddr == NULL) { |
1037 | 0 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
1038 | 0 | errno, "malloc"); |
1039 | 0 | if (curaddr->broadaddr != NULL) |
1040 | 0 | free(curaddr->broadaddr); |
1041 | 0 | if (curaddr->netmask != NULL) |
1042 | 0 | free(curaddr->netmask); |
1043 | 0 | if (curaddr->addr != NULL) |
1044 | 0 | free(curaddr->addr); |
1045 | 0 | free(curaddr); |
1046 | 0 | return (-1); |
1047 | 0 | } |
1048 | 0 | } else |
1049 | 0 | curaddr->dstaddr = NULL; |
1050 | | |
1051 | | /* |
1052 | | * Find the end of the list of addresses. |
1053 | | */ |
1054 | 0 | for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) { |
1055 | 0 | nextaddr = prevaddr->next; |
1056 | 0 | if (nextaddr == NULL) { |
1057 | | /* |
1058 | | * This is the end of the list. |
1059 | | */ |
1060 | 0 | break; |
1061 | 0 | } |
1062 | 0 | } |
1063 | |
|
1064 | 0 | if (prevaddr == NULL) { |
1065 | | /* |
1066 | | * The list was empty; this is the first member. |
1067 | | */ |
1068 | 0 | curdev->addresses = curaddr; |
1069 | 0 | } else { |
1070 | | /* |
1071 | | * "prevaddr" is the last member of the list; append |
1072 | | * this member to it. |
1073 | | */ |
1074 | 0 | prevaddr->next = curaddr; |
1075 | 0 | } |
1076 | |
|
1077 | 0 | return (0); |
1078 | 0 | } |
1079 | | |
1080 | | /* |
1081 | | * Look for a given device in the specified list of devices. |
1082 | | * |
1083 | | * If we find it, return 0 and set *curdev_ret to point to it. |
1084 | | * |
1085 | | * If we don't find it, attempt to add an entry for it, with the specified |
1086 | | * flags and description, and, if that succeeds, return 0, otherwise |
1087 | | * return -1 and set errbuf to an error message. |
1088 | | */ |
1089 | | pcap_if_t * |
1090 | | find_or_add_dev(pcap_if_list_t *devlistp, const char *name, bpf_u_int32 flags, |
1091 | | get_if_flags_func get_flags_func, const char *description, char *errbuf) |
1092 | 0 | { |
1093 | 0 | pcap_if_t *curdev; |
1094 | | |
1095 | | /* |
1096 | | * Is there already an entry in the list for this device? |
1097 | | */ |
1098 | 0 | curdev = find_dev(devlistp, name); |
1099 | 0 | if (curdev != NULL) { |
1100 | | /* |
1101 | | * Yes, return it. |
1102 | | */ |
1103 | 0 | return (curdev); |
1104 | 0 | } |
1105 | | |
1106 | | /* |
1107 | | * No, we didn't find it. |
1108 | | */ |
1109 | | |
1110 | | /* |
1111 | | * Try to get additional flags for the device. |
1112 | | */ |
1113 | 0 | if ((*get_flags_func)(name, &flags, errbuf) == -1) { |
1114 | | /* |
1115 | | * Failed. |
1116 | | */ |
1117 | 0 | return (NULL); |
1118 | 0 | } |
1119 | | |
1120 | | /* |
1121 | | * Now, try to add it to the list of devices. |
1122 | | */ |
1123 | 0 | return (add_dev(devlistp, name, flags, description, errbuf)); |
1124 | 0 | } |
1125 | | |
1126 | | /* |
1127 | | * Look for a given device in the specified list of devices, and return |
1128 | | * the entry for it if we find it or NULL if we don't. |
1129 | | */ |
1130 | | pcap_if_t * |
1131 | | find_dev(pcap_if_list_t *devlistp, const char *name) |
1132 | 0 | { |
1133 | 0 | pcap_if_t *curdev; |
1134 | | |
1135 | | /* |
1136 | | * Is there an entry in the list for this device? |
1137 | | */ |
1138 | 0 | for (curdev = devlistp->beginning; curdev != NULL; |
1139 | 0 | curdev = curdev->next) { |
1140 | 0 | if (strcmp(name, curdev->name) == 0) { |
1141 | | /* |
1142 | | * We found it, so, yes, there is. No need to |
1143 | | * add it. Provide the entry we found to our |
1144 | | * caller. |
1145 | | */ |
1146 | 0 | return (curdev); |
1147 | 0 | } |
1148 | 0 | } |
1149 | | |
1150 | | /* |
1151 | | * No. |
1152 | | */ |
1153 | 0 | return (NULL); |
1154 | 0 | } |
1155 | | |
1156 | | /* |
1157 | | * Attempt to add an entry for a device, with the specified flags |
1158 | | * and description, and, if that succeeds, return 0 and return a pointer |
1159 | | * to the new entry, otherwise return NULL and set errbuf to an error |
1160 | | * message. |
1161 | | * |
1162 | | * If we weren't given a description, try to get one. |
1163 | | */ |
1164 | | pcap_if_t * |
1165 | | add_dev(pcap_if_list_t *devlistp, const char *name, bpf_u_int32 flags, |
1166 | | const char *description, char *errbuf) |
1167 | 0 | { |
1168 | 0 | pcap_if_t *curdev, *prevdev, *nextdev; |
1169 | 0 | u_int this_figure_of_merit, nextdev_figure_of_merit; |
1170 | |
|
1171 | 0 | curdev = malloc(sizeof(pcap_if_t)); |
1172 | 0 | if (curdev == NULL) { |
1173 | 0 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
1174 | 0 | errno, "malloc"); |
1175 | 0 | return (NULL); |
1176 | 0 | } |
1177 | | |
1178 | | /* |
1179 | | * Fill in the entry. |
1180 | | */ |
1181 | 0 | curdev->next = NULL; |
1182 | 0 | curdev->name = strdup(name); |
1183 | 0 | if (curdev->name == NULL) { |
1184 | 0 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
1185 | 0 | errno, "malloc"); |
1186 | 0 | free(curdev); |
1187 | 0 | return (NULL); |
1188 | 0 | } |
1189 | 0 | if (description == NULL) { |
1190 | | /* |
1191 | | * We weren't handed a description for the interface. |
1192 | | */ |
1193 | 0 | curdev->description = NULL; |
1194 | 0 | } else { |
1195 | | /* |
1196 | | * We were handed a description; make a copy. |
1197 | | */ |
1198 | 0 | curdev->description = strdup(description); |
1199 | 0 | if (curdev->description == NULL) { |
1200 | 0 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
1201 | 0 | errno, "malloc"); |
1202 | 0 | free(curdev->name); |
1203 | 0 | free(curdev); |
1204 | 0 | return (NULL); |
1205 | 0 | } |
1206 | 0 | } |
1207 | 0 | curdev->addresses = NULL; /* list starts out as empty */ |
1208 | 0 | curdev->flags = flags; |
1209 | | |
1210 | | /* |
1211 | | * Add it to the list, in the appropriate location. |
1212 | | * First, get the "figure of merit" for this interface. |
1213 | | */ |
1214 | 0 | this_figure_of_merit = get_figure_of_merit(curdev); |
1215 | | |
1216 | | /* |
1217 | | * Now look for the last interface with an figure of merit |
1218 | | * less than or equal to the new interface's figure of merit. |
1219 | | * |
1220 | | * We start with "prevdev" being NULL, meaning we're before |
1221 | | * the first element in the list. |
1222 | | */ |
1223 | 0 | prevdev = NULL; |
1224 | 0 | for (;;) { |
1225 | | /* |
1226 | | * Get the interface after this one. |
1227 | | */ |
1228 | 0 | if (prevdev == NULL) { |
1229 | | /* |
1230 | | * The next element is the first element. |
1231 | | */ |
1232 | 0 | nextdev = devlistp->beginning; |
1233 | 0 | } else |
1234 | 0 | nextdev = prevdev->next; |
1235 | | |
1236 | | /* |
1237 | | * Are we at the end of the list? |
1238 | | */ |
1239 | 0 | if (nextdev == NULL) { |
1240 | | /* |
1241 | | * Yes - we have to put the new entry after "prevdev". |
1242 | | */ |
1243 | 0 | break; |
1244 | 0 | } |
1245 | | |
1246 | | /* |
1247 | | * Is the new interface's figure of merit less |
1248 | | * than the next interface's figure of merit, |
1249 | | * meaning that the new interface is better |
1250 | | * than the next interface? |
1251 | | */ |
1252 | 0 | nextdev_figure_of_merit = get_figure_of_merit(nextdev); |
1253 | 0 | if (this_figure_of_merit < nextdev_figure_of_merit) { |
1254 | | /* |
1255 | | * Yes - we should put the new entry |
1256 | | * before "nextdev", i.e. after "prevdev". |
1257 | | */ |
1258 | 0 | break; |
1259 | 0 | } |
1260 | | |
1261 | 0 | prevdev = nextdev; |
1262 | 0 | } |
1263 | | |
1264 | | /* |
1265 | | * Insert before "nextdev". |
1266 | | */ |
1267 | 0 | curdev->next = nextdev; |
1268 | | |
1269 | | /* |
1270 | | * Insert after "prevdev" - unless "prevdev" is null, |
1271 | | * in which case this is the first interface. |
1272 | | */ |
1273 | 0 | if (prevdev == NULL) { |
1274 | | /* |
1275 | | * This is the first interface. Make it |
1276 | | * the first element in the list of devices. |
1277 | | */ |
1278 | 0 | devlistp->beginning = curdev; |
1279 | 0 | } else |
1280 | 0 | prevdev->next = curdev; |
1281 | 0 | return (curdev); |
1282 | 0 | } |
1283 | | |
1284 | | /* |
1285 | | * Free a list of interfaces. |
1286 | | */ |
1287 | | void |
1288 | | pcap_freealldevs(pcap_if_t *alldevs) |
1289 | 0 | { |
1290 | 0 | pcap_if_t *curdev, *nextdev; |
1291 | 0 | pcap_addr_t *curaddr, *nextaddr; |
1292 | |
|
1293 | 0 | for (curdev = alldevs; curdev != NULL; curdev = nextdev) { |
1294 | 0 | nextdev = curdev->next; |
1295 | | |
1296 | | /* |
1297 | | * Free all addresses. |
1298 | | */ |
1299 | 0 | for (curaddr = curdev->addresses; curaddr != NULL; curaddr = nextaddr) { |
1300 | 0 | nextaddr = curaddr->next; |
1301 | 0 | if (curaddr->addr) |
1302 | 0 | free(curaddr->addr); |
1303 | 0 | if (curaddr->netmask) |
1304 | 0 | free(curaddr->netmask); |
1305 | 0 | if (curaddr->broadaddr) |
1306 | 0 | free(curaddr->broadaddr); |
1307 | 0 | if (curaddr->dstaddr) |
1308 | 0 | free(curaddr->dstaddr); |
1309 | 0 | free(curaddr); |
1310 | 0 | } |
1311 | | |
1312 | | /* |
1313 | | * Free the name string. |
1314 | | */ |
1315 | 0 | free(curdev->name); |
1316 | | |
1317 | | /* |
1318 | | * Free the description string, if any. |
1319 | | */ |
1320 | 0 | if (curdev->description != NULL) |
1321 | 0 | free(curdev->description); |
1322 | | |
1323 | | /* |
1324 | | * Free the interface. |
1325 | | */ |
1326 | 0 | free(curdev); |
1327 | 0 | } |
1328 | 0 | } |
1329 | | |
1330 | | /* |
1331 | | * pcap-npf.c has its own pcap_lookupdev(), for compatibility reasons, as |
1332 | | * it actually returns the names of all interfaces, with a NUL separator |
1333 | | * between them; some callers may depend on that. |
1334 | | * |
1335 | | * MS-DOS has its own pcap_lookupdev(), but that might be useful only |
1336 | | * as an optimization. |
1337 | | * |
1338 | | * In all other cases, we just use pcap_findalldevs() to get a list of |
1339 | | * devices, and pick from that list. |
1340 | | */ |
1341 | | #if !defined(HAVE_PACKET32) && !defined(MSDOS) |
1342 | | /* |
1343 | | * Return the name of a network interface attached to the system, or NULL |
1344 | | * if none can be found. The interface must be configured up; the |
1345 | | * lowest unit number is preferred; loopback is ignored. |
1346 | | */ |
1347 | | char * |
1348 | | pcap_lookupdev(char *errbuf) |
1349 | 0 | { |
1350 | 0 | pcap_if_t *alldevs; |
1351 | | #ifdef _WIN32 |
1352 | | /* |
1353 | | * Windows - use the same size as the old WinPcap 3.1 code. |
1354 | | * XXX - this is probably bigger than it needs to be. |
1355 | | */ |
1356 | | #define IF_NAMESIZE 8192 |
1357 | | #else |
1358 | | /* |
1359 | | * UN*X - use the system's interface name size. |
1360 | | * XXX - that might not be large enough for capture devices |
1361 | | * that aren't regular network interfaces. |
1362 | | */ |
1363 | | /* for old BSD systems, including bsdi3 */ |
1364 | | #ifndef IF_NAMESIZE |
1365 | | #define IF_NAMESIZE IFNAMSIZ |
1366 | | #endif |
1367 | 0 | #endif |
1368 | 0 | static char device[IF_NAMESIZE + 1]; |
1369 | 0 | char *ret; |
1370 | |
|
1371 | 0 | if (pcap_findalldevs(&alldevs, errbuf) == -1) |
1372 | 0 | return (NULL); |
1373 | | |
1374 | 0 | if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) { |
1375 | | /* |
1376 | | * There are no devices on the list, or the first device |
1377 | | * on the list is a loopback device, which means there |
1378 | | * are no non-loopback devices on the list. This means |
1379 | | * we can't return any device. |
1380 | | * |
1381 | | * XXX - why not return a loopback device? If we can't |
1382 | | * capture on it, it won't be on the list, and if it's |
1383 | | * on the list, there aren't any non-loopback devices, |
1384 | | * so why not just supply it as the default device? |
1385 | | */ |
1386 | 0 | (void)pcap_strlcpy(errbuf, "no suitable device found", |
1387 | 0 | PCAP_ERRBUF_SIZE); |
1388 | 0 | ret = NULL; |
1389 | 0 | } else { |
1390 | | /* |
1391 | | * Return the name of the first device on the list. |
1392 | | */ |
1393 | 0 | (void)pcap_strlcpy(device, alldevs->name, sizeof(device)); |
1394 | 0 | ret = device; |
1395 | 0 | } |
1396 | |
|
1397 | 0 | pcap_freealldevs(alldevs); |
1398 | 0 | return (ret); |
1399 | 0 | } |
1400 | | #endif /* !defined(HAVE_PACKET32) && !defined(MSDOS) */ |
1401 | | |
1402 | | #if !defined(_WIN32) && !defined(MSDOS) |
1403 | | /* |
1404 | | * We don't just fetch the entire list of devices, search for the |
1405 | | * particular device, and use its first IPv4 address, as that's too |
1406 | | * much work to get just one device's netmask. |
1407 | | * |
1408 | | * If we had an API to get attributes for a given device, we could |
1409 | | * use that. |
1410 | | */ |
1411 | | int |
1412 | | pcap_lookupnet(const char *device, bpf_u_int32 *netp, bpf_u_int32 *maskp, |
1413 | | char *errbuf) |
1414 | 0 | { |
1415 | 0 | register int fd; |
1416 | 0 | register struct sockaddr_in *sin4; |
1417 | 0 | struct ifreq ifr; |
1418 | | |
1419 | | /* |
1420 | | * The pseudo-device "any" listens on all interfaces and therefore |
1421 | | * has the network address and -mask "0.0.0.0" therefore catching |
1422 | | * all traffic. Using NULL for the interface is the same as "any". |
1423 | | */ |
1424 | 0 | if (!device || strcmp(device, "any") == 0 |
1425 | | #ifdef HAVE_DAG_API |
1426 | | || strstr(device, "dag") != NULL |
1427 | | #endif |
1428 | | #ifdef HAVE_SEPTEL_API |
1429 | | || strstr(device, "septel") != NULL |
1430 | | #endif |
1431 | | #ifdef PCAP_SUPPORT_BT |
1432 | | || strstr(device, "bluetooth") != NULL |
1433 | | #endif |
1434 | 0 | #ifdef PCAP_SUPPORT_USB |
1435 | 0 | || strstr(device, "usbmon") != NULL |
1436 | 0 | #endif |
1437 | | #ifdef HAVE_SNF_API |
1438 | | || strstr(device, "snf") != NULL |
1439 | | #endif |
1440 | | #ifdef PCAP_SUPPORT_NETMAP |
1441 | | || strncmp(device, "netmap:", 7) == 0 |
1442 | | || strncmp(device, "vale", 4) == 0 |
1443 | | #endif |
1444 | 0 | ) { |
1445 | 0 | *netp = *maskp = 0; |
1446 | 0 | return 0; |
1447 | 0 | } |
1448 | | |
1449 | 0 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
1450 | 0 | if (fd < 0) { |
1451 | 0 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
1452 | 0 | errno, "socket"); |
1453 | 0 | return (-1); |
1454 | 0 | } |
1455 | 0 | memset(&ifr, 0, sizeof(ifr)); |
1456 | 0 | #ifdef linux |
1457 | | /* XXX Work around Linux kernel bug */ |
1458 | 0 | ifr.ifr_addr.sa_family = AF_INET; |
1459 | 0 | #endif |
1460 | 0 | (void)pcap_strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); |
1461 | 0 | if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) { |
1462 | 0 | if (errno == EADDRNOTAVAIL) { |
1463 | 0 | (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
1464 | 0 | "%s: no IPv4 address assigned", device); |
1465 | 0 | } else { |
1466 | 0 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
1467 | 0 | errno, "SIOCGIFADDR: %s", device); |
1468 | 0 | } |
1469 | 0 | (void)close(fd); |
1470 | 0 | return (-1); |
1471 | 0 | } |
1472 | 0 | sin4 = (struct sockaddr_in *)&ifr.ifr_addr; |
1473 | 0 | *netp = sin4->sin_addr.s_addr; |
1474 | 0 | memset(&ifr, 0, sizeof(ifr)); |
1475 | 0 | #ifdef linux |
1476 | | /* XXX Work around Linux kernel bug */ |
1477 | 0 | ifr.ifr_addr.sa_family = AF_INET; |
1478 | 0 | #endif |
1479 | 0 | (void)pcap_strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); |
1480 | 0 | if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) { |
1481 | 0 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
1482 | 0 | errno, "SIOCGIFNETMASK: %s", device); |
1483 | 0 | (void)close(fd); |
1484 | 0 | return (-1); |
1485 | 0 | } |
1486 | 0 | (void)close(fd); |
1487 | 0 | *maskp = sin4->sin_addr.s_addr; |
1488 | 0 | if (*maskp == 0) { |
1489 | 0 | if (IN_CLASSA(*netp)) |
1490 | 0 | *maskp = IN_CLASSA_NET; |
1491 | 0 | else if (IN_CLASSB(*netp)) |
1492 | 0 | *maskp = IN_CLASSB_NET; |
1493 | 0 | else if (IN_CLASSC(*netp)) |
1494 | 0 | *maskp = IN_CLASSC_NET; |
1495 | 0 | else { |
1496 | 0 | (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
1497 | 0 | "inet class for 0x%x unknown", *netp); |
1498 | 0 | return (-1); |
1499 | 0 | } |
1500 | 0 | } |
1501 | 0 | *netp &= *maskp; |
1502 | 0 | return (0); |
1503 | 0 | } |
1504 | | #endif /* !defined(_WIN32) && !defined(MSDOS) */ |
1505 | | |
1506 | | #ifdef ENABLE_REMOTE |
1507 | | #include "pcap-rpcap.h" |
1508 | | |
1509 | | /* |
1510 | | * Extract a substring from a string. |
1511 | | */ |
1512 | | static char * |
1513 | | get_substring(const char *p, size_t len, char *ebuf) |
1514 | | { |
1515 | | char *token; |
1516 | | |
1517 | | token = malloc(len + 1); |
1518 | | if (token == NULL) { |
1519 | | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
1520 | | errno, "malloc"); |
1521 | | return (NULL); |
1522 | | } |
1523 | | memcpy(token, p, len); |
1524 | | token[len] = '\0'; |
1525 | | return (token); |
1526 | | } |
1527 | | |
1528 | | /* |
1529 | | * Parse a capture source that might be a URL. |
1530 | | * |
1531 | | * If the source is not a URL, *schemep, *userinfop, *hostp, and *portp |
1532 | | * are set to NULL, *pathp is set to point to the source, and 0 is |
1533 | | * returned. |
1534 | | * |
1535 | | * If source is a URL, and the URL refers to a local device (a special |
1536 | | * case of rpcap:), *schemep, *userinfop, *hostp, and *portp are set |
1537 | | * to NULL, *pathp is set to point to the device name, and 0 is returned. |
1538 | | * |
1539 | | * If source is a URL, and it's not a special case that refers to a local |
1540 | | * device, and the parse succeeds: |
1541 | | * |
1542 | | * *schemep is set to point to an allocated string containing the scheme; |
1543 | | * |
1544 | | * if user information is present in the URL, *userinfop is set to point |
1545 | | * to an allocated string containing the user information, otherwise |
1546 | | * it's set to NULL; |
1547 | | * |
1548 | | * if host information is present in the URL, *hostp is set to point |
1549 | | * to an allocated string containing the host information, otherwise |
1550 | | * it's set to NULL; |
1551 | | * |
1552 | | * if a port number is present in the URL, *portp is set to point |
1553 | | * to an allocated string containing the port number, otherwise |
1554 | | * it's set to NULL; |
1555 | | * |
1556 | | * *pathp is set to point to an allocated string containing the |
1557 | | * path; |
1558 | | * |
1559 | | * and 0 is returned. |
1560 | | * |
1561 | | * If the parse fails, ebuf is set to an error string, and -1 is returned. |
1562 | | */ |
1563 | | static int |
1564 | | pcap_parse_source(const char *source, char **schemep, char **userinfop, |
1565 | | char **hostp, char **portp, char **pathp, char *ebuf) |
1566 | | { |
1567 | | char *colonp; |
1568 | | size_t scheme_len; |
1569 | | char *scheme; |
1570 | | const char *endp; |
1571 | | size_t authority_len; |
1572 | | char *authority; |
1573 | | char *parsep, *atsignp, *bracketp; |
1574 | | char *userinfo, *host, *port, *path; |
1575 | | |
1576 | | /* |
1577 | | * Start out returning nothing. |
1578 | | */ |
1579 | | *schemep = NULL; |
1580 | | *userinfop = NULL; |
1581 | | *hostp = NULL; |
1582 | | *portp = NULL; |
1583 | | *pathp = NULL; |
1584 | | |
1585 | | /* |
1586 | | * RFC 3986 says: |
1587 | | * |
1588 | | * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] |
1589 | | * |
1590 | | * hier-part = "//" authority path-abempty |
1591 | | * / path-absolute |
1592 | | * / path-rootless |
1593 | | * / path-empty |
1594 | | * |
1595 | | * authority = [ userinfo "@" ] host [ ":" port ] |
1596 | | * |
1597 | | * userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) |
1598 | | * |
1599 | | * Step 1: look for the ":" at the end of the scheme. |
1600 | | * A colon in the source is *NOT* sufficient to indicate that |
1601 | | * this is a URL, as interface names on some platforms might |
1602 | | * include colons (e.g., I think some Solaris interfaces |
1603 | | * might). |
1604 | | */ |
1605 | | colonp = strchr(source, ':'); |
1606 | | if (colonp == NULL) { |
1607 | | /* |
1608 | | * The source is the device to open. |
1609 | | * Return a NULL pointer for the scheme, user information, |
1610 | | * host, and port, and return the device as the path. |
1611 | | */ |
1612 | | *pathp = strdup(source); |
1613 | | if (*pathp == NULL) { |
1614 | | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
1615 | | errno, "malloc"); |
1616 | | return (-1); |
1617 | | } |
1618 | | return (0); |
1619 | | } |
1620 | | |
1621 | | /* |
1622 | | * All schemes must have "//" after them, i.e. we only support |
1623 | | * hier-part = "//" authority path-abempty, not |
1624 | | * hier-part = path-absolute |
1625 | | * hier-part = path-rootless |
1626 | | * hier-part = path-empty |
1627 | | * |
1628 | | * We need that in order to distinguish between a local device |
1629 | | * name that happens to contain a colon and a URI. |
1630 | | */ |
1631 | | if (strncmp(colonp + 1, "//", 2) != 0) { |
1632 | | /* |
1633 | | * The source is the device to open. |
1634 | | * Return a NULL pointer for the scheme, user information, |
1635 | | * host, and port, and return the device as the path. |
1636 | | */ |
1637 | | *pathp = strdup(source); |
1638 | | if (*pathp == NULL) { |
1639 | | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
1640 | | errno, "malloc"); |
1641 | | return (-1); |
1642 | | } |
1643 | | return (0); |
1644 | | } |
1645 | | |
1646 | | /* |
1647 | | * XXX - check whether the purported scheme could be a scheme? |
1648 | | */ |
1649 | | |
1650 | | /* |
1651 | | * OK, this looks like a URL. |
1652 | | * Get the scheme. |
1653 | | */ |
1654 | | scheme_len = colonp - source; |
1655 | | scheme = malloc(scheme_len + 1); |
1656 | | if (scheme == NULL) { |
1657 | | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
1658 | | errno, "malloc"); |
1659 | | return (-1); |
1660 | | } |
1661 | | memcpy(scheme, source, scheme_len); |
1662 | | scheme[scheme_len] = '\0'; |
1663 | | |
1664 | | /* |
1665 | | * Treat file: specially - take everything after file:// as |
1666 | | * the pathname. |
1667 | | */ |
1668 | | if (pcap_strcasecmp(scheme, "file") == 0) { |
1669 | | *pathp = strdup(colonp + 3); |
1670 | | if (*pathp == NULL) { |
1671 | | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
1672 | | errno, "malloc"); |
1673 | | free(scheme); |
1674 | | return (-1); |
1675 | | } |
1676 | | *schemep = scheme; |
1677 | | return (0); |
1678 | | } |
1679 | | |
1680 | | /* |
1681 | | * The WinPcap documentation says you can specify a local |
1682 | | * interface with "rpcap://{device}"; we special-case |
1683 | | * that here. If the scheme is "rpcap", and there are |
1684 | | * no slashes past the "//", we just return the device. |
1685 | | * |
1686 | | * XXX - %-escaping? |
1687 | | */ |
1688 | | if (pcap_strcasecmp(scheme, "rpcap") == 0 && |
1689 | | strchr(colonp + 3, '/') == NULL) { |
1690 | | /* |
1691 | | * Local device. |
1692 | | * |
1693 | | * Return a NULL pointer for the scheme, user information, |
1694 | | * host, and port, and return the device as the path. |
1695 | | */ |
1696 | | free(scheme); |
1697 | | *pathp = strdup(colonp + 3); |
1698 | | if (*pathp == NULL) { |
1699 | | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
1700 | | errno, "malloc"); |
1701 | | return (-1); |
1702 | | } |
1703 | | return (0); |
1704 | | } |
1705 | | |
1706 | | /* |
1707 | | * OK, now start parsing the authority. |
1708 | | * Get token, terminated with / or terminated at the end of |
1709 | | * the string. |
1710 | | */ |
1711 | | authority_len = strcspn(colonp + 3, "/"); |
1712 | | authority = get_substring(colonp + 3, authority_len, ebuf); |
1713 | | if (authority == NULL) { |
1714 | | /* |
1715 | | * Error. |
1716 | | */ |
1717 | | free(scheme); |
1718 | | return (-1); |
1719 | | } |
1720 | | endp = colonp + 3 + authority_len; |
1721 | | |
1722 | | /* |
1723 | | * Now carve the authority field into its components. |
1724 | | */ |
1725 | | parsep = authority; |
1726 | | |
1727 | | /* |
1728 | | * Is there a userinfo field? |
1729 | | */ |
1730 | | atsignp = strchr(parsep, '@'); |
1731 | | if (atsignp != NULL) { |
1732 | | /* |
1733 | | * Yes. |
1734 | | */ |
1735 | | size_t userinfo_len; |
1736 | | |
1737 | | userinfo_len = atsignp - parsep; |
1738 | | userinfo = get_substring(parsep, userinfo_len, ebuf); |
1739 | | if (userinfo == NULL) { |
1740 | | /* |
1741 | | * Error. |
1742 | | */ |
1743 | | free(authority); |
1744 | | free(scheme); |
1745 | | return (-1); |
1746 | | } |
1747 | | parsep = atsignp + 1; |
1748 | | } else { |
1749 | | /* |
1750 | | * No. |
1751 | | */ |
1752 | | userinfo = NULL; |
1753 | | } |
1754 | | |
1755 | | /* |
1756 | | * Is there a host field? |
1757 | | */ |
1758 | | if (*parsep == '\0') { |
1759 | | /* |
1760 | | * No; there's no host field or port field. |
1761 | | */ |
1762 | | host = NULL; |
1763 | | port = NULL; |
1764 | | } else { |
1765 | | /* |
1766 | | * Yes. |
1767 | | */ |
1768 | | size_t host_len; |
1769 | | |
1770 | | /* |
1771 | | * Is it an IP-literal? |
1772 | | */ |
1773 | | if (*parsep == '[') { |
1774 | | /* |
1775 | | * Yes. |
1776 | | * Treat verything up to the closing square |
1777 | | * bracket as the IP-Literal; we don't worry |
1778 | | * about whether it's a valid IPv6address or |
1779 | | * IPvFuture (or an IPv4address, for that |
1780 | | * matter, just in case we get handed a |
1781 | | * URL with an IPv4 IP-Literal, of the sort |
1782 | | * that pcap_createsrcstr() used to generate, |
1783 | | * and that pcap_parsesrcstr(), in the original |
1784 | | * WinPcap code, accepted). |
1785 | | */ |
1786 | | bracketp = strchr(parsep, ']'); |
1787 | | if (bracketp == NULL) { |
1788 | | /* |
1789 | | * There's no closing square bracket. |
1790 | | */ |
1791 | | pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, |
1792 | | "IP-literal in URL doesn't end with ]"); |
1793 | | free(userinfo); |
1794 | | free(authority); |
1795 | | free(scheme); |
1796 | | return (-1); |
1797 | | } |
1798 | | if (*(bracketp + 1) != '\0' && |
1799 | | *(bracketp + 1) != ':') { |
1800 | | /* |
1801 | | * There's extra crud after the |
1802 | | * closing square bracketn. |
1803 | | */ |
1804 | | pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, |
1805 | | "Extra text after IP-literal in URL"); |
1806 | | free(userinfo); |
1807 | | free(authority); |
1808 | | free(scheme); |
1809 | | return (-1); |
1810 | | } |
1811 | | host_len = (bracketp - 1) - parsep; |
1812 | | host = get_substring(parsep + 1, host_len, ebuf); |
1813 | | if (host == NULL) { |
1814 | | /* |
1815 | | * Error. |
1816 | | */ |
1817 | | free(userinfo); |
1818 | | free(authority); |
1819 | | free(scheme); |
1820 | | return (-1); |
1821 | | } |
1822 | | parsep = bracketp + 1; |
1823 | | } else { |
1824 | | /* |
1825 | | * No. |
1826 | | * Treat everything up to a : or the end of |
1827 | | * the string as the host. |
1828 | | */ |
1829 | | host_len = strcspn(parsep, ":"); |
1830 | | host = get_substring(parsep, host_len, ebuf); |
1831 | | if (host == NULL) { |
1832 | | /* |
1833 | | * Error. |
1834 | | */ |
1835 | | free(userinfo); |
1836 | | free(authority); |
1837 | | free(scheme); |
1838 | | return (-1); |
1839 | | } |
1840 | | parsep = parsep + host_len; |
1841 | | } |
1842 | | |
1843 | | /* |
1844 | | * Is there a port field? |
1845 | | */ |
1846 | | if (*parsep == ':') { |
1847 | | /* |
1848 | | * Yes. It's the rest of the authority field. |
1849 | | */ |
1850 | | size_t port_len; |
1851 | | |
1852 | | parsep++; |
1853 | | port_len = strlen(parsep); |
1854 | | port = get_substring(parsep, port_len, ebuf); |
1855 | | if (port == NULL) { |
1856 | | /* |
1857 | | * Error. |
1858 | | */ |
1859 | | free(host); |
1860 | | free(userinfo); |
1861 | | free(authority); |
1862 | | free(scheme); |
1863 | | return (-1); |
1864 | | } |
1865 | | } else { |
1866 | | /* |
1867 | | * No. |
1868 | | */ |
1869 | | port = NULL; |
1870 | | } |
1871 | | } |
1872 | | free(authority); |
1873 | | |
1874 | | /* |
1875 | | * Everything else is the path. Strip off the leading /. |
1876 | | */ |
1877 | | if (*endp == '\0') |
1878 | | path = strdup(""); |
1879 | | else |
1880 | | path = strdup(endp + 1); |
1881 | | if (path == NULL) { |
1882 | | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
1883 | | errno, "malloc"); |
1884 | | free(port); |
1885 | | free(host); |
1886 | | free(userinfo); |
1887 | | free(scheme); |
1888 | | return (-1); |
1889 | | } |
1890 | | *schemep = scheme; |
1891 | | *userinfop = userinfo; |
1892 | | *hostp = host; |
1893 | | *portp = port; |
1894 | | *pathp = path; |
1895 | | return (0); |
1896 | | } |
1897 | | |
1898 | | int |
1899 | | pcap_createsrcstr(char *source, int type, const char *host, const char *port, |
1900 | | const char *name, char *errbuf) |
1901 | | { |
1902 | | switch (type) { |
1903 | | |
1904 | | case PCAP_SRC_FILE: |
1905 | | pcap_strlcpy(source, PCAP_SRC_FILE_STRING, PCAP_BUF_SIZE); |
1906 | | if (name != NULL && *name != '\0') { |
1907 | | pcap_strlcat(source, name, PCAP_BUF_SIZE); |
1908 | | return (0); |
1909 | | } else { |
1910 | | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
1911 | | "The file name cannot be NULL."); |
1912 | | return (-1); |
1913 | | } |
1914 | | |
1915 | | case PCAP_SRC_IFREMOTE: |
1916 | | pcap_strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE); |
1917 | | if (host != NULL && *host != '\0') { |
1918 | | if (strchr(host, ':') != NULL) { |
1919 | | /* |
1920 | | * The host name contains a colon, so it's |
1921 | | * probably an IPv6 address, and needs to |
1922 | | * be included in square brackets. |
1923 | | */ |
1924 | | pcap_strlcat(source, "[", PCAP_BUF_SIZE); |
1925 | | pcap_strlcat(source, host, PCAP_BUF_SIZE); |
1926 | | pcap_strlcat(source, "]", PCAP_BUF_SIZE); |
1927 | | } else |
1928 | | pcap_strlcat(source, host, PCAP_BUF_SIZE); |
1929 | | |
1930 | | if (port != NULL && *port != '\0') { |
1931 | | pcap_strlcat(source, ":", PCAP_BUF_SIZE); |
1932 | | pcap_strlcat(source, port, PCAP_BUF_SIZE); |
1933 | | } |
1934 | | |
1935 | | pcap_strlcat(source, "/", PCAP_BUF_SIZE); |
1936 | | } else { |
1937 | | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
1938 | | "The host name cannot be NULL."); |
1939 | | return (-1); |
1940 | | } |
1941 | | |
1942 | | if (name != NULL && *name != '\0') |
1943 | | pcap_strlcat(source, name, PCAP_BUF_SIZE); |
1944 | | |
1945 | | return (0); |
1946 | | |
1947 | | case PCAP_SRC_IFLOCAL: |
1948 | | pcap_strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE); |
1949 | | |
1950 | | if (name != NULL && *name != '\0') |
1951 | | pcap_strlcat(source, name, PCAP_BUF_SIZE); |
1952 | | |
1953 | | return (0); |
1954 | | |
1955 | | default: |
1956 | | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
1957 | | "The interface type is not valid."); |
1958 | | return (-1); |
1959 | | } |
1960 | | } |
1961 | | |
1962 | | int |
1963 | | pcap_parsesrcstr(const char *source, int *type, char *host, char *port, |
1964 | | char *name, char *errbuf) |
1965 | | { |
1966 | | char *scheme, *tmpuserinfo, *tmphost, *tmpport, *tmppath; |
1967 | | |
1968 | | /* Initialization stuff */ |
1969 | | if (host) |
1970 | | *host = '\0'; |
1971 | | if (port) |
1972 | | *port = '\0'; |
1973 | | if (name) |
1974 | | *name = '\0'; |
1975 | | |
1976 | | /* Parse the source string */ |
1977 | | if (pcap_parse_source(source, &scheme, &tmpuserinfo, &tmphost, |
1978 | | &tmpport, &tmppath, errbuf) == -1) { |
1979 | | /* |
1980 | | * Fail. |
1981 | | */ |
1982 | | return (-1); |
1983 | | } |
1984 | | |
1985 | | if (scheme == NULL) { |
1986 | | /* |
1987 | | * Local device. |
1988 | | */ |
1989 | | if (name && tmppath) |
1990 | | pcap_strlcpy(name, tmppath, PCAP_BUF_SIZE); |
1991 | | if (type) |
1992 | | *type = PCAP_SRC_IFLOCAL; |
1993 | | free(tmppath); |
1994 | | free(tmpport); |
1995 | | free(tmphost); |
1996 | | free(tmpuserinfo); |
1997 | | return (0); |
1998 | | } |
1999 | | |
2000 | | if (strcmp(scheme, "rpcap") == 0) { |
2001 | | /* |
2002 | | * rpcap:// |
2003 | | * |
2004 | | * pcap_parse_source() has already handled the case of |
2005 | | * rpcap://device |
2006 | | */ |
2007 | | if (host && tmphost) { |
2008 | | if (tmpuserinfo) |
2009 | | pcap_snprintf(host, PCAP_BUF_SIZE, "%s@%s", |
2010 | | tmpuserinfo, tmphost); |
2011 | | else |
2012 | | pcap_strlcpy(host, tmphost, PCAP_BUF_SIZE); |
2013 | | } |
2014 | | if (port && tmpport) |
2015 | | pcap_strlcpy(port, tmpport, PCAP_BUF_SIZE); |
2016 | | if (name && tmppath) |
2017 | | pcap_strlcpy(name, tmppath, PCAP_BUF_SIZE); |
2018 | | if (type) |
2019 | | *type = PCAP_SRC_IFREMOTE; |
2020 | | free(tmppath); |
2021 | | free(tmpport); |
2022 | | free(tmphost); |
2023 | | free(tmpuserinfo); |
2024 | | free(scheme); |
2025 | | return (0); |
2026 | | } |
2027 | | |
2028 | | if (strcmp(scheme, "file") == 0) { |
2029 | | /* |
2030 | | * file:// |
2031 | | */ |
2032 | | if (name && tmppath) |
2033 | | pcap_strlcpy(name, tmppath, PCAP_BUF_SIZE); |
2034 | | if (type) |
2035 | | *type = PCAP_SRC_FILE; |
2036 | | free(tmppath); |
2037 | | free(tmpport); |
2038 | | free(tmphost); |
2039 | | free(tmpuserinfo); |
2040 | | free(scheme); |
2041 | | return (0); |
2042 | | } |
2043 | | |
2044 | | /* |
2045 | | * Neither rpcap: nor file:; just treat the entire string |
2046 | | * as a local device. |
2047 | | */ |
2048 | | if (name) |
2049 | | pcap_strlcpy(name, source, PCAP_BUF_SIZE); |
2050 | | if (type) |
2051 | | *type = PCAP_SRC_IFLOCAL; |
2052 | | free(tmppath); |
2053 | | free(tmpport); |
2054 | | free(tmphost); |
2055 | | free(tmpuserinfo); |
2056 | | free(scheme); |
2057 | | return (0); |
2058 | | } |
2059 | | #endif |
2060 | | |
2061 | | pcap_t * |
2062 | | pcap_create(const char *device, char *errbuf) |
2063 | 0 | { |
2064 | 0 | size_t i; |
2065 | 0 | int is_theirs; |
2066 | 0 | pcap_t *p; |
2067 | 0 | char *device_str; |
2068 | | |
2069 | | /* |
2070 | | * A null device name is equivalent to the "any" device - |
2071 | | * which might not be supported on this platform, but |
2072 | | * this means that you'll get a "not supported" error |
2073 | | * rather than, say, a crash when we try to dereference |
2074 | | * the null pointer. |
2075 | | */ |
2076 | 0 | if (device == NULL) |
2077 | 0 | device_str = strdup("any"); |
2078 | 0 | else { |
2079 | | #ifdef _WIN32 |
2080 | | /* |
2081 | | * On Windows, for backwards compatibility reasons, |
2082 | | * pcap_lookupdev() returns a pointer to a sequence of |
2083 | | * pairs of UTF-16LE device names and local code page |
2084 | | * description strings. |
2085 | | * |
2086 | | * This means that if a program uses pcap_lookupdev() |
2087 | | * to get a default device, and hands that to an API |
2088 | | * that opens devices, we'll get handed a UTF-16LE |
2089 | | * string, not a string in the local code page. |
2090 | | * |
2091 | | * To work around that, we check whether the string |
2092 | | * looks as if it might be a UTF-16LE strinh and, if |
2093 | | * so, convert it back to the local code page's |
2094 | | * extended ASCII. |
2095 | | * |
2096 | | * XXX - you *cannot* reliably detect whether a |
2097 | | * string is UTF-16LE or not; "a" could either |
2098 | | * be a one-character ASCII string or the first |
2099 | | * character of a UTF-16LE string. This particular |
2100 | | * version of this heuristic dates back to WinPcap |
2101 | | * 4.1.1; PacketOpenAdapter() does uses the same |
2102 | | * heuristic, with the exact same vulnerability. |
2103 | | */ |
2104 | | if (device[0] != '\0' && device[1] == '\0') { |
2105 | | size_t length; |
2106 | | |
2107 | | length = wcslen((wchar_t *)device); |
2108 | | device_str = (char *)malloc(length + 1); |
2109 | | if (device_str == NULL) { |
2110 | | pcap_fmt_errmsg_for_errno(errbuf, |
2111 | | PCAP_ERRBUF_SIZE, errno, |
2112 | | "malloc"); |
2113 | | return (NULL); |
2114 | | } |
2115 | | |
2116 | | pcap_snprintf(device_str, length + 1, "%ws", |
2117 | | (const wchar_t *)device); |
2118 | | } else |
2119 | | #endif |
2120 | 0 | device_str = strdup(device); |
2121 | 0 | } |
2122 | 0 | if (device_str == NULL) { |
2123 | 0 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
2124 | 0 | errno, "malloc"); |
2125 | 0 | return (NULL); |
2126 | 0 | } |
2127 | | |
2128 | | /* |
2129 | | * Try each of the non-local-network-interface capture |
2130 | | * source types until we find one that works for this |
2131 | | * device or run out of types. |
2132 | | */ |
2133 | 0 | for (i = 0; capture_source_types[i].create_op != NULL; i++) { |
2134 | 0 | is_theirs = 0; |
2135 | 0 | p = capture_source_types[i].create_op(device_str, errbuf, |
2136 | 0 | &is_theirs); |
2137 | 0 | if (is_theirs) { |
2138 | | /* |
2139 | | * The device name refers to a device of the |
2140 | | * type in question; either it succeeded, |
2141 | | * in which case p refers to a pcap_t to |
2142 | | * later activate for the device, or it |
2143 | | * failed, in which case p is null and we |
2144 | | * should return that to report the failure |
2145 | | * to create. |
2146 | | */ |
2147 | 0 | if (p == NULL) { |
2148 | | /* |
2149 | | * We assume the caller filled in errbuf. |
2150 | | */ |
2151 | 0 | free(device_str); |
2152 | 0 | return (NULL); |
2153 | 0 | } |
2154 | 0 | p->opt.device = device_str; |
2155 | 0 | return (p); |
2156 | 0 | } |
2157 | 0 | } |
2158 | | |
2159 | | /* |
2160 | | * OK, try it as a regular network interface. |
2161 | | */ |
2162 | 0 | p = pcap_create_interface(device_str, errbuf); |
2163 | 0 | if (p == NULL) { |
2164 | | /* |
2165 | | * We assume the caller filled in errbuf. |
2166 | | */ |
2167 | 0 | free(device_str); |
2168 | 0 | return (NULL); |
2169 | 0 | } |
2170 | 0 | p->opt.device = device_str; |
2171 | 0 | return (p); |
2172 | 0 | } |
2173 | | |
2174 | | /* |
2175 | | * Set nonblocking mode on an unactivated pcap_t; this sets a flag |
2176 | | * checked by pcap_activate(), which sets the mode after calling |
2177 | | * the activate routine. |
2178 | | */ |
2179 | | static int |
2180 | | pcap_setnonblock_unactivated(pcap_t *p, int nonblock) |
2181 | 0 | { |
2182 | 0 | p->opt.nonblock = nonblock; |
2183 | 0 | return (0); |
2184 | 0 | } |
2185 | | |
2186 | | static void |
2187 | | initialize_ops(pcap_t *p) |
2188 | 0 | { |
2189 | | /* |
2190 | | * Set operation pointers for operations that only work on |
2191 | | * an activated pcap_t to point to a routine that returns |
2192 | | * a "this isn't activated" error. |
2193 | | */ |
2194 | 0 | p->read_op = pcap_read_not_initialized; |
2195 | 0 | p->inject_op = pcap_inject_not_initialized; |
2196 | 0 | p->setfilter_op = pcap_setfilter_not_initialized; |
2197 | 0 | p->setdirection_op = pcap_setdirection_not_initialized; |
2198 | 0 | p->set_datalink_op = pcap_set_datalink_not_initialized; |
2199 | 0 | p->getnonblock_op = pcap_getnonblock_not_initialized; |
2200 | 0 | p->stats_op = pcap_stats_not_initialized; |
2201 | | #ifdef _WIN32 |
2202 | | p->stats_ex_op = pcap_stats_ex_not_initialized; |
2203 | | p->setbuff_op = pcap_setbuff_not_initialized; |
2204 | | p->setmode_op = pcap_setmode_not_initialized; |
2205 | | p->setmintocopy_op = pcap_setmintocopy_not_initialized; |
2206 | | p->getevent_op = pcap_getevent_not_initialized; |
2207 | | p->oid_get_request_op = pcap_oid_get_request_not_initialized; |
2208 | | p->oid_set_request_op = pcap_oid_set_request_not_initialized; |
2209 | | p->sendqueue_transmit_op = pcap_sendqueue_transmit_not_initialized; |
2210 | | p->setuserbuffer_op = pcap_setuserbuffer_not_initialized; |
2211 | | p->live_dump_op = pcap_live_dump_not_initialized; |
2212 | | p->live_dump_ended_op = pcap_live_dump_ended_not_initialized; |
2213 | | p->get_airpcap_handle_op = pcap_get_airpcap_handle_not_initialized; |
2214 | | #endif |
2215 | | |
2216 | | /* |
2217 | | * Default cleanup operation - implementations can override |
2218 | | * this, but should call pcap_cleanup_live_common() after |
2219 | | * doing their own additional cleanup. |
2220 | | */ |
2221 | 0 | p->cleanup_op = pcap_cleanup_live_common; |
2222 | | |
2223 | | /* |
2224 | | * In most cases, the standard one-shot callback can |
2225 | | * be used for pcap_next()/pcap_next_ex(). |
2226 | | */ |
2227 | 0 | p->oneshot_callback = pcap_oneshot; |
2228 | 0 | } |
2229 | | |
2230 | | static pcap_t * |
2231 | | pcap_alloc_pcap_t(char *ebuf, size_t size) |
2232 | 11.9k | { |
2233 | 11.9k | char *chunk; |
2234 | 11.9k | pcap_t *p; |
2235 | | |
2236 | | /* |
2237 | | * Allocate a chunk of memory big enough for a pcap_t |
2238 | | * plus a structure following it of size "size". The |
2239 | | * structure following it is a private data structure |
2240 | | * for the routines that handle this pcap_t. |
2241 | | * |
2242 | | * The structure following it must be aligned on |
2243 | | * the appropriate alignment boundary for this platform. |
2244 | | * We align on an 8-byte boundary as that's probably what |
2245 | | * at least some platforms do, even with 32-bit integers, |
2246 | | * and because we can't be sure that some values won't |
2247 | | * require 8-byte alignment even on platforms with 32-bit |
2248 | | * integers. |
2249 | | */ |
2250 | 35.7k | #define PCAP_T_ALIGNED_SIZE ((sizeof(pcap_t) + 7U) & ~0x7U) |
2251 | 11.9k | chunk = malloc(PCAP_T_ALIGNED_SIZE + size); |
2252 | 11.9k | if (chunk == NULL) { |
2253 | 0 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
2254 | 0 | errno, "malloc"); |
2255 | 0 | return (NULL); |
2256 | 0 | } |
2257 | 11.9k | memset(chunk, 0, PCAP_T_ALIGNED_SIZE + size); |
2258 | | |
2259 | | /* |
2260 | | * Get a pointer to the pcap_t at the beginning. |
2261 | | */ |
2262 | 11.9k | p = (pcap_t *)chunk; |
2263 | | |
2264 | | #ifdef _WIN32 |
2265 | | p->handle = INVALID_HANDLE_VALUE; /* not opened yet */ |
2266 | | #else /* _WIN32 */ |
2267 | 11.9k | p->fd = -1; /* not opened yet */ |
2268 | 11.9k | #ifndef MSDOS |
2269 | 11.9k | p->selectable_fd = -1; |
2270 | 11.9k | p->required_select_timeout = NULL; |
2271 | 11.9k | #endif /* MSDOS */ |
2272 | 11.9k | #endif /* _WIN32 */ |
2273 | | |
2274 | 11.9k | if (size == 0) { |
2275 | | /* No private data was requested. */ |
2276 | 0 | p->priv = NULL; |
2277 | 11.9k | } else { |
2278 | | /* |
2279 | | * Set the pointer to the private data; that's the structure |
2280 | | * of size "size" following the pcap_t. |
2281 | | */ |
2282 | 11.9k | p->priv = (void *)(chunk + PCAP_T_ALIGNED_SIZE); |
2283 | 11.9k | } |
2284 | | |
2285 | 11.9k | return (p); |
2286 | 11.9k | } |
2287 | | |
2288 | | pcap_t * |
2289 | | pcap_create_common(char *ebuf, size_t size) |
2290 | 0 | { |
2291 | 0 | pcap_t *p; |
2292 | |
|
2293 | 0 | p = pcap_alloc_pcap_t(ebuf, size); |
2294 | 0 | if (p == NULL) |
2295 | 0 | return (NULL); |
2296 | | |
2297 | | /* |
2298 | | * Default to "can't set rfmon mode"; if it's supported by |
2299 | | * a platform, the create routine that called us can set |
2300 | | * the op to its routine to check whether a particular |
2301 | | * device supports it. |
2302 | | */ |
2303 | 0 | p->can_set_rfmon_op = pcap_cant_set_rfmon; |
2304 | | |
2305 | | /* |
2306 | | * If pcap_setnonblock() is called on a not-yet-activated |
2307 | | * pcap_t, default to setting a flag and turning |
2308 | | * on non-blocking mode when activated. |
2309 | | */ |
2310 | 0 | p->setnonblock_op = pcap_setnonblock_unactivated; |
2311 | |
|
2312 | 0 | initialize_ops(p); |
2313 | | |
2314 | | /* put in some defaults*/ |
2315 | 0 | p->snapshot = 0; /* max packet size unspecified */ |
2316 | 0 | p->opt.timeout = 0; /* no timeout specified */ |
2317 | 0 | p->opt.buffer_size = 0; /* use the platform's default */ |
2318 | 0 | p->opt.promisc = 0; |
2319 | 0 | p->opt.rfmon = 0; |
2320 | 0 | p->opt.immediate = 0; |
2321 | 0 | p->opt.tstamp_type = -1; /* default to not setting time stamp type */ |
2322 | 0 | p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO; |
2323 | | /* |
2324 | | * Platform-dependent options. |
2325 | | */ |
2326 | 0 | #ifdef __linux__ |
2327 | 0 | p->opt.protocol = 0; |
2328 | 0 | #endif |
2329 | | #ifdef _WIN32 |
2330 | | p->opt.nocapture_local = 0; |
2331 | | #endif |
2332 | | |
2333 | | /* |
2334 | | * Start out with no BPF code generation flags set. |
2335 | | */ |
2336 | 0 | p->bpf_codegen_flags = 0; |
2337 | |
|
2338 | 0 | return (p); |
2339 | 0 | } |
2340 | | |
2341 | | int |
2342 | | pcap_check_activated(pcap_t *p) |
2343 | 0 | { |
2344 | 0 | if (p->activated) { |
2345 | 0 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't perform " |
2346 | 0 | " operation on activated capture"); |
2347 | 0 | return (-1); |
2348 | 0 | } |
2349 | 0 | return (0); |
2350 | 0 | } |
2351 | | |
2352 | | int |
2353 | | pcap_set_snaplen(pcap_t *p, int snaplen) |
2354 | 0 | { |
2355 | 0 | if (pcap_check_activated(p)) |
2356 | 0 | return (PCAP_ERROR_ACTIVATED); |
2357 | 0 | p->snapshot = snaplen; |
2358 | 0 | return (0); |
2359 | 0 | } |
2360 | | |
2361 | | int |
2362 | | pcap_set_promisc(pcap_t *p, int promisc) |
2363 | 0 | { |
2364 | 0 | if (pcap_check_activated(p)) |
2365 | 0 | return (PCAP_ERROR_ACTIVATED); |
2366 | 0 | p->opt.promisc = promisc; |
2367 | 0 | return (0); |
2368 | 0 | } |
2369 | | |
2370 | | int |
2371 | | pcap_set_rfmon(pcap_t *p, int rfmon) |
2372 | 0 | { |
2373 | 0 | if (pcap_check_activated(p)) |
2374 | 0 | return (PCAP_ERROR_ACTIVATED); |
2375 | 0 | p->opt.rfmon = rfmon; |
2376 | 0 | return (0); |
2377 | 0 | } |
2378 | | |
2379 | | int |
2380 | | pcap_set_timeout(pcap_t *p, int timeout_ms) |
2381 | 0 | { |
2382 | 0 | if (pcap_check_activated(p)) |
2383 | 0 | return (PCAP_ERROR_ACTIVATED); |
2384 | 0 | p->opt.timeout = timeout_ms; |
2385 | 0 | return (0); |
2386 | 0 | } |
2387 | | |
2388 | | int |
2389 | | pcap_set_tstamp_type(pcap_t *p, int tstamp_type) |
2390 | 0 | { |
2391 | 0 | int i; |
2392 | |
|
2393 | 0 | if (pcap_check_activated(p)) |
2394 | 0 | return (PCAP_ERROR_ACTIVATED); |
2395 | | |
2396 | | /* |
2397 | | * The argument should have been u_int, but that's too late |
2398 | | * to change now - it's an API. |
2399 | | */ |
2400 | 0 | if (tstamp_type < 0) |
2401 | 0 | return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP); |
2402 | | |
2403 | | /* |
2404 | | * If p->tstamp_type_count is 0, we only support PCAP_TSTAMP_HOST; |
2405 | | * the default time stamp type is PCAP_TSTAMP_HOST. |
2406 | | */ |
2407 | 0 | if (p->tstamp_type_count == 0) { |
2408 | 0 | if (tstamp_type == PCAP_TSTAMP_HOST) { |
2409 | 0 | p->opt.tstamp_type = tstamp_type; |
2410 | 0 | return (0); |
2411 | 0 | } |
2412 | 0 | } else { |
2413 | | /* |
2414 | | * Check whether we claim to support this type of time stamp. |
2415 | | */ |
2416 | 0 | for (i = 0; i < p->tstamp_type_count; i++) { |
2417 | 0 | if (p->tstamp_type_list[i] == (u_int)tstamp_type) { |
2418 | | /* |
2419 | | * Yes. |
2420 | | */ |
2421 | 0 | p->opt.tstamp_type = tstamp_type; |
2422 | 0 | return (0); |
2423 | 0 | } |
2424 | 0 | } |
2425 | 0 | } |
2426 | | |
2427 | | /* |
2428 | | * We don't support this type of time stamp. |
2429 | | */ |
2430 | 0 | return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP); |
2431 | 0 | } |
2432 | | |
2433 | | int |
2434 | | pcap_set_immediate_mode(pcap_t *p, int immediate) |
2435 | 0 | { |
2436 | 0 | if (pcap_check_activated(p)) |
2437 | 0 | return (PCAP_ERROR_ACTIVATED); |
2438 | 0 | p->opt.immediate = immediate; |
2439 | 0 | return (0); |
2440 | 0 | } |
2441 | | |
2442 | | int |
2443 | | pcap_set_buffer_size(pcap_t *p, int buffer_size) |
2444 | 0 | { |
2445 | 0 | if (pcap_check_activated(p)) |
2446 | 0 | return (PCAP_ERROR_ACTIVATED); |
2447 | 0 | if (buffer_size <= 0) { |
2448 | | /* |
2449 | | * Silently ignore invalid values. |
2450 | | */ |
2451 | 0 | return (0); |
2452 | 0 | } |
2453 | 0 | p->opt.buffer_size = buffer_size; |
2454 | 0 | return (0); |
2455 | 0 | } |
2456 | | |
2457 | | int |
2458 | | pcap_set_tstamp_precision(pcap_t *p, int tstamp_precision) |
2459 | 0 | { |
2460 | 0 | int i; |
2461 | |
|
2462 | 0 | if (pcap_check_activated(p)) |
2463 | 0 | return (PCAP_ERROR_ACTIVATED); |
2464 | | |
2465 | | /* |
2466 | | * The argument should have been u_int, but that's too late |
2467 | | * to change now - it's an API. |
2468 | | */ |
2469 | 0 | if (tstamp_precision < 0) |
2470 | 0 | return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP); |
2471 | | |
2472 | | /* |
2473 | | * If p->tstamp_precision_count is 0, we only support setting |
2474 | | * the time stamp precision to microsecond precision; every |
2475 | | * pcap module *MUST* support microsecond precision, even if |
2476 | | * it does so by converting the native precision to |
2477 | | * microseconds. |
2478 | | */ |
2479 | 0 | if (p->tstamp_precision_count == 0) { |
2480 | 0 | if (tstamp_precision == PCAP_TSTAMP_PRECISION_MICRO) { |
2481 | 0 | p->opt.tstamp_precision = tstamp_precision; |
2482 | 0 | return (0); |
2483 | 0 | } |
2484 | 0 | } else { |
2485 | | /* |
2486 | | * Check whether we claim to support this precision of |
2487 | | * time stamp. |
2488 | | */ |
2489 | 0 | for (i = 0; i < p->tstamp_precision_count; i++) { |
2490 | 0 | if (p->tstamp_precision_list[i] == (u_int)tstamp_precision) { |
2491 | | /* |
2492 | | * Yes. |
2493 | | */ |
2494 | 0 | p->opt.tstamp_precision = tstamp_precision; |
2495 | 0 | return (0); |
2496 | 0 | } |
2497 | 0 | } |
2498 | 0 | } |
2499 | | |
2500 | | /* |
2501 | | * We don't support this time stamp precision. |
2502 | | */ |
2503 | 0 | return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP); |
2504 | 0 | } |
2505 | | |
2506 | | int |
2507 | | pcap_get_tstamp_precision(pcap_t *p) |
2508 | 0 | { |
2509 | 0 | return (p->opt.tstamp_precision); |
2510 | 0 | } |
2511 | | |
2512 | | int |
2513 | | pcap_activate(pcap_t *p) |
2514 | 0 | { |
2515 | 0 | int status; |
2516 | | |
2517 | | /* |
2518 | | * Catch attempts to re-activate an already-activated |
2519 | | * pcap_t; this should, for example, catch code that |
2520 | | * calls pcap_open_live() followed by pcap_activate(), |
2521 | | * as some code that showed up in a Stack Exchange |
2522 | | * question did. |
2523 | | */ |
2524 | 0 | if (pcap_check_activated(p)) |
2525 | 0 | return (PCAP_ERROR_ACTIVATED); |
2526 | 0 | status = p->activate_op(p); |
2527 | 0 | if (status >= 0) { |
2528 | | /* |
2529 | | * If somebody requested non-blocking mode before |
2530 | | * calling pcap_activate(), turn it on now. |
2531 | | */ |
2532 | 0 | if (p->opt.nonblock) { |
2533 | 0 | status = p->setnonblock_op(p, 1); |
2534 | 0 | if (status < 0) { |
2535 | | /* |
2536 | | * Failed. Undo everything done by |
2537 | | * the activate operation. |
2538 | | */ |
2539 | 0 | p->cleanup_op(p); |
2540 | 0 | initialize_ops(p); |
2541 | 0 | return (status); |
2542 | 0 | } |
2543 | 0 | } |
2544 | 0 | p->activated = 1; |
2545 | 0 | } else { |
2546 | 0 | if (p->errbuf[0] == '\0') { |
2547 | | /* |
2548 | | * No error message supplied by the activate routine; |
2549 | | * for the benefit of programs that don't specially |
2550 | | * handle errors other than PCAP_ERROR, return the |
2551 | | * error message corresponding to the status. |
2552 | | */ |
2553 | 0 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s", |
2554 | 0 | pcap_statustostr(status)); |
2555 | 0 | } |
2556 | | |
2557 | | /* |
2558 | | * Undo any operation pointer setting, etc. done by |
2559 | | * the activate operation. |
2560 | | */ |
2561 | 0 | initialize_ops(p); |
2562 | 0 | } |
2563 | 0 | return (status); |
2564 | 0 | } |
2565 | | |
2566 | | pcap_t * |
2567 | | pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, char *errbuf) |
2568 | 0 | { |
2569 | 0 | pcap_t *p; |
2570 | 0 | int status; |
2571 | | #ifdef ENABLE_REMOTE |
2572 | | char host[PCAP_BUF_SIZE + 1]; |
2573 | | char port[PCAP_BUF_SIZE + 1]; |
2574 | | char name[PCAP_BUF_SIZE + 1]; |
2575 | | int srctype; |
2576 | | |
2577 | | /* |
2578 | | * A null device name is equivalent to the "any" device - |
2579 | | * which might not be supported on this platform, but |
2580 | | * this means that you'll get a "not supported" error |
2581 | | * rather than, say, a crash when we try to dereference |
2582 | | * the null pointer. |
2583 | | */ |
2584 | | if (device == NULL) |
2585 | | device = "any"; |
2586 | | |
2587 | | /* |
2588 | | * Retrofit - we have to make older applications compatible with |
2589 | | * remote capture. |
2590 | | * So we're calling pcap_open_remote() from here; this is a very |
2591 | | * dirty hack. |
2592 | | * Obviously, we cannot exploit all the new features; for instance, |
2593 | | * we cannot send authentication, we cannot use a UDP data connection, |
2594 | | * and so on. |
2595 | | */ |
2596 | | if (pcap_parsesrcstr(device, &srctype, host, port, name, errbuf)) |
2597 | | return (NULL); |
2598 | | |
2599 | | if (srctype == PCAP_SRC_IFREMOTE) { |
2600 | | /* |
2601 | | * Although we already have host, port and iface, we prefer |
2602 | | * to pass only 'device' to pcap_open_rpcap(), so that it has |
2603 | | * to call pcap_parsesrcstr() again. |
2604 | | * This is less optimized, but much clearer. |
2605 | | */ |
2606 | | return (pcap_open_rpcap(device, snaplen, |
2607 | | promisc ? PCAP_OPENFLAG_PROMISCUOUS : 0, to_ms, |
2608 | | NULL, errbuf)); |
2609 | | } |
2610 | | if (srctype == PCAP_SRC_FILE) { |
2611 | | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "unknown URL scheme \"file\""); |
2612 | | return (NULL); |
2613 | | } |
2614 | | if (srctype == PCAP_SRC_IFLOCAL) { |
2615 | | /* |
2616 | | * If it starts with rpcap://, that refers to a local device |
2617 | | * (no host part in the URL). Remove the rpcap://, and |
2618 | | * fall through to the regular open path. |
2619 | | */ |
2620 | | if (strncmp(device, PCAP_SRC_IF_STRING, strlen(PCAP_SRC_IF_STRING)) == 0) { |
2621 | | size_t len = strlen(device) - strlen(PCAP_SRC_IF_STRING) + 1; |
2622 | | |
2623 | | if (len > 0) |
2624 | | device += strlen(PCAP_SRC_IF_STRING); |
2625 | | } |
2626 | | } |
2627 | | #endif /* ENABLE_REMOTE */ |
2628 | |
|
2629 | 0 | p = pcap_create(device, errbuf); |
2630 | 0 | if (p == NULL) |
2631 | 0 | return (NULL); |
2632 | 0 | status = pcap_set_snaplen(p, snaplen); |
2633 | 0 | if (status < 0) |
2634 | 0 | goto fail; |
2635 | 0 | status = pcap_set_promisc(p, promisc); |
2636 | 0 | if (status < 0) |
2637 | 0 | goto fail; |
2638 | 0 | status = pcap_set_timeout(p, to_ms); |
2639 | 0 | if (status < 0) |
2640 | 0 | goto fail; |
2641 | | /* |
2642 | | * Mark this as opened with pcap_open_live(), so that, for |
2643 | | * example, we show the full list of DLT_ values, rather |
2644 | | * than just the ones that are compatible with capturing |
2645 | | * when not in monitor mode. That allows existing applications |
2646 | | * to work the way they used to work, but allows new applications |
2647 | | * that know about the new open API to, for example, find out the |
2648 | | * DLT_ values that they can select without changing whether |
2649 | | * the adapter is in monitor mode or not. |
2650 | | */ |
2651 | 0 | p->oldstyle = 1; |
2652 | 0 | status = pcap_activate(p); |
2653 | 0 | if (status < 0) |
2654 | 0 | goto fail; |
2655 | 0 | return (p); |
2656 | 0 | fail: |
2657 | 0 | if (status == PCAP_ERROR) |
2658 | 0 | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %.*s", device, |
2659 | 0 | PCAP_ERRBUF_SIZE - 3, p->errbuf); |
2660 | 0 | else if (status == PCAP_ERROR_NO_SUCH_DEVICE || |
2661 | 0 | status == PCAP_ERROR_PERM_DENIED || |
2662 | 0 | status == PCAP_ERROR_PROMISC_PERM_DENIED) |
2663 | 0 | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%.*s)", device, |
2664 | 0 | pcap_statustostr(status), PCAP_ERRBUF_SIZE - 6, p->errbuf); |
2665 | 0 | else |
2666 | 0 | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", device, |
2667 | 0 | pcap_statustostr(status)); |
2668 | 0 | pcap_close(p); |
2669 | 0 | return (NULL); |
2670 | 0 | } |
2671 | | |
2672 | | pcap_t * |
2673 | | pcap_open_offline_common(char *ebuf, size_t size) |
2674 | 11.9k | { |
2675 | 11.9k | pcap_t *p; |
2676 | | |
2677 | 11.9k | p = pcap_alloc_pcap_t(ebuf, size); |
2678 | 11.9k | if (p == NULL) |
2679 | 0 | return (NULL); |
2680 | | |
2681 | 11.9k | p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO; |
2682 | | |
2683 | 11.9k | return (p); |
2684 | 11.9k | } |
2685 | | |
2686 | | int |
2687 | | pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user) |
2688 | 15.7k | { |
2689 | 15.7k | return (p->read_op(p, cnt, callback, user)); |
2690 | 15.7k | } |
2691 | | |
2692 | | int |
2693 | | pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user) |
2694 | 0 | { |
2695 | 0 | register int n; |
2696 | |
|
2697 | 0 | for (;;) { |
2698 | 0 | if (p->rfile != NULL) { |
2699 | | /* |
2700 | | * 0 means EOF, so don't loop if we get 0. |
2701 | | */ |
2702 | 0 | n = pcap_offline_read(p, cnt, callback, user); |
2703 | 0 | } else { |
2704 | | /* |
2705 | | * XXX keep reading until we get something |
2706 | | * (or an error occurs) |
2707 | | */ |
2708 | 0 | do { |
2709 | 0 | n = p->read_op(p, cnt, callback, user); |
2710 | 0 | } while (n == 0); |
2711 | 0 | } |
2712 | 0 | if (n <= 0) |
2713 | 0 | return (n); |
2714 | 0 | if (!PACKET_COUNT_IS_UNLIMITED(cnt)) { |
2715 | 0 | cnt -= n; |
2716 | 0 | if (cnt <= 0) |
2717 | 0 | return (0); |
2718 | 0 | } |
2719 | 0 | } |
2720 | 0 | } |
2721 | | |
2722 | | /* |
2723 | | * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate. |
2724 | | */ |
2725 | | void |
2726 | | pcap_breakloop(pcap_t *p) |
2727 | 0 | { |
2728 | 0 | p->break_loop = 1; |
2729 | 0 | } |
2730 | | |
2731 | | int |
2732 | | pcap_datalink(pcap_t *p) |
2733 | 173k | { |
2734 | 173k | if (!p->activated) |
2735 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
2736 | 173k | return (p->linktype); |
2737 | 173k | } |
2738 | | |
2739 | | int |
2740 | | pcap_datalink_ext(pcap_t *p) |
2741 | 0 | { |
2742 | 0 | if (!p->activated) |
2743 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
2744 | 0 | return (p->linktype_ext); |
2745 | 0 | } |
2746 | | |
2747 | | int |
2748 | | pcap_list_datalinks(pcap_t *p, int **dlt_buffer) |
2749 | 0 | { |
2750 | 0 | if (!p->activated) |
2751 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
2752 | 0 | if (p->dlt_count == 0) { |
2753 | | /* |
2754 | | * We couldn't fetch the list of DLTs, which means |
2755 | | * this platform doesn't support changing the |
2756 | | * DLT for an interface. Return a list of DLTs |
2757 | | * containing only the DLT this device supports. |
2758 | | */ |
2759 | 0 | *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer)); |
2760 | 0 | if (*dlt_buffer == NULL) { |
2761 | 0 | pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), |
2762 | 0 | errno, "malloc"); |
2763 | 0 | return (PCAP_ERROR); |
2764 | 0 | } |
2765 | 0 | **dlt_buffer = p->linktype; |
2766 | 0 | return (1); |
2767 | 0 | } else { |
2768 | 0 | *dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count); |
2769 | 0 | if (*dlt_buffer == NULL) { |
2770 | 0 | pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), |
2771 | 0 | errno, "malloc"); |
2772 | 0 | return (PCAP_ERROR); |
2773 | 0 | } |
2774 | 0 | (void)memcpy(*dlt_buffer, p->dlt_list, |
2775 | 0 | sizeof(**dlt_buffer) * p->dlt_count); |
2776 | 0 | return (p->dlt_count); |
2777 | 0 | } |
2778 | 0 | } |
2779 | | |
2780 | | /* |
2781 | | * In Windows, you might have a library built with one version of the |
2782 | | * C runtime library and an application built with another version of |
2783 | | * the C runtime library, which means that the library might use one |
2784 | | * version of malloc() and free() and the application might use another |
2785 | | * version of malloc() and free(). If so, that means something |
2786 | | * allocated by the library cannot be freed by the application, so we |
2787 | | * need to have a pcap_free_datalinks() routine to free up the list |
2788 | | * allocated by pcap_list_datalinks(), even though it's just a wrapper |
2789 | | * around free(). |
2790 | | */ |
2791 | | void |
2792 | | pcap_free_datalinks(int *dlt_list) |
2793 | 0 | { |
2794 | 0 | free(dlt_list); |
2795 | 0 | } |
2796 | | |
2797 | | int |
2798 | | pcap_set_datalink(pcap_t *p, int dlt) |
2799 | 0 | { |
2800 | 0 | int i; |
2801 | 0 | const char *dlt_name; |
2802 | |
|
2803 | 0 | if (dlt < 0) |
2804 | 0 | goto unsupported; |
2805 | | |
2806 | 0 | if (p->dlt_count == 0 || p->set_datalink_op == NULL) { |
2807 | | /* |
2808 | | * We couldn't fetch the list of DLTs, or we don't |
2809 | | * have a "set datalink" operation, which means |
2810 | | * this platform doesn't support changing the |
2811 | | * DLT for an interface. Check whether the new |
2812 | | * DLT is the one this interface supports. |
2813 | | */ |
2814 | 0 | if (p->linktype != dlt) |
2815 | 0 | goto unsupported; |
2816 | | |
2817 | | /* |
2818 | | * It is, so there's nothing we need to do here. |
2819 | | */ |
2820 | 0 | return (0); |
2821 | 0 | } |
2822 | 0 | for (i = 0; i < p->dlt_count; i++) |
2823 | 0 | if (p->dlt_list[i] == (u_int)dlt) |
2824 | 0 | break; |
2825 | 0 | if (i >= p->dlt_count) |
2826 | 0 | goto unsupported; |
2827 | 0 | if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB && |
2828 | 0 | dlt == DLT_DOCSIS) { |
2829 | | /* |
2830 | | * This is presumably an Ethernet device, as the first |
2831 | | * link-layer type it offers is DLT_EN10MB, and the only |
2832 | | * other type it offers is DLT_DOCSIS. That means that |
2833 | | * we can't tell the driver to supply DOCSIS link-layer |
2834 | | * headers - we're just pretending that's what we're |
2835 | | * getting, as, presumably, we're capturing on a dedicated |
2836 | | * link to a Cisco Cable Modem Termination System, and |
2837 | | * it's putting raw DOCSIS frames on the wire inside low-level |
2838 | | * Ethernet framing. |
2839 | | */ |
2840 | 0 | p->linktype = dlt; |
2841 | 0 | return (0); |
2842 | 0 | } |
2843 | 0 | if (p->set_datalink_op(p, dlt) == -1) |
2844 | 0 | return (-1); |
2845 | 0 | p->linktype = dlt; |
2846 | 0 | return (0); |
2847 | | |
2848 | 0 | unsupported: |
2849 | 0 | dlt_name = pcap_datalink_val_to_name(dlt); |
2850 | 0 | if (dlt_name != NULL) { |
2851 | 0 | (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf), |
2852 | 0 | "%s is not one of the DLTs supported by this device", |
2853 | 0 | dlt_name); |
2854 | 0 | } else { |
2855 | 0 | (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf), |
2856 | 0 | "DLT %d is not one of the DLTs supported by this device", |
2857 | 0 | dlt); |
2858 | 0 | } |
2859 | 0 | return (-1); |
2860 | 0 | } |
2861 | | |
2862 | | /* |
2863 | | * This array is designed for mapping upper and lower case letter |
2864 | | * together for a case independent comparison. The mappings are |
2865 | | * based upon ascii character sequences. |
2866 | | */ |
2867 | | static const u_char charmap[] = { |
2868 | | (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003', |
2869 | | (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007', |
2870 | | (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013', |
2871 | | (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017', |
2872 | | (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023', |
2873 | | (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027', |
2874 | | (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033', |
2875 | | (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037', |
2876 | | (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043', |
2877 | | (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047', |
2878 | | (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053', |
2879 | | (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057', |
2880 | | (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063', |
2881 | | (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067', |
2882 | | (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073', |
2883 | | (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077', |
2884 | | (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143', |
2885 | | (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147', |
2886 | | (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153', |
2887 | | (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157', |
2888 | | (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163', |
2889 | | (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167', |
2890 | | (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133', |
2891 | | (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137', |
2892 | | (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143', |
2893 | | (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147', |
2894 | | (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153', |
2895 | | (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157', |
2896 | | (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163', |
2897 | | (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167', |
2898 | | (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173', |
2899 | | (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177', |
2900 | | (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203', |
2901 | | (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207', |
2902 | | (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213', |
2903 | | (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217', |
2904 | | (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223', |
2905 | | (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227', |
2906 | | (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233', |
2907 | | (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237', |
2908 | | (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243', |
2909 | | (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247', |
2910 | | (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253', |
2911 | | (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257', |
2912 | | (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263', |
2913 | | (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267', |
2914 | | (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273', |
2915 | | (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277', |
2916 | | (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343', |
2917 | | (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347', |
2918 | | (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353', |
2919 | | (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357', |
2920 | | (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363', |
2921 | | (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367', |
2922 | | (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333', |
2923 | | (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337', |
2924 | | (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343', |
2925 | | (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347', |
2926 | | (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353', |
2927 | | (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357', |
2928 | | (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363', |
2929 | | (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367', |
2930 | | (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373', |
2931 | | (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377', |
2932 | | }; |
2933 | | |
2934 | | int |
2935 | | pcap_strcasecmp(const char *s1, const char *s2) |
2936 | 0 | { |
2937 | 0 | register const u_char *cm = charmap, |
2938 | 0 | *us1 = (const u_char *)s1, |
2939 | 0 | *us2 = (const u_char *)s2; |
2940 | |
|
2941 | 0 | while (cm[*us1] == cm[*us2++]) |
2942 | 0 | if (*us1++ == '\0') |
2943 | 0 | return(0); |
2944 | 0 | return (cm[*us1] - cm[*--us2]); |
2945 | 0 | } |
2946 | | |
2947 | | struct dlt_choice { |
2948 | | const char *name; |
2949 | | const char *description; |
2950 | | int dlt; |
2951 | | }; |
2952 | | |
2953 | | #define DLT_CHOICE(code, description) { #code, description, DLT_ ## code } |
2954 | | #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 } |
2955 | | |
2956 | | static struct dlt_choice dlt_choices[] = { |
2957 | | DLT_CHOICE(NULL, "BSD loopback"), |
2958 | | DLT_CHOICE(EN10MB, "Ethernet"), |
2959 | | DLT_CHOICE(IEEE802, "Token ring"), |
2960 | | DLT_CHOICE(ARCNET, "BSD ARCNET"), |
2961 | | DLT_CHOICE(SLIP, "SLIP"), |
2962 | | DLT_CHOICE(PPP, "PPP"), |
2963 | | DLT_CHOICE(FDDI, "FDDI"), |
2964 | | DLT_CHOICE(ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"), |
2965 | | DLT_CHOICE(RAW, "Raw IP"), |
2966 | | DLT_CHOICE(SLIP_BSDOS, "BSD/OS SLIP"), |
2967 | | DLT_CHOICE(PPP_BSDOS, "BSD/OS PPP"), |
2968 | | DLT_CHOICE(ATM_CLIP, "Linux Classical IP-over-ATM"), |
2969 | | DLT_CHOICE(PPP_SERIAL, "PPP over serial"), |
2970 | | DLT_CHOICE(PPP_ETHER, "PPPoE"), |
2971 | | DLT_CHOICE(SYMANTEC_FIREWALL, "Symantec Firewall"), |
2972 | | DLT_CHOICE(C_HDLC, "Cisco HDLC"), |
2973 | | DLT_CHOICE(IEEE802_11, "802.11"), |
2974 | | DLT_CHOICE(FRELAY, "Frame Relay"), |
2975 | | DLT_CHOICE(LOOP, "OpenBSD loopback"), |
2976 | | DLT_CHOICE(ENC, "OpenBSD encapsulated IP"), |
2977 | | DLT_CHOICE(LINUX_SLL, "Linux cooked v1"), |
2978 | | DLT_CHOICE(LTALK, "Localtalk"), |
2979 | | DLT_CHOICE(PFLOG, "OpenBSD pflog file"), |
2980 | | DLT_CHOICE(PFSYNC, "Packet filter state syncing"), |
2981 | | DLT_CHOICE(PRISM_HEADER, "802.11 plus Prism header"), |
2982 | | DLT_CHOICE(IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"), |
2983 | | DLT_CHOICE(SUNATM, "Sun raw ATM"), |
2984 | | DLT_CHOICE(IEEE802_11_RADIO, "802.11 plus radiotap header"), |
2985 | | DLT_CHOICE(ARCNET_LINUX, "Linux ARCNET"), |
2986 | | DLT_CHOICE(JUNIPER_MLPPP, "Juniper Multi-Link PPP"), |
2987 | | DLT_CHOICE(JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"), |
2988 | | DLT_CHOICE(JUNIPER_ES, "Juniper Encryption Services PIC"), |
2989 | | DLT_CHOICE(JUNIPER_GGSN, "Juniper GGSN PIC"), |
2990 | | DLT_CHOICE(JUNIPER_MFR, "Juniper FRF.16 Frame Relay"), |
2991 | | DLT_CHOICE(JUNIPER_ATM2, "Juniper ATM2 PIC"), |
2992 | | DLT_CHOICE(JUNIPER_SERVICES, "Juniper Advanced Services PIC"), |
2993 | | DLT_CHOICE(JUNIPER_ATM1, "Juniper ATM1 PIC"), |
2994 | | DLT_CHOICE(APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"), |
2995 | | DLT_CHOICE(MTP2_WITH_PHDR, "SS7 MTP2 with Pseudo-header"), |
2996 | | DLT_CHOICE(MTP2, "SS7 MTP2"), |
2997 | | DLT_CHOICE(MTP3, "SS7 MTP3"), |
2998 | | DLT_CHOICE(SCCP, "SS7 SCCP"), |
2999 | | DLT_CHOICE(DOCSIS, "DOCSIS"), |
3000 | | DLT_CHOICE(LINUX_IRDA, "Linux IrDA"), |
3001 | | DLT_CHOICE(IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"), |
3002 | | DLT_CHOICE(JUNIPER_MONITOR, "Juniper Passive Monitor PIC"), |
3003 | | DLT_CHOICE(BACNET_MS_TP, "BACnet MS/TP"), |
3004 | | DLT_CHOICE(PPP_PPPD, "PPP for pppd, with direction flag"), |
3005 | | DLT_CHOICE(JUNIPER_PPPOE, "Juniper PPPoE"), |
3006 | | DLT_CHOICE(JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"), |
3007 | | DLT_CHOICE(GPRS_LLC, "GPRS LLC"), |
3008 | | DLT_CHOICE(GPF_T, "GPF-T"), |
3009 | | DLT_CHOICE(GPF_F, "GPF-F"), |
3010 | | DLT_CHOICE(JUNIPER_PIC_PEER, "Juniper PIC Peer"), |
3011 | | DLT_CHOICE(ERF_ETH, "Ethernet with Endace ERF header"), |
3012 | | DLT_CHOICE(ERF_POS, "Packet-over-SONET with Endace ERF header"), |
3013 | | DLT_CHOICE(LINUX_LAPD, "Linux vISDN LAPD"), |
3014 | | DLT_CHOICE(JUNIPER_ETHER, "Juniper Ethernet"), |
3015 | | DLT_CHOICE(JUNIPER_PPP, "Juniper PPP"), |
3016 | | DLT_CHOICE(JUNIPER_FRELAY, "Juniper Frame Relay"), |
3017 | | DLT_CHOICE(JUNIPER_CHDLC, "Juniper C-HDLC"), |
3018 | | DLT_CHOICE(MFR, "FRF.16 Frame Relay"), |
3019 | | DLT_CHOICE(JUNIPER_VP, "Juniper Voice PIC"), |
3020 | | DLT_CHOICE(A429, "Arinc 429"), |
3021 | | DLT_CHOICE(A653_ICM, "Arinc 653 Interpartition Communication"), |
3022 | | DLT_CHOICE(USB_FREEBSD, "USB with FreeBSD header"), |
3023 | | DLT_CHOICE(BLUETOOTH_HCI_H4, "Bluetooth HCI UART transport layer"), |
3024 | | DLT_CHOICE(IEEE802_16_MAC_CPS, "IEEE 802.16 MAC Common Part Sublayer"), |
3025 | | DLT_CHOICE(USB_LINUX, "USB with Linux header"), |
3026 | | DLT_CHOICE(CAN20B, "Controller Area Network (CAN) v. 2.0B"), |
3027 | | DLT_CHOICE(IEEE802_15_4_LINUX, "IEEE 802.15.4 with Linux padding"), |
3028 | | DLT_CHOICE(PPI, "Per-Packet Information"), |
3029 | | DLT_CHOICE(IEEE802_16_MAC_CPS_RADIO, "IEEE 802.16 MAC Common Part Sublayer plus radiotap header"), |
3030 | | DLT_CHOICE(JUNIPER_ISM, "Juniper Integrated Service Module"), |
3031 | | DLT_CHOICE(IEEE802_15_4, "IEEE 802.15.4 with FCS"), |
3032 | | DLT_CHOICE(SITA, "SITA pseudo-header"), |
3033 | | DLT_CHOICE(ERF, "Endace ERF header"), |
3034 | | DLT_CHOICE(RAIF1, "Ethernet with u10 Networks pseudo-header"), |
3035 | | DLT_CHOICE(IPMB_KONTRON, "IPMB with Kontron pseudo-header"), |
3036 | | DLT_CHOICE(JUNIPER_ST, "Juniper Secure Tunnel"), |
3037 | | DLT_CHOICE(BLUETOOTH_HCI_H4_WITH_PHDR, "Bluetooth HCI UART transport layer plus pseudo-header"), |
3038 | | DLT_CHOICE(AX25_KISS, "AX.25 with KISS header"), |
3039 | | DLT_CHOICE(IPMB_LINUX, "IPMB with Linux/Pigeon Point pseudo-header"), |
3040 | | DLT_CHOICE(IEEE802_15_4_NONASK_PHY, "IEEE 802.15.4 with non-ASK PHY data"), |
3041 | | DLT_CHOICE(MPLS, "MPLS with label as link-layer header"), |
3042 | | DLT_CHOICE(LINUX_EVDEV, "Linux evdev events"), |
3043 | | DLT_CHOICE(USB_LINUX_MMAPPED, "USB with padded Linux header"), |
3044 | | DLT_CHOICE(DECT, "DECT"), |
3045 | | DLT_CHOICE(AOS, "AOS Space Data Link protocol"), |
3046 | | DLT_CHOICE(WIHART, "Wireless HART"), |
3047 | | DLT_CHOICE(FC_2, "Fibre Channel FC-2"), |
3048 | | DLT_CHOICE(FC_2_WITH_FRAME_DELIMS, "Fibre Channel FC-2 with frame delimiters"), |
3049 | | DLT_CHOICE(IPNET, "Solaris ipnet"), |
3050 | | DLT_CHOICE(CAN_SOCKETCAN, "CAN-bus with SocketCAN headers"), |
3051 | | DLT_CHOICE(IPV4, "Raw IPv4"), |
3052 | | DLT_CHOICE(IPV6, "Raw IPv6"), |
3053 | | DLT_CHOICE(IEEE802_15_4_NOFCS, "IEEE 802.15.4 without FCS"), |
3054 | | DLT_CHOICE(DBUS, "D-Bus"), |
3055 | | DLT_CHOICE(JUNIPER_VS, "Juniper Virtual Server"), |
3056 | | DLT_CHOICE(JUNIPER_SRX_E2E, "Juniper SRX E2E"), |
3057 | | DLT_CHOICE(JUNIPER_FIBRECHANNEL, "Juniper Fibre Channel"), |
3058 | | DLT_CHOICE(DVB_CI, "DVB-CI"), |
3059 | | DLT_CHOICE(MUX27010, "MUX27010"), |
3060 | | DLT_CHOICE(STANAG_5066_D_PDU, "STANAG 5066 D_PDUs"), |
3061 | | DLT_CHOICE(JUNIPER_ATM_CEMIC, "Juniper ATM CEMIC"), |
3062 | | DLT_CHOICE(NFLOG, "Linux netfilter log messages"), |
3063 | | DLT_CHOICE(NETANALYZER, "Ethernet with Hilscher netANALYZER pseudo-header"), |
3064 | | DLT_CHOICE(NETANALYZER_TRANSPARENT, "Ethernet with Hilscher netANALYZER pseudo-header and with preamble and SFD"), |
3065 | | DLT_CHOICE(IPOIB, "RFC 4391 IP-over-Infiniband"), |
3066 | | DLT_CHOICE(MPEG_2_TS, "MPEG-2 transport stream"), |
3067 | | DLT_CHOICE(NG40, "ng40 protocol tester Iub/Iur"), |
3068 | | DLT_CHOICE(NFC_LLCP, "NFC LLCP PDUs with pseudo-header"), |
3069 | | DLT_CHOICE(INFINIBAND, "InfiniBand"), |
3070 | | DLT_CHOICE(SCTP, "SCTP"), |
3071 | | DLT_CHOICE(USBPCAP, "USB with USBPcap header"), |
3072 | | DLT_CHOICE(RTAC_SERIAL, "Schweitzer Engineering Laboratories RTAC packets"), |
3073 | | DLT_CHOICE(BLUETOOTH_LE_LL, "Bluetooth Low Energy air interface"), |
3074 | | DLT_CHOICE(NETLINK, "Linux netlink"), |
3075 | | DLT_CHOICE(BLUETOOTH_LINUX_MONITOR, "Bluetooth Linux Monitor"), |
3076 | | DLT_CHOICE(BLUETOOTH_BREDR_BB, "Bluetooth Basic Rate/Enhanced Data Rate baseband packets"), |
3077 | | DLT_CHOICE(BLUETOOTH_LE_LL_WITH_PHDR, "Bluetooth Low Energy air interface with pseudo-header"), |
3078 | | DLT_CHOICE(PROFIBUS_DL, "PROFIBUS data link layer"), |
3079 | | DLT_CHOICE(PKTAP, "Apple DLT_PKTAP"), |
3080 | | DLT_CHOICE(EPON, "Ethernet with 802.3 Clause 65 EPON preamble"), |
3081 | | DLT_CHOICE(IPMI_HPM_2, "IPMI trace packets"), |
3082 | | DLT_CHOICE(ZWAVE_R1_R2, "Z-Wave RF profile R1 and R2 packets"), |
3083 | | DLT_CHOICE(ZWAVE_R3, "Z-Wave RF profile R3 packets"), |
3084 | | DLT_CHOICE(WATTSTOPPER_DLM, "WattStopper Digital Lighting Management (DLM) and Legrand Nitoo Open protocol"), |
3085 | | DLT_CHOICE(ISO_14443, "ISO 14443 messages"), |
3086 | | DLT_CHOICE(RDS, "IEC 62106 Radio Data System groups"), |
3087 | | DLT_CHOICE(USB_DARWIN, "USB with Darwin header"), |
3088 | | DLT_CHOICE(OPENFLOW, "OpenBSD DLT_OPENFLOW"), |
3089 | | DLT_CHOICE(SDLC, "IBM SDLC frames"), |
3090 | | DLT_CHOICE(TI_LLN_SNIFFER, "TI LLN sniffer frames"), |
3091 | | DLT_CHOICE(VSOCK, "Linux vsock"), |
3092 | | DLT_CHOICE(NORDIC_BLE, "Nordic Semiconductor Bluetooth LE sniffer frames"), |
3093 | | DLT_CHOICE(DOCSIS31_XRA31, "Excentis XRA-31 DOCSIS 3.1 RF sniffer frames"), |
3094 | | DLT_CHOICE(ETHERNET_MPACKET, "802.3br mPackets"), |
3095 | | DLT_CHOICE(DISPLAYPORT_AUX, "DisplayPort AUX channel monitoring data"), |
3096 | | DLT_CHOICE(LINUX_SLL2, "Linux cooked v2"), |
3097 | | DLT_CHOICE_SENTINEL |
3098 | | }; |
3099 | | |
3100 | | int |
3101 | | pcap_datalink_name_to_val(const char *name) |
3102 | 0 | { |
3103 | 0 | int i; |
3104 | |
|
3105 | 0 | for (i = 0; dlt_choices[i].name != NULL; i++) { |
3106 | 0 | if (pcap_strcasecmp(dlt_choices[i].name, name) == 0) |
3107 | 0 | return (dlt_choices[i].dlt); |
3108 | 0 | } |
3109 | 0 | return (-1); |
3110 | 0 | } |
3111 | | |
3112 | | const char * |
3113 | | pcap_datalink_val_to_name(int dlt) |
3114 | 0 | { |
3115 | 0 | int i; |
3116 | |
|
3117 | 0 | for (i = 0; dlt_choices[i].name != NULL; i++) { |
3118 | 0 | if (dlt_choices[i].dlt == dlt) |
3119 | 0 | return (dlt_choices[i].name); |
3120 | 0 | } |
3121 | 0 | return (NULL); |
3122 | 0 | } |
3123 | | |
3124 | | const char * |
3125 | | pcap_datalink_val_to_description(int dlt) |
3126 | 0 | { |
3127 | 0 | int i; |
3128 | |
|
3129 | 0 | for (i = 0; dlt_choices[i].name != NULL; i++) { |
3130 | 0 | if (dlt_choices[i].dlt == dlt) |
3131 | 0 | return (dlt_choices[i].description); |
3132 | 0 | } |
3133 | 0 | return (NULL); |
3134 | 0 | } |
3135 | | |
3136 | | const char * |
3137 | | pcap_datalink_val_to_description_or_dlt(int dlt) |
3138 | 0 | { |
3139 | 0 | static char unkbuf[40]; |
3140 | 0 | const char *description; |
3141 | |
|
3142 | 0 | description = pcap_datalink_val_to_description(dlt); |
3143 | 0 | if (description != NULL) { |
3144 | 0 | return description; |
3145 | 0 | } else { |
3146 | 0 | (void)pcap_snprintf(unkbuf, sizeof(unkbuf), "DLT %u", dlt); |
3147 | 0 | return unkbuf; |
3148 | 0 | } |
3149 | 0 | } |
3150 | | |
3151 | | struct tstamp_type_choice { |
3152 | | const char *name; |
3153 | | const char *description; |
3154 | | int type; |
3155 | | }; |
3156 | | |
3157 | | static struct tstamp_type_choice tstamp_type_choices[] = { |
3158 | | { "host", "Host", PCAP_TSTAMP_HOST }, |
3159 | | { "host_lowprec", "Host, low precision", PCAP_TSTAMP_HOST_LOWPREC }, |
3160 | | { "host_hiprec", "Host, high precision", PCAP_TSTAMP_HOST_HIPREC }, |
3161 | | { "adapter", "Adapter", PCAP_TSTAMP_ADAPTER }, |
3162 | | { "adapter_unsynced", "Adapter, not synced with system time", PCAP_TSTAMP_ADAPTER_UNSYNCED }, |
3163 | | { NULL, NULL, 0 } |
3164 | | }; |
3165 | | |
3166 | | int |
3167 | | pcap_tstamp_type_name_to_val(const char *name) |
3168 | 0 | { |
3169 | 0 | int i; |
3170 | |
|
3171 | 0 | for (i = 0; tstamp_type_choices[i].name != NULL; i++) { |
3172 | 0 | if (pcap_strcasecmp(tstamp_type_choices[i].name, name) == 0) |
3173 | 0 | return (tstamp_type_choices[i].type); |
3174 | 0 | } |
3175 | 0 | return (PCAP_ERROR); |
3176 | 0 | } |
3177 | | |
3178 | | const char * |
3179 | | pcap_tstamp_type_val_to_name(int tstamp_type) |
3180 | 0 | { |
3181 | 0 | int i; |
3182 | |
|
3183 | 0 | for (i = 0; tstamp_type_choices[i].name != NULL; i++) { |
3184 | 0 | if (tstamp_type_choices[i].type == tstamp_type) |
3185 | 0 | return (tstamp_type_choices[i].name); |
3186 | 0 | } |
3187 | 0 | return (NULL); |
3188 | 0 | } |
3189 | | |
3190 | | const char * |
3191 | | pcap_tstamp_type_val_to_description(int tstamp_type) |
3192 | 0 | { |
3193 | 0 | int i; |
3194 | |
|
3195 | 0 | for (i = 0; tstamp_type_choices[i].name != NULL; i++) { |
3196 | 0 | if (tstamp_type_choices[i].type == tstamp_type) |
3197 | 0 | return (tstamp_type_choices[i].description); |
3198 | 0 | } |
3199 | 0 | return (NULL); |
3200 | 0 | } |
3201 | | |
3202 | | int |
3203 | | pcap_snapshot(pcap_t *p) |
3204 | 0 | { |
3205 | 0 | if (!p->activated) |
3206 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
3207 | 0 | return (p->snapshot); |
3208 | 0 | } |
3209 | | |
3210 | | int |
3211 | | pcap_is_swapped(pcap_t *p) |
3212 | 0 | { |
3213 | 0 | if (!p->activated) |
3214 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
3215 | 0 | return (p->swapped); |
3216 | 0 | } |
3217 | | |
3218 | | int |
3219 | | pcap_major_version(pcap_t *p) |
3220 | 0 | { |
3221 | 0 | if (!p->activated) |
3222 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
3223 | 0 | return (p->version_major); |
3224 | 0 | } |
3225 | | |
3226 | | int |
3227 | | pcap_minor_version(pcap_t *p) |
3228 | 0 | { |
3229 | 0 | if (!p->activated) |
3230 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
3231 | 0 | return (p->version_minor); |
3232 | 0 | } |
3233 | | |
3234 | | int |
3235 | | pcap_bufsize(pcap_t *p) |
3236 | 0 | { |
3237 | 0 | if (!p->activated) |
3238 | 0 | return (PCAP_ERROR_NOT_ACTIVATED); |
3239 | 0 | return (p->bufsize); |
3240 | 0 | } |
3241 | | |
3242 | | FILE * |
3243 | | pcap_file(pcap_t *p) |
3244 | 0 | { |
3245 | 0 | return (p->rfile); |
3246 | 0 | } |
3247 | | |
3248 | | int |
3249 | | pcap_fileno(pcap_t *p) |
3250 | 0 | { |
3251 | 0 | #ifndef _WIN32 |
3252 | 0 | return (p->fd); |
3253 | | #else |
3254 | | if (p->handle != INVALID_HANDLE_VALUE) |
3255 | | return ((int)(DWORD)p->handle); |
3256 | | else |
3257 | | return (PCAP_ERROR); |
3258 | | #endif |
3259 | 0 | } |
3260 | | |
3261 | | #if !defined(_WIN32) && !defined(MSDOS) |
3262 | | int |
3263 | | pcap_get_selectable_fd(pcap_t *p) |
3264 | 0 | { |
3265 | 0 | return (p->selectable_fd); |
3266 | 0 | } |
3267 | | |
3268 | | struct timeval * |
3269 | | pcap_get_required_select_timeout(pcap_t *p) |
3270 | 0 | { |
3271 | 0 | return (p->required_select_timeout); |
3272 | 0 | } |
3273 | | #endif |
3274 | | |
3275 | | void |
3276 | | pcap_perror(pcap_t *p, const char *prefix) |
3277 | 0 | { |
3278 | 0 | fprintf(stderr, "%s: %s\n", prefix, p->errbuf); |
3279 | 0 | } |
3280 | | |
3281 | | char * |
3282 | | pcap_geterr(pcap_t *p) |
3283 | 3.14k | { |
3284 | 3.14k | return (p->errbuf); |
3285 | 3.14k | } |
3286 | | |
3287 | | int |
3288 | | pcap_getnonblock(pcap_t *p, char *errbuf) |
3289 | 0 | { |
3290 | 0 | int ret; |
3291 | |
|
3292 | 0 | ret = p->getnonblock_op(p); |
3293 | 0 | if (ret == -1) { |
3294 | | /* |
3295 | | * The get nonblock operation sets p->errbuf; this |
3296 | | * function *shouldn't* have had a separate errbuf |
3297 | | * argument, as it didn't need one, but I goofed |
3298 | | * when adding it. |
3299 | | * |
3300 | | * We copy the error message to errbuf, so callers |
3301 | | * can find it in either place. |
3302 | | */ |
3303 | 0 | pcap_strlcpy(errbuf, p->errbuf, PCAP_ERRBUF_SIZE); |
3304 | 0 | } |
3305 | 0 | return (ret); |
3306 | 0 | } |
3307 | | |
3308 | | /* |
3309 | | * Get the current non-blocking mode setting, under the assumption that |
3310 | | * it's just the standard POSIX non-blocking flag. |
3311 | | */ |
3312 | | #if !defined(_WIN32) && !defined(MSDOS) |
3313 | | int |
3314 | | pcap_getnonblock_fd(pcap_t *p) |
3315 | 0 | { |
3316 | 0 | int fdflags; |
3317 | |
|
3318 | 0 | fdflags = fcntl(p->fd, F_GETFL, 0); |
3319 | 0 | if (fdflags == -1) { |
3320 | 0 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
3321 | 0 | errno, "F_GETFL"); |
3322 | 0 | return (-1); |
3323 | 0 | } |
3324 | 0 | if (fdflags & O_NONBLOCK) |
3325 | 0 | return (1); |
3326 | 0 | else |
3327 | 0 | return (0); |
3328 | 0 | } |
3329 | | #endif |
3330 | | |
3331 | | int |
3332 | | pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf) |
3333 | 0 | { |
3334 | 0 | int ret; |
3335 | |
|
3336 | 0 | ret = p->setnonblock_op(p, nonblock); |
3337 | 0 | if (ret == -1) { |
3338 | | /* |
3339 | | * The set nonblock operation sets p->errbuf; this |
3340 | | * function *shouldn't* have had a separate errbuf |
3341 | | * argument, as it didn't need one, but I goofed |
3342 | | * when adding it. |
3343 | | * |
3344 | | * We copy the error message to errbuf, so callers |
3345 | | * can find it in either place. |
3346 | | */ |
3347 | 0 | pcap_strlcpy(errbuf, p->errbuf, PCAP_ERRBUF_SIZE); |
3348 | 0 | } |
3349 | 0 | return (ret); |
3350 | 0 | } |
3351 | | |
3352 | | #if !defined(_WIN32) && !defined(MSDOS) |
3353 | | /* |
3354 | | * Set non-blocking mode, under the assumption that it's just the |
3355 | | * standard POSIX non-blocking flag. (This can be called by the |
3356 | | * per-platform non-blocking-mode routine if that routine also |
3357 | | * needs to do some additional work.) |
3358 | | */ |
3359 | | int |
3360 | | pcap_setnonblock_fd(pcap_t *p, int nonblock) |
3361 | 0 | { |
3362 | 0 | int fdflags; |
3363 | |
|
3364 | 0 | fdflags = fcntl(p->fd, F_GETFL, 0); |
3365 | 0 | if (fdflags == -1) { |
3366 | 0 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
3367 | 0 | errno, "F_GETFL"); |
3368 | 0 | return (-1); |
3369 | 0 | } |
3370 | 0 | if (nonblock) |
3371 | 0 | fdflags |= O_NONBLOCK; |
3372 | 0 | else |
3373 | 0 | fdflags &= ~O_NONBLOCK; |
3374 | 0 | if (fcntl(p->fd, F_SETFL, fdflags) == -1) { |
3375 | 0 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
3376 | 0 | errno, "F_SETFL"); |
3377 | 0 | return (-1); |
3378 | 0 | } |
3379 | 0 | return (0); |
3380 | 0 | } |
3381 | | #endif |
3382 | | |
3383 | | /* |
3384 | | * Generate error strings for PCAP_ERROR_ and PCAP_WARNING_ values. |
3385 | | */ |
3386 | | const char * |
3387 | | pcap_statustostr(int errnum) |
3388 | 0 | { |
3389 | 0 | static char ebuf[15+10+1]; |
3390 | |
|
3391 | 0 | switch (errnum) { |
3392 | | |
3393 | 0 | case PCAP_WARNING: |
3394 | 0 | return("Generic warning"); |
3395 | | |
3396 | 0 | case PCAP_WARNING_TSTAMP_TYPE_NOTSUP: |
3397 | 0 | return ("That type of time stamp is not supported by that device"); |
3398 | | |
3399 | 0 | case PCAP_WARNING_PROMISC_NOTSUP: |
3400 | 0 | return ("That device doesn't support promiscuous mode"); |
3401 | | |
3402 | 0 | case PCAP_ERROR: |
3403 | 0 | return("Generic error"); |
3404 | | |
3405 | 0 | case PCAP_ERROR_BREAK: |
3406 | 0 | return("Loop terminated by pcap_breakloop"); |
3407 | | |
3408 | 0 | case PCAP_ERROR_NOT_ACTIVATED: |
3409 | 0 | return("The pcap_t has not been activated"); |
3410 | | |
3411 | 0 | case PCAP_ERROR_ACTIVATED: |
3412 | 0 | return ("The setting can't be changed after the pcap_t is activated"); |
3413 | | |
3414 | 0 | case PCAP_ERROR_NO_SUCH_DEVICE: |
3415 | 0 | return ("No such device exists"); |
3416 | | |
3417 | 0 | case PCAP_ERROR_RFMON_NOTSUP: |
3418 | 0 | return ("That device doesn't support monitor mode"); |
3419 | | |
3420 | 0 | case PCAP_ERROR_NOT_RFMON: |
3421 | 0 | return ("That operation is supported only in monitor mode"); |
3422 | | |
3423 | 0 | case PCAP_ERROR_PERM_DENIED: |
3424 | 0 | return ("You don't have permission to capture on that device"); |
3425 | | |
3426 | 0 | case PCAP_ERROR_IFACE_NOT_UP: |
3427 | 0 | return ("That device is not up"); |
3428 | | |
3429 | 0 | case PCAP_ERROR_CANTSET_TSTAMP_TYPE: |
3430 | 0 | return ("That device doesn't support setting the time stamp type"); |
3431 | | |
3432 | 0 | case PCAP_ERROR_PROMISC_PERM_DENIED: |
3433 | 0 | return ("You don't have permission to capture in promiscuous mode on that device"); |
3434 | | |
3435 | 0 | case PCAP_ERROR_TSTAMP_PRECISION_NOTSUP: |
3436 | 0 | return ("That device doesn't support that time stamp precision"); |
3437 | 0 | } |
3438 | 0 | (void)pcap_snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum); |
3439 | 0 | return(ebuf); |
3440 | 0 | } |
3441 | | |
3442 | | /* |
3443 | | * Not all systems have strerror(). |
3444 | | */ |
3445 | | const char * |
3446 | | pcap_strerror(int errnum) |
3447 | 0 | { |
3448 | 0 | #ifdef HAVE_STRERROR |
3449 | | #ifdef _WIN32 |
3450 | | static char errbuf[PCAP_ERRBUF_SIZE]; |
3451 | | errno_t err = strerror_s(errbuf, PCAP_ERRBUF_SIZE, errnum); |
3452 | | |
3453 | | if (err != 0) /* err = 0 if successful */ |
3454 | | pcap_strlcpy(errbuf, "strerror_s() error", PCAP_ERRBUF_SIZE); |
3455 | | return (errbuf); |
3456 | | #else |
3457 | 0 | return (strerror(errnum)); |
3458 | 0 | #endif /* _WIN32 */ |
3459 | | #else |
3460 | | extern int sys_nerr; |
3461 | | extern const char *const sys_errlist[]; |
3462 | | static char errbuf[PCAP_ERRBUF_SIZE]; |
3463 | | |
3464 | | if ((unsigned int)errnum < sys_nerr) |
3465 | | return ((char *)sys_errlist[errnum]); |
3466 | | (void)pcap_snprintf(errbuf, sizeof errbuf, "Unknown error: %d", errnum); |
3467 | | return (errbuf); |
3468 | | #endif |
3469 | 0 | } |
3470 | | |
3471 | | int |
3472 | | pcap_setfilter(pcap_t *p, struct bpf_program *fp) |
3473 | 0 | { |
3474 | 0 | return (p->setfilter_op(p, fp)); |
3475 | 0 | } |
3476 | | |
3477 | | /* |
3478 | | * Set direction flag, which controls whether we accept only incoming |
3479 | | * packets, only outgoing packets, or both. |
3480 | | * Note that, depending on the platform, some or all direction arguments |
3481 | | * might not be supported. |
3482 | | */ |
3483 | | int |
3484 | | pcap_setdirection(pcap_t *p, pcap_direction_t d) |
3485 | 0 | { |
3486 | 0 | if (p->setdirection_op == NULL) { |
3487 | 0 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3488 | 0 | "Setting direction is not implemented on this platform"); |
3489 | 0 | return (-1); |
3490 | 0 | } else |
3491 | 0 | return (p->setdirection_op(p, d)); |
3492 | 0 | } |
3493 | | |
3494 | | int |
3495 | | pcap_stats(pcap_t *p, struct pcap_stat *ps) |
3496 | 0 | { |
3497 | 0 | return (p->stats_op(p, ps)); |
3498 | 0 | } |
3499 | | |
3500 | | #ifdef _WIN32 |
3501 | | struct pcap_stat * |
3502 | | pcap_stats_ex(pcap_t *p, int *pcap_stat_size) |
3503 | | { |
3504 | | return (p->stats_ex_op(p, pcap_stat_size)); |
3505 | | } |
3506 | | |
3507 | | int |
3508 | | pcap_setbuff(pcap_t *p, int dim) |
3509 | | { |
3510 | | return (p->setbuff_op(p, dim)); |
3511 | | } |
3512 | | |
3513 | | int |
3514 | | pcap_setmode(pcap_t *p, int mode) |
3515 | | { |
3516 | | return (p->setmode_op(p, mode)); |
3517 | | } |
3518 | | |
3519 | | int |
3520 | | pcap_setmintocopy(pcap_t *p, int size) |
3521 | | { |
3522 | | return (p->setmintocopy_op(p, size)); |
3523 | | } |
3524 | | |
3525 | | HANDLE |
3526 | | pcap_getevent(pcap_t *p) |
3527 | | { |
3528 | | return (p->getevent_op(p)); |
3529 | | } |
3530 | | |
3531 | | int |
3532 | | pcap_oid_get_request(pcap_t *p, bpf_u_int32 oid, void *data, size_t *lenp) |
3533 | | { |
3534 | | return (p->oid_get_request_op(p, oid, data, lenp)); |
3535 | | } |
3536 | | |
3537 | | int |
3538 | | pcap_oid_set_request(pcap_t *p, bpf_u_int32 oid, const void *data, size_t *lenp) |
3539 | | { |
3540 | | return (p->oid_set_request_op(p, oid, data, lenp)); |
3541 | | } |
3542 | | |
3543 | | pcap_send_queue * |
3544 | | pcap_sendqueue_alloc(u_int memsize) |
3545 | | { |
3546 | | pcap_send_queue *tqueue; |
3547 | | |
3548 | | /* Allocate the queue */ |
3549 | | tqueue = (pcap_send_queue *)malloc(sizeof(pcap_send_queue)); |
3550 | | if (tqueue == NULL){ |
3551 | | return (NULL); |
3552 | | } |
3553 | | |
3554 | | /* Allocate the buffer */ |
3555 | | tqueue->buffer = (char *)malloc(memsize); |
3556 | | if (tqueue->buffer == NULL) { |
3557 | | free(tqueue); |
3558 | | return (NULL); |
3559 | | } |
3560 | | |
3561 | | tqueue->maxlen = memsize; |
3562 | | tqueue->len = 0; |
3563 | | |
3564 | | return (tqueue); |
3565 | | } |
3566 | | |
3567 | | void |
3568 | | pcap_sendqueue_destroy(pcap_send_queue *queue) |
3569 | | { |
3570 | | free(queue->buffer); |
3571 | | free(queue); |
3572 | | } |
3573 | | |
3574 | | int |
3575 | | pcap_sendqueue_queue(pcap_send_queue *queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data) |
3576 | | { |
3577 | | if (queue->len + sizeof(struct pcap_pkthdr) + pkt_header->caplen > queue->maxlen){ |
3578 | | return (-1); |
3579 | | } |
3580 | | |
3581 | | /* Copy the pcap_pkthdr header*/ |
3582 | | memcpy(queue->buffer + queue->len, pkt_header, sizeof(struct pcap_pkthdr)); |
3583 | | queue->len += sizeof(struct pcap_pkthdr); |
3584 | | |
3585 | | /* copy the packet */ |
3586 | | memcpy(queue->buffer + queue->len, pkt_data, pkt_header->caplen); |
3587 | | queue->len += pkt_header->caplen; |
3588 | | |
3589 | | return (0); |
3590 | | } |
3591 | | |
3592 | | u_int |
3593 | | pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue *queue, int sync) |
3594 | | { |
3595 | | return (p->sendqueue_transmit_op(p, queue, sync)); |
3596 | | } |
3597 | | |
3598 | | int |
3599 | | pcap_setuserbuffer(pcap_t *p, int size) |
3600 | | { |
3601 | | return (p->setuserbuffer_op(p, size)); |
3602 | | } |
3603 | | |
3604 | | int |
3605 | | pcap_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks) |
3606 | | { |
3607 | | return (p->live_dump_op(p, filename, maxsize, maxpacks)); |
3608 | | } |
3609 | | |
3610 | | int |
3611 | | pcap_live_dump_ended(pcap_t *p, int sync) |
3612 | | { |
3613 | | return (p->live_dump_ended_op(p, sync)); |
3614 | | } |
3615 | | |
3616 | | PAirpcapHandle |
3617 | | pcap_get_airpcap_handle(pcap_t *p) |
3618 | | { |
3619 | | PAirpcapHandle handle; |
3620 | | |
3621 | | handle = p->get_airpcap_handle_op(p); |
3622 | | if (handle == NULL) { |
3623 | | (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf), |
3624 | | "This isn't an AirPcap device"); |
3625 | | } |
3626 | | return (handle); |
3627 | | } |
3628 | | #endif |
3629 | | |
3630 | | /* |
3631 | | * On some platforms, we need to clean up promiscuous or monitor mode |
3632 | | * when we close a device - and we want that to happen even if the |
3633 | | * application just exits without explicitl closing devices. |
3634 | | * On those platforms, we need to register a "close all the pcaps" |
3635 | | * routine to be called when we exit, and need to maintain a list of |
3636 | | * pcaps that need to be closed to clean up modes. |
3637 | | * |
3638 | | * XXX - not thread-safe. |
3639 | | */ |
3640 | | |
3641 | | /* |
3642 | | * List of pcaps on which we've done something that needs to be |
3643 | | * cleaned up. |
3644 | | * If there are any such pcaps, we arrange to call "pcap_close_all()" |
3645 | | * when we exit, and have it close all of them. |
3646 | | */ |
3647 | | static struct pcap *pcaps_to_close; |
3648 | | |
3649 | | /* |
3650 | | * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to |
3651 | | * be called on exit. |
3652 | | */ |
3653 | | static int did_atexit; |
3654 | | |
3655 | | static void |
3656 | | pcap_close_all(void) |
3657 | 0 | { |
3658 | 0 | struct pcap *handle; |
3659 | |
|
3660 | 0 | while ((handle = pcaps_to_close) != NULL) |
3661 | 0 | pcap_close(handle); |
3662 | 0 | } |
3663 | | |
3664 | | int |
3665 | | pcap_do_addexit(pcap_t *p) |
3666 | 0 | { |
3667 | | /* |
3668 | | * If we haven't already done so, arrange to have |
3669 | | * "pcap_close_all()" called when we exit. |
3670 | | */ |
3671 | 0 | if (!did_atexit) { |
3672 | 0 | if (atexit(pcap_close_all) != 0) { |
3673 | | /* |
3674 | | * "atexit()" failed; let our caller know. |
3675 | | */ |
3676 | 0 | pcap_strlcpy(p->errbuf, "atexit failed", PCAP_ERRBUF_SIZE); |
3677 | 0 | return (0); |
3678 | 0 | } |
3679 | 0 | did_atexit = 1; |
3680 | 0 | } |
3681 | 0 | return (1); |
3682 | 0 | } |
3683 | | |
3684 | | void |
3685 | | pcap_add_to_pcaps_to_close(pcap_t *p) |
3686 | 0 | { |
3687 | 0 | p->next = pcaps_to_close; |
3688 | 0 | pcaps_to_close = p; |
3689 | 0 | } |
3690 | | |
3691 | | void |
3692 | | pcap_remove_from_pcaps_to_close(pcap_t *p) |
3693 | 0 | { |
3694 | 0 | pcap_t *pc, *prevpc; |
3695 | |
|
3696 | 0 | for (pc = pcaps_to_close, prevpc = NULL; pc != NULL; |
3697 | 0 | prevpc = pc, pc = pc->next) { |
3698 | 0 | if (pc == p) { |
3699 | | /* |
3700 | | * Found it. Remove it from the list. |
3701 | | */ |
3702 | 0 | if (prevpc == NULL) { |
3703 | | /* |
3704 | | * It was at the head of the list. |
3705 | | */ |
3706 | 0 | pcaps_to_close = pc->next; |
3707 | 0 | } else { |
3708 | | /* |
3709 | | * It was in the middle of the list. |
3710 | | */ |
3711 | 0 | prevpc->next = pc->next; |
3712 | 0 | } |
3713 | 0 | break; |
3714 | 0 | } |
3715 | 0 | } |
3716 | 0 | } |
3717 | | |
3718 | | void |
3719 | | pcap_cleanup_live_common(pcap_t *p) |
3720 | 0 | { |
3721 | 0 | if (p->buffer != NULL) { |
3722 | 0 | free(p->buffer); |
3723 | 0 | p->buffer = NULL; |
3724 | 0 | } |
3725 | 0 | if (p->dlt_list != NULL) { |
3726 | 0 | free(p->dlt_list); |
3727 | 0 | p->dlt_list = NULL; |
3728 | 0 | p->dlt_count = 0; |
3729 | 0 | } |
3730 | 0 | if (p->tstamp_type_list != NULL) { |
3731 | 0 | free(p->tstamp_type_list); |
3732 | 0 | p->tstamp_type_list = NULL; |
3733 | 0 | p->tstamp_type_count = 0; |
3734 | 0 | } |
3735 | 0 | if (p->tstamp_precision_list != NULL) { |
3736 | 0 | free(p->tstamp_precision_list); |
3737 | 0 | p->tstamp_precision_list = NULL; |
3738 | 0 | p->tstamp_precision_count = 0; |
3739 | 0 | } |
3740 | 0 | pcap_freecode(&p->fcode); |
3741 | 0 | #if !defined(_WIN32) && !defined(MSDOS) |
3742 | 0 | if (p->fd >= 0) { |
3743 | 0 | close(p->fd); |
3744 | 0 | p->fd = -1; |
3745 | 0 | } |
3746 | 0 | p->selectable_fd = -1; |
3747 | 0 | #endif |
3748 | 0 | } |
3749 | | |
3750 | | /* |
3751 | | * API compatible with WinPcap's "send a packet" routine - returns -1 |
3752 | | * on error, 0 otherwise. |
3753 | | * |
3754 | | * XXX - what if we get a short write? |
3755 | | */ |
3756 | | int |
3757 | | pcap_sendpacket(pcap_t *p, const u_char *buf, int size) |
3758 | 0 | { |
3759 | 0 | if (p->inject_op(p, buf, size) == -1) |
3760 | 0 | return (-1); |
3761 | 0 | return (0); |
3762 | 0 | } |
3763 | | |
3764 | | /* |
3765 | | * API compatible with OpenBSD's "send a packet" routine - returns -1 on |
3766 | | * error, number of bytes written otherwise. |
3767 | | */ |
3768 | | int |
3769 | | pcap_inject(pcap_t *p, const void *buf, size_t size) |
3770 | 0 | { |
3771 | 0 | return (p->inject_op(p, buf, size)); |
3772 | 0 | } |
3773 | | |
3774 | | void |
3775 | | pcap_close(pcap_t *p) |
3776 | 11.6k | { |
3777 | 11.6k | if (p->opt.device != NULL) |
3778 | 0 | free(p->opt.device); |
3779 | 11.6k | p->cleanup_op(p); |
3780 | 11.6k | free(p); |
3781 | 11.6k | } |
3782 | | |
3783 | | /* |
3784 | | * Given a BPF program, a pcap_pkthdr structure for a packet, and the raw |
3785 | | * data for the packet, check whether the packet passes the filter. |
3786 | | * Returns the return value of the filter program, which will be zero if |
3787 | | * the packet doesn't pass and non-zero if the packet does pass. |
3788 | | */ |
3789 | | int |
3790 | | pcap_offline_filter(const struct bpf_program *fp, const struct pcap_pkthdr *h, |
3791 | | const u_char *pkt) |
3792 | 0 | { |
3793 | 0 | const struct bpf_insn *fcode = fp->bf_insns; |
3794 | |
|
3795 | 0 | if (fcode != NULL) |
3796 | 0 | return (bpf_filter(fcode, pkt, h->len, h->caplen)); |
3797 | 0 | else |
3798 | 0 | return (0); |
3799 | 0 | } |
3800 | | |
3801 | | static int |
3802 | | pcap_can_set_rfmon_dead(pcap_t *p) |
3803 | 0 | { |
3804 | 0 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3805 | 0 | "Rfmon mode doesn't apply on a pcap_open_dead pcap_t"); |
3806 | 0 | return (PCAP_ERROR); |
3807 | 0 | } |
3808 | | |
3809 | | static int |
3810 | | pcap_read_dead(pcap_t *p, int cnt _U_, pcap_handler callback _U_, |
3811 | | u_char *user _U_) |
3812 | 0 | { |
3813 | 0 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3814 | 0 | "Packets aren't available from a pcap_open_dead pcap_t"); |
3815 | 0 | return (-1); |
3816 | 0 | } |
3817 | | |
3818 | | static int |
3819 | | pcap_inject_dead(pcap_t *p, const void *buf _U_, size_t size _U_) |
3820 | 0 | { |
3821 | 0 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3822 | 0 | "Packets can't be sent on a pcap_open_dead pcap_t"); |
3823 | 0 | return (-1); |
3824 | 0 | } |
3825 | | |
3826 | | static int |
3827 | | pcap_setfilter_dead(pcap_t *p, struct bpf_program *fp _U_) |
3828 | 0 | { |
3829 | 0 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3830 | 0 | "A filter cannot be set on a pcap_open_dead pcap_t"); |
3831 | 0 | return (-1); |
3832 | 0 | } |
3833 | | |
3834 | | static int |
3835 | | pcap_setdirection_dead(pcap_t *p, pcap_direction_t d _U_) |
3836 | 0 | { |
3837 | 0 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3838 | 0 | "The packet direction cannot be set on a pcap_open_dead pcap_t"); |
3839 | 0 | return (-1); |
3840 | 0 | } |
3841 | | |
3842 | | static int |
3843 | | pcap_set_datalink_dead(pcap_t *p, int dlt _U_) |
3844 | 0 | { |
3845 | 0 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3846 | 0 | "The link-layer header type cannot be set on a pcap_open_dead pcap_t"); |
3847 | 0 | return (-1); |
3848 | 0 | } |
3849 | | |
3850 | | static int |
3851 | | pcap_getnonblock_dead(pcap_t *p) |
3852 | 0 | { |
3853 | 0 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3854 | 0 | "A pcap_open_dead pcap_t does not have a non-blocking mode setting"); |
3855 | 0 | return (-1); |
3856 | 0 | } |
3857 | | |
3858 | | static int |
3859 | | pcap_setnonblock_dead(pcap_t *p, int nonblock _U_) |
3860 | 0 | { |
3861 | 0 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3862 | 0 | "A pcap_open_dead pcap_t does not have a non-blocking mode setting"); |
3863 | 0 | return (-1); |
3864 | 0 | } |
3865 | | |
3866 | | static int |
3867 | | pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_) |
3868 | 0 | { |
3869 | 0 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3870 | 0 | "Statistics aren't available from a pcap_open_dead pcap_t"); |
3871 | 0 | return (-1); |
3872 | 0 | } |
3873 | | |
3874 | | #ifdef _WIN32 |
3875 | | struct pcap_stat * |
3876 | | pcap_stats_ex_dead(pcap_t *p, int *pcap_stat_size _U_) |
3877 | | { |
3878 | | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3879 | | "Statistics aren't available from a pcap_open_dead pcap_t"); |
3880 | | return (NULL); |
3881 | | } |
3882 | | |
3883 | | static int |
3884 | | pcap_setbuff_dead(pcap_t *p, int dim) |
3885 | | { |
3886 | | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3887 | | "The kernel buffer size cannot be set on a pcap_open_dead pcap_t"); |
3888 | | return (-1); |
3889 | | } |
3890 | | |
3891 | | static int |
3892 | | pcap_setmode_dead(pcap_t *p, int mode) |
3893 | | { |
3894 | | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3895 | | "impossible to set mode on a pcap_open_dead pcap_t"); |
3896 | | return (-1); |
3897 | | } |
3898 | | |
3899 | | static int |
3900 | | pcap_setmintocopy_dead(pcap_t *p, int size) |
3901 | | { |
3902 | | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3903 | | "The mintocopy parameter cannot be set on a pcap_open_dead pcap_t"); |
3904 | | return (-1); |
3905 | | } |
3906 | | |
3907 | | static HANDLE |
3908 | | pcap_getevent_dead(pcap_t *p) |
3909 | | { |
3910 | | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3911 | | "A pcap_open_dead pcap_t has no event handle"); |
3912 | | return (INVALID_HANDLE_VALUE); |
3913 | | } |
3914 | | |
3915 | | static int |
3916 | | pcap_oid_get_request_dead(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_, |
3917 | | size_t *lenp _U_) |
3918 | | { |
3919 | | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3920 | | "An OID get request cannot be performed on a pcap_open_dead pcap_t"); |
3921 | | return (PCAP_ERROR); |
3922 | | } |
3923 | | |
3924 | | static int |
3925 | | pcap_oid_set_request_dead(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_, |
3926 | | size_t *lenp _U_) |
3927 | | { |
3928 | | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3929 | | "An OID set request cannot be performed on a pcap_open_dead pcap_t"); |
3930 | | return (PCAP_ERROR); |
3931 | | } |
3932 | | |
3933 | | static u_int |
3934 | | pcap_sendqueue_transmit_dead(pcap_t *p, pcap_send_queue *queue, int sync) |
3935 | | { |
3936 | | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3937 | | "Packets cannot be transmitted on a pcap_open_dead pcap_t"); |
3938 | | return (0); |
3939 | | } |
3940 | | |
3941 | | static int |
3942 | | pcap_setuserbuffer_dead(pcap_t *p, int size) |
3943 | | { |
3944 | | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3945 | | "The user buffer cannot be set on a pcap_open_dead pcap_t"); |
3946 | | return (-1); |
3947 | | } |
3948 | | |
3949 | | static int |
3950 | | pcap_live_dump_dead(pcap_t *p, char *filename, int maxsize, int maxpacks) |
3951 | | { |
3952 | | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3953 | | "Live packet dumping cannot be performed on a pcap_open_dead pcap_t"); |
3954 | | return (-1); |
3955 | | } |
3956 | | |
3957 | | static int |
3958 | | pcap_live_dump_ended_dead(pcap_t *p, int sync) |
3959 | | { |
3960 | | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
3961 | | "Live packet dumping cannot be performed on a pcap_open_dead pcap_t"); |
3962 | | return (-1); |
3963 | | } |
3964 | | |
3965 | | static PAirpcapHandle |
3966 | | pcap_get_airpcap_handle_dead(pcap_t *p) |
3967 | | { |
3968 | | return (NULL); |
3969 | | } |
3970 | | #endif /* _WIN32 */ |
3971 | | |
3972 | | static void |
3973 | | pcap_cleanup_dead(pcap_t *p _U_) |
3974 | 0 | { |
3975 | | /* Nothing to do. */ |
3976 | 0 | } |
3977 | | |
3978 | | pcap_t * |
3979 | | pcap_open_dead_with_tstamp_precision(int linktype, int snaplen, u_int precision) |
3980 | 0 | { |
3981 | 0 | pcap_t *p; |
3982 | |
|
3983 | 0 | switch (precision) { |
3984 | | |
3985 | 0 | case PCAP_TSTAMP_PRECISION_MICRO: |
3986 | 0 | case PCAP_TSTAMP_PRECISION_NANO: |
3987 | 0 | break; |
3988 | | |
3989 | 0 | default: |
3990 | | /* |
3991 | | * This doesn't really matter, but we don't have any way |
3992 | | * to report particular errors, so the only failure we |
3993 | | * should have is a memory allocation failure. Just |
3994 | | * pick microsecond precision. |
3995 | | */ |
3996 | 0 | precision = PCAP_TSTAMP_PRECISION_MICRO; |
3997 | 0 | break; |
3998 | 0 | } |
3999 | 0 | p = malloc(sizeof(*p)); |
4000 | 0 | if (p == NULL) |
4001 | 0 | return NULL; |
4002 | 0 | memset (p, 0, sizeof(*p)); |
4003 | 0 | p->snapshot = snaplen; |
4004 | 0 | p->linktype = linktype; |
4005 | 0 | p->opt.tstamp_precision = precision; |
4006 | 0 | p->can_set_rfmon_op = pcap_can_set_rfmon_dead; |
4007 | 0 | p->read_op = pcap_read_dead; |
4008 | 0 | p->inject_op = pcap_inject_dead; |
4009 | 0 | p->setfilter_op = pcap_setfilter_dead; |
4010 | 0 | p->setdirection_op = pcap_setdirection_dead; |
4011 | 0 | p->set_datalink_op = pcap_set_datalink_dead; |
4012 | 0 | p->getnonblock_op = pcap_getnonblock_dead; |
4013 | 0 | p->setnonblock_op = pcap_setnonblock_dead; |
4014 | 0 | p->stats_op = pcap_stats_dead; |
4015 | | #ifdef _WIN32 |
4016 | | p->stats_ex_op = pcap_stats_ex_dead; |
4017 | | p->setbuff_op = pcap_setbuff_dead; |
4018 | | p->setmode_op = pcap_setmode_dead; |
4019 | | p->setmintocopy_op = pcap_setmintocopy_dead; |
4020 | | p->getevent_op = pcap_getevent_dead; |
4021 | | p->oid_get_request_op = pcap_oid_get_request_dead; |
4022 | | p->oid_set_request_op = pcap_oid_set_request_dead; |
4023 | | p->sendqueue_transmit_op = pcap_sendqueue_transmit_dead; |
4024 | | p->setuserbuffer_op = pcap_setuserbuffer_dead; |
4025 | | p->live_dump_op = pcap_live_dump_dead; |
4026 | | p->live_dump_ended_op = pcap_live_dump_ended_dead; |
4027 | | p->get_airpcap_handle_op = pcap_get_airpcap_handle_dead; |
4028 | | #endif |
4029 | 0 | p->cleanup_op = pcap_cleanup_dead; |
4030 | | |
4031 | | /* |
4032 | | * A "dead" pcap_t never requires special BPF code generation. |
4033 | | */ |
4034 | 0 | p->bpf_codegen_flags = 0; |
4035 | |
|
4036 | 0 | p->activated = 1; |
4037 | 0 | return (p); |
4038 | 0 | } |
4039 | | |
4040 | | pcap_t * |
4041 | | pcap_open_dead(int linktype, int snaplen) |
4042 | 0 | { |
4043 | 0 | return (pcap_open_dead_with_tstamp_precision(linktype, snaplen, |
4044 | 0 | PCAP_TSTAMP_PRECISION_MICRO)); |
4045 | 0 | } |
4046 | | |
4047 | | #ifdef YYDEBUG |
4048 | | /* |
4049 | | * Set the internal "debug printout" flag for the filter expression parser. |
4050 | | * The code to print that stuff is present only if YYDEBUG is defined, so |
4051 | | * the flag, and the routine to set it, are defined only if YYDEBUG is |
4052 | | * defined. |
4053 | | * |
4054 | | * This is intended for libpcap developers, not for general use. |
4055 | | * If you want to set these in a program, you'll have to declare this |
4056 | | * routine yourself, with the appropriate DLL import attribute on Windows; |
4057 | | * it's not declared in any header file, and won't be declared in any |
4058 | | * header file provided by libpcap. |
4059 | | */ |
4060 | | PCAP_API void pcap_set_parser_debug(int value); |
4061 | | |
4062 | | PCAP_API_DEF void |
4063 | | pcap_set_parser_debug(int value) |
4064 | | { |
4065 | | pcap_debug = value; |
4066 | | } |
4067 | | #endif |