]> The Tcpdump Group git mirrors - libpcap/blobdiff - pcap-nit.c
pcap_create_interface() needs the interface name on Linux.
[libpcap] / pcap-nit.c
index ea81eb579f581d30c3133135448c1daf891c0f2b..1b626e215a22cc20f6c3c23e43b807ac2cba619e 100644 (file)
@@ -114,11 +114,11 @@ pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
                if (cc < 0) {
                        if (errno == EWOULDBLOCK)
                                return (0);
-                       snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s",
+                       pcap_snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s",
                                pcap_strerror(errno));
                        return (-1);
                }
-               bp = p->buffer;
+               bp = (u_char *)p->buffer;
        } else
                bp = p->bp;
 
@@ -168,7 +168,7 @@ pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
                        continue;
 
                default:
-                       snprintf(p->errbuf, sizeof(p->errbuf),
+                       pcap_snprintf(p->errbuf, sizeof(p->errbuf),
                            "bad nit state %d", nh->nh_state);
                        return (-1);
                }
@@ -206,12 +206,12 @@ pcap_inject_nit(pcap_t *p, const void *buf, size_t size)
        strncpy(sa.sa_data, device, sizeof(sa.sa_data));
        ret = sendto(p->fd, buf, size, 0, &sa, sizeof(sa));
        if (ret == -1) {
-               snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
+               pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
                    pcap_strerror(errno));
                return (-1);
        }
        return (ret);
-}                           
+}
 
 static int
 nit_setflags(pcap_t *p)
@@ -249,7 +249,7 @@ nit_setflags(pcap_t *p)
                nioc.nioc_flags |= NF_PROMISC;
 
        if (ioctl(p->fd, SIOCSNIT, &nioc) < 0) {
-               snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCSNIT: %s",
+               pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCSNIT: %s",
                    pcap_strerror(errno));
                return (-1);
        }
@@ -280,15 +280,22 @@ pcap_activate_nit(pcap_t *p)
        memset(p, 0, sizeof(*p));
        p->fd = fd = socket(AF_NIT, SOCK_RAW, NITPROTO_RAW);
        if (fd < 0) {
-               snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
+               pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
                    "socket: %s", pcap_strerror(errno));
                goto bad;
        }
        snit.snit_family = AF_NIT;
-       (void)strncpy(snit.snit_ifname, p->opt.source, NITIFSIZ);
+       (void)strncpy(snit.snit_ifname, p->opt.device, NITIFSIZ);
 
        if (bind(fd, (struct sockaddr *)&snit, sizeof(snit))) {
-               snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
+               /*
+                * XXX - there's probably a particular bind error that
+                * means "there's no such device" and a particular bind
+                * error that means "that device doesn't support NIT";
+                * they might be the same error, if they both end up
+                * meaning "NIT doesn't know about that device".
+                */
+               pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
                    "bind: %s: %s", snit.snit_ifname, pcap_strerror(errno));
                goto bad;
        }
@@ -301,7 +308,7 @@ pcap_activate_nit(pcap_t *p)
        p->linktype = DLT_EN10MB;
 
        p->bufsize = BUFSPACE;
-       p->buffer = (u_char *)malloc(p->bufsize);
+       p->buffer = malloc(p->bufsize);
        if (p->buffer == NULL) {
                strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
                goto bad;
@@ -348,11 +355,11 @@ pcap_activate_nit(pcap_t *p)
 }
 
 pcap_t *
-pcap_create_interface(const char *device, char *ebuf)
+pcap_create_interface(const char *device _U_, char *ebuf)
 {
        pcap_t *p;
 
-       p = pcap_create_common(device, ebuf, sizeof (struct pcap_nit));
+       p = pcap_create_common(ebuf, sizeof (struct pcap_nit));
        if (p == NULL)
                return (NULL);
 
@@ -360,8 +367,18 @@ pcap_create_interface(const char *device, char *ebuf)
        return (p);
 }
 
+/*
+ * XXX - there's probably a particular bind error that means "that device
+ * doesn't support NIT"; if so, we should try a bind and use that.
+ */
+static int
+can_be_bound(const char *name _U_)
+{
+       return (1);
+}
+
 int
 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
 {
-       return (0);
+       return (pcap_findalldevs_interfaces(alldevsp, errbuf, can_be_bound));
 }