diff --git a/miniupnpd/Changelog.txt b/miniupnpd/Changelog.txt index 7dae342..33e2031 100644 --- a/miniupnpd/Changelog.txt +++ b/miniupnpd/Changelog.txt @@ -2,6 +2,7 @@ $Id: Changelog.txt,v 1.464 2020/05/10 17:57:56 nanard Exp $ 2020/06/03: configure --disable-fork to disable going to background + improve upnp_get_portmapping_number_of_entries() 2020/05/07: Linux: use libcap or libcap-ng to drop unnecessary capabilities diff --git a/miniupnpd/commonrdr.h b/miniupnpd/commonrdr.h index 6aed756..667b0ea 100644 --- a/miniupnpd/commonrdr.h +++ b/miniupnpd/commonrdr.h @@ -1,7 +1,7 @@ /* $Id: commonrdr.h,v 1.11 2019/10/02 22:12:02 nanard Exp $ */ /* MiniUPnP project - * (c) 2006-2019 Thomas Bernard - * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ + * (c) 2006-2020 Thomas Bernard + * http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/ * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ #ifndef COMMONRDR_H_INCLUDED @@ -19,6 +19,11 @@ init_redirect(void); void shutdown_redirect(void); +/* get_redirect_rule_count() + * return value : -1 for error or the number of redirection rules */ +int +get_redirect_rule_count(const char * ifname); + /* get_redirect_rule() gets internal IP and port from * interface, external port and protocol * return value : diff --git a/miniupnpd/pf/obsdrdr.c b/miniupnpd/pf/obsdrdr.c index a1f8492..5f4c725 100644 --- a/miniupnpd/pf/obsdrdr.c +++ b/miniupnpd/pf/obsdrdr.c @@ -522,6 +522,31 @@ add_filter_rule2(const char * ifname, #endif } +/* get_redirect_rule_count() + * return value : -1 for error or the number of rdr rules */ +int +get_redirect_rule_count(const char * ifname) +{ + struct pfioc_rule pr; + UNUSED(ifname); + + if(dev<0) { + syslog(LOG_ERR, "pf device is not open"); + return -1; + } + memset(&pr, 0, sizeof(pr)); + strlcpy(pr.anchor, anchor_name, MAXPATHLEN); +#ifndef PF_NEWSTYLE + pr.rule.action = PF_RDR; +#endif + if(ioctl(dev, DIOCGETRULES, &pr) < 0) + { + syslog(LOG_ERR, "ioctl(dev, DIOCGETRULES, ...): %m"); + return -1; + } + return pr.nr; +} + /* get_redirect_rule() * return value : 0 success (found) * -1 = error or rule not found */ diff --git a/miniupnpd/upnpredirect.c b/miniupnpd/upnpredirect.c index 74926f0..25c3744 100644 --- a/miniupnpd/upnpredirect.c +++ b/miniupnpd/upnpredirect.c @@ -579,11 +579,13 @@ upnp_delete_redirection(unsigned short eport, const char * protocol) return _upnp_delete_redir(eport, proto_atoi(protocol)); } -/* upnp_get_portmapping_number_of_entries() - * TODO: improve this code. */ +/* upnp_get_portmapping_number_of_entries() */ int upnp_get_portmapping_number_of_entries() { +#if defined(USE_PF) + return get_redirect_rule_count(ext_if_name); +#else int n = 0, r = 0; unsigned short eport, iport; char protocol[4], iaddr[32], desc[64], rhost[32]; @@ -598,6 +600,7 @@ upnp_get_portmapping_number_of_entries() n++; } while(r==0); return (n-1); +#endif } /* functions used to remove unused rules