1/* $Id: testpfpinhole.c,v 1.12 2014/05/15 21:23:43 nanard Exp $ */
2/* MiniUPnP project
3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4 * (c) 2012-2014 Thomas Bernard
5 * This software is subject to the conditions detailed
6 * in the LICENCE file provided within the distribution */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <sys/types.h>
11#include <netinet/in.h>
12#include <syslog.h>
13
14#include "../config.h"
15#include "obsdrdr.h"
16#include "pfpinhole.h"
17
18int runtime_flags = 0;
19const char * tag = NULL;
20
21const char * anchor_name = "miniupnpd";
22const char * queue = NULL;
23
24#ifdef ENABLE_IPV6
25static int print_pinhole(int uid)
26{
27	int r;
28	char rem_host[64];
29	unsigned short rem_port;
30	char int_client[64];
31	unsigned short int_port;
32	int proto;
33	unsigned int timestamp;
34	u_int64_t packets, bytes;
35	char desc[64];
36
37	r = get_pinhole_info((unsigned short)uid,
38	                     rem_host, sizeof(rem_host), &rem_port,
39	                     int_client, sizeof(int_client), &int_port,
40	                     &proto,
41	                     desc, sizeof(desc),
42	                     &timestamp,
43	                     &packets, &bytes);
44	if(r < 0) {
45		fprintf(stderr, "get_pinhole(%d) returned %d\n", uid, r);
46	} else {
47		printf("pinhole %d : [%s]:%hu => [%s]:%hu proto=%d ts=%u\n",
48		       uid, rem_host, rem_port, int_client, int_port,
49		       proto, timestamp);
50		printf("    desc='%s'\n", desc);
51		printf("    packets=%llu bytes=%llu\n", packets, bytes);
52	}
53	return r;
54}
55#endif
56
57int main(int argc, char * *argv)
58{
59#ifndef ENABLE_IPV6
60	fprintf(stderr,"nothing to test, ENABLE_IPV6 is not defined in config.h\n");
61	return 1;
62#else
63	int uid;
64	int ret;
65
66	openlog("testpfpinhole", LOG_PERROR, LOG_USER);
67	if(init_redirect() < 0) {
68		fprintf(stderr, "init_redirect() failed\n");
69		return 1;
70	}
71
72	uid = add_pinhole("ep0", "2001::1:2:3", 12345, "123::ff", 54321, IPPROTO_UDP, "description test 1", 424242);
73	if(uid < 0) {
74		fprintf(stderr, "add_pinhole() failed\n");
75	}
76	printf("add_pinhole() returned %d\n", uid);
77	uid = add_pinhole("ep0", NULL, 0, "dead:beef::42:42", 8080, IPPROTO_UDP, "description test 2", 4321000);
78	if(uid < 0) {
79		fprintf(stderr, "add_pinhole() failed\n");
80	}
81	printf("add_pinhole() returned %d\n", uid);
82
83	print_pinhole(1);
84	print_pinhole(2);
85	clean_pinhole_list(NULL);
86
87	ret = delete_pinhole(1);
88	printf("delete_pinhole() returned %d\n", ret);
89	ret = delete_pinhole(2);
90	printf("delete_pinhole() returned %d\n", ret);
91#endif
92	return 0;
93}
94
95