1 | /*************************************** 2 | $Revision: 1.2 $ 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 | 51 | #include "DF_class_names.def" 52 | #include "DF_class_codes.def" 53 | #include "DF_class_aliases.def" 54 | #include "DF_class_aliases_map.def" 55 | #include "DF_class_dbase_code_map.def" 56 | #include "DF_class_templates.def" 57 | #include "DF_class_templates_v.def" 58 | 59 | #include "DF_attribute_names.def" 60 | #include "DF_attribute_codes.def" 61 | #include "DF_attribute_aliases.def" 62 | #include "DF_attribute_aliases_map.def" 63 | 64 | /*+ Source database mirrors +*/ 65 | char * const Sources[] = { 66 | "RIPE3", 67 | "RIPE", 68 | "ARIN", 69 | "APNIC", 70 | NULL 71 | }; /* Sources */ 72 | 73 | char * const Server_queries[] = { 74 | "help", 75 | "sources", 76 | "version", 77 | "version", 78 | NULL 79 | }; /* Server_queries */ 80 | 81 | char * const Server_query[] = { 82 | "You need help ...\n", 83 | "The sources are ...\n", 84 | "The version is ...\n", 85 | NULL 86 | }; /* Server_query */ 87 | 88 | char * const Filter_names[] = { 89 | "aut-num", 90 | "domain", 91 | "inet6num", 92 | "inetnum", 93 | "inet-rtr", 94 | "key-cert", 95 | "limerick", 96 | "mntner", 97 | "route", 98 | "origin", 99 | "as-set", 100 | "route-set", 101 | "members", 102 | NULL 103 | }; /* Filter_names */ 104 | 105 | char * const *DF_get_filter_names(void) { 106 | return Filter_names; 107 | } /* DF_get_filter_names() */ 108 | 109 | char * const *DF_get_class_names(void) { 110 | return Class_names; 111 | } /* DF_get_class_names() */ 112 | 113 | char * const *DF_get_class_aliases(void) { 114 | return Class_aliases; 115 | } /* DF_get_class_aliases() */ 116 | 117 | int DF_get_class_index(int alias_index) { 118 | return Class_aliases_map[alias_index]; 119 | } /* DF_get_class_index() */ 120 | 121 | char * const DF_get_class_name(int alias_index) { 122 | return Class_names[Class_aliases_map[alias_index]]; 123 | } /* DF_get_class_name() */ 124 | 125 | char * const DF_get_class_code(C_Type_t index) { 126 | return Class_codes[index]; 127 | } /* DF_get_class_code() */ 128 | 129 | int DF_get_class_dbase_code(int class_index) { 130 | return Class_dbase_code_map[class_index]; 131 | } /* DF_get_class_dbase_code() */ 132 | 133 | char * const *DF_get_sources(void) { 134 | return Sources; 135 | } /* DF_get_sources() */ 136 | 137 | const char *DF_get_source(int index) { 138 | return Sources[index]; 139 | } /* DF_get_source() */ 140 | 141 | char * const *DF_get_attribute_aliases(void) { 142 | return Attribute_aliases; 143 | } /* DF_get_attribute_aliases() */ 144 | 145 | const char *DF_get_attribute_name(A_Type_t index) { 146 | return Attribute_names[index]; 147 | } /* DF_get_attribute_name() */ 148 | 149 | const char *DF_get_attribute_code(A_Type_t index) { 150 | return Attribute_codes[index]; 151 | } /* DF_get_attribute_code() */ 152 | 153 | char * const *DF_get_attribute_names(void) { 154 | return Attribute_names; 155 | } /* DF_get_attribute_names() */ 156 | 157 | int DF_get_attribute_index(int alias_index) { 158 | return Attribute_aliases_map[alias_index]; 159 | } /* DF_get_attribute_index() */ 160 | 161 | const char *DF_get_class_template(C_Type_t index) { 162 | return Templates[index]; 163 | } /* DF_get_class_template() */ 164 | 165 | const char *DF_get_class_template_v(C_Type_t index) { 166 | return Templates_v[index]; 167 | } /* DF_get_class_template_v() */ 168 | 169 | const char *DF_get_server_query(int index) { 170 | return Server_query[index]; 171 | } /* DF_get_server_query() */ 172 | 173 | char * const *DF_get_server_queries(void) { 174 | return Server_queries; 175 | } /* DF_get_server_queries() */ 176 | 177 | const char *DF_get_attribute_desc(A_Type_t index) { 178 | /* 179 | return (char *)Attributes_details[attr_index][0]; 180 | */ 181 | return NULL; 182 | } /* DF_get_attribute_desc() */ 183 | 184 | const char *DF_get_attribute_frmt(A_Type_t index) { 185 | /* 186 | return (char *)Attributes_details[attr_index][1]; 187 | */ 188 | return NULL; 189 | } /* DF_get_attribute_frmt() */ 190 | 191 | /* source_foreach() */ 192 | /*++++++++++++++++++++++++++++++++++++++ 193 | Function to adds the source string to the created string from the Glist of sources. 194 | It is called via g_list_foreach(). 195 | 196 | void *element_data The source name. 197 | 198 | void *result_buf_ptr The string to be populated. 199 | 200 | More: 201 | +html+ <PRE> 202 | Authors: 203 | ottrey 204 | 205 | +html+ </PRE><DL COMPACT> 206 | +html+ <DT>Online References: 207 | +html+ <DD><UL> 208 | +html+ </UL></DL> 209 | 210 | ++++++++++++++++++++++++++++++++++++++*/ 211 | static void source_foreach(void *element_data, void *result_buf_ptr) { 212 | char *source = element_data; 213 | char *result_buf = (char *)result_buf_ptr; 214 | 215 | strcat(result_buf, element_data); 216 | strcat(result_buf, ","); 217 | 218 | } /* source_foreach() */ 219 | 220 | /* DF_sources_to_string() */ 221 | /*++++++++++++++++++++++++++++++++++++++ 222 | Creates a string from Sources. 223 | 224 | char * DF_sources_to_string Returns a string of the Sources. 225 | 226 | More: 227 | +html+ <PRE> 228 | Authors: 229 | ottrey 230 | 231 | +html+ </PRE><DL COMPACT> 232 | +html+ <DT>Online References: 233 | +html+ <DD><UL> 234 | +html+ </UL></DL> 235 | 236 | ++++++++++++++++++++++++++++++++++++++*/ 237 | char * DF_sources_to_string(void) { 238 | char *result=NULL; 239 | char result_buf[STR_XL]; 240 | int result_len; 241 | int i; 242 | 243 | strcpy(result_buf, "{"); 244 | for (i=0; Sources[i] != NULL; i++) { 245 | strcat(result_buf, Sources[i]); 246 | strcat(result_buf, ","); 247 | } 248 | result_len = strlen(result_buf); 249 | result_buf[result_len-1] = '}'; 250 | result_buf[result_len] = '\0'; 251 | 252 | result = (char *)calloc(1, result_len+1); 253 | strcpy(result, result_buf); 254 | 255 | return result; 256 | 257 | } /* DF_sources_to_string() */ 258 | 259 | /* DF_sources_list_to_string() */ 260 | /*++++++++++++++++++++++++++++++++++++++ 261 | Creates a string from the sources in the GList. 262 | 263 | GList *sources_list The GList of sources. 264 | 265 | More: 266 | +html+ <PRE> 267 | Authors: 268 | ottrey 269 | 270 | +html+ </PRE><DL COMPACT> 271 | +html+ <DT>Online References: 272 | +html+ <DD><UL> 273 | +html+ </UL></DL> 274 | 275 | ++++++++++++++++++++++++++++++++++++++*/ 276 | char *DF_sources_list_to_string(GList *sources_list) { 277 | char *result=NULL; 278 | char result_buf[STR_XL]; 279 | int result_len; 280 | 281 | strcpy(result_buf, "{"); 282 | g_list_foreach(sources_list, source_foreach, &result_buf); 283 | result_len = strlen(result_buf); 284 | if (result_len == 1) { 285 | /* If an empty set */ 286 | result_buf[1] = '}'; 287 | result_buf[2] = '\0'; 288 | } 289 | else { 290 | result_buf[result_len-1] = '}'; 291 | result_buf[result_len] = '\0'; 292 | } 293 | 294 | result = (char *)calloc(1, result_len+1); 295 | strcpy(result, result_buf); 296 | 297 | return result; 298 | 299 | } /* DF_sources_list_to_string() */ 300 | 301 | 302 | /* DF_attributes_to_string() */ 303 | /*++++++++++++++++++++++++++++++++++++++ 304 | Returns a string of all the attributes. Only there for debugging and tracing purposes. 305 | 306 | int offset The offset (Ie short or long name). 307 | 308 | More: 309 | +html+ <PRE> 310 | Authors: 311 | ottrey 312 | 313 | +html+ </PRE><DL COMPACT> 314 | +html+ <DT>Online References: 315 | +html+ <DD><UL> 316 | +html+ </UL></DL> 317 | 318 | ++++++++++++++++++++++++++++++++++++++*/ 319 | char *DF_attributes_to_string(void) { 320 | int i; 321 | char *str; 322 | char str_buffer[4096]; 323 | int str_len; 324 | 325 | strcpy(str_buffer, "{\""); 326 | for (i=0; Attribute_names[i] != NULL; i++) { 327 | strcat(str_buffer, Attribute_names[i]); 328 | strcat(str_buffer, "\", \""); 329 | } 330 | str_len = strlen(str_buffer); 331 | str_buffer[str_len-3] = '}'; 332 | str_buffer[str_len-2] = '\0'; 333 | str_len--; 334 | 335 | str = (char *)calloc(1, str_len); 336 | strcpy(str, str_buffer); 337 | 338 | return str; 339 | 340 | } /* DF_attributes_to_string() */ 341 | 342 | /* XXX This could be done MUCH more efficiently (with a hash) */ 343 | A_Type_t DF_attribute_code2type(const gchar *token) { 344 | A_Type_t result=-1; 345 | 346 | int i; 347 | for (i=0; Attribute_aliases[i] != NULL; i++) { 348 | if (strcmp(Attribute_aliases[i], token) == 0) { 349 | result = Attribute_aliases_map[i]; 350 | break; 351 | } 352 | } 353 | 354 | return result; 355 | } /* DF_attribute_code2type() */ 356 | 357 | /* XXX This could be done MUCH more efficiently (with a hash) */ 358 | A_Type_t DF_attribute_name2type(const gchar *token) { 359 | A_Type_t result=-1; 360 | 361 | int i; 362 | for (i=0; Attribute_aliases[i] != NULL; i++) { 363 | if (strcmp(Attribute_aliases[i], token) == 0) { 364 | result = Attribute_aliases_map[i]; 365 | break; 366 | } 367 | } 368 | 369 | return result; 370 | } /* DF_attribute_name2type() */ 371 | 372 | /* XXX This could be done MUCH more efficiently (with a hash) */ 373 | C_Type_t DF_class_code2type(const gchar *token) { 374 | C_Type_t result=-1; 375 | 376 | int i; 377 | for (i=0; Class_aliases[i] != NULL; i++) { 378 | if (strcmp(Class_aliases[i], token) == 0) { 379 | result = Class_aliases_map[i]; 380 | break; 381 | } 382 | } 383 | 384 | return result; 385 | } /* DF_class_code2type() */ 386 | 387 | /* XXX This could be done MUCH more efficiently (with a hash) */ 388 | C_Type_t DF_class_name2type(const gchar *token) { 389 | C_Type_t result=-1; 390 | 391 | int i; 392 | for (i=0; Class_aliases[i] != NULL; i++) { 393 | if (strcmp(Class_aliases[i], token) == 0) { 394 | result = Class_aliases_map[i]; 395 | break; 396 | } 397 | } 398 | 399 | return result; 400 | } /* DF_class_name2type() */ 401 |