misc_radius/extra.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00037 static char int_buf[INT2STR_MAX_LEN*MAX_EXTRA];
00038 static char *static_detector = 0;
00039
00040
00041
00042 void init_extra_engine(void)
00043 {
00044 int i;
00045
00046 static_detector = int2str( (unsigned long)3, &i) + i;
00047 }
00048
00049
00050
00051
00052
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
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
00091 if (tail == 0) {
00092 head = extra;
00093 } else {
00094 tail->next = extra;
00095 }
00096 tail = extra;
00097 n++;
00098
00099
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
00108 while (*s && isspace((int)*s)) s++;
00109 if (*s != EQUAL) goto parse_error;
00110 s++;
00111 }
00112 extra->name.s = foo;
00113
00114
00115 while (*s && isspace((int)*s)) s++;
00116
00117
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
00124 while (*s && isspace((int)*s)) s++;
00125 if (*s && ((*(s++) != SEPARATOR) || (*s == 0)))
00126 goto parse_error;
00127 }
00128
00129
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
00148
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
00163
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
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
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
00189 val_arr[n].s = 0;
00190 val_arr[n].len = 0;
00191 } else if (value.flags&PV_VAL_INT) {
00192
00193 val_arr[n].s = (char *)value.ri;
00194 val_arr[n].len = -1;
00195 } else {
00196
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
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 }