]> The Tcpdump Group git mirrors - libpcap/blob - pcap.c
ded9e7b0dda5d98f887da375a682f37d2a9381ff
[libpcap] / pcap.c
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 #ifndef lint
35 static const char rcsid[] =
36 "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.49 2003-02-13 07:54:59 guy Exp $ (LBL)";
37 #endif
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #ifdef WIN32
44 #include <pcap-stdinc.h>
45 #else /* WIN32 */
46 #include <sys/types.h>
47 #endif /* WIN32 */
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #ifndef WIN32
53 #include <unistd.h>
54 #endif
55 #include <fcntl.h>
56 #include <errno.h>
57
58 #ifdef HAVE_OS_PROTO_H
59 #include "os-proto.h"
60 #endif
61
62 #include "pcap-int.h"
63
64 int
65 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
66 {
67
68 if (p->sf.rfile != NULL)
69 return (pcap_offline_read(p, cnt, callback, user));
70 return (pcap_read(p, cnt, callback, user));
71 }
72
73 int
74 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
75 {
76 register int n;
77
78 for (;;) {
79 if (p->sf.rfile != NULL)
80 n = pcap_offline_read(p, cnt, callback, user);
81 else {
82 /*
83 * XXX keep reading until we get something
84 * (or an error occurs)
85 */
86 do {
87 n = pcap_read(p, cnt, callback, user);
88 } while (n == 0);
89 }
90 if (n <= 0)
91 return (n);
92 if (cnt > 0) {
93 cnt -= n;
94 if (cnt <= 0)
95 return (0);
96 }
97 }
98 }
99
100 struct singleton {
101 struct pcap_pkthdr *hdr;
102 const u_char *pkt;
103 };
104
105
106 static void
107 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt)
108 {
109 struct singleton *sp = (struct singleton *)userData;
110 *sp->hdr = *h;
111 sp->pkt = pkt;
112 }
113
114 const u_char *
115 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
116 {
117 struct singleton s;
118
119 s.hdr = h;
120 if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0)
121 return (0);
122 return (s.pkt);
123 }
124
125 int
126 pcap_datalink(pcap_t *p)
127 {
128 return (p->linktype);
129 }
130
131 int
132 pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
133 {
134 if (p->dlt_count == 0) {
135 /*
136 * We couldn't fetch the list of DLTs, which means
137 * this platform doesn't support changing the
138 * DLT for an interface. Return a list of DLTs
139 * containing only the DLT this device supports.
140 */
141 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
142 if (*dlt_buffer == NULL) {
143 (void)snprintf(p->errbuf, sizeof(p->errbuf),
144 "malloc: %s", pcap_strerror(errno));
145 return (-1);
146 }
147 **dlt_buffer = p->linktype;
148 return (1);
149 } else {
150 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer) * p->dlt_count);
151 if (*dlt_buffer == NULL) {
152 (void)snprintf(p->errbuf, sizeof(p->errbuf),
153 "malloc: %s", pcap_strerror(errno));
154 return (-1);
155 }
156 (void)memcpy(*dlt_buffer, p->dlt_list,
157 sizeof(**dlt_buffer) * p->dlt_count);
158 return (p->dlt_count);
159 }
160 }
161
162 int
163 pcap_set_datalink(pcap_t *p, int dlt)
164 {
165 int i;
166 const char *dlt_name;
167
168 if (p->dlt_count == 0) {
169 /*
170 * We couldn't fetch the list of DLTs, which means
171 * this platform doesn't support changing the
172 * DLT for an interface. Check whether the new
173 * DLT is the one this interface supports.
174 */
175 if (p->linktype != dlt)
176 goto unsupported;
177
178 /*
179 * It is, so there's nothing we need to do here.
180 */
181 return (0);
182 }
183 for (i = 0; i < p->dlt_count; i++)
184 if (p->dlt_list[i] == dlt)
185 break;
186 if (i >= p->dlt_count)
187 goto unsupported;
188 if (pcap_set_datalink_platform(p, dlt) == -1)
189 return (-1);
190 p->linktype = dlt;
191 return (0);
192
193 unsupported:
194 dlt_name = pcap_datalink_val_to_name(dlt);
195 if (dlt_name != NULL) {
196 (void) snprintf(p->errbuf, sizeof(p->errbuf),
197 "%s is not one of the DLTs supported by this device",
198 dlt_name);
199 } else {
200 (void) snprintf(p->errbuf, sizeof(p->errbuf),
201 "DLT %d is not one of the DLTs supported by this device",
202 dlt);
203 }
204 return (-1);
205 }
206
207 struct dlt_choice {
208 const char *name;
209 int dlt;
210 };
211
212 #define DLT_CHOICE(code) { #code, code }
213 #define DLT_CHOICE_SENTINEL { NULL, 0 }
214
215 static struct dlt_choice dlt_choices[] = {
216 DLT_CHOICE(DLT_ARCNET),
217 DLT_CHOICE(DLT_ARCNET_LINUX),
218 DLT_CHOICE(DLT_EN10MB),
219 DLT_CHOICE(DLT_SLIP),
220 DLT_CHOICE(DLT_SLIP_BSDOS),
221 DLT_CHOICE(DLT_NULL),
222 DLT_CHOICE(DLT_LOOP),
223 DLT_CHOICE(DLT_PPP),
224 DLT_CHOICE(DLT_C_HDLC),
225 DLT_CHOICE(DLT_PPP_SERIAL),
226 DLT_CHOICE(DLT_PPP_ETHER),
227 DLT_CHOICE(DLT_PPP_BSDOS),
228 DLT_CHOICE(DLT_FDDI),
229 DLT_CHOICE(DLT_IEEE802),
230 DLT_CHOICE(DLT_IEEE802_11),
231 DLT_CHOICE(DLT_PRISM_HEADER),
232 DLT_CHOICE(DLT_IEEE802_11_RADIO),
233 DLT_CHOICE(DLT_ATM_RFC1483),
234 DLT_CHOICE(DLT_ATM_CLIP),
235 DLT_CHOICE(DLT_SUNATM),
236 DLT_CHOICE(DLT_RAW),
237 DLT_CHOICE(DLT_LINUX_SLL),
238 DLT_CHOICE(DLT_LTALK),
239 DLT_CHOICE(DLT_IP_OVER_FC),
240 DLT_CHOICE(DLT_FRELAY),
241 DLT_CHOICE_SENTINEL
242 };
243
244 /*
245 * This array is designed for mapping upper and lower case letter
246 * together for a case independent comparison. The mappings are
247 * based upon ascii character sequences.
248 */
249 static const u_char charmap[] = {
250 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
251 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
252 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
253 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
254 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
255 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
256 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
257 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
258 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
259 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
260 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
261 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
262 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
263 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
264 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
265 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
266 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
267 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
268 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
269 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
270 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
271 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
272 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
273 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
274 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
275 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
276 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
277 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
278 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
279 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
280 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
281 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
282 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
283 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
284 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
285 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
286 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
287 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
288 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
289 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
290 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
291 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
292 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
293 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
294 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
295 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
296 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
297 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
298 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
299 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
300 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
301 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
302 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
303 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
304 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
305 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
306 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
307 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
308 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
309 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
310 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
311 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
312 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
313 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
314 };
315
316 static int
317 pcap_strcasecmp(const char *s1, const char *s2)
318 {
319 register const u_char *cm = charmap,
320 *us1 = (u_char *)s1,
321 *us2 = (u_char *)s2;
322
323 while (cm[*us1] == cm[*us2++])
324 if (*us1++ == '\0')
325 return(0);
326 return (cm[*us1] - cm[*--us2]);
327 }
328
329 int
330 pcap_datalink_name_to_val(const char *name)
331 {
332 int i;
333
334 for (i = 0; dlt_choices[i].name != NULL; i++) {
335 if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
336 name) == 0)
337 return (dlt_choices[i].dlt);
338 }
339 return (-1);
340 }
341
342 const char *
343 pcap_datalink_val_to_name(int dlt)
344 {
345 int i;
346
347 for (i = 0; dlt_choices[i].name != NULL; i++) {
348 if (dlt_choices[i].dlt == dlt)
349 return (dlt_choices[i].name + sizeof("DLT_") - 1);
350 }
351 return (NULL);
352 }
353
354 int
355 pcap_snapshot(pcap_t *p)
356 {
357 return (p->snapshot);
358 }
359
360 int
361 pcap_is_swapped(pcap_t *p)
362 {
363 return (p->sf.swapped);
364 }
365
366 int
367 pcap_major_version(pcap_t *p)
368 {
369 return (p->sf.version_major);
370 }
371
372 int
373 pcap_minor_version(pcap_t *p)
374 {
375 return (p->sf.version_minor);
376 }
377
378 FILE *
379 pcap_file(pcap_t *p)
380 {
381 return (p->sf.rfile);
382 }
383
384 int
385 pcap_fileno(pcap_t *p)
386 {
387 #ifndef WIN32
388 return (p->fd);
389 #else
390 if (p->adapter != NULL)
391 return ((int)(DWORD)p->adapter->hFile);
392 else
393 return (-1);
394 #endif
395 }
396
397 void
398 pcap_perror(pcap_t *p, char *prefix)
399 {
400 fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
401 }
402
403 char *
404 pcap_geterr(pcap_t *p)
405 {
406 return (p->errbuf);
407 }
408
409 /*
410 * NOTE: in the future, these may need to call platform-dependent routines,
411 * e.g. on platforms with memory-mapped packet-capture mechanisms where
412 * "pcap_read()" uses "select()" or "poll()" to wait for packets to arrive.
413 */
414 int
415 pcap_getnonblock(pcap_t *p, char *errbuf)
416 {
417 #ifndef WIN32
418 int fdflags;
419 #endif
420
421 if (p->sf.rfile != NULL) {
422 /*
423 * This is a savefile, not a live capture file, so
424 * never say it's in non-blocking mode.
425 */
426 return (0);
427 }
428 #ifndef WIN32
429 fdflags = fcntl(p->fd, F_GETFL, 0);
430 if (fdflags == -1) {
431 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
432 pcap_strerror(errno));
433 return (-1);
434 }
435 if (fdflags & O_NONBLOCK)
436 return (1);
437 else
438 return (0);
439 #else
440 return (p->nonblock);
441 #endif
442 }
443
444 int
445 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
446 {
447 #ifndef WIN32
448 int fdflags;
449 #else
450 int newtimeout;
451 #endif
452
453 if (p->sf.rfile != NULL) {
454 /*
455 * This is a savefile, not a live capture file, so
456 * ignore requests to put it in non-blocking mode.
457 */
458 return (0);
459 }
460 #ifndef WIN32
461 fdflags = fcntl(p->fd, F_GETFL, 0);
462 if (fdflags == -1) {
463 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
464 pcap_strerror(errno));
465 return (-1);
466 }
467 if (nonblock)
468 fdflags |= O_NONBLOCK;
469 else
470 fdflags &= ~O_NONBLOCK;
471 if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
472 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
473 pcap_strerror(errno));
474 return (-1);
475 }
476 #else
477 if (nonblock) {
478 /*
479 * Set the read timeout to -1 for non-blocking mode.
480 */
481 newtimeout = -1;
482 } else {
483 /*
484 * Restore the timeout set when the device was opened.
485 * (Note that this may be -1, in which case we're not
486 * really leaving non-blocking mode.)
487 */
488 newtimeout = p->timeout;
489 }
490 if (!PacketSetReadTimeout(p->adapter, newtimeout)) {
491 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
492 "PacketSetReadTimeout: %s", pcap_win32strerror());
493 return (-1);
494 }
495 p->nonblock = (newtimeout == -1);
496 #endif
497 return (0);
498 }
499
500 #ifdef WIN32
501 /*
502 * Generate a string for the last Win32-specific error (i.e. an error generated when
503 * calling a Win32 API).
504 * For errors occurred during standard C calls, we still use pcap_strerror()
505 */
506 char *
507 pcap_win32strerror(void)
508 {
509 DWORD error;
510 static char errbuf[PCAP_ERRBUF_SIZE+1];
511 int errlen;
512
513 error = GetLastError();
514 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
515 PCAP_ERRBUF_SIZE, NULL);
516
517 /*
518 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
519 * message. Get rid of it.
520 */
521 errlen = strlen(errbuf);
522 if (errlen >= 2) {
523 errbuf[errlen - 1] = '\0';
524 errbuf[errlen - 2] = '\0';
525 }
526 return (errbuf);
527 }
528 #endif
529
530 /*
531 * Not all systems have strerror().
532 */
533 char *
534 pcap_strerror(int errnum)
535 {
536 #ifdef HAVE_STRERROR
537 return (strerror(errnum));
538 #else
539 extern int sys_nerr;
540 extern const char *const sys_errlist[];
541 static char ebuf[20];
542
543 if ((unsigned int)errnum < sys_nerr)
544 return ((char *)sys_errlist[errnum]);
545 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
546 return(ebuf);
547 #endif
548 }
549
550 pcap_t *
551 pcap_open_dead(int linktype, int snaplen)
552 {
553 pcap_t *p;
554
555 p = malloc(sizeof(*p));
556 if (p == NULL)
557 return NULL;
558 memset (p, 0, sizeof(*p));
559 #ifndef WIN32
560 p->fd = -1;
561 #else
562 p->adapter = NULL;
563 #endif /* WIN32 */
564 p->snapshot = snaplen;
565 p->linktype = linktype;
566 return p;
567 }
568
569 void
570 pcap_close(pcap_t *p)
571 {
572 /*XXX*/
573 #ifndef WIN32
574 if (p->fd >= 0) {
575 #ifdef linux
576 pcap_close_linux(p);
577 #endif
578 close(p->fd);
579 }
580 #else /* WIN32 */
581 if (p->adapter != NULL) {
582 PacketCloseAdapter(p->adapter);
583 p->adapter = NULL;
584 }
585 #endif /* WIN32 */
586 if (p->sf.rfile != NULL) {
587 if (p->sf.rfile != stdin)
588 (void)fclose(p->sf.rfile);
589 if (p->sf.base != NULL)
590 free(p->sf.base);
591 } else if (p->buffer != NULL)
592 free(p->buffer);
593 if (p->dlt_list != NULL)
594 free(p->dlt_list);
595
596 pcap_freecode(&p->fcode);
597 free(p);
598 }
599
600 /*
601 * We make the version string static, and return a pointer to it, rather
602 * than exporting the version string directly. On at least some UNIXes,
603 * if you import data from a shared library into an program, the data is
604 * bound into the program binary, so if the string in the version of the
605 * library with which the program was linked isn't the same as the
606 * string in the version of the library with which the program is being
607 * run, various undesirable things may happen (warnings, the string
608 * being the one from the version of the library with which the program
609 * was linked, or even weirder things, such as the string being the one
610 * from the library but being truncated).
611 */
612 #ifdef WIN32
613 /*
614 * XXX - it'd be nice if we could somehow generate this when building WinPcap.
615 */
616 static const char pcap_version_string[] =
617 "WinPcap version 3.0beta, based on libpcap version 0.8";
618 #else
619 #include "version.h"
620 #endif
621
622 const char *
623 pcap_lib_version(void)
624 {
625 return (pcap_version_string);
626 }