miniupnpd/pf/pfpinhole.c: use label to store pinhole description

This commit is contained in:
Thomas Bernard 2014-05-15 23:27:51 +02:00
parent 7154d30adc
commit b9c20cecab
2 changed files with 21 additions and 9 deletions

View File

@ -1,4 +1,4 @@
/* $Id: pfpinhole.c,v 1.19 2012/05/21 15:47:57 nanard Exp $ */
/* $Id: pfpinhole.c,v 1.22 2014/05/15 21:23:43 nanard Exp $ */
/* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2012 Thomas Bernard
@ -49,7 +49,8 @@ extern int dev;
static int next_uid = 1;
#define PINEHOLE_LABEL_FORMAT "pinhole-%d ts-%u"
#define PINEHOLE_LABEL_FORMAT "pinhole-%d ts-%u: %s"
#define PINEHOLE_LABEL_FORMAT_SKIPDESC "pinhole-%d ts-%u: %*s"
int add_pinhole(const char * ifname,
const char * rem_host, unsigned short rem_port,
@ -108,7 +109,7 @@ int add_pinhole(const char * ifname,
pcr.rule.keep_state = 1;
uid = next_uid;
snprintf(pcr.rule.label, PF_RULE_LABEL_SIZE,
PINEHOLE_LABEL_FORMAT, uid, timestamp);
PINEHOLE_LABEL_FORMAT, uid, timestamp, desc);
if(queue)
strlcpy(pcr.rule.qname, queue, PF_QNAME_SIZE);
if(tag)
@ -224,7 +225,6 @@ get_pinhole_info(unsigned short uid,
syslog(LOG_ERR, "pf device is not open");
return -1;
}
if (desc) *desc = 0; /* XXX - use label for storing it? */
snprintf(label_start, sizeof(label_start),
"pinhole-%hu", uid);
memset(&pr, 0, sizeof(pr));
@ -261,6 +261,14 @@ get_pinhole_info(unsigned short uid,
*proto = pr.rule.proto;
if(timestamp)
sscanf(p, "ts-%u", timestamp);
if(desc) {
strsep(&p, " ");
if(p) {
strlcpy(desc, p, desclen);
} else {
desc[0] = '\0';
}
}
#ifdef PFRULE_INOUT_COUNTS
if(packets)
*packets = pr.rule.packets[0] + pr.rule.packets[1];
@ -322,7 +330,7 @@ int clean_pinhole_list(unsigned int * next_timestamp)
syslog(LOG_ERR, "ioctl(dev, DIOCGETRULE): %m");
return -1;
}
if(sscanf(pr.rule.label, PINEHOLE_LABEL_FORMAT, &uid, &ts) != 2) {
if(sscanf(pr.rule.label, PINEHOLE_LABEL_FORMAT_SKIPDESC, &uid, &ts) != 2) {
syslog(LOG_INFO, "rule with label '%s' is not a IGD pinhole", pr.rule.label);
continue;
}

View File

@ -1,4 +1,4 @@
/* $Id: testpfpinhole.c,v 1.11 2014/02/28 16:49:15 nanard Exp $ */
/* $Id: testpfpinhole.c,v 1.12 2014/05/15 21:23:43 nanard Exp $ */
/* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2012-2014 Thomas Bernard
@ -32,11 +32,14 @@ static int print_pinhole(int uid)
int proto;
unsigned int timestamp;
u_int64_t packets, bytes;
char desc[64];
r = get_pinhole_info((unsigned short)uid,
rem_host, sizeof(rem_host), &rem_port,
int_client, sizeof(int_client), &int_port,
&proto, &timestamp,
&proto,
desc, sizeof(desc),
&timestamp,
&packets, &bytes);
if(r < 0) {
fprintf(stderr, "get_pinhole(%d) returned %d\n", uid, r);
@ -44,6 +47,7 @@ static int print_pinhole(int uid)
printf("pinhole %d : [%s]:%hu => [%s]:%hu proto=%d ts=%u\n",
uid, rem_host, rem_port, int_client, int_port,
proto, timestamp);
printf(" desc='%s'\n", desc);
printf(" packets=%llu bytes=%llu\n", packets, bytes);
}
return r;
@ -65,12 +69,12 @@ int main(int argc, char * *argv)
return 1;
}
uid = add_pinhole("ep0", "2001::1:2:3", 12345, "123::ff", 54321, IPPROTO_UDP, 424242);
uid = add_pinhole("ep0", "2001::1:2:3", 12345, "123::ff", 54321, IPPROTO_UDP, "description test 1", 424242);
if(uid < 0) {
fprintf(stderr, "add_pinhole() failed\n");
}
printf("add_pinhole() returned %d\n", uid);
uid = add_pinhole("ep0", NULL, 0, "dead:beef::42:42", 8080, IPPROTO_UDP, 4321000);
uid = add_pinhole("ep0", NULL, 0, "dead:beef::42:42", 8080, IPPROTO_UDP, "description test 2", 4321000);
if(uid < 0) {
fprintf(stderr, "add_pinhole() failed\n");
}