+ if (if_indextoname(iface, ifname) == NULL)
+ return -1;
+
+ /*
+ * Using a timeout for BPF is fairly portable across BSDs. On most
+ * modern versions, using the timeout/nonblock/poll method results in
+ * fairly sane behavior, with the timeout only coming into play during
+ * the next_ex() call itself (so, for us, that's only when there's
+ * data). On older versions, it may result in a PCAP_TIMEOUT busy-wait
+ * on some versions, though, as the poll() may terminate at the
+ * PCAP_TIMEOUT instead of the poll() timeout.
+ */
+ pp = pcap_open_live(ifname, 1500, 0, PCAP_TIMEOUT, __pcap_errbuf);
+ if (pp == NULL) {
+ return (-1);
+ }
+ err = pcap_set_datalink(pp, DLT_EN10MB);
+ if (err == -1) {
+ daemon_log(LOG_ERR, "pcap_set_datalink: %s", pcap_geterr(pp));
+ pcap_close(pp);
+ return (-1);
+ }
+ err = pcap_setdirection(pp, PCAP_D_IN);
+ if (err == -1) {
+ daemon_log(LOG_ERR, "pcap_setdirection: %s", pcap_geterr(pp));
+ pcap_close(pp);
+ return (-1);
+ }
+
+ fd = pcap_get_selectable_fd(pp);
+ if (fd == -1) {
+ pcap_close(pp);
+ return (-1);
+ }
+
+ /*
+ * Using setnonblock is a portability stop-gap. Using the timeout in
+ * combination with setnonblock will ensure on most BSDs that the
+ * next_ex call returns in a timely fashion.
+ */
+ err = pcap_setnonblock(pp, 1, __pcap_errbuf);
+ if (err == -1) {
+ pcap_close(pp);
+ return (-1);
+ }
+
+ filter = avahi_strdup_printf("arp and (ether dst ff:ff:ff:ff:ff:ff or "
+ "%02x:%02x:%02x:%02x:%02x:%02x)",
+ __lladdr[0], __lladdr[1],
+ __lladdr[2], __lladdr[3],
+ __lladdr[4], __lladdr[5]);
+ DEBUG(daemon_log(LOG_DEBUG, "Using pcap filter '%s'", filter));
+
+ err = pcap_compile(pp, &bpf, filter, 1, 0);
+ avahi_free(filter);
+ if (err == -1) {
+ daemon_log(LOG_ERR, "pcap_compile: %s", pcap_geterr(pp));
+ pcap_close(pp);
+ return (-1);
+ }
+ err = pcap_setfilter(pp, &bpf);
+ if (err == -1) {
+ daemon_log(LOG_ERR, "pcap_setfilter: %s", pcap_geterr(pp));
+ pcap_close(pp);
+ return (-1);
+ }
+ pcap_freecode(&bpf);
+
+ /* Stash pcap-specific context away. */
+ memcpy(hw_address, __lladdr, ETHER_ADDRLEN);
+ __pp = pp;
+
+ return (fd);
+}
+
+static void close_socket(int fd AVAHI_GCC_UNUSED) {
+ assert(__pp != NULL);
+ pcap_close(__pp);
+ __pp = NULL;