Skip to content

Commit 9792990

Browse files
committed
Add a "stats" function pointer to the pcap_t structure, which handles
getting statistics for a pcap_t. Have "pcap_stats()" call it, rather than being a per-platform function; have stats routines for non-live pcap_t's that return an error.
1 parent e648c9e commit 9792990

14 files changed

+69
-53
lines changed

pcap-bpf.c

+4-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
#ifndef lint
2222
static const char rcsid[] =
23-
"@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.62 2003-07-25 03:25:45 guy Exp $ (LBL)";
23+
"@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.63 2003-07-25 04:04:56 guy Exp $ (LBL)";
2424
#endif
2525

2626
#ifdef HAVE_CONFIG_H
@@ -103,17 +103,11 @@ static int odmlockid = 0;
103103

104104
#include "gencode.h"
105105

106-
int
107-
pcap_stats(pcap_t *p, struct pcap_stat *ps)
106+
static int
107+
pcap_stats_bpf(pcap_t *p, struct pcap_stat *ps)
108108
{
109109
struct bpf_stat s;
110110

111-
#ifdef HAVE_DAG_API
112-
if (p->md.is_dag) {
113-
return dag_stats(p, ps);
114-
}
115-
#endif /* HAVE_DAG_API */
116-
117111
/*
118112
* "ps_recv" counts packets handed to the filter, not packets
119113
* that passed the filter. This includes packets later dropped
@@ -736,6 +730,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
736730
memset(p->buffer, 0x0, p->bufsize);
737731
#endif
738732

733+
p->stats_op = pcap_stats_bpf;
739734
p->close_op = pcap_close_bpf;
740735

741736
return (p);

pcap-dag.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#ifndef lint
2121
static const char rcsid[] =
22-
"@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.2 2003-07-25 03:25:45 guy Exp $ (LBL)";
22+
"@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.3 2003-07-25 04:04:57 guy Exp $ (LBL)";
2323
#endif
2424

2525
#ifdef HAVE_CONFIG_H
@@ -67,6 +67,8 @@ static int atexit_handler_installed = 0;
6767
#define dag_set_datalink_platform pcap_set_datalink_platform
6868
#endif /* DAG_ONLY */
6969

70+
static int dag_stats(pcap_t *p, struct pcap_stat *ps);
71+
7072
static void delete_pcap_dag(pcap_t *p) {
7173
pcap_dag_node_t *curr = NULL, *prev = NULL;
7274

@@ -385,12 +387,13 @@ pcap_t *dag_open_live(const char *device, int snaplen, int promisc, int to_ms, c
385387
return NULL;
386388
}
387389

390+
handle->stats_op = dag_stats;
388391
handle->close_op = dag_platform_close;
389392

390393
return handle;
391394
}
392395

393-
int dag_stats(pcap_t *p, struct pcap_stat *ps) {
396+
static int dag_stats(pcap_t *p, struct pcap_stat *ps) {
394397
/* This needs to be filled out correctly. Hopefully a dagapi call will
395398
provide all necessary information.
396399
*/

pcap-dag.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
*
88
* Author: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
99
*
10-
* @(#) $Header: /tcpdump/master/libpcap/pcap-dag.h,v 1.1 2003-07-23 05:29:21 guy Exp $ (LBL)
10+
* @(#) $Header: /tcpdump/master/libpcap/pcap-dag.h,v 1.2 2003-07-25 04:04:57 guy Exp $ (LBL)
1111
*/
1212

13-
int dag_stats(pcap_t *p, struct pcap_stat *ps);
1413
int dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user);
1514
pcap_t *dag_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf);
1615
int dag_setfilter(pcap_t *p, struct bpf_program *fp);
17-
void dag_platform_close(pcap_t *p);
1816

pcap-dlpi.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
#ifndef lint
4040
static const char rcsid[] =
41-
"@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.86 2003-07-25 03:25:45 guy Exp $ (LBL)";
41+
"@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.87 2003-07-25 04:04:57 guy Exp $ (LBL)";
4242
#endif
4343

4444
#ifdef HAVE_CONFIG_H
@@ -151,8 +151,8 @@ static int dlpi_kread(int, off_t, void *, u_int, char *);
151151
static int get_dlpi_ppa(int, const char *, int, char *);
152152
#endif
153153

154-
int
155-
pcap_stats(pcap_t *p, struct pcap_stat *ps)
154+
static int
155+
pcap_stats_dlpi(pcap_t *p, struct pcap_stat *ps)
156156
{
157157

158158
/*
@@ -672,6 +672,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
672672
goto bad;
673673
}
674674

675+
p->stats_op = pcap_stats_dlpi;
675676
p->close_op = pcap_close_dlpi;
676677

677678
return (p);

pcap-int.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3131
* SUCH DAMAGE.
3232
*
33-
* @(#) $Header: /tcpdump/master/libpcap/pcap-int.h,v 1.48 2003-07-25 03:25:46 guy Exp $ (LBL)
33+
* @(#) $Header: /tcpdump/master/libpcap/pcap-int.h,v 1.49 2003-07-25 04:04:57 guy Exp $ (LBL)
3434
*/
3535

3636
#ifndef pcap_int_h
@@ -118,6 +118,7 @@ struct pcap {
118118
/*
119119
* Methods.
120120
*/
121+
int (*stats_op)(pcap_t *, struct pcap_stat *);
121122
void (*close_op)(pcap_t *);
122123

123124
/*

pcap-linux.c

+6-10
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#ifndef lint
2929
static const char rcsid[] =
30-
"@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.91 2003-07-25 03:25:46 guy Exp $ (LBL)";
30+
"@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.92 2003-07-25 04:04:58 guy Exp $ (LBL)";
3131
#endif
3232

3333
/*
@@ -187,7 +187,8 @@ static void map_arphrd_to_dlt(pcap_t *, int, int);
187187
static int live_open_old(pcap_t *, const char *, int, int, char *);
188188
static int live_open_new(pcap_t *, const char *, int, int, char *);
189189
static int pcap_read_packet(pcap_t *, pcap_handler, u_char *);
190-
static void pcap_close_linux(pcap_t *handle);
190+
static int pcap_stats_linux(pcap_t *, struct pcap_stat *);
191+
static void pcap_close_linux(pcap_t *);
191192

192193
/*
193194
* Wrap some ioctl calls
@@ -394,6 +395,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
394395
return NULL;
395396
}
396397

398+
handle->stats_op = pcap_stats_linux;
397399
handle->close_op = pcap_close_linux;
398400

399401
return handle;
@@ -659,20 +661,14 @@ pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata)
659661
* patches); otherwise, that information isn't available, and we lie
660662
* and report 0 as the count of dropped packets.
661663
*/
662-
int
663-
pcap_stats(pcap_t *handle, struct pcap_stat *stats)
664+
static int
665+
pcap_stats_linux(pcap_t *handle, struct pcap_stat *stats)
664666
{
665667
#ifdef HAVE_TPACKET_STATS
666668
struct tpacket_stats kstats;
667669
socklen_t len = sizeof (struct tpacket_stats);
668670
#endif
669671

670-
#ifdef HAVE_DAG_API
671-
if (handle->md.is_dag) {
672-
return dag_stats(handle, stats);
673-
}
674-
#endif /* HAVE_DAG_API */
675-
676672
#ifdef HAVE_TPACKET_STATS
677673
/*
678674
* Try to get the packet counts from the kernel.

pcap-nit.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
#ifndef lint
2222
static const char rcsid[] =
23-
"@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.45 2003-07-25 03:25:46 guy Exp $ (LBL)";
23+
"@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.46 2003-07-25 04:04:58 guy Exp $ (LBL)";
2424
#endif
2525

2626
#ifdef HAVE_CONFIG_H
@@ -71,8 +71,8 @@ static const char rcsid[] =
7171
/* Forwards */
7272
static int nit_setflags(int, int, int, char *);
7373

74-
int
75-
pcap_stats(pcap_t *p, struct pcap_stat *ps)
74+
static int
75+
pcap_stats_nit(pcap_t *p, struct pcap_stat *ps)
7676
{
7777

7878
/*
@@ -260,6 +260,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
260260
goto bad;
261261
}
262262

263+
p->stats_op = pcap_stats_nit;
263264
p->close_op = pcap_close_nit;
264265

265266
return (p);

pcap-null.c

+1-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
#ifndef lint
2222
static const char rcsid[] =
23-
"@(#) $Header: /tcpdump/master/libpcap/pcap-null.c,v 1.16 2002-12-22 02:36:49 guy Exp $ (LBL)";
23+
"@(#) $Header: /tcpdump/master/libpcap/pcap-null.c,v 1.17 2003-07-25 04:04:58 guy Exp $ (LBL)";
2424
#endif
2525

2626
#ifdef HAVE_CONFIG_H
@@ -39,13 +39,6 @@ static const char rcsid[] =
3939

4040
static char nosup[] = "live packet capture not supported on this system";
4141

42-
int
43-
pcap_stats(pcap_t *p, struct pcap_stat *ps)
44-
{
45-
(void)snprintf(p->errbuf, sizeof(p->errbuf), "pcap_stats: %s", nosup);
46-
return (-1);
47-
}
48-
4942
int
5043
pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
5144
{

pcap-pf.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
#ifndef lint
2626
static const char rcsid[] =
27-
"@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.74 2003-07-25 03:25:47 guy Exp $ (LBL)";
27+
"@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.75 2003-07-25 04:04:59 guy Exp $ (LBL)";
2828
#endif
2929

3030
#ifdef HAVE_CONFIG_H
@@ -198,8 +198,8 @@ pcap_read(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
198198
return (n);
199199
}
200200

201-
int
202-
pcap_stats(pcap_t *p, struct pcap_stat *ps)
201+
static int
202+
pcap_stats_pf(pcap_t *p, struct pcap_stat *ps)
203203
{
204204

205205
/*
@@ -413,6 +413,7 @@ your system may not be properly configured; see the packetfilter(4) man page\n",
413413
goto bad;
414414
}
415415

416+
p->stats_op = pcap_stats_pf;
416417
p->close_op = pcap_close_pf;
417418

418419
return (p);

pcap-snit.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#ifndef lint
2727
static const char rcsid[] =
28-
"@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.61 2003-07-25 03:25:47 guy Exp $ (LBL)";
28+
"@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.62 2003-07-25 04:04:59 guy Exp $ (LBL)";
2929
#endif
3030

3131
#ifdef HAVE_CONFIG_H
@@ -84,8 +84,8 @@ static const char rcsid[] =
8484
/* Forwards */
8585
static int nit_setflags(int, int, int, char *);
8686

87-
int
88-
pcap_stats(pcap_t *p, struct pcap_stat *ps)
87+
static int
88+
pcap_stats_snit(pcap_t *p, struct pcap_stat *ps)
8989
{
9090

9191
/*
@@ -318,6 +318,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
318318
goto bad;
319319
}
320320

321+
p->stats_op = pcap_stats_snit;
321322
p->close_op = pcap_close_snit;
322323

323324
return (p);

pcap-snoop.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
#ifndef lint
2222
static const char rcsid[] =
23-
"@(#) $Header: /tcpdump/master/libpcap/pcap-snoop.c,v 1.40 2003-07-25 03:25:47 guy Exp $ (LBL)";
23+
"@(#) $Header: /tcpdump/master/libpcap/pcap-snoop.c,v 1.41 2003-07-25 04:04:59 guy Exp $ (LBL)";
2424
#endif
2525

2626
#ifdef HAVE_CONFIG_H
@@ -102,8 +102,8 @@ pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
102102
return (0);
103103
}
104104

105-
int
106-
pcap_stats(pcap_t *p, struct pcap_stat *ps)
105+
static int
106+
pcap_stats_snoop(pcap_t *p, struct pcap_stat *ps)
107107
{
108108
register struct rawstats *rs;
109109
struct rawstats rawstats;
@@ -294,6 +294,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
294294
goto bad;
295295
}
296296

297+
p->stats_op = pcap_stats_snoop;
297298
p->close_op = pcap_close_snoop;
298299

299300
return (p);

pcap-win32.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
#ifndef lint
3434
static const char rcsid[] =
35-
"@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.9 2003-07-25 03:25:47 guy Exp $ (LBL)";
35+
"@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.10 2003-07-25 04:04:59 guy Exp $ (LBL)";
3636
#endif
3737

3838
#include <pcap-int.h>
@@ -75,8 +75,8 @@ wsockinit()
7575
}
7676

7777

78-
int
79-
pcap_stats(pcap_t *p, struct pcap_stat *ps)
78+
static int
79+
pcap_stats_win32(pcap_t *p, struct pcap_stat *ps)
8080
{
8181

8282
if(PacketGetStats(p->adapter, (struct bpf_stat*)ps) != TRUE){
@@ -254,6 +254,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
254254

255255
PacketSetReadTimeout(p->adapter, to_ms);
256256

257+
p->stats_op = pcap_stats_win32;
257258
p->close_op = pcap_close_win32;
258259

259260
return (p);

pcap.c

+16-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
#ifndef lint
3535
static const char rcsid[] =
36-
"@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.57 2003-07-25 03:25:48 guy Exp $ (LBL)";
36+
"@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.58 2003-07-25 04:05:00 guy Exp $ (LBL)";
3737
#endif
3838

3939
#ifdef HAVE_CONFIG_H
@@ -610,6 +610,20 @@ pcap_strerror(int errnum)
610610
#endif
611611
}
612612

613+
int
614+
pcap_stats(pcap_t *p, struct pcap_stat *ps)
615+
{
616+
return p->stats_op(p, ps);
617+
}
618+
619+
static int
620+
pcap_stats_dead(pcap_t *p, struct pcap_stat *ps)
621+
{
622+
snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
623+
"Statistics aren't available from a pcap_open_dead pcap_t");
624+
return (-1);
625+
}
626+
613627
static void
614628
pcap_close_dead(pcap_t *p)
615629
{
@@ -627,6 +641,7 @@ pcap_open_dead(int linktype, int snaplen)
627641
memset (p, 0, sizeof(*p));
628642
p->snapshot = snaplen;
629643
p->linktype = linktype;
644+
p->stats_op = pcap_stats_dead;
630645
p->close_op = pcap_close_dead;
631646
return p;
632647
}

0 commit comments

Comments
 (0)