xref: /freebsd/sbin/ipf/libipf/printhashnode.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
141edb306SCy Schubert 
241edb306SCy Schubert /*
341edb306SCy Schubert  * Copyright (C) 2012 by Darren Reed.
441edb306SCy Schubert  *
541edb306SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
641edb306SCy Schubert  */
741edb306SCy Schubert 
841edb306SCy Schubert #include "ipf.h"
941edb306SCy Schubert 
1041edb306SCy Schubert 
1141edb306SCy Schubert iphtent_t *
printhashnode(iphtable_t * iph,iphtent_t * ipep,copyfunc_t copyfunc,int opts,wordtab_t * fields)12efeb8bffSCy Schubert printhashnode(iphtable_t *iph, iphtent_t *ipep, copyfunc_t copyfunc, int opts,
13efeb8bffSCy Schubert 	wordtab_t *fields)
1441edb306SCy Schubert {
1541edb306SCy Schubert 	iphtent_t ipe;
1641edb306SCy Schubert 	u_int hv;
1741edb306SCy Schubert 	int i;
1841edb306SCy Schubert 
1941edb306SCy Schubert 	if ((*copyfunc)(ipep, &ipe, sizeof(ipe)))
20*2582ae57SCy Schubert 		return (NULL);
2141edb306SCy Schubert 
2241edb306SCy Schubert 	hv = IPE_V4_HASH_FN(ipe.ipe_addr.i6[0], ipe.ipe_mask.i6[0],
2341edb306SCy Schubert 			    iph->iph_size);
2441edb306SCy Schubert 
2541edb306SCy Schubert 	if (fields != NULL) {
2641edb306SCy Schubert 		for (i = 0; fields[i].w_value != 0; i++) {
2741edb306SCy Schubert 			printpoolfield(&ipe, IPLT_HASH, i);
2841edb306SCy Schubert 			if (fields[i + 1].w_value != 0)
2941edb306SCy Schubert 				printf("\t");
3041edb306SCy Schubert 		}
3141edb306SCy Schubert 		printf("\n");
3241edb306SCy Schubert 	} else if ((opts & OPT_DEBUG) != 0) {
3341edb306SCy Schubert #ifdef USE_INET6
3441edb306SCy Schubert 		if (ipe.ipe_family == AF_INET6) {
3541edb306SCy Schubert 			char buf[INET6_ADDRSTRLEN + 1];
3641edb306SCy Schubert 			const char *str;
3741edb306SCy Schubert 
3841edb306SCy Schubert 			buf[0] = '\0';
3941edb306SCy Schubert 			str = inet_ntop(AF_INET6, &ipe.ipe_addr.in6,
4041edb306SCy Schubert 				buf, sizeof(buf) - 1);
4141edb306SCy Schubert 			if (str == NULL)
4241edb306SCy Schubert 				str = "???";
4341edb306SCy Schubert 			PRINTF("\t%d\tAddress: %s", hv, str);
4441edb306SCy Schubert 			printmask(ipe.ipe_family, (u_32_t *)&ipe.ipe_mask.in4_addr);
4541edb306SCy Schubert 			PRINTF("\tRef. Count: %d\tGroup: %s\n", ipe.ipe_ref,
4641edb306SCy Schubert 				ipe.ipe_group);
4741edb306SCy Schubert #ifdef USE_QUAD_T
4841edb306SCy Schubert 			PRINTF("\tHits: %"PRIu64"\tBytes: %"PRIu64"\n",
4941edb306SCy Schubert 			       ipe.ipe_hits, ipe.ipe_bytes);
5041edb306SCy Schubert #else
5141edb306SCy Schubert 			PRINTF("\tHits: %lu\tBytes: %lu\n",
5241edb306SCy Schubert 			       ipe.ipe_hits, ipe.ipe_bytes);
5341edb306SCy Schubert #endif /* USE_QUAD_T */
5441edb306SCy Schubert 		} else if (ipe.ipe_family == AF_INET) {
5541edb306SCy Schubert #else
5641edb306SCy Schubert 		if (ipe.ipe_family == AF_INET) {
5741edb306SCy Schubert #endif /* USE_INET6 */
5841edb306SCy Schubert 			PRINTF("\t%d\tAddress: %s", hv,
5941edb306SCy Schubert 				inet_ntoa(ipe.ipe_addr.in4));
6041edb306SCy Schubert 			printmask(ipe.ipe_family, (u_32_t *)&ipe.ipe_mask.in4_addr);
6141edb306SCy Schubert 			PRINTF("\tRef. Count: %d\tGroup: %s\n", ipe.ipe_ref,
6241edb306SCy Schubert 				ipe.ipe_group);
6341edb306SCy Schubert #ifdef USE_QUAD_T
6441edb306SCy Schubert 			PRINTF("\tHits: %"PRIu64"\tBytes: %"PRIu64"\n",
6541edb306SCy Schubert 			       ipe.ipe_hits, ipe.ipe_bytes);
6641edb306SCy Schubert #else
6741edb306SCy Schubert 			PRINTF("\tHits: %lu\tBytes: %lu\n",
6841edb306SCy Schubert 			       ipe.ipe_hits, ipe.ipe_bytes);
6941edb306SCy Schubert #endif /* USE_QUAD_T */
7041edb306SCy Schubert 		} else {
7141edb306SCy Schubert 			PRINTF("\tAddress: family: %d\n",
7241edb306SCy Schubert 				ipe.ipe_family);
7341edb306SCy Schubert 		}
7441edb306SCy Schubert 	} else {
7541edb306SCy Schubert 		putchar(' ');
7641edb306SCy Schubert 		printip(ipe.ipe_family, (u_32_t *)&ipe.ipe_addr.in4_addr);
7741edb306SCy Schubert 		printmask(ipe.ipe_family, (u_32_t *)&ipe.ipe_mask.in4_addr);
7841edb306SCy Schubert 		if (ipe.ipe_value != 0) {
7941edb306SCy Schubert 			switch (iph->iph_type & ~IPHASH_ANON)
8041edb306SCy Schubert 			{
8141edb306SCy Schubert 			case IPHASH_GROUPMAP :
8241edb306SCy Schubert 				if (strncmp(ipe.ipe_group, iph->iph_name,
8341edb306SCy Schubert 					    FR_GROUPLEN))
8441edb306SCy Schubert 					PRINTF(", group=%s", ipe.ipe_group);
8541edb306SCy Schubert 				break;
8641edb306SCy Schubert 			}
8741edb306SCy Schubert 		}
8841edb306SCy Schubert 		putchar(';');
8941edb306SCy Schubert 	}
9041edb306SCy Schubert 
9141edb306SCy Schubert 	ipep = ipe.ipe_next;
92*2582ae57SCy Schubert 	return (ipep);
9341edb306SCy Schubert }
94