modules/df/defs.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. DF_get_filter_names
  2. DF_get_class_names
  3. DF_get_class_aliases
  4. DF_get_class_index
  5. DF_get_class_name
  6. DF_get_class_code
  7. DF_get_class_dbase_code
  8. DF_get_class_sql_table
  9. DF_get_attribute_aliases
  10. DF_get_attribute_name
  11. DF_get_attribute_code
  12. DF_get_attribute_names
  13. DF_get_attribute_index
  14. DF_get_class_template
  15. DF_get_class_template_v
  16. DF_get_server_queries
  17. DF_get_update_query
  18. DF_get_update_query_type
  19. DF_get_insert_query
  20. DF_get_insert_query_type
  21. DF_get_select_query
  22. DF_get_select_query_type
  23. DF_get_dummy_query
  24. DF_get_dummy_query_type
  25. DF_get_attribute_desc
  26. DF_get_attribute_frmt
  27. DF_attributes_to_string
  28. DF_attribute_code2type
  29. init_name2type_hash
  30. DF_attribute_name2type
  31. DF_class_code2type
  32. DF_class_name2type
  33. DF_class_type2name
  34. DF_attrcode_has_radix_lookup
  35. DF_attrcode_radix_load_v4
  36. DF_attrcode_radix_load_v6
  37. DF_attrcode_radix_family

   1 /***************************************
   2   $Revision: 1.26 $
   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,2000,2001                    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 #define  DEFS_IMPL
  33 #include "rip.h"
  34 
  35 #include <stdio.h>
  36 #include <stdlib.h>
  37 #include <stdarg.h>
  38 #include <strings.h>
  39 #include <glib.h>
  40 #include <pthread.h>
  41 
  42 #include "DF_class_names.def"
  43 #include "DF_class_codes.def"
  44 #include "DF_class_aliases.def"
  45 #include "DF_class_aliases_map.def"
  46 #include "DF_class_dbase_code_map.def"
  47 #include "DF_class_templates.def"
  48 #include "DF_class_templates_v.def"
  49 
  50 #include "DF_attribute_names.def"
  51 #include "DF_attribute_codes.def"
  52 #include "DF_attribute_aliases.def"
  53 #include "DF_attribute_aliases_map.def"
  54 
  55 #include "UD_queries.def"
  56 
  57 
  58 
  59 /* getsubopt requires a vector of pointers to a list of possible options
  60    It's used for parsing the source list.
  61    Therefore a quick 
  62  XXX  !!!!
  63    hack: hardcode it. Will be initialised from the Sources array 
  64    once the config module is defined
  65 */
  66 
  67 char * const  Server_queries[] = {
  68   "sources",
  69   "version",
  70   NULL
  71 }; /* Server_queries */
  72 
  73 /* Filtered names of classes (only "public" objects, no person or role).
  74 
  75    XXX this also should be generated from XML... 
  76 */
  77 char * const  Filter_names[] = {
  78   "aut-num",
  79   "domain",
  80   "inet6num",
  81   "inetnum",
  82   "inet-rtr",
  83   "key-cert",
  84   "limerick",
  85   "mntner",
  86   "route",
  87   "origin",
  88   "as-set",
  89   "route-set",   
  90   "members",
  91   "peering-set",
  92   "filter-set",
  93   "rtr-set",
  94   NULL
  95 }; /* Filter_names */
  96 
  97 char * const *DF_get_filter_names(void) {
     /* [<][>][^][v][top][bottom][index][help] */
  98   return Filter_names;
  99 } /* DF_get_filter_names() */
 100 
 101 char * const *DF_get_class_names(void) {
     /* [<][>][^][v][top][bottom][index][help] */
 102   return Class_names;
 103 } /* DF_get_class_names() */
 104 
 105 char * const *DF_get_class_aliases(void) {
     /* [<][>][^][v][top][bottom][index][help] */
 106   return Class_aliases;
 107 } /* DF_get_class_aliases() */
 108 
 109 int DF_get_class_index(int alias_index) {
     /* [<][>][^][v][top][bottom][index][help] */
 110   return Class_aliases_map[alias_index];
 111 } /* DF_get_class_index() */
 112 
 113 #if 0
 114 char * const DF_get_class_name(int alias_index) {
     /* [<][>][^][v][top][bottom][index][help] */
 115   return Class_names[Class_aliases_map[alias_index]];
 116 } /* DF_get_class_name() */
 117 #endif
 118 
 119 char * const DF_get_class_code(C_Type_t index) {
     /* [<][>][^][v][top][bottom][index][help] */
 120   if( index == C_ANY ) {
 121     return "*";
 122   }
 123   else {
 124     return Class_codes[index];
 125   }
 126 } /* DF_get_class_code() */
 127 
 128 int DF_get_class_dbase_code(int class_index) {
     /* [<][>][^][v][top][bottom][index][help] */
 129   return Class_dbase_code_map[class_index];
 130 } /* DF_get_class_dbase_code() */
 131 
 132 /* Main tables names for object types */
 133 char * const Type2main[] = {
 134 "as_block",
 135 "as_set",
 136 "aut_num",
 137 "domain",
 138 "inet_rtr",
 139 "inet6num",
 140 "inetnum",
 141 "key_cert",
 142 "limerick",
 143 "mntner",
 144 "person_role", /*pn*/
 145 "person_role", /*ro*/
 146 "route",
 147 "route_set",
 148 "filter_set",
 149 "peering_set",
 150 "rtr_set",
 151 NULL
 152 };
 153 
 154 char * const DF_get_class_sql_table(C_Type_t index) {
     /* [<][>][^][v][top][bottom][index][help] */
 155  return Type2main[index];
 156 } /* DF_get_class_sql_table() */ 
 157 
 158 
 159 
 160 char * const *DF_get_attribute_aliases(void) {
     /* [<][>][^][v][top][bottom][index][help] */
 161   return Attribute_aliases;
 162 } /* DF_get_attribute_aliases() */
 163 
 164 const char *DF_get_attribute_name(A_Type_t index) {
     /* [<][>][^][v][top][bottom][index][help] */
 165   return Attribute_names[index];
 166 } /* DF_get_attribute_name() */
 167 
 168 const char *DF_get_attribute_code(A_Type_t index) {
     /* [<][>][^][v][top][bottom][index][help] */
 169   return Attribute_codes[index];
 170 } /* DF_get_attribute_code() */
 171 
 172 char * const *DF_get_attribute_names(void) {
     /* [<][>][^][v][top][bottom][index][help] */
 173   return Attribute_names;
 174 } /* DF_get_attribute_names() */
 175 
 176 int DF_get_attribute_index(int alias_index) {
     /* [<][>][^][v][top][bottom][index][help] */
 177   return Attribute_aliases_map[alias_index];
 178 } /* DF_get_attribute_index() */
 179 
 180 const char *DF_get_class_template(C_Type_t index) {
     /* [<][>][^][v][top][bottom][index][help] */
 181   return Templates[index];
 182 } /* DF_get_class_template() */
 183 
 184 const char *DF_get_class_template_v(C_Type_t index) {
     /* [<][>][^][v][top][bottom][index][help] */
 185   return Templates_v[index];
 186 } /* DF_get_class_template_v() */
 187 
 188 char * const *DF_get_server_queries(void) {
     /* [<][>][^][v][top][bottom][index][help] */
 189   return Server_queries;
 190 } /* DF_get_server_queries() */
 191 
 192 const char *DF_get_update_query(A_Type_t index){
     /* [<][>][^][v][top][bottom][index][help] */
 193   return Update[index].qry;
 194 } /* DF_get_update_query() */
 195 
 196 UD_qtype DF_get_update_query_type(A_Type_t index){
     /* [<][>][^][v][top][bottom][index][help] */
 197   return Update[index].qtype;
 198 } /* DF_get_update_query_type() */
 199 
 200 const char *DF_get_insert_query(A_Type_t index){
     /* [<][>][^][v][top][bottom][index][help] */
 201   return Insert[index].qry;
 202 } /* DF_get_insert_query() */
 203   
 204 UD_qtype DF_get_insert_query_type(A_Type_t index){
     /* [<][>][^][v][top][bottom][index][help] */
 205   return Insert[index].qtype;
 206 } /* DF_get_insert_query_type() */
 207     
 208 const char *DF_get_select_query(A_Type_t index){
     /* [<][>][^][v][top][bottom][index][help] */
 209   return Select[index].qry;
 210 } /* DF_get_select_query() */
 211   
 212 UD_qtype DF_get_select_query_type(A_Type_t index){
     /* [<][>][^][v][top][bottom][index][help] */
 213   return Select[index].qtype;
 214 } /* DF_get_select_query_type() */
 215         
 216 const char *DF_get_dummy_query(A_Type_t index){
     /* [<][>][^][v][top][bottom][index][help] */
 217   return Dummy[index].qry;
 218 } /* DF_get_dummy_query() */
 219   
 220 UD_qtype DF_get_dummy_query_type(A_Type_t index){
     /* [<][>][^][v][top][bottom][index][help] */
 221   return Dummy[index].qtype;
 222 } /* DF_get_dummy_query_type() */
 223 
 224 
 225 #if 0 /* not used anywhere */
 226 const char *DF_get_attribute_desc(A_Type_t index) {
     /* [<][>][^][v][top][bottom][index][help] */
 227 /*
 228   return (char *)Attributes_details[attr_index][0];
 229 */
 230   return NULL;
 231 } /* DF_get_attribute_desc() */
 232 
 233 const char *DF_get_attribute_frmt(A_Type_t index) {
     /* [<][>][^][v][top][bottom][index][help] */
 234 /*
 235   return (char *)Attributes_details[attr_index][1];
 236 */
 237   return NULL;
 238 } /* DF_get_attribute_frmt() */
 239 
 240 /* DF_attributes_to_string() */
 241 /*++++++++++++++++++++++++++++++++++++++
 242   Returns a string of all the attributes.  Only there for debugging and tracing purposes.
 243 
 244   More:
 245   +html+ <PRE>
 246   Authors:
 247         ottrey
 248 
 249   +html+ </PRE><DL COMPACT>
 250   +html+ <DT>Online References:
 251   +html+ <DD><UL>
 252   +html+ </UL></DL>
 253 
 254   ++++++++++++++++++++++++++++++++++++++*/
 255 char *DF_attributes_to_string(void) {
     /* [<][>][^][v][top][bottom][index][help] */
 256   int i;
 257   char *str;
 258   char str_buffer[4096];
 259   unsigned str_len;
 260 
 261   strcpy(str_buffer, "{\"");
 262   for (i=0; Attribute_names[i] != NULL; i++) {
 263     strcat(str_buffer, Attribute_names[i]);
 264     strcat(str_buffer, "\", \"");
 265   }
 266   str_len = strlen(str_buffer);
 267   str_buffer[str_len-3] = '}';
 268   str_buffer[str_len-2] = '\0';
 269   str_len--;
 270 
 271   /* str = (char *)calloc(1, str_len); */
 272   /*dieif( wr_malloc((void **)&str, str_len ) != UT_OK);  
 273   strcpy(str, str_buffer); */
 274 
 275   return UT_strdup(str_buffer);
 276 
 277 } /* DF_attributes_to_string() */
 278 #endif /* not used */
 279 
 280 
 281 /* XXX This could be done MUCH more efficiently (with a hash) */
 282 A_Type_t DF_attribute_code2type(const gchar *token) {
     /* [<][>][^][v][top][bottom][index][help] */
 283   A_Type_t result=-1;
 284 
 285   int i;
 286   for (i=0; Attribute_aliases[i] != NULL; i++) {
 287     if (strcmp(Attribute_aliases[i], token) == 0) {
 288       result = Attribute_aliases_map[i];
 289       break;
 290     }
 291   }
 292 
 293   return result;
 294 } /* DF_attribute_code2type() */
 295 
 296 /* 
 297 Description:
 298 
 299   Find the type identifier for the given long attribute name.  This can 
 300   be used to get the attribute code via the DF_get_attribute_code()
 301   function.
 302  
 303 Arguments:
 304 
 305   const gchar *token; attribute name, e.g. "person", "aut-num", or "limerick"
 306 
 307 Returns:
 308  
 309   A_Type_t with the attribute's code, or -1 on error (bad attribute name).
 310 
 311 Notes:
 312  
 313   Uses a hash table for speedy conversion.  The first time this is called, 
 314   the hash table will be built.  Subsequent calls use that table.
 315    
 316   It might be better to provide a single function to translate from an 
 317   attribute name to the attribute code, but for now, just use 
 318   DF_get_attribute_code() with the value returned here.  - SK
 319 */
 320 static GHashTable *name2type_hash = NULL;
 321 
 322 static void init_name2type_hash()
     /* [<][>][^][v][top][bottom][index][help] */
 323 {
 324     A_Type_t *val;
 325     int i;
 326 
 327     name2type_hash = g_hash_table_new(g_str_hash, g_str_equal);
 328     for (i=0; Attribute_aliases[i] != NULL; i++) {
 329         /*wr_malloc((void *)&val, sizeof(A_Type_t));*/
 330         val = UT_malloc(sizeof(A_Type_t));
 331         *val = Attribute_aliases_map[i];
 332         g_hash_table_insert(name2type_hash, Attribute_aliases[i], val);
 333     }
 334 }
 335 
 336 A_Type_t DF_attribute_name2type (const gchar *token) 
     /* [<][>][^][v][top][bottom][index][help] */
 337 {
 338     static pthread_once_t once_control = PTHREAD_ONCE_INIT;
 339     A_Type_t *result;
 340 
 341     /* build table on first call */
 342     pthread_once(&once_control, init_name2type_hash);
 343 
 344     /* find the type in our has table, returning if found */
 345     result = g_hash_table_lookup(name2type_hash, token);
 346     if (result != NULL) {
 347         return *result;
 348     } else {
 349         return -1;
 350     }
 351 } /* DF_attribute_name2type() */
 352 
 353 /* XXX This could be done MUCH more efficiently (with a hash) */
 354 C_Type_t DF_class_code2type(const gchar *token) {
     /* [<][>][^][v][top][bottom][index][help] */
 355   C_Type_t result=-1;
 356 
 357   int i;
 358   for (i=0; Class_aliases[i] != NULL; i++) {
 359     if (strcmp(Class_aliases[i], token) == 0) {
 360       result = Class_aliases_map[i];
 361       break;
 362     }
 363   }
 364 
 365   return result;
 366 } /* DF_class_code2type() */
 367 
 368 /* XXX This could be done MUCH more efficiently (with a hash) */
 369 C_Type_t DF_class_name2type(const gchar *token) {
     /* [<][>][^][v][top][bottom][index][help] */
 370   C_Type_t result=-1;
 371 
 372   int i;
 373   for (i=0; Class_aliases[i] != NULL; i++) {
 374     if (strcmp(Class_aliases[i], token) == 0) {
 375       result = Class_aliases_map[i];
 376       break;
 377     }
 378   }
 379 
 380   return result;
 381 } /* DF_class_name2type() */
 382 
 383 /* Returns class name for a given type */
 384 const char *DF_class_type2name(C_Type_t class) {
     /* [<][>][^][v][top][bottom][index][help] */
 385         return(Class_names[class]);
 386 }/* DF_class_type2name() */
 387 
 388 
 389 /* check in the queries if this attribute can trigger a radix lookup */
 390 int DF_attrcode_has_radix_lookup(A_Type_t attr) 
     /* [<][>][^][v][top][bottom][index][help] */
 391 {
 392   int i;
 393   
 394   for (i=0; Query[i].query != NULL; i++) {
 395     if( Query[i].refer == R_RADIX &&
 396         Query[i].attribute == attr ) {
 397       return 1;
 398     }
 399   }
 400   return 0;
 401 }
 402 
 403 /* return the sql query to load the radix ipv4 tree for this attribute
 404    or NULL if no ipv4 radix is used for this attribute */
 405 char * DF_attrcode_radix_load_v4(A_Type_t attr) 
     /* [<][>][^][v][top][bottom][index][help] */
 406 {
 407     int i;
 408     
 409     for(i=0; 
 410         DF_radix_load[i].attr != -1 && DF_radix_load[i].family != -1;   
 411         i++) {
 412         
 413         if( DF_radix_load[i].attr == attr ) {
 414             return DF_radix_load[i].ipv4_load;
 415         }
 416     }
 417     return NULL;
 418 }
 419 
 420 /* return the sql query to load the radix ipv4 tree for this attribute
 421    or NULL if no ipv4 radix is used for this attribute */
 422 char * DF_attrcode_radix_load_v6(A_Type_t attr) 
     /* [<][>][^][v][top][bottom][index][help] */
 423 {
 424     int i;
 425     
 426     for(i=0; 
 427         DF_radix_load[i].attr != -1 && DF_radix_load[i].family != -1;   
 428         i++) {
 429         
 430         if( DF_radix_load[i].attr == attr ) {
 431             return DF_radix_load[i].ipv6_load;
 432         }
 433     }
 434     return NULL;
 435 }
 436 
 437 /* return the family of the radix tree(s) used for this attribute
 438    or -1 if no radix is used for this attribute */
 439 rx_fam_t DF_attrcode_radix_family(A_Type_t attr) {
     /* [<][>][^][v][top][bottom][index][help] */
 440     int i;
 441     
 442     for(i=0; 
 443         DF_radix_load[i].attr != -1 && DF_radix_load[i].family != -1;   
 444         i++) {
 445         
 446         if( DF_radix_load[i].attr == attr ) {
 447             return DF_radix_load[i].family;
 448         }
 449     }
 450     return -1;
 451 }

/* [<][>][^][v][top][bottom][index][help] */