コード例 #1
0
ファイル: ratelimit_helper.c プロジェクト: UIKit0/OpenSIPS
/* NOTE: assumes that the pipe has been locked. If fails, releases the lock */
static int rl_get_counter(str *name, rl_pipe_t * pipe)
{
	str res;
	unsigned int hid = RL_GET_INDEX(*name);
	int new_counter;

	RL_SET_PENDING(pipe);
	RL_RELEASE_LOCK(hid);

	if (rl_set_name(name) < 0)
		return -1;
	if (cdbf.get(cdbc, &rl_name_buffer, &res) < 0) {
		LM_ERR("cannot retrieve key\n");
		return -1;
	}
	if (str2sint(&res, &new_counter) < 0) {
		LM_ERR("invalid value %.*s - should be integer\n", res.len, res.s);
		return -1;
	}
	if (res.s)
		pkg_free(res.s);
	RL_GET_LOCK(hid);
	RL_RESET_PENDING(pipe);
	pipe->counter = new_counter;
	return 0;
}
コード例 #2
0
/* NOTE: assumes that the pipe has been locked. If fails, releases the lock */
static int rl_change_counter(str *name, rl_pipe_t *pipe, int c)
{
	int new_counter;

	if (rl_set_name(name) < 0)
		return -1;

	if (pipe->my_counter + c <= 0) {
		LM_DBG("Counter going negative\n");
		return 1;
	}

	if (cdbf.add(cdbc, &rl_name_buffer, c ? c : -(pipe->my_counter),
				rl_expire_time, &new_counter) < 0){
		LM_ERR("cannot change counter for pipe %.*s with %d\n",
				name->len, name->s, c);
		return -1;
	}

	pipe->my_counter = c ? pipe->my_counter + c : 0;
	pipe->counter = new_counter;
	LM_DBG("changed with %d; my_counter: %d; counter: %d\n",
			c, pipe->my_counter, new_counter);

	return 0;
}
コード例 #3
0
ファイル: ratelimit_helper.c プロジェクト: ihassin/opensips
/* NOTE: assumes that the pipe has been locked */
static int rl_get_counter(str *name, rl_pipe_t * pipe)
{
	int new_counter;

	if (rl_set_name(name) < 0)
		return -1;
	if (cdbf.get_counter(cdbc, &rl_name_buffer, &new_counter) < 0) {
		LM_ERR("cannot retrieve key\n");
		return -1;
	}

	pipe->counter = new_counter;
	return 0;
}
コード例 #4
0
ファイル: ratelimit_helper.c プロジェクト: UIKit0/OpenSIPS
/* NOTE: assumes that the pipe has been locked. If fails, releases the lock */
static int rl_change_counter(str *name, rl_pipe_t *pipe, int c)
{
	unsigned int hid = RL_GET_INDEX(*name);
	int new_counter;

	RL_SET_PENDING(pipe);
	RL_RELEASE_LOCK(hid);
	if (rl_set_name(name) < 0)
		return -1;

	/* if the command should be reset */
	/* XXX: This is not needed since add takes also negative numbers 
	if (c > 0) {
		if (cdbf.add(cdbc, &rl_name_buffer, c, rl_expire_time, &new_counter)<0){
			LM_ERR("cannot increase buffer for pipe %.*s\n",
					name->len, name->s);
			return -1;
		}
	} else {
		if (cdbf.sub(cdbc, &rl_name_buffer, c ? c : pipe->my_counter,
					rl_expire_time, &new_counter) < 0){
			LM_ERR("cannot change counter for pipe %.*s with %d\n",
					name->len, name->s, c);
			return -1;
		}
	}
	*/
	if (cdbf.add(cdbc, &rl_name_buffer, c ? c : -(pipe->my_counter),
				rl_expire_time, &new_counter) < 0){
		LM_ERR("cannot change counter for pipe %.*s with %d\n",
				name->len, name->s, c);
		return -1;
	}

	RL_GET_LOCK(hid);
	RL_RESET_PENDING(pipe);
	pipe->my_counter = c ? pipe->my_counter + c : 0;
	pipe->counter = new_counter;
	LM_DBG("changed with %d; my_counter: %d; counter: %d\n",
			c, pipe->my_counter, new_counter);

	return 0;
}