1 | /***************************************
2 | $Revision: 1.14 $
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 |
75 |
76 | char * const Server_query[] = {
77 | "You need help ...\n",
78 | "The sources are ...\n",
79 | "The version is ...\n",
80 | NULL
81 | }; /* Server_query */
82 |
83 | char * const Server_queries[] = {
84 | "help",
85 | "sources",
86 | "version",
87 | NULL
88 | }; /* Server_queries */
89 |
90 | /* XXX this also should be generated from XML... */
91 | char * const Filter_names[] = {
92 | "aut-num",
93 | "domain",
94 | "inet6num",
95 | "inetnum",
96 | "inet-rtr",
97 | "key-cert",
98 | "limerick",
99 | "mntner",
100 | "route",
101 | "origin",
102 | "as-set",
103 | "route-set",
104 | "members",
105 | "peering-set",
106 | "filter-set",
107 | "rtr-set",
108 | NULL
109 | }; /* Filter_names */
110 |
111 | char * const *DF_get_filter_names(void) {
112 | return Filter_names;
113 | } /* DF_get_filter_names() */
114 |
115 | char * const *DF_get_class_names(void) {
116 | return Class_names;
117 | } /* DF_get_class_names() */
118 |
119 | char * const *DF_get_class_aliases(void) {
120 | return Class_aliases;
121 | } /* DF_get_class_aliases() */
122 |
123 | int DF_get_class_index(int alias_index) {
124 | return Class_aliases_map[alias_index];
125 | } /* DF_get_class_index() */
126 |
127 | char * const DF_get_class_name(int alias_index) {
128 | return Class_names[Class_aliases_map[alias_index]];
129 | } /* DF_get_class_name() */
130 |
131 | char * const DF_get_class_code(C_Type_t index) {
132 | if( index == -1 ) {
133 | return "*";
134 | }
135 | else {
136 | return Class_codes[index];
137 | }
138 | } /* DF_get_class_code() */
139 |
140 | int DF_get_class_dbase_code(int class_index) {
141 | return Class_dbase_code_map[class_index];
142 | } /* DF_get_class_dbase_code() */
143 |
144 | /* Main tables names for object types */
145 | char * const Type2main[] = {
146 | "as_block",
147 | "as_set",
148 | "aut_num",
149 | "domain",
150 | "inet_rtr",
151 | "inet6num",
152 | "inetnum",
153 | "key_cert",
154 | "limerick",
155 | "mntner",
156 | "person_role", /*pn*/
157 | "person_role", /*ro*/
158 | "route",
159 | "route_set",
160 | "filter_set",
161 | "peering_set",
162 | "rtr_set",
163 | NULL
164 | };
165 |
166 | char * const DF_get_class_sql_table(C_Type_t index) {
167 | return Type2main[index];
168 | } /* DF_get_class_sql_table() */
169 |
170 |
171 |
172 | char * const *DF_get_attribute_aliases(void) {
173 | return Attribute_aliases;
174 | } /* DF_get_attribute_aliases() */
175 |
176 | const char *DF_get_attribute_name(A_Type_t index) {
177 | return Attribute_names[index];
178 | } /* DF_get_attribute_name() */
179 |
180 | const char *DF_get_attribute_code(A_Type_t index) {
181 | return Attribute_codes[index];
182 | } /* DF_get_attribute_code() */
183 |
184 | char * const *DF_get_attribute_names(void) {
185 | return Attribute_names;
186 | } /* DF_get_attribute_names() */
187 |
188 | int DF_get_attribute_index(int alias_index) {
189 | return Attribute_aliases_map[alias_index];
190 | } /* DF_get_attribute_index() */
191 |
192 | const char *DF_get_class_template(C_Type_t index) {
193 | return Templates[index];
194 | } /* DF_get_class_template() */
195 |
196 | const char *DF_get_class_template_v(C_Type_t index) {
197 | return Templates_v[index];
198 | } /* DF_get_class_template_v() */
199 |
200 | const char *DF_get_server_query(int index) {
201 | return Server_query[index];
202 | } /* DF_get_server_query() */
203 |
204 | char * const *DF_get_server_queries(void) {
205 | return Server_queries;
206 | } /* DF_get_server_queries() */
207 |
208 | const char *DF_get_update_query(A_Type_t index){
209 | return Update[index].qry;
210 | } /* DF_get_update_query() */
211 |
212 | UD_qtype DF_get_update_query_type(A_Type_t index){
213 | return Update[index].qtype;
214 | } /* DF_get_update_query_type() */
215 |
216 | const char *DF_get_insert_query(A_Type_t index){
217 | return Insert[index].qry;
218 | } /* DF_get_insert_query() */
219 |
220 | UD_qtype DF_get_insert_query_type(A_Type_t index){
221 | return Insert[index].qtype;
222 | } /* DF_get_insert_query_type() */
223 |
224 | const char *DF_get_select_query(A_Type_t index){
225 | return Select[index].qry;
226 | } /* DF_get_select_query() */
227 |
228 | UD_qtype DF_get_select_query_type(A_Type_t index){
229 | return Select[index].qtype;
230 | } /* DF_get_select_query_type() */
231 |
232 | const char *DF_get_dummy_query(A_Type_t index){
233 | return Dummy[index].qry;
234 | } /* DF_get_dummy_query() */
235 |
236 | UD_qtype DF_get_dummy_query_type(A_Type_t index){
237 | return Dummy[index].qtype;
238 | } /* DF_get_dummy_query_type() */
239 |
240 |
241 |
242 | const char *DF_get_attribute_desc(A_Type_t index) {
243 | /*
244 | return (char *)Attributes_details[attr_index][0];
245 | */
246 | return NULL;
247 | } /* DF_get_attribute_desc() */
248 |
249 | const char *DF_get_attribute_frmt(A_Type_t index) {
250 | /*
251 | return (char *)Attributes_details[attr_index][1];
252 | */
253 | return NULL;
254 | } /* DF_get_attribute_frmt() */
255 |
256 | /* DF_attributes_to_string() */
257 | /*++++++++++++++++++++++++++++++++++++++
258 | Returns a string of all the attributes. Only there for debugging and tracing purposes.
259 |
260 | int offset The offset (Ie short or long name).
261 |
262 | More:
263 | +html+ <PRE>
264 | Authors:
265 | ottrey
266 |
267 | +html+ </PRE><DL COMPACT>
268 | +html+ <DT>Online References:
269 | +html+ <DD><UL>
270 | +html+ </UL></DL>
271 |
272 | ++++++++++++++++++++++++++++++++++++++*/
273 | char *DF_attributes_to_string(void) {
274 | int i;
275 | char *str;
276 | char str_buffer[4096];
277 | int str_len;
278 |
279 | strcpy(str_buffer, "{\"");
280 | for (i=0; Attribute_names[i] != NULL; i++) {
281 | strcat(str_buffer, Attribute_names[i]);
282 | strcat(str_buffer, "\", \"");
283 | }
284 | str_len = strlen(str_buffer);
285 | str_buffer[str_len-3] = '}';
286 | str_buffer[str_len-2] = '\0';
287 | str_len--;
288 |
289 | /* str = (char *)calloc(1, str_len); */
290 | dieif( wr_malloc((void **)&str, str_len ) != UT_OK);
291 | strcpy(str, str_buffer);
292 |
293 | return str;
294 |
295 | } /* DF_attributes_to_string() */
296 |
297 | /* XXX This could be done MUCH more efficiently (with a hash) */
298 | A_Type_t DF_attribute_code2type(const gchar *token) {
299 | A_Type_t result=-1;
300 |
301 | int i;
302 | for (i=0; Attribute_aliases[i] != NULL; i++) {
303 | if (strcmp(Attribute_aliases[i], token) == 0) {
304 | result = Attribute_aliases_map[i];
305 | break;
306 | }
307 | }
308 |
309 | return result;
310 | } /* DF_attribute_code2type() */
311 |
312 | /* XXX This could be done MUCH more efficiently (with a hash) */
313 | A_Type_t DF_attribute_name2type(const gchar *token) {
314 | A_Type_t result=-1;
315 |
316 | int i;
317 | for (i=0; Attribute_aliases[i] != NULL; i++) {
318 | if (strcmp(Attribute_aliases[i], token) == 0) {
319 | result = Attribute_aliases_map[i];
320 | break;
321 | }
322 | }
323 |
324 | return result;
325 | } /* DF_attribute_name2type() */
326 |
327 | /* XXX This could be done MUCH more efficiently (with a hash) */
328 | C_Type_t DF_class_code2type(const gchar *token) {
329 | C_Type_t result=-1;
330 |
331 | int i;
332 | for (i=0; Class_aliases[i] != NULL; i++) {
333 | if (strcmp(Class_aliases[i], token) == 0) {
334 | result = Class_aliases_map[i];
335 | break;
336 | }
337 | }
338 |
339 | return result;
340 | } /* DF_class_code2type() */
341 |
342 | /* XXX This could be done MUCH more efficiently (with a hash) */
343 | C_Type_t DF_class_name2type(const gchar *token) {
344 | C_Type_t result=-1;
345 |
346 | int i;
347 | for (i=0; Class_aliases[i] != NULL; i++) {
348 | if (strcmp(Class_aliases[i], token) == 0) {
349 | result = Class_aliases_map[i];
350 | break;
351 | }
352 | }
353 |
354 | return result;
355 | } /* DF_class_name2type() */
356 |
357 |
358 | /* check in the queries if this attribute can trigger a radix lookup */
359 | int DF_attrcode_has_radix_lookup(A_Type_t attr)
360 | {
361 | int i;
362 |
363 | for (i=0; Query[i].query != NULL; i++) {
364 | if( Query[i].refer == R_RADIX &&
365 | Query[i].attribute == attr ) {
366 | return 1;
367 | }
368 | }
369 | return 0;
370 | }