]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/lib/printhash.c
This commit was generated by cvs2svn to compensate for changes in r170263,
[FreeBSD/FreeBSD.git] / contrib / ipfilter / lib / printhash.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 2002 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  */
8
9 #include "ipf.h"
10
11 #define PRINTF  (void)printf
12 #define FPRINTF (void)fprintf
13
14
15 iphtable_t *printhash(hp, copyfunc, name, opts)
16 iphtable_t *hp;
17 copyfunc_t copyfunc;
18 char *name;
19 int opts;
20 {
21         iphtent_t *ipep, **table;
22         iphtable_t iph;
23         int i, printed;
24         size_t sz;
25
26         if ((*copyfunc)((char *)hp, (char *)&iph, sizeof(iph)))
27                 return NULL;
28
29         if ((name != NULL) && strncmp(name, iph.iph_name, FR_GROUPLEN))
30                 return iph.iph_next;
31
32         if ((opts & OPT_DEBUG) == 0) {
33                 if ((iph.iph_type & IPHASH_ANON) == IPHASH_ANON)
34                         PRINTF("# 'anonymous' table\n");
35                 switch (iph.iph_type & ~IPHASH_ANON)
36                 {
37                 case IPHASH_LOOKUP :
38                         PRINTF("table");
39                         break;
40                 case IPHASH_GROUPMAP :
41                         PRINTF("group-map");
42                         if (iph.iph_flags & FR_INQUE)
43                                 PRINTF(" in");
44                         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 }