]> The Tcpdump Group git mirrors - libpcap/blob - pcap.c
042687ab5fb50e9127b54726b4bc73c9cf9945d0
[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.41 2002-08-02 03:44:21 guy Exp $ (LBL)";
37 #endif
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #ifdef WIN32
44 #include <pcap-stdinc.h>
45 #else /* WIN32 */
46 #include <sys/types.h>
47 #endif /* WIN32 */
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #ifndef WIN32
53 #include <unistd.h>
54 #endif
55 #include <fcntl.h>
56 #include <errno.h>
57
58 #ifdef HAVE_OS_PROTO_H
59 #include "os-proto.h"
60 #endif
61
62 #include "pcap-int.h"
63
64 int
65 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
66 {
67
68 if (p->sf.rfile != NULL)
69 return (pcap_offline_read(p, cnt, callback, user));
70 return (pcap_read(p, cnt, callback, user));
71 }
72
73 int
74 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
75 {
76 register int n;
77
78 for (;;) {
79 if (p->sf.rfile != NULL)
80 n = pcap_offline_read(p, cnt, callback, user);
81 else {
82 /*
83 * XXX keep reading until we get something
84 * (or an error occurs)
85 */
86 do {
87 n = pcap_read(p, cnt, callback, user);
88 } while (n == 0);
89 }
90 if (n <= 0)
91 return (n);
92 if (cnt > 0) {
93 cnt -= n;
94 if (cnt <= 0)
95 return (0);
96 }
97 }
98 }
99
100 struct singleton {
101 struct pcap_pkthdr *hdr;
102 const u_char *pkt;
103 };
104
105
106 static void
107 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt)
108 {
109 struct singleton *sp = (struct singleton *)userData;
110 *sp->hdr = *h;
111 sp->pkt = pkt;
112 }
113
114 const u_char *
115 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
116 {
117 struct singleton s;
118
119 s.hdr = h;
120 if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0)
121 return (0);
122 return (s.pkt);
123 }
124
125 int
126 pcap_datalink(pcap_t *p)
127 {
128 return (p->linktype);
129 }
130
131 int
132 pcap_snapshot(pcap_t *p)
133 {
134 return (p->snapshot);
135 }
136
137 int
138 pcap_is_swapped(pcap_t *p)
139 {
140 return (p->sf.swapped);
141 }
142
143 int
144 pcap_major_version(pcap_t *p)
145 {
146 return (p->sf.version_major);
147 }
148
149 int
150 pcap_minor_version(pcap_t *p)
151 {
152 return (p->sf.version_minor);
153 }
154
155 FILE *
156 pcap_file(pcap_t *p)
157 {
158 return (p->sf.rfile);
159 }
160
161 int
162 pcap_fileno(pcap_t *p)
163 {
164 #ifndef WIN32
165 return (p->fd);
166 #else
167 if (p->adapter != NULL)
168 return ((int)(DWORD)p->adapter->hFile);
169 else
170 return (-1);
171 #endif
172 }
173
174 void
175 pcap_perror(pcap_t *p, char *prefix)
176 {
177 fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
178 }
179
180 char *
181 pcap_geterr(pcap_t *p)
182 {
183 return (p->errbuf);
184 }
185
186 /*
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.
190 */
191 int
192 pcap_getnonblock(pcap_t *p, char *errbuf)
193 {
194 #ifndef WIN32
195 int fdflags;
196 #endif
197
198 if (p->sf.rfile != NULL) {
199 /*
200 * This is a savefile, not a live capture file, so
201 * never say it's in non-blocking mode.
202 */
203 return (0);
204 }
205 #ifndef WIN32
206 fdflags = fcntl(p->fd, F_GETFL, 0);
207 if (fdflags == -1) {
208 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
209 pcap_strerror(errno));
210 return (-1);
211 }
212 if (fdflags & O_NONBLOCK)
213 return (1);
214 else
215 return (0);
216 #else
217 return (p->nonblock);
218 #endif
219 }
220
221 int
222 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
223 {
224 #ifndef WIN32
225 int fdflags;
226 #else
227 int newtimeout;
228 #endif
229
230 if (p->sf.rfile != NULL) {
231 /*
232 * This is a savefile, not a live capture file, so
233 * ignore requests to put it in non-blocking mode.
234 */
235 return (0);
236 }
237 #ifndef WIN32
238 fdflags = fcntl(p->fd, F_GETFL, 0);
239 if (fdflags == -1) {
240 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
241 pcap_strerror(errno));
242 return (-1);
243 }
244 if (nonblock)
245 fdflags |= O_NONBLOCK;
246 else
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));
251 return (-1);
252 }
253 #else
254 if (nonblock) {
255 /*
256 * Set the read timeout to -1 for non-blocking mode.
257 */
258 newtimeout = -1;
259 } else {
260 /*
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.)
264 */
265 newtimeout = p->timeout;
266 }
267 if (!PacketSetReadTimeout(p->adapter, newtimeout)) {
268 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
269 "PacketSetReadTimeout: %s", pcap_win32strerror());
270 return (-1);
271 }
272 p->nonblock = (newtimeout == -1);
273 #endif
274 return (0);
275 }
276
277 #ifdef WIN32
278 /*
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()
282 */
283 char *
284 pcap_win32strerror(void)
285 {
286 DWORD error;
287 static char errbuf[PCAP_ERRBUF_SIZE+1];
288 int errlen;
289
290 error = GetLastError();
291 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
292 PCAP_ERRBUF_SIZE, NULL);
293
294 /*
295 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
296 * message. Get rid of it.
297 */
298 errlen = strlen(errbuf);
299 if (errlen >= 2) {
300 errbuf[errlen - 1] = '\0';
301 errbuf[errlen - 2] = '\0';
302 }
303 return (errbuf);
304 }
305 #endif
306
307 /*
308 * Not all systems have strerror().
309 */
310 char *
311 pcap_strerror(int errnum)
312 {
313 #ifdef HAVE_STRERROR
314 return (strerror(errnum));
315 #else
316 extern int sys_nerr;
317 extern const char *const sys_errlist[];
318 static char ebuf[20];
319
320 if ((unsigned int)errnum < sys_nerr)
321 return ((char *)sys_errlist[errnum]);
322 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
323 return(ebuf);
324 #endif
325 }
326
327 pcap_t *
328 pcap_open_dead(int linktype, int snaplen)
329 {
330 pcap_t *p;
331
332 p = malloc(sizeof(*p));
333 if (p == NULL)
334 return NULL;
335 memset (p, 0, sizeof(*p));
336 #ifndef WIN32
337 p->fd = -1;
338 #else
339 p->adapter = NULL;
340 #endif /* WIN32 */
341 p->snapshot = snaplen;
342 p->linktype = linktype;
343 return p;
344 }
345
346 void
347 pcap_close(pcap_t *p)
348 {
349 /*XXX*/
350 #ifndef WIN32
351 if (p->fd >= 0) {
352 #ifdef linux
353 pcap_close_linux(p);
354 #endif
355 close(p->fd);
356 }
357 #else /* WIN32 */
358 if (p->adapter != NULL) {
359 PacketCloseAdapter(p->adapter);
360 p->adapter = NULL;
361 }
362 #endif /* WIN32 */
363 if (p->sf.rfile != NULL) {
364 if (p->sf.rfile != stdin)
365 (void)fclose(p->sf.rfile);
366 if (p->sf.base != NULL)
367 free(p->sf.base);
368 } else if (p->buffer != NULL)
369 free(p->buffer);
370
371 pcap_freecode(&p->fcode);
372 free(p);
373 }