forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.c
More file actions
286 lines (232 loc) · 5.61 KB
/
Copy pathtree.c
File metadata and controls
286 lines (232 loc) · 5.61 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* $Id$
*
* Copyright (C) 2006 Voice Sistem SRL
*
* This file is part of opensips, a free SIP server.
*
* opensips 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.
*
* opensips 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* History:
* ---------
* 2006-09-08 first version (bogdan)
*/
/*!
* \file
* \brief MI :: Tree
* \ingroup mi
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "../mem/mem.h"
#include "../mem/shm_mem.h"
#include "../dprint.h"
#include "tree.h"
#include "fmt.h"
static int use_shm = 0;
struct mi_root *init_mi_tree(unsigned int code, char *reason, int reason_len)
{
struct mi_root *root;
if (use_shm)
root = (struct mi_root *)shm_malloc(sizeof(struct mi_root));
else
root = (struct mi_root *)pkg_malloc(sizeof(struct mi_root));
if (!root) {
LM_ERR("no more pkg mem\n");
return NULL;
}
memset(root,0,sizeof(struct mi_root));
root->node.next = root->node.last = &root->node;
if (reason && reason_len) {
root->reason.s = reason;
root->reason.len = reason_len;
}
root->code = code;
return root;
}
static void free_mi_node(struct mi_node *parent)
{
struct mi_node *p, *q;
for(p = parent->kids ; p ; ){
q = p;
p = p->next;
free_mi_node(q);
}
if (use_shm) {
shm_free(parent);
} else {
del_mi_attr_list(parent);
pkg_free(parent);
}
}
void free_mi_tree(struct mi_root *parent)
{
struct mi_node *p, *q;
for(p = parent->node.kids ; p ; ){
q = p;
p = p->next;
free_mi_node(q);
}
if (use_shm)
shm_free(parent);
else
pkg_free(parent);
}
static inline struct mi_node *create_mi_node(char *name, int name_len,
char *value, int value_len, int flags)
{
struct mi_node *new;
int size_mem;
int name_pos;
int value_pos;
if (!name) name_len=0;
if (!name_len) name=0;
if (!value) value_len=0;
if (!value_len) value=0;
if (!name && !value)
return NULL;
size_mem = sizeof(struct mi_node);
value_pos = name_pos = 0;
if (name && (flags & MI_DUP_NAME)){
name_pos = size_mem;
size_mem += name_len;
}
if (value && (flags & MI_DUP_VALUE)){
value_pos = size_mem;
size_mem += value_len;
}
if (use_shm)
new = (struct mi_node *)shm_malloc(size_mem);
else
new = (struct mi_node *)pkg_malloc(size_mem);
if(!new) {
LM_ERR("no more pkg mem\n");
return NULL;
}
memset(new,0,size_mem);
if (name) {
new->name.len = name_len;
if(flags & MI_DUP_NAME){
new->name.s = ((char *)new) + name_pos;
strncpy(new->name.s, name, name_len);
} else{
new->name.s = name;
}
}
if (value) {
new->value.len = value_len;
if(flags & MI_DUP_VALUE){
new->value.s = ((char *)new) + value_pos;
strncpy(new->value.s, value, value_len);
}else{
new->value.s = value;
}
}
new->last = new;
return new;
}
static inline struct mi_node *add_next(struct mi_node *brother,
char *name, int name_len, char *value, int value_len, int flags)
{
struct mi_node *new;
if(!brother)
return NULL;
new = create_mi_node(name, name_len, value, value_len, flags);
if(!new)
return NULL;
brother->last->next = new;
brother->last = new;
return new;
}
struct mi_node *add_mi_node_sibling( struct mi_node *brother, int flags,
char *name, int name_len, char *value, int value_len)
{
return add_next(brother, name, name_len, value, value_len, flags);
}
struct mi_node *addf_mi_node_sibling(struct mi_node *brother, int flags,
char *name, int name_len, char *fmt_val, ...)
{
va_list ap;
char *p;
int len;
va_start(ap, fmt_val);
p = mi_print_fmt( fmt_val, ap, &len);
va_end(ap);
if (p==NULL)
return 0;
return add_mi_node_sibling( brother, flags|MI_DUP_VALUE,
name, name_len, p, len);
}
struct mi_node *add_mi_node_child( struct mi_node *parent, int flags,
char *name, int name_len, char *value, int value_len)
{
if(parent->kids){
return add_next(parent->kids, name, name_len, value, value_len, flags);
}else{
parent->kids = create_mi_node(name, name_len, value, value_len, flags);
return parent->kids;
}
}
struct mi_node *addf_mi_node_child(struct mi_node *parent, int flags,
char *name, int name_len, char *fmt_val, ...)
{
va_list ap;
char *p;
int len;
va_start(ap, fmt_val);
p = mi_print_fmt( fmt_val, ap, &len);
va_end(ap);
if (p==NULL)
return 0;
return add_mi_node_child( parent, flags|MI_DUP_VALUE,
name, name_len, p, len);
}
static int clone_mi_node(struct mi_node *org, struct mi_node *parent)
{
struct mi_node *p, *q;
for(p = org->kids ; p ; p=p->next){
q = add_mi_node_child( parent, MI_DUP_VALUE|MI_DUP_NAME,
p->name.s, p->name.len, p->value.s, p->value.len);
if (q==NULL)
return -1;
if (clone_mi_node( p, q)!=0)
return -1;
}
return 0;
}
struct mi_root* clone_mi_tree(struct mi_root *org, int shm)
{
struct mi_root *root;
use_shm = shm?1:0;
root = init_mi_tree( org->code, org->reason.s, org->reason.len);
if (root==NULL)
goto done;
if (clone_mi_node( &(org->node), &(root->node) )!=0 ) {
free_mi_tree(root);
root = NULL;
goto done;
}
done:
use_shm=0;
return root;
}
void free_shm_mi_tree(struct mi_root *parent)
{
use_shm = 1;
free_mi_tree(parent);
use_shm = 0;
}