示例#1
0
/*
 * Parse a digest parameter
 */
static inline int parse_digest_param(str* _s, dig_cred_t* _c)
{
	dig_par_t t;
	str* ptr;
	str dummy;

	     /* Get type of the parameter */
	if (parse_param_name(_s, &t) < 0) {
		return -1;
	}

	_s->s++;  /* skip = */
	_s->len--;

	     /* Find the begining of body */
	trim_leading(_s);

	if (_s->len == 0) {
		return -2;
	}

	     /* Decide in which attribute the
	      * body content will be stored
	      */
	switch(t) {
	case PAR_USERNAME:  ptr = &_c->username.whole;  break;
	case PAR_REALM:     ptr = &_c->realm;           break;
	case PAR_NONCE:     ptr = &_c->nonce;           break;
	case PAR_URI:       ptr = &_c->uri;             break;
	case PAR_RESPONSE:  ptr = &_c->response;        break;
	case PAR_CNONCE:    ptr = &_c->cnonce;          break;
	case PAR_OPAQUE:    ptr = &_c->opaque;          break;
	case PAR_QOP:       ptr = &_c->qop.qop_str;     break;
	case PAR_NC:        ptr = &_c->nc;              break;
	case PAR_ALGORITHM: ptr = &_c->alg.alg_str;     break;
	case PAR_OTHER:     ptr = &dummy;               break;
	default:            ptr = &dummy;               break;
	}

	     /* If the first character is quote, it is
	      * a quoted string, otherwise it is a token
	      */
	if (_s->s[0] == '\"') {
		if (parse_quoted(_s, ptr) < 0) {
			return -3;
		}
	} else {
		if (parse_token(_s, ptr) < 0) {
			return -4;
		}
	}
	
	return 0;
}
示例#2
0
/*
 * Parse parameters
 * _s is string containing parameters, it will be updated to point behind the parameters
 * _c is class of parameters
 * _h is pointer to structure that will be filled with pointer to well known parameters
 * linked list of parsed parameters will be stored in
 * the variable _p is pointing to
 * The function returns 0 on success and negative number
 * on an error
 */
int parse_params(str* _s, pclass_t _c, param_hooks_t* _h, param_t** _p)
{
	param_t* t;
	param_t* last;

	if (!_s || !_h || !_p) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

	memset(_h, 0, sizeof(param_hooks_t));
	last = NULL;
	*_p = 0;

	if (!_s->s) { /* no parameters at all -- we're done */
		LM_DBG("empty uri params, skipping\n");
		return 0;
	}

	LM_DBG("Parsing params for:[%.*s]\n",_s->len,_s->s);

	while(1) {
		t = (param_t*)pkg_malloc(sizeof(param_t));
		if (t == 0) {
			LM_ERR("no pkg memory left\n");
			goto error;
		}
		memset(t, 0, sizeof(param_t));

		parse_param_name(_s, _c, _h, t);
		trim_leading(_s);
		
		if (_s->len == 0) { /* The last parameter without body */
			t->len = t->name.len;
			goto ok;
		}
		
		if (_s->s[0] == '=') {
			_s->s++;
			_s->len--;
			trim_leading(_s);

			if (_s->len == 0) {
				LM_ERR("body missing\n");
				goto error;
			}

			if (parse_param_body(_s, t) < 0) {
				LM_ERR("failed to parse param body\n");
				goto error;
			}

			t->len = _s->s - t->name.s;

			trim_leading(_s);
			if (_s->len == 0) {
				goto ok;
			}
		} else {
			t->len = t->name.len;
		}

		if (_s->s[0]==',') goto ok; /* To be able to parse header parameters */
		if (_s->s[0]=='>') goto ok; /* To be able to parse URI parameters */

		if (_s->s[0] != ';') {
			LM_ERR("invalid character, ; expected, found %c \n",_s->s[0]);
			goto error;
		}

		_s->s++;
		_s->len--;
		trim_leading(_s);
		
		if (_s->len == 0) {
			LM_ERR("param name missing after ;\n");
			goto error;
		}

		if (last) {last->next=t;} else {*_p = t;}
		last = t;
	}

error:
	if (t) pkg_free(t);
	free_params(*_p);
	*_p = 0;
	return -2;

ok:
	if (last) {last->next=t;} else {*_p = t;}
	_h->last_param = last = t;
	return 0;
}
/*
 * Parse parameters
 * _s is string containing parameters, it will be updated to point behind the parameters
 * _c is class of parameters
 * _h is pointer to structure that will be filled with pointer to well known parameters
 * linked list of parsed parameters will be stored in
 * the variable _p is pointing to
 * The function returns 0 on success and negative number
 * on an error
 */
int parse_params(str* _s, pclass_t _c, param_hooks_t* _h, param_t** _p)
{
	param_t* t;

	if (!_s || !_h || !_p) {
		LOG(L_ERR, "parse_params(): Invalid parameter value\n");
		return -1;
	}

	memset(_h, 0, sizeof(param_hooks_t));
	*_p = 0;

	if (!_s->s) { /* no parameters at all -- we're done */
		DBG("DEBUG: parse_params: empty uri params, skipping\n");
		return 0;
	}
			
	while(1) {
		t = (param_t*)pkg_malloc(sizeof(param_t));
		if (t == 0) {
			LOG(L_ERR, "parse_params(): No memory left\n");
			goto error;
		}
		memset(t, 0, sizeof(param_t));

		parse_param_name(_s, _c, _h, t);
		trim_leading(_s);
		
		if (_s->len == 0) { /* The last parameter without body */
			goto ok;
		}
		
		if (_s->s[0] == '=') {
			_s->s++;
			_s->len--;
			trim_leading(_s);

			if (_s->len == 0) {
				LOG(L_ERR, "parse_params(): Body missing\n");
				goto error;
			}

			if (parse_param_body(_s, t) < 0) {
				LOG(L_ERR, "parse_params(): Error while parsing param body\n");
				goto error;
			}

			t->len = _s->s - t->name.s;

			trim_leading(_s);
			if (_s->len == 0) {
				goto ok;
			}
		} else {
			t->len = _s->s - t->name.s;
		}

		if (_s->s[0] == ',') goto ok; /* To be able to parse header parameters */
		if (_s->s[0] == '>') goto ok; /* To be able to parse URI parameters */

		if (_s->s[0] != ';') {
			LOG(L_ERR, "parse_params(): Invalid character, ; expected\n");
			goto error;
		}

		_s->s++;
		_s->len--;
		trim_leading(_s);
		
		if (_s->len == 0) {
			LOG(L_ERR, "parse_params(): Param name missing after ;\n");
			goto error;
		}

		t->next = *_p;
		*_p = t;
	}

 error:
	if (t) pkg_free(t);
	free_params(*_p);
	return -2;

 ok:
	t->next = *_p;
	*_p = t;
	return 0;
}
示例#4
0
/*!  \brief
 * Only parse one parameter
 * Returns:
 * 	t: out parameter
 * 	-1: on error
 * 	0: success, but expect a next paramter
 * 	1: success and expect no more parameters
 */
static inline int parse_param2(
		str *_s, pclass_t _c, param_hooks_t *_h, param_t *t, char separator)
{
	memset(t, 0, sizeof(param_t));

	parse_param_name(_s, _c, _h, t, separator);
	trim_leading(_s);

	if(_s->len == 0) { /* The last parameter without body */
		t->len = t->name.len;
		goto ok;
	}

	if(_s->s[0] == '=') {
		_s->s++;
		_s->len--;
		trim_leading(_s);

		if(_s->len == 0) {
			/* Be forgiving and accept parameters with missing value,
			 * we just set the length of parameter body to 0. */
			t->body.s = _s->s;
			t->body.len = 0;
		} else if(parse_param_body(_s, t, separator) < 0) {
			LM_ERR("Error while parsing param body\n");
			goto error;
		}

		t->len = _s->s - t->name.s;

		trim_leading(_s);
		if(_s->len == 0) {
			goto ok;
		}
	} else {
		t->len = t->name.len;
	}

	if(_s->s[0] == ',')
		goto ok; /* To be able to parse header parameters */
	if(_s->s[0] == '>')
		goto ok; /* To be able to parse URI parameters */

	if(_s->s[0] != separator) {
		LM_ERR("Invalid character, %c expected\n", separator);
		goto error;
	}

	_s->s++;
	_s->len--;
	trim_leading(_s);

	if(_s->len == 0) {
		LM_ERR("Param name missing after %c\n", separator);
		goto error;
	}

	return 0; /* expect more params */

ok:
	return 1; /* done with parsing for params */
error:
	return -1;
}
/**
 * Only parse one parameter
 * Returns:
 * 	t: out parameter
 * 	-1: on error
 * 	0: success, but expect a next paramter
 * 	1: success and exepect no more parameters
 */
inline int parse_param(str *_s, pclass_t _c, param_hooks_t *_h, param_t *t)
{
	memset(t, 0, sizeof(param_t));

	parse_param_name(_s, _c, _h, t);
	trim_leading(_s);
	
	if (_s->len == 0) { /* The last parameter without body */
		t->len = t->name.len;
		goto ok;
	}
	
	if (_s->s[0] == '=') {
		_s->s++;
		_s->len--;
		trim_leading(_s);

		if (_s->len == 0) {
			LOG(L_ERR, "parse_params(): Body missing\n");
			goto error;
		}

		if (parse_param_body(_s, t) < 0) {
			LOG(L_ERR, "parse_params(): Error while parsing param body\n");
			goto error;
		}

		t->len = _s->s - t->name.s;

		trim_leading(_s);
		if (_s->len == 0) {
			goto ok;
		}
	} else {
		t->len = t->name.len;
	}

	if (_s->s[0] == ',') goto ok; /* To be able to parse header parameters */
	if (_s->s[0] == '>') goto ok; /* To be able to parse URI parameters */

	if (_s->s[0] != ';') {
		LOG(L_ERR, "parse_params(): Invalid character, ; expected\n");
		goto error;
	}

	_s->s++;
	_s->len--;
	trim_leading(_s);
	
	if (_s->len == 0) {
		LOG(L_ERR, "parse_params(): Param name missing after ;\n");
		goto error;
	}

	return 0; /* expect more params */

ok:
	return 1; /* done with parsing for params */
error:
	return -1;
}