]> The Tcpdump Group git mirrors - libpcap/blob - pcap.c
From Gisle Vanem:
[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[] _U_ =
36 "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.77 2004-12-17 19:55:41 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 _MSC_VER
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 #ifdef HAVE_DAG_API
65 #include <dagnew.h>
66 #include <dagapi.h>
67 #endif
68
69 int
70 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
71 {
72
73 return p->read_op(p, cnt, callback, user);
74 }
75
76 /*
77 * XXX - is this necessary?
78 */
79 int
80 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
81 {
82
83 return p->read_op(p, cnt, callback, user);
84 }
85
86 int
87 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
88 {
89 register int n;
90
91 for (;;) {
92 if (p->sf.rfile != NULL) {
93 /*
94 * 0 means EOF, so don't loop if we get 0.
95 */
96 n = pcap_offline_read(p, cnt, callback, user);
97 } else {
98 /*
99 * XXX keep reading until we get something
100 * (or an error occurs)
101 */
102 do {
103 n = p->read_op(p, cnt, callback, user);
104 } while (n == 0);
105 }
106 if (n <= 0)
107 return (n);
108 if (cnt > 0) {
109 cnt -= n;
110 if (cnt <= 0)
111 return (0);
112 }
113 }
114 }
115
116 struct singleton {
117 struct pcap_pkthdr *hdr;
118 const u_char *pkt;
119 };
120
121
122 static void
123 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt)
124 {
125 struct singleton *sp = (struct singleton *)userData;
126 *sp->hdr = *h;
127 sp->pkt = pkt;
128 }
129
130 const u_char *
131 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
132 {
133 struct singleton s;
134
135 s.hdr = h;
136 if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0)
137 return (0);
138 return (s.pkt);
139 }
140
141 struct pkt_for_fakecallback {
142 struct pcap_pkthdr *hdr;
143 const u_char **pkt;
144 };
145
146 static void
147 pcap_fakecallback(u_char *userData, const struct pcap_pkthdr *h,
148 const u_char *pkt)
149 {
150 struct pkt_for_fakecallback *sp = (struct pkt_for_fakecallback *)userData;
151
152 *sp->hdr = *h;
153 *sp->pkt = pkt;
154 }
155
156 int
157 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
158 const u_char **pkt_data)
159 {
160 struct pkt_for_fakecallback s;
161
162 s.hdr = &p->pcap_header;
163 s.pkt = pkt_data;
164
165 /* Saves a pointer to the packet headers */
166 *pkt_header= &p->pcap_header;
167
168 if (p->sf.rfile != NULL) {
169 int status;
170
171 /* We are on an offline capture */
172 status = pcap_offline_read(p, 1, pcap_fakecallback,
173 (u_char *)&s);
174
175 /*
176 * Return codes for pcap_offline_read() are:
177 * - 0: EOF
178 * - -1: error
179 * - >1: OK
180 * The first one ('0') conflicts with the return code of
181 * 0 from pcap_read() meaning "no packets arrived before
182 * the timeout expired", so we map it to -2 so you can
183 * distinguish between an EOF from a savefile and a
184 * "no packets arrived before the timeout expired, try
185 * again" from a live capture.
186 */
187 if (status == 0)
188 return (-2);
189 else
190 return (status);
191 }
192
193 /*
194 * Return codes for pcap_read() are:
195 * - 0: timeout
196 * - -1: error
197 * - -2: loop was broken out of with pcap_breakloop()
198 * - >1: OK
199 * The first one ('0') conflicts with the return code of 0 from
200 * pcap_offline_read() meaning "end of file".
201 */
202 return (p->read_op(p, 1, pcap_fakecallback, (u_char *)&s));
203 }
204
205 /*
206 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
207 */
208 void
209 pcap_breakloop(pcap_t *p)
210 {
211 p->break_loop = 1;
212 }
213
214 int
215 pcap_datalink(pcap_t *p)
216 {
217 return (p->linktype);
218 }
219
220 int
221 pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
222 {
223 if (p->dlt_count == 0) {
224 /*
225 * We couldn't fetch the list of DLTs, which means
226 * this platform doesn't support changing the
227 * DLT for an interface. Return a list of DLTs
228 * containing only the DLT this device supports.
229 */
230 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
231 if (*dlt_buffer == NULL) {
232 (void)snprintf(p->errbuf, sizeof(p->errbuf),
233 "malloc: %s", pcap_strerror(errno));
234 return (-1);
235 }
236 **dlt_buffer = p->linktype;
237 return (1);
238 } else {
239 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer) * p->dlt_count);
240 if (*dlt_buffer == NULL) {
241 (void)snprintf(p->errbuf, sizeof(p->errbuf),
242 "malloc: %s", pcap_strerror(errno));
243 return (-1);
244 }
245 (void)memcpy(*dlt_buffer, p->dlt_list,
246 sizeof(**dlt_buffer) * p->dlt_count);
247 return (p->dlt_count);
248 }
249 }
250
251 int
252 pcap_set_datalink(pcap_t *p, int dlt)
253 {
254 int i;
255 const char *dlt_name;
256
257 if (p->dlt_count == 0 || p->set_datalink_op == NULL) {
258 /*
259 * We couldn't fetch the list of DLTs, or we don't
260 * have a "set datalink" operation, which means
261 * this platform doesn't support changing the
262 * DLT for an interface. Check whether the new
263 * DLT is the one this interface supports.
264 */
265 if (p->linktype != dlt)
266 goto unsupported;
267
268 /*
269 * It is, so there's nothing we need to do here.
270 */
271 return (0);
272 }
273 for (i = 0; i < p->dlt_count; i++)
274 if (p->dlt_list[i] == dlt)
275 break;
276 if (i >= p->dlt_count)
277 goto unsupported;
278 if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB &&
279 dlt == DLT_DOCSIS) {
280 /*
281 * This is presumably an Ethernet device, as the first
282 * link-layer type it offers is DLT_EN10MB, and the only
283 * other type it offers is DLT_DOCSIS. That means that
284 * we can't tell the driver to supply DOCSIS link-layer
285 * headers - we're just pretending that's what we're
286 * getting, as, presumably, we're capturing on a dedicated
287 * link to a Cisco Cable Modem Termination System, and
288 * it's putting raw DOCSIS frames on the wire inside low-level
289 * Ethernet framing.
290 */
291 p->linktype = dlt;
292 return (0);
293 }
294 if (p->set_datalink_op(p, dlt) == -1)
295 return (-1);
296 p->linktype = dlt;
297 return (0);
298
299 unsupported:
300 dlt_name = pcap_datalink_val_to_name(dlt);
301 if (dlt_name != NULL) {
302 (void) snprintf(p->errbuf, sizeof(p->errbuf),
303 "%s is not one of the DLTs supported by this device",
304 dlt_name);
305 } else {
306 (void) snprintf(p->errbuf, sizeof(p->errbuf),
307 "DLT %d is not one of the DLTs supported by this device",
308 dlt);
309 }
310 return (-1);
311 }
312
313 struct dlt_choice {
314 const char *name;
315 const char *description;
316 int dlt;
317 };
318
319 #define DLT_CHOICE(code, description) { #code, description, code }
320 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
321
322 static struct dlt_choice dlt_choices[] = {
323 DLT_CHOICE(DLT_NULL, "BSD loopback"),
324 DLT_CHOICE(DLT_EN10MB, "Ethernet"),
325 DLT_CHOICE(DLT_IEEE802, "Token ring"),
326 DLT_CHOICE(DLT_ARCNET, "ARCNET"),
327 DLT_CHOICE(DLT_SLIP, "SLIP"),
328 DLT_CHOICE(DLT_PPP, "PPP"),
329 DLT_CHOICE(DLT_FDDI, "FDDI"),
330 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"),
331 DLT_CHOICE(DLT_RAW, "Raw IP"),
332 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
333 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
334 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
335 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
336 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
337 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
338 DLT_CHOICE(DLT_IEEE802_11, "802.11"),
339 DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
340 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
341 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
342 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
343 DLT_CHOICE(DLT_LTALK, "Localtalk"),
344 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
345 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
346 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
347 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
348 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus BSD radio information header"),
349 DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
350 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
351 DLT_CHOICE(DLT_DOCSIS, "DOCSIS"),
352 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
353 DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
354 DLT_CHOICE(DLT_SYMANTEC_FIREWALL, "Symantec Firewall"),
355 DLT_CHOICE(DLT_JUNIPER_ATM1, "Juniper ATM1 PIC"),
356 DLT_CHOICE(DLT_JUNIPER_ATM2, "Juniper ATM2 PIC"),
357 DLT_CHOICE(DLT_PPP_WITHDIRECTION, "PPP with direction"),
358 DLT_CHOICE(DLT_JUNIPER_PPPOE, "Juniper PPPoE"),
359 DLT_CHOICE(DLT_JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"),
360 DLT_CHOICE(DLT_GPRS_LLC, "GPRS LLC"),
361 DLT_CHOICE(DLT_GPF_T, "GPF-T"),
362 DLT_CHOICE(DLT_GPF_F, "GPF-F"),
363 DLT_CHOICE_SENTINEL
364 };
365
366 /*
367 * This array is designed for mapping upper and lower case letter
368 * together for a case independent comparison. The mappings are
369 * based upon ascii character sequences.
370 */
371 static const u_char charmap[] = {
372 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
373 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
374 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
375 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
376 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
377 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
378 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
379 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
380 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
381 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
382 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
383 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
384 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
385 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
386 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
387 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
388 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
389 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
390 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
391 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
392 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
393 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
394 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
395 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
396 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
397 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
398 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
399 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
400 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
401 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
402 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
403 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
404 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
405 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
406 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
407 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
408 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
409 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
410 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
411 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
412 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
413 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
414 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
415 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
416 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
417 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
418 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
419 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
420 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
421 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
422 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
423 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
424 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
425 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
426 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
427 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
428 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
429 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
430 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
431 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
432 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
433 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
434 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
435 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
436 };
437
438 int
439 pcap_strcasecmp(const char *s1, const char *s2)
440 {
441 register const u_char *cm = charmap,
442 *us1 = (u_char *)s1,
443 *us2 = (u_char *)s2;
444
445 while (cm[*us1] == cm[*us2++])
446 if (*us1++ == '\0')
447 return(0);
448 return (cm[*us1] - cm[*--us2]);
449 }
450
451 int
452 pcap_datalink_name_to_val(const char *name)
453 {
454 int i;
455
456 for (i = 0; dlt_choices[i].name != NULL; i++) {
457 if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
458 name) == 0)
459 return (dlt_choices[i].dlt);
460 }
461 return (-1);
462 }
463
464 const char *
465 pcap_datalink_val_to_name(int dlt)
466 {
467 int i;
468
469 for (i = 0; dlt_choices[i].name != NULL; i++) {
470 if (dlt_choices[i].dlt == dlt)
471 return (dlt_choices[i].name + sizeof("DLT_") - 1);
472 }
473 return (NULL);
474 }
475
476 const char *
477 pcap_datalink_val_to_description(int dlt)
478 {
479 int i;
480
481 for (i = 0; dlt_choices[i].name != NULL; i++) {
482 if (dlt_choices[i].dlt == dlt)
483 return (dlt_choices[i].description);
484 }
485 return (NULL);
486 }
487
488 int
489 pcap_snapshot(pcap_t *p)
490 {
491 return (p->snapshot);
492 }
493
494 int
495 pcap_is_swapped(pcap_t *p)
496 {
497 return (p->sf.swapped);
498 }
499
500 int
501 pcap_major_version(pcap_t *p)
502 {
503 return (p->sf.version_major);
504 }
505
506 int
507 pcap_minor_version(pcap_t *p)
508 {
509 return (p->sf.version_minor);
510 }
511
512 FILE *
513 pcap_file(pcap_t *p)
514 {
515 return (p->sf.rfile);
516 }
517
518 int
519 pcap_fileno(pcap_t *p)
520 {
521 #ifndef WIN32
522 return (p->fd);
523 #else
524 if (p->adapter != NULL)
525 return ((int)(DWORD)p->adapter->hFile);
526 else
527 return (-1);
528 #endif
529 }
530
531 #ifndef WIN32
532 int
533 pcap_get_selectable_fd(pcap_t *p)
534 {
535 return (p->selectable_fd);
536 }
537 #endif
538
539 void
540 pcap_perror(pcap_t *p, char *prefix)
541 {
542 fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
543 }
544
545 char *
546 pcap_geterr(pcap_t *p)
547 {
548 return (p->errbuf);
549 }
550
551 int
552 pcap_getnonblock(pcap_t *p, char *errbuf)
553 {
554 return p->getnonblock_op(p, errbuf);
555 }
556
557 /*
558 * Get the current non-blocking mode setting, under the assumption that
559 * it's just the standard POSIX non-blocking flag.
560 *
561 * We don't look at "p->nonblock", in case somebody tweaked the FD
562 * directly.
563 */
564 #ifndef WIN32
565 int
566 pcap_getnonblock_fd(pcap_t *p, char *errbuf)
567 {
568 int fdflags;
569
570 fdflags = fcntl(p->fd, F_GETFL, 0);
571 if (fdflags == -1) {
572 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
573 pcap_strerror(errno));
574 return (-1);
575 }
576 if (fdflags & O_NONBLOCK)
577 return (1);
578 else
579 return (0);
580 }
581 #endif
582
583 int
584 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
585 {
586 return p->setnonblock_op(p, nonblock, errbuf);
587 }
588
589 #ifndef WIN32
590 /*
591 * Set non-blocking mode, under the assumption that it's just the
592 * standard POSIX non-blocking flag. (This can be called by the
593 * per-platform non-blocking-mode routine if that routine also
594 * needs to do some additional work.)
595 */
596 int
597 pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf)
598 {
599 int fdflags;
600
601 fdflags = fcntl(p->fd, F_GETFL, 0);
602 if (fdflags == -1) {
603 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
604 pcap_strerror(errno));
605 return (-1);
606 }
607 if (nonblock)
608 fdflags |= O_NONBLOCK;
609 else
610 fdflags &= ~O_NONBLOCK;
611 if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
612 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
613 pcap_strerror(errno));
614 return (-1);
615 }
616 return (0);
617 }
618 #endif
619
620 #ifdef WIN32
621 /*
622 * Generate a string for the last Win32-specific error (i.e. an error generated when
623 * calling a Win32 API).
624 * For errors occurred during standard C calls, we still use pcap_strerror()
625 */
626 char *
627 pcap_win32strerror(void)
628 {
629 DWORD error;
630 static char errbuf[PCAP_ERRBUF_SIZE+1];
631 int errlen;
632
633 error = GetLastError();
634 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
635 PCAP_ERRBUF_SIZE, NULL);
636
637 /*
638 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
639 * message. Get rid of it.
640 */
641 errlen = strlen(errbuf);
642 if (errlen >= 2) {
643 errbuf[errlen - 1] = '\0';
644 errbuf[errlen - 2] = '\0';
645 }
646 return (errbuf);
647 }
648 #endif
649
650 /*
651 * Not all systems have strerror().
652 */
653 char *
654 pcap_strerror(int errnum)
655 {
656 #ifdef HAVE_STRERROR
657 return (strerror(errnum));
658 #else
659 extern int sys_nerr;
660 extern const char *const sys_errlist[];
661 static char ebuf[20];
662
663 if ((unsigned int)errnum < sys_nerr)
664 return ((char *)sys_errlist[errnum]);
665 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
666 return(ebuf);
667 #endif
668 }
669
670 int
671 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
672 {
673 return p->setfilter_op(p, fp);
674 }
675
676 int
677 pcap_stats(pcap_t *p, struct pcap_stat *ps)
678 {
679 return p->stats_op(p, ps);
680 }
681
682 static int
683 pcap_stats_dead(pcap_t *p, struct pcap_stat *ps)
684 {
685 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
686 "Statistics aren't available from a pcap_open_dead pcap_t");
687 return (-1);
688 }
689
690 void
691 pcap_close_common(pcap_t *p)
692 {
693 if (p->buffer != NULL)
694 free(p->buffer);
695 if (p->fd >= 0)
696 close(p->fd);
697 }
698
699 static void
700 pcap_close_dead(pcap_t *p)
701 {
702 /* Nothing to do. */
703 }
704
705 pcap_t *
706 pcap_open_dead(int linktype, int snaplen)
707 {
708 pcap_t *p;
709
710 p = malloc(sizeof(*p));
711 if (p == NULL)
712 return NULL;
713 memset (p, 0, sizeof(*p));
714 p->snapshot = snaplen;
715 p->linktype = linktype;
716 p->stats_op = pcap_stats_dead;
717 p->close_op = pcap_close_dead;
718 return p;
719 }
720
721 /*
722 * API compatible with WinPcap's "send a packet" routine - returns -1
723 * on error, 0 otherwise.
724 *
725 * XXX - what if we get a short write?
726 */
727 int
728 pcap_sendpacket(pcap_t *p, const u_char *buf, int size)
729 {
730 if (p->inject_op(p, buf, size) == -1)
731 return (-1);
732 return (0);
733 }
734
735 /*
736 * API compatible with OpenBSD's "send a packet" routine - returns -1 on
737 * error, number of bytes written otherwise.
738 */
739 int
740 pcap_inject(pcap_t *p, const void *buf, size_t size)
741 {
742 return (p->inject_op(p, buf, size));
743 }
744
745 void
746 pcap_close(pcap_t *p)
747 {
748 p->close_op(p);
749 if (p->dlt_list != NULL)
750 free(p->dlt_list);
751 pcap_freecode(&p->fcode);
752 free(p);
753 }
754
755 /*
756 * We make the version string static, and return a pointer to it, rather
757 * than exporting the version string directly. On at least some UNIXes,
758 * if you import data from a shared library into an program, the data is
759 * bound into the program binary, so if the string in the version of the
760 * library with which the program was linked isn't the same as the
761 * string in the version of the library with which the program is being
762 * run, various undesirable things may happen (warnings, the string
763 * being the one from the version of the library with which the program
764 * was linked, or even weirder things, such as the string being the one
765 * from the library but being truncated).
766 */
767 #ifdef WIN32
768 /*
769 * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap
770 * version numbers when building WinPcap. (It'd be nice to do so for
771 * the packet.dll version number as well.)
772 */
773 static const char wpcap_version_string[] = "3.0";
774 static const char pcap_version_string_fmt[] =
775 "WinPcap version %s, based on libpcap version 0.8";
776 static const char pcap_version_string_packet_dll_fmt[] =
777 "WinPcap version %s (packet.dll version %s), based on libpcap version 0.8";
778 static char *pcap_version_string;
779
780 const char *
781 pcap_lib_version(void)
782 {
783 char *packet_version_string;
784 size_t pcap_version_string_len;
785
786 if (pcap_version_string == NULL) {
787 /*
788 * Generate the version string.
789 */
790 packet_version_string = PacketGetVersion();
791 if (strcmp(wpcap_version_string, packet_version_string) == 0) {
792 /*
793 * WinPcap version string and packet.dll version
794 * string are the same; just report the WinPcap
795 * version.
796 */
797 pcap_version_string_len =
798 (sizeof pcap_version_string_fmt - 2) +
799 strlen(wpcap_version_string);
800 pcap_version_string = malloc(pcap_version_string_len);
801 sprintf(pcap_version_string, pcap_version_string_fmt,
802 wpcap_version_string);
803 } else {
804 /*
805 * WinPcap version string and packet.dll version
806 * string are different; that shouldn't be the
807 * case (the two libraries should come from the
808 * same version of WinPcap), so we report both
809 * versions.
810 */
811 pcap_version_string_len =
812 (sizeof pcap_version_string_packet_dll_fmt - 4) +
813 strlen(wpcap_version_string) +
814 strlen(packet_version_string);
815 pcap_version_string = malloc(pcap_version_string_len);
816 sprintf(pcap_version_string,
817 pcap_version_string_packet_dll_fmt,
818 wpcap_version_string, packet_version_string);
819 }
820 }
821 return (pcap_version_string);
822 }
823 #else
824 #include "version.h"
825
826 const char *
827 pcap_lib_version(void)
828 {
829 return (pcap_version_string);
830 }
831 #endif