Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libpcap * Add support for DLT_IEEE802_11.



details:   https://anonhg.NetBSD.org/src/rev/3a773547a0f2
branches:  trunk
changeset: 536817:3a773547a0f2
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Sun Sep 22 16:13:01 2002 +0000

description:
* Add support for DLT_IEEE802_11.
* When opening a live pcap, obtain the list of supported DLTs from
  the BPF.
* Add pcap_list_datalinks() to obtain a list of supported DLTs
  supported by the interface associated with the pcap descriptor.
* Add pcap_set_datalink() to set the current DLT of the pcap.
* Bump shlib 1.2 -> 1.3; new functions added.

>From David Young <dyoung%ojctech.com@localhost>, with some minor changes by me.

diffstat:

 lib/libpcap/gencode.c     |   9 +++++-
 lib/libpcap/pcap-bpf.c    |  55 +++++++++++++++++++++++++++++++++++++++++++++-
 lib/libpcap/pcap-int.h    |   4 ++-
 lib/libpcap/pcap.3        |  28 +++++++++++++++++++++++-
 lib/libpcap/pcap.c        |  25 +++++++++++++++++++-
 lib/libpcap/pcap.h        |   4 ++-
 lib/libpcap/shlib_version |   4 +-
 7 files changed, 118 insertions(+), 11 deletions(-)

diffs (283 lines):

diff -r fe566c9ebccf -r 3a773547a0f2 lib/libpcap/gencode.c
--- a/lib/libpcap/gencode.c     Sun Sep 22 16:07:31 2002 +0000
+++ b/lib/libpcap/gencode.c     Sun Sep 22 16:13:01 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gencode.c,v 1.31 2002/08/26 11:21:18 yamt Exp $        */
+/*     $NetBSD: gencode.c,v 1.32 2002/09/22 16:13:01 thorpej Exp $     */
 
 /*
  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
@@ -26,7 +26,7 @@
 static const char rcsid[] =
     "@(#) Header: gencode.c,v 1.93 97/06/12 14:22:47 leres Exp  (LBL)";
 #else
-__RCSID("$NetBSD: gencode.c,v 1.31 2002/08/26 11:21:18 yamt Exp $");
+__RCSID("$NetBSD: gencode.c,v 1.32 2002/09/22 16:13:01 thorpej Exp $");
 #endif
 #endif
 
@@ -562,6 +562,11 @@
                off_nl = 6;     /* XXX in reality, variable! */
                return;
 
+       case DLT_IEEE802_11:
+               off_linktype = 30; /* XXX variable */
+               off_nl = 32;
+               return;
+
        case DLT_EN10MB:
                off_linktype = 12;
                off_nl = 14;
diff -r fe566c9ebccf -r 3a773547a0f2 lib/libpcap/pcap-bpf.c
--- a/lib/libpcap/pcap-bpf.c    Sun Sep 22 16:07:31 2002 +0000
+++ b/lib/libpcap/pcap-bpf.c    Sun Sep 22 16:13:01 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pcap-bpf.c,v 1.7 1999/07/02 10:05:22 itojun Exp $      */
+/*     $NetBSD: pcap-bpf.c,v 1.8 2002/09/22 16:13:01 thorpej Exp $     */
 
 /*
  * Copyright (c) 1993, 1994, 1995, 1996
@@ -26,7 +26,7 @@
 static const char rcsid[] =
     "@(#) Header: pcap-bpf.c,v 1.29 96/12/31 20:53:40 leres Exp  (LBL)";
 #else
-__RCSID("$NetBSD: pcap-bpf.c,v 1.7 1999/07/02 10:05:22 itojun Exp $");
+__RCSID("$NetBSD: pcap-bpf.c,v 1.8 2002/09/22 16:13:01 thorpej Exp $");
 #endif
 #endif
 
@@ -171,9 +171,12 @@
        int fd;
        struct ifreq ifr;
        struct bpf_version bv;
+       struct bpf_dltlist bdl;
        u_int v;
        pcap_t *p;
 
+       bzero(&bdl, sizeof(bdl));
+
        p = (pcap_t *)malloc(sizeof(*p));
        if (p == NULL) {
                (void)snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
@@ -226,6 +229,30 @@
 #endif
        p->linktype = v;
 
+       /*
+        * We know the default link type -- now determine any additional
+        * DLTs this interface supports.  If this fails, it's not fatal;
+        * we just don't get to use the feature later.
+        */
+       if (ioctl(fd, BIOCGDLTLIST, (caddr_t) &bdl) == 0) {
+               bdl.bfl_list = (u_int *) malloc(sizeof(u_int) * bdl.bfl_len);
+               if (bdl.bfl_list == NULL) {
+                       (void)snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
+                           pcap_strerror(errno));
+                       goto bad;
+               }
+
+               if (ioctl(fd, BIOCGDLTLIST, (caddr_t) &bdl) < 0) {
+                       (void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
+                           "BIOCGDLTLIST: %s", pcap_strerror(errno));
+                       free(bdl.bfl_list);
+                       goto bad;
+               }
+
+               p->dlt_count = bdl.bfl_len;
+               p->dlt_list = bdl.bfl_list;
+       }
+
        /* set timeout */
        if (to_ms != 0) {
                struct timeval to;
@@ -257,6 +284,8 @@
        return (p);
  bad:
        (void)close(fd);
+       if (bdl.bfl_list != NULL)
+               free(bdl.bfl_list);
        free(p);
        return (NULL);
 }
@@ -280,3 +309,25 @@
        }
        return (0);
 }
+
+int
+pcap_set_datalink(pcap_t *p, int dlt)
+{
+       int i;
+
+       for (i = 0; i < p->dlt_count; i++)
+               if (p->dlt_list[i] == dlt)
+                       break;
+       if (i >= p->dlt_count) {
+               (void) snprintf(p->errbuf, sizeof(p->errbuf),
+                   "No such DLT as %d", dlt);
+               return -1;
+       }
+       if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) {
+               (void) snprintf(p->errbuf, sizeof(p->errbuf),
+                   "Cannot set DLT %d: %s", dlt, strerror(errno));
+               return -1;
+       }
+       p->linktype = dlt;
+       return 0;
+}
diff -r fe566c9ebccf -r 3a773547a0f2 lib/libpcap/pcap-int.h
--- a/lib/libpcap/pcap-int.h    Sun Sep 22 16:07:31 2002 +0000
+++ b/lib/libpcap/pcap-int.h    Sun Sep 22 16:13:01 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pcap-int.h,v 1.8 2001/02/07 17:35:56 itojun Exp $      */
+/*     $NetBSD: pcap-int.h,v 1.9 2002/09/22 16:13:01 thorpej Exp $     */
 
 /*
  * Copyright (c) 1994, 1995, 1996
@@ -98,6 +98,8 @@
        struct bpf_program fcode;
 
        char errbuf[PCAP_ERRBUF_SIZE];
+       int dlt_count;
+       int *dlt_list;
 };
 
 /*
diff -r fe566c9ebccf -r 3a773547a0f2 lib/libpcap/pcap.3
--- a/lib/libpcap/pcap.3        Sun Sep 22 16:07:31 2002 +0000
+++ b/lib/libpcap/pcap.3        Sun Sep 22 16:13:01 2002 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: pcap.3,v 1.12 2002/02/07 07:00:51 ross Exp $
+.\" $NetBSD: pcap.3,v 1.13 2002/09/22 16:13:01 thorpej Exp $
 .\"
 .\" Copyright (c) 1994, 1996, 1997
 .\"    The Regents of the University of California.  All rights reserved.
@@ -61,6 +61,10 @@
 "bpf_uint32 netmask" "char *errbuf"
 .Ft int
 .Fn pcap_setfilter "pcap_t *p" "struct bpf_program *fp"
+.Ft int
+.Fn pcap_set_datalink "pcap_t *p" "int dlt"
+.Ft int
+.Fn pcap_list_datalinks "pcap_t *p" "int **dlt_buf"
 .Ft u_char *
 .Fn pcap_next "pcap_t *p" "struct pcap_pkthdr *h"
 .Ft int
@@ -261,6 +265,28 @@
 .Em 0
 is returned on success.
 .Pp
+.Fn pcap_set_datalink
+is used to set the current data link type of the pcap descriptor
+to the type specified by
+.Fa dlt .
+This operation is supported only of the interface associated with
+the pcap descriptor supports multiple data link types.
+.Em \-1
+is return on faulure;
+.Em 0
+is returned on success.
+.Pp
+.Fn pcap_list_datalinks
+is used to get a list of the support data link types of the interface
+assocated with the pcap descriptor.
+.Fn pcap_list_datalinks
+allocates an array to hold the list and sets
+.Fa *dlt_buf .
+The caller is responsible for freeing the array.
+.Em \-1
+is returned on failure;
+otherwise, the number of data link types in the array is returned.
+.Pp
 .Fn pcap_loop
 is similar to
 .Fn pcap_dispatch
diff -r fe566c9ebccf -r 3a773547a0f2 lib/libpcap/pcap.c
--- a/lib/libpcap/pcap.c        Sun Sep 22 16:07:31 2002 +0000
+++ b/lib/libpcap/pcap.c        Sun Sep 22 16:13:01 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pcap.c,v 1.8 2000/04/12 14:40:33 itojun Exp $  */
+/*     $NetBSD: pcap.c,v 1.9 2002/09/22 16:13:01 thorpej Exp $ */
 
 /*
  * Copyright (c) 1993, 1994, 1995, 1996
@@ -39,7 +39,7 @@
 static const char rcsid[] =
     "@(#) Header: pcap.c,v 1.27 96/11/27 18:43:25 leres Exp  (LBL)";
 #else
-__RCSID("$NetBSD: pcap.c,v 1.8 2000/04/12 14:40:33 itojun Exp $");
+__RCSID("$NetBSD: pcap.c,v 1.9 2002/09/22 16:13:01 thorpej Exp $");
 #endif
 #endif
 
@@ -49,6 +49,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <errno.h>
 
 #include "gnuc.h"
 #ifdef HAVE_OS_PROTO_H
@@ -125,6 +126,24 @@
 }
 
 int
+pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
+{
+       if (p->dlt_count <= 0) {
+               *dlt_buffer = NULL;
+               return -1;
+       }
+       *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer) * p->dlt_count);
+       if (*dlt_buffer == NULL) {
+               (void)snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s",
+                   pcap_strerror(errno));
+               return -1;
+       }
+       (void)memcpy(*dlt_buffer, p->dlt_list,
+           sizeof(**dlt_buffer) * p->dlt_count);
+       return (p->dlt_count);
+}
+
+int
 pcap_snapshot(pcap_t *p)
 {
        return (p->snapshot);
@@ -208,6 +227,8 @@
        if (p->md.device != NULL)
                free(p->md.device);
 #endif
+       if (p->dlt_list != NULL)
+               free(p->dlt_list);
 
        free(p);
 }
diff -r fe566c9ebccf -r 3a773547a0f2 lib/libpcap/pcap.h
--- a/lib/libpcap/pcap.h        Sun Sep 22 16:07:31 2002 +0000
+++ b/lib/libpcap/pcap.h        Sun Sep 22 16:13:01 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pcap.h,v 1.9 2001/01/06 02:11:18 christos Exp $        */
+/*     $NetBSD: pcap.h,v 1.10 2002/09/22 16:13:01 thorpej Exp $        */
 
 /*
  * Copyright (c) 1993, 1994, 1995, 1996
@@ -123,6 +123,8 @@
 /* XXX */
 int    pcap_freecode(pcap_t *, struct bpf_program *);
 int    pcap_datalink(pcap_t *);
+int    pcap_list_datalinks(pcap_t *, int **);
+int    pcap_set_datalink(pcap_t *, int);
 int    pcap_snapshot(pcap_t *);
 int    pcap_is_swapped(pcap_t *);
 int    pcap_major_version(pcap_t *);
diff -r fe566c9ebccf -r 3a773547a0f2 lib/libpcap/shlib_version
--- a/lib/libpcap/shlib_version Sun Sep 22 16:07:31 2002 +0000
+++ b/lib/libpcap/shlib_version Sun Sep 22 16:13:01 2002 +0000
@@ -1,5 +1,5 @@
-#      $NetBSD: shlib_version,v 1.7 2000/10/06 16:39:24 thorpej Exp $
+#      $NetBSD: shlib_version,v 1.8 2002/09/22 16:13:01 thorpej Exp $
 #      Remember to update distrib/sets/lists/base/shl.* when changing
 #
 major=1
-minor=2
+minor=3



Home | Main Index | Thread Index | Old Index