diff --git a/libnetutil/netutil.cc b/libnetutil/netutil.cc index a6e7bea57..347aba637 100644 --- a/libnetutil/netutil.cc +++ b/libnetutil/netutil.cc @@ -1391,6 +1391,7 @@ static struct interface_info *getinterfaces_dnet(int *howmany, char *errstr, siz return dcrn.ifaces; } +static struct interface_info *mydevs = NULL; /* Returns an allocated array of struct interface_info representing the available interfaces. The number of interfaces is returned in *howmany. This function just does caching of results; the real work is done in @@ -1398,13 +1399,10 @@ static struct interface_info *getinterfaces_dnet(int *howmany, char *errstr, siz On error, NULL is returned, howmany is set to -1 and the supplied error buffer "errstr", if not NULL, will contain an error message. */ struct interface_info *getinterfaces(int *howmany, char *errstr, size_t errstrlen) { - static int initialized = 0; - static struct interface_info *mydevs; static int numifaces = 0; - if (!initialized) { + if (mydevs == NULL) { mydevs = getinterfaces_dnet(&numifaces, errstr, errstrlen); - initialized = 1; } /* These will propagate any error produced in getinterfaces_xxxx() to @@ -1414,6 +1412,10 @@ struct interface_info *getinterfaces(int *howmany, char *errstr, size_t errstrle return mydevs; } +void freeinterfaces(void) { + free(mydevs); + mydevs = NULL; +} /* The 'dev' passed in must be at least 32 bytes long. Returns 0 on success. */ int ipaddr2devname(char *dev, const struct sockaddr_storage *addr) { diff --git a/libnetutil/netutil.h b/libnetutil/netutil.h index d4af70540..437f74663 100644 --- a/libnetutil/netutil.h +++ b/libnetutil/netutil.h @@ -337,6 +337,9 @@ int sockaddr_equal_zero(const struct sockaddr_storage *s); On error, NULL is returned, howmany is set to -1 and the supplied error buffer "errstr", if not NULL, will contain an error message. */ struct interface_info *getinterfaces(int *howmany, char *errstr, size_t errstrlen); +/* Frees the array of cached struct interface_info used by getinterfaces. Can + be used to force a refresh or to release memory. */ +void freeinterfaces(void); /* This struct is abused to carry either routes or interfaces, depending on the function it's used in. */ diff --git a/nmap.cc b/nmap.cc index c8eb6a677..afd2c09a7 100644 --- a/nmap.cc +++ b/nmap.cc @@ -1788,6 +1788,7 @@ void nmap_free_mem() { PortList::freePortMap(); cp_free(); free_services(); + freeinterfaces(); AllProbes::service_scan_free(); traceroute_hop_cache_clear(); nsock_set_default_engine(NULL);