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