misc_radius/extra.c

Go to the documentation of this file.
00001 /*
00002  * extra.c Handling of extra attributes (adapted from acc module)
00003  *
00004  * Copyright (C) 2004-2006 Voice Sistem SRL
00005  * Copyright (C) 2008 Juha Heinanen <jh@tutpro.com>
00006  *
00007  * This file is part of Kamailio, a free SIP server.
00008  *
00009  * Kamailio is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version
00013  *
00014  * Kamailio is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License 
00020  * along with this program; if not, write to the Free Software 
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  */
00024 
00025 #include <string.h>
00026 #include <ctype.h>
00027 #include "../../mem/mem.h"
00028 #include "../../ut.h"
00029 #include "extra.h"
00030 
00031 
00032 #define EQUAL '='
00033 #define SEPARATOR ';'
00034 
00035 
00036 /* here we copy the strings returned by int2str (which uses a static buffer) */
00037 static char int_buf[INT2STR_MAX_LEN*MAX_EXTRA];
00038 static char *static_detector = 0;
00039 
00040 
00041 /* Initialize extra engine */
00042 void init_extra_engine(void)
00043 {
00044    int i;
00045    /* ugly trick to get the address of the static buffer */
00046    static_detector = int2str( (unsigned long)3, &i) + i;
00047 }
00048 
00049 
00050 /*
00051  * Parse extra module parameter value to extra_attr list, where each
00052  * element contains name of attribute and pseudo variable specification.
00053  */
00054 struct extra_attr *parse_extra_str(char *extra_str)
00055 {
00056     struct extra_attr *head;
00057     struct extra_attr *tail;
00058     struct extra_attr *extra;
00059     char *foo;
00060     char *s;
00061     int  n;
00062     str stmp;
00063 
00064     n = 0;
00065     head = 0;
00066     extra = 0;
00067     tail = 0;
00068     s = extra_str;
00069 
00070     if (s==0) {
00071    LM_ERR("null string received\n");
00072    goto error;
00073     }
00074 
00075     while (*s) {
00076    /* skip white spaces */
00077    while (*s && isspace((int)*s))  s++;
00078    if (*s == 0) goto parse_error;
00079    if (n == MAX_EXTRA) {
00080        LM_ERR("too many extras -> please increase the internal buffer\n");
00081        goto error;
00082    }
00083    extra = (struct extra_attr*)pkg_malloc(sizeof(struct extra_attr));
00084    if (extra == 0) {
00085        LM_ERR("no more pkg memory\n");
00086        goto error;
00087    }
00088    memset( extra, 0, sizeof(struct extra_attr));
00089 
00090    /* link the new extra at the end */
00091    if (tail == 0) {
00092        head = extra;
00093    } else {
00094        tail->next = extra;
00095    }
00096    tail = extra;
00097    n++;
00098 
00099    /* get name */
00100    foo = s;
00101    while (*s && !isspace((int)*s) && EQUAL != *s)  s++;
00102    if (*s == 0) goto parse_error;
00103    if (*s==EQUAL) {
00104        extra->name.len = (s++) - foo;
00105    } else {
00106        extra->name.len = (s++) - foo;
00107        /* skip spaces */
00108        while (*s && isspace((int)*s))  s++;
00109        if (*s != EQUAL) goto parse_error;
00110        s++;
00111    }
00112    extra->name.s = foo;
00113 
00114    /* skip spaces */
00115    while (*s && isspace((int)*s))  s++;
00116 
00117    /* get value type */
00118    stmp.s = s; stmp.len = strlen(s);
00119    if ((foo = pv_parse_spec(&stmp, &extra->spec)) == 0 )
00120        goto parse_error;
00121    s = foo;
00122 
00123    /* skip spaces */
00124    while (*s && isspace((int)*s))  s++;
00125    if (*s && ((*(s++) != SEPARATOR) || (*s == 0)))
00126        goto parse_error;
00127     }
00128 
00129     /* go throught all extras and make the names null terminated */
00130     for( extra = head; extra; extra = extra->next)
00131    extra->name.s[extra->name.len] = 0;
00132 
00133     return head;
00134 
00135 parse_error:
00136     LM_ERR("parse failed in <%s> around position %d\n",
00137       extra_str, (int)(long)(s-extra_str));
00138 
00139 error:
00140     LM_ERR("error\n");
00141     destroy_extras(head);
00142     return 0;
00143 }
00144 
00145 
00146 /*
00147  * Fill attr array name component with names of extra attributes
00148  * starting from offset. Return number of attributes added.
00149  */
00150 int extra2attrs(struct extra_attr *extra, struct attr *attrs, int offset)
00151 {
00152     int i;
00153 
00154     for (i = 0; extra; i++, extra = extra->next) {
00155    attrs[offset+i].n = extra->name.s;
00156     }
00157     return i;
00158 }
00159 
00160 
00161 /*
00162  * Get pseudo variable values of extra attributes to val_arr.
00163  * Return number of values or -1 in case of error.
00164  */
00165 int extra2strar( struct extra_attr *extra, struct sip_msg *rq, str *val_arr)
00166 {
00167     pv_value_t value;
00168     int n;
00169     int r;
00170 
00171     n = 0;
00172     r = 0;
00173 
00174     while (extra) {
00175    /* get the value */
00176    if (pv_get_spec_value(rq, &extra->spec, &value) != 0) {
00177        LM_ERR("failed to get value of extra attribute'%.*s'\n",
00178          extra->name.len,extra->name.s);
00179    }
00180 
00181    /* check for overflow */
00182    if (n == MAX_EXTRA) {
00183        LM_WARN("array too short -> ommiting extras for accounting\n");
00184        return -1;
00185    }
00186 
00187    if(value.flags&PV_VAL_NULL) {
00188        /* convert <null> to empty to have consistency */
00189        val_arr[n].s = 0;
00190        val_arr[n].len = 0;
00191    } else if (value.flags&PV_VAL_INT) {
00192        /* len = -1 denotes int type */
00193        val_arr[n].s = (char *)value.ri;
00194        val_arr[n].len = -1;
00195    } else {
00196        /* set the value into the acc buffer */
00197        if (value.rs.s+value.rs.len == static_detector) {
00198       val_arr[n].s = int_buf + r*INT2STR_MAX_LEN;
00199       val_arr[n].len = value.rs.len;
00200       memcpy(val_arr[n].s, value.rs.s, value.rs.len);
00201       r++;
00202        } else {
00203       val_arr[n] = value.rs;
00204        }
00205    }
00206    n++;
00207    extra = extra->next;
00208     }
00209 
00210     return n;
00211 }
00212 
00213 
00214 /* Free memory allocated for extra attributes */
00215 void destroy_extras(struct extra_attr *extra)
00216 {
00217     struct extra_attr *foo;
00218 
00219     while (extra) {
00220    foo = extra;
00221    extra = extra->next;
00222    pkg_free(foo);
00223     }
00224 }

Generated on Wed May 23 20:00:26 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6