xref: /freebsd/sbin/ipf/libipf/printhashnode.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
1 
2 /*
3  * Copyright (C) 2012 by Darren Reed.
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  */
7 
8 #include "ipf.h"
9 
10 
11 iphtent_t *
printhashnode(iphtable_t * iph,iphtent_t * ipep,copyfunc_t copyfunc,int opts,wordtab_t * fields)12 printhashnode(iphtable_t *iph, iphtent_t *ipep, copyfunc_t copyfunc, int opts,
13 	wordtab_t *fields)
14 {
15 	iphtent_t ipe;
16 	u_int hv;
17 	int i;
18 
19 	if ((*copyfunc)(ipep, &ipe, sizeof(ipe)))
20 		return (NULL);
21 
22 	hv = IPE_V4_HASH_FN(ipe.ipe_addr.i6[0], ipe.ipe_mask.i6[0],
23 			    iph->iph_size);
24 
25 	if (fields != NULL) {
26 		for (i = 0; fields[i].w_value != 0; i++) {
27 			printpoolfield(&ipe, IPLT_HASH, i);
28 			if (fields[i + 1].w_value != 0)
29 				printf("\t");
30 		}
31 		printf("\n");
32 	} else if ((opts & OPT_DEBUG) != 0) {
33 #ifdef USE_INET6
34 		if (ipe.ipe_family == AF_INET6) {
35 			char buf[INET6_ADDRSTRLEN + 1];
36 			const char *str;
37 
38 			buf[0] = '\0';
39 			str = inet_ntop(AF_INET6, &ipe.ipe_addr.in6,
40 				buf, sizeof(buf) - 1);
41 			if (str == NULL)
42 				str = "???";
43 			PRINTF("\t%d\tAddress: %s", hv, str);
44 			printmask(ipe.ipe_family, (u_32_t *)&ipe.ipe_mask.in4_addr);
45 			PRINTF("\tRef. Count: %d\tGroup: %s\n", ipe.ipe_ref,
46 				ipe.ipe_group);
47 #ifdef USE_QUAD_T
48 			PRINTF("\tHits: %"PRIu64"\tBytes: %"PRIu64"\n",
49 			       ipe.ipe_hits, ipe.ipe_bytes);
50 #else
51 			PRINTF("\tHits: %lu\tBytes: %lu\n",
52 			       ipe.ipe_hits, ipe.ipe_bytes);
53 #endif /* USE_QUAD_T */
54 		} else if (ipe.ipe_family == AF_INET) {
55 #else
56 		if (ipe.ipe_family == AF_INET) {
57 #endif /* USE_INET6 */
58 			PRINTF("\t%d\tAddress: %s", hv,
59 				inet_ntoa(ipe.ipe_addr.in4));
60 			printmask(ipe.ipe_family, (u_32_t *)&ipe.ipe_mask.in4_addr);
61 			PRINTF("\tRef. Count: %d\tGroup: %s\n", ipe.ipe_ref,
62 				ipe.ipe_group);
63 #ifdef USE_QUAD_T
64 			PRINTF("\tHits: %"PRIu64"\tBytes: %"PRIu64"\n",
65 			       ipe.ipe_hits, ipe.ipe_bytes);
66 #else
67 			PRINTF("\tHits: %lu\tBytes: %lu\n",
68 			       ipe.ipe_hits, ipe.ipe_bytes);
69 #endif /* USE_QUAD_T */
70 		} else {
71 			PRINTF("\tAddress: family: %d\n",
72 				ipe.ipe_family);
73 		}
74 	} else {
75 		putchar(' ');
76 		printip(ipe.ipe_family, (u_32_t *)&ipe.ipe_addr.in4_addr);
77 		printmask(ipe.ipe_family, (u_32_t *)&ipe.ipe_mask.in4_addr);
78 		if (ipe.ipe_value != 0) {
79 			switch (iph->iph_type & ~IPHASH_ANON)
80 			{
81 			case IPHASH_GROUPMAP :
82 				if (strncmp(ipe.ipe_group, iph->iph_name,
83 					    FR_GROUPLEN))
84 					PRINTF(", group=%s", ipe.ipe_group);
85 				break;
86 			}
87 		}
88 		putchar(';');
89 	}
90 
91 	ipep = ipe.ipe_next;
92 	return (ipep);
93 }
94