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
* 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)