]> The Tcpdump Group git mirrors - libpcap/blob - pcap.c
69e585686a1d4758221049f69a1d41bcfed42cc6
[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[] =
36 "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.38 2001-12-29 21:55:32 guy Exp $ (LBL)";
37 #endif
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include <sys/types.h>
44
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <errno.h>
51
52 #ifdef HAVE_OS_PROTO_H
53 #include "os-proto.h"
54 #endif
55
56 #include "pcap-int.h"
57
58 int
59 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
60 {
61
62 if (p->sf.rfile != NULL)
63 return (pcap_offline_read(p, cnt, callback, user));
64 return (pcap_read(p, cnt, callback, user));
65 }
66
67 int
68 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
69 {
70 register int n;
71
72 for (;;) {
73 if (p->sf.rfile != NULL)
74 n = pcap_offline_read(p, cnt, callback, user);
75 else {
76 /*
77 * XXX keep reading until we get something
78 * (or an error occurs)
79 */
80 do {
81 n = pcap_read(p, cnt, callback, user);
82 } while (n == 0);
83 }
84 if (n <= 0)
85 return (n);
86 if (cnt > 0) {
87 cnt -= n;
88 if (cnt <= 0)
89 return (0);
90 }
91 }
92 }
93
94 struct singleton {
95 struct pcap_pkthdr *hdr;
96 const u_char *pkt;
97 };
98
99
100 static void
101 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt)
102 {
103 struct singleton *sp = (struct singleton *)userData;
104 *sp->hdr = *h;
105 sp->pkt = pkt;
106 }
107
108 const u_char *
109 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
110 {
111 struct singleton s;
112
113 s.hdr = h;
114 if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0)
115 return (0);
116 return (s.pkt);
117 }
118
119 int
120 pcap_datalink(pcap_t *p)
121 {
122 return (p->linktype);
123 }
124
125 int
126 pcap_snapshot(pcap_t *p)
127 {
128 return (p->snapshot);
129 }
130
131 int
132 pcap_is_swapped(pcap_t *p)
133 {
134 return (p->sf.swapped);
135 }
136
137 int
138 pcap_major_version(pcap_t *p)
139 {
140 return (p->sf.version_major);
141 }
142
143 int
144 pcap_minor_version(pcap_t *p)
145 {
146 return (p->sf.version_minor);
147 }
148
149 FILE *
150 pcap_file(pcap_t *p)
151 {
152 return (p->sf.rfile);
153 }
154
155 int
156 pcap_fileno(pcap_t *p)
157 {
158 return (p->fd);
159 }
160
161 void
162 pcap_perror(pcap_t *p, char *prefix)
163 {
164 fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
165 }
166
167 char *
168 pcap_geterr(pcap_t *p)
169 {
170 return (p->errbuf);
171 }
172
173 /*
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.
177 */
178 int
179 pcap_getnonblock(pcap_t *p, char *errbuf)
180 {
181 int fdflags;
182
183 if (p->sf.rfile != NULL) {
184 /*
185 * This is a savefile, not a live capture file, so
186 * never say it's in non-blocking mode.
187 */
188 return (0);
189 }
190 fdflags = fcntl(p->fd, F_GETFL, 0);
191 if (fdflags == -1) {
192 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
193 pcap_strerror(errno));
194 return (-1);
195 }
196 if (fdflags & O_NONBLOCK)
197 return (1);
198 else
199 return (0);
200 }
201
202 int
203 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
204 {
205 int fdflags;
206
207 if (p->sf.rfile != NULL) {
208 /*
209 * This is a savefile, not a live capture file, so
210 * ignore requests to put it in non-blocking mode.
211 */
212 return (0);
213 }
214 fdflags = fcntl(p->fd, F_GETFL, 0);
215 if (fdflags == -1) {
216 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
217 pcap_strerror(errno));
218 return (-1);
219 }
220 if (nonblock)
221 fdflags |= O_NONBLOCK;
222 else
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));
227 return (-1);
228 }
229 return (0);
230 }
231
232 /*
233 * Not all systems have strerror().
234 */
235 char *
236 pcap_strerror(int errnum)
237 {
238 #ifdef HAVE_STRERROR
239 return (strerror(errnum));
240 #else
241 extern int sys_nerr;
242 extern const char *const sys_errlist[];
243 static char ebuf[20];
244
245 if ((unsigned int)errnum < sys_nerr)
246 return ((char *)sys_errlist[errnum]);
247 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
248 return(ebuf);
249 #endif
250 }
251
252 pcap_t *
253 pcap_open_dead(int linktype, int snaplen)
254 {
255 pcap_t *p;
256
257 p = malloc(sizeof(*p));
258 if (p == NULL)
259 return NULL;
260 memset (p, 0, sizeof(*p));
261 p->fd = -1;
262 p->snapshot = snaplen;
263 p->linktype = linktype;
264 return p;
265 }
266
267 void
268 pcap_close(pcap_t *p)
269 {
270 /*XXX*/
271 if (p->fd >= 0) {
272 #ifdef linux
273 pcap_close_linux(p);
274 #endif
275 close(p->fd);
276 }
277 if (p->sf.rfile != NULL) {
278 if (p->sf.rfile != stdin)
279 (void)fclose(p->sf.rfile);
280 if (p->sf.base != NULL)
281 free(p->sf.base);
282 } else if (p->buffer != NULL)
283 free(p->buffer);
284
285 pcap_freecode(&p->fcode);
286 free(p);
287 }