static pcap_t* open_pcap_dev(const char* ifname, int frameSize, char* errbuf) { pcap_t* handle = pcap_create(ifname, errbuf); if (handle) { int err; err = pcap_set_snaplen(handle, frameSize); if (err) AVB_LOGF_WARNING("Cannot set snap len %d", err); err = pcap_set_promisc(handle, 1); if (err) AVB_LOGF_WARNING("Cannot set promisc %d", err); err = pcap_set_immediate_mode(handle, 1); if (err) AVB_LOGF_WARNING("Cannot set immediate mode %d", err); // we need timeout (here 100ms) otherwise we could block for ever err = pcap_set_timeout(handle, 100); if (err) AVB_LOGF_WARNING("Cannot set timeout %d", err); err = pcap_set_tstamp_precision(handle, PCAP_TSTAMP_PRECISION_NANO); if (err) AVB_LOGF_WARNING("Cannot set tstamp nano precision %d", err); err = pcap_set_tstamp_type(handle, PCAP_TSTAMP_ADAPTER_UNSYNCED); if (err) AVB_LOGF_WARNING("Cannot set tstamp adapter unsynced %d", err); err = pcap_activate(handle); if (err) AVB_LOGF_WARNING("Cannot activate pcap %d", err); } return handle; }
static void prep_pcap_handle(pcap_t *handle) { int err; err = pcap_set_rfmon(handle, options.rfmon); if(err) die(0, "DEBUG: pcap handle should not be activated at %s:%d", __FILE__, __LINE__); err = pcap_set_promisc(handle, options.promisc); if(err) die(0, "DEBUG: pcap handle should not be activated at %s:%d", __FILE__, __LINE__); err = pcap_set_snaplen(handle, options.snaplen); if(err) die(0, "DEBUG: pcap handle should not be activated at %s:%d", __FILE__, __LINE__); err = pcap_set_timeout(handle, options.read_timeout); if(err) die(0, "DEBUG: pcap handle should not be activated at %s:%d", __FILE__, __LINE__); if(options.buffer_size > 0) { err = pcap_set_buffer_size(handle, options.buffer_size); if(err) die(0, "DEBUG: pcap handle should not be activated at %s:%d", __FILE__, __LINE__); } if(options.tstamp_type != PCAP_ERROR) { err = pcap_set_tstamp_type(handle, options.tstamp_type); if(err == PCAP_ERROR_ACTIVATED) die(0, "DEBUG: pcap handle should not be activated at %s:%d", __FILE__, __LINE__); else if(err == PCAP_ERROR_CANTSET_TSTAMP_TYPE) die(0, "pcap_set_tstamp_type(): Device does not support setting the timestamp"); else if(err == PCAP_WARNING_TSTAMP_TYPE_NOTSUP) plog(0, "pcap_set_tstamp_type(): Device does not support specified tstamp type"); } if(options.tstamp_nano) { err = pcap_set_tstamp_precision(handle, PCAP_TSTAMP_PRECISION_NANO); if(err == PCAP_ERROR_ACTIVATED) die(0, "DEBUG: pcap handle should not be activated at %s:%d", __FILE__, __LINE__); else if(err == PCAP_ERROR_TSTAMP_PRECISION_NOTSUP) die(0, "pcap_set_tstamp_precision(): Device does not support nanosecond precision"); } if(options.linktype != PCAP_ERROR) { err = pcap_set_datalink(handle, options.linktype); if(err) die(0, "pcap_set_datalink(): %s", pcap_geterr(handle)); } }
/* * Request high-resolution time stamps. * * We don't check for errors - if this fails, we just live with boring old * microsecond-resolution time stamps. The only errors pcap_set_tstamp_precision() * is documenting as returning are PCAP_ERROR_TSTAMP_PRECISION_NOTSUP, which just * means we can't do nanosecond precision on this adapter, in which case we * just live with whatever resolution we get by default, and * PCAP_ERROR_ACTIVATED, which shouldn't happen as we shouldn't call this * after we've activated the pcap_t. */ void request_high_resolution_timestamp(pcap_t *pcap_h) { #ifdef __APPLE__ /* * On OS X, if you build with a newer SDK, pcap_set_tstamp_precision() * is available, so the code will be built with it. * * However, if you then try to run on an older release that * doesn't have pcap_set_tstamp_precision(), the dynamic linker * will fail, as it won't find pcap_set_tstamp_precision(). * * libpcap doesn't use OS X "weak linking" for new routines, * so we can't just check whether a pointer to * pcap_set_tstamp_precision() is null and, if it is, not * call it. We have to, instead, use dlopen() to load * libpcap, and dlsym() to find a pointer to pcap_set_tstamp_precision(), * and if we find the pointer, call it. */ static gboolean initialized = FALSE; static int (*p_pcap_set_tstamp_precision)(pcap_t *, int); if (!initialized) { p_pcap_set_tstamp_precision = (int (*)(pcap_t *, int)) dlsym(RTLD_NEXT, "pcap_set_tstamp_precision"); initialized = TRUE; } if (p_pcap_set_tstamp_precision != NULL) (*p_pcap_set_tstamp_precision)(pcap_h, PCAP_TSTAMP_PRECISION_NANO); #else /* __APPLE__ */ /* * On other UN*Xes we require that we be run on an OS version * with a libpcap equal to or later than the version with which * we were built. */ pcap_set_tstamp_precision(pcap_h, PCAP_TSTAMP_PRECISION_NANO); #endif /* __APPLE__ */ }