#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <strings.h>
#include <ctype.h>
#include <sip.h>
#include "sip_miscdefs.h"
#include "sip_msg.h"
#include "sip_parse_uri.h"
int
sip_atoi(_sip_header_t *sip_header, int *num)
{
boolean_t num_found = B_FALSE;
*num = 0;
while (sip_header->sip_hdr_current < sip_header->sip_hdr_end) {
if (isspace(*sip_header->sip_hdr_current)) {
sip_header->sip_hdr_current++;
if (num_found)
break;
} else if (isdigit(*sip_header->sip_hdr_current)) {
*num = (*num * 10) +
(*sip_header->sip_hdr_current - '0');
num_found = B_TRUE;
sip_header->sip_hdr_current++;
} else {
break;
}
}
if (!num_found)
return (EINVAL);
return (0);
}
int
sip_find_token(_sip_header_t *sip_header, char token)
{
while (sip_header->sip_hdr_current < sip_header->sip_hdr_end) {
if (token != SIP_COMMA &&
*sip_header->sip_hdr_current == SIP_COMMA) {
sip_header->sip_hdr_current--;
return (1);
}
if (*sip_header->sip_hdr_current++ == token) {
return (0);
}
}
return (1);
}
int
sip_find_cr(_sip_header_t *sip_header)
{
sip_header->sip_hdr_current = sip_header->sip_hdr_end;
while (*sip_header->sip_hdr_current-- != '\n') {
if (sip_header->sip_hdr_current == sip_header->sip_hdr_start)
return (1);
}
return (0);
}
int
sip_find_separator(_sip_header_t *sip_header, char separator_1st,
char separator_2nd, char separator_3rd, boolean_t ignore_space)
{
assert(separator_1st != (char)NULL || separator_2nd != (char)NULL);
while (sip_header->sip_hdr_current < sip_header->sip_hdr_end) {
if (ignore_space && (*sip_header->sip_hdr_current == SIP_SP)) {
sip_header->sip_hdr_current++;
continue;
}
if (isspace(*sip_header->sip_hdr_current) ||
(separator_1st != 0 &&
(*sip_header->sip_hdr_current == separator_1st)) ||
(separator_2nd != 0 &&
(*sip_header->sip_hdr_current == separator_2nd)) ||
(separator_3rd != 0 &&
(*sip_header->sip_hdr_current == separator_3rd))) {
return (0);
}
if (*sip_header->sip_hdr_current == '\\')
sip_header->sip_hdr_current++;
sip_header->sip_hdr_current++;
}
return (1);
}
int
sip_find_white_space(_sip_header_t *sip_header)
{
while (sip_header->sip_hdr_current < sip_header->sip_hdr_end) {
if (isspace(*sip_header->sip_hdr_current))
return (0);
sip_header->sip_hdr_current++;
}
return (1);
}
int
sip_skip_white_space(_sip_header_t *sip_header)
{
while (sip_header->sip_hdr_current < sip_header->sip_hdr_end) {
if (!isspace(*sip_header->sip_hdr_current))
return (0);
sip_header->sip_hdr_current++;
}
return (1);
}
int
sip_reverse_skip_white_space(_sip_header_t *sip_header)
{
while (sip_header->sip_hdr_current >= sip_header->sip_hdr_start) {
if (!isspace(*sip_header->sip_hdr_current))
return (0);
sip_header->sip_hdr_current--;
}
return (1);
}
int
sip_parse_goto_values(_sip_header_t *sip_header)
{
if (sip_find_token(sip_header, SIP_HCOLON) != 0)
return (1);
if (sip_skip_white_space(sip_header) != 0)
return (1);
return (0);
}
int
sip_goto_next_value(_sip_header_t *sip_header)
{
boolean_t quoted = B_FALSE;
while (sip_header->sip_hdr_current < sip_header->sip_hdr_end) {
if (*sip_header->sip_hdr_current == SIP_QUOTE) {
if (quoted)
quoted = B_FALSE;
else
quoted = B_TRUE;
} else if (!quoted &&
*sip_header->sip_hdr_current == SIP_COMMA) {
sip_header->sip_hdr_current--;
return (0);
}
sip_header->sip_hdr_current++;
}
if (quoted)
return (1);
return (0);
}
int
sip_parse_params(_sip_header_t *sip_header, sip_param_t **parsed_list)
{
sip_param_t *param = NULL;
sip_param_t *new_param;
char *tmp_ptr;
if (parsed_list == NULL)
return (0);
*parsed_list = NULL;
for (;;) {
boolean_t quoted_name = B_FALSE;
if (sip_skip_white_space(sip_header) != 0)
return (0);
if (*sip_header->sip_hdr_current != SIP_SEMI)
return (0);
sip_header->sip_hdr_current++;
new_param = calloc(1, sizeof (sip_param_t));
if (new_param == NULL)
return (ENOMEM);
if (param != NULL)
param->param_next = new_param;
else
*parsed_list = new_param;
param = new_param;
if (sip_skip_white_space(sip_header) != 0)
return (EPROTO);
tmp_ptr = sip_header->sip_hdr_current;
param->param_name.sip_str_ptr = tmp_ptr;
if (sip_find_separator(sip_header, SIP_EQUAL, SIP_SEMI,
SIP_COMMA, B_FALSE) != 0) {
param->param_name.sip_str_len =
sip_header->sip_hdr_current - tmp_ptr;
param->param_value.sip_str_ptr = NULL;
param->param_value.sip_str_len = 0;
return (0);
}
param->param_name.sip_str_len =
sip_header->sip_hdr_current - tmp_ptr;
if (sip_skip_white_space(sip_header) != 0 ||
*sip_header->sip_hdr_current == SIP_COMMA) {
param->param_value.sip_str_ptr = NULL;
param->param_value.sip_str_len = 0;
return (0);
}
if (*sip_header->sip_hdr_current == SIP_SEMI) {
param->param_value.sip_str_ptr = NULL;
param->param_value.sip_str_len = 0;
continue;
}
assert(*sip_header->sip_hdr_current == SIP_EQUAL);
sip_header->sip_hdr_current++;
if (sip_skip_white_space(sip_header) != 0)
return (EPROTO);
if (*sip_header->sip_hdr_current == SIP_QUOTE) {
sip_header->sip_hdr_current++;
quoted_name = B_TRUE;
}
param->param_value.sip_str_ptr = sip_header->sip_hdr_current;
tmp_ptr = sip_header->sip_hdr_current;
if (quoted_name && sip_find_token(sip_header, SIP_QUOTE) != 0) {
return (EPROTO);
} else if (sip_find_separator(sip_header, SIP_SEMI, SIP_COMMA,
0, B_FALSE) != 0) {
return (EPROTO);
}
param->param_value.sip_str_len = sip_header->sip_hdr_current -
tmp_ptr;
if (quoted_name)
param->param_value.sip_str_len--;
}
}
boolean_t
sip_is_empty_hdr(_sip_header_t *sip_header)
{
if (sip_find_token(sip_header, SIP_HCOLON) != 0) {
sip_header->sip_hdr_current = sip_header->sip_hdr_start;
return (B_FALSE);
}
if (sip_skip_white_space(sip_header) == 0) {
sip_header->sip_hdr_current = sip_header->sip_hdr_start;
return (B_FALSE);
}
sip_header->sip_hdr_current = sip_header->sip_hdr_start;
return (B_TRUE);
}
int
sip_parse_hdr_empty(_sip_header_t *hdr, sip_parsed_header_t **phdr)
{
sip_parsed_header_t *parsed_header;
if (hdr == NULL || phdr == NULL)
return (EINVAL);
if (hdr->sip_hdr_parsed != NULL) {
*phdr = hdr->sip_hdr_parsed;
return (0);
}
*phdr = NULL;
parsed_header = calloc(1, sizeof (sip_parsed_header_t));
if (parsed_header == NULL)
return (ENOMEM);
parsed_header->sip_header = hdr;
parsed_header->value = NULL;
*phdr = parsed_header;
return (0);
}
static void
sip_parse_uri_str(sip_str_t *sip_str, sip_hdr_value_t *value)
{
int error;
if (sip_str->sip_str_len > 0) {
value->sip_value_parsed_uri = sip_parse_uri(sip_str, &error);
if (value->sip_value_parsed_uri == NULL)
return;
if (error != 0 ||
value->sip_value_parsed_uri->sip_uri_errflags != 0) {
value->sip_value_state = SIP_VALUE_BAD;
}
}
}
int
sip_prim_parsers(_sip_header_t *sip_header, sip_parsed_header_t **header)
{
if (sip_header == NULL || header == NULL)
return (EINVAL);
if (sip_header->sip_hdr_parsed != NULL) {
*header = sip_header->sip_hdr_parsed;
return (0);
}
*header = NULL;
assert(sip_header->sip_hdr_start == sip_header->sip_hdr_current);
if (sip_parse_goto_values(sip_header) != 0)
return (EPROTO);
return (0);
}
int
sip_get_protocol_version(_sip_header_t *sip_header,
sip_proto_version_t *sip_proto_version)
{
if (sip_skip_white_space(sip_header) != 0)
return (1);
if (strncasecmp(sip_header->sip_hdr_current, SIP, strlen(SIP)) == 0) {
sip_proto_version->name.sip_str_ptr =
sip_header->sip_hdr_current;
sip_proto_version->name.sip_str_len = strlen(SIP);
if (sip_find_token(sip_header, SIP_SLASH) != 0)
return (1);
if (sip_skip_white_space(sip_header) != 0)
return (1);
sip_proto_version->version.sip_str_ptr =
sip_header->sip_hdr_current;
while (isdigit(*sip_header->sip_hdr_current)) {
sip_header->sip_hdr_current++;
if (sip_header->sip_hdr_current >=
sip_header->sip_hdr_end) {
return (1);
}
}
if (*sip_header->sip_hdr_current != SIP_PERIOD)
return (1);
sip_header->sip_hdr_current++;
if (!isdigit(*sip_header->sip_hdr_current))
return (1);
while (isdigit(*sip_header->sip_hdr_current)) {
sip_header->sip_hdr_current++;
if (sip_header->sip_hdr_current >=
sip_header->sip_hdr_end) {
return (1);
}
}
sip_proto_version->version.sip_str_len =
sip_header->sip_hdr_current -
sip_proto_version->version.sip_str_ptr;
return (0);
}
return (1);
}
int
sip_parse_hdr_parser1(_sip_header_t *hdr, sip_parsed_header_t **phdr, char sep)
{
sip_parsed_header_t *parsed_header;
int ret;
sip_hdr_value_t *value = NULL;
sip_hdr_value_t *last_value = NULL;
if ((ret = sip_prim_parsers(hdr, phdr)) != 0)
return (ret);
if (*phdr != NULL) {
hdr->sip_hdr_parsed = *phdr;
return (0);
}
parsed_header = calloc(1, sizeof (sip_parsed_header_t));
if (parsed_header == NULL)
return (ENOMEM);
parsed_header->sip_parsed_header_version = SIP_PARSED_HEADER_VERSION_1;
parsed_header->sip_header = hdr;
while (hdr->sip_hdr_current < hdr->sip_hdr_end) {
value = calloc(1, sizeof (sip_hdr_value_t));
if (value == NULL) {
sip_free_phdr(parsed_header);
return (ENOMEM);
}
if (last_value != NULL)
last_value->sip_next_value = value;
else
parsed_header->value = (sip_value_t *)value;
value->sip_value_start = hdr->sip_hdr_current;
value->sip_value_header = parsed_header;
if (sip_find_separator(hdr, sep, SIP_COMMA, SIP_SEMI,
B_FALSE) == 0) {
char c = *hdr->sip_hdr_current;
if (isspace(c) && sep == 0) {
value->str_val_ptr = value->sip_value_start;
value->str_val_len = hdr->sip_hdr_current -
value->sip_value_start;
if (sip_skip_white_space(hdr) != 0) {
value->sip_value_end =
hdr->sip_hdr_current;
goto end;
}
c = *(hdr->sip_hdr_current);
}
if (c == SIP_COMMA) {
char *t = hdr->sip_hdr_current;
hdr->sip_hdr_current--;
(void) sip_reverse_skip_white_space(hdr);
value->str_val_ptr = value->sip_value_start;
value->str_val_len = hdr->sip_hdr_current -
value->sip_value_start + 1;
hdr->sip_hdr_current = t;
goto get_next_val;
}
if ((sep != 0) && (c == sep)) {
value->strs1_val_ptr = value->sip_value_start;
value->strs1_val_len = hdr->sip_hdr_current -
value->sip_value_start;
value->strs2_val_ptr =
(++hdr->sip_hdr_current);
if (sip_find_separator(hdr, SIP_SEMI, SIP_COMMA,
0, B_FALSE) == 0) {
char t = *(hdr->sip_hdr_current);
value->strs2_val_len =
hdr->sip_hdr_current -
value->strs2_val_ptr;
if (t == SIP_COMMA)
goto get_next_val;
} else {
value->strs2_val_len =
hdr->sip_hdr_current -
value->strs2_val_ptr;
value->sip_value_end =
hdr->sip_hdr_current;
goto end;
}
} else if (sep != 0) {
value->sip_value_state = SIP_VALUE_BAD;
goto get_next_val;
}
if (c == SIP_SEMI) {
char *t = hdr->sip_hdr_current;
hdr->sip_hdr_current--;
(void) sip_reverse_skip_white_space(hdr);
value->str_val_ptr = value->sip_value_start;
value->str_val_len = hdr->sip_hdr_current -
value->str_val_ptr + 1;
hdr->sip_hdr_current = t;
}
ret = sip_parse_params(hdr, &value->sip_param_list);
if (ret == EPROTO) {
value->sip_value_state = SIP_VALUE_BAD;
} else if (ret != 0) {
sip_free_phdr(parsed_header);
return (ret);
}
goto get_next_val;
} else {
value->str_val_ptr = value->sip_value_start;
value->str_val_len = hdr->sip_hdr_current -
value->sip_value_start;
value->sip_value_end = hdr->sip_hdr_current;
goto end;
}
get_next_val:
if (sip_find_token(hdr, SIP_COMMA) != 0) {
value->sip_value_end = hdr->sip_hdr_current;
break;
}
value->sip_value_end = hdr->sip_hdr_current - 1;
last_value = value;
(void) sip_skip_white_space(hdr);
}
end:
*phdr = parsed_header;
hdr->sip_hdr_parsed = *phdr;
return (0);
}
int
sip_parse_hdr_parser2(_sip_header_t *hdr, sip_parsed_header_t **phdr,
int val_type)
{
sip_parsed_header_t *parsed_header;
int ret = 0;
sip_hdr_value_t *value = NULL;
if ((ret = sip_prim_parsers(hdr, phdr)) != 0)
return (ret);
if (*phdr != NULL) {
hdr->sip_hdr_parsed = *phdr;
return (0);
}
parsed_header = calloc(1, sizeof (sip_parsed_header_t));
if (parsed_header == NULL)
return (ENOMEM);
parsed_header->sip_parsed_header_version = SIP_PARSED_HEADER_VERSION_1;
parsed_header->sip_header = hdr;
value = calloc(1, sizeof (sip_hdr_value_t));
if (value == NULL) {
sip_free_phdr(parsed_header);
return (ENOMEM);
}
parsed_header->value = (sip_value_t *)value;
value->sip_value_start = hdr->sip_hdr_current;
value->sip_value_header = parsed_header;
ret = sip_atoi(hdr, &value->int_val);
if (ret != 0) {
value->int_val = 0;
value->sip_value_state = SIP_VALUE_BAD;
}
value->sip_value_end = hdr->sip_hdr_current - 1;
*phdr = parsed_header;
hdr->sip_hdr_parsed = *phdr;
return (0);
}
int
sip_parse_hdr_parser3(_sip_header_t *hdr, sip_parsed_header_t **phdr, int type,
boolean_t parse_uri)
{
sip_parsed_header_t *parsed_header;
sip_hdr_value_t *value = NULL;
sip_hdr_value_t *last_value = NULL;
int ret;
if ((ret = sip_prim_parsers(hdr, phdr)) != 0)
return (ret);
if (*phdr != NULL) {
hdr->sip_hdr_parsed = *phdr;
return (0);
}
parsed_header = calloc(1, sizeof (sip_parsed_header_t));
if (parsed_header == NULL)
return (ENOMEM);
parsed_header->sip_parsed_header_version = SIP_PARSED_HEADER_VERSION_1;
parsed_header->sip_header = hdr;
while (hdr->sip_hdr_current < hdr->sip_hdr_end) {
int r;
value = calloc(1, sizeof (sip_hdr_value_t));
if (value == NULL) {
sip_free_phdr(parsed_header);
return (ENOMEM);
}
if (last_value != NULL)
last_value->sip_next_value = value;
else
parsed_header->value = (sip_value_t *)value;
value->sip_value_start = hdr->sip_hdr_current;
value->sip_value_header = parsed_header;
if (type == SIP_STRS_VAL) {
if (sip_find_token(hdr, SIP_LAQUOT) == 0) {
char *cur;
cur = hdr->sip_hdr_current;
hdr->sip_hdr_current = value->sip_value_start;
if (*(hdr->sip_hdr_current) != SIP_LAQUOT) {
char *tmp = hdr->sip_hdr_current;
if (*hdr->sip_hdr_current ==
SIP_QUOTE) {
hdr->sip_hdr_current++;
tmp++;
if (sip_find_token(hdr,
SIP_QUOTE) != 0) {
value->sip_value_state =
SIP_VALUE_BAD;
goto get_next_val;
}
hdr->sip_hdr_current -= 2;
} else {
hdr->sip_hdr_current = cur - 2;
(void)
sip_reverse_skip_white_space
(hdr);
}
value->strs1_val_ptr = tmp;
value->strs1_val_len =
hdr->sip_hdr_current - tmp + 1;
} else {
value->strs1_val_ptr = NULL;
value->strs1_val_len = 0;
}
hdr->sip_hdr_current = cur;
value->strs2_val_ptr = hdr->sip_hdr_current;
if (sip_find_token(hdr, SIP_RAQUOT)) {
value->strs1_val_ptr = NULL;
value->strs1_val_len = 0;
value->strs2_val_ptr = NULL;
value->strs2_val_len = 0;
value->sip_value_state = SIP_VALUE_BAD;
goto get_next_val;
}
value->strs2_val_len = hdr->sip_hdr_current -
value->strs2_val_ptr - 1;
} else {
char *cur;
value->strs1_val_ptr = NULL;
value->strs1_val_len = 0;
cur = value->sip_value_start;
hdr->sip_hdr_current = cur;
if (sip_find_separator(hdr, SIP_COMMA,
0, 0, B_FALSE) != 0) {
value->strs2_val_ptr = cur;
value->strs2_val_len =
hdr->sip_hdr_current -
value->strs2_val_ptr - 1;
} else if (*hdr->sip_hdr_current == SIP_SP) {
value->strs2_val_ptr = cur;
cur = hdr->sip_hdr_current - 1;
if (sip_skip_white_space(hdr) != 0) {
value->strs2_val_len = cur -
value->strs2_val_ptr - 1;
} else if (*hdr->sip_hdr_current ==
SIP_COMMA) {
value->strs2_val_len = cur -
value->strs2_val_ptr - 1;
} else {
value->sip_value_state =
SIP_VALUE_BAD;
goto get_next_val;
}
} else {
value->strs2_val_ptr = cur;
value->strs2_val_len =
hdr->sip_hdr_current -
value->strs2_val_ptr;
}
}
if (parse_uri)
sip_parse_uri_str(&value->strs_s2, value);
}
if (type == SIP_STR_VAL) {
if (sip_find_token(hdr, SIP_LAQUOT) == 0) {
value->str_val_ptr = hdr->sip_hdr_current;
if (sip_find_token(hdr, SIP_RAQUOT) == 0) {
value->str_val_len =
hdr->sip_hdr_current -
value->str_val_ptr - 1;
} else {
value->str_val_ptr = NULL;
value->str_val_len = 0;
value->sip_value_state = SIP_VALUE_BAD;
goto get_next_val;
}
hdr->sip_hdr_current--;
} else {
value->str_val_ptr = NULL;
value->str_val_len = 0;
value->sip_value_state = SIP_VALUE_BAD;
goto get_next_val;
}
if (parse_uri)
sip_parse_uri_str(&value->str_val, value);
}
r = sip_find_separator(hdr, SIP_COMMA, SIP_SEMI, 0,
B_FALSE);
if (r != 0) {
value->sip_value_end = hdr->sip_hdr_current;
goto end;
}
if (*hdr->sip_hdr_current == SIP_SEMI) {
(void) sip_parse_params(hdr,
&(value->sip_param_list));
goto get_next_val;
}
if (*hdr->sip_hdr_current == SIP_COMMA) {
hdr->sip_hdr_current--;
goto get_next_val;
}
get_next_val:
if (sip_find_token(hdr, SIP_COMMA) != 0) {
value->sip_value_end = hdr->sip_hdr_current;
break;
}
value->sip_value_end = hdr->sip_hdr_current - 1;
last_value = value;
(void) sip_skip_white_space(hdr);
}
end:
*phdr = parsed_header;
hdr->sip_hdr_parsed = *phdr;
return (0);
}
int
sip_parse_hdr_parser4(_sip_header_t *hdr, sip_parsed_header_t **phdr)
{
sip_parsed_header_t *parsed_header;
sip_hdr_value_t *value = NULL;
int ret;
if ((ret = sip_prim_parsers(hdr, phdr)) != 0)
return (ret);
if (*phdr != NULL) {
hdr->sip_hdr_parsed = *phdr;
return (0);
}
parsed_header = calloc(1, sizeof (sip_parsed_header_t));
if (parsed_header == NULL)
return (ENOMEM);
parsed_header->sip_parsed_header_version = SIP_PARSED_HEADER_VERSION_1;
parsed_header->sip_header = hdr;
value = calloc(1, sizeof (sip_hdr_value_t));
if (value == NULL) {
sip_free_phdr(parsed_header);
return (ENOMEM);
}
parsed_header->value = (sip_value_t *)value;
value->sip_value_start = hdr->sip_hdr_current;
value->sip_value_header = parsed_header;
value->str_val_ptr = hdr->sip_hdr_current;
value->str_val_len = hdr->sip_hdr_end - value->str_val_ptr - 2;
value->sip_value_end = hdr->sip_hdr_end;
*phdr = parsed_header;
hdr->sip_hdr_parsed = *phdr;
return (0);
}
int
sip_parse_hdr_parser5(_sip_header_t *hdr, sip_parsed_header_t **phdr,
boolean_t parse_uri)
{
sip_parsed_header_t *parsed_header;
sip_hdr_value_t *value = NULL;
sip_param_t *tmp_param;
boolean_t first_param = B_TRUE;
int ret;
if ((ret = sip_prim_parsers(hdr, phdr)) != 0)
return (ret);
if (*phdr != NULL) {
hdr->sip_hdr_parsed = *phdr;
return (0);
}
parsed_header = calloc(1, sizeof (sip_parsed_header_t));
if (parsed_header == NULL)
return (ENOMEM);
parsed_header->sip_parsed_header_version = SIP_PARSED_HEADER_VERSION_1;
parsed_header->sip_header = hdr;
value = calloc(1, sizeof (sip_hdr_value_t));
if (value == NULL) {
sip_free_phdr(parsed_header);
return (ENOMEM);
}
parsed_header->value = (sip_value_t *)value;
value->sip_value_start = hdr->sip_hdr_current;
value->auth_scheme_ptr = value->sip_value_start;
value->sip_value_header = parsed_header;
if (sip_find_white_space(hdr)) {
value->sip_value_state = SIP_VALUE_BAD;
return (EINVAL);
}
value->auth_scheme_len = hdr->sip_hdr_current - value->auth_scheme_ptr;
tmp_param = value->auth_param;
for (;;) {
char *tmp_cur;
boolean_t quoted_name = B_FALSE;
char quoted_char = (char)0;
sip_param_t *new_param;
boolean_t pval_is_uri = B_FALSE;
if (sip_skip_white_space(hdr) != 0) {
value->sip_value_state = SIP_VALUE_BAD;
return (EPROTO);
}
tmp_cur = hdr->sip_hdr_current;
new_param = calloc(1, sizeof (sip_param_t));
if (new_param == NULL)
return (ENOMEM);
if (first_param == B_FALSE)
tmp_param->param_next = new_param;
else
value->auth_param = new_param;
tmp_param = new_param;
tmp_param->param_name.sip_str_ptr = tmp_cur;
if (sip_find_separator(hdr, SIP_EQUAL, SIP_COMMA, 0,
B_FALSE) != 0) {
tmp_param->param_name.sip_str_len =
hdr->sip_hdr_current - tmp_cur;
tmp_param->param_value.sip_str_ptr = NULL;
tmp_param->param_value.sip_str_len = 0;
value->sip_value_end = hdr->sip_hdr_current;
goto end;
}
tmp_param->param_name.sip_str_len = hdr->sip_hdr_current -
tmp_cur;
if (sip_skip_white_space(hdr) != 0 ||
*hdr->sip_hdr_current == SIP_COMMA) {
tmp_param->param_value.sip_str_ptr = NULL;
tmp_param->param_value.sip_str_len = 0;
continue;
}
hdr->sip_hdr_current++;
if (sip_skip_white_space(hdr) != 0) {
value->sip_value_state = SIP_VALUE_BAD;
free(tmp_param);
return (EPROTO);
}
if (*hdr->sip_hdr_current == SIP_QUOTE ||
*hdr->sip_hdr_current == SIP_LAQUOT) {
if (*hdr->sip_hdr_current == SIP_QUOTE)
quoted_char = SIP_QUOTE;
else {
quoted_char = SIP_RAQUOT;
pval_is_uri = B_TRUE;
}
hdr->sip_hdr_current++;
quoted_name = B_TRUE;
}
tmp_cur = hdr->sip_hdr_current;
tmp_param->param_value.sip_str_ptr = tmp_cur;
if (quoted_name) {
if (sip_find_token(hdr, quoted_char) != 0) {
value->sip_value_state = SIP_VALUE_BAD;
free(tmp_param);
return (EPROTO);
}
tmp_param->param_value.sip_str_len =
hdr->sip_hdr_current - tmp_cur - 1;
}
if (sip_find_token(hdr, SIP_COMMA) != 0) {
value->sip_value_end = hdr->sip_hdr_current;
goto end;
} else {
if (!quoted_name) {
char *t = hdr->sip_hdr_current;
hdr->sip_hdr_current--;
(void) sip_reverse_skip_white_space(hdr);
tmp_param->param_value.sip_str_len =
hdr->sip_hdr_current - tmp_cur;
hdr->sip_hdr_current = t;
}
}
if (first_param == B_TRUE)
first_param = B_FALSE;
if (pval_is_uri && parse_uri)
sip_parse_uri_str(&tmp_param->param_value, value);
}
end:
*phdr = parsed_header;
hdr->sip_hdr_parsed = *phdr;
return (0);
}
static int
_sip_get_request_uri(_sip_header_t *sip_header, sip_message_type_t *msg_info)
{
int size = 0;
char *start_ptr;
if (sip_skip_white_space(sip_header) != 0)
return (EINVAL);
start_ptr = sip_header->sip_hdr_current;
while (!isspace(*sip_header->sip_hdr_current)) {
if (sip_header->sip_hdr_current >= sip_header->sip_hdr_end)
return (EINVAL);
sip_header->sip_hdr_current++;
}
size = sip_header->sip_hdr_current - start_ptr;
msg_info->U.sip_request.sip_request_uri.sip_str_ptr = start_ptr;
msg_info->U.sip_request.sip_request_uri.sip_str_len = size;
if (size > 0) {
int error;
msg_info->U.sip_request.sip_parse_uri = sip_parse_uri(
&msg_info->U.sip_request.sip_request_uri, &error);
if (msg_info->U.sip_request.sip_parse_uri == NULL)
return (error);
}
return (0);
}
int
sip_parse_first_line(_sip_header_t *sip_header, sip_message_type_t **msg_info)
{
sip_message_type_t *sip_msg_info;
boolean_t sip_is_request = B_TRUE;
int ret;
if (sip_header == NULL || msg_info == NULL)
return (EINVAL);
if (sip_skip_white_space(sip_header) != 0)
return (EPROTO);
if (sip_header->sip_hdr_current + strlen(SIP_VERSION) >=
sip_header->sip_hdr_end) {
return (EPROTO);
}
#ifdef __solaris__
assert(mutex_held(&sip_header->sip_hdr_sipmsg->sip_msg_mutex));
#endif
sip_msg_info = malloc(sizeof (sip_message_type_t));
if (sip_msg_info == NULL)
return (ENOMEM);
ret = sip_get_protocol_version(sip_header,
&sip_msg_info->sip_proto_version);
if (ret == 0) {
sip_is_request = B_FALSE;
} else if (ret == 2) {
free(sip_msg_info);
return (EPROTO);
}
if (sip_skip_white_space(sip_header) != 0) {
free(sip_msg_info);
return (EPROTO);
}
if (!sip_is_request) {
if (sip_skip_white_space(sip_header) != 0) {
free(sip_msg_info);
return (EPROTO);
}
if (sip_header->sip_hdr_current + SIP_SIZE_OF_STATUS_CODE >=
sip_header->sip_hdr_end) {
free(sip_msg_info);
return (EPROTO);
}
if (sip_atoi(sip_header,
&sip_msg_info->U.sip_response.sip_response_code)) {
free(sip_msg_info);
return (EPROTO);
}
if (sip_msg_info->U.sip_response.sip_response_code < 100 ||
sip_msg_info->U.sip_response.sip_response_code > 700) {
free(sip_msg_info);
return (EPROTO);
}
if (sip_skip_white_space(sip_header) != 0) {
sip_msg_info->sip_resp_phrase_len = 0;
sip_msg_info->sip_resp_phrase_ptr = NULL;
} else {
sip_msg_info->sip_resp_phrase_ptr =
sip_header->sip_hdr_current;
if (sip_find_cr(sip_header) != 0) {
free(sip_msg_info);
return (EPROTO);
}
sip_msg_info->sip_resp_phrase_len =
sip_header->sip_hdr_current -
sip_msg_info->sip_resp_phrase_ptr;
}
sip_msg_info->is_request = B_FALSE;
} else {
int i;
sip_msg_info->is_request = B_TRUE;
for (i = 1; i < MAX_SIP_METHODS; i++) {
if (strncmp(sip_methods[i].name,
sip_header->sip_hdr_current,
sip_methods[i].len) == 0) {
sip_msg_info->sip_req_method = i;
sip_header->sip_hdr_current +=
sip_methods[i].len;
if (!isspace(*sip_header->sip_hdr_current++) ||
!isalpha(*sip_header->sip_hdr_current)) {
free(sip_msg_info);
return (EPROTO);
}
if ((ret = _sip_get_request_uri(sip_header,
sip_msg_info)) != 0) {
free(sip_msg_info);
return (ret);
}
ret = sip_get_protocol_version(sip_header,
&sip_msg_info->sip_proto_version);
if (ret != 0) {
free(sip_msg_info);
return (EPROTO);
}
goto done;
}
}
free(sip_msg_info);
return (EPROTO);
}
done:
sip_msg_info->sip_next = *msg_info;
*msg_info = sip_msg_info;
return (0);
}