/[pdpsoft]/trunk/grid-mw-security/ees/src/aos/aos.c
ViewVC logotype

Contents of /trunk/grid-mw-security/ees/src/aos/aos.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 872 - (show annotations) (download) (as text)
Tue Oct 6 15:02:24 2009 UTC (12 years, 11 months ago) by aramv
File MIME type: text/x-chdr
File size: 8420 byte(s)
Renamed some methods
1 #include "plugin_manager.h"
2 #include "aos.h"
3 #include "_aos.h"
4
5 int AOS_is_initialized(){
6 return _is_initialized;
7 }
8
9 size_t AOS_buffer_size(arg_type argtype){
10 switch (argtype){
11 case TYPE_NONE :
12 return 0;
13 case TYPE_SCHAR :
14 return sizeof(int);
15 case TYPE_UCHAR :
16 return sizeof(unsigned int);
17 case TYPE_SHORT :
18 return sizeof(int);
19 case TYPE_USHORT :
20 return sizeof(unsigned int);
21 case TYPE_INT :
22 return sizeof(int);
23 case TYPE_UINT :
24 return sizeof(unsigned int);
25 case TYPE_LONGINT :
26 return sizeof(long int);
27 case TYPE_ULONGINT :
28 return sizeof(unsigned long int);
29 #ifdef HAVE_LONG_LONG_INT
30 case TYPE_LONGLONGINT :
31 return sizeof(long long int);
32 case TYPE_ULONGLONGINT :
33 return sizeof(unsigned long long int);
34 #endif
35 case TYPE_FLOAT :
36 return sizeof(double);
37 case TYPE_DOUBLE :
38 return sizeof(double);
39 #ifdef HAVE_LONG_DOUBLE
40 case TYPE_LONGDOUBLE :
41 return sizeof(long double);
42 #endif
43 case TYPE_CHAR :
44 return sizeof(int);
45 #ifdef HAVE_WINT_T
46 case TYPE_WIDE_CHAR :
47 return sizeof(wint_t);
48 #endif
49 case TYPE_STRING :
50 return sizeof(const char*);
51 #ifdef HAVE_WCHAR_T
52 case TYPE_WIDE_STRING :
53 return sizeof(const wchar_t*);
54 #endif
55 case TYPE_POINTER :
56 return sizeof(void*);
57 case TYPE_COUNT_SCHAR_POINTER :
58 return sizeof(signed char*);
59 case TYPE_COUNT_SHORT_POINTER :
60 return sizeof(short *);
61 case TYPE_COUNT_INT_POINTER :
62 return sizeof(int*);
63 case TYPE_COUNT_LONGINT_POINTER :
64 return sizeof(long int*);
65 #ifdef HAVE_LONG_LONG_INT
66 case TYPE_COUNT_LONGLONGINT_POINTER :
67 return sizeof(long long int*);
68 #endif
69 default:
70 return 0;
71 }
72 }
73
74 /* general high-level wrapper functions */
75
76 EES_RC AOS_set_int(const char* label, int value){
77 int* stack_value = &value;
78 return AOS_set(label, stack_value, TYPE_INT, 0, NULL, NULL, &list_assertions, size_assertions);
79 }
80
81 EES_RC AOS_set_string(const char* label, char* value){
82 return AOS_set(label, value, TYPE_STRING, strlen(value)+1, NULL, NULL, &list_assertions, size_assertions);
83 }
84
85 EES_RC AOS_set_pem(const char* label, char* value){
86 return AOS_set(label, value, TYPE_PEM, strlen(value)+1, NULL, NULL, &list_assertions, size_assertions);
87 }
88
89 /* base functions */
90
91 EES_RC AOS_set(const char *label, void *value, arg_type type, size_t arg_bufsize, aos_argument_t * parent, aos_argument_t * child, aos_argument_t **head_node, unsigned int *args_size){
92
93 aos_argument_t * args_new = NULL;
94 aos_argument_t * last_node = NULL;
95 size_t buf_size;
96 buf_size = arg_bufsize;
97
98 if(!AOS_is_initialized()){
99 eef_log(LOG_ERR, "AOS is not yet initialized. Aborting.");
100 return EES_FAILURE;
101 }
102
103 if(!(AOS_get_value_by_label(label, head_node, args_size) == NULL)){
104 eef_log(LOG_ERR, "Block with label %s already exists.", label);
105 return EES_FAILURE;
106 }
107
108 /* allocate an extra argument in the list */
109 if((args_new = calloc(1, (sizeof(aos_argument_t)))) == NULL){
110 eef_log(LOG_ERR, "Failed to realloc a new argument on the list. %s", strerror(errno));
111 return EES_FAILURE;
112 }
113
114 if(args_new){
115 eef_log(LOG_DEBUG, "Malloc succeeded. Argument lists is now %lu elements.\n", (unsigned long) (*args_size));
116 } else {
117 eef_log(LOG_ERR, "args_new not set: %s", strerror(errno));
118 return EES_FAILURE;
119 }
120
121 if(buf_size == 0) {
122 args_new->data = value;
123 args_new->needs_free = 0;
124 eef_log(LOG_DEBUG, "1 pointer allocated at %p. No need to free!\n", args_new->data);
125 } else {
126 args_new->data = calloc(1, buf_size);
127 eef_log(LOG_DEBUG, "Storing bytes: %i\n", buf_size);
128 memcpy(args_new->data, value, buf_size);
129 args_new->needs_free = 1;
130 eef_log(LOG_DEBUG, "%i bytes allocated at %p. (index %lu) Remember to free it or DIE!\n", buf_size, args_new->data, (unsigned long) (*args_size));
131 }
132
133 args_new->type = type;
134 args_new->label = label;
135 args_new->parent = parent;
136 args_new->child = child;
137 args_new->next = NULL;
138 args_new->setting_plugin = get_running_plugin();
139
140 last_node = AOS_get_last_node(head_node, args_size);
141 if(last_node != NULL){
142 (last_node)->next = args_new;
143 } else {
144 *head_node = args_new;
145 }
146
147 (*args_size)++;
148 return EES_SUCCESS;
149 }
150
151 aos_argument_t * AOS_get_last_node(aos_argument_t **head_node, unsigned int *args_size){
152 aos_argument_t * current_arg;
153 unsigned int i;
154 current_arg = *head_node;
155 for(i = 0; i < (*args_size); i++){
156 if(current_arg->next != NULL){
157 current_arg = current_arg->next;
158 }
159 }
160 return current_arg;
161 }
162
163 void* AOS_get_value_by_label(const char *label, aos_argument_t **head_node, unsigned int *args_size){
164 aos_argument_t* arg;
165 arg = AOS_get_argument_by_label(label, head_node, args_size);
166 if(arg == NULL){
167 return NULL;
168 } else {
169 return arg->data;
170 }
171 }
172
173 aos_argument_t * AOS_get_argument_by_label(const char *label, aos_argument_t **head_node, unsigned int *args_size){
174 unsigned int i;
175 aos_argument_t *current_arg;
176 current_arg = *head_node;
177 for(i = 0; i < (*args_size); i++){
178 if(strncmp(current_arg->label, label, strlen(label))==0){
179 return current_arg;
180 }
181 if(current_arg->next != NULL){
182 current_arg = current_arg->next;
183 }
184 }
185 return NULL;
186 }
187
188 /* frees every copied byte */
189 void AOS_free_argslist(aos_argument_t **head_node, unsigned int *args_size) {
190 unsigned int i;
191 aos_argument_t * current_arg;
192 aos_argument_t * next_arg;
193 current_arg = *head_node;
194 for(i = 0; i < (*args_size); i++){
195 eef_log(LOG_DEBUG, "Element %i points to %p\n", i, current_arg->data);
196 next_arg = current_arg->next;
197 if(current_arg->needs_free){
198 eef_log(LOG_DEBUG, "Freeing arg %i\n",i);
199 free(current_arg->data);
200 free(current_arg);
201 }
202 current_arg = next_arg;
203 }
204 (*args_size) = 0;
205 }
206
207 EES_RC AOS_delete_argument_by_label(const char* label, aos_argument_t **head_node, unsigned int *args_size){
208 aos_argument_t * current_arg;
209 aos_argument_t * previous_arg;
210 current_arg = NULL;
211 previous_arg = NULL;
212
213 if(label == NULL){
214 return EES_FAILURE;
215 }
216
217 for(current_arg = *head_node; current_arg != NULL; previous_arg = current_arg, current_arg = current_arg->next){
218 if(strncmp(current_arg->label, label, strlen(label)) == 0){
219 if(current_arg->setting_plugin != get_running_plugin()){
220 eef_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_arg->label, get_running_plugin()->pluginname);
221 return EES_FAILURE;
222 }
223 if(previous_arg == NULL){
224 /* head node deleted. linking start of list to next node */
225 *head_node = current_arg->next;
226 } else {
227 /* link previous to deletee's next */
228 previous_arg->next = current_arg->next;
229 }
230 break;
231 }
232 }
233
234 eef_log(LOG_ERR, "Deleting arg: %s", current_arg->label);
235 free(current_arg->data);
236 free(current_arg);
237 (*args_size)--;
238
239 return EES_SUCCESS;
240 }
241
242 /* initializer & terminator */
243
244 EES_RC AOS_Init (void){
245 size_assertions = calloc(1, sizeof(unsigned int));
246 *size_assertions = 0;
247 list_assertions = NULL;
248
249 size_obligations = calloc(1, sizeof(unsigned int));
250 *size_obligations = 0;
251 list_obligations = NULL;
252
253 _is_initialized = 1;
254
255 return EES_SUCCESS;
256 }
257
258
259 EES_RC AOS_Term (void){
260 AOS_free_argslist(&list_assertions, size_assertions);
261 AOS_free_argslist(&list_obligations, size_obligations);
262 free(size_assertions);
263 free(size_obligations);
264 _is_initialized = 0;
265
266 return EES_SUCCESS;
267 }
268
269
270 /* AOS high-level wrapper functions */
271
272 EES_RC setAssertion(const char* label, char* value){
273 return AOS_set(label, value, TYPE_STRING, strlen(value)+1, NULL, NULL, &list_assertions, size_assertions);
274 }
275
276 char* getAssertion(const char* label){
277 return AOS_get_value_by_label(label, &list_assertions, size_assertions);
278 }
279
280 EES_RC setObligation(const char* label, char* value){
281 if(AOS_set(label, value, TYPE_STRING, strlen(value)+1, NULL, NULL, &list_obligations, size_obligations)){
282 return EES_SUCCESS;
283 } else {
284 return EES_FAILURE;
285 }
286 }
287
288 char* getObligation(const char* label){
289 return AOS_get_value_by_label(label, &list_obligations, size_obligations);
290 }
291
292 EES_RC destroyObligation(const char* label){
293 return AOS_delete_argument_by_label(label, &list_obligations, size_obligations);
294 }
295

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28