Add get_pinhole()

This commit is contained in:
Thomas Bernard 2012-04-21 00:08:06 +02:00
parent c3d979a9cd
commit 8148acc55c
3 changed files with 89 additions and 3 deletions

View File

@ -1,4 +1,4 @@
/* $Id: pfpinhole.c,v 1.7 2012/04/20 14:48:03 nanard Exp $ */
/* $Id: pfpinhole.c,v 1.9 2012/04/20 22:07:28 nanard Exp $ */
/* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2006-2012 Thomas Bernard
@ -195,5 +195,59 @@ int delete_pinhole(unsigned short uid)
return -1;
}
int get_pinhole(unsigned short uid,
char * rem_host, int rem_hostlen, unsigned short * rem_port,
char * int_client, int int_clientlen, unsigned short * int_port,
int * proto, unsigned int * timestamp)
{
int i, n;
struct pfioc_rule pr;
char label_start[PF_RULE_LABEL_SIZE];
char tmp_label[PF_RULE_LABEL_SIZE];
char * p;
if(dev<0) {
syslog(LOG_ERR, "pf device is not open");
return -1;
}
snprintf(label_start, sizeof(label_start),
"pinhole-%hu", uid);
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<n; i++) {
pr.nr = i;
if(ioctl(dev, DIOCGETRULE, &pr) < 0) {
syslog(LOG_ERR, "ioctl(dev, DIOCGETRULE): %m");
return -1;
}
strlcpy(tmp_label, pr.rule.label, sizeof(tmp_label));
p = tmp_label;
strsep(&p, " ");
if(0 == strcmp(tmp_label, label_start)) {
if(inet_ntop(AF_INET6, &pr.rule.src.addr.v.a.addr.v6, rem_host, rem_hostlen) == NULL) {
return -2;
}
*rem_port = ntohs(pr.rule.src.port[0]);
if(inet_ntop(AF_INET6, &pr.rule.dst.addr.v.a.addr.v6, int_client, int_clientlen) == NULL) {
return -2;
}
*int_port = ntohs(pr.rule.dst.port[0]);
*proto = pr.rule.proto;
sscanf(p, "ts-%u", timestamp);
return 0;
}
}
/* not found */
return -1;
}
#endif /* ENABLE_IPV6 */

View File

@ -1,4 +1,4 @@
/* $Id: pfpinhole.h,v 1.3 2012/04/20 14:34:11 nanard Exp $ */
/* $Id: pfpinhole.h,v 1.4 2012/04/20 21:49:13 nanard Exp $ */
/* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2012 Thomas Bernard
@ -15,5 +15,10 @@ int add_pinhole(const char * ifname,
int delete_pinhole(unsigned short uid);
int get_pinhole(unsigned short uid,
char * rem_host, int rem_hostlen, unsigned short * rem_port,
char * int_client, int int_clientlen, unsigned short * int_port,
int * proto, unsigned int * timestamp);
#endif

View File

@ -1,4 +1,4 @@
/* $Id: testpfpinhole.c,v 1.5 2012/04/20 14:36:23 nanard Exp $ */
/* $Id: testpfpinhole.c,v 1.6 2012/04/20 21:49:13 nanard Exp $ */
/* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2006-2012 Thomas Bernard
@ -21,6 +21,30 @@ const char * tag = NULL;
const char * anchor_name = "miniupnpd";
const char * queue = NULL;
static int print_pinhole(int uid)
{
int r;
char rem_host[64];
unsigned short rem_port;
char int_client[64];
unsigned short int_port;
int proto;
unsigned int timestamp;
r = get_pinhole((unsigned short)uid,
rem_host, sizeof(rem_host), &rem_port,
int_client, sizeof(int_client), &int_port,
&proto, &timestamp);
if(r < 0) {
fprintf(stderr, "get_pinhole(%d) returned %d\n", uid, r);
} else {
printf("pinhole %d : [%s]:%hu => [%s]:%hu proto=%d ts=%u\n",
uid, rem_host, rem_port, int_client, int_port,
proto, timestamp);
}
return r;
}
int main(int argc, char * *argv)
{
#ifndef ENABLE_IPV6
@ -47,6 +71,9 @@ int main(int argc, char * *argv)
}
printf("add_pinhole() returned %d\n", uid);
print_pinhole(1);
print_pinhole(2);
ret = delete_pinhole(1);
printf("delete_pinhole() returned %d\n", ret);
ret = delete_pinhole(2);