From be124b6508461ebf57e158eac830886bd094776a Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Mon, 18 Jan 2016 22:46:30 +0100 Subject: [PATCH] add find_pinhole() to pf/ code also see #188 --- miniupnpd/pf/Makefile | 3 +- miniupnpd/pf/pfpinhole.c | 69 ++++++++++++++++++++++++++++++++++-- miniupnpd/pf/pfpinhole.h | 8 ++++- miniupnpd/pf/testpfpinhole.c | 15 ++++++-- 4 files changed, 88 insertions(+), 7 deletions(-) diff --git a/miniupnpd/pf/Makefile b/miniupnpd/pf/Makefile index d0d28c9..f94c074 100644 --- a/miniupnpd/pf/Makefile +++ b/miniupnpd/pf/Makefile @@ -1,6 +1,7 @@ # $Id: Makefile,v 1.4 2012/04/18 20:45:33 nanard Exp $ # made for GNU Make (and BSD make) -CFLAGS = -Wall -g -ansi -DTEST +CFLAGS = -Wall -g -DTEST +CFLAGS += -Wextra EXECUTABLES = testobsdrdr testpfpinhole all: $(EXECUTABLES) diff --git a/miniupnpd/pf/pfpinhole.c b/miniupnpd/pf/pfpinhole.c index b05b75d..449a700 100644 --- a/miniupnpd/pf/pfpinhole.c +++ b/miniupnpd/pf/pfpinhole.c @@ -1,7 +1,8 @@ /* $Id: pfpinhole.c,v 1.24 2014/12/05 09:54:55 nanard Exp $ */ -/* MiniUPnP project +/* vim: tabstop=4 shiftwidth=4 noexpandtab + * MiniUPnP project * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ - * (c) 2012 Thomas Bernard + * (c) 2012-2016 Thomas Bernard * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ @@ -40,7 +41,7 @@ * flags S/SA keep state * label "pinhole-2 ts-4321000" * - * with the label "pinhole-$uid ts-$timestamp" + * with the label "pinhole-$uid ts-$timestamp: $description" */ #ifdef ENABLE_UPNPPINHOLE @@ -158,6 +159,68 @@ int add_pinhole(const char * ifname, return uid; } +int find_pinhole(const char * ifname, + const char * rem_host, unsigned short rem_port, + const char * int_client, unsigned short int_port, + int proto, + char *desc, int desc_len, unsigned int * timestamp) +{ + int uid; + unsigned int ts; + int i, n; + struct pfioc_rule pr; + struct in6_addr saddr; + struct in6_addr daddr; + UNUSED(ifname); + + if(dev<0) { + syslog(LOG_ERR, "pf device is not open"); + return -1; + } + if(rem_host && (rem_host[0] != '\0')) { + inet_pton(AF_INET6, rem_host, &saddr); + } else { + memset(&saddr, 0, sizeof(struct in6_addr)); + } + inet_pton(AF_INET6, int_client, &daddr); + memset(&pr, 0, sizeof(pr)); + strlcpy(pr.anchor, anchor_name, MAXPATHLEN); +#ifndef PF_NEWSTYLE + pr.rule.action = PF_PASS; +#endif + if(ioctl(dev, DIOCGETRULES, &pr) < 0) { + syslog(LOG_ERR, "ioctl(dev, DIOCGETRULES, ...): %m"); + return -1; + } + n = pr.nr; + for(i=0; i