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