improve upnp_get_portmapping_number_of_entries()

This commit is contained in:
Thomas Bernard 2020-06-04 00:27:49 +02:00
parent ddf328845a
commit 26c46e5a49
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C
4 changed files with 38 additions and 4 deletions

View File

@ -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

View File

@ -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 :

View File

@ -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 */

View File

@ -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