int replace_avp(unsigned short flags, int name, int_str val, int index) { struct usr_avp* avp, *avp_prev; struct usr_avp* avp_new, *avp_del; if(index < 0) { LM_ERR("Index with negative value\n"); return -1; } avp_del = search_index_avp(flags, name, 0, index); if(avp_del == NULL) { LM_DBG("AVP to replace not found\n"); return -1; } avp_new = new_avp(flags, name, val); if(avp_new == NULL) { LM_ERR("Failed to create new avp structure\n"); return -1; } for( avp_prev=0,avp=*crt_avps ; avp ; avp_prev=avp,avp=avp->next ) { if (avp==avp_del) { if (avp_prev) avp_prev->next=avp_new; else *crt_avps = avp_new; avp_new->next = avp_del->next; shm_free(avp_del); return 0; } } return 0; }
static struct usr_avp *pack_evi_params_as_avp_list(evi_params_t *params) { struct usr_avp *avp, *head=NULL; evi_param_t *e_param; int_str val; int avp_id; /* take all the EVI parameters and convert them into AVPs */ for( e_param=params->first ; e_param ; e_param=e_param->next ) { /* get an AVP name matching the param name */ if (parse_avp_spec( &e_param->name, &avp_id)<0) { LM_ERR("cannot get AVP ID for name <%.*s>, skipping..\n", e_param->name.len, e_param->name.s); continue; } /* create a new AVP */ if (e_param->flags&EVI_STR_VAL) { val.s = e_param->val.s; avp = new_avp( AVP_VAL_STR, avp_id, val); } else if (e_param->flags&EVI_INT_VAL) { val.n = e_param->val.n; avp = new_avp( 0, avp_id, val); } else { LM_BUG("EVI param no STR, nor INT, ignoring...\n"); continue; } if (avp==NULL) { LM_ERR("cannot get create new AVP name <%.*s>, skipping..\n", e_param->name.len, e_param->name.s); continue; } /* link the AVP */ avp->next = head; head = avp; } return head; }
int add_avp(unsigned short flags, int name, int_str val) { struct usr_avp* avp; avp = new_avp(flags, name, val); if(avp == NULL) { LM_ERR("Failed to create new avp structure\n"); return -1; } avp->next = *crt_avps; *crt_avps = avp; return 0; }
struct usr_avp *clone_avp_list(struct usr_avp *old) { struct usr_avp *a; int_str val; if (!old) return NULL; /* create a copy of the old AVP */ get_avp_val( old, &val ); a = new_avp( old->flags, old->id, val); if (a==NULL) { LM_ERR("cloning failed, trunking the list\n"); return NULL; } a->next = clone_avp_list(old->next); return a; }
int add_avp_last(unsigned short flags, int name, int_str val) { struct usr_avp* avp; struct usr_avp* last_avp; avp = new_avp(flags, name, val); if(avp == NULL) { LM_ERR("Failed to create new avp structure\n"); return -1; } /* get end of the list */ for( last_avp=*crt_avps ; last_avp && last_avp->next ; last_avp=last_avp->next); if (last_avp==NULL) { avp->next = *crt_avps; *crt_avps = last_avp = avp; } else { last_avp->next = avp; avp->next = NULL; last_avp = avp; } return 0; }
/** * loal_from_file: * @param filename the file containing a loals text representation. * * Given a filename it will attempt to load a loal containing a copy of * the avpls represented in the file. * * Return value: if successful a pointer to the new populated loal, else NULL. * **/ extern LoAL* loal_from_file(gchar* filename) { FILE *fp = NULL; gchar c; int i = 0; guint32 linenum = 1; gchar linenum_buf[MAX_ITEM_LEN]; gchar name[MAX_ITEM_LEN]; gchar value[MAX_ITEM_LEN]; gchar op = '?'; LoAL *loal = new_loal(filename); AVPL* curr = NULL; AVP* avp; enum _load_loal_states { START, BEFORE_NAME, IN_NAME, IN_VALUE, MY_IGNORE } state; #ifndef _WIN32 if (! getuid()) { return load_loal_error(fp,loal,curr,linenum,"MATE Will not run as root"); } #endif state = START; if (( fp = ws_fopen(filename,"r") )) { while(( c = (gchar) fgetc(fp) )){ if ( feof(fp) ) { if ( ferror(fp) ) { report_read_failure(filename,errno); return load_loal_error(fp,loal,curr,linenum,"Error while reading '%f'",filename); } break; } if ( c == '\n' ) { linenum++; } if ( i >= MAX_ITEM_LEN - 1 ) { return load_loal_error(fp,loal,curr,linenum,"Maximum item length exceeded"); } switch(state) { case MY_IGNORE: switch (c) { case '\n': state = START; i = 0; continue; default: continue; } case START: switch (c) { case ' ': case '\t': /* ignore whitespace at line start */ continue; case '\n': /* ignore empty lines */ i = 0; continue; case AVP_NAME_CHAR: state = IN_NAME; i = 0; name[i++] = c; name[i] = '\0'; g_snprintf(linenum_buf,sizeof(linenum_buf),"%s:%u",filename,linenum); curr = new_avpl(linenum_buf); continue; case '#': state = MY_IGNORE; continue; default: return load_loal_error(fp,loal,curr,linenum,"expecting name got: '%c'",c); } case BEFORE_NAME: i = 0; name[0] = '\0'; switch (c) { case '\\': c = (gchar) fgetc(fp); if (c != '\n') ungetc(c,fp); continue; case ' ': case '\t': continue; case AVP_NAME_CHAR: state = IN_NAME; name[i++] = c; name[i] = '\0'; continue; case '\n': loal_append(loal,curr); state = START; continue; default: return load_loal_error(fp,loal,curr,linenum,"expecting name got: '%c'",c); } case IN_NAME: switch (c) { case ';': state = BEFORE_NAME; op = '?'; name[i] = '\0'; value[0] = '\0'; i = 0; avp = new_avp(name,value,op); if (! insert_avp(curr,avp) ) { delete_avp(avp); } continue; case AVP_OP_CHAR: name[i] = '\0'; i = 0; op = c; state = IN_VALUE; continue; case AVP_NAME_CHAR: name[i++] = c; continue; case '\n': return load_loal_error(fp,loal,curr,linenum,"operator expected found new line"); default: return load_loal_error(fp,loal,curr,linenum,"name or match operator expected found '%c'",c); } case IN_VALUE: switch (c) { case '\\': value[i++] = (gchar) fgetc(fp); continue; case ';': state = BEFORE_NAME; value[i] = '\0'; i = 0; avp = new_avp(name,value,op); if (! insert_avp(curr,avp) ) { delete_avp(avp); } continue; case '\n': return load_loal_error(fp,loal,curr,linenum,"';' expected found new line"); default: value[i++] = c; continue; } } } fclose (fp); return loal; } else { report_open_failure(filename,errno,FALSE); return load_loal_error(NULL,loal,NULL,0,"Cannot Open file '%s'",filename); } }
int w_insert_avp(struct sip_msg* msg, char* name, char* value, char *index_char) { int index = *(int*)index_char; int avp_name; struct usr_avp *avp= NULL, *prev_avp= NULL; struct usr_avp *avp_new; unsigned short name_type; pv_value_t xvalue; int flags = 0; int_str avp_val; pv_elem_t* pv_dest = (pv_elem_t*)name; pv_elem_t* pv_src = (pv_elem_t*)value; /* get avp name */ if(pv_get_avp_name(msg, &pv_dest->spec.pvp, &avp_name, &name_type)< 0) { LM_ERR("failed to get src AVP name\n"); return -1; } /* get value to be inserted */ avp = NULL; flags = 0; if(pv_src->spec.type == PVT_NONE) { avp_val.s = pv_src->text; flags = AVP_VAL_STR; } else { if(pv_get_spec_value(msg, &(pv_src->spec), &xvalue)!=0) { LM_ERR("cannot get src value\n"); return -1; } if(xvalue.flags&PV_TYPE_INT) { avp_val.n = xvalue.ri; } else { flags = AVP_VAL_STR; avp_val.s = xvalue.rs; } } name_type |= flags; /* insert it at the right place */ if(index == 0) { if(add_avp(name_type, avp_name, avp_val) < 0) { LM_ERR("Failed to add new avp\n"); return -1; } return 1; } /* search the previous avp */ index--; avp = NULL; while ( (avp=search_first_avp( name_type, avp_name, 0, avp))!=0 ) { if( index == 0 ) { break; } index--; prev_avp = avp; } /* if the index is greater then the count */ if(avp == NULL) { if(prev_avp == NULL) { if(add_avp(name_type, avp_name, avp_val) < 0) { LM_ERR("Failed to add new avp\n"); return -1; } return 1; } avp = prev_avp; } /* if a previous record was found -> insert the new avp after it */ avp_new = new_avp(name_type, avp_name, avp_val); if(avp_new == NULL) { LM_ERR("Failed to allocate new avp structure\n"); return -1; } LM_DBG("am alocat un avp nou\n"); avp_new->next = avp->next; avp->next = avp_new; return 1; }