aaa_avps.h

Go to the documentation of this file.
00001 /*
00002  * $Id: aaa_avps.h 4518 2008-07-28 15:39:28Z henningw $
00003  *
00004  * Copyright (C) 2005 Voice Sistem SRL
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * History:
00023  * --------
00024  *  2005-05-31 created by bogdan
00025  */
00026 
00027 #ifndef _OPENSER_AAA_AVPS_H_
00028 #define _OPENSER_AAA_AVPS_H_
00029 
00030 #include "../../mem/mem.h"
00031 #include "../../dprint.h"
00032 #include "../../trim.h"
00033 #include "../../pvar.h"
00034 
00035 #include <string.h>
00036 
00037 
00038 struct aaa_avp {
00039    int_str avp_name;
00040    unsigned short avp_type;
00041    str attr_name;
00042    struct aaa_avp *next;
00043 };
00044 
00045 
00046 
00047 static inline void free_aaa_avp(struct aaa_avp *avp)
00048 {
00049    if (avp) {
00050       if (avp->avp_type&AVP_NAME_STR && avp->avp_name.s.s!=avp->attr_name.s)
00051          pkg_free(avp->avp_name.s.s);
00052       if (avp->attr_name.s)
00053          pkg_free(avp->attr_name.s);
00054       pkg_free(avp);
00055    }
00056 }
00057 
00058 
00059 
00060 static inline void free_aaa_avp_list(struct aaa_avp *avp)
00061 {
00062    struct aaa_avp *tmp;
00063 
00064    while (avp) {
00065       tmp = avp;
00066       avp = avp->next;
00067       free_aaa_avp( tmp );
00068    }
00069 }
00070 
00071 
00072 
00073 static inline int parse_aaa_avps(char *definition,
00074                               struct aaa_avp **avp_def, int *cnt)
00075 {
00076    struct aaa_avp *avp;
00077    int_str avp_name;
00078    pv_spec_t avp_spec;
00079    str  foo;
00080    char *p;
00081    char *e;
00082    char *s;
00083    char t;
00084 
00085    p = definition;
00086    *avp_def = 0;
00087    *cnt = 0;
00088 
00089    if (p==0 || *p==0)
00090       return 0;
00091 
00092    /* get element by element */
00093    while ( (e=strchr(p,';'))!=0 || (e=p+strlen(p))!=p ) {
00094       /* new aaa_avp struct */
00095       if ( (avp=(struct aaa_avp*)pkg_malloc(sizeof(struct aaa_avp)))==0 ) {
00096          LM_ERR("no more pkg mem\n");
00097          goto error;
00098       }
00099       memset( avp, 0, sizeof(struct aaa_avp));
00100       /* definition is between p and e */
00101       if ( (s=strchr(p,'='))!=0 && s<e ) {
00102          /* avp = attr */
00103          foo.s = p;
00104          foo.len = s-p;
00105          trim( &foo );
00106          if (foo.len==0)
00107             goto parse_error;
00108          t = foo.s[foo.len];
00109          foo.s[foo.len] = '\0';
00110          
00111          if (pv_parse_spec(&foo, &avp_spec)==0
00112                || avp_spec.type!=PVT_AVP) {
00113             LM_ERR("malformed or non AVP %s AVP definition\n", foo.s);
00114             goto parse_error;;
00115          }
00116 
00117          if(pv_get_avp_name(0, &(avp_spec.pvp), &avp_name,
00118                   &avp->avp_type)!=0)
00119          {
00120             LM_ERR("[%s]- invalid AVP definition\n", foo.s);
00121             goto parse_error;
00122          }
00123          foo.s[foo.len] = t;
00124 
00125          /* copy the avp name into the avp structure */
00126          if (avp->avp_type&AVP_NAME_STR) {
00127             avp->avp_name.s.s =
00128                (char*)pkg_malloc(avp_name.s.len+1);
00129             if (avp->avp_name.s.s==0) {
00130                LM_ERR("no more pkg mem\n");
00131                goto error;
00132             }
00133             avp->avp_name.s.len = avp_name.s.len;
00134             memcpy(avp->avp_name.s.s, avp_name.s.s, avp_name.s.len);
00135             avp->avp_name.s.s[avp->avp_name.s.len] = 0;
00136          } else {
00137             avp->avp_name.n = avp_name.n;
00138          }
00139          /* go to after the equal sign */
00140          p = s+1;
00141       }
00142       /* attr - is between p and e*/
00143       foo.s = p;
00144       foo.len = e-p;
00145       trim( &foo );
00146       if (foo.len==0)
00147          goto parse_error;
00148       /* copy the attr into the avp structure */
00149       avp->attr_name.s = (char*)pkg_malloc( foo.len+1 );
00150       if (avp->attr_name.s==0) {
00151          LM_ERR("no more pkg mem\n");
00152          goto error;
00153       }
00154       avp->attr_name.len = foo.len;
00155       memcpy( avp->attr_name.s, foo.s, foo.len );
00156       avp->attr_name.s[foo.len] = 0;
00157       /* was an avp name specified? */
00158       if (avp->avp_name.s.s==0) {
00159          /* use as avp_name the attr name */
00160          avp->avp_type = AVP_NAME_STR;
00161          avp->avp_name.s = avp->attr_name;
00162       }
00163       /* link the element */
00164       avp->next = *avp_def;
00165       *avp_def = avp;
00166       (*cnt)++;
00167       avp = 0;
00168       /* go to the end */
00169       p = e;
00170       if (*p==';')
00171          p++;
00172       if (*p==0)
00173          break;
00174    }
00175 
00176    return 0;
00177 parse_error:
00178    LM_ERR("parse failed in \"%s\" at pos %d(%s)\n",
00179       definition, (int)(long)(p-definition),p);
00180 error:
00181    free_aaa_avp( avp );
00182    free_aaa_avp_list( *avp_def );
00183    *avp_def = 0;
00184    *cnt = 0;
00185    return -1;
00186 }
00187 
00188 #endif

Generated on Thu May 17 10:00:23 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6