xref: /freebsd/sbin/ipf/libipf/printpoolfield.c (revision 177034c44ed18d99e6cc85dfddd2bb04b41d38ac)
1 /*
2  * Copyright (C) 2012 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: printpoolfield.c,v 1.1.2.4 2012/01/26 05:44:26 darren_r Exp $
7  */
8 
9 #include "ipf.h"
10 
11 wordtab_t poolfields[] = {
12 	{ "all",	-2 },
13 	{ "address",	1 },
14 	{ "mask",	2 },
15 	{ "ifname",	3 },
16 	{ "pkts",	4 },
17 	{ "bytes",	5 },
18 	{ "family",	6 },
19 	{ NULL, 0 }
20 };
21 
22 
23 void
24 printpoolfield(void *p, int ptype, int fieldnum)
25 {
26 	addrfamily_t *a;
27 	char abuf[80];
28 	int i;
29 
30 	switch (fieldnum)
31 	{
32 	case -2 :
33 		for (i = 1; poolfields[i].w_word != NULL; i++) {
34 			if (poolfields[i].w_value > 0) {
35 				printpoolfield(p, ptype, i);
36 				if (poolfields[i + 1].w_value > 0)
37 					putchar('\t');
38 			}
39 		}
40 		break;
41 
42 	case 1:
43 		if (ptype == IPLT_POOL) {
44 			ip_pool_node_t *node = (ip_pool_node_t *)p;
45 
46 			if (node->ipn_info)
47 				PRINTF("!");
48 			a = &node->ipn_addr;
49 			PRINTF("%s", inet_ntop(a->adf_family, &a->adf_addr,
50 					       abuf, sizeof(abuf)));
51 		} else if (ptype == IPLT_HASH) {
52 			iphtent_t *node = (iphtent_t *)p;
53 
54 			PRINTF("%s", inet_ntop(node->ipe_family,
55 					       &node->ipe_addr,
56 					       abuf, sizeof(abuf)));
57 		} else if (ptype == IPLT_DSTLIST) {
58 			ipf_dstnode_t *node = (ipf_dstnode_t *)p;
59 
60 			a = &node->ipfd_dest.fd_addr;
61 			PRINTF("%s", inet_ntop(a->adf_family, &a->adf_addr,
62 					       abuf, sizeof(abuf)));
63 		}
64 		break;
65 
66 	case 2:
67 		if (ptype == IPLT_POOL) {
68 			ip_pool_node_t *node = (ip_pool_node_t *)p;
69 
70 			a = &node->ipn_mask;
71 			PRINTF("%s", inet_ntop(a->adf_family, &a->adf_addr,
72 					       abuf, sizeof(abuf)));
73 		} else if (ptype == IPLT_HASH) {
74 			iphtent_t *node = (iphtent_t *)p;
75 
76 			PRINTF("%s", inet_ntop(node->ipe_family,
77 					       &node->ipe_mask,
78 					       abuf, sizeof(abuf)));
79 		} else if (ptype == IPLT_DSTLIST) {
80 			PRINTF("%s", "");
81 		}
82 		break;
83 
84 	case 3:
85 		if (ptype == IPLT_POOL) {
86 			PRINTF("%s", "");
87 		} else if (ptype == IPLT_HASH) {
88 			PRINTF("%s", "");
89 		} else if (ptype == IPLT_DSTLIST) {
90 			ipf_dstnode_t *node = (ipf_dstnode_t *)p;
91 
92 			if (node->ipfd_dest.fd_name == -1) {
93 				PRINTF("%s", "");
94 			} else {
95 				PRINTF("%s", node->ipfd_names +
96 				       node->ipfd_dest.fd_name);
97 			}
98 		}
99 		break;
100 
101 	case 4:
102 		if (ptype == IPLT_POOL) {
103 			ip_pool_node_t *node = (ip_pool_node_t *)p;
104 
105 #ifdef USE_QUAD_T
106 			PRINTF("%"PRIu64"", node->ipn_hits);
107 #else
108 			PRINTF("%lu", node->ipn_hits);
109 #endif
110 		} else if (ptype == IPLT_HASH) {
111 			iphtent_t *node = (iphtent_t *)p;
112 
113 #ifdef USE_QUAD_T
114 			PRINTF("%"PRIu64"", node->ipe_hits);
115 #else
116 			PRINTF("%lu", node->ipe_hits);
117 #endif
118 		} else if (ptype == IPLT_DSTLIST) {
119 			printf("0");
120 		}
121 		break;
122 
123 	case 5:
124 		if (ptype == IPLT_POOL) {
125 			ip_pool_node_t *node = (ip_pool_node_t *)p;
126 
127 #ifdef USE_QUAD_T
128 			PRINTF("%"PRIu64"", node->ipn_bytes);
129 #else
130 			PRINTF("%lu", node->ipn_bytes);
131 #endif
132 		} else if (ptype == IPLT_HASH) {
133 			iphtent_t *node = (iphtent_t *)p;
134 
135 #ifdef USE_QUAD_T
136 			PRINTF("%"PRIu64"", node->ipe_bytes);
137 #else
138 			PRINTF("%lu", node->ipe_bytes);
139 #endif
140 		} else if (ptype == IPLT_DSTLIST) {
141 			printf("0");
142 		}
143 		break;
144 
145 	case 6:
146 		if (ptype == IPLT_POOL) {
147 			ip_pool_node_t *node = (ip_pool_node_t *)p;
148 
149 			PRINTF("%s", familyname(node->ipn_addr.adf_family));
150 		} else if (ptype == IPLT_HASH) {
151 			iphtent_t *node = (iphtent_t *)p;
152 
153 			PRINTF("%s", familyname(node->ipe_family));
154 		} else if (ptype == IPLT_DSTLIST) {
155 			ipf_dstnode_t *node = (ipf_dstnode_t *)p;
156 
157 			a = &node->ipfd_dest.fd_addr;
158 			PRINTF("%s", familyname(a->adf_family));
159 		}
160 		break;
161 
162 	default :
163 		break;
164 	}
165 }
166