1 1.1 christos /* 2 1.1 christos * services/view.h - named views containing local zones authority service. 3 1.1 christos * 4 1.1 christos * Copyright (c) 2016, NLnet Labs. All rights reserved. 5 1.1 christos * 6 1.1 christos * This software is open source. 7 1.1 christos * 8 1.1 christos * Redistribution and use in source and binary forms, with or without 9 1.1 christos * modification, are permitted provided that the following conditions 10 1.1 christos * are met: 11 1.1 christos * 12 1.1 christos * Redistributions of source code must retain the above copyright notice, 13 1.1 christos * this list of conditions and the following disclaimer. 14 1.1 christos * 15 1.1 christos * Redistributions in binary form must reproduce the above copyright notice, 16 1.1 christos * this list of conditions and the following disclaimer in the documentation 17 1.1 christos * and/or other materials provided with the distribution. 18 1.1 christos * 19 1.1 christos * Neither the name of the NLNET LABS nor the names of its contributors may 20 1.1 christos * be used to endorse or promote products derived from this software without 21 1.1 christos * specific prior written permission. 22 1.1 christos * 23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 1.1 christos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 1.1 christos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 1.1 christos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 1.1 christos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 1.1 christos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 1.1 christos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 1.1 christos */ 35 1.1 christos 36 1.1 christos /** 37 1.1 christos * \file 38 1.1 christos * 39 1.1 christos * This file contains functions to enable named views that can hold local zone 40 1.1 christos * authority service. 41 1.1 christos */ 42 1.1 christos 43 1.1 christos #ifndef SERVICES_VIEW_H 44 1.1 christos #define SERVICES_VIEW_H 45 1.1 christos #include "util/rbtree.h" 46 1.1 christos #include "util/locks.h" 47 1.1 christos struct regional; 48 1.1 christos struct config_file; 49 1.1 christos struct config_view; 50 1.1 christos struct respip_set; 51 1.1 christos 52 1.1 christos 53 1.1 christos /** 54 1.1 christos * Views storage, shared. 55 1.1 christos */ 56 1.1 christos struct views { 57 1.1.1.2 christos /** lock on the view tree. When locking order, the views lock 58 1.1.1.2 christos * is before the forwards,hints,anchors,localzones lock. */ 59 1.1 christos lock_rw_type lock; 60 1.1 christos /** rbtree of struct view */ 61 1.1 christos rbtree_type vtree; 62 1.1 christos }; 63 1.1 christos 64 1.1 christos /** 65 1.1 christos * View. Named structure holding local authority zones. 66 1.1 christos */ 67 1.1 christos struct view { 68 1.1 christos /** rbtree node, key is name */ 69 1.1 christos rbnode_type node; 70 1.1 christos /** view name. 71 1.1 christos * Has to be right after rbnode_t due to pointer arithmetic in 72 1.1 christos * view_create's lock protect */ 73 1.1 christos char* name; 74 1.1 christos /** view specific local authority zones */ 75 1.1 christos struct local_zones* local_zones; 76 1.1 christos /** response-ip configuration data for this view */ 77 1.1 christos struct respip_set* respip_set; 78 1.1 christos /** Fallback to global local_zones when there is no match in the view 79 1.1 christos * specific tree. 1 for yes, 0 for no */ 80 1.1 christos int isfirst; 81 1.1 christos /** lock on the data in the structure 82 1.1 christos * For the node and name you need to also hold the views_tree lock to 83 1.1 christos * change them. */ 84 1.1 christos lock_rw_type lock; 85 1.1 christos }; 86 1.1 christos 87 1.1 christos 88 1.1 christos /** 89 1.1 christos * Create views storage 90 1.1 christos * @return new struct or NULL on error. 91 1.1 christos */ 92 1.1 christos struct views* views_create(void); 93 1.1 christos 94 1.1 christos /** 95 1.1 christos * Delete views storage 96 1.1 christos * @param v: views to delete. 97 1.1 christos */ 98 1.1 christos void views_delete(struct views* v); 99 1.1 christos 100 1.1 christos /** 101 1.1 christos * Apply config settings; 102 1.1 christos * Takes care of locking. 103 1.1 christos * @param v: view is set up. 104 1.1 christos * @param cfg: config data. 105 1.1 christos * @return false on error. 106 1.1 christos */ 107 1.1 christos int views_apply_cfg(struct views* v, struct config_file* cfg); 108 1.1 christos 109 1.1 christos /** 110 1.1 christos * Compare two view entries in rbtree. Sort canonical. 111 1.1 christos * @param v1: view 1 112 1.1 christos * @param v2: view 2 113 1.1 christos * @return: negative, positive or 0 comparison value. 114 1.1 christos */ 115 1.1 christos int view_cmp(const void* v1, const void* v2); 116 1.1 christos 117 1.1 christos /** 118 1.1 christos * Delete one view 119 1.1 christos * @param v: view to delete. 120 1.1 christos */ 121 1.1 christos void view_delete(struct view* v); 122 1.1 christos 123 1.1 christos /** 124 1.1 christos * Debug helper. Print all views 125 1.1 christos * Takes care of locking. 126 1.1 christos * @param v: the views tree 127 1.1 christos */ 128 1.1 christos void views_print(struct views* v); 129 1.1 christos 130 1.1.1.2 christos /** 131 1.1.1.2 christos * Find a view by name. 132 1.1 christos * @param vs: views 133 1.1 christos * @param name: name of the view we are looking for 134 1.1 christos * @param write: 1 for obtaining write lock on found view, 0 for read lock 135 1.1 christos * @return: locked view or NULL. 136 1.1 christos */ 137 1.1 christos struct view* views_find_view(struct views* vs, const char* name, int write); 138 1.1 christos 139 1.1.1.2 christos /** 140 1.1.1.2 christos * Calculate memory usage of views. 141 1.1.1.2 christos * @param vs: the views tree. The routine locks and unlocks the structure 142 1.1.1.2 christos * for reading. 143 1.1.1.2 christos * @return memory in bytes. 144 1.1.1.2 christos */ 145 1.1.1.2 christos size_t views_get_mem(struct views* vs); 146 1.1.1.2 christos 147 1.1.1.2 christos /** 148 1.1.1.2 christos * Calculate memory usage of view. 149 1.1.1.2 christos * @param v: the view. The routine locks and unlocks the structure for reading. 150 1.1.1.2 christos * @return memory in bytes. 151 1.1.1.2 christos */ 152 1.1.1.2 christos size_t view_get_mem(struct view* v); 153 1.1.1.2 christos 154 1.1.1.2 christos /** 155 1.1.1.2 christos * Swap internal tree with preallocated entries. Caller should manage 156 1.1.1.2 christos * the locks. 157 1.1.1.2 christos * @param vs: views tree 158 1.1.1.2 christos * @param data: preallocated information. 159 1.1.1.2 christos */ 160 1.1.1.2 christos void views_swap_tree(struct views* vs, struct views* data); 161 1.1.1.2 christos 162 1.1 christos #endif /* SERVICES_VIEW_H */ 163