1 | /***************************************
2 | $Revision: 1.6 $
3 |
4 | Definitions module (df)
5 |
6 | Status: NOT REVUED, NOT TESTED
7 |
8 | ******************/ /******************
9 | Filename : defs.c
10 | Author : ottrey@ripe.net
11 | OSs Tested : Solaris
12 | Related Modules :
13 | Problems :
14 | To Do :
15 | Comments :
16 | ******************/ /******************
17 | Copyright (c) 1999 RIPE NCC
18 |
19 | All Rights Reserved
20 |
21 | Permission to use, copy, modify, and distribute this software and its
22 | documentation for any purpose and without fee is hereby granted,
23 | provided that the above copyright notice appear in all copies and that
24 | both that copyright notice and this permission notice appear in
25 | supporting documentation, and that the name of the author not be
26 | used in advertising or publicity pertaining to distribution of the
27 | software without specific, written prior permission.
28 |
29 | THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
30 | ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
31 | AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
32 | DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
33 | AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
34 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35 | ***************************************/
36 | #include <stdio.h>
37 | #include <stdlib.h>
38 | #include <stdarg.h>
39 | #include <strings.h>
40 | #include <glib.h>
41 |
42 | /*+ String sizes +*/
43 | #define STR_S 63
44 | #define STR_M 255
45 | #define STR_L 1023
46 | #define STR_XL 4095
47 | #define STR_XXL 16383
48 |
49 | #include "defs.h"
50 | #include "memwrap.h"
51 |
52 | #include "DF_class_names.def"
53 | #include "DF_class_codes.def"
54 | #include "DF_class_aliases.def"
55 | #include "DF_class_aliases_map.def"
56 | #include "DF_class_dbase_code_map.def"
57 | #include "DF_class_templates.def"
58 | #include "DF_class_templates_v.def"
59 |
60 | #include "DF_attribute_names.def"
61 | #include "DF_attribute_codes.def"
62 | #include "DF_attribute_aliases.def"
63 | #include "DF_attribute_aliases_map.def"
64 |
65 | /*+ Source database mirrors +*/
66 | typedef struct {
67 | char src[32];
68 | char db[32];
69 | } source_info_t;
70 |
71 | source_info_t const Sources[] = {
72 | {"RIPE","RIPE6"},
73 |
74 | /* "ARIN",
75 | "APNIC",
76 | */
77 |
78 | {"",""}
79 | }; /* Sources */
80 |
81 | /* getsubopt requires a vector of pointers to a list of possible options
82 | It's used for parsing the source list.
83 | Therefore a quick
84 | XXX !!!!
85 | hack: hardcode it. Will be initialised from the Sources array
86 | once the config module is defined
87 | */
88 |
89 | char * const Sources_vector[] = {
90 | "RIPE",
91 | NULL
92 | };
93 |
94 | char * const Server_queries[] = {
95 | "help",
96 | "sources",
97 | "version",
98 | "version",
99 | NULL
100 | }; /* Server_queries */
101 |
102 | char * const Server_query[] = {
103 | "You need help ...\n",
104 | "The sources are ...\n",
105 | "The version is ...\n",
106 | NULL
107 | }; /* Server_query */
108 |
109 | char * const Filter_names[] = {
110 | "aut-num",
111 | "domain",
112 | "inet6num",
113 | "inetnum",
114 | "inet-rtr",
115 | "key-cert",
116 | "limerick",
117 | "mntner",
118 | "route",
119 | "origin",
120 | "as-set",
121 | "route-set",
122 | "members",
123 | NULL
124 | }; /* Filter_names */
125 |
126 | char * const *DF_get_filter_names(void) {
127 | return Filter_names;
128 | } /* DF_get_filter_names() */
129 |
130 | char * const *DF_get_class_names(void) {
131 | return Class_names;
132 | } /* DF_get_class_names() */
133 |
134 | char * const *DF_get_class_aliases(void) {
135 | return Class_aliases;
136 | } /* DF_get_class_aliases() */
137 |
138 | int DF_get_class_index(int alias_index) {
139 | return Class_aliases_map[alias_index];
140 | } /* DF_get_class_index() */
141 |
142 | char * const DF_get_class_name(int alias_index) {
143 | return Class_names[Class_aliases_map[alias_index]];
144 | } /* DF_get_class_name() */
145 |
146 | char * const DF_get_class_code(C_Type_t index) {
147 | if( index == -1 ) {
148 | return "*";
149 | }
150 | else {
151 | return Class_codes[index];
152 | }
153 | } /* DF_get_class_code() */
154 |
155 | int DF_get_class_dbase_code(int class_index) {
156 | return Class_dbase_code_map[class_index];
157 | } /* DF_get_class_dbase_code() */
158 |
159 | char * const *DF_get_sources(void) {
160 | return Sources_vector;
161 | } /* DF_get_sources() */
162 |
163 | const char *DF_get_source(int index) {
164 | const char *s = Sources[index].src;
165 |
166 | return (*s == 0)
167 | ? NULL
168 | : s;
169 |
170 | } /* DF_get_source() */
171 |
172 | const char *DF_get_database(int index) {
173 | const char *s = Sources[index].db;
174 |
175 | return (*s == 0)
176 | ? NULL
177 | : s;
178 |
179 | } /* DF_get_database() */
180 |
181 |
182 | char * const *DF_get_attribute_aliases(void) {
183 | return Attribute_aliases;
184 | } /* DF_get_attribute_aliases() */
185 |
186 | const char *DF_get_attribute_name(A_Type_t index) {
187 | return Attribute_names[index];
188 | } /* DF_get_attribute_name() */
189 |
190 | const char *DF_get_attribute_code(A_Type_t index) {
191 | return Attribute_codes[index];
192 | } /* DF_get_attribute_code() */
193 |
194 | char * const *DF_get_attribute_names(void) {
195 | return Attribute_names;
196 | } /* DF_get_attribute_names() */
197 |
198 | int DF_get_attribute_index(int alias_index) {
199 | return Attribute_aliases_map[alias_index];
200 | } /* DF_get_attribute_index() */
201 |
202 | const char *DF_get_class_template(C_Type_t index) {
203 | return Templates[index];
204 | } /* DF_get_class_template() */
205 |
206 | const char *DF_get_class_template_v(C_Type_t index) {
207 | return Templates_v[index];
208 | } /* DF_get_class_template_v() */
209 |
210 | const char *DF_get_server_query(int index) {
211 | return Server_query[index];
212 | } /* DF_get_server_query() */
213 |
214 | char * const *DF_get_server_queries(void) {
215 | return Server_queries;
216 | } /* DF_get_server_queries() */
217 |
218 | const char *DF_get_attribute_desc(A_Type_t index) {
219 | /*
220 | return (char *)Attributes_details[attr_index][0];
221 | */
222 | return NULL;
223 | } /* DF_get_attribute_desc() */
224 |
225 | const char *DF_get_attribute_frmt(A_Type_t index) {
226 | /*
227 | return (char *)Attributes_details[attr_index][1];
228 | */
229 | return NULL;
230 | } /* DF_get_attribute_frmt() */
231 |
232 | /* source_foreach() */
233 | /*++++++++++++++++++++++++++++++++++++++
234 | Function to adds the source string to the created string from the Glist of sources.
235 | It is called via g_list_foreach().
236 |
237 | void *element_data The source name.
238 |
239 | void *result_buf_ptr The string to be populated.
240 |
241 | More:
242 | +html+ <PRE>
243 | Authors:
244 | ottrey
245 |
246 | +html+ </PRE><DL COMPACT>
247 | +html+ <DT>Online References:
248 | +html+ <DD><UL>
249 | +html+ </UL></DL>
250 |
251 | ++++++++++++++++++++++++++++++++++++++*/
252 | static void source_foreach(void *element_data, void *result_buf_ptr) {
253 | char *source = element_data;
254 | char *result_buf = (char *)result_buf_ptr;
255 |
256 | strcat(result_buf, source);
257 | strcat(result_buf, ",");
258 |
259 | } /* source_foreach() */
260 |
261 | /* DF_sources_to_string() */
262 | /*++++++++++++++++++++++++++++++++++++++
263 | Creates a string from Sources.
264 |
265 | char * DF_sources_to_string Returns a string of the Sources.
266 |
267 | More:
268 | +html+ <PRE>
269 | Authors:
270 | ottrey
271 |
272 | +html+ </PRE><DL COMPACT>
273 | +html+ <DT>Online References:
274 | +html+ <DD><UL>
275 | +html+ </UL></DL>
276 |
277 | ++++++++++++++++++++++++++++++++++++++*/
278 | char * DF_sources_to_string(void) {
279 | char *result=NULL;
280 | char result_buf[STR_XL];
281 | int result_len;
282 | int i;
283 |
284 | strcpy(result_buf, "{");
285 | for (i=0; Sources[i].src[0] != 0 ; i++) {
286 | strcat(result_buf, Sources[i].src);
287 | strcat(result_buf, ",");
288 | }
289 | result_len = strlen(result_buf);
290 | result_buf[result_len-1] = '}';
291 | result_buf[result_len] = '\0';
292 |
293 | // result = (char *)calloc(1, result_len+1);
294 | dieif( wr_malloc((void **)&result, result_len+1) != UT_OK);
295 | strcpy(result, result_buf);
296 |
297 | return result;
298 |
299 | } /* DF_sources_to_string() */
300 |
301 | /* DF_sources_list_to_string() */
302 | /*++++++++++++++++++++++++++++++++++++++
303 | Creates a string from the sources in the GList.
304 |
305 | GList *sources_list The GList of sources.
306 |
307 | More:
308 | +html+ <PRE>
309 | Authors:
310 | ottrey
311 |
312 | +html+ </PRE><DL COMPACT>
313 | +html+ <DT>Online References:
314 | +html+ <DD><UL>
315 | +html+ </UL></DL>
316 |
317 | ++++++++++++++++++++++++++++++++++++++*/
318 | char *DF_sources_list_to_string(GList *sources_list) {
319 | char *result=NULL;
320 | char result_buf[STR_XL];
321 | int result_len;
322 |
323 | strcpy(result_buf, "{");
324 | g_list_foreach(sources_list, source_foreach, &result_buf);
325 | result_len = strlen(result_buf);
326 | if (result_len == 1) {
327 | /* If an empty set */
328 | result_buf[1] = '}';
329 | result_buf[2] = '\0';
330 | }
331 | else {
332 | result_buf[result_len-1] = '}';
333 | result_buf[result_len] = '\0';
334 | }
335 |
336 | //result = (char *)calloc(1, result_len+1);
337 | dieif( wr_malloc((void **)&result, result_len+1) != UT_OK);
338 | strcpy(result, result_buf);
339 |
340 | return result;
341 |
342 | } /* DF_sources_list_to_string() */
343 |
344 |
345 | /* DF_attributes_to_string() */
346 | /*++++++++++++++++++++++++++++++++++++++
347 | Returns a string of all the attributes. Only there for debugging and tracing purposes.
348 |
349 | int offset The offset (Ie short or long name).
350 |
351 | More:
352 | +html+ <PRE>
353 | Authors:
354 | ottrey
355 |
356 | +html+ </PRE><DL COMPACT>
357 | +html+ <DT>Online References:
358 | +html+ <DD><UL>
359 | +html+ </UL></DL>
360 |
361 | ++++++++++++++++++++++++++++++++++++++*/
362 | char *DF_attributes_to_string(void) {
363 | int i;
364 | char *str;
365 | char str_buffer[4096];
366 | int str_len;
367 |
368 | strcpy(str_buffer, "{\"");
369 | for (i=0; Attribute_names[i] != NULL; i++) {
370 | strcat(str_buffer, Attribute_names[i]);
371 | strcat(str_buffer, "\", \"");
372 | }
373 | str_len = strlen(str_buffer);
374 | str_buffer[str_len-3] = '}';
375 | str_buffer[str_len-2] = '\0';
376 | str_len--;
377 |
378 | // str = (char *)calloc(1, str_len);
379 | dieif( wr_malloc((void **)&str, str_len ) != UT_OK);
380 | strcpy(str, str_buffer);
381 |
382 | return str;
383 |
384 | } /* DF_attributes_to_string() */
385 |
386 | /* XXX This could be done MUCH more efficiently (with a hash) */
387 | A_Type_t DF_attribute_code2type(const gchar *token) {
388 | A_Type_t result=-1;
389 |
390 | int i;
391 | for (i=0; Attribute_aliases[i] != NULL; i++) {
392 | if (strcmp(Attribute_aliases[i], token) == 0) {
393 | result = Attribute_aliases_map[i];
394 | break;
395 | }
396 | }
397 |
398 | return result;
399 | } /* DF_attribute_code2type() */
400 |
401 | /* XXX This could be done MUCH more efficiently (with a hash) */
402 | A_Type_t DF_attribute_name2type(const gchar *token) {
403 | A_Type_t result=-1;
404 |
405 | int i;
406 | for (i=0; Attribute_aliases[i] != NULL; i++) {
407 | if (strcmp(Attribute_aliases[i], token) == 0) {
408 | result = Attribute_aliases_map[i];
409 | break;
410 | }
411 | }
412 |
413 | return result;
414 | } /* DF_attribute_name2type() */
415 |
416 | /* XXX This could be done MUCH more efficiently (with a hash) */
417 | C_Type_t DF_class_code2type(const gchar *token) {
418 | C_Type_t result=-1;
419 |
420 | int i;
421 | for (i=0; Class_aliases[i] != NULL; i++) {
422 | if (strcmp(Class_aliases[i], token) == 0) {
423 | result = Class_aliases_map[i];
424 | break;
425 | }
426 | }
427 |
428 | return result;
429 | } /* DF_class_code2type() */
430 |
431 | /* XXX This could be done MUCH more efficiently (with a hash) */
432 | C_Type_t DF_class_name2type(const gchar *token) {
433 | C_Type_t result=-1;
434 |
435 | int i;
436 | for (i=0; Class_aliases[i] != NULL; i++) {
437 | if (strcmp(Class_aliases[i], token) == 0) {
438 | result = Class_aliases_map[i];
439 | break;
440 | }
441 | }
442 |
443 | return result;
444 | } /* DF_class_name2type() */
445 |