/[radius]/radius/lib/dict.c
ViewVC logotype

Diff of /radius/lib/dict.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.8 by gray, Mon Aug 5 08:01:31 2002 UTC revision 1.9 by gray, Tue Apr 29 13:57:59 2003 UTC
# Line 1  Line 1 
1  /* This file is part of GNU RADIUS.  /* This file is part of GNU Radius.
2     Copyright (C) 2000,2001, Sergey Poznyakoff     Copyright (C) 2000,2001,2002,2003 Sergey Poznyakoff
3    
4     This program is free software; you can redistribute it and/or modify     GNU Radius is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.     (at your option) any later version.
8        
9     This program is distributed in the hope that it will be useful,     GNU Radius is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.     GNU General Public License for more details.
13        
14     You should have received a copy of the GNU General Public License     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software Foundation,     along with GNU Radius; if not, write to the Free Software Foundation,
16     Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */     Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17    
 #ifndef lint  
 static char rcsid[] =  
 "$Id$";  
 #endif  
   
18  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
19  # include <config.h>  # include <config.h>
20  #endif  #endif
# Line 32  static char rcsid[] = Line 27  static char rcsid[] =
27    
28  #include <radius.h>  #include <radius.h>
29  #include <radpaths.h>  #include <radpaths.h>
30  #include <slist.h>  #include <list.h>
31  #include <symtab.h>  #include <symtab.h>
32    
33  #ifndef DICT_INDEX_SIZE  #ifndef DICT_INDEX_SIZE
# Line 41  static char rcsid[] = Line 36  static char rcsid[] =
36    
37  static Symtab    *dict_attr_tab;  static Symtab    *dict_attr_tab;
38  static DICT_ATTR *dict_attr_index[DICT_INDEX_SIZE];  static DICT_ATTR *dict_attr_index[DICT_INDEX_SIZE];
39  static DICT_VALUE  *dictionary_values;  static LIST /* of DICT_VALUE */ *dictionary_values;
40  static DICT_VENDOR *dictionary_vendors;  static LIST /* of DICT_VENDOR */ *dictionary_vendors;
41  static int         vendorno;  static int         vendorno;
42    
 int nfields(int  fc, int  minf, int  maxfm, char *file, int  lineno);  
   
43  /* ************************************************************************ */  /* ************************************************************************ */
44    
45  static void  static int
46  free_vendor(vp)  free_vendor(void *ptr, void *closure ARG_UNUSED)
         DICT_VENDOR *vp;  
47  {  {
48            DICT_VENDOR *vp = ptr;
49          if (vp->vendorname)          if (vp->vendorname)
50                  efree(vp->vendorname);                  efree(vp->vendorname);
51            efree(vp);
52            return 0;
53  }  }
54    
55  static void  static int
56  free_value(vp)  free_value(void *ptr, void *closure ARG_UNUSED)
         DICT_VALUE *vp;  
57  {  {
58            DICT_VALUE *vp = ptr;
59          efree(vp->name);          efree(vp->name);
60            efree(vp);
61            return 0;
62  }  }
63    
64  void  void
# Line 73  dict_free() Line 70  dict_free()
70                  dict_attr_tab = symtab_create(sizeof(DICT_ATTR), NULL);                  dict_attr_tab = symtab_create(sizeof(DICT_ATTR), NULL);
71          memset(dict_attr_index, 0, sizeof dict_attr_index);          memset(dict_attr_index, 0, sizeof dict_attr_index);
72    
73          free_slist((struct slist*)dictionary_values, free_value);          list_destroy(&dictionary_values, free_value, NULL);
74          dictionary_values = NULL;          list_destroy(&dictionary_vendors, free_vendor, NULL);
           
         free_slist((struct slist*)dictionary_vendors, free_vendor);  
   
         dictionary_vendors = NULL;  
75          vendorno = 1;          vendorno = 1;
76  }  }
77    
78  int  static int
79  nfields(fc, minf, maxf, file, lineno)  nfields(int fc, int minf, int maxf, char *file, int lineno)
         int  fc;  
         int  minf;  
         int  maxf;  
         char *file;  
         int  lineno;  
80  {  {
81          if (fc < minf) {          if (fc < minf) {
82                  radlog(L_ERR,                  radlog(L_ERR, "%s:%d: %s", file, lineno, _("too few fields"));
                        "%s:%d: %s",  
                        file, lineno,  
                        _("too few fields"));  
83                  return -1;                  return -1;
84          } else if (fc > maxf) {          } else if (fc > maxf) {
85                  radlog(L_ERR,                  radlog(L_ERR, "%s:%d: %s", file, lineno, _("too many fields"));
                        "%s:%d: %s",  
                        file, lineno,  
                        _("too many fields"));  
86                  return -1;                  return -1;
87          }          }
88          return 0;          return 0;
# Line 110  nfields(fc, minf, maxf, file, lineno) Line 92  nfields(fc, minf, maxf, file, lineno)
92   *      Add vendor to the list.   *      Add vendor to the list.
93   */   */
94  int  int
95  addvendor(name, value)  addvendor(char *name, int value)
         char *name;  
         int value;  
96  {  {
97          DICT_VENDOR *vval;          DICT_VENDOR *vval;
98    
99          vval = mem_alloc(sizeof(DICT_VENDOR));          vval = emalloc(sizeof(DICT_VENDOR));
100                    
101          vval->vendorname = estrdup(name);          vval->vendorname = estrdup(name);
102          vval->vendorpec  = value;          vval->vendorpec  = value;
103          vval->vendorcode = vendorno++;          vval->vendorcode = vendorno++;
104            if (!dictionary_vendors)
105          /* Insert at front. */                  dictionary_vendors = list_create();
106          vval->next = dictionary_vendors;          list_prepend(dictionary_vendors, vval);
         dictionary_vendors = vval;  
107    
108          return 0;          return 0;
109  }  }
# Line 143  static ATTR_PARSER_TAB *attr_parser_tab; Line 122  static ATTR_PARSER_TAB *attr_parser_tab;
122  static attr_parser_fp dict_find_parser(int);  static attr_parser_fp dict_find_parser(int);
123    
124  attr_parser_fp  attr_parser_fp
125  dict_find_parser(attr)  dict_find_parser(int attr)
126  {  {
127          ATTR_PARSER_TAB *ep;          ATTR_PARSER_TAB *ep;
128          for (ep = attr_parser_tab; ep; ep = ep->next)          for (ep = attr_parser_tab; ep; ep = ep->next)
# Line 153  dict_find_parser(attr) Line 132  dict_find_parser(attr)
132  }  }
133    
134  void  void
135  dict_register_parser(attr, fun)  dict_register_parser(int attr, attr_parser_fp fun)
         int attr;  
         attr_parser_fp fun;  
136  {  {
137          ATTR_PARSER_TAB *e = mem_alloc(sizeof(*e));          ATTR_PARSER_TAB *e = mem_alloc(sizeof(*e));
138          e->attr = attr;          e->attr = attr;
# Line 198  static struct keyword type_kw[] = { Line 175  static struct keyword type_kw[] = {
175    
176  /*ARGSUSED*/  /*ARGSUSED*/
177  int  int
178  _dict_include(errcnt, fc, fv, file, lineno)  _dict_include(int *errcnt, int fc, char **fv, char *file, int lineno)
         int    *errcnt;  
         int     fc;  
         char    **fv;  
         char    *file;  
         int     lineno;  
179  {  {
180          if (nfields(fc, 2, 2, file, lineno))          if (nfields(fc, 2, 2, file, lineno))
181                  return 0;                  return 0;
# Line 212  _dict_include(errcnt, fc, fv, file, line Line 184  _dict_include(errcnt, fc, fv, file, line
184  }  }
185    
186  int  int
187  parse_flags(ptr, flags, filename, line)  parse_flags(char **ptr, int *flags, char *filename, int line)
         char **ptr;  
         int *flags;  
         char *filename;  
         int line;  
188  {  {
189          int i;          int i;
190          char *p;          char *p;
# Line 267  parse_flags(ptr, flags, filename, line) Line 235  parse_flags(ptr, flags, filename, line)
235  }  }
236    
237  int  int
238  _dict_attribute(errcnt, fc, fv, file, lineno)  _dict_attribute(int *errcnt, int fc, char **fv, char *file, int lineno)
         int    *errcnt;  
         int     fc;  
         char    **fv;  
         char    *file;  
         int     lineno;  
239  {  {
240          DICT_ATTR *attr;          DICT_ATTR *attr;
241          int type;          int type;
# Line 406  _dict_attribute(errcnt, fc, fv, file, li Line 369  _dict_attribute(errcnt, fc, fv, file, li
369  }  }
370    
371  int  int
372  _dict_value(errcnt, fc, fv, file, lineno)  _dict_value(int *errcnt, int fc, char **fv, char *file, int lineno)
         int    *errcnt;  
         int     fc;  
         char    **fv;  
         char    *file;  
         int     lineno;  
373  {  {
374          DICT_VALUE *dval;          DICT_VALUE *dval;
375          DICT_ATTR *attr;          DICT_ATTR *attr;
# Line 433  _dict_value(errcnt, fc, fv, file, lineno Line 391  _dict_value(errcnt, fc, fv, file, lineno
391          attr = sym_lookup_or_install(dict_attr_tab, VALUE_ATTR, 1);          attr = sym_lookup_or_install(dict_attr_tab, VALUE_ATTR, 1);
392                    
393          /* Create a new VALUE entry for the list */          /* Create a new VALUE entry for the list */
394          dval = mem_alloc(sizeof(DICT_VALUE));          dval = emalloc(sizeof(DICT_VALUE));
395                                                    
396          dval->name = estrdup(VALUE_NAME);          dval->name = estrdup(VALUE_NAME);
397          dval->attr = attr;          dval->attr = attr;
398          dval->value = value;          dval->value = value;
399    
400          /* Insert at front. */          /* Insert at front. */
401          dval->next = dictionary_values;          if (!dictionary_values)
402          dictionary_values = dval;                  dictionary_values = list_create();
403            list_prepend(dictionary_values, dval);
404                    
405          return 0;          return 0;
406  }  }
407    
408  int  int
409  _dict_vendor(errcnt, fc, fv, file, lineno)  _dict_vendor(int *errcnt, int fc, char **fv, char *file, int lineno)
         int    *errcnt;  
         int     fc;  
         char    **fv;  
         char    *file;  
         int     lineno;  
410  {  {
411          int             value;          int value;
412          char            *p;          char *p;
413    
414          if (nfields(fc, 3, 3, file, lineno))          if (nfields(fc, 3, 3, file, lineno))
415                  return 0;                  return 0;
# Line 493  static struct keyword dict_kw[] = { Line 447  static struct keyword dict_kw[] = {
447  };  };
448    
449  int  int
450  parse_dict_entry(errcnt, fc, fv, file, lineno)  parse_dict_entry(int *errcnt, int fc, char **fv, char *file, int lineno)
         int    *errcnt;  
         int     fc;  
         char    **fv;  
         char    *file;  
         int     lineno;  
451  {  {
452          switch (xlat_keyword(dict_kw, KEYWORD, -1)) {          switch (xlat_keyword(dict_kw, KEYWORD, -1)) {
453          case KW_INCLUDE:          case KW_INCLUDE:
# Line 524  parse_dict_entry(errcnt, fc, fv, file, l Line 473  parse_dict_entry(errcnt, fc, fv, file, l
473  }  }
474    
475  int  int
476  parse_dict(name)  parse_dict(char *name)
         char *name;  
477  {  {
478          char *path;          char *path;
479          int   rc;          int   rc;
# Line 568  struct attr_value { Line 516  struct attr_value {
516  };  };
517    
518  int  int
519  attrval_cmp(av, attr)  attrval_cmp(struct attr_value *av, DICT_ATTR *attr)
         struct attr_value *av;  
         DICT_ATTR *attr;  
520  {  {
521          if (attr->value == av->value) {          if (attr->value == av->value) {
522                  av->da = attr;                  av->da = attr;
# Line 580  attrval_cmp(av, attr) Line 526  attrval_cmp(av, attr)
526  }  }
527    
528  DICT_ATTR *  DICT_ATTR *
529  attr_number_to_dict(attribute)  attr_number_to_dict(int attribute)
         int     attribute;  
530  {  {
531          struct attr_value av;          struct attr_value av;
532          if (attribute < DICT_INDEX_SIZE)          if (attribute < DICT_INDEX_SIZE)
# Line 597  attr_number_to_dict(attribute) Line 542  attr_number_to_dict(attribute)
542   */   */
543    
544  DICT_ATTR *  DICT_ATTR *
545  attr_name_to_dict(attrname)  attr_name_to_dict(char *attrname)
         char    *attrname;  
546  {  {
547          return sym_lookup(dict_attr_tab, attrname);          return sym_lookup(dict_attr_tab, attrname);
548  }  }
# Line 612  struct val_lookup { Line 556  struct val_lookup {
556          int number;          int number;
557  };  };
558    
559  int  static int
560  valname_cmp(v, d)  valname_cmp(const void *item, const void *data)
         DICT_VALUE *v;  
         struct val_lookup *d;  
561  {  {
562          return !(d->number == v->attr->value          const DICT_VALUE *v = item;
563                   && strcmp(v->name, d->name) == 0);          const struct val_lookup *d = data;
564            if (d->number == v->attr->value && strcmp(v->name, d->name) == 0)
565                    return 0;
566            return 1;
567  }  }
568    
569  DICT_VALUE *  DICT_VALUE *
570  value_name_to_value(valname, attr)  value_name_to_value(char *valname, int attr)
         char    *valname;  
         int attr;  
571  {  {
572          struct val_lookup data;          struct val_lookup data;
573          data.name = valname;          data.name = valname;
574          data.number = attr;          data.number = attr;
575          return (DICT_VALUE *)find_slist((struct slist*) dictionary_values,          return list_locate(dictionary_values, &data, valname_cmp);
                                         valname_cmp,  
                                         &data);  
576  }  }
577    
578  /*  /*
# Line 639  value_name_to_value(valname, attr) Line 580  value_name_to_value(valname, attr)
580   * the associated attribute name.   * the associated attribute name.
581   */   */
582  int  int
583  valnum_cmp(v, d)  valnum_cmp(const void *item, const void *data)
         DICT_VALUE *v;  
         struct val_lookup *d;  
584  {  {
585          return !(strcmp(d->attrname, v->attr->name) == 0          const DICT_VALUE *v = item;
586                   && d->number == v->value);          const struct val_lookup *d = data;
587    
588            if (strcmp(d->attrname, v->attr->name) == 0 && d->number == v->value)
589                    return 0;
590            return 1;
591  }  }
592    
593  DICT_VALUE *  DICT_VALUE *
594  value_lookup(value, attrname)  value_lookup(UINT4 value, char *attrname)
         UINT4   value;  
         char    *attrname;  
595  {  {
596          struct val_lookup data;          struct val_lookup data;
597          data.number = value;          data.number = value;
598          data.attrname = attrname;          data.attrname = attrname;
599          return (DICT_VALUE *)find_slist((struct slist*) dictionary_values,          return list_locate(dictionary_values, &data, valnum_cmp);
                                         valnum_cmp,  
                                         &data);  
600  }  }
601    
602  /*  /*
# Line 665  value_lookup(value, attrname) Line 604  value_lookup(value, attrname)
604   * based on it's internal number.   * based on it's internal number.
605   */   */
606  int  int
607  code_cmp(v, code)  code_cmp(const void *item, const void *data)
         DICT_VENDOR *v;  
         int *code;  
608  {  {
609          return v->vendorcode - *code;          const DICT_VENDOR *v = item;
610            const int *code = data;
611    
612            return v->vendorcode != *code;
613  }  }
614    
615  int  int
616  vendor_id_to_pec(code)  vendor_id_to_pec(int code)
         int code;  
617  {  {
618          DICT_VENDOR *vp;          DICT_VENDOR *vp;
619    
620          vp = (DICT_VENDOR*)find_slist((struct slist*) dictionary_vendors,          vp = list_locate(dictionary_vendors, &code, code_cmp);
                                       code_cmp,  
                                       &code);  
621          return vp ? vp->vendorpec : 0;          return vp ? vp->vendorpec : 0;
622  }  }
623    
# Line 688  vendor_id_to_pec(code) Line 625  vendor_id_to_pec(code)
625   * Get the internal code of the vendor based on its PEC.   * Get the internal code of the vendor based on its PEC.
626   */   */
627  int  int
628  pec_cmp(v, pec)  pec_cmp(const void *item, const void *data)
         DICT_VENDOR *v;  
         int *pec;  
629  {  {
630          return v->vendorpec - *pec;          const DICT_VENDOR *v = item;
631            const int *pec = data;
632    
633            return v->vendorpec != *pec;
634  }  }
635    
636  int  int
637  vendor_pec_to_id(pec)  vendor_pec_to_id(int pec)
         int pec;  
638  {  {
639          DICT_VENDOR *vp;          DICT_VENDOR *vp;
640    
641          vp = (DICT_VENDOR*)find_slist((struct slist*) dictionary_vendors,          vp = list_locate(dictionary_vendors, &pec, pec_cmp);
                                       pec_cmp,  
                                       &pec);  
642          return vp ? vp->vendorcode : 0;          return vp ? vp->vendorcode : 0;
643  }  }
644                    
645  char *  char *
646  vendor_pec_to_name(pec)  vendor_pec_to_name(int pec)
         int pec;  
647  {  {
648          DICT_VENDOR *vp;          DICT_VENDOR *vp;
649    
650          vp = (DICT_VENDOR*)find_slist((struct slist*) dictionary_vendors,          vp = list_locate(dictionary_vendors, &pec, pec_cmp);
                                       pec_cmp,  
                                       &pec);  
651          return vp ? vp->vendorname : NULL;          return vp ? vp->vendorname : NULL;
652  }  }
653                    
# Line 724  vendor_pec_to_name(pec) Line 656  vendor_pec_to_name(pec)
656   * Get the internal code of the vendor based on its name.   * Get the internal code of the vendor based on its name.
657   */   */
658  int  int
659  vendor_cmp(v, s)  vendor_cmp(const void *item, const void *data)
         DICT_VENDOR *v;  
         char        *s;  
660  {  {
661            const DICT_VENDOR *v = item;
662            const char *s = data;
663    
664          return strcmp(v->vendorname, s);          return strcmp(v->vendorname, s);
665  }  }
666    
667  int  int
668  vendor_name_to_id(name)  vendor_name_to_id(char *name)
         char *name;  
669  {  {
670          DICT_VENDOR *vp;          DICT_VENDOR *vp;
671    
672          vp = (DICT_VENDOR*)find_slist((struct slist*) dictionary_vendors,          vp = list_locate(dictionary_vendors, name, vendor_cmp);
                                       vendor_cmp,  
                                       name);  
673          return vp ? vp->vendorcode : 0;          return vp ? vp->vendorcode : 0;
674  }  }
675                    

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.9

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26