Skip to content

Commit

Permalink
Merge branch 'master' of github.com:coturn/coturn into coturn-master
Browse files Browse the repository at this point in the history
  • Loading branch information
misi committed Dec 8, 2020
2 parents b13cc41 + 00659f3 commit c8ba707
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 61 deletions.
5 changes: 0 additions & 5 deletions src/apps/common/ns_turn_openssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,4 @@
#include <openssl/dh.h>
#include <openssl/bn.h>

#if (defined LIBRESSL_VERSION_NUMBER && OPENSSL_VERSION_NUMBER == 0x20000000L)
#undef OPENSSL_VERSION_NUMBER
#define OPENSSL_VERSION_NUMBER 0x1000107FL
#endif

#endif //__NST_OPENSSL_LIB__
78 changes: 27 additions & 51 deletions src/apps/common/ns_turn_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,42 +158,16 @@ void set_no_stdout_log(int val)
no_stdout_log = val;
}

void turn_log_func_default(TURN_LOG_LEVEL level, const char* format, ...)
{
#if !defined(TURN_LOG_FUNC_IMPL)
{
va_list args;
va_start(args,format);
vrtpprintf(level, format, args);
va_end(args);
}
#endif
#define MAX_LOG_TIMESTAMP_FORMAT_LEN 48
static char turn_log_timestamp_format[MAX_LOG_TIMESTAMP_FORMAT_LEN] = "%FT%T%z";

{
va_list args;
va_start(args,format);
#if defined(TURN_LOG_FUNC_IMPL)
TURN_LOG_FUNC_IMPL(level,format,args);
#else
#define MAX_RTPPRINTF_BUFFER_SIZE (1024)
char s[MAX_RTPPRINTF_BUFFER_SIZE+1];
#undef MAX_RTPPRINTF_BUFFER_SIZE
if (level == TURN_LOG_LEVEL_ERROR) {
snprintf(s,sizeof(s)-100,"%lu: ERROR: ",(unsigned long)log_time());
size_t slen = strlen(s);
vsnprintf(s+slen,sizeof(s)-slen-1,format, args);
fwrite(s,strlen(s),1,stdout);
} else if(!no_stdout_log) {
snprintf(s,sizeof(s)-100,"%lu: ",(unsigned long)log_time());
size_t slen = strlen(s);
vsnprintf(s+slen,sizeof(s)-slen-1,format, args);
fwrite(s,strlen(s),1,stdout);
}
#endif
va_end(args);
}
void set_turn_log_timestamp_format(char* new_format)
{
strncpy(turn_log_timestamp_format, new_format, MAX_LOG_TIMESTAMP_FORMAT_LEN-1);
}

int use_new_log_timestamp_format = 0;

void addr_debug_print(int verbose, const ioa_addr *addr, const char* s)
{
if (verbose) {
Expand Down Expand Up @@ -512,20 +486,29 @@ static int get_syslog_level(TURN_LOG_LEVEL level)
return LOG_INFO;
}

int vrtpprintf(TURN_LOG_LEVEL level, const char *format, va_list args)
void turn_log_func_default(TURN_LOG_LEVEL level, const char* format, ...)
{
va_list args;
va_start(args,format);
#if defined(TURN_LOG_FUNC_IMPL)
TURN_LOG_FUNC_IMPL(level,format,args);
#else
/* Fix for Issue 24, raised by John Selbie: */
#define MAX_RTPPRINTF_BUFFER_SIZE (1024)
char s[MAX_RTPPRINTF_BUFFER_SIZE+1];
#undef MAX_RTPPRINTF_BUFFER_SIZE

size_t sz;

snprintf(s, sizeof(s), "%lu: ",(unsigned long)log_time());
sz=strlen(s);
vsnprintf(s+sz, sizeof(s)-1-sz, format, args);
s[sizeof(s)-1]=0;

size_t so_far = 0;
if (use_new_log_timestamp_format) {
time_t now = time(NULL);
so_far += strftime(s, sizeof(s), turn_log_timestamp_format, localtime(&now));
} else {
so_far += snprintf(s, sizeof(s), "%lu: ", (unsigned long)log_time());
}
so_far += snprintf(s + so_far, sizeof(s)-100, (level == TURN_LOG_LEVEL_ERROR) ? ": ERROR: " : ": ");
so_far += vsnprintf(s + so_far,sizeof(s) - (so_far+1), format, args);
/* always write to stdout */
fwrite(s, so_far, 1, stdout);
/* write to syslog or to log file */
if(to_syslog) {
syslog(get_syslog_level(level),"%s",s);
} else {
Expand All @@ -538,16 +521,9 @@ int vrtpprintf(TURN_LOG_LEVEL level, const char *format, va_list args)
}
log_unlock();
}
#endif
va_end(args);

return 0;
}

void rtpprintf(const char *format, ...)
{
va_list args;
va_start (args, format);
vrtpprintf(TURN_LOG_LEVEL_INFO, format, args);
va_end (args);
}

///////////// ORIGIN ///////////////////
Expand Down
3 changes: 3 additions & 0 deletions src/apps/common/ns_turn_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ void set_no_stdout_log(int val);
void set_log_to_syslog(int val);
void set_simple_log(int val);

void set_turn_log_timestamp_format(char* new_format);

void turn_log_func_default(TURN_LOG_LEVEL level, const char* format, ...);

void addr_debug_print(int verbose, const ioa_addr *addr, const char* s);
Expand All @@ -69,6 +71,7 @@ void addr_debug_print(int verbose, const ioa_addr *addr, const char* s);

extern volatile int _log_time_value_set;
extern volatile turn_time_t _log_time_value;
extern int use_new_log_timestamp_format;

void rtpprintf(const char *format, ...);
int vrtpprintf(TURN_LOG_LEVEL level, const char *format, va_list args);
Expand Down
10 changes: 10 additions & 0 deletions src/apps/relay/mainrelay.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,8 @@ static char Usage[] = "Usage: turnserver [options]\n"
" --simple-log This flag means that no log file rollover will be used, and the log file\n"
" name will be constructed as-is, without PID and date appendage.\n"
" This option can be used, for example, together with the logrotate tool.\n"
" --new-log-timestamp Enable full ISO-8601 timestamp in all logs.\n"
" --new-timestamp_format <format> Set timestamp format (in strftime(1) format)\n"
" --stale-nonce[=<value>] Use extra security with nonce value having limited lifetime (default 600 secs).\n"
" --max-allocate-lifetime <value> Set the maximum value for the allocation lifetime. Default to 3600 secs.\n"
" --channel-lifetime <value> Set the lifetime for channel binding, default to 600 secs.\n"
Expand Down Expand Up @@ -761,6 +763,8 @@ enum EXTRA_OPTS {
NO_STDOUT_LOG_OPT,
SYSLOG_OPT,
SIMPLE_LOG_OPT,
NEW_LOG_TIMESTAMP_OPT,
NEW_TIMESTAMP_FORMAT_OPT,
AUX_SERVER_OPT,
UDP_SELF_BALANCE_OPT,
ALTERNATE_SERVER_OPT,
Expand Down Expand Up @@ -899,6 +903,8 @@ static const struct myoption long_options[] = {
{ "no-stdout-log", optional_argument, NULL, NO_STDOUT_LOG_OPT },
{ "syslog", optional_argument, NULL, SYSLOG_OPT },
{ "simple-log", optional_argument, NULL, SIMPLE_LOG_OPT },
{ "new-log-timestamp", optional_argument, NULL, NEW_LOG_TIMESTAMP_OPT },
{ "new-timestamp_format", required_argument, NULL, NEW_TIMESTAMP_FORMAT_OPT },
{ "aux-server", required_argument, NULL, AUX_SERVER_OPT },
{ "udp-self-balance", optional_argument, NULL, UDP_SELF_BALANCE_OPT },
{ "alternate-server", required_argument, NULL, ALTERNATE_SERVER_OPT },
Expand Down Expand Up @@ -1717,6 +1723,10 @@ static void read_config_file(int argc, char **argv, int pass)
set_log_to_syslog(get_bool_value(value));
} else if((pass==0) && (c==SIMPLE_LOG_OPT)) {
set_simple_log(get_bool_value(value));
} else if ((pass==0) && (c==NEW_LOG_TIMESTAMP_OPT)) {
use_new_log_timestamp_format=1;
} else if ((pass==0) && (c==NEW_TIMESTAMP_FORMAT_OPT)) {
set_turn_log_timestamp_format(value);
} else if((pass == 0) && (c != 'u')) {
set_option(c, value);
} else if((pass > 0) && (c == 'u')) {
Expand Down
111 changes: 108 additions & 3 deletions src/apps/relay/ns_ioalib_engine_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1833,7 +1833,7 @@ int ssl_read(evutil_socket_t fd, SSL* ssl, ioa_network_buffer_handle nbh, int ve
BIO* rbio = BIO_new_mem_buf(buffer, old_buffer_len);
BIO_set_mem_eof_return(rbio, -1);

#if OPENSSL_VERSION_NUMBER < 0x10100000L
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined LIBRESSL_VERSION_NUMBER
ssl->rbio = rbio;
#else
SSL_set0_rbio(ssl,rbio);
Expand Down Expand Up @@ -1928,7 +1928,7 @@ int ssl_read(evutil_socket_t fd, SSL* ssl, ioa_network_buffer_handle nbh, int ve
if(ret>0) {
ioa_network_buffer_add_offset_size(nbh, (uint16_t)buf_size, 0, (size_t)ret);
}
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined LIBRESSL_VERSION_NUMBER
ssl->rbio = NULL;
BIO_free(rbio);
#else
Expand Down Expand Up @@ -2166,6 +2166,101 @@ static TURN_TLS_TYPE check_tentative_tls(ioa_socket_raw fd)
}
#endif


static size_t proxy_string_field(char *field, size_t max, uint8_t *buf, size_t index, size_t len)
{
size_t count = 0;
while((index < len) && (count < max)) {
if((0x20 == buf[index]) || (0x0D == buf[index])) {
field[count] = 0x00;
return ++index;
}
field[count++] = buf[index++];
}
return 0;
}

static ssize_t socket_parse_proxy_v1(ioa_socket_handle s, uint8_t *buf, size_t len)
{
if(len < 11) {
return 0 ;
}

/* Check for proxy-v1 magic field */
char magic[] = {0x50, 0x52, 0x4F, 0x58, 0x59, 0x20};
if(memcmp(magic, buf, sizeof(magic))) {
return -1;
}

/* Read family */
char tcp4[] = {0x54, 0x43, 0x50, 0x34, 0x20};
char tcp6[] = {0x54, 0x43, 0x50, 0x36, 0x20};
int family;
if(0 == memcmp(tcp4, &buf[6], sizeof(tcp4))) { /* IPv4 */
family = AF_INET;
} else if(0 == memcmp(tcp6, &buf[6], sizeof(tcp6))) { /* IPv6 */
family = AF_INET6;
} else {
return -1;
}

char saddr[40];
char daddr[40];
char sport[6];
char dport[6];

size_t tlen = 11;
/* Read source address */
tlen = proxy_string_field(saddr, sizeof(saddr), buf, tlen, len);
if(0 == tlen) return -1;

/* Read dest address */
tlen = proxy_string_field(daddr, sizeof(daddr), buf, tlen, len);
if(0 == tlen) return -1;

/* Read source port */
tlen = proxy_string_field(sport, sizeof(sport), buf, tlen, len);
if(0 == tlen) return -1;

/* Read dest port */
tlen = proxy_string_field(dport, sizeof(dport), buf, tlen, len);
if(0 == tlen) return -1;

/* Final line feed */
if ((len <= tlen) || (0x0A != buf[tlen])) return -1;

tlen++;

int sport_int = atoi(sport);
int dport_int = atoi(dport);
if((sport_int < 0) || (0xFFFF < sport_int)) return -1;
if((dport_int < 0) || (0xFFFF < dport_int)) return -1;

if (AF_INET == family) {
struct sockaddr_in remote, local;
remote.sin_family = local.sin_family = AF_INET;
if(1 != inet_pton(AF_INET, saddr, &remote.sin_addr.s_addr)) return -1;
if(1 != inet_pton(AF_INET, daddr, &local.sin_addr.s_addr)) return -1;
remote.sin_port = htons((uint16_t)sport_int);
local.sin_port = htons((uint16_t)dport_int);

addr_cpy4(&(s->local_addr), &local);
addr_cpy4(&(s->remote_addr), &remote);

} else {
struct sockaddr_in6 remote, local;
remote.sin6_family = local.sin6_family = AF_INET6;
if(1 != inet_pton(AF_INET6, saddr, &remote.sin6_addr.s6_addr)) return -1;
if(1 != inet_pton(AF_INET6, daddr, &local.sin6_addr.s6_addr)) return -1;
remote.sin6_port = htons((uint16_t)sport_int);
local.sin6_port = htons((uint16_t)dport_int);

addr_cpy6(&(s->local_addr), &local);
addr_cpy6(&(s->remote_addr), &remote);
}
return tlen;
}

static ssize_t socket_parse_proxy_v2(ioa_socket_handle s, uint8_t *buf, size_t len)
{
if(len < 16){
Expand Down Expand Up @@ -2227,6 +2322,16 @@ static ssize_t socket_parse_proxy_v2(ioa_socket_handle s, uint8_t *buf, size_t l
return tlen;
}

static ssize_t socket_parse_proxy(ioa_socket_handle s, uint8_t *buf, size_t len)
{
ssize_t tlen = socket_parse_proxy_v2(s, buf, len);
if(-1 == tlen) {
tlen = socket_parse_proxy_v1(s, buf, len);
}

return tlen;
}

static int socket_input_worker(ioa_socket_handle s)
{
int len = 0;
Expand Down Expand Up @@ -2450,7 +2555,7 @@ static int socket_input_worker(ioa_socket_handle s)
blen=(ev_ssize_t)STUN_BUFFER_SIZE;

if(s->st == TCP_SOCKET_PROXY){
ssize_t tlen = socket_parse_proxy_v2(s, buf_elem->buf.buf, blen);
ssize_t tlen = socket_parse_proxy(s, buf_elem->buf.buf, blen);
blen = 0;
if (tlen < 0){
s->tobeclosed = 1;
Expand Down
4 changes: 2 additions & 2 deletions src/client/ns_turn_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ int stun_produce_integrity_key_str(const uint8_t *uname, const uint8_t *realm, c
unsigned int keylen = 0;
EVP_MD_CTX ctx;
EVP_MD_CTX_init(&ctx);
#ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
#if defined EVP_MD_CTX_FLAG_NON_FIPS_ALLOW && !defined(LIBRESSL_VERSION_NUMBER)
if (FIPS_mode()) {
EVP_MD_CTX_set_flags(&ctx,EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
}
Expand All @@ -256,7 +256,7 @@ int stun_produce_integrity_key_str(const uint8_t *uname, const uint8_t *realm, c
#else
unsigned int keylen = 0;
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
#ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
#if defined EVP_MD_CTX_FLAG_NON_FIPS_ALLOW && ! defined(LIBRESSL_VERSION_NUMBER)
if (FIPS_mode()) {
EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
}
Expand Down

0 comments on commit c8ba707

Please sign in to comment.