File: pike_mi.c

package info (click to toggle)
kamailio 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 56,100 kB
  • sloc: ansic: 552,832; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (123 lines) | stat: -rw-r--r-- 2,816 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * $Id$
 *
 * Header file for PIKE MI functions
 *
 * Copyright (C) 2006 Voice Sistem SRL
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * Kamailio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * Kamailio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * History:
 * --------
 *  2006-12-05  created (bogdan)
 */

#include "ip_tree.h"
#include "pike_mi.h"

#define IPv6_LEN 16
#define IPv4_LEN 4
#define MAX_IP_LEN IPv6_LEN


static struct ip_node *ip_stack[MAX_IP_LEN];


static inline void print_ip_stack( int level, struct mi_node *node)
{
	if (level==IPv6_LEN) {
		/* IPv6 */
		addf_mi_node_child( node, 0, 0, 0,
			"%x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x",
			ip_stack[0]->byte,  ip_stack[1]->byte,
			ip_stack[2]->byte,  ip_stack[3]->byte,
			ip_stack[4]->byte,  ip_stack[5]->byte,
			ip_stack[6]->byte,  ip_stack[7]->byte,
			ip_stack[8]->byte,  ip_stack[9]->byte,
			ip_stack[10]->byte, ip_stack[11]->byte,
			ip_stack[12]->byte, ip_stack[13]->byte,
			ip_stack[14]->byte, ip_stack[15]->byte );
	} else if (level==IPv4_LEN) {
		/* IPv4 */
		addf_mi_node_child( node, 0, 0, 0, "%d.%d.%d.%d",
			ip_stack[0]->byte,
			ip_stack[1]->byte,
			ip_stack[2]->byte,
			ip_stack[3]->byte );
	} else {
		LM_CRIT("leaf node at depth %d!!!\n", level);
		return;
	}
}


static void print_red_ips( struct ip_node *ip, int level, struct mi_node *node)
{
	struct ip_node *foo;

	if (level==MAX_IP_LEN) {
		LM_CRIT("tree deeper than %d!!!\n", MAX_IP_LEN);
		return;
	}
	ip_stack[level] = ip;

	/* is the node marked red? */
	if ( ip->flags&NODE_ISRED_FLAG)
		print_ip_stack(level+1,node);

	/* go through all kids */
	foo = ip->kids;
	while(foo){
		print_red_ips( foo, level+1, node);
		foo = foo->next;
	}

}



/*
  Syntax of "pike_list" :
    no nodes
*/
struct mi_root* mi_pike_list(struct mi_root* cmd_tree, void* param)
{
	struct mi_root* rpl_tree;
	struct ip_node *ip;
	int i;

	rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
	if (rpl_tree==0)
		return 0;

	for( i=0 ; i<MAX_IP_BRANCHES ; i++ ) {

		if (get_tree_branch(i)==0)
			continue;

		lock_tree_branch(i);

		if ( (ip=get_tree_branch(i))!=NULL )
			print_red_ips( ip, 0, &rpl_tree->node );

		unlock_tree_branch(i);
	}

	return rpl_tree;
}