aaa_avps.h

00001 /*
00002  * $Id$
00003  *
00004  * Common functions for Digest Authentication and Accounting Modules
00005  *
00006  * Copyright (C) 2001-2004 FhG Fokus
00007  *
00008  * This file is part of ser, a free SIP server.
00009  *
00010  * ser is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * For a license to use the ser software under conditions
00016  * other than those described here, or to purchase support for this
00017  * software, please contact iptel.org by e-mail at the following addresses:
00018  *    info@iptel.org
00019  *
00020  * ser is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License
00026  * along with this program; if not, write to the Free Software
00027  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028  *
00029  */
00030 
00031 #ifndef _SER_AAA_AVPS_H_
00032 #define _SER_AAA_AVPS_H_
00033 
00034 #include "../../mem/mem.h"
00035 #include "../../parser/parser_f.h"
00036 #include "../../dprint.h"
00037 
00038 #include <string.h>
00039 
00040 /*
00041  * Parse list of tokens separated by some char and put each token
00042  * into result array. Caller frees result array!
00043  */
00044 static inline int
00045 parse_token_list(char *p, char *pend, char separator, str **result)
00046 {
00047         int i;
00048 
00049         i = 0;
00050         *result = NULL;
00051         while ((pend - p) > 0) {
00052                 *result = pkg_realloc(*result, sizeof(**result) * (i + 1));
00053                 if (*result == NULL)
00054                         return -1;
00055                 (*result)[i].s = p;
00056                 p = eat_token2_end(p, pend, separator) + 1;
00057                 (*result)[i].len = p - (*result)[i].s - 1;
00058                 i++;
00059         }
00060         return i;
00061 }
00062 
00063 
00064 /*
00065  * Parse the list of AVP names separated by '|' into an array
00066  * of names, each element of the array is str string
00067  */
00068 static int
00069 aaa_avps_init(str *avp_list, str **parsed_avps, int *avps_n)
00070 {
00071         int errcode, i;
00072         char *cp;
00073 
00074         if (!avp_list->s || !avp_list->len) {
00075                      /* AVPs disabled, nothing to do */
00076                 *avps_n = 0;
00077                 return 1;
00078         }
00079 
00080         cp = pkg_malloc(avp_list->len + 1);
00081         if (cp == NULL) {
00082                 LOG(L_ERR, "aaa_avps::aaa_avps_init(): can't allocate memory\n");
00083                 errcode = -1;
00084                 goto bad;
00085         }
00086         memcpy(cp, avp_list->s, avp_list->len);
00087         *avps_n = parse_token_list(cp, cp + avp_list->len, '|', parsed_avps);
00088         if (*avps_n == -1) {
00089                 LOG(L_ERR, "aaa_avps::aaa_avps_init(): can't parse avps_column_int "
00090                     "parameter\n");
00091                 errcode = -2;
00092                 pkg_free(cp);
00093                 goto bad;
00094         }
00095 
00096         for (i = 0; i < *avps_n; i++) {
00097                 (*parsed_avps)[i].s[(*parsed_avps)[i].len] = '\0';
00098         }
00099 
00100         return 0;
00101 bad:
00102         if (*parsed_avps != NULL) {
00103                 pkg_free((*parsed_avps)[0].s);
00104                 pkg_free(*parsed_avps);
00105         }
00106 
00107         return errcode;
00108 }
00109 
00110 #endif