diff options
author | Thomas Habets <habets@google.com> | 2014-12-05 09:23:55 +0000 |
---|---|---|
committer | Thomas Habets <habets@google.com> | 2014-12-05 11:02:05 +0000 |
commit | 995c5d9b93678267b5fcdaa93fd9022c58991f43 (patch) | |
tree | 4b7ca0d7d068513321f1f369de99aabf48b11be1 | |
parent | 6592576c979c0f0d85d0c15e1bab6a874cb5eb1c (diff) | |
download | arping-995c5d9b93678267b5fcdaa93fd9022c58991f43.tar.gz |
Use pcap_create() instead of pcap_open_live(), when available.
pcap_open_live() does the same thing this code does, but future
commits will add stuff that only works when using pcap_create().
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | src/arping.c | 55 |
2 files changed, 52 insertions, 5 deletions
diff --git a/configure.ac b/configure.ac index d451cae..8a1247c 100644 --- a/configure.ac +++ b/configure.ac @@ -65,7 +65,7 @@ AC_FUNC_SELECT_ARGTYPES AC_FUNC_SETVBUF_REVERSED AC_TYPE_SIGNAL AC_CHECK_FUNCS([gettimeofday memset select strchr strdup strerror strstr \ -getifaddrs cap_init]) +getifaddrs cap_init pcap_create]) if test x$ac_cv_func_getifaddrs = xyes; then AC_LIBOBJ([findif_getifaddrs]) diff --git a/src/arping.c b/src/arping.c index d0f501d..9216fa7 100644 --- a/src/arping.c +++ b/src/arping.c @@ -318,6 +318,53 @@ drop_privileges() drop_capabilities(); } + +/** + * Do pcap_open_live(), except by using the pcap_create() interface + * introduced in 2008 (libpcap 0.4) where available. + * + * FIXME: Use pcap_set_buffer_size()? + */ +static pcap_t* +try_pcap_open_live(const char *device, int snaplen, + int promisc, int to_ms, char *errbuf) +{ +#ifdef HAVE_PCAP_CREATE + pcap_t* pcap; + int rc; + + if (!(pcap = pcap_create(device, errbuf))) { + goto err; + } + if ((rc = pcap_set_snaplen(pcap, snaplen))) { + snprintf(errbuf, PCAP_ERRBUF_SIZE, "pcap_set_snaplen(): %s", pcap_statustostr(rc)); + goto err; + } + if ((rc = pcap_set_promisc(pcap, promisc))) { + snprintf(errbuf, PCAP_ERRBUF_SIZE, "pcap_set_promisc(): %s", pcap_statustostr(rc)); + goto err; + } + if ((rc = pcap_set_timeout(pcap, to_ms))) { + snprintf(errbuf, PCAP_ERRBUF_SIZE, "pcap_set_timeout(): %s", pcap_statustostr(rc)); + goto err; + } + + // FIXME: pcap_set_tstamp_type + if ((rc = pcap_activate(pcap))) { + snprintf(errbuf, PCAP_ERRBUF_SIZE, "pcap_activate(): %s", pcap_statustostr(rc)); + goto err; + } + return pcap; +err: + if (pcap) { + pcap_close(pcap); + } + return NULL; +#else + return pcap_open_live(device, snaplen, promisc, to_ms, errbuf); +#endif +} + /** * Some stupid OSs (Solaris) think it's a good idea to put network * devices in /dev and then play musical chairs with them. @@ -335,22 +382,22 @@ do_pcap_open_live(const char *device, int snaplen, pcap_t* ret; char buf[PATH_MAX]; - if ((ret = pcap_open_live(device, snaplen, promisc, to_ms, errbuf))) { + if ((ret = try_pcap_open_live(device, snaplen, promisc, to_ms, errbuf))) { return ret; } snprintf(buf, sizeof(buf), "/dev/%s", device); - if ((ret = pcap_open_live(buf, snaplen, promisc, to_ms, errbuf))) { + if ((ret = try_pcap_open_live(buf, snaplen, promisc, to_ms, errbuf))) { return ret; } snprintf(buf, sizeof(buf), "/dev/net/%s", device); - if ((ret = pcap_open_live(buf, snaplen, promisc, to_ms, errbuf))) { + if ((ret = try_pcap_open_live(buf, snaplen, promisc, to_ms, errbuf))) { return ret; } /* Call original again to reset the error message. */ - return pcap_open_live(device, snaplen, promisc, to_ms, errbuf); + return try_pcap_open_live(device, snaplen, promisc, to_ms, errbuf); } /** |