1 | /***************************************
2 | $Revision: 1.16 $
3 |
4 | Definitions module (df)
5 |
6 | Status: NOT REVUED, NOT TESTED
7 |
8 | ******************/ /******************
9 | Filename : defs.c
10 | Authors : ottrey@ripe.net
11 | marek@ripe.net
12 | ******************/ /******************
13 | Copyright (c) 1999 RIPE NCC
14 |
15 | All Rights Reserved
16 |
17 | Permission to use, copy, modify, and distribute this software and its
18 | documentation for any purpose and without fee is hereby granted,
19 | provided that the above copyright notice appear in all copies and that
20 | both that copyright notice and this permission notice appear in
21 | supporting documentation, and that the name of the author not be
22 | used in advertising or publicity pertaining to distribution of the
23 | software without specific, written prior permission.
24 |
25 | THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
26 | ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
27 | AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
28 | DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
29 | AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
30 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 | ***************************************/
32 | #include <stdio.h>
33 | #include <stdlib.h>
34 | #include <stdarg.h>
35 | #include <strings.h>
36 | #include <glib.h>
37 |
38 | /*+ String sizes +*/
39 | #define STR_S 63
40 | #define STR_M 255
41 | #define STR_L 1023
42 | #define STR_XL 4095
43 | #define STR_XXL 16383
44 |
45 | #define DEFS_IMPL
46 | #include "defs.h"
47 | #include "memwrap.h"
48 |
49 | #include "DF_class_names.def"
50 | #include "DF_class_codes.def"
51 | #include "DF_class_aliases.def"
52 | #include "DF_class_aliases_map.def"
53 | #include "DF_class_dbase_code_map.def"
54 | #include "DF_class_templates.def"
55 | #include "DF_class_templates_v.def"
56 |
57 | #include "DF_attribute_names.def"
58 | #include "DF_attribute_codes.def"
59 | #include "DF_attribute_aliases.def"
60 | #include "DF_attribute_aliases_map.def"
61 |
62 | #include "UD_queries.def"
63 |
64 |
65 |
66 | /* getsubopt requires a vector of pointers to a list of possible options
67 | It's used for parsing the source list.
68 | Therefore a quick
69 | XXX !!!!
70 | hack: hardcode it. Will be initialised from the Sources array
71 | once the config module is defined
72 | */
73 |
74 | char * const Server_queries[] = {
75 | "sources",
76 | "version",
77 | NULL
78 | }; /* Server_queries */
79 |
80 | /* XXX this also should be generated from XML... */
81 | char * const Filter_names[] = {
82 | "aut-num",
83 | "domain",
84 | "inet6num",
85 | "inetnum",
86 | "inet-rtr",
87 | "key-cert",
88 | "limerick",
89 | "mntner",
90 | "route",
91 | "origin",
92 | "as-set",
93 | "route-set",
94 | "members",
95 | "peering-set",
96 | "filter-set",
97 | "rtr-set",
98 | NULL
99 | }; /* Filter_names */
100 |
101 | char * const *DF_get_filter_names(void) {
102 | return Filter_names;
103 | } /* DF_get_filter_names() */
104 |
105 | char * const *DF_get_class_names(void) {
106 | return Class_names;
107 | } /* DF_get_class_names() */
108 |
109 | char * const *DF_get_class_aliases(void) {
110 | return Class_aliases;
111 | } /* DF_get_class_aliases() */
112 |
113 | int DF_get_class_index(int alias_index) {
114 | return Class_aliases_map[alias_index];
115 | } /* DF_get_class_index() */
116 |
117 | char * const DF_get_class_name(int alias_index) {
118 | return Class_names[Class_aliases_map[alias_index]];
119 | } /* DF_get_class_name() */
120 |
121 | char * const DF_get_class_code(C_Type_t index) {
122 | if( index == -1 ) {
123 | return "*";
124 | }
125 | else {
126 | return Class_codes[index];
127 | }
128 | } /* DF_get_class_code() */
129 |
130 | int DF_get_class_dbase_code(int class_index) {
131 | return Class_dbase_code_map[class_index];
132 | } /* DF_get_class_dbase_code() */
133 |
134 | /* Main tables names for object types */
135 | char * const Type2main[] = {
136 | "as_block",
137 | "as_set",
138 | "aut_num",
139 | "domain",
140 | "inet_rtr",
141 | "inet6num",
142 | "inetnum",
143 | "key_cert",
144 | "limerick",
145 | "mntner",
146 | "person_role", /*pn*/
147 | "person_role", /*ro*/
148 | "route",
149 | "route_set",
150 | "filter_set",
151 | "peering_set",
152 | "rtr_set",
153 | NULL
154 | };
155 |
156 | char * const DF_get_class_sql_table(C_Type_t index) {
157 | return Type2main[index];
158 | } /* DF_get_class_sql_table() */
159 |
160 |
161 |
162 | char * const *DF_get_attribute_aliases(void) {
163 | return Attribute_aliases;
164 | } /* DF_get_attribute_aliases() */
165 |
166 | const char *DF_get_attribute_name(A_Type_t index) {
167 | return Attribute_names[index];
168 | } /* DF_get_attribute_name() */
169 |
170 | const char *DF_get_attribute_code(A_Type_t index) {
171 | return Attribute_codes[index];
172 | } /* DF_get_attribute_code() */
173 |
174 | char * const *DF_get_attribute_names(void) {
175 | return Attribute_names;
176 | } /* DF_get_attribute_names() */
177 |
178 | int DF_get_attribute_index(int alias_index) {
179 | return Attribute_aliases_map[alias_index];
180 | } /* DF_get_attribute_index() */
181 |
182 | const char *DF_get_class_template(C_Type_t index) {
183 | return Templates[index];
184 | } /* DF_get_class_template() */
185 |
186 | const char *DF_get_class_template_v(C_Type_t index) {
187 | return Templates_v[index];
188 | } /* DF_get_class_template_v() */
189 |
190 | char * const *DF_get_server_queries(void) {
191 | return Server_queries;
192 | } /* DF_get_server_queries() */
193 |
194 | const char *DF_get_update_query(A_Type_t index){
195 | return Update[index].qry;
196 | } /* DF_get_update_query() */
197 |
198 | UD_qtype DF_get_update_query_type(A_Type_t index){
199 | return Update[index].qtype;
200 | } /* DF_get_update_query_type() */
201 |
202 | const char *DF_get_insert_query(A_Type_t index){
203 | return Insert[index].qry;
204 | } /* DF_get_insert_query() */
205 |
206 | UD_qtype DF_get_insert_query_type(A_Type_t index){
207 | return Insert[index].qtype;
208 | } /* DF_get_insert_query_type() */
209 |
210 | const char *DF_get_select_query(A_Type_t index){
211 | return Select[index].qry;
212 | } /* DF_get_select_query() */
213 |
214 | UD_qtype DF_get_select_query_type(A_Type_t index){
215 | return Select[index].qtype;
216 | } /* DF_get_select_query_type() */
217 |
218 | const char *DF_get_dummy_query(A_Type_t index){
219 | return Dummy[index].qry;
220 | } /* DF_get_dummy_query() */
221 |
222 | UD_qtype DF_get_dummy_query_type(A_Type_t index){
223 | return Dummy[index].qtype;
224 | } /* DF_get_dummy_query_type() */
225 |
226 |
227 |
228 | const char *DF_get_attribute_desc(A_Type_t index) {
229 | /*
230 | return (char *)Attributes_details[attr_index][0];
231 | */
232 | return NULL;
233 | } /* DF_get_attribute_desc() */
234 |
235 | const char *DF_get_attribute_frmt(A_Type_t index) {
236 | /*
237 | return (char *)Attributes_details[attr_index][1];
238 | */
239 | return NULL;
240 | } /* DF_get_attribute_frmt() */
241 |
242 | /* DF_attributes_to_string() */
243 | /*++++++++++++++++++++++++++++++++++++++
244 | Returns a string of all the attributes. Only there for debugging and tracing purposes.
245 |
246 | int offset The offset (Ie short or long name).
247 |
248 | More:
249 | +html+ <PRE>
250 | Authors:
251 | ottrey
252 |
253 | +html+ </PRE><DL COMPACT>
254 | +html+ <DT>Online References:
255 | +html+ <DD><UL>
256 | +html+ </UL></DL>
257 |
258 | ++++++++++++++++++++++++++++++++++++++*/
259 | char *DF_attributes_to_string(void) {
260 | int i;
261 | char *str;
262 | char str_buffer[4096];
263 | int str_len;
264 |
265 | strcpy(str_buffer, "{\"");
266 | for (i=0; Attribute_names[i] != NULL; i++) {
267 | strcat(str_buffer, Attribute_names[i]);
268 | strcat(str_buffer, "\", \"");
269 | }
270 | str_len = strlen(str_buffer);
271 | str_buffer[str_len-3] = '}';
272 | str_buffer[str_len-2] = '\0';
273 | str_len--;
274 |
275 | /* str = (char *)calloc(1, str_len); */
276 | dieif( wr_malloc((void **)&str, str_len ) != UT_OK);
277 | strcpy(str, str_buffer);
278 |
279 | return str;
280 |
281 | } /* DF_attributes_to_string() */
282 |
283 | /* XXX This could be done MUCH more efficiently (with a hash) */
284 | A_Type_t DF_attribute_code2type(const gchar *token) {
285 | A_Type_t result=-1;
286 |
287 | int i;
288 | for (i=0; Attribute_aliases[i] != NULL; i++) {
289 | if (strcmp(Attribute_aliases[i], token) == 0) {
290 | result = Attribute_aliases_map[i];
291 | break;
292 | }
293 | }
294 |
295 | return result;
296 | } /* DF_attribute_code2type() */
297 |
298 | /* XXX This could be done MUCH more efficiently (with a hash) */
299 | A_Type_t DF_attribute_name2type(const gchar *token) {
300 | A_Type_t result=-1;
301 |
302 | int i;
303 | for (i=0; Attribute_aliases[i] != NULL; i++) {
304 | if (strcmp(Attribute_aliases[i], token) == 0) {
305 | result = Attribute_aliases_map[i];
306 | break;
307 | }
308 | }
309 |
310 | return result;
311 | } /* DF_attribute_name2type() */
312 |
313 | /* XXX This could be done MUCH more efficiently (with a hash) */
314 | C_Type_t DF_class_code2type(const gchar *token) {
315 | C_Type_t result=-1;
316 |
317 | int i;
318 | for (i=0; Class_aliases[i] != NULL; i++) {
319 | if (strcmp(Class_aliases[i], token) == 0) {
320 | result = Class_aliases_map[i];
321 | break;
322 | }
323 | }
324 |
325 | return result;
326 | } /* DF_class_code2type() */
327 |
328 | /* XXX This could be done MUCH more efficiently (with a hash) */
329 | C_Type_t DF_class_name2type(const gchar *token) {
330 | C_Type_t result=-1;
331 |
332 | int i;
333 | for (i=0; Class_aliases[i] != NULL; i++) {
334 | if (strcmp(Class_aliases[i], token) == 0) {
335 | result = Class_aliases_map[i];
336 | break;
337 | }
338 | }
339 |
340 | return result;
341 | } /* DF_class_name2type() */
342 |
343 |
344 | /* check in the queries if this attribute can trigger a radix lookup */
345 | int DF_attrcode_has_radix_lookup(A_Type_t attr)
346 | {
347 | int i;
348 |
349 | for (i=0; Query[i].query != NULL; i++) {
350 | if( Query[i].refer == R_RADIX &&
351 | Query[i].attribute == attr ) {
352 | return 1;
353 | }
354 | }
355 | return 0;
356 | }