+#ifdef __APPLE__
+static int
+device_exists(int fd, const char *name, char *errbuf)
+{
+ int status;
+ struct ifreq ifr;
+
+ if (strlen(name) >= sizeof(ifr.ifr_name)) {
+ /* The name is too long, so it can't possibly exist. */
+ return (PCAP_ERROR_NO_SUCH_DEVICE);
+ }
+ (void)pcap_strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+ status = ioctl(fd, SIOCGIFFLAGS, (caddr_t)&ifr);
+
+ if (status < 0) {
+ if (errno == ENXIO || errno == EINVAL) {
+ /*
+ * macOS and *BSD return one of those two
+ * errors if the device doesn't exist.
+ * Don't fill in an error, as this is
+ * an "expected" condition.
+ */
+ return (PCAP_ERROR_NO_SUCH_DEVICE);
+ }
+
+ /*
+ * Some other error - provide a message for it, as
+ * it's "unexpected".
+ */
+ pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, errno,
+ "Can't get interface flags on %s", name);
+ return (PCAP_ERROR);
+ }
+
+ /*
+ * The device exists.
+ */
+ return (0);
+}
+#endif
+