]> The Tcpdump Group git mirrors - libpcap/commitdiff
If iface_bind() fails, that's a hard error.
authorGuy Harris <[email protected]>
Sun, 2 Dec 2018 02:32:02 +0000 (18:32 -0800)
committerGuy Harris <[email protected]>
Sun, 2 Dec 2018 02:32:02 +0000 (18:32 -0800)
If you can open a PF_PACKET socket, you don't need to fall back on
PF_INET/SOCK_PACKET sockets, and we've already opened that socket by the
time we call iface_bind().

Have iface_bind() return 0 on success and a PCAP_ERROR_ value on
failure.  If the bind() call fails with ENODEV, return
PCAP_ERROR_NO_SUCH_DEVICE, otherwise return PCAP_ERROR.

If getsockopt(SO_ERROR) fails, that's an error; if it succeeds, and
supplies a pending error code, that's an error, too.

pcap-linux.c

index 813e1187e21815760cdfe622ba70524c714a7327..47885cd9e5ae4e2cce678f678cd8386431ecdaff 100644 (file)
@@ -3835,12 +3835,9 @@ activate_new(pcap_t *handle)
                }
 
                if ((err = iface_bind(sock_fd, handlep->ifindex,
-                   handle->errbuf, protocol)) != 1) {
+                   handle->errbuf, protocol)) != 0) {
                        close(sock_fd);
-                       if (err < 0)
-                               return err;
-                       else
-                               return 0;       /* try old mechanism */
+                       return err;
                }
        } else {
                /*
@@ -5755,14 +5752,13 @@ iface_get_id(int fd, const char *device, char *ebuf)
 
 /*
  *  Bind the socket associated with FD to the given device.
- *  Return 1 on success, 0 if we should try a SOCK_PACKET socket,
- *  or a PCAP_ERROR_ value on a hard error.
+ *  Return 0 on success or a PCAP_ERROR_ value on a hard error.
  */
 static int
 iface_bind(int fd, int ifindex, char *ebuf, int protocol)
 {
        struct sockaddr_ll      sll;
-       int                     err;
+       int                     ret, err;
        socklen_t               errlen = sizeof(err);
 
        memset(&sll, 0, sizeof(sll));
@@ -5780,11 +5776,14 @@ iface_bind(int fd, int ifindex, char *ebuf, int protocol)
                         * libpcap developers.
                         */
                        return PCAP_ERROR_IFACE_NOT_UP;
-               } else {
-                       pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
-                           errno, "bind");
-                       return PCAP_ERROR;
                }
+               if (errno == ENODEV)
+                       ret = PCAP_ERROR_NO_SUCH_DEVICE;
+               else
+                       ret = PCAP_ERROR;
+               pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
+                   errno, "bind");
+               return ret;
        }
 
        /* Any pending errors, e.g., network is down? */
@@ -5792,7 +5791,7 @@ iface_bind(int fd, int ifindex, char *ebuf, int protocol)
        if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
                pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
                    errno, "getsockopt (SO_ERROR)");
-               return 0;
+               return PCAP_ERROR;
        }
 
        if (err == ENETDOWN) {
@@ -5807,10 +5806,10 @@ iface_bind(int fd, int ifindex, char *ebuf, int protocol)
        } else if (err > 0) {
                pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
                    err, "bind");
-               return 0;
+               return PCAP_ERROR;
        }
 
-       return 1;
+       return 0;
 }
 
 #ifdef IW_MODE_MONITOR