2 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <libubox/blobmsg_json.h>
15 #include <libubox/avl-cmp.h>
28 static const struct blobmsg_policy service_validate_attrs[__SERVICE_VAL_MAX] = {
29 [SERVICE_VAL_PACKAGE] = { "package", BLOBMSG_TYPE_STRING },
30 [SERVICE_VAL_TYPE] = { "type", BLOBMSG_TYPE_STRING },
31 [SERVICE_VAL_DATA] = { "data", BLOBMSG_TYPE_TABLE },
34 static struct avl_tree validators;
37 service_validate_dump_all(struct blob_buf *b, char *p, char *s)
39 struct json_object *r = json_object_new_object();
45 avl_for_each_element(&validators, v, avl) {
46 struct json_object *o, *t;
49 if (p && strcmp(p, v->package))
52 if (s && strcmp(s, v->type))
55 o = json_object_object_get(r, v->package);
57 o = json_object_new_object();
58 json_object_object_add(r, v->package, o);
60 t = json_object_object_get(o, v->type);
62 t = json_object_new_object();
63 json_object_object_add(o, v->type, t);
65 avl_for_each_element(&v->rules, vr, avl)
66 json_object_object_add(t, vr->option, json_object_new_string(vr->rule));
68 blobmsg_add_object(b, r);
73 service_validate_dump(struct blob_buf *b, struct service *s)
76 void *i = blobmsg_open_array(b, "validate");
78 list_for_each_entry(v, &s->validators, list) {
80 void *k, *j = blobmsg_open_table(b, "validate");
82 blobmsg_add_string(b, "package", v->package);
83 blobmsg_add_string(b, "type", v->type);
84 k = blobmsg_open_table(b, "rules");
85 avl_for_each_element(&v->rules, vr, avl)
86 blobmsg_add_string(b, vr->option, vr->rule);
87 blobmsg_close_table(b, k);
88 blobmsg_close_table(b, j);
90 blobmsg_close_array(b, i);
94 service_validate_del(struct service *s)
96 struct validate *v, *n;
98 if (list_empty(&s->validators))
101 list_for_each_entry_safe(v, n, &s->validators, list) {
102 struct vrule *vr, *a;
104 avl_remove_all_elements(&v->rules, vr, avl, a)
107 avl_delete(&validators, &v->avl);
114 service_validate_add(struct service *s, struct blob_attr *msg)
116 struct blob_attr *tb[__SERVICE_VAL_MAX];
118 char *type, *package;
119 struct blob_attr *cur;
122 blobmsg_parse(service_validate_attrs, __SERVICE_VAL_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
123 if (!tb[SERVICE_VAL_PACKAGE] || !tb[SERVICE_VAL_TYPE] || !tb[SERVICE_VAL_DATA])
126 v = calloc_a(sizeof(*v), &package, blobmsg_data_len(tb[SERVICE_VAL_PACKAGE]) + 1,
127 &type, blobmsg_data_len(tb[SERVICE_VAL_TYPE]) + 1);
132 v->avl.key = v->package = package;
133 strcpy(v->package, blobmsg_get_string(tb[SERVICE_VAL_PACKAGE]));
134 strcpy(v->type, blobmsg_get_string(tb[SERVICE_VAL_TYPE]));
136 list_add(&v->list, &s->validators);
137 if (avl_insert(&validators, &v->avl)) {
141 avl_init(&v->rules, avl_strcmp, false, NULL);
143 blobmsg_for_each_attr(cur, tb[SERVICE_VAL_DATA], rem) {
146 struct vrule *vr = calloc_a(sizeof(*vr), &option, strlen(blobmsg_name(cur)) + 1,
147 &rule, strlen(blobmsg_get_string(cur)) + 1);
149 vr->avl.key = vr->option = option;
151 strcpy(vr->option, blobmsg_name(cur));
152 strcpy(vr->rule, blobmsg_get_string(cur));
153 if (avl_insert(&v->rules, &vr->avl))
159 service_validate_init(void)
161 avl_init(&validators, avl_strcmp, true, NULL);