]> The Tcpdump Group git mirrors - libpcap/commitdiff
Make this code a bit more like future code.
authorGuy Harris <[email protected]>
Tue, 14 Mar 2017 03:53:13 +0000 (20:53 -0700)
committerGuy Harris <[email protected]>
Tue, 14 Mar 2017 03:53:13 +0000 (20:53 -0700)
This is a bit more like some under-development code that supports remote
interfaces with pcap_create()/pcap_activate() and uses them for remote
interfaces in pcap_open().

pcap-new.c

index 5b9a8a4bf88bbfc845c24e7564feedf356327910..d05731b642025e0b3d028bfda43e2cec78c28c3a 100644 (file)
@@ -572,75 +572,12 @@ pcap_t *pcap_open(const char *source, int snaplen, int flags, int read_timeout,
        switch (type)
        {
        case PCAP_SRC_FILE:
-               fp = pcap_open_offline(name, errbuf);
-               break;
+               return pcap_open_offline(name, errbuf);
 
        case PCAP_SRC_IFLOCAL:
                fp = pcap_create(name, errbuf);
-               if (fp == NULL)
-                       return (NULL);
-               status = pcap_set_snaplen(fp, snaplen);
-               if (status < 0)
-                       goto fail;
-               if (flags & PCAP_OPENFLAG_PROMISCUOUS)
-               {
-                       status = pcap_set_promisc(fp, 1);
-                       if (status < 0)
-                               goto fail;
-               }
-               if (flags & PCAP_OPENFLAG_MAX_RESPONSIVENESS)
-               {
-                       status = pcap_set_immediate_mode(fp, 1);
-                       if (status < 0)
-                               goto fail;
-               }
-               status = pcap_set_timeout(fp, read_timeout);
-               if (status < 0)
-                       goto fail;
-               status = pcap_activate(fp);
-               if (status < 0)
-                       goto fail;
-#ifdef _WIN32
-               /*
-                * This flag is supported on Windows only.
-                * XXX - is there a way to support it with
-                * the capture mechanisms on UN*X?  It's not
-                * exactly a "set direction" operation; I
-                * think it means "do not capture packets
-                * injected with pcap_sendpacket() or
-                * pcap_inject()".
-                */
-               if (fp->adapter != NULL)
-               {
-                       /* disable loopback capture if requested */
-                       if (flags & PCAP_OPENFLAG_NOCAPTURE_LOCAL)
-                       {
-                               if (!PacketSetLoopbackBehavior(fp->adapter, NPF_DISABLE_LOOPBACK))
-                               {
-                                       pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Unable to disable the capture of loopback packets.");
-                                       pcap_close(fp);
-                                       return NULL;
-                               }
-                       }
-               }
-#endif /* _WIN32 */
                break;
 
-       fail:
-               if (status == PCAP_ERROR)
-                       pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
-                           name, fp->errbuf);
-               else if (status == PCAP_ERROR_NO_SUCH_DEVICE ||
-                   status == PCAP_ERROR_PERM_DENIED ||
-                   status == PCAP_ERROR_PROMISC_PERM_DENIED)
-                       pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%s)",
-                           name, pcap_statustostr(status), fp->errbuf);
-               else
-                       pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
-                           name, pcap_statustostr(status));
-               pcap_close(fp);
-               return NULL;
-
        case PCAP_SRC_IFREMOTE:
                /*
                 * Although we already have host, port and iface, we prefer
@@ -648,14 +585,76 @@ pcap_t *pcap_open(const char *source, int snaplen, int flags, int read_timeout,
                 * has to call pcap_parsesrcstr() again.
                 * This is less optimized, but much clearer.
                 */
-               fp = pcap_open_rpcap(source, snaplen, flags, read_timeout, auth, errbuf);
-               break;
+               return pcap_open_rpcap(source, snaplen, flags, read_timeout, auth, errbuf);
 
        default:
                strlcpy(errbuf, "Source type not supported", PCAP_ERRBUF_SIZE);
                return NULL;
        }
+
+       if (fp == NULL)
+               return (NULL);
+       status = pcap_set_snaplen(fp, snaplen);
+       if (status < 0)
+               goto fail;
+       if (flags & PCAP_OPENFLAG_PROMISCUOUS)
+       {
+               status = pcap_set_promisc(fp, 1);
+               if (status < 0)
+                       goto fail;
+       }
+       if (flags & PCAP_OPENFLAG_MAX_RESPONSIVENESS)
+       {
+               status = pcap_set_immediate_mode(fp, 1);
+               if (status < 0)
+                       goto fail;
+       }
+       status = pcap_set_timeout(fp, read_timeout);
+       if (status < 0)
+               goto fail;
+       status = pcap_activate(fp);
+       if (status < 0)
+               goto fail;
+#ifdef _WIN32
+       /*
+        * This flag is supported on Windows only.
+        * XXX - is there a way to support it with
+        * the capture mechanisms on UN*X?  It's not
+        * exactly a "set direction" operation; I
+        * think it means "do not capture packets
+        * injected with pcap_sendpacket() or
+        * pcap_inject()".
+        */
+       if (fp->adapter != NULL)
+       {
+               /* disable loopback capture if requested */
+               if (flags & PCAP_OPENFLAG_NOCAPTURE_LOCAL)
+               {
+                       if (!PacketSetLoopbackBehavior(fp->adapter, NPF_DISABLE_LOOPBACK))
+                       {
+                               pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Unable to disable the capture of loopback packets.");
+                               pcap_close(fp);
+                               return NULL;
+                       }
+               }
+       }
+#endif /* _WIN32 */
        return fp;
+
+fail:
+       if (status == PCAP_ERROR)
+               pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
+                   name, fp->errbuf);
+       else if (status == PCAP_ERROR_NO_SUCH_DEVICE ||
+           status == PCAP_ERROR_PERM_DENIED ||
+           status == PCAP_ERROR_PROMISC_PERM_DENIED)
+               pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%s)",
+                   name, pcap_statustostr(status), fp->errbuf);
+       else
+               pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
+                   name, pcap_statustostr(status));
+       pcap_close(fp);
+       return NULL;
 }
 
 struct pcap_samp *pcap_setsampling(pcap_t *p)