int ops_dbload_avps (struct sip_msg* msg, struct fis_param *sp, struct db_param *dbp, int use_domain) { struct sip_uri uri; db_res_t *res; str uuid; int i, n, sh_flg; if (sp->flags&AVPOPS_VAL_NONE) { /* get and parse uri */ if (parse_source_uri( msg, sp->flags, &uri)<0 ) { LOG(L_ERR,"ERROR:avpops:load_avps: failed to get uri\n"); goto error; } /* do DB query */ res = db_load_avp( 0, (sp->flags&AVPOPS_FLAG_DOMAIN)?&empty:&uri.user, (use_domain||(sp->flags&AVPOPS_FLAG_DOMAIN))?&uri.host:0, dbp->sa.s, dbp->table , dbp->scheme); } else if (sp->flags&AVPOPS_VAL_AVP) { /* get uuid from avp */ if (get_avp_as_str( sp, &uuid)<0) { LOG(L_ERR,"ERROR:avpops:load_avps: failed to get uuid\n"); goto error; } /* do DB query */ res = db_load_avp( &uuid, 0, 0, dbp->sa.s, dbp->table, dbp->scheme); } else if (sp->flags&AVPOPS_VAL_STR) { /* use the STR val as uuid */ /* do DB query */ res = db_load_avp( sp->val.s, 0, 0, dbp->sa.s, dbp->table, dbp->scheme); } else { LOG(L_CRIT,"BUG:avpops:load_avps: invalid flag combination (%d)\n", sp->flags); goto error; } /* res query ? */ if (res==0) { LOG(L_ERR,"ERROR:avpops:load_avps: db_load failed\n"); goto error; } sh_flg = (dbp->scheme)?dbp->scheme->db_flags:-1; /* process the results */ for( n=0,i=0 ; i<res->n ; i++) { /* validate row */ if ( dbrow2avp( &res->rows[i], dbp->a.flags, dbp->a.val, sh_flg) < 0 ) continue; n++; } db_close_query( res ); DBG("DEBUG:avpops:load_avps: loaded avps = %d\n",n); return n?1:-1; error: return -1; }
int ops_dbload_avps (struct sip_msg* msg, struct fis_param *sp, struct db_param *dbp, struct db_url *url, int use_domain, str *prefix) { struct sip_uri uri; db_res_t *res = NULL; str uuid; int i, n, sh_flg; str *s0, *s1, *s2; int avp_name; int avp_type = 0; pv_value_t xvalue; s0 = s1 = s2 = NULL; if (!((sp->opd&AVPOPS_VAL_PVAR)||(sp->opd&AVPOPS_VAL_STR))) { LM_CRIT("invalid flag combination (%d/%d)\n", sp->opd, sp->ops); goto error; } /* get uuid from avp */ if (sp->opd&AVPOPS_VAL_PVAR) { if(pv_get_spec_value(msg, &(sp->u.sval), &xvalue)!=0) { LM_CRIT("failed to get PVAR value (%d/%d)\n", sp->opd, sp->ops); goto error; } if(xvalue.flags&(PV_VAL_NULL|PV_VAL_EMPTY)) { LM_ERR("no value for first param\n"); goto error; } uuid = xvalue.rs; } else { uuid.s = sp->u.s.s; uuid.len = sp->u.s.len; } if(sp->opd&AVPOPS_FLAG_UUID0) { s0 = &uuid; } else { /* parse uri */ if (parse_uri(uuid.s, uuid.len, &uri)<0) { LM_ERR("failed to parse uri\n"); goto error; } /* check uri */ if(!uri.user.s|| !uri.user.len|| !uri.host.len|| !uri.host.s) { LM_ERR("incomplet uri <%.*s>\n", uuid.len, uuid.s); goto error; } if((sp->opd&AVPOPS_FLAG_URI0)||(sp->opd&AVPOPS_FLAG_USER0)) s1 = &uri.user; if((sp->opd&AVPOPS_FLAG_URI0)||(sp->opd&AVPOPS_FLAG_DOMAIN0)) s2 = &uri.host; } /* is dynamic avp name ? */ if(dbp->a.type==AVPOPS_VAL_PVAR) { if(pv_has_dname(&(dbp->a.u.sval))) { if(pv_get_spec_name(msg, &(dbp->a.u.sval.pvp), &xvalue)!=0) { LM_CRIT("failed to get value for P2\n"); goto error; } if(xvalue.flags&(PV_VAL_NULL|PV_VAL_EMPTY)) { LM_ERR("no value for p2\n"); goto error; } if(xvalue.flags&PV_VAL_STR) { if(xvalue.rs.len>=AVPOPS_ATTR_LEN) { LM_ERR("name too long [%d/%.*s...]\n", xvalue.rs.len, 16, xvalue.rs.s); goto error; } dbp->sa.s = avpops_attr_buf; memcpy(dbp->sa.s, xvalue.rs.s, xvalue.rs.len); dbp->sa.len = xvalue.rs.len; dbp->sa.s[dbp->sa.len] = '\0'; } else { LM_INFO("no string value for p2\n"); goto error; } } } /* do DB query */ res = db_load_avp( url, s0, s1, ((use_domain)||(sp->opd&AVPOPS_FLAG_DOMAIN0))?s2:0, dbp->sa.s, &dbp->table, dbp->scheme); /* res query ? */ if (res==0) { LM_ERR("db_load failed\n"); goto error; } sh_flg = (dbp->scheme)?dbp->scheme->db_flags:-1; /* validate row */ avp_name = -1; if(dbp->a.type==AVPOPS_VAL_PVAR) { if(pv_has_dname(&dbp->a.u.sval)) { if(xvalue.flags&PV_TYPE_INT) { avp_name = xvalue.ri; } else { if (prefix) { if (xvalue.rs.len + prefix->len > AVPOPS_ATTR_LEN) { LM_ERR("name too long [%d/%.*s...]\n", prefix->len + xvalue.rs.len, 16, prefix->s); goto error; } memcpy(avpops_attr_buf, prefix->s, prefix->len); memcpy(avpops_attr_buf + prefix->len, xvalue.rs.s, xvalue.rs.len); xvalue.rs.s = avpops_attr_buf; xvalue.rs.len = prefix->len + xvalue.rs.len; } avp_name = get_avp_id(&xvalue.rs); if (avp_name < 0) { LM_ERR("cannot get avp id\n"); return -1; } } } else { avp_name = dbp->a.u.sval.pvp.pvn.u.isname.name.n; avp_type = dbp->a.u.sval.pvp.pvn.u.isname.type; } } /* process the results */ for( n=0,i=0 ; i<res->n ; i++) { if (dbrow2avp(&res->rows[i], dbp, avp_name, avp_type, sh_flg, prefix) < 0) continue; n++; } db_close_query( url, res ); LM_DBG("loaded avps = %d\n",n); return n?1:-1; error: return -1; }