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.40 2002-08-01 08:33:04 risso Exp $ (LBL)";
43 #include <pcap-stdinc.h>
51 #ifdef HAVE_OS_PROTO_H
58 pcap_dispatch(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
61 if (p
->sf
.rfile
!= NULL
)
62 return (pcap_offline_read(p
, cnt
, callback
, user
));
63 return (pcap_read(p
, cnt
, callback
, user
));
67 pcap_loop(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
72 if (p
->sf
.rfile
!= NULL
)
73 n
= pcap_offline_read(p
, cnt
, callback
, user
);
76 * XXX keep reading until we get something
77 * (or an error occurs)
80 n
= pcap_read(p
, cnt
, callback
, user
);
94 struct pcap_pkthdr
*hdr
;
100 pcap_oneshot(u_char
*userData
, const struct pcap_pkthdr
*h
, const u_char
*pkt
)
102 struct singleton
*sp
= (struct singleton
*)userData
;
108 pcap_next(pcap_t
*p
, struct pcap_pkthdr
*h
)
113 if (pcap_dispatch(p
, 1, pcap_oneshot
, (u_char
*)&s
) <= 0)
119 pcap_datalink(pcap_t
*p
)
121 return (p
->linktype
);
125 pcap_snapshot(pcap_t
*p
)
127 return (p
->snapshot
);
131 pcap_is_swapped(pcap_t
*p
)
133 return (p
->sf
.swapped
);
137 pcap_major_version(pcap_t
*p
)
139 return (p
->sf
.version_major
);
143 pcap_minor_version(pcap_t
*p
)
145 return (p
->sf
.version_minor
);
151 return (p
->sf
.rfile
);
155 pcap_fileno(pcap_t
*p
)
160 if (p
->adapter
!= NULL
)
161 return ((int)(DWORD
)p
->adapter
->hFile
);
168 pcap_perror(pcap_t
*p
, char *prefix
)
170 fprintf(stderr
, "%s: %s\n", prefix
, p
->errbuf
);
174 pcap_geterr(pcap_t
*p
)
180 * NOTE: in the future, these may need to call platform-dependent routines,
181 * e.g. on platforms with memory-mapped packet-capture mechanisms where
182 * "pcap_read()" uses "select()" or "poll()" to wait for packets to arrive.
185 pcap_getnonblock(pcap_t
*p
, char *errbuf
)
191 if (p
->sf
.rfile
!= NULL
) {
193 * This is a savefile, not a live capture file, so
194 * never say it's in non-blocking mode.
199 fdflags
= fcntl(p
->fd
, F_GETFL
, 0);
201 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "F_GETFL: %s",
202 pcap_strerror(errno
));
205 if (fdflags
& O_NONBLOCK
)
210 return (p
->nonblock
);
215 pcap_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
223 if (p
->sf
.rfile
!= NULL
) {
225 * This is a savefile, not a live capture file, so
226 * ignore requests to put it in non-blocking mode.
231 fdflags
= fcntl(p
->fd
, F_GETFL
, 0);
233 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "F_GETFL: %s",
234 pcap_strerror(errno
));
238 fdflags
|= O_NONBLOCK
;
240 fdflags
&= ~O_NONBLOCK
;
241 if (fcntl(p
->fd
, F_SETFL
, fdflags
) == -1) {
242 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "F_SETFL: %s",
243 pcap_strerror(errno
));
249 * Set the read timeout to -1 for non-blocking mode.
254 * Restore the timeout set when the device was opened.
255 * (Note that this may be -1, in which case we're not
256 * really leaving non-blocking mode.)
258 newtimeout
= p
->timeout
;
260 if (!PacketSetReadTimeout(p
->adapter
, newtimeout
)) {
261 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
262 "PacketSetReadTimeout: %s", pcap_win32strerror());
265 p
->nonblock
= (newtimeout
== -1);
272 * Generate a string for the last Win32-specific error (i.e. an error generated when
273 * calling a Win32 API).
274 * For errors occurred during standard C calls, we still use pcap_strerror()
277 pcap_win32strerror(void)
280 static char errbuf
[PCAP_ERRBUF_SIZE
+1];
283 error
= GetLastError();
284 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error
, 0, errbuf
,
285 PCAP_ERRBUF_SIZE
, NULL
);
288 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
289 * message. Get rid of it.
291 errlen
= strlen(errbuf
);
293 errbuf
[errlen
- 1] = '\0';
294 errbuf
[errlen
- 2] = '\0';
301 * Not all systems have strerror().
304 pcap_strerror(int errnum
)
307 return (strerror(errnum
));
310 extern const char *const sys_errlist
[];
311 static char ebuf
[20];
313 if ((unsigned int)errnum
< sys_nerr
)
314 return ((char *)sys_errlist
[errnum
]);
315 (void)snprintf(ebuf
, sizeof ebuf
, "Unknown error: %d", errnum
);
321 pcap_open_dead(int linktype
, int snaplen
)
325 p
= malloc(sizeof(*p
));
328 memset (p
, 0, sizeof(*p
));
334 p
->snapshot
= snaplen
;
335 p
->linktype
= linktype
;
340 pcap_close(pcap_t
*p
)
351 if (p
->adapter
!= NULL
) {
352 PacketCloseAdapter(p
->adapter
);
356 if (p
->sf
.rfile
!= NULL
) {
357 if (p
->sf
.rfile
!= stdin
)
358 (void)fclose(p
->sf
.rfile
);
359 if (p
->sf
.base
!= NULL
)
361 } else if (p
->buffer
!= NULL
)
364 pcap_freecode(&p
->fcode
);