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.41 2002-08-02 03:44:21 guy Exp $ (LBL)";
44 #include <pcap-stdinc.h>
46 #include <sys/types.h>
58 #ifdef HAVE_OS_PROTO_H
65 pcap_dispatch(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
68 if (p
->sf
.rfile
!= NULL
)
69 return (pcap_offline_read(p
, cnt
, callback
, user
));
70 return (pcap_read(p
, cnt
, callback
, user
));
74 pcap_loop(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
79 if (p
->sf
.rfile
!= NULL
)
80 n
= pcap_offline_read(p
, cnt
, callback
, user
);
83 * XXX keep reading until we get something
84 * (or an error occurs)
87 n
= pcap_read(p
, cnt
, callback
, user
);
101 struct pcap_pkthdr
*hdr
;
107 pcap_oneshot(u_char
*userData
, const struct pcap_pkthdr
*h
, const u_char
*pkt
)
109 struct singleton
*sp
= (struct singleton
*)userData
;
115 pcap_next(pcap_t
*p
, struct pcap_pkthdr
*h
)
120 if (pcap_dispatch(p
, 1, pcap_oneshot
, (u_char
*)&s
) <= 0)
126 pcap_datalink(pcap_t
*p
)
128 return (p
->linktype
);
132 pcap_snapshot(pcap_t
*p
)
134 return (p
->snapshot
);
138 pcap_is_swapped(pcap_t
*p
)
140 return (p
->sf
.swapped
);
144 pcap_major_version(pcap_t
*p
)
146 return (p
->sf
.version_major
);
150 pcap_minor_version(pcap_t
*p
)
152 return (p
->sf
.version_minor
);
158 return (p
->sf
.rfile
);
162 pcap_fileno(pcap_t
*p
)
167 if (p
->adapter
!= NULL
)
168 return ((int)(DWORD
)p
->adapter
->hFile
);
175 pcap_perror(pcap_t
*p
, char *prefix
)
177 fprintf(stderr
, "%s: %s\n", prefix
, p
->errbuf
);
181 pcap_geterr(pcap_t
*p
)
187 * NOTE: in the future, these may need to call platform-dependent routines,
188 * e.g. on platforms with memory-mapped packet-capture mechanisms where
189 * "pcap_read()" uses "select()" or "poll()" to wait for packets to arrive.
192 pcap_getnonblock(pcap_t
*p
, char *errbuf
)
198 if (p
->sf
.rfile
!= NULL
) {
200 * This is a savefile, not a live capture file, so
201 * never say it's in non-blocking mode.
206 fdflags
= fcntl(p
->fd
, F_GETFL
, 0);
208 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "F_GETFL: %s",
209 pcap_strerror(errno
));
212 if (fdflags
& O_NONBLOCK
)
217 return (p
->nonblock
);
222 pcap_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
230 if (p
->sf
.rfile
!= NULL
) {
232 * This is a savefile, not a live capture file, so
233 * ignore requests to put it in non-blocking mode.
238 fdflags
= fcntl(p
->fd
, F_GETFL
, 0);
240 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "F_GETFL: %s",
241 pcap_strerror(errno
));
245 fdflags
|= O_NONBLOCK
;
247 fdflags
&= ~O_NONBLOCK
;
248 if (fcntl(p
->fd
, F_SETFL
, fdflags
) == -1) {
249 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "F_SETFL: %s",
250 pcap_strerror(errno
));
256 * Set the read timeout to -1 for non-blocking mode.
261 * Restore the timeout set when the device was opened.
262 * (Note that this may be -1, in which case we're not
263 * really leaving non-blocking mode.)
265 newtimeout
= p
->timeout
;
267 if (!PacketSetReadTimeout(p
->adapter
, newtimeout
)) {
268 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
269 "PacketSetReadTimeout: %s", pcap_win32strerror());
272 p
->nonblock
= (newtimeout
== -1);
279 * Generate a string for the last Win32-specific error (i.e. an error generated when
280 * calling a Win32 API).
281 * For errors occurred during standard C calls, we still use pcap_strerror()
284 pcap_win32strerror(void)
287 static char errbuf
[PCAP_ERRBUF_SIZE
+1];
290 error
= GetLastError();
291 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error
, 0, errbuf
,
292 PCAP_ERRBUF_SIZE
, NULL
);
295 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
296 * message. Get rid of it.
298 errlen
= strlen(errbuf
);
300 errbuf
[errlen
- 1] = '\0';
301 errbuf
[errlen
- 2] = '\0';
308 * Not all systems have strerror().
311 pcap_strerror(int errnum
)
314 return (strerror(errnum
));
317 extern const char *const sys_errlist
[];
318 static char ebuf
[20];
320 if ((unsigned int)errnum
< sys_nerr
)
321 return ((char *)sys_errlist
[errnum
]);
322 (void)snprintf(ebuf
, sizeof ebuf
, "Unknown error: %d", errnum
);
328 pcap_open_dead(int linktype
, int snaplen
)
332 p
= malloc(sizeof(*p
));
335 memset (p
, 0, sizeof(*p
));
341 p
->snapshot
= snaplen
;
342 p
->linktype
= linktype
;
347 pcap_close(pcap_t
*p
)
358 if (p
->adapter
!= NULL
) {
359 PacketCloseAdapter(p
->adapter
);
363 if (p
->sf
.rfile
!= NULL
) {
364 if (p
->sf
.rfile
!= stdin
)
365 (void)fclose(p
->sf
.rfile
);
366 if (p
->sf
.base
!= NULL
)
368 } else if (p
->buffer
!= NULL
)
371 pcap_freecode(&p
->fcode
);