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