modules_k/auth_db/aaa_avps.h
31ccf6a2
 /*
  * $Id$
  *
  * Copyright (C) 2005 Voice Sistem SRL
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
31ccf6a2
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
31ccf6a2
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
27642a08
  * Kamailio is distributed in the hope that it will be useful,
31ccf6a2
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * History:
  * --------
  *  2005-05-31 created by bogdan
  */
 
cdece58c
 #ifndef _OPENSER_AAA_AVPS_H_
 #define _OPENSER_AAA_AVPS_H_
31ccf6a2
 
 #include "../../mem/mem.h"
 #include "../../dprint.h"
 #include "../../trim.h"
b0a7f212
 #include "../../pvar.h"
31ccf6a2
 
 #include <string.h>
 
 
 struct aaa_avp {
 	int_str avp_name;
cdece58c
 	unsigned short avp_type;
31ccf6a2
 	str attr_name;
 	struct aaa_avp *next;
 };
 
 
 
 static inline void free_aaa_avp(struct aaa_avp *avp)
 {
 	if (avp) {
2e017606
 		if (avp->avp_type&AVP_NAME_STR && avp->avp_name.s.s!=avp->attr_name.s)
 			pkg_free(avp->avp_name.s.s);
31ccf6a2
 		if (avp->attr_name.s)
 			pkg_free(avp->attr_name.s);
 		pkg_free(avp);
 	}
 }
 
 
 
10cf96be
 static inline void free_aaa_avp_list(struct aaa_avp *avp)
31ccf6a2
 {
 	struct aaa_avp *tmp;
 
 	while (avp) {
 		tmp = avp;
 		avp = avp->next;
 		free_aaa_avp( tmp );
 	}
 }
 
 
 
 static inline int parse_aaa_avps(char *definition,
 										struct aaa_avp **avp_def, int *cnt)
 {
 	struct aaa_avp *avp;
 	int_str avp_name;
b0a7f212
 	pv_spec_t avp_spec;
31ccf6a2
 	str  foo;
 	char *p;
 	char *e;
 	char *s;
cdece58c
 	char t;
31ccf6a2
 
 	p = definition;
 	*avp_def = 0;
 	*cnt = 0;
 
 	if (p==0 || *p==0)
 		return 0;
 
 	/* get element by element */
 	while ( (e=strchr(p,';'))!=0 || (e=p+strlen(p))!=p ) {
 		/* new aaa_avp struct */
 		if ( (avp=(struct aaa_avp*)pkg_malloc(sizeof(struct aaa_avp)))==0 ) {
789504a6
 			LM_ERR("no more pkg mem\n");
31ccf6a2
 			goto error;
 		}
 		memset( avp, 0, sizeof(struct aaa_avp));
 		/* definition is between p and e */
 		if ( (s=strchr(p,'='))!=0 && s<e ) {
 			/* avp = attr */
 			foo.s = p;
 			foo.len = s-p;
 			trim( &foo );
 			if (foo.len==0)
 				goto parse_error;
cdece58c
 			t = foo.s[foo.len];
 			foo.s[foo.len] = '\0';
 			
b0a7f212
 			if (pv_parse_spec(&foo, &avp_spec)==0
 					|| avp_spec.type!=PVT_AVP) {
789504a6
 				LM_ERR("malformed or non AVP %s AVP definition\n", foo.s);
cdece58c
 				goto parse_error;;
 			}
 
b0a7f212
 			if(pv_get_avp_name(0, &(avp_spec.pvp), &avp_name,
 						&avp->avp_type)!=0)
cdece58c
 			{
789504a6
 				LM_ERR("[%s]- invalid AVP definition\n", foo.s);
31ccf6a2
 				goto parse_error;
cdece58c
 			}
 			foo.s[foo.len] = t;
 
31ccf6a2
 			/* copy the avp name into the avp structure */
 			if (avp->avp_type&AVP_NAME_STR) {
2e017606
 				avp->avp_name.s.s =
 					(char*)pkg_malloc(avp_name.s.len+1);
 				if (avp->avp_name.s.s==0) {
789504a6
 					LM_ERR("no more pkg mem\n");
31ccf6a2
 					goto error;
 				}
2e017606
 				avp->avp_name.s.len = avp_name.s.len;
 				memcpy(avp->avp_name.s.s, avp_name.s.s, avp_name.s.len);
 				avp->avp_name.s.s[avp->avp_name.s.len] = 0;
31ccf6a2
 			} else {
 				avp->avp_name.n = avp_name.n;
 			}
 			/* go to after the equal sign */
 			p = s+1;
 		}
9a39e5fd
 		/* attr - is between p and e*/
31ccf6a2
 		foo.s = p;
 		foo.len = e-p;
 		trim( &foo );
 		if (foo.len==0)
 			goto parse_error;
 		/* copy the attr into the avp structure */
 		avp->attr_name.s = (char*)pkg_malloc( foo.len+1 );
 		if (avp->attr_name.s==0) {
789504a6
 			LM_ERR("no more pkg mem\n");
31ccf6a2
 			goto error;
 		}
 		avp->attr_name.len = foo.len;
 		memcpy( avp->attr_name.s, foo.s, foo.len );
 		avp->attr_name.s[foo.len] = 0;
 		/* was an avp name specified? */
2e017606
 		if (avp->avp_name.s.s==0) {
31ccf6a2
 			/* use as avp_name the attr name */
 			avp->avp_type = AVP_NAME_STR;
2e017606
 			avp->avp_name.s = avp->attr_name;
31ccf6a2
 		}
 		/* link the element */
 		avp->next = *avp_def;
 		*avp_def = avp;
 		(*cnt)++;
 		avp = 0;
 		/* go to the end */
 		p = e;
 		if (*p==';')
 			p++;
 		if (*p==0)
 			break;
 	}
 
 	return 0;
 parse_error:
789504a6
 	LM_ERR("parse failed in \"%s\" at pos %d(%s)\n",
07af0af6
 		definition, (int)(long)(p-definition),p);
31ccf6a2
 error:
 	free_aaa_avp( avp );
 	free_aaa_avp_list( *avp_def );
 	*avp_def = 0;
 	*cnt = 0;
 	return -1;
 }
 
 #endif