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 /* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2012 Thomas Bernard * (c) 2012 Thomas Bernard
@ -49,7 +49,8 @@ extern int dev;
static int next_uid = 1; 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, int add_pinhole(const char * ifname,
const char * rem_host, unsigned short rem_port, const char * rem_host, unsigned short rem_port,
@ -108,7 +109,7 @@ int add_pinhole(const char * ifname,
pcr.rule.keep_state = 1; pcr.rule.keep_state = 1;
uid = next_uid; uid = next_uid;
snprintf(pcr.rule.label, PF_RULE_LABEL_SIZE, snprintf(pcr.rule.label, PF_RULE_LABEL_SIZE,
PINEHOLE_LABEL_FORMAT, uid, timestamp); PINEHOLE_LABEL_FORMAT, uid, timestamp, desc);
if(queue) if(queue)
strlcpy(pcr.rule.qname, queue, PF_QNAME_SIZE); strlcpy(pcr.rule.qname, queue, PF_QNAME_SIZE);
if(tag) if(tag)
@ -224,7 +225,6 @@ get_pinhole_info(unsigned short uid,
syslog(LOG_ERR, "pf device is not open"); syslog(LOG_ERR, "pf device is not open");
return -1; return -1;
} }
if (desc) *desc = 0; /* XXX - use label for storing it? */
snprintf(label_start, sizeof(label_start), snprintf(label_start, sizeof(label_start),
"pinhole-%hu", uid); "pinhole-%hu", uid);
memset(&pr, 0, sizeof(pr)); memset(&pr, 0, sizeof(pr));
@ -261,6 +261,14 @@ get_pinhole_info(unsigned short uid,
*proto = pr.rule.proto; *proto = pr.rule.proto;
if(timestamp) if(timestamp)
sscanf(p, "ts-%u", timestamp); sscanf(p, "ts-%u", timestamp);
if(desc) {
strsep(&p, " ");
if(p) {
strlcpy(desc, p, desclen);
} else {
desc[0] = '\0';
}
}
#ifdef PFRULE_INOUT_COUNTS #ifdef PFRULE_INOUT_COUNTS
if(packets) if(packets)
*packets = pr.rule.packets[0] + pr.rule.packets[1]; *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"); syslog(LOG_ERR, "ioctl(dev, DIOCGETRULE): %m");
return -1; 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); syslog(LOG_INFO, "rule with label '%s' is not a IGD pinhole", pr.rule.label);
continue; 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 /* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2012-2014 Thomas Bernard * (c) 2012-2014 Thomas Bernard
@ -32,11 +32,14 @@ static int print_pinhole(int uid)
int proto; int proto;
unsigned int timestamp; unsigned int timestamp;
u_int64_t packets, bytes; u_int64_t packets, bytes;
char desc[64];
r = get_pinhole_info((unsigned short)uid, r = get_pinhole_info((unsigned short)uid,
rem_host, sizeof(rem_host), &rem_port, rem_host, sizeof(rem_host), &rem_port,
int_client, sizeof(int_client), &int_port, int_client, sizeof(int_client), &int_port,
&proto, &timestamp, &proto,
desc, sizeof(desc),
&timestamp,
&packets, &bytes); &packets, &bytes);
if(r < 0) { if(r < 0) {
fprintf(stderr, "get_pinhole(%d) returned %d\n", uid, r); 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", printf("pinhole %d : [%s]:%hu => [%s]:%hu proto=%d ts=%u\n",
uid, rem_host, rem_port, int_client, int_port, uid, rem_host, rem_port, int_client, int_port,
proto, timestamp); proto, timestamp);
printf(" desc='%s'\n", desc);
printf(" packets=%llu bytes=%llu\n", packets, bytes); printf(" packets=%llu bytes=%llu\n", packets, bytes);
} }
return r; return r;
@ -65,12 +69,12 @@ int main(int argc, char * *argv)
return 1; 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) { if(uid < 0) {
fprintf(stderr, "add_pinhole() failed\n"); fprintf(stderr, "add_pinhole() failed\n");
} }
printf("add_pinhole() returned %d\n", uid); 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) { if(uid < 0) {
fprintf(stderr, "add_pinhole() failed\n"); fprintf(stderr, "add_pinhole() failed\n");
} }