2 * services/view.c - named views containing local zones authority service.
4 * Copyright (c) 2016, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains functions to enable named views that can hold local zone
43 #include "services/view.h"
44 #include "services/localzone.h"
45 #include "util/config_file.h"
48 view_cmp(const void* v1, const void* v2)
50 struct view* a = (struct view*)v1;
51 struct view* b = (struct view*)v2;
53 return strcmp(a->name, b->name);
59 struct views* v = (struct views*)calloc(1,
63 rbtree_init(&v->vtree, &view_cmp);
64 lock_rw_init(&v->lock);
65 lock_protect(&v->lock, &v->vtree, sizeof(v->vtree));
70 view_delete(struct view* v)
74 lock_rw_destroy(&v->lock);
75 local_zones_delete(v->local_zones);
81 delviewnode(rbnode_t* n, void* ATTR_UNUSED(arg))
83 struct view* v = (struct view*)n;
88 views_delete(struct views* v)
92 lock_rw_destroy(&v->lock);
93 traverse_postorder(&v->vtree, delviewnode, NULL);
97 /** create a new view */
99 view_create(char* name)
101 struct view* v = (struct view*)calloc(1, sizeof(*v));
105 if(!(v->name = strdup(name))) {
109 lock_rw_init(&v->lock);
110 lock_protect(&v->lock, &v->name, sizeof(*v)-sizeof(rbnode_t));
114 /** enter a new view returns with WRlock */
116 views_enter_view_name(struct views* vs, char* name)
118 struct view* v = view_create(name);
120 log_err("out of memory");
125 lock_rw_wrlock(&vs->lock);
126 lock_rw_wrlock(&v->lock);
127 if(!rbtree_insert(&vs->vtree, &v->node)) {
128 log_warn("duplicate view: %s", name);
129 lock_rw_unlock(&v->lock);
131 lock_rw_unlock(&vs->lock);
134 lock_rw_unlock(&vs->lock);
139 views_apply_cfg(struct views* vs, struct config_file* cfg)
141 struct config_view* cv;
143 struct config_file lz_cfg;
144 /* Check existence of name in first view (last in config). Rest of
145 * views are already checked when parsing config. */
146 if(cfg->views && !cfg->views->name) {
147 log_err("view without a name");
150 for(cv = cfg->views; cv; cv = cv->next) {
151 /* create and enter view */
152 if(!(v = views_enter_view_name(vs, cv->name)))
154 v->isfirst = cv->isfirst;
155 if(cv->local_zones || cv->local_data) {
156 if(!(v->local_zones = local_zones_create())){
157 lock_rw_unlock(&v->lock);
160 memset(&lz_cfg, 0, sizeof(lz_cfg));
161 lz_cfg.local_zones = cv->local_zones;
162 lz_cfg.local_data = cv->local_data;
163 lz_cfg.local_zones_nodefault =
164 cv->local_zones_nodefault;
165 if(!local_zones_apply_cfg(v->local_zones, &lz_cfg)){
166 lock_rw_unlock(&v->lock);
169 /* local_zones, local_zones_nodefault and local_data
170 * are free'd from config_view by local_zones_apply_cfg.
171 * Set pointers to NULL. */
172 cv->local_zones = NULL;
173 cv->local_data = NULL;
174 cv->local_zones_nodefault = NULL;
176 lock_rw_unlock(&v->lock);
181 /** find a view by name */
183 views_find_view(struct views* vs, const char* name, int write)
188 key.name = (char *)name;
189 lock_rw_rdlock(&vs->lock);
190 if(!(v = (struct view*)rbtree_search(&vs->vtree, &key.node))) {
191 lock_rw_unlock(&vs->lock);
195 lock_rw_wrlock(&v->lock);
197 lock_rw_rdlock(&v->lock);
199 lock_rw_unlock(&vs->lock);
203 void views_print(struct views* v)
205 /* TODO implement print */