Exemple #1
0
/*!
 * \brief Delete a urecord from domain
 * \param _d domain where the record should be deleted
 * \param _aor address of record
 * \param _r deleted record
 * \return 0 on success, -1 if the record could not be deleted
 */
int delete_urecord(udomain_t* _d, str* _aor, struct urecord* _r)
{
    struct ucontact* c, *t;

    if (db_mode==DB_ONLY) {
        if (_r==0)
            get_static_urecord( _d, _aor, &_r);
        if (db_delete_urecord(_d, _r)<0) {
            LM_ERR("DB delete failed\n");
            return -1;
        }
        free_urecord(_r);
        return 0;
    }

    if (_r==0) {
        if (get_urecord(_d, _aor, &_r) > 0) {
            return 0;
        }
    }

    c = _r->contacts;
    while(c) {
        t = c;
        c = c->next;
        if (delete_ucontact(_r, t) < 0) {
            LM_ERR("deleting contact failed\n");
            return -1;
        }
    }
    release_urecord(_r);
    return 0;
}
Exemple #2
0
/*!
 * \brief Release urecord previously obtained through get_urecord
 * \warning Failing to calls this function after get_urecord will
 * result in a memory leak when the DB_ONLY mode is used. When
 * the records is later deleted, e.g. with delete_urecord, then
 * its not necessary, as this function already releases the record.
 * \param _r released record
 */
void release_urecord(urecord_t* _r)
{
	if (db_mode==DB_ONLY) {
		free_urecord(_r);
	} else if (_r->contacts == 0) {
		mem_delete_urecord(_r->slot->d, _r);
	}
}
Exemple #3
0
/*
 * Remove a record from domain
 */
void mem_delete_urecord(udomain_t* _d, struct urecord* _r)
{
	if (_r->watchers == 0) {
		udomain_remove(_d, _r);
		slot_rem(_r->slot, _r);
		free_urecord(_r);
		_d->users--; /* FIXME */
	}
		
}
Exemple #4
0
/*
 * Deinitialize given slot structure
 */
void deinit_slot(hslot_t* _s)
{
	struct urecord* ptr;
	
	     /* Remove all elements */
	while(_s->first) {
		ptr = _s->first;
		_s->first = _s->first->s_ll.next;
		free_urecord(ptr);
	}
	
	_s->n = 0;
	_s->last = 0;
        _s->d = 0;
}
Exemple #5
0
void free_value_urecord( void * val )
{
	free_urecord( (urecord_t *) val);
}
Exemple #6
0
/*!
 * \brief Remove a record from domain in memory
 * \param _d domain the record belongs to
 * \param _r deleted record
 */
void mem_delete_urecord(udomain_t* _d, struct urecord* _r)
{
    slot_rem(_r->slot, _r);
    free_urecord(_r);
    update_stat( _d->users, -1);
}
Exemple #7
0
/*!
 * \brief Loads from DB all contacts for a RUID
 * \param _c database connection
 * \param _d domain
 * \param _aor address of record
 * \return pointer to the record on success, 0 on errors or if nothing is found
 */
urecord_t* db_load_urecord_by_ruid(udomain_t* _d, str *_ruid)
{
    ucontact_info_t *ci;
    db_key_t columns[18];
    db_key_t keys[1];
    db_key_t order;
    db_val_t vals[1];
    db1_res_t* res = NULL;
    db_row_t *row;
    str contact;
    str aor;
    char aorbuf[512];
    str domain;

    urecord_t* r;
    ucontact_t* c;

    keys[0] = &ruid_col;
    vals[0].type = DB1_STR;
    vals[0].nul = 0;
    vals[0].val.str_val = *_ruid;

    columns[0] = &contact_col;
    columns[1] = &expires_col;
    columns[2] = &q_col;
    columns[3] = &callid_col;
    columns[4] = &cseq_col;
    columns[5] = &flags_col;
    columns[6] = &cflags_col;
    columns[7] = &user_agent_col;
    columns[8] = &received_col;
    columns[9] = &path_col;
    columns[10] = &sock_col;
    columns[11] = &methods_col;
    columns[12] = &last_mod_col;
    columns[13] = &ruid_col;
    columns[14] = &instance_col;
    columns[15] = &user_col;
    columns[16] = &reg_id_col;
    columns[17] = &domain_col;

    if (desc_time_order)
        order = &last_mod_col;
    else
        order = &q_col;

    if (ul_db_layer_query(_d,  &vals[0].val.str_val,  &vals[1].val.str_val, keys, 0, vals, columns, 1, 18, order,
                          &res) < 0) {
        LM_ERR("db_query failed\n");
        return 0;
    }

    if (RES_ROW_N(res) == 0) {
        LM_DBG("aor %.*s not found in table %.*s\n",_ruid->len, _ruid->s,
               _d->name->len, _d->name->s);
        ul_db_layer_free_result(_d, res);
        return 0;
    }

    r = 0;

    /* use first row - shouldn't be more */
    row = RES_ROWS(res);

    ci = dbrow2info(ROW_VALUES(RES_ROWS(res)), &contact);
    if (ci==0) {
        LM_ERR("skipping record for %.*s in table %s\n",
               _ruid->len, _ruid->s, _d->name->s);
        goto done;
    }

    aor.s = (char*)VAL_STRING(ROW_VALUES(row) + 15);
    aor.len = strlen(aor.s);

    if (use_domain) {
        domain.s = (char*)VAL_STRING(ROW_VALUES(row) + 17);
        if (VAL_NULL(ROW_VALUES(row)+17) || domain.s==0 || domain.s[0]==0) {
            LM_CRIT("empty domain record for user %.*s...skipping\n",
                    aor.len, aor.s);
            goto done;
        }
        domain.len = strlen(domain.s);
        if(aor.len + domain.len + 2 >= 512) {
            LM_ERR("AoR is too big\n");
            goto done;
        }
        memcpy(aorbuf, aor.s, aor.len);
        aorbuf[aor.len] = '@';
        memcpy(aorbuf + aor.len + 1, domain.s, domain.len);
        aor.len += 1 + domain.len;
        aor.s = aorbuf;
        aor.s[aor.len] = '\0';
    }
    get_static_urecord( _d, &aor, &r);

    if ( (c=mem_insert_ucontact(r, &contact, ci)) == 0) {
        LM_ERR("mem_insert failed\n");
        free_urecord(r);
        ul_db_layer_free_result(_d, res);
        return 0;
    }

    /* We have to do this, because insert_ucontact sets state to CS_NEW
     * and we have the contact in the database already */
    c->state = CS_SYNC;

done:
    ul_db_layer_free_result(_d, res);
    ;
    return r;
}
Exemple #8
0
/*!
 * \brief Loads from DB all contacts for an AOR
 * \param _c database connection
 * \param _d domain
 * \param _aor address of record
 * \return pointer to the record on success, 0 on errors or if nothing is found
 */
urecord_t* db_load_urecord(udomain_t* _d, str *_aor)
{
    ucontact_info_t *ci;
    db_key_t columns[16];
    db_key_t keys[2];
    db_key_t order;
    db_val_t vals[2];
    db1_res_t* res = NULL;
    str contact;
    char *domain;
    int i;

    urecord_t* r;
    ucontact_t* c;

    keys[0] = &user_col;
    vals[0].type = DB1_STR;
    vals[0].nul = 0;
    if (use_domain) {
        keys[1] = &domain_col;
        vals[1].type = DB1_STR;
        vals[1].nul = 0;
        domain = memchr(_aor->s, '@', _aor->len);
        vals[0].val.str_val.s   = _aor->s;
        if (domain==0) {
            vals[0].val.str_val.len = 0;
            vals[1].val.str_val = *_aor;
        } else {
            vals[0].val.str_val.len = domain - _aor->s;
            vals[1].val.str_val.s   = domain+1;
            vals[1].val.str_val.len = _aor->s + _aor->len - domain - 1;
        }
    } else {
        vals[0].val.str_val = *_aor;
    }

    columns[0] = &contact_col;
    columns[1] = &expires_col;
    columns[2] = &q_col;
    columns[3] = &callid_col;
    columns[4] = &cseq_col;
    columns[5] = &flags_col;
    columns[6] = &cflags_col;
    columns[7] = &user_agent_col;
    columns[8] = &received_col;
    columns[9] = &path_col;
    columns[10] = &sock_col;
    columns[11] = &methods_col;
    columns[12] = &last_mod_col;
    columns[13] = &ruid_col;
    columns[14] = &instance_col;
    columns[15] = &reg_id_col;

    if (desc_time_order)
        order = &last_mod_col;
    else
        order = &q_col;

    if (ul_db_layer_query(_d,  &vals[0].val.str_val,  &vals[1].val.str_val, keys, 0, vals, columns, (use_domain)?2:1, 16, order,
                          &res) < 0) {
        LM_ERR("db_query failed\n");
        return 0;
    }

    if (RES_ROW_N(res) == 0) {
        LM_DBG("aor %.*s not found in table %.*s\n",_aor->len, _aor->s, _d->name->len, _d->name->s);

        ul_db_layer_free_result(_d, res);
        return 0;
    }

    r = 0;

    for(i = 0; i < RES_ROW_N(res); i++) {
        ci = dbrow2info(  ROW_VALUES(RES_ROWS(res) + i), &contact);
        if (ci==0) {
            LM_ERR("skipping record for %.*s in table %s\n",
                   _aor->len, _aor->s, _d->name->s);
            continue;
        }

        if ( r==0 )
            get_static_urecord( _d, _aor, &r);

        if ( (c=mem_insert_ucontact(r, &contact, ci)) == 0) {
            LM_ERR("mem_insert failed\n");
            free_urecord(r);
            ul_db_layer_free_result(_d, res);
            return 0;
        }

        /* We have to do this, because insert_ucontact sets state to CS_NEW
         * and we have the contact in the database already */
        c->state = CS_SYNC;
    }

    ul_db_layer_free_result(_d, res);
    return r;
}