/src/opensips/evi/evi_params.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2011 OpenSIPS Solutions |
3 | | * |
4 | | * This file is part of opensips, a free SIP server. |
5 | | * |
6 | | * opensips is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation; either version 2 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * opensips is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19 | | * |
20 | | * |
21 | | * history: |
22 | | * --------- |
23 | | * 2011-05-xx created (razvancrainea) |
24 | | */ |
25 | | |
26 | | #include "evi_params.h" |
27 | | #include "../mem/mem.h" |
28 | | #include "../mem/shm_mem.h" |
29 | | #include <string.h> |
30 | | |
31 | | |
32 | | /* creates an element and links it to the parameters list |
33 | | * but without populating the parameter value */ |
34 | | evi_param_p evi_param_create(evi_params_p list, const str *name) |
35 | 0 | { |
36 | 0 | evi_param_p new_p; |
37 | |
|
38 | 0 | if (!list) { |
39 | 0 | LM_ERR("invalid param list\n"); |
40 | 0 | return 0; |
41 | 0 | } |
42 | | |
43 | 0 | new_p = pkg_malloc(sizeof(evi_param_t)); |
44 | 0 | if (!new_p) { |
45 | 0 | LM_ERR("no more pkg mem for new parameter\n"); |
46 | 0 | return 0; |
47 | 0 | } |
48 | 0 | memset(new_p, 0, sizeof(evi_param_t)); |
49 | |
|
50 | 0 | if (name) |
51 | 0 | new_p->name = *name; |
52 | |
|
53 | 0 | new_p->next = NULL; |
54 | 0 | if (list->last) { |
55 | 0 | list->last->next = new_p; |
56 | 0 | list->last = new_p; |
57 | 0 | } else { |
58 | 0 | list->last = list->first = new_p; |
59 | 0 | } |
60 | 0 | return new_p; |
61 | 0 | } |
62 | | |
63 | | int evi_param_set(evi_param_p el, const void *param, int flags) |
64 | 0 | { |
65 | 0 | if (!el) { |
66 | 0 | LM_ERR("no parameter specified\n"); |
67 | 0 | return 1; |
68 | 0 | } |
69 | | |
70 | 0 | if (!(flags & (EVI_INT_VAL|EVI_STR_VAL))) { |
71 | 0 | LM_ERR("params should be int or str [%x]\n", flags); |
72 | 0 | return -1; |
73 | 0 | } |
74 | | |
75 | 0 | el->flags = flags; |
76 | |
|
77 | 0 | if (flags & EVI_INT_VAL) { |
78 | 0 | el->val.n = *((int*)param); |
79 | 0 | LM_DBG("set int %.*s=%d\n", el->name.len, el->name.s, |
80 | 0 | el->val.n); |
81 | 0 | } else { |
82 | 0 | memcpy(&el->val, param, sizeof(str)); |
83 | 0 | LM_DBG("set str %.*s='%.*s'\n", el->name.len, el->name.s, |
84 | 0 | el->val.s.len, el->val.s.s); |
85 | 0 | } |
86 | |
|
87 | 0 | return 0; |
88 | 0 | } |
89 | | |
90 | | |
91 | | |
92 | | /* adds a new parameter to the list */ |
93 | | int evi_param_add(evi_params_p list, const str *name, const void *param, int flags) |
94 | 0 | { |
95 | 0 | evi_param_p new_p; |
96 | |
|
97 | 0 | if (!(EVI_INT_VAL & flags) && !(EVI_STR_VAL & flags)) { |
98 | 0 | LM_ERR("params should be int or str [%x]\n", flags); |
99 | 0 | return -1; |
100 | 0 | } |
101 | 0 | new_p = evi_param_create(list, name); |
102 | 0 | if (!new_p) { |
103 | 0 | LM_ERR("cannot create parameter\n"); |
104 | 0 | return -1; |
105 | 0 | } |
106 | 0 | if (evi_param_set(new_p, param, flags) < 0) { |
107 | 0 | LM_ERR("cannot set the parameter value\n"); |
108 | 0 | return -1; |
109 | 0 | } |
110 | 0 | return 0; |
111 | 0 | } |
112 | | |
113 | | /* allocs a new structure and initializes it with 0 */ |
114 | | evi_params_p evi_get_params(void) |
115 | 0 | { |
116 | 0 | evi_params_p new_list = pkg_malloc(sizeof(evi_params_t)); |
117 | 0 | if (!new_list) { |
118 | 0 | LM_ERR("no more pkg memory for the list\n"); |
119 | 0 | return NULL; |
120 | 0 | } |
121 | 0 | memset(new_list, 0, sizeof(evi_params_t)); |
122 | | |
123 | | /* used to remember to free it */ |
124 | 0 | new_list->flags = EVI_FREE_LIST; |
125 | |
|
126 | 0 | return new_list; |
127 | 0 | } |
128 | | |
129 | | /* frees a parameters list */ |
130 | | void evi_free_params(evi_params_p list) |
131 | 0 | { |
132 | 0 | evi_param_p node, nxt; |
133 | |
|
134 | 0 | if (!list) |
135 | 0 | return; |
136 | | |
137 | 0 | for (node = list->first; node; node = nxt) { |
138 | 0 | nxt = node->next; |
139 | 0 | pkg_free(node); |
140 | 0 | } |
141 | |
|
142 | 0 | list->first = list->last = NULL; |
143 | | |
144 | | /* list should be freed */ |
145 | 0 | pkg_free(list); |
146 | 0 | } |
147 | | |
148 | | evi_params_p evi_dup_shm_params(evi_params_p pkg_params) |
149 | 0 | { |
150 | 0 | int parambufs_size, strbufs_size; |
151 | 0 | evi_params_p shm_params; |
152 | 0 | evi_param_p param, prev, sp; |
153 | 0 | char *p; |
154 | |
|
155 | 0 | if(!pkg_params) |
156 | 0 | return NULL; |
157 | | |
158 | 0 | parambufs_size = sizeof(evi_params_t); |
159 | 0 | strbufs_size = 0; |
160 | 0 | for (param = pkg_params->first; param; param = param->next) { |
161 | 0 | parambufs_size += sizeof(evi_param_t); |
162 | 0 | strbufs_size += param->name.len; |
163 | 0 | if (param->flags & EVI_STR_VAL) |
164 | 0 | strbufs_size += param->val.s.len; |
165 | 0 | } |
166 | |
|
167 | 0 | shm_params = shm_malloc(parambufs_size + strbufs_size); |
168 | 0 | if (!shm_params) { |
169 | 0 | return NULL; |
170 | 0 | } |
171 | 0 | shm_params->flags = 0; |
172 | |
|
173 | 0 | sp = (evi_param_p)(shm_params + 1); |
174 | 0 | p = (char *)(shm_params) + parambufs_size; |
175 | 0 | for (param = pkg_params->first, prev = NULL; param; |
176 | 0 | prev = sp++, param = param->next) { |
177 | 0 | sp->flags = param->flags; |
178 | 0 | sp->next = NULL; |
179 | 0 | sp->name.len = param->name.len; |
180 | 0 | if (sp->name.len) { |
181 | 0 | sp->name.s = p; |
182 | 0 | p += param->name.len; |
183 | 0 | memcpy(sp->name.s, param->name.s, param->name.len); |
184 | 0 | } |
185 | 0 | if (param->flags & EVI_STR_VAL) { |
186 | 0 | sp->val.s.len = param->val.s.len; |
187 | 0 | sp->val.s.s = p; |
188 | 0 | p += param->val.s.len; |
189 | 0 | memcpy(sp->val.s.s, param->val.s.s, param->val.s.len); |
190 | 0 | } else |
191 | 0 | sp->val.n = param->val.n; |
192 | 0 | if (prev) { |
193 | 0 | prev->next = sp; |
194 | 0 | shm_params->last = sp; |
195 | 0 | } else |
196 | 0 | shm_params->first = sp; |
197 | 0 | } |
198 | 0 | return shm_params; |
199 | 0 | } |
200 | | |
201 | | void evi_free_shm_params(evi_params_p shm_params) |
202 | 0 | { |
203 | 0 | shm_free(shm_params); |
204 | 0 | } |