printhash.c revision 145519
185974Sdfr/* $FreeBSD: head/contrib/ipfilter/lib/printhash.c 145519 2005-04-25 18:20:15Z darrenr $ */ 285974Sdfr 385974Sdfr/* 485974Sdfr * Copyright (C) 2002 by Darren Reed. 585974Sdfr * 685974Sdfr * See the IPFILTER.LICENCE file for details on licencing. 785974Sdfr */ 885974Sdfr 985974Sdfr#include "ipf.h" 1085974Sdfr 1185974Sdfr#define PRINTF (void)printf 1285974Sdfr#define FPRINTF (void)fprintf 1385974Sdfr 1485974Sdfr 1585974Sdfriphtable_t *printhash(hp, copyfunc, name, opts) 1685974Sdfriphtable_t *hp; 1785974Sdfrcopyfunc_t copyfunc; 1885974Sdfrchar *name; 1985974Sdfrint opts; 2085974Sdfr{ 2185974Sdfr iphtent_t *ipep, **table; 2285974Sdfr iphtable_t iph; 2385974Sdfr int i, printed; 2485974Sdfr size_t sz; 2585974Sdfr 2685974Sdfr if ((*copyfunc)((char *)hp, (char *)&iph, sizeof(iph))) 2792986Sobrien return NULL; 2892986Sobrien 2992986Sobrien if ((name != NULL) && strncmp(name, iph.iph_name, FR_GROUPLEN)) 3085974Sdfr return iph.iph_next; 3185974Sdfr 3285974Sdfr if ((opts & OPT_DEBUG) == 0) { 3385974Sdfr if ((iph.iph_type & IPHASH_ANON) == IPHASH_ANON) 3485974Sdfr PRINTF("# 'anonymous' table\n"); 3585974Sdfr switch (iph.iph_type & ~IPHASH_ANON) 3685974Sdfr { 3785974Sdfr case IPHASH_LOOKUP : 3885974Sdfr PRINTF("table"); 3985974Sdfr break; 40118696Smarcel case IPHASH_GROUPMAP : 41118696Smarcel PRINTF("group-map"); 4285974Sdfr if (iph.iph_flags & FR_INQUE) 43118696Smarcel PRINTF(" in"); 4485974Sdfr else if (iph.iph_flags & FR_OUTQUE) 45 PRINTF(" out"); 46 else 47 PRINTF(" ???"); 48 break; 49 default : 50 PRINTF("%#x", iph.iph_type); 51 break; 52 } 53 PRINTF(" role = "); 54 } else { 55 PRINTF("Hash Table Number: %s", iph.iph_name); 56 if ((iph.iph_type & IPHASH_ANON) == IPHASH_ANON) 57 PRINTF("(anon)"); 58 putchar(' '); 59 PRINTF("Role: "); 60 } 61 62 switch (iph.iph_unit) 63 { 64 case IPL_LOGNAT : 65 PRINTF("nat"); 66 break; 67 case IPL_LOGIPF : 68 PRINTF("ipf"); 69 break; 70 case IPL_LOGAUTH : 71 PRINTF("auth"); 72 break; 73 case IPL_LOGCOUNT : 74 PRINTF("count"); 75 break; 76 default : 77 PRINTF("#%d", iph.iph_unit); 78 break; 79 } 80 81 if ((opts & OPT_DEBUG) == 0) { 82 if ((iph.iph_type & ~IPHASH_ANON) == IPHASH_LOOKUP) 83 PRINTF(" type = hash"); 84 PRINTF(" number = %s size = %lu", 85 iph.iph_name, (u_long)iph.iph_size); 86 if (iph.iph_seed != 0) 87 PRINTF(" seed = %lu", iph.iph_seed); 88 putchar('\n'); 89 } else { 90 PRINTF(" Type: "); 91 switch (iph.iph_type & ~IPHASH_ANON) 92 { 93 case IPHASH_LOOKUP : 94 PRINTF("lookup"); 95 break; 96 case IPHASH_GROUPMAP : 97 PRINTF("groupmap Group. %s", iph.iph_name); 98 break; 99 default : 100 break; 101 } 102 103 putchar('\n'); 104 PRINTF("\t\tSize: %lu\tSeed: %lu", 105 (u_long)iph.iph_size, iph.iph_seed); 106 PRINTF("\tRef. Count: %d\tMasks: %#x\n", iph.iph_ref, 107 iph.iph_masks); 108 } 109 110 if ((opts & OPT_DEBUG) != 0) { 111 struct in_addr m; 112 113 for (i = 0; i < 32; i++) { 114 if ((1 << i) & iph.iph_masks) { 115 ntomask(4, i, &m.s_addr); 116 PRINTF("\t\tMask: %s\n", inet_ntoa(m)); 117 } 118 } 119 } 120 121 if ((opts & OPT_DEBUG) == 0) 122 PRINTF("\t{"); 123 124 sz = iph.iph_size * sizeof(*table); 125 table = malloc(sz); 126 if ((*copyfunc)((char *)iph.iph_table, (char *)table, sz)) 127 return NULL; 128 129 for (i = 0, printed = 0; i < iph.iph_size; i++) { 130 for (ipep = table[i]; ipep != NULL; ) { 131 ipep = printhashnode(&iph, ipep, copyfunc, opts); 132 printed++; 133 } 134 } 135 if (printed == 0) 136 putchar(';'); 137 138 free(table); 139 140 if ((opts & OPT_DEBUG) == 0) 141 PRINTF(" };\n"); 142 143 return iph.iph_next; 144} 145