2 * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char rcsid[] =
36 "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.38 2001/12/29 21:55:32 guy Exp $ (LBL)";
43 #include <sys/types.h>
52 #ifdef HAVE_OS_PROTO_H
59 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
62 if (p->sf.rfile != NULL)
63 return (pcap_offline_read(p, cnt, callback, user));
64 return (pcap_read(p, cnt, callback, user));
68 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
73 if (p->sf.rfile != NULL)
74 n = pcap_offline_read(p, cnt, callback, user);
77 * XXX keep reading until we get something
78 * (or an error occurs)
81 n = pcap_read(p, cnt, callback, user);
95 struct pcap_pkthdr *hdr;
101 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt)
103 struct singleton *sp = (struct singleton *)userData;
109 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
114 if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0)
120 pcap_datalink(pcap_t *p)
122 return (p->linktype);
126 pcap_snapshot(pcap_t *p)
128 return (p->snapshot);
132 pcap_is_swapped(pcap_t *p)
134 return (p->sf.swapped);
138 pcap_major_version(pcap_t *p)
140 return (p->sf.version_major);
144 pcap_minor_version(pcap_t *p)
146 return (p->sf.version_minor);
152 return (p->sf.rfile);
156 pcap_fileno(pcap_t *p)
162 pcap_perror(pcap_t *p, char *prefix)
164 fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
168 pcap_geterr(pcap_t *p)
174 * NOTE: in the future, these may need to call platform-dependent routines,
175 * e.g. on platforms with memory-mapped packet-capture mechanisms where
176 * "pcap_read()" uses "select()" or "poll()" to wait for packets to arrive.
179 pcap_getnonblock(pcap_t *p, char *errbuf)
183 if (p->sf.rfile != NULL) {
185 * This is a savefile, not a live capture file, so
186 * never say it's in non-blocking mode.
190 fdflags = fcntl(p->fd, F_GETFL, 0);
192 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
193 pcap_strerror(errno));
196 if (fdflags & O_NONBLOCK)
203 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
207 if (p->sf.rfile != NULL) {
209 * This is a savefile, not a live capture file, so
210 * ignore requests to put it in non-blocking mode.
214 fdflags = fcntl(p->fd, F_GETFL, 0);
216 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
217 pcap_strerror(errno));
221 fdflags |= O_NONBLOCK;
223 fdflags &= ~O_NONBLOCK;
224 if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
225 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
226 pcap_strerror(errno));
233 * Not all systems have strerror().
236 pcap_strerror(int errnum)
239 return (strerror(errnum));
242 extern const char *const sys_errlist[];
243 static char ebuf[20];
245 if ((unsigned int)errnum < sys_nerr)
246 return ((char *)sys_errlist[errnum]);
247 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
253 pcap_open_dead(int linktype, int snaplen)
257 p = malloc(sizeof(*p));
260 memset (p, 0, sizeof(*p));
262 p->snapshot = snaplen;
263 p->linktype = linktype;
268 pcap_close(pcap_t *p)
277 if (p->sf.rfile != NULL) {
278 if (p->sf.rfile != stdin)
279 (void)fclose(p->sf.rfile);
280 if (p->sf.base != NULL)
282 } else if (p->buffer != NULL)
285 pcap_freecode(&p->fcode);