1    | /***************************************
2    |   $Revision: 1.9 $
3    | 
4    |   Radix tree (rx).  rx_print.c - functions to print a forest/tree/node
5    |                                  (mainly for debugging purposes)
6    | 
7    |   Status: NOT REVUED, TESTED, INCOMPLETE
8    | 
9    |   Design and implementation by: Marek Bukowy
10   | 
11   |   ******************/ /******************
12   |   Copyright (c) 1999                              RIPE NCC
13   |  
14   |   All Rights Reserved
15   |   
16   |   Permission to use, copy, modify, and distribute this software and its
17   |   documentation for any purpose and without fee is hereby granted,
18   |   provided that the above copyright notice appear in all copies and that
19   |   both that copyright notice and this permission notice appear in
20   |   supporting documentation, and that the name of the author not be
21   |   used in advertising or publicity pertaining to distribution of the
22   |   software without specific, written prior permission.
23   |   
24   |   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25   |   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
26   |   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
27   |   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
28   |   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
29   |   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30   |   ***************************************/
31   | 
32   | #define RX_IMPL
33   | #include <rxroutines.h>
34   | #include "socket.h"
35   | 
36   | er_ret_t
37   | rx_walk_hook_printnode(rx_node_t *node, int level, int nodecounter, void *con)
38   | {
39   |   sk_conn_st *condat = (sk_conn_st *) con;
40   |   char line[200]="", buf[1024];
41   | 
42   |   int i;
43   | 
44   |   // indent
45   |   for(i=0;i<level;i++) strcat(line,"  ");
46   | 
47   |   sprintf( line+strlen(line) ,"** level %d ** ", level);
48   |   // @ %p; parent %p, child[0]=%p, child[1]=%p\n", 
49   |   //  node, node->parent_ptr, node->child_ptr[0], node->child_ptr[1] );
50   | 
51   |   rx_nod_print(node, buf, 1024);
52   |   
53   |   SK_cd_puts(condat, line);
54   |   SK_cd_puts(condat, buf);
55   |   SK_cd_puts(condat, "\n");
56   |   return RX_OK;
57   | }
58   | 
59   | /***************************************************************************/
60   | 
61   | er_ret_t
62   | rx_tree_print( sk_conn_st *condat, rx_tree_t *tree ) 
63   | {
64   | int cnt;
65   | er_ret_t err;
66   | char line[200]="";
67   | 
68   |  if( tree->top_ptr != NULL ) {
69   |    cnt = rx_walk_tree(tree->top_ptr, rx_walk_hook_printnode, 
70   | 		      RX_WALK_CNTGLU,  // print also glue nodes
71   | 		      255, 0, 0, condat, &err);
72   |    sprintf(line,"Traversed %d nodes\n", cnt);
73   |    SK_cd_puts(condat,line);
74   |  }
75   |  else {
76   |    SK_cd_puts(condat,"The tree is empty!\n");
77   |  }
78   | 
79   |  return err;
80   | }
81   | 
82   | 
83   | /***************************************************************************/
84   | static
85   | void
86   | rx_space_printone(void *voptr, void *condat)
87   | {
88   |   rx_tree_t *ptr = voptr;
89   |   char aout[1024];
90   |   char prstr[IP_PREFSTR_MAX];
91   |   
92   |   *aout=0;
93   | 
94   |   sprintf(aout+strlen(aout), "%50s:%d\n", "reg_id",   ptr->reg_id);
95   |   sprintf(aout+strlen(aout), "%50s:%d\n", "space",    ptr->space );
96   |   sprintf(aout+strlen(aout), "%50s:%d\n", "family",   ptr->family );
97   |   sprintf(aout+strlen(aout), "%50s:%d\n", "subtrees", ptr->subtrees);
98   |   sprintf(aout+strlen(aout), "%50s:%d\n", "mem_mode", ptr->mem_mode);
99   |   sprintf(aout+strlen(aout), "%50s:%d\n", "num_nodes",ptr->num_nodes);
100  |   sprintf(aout+strlen(aout), "%50s:%08x\n", "top_ptr", (int) ptr->top_ptr);
101  |   sprintf(aout+strlen(aout), "%50s:%d\n", "maxbits",  ptr->maxbits);
102  | 
103  |   if( IP_pref_b2a(  &(ptr->prefix), prstr, IP_PREFSTR_MAX) != IP_OK )
104  |     die; // program error.
105  | 
106  |   sprintf(aout+strlen(aout), "%50s:%s\n", "prefix", prstr);
107  |   SK_cd_puts( (sk_conn_st *)condat,aout);
108  | }
109  | 
110  | 
111  | /***************************************************************************/
112  | /*+ print the whole forest +*/
113  | 
114  | void
115  | rx_space_list(sk_conn_st *condat)
116  | {
117  |   g_list_foreach( rx_forest, rx_space_printone, condat);
118  | }
119  | /***************************************************************************/
120  | 
121  | void
122  | rx_nod_print( rx_node_t *node, char *buf, int maxchar )
123  | {
124  |   char pref[IP_PREFSTR_MAX];
125  |   
126  |   if( IP_pref_b2a(  &(node->prefix), pref, IP_PREFSTR_MAX) != IP_OK ) {
127  |     die;
128  |   }
129  |   
130  |   snprintf(buf, maxchar, "%s%s", 
131  | 	   ( node->glue ) ? "++glue++" : "", pref);
132  | }
133  | /***************************************************************************/
134  | 
135  | void 
136  | rx_stk_print( rx_nodcpy_t   stack[],         // stack==array of node_copies
137  | 	      int           stackdepth )
138  | {
139  |   int i;
140  |   rx_node_t *node;
141  |   char buf[1024];
142  | 
143  |   ER_dbg_va(FAC_RX, ASP_RX_STKBLD_DET, 
144  | 	    "stack dump: %d elements", stackdepth);
145  | 
146  |   for(i = 0; i < stackdepth; i++) {
147  |     node = & stack[i].cpy;
148  | 
149  |     rx_nod_print(node, buf, 1024);
150  | 
151  |     ER_dbg_va(FAC_RX, ASP_RX_STKBLD_DET, "position %d: %s", i, buf);
152  |   }
153  | }