xref: /freebsd/contrib/libpcap/pcap.c (revision ce834215a70ff69e7e222827437116eee2f9ac6f)
1 /*
2  * Copyright (c) 1993, 1994, 1995, 1996
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: pcap.c,v 1.27 96/11/27 18:43:25 leres Exp $ (LBL)";
37 #endif
38 
39 #include <sys/types.h>
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include "gnuc.h"
47 #ifdef HAVE_OS_PROTO_H
48 #include "os-proto.h"
49 #endif
50 
51 #include "pcap-int.h"
52 
53 int
54 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
55 {
56 	register int cc;
57 
58 	if (p->sf.rfile != NULL)
59 		return (pcap_offline_read(p, cnt, callback, user));
60 	/* XXX keep reading until we get something (or an error occurs) */
61 	do {
62 		cc = pcap_read(p, cnt, callback, user);
63 	} while (cc == 0);
64 	return (cc);
65 }
66 
67 int
68 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
69 {
70 	for (;;) {
71 		int n = pcap_dispatch(p, cnt, callback, user);
72 		if (n <= 0)
73 			return (n);
74 		if (cnt > 0) {
75 			cnt -= n;
76 			if (cnt <= 0)
77 				return (0);
78 		}
79 	}
80 }
81 
82 struct singleton {
83 	struct pcap_pkthdr *hdr;
84 	const u_char *pkt;
85 };
86 
87 
88 static void
89 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt)
90 {
91 	struct singleton *sp = (struct singleton *)userData;
92 	*sp->hdr = *h;
93 	sp->pkt = pkt;
94 }
95 
96 const u_char *
97 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
98 {
99 	struct singleton s;
100 
101 	s.hdr = h;
102 	if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0)
103 		return (0);
104 	return (s.pkt);
105 }
106 
107 int
108 pcap_datalink(pcap_t *p)
109 {
110 	return (p->linktype);
111 }
112 
113 int
114 pcap_snapshot(pcap_t *p)
115 {
116 	return (p->snapshot);
117 }
118 
119 int
120 pcap_is_swapped(pcap_t *p)
121 {
122 	return (p->sf.swapped);
123 }
124 
125 int
126 pcap_major_version(pcap_t *p)
127 {
128 	return (p->sf.version_major);
129 }
130 
131 int
132 pcap_minor_version(pcap_t *p)
133 {
134 	return (p->sf.version_minor);
135 }
136 
137 FILE *
138 pcap_file(pcap_t *p)
139 {
140 	return (p->sf.rfile);
141 }
142 
143 int
144 pcap_fileno(pcap_t *p)
145 {
146 	return (p->fd);
147 }
148 
149 void
150 pcap_perror(pcap_t *p, char *prefix)
151 {
152 	fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
153 }
154 
155 char *
156 pcap_geterr(pcap_t *p)
157 {
158 	return (p->errbuf);
159 }
160 
161 /*
162  * Not all systems have strerror().
163  */
164 char *
165 pcap_strerror(int errnum)
166 {
167 #ifdef HAVE_STRERROR
168 	return (strerror(errnum));
169 #else
170 	extern int sys_nerr;
171 	extern const char *const sys_errlist[];
172 	static char ebuf[20];
173 
174 	if ((unsigned int)errnum < sys_nerr)
175 		return ((char *)sys_errlist[errnum]);
176 	(void)sprintf(ebuf, "Unknown error: %d", errnum);
177 	return(ebuf);
178 #endif
179 }
180 
181 void
182 pcap_close(pcap_t *p)
183 {
184 	/*XXX*/
185 	if (p->fd >= 0)
186 		close(p->fd);
187 	if (p->sf.rfile != NULL) {
188 		(void)fclose(p->sf.rfile);
189 		if (p->sf.base != NULL)
190 			free(p->sf.base);
191 	} else if (p->buffer != NULL)
192 		free(p->buffer);
193 #ifdef linux
194 	if (p->md.device != NULL)
195 		free(p->md.device);
196 #endif
197 
198 	free(p);
199 }
200