]>
The Tcpdump Group git mirrors - libpcap/blob - pcap.c
72caa6fb6ee5edfb8ae606e348d4cebe58c94070
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.34 2000-10-25 06:59:10 guy Exp $ (LBL)";
43 #include <sys/types.h>
50 #ifdef HAVE_OS_PROTO_H
57 pcap_dispatch(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
60 if (p
->sf
.rfile
!= NULL
)
61 return (pcap_offline_read(p
, cnt
, callback
, user
));
62 return (pcap_read(p
, cnt
, callback
, user
));
66 pcap_loop(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
71 if (p
->sf
.rfile
!= NULL
)
72 n
= pcap_offline_read(p
, cnt
, callback
, user
);
75 * XXX keep reading until we get something
76 * (or an error occurs)
79 n
= pcap_read(p
, cnt
, callback
, user
);
93 struct pcap_pkthdr
*hdr
;
99 pcap_oneshot(u_char
*userData
, const struct pcap_pkthdr
*h
, const u_char
*pkt
)
101 struct singleton
*sp
= (struct singleton
*)userData
;
107 pcap_next(pcap_t
*p
, struct pcap_pkthdr
*h
)
112 if (pcap_dispatch(p
, 1, pcap_oneshot
, (u_char
*)&s
) <= 0)
118 pcap_datalink(pcap_t
*p
)
120 return (p
->linktype
);
124 pcap_snapshot(pcap_t
*p
)
126 return (p
->snapshot
);
130 pcap_is_swapped(pcap_t
*p
)
132 return (p
->sf
.swapped
);
136 pcap_major_version(pcap_t
*p
)
138 return (p
->sf
.version_major
);
142 pcap_minor_version(pcap_t
*p
)
144 return (p
->sf
.version_minor
);
150 return (p
->sf
.rfile
);
154 pcap_fileno(pcap_t
*p
)
160 pcap_perror(pcap_t
*p
, char *prefix
)
162 fprintf(stderr
, "%s: %s\n", prefix
, p
->errbuf
);
166 pcap_geterr(pcap_t
*p
)
172 * Not all systems have strerror().
175 pcap_strerror(int errnum
)
178 return (strerror(errnum
));
181 extern const char *const sys_errlist
[];
182 static char ebuf
[20];
184 if ((unsigned int)errnum
< sys_nerr
)
185 return ((char *)sys_errlist
[errnum
]);
186 (void)snprintf(ebuf
, sizeof ebuf
, "Unknown error: %d", errnum
);
192 pcap_open_dead(int linktype
, int snaplen
)
196 p
= malloc(sizeof(*p
));
199 memset (p
, 0, sizeof(*p
));
201 p
->snapshot
= snaplen
;
202 p
->linktype
= linktype
;
207 pcap_close(pcap_t
*p
)
212 if (p
->sf
.rfile
!= NULL
) {
213 if (p
->sf
.rfile
!= stdin
)
214 (void)fclose(p
->sf
.rfile
);
215 if (p
->sf
.base
!= NULL
)
217 } else if (p
->buffer
!= NULL
)
220 if (p
->md
.device
!= NULL
)
222 pcap_freecode(p
, &p
->fcode
);