diff --git a/libnetutil/netutil.cc b/libnetutil/netutil.cc index d73881cfe..b027826d0 100644 --- a/libnetutil/netutil.cc +++ b/libnetutil/netutil.cc @@ -4073,16 +4073,6 @@ int DnetName2PcapName(const char *dnetdev, char *pcapdev, int pcapdevlen) { #endif -/* Compute exponential sleep time for my_pcap_open_live(). Returned - * value is 5 to the times-th power (5^times) */ -static unsigned int compute_sleep_time(unsigned int times){ - unsigned int i=0; - unsigned int result=1; - for(i=0; i= 3) { - return NULL; - } else { - netutil_error("pcap_open_live(%s, %d, %d, %d) FAILED. Reported error: %s. Will wait %d seconds then retry.", pcapdev, snaplen, promisc, to_ms, err0r, compute_sleep_time(failed)); - } - sleep( compute_sleep_time(failed) ); - } - } while (!pt); + +#ifdef __amigaos__ + // Amiga doesn't have pcap_create + // TODO: Does Nmap still work on Amiga? + pt = pcap_open_live(pcapdev, snaplen, promisc, to_ms, err0r); + if (!pt) { + netutil_error("pcap_open_live(%s, %d, %d, %d) FAILED. Reported error: %s.", pcapdev, snaplen, promisc, to_ms, err0r); + return NULL; + } +#else + pt = pcap_create(pcapdev, err0r); + if (!pt) { + netutil_error("pcap_create(%s) FAILED: %s.", pcapdev, err0r); + return NULL; + } + +#define MY_PCAP_SET(func, p_t, val) do {\ + failed = func(p_t, val);\ + if (failed) {\ + netutil_error(#func "(%d) FAILED: %d.", val, failed);\ + pcap_close(p_t);\ + return NULL;\ + }\ +} while(0); + + MY_PCAP_SET(pcap_set_snaplen, pt, snaplen); + MY_PCAP_SET(pcap_set_promisc, pt, promisc); + MY_PCAP_SET(pcap_set_timeout, pt, to_ms); + + failed = pcap_activate(pt); + if (failed < 0) { + // PCAP error + netutil_error("pcap_activate(%s) FAILED: %s.", pcapdev, pcap_geterr(pt)); + pcap_close(pt); + return NULL; + } + else if (failed > 0) { + // PCAP warning, report but assume it'll still work + netutil_error("pcap_activate(%s) WARNING: %s.", pcapdev, pcap_geterr(pt)); + } +#endif /* not __amigaos__ */ #ifdef WIN32 if (wait == WAIT_ABANDONED || wait == WAIT_OBJECT_0) { diff --git a/nsock/src/nsock_pcap.c b/nsock/src/nsock_pcap.c index 5629e87b8..18bd41b1b 100644 --- a/nsock/src/nsock_pcap.c +++ b/nsock/src/nsock_pcap.c @@ -200,6 +200,11 @@ static int nsock_pcap_get_l3_offset(pcap_t *pt, int *dl) { static int nsock_pcap_try_open(struct npool *nsp, mspcap *mp, const char *dev, int snaplen, int promisc, int timeout_ms, char *errbuf) { + /* TODO: Convert from pcap_open_live to pcap_create and set appropriate options. + * Maybe expose an API for setting said options. We can't assume that all + * Nsock apps will want the same options that Nmap wants, especially the + * options Nmap wants for port scanning. + */ mp->pt = pcap_open_live(dev, snaplen, promisc, timeout_ms, errbuf); if (!mp->pt) { nsock_log_error("pcap_open_live(%s, %d, %d, %d) failed with error: %s",