Commit 4d07e229 authored by Pekka Pessi's avatar Pekka Pessi

stun: using isize_t, usize_t, and issize_t in API.

Internally, this looks like all mess.

darcs-hash:20060906194732-65a35-2d2179a81ffccf4b7f89d28aba1fc9e2e69f2af8.gz
parent 2b4bbc4e
......@@ -202,11 +202,11 @@ SOFIAPUBFUN su_addrinfo_t const *stun_server_address(stun_handle_t *sh);
SOFIAPUBFUN
int stun_process_message(stun_handle_t *sh, su_socket_t s,
su_sockaddr_t *sa, socklen_t salen,
void *data, int len);
void *data, isize_t len);
SOFIAPUBFUN
int stun_process_request(su_socket_t s, stun_msg_t *req,
int sid, su_sockaddr_t *from_addr,
int from_len);
socklen_t from_len);
/* -------------------------------------------------------------------
* Functions for 'Binding Discovery' usage (RFC3489/3489bis) */
......@@ -237,8 +237,8 @@ SOFIAPUBFUN stun_nattype_t stun_nattype(stun_discovery_t *sd);
SOFIAPUBFUN
int stun_test_lifetime(stun_handle_t *sh,
stun_discovery_f, stun_discovery_magic_t *magic,
tag_type_t tag, tag_value_t value, ...);
stun_discovery_f, stun_discovery_magic_t *magic,
tag_type_t tag, tag_value_t value, ...);
SOFIAPUBFUN int stun_lifetime(stun_discovery_t *sd);
/* -------------------------------------------------------------------
......@@ -247,12 +247,12 @@ SOFIAPUBFUN int stun_lifetime(stun_discovery_t *sd);
SOFIAPUBFUN
int stun_set_uname_pwd(stun_handle_t *sh,
const char *uname,
int len_uname,
isize_t len_uname,
const char *pwd,
int len_pwd);
isize_t len_pwd);
SOFIAPUBFUN int stun_msg_is_keepalive(uint16_t data);
SOFIAPUBFUN int stun_message_length(void *data, int len, int end_of_message);
SOFIAPUBFUN int stun_message_length(void *data, isize_t len, int end_of_message);
/* Create a keepalive dispatcher for bound SIP sockets */
......
......@@ -2598,19 +2598,27 @@ int stun_process_error_response(stun_msg_t *msg)
*/
int stun_set_uname_pwd(stun_handle_t *sh,
const char *uname,
int len_uname,
isize_t len_uname,
const char *pwd,
int len_pwd)
isize_t len_pwd)
{
enter;
sh->sh_username.data = (unsigned char *) malloc(len_uname);
memcpy(sh->sh_username.data, uname, len_uname);
sh->sh_username.size = len_uname;
sh->sh_username.data = malloc(len_uname);
if (sh->sh_username.data) {
memcpy(sh->sh_username.data, uname, len_uname);
sh->sh_username.size = len_uname;
}
else
return -1;
sh->sh_passwd.data = (unsigned char *) malloc(len_pwd);
memcpy(sh->sh_passwd.data, pwd, len_pwd);
sh->sh_passwd.size = len_pwd;
sh->sh_passwd.data = malloc(len_pwd);
if (sh->sh_passwd.data) {
memcpy(sh->sh_passwd.data, pwd, len_pwd);
sh->sh_passwd.size = len_pwd;
}
else
return -1;
sh->sh_use_msgint = 1; /* turn on message integrity ussage */
......@@ -2854,25 +2862,25 @@ int stun_msg_is_keepalive(uint16_t data)
}
/**
* Determines length of STUN message (0 if not stun).
* Determines length of STUN message (0 (-1?) if not stun).
*/
int stun_message_length(void *data, int len, int end_of_message)
int stun_message_length(void *data, isize_t len, int end_of_message)
{
unsigned char *p;
uint16_t tmp16, msg_type;
uint16_t msg_type;
if (len < 4)
return -1;
/* parse header first */
p = data;
memcpy(&tmp16, p, 2);
msg_type = ntohs(tmp16);
msg_type = (p[0] << 8) | p[1];
if (msg_type == BINDING_REQUEST ||
msg_type == BINDING_RESPONSE ||
msg_type == BINDING_ERROR_RESPONSE) {
p+=2;
memcpy(&tmp16, p, 2);
/* return message length */
return ntohs(tmp16);
return (p[0] << 8) | p[1];
}
else
return -1;
......@@ -2881,13 +2889,16 @@ int stun_message_length(void *data, int len, int end_of_message)
/** Process incoming message */
int stun_process_message(stun_handle_t *sh, su_socket_t s,
su_sockaddr_t *sa, socklen_t salen,
void *data, int len)
void *data, isize_t len)
{
int retval = -1;
stun_msg_t msg;
enter;
if (len >= 65536)
len = 65536;
/* Message received. */
msg.enc_buf.data = data;
msg.enc_buf.size = len;
......@@ -3059,7 +3070,7 @@ int stun_keepalive_destroy(stun_handle_t *sh, su_socket_t s)
int stun_process_request(su_socket_t s, stun_msg_t *req,
int sid, su_sockaddr_t *from_addr,
int from_len)
socklen_t from_len)
{
stun_msg_t resp;
su_sockaddr_t mod_addr[1] = {{ 0 }}, src_addr[1] = {{ 0 }}, chg_addr[1] = {{ 0 }};
......
......@@ -241,9 +241,10 @@ int stun_parse_attr_error_code(stun_attr_t *attr, const unsigned char *p, unsign
error->code = (tmp & STUN_EC_CLASS)*100 + (tmp & STUN_EC_NUM);
error->phrase = (char *) malloc(len-4);
error->phrase = (char *) malloc(len-3);
strncpy(error->phrase, (char*)p+4, len-4);
error->phrase[len - 4] = '\0';
attr->pattr = error;
stun_init_buffer(&attr->enc_buf);
......@@ -381,39 +382,36 @@ int stun_encode_uint32(stun_attr_t *attr) {
int stun_encode_error_code(stun_attr_t *attr) {
short int class, num;
char *reason;
int phrase_len, result;
size_t phrase_len, padded;
stun_attr_errorcode_t *error;
error = (stun_attr_errorcode_t *) attr->pattr;
class = error->code / 100;
num = error->code % 100;
phrase_len = strlen(error->phrase);
if (phrase_len + 8 > 65536)
phrase_len = 65536 - 8;
/* note: align the phrase len (see RFC3489:11.2.9) */
phrase_len += (phrase_len % 4 == 0 ? 0 : 4 - (phrase_len % 4));
reason = malloc(attr->enc_buf.size);
memset(reason, 0, attr->enc_buf.size);
memcpy(reason, error->phrase, phrase_len);
padded = phrase_len + (phrase_len % 4 == 0 ? 0 : 4 - (phrase_len % 4));
/* note: error-code has four octets of headers plus the
* reason field -> len+4 octets */
attr->enc_buf.size = phrase_len + 4;
assert(attr->enc_buf.size + 4 < 65536);
if (stun_encode_type_len(attr, (uint16_t)attr->enc_buf.size) < 0) {
result = -1;
if (stun_encode_type_len(attr, (uint16_t)(padded + 4)) < 0) {
return -1;
}
else {
assert(attr->enc_buf.size == padded + 8);
memset(attr->enc_buf.data+4, 0, 2);
memcpy(attr->enc_buf.data+6, &class, 1);
memcpy(attr->enc_buf.data+7, &num, 1);
attr->enc_buf.data[6] = class;
attr->enc_buf.data[7] = num;
/* note: 4 octets of TLV header and 4 octets of error-code header */
memcpy(attr->enc_buf.data+8, reason, attr->enc_buf.size - 8);
memcpy(attr->enc_buf.data+8, error->phrase,
phrase_len);
memset(attr->enc_buf.data + 8 + phrase_len, 0, padded - phrase_len);
}
free(reason);
return attr->enc_buf.size;
}
......
......@@ -92,10 +92,6 @@ extern char const STUN_DEBUG[]; /* dummy declaration for Doxygen */
int stun_is_requested(tag_type_t tag, tag_value_t value, ...);
/** other functions */
int stun_set_uname_pwd(stun_handle_t *se, const char *uname, int len_uname,
const char *pwd, int len_pwd);
/* internal functions declaration */
int stun_make_sharedsecret_req(stun_msg_t *msg);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment