xref: /freebsd/sbin/ipf/libipf/printpoolfield.c (revision efeb8bffe34422937c7f8df836afb5b817366d16)
141edb306SCy Schubert /*
241edb306SCy Schubert  * Copyright (C) 2012 by Darren Reed.
341edb306SCy Schubert  *
441edb306SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
541edb306SCy Schubert  *
641edb306SCy Schubert  * $Id: printpoolfield.c,v 1.1.2.4 2012/01/26 05:44:26 darren_r Exp $
741edb306SCy Schubert  */
841edb306SCy Schubert 
941edb306SCy Schubert #include "ipf.h"
1041edb306SCy Schubert 
1141edb306SCy Schubert wordtab_t poolfields[] = {
1241edb306SCy Schubert 	{ "all",	-2 },
1341edb306SCy Schubert 	{ "address",	1 },
1441edb306SCy Schubert 	{ "mask",	2 },
1541edb306SCy Schubert 	{ "ifname",	3 },
1641edb306SCy Schubert 	{ "pkts",	4 },
1741edb306SCy Schubert 	{ "bytes",	5 },
1841edb306SCy Schubert 	{ "family",	6 },
1941edb306SCy Schubert 	{ NULL, 0 }
2041edb306SCy Schubert };
2141edb306SCy Schubert 
2241edb306SCy Schubert 
2341edb306SCy Schubert void
printpoolfield(void * p,int ptype,int fieldnum)24*efeb8bffSCy Schubert printpoolfield(void *p, int ptype, int fieldnum)
2541edb306SCy Schubert {
2641edb306SCy Schubert 	addrfamily_t *a;
2741edb306SCy Schubert 	char abuf[80];
2841edb306SCy Schubert 	int i;
2941edb306SCy Schubert 
3041edb306SCy Schubert 	switch (fieldnum)
3141edb306SCy Schubert 	{
3241edb306SCy Schubert 	case -2 :
3341edb306SCy Schubert 		for (i = 1; poolfields[i].w_word != NULL; i++) {
3441edb306SCy Schubert 			if (poolfields[i].w_value > 0) {
3541edb306SCy Schubert 				printpoolfield(p, ptype, i);
3641edb306SCy Schubert 				if (poolfields[i + 1].w_value > 0)
3741edb306SCy Schubert 					putchar('\t');
3841edb306SCy Schubert 			}
3941edb306SCy Schubert 		}
4041edb306SCy Schubert 		break;
4141edb306SCy Schubert 
4241edb306SCy Schubert 	case 1:
4341edb306SCy Schubert 		if (ptype == IPLT_POOL) {
4441edb306SCy Schubert 			ip_pool_node_t *node = (ip_pool_node_t *)p;
4541edb306SCy Schubert 
4641edb306SCy Schubert 			if (node->ipn_info)
4741edb306SCy Schubert 				PRINTF("!");
4841edb306SCy Schubert 			a = &node->ipn_addr;
4941edb306SCy Schubert 			PRINTF("%s", inet_ntop(a->adf_family, &a->adf_addr,
5041edb306SCy Schubert 					       abuf, sizeof(abuf)));
5141edb306SCy Schubert 		} else if (ptype == IPLT_HASH) {
5241edb306SCy Schubert 			iphtent_t *node = (iphtent_t *)p;
5341edb306SCy Schubert 
5441edb306SCy Schubert 			PRINTF("%s", inet_ntop(node->ipe_family,
5541edb306SCy Schubert 					       &node->ipe_addr,
5641edb306SCy Schubert 					       abuf, sizeof(abuf)));
5741edb306SCy Schubert 		} else if (ptype == IPLT_DSTLIST) {
5841edb306SCy Schubert 			ipf_dstnode_t *node = (ipf_dstnode_t *)p;
5941edb306SCy Schubert 
6041edb306SCy Schubert 			a = &node->ipfd_dest.fd_addr;
6141edb306SCy Schubert 			PRINTF("%s", inet_ntop(a->adf_family, &a->adf_addr,
6241edb306SCy Schubert 					       abuf, sizeof(abuf)));
6341edb306SCy Schubert 		}
6441edb306SCy Schubert 		break;
6541edb306SCy Schubert 
6641edb306SCy Schubert 	case 2:
6741edb306SCy Schubert 		if (ptype == IPLT_POOL) {
6841edb306SCy Schubert 			ip_pool_node_t *node = (ip_pool_node_t *)p;
6941edb306SCy Schubert 
7041edb306SCy Schubert 			a = &node->ipn_mask;
7141edb306SCy Schubert 			PRINTF("%s", inet_ntop(a->adf_family, &a->adf_addr,
7241edb306SCy Schubert 					       abuf, sizeof(abuf)));
7341edb306SCy Schubert 		} else if (ptype == IPLT_HASH) {
7441edb306SCy Schubert 			iphtent_t *node = (iphtent_t *)p;
7541edb306SCy Schubert 
7641edb306SCy Schubert 			PRINTF("%s", inet_ntop(node->ipe_family,
7741edb306SCy Schubert 					       &node->ipe_mask,
7841edb306SCy Schubert 					       abuf, sizeof(abuf)));
7941edb306SCy Schubert 		} else if (ptype == IPLT_DSTLIST) {
8041edb306SCy Schubert 			PRINTF("%s", "");
8141edb306SCy Schubert 		}
8241edb306SCy Schubert 		break;
8341edb306SCy Schubert 
8441edb306SCy Schubert 	case 3:
8541edb306SCy Schubert 		if (ptype == IPLT_POOL) {
8641edb306SCy Schubert 			PRINTF("%s", "");
8741edb306SCy Schubert 		} else if (ptype == IPLT_HASH) {
8841edb306SCy Schubert 			PRINTF("%s", "");
8941edb306SCy Schubert 		} else if (ptype == IPLT_DSTLIST) {
9041edb306SCy Schubert 			ipf_dstnode_t *node = (ipf_dstnode_t *)p;
9141edb306SCy Schubert 
9241edb306SCy Schubert 			if (node->ipfd_dest.fd_name == -1) {
9341edb306SCy Schubert 				PRINTF("%s", "");
9441edb306SCy Schubert 			} else {
9541edb306SCy Schubert 				PRINTF("%s", node->ipfd_names +
9641edb306SCy Schubert 				       node->ipfd_dest.fd_name);
9741edb306SCy Schubert 			}
9841edb306SCy Schubert 		}
9941edb306SCy Schubert 		break;
10041edb306SCy Schubert 
10141edb306SCy Schubert 	case 4:
10241edb306SCy Schubert 		if (ptype == IPLT_POOL) {
10341edb306SCy Schubert 			ip_pool_node_t *node = (ip_pool_node_t *)p;
10441edb306SCy Schubert 
10541edb306SCy Schubert #ifdef USE_QUAD_T
10641edb306SCy Schubert 			PRINTF("%"PRIu64"", node->ipn_hits);
10741edb306SCy Schubert #else
10841edb306SCy Schubert 			PRINTF("%lu", node->ipn_hits);
10941edb306SCy Schubert #endif
11041edb306SCy Schubert 		} else if (ptype == IPLT_HASH) {
11141edb306SCy Schubert 			iphtent_t *node = (iphtent_t *)p;
11241edb306SCy Schubert 
11341edb306SCy Schubert #ifdef USE_QUAD_T
11441edb306SCy Schubert 			PRINTF("%"PRIu64"", node->ipe_hits);
11541edb306SCy Schubert #else
11641edb306SCy Schubert 			PRINTF("%lu", node->ipe_hits);
11741edb306SCy Schubert #endif
11841edb306SCy Schubert 		} else if (ptype == IPLT_DSTLIST) {
11941edb306SCy Schubert 			printf("0");
12041edb306SCy Schubert 		}
12141edb306SCy Schubert 		break;
12241edb306SCy Schubert 
12341edb306SCy Schubert 	case 5:
12441edb306SCy Schubert 		if (ptype == IPLT_POOL) {
12541edb306SCy Schubert 			ip_pool_node_t *node = (ip_pool_node_t *)p;
12641edb306SCy Schubert 
12741edb306SCy Schubert #ifdef USE_QUAD_T
12841edb306SCy Schubert 			PRINTF("%"PRIu64"", node->ipn_bytes);
12941edb306SCy Schubert #else
13041edb306SCy Schubert 			PRINTF("%lu", node->ipn_bytes);
13141edb306SCy Schubert #endif
13241edb306SCy Schubert 		} else if (ptype == IPLT_HASH) {
13341edb306SCy Schubert 			iphtent_t *node = (iphtent_t *)p;
13441edb306SCy Schubert 
13541edb306SCy Schubert #ifdef USE_QUAD_T
13641edb306SCy Schubert 			PRINTF("%"PRIu64"", node->ipe_bytes);
13741edb306SCy Schubert #else
13841edb306SCy Schubert 			PRINTF("%lu", node->ipe_bytes);
13941edb306SCy Schubert #endif
14041edb306SCy Schubert 		} else if (ptype == IPLT_DSTLIST) {
14141edb306SCy Schubert 			printf("0");
14241edb306SCy Schubert 		}
14341edb306SCy Schubert 		break;
14441edb306SCy Schubert 
14541edb306SCy Schubert 	case 6:
14641edb306SCy Schubert 		if (ptype == IPLT_POOL) {
14741edb306SCy Schubert 			ip_pool_node_t *node = (ip_pool_node_t *)p;
14841edb306SCy Schubert 
14941edb306SCy Schubert 			PRINTF("%s", familyname(node->ipn_addr.adf_family));
15041edb306SCy Schubert 		} else if (ptype == IPLT_HASH) {
15141edb306SCy Schubert 			iphtent_t *node = (iphtent_t *)p;
15241edb306SCy Schubert 
15341edb306SCy Schubert 			PRINTF("%s", familyname(node->ipe_family));
15441edb306SCy Schubert 		} else if (ptype == IPLT_DSTLIST) {
15541edb306SCy Schubert 			ipf_dstnode_t *node = (ipf_dstnode_t *)p;
15641edb306SCy Schubert 
15741edb306SCy Schubert 			a = &node->ipfd_dest.fd_addr;
15841edb306SCy Schubert 			PRINTF("%s", familyname(a->adf_family));
15941edb306SCy Schubert 		}
16041edb306SCy Schubert 		break;
16141edb306SCy Schubert 
16241edb306SCy Schubert 	default :
16341edb306SCy Schubert 		break;
16441edb306SCy Schubert 	}
16541edb306SCy Schubert }
166