Use bool, instead of int, for the functions in ns_turn_msg.c (#1553)

And address knockon effects in other files, e.g. adjust if-statements
and other function parameters and return types.
This commit is contained in:
Michael Jones 2024-08-23 19:49:14 -05:00 committed by GitHub
parent 858b088a88
commit b523616b1f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 539 additions and 839 deletions

View File

@ -66,36 +66,36 @@ void stun_tid_generate_in_message(stun_buffer *buf, stun_tid *id) {
////////////////////////////////////////////////////////
static inline int is_channel_msg(const stun_buffer *buf) {
static inline bool is_channel_msg(const stun_buffer *buf) {
if (buf && buf->len > 0) {
return is_channel_msg_str(buf->buf, (size_t)(buf->len));
}
return 0;
return false;
}
int stun_is_command_message(const stun_buffer *buf) {
bool stun_is_command_message(const stun_buffer *buf) {
if (!buf || buf->len <= 0) {
return 0;
return false;
} else {
return stun_is_command_message_str(buf->buf, (size_t)(buf->len));
}
}
int stun_is_request(const stun_buffer *buf) { return stun_is_request_str(buf->buf, (size_t)buf->len); }
bool stun_is_request(const stun_buffer *buf) { return stun_is_request_str(buf->buf, (size_t)buf->len); }
int stun_is_success_response(const stun_buffer *buf) {
bool stun_is_success_response(const stun_buffer *buf) {
return stun_is_success_response_str(buf->buf, (size_t)(buf->len));
}
int stun_is_error_response(const stun_buffer *buf, int *err_code, uint8_t *err_msg, size_t err_msg_size) {
bool stun_is_error_response(const stun_buffer *buf, int *err_code, uint8_t *err_msg, size_t err_msg_size) {
return stun_is_error_response_str(buf->buf, (size_t)(buf->len), err_code, err_msg, err_msg_size);
}
int stun_is_response(const stun_buffer *buf) { return stun_is_response_str(buf->buf, (size_t)(buf->len)); }
bool stun_is_response(const stun_buffer *buf) { return stun_is_response_str(buf->buf, (size_t)(buf->len)); }
int stun_is_indication(const stun_buffer *buf) {
bool stun_is_indication(const stun_buffer *buf) {
if (is_channel_msg(buf)) {
return 0;
return false;
}
return IS_STUN_INDICATION(stun_get_msg_type(buf));
}
@ -113,7 +113,7 @@ uint16_t stun_get_msg_type(const stun_buffer *buf) {
static void stun_init_command(uint16_t message_type, stun_buffer *buf) {
buf->len = stun_get_size(buf);
stun_init_command_str(message_type, buf->buf, (size_t *)(&(buf->len)));
stun_init_command_str(message_type, buf->buf, &(buf->len));
}
void stun_init_request(uint16_t method, stun_buffer *buf) { stun_init_command(stun_make_request(method), buf); }
@ -122,33 +122,33 @@ void stun_init_indication(uint16_t method, stun_buffer *buf) { stun_init_command
void stun_init_success_response(uint16_t method, stun_buffer *buf, stun_tid *id) {
buf->len = stun_get_size(buf);
stun_init_success_response_str(method, buf->buf, (size_t *)(&(buf->len)), id);
stun_init_success_response_str(method, buf->buf, &(buf->len), id);
}
void stun_init_error_response(uint16_t method, stun_buffer *buf, uint16_t error_code, const uint8_t *reason,
stun_tid *id) {
buf->len = stun_get_size(buf);
stun_init_error_response_str(method, buf->buf, (size_t *)(&(buf->len)), error_code, reason, id);
stun_init_error_response_str(method, buf->buf, &(buf->len), error_code, reason, id);
}
///////////////////////////////////////////////////////////////////////////////
int stun_get_command_message_len(const stun_buffer *buf) {
return stun_get_command_message_len_str(buf->buf, (size_t)(buf->len));
return stun_get_command_message_len_str(buf->buf, buf->len);
}
///////////////////////////////////////////////////////////////////////////////
int stun_init_channel_message(uint16_t chnumber, stun_buffer *buf, int length, int do_padding) {
return stun_init_channel_message_str(chnumber, buf->buf, (size_t *)(&(buf->len)), length, do_padding);
bool stun_init_channel_message(uint16_t chnumber, stun_buffer *buf, int length, bool do_padding) {
return stun_init_channel_message_str(chnumber, buf->buf, &(buf->len), length, do_padding);
}
int stun_is_channel_message(stun_buffer *buf, uint16_t *chnumber, int is_padding_mandatory) {
bool stun_is_channel_message(stun_buffer *buf, uint16_t *chnumber, bool is_padding_mandatory) {
if (!buf) {
return 0;
return false;
}
size_t blen = (size_t)buf->len;
int ret = stun_is_channel_message_str(buf->buf, &blen, chnumber, is_padding_mandatory);
size_t blen = buf->len;
bool ret = stun_is_channel_message_str(buf->buf, &blen, chnumber, is_padding_mandatory);
if (ret) {
buf->len = blen;
}
@ -157,66 +157,60 @@ int stun_is_channel_message(stun_buffer *buf, uint16_t *chnumber, int is_padding
///////////////////////////////////////////////////////////////////////////////
int stun_set_allocate_request(stun_buffer *buf, uint32_t lifetime, int af4, int af6, uint8_t transport, int mobile,
const char *rt, int ep) {
return stun_set_allocate_request_str(buf->buf, (size_t *)(&(buf->len)), lifetime, af4, af6, transport, mobile, rt,
ep);
bool stun_set_allocate_request(stun_buffer *buf, uint32_t lifetime, bool af4, bool af6, uint8_t transport, bool mobile,
const char *rt, int ep) {
return stun_set_allocate_request_str(buf->buf, &(buf->len), lifetime, af4, af6, transport, mobile, rt, ep);
}
int stun_set_allocate_response(stun_buffer *buf, stun_tid *tid, const ioa_addr *relayed_addr1,
const ioa_addr *relayed_addr2, const ioa_addr *reflexive_addr, uint32_t lifetime,
uint32_t max_lifetime, int error_code, const uint8_t *reason, uint64_t reservation_token,
char *mobile_id) {
bool stun_set_allocate_response(stun_buffer *buf, stun_tid *tid, const ioa_addr *relayed_addr1,
const ioa_addr *relayed_addr2, const ioa_addr *reflexive_addr, uint32_t lifetime,
uint32_t max_lifetime, int error_code, const uint8_t *reason,
uint64_t reservation_token, char *mobile_id) {
return stun_set_allocate_response_str(buf->buf, (size_t *)(&(buf->len)), tid, relayed_addr1, relayed_addr2,
reflexive_addr, lifetime, max_lifetime, error_code, reason, reservation_token,
mobile_id);
return stun_set_allocate_response_str(buf->buf, &(buf->len), tid, relayed_addr1, relayed_addr2, reflexive_addr,
lifetime, max_lifetime, error_code, reason, reservation_token, mobile_id);
}
///////////////////////////////////////////////////////////////////////////////
uint16_t stun_set_channel_bind_request(stun_buffer *buf, const ioa_addr *peer_addr, uint16_t channel_number) {
return stun_set_channel_bind_request_str(buf->buf, (size_t *)(&(buf->len)), peer_addr, channel_number);
return stun_set_channel_bind_request_str(buf->buf, &(buf->len), peer_addr, channel_number);
}
void stun_set_channel_bind_response(stun_buffer *buf, stun_tid *tid, int error_code, const uint8_t *reason) {
stun_set_channel_bind_response_str(buf->buf, (size_t *)(&(buf->len)), tid, error_code, reason);
stun_set_channel_bind_response_str(buf->buf, &(buf->len), tid, error_code, reason);
}
////////////////////////////////////////////////////////////////
stun_attr_ref stun_attr_get_first(const stun_buffer *buf) {
return stun_attr_get_first_str(buf->buf, (size_t)(buf->len));
}
stun_attr_ref stun_attr_get_first(const stun_buffer *buf) { return stun_attr_get_first_str(buf->buf, buf->len); }
stun_attr_ref stun_attr_get_next(const stun_buffer *buf, stun_attr_ref prev) {
return stun_attr_get_next_str(buf->buf, (size_t)(buf->len), prev);
return stun_attr_get_next_str(buf->buf, buf->len, prev);
}
int stun_attr_add(stun_buffer *buf, uint16_t attr, const char *avalue, int alen) {
return stun_attr_add_str(buf->buf, (size_t *)(&(buf->len)), attr, (const uint8_t *)avalue, alen);
bool stun_attr_add(stun_buffer *buf, uint16_t attr, const char *avalue, int alen) {
return stun_attr_add_str(buf->buf, &(buf->len), attr, (const uint8_t *)avalue, alen);
}
int stun_attr_add_channel_number(stun_buffer *buf, uint16_t chnumber) {
return stun_attr_add_channel_number_str(buf->buf, (size_t *)(&(buf->len)), chnumber);
bool stun_attr_add_channel_number(stun_buffer *buf, uint16_t chnumber) {
return stun_attr_add_channel_number_str(buf->buf, &(buf->len), chnumber);
}
int stun_attr_add_addr(stun_buffer *buf, uint16_t attr_type, const ioa_addr *ca) {
return stun_attr_add_addr_str(buf->buf, (size_t *)(&(buf->len)), attr_type, ca);
bool stun_attr_add_addr(stun_buffer *buf, uint16_t attr_type, const ioa_addr *ca) {
return stun_attr_add_addr_str(buf->buf, &(buf->len), attr_type, ca);
}
int stun_attr_get_addr(const stun_buffer *buf, stun_attr_ref attr, ioa_addr *ca, const ioa_addr *default_addr) {
return stun_attr_get_addr_str(buf->buf, (size_t)(buf->len), attr, ca, default_addr);
bool stun_attr_get_addr(const stun_buffer *buf, stun_attr_ref attr, ioa_addr *ca, const ioa_addr *default_addr) {
return stun_attr_get_addr_str(buf->buf, buf->len, attr, ca, default_addr);
}
int stun_attr_get_first_addr(const stun_buffer *buf, uint16_t attr_type, ioa_addr *ca, const ioa_addr *default_addr) {
return stun_attr_get_first_addr_str(buf->buf, (size_t)(buf->len), attr_type, ca, default_addr);
bool stun_attr_get_first_addr(const stun_buffer *buf, uint16_t attr_type, ioa_addr *ca, const ioa_addr *default_addr) {
return stun_attr_get_first_addr_str(buf->buf, buf->len, attr_type, ca, default_addr);
}
int stun_attr_add_even_port(stun_buffer *buf, uint8_t value) {
bool stun_attr_add_even_port(stun_buffer *buf, uint8_t value) {
if (value) {
value = 0x80;
}
@ -224,27 +218,24 @@ int stun_attr_add_even_port(stun_buffer *buf, uint8_t value) {
}
uint16_t stun_attr_get_first_channel_number(const stun_buffer *buf) {
return stun_attr_get_first_channel_number_str(buf->buf, (size_t)(buf->len));
return stun_attr_get_first_channel_number_str(buf->buf, buf->len);
}
stun_attr_ref stun_attr_get_first_by_type(const stun_buffer *buf, uint16_t attr_type) {
return stun_attr_get_first_by_type_str(buf->buf, (size_t)(buf->len), attr_type);
return stun_attr_get_first_by_type_str(buf->buf, buf->len, attr_type);
}
///////////////////////////////////////////////////////////////////////////////
void stun_set_binding_request(stun_buffer *buf) { stun_set_binding_request_str(buf->buf, (size_t *)(&(buf->len))); }
int stun_set_binding_response(stun_buffer *buf, stun_tid *tid, const ioa_addr *reflexive_addr, int error_code,
const uint8_t *reason) {
return stun_set_binding_response_str(buf->buf, (size_t *)(&(buf->len)), tid, reflexive_addr, error_code, reason, 0, 0,
1);
bool stun_set_binding_response(stun_buffer *buf, stun_tid *tid, const ioa_addr *reflexive_addr, int error_code,
const uint8_t *reason) {
return stun_set_binding_response_str(buf->buf, &(buf->len), tid, reflexive_addr, error_code, reason, 0, false, true);
}
void stun_prepare_binding_request(stun_buffer *buf) { stun_set_binding_request_str(buf->buf, (size_t *)(&(buf->len))); }
int stun_is_binding_response(const stun_buffer *buf) {
return stun_is_binding_response_str(buf->buf, (size_t)(buf->len));
}
bool stun_is_binding_response(const stun_buffer *buf) { return stun_is_binding_response_str(buf->buf, buf->len); }
///////////////////////////////////////////////////////

View File

@ -62,12 +62,12 @@ void stun_tid_from_message(const stun_buffer *buf, stun_tid *id);
///////////////////////////////////////////////////////////////
int stun_is_command_message(const stun_buffer *buf);
int stun_is_request(const stun_buffer *buf);
int stun_is_response(const stun_buffer *buf);
int stun_is_success_response(const stun_buffer *buf);
int stun_is_error_response(const stun_buffer *buf, int *err_code, uint8_t *err_msg, size_t err_msg_size);
int stun_is_indication(const stun_buffer *buf);
bool stun_is_command_message(const stun_buffer *buf);
bool stun_is_request(const stun_buffer *buf);
bool stun_is_response(const stun_buffer *buf);
bool stun_is_success_response(const stun_buffer *buf);
bool stun_is_error_response(const stun_buffer *buf, int *err_code, uint8_t *err_msg, size_t err_msg_size);
bool stun_is_indication(const stun_buffer *buf);
uint16_t stun_get_method(const stun_buffer *buf);
uint16_t stun_get_msg_type(const stun_buffer *buf);
@ -81,17 +81,17 @@ void stun_init_error_response(uint16_t method, stun_buffer *buf, uint16_t error_
///////////////////////////////////////////////////////////////
int stun_attr_add(stun_buffer *buf, uint16_t attr, const char *avalue, int alen);
int stun_attr_add_channel_number(stun_buffer *buf, uint16_t chnumber);
int stun_attr_add_addr(stun_buffer *buf, uint16_t attr_type, const ioa_addr *ca);
bool stun_attr_add(stun_buffer *buf, uint16_t attr, const char *avalue, int alen);
bool stun_attr_add_channel_number(stun_buffer *buf, uint16_t chnumber);
bool stun_attr_add_addr(stun_buffer *buf, uint16_t attr_type, const ioa_addr *ca);
stun_attr_ref stun_attr_get_first(const stun_buffer *buf);
stun_attr_ref stun_attr_get_first_by_type(const stun_buffer *buf, uint16_t attr_type);
stun_attr_ref stun_attr_get_next(const stun_buffer *buf, stun_attr_ref prev);
int stun_attr_get_addr(const stun_buffer *buf, stun_attr_ref attr, ioa_addr *ca, const ioa_addr *default_addr);
int stun_attr_add_even_port(stun_buffer *buf, uint8_t value);
bool stun_attr_get_addr(const stun_buffer *buf, stun_attr_ref attr, ioa_addr *ca, const ioa_addr *default_addr);
bool stun_attr_add_even_port(stun_buffer *buf, uint8_t value);
int stun_attr_get_first_addr(const stun_buffer *buf, uint16_t attr_type, ioa_addr *ca, const ioa_addr *default_addr);
bool stun_attr_get_first_addr(const stun_buffer *buf, uint16_t attr_type, ioa_addr *ca, const ioa_addr *default_addr);
uint16_t stun_attr_get_first_channel_number(const stun_buffer *buf);
///////////////////////////////////////////////////////////////
@ -100,26 +100,26 @@ int stun_get_command_message_len(const stun_buffer *buf);
///////////////////////////////////////////////////////////////
int stun_init_channel_message(uint16_t chnumber, stun_buffer *buf, int length, int do_padding);
int stun_is_channel_message(stun_buffer *buf, uint16_t *chnumber, int is_padding_madatory);
bool stun_init_channel_message(uint16_t chnumber, stun_buffer *buf, int length, bool do_padding);
bool stun_is_channel_message(stun_buffer *buf, uint16_t *chnumber, bool is_padding_madatory);
///////////////////////////////////////////////////////////////
int stun_set_allocate_request(stun_buffer *buf, uint32_t lifetime, int af4, int af6, uint8_t transport, int mobile,
const char *rt, int ep);
int stun_set_allocate_response(stun_buffer *buf, stun_tid *tid, const ioa_addr *relayed_addr1,
const ioa_addr *relayed_addr2, const ioa_addr *reflexive_addr, uint32_t lifetime,
uint32_t max_lifetime, int error_code, const uint8_t *reason, uint64_t reservation_token,
char *mobile_id);
bool stun_set_allocate_request(stun_buffer *buf, uint32_t lifetime, bool af4, bool af6, uint8_t transport, bool mobile,
const char *rt, int ep);
bool stun_set_allocate_response(stun_buffer *buf, stun_tid *tid, const ioa_addr *relayed_addr1,
const ioa_addr *relayed_addr2, const ioa_addr *reflexive_addr, uint32_t lifetime,
uint32_t max_lifetime, int error_code, const uint8_t *reason,
uint64_t reservation_token, char *mobile_id);
///////////////////////////////////////////////////////////////
void stun_set_binding_request(stun_buffer *buf);
int stun_set_binding_response(stun_buffer *buf, stun_tid *tid, const ioa_addr *reflexive_addr, int error_code,
const uint8_t *reason);
bool stun_set_binding_response(stun_buffer *buf, stun_tid *tid, const ioa_addr *reflexive_addr, int error_code,
const uint8_t *reason);
void stun_prepare_binding_request(stun_buffer *buf);
int stun_is_binding_response(const stun_buffer *buf);
bool stun_is_binding_response(const stun_buffer *buf);
///////////////////////////////////////////////////////////////

View File

@ -371,7 +371,7 @@ static int stunclient_send(stun_buffer *buf, int sockfd, ioa_addr *local_addr, i
stun_attr_add_change_request_str((uint8_t *)buf->buf, (size_t *)&(buf->len), change_ip, change_port);
}
if (padding) {
if (stun_attr_add_padding_str((uint8_t *)buf->buf, (size_t *)&(buf->len), 1500) < 0) {
if (!stun_attr_add_padding_str((uint8_t *)buf->buf, (size_t *)&(buf->len), 1500)) {
printf("%s: ERROR: Cannot add padding\n", __FUNCTION__);
}
}
@ -437,8 +437,7 @@ static int stunclient_receive(stun_buffer *buf, int sockfd, ioa_addr *local_addr
if (stun_is_binding_response(buf)) {
addr_set_any(reflexive_addr);
if (stun_attr_get_first_addr(buf, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, reflexive_addr, NULL) >= 0) {
if (stun_attr_get_first_addr(buf, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, reflexive_addr, NULL)) {
stun_attr_ref sar = stun_attr_get_first_by_type_str(buf->buf, buf->len, STUN_ATTRIBUTE_OTHER_ADDRESS);
if (sar) {
*rfc5780 = true;
@ -446,7 +445,7 @@ static int stunclient_receive(stun_buffer *buf, int sockfd, ioa_addr *local_addr
printf("RFC 5780 response %d\n", ++counter);
ioa_addr mapped_addr;
addr_set_any(&mapped_addr);
if (stun_attr_get_first_addr(buf, STUN_ATTRIBUTE_MAPPED_ADDRESS, &mapped_addr, NULL) >= 0) {
if (stun_attr_get_first_addr(buf, STUN_ATTRIBUTE_MAPPED_ADDRESS, &mapped_addr, NULL)) {
if (!addr_eq(&mapped_addr, reflexive_addr)) {
printf("-= ALG detected! Mapped and XOR-Mapped differ! =-\n");
addr_debug_print(1, &mapped_addr, "Mapped Address: ");

View File

@ -84,7 +84,7 @@ static int setup_ikm_key(const char *kid, const char *ikm_key, const turn_time_t
char err_msg[1025] = "\0";
size_t err_msg_size = sizeof(err_msg) - 1;
if (convert_oauth_key_data(&okd, key, err_msg, err_msg_size) < 0) {
if (!convert_oauth_key_data(&okd, key, err_msg, err_msg_size)) {
fprintf(stderr, "%s\n", err_msg);
return -1;
}
@ -113,7 +113,7 @@ static int encode_token(const char *server_name, const char *gcm_nonce, const ch
gcm_nonce = NULL;
}
if (encode_oauth_token((const uint8_t *)server_name, &etoken, &key, &ot, (const uint8_t *)gcm_nonce) < 0) {
if (!encode_oauth_token((const uint8_t *)server_name, &etoken, &key, &ot, (const uint8_t *)gcm_nonce)) {
fprintf(stderr, "%s: cannot encode oauth token\n", __FUNCTION__);
return -1;
}
@ -139,7 +139,7 @@ static int validate_decode_token(const char *server_name, const oauth_key key, c
memcpy(etoken.token, tmp, etoken.size);
free(tmp);
if (decode_oauth_token((const uint8_t *)server_name, &etoken, &key, dot) < 0) {
if (!decode_oauth_token((const uint8_t *)server_name, &etoken, &key, dot)) {
fprintf(stderr, "%s: cannot decode oauth token\n", __FUNCTION__);
return -1;
} else {

View File

@ -2693,7 +2693,7 @@ static int adminmain(int argc, char **argv) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong user name structure or symbols, choose another name: %s\n", user);
exit(-1);
}
if (SASLprep((uint8_t *)user) < 0) {
if (!SASLprep((uint8_t *)user)) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong user name: %s\n", user);
exit(-1);
}
@ -2701,14 +2701,14 @@ static int adminmain(int argc, char **argv) {
case 'r':
set_default_realm_name(optarg);
STRCPY(realm, optarg);
if (SASLprep((uint8_t *)realm) < 0) {
if (!SASLprep((uint8_t *)realm)) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong realm: %s\n", realm);
exit(-1);
}
break;
case 'p':
STRCPY(pwd, optarg);
if (SASLprep((uint8_t *)pwd) < 0) {
if (!SASLprep((uint8_t *)pwd)) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong password: %s\n", pwd);
exit(-1);
}

View File

@ -960,7 +960,7 @@ static int run_cli_input(struct cli_session *cs, const char *buf0, unsigned int
if (sl) {
cs->cmds += 1;
if (cli_password[0] && !(cs->auth_completed)) {
if (check_password(cmd, cli_password)) {
if (check_password_equal(cmd, cli_password)) {
if (cs->cmds >= CLI_PASSWORD_TRY_NUMBER) {
addr_debug_print(1, &(cs->addr), "CLI authentication error");
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "CLI authentication error\n");
@ -3027,7 +3027,7 @@ static void write_https_oauth_show_keys(ioa_socket_handle s, const char *kid) {
oauth_key okey;
memset(&okey, 0, sizeof(okey));
if (convert_oauth_key_data(&okd, &okey, err_msg, err_msg_size) < 0) {
if (!convert_oauth_key_data(&okd, &okey, err_msg, err_msg_size)) {
str_buffer_append(sb, err_msg);
} else {
@ -3312,7 +3312,7 @@ static void handle_logon_request(ioa_socket_handle s, struct http_request *hr) {
password_t password;
char realm[STUN_MAX_REALM_SIZE + 1] = "\0";
if ((*(dbd->get_admin_user))((const uint8_t *)uname, (uint8_t *)realm, password) >= 0) {
if (!check_password(pwd, (char *)password)) {
if (!check_password_equal(pwd, (char *)password)) {
STRCPY(as->as_login, uname);
STRCPY(as->as_realm, realm);
as->as_eff_realm[0] = 0;

View File

@ -449,7 +449,7 @@ int get_user_key(int in_oauth, int *out_oauth, int *max_session_time, uint8_t *u
oauth_key okey;
memset(&okey, 0, sizeof(okey));
if (convert_oauth_key_data(&okd, &okey, err_msg, err_msg_size) < 0) {
if (!convert_oauth_key_data(&okd, &okey, err_msg, err_msg_size)) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s\n", err_msg);
return -1;
}
@ -476,7 +476,7 @@ int get_user_key(int in_oauth, int *out_oauth, int *max_session_time, uint8_t *u
}
}
if (decode_oauth_token((const uint8_t *)server_name, &etoken, &okey, &dot) < 0) {
if (!decode_oauth_token((const uint8_t *)server_name, &etoken, &okey, &dot)) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot decode oauth token\n");
return -1;
}
@ -576,7 +576,7 @@ int get_user_key(int in_oauth, int *out_oauth, int *max_session_time, uint8_t *u
if (secret) {
if (stun_calculate_hmac(usname, strlen((char *)usname), (const uint8_t *)secret, strlen(secret), hmac,
&hmac_len, SHATYPE_DEFAULT) >= 0) {
&hmac_len, SHATYPE_DEFAULT)) {
size_t pwd_length = 0;
char *pwd = base64_encode(hmac, hmac_len, &pwd_length);
@ -584,9 +584,7 @@ int get_user_key(int in_oauth, int *out_oauth, int *max_session_time, uint8_t *u
if (pwd_length < 1) {
free(pwd);
} else {
if (stun_produce_integrity_key_str((uint8_t *)usname, realm, (uint8_t *)pwd, key, SHATYPE_DEFAULT) >=
0) {
if (stun_produce_integrity_key_str((uint8_t *)usname, realm, (uint8_t *)pwd, key, SHATYPE_DEFAULT)) {
if (stun_check_message_integrity_by_key_str(TURN_CREDENTIALS_LONG_TERM, ioa_network_buffer_data(nbh),
ioa_network_buffer_get_size(nbh), key, pwdtmp,
SHATYPE_DEFAULT) > 0) {
@ -740,7 +738,7 @@ int add_static_user_account(char *user) {
strncpy(usname, user, ulen);
usname[ulen] = 0;
if (SASLprep((uint8_t *)usname) < 0) {
if (!SASLprep((uint8_t *)usname)) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong user name: %s\n", user);
free(usname);
return -1;
@ -965,11 +963,10 @@ int adminuser(uint8_t *user, uint8_t *realm, uint8_t *pwd, uint8_t *secret, uint
{
stun_produce_integrity_key_str(user, realm, pwd, key, SHATYPE_DEFAULT);
size_t i = 0;
size_t sz = get_hmackey_size(SHATYPE_DEFAULT);
int maxsz = (int)(sz * 2) + 1;
char *s = skey;
for (i = 0; (i < sz) && (maxsz > 2); i++) {
for (size_t i = 0; (i < sz) && (maxsz > 2); i++) {
snprintf(s, (size_t)(sz * 2), "%02x", (unsigned int)key[i]);
maxsz -= 2;
s += 2;
@ -1078,7 +1075,7 @@ void run_db_test(void) {
oauth_key oak;
char err_msg[1025];
err_msg[0] = 0;
if (convert_oauth_key_data(&oakd, &oak, err_msg, sizeof(err_msg) - 1) < 0) {
if (!convert_oauth_key_data(&oakd, &oak, err_msg, sizeof(err_msg) - 1)) {
printf(" ERROR: %s\n", err_msg);
} else {
printf(" OK!\n");

View File

@ -127,7 +127,7 @@ static int check_oauth(void) {
char err_msg[1025] = "\0";
size_t err_msg_size = sizeof(err_msg) - 1;
if (convert_oauth_key_data(&okd, &key, err_msg, err_msg_size) < 0) {
if (!convert_oauth_key_data(&okd, &key, err_msg, err_msg_size)) {
fprintf(stderr, "%s\n", err_msg);
goto err;
}
@ -143,7 +143,7 @@ static int check_oauth(void) {
encoded_oauth_token etoken;
memset(&etoken, 0, sizeof(etoken));
if (encode_oauth_token((const uint8_t *)server_name, &etoken, &key, &ot, (const uint8_t *)gcm_nonce) < 0) {
if (!encode_oauth_token((const uint8_t *)server_name, &etoken, &key, &ot, (const uint8_t *)gcm_nonce)) {
fprintf(stderr, "%s: cannot encode oauth token\n", __FUNCTION__);
goto err;
}
@ -152,7 +152,7 @@ static int check_oauth(void) {
print_field5769("encoded token", etoken.token, etoken.size);
}
if (decode_oauth_token((const uint8_t *)server_name, &etoken, &key, &dot) < 0) {
if (!decode_oauth_token((const uint8_t *)server_name, &etoken, &key, &dot)) {
fprintf(stderr, "%s: cannot decode oauth token\n", __FUNCTION__);
goto err;
}
@ -459,8 +459,7 @@ int main(int argc, const char **argv) {
printf("RFC 5769 IPv4 encoding result: ");
res = stun_attr_get_first_addr_str(buf, sizeof(respv4) - 1, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, &addr4, NULL);
if (res < 0) {
if (!stun_attr_get_first_addr_str(buf, sizeof(respv4) - 1, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, &addr4, NULL)) {
printf("failure on message structure check\n");
exit(-1);
}
@ -548,8 +547,7 @@ int main(int argc, const char **argv) {
printf("RFC 5769 IPv6 encoding result: ");
res = stun_attr_get_first_addr_str(buf, sizeof(respv6) - 1, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, &addr6, NULL);
if (res < 0) {
if (!stun_attr_get_first_addr_str(buf, sizeof(respv6) - 1, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, &addr6, NULL)) {
printf("failure on message structure check\n");
exit(-1);
}

View File

@ -307,7 +307,7 @@ static int run_stunclient(const char *rip, int rport, int *port, bool *rfc5780,
stun_attr_add_change_request_str((uint8_t *)buf.buf, (size_t *)&(buf.len), change_ip, change_port);
}
if (padding) {
if (stun_attr_add_padding_str((uint8_t *)buf.buf, (size_t *)&(buf.len), 1500) < 0) {
if (!stun_attr_add_padding_str((uint8_t *)buf.buf, (size_t *)&(buf.len), 1500)) {
printf("%s: ERROR: Cannot add padding\n", __FUNCTION__);
}
}
@ -369,7 +369,7 @@ static int run_stunclient(const char *rip, int rport, int *port, bool *rfc5780,
ioa_addr reflexive_addr;
addr_set_any(&reflexive_addr);
if (stun_attr_get_first_addr(&buf, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, &reflexive_addr, NULL) >= 0) {
if (stun_attr_get_first_addr(&buf, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, &reflexive_addr, NULL)) {
stun_attr_ref sar = stun_attr_get_first_by_type_str(buf.buf, buf.len, STUN_ATTRIBUTE_OTHER_ADDRESS);
if (sar) {

View File

@ -210,17 +210,17 @@ int main(int argc, char **argv) {
char err_msg[1025] = "\0";
size_t err_msg_size = sizeof(err_msg) - 1;
if (convert_oauth_key_data(&okd_array[0], &okey_array[0], err_msg, err_msg_size) < 0) {
if (!convert_oauth_key_data(&okd_array[0], &okey_array[0], err_msg, err_msg_size)) {
fprintf(stderr, "%s\n", err_msg);
exit(-1);
}
if (convert_oauth_key_data(&okd_array[1], &okey_array[1], err_msg, err_msg_size) < 0) {
if (!convert_oauth_key_data(&okd_array[1], &okey_array[1], err_msg, err_msg_size)) {
fprintf(stderr, "%s\n", err_msg);
exit(-1);
}
if (convert_oauth_key_data(&okd_array[2], &okey_array[2], err_msg, err_msg_size) < 0) {
if (!convert_oauth_key_data(&okd_array[2], &okey_array[2], err_msg, err_msg_size)) {
fprintf(stderr, "%s\n", err_msg);
exit(-1);
}
@ -418,7 +418,7 @@ int main(int argc, char **argv) {
hmac[0] = 0;
if (stun_calculate_hmac(g_uname, strlen((char *)g_uname), (uint8_t *)g_auth_secret, strlen(g_auth_secret), hmac,
&hmac_len, shatype) >= 0) {
&hmac_len, shatype)) {
size_t pwd_length = 0;
char *pwd = base64_encode(hmac, hmac_len, &pwd_length);

View File

@ -74,8 +74,7 @@ typedef struct {
uint8_t nonce[STUN_MAX_NONCE_SIZE + 1];
uint8_t realm[STUN_MAX_REALM_SIZE + 1];
/* oAuth */
int oauth; // Cannot (yet) be converted to bool, as many functions take an int* instead of bool*, those must be
// addressed first.
bool oauth;
uint8_t server_name[STUN_MAX_SERVER_NAME_SIZE + 1];
hmackey_t key;
bool key_set;

View File

@ -487,7 +487,7 @@ beg_allocate:
int attr_type = stun_attr_get_type(sar);
if (attr_type == STUN_ATTRIBUTE_XOR_RELAYED_ADDRESS) {
if (stun_attr_get_addr(&response_message, sar, relay_addr, NULL) < 0) {
if (!stun_attr_get_addr(&response_message, sar, relay_addr, NULL)) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: !!!: relay addr cannot be received (1)\n", __FUNCTION__);
return -1;
} else {
@ -551,8 +551,8 @@ beg_allocate:
}
ioa_addr alternate_server;
if (stun_attr_get_first_addr(&response_message, STUN_ATTRIBUTE_ALTERNATE_SERVER, &alternate_server,
NULL) == -1) {
if (!stun_attr_get_first_addr(&response_message, STUN_ATTRIBUTE_ALTERNATE_SERVER, &alternate_server,
NULL)) {
// error
} else if (turn_addr && turn_port) {
addr_to_string_no_port(&alternate_server, (uint8_t *)turn_addr);
@ -1549,8 +1549,8 @@ beg_cb:
TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "success\n");
}
atc->tcp_data_bound = true;
} else if (stun_is_challenge_response_str(response_message.buf, (size_t)response_message.len, &err_code,
err_msg, sizeof(err_msg), clnet_info->realm, clnet_info->nonce,
} else if (stun_is_challenge_response_str(response_message.buf, response_message.len, &err_code, err_msg,
sizeof(err_msg), clnet_info->realm, clnet_info->nonce,
clnet_info->server_name, &(clnet_info->oauth))) {
goto beg_cb;
} else if (stun_is_error_response(&response_message, &err_code, err_msg, sizeof(err_msg))) {

View File

@ -756,7 +756,7 @@ static int client_read(app_ur_session *elem, int is_tcp_data, app_tcp_conn_info
}
return rc;
} else if (stun_is_challenge_response_str(elem->in_buffer.buf, (size_t)elem->in_buffer.len, &err_code, err_msg,
} else if (stun_is_challenge_response_str(elem->in_buffer.buf, elem->in_buffer.len, &err_code, err_msg,
sizeof(err_msg), clnet_info->realm, clnet_info->nonce,
clnet_info->server_name, &(clnet_info->oauth))) {
if (is_TCP_relay() && (stun_get_method(&(elem->in_buffer)) == STUN_METHOD_CONNECT)) {
@ -1639,19 +1639,19 @@ int add_integrity(app_ur_conn_info *clnet_info, stun_buffer *message) {
otoken.enc_block.key_length = 20;
}
RAND_bytes((unsigned char *)(otoken.enc_block.mac_key), otoken.enc_block.key_length);
if (encode_oauth_token(clnet_info->server_name, &etoken, &(okey_array[cok]), &otoken, nonce) < 0) {
if (!encode_oauth_token(clnet_info->server_name, &etoken, &(okey_array[cok]), &otoken, nonce)) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, " Cannot encode token\n");
return -1;
}
stun_attr_add_str(message->buf, (size_t *)&(message->len), STUN_ATTRIBUTE_OAUTH_ACCESS_TOKEN,
stun_attr_add_str(message->buf, &(message->len), STUN_ATTRIBUTE_OAUTH_ACCESS_TOKEN,
(const uint8_t *)etoken.token, (int)etoken.size);
memcpy(clnet_info->key, otoken.enc_block.mac_key, otoken.enc_block.key_length);
clnet_info->key_set = true;
}
if (stun_attr_add_integrity_by_key_str(message->buf, (size_t *)&(message->len), (uint8_t *)okey_array[cok].kid,
clnet_info->realm, clnet_info->key, clnet_info->nonce, shatype) < 0) {
if (!stun_attr_add_integrity_by_key_str(message->buf, &(message->len), (uint8_t *)okey_array[cok].kid,
clnet_info->realm, clnet_info->key, clnet_info->nonce, shatype)) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, " Cannot add integrity to the message\n");
return -1;
}
@ -1659,15 +1659,15 @@ int add_integrity(app_ur_conn_info *clnet_info, stun_buffer *message) {
// self-test:
{
password_t pwd;
if (stun_check_message_integrity_by_key_str(get_turn_credentials_type(), message->buf, (size_t)(message->len),
if (stun_check_message_integrity_by_key_str(get_turn_credentials_type(), message->buf, message->len,
clnet_info->key, pwd, shatype) < 1) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, " Self-test of integrity does not comple correctly !\n");
return -1;
}
}
} else {
if (stun_attr_add_integrity_by_user_str(message->buf, (size_t *)&(message->len), g_uname, clnet_info->realm,
g_upwd, clnet_info->nonce, shatype) < 0) {
if (!stun_attr_add_integrity_by_user_str(message->buf, (size_t *)&(message->len), g_uname, clnet_info->realm,
g_upwd, clnet_info->nonce, shatype)) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, " Cannot add integrity to the message\n");
return -1;
}

View File

@ -277,7 +277,7 @@ protected:
if (!_value) {
throw WrongStunAttrFormatException();
}
if (stun_attr_add_str(buffer, &sz, _attr_type, _value, _sz) < 0) {
if (!stun_attr_add_str(buffer, &sz, _attr_type, _value, _sz)) {
throw WrongStunBufferFormatException();
}
return 0;
@ -317,7 +317,7 @@ public:
void setChannelNumber(uint16_t cn) { _cn = cn; }
protected:
virtual int addToBuffer(uint8_t *buffer, size_t &sz) { return stun_attr_add_channel_number_str(buffer, &sz, _cn); }
virtual bool addToBuffer(uint8_t *buffer, size_t &sz) { return stun_attr_add_channel_number_str(buffer, &sz, _cn); }
private:
uint16_t _cn;
@ -341,7 +341,7 @@ public:
void setEvenPort(uint8_t ep) { _ep = ep; }
protected:
virtual int addToBuffer(uint8_t *buffer, size_t &sz) {
virtual bool addToBuffer(uint8_t *buffer, size_t &sz) {
return stun_attr_add_str(buffer, &sz, STUN_ATTRIBUTE_EVEN_PORT, &_ep, 1);
}
@ -367,7 +367,7 @@ public:
void setReservationToken(uint64_t rt) { _rt = rt; }
protected:
virtual int addToBuffer(uint8_t *buffer, size_t &sz) {
virtual bool addToBuffer(uint8_t *buffer, size_t &sz) {
uint64_t reservation_token = ioa_ntoh64(_rt);
return stun_attr_add_str(buffer, &sz, STUN_ATTRIBUTE_RESERVATION_TOKEN, (uint8_t *)(&reservation_token), 8);
}
@ -392,7 +392,7 @@ public:
}
size_t sz = 0;
const uint8_t *buf = iter.getRawBuffer(sz);
if (stun_attr_get_addr_str(buf, sz, getSar(iter), &_addr, NULL) < 0) {
if (!stun_attr_get_addr_str(buf, sz, getSar(iter), &_addr, NULL)) {
throw WrongStunAttrFormatException();
}
}
@ -401,7 +401,7 @@ public:
void setAddr(ioa_addr &addr) { addr_cpy(&_addr, &addr); }
protected:
virtual int addToBuffer(uint8_t *buffer, size_t &sz) {
virtual bool addToBuffer(uint8_t *buffer, size_t &sz) {
return stun_attr_add_addr_str(buffer, &sz, getType(), &_addr);
}
@ -414,43 +414,31 @@ private:
*/
class StunAttrChangeRequest : public StunAttr {
public:
StunAttrChangeRequest() : _changeIp(0), _changePort(0) { setType(STUN_ATTRIBUTE_CHANGE_REQUEST); }
StunAttrChangeRequest() : _changeIp(false), _changePort(false) { setType(STUN_ATTRIBUTE_CHANGE_REQUEST); }
StunAttrChangeRequest(const StunAttrIterator &iter) : StunAttr(iter) {
if (iter.eof()) {
throw EndOfStunMsgException();
}
if (stun_attr_get_change_request_str(getSar(iter), &_changeIp, &_changePort) < 0) {
if (!stun_attr_get_change_request_str(getSar(iter), &_changeIp, &_changePort)) {
throw WrongStunAttrFormatException();
}
}
virtual ~StunAttrChangeRequest() {}
bool getChangeIp() const { return _changeIp; }
void setChangeIp(bool ci) {
if (ci) {
_changeIp = 1;
} else {
_changeIp = 0;
}
}
void setChangeIp(bool ci) { _changeIp = ci; }
bool getChangePort() const { return _changePort; }
void setChangePort(bool cp) {
if (cp) {
_changePort = 1;
} else {
_changePort = 0;
}
}
void setChangePort(bool cp) { _changePort = cp; }
protected:
virtual int addToBuffer(uint8_t *buffer, size_t &sz) {
virtual bool addToBuffer(uint8_t *buffer, size_t &sz) {
return stun_attr_add_change_request_str(buffer, &sz, _changeIp, _changePort);
}
private:
int _changeIp;
int _changePort;
bool _changeIp;
bool _changePort;
};
/**
@ -476,7 +464,7 @@ public:
void setResponsePort(uint16_t p) { _rp = p; }
protected:
virtual int addToBuffer(uint8_t *buffer, size_t &sz) { return stun_attr_add_response_port_str(buffer, &sz, _rp); }
virtual bool addToBuffer(uint8_t *buffer, size_t &sz) { return stun_attr_add_response_port_str(buffer, &sz, _rp); }
private:
uint16_t _rp;
@ -508,7 +496,7 @@ public:
void setPadding(uint16_t p) { _p = p; }
protected:
virtual int addToBuffer(uint8_t *buffer, size_t &sz) { return stun_attr_add_padding_str(buffer, &sz, _p); }
virtual bool addToBuffer(uint8_t *buffer, size_t &sz) { return stun_attr_add_padding_str(buffer, &sz, _p); }
private:
uint16_t _p;
@ -600,7 +588,7 @@ public:
* Check if the raw buffer is a challenge response (the one with 401 error and realm and nonce values).
*/
static bool isChallengeResponse(const uint8_t *buf, size_t sz, int &err_code, uint8_t *err_msg, size_t err_msg_size,
uint8_t *realm, uint8_t *nonce, uint8_t *server_name, int *oauth) {
uint8_t *realm, uint8_t *nonce, uint8_t *server_name, bool *oauth) {
return stun_is_challenge_response_str(buf, sz, &err_code, err_msg, err_msg_size, realm, nonce, server_name, oauth);
}
@ -743,7 +731,7 @@ protected:
*/
class StunMsgRequest : public StunMsg {
public:
StunMsgRequest(uint16_t method) : _method(method){};
StunMsgRequest(uint16_t method) : _method(method) {};
StunMsgRequest(uint8_t *buffer, size_t total_sz, size_t sz, bool constructed)
: StunMsg(buffer, total_sz, sz, constructed), _method(0) {
@ -816,11 +804,11 @@ private:
*/
class StunMsgResponse : public StunMsg {
public:
StunMsgResponse(uint16_t method, stun_tid &tid) : _method(method), _err(0), _reason(""), _tid(tid){};
StunMsgResponse(uint16_t method, stun_tid &tid) : _method(method), _err(0), _reason(""), _tid(tid) {};
StunMsgResponse(uint16_t method, int error_code, std::string reason, stun_tid &tid)
: _method(method), _err(error_code), _reason(reason), _tid(tid){
: _method(method), _err(error_code), _reason(reason), _tid(tid) {
};
};
StunMsgResponse(uint8_t *buffer, size_t total_sz, size_t sz, bool constructed)
: StunMsg(buffer, total_sz, sz, constructed), _method(0), _err(0), _reason("") {
@ -906,7 +894,7 @@ public:
*/
void constructBindingResponse(stun_tid &tid, const ioa_addr &reflexive_addr, int error_code, const uint8_t *reason) {
stun_set_binding_response_str(_buffer, &_sz, &tid, &reflexive_addr, error_code, reason, 0, 0, 1);
stun_set_binding_response_str(_buffer, &_sz, &tid, &reflexive_addr, error_code, reason, 0, false, true);
}
bool isBindingResponse() const { return stun_is_binding_response_str(_buffer, _sz); }
@ -972,7 +960,7 @@ private:
*/
class StunMsgIndication : public StunMsg {
public:
StunMsgIndication(uint16_t method) : _method(method){};
StunMsgIndication(uint16_t method) : _method(method) {};
StunMsgIndication(uint8_t *buffer, size_t total_sz, size_t sz, bool constructed)
: StunMsg(buffer, total_sz, sz, constructed), _method(0) {
@ -1017,12 +1005,12 @@ private:
*/
class StunMsgChannel : public StunMsg {
public:
StunMsgChannel(uint16_t cn, int length) : _cn(cn), _len(length){};
StunMsgChannel(uint16_t cn, int length) : _cn(cn), _len(length) {};
StunMsgChannel(uint8_t *buffer, size_t total_sz, size_t sz, bool constructed)
: StunMsg(buffer, total_sz, sz, constructed), _cn(0) {
if (constructed) {
if (!stun_is_channel_message_str(buffer, &_sz, &_cn, 0)) {
if (!stun_is_channel_message_str(buffer, &_sz, &_cn, false)) {
throw WrongStunBufferFormatException();
}
if (_sz > 0xFFFF || _sz < 4) {
@ -1065,7 +1053,7 @@ protected:
return false;
}
uint16_t cn = 0;
if (!stun_is_channel_message_str(_buffer, &_sz, &cn, 0)) {
if (!stun_is_channel_message_str(_buffer, &_sz, &cn, false)) {
return false;
}
if (_cn != cn) {

File diff suppressed because it is too large Load Diff

View File

@ -35,6 +35,8 @@
#include "ns_turn_ioaddr.h"
#include "ns_turn_msg_defs.h"
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -73,7 +75,7 @@ typedef const void *stun_attr_ref;
//////////////////////////////////////////////////////////////
int stun_tid_equals(const stun_tid *id1, const stun_tid *id2);
bool stun_tid_equals(const stun_tid *id1, const stun_tid *id2);
void stun_tid_cpy(stun_tid *id_dst, const stun_tid *id_src);
void stun_tid_generate(stun_tid *id);
@ -107,31 +109,31 @@ void stun_init_error_response_str(uint16_t method, uint8_t *buf, size_t *len, ui
const uint8_t *reason, stun_tid *id);
void old_stun_init_error_response_str(uint16_t method, uint8_t *buf, size_t *len, uint16_t error_code,
const uint8_t *reason, stun_tid *id, uint32_t cookie);
int stun_init_channel_message_str(uint16_t chnumber, uint8_t *buf, size_t *len, int length, int do_padding);
bool stun_init_channel_message_str(uint16_t chnumber, uint8_t *buf, size_t *len, int length, bool do_padding);
int stun_is_command_message_str(const uint8_t *buf, size_t blen);
int old_stun_is_command_message_str(const uint8_t *buf, size_t blen, uint32_t *cookie);
int stun_is_command_message_full_check_str(const uint8_t *buf, size_t blen, int must_check_fingerprint,
int *fingerprint_present);
int stun_is_command_message_offset_str(const uint8_t *buf, size_t blen, int offset);
int stun_is_request_str(const uint8_t *buf, size_t len);
int stun_is_success_response_str(const uint8_t *buf, size_t len);
int stun_is_error_response_str(const uint8_t *buf, size_t len, int *err_code, uint8_t *err_msg, size_t err_msg_size);
int stun_is_challenge_response_str(const uint8_t *buf, size_t len, int *err_code, uint8_t *err_msg, size_t err_msg_size,
uint8_t *realm, uint8_t *nonce, uint8_t *server_name, int *oauth);
int stun_is_response_str(const uint8_t *buf, size_t len);
int stun_is_indication_str(const uint8_t *buf, size_t len);
bool stun_is_command_message_str(const uint8_t *buf, size_t blen);
bool old_stun_is_command_message_str(const uint8_t *buf, size_t blen, uint32_t *cookie);
bool stun_is_command_message_full_check_str(const uint8_t *buf, size_t blen, int must_check_fingerprint,
int *fingerprint_present);
bool stun_is_request_str(const uint8_t *buf, size_t len);
bool stun_is_success_response_str(const uint8_t *buf, size_t len);
bool stun_is_error_response_str(const uint8_t *buf, size_t len, int *err_code, uint8_t *err_msg, size_t err_msg_size);
bool stun_is_challenge_response_str(const uint8_t *buf, size_t len, int *err_code, uint8_t *err_msg,
size_t err_msg_size, uint8_t *realm, uint8_t *nonce, uint8_t *server_name,
bool *oauth);
bool stun_is_response_str(const uint8_t *buf, size_t len);
bool stun_is_indication_str(const uint8_t *buf, size_t len);
uint16_t stun_get_method_str(const uint8_t *buf, size_t len);
uint16_t stun_get_msg_type_str(const uint8_t *buf, size_t len);
int stun_is_channel_message_str(const uint8_t *buf, size_t *blen, uint16_t *chnumber, int mandatory_padding);
int is_channel_msg_str(const uint8_t *buf, size_t blen);
bool stun_is_channel_message_str(const uint8_t *buf, size_t *blen, uint16_t *chnumber, bool mandatory_padding);
bool is_channel_msg_str(const uint8_t *buf, size_t blen);
void stun_set_binding_request_str(uint8_t *buf, size_t *len);
int stun_set_binding_response_str(uint8_t *buf, size_t *len, stun_tid *tid, const ioa_addr *reflexive_addr,
int error_code, const uint8_t *reason, uint32_t cookie, int old_stun,
int no_stun_backward_compatibility);
int stun_is_binding_request_str(const uint8_t *buf, size_t len, size_t offset);
int stun_is_binding_response_str(const uint8_t *buf, size_t len);
bool stun_set_binding_response_str(uint8_t *buf, size_t *len, stun_tid *tid, const ioa_addr *reflexive_addr,
int error_code, const uint8_t *reason, uint32_t cookie, bool old_stun,
bool no_stun_backward_compatibility);
bool stun_is_binding_request_str(const uint8_t *buf, size_t len, size_t offset);
bool stun_is_binding_response_str(const uint8_t *buf, size_t len);
void stun_tid_from_message_str(const uint8_t *buf, size_t len, stun_tid *id);
void stun_tid_message_cpy(uint8_t *buf, const stun_tid *id);
@ -141,7 +143,7 @@ int stun_get_command_message_len_str(const uint8_t *buf, size_t len);
const uint8_t *get_default_reason(int error_code);
int stun_attr_is_addr(stun_attr_ref attr);
bool stun_attr_is_addr(stun_attr_ref attr);
int stun_attr_get_type(stun_attr_ref attr);
int stun_attr_get_len(stun_attr_ref attr);
const uint8_t *stun_attr_get_value(stun_attr_ref attr);
@ -152,25 +154,23 @@ uint64_t stun_attr_get_reservation_token_value(stun_attr_ref attr);
stun_attr_ref stun_attr_get_first_by_type_str(const uint8_t *buf, size_t len, uint16_t attr_type);
stun_attr_ref stun_attr_get_first_str(const uint8_t *buf, size_t len);
stun_attr_ref stun_attr_get_next_str(const uint8_t *buf, size_t len, stun_attr_ref prev);
int stun_attr_add_str(uint8_t *buf, size_t *len, uint16_t attr, const uint8_t *avalue, int alen);
int stun_attr_add_addr_str(uint8_t *buf, size_t *len, uint16_t attr_type, const ioa_addr *ca);
int stun_attr_get_addr_str(const uint8_t *buf, size_t len, stun_attr_ref attr, ioa_addr *ca,
const ioa_addr *default_addr);
int stun_attr_get_first_addr_str(const uint8_t *buf, size_t len, uint16_t attr_type, ioa_addr *ca,
const ioa_addr *default_addr);
int stun_attr_add_channel_number_str(uint8_t *buf, size_t *len, uint16_t chnumber);
int stun_attr_add_bandwidth_str(uint8_t *buf, size_t *len, band_limit_t bps);
int stun_attr_add_address_error_code(uint8_t *buf, size_t *len, int requested_address_family, int error_code);
/* return +1 if present, 0 if not, -1 if error: */
int stun_attr_get_address_error_code(uint8_t *buf, size_t len, int *requested_address_family, int *error_code);
bool stun_attr_add_str(uint8_t *buf, size_t *len, uint16_t attr, const uint8_t *avalue, int alen);
bool stun_attr_add_addr_str(uint8_t *buf, size_t *len, uint16_t attr_type, const ioa_addr *ca);
bool stun_attr_get_addr_str(const uint8_t *buf, size_t len, stun_attr_ref attr, ioa_addr *ca,
const ioa_addr *default_addr);
bool stun_attr_get_first_addr_str(const uint8_t *buf, size_t len, uint16_t attr_type, ioa_addr *ca,
const ioa_addr *default_addr);
bool stun_attr_add_channel_number_str(uint8_t *buf, size_t *len, uint16_t chnumber);
bool stun_attr_add_bandwidth_str(uint8_t *buf, size_t *len, band_limit_t bps);
bool stun_attr_add_address_error_code(uint8_t *buf, size_t *len, int requested_address_family, int error_code);
uint16_t stun_attr_get_first_channel_number_str(const uint8_t *buf, size_t len);
int stun_set_allocate_request_str(uint8_t *buf, size_t *len, uint32_t lifetime, int af4, int af6, uint8_t transport,
int mobile, const char *rt, int ep);
int stun_set_allocate_response_str(uint8_t *buf, size_t *len, stun_tid *tid, const ioa_addr *relayed_addr1,
const ioa_addr *relayed_addr2, const ioa_addr *reflexive_addr, uint32_t lifetime,
uint32_t max_lifetime, int error_code, const uint8_t *reason,
uint64_t reservation_token, char *mobile_id);
bool stun_set_allocate_request_str(uint8_t *buf, size_t *len, uint32_t lifetime, bool af4, bool af6, uint8_t transport,
bool mobile, const char *rt, int ep);
bool stun_set_allocate_response_str(uint8_t *buf, size_t *len, stun_tid *tid, const ioa_addr *relayed_addr1,
const ioa_addr *relayed_addr2, const ioa_addr *reflexive_addr, uint32_t lifetime,
uint32_t max_lifetime, int error_code, const uint8_t *reason,
uint64_t reservation_token, char *mobile_id);
uint16_t stun_set_channel_bind_request_str(uint8_t *buf, size_t *len, const ioa_addr *peer_addr,
uint16_t channel_number);
@ -179,9 +179,9 @@ void stun_set_channel_bind_response_str(uint8_t *buf, size_t *len, stun_tid *tid
int stun_get_requested_address_family(stun_attr_ref attr);
int stun_attr_add_fingerprint_str(uint8_t *buf, size_t *len);
bool stun_attr_add_fingerprint_str(uint8_t *buf, size_t *len);
int SASLprep(uint8_t *s);
bool SASLprep(uint8_t *s);
#define print_bin(str, len, field) print_bin_func(str, len, field, __FUNCTION__)
void print_bin_func(const char *name, size_t len, const void *s, const char *func);
@ -193,14 +193,14 @@ int stun_check_message_integrity_by_key_str(turn_credential_type ct, uint8_t *bu
password_t pwd, SHATYPE shatype);
int stun_check_message_integrity_str(turn_credential_type ct, uint8_t *buf, size_t len, const uint8_t *uname,
const uint8_t *realm, const uint8_t *upwd, SHATYPE shatype);
int stun_attr_add_integrity_str(turn_credential_type ct, uint8_t *buf, size_t *len, hmackey_t key, password_t pwd,
SHATYPE shatype);
int stun_attr_add_integrity_by_key_str(uint8_t *buf, size_t *len, const uint8_t *uname, const uint8_t *realm,
hmackey_t key, const uint8_t *nonce, SHATYPE shatype);
int stun_attr_add_integrity_by_user_str(uint8_t *buf, size_t *len, const uint8_t *uname, const uint8_t *realm,
const uint8_t *upwd, const uint8_t *nonce, SHATYPE shatype);
int stun_attr_add_integrity_by_user_short_term_str(uint8_t *buf, size_t *len, const uint8_t *uname, password_t pwd,
SHATYPE shatype);
bool stun_attr_add_integrity_str(turn_credential_type ct, uint8_t *buf, size_t *len, hmackey_t key, password_t pwd,
SHATYPE shatype);
bool stun_attr_add_integrity_by_key_str(uint8_t *buf, size_t *len, const uint8_t *uname, const uint8_t *realm,
hmackey_t key, const uint8_t *nonce, SHATYPE shatype);
bool stun_attr_add_integrity_by_user_str(uint8_t *buf, size_t *len, const uint8_t *uname, const uint8_t *realm,
const uint8_t *upwd, const uint8_t *nonce, SHATYPE shatype);
bool stun_attr_add_integrity_by_user_short_term_str(uint8_t *buf, size_t *len, const uint8_t *uname, password_t pwd,
SHATYPE shatype);
size_t get_hmackey_size(SHATYPE shatype);
/*
@ -211,32 +211,32 @@ size_t get_hmackey_size(SHATYPE shatype);
long turn_random(void);
long turn_random_number(void);
int stun_produce_integrity_key_str(const uint8_t *uname, const uint8_t *realm, const uint8_t *upwd, hmackey_t key,
SHATYPE shatype);
int stun_calculate_hmac(const uint8_t *buf, size_t len, const uint8_t *key, size_t sz, uint8_t *hmac,
unsigned int *hmac_len, SHATYPE shatype);
bool stun_produce_integrity_key_str(const uint8_t *uname, const uint8_t *realm, const uint8_t *upwd, hmackey_t key,
SHATYPE shatype);
bool stun_calculate_hmac(const uint8_t *buf, size_t len, const uint8_t *key, size_t sz, uint8_t *hmac,
unsigned int *hmac_len, SHATYPE shatype);
/* RFC 5780 */
int stun_attr_get_change_request_str(stun_attr_ref attr, int *change_ip, int *change_port);
int stun_attr_add_change_request_str(uint8_t *buf, size_t *len, int change_ip, int change_port);
bool stun_attr_get_change_request_str(stun_attr_ref attr, bool *change_ip, bool *change_port);
bool stun_attr_add_change_request_str(uint8_t *buf, size_t *len, bool change_ip, bool change_port);
int stun_attr_get_response_port_str(stun_attr_ref attr);
int stun_attr_add_response_port_str(uint8_t *buf, size_t *len, uint16_t port);
bool stun_attr_add_response_port_str(uint8_t *buf, size_t *len, uint16_t port);
int stun_attr_get_padding_len_str(stun_attr_ref attr);
int stun_attr_add_padding_str(uint8_t *buf, size_t *len, uint16_t padding_len);
bool stun_attr_add_padding_str(uint8_t *buf, size_t *len, uint16_t padding_len);
/* HTTP */
int is_http(const char *s, size_t blen);
/* OAUTH */
int convert_oauth_key_data(const oauth_key_data *oakd, oauth_key *key, char *err_msg, size_t err_msg_size);
int decode_oauth_token(const uint8_t *server_name, const encoded_oauth_token *etoken, const oauth_key *key,
oauth_token *dtoken);
int encode_oauth_token(const uint8_t *server_name, encoded_oauth_token *etoken, const oauth_key *key,
const oauth_token *dtoken, const uint8_t *nonce);
bool convert_oauth_key_data(const oauth_key_data *oakd, oauth_key *key, char *err_msg, size_t err_msg_size);
bool decode_oauth_token(const uint8_t *server_name, const encoded_oauth_token *etoken, const oauth_key *key,
oauth_token *dtoken);
bool encode_oauth_token(const uint8_t *server_name, encoded_oauth_token *etoken, const oauth_key *key,
const oauth_token *dtoken, const uint8_t *nonce);
/* Encrypted password */
void generate_new_enc_password(const char *pwd, char *result);
int check_password(const char *pin, const char *pwd);
bool check_password_equal(const char *pin, const char *pwd);
///////////////////////////////////////////////////////////////

View File

@ -1778,7 +1778,7 @@ static int handle_turn_refresh(turn_turnserver *server, ts_ur_super_session *ss,
if ((server->fingerprint) || ss->enforce_fingerprints) {
size_t len = ioa_network_buffer_get_size(nbh);
if (stun_attr_add_fingerprint_str(ioa_network_buffer_data(nbh), &len) < 0) {
if (!stun_attr_add_fingerprint_str(ioa_network_buffer_data(nbh), &len)) {
*err_code = 500;
ioa_network_buffer_delete(server->e, nbh);
return -1;
@ -2289,8 +2289,8 @@ static int handle_turn_connect(turn_turnserver *server, ts_ur_super_session *ss,
switch (attr_type) {
SKIP_ATTRIBUTES;
case STUN_ATTRIBUTE_XOR_PEER_ADDRESS: {
if (stun_attr_get_addr_str(ioa_network_buffer_data(in_buffer->nbh), ioa_network_buffer_get_size(in_buffer->nbh),
sar, &peer_addr, NULL) == -1) {
if (!stun_attr_get_addr_str(ioa_network_buffer_data(in_buffer->nbh),
ioa_network_buffer_get_size(in_buffer->nbh), sar, &peer_addr, NULL)) {
*err_code = 400;
*reason = (const uint8_t *)"Bad Peer Address";
} else {
@ -2748,8 +2748,8 @@ static int handle_turn_binding(turn_turnserver *server, ts_ur_super_session *ss,
uint32_t cookie, int old_stun) {
FUNCSTART;
int change_ip = 0;
int change_port = 0;
bool change_ip = false;
bool change_port = false;
int padding = 0;
int response_port_present = 0;
uint16_t response_port = 0;
@ -2857,7 +2857,7 @@ static int handle_turn_binding(turn_turnserver *server, ts_ur_super_session *ss,
size_t len = ioa_network_buffer_get_size(nbh);
if (stun_set_binding_response_str(ioa_network_buffer_data(nbh), &len, tid,
get_remote_addr_from_ioa_socket(ss->client_socket), 0, NULL, cookie, old_stun,
*server->no_stun_backward_compatibility) >= 0) {
*server->no_stun_backward_compatibility)) {
addr_cpy(response_origin, get_local_addr_from_ioa_socket(ss->client_socket));
@ -4558,7 +4558,7 @@ static int read_client_connection(turn_turnserver *server, ts_ur_super_session *
if ((server->fingerprint) || ss->enforce_fingerprints) {
size_t len = ioa_network_buffer_get_size(nbh);
if (stun_attr_add_fingerprint_str(ioa_network_buffer_data(nbh), &len) < 0) {
if (!stun_attr_add_fingerprint_str(ioa_network_buffer_data(nbh), &len)) {
FUNCEND;
ioa_network_buffer_delete(server->e, nbh);
return -1;