From da332ed9e718129d30efc467402d750e3c0fa9a3 Mon Sep 17 00:00:00 2001 From: Michael Jones Date: Sat, 27 Jan 2024 18:38:40 -0600 Subject: [PATCH] Add the InsertBraces command for clang-format to ensure that all conditionals always have braces (#1408) - Why? Because code where conditionals lack braces is much harder to read, and prone to indentation confusion. - How? Just added an extra flag to .clang-format and re-ran clang-format on all the files. I also moved .clang-format up to the top level of the repo so that it can be applied to the fuzz targets as well. --- src/.clang-format => .clang-format | 3 +- .github/workflows/lint.yml | 14 +- fuzzing/FuzzStun.c | 3 +- src/apps/common/apputils.c | 171 +++++++---- src/apps/common/hiredis_libevent2.c | 29 +- src/apps/common/ns_turn_utils.c | 124 +++++--- src/apps/common/stun_buffer.c | 23 +- src/apps/common/win/getopt.c | 120 +++++--- src/apps/natdiscovery/natdiscovery.c | 30 +- src/apps/oauth/oauth.c | 9 +- src/apps/peer/mainudpserver.c | 6 +- src/apps/peer/udpserver.c | 30 +- src/apps/relay/acme.c | 21 +- src/apps/relay/dbdrivers/dbd_mongo.c | 104 ++++--- src/apps/relay/dbdrivers/dbd_mysql.c | 179 +++++++----- src/apps/relay/dbdrivers/dbd_pgsql.c | 34 ++- src/apps/relay/dbdrivers/dbd_redis.c | 223 +++++++++------ src/apps/relay/dbdrivers/dbd_sqlite.c | 31 +- src/apps/relay/dbdrivers/dbdriver.c | 3 +- src/apps/relay/dtls_listener.c | 53 ++-- src/apps/relay/http_server.c | 10 +- src/apps/relay/libtelnet.c | 77 +++-- src/apps/relay/mainrelay.c | 349 +++++++++++++++-------- src/apps/relay/mainrelay.h | 6 +- src/apps/relay/netengine.c | 92 +++--- src/apps/relay/ns_ioalib_engine_impl.c | 378 ++++++++++++++++--------- src/apps/relay/tls_listener.c | 36 ++- src/apps/relay/turn_admin_server.c | 321 +++++++++++++-------- src/apps/relay/turn_ports.c | 14 +- src/apps/relay/userdb.c | 53 ++-- src/apps/rfc5769/rfc5769check.c | 9 +- src/apps/stunclient/stunclient.c | 45 ++- src/apps/uclient/mainuclient.c | 13 +- src/apps/uclient/startuclient.c | 298 ++++++++++++------- src/apps/uclient/uclient.c | 240 ++++++++++------ src/client++/TurnMsgLib.h | 109 ++++--- src/client/ns_turn_ioaddr.c | 119 +++++--- src/client/ns_turn_msg.c | 325 +++++++++++++-------- src/client/ns_turn_msg_addr.c | 19 +- src/server/ns_turn_allocation.c | 38 ++- src/server/ns_turn_allocation.h | 3 +- src/server/ns_turn_khash.h | 6 +- src/server/ns_turn_maps.c | 103 ++++--- src/server/ns_turn_maps_rtcp.c | 27 +- src/server/ns_turn_server.c | 320 +++++++++++++-------- 45 files changed, 2702 insertions(+), 1518 deletions(-) rename src/.clang-format => .clang-format (98%) diff --git a/src/.clang-format b/.clang-format similarity index 98% rename from src/.clang-format rename to .clang-format index b0e04cc..4ca1bf4 100644 --- a/src/.clang-format +++ b/.clang-format @@ -50,7 +50,7 @@ BreakConstructorInitializersBeforeComma: false BreakConstructorInitializers: BeforeColon BreakAfterJavaFieldAnnotations: false BreakStringLiterals: true -ColumnLimit: 120 +ColumnLimit: 120 CommentPragmas: '^ IWYU pragma:' CompactNamespaces: false ConstructorInitializerAllOnOneLineOrOnePerLine: false @@ -133,5 +133,6 @@ StatementMacros: TabWidth: 8 UseCRLF: false UseTab: Never +InsertBraces: true ... diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index fd114bc..88c5bf3 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -13,17 +13,23 @@ jobs: steps: - name: Install dependencies run: | - sudo apt-get update - sudo apt-get install -y \ + sudo apt update + sudo apt install -y clang-format-15 + sudo apt install -y \ libevent-dev \ libssl-dev \ libpq-dev libmariadb-dev libsqlite3-dev \ libhiredis-dev \ libmongoc-dev \ libmicrohttpd-dev \ - clang-format - uses: actions/checkout@v4 - name: configure run: ./configure - name: lint - run: clang-format --version && make lint + run: | + if which clang-format-15 2>&1 >/dev/null + then + sudo cp $(which clang-format-15) $(which clang-format) + fi + clang-format --version + make lint diff --git a/fuzzing/FuzzStun.c b/fuzzing/FuzzStun.c index 28ef0f2..f79c157 100644 --- a/fuzzing/FuzzStun.c +++ b/fuzzing/FuzzStun.c @@ -24,7 +24,6 @@ extern int LLVMFuzzerTestOneInput(const uint8_t *Data, uint8_t realm[33]; uint8_t upwd[33]; strcpy((char *)upwd, "VOkJxbRl1RmTxUk/WvJxBt"); - stun_check_message_integrity_str(TURN_CREDENTIALS_SHORT_TERM, (uint8_t *)Data, - Size, uname, realm, upwd, shatype); + stun_check_message_integrity_str(TURN_CREDENTIALS_SHORT_TERM, (uint8_t *)Data, Size, uname, realm, upwd, shatype); return 0; } diff --git a/src/apps/common/apputils.c b/src/apps/common/apputils.c index 4c39ddb..7cdb638 100644 --- a/src/apps/common/apputils.c +++ b/src/apps/common/apputils.c @@ -183,9 +183,9 @@ int socket_tcp_set_keepalive(evutil_socket_t fd, SOCKET_TYPE st) { int socket_set_reusable(evutil_socket_t fd, int flag, SOCKET_TYPE st) { UNUSED_ARG(st); - if (fd < 0) + if (fd < 0) { return -1; - else { + } else { #if defined(WINDOWS) int use_reuseaddr = IS_TURN_SERVER; @@ -197,8 +197,9 @@ int socket_set_reusable(evutil_socket_t fd, int flag, SOCKET_TYPE st) { if (use_reuseaddr) { int on = flag; int ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, (socklen_t)sizeof(on)); - if (ret < 0) + if (ret < 0) { perror("SO_REUSEADDR"); + } } #endif @@ -208,8 +209,9 @@ int socket_set_reusable(evutil_socket_t fd, int flag, SOCKET_TYPE st) { if (is_sctp_socket(st)) { int on = flag; int ret = setsockopt(fd, IPPROTO_SCTP, SCTP_REUSE_PORT, (const void *)&on, (socklen_t)sizeof(on)); - if (ret < 0) + if (ret < 0) { perror("SCTP_REUSE_PORT"); + } } } #endif @@ -238,10 +240,11 @@ int sock_bind_to_device(evutil_socket_t fd, const unsigned char *ifname) { strncpy(ifr.ifr_name, (const char *)ifname, sizeof(ifr.ifr_name)); if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0) { - if (socket_eperm()) + if (socket_eperm()) { perror("You must obtain superuser privileges to bind a socket to device"); - else + } else { perror("Cannot bind socket to device"); + } return -1; } @@ -255,9 +258,9 @@ int sock_bind_to_device(evutil_socket_t fd, const unsigned char *ifname) { } int addr_connect(evutil_socket_t fd, const ioa_addr *addr, int *out_errno) { - if (!addr || fd < 0) + if (!addr || fd < 0) { return -1; - else { + } else { int err = 0; do { if (addr->ss.sa_family == AF_INET) { @@ -269,11 +272,13 @@ int addr_connect(evutil_socket_t fd, const ioa_addr *addr, int *out_errno) { } } while (err < 0 && socket_eintr()); - if (out_errno) + if (out_errno) { *out_errno = socket_errno(); + } - if (err < 0 && !socket_einprogress()) + if (err < 0 && !socket_einprogress()) { perror("Connect"); + } return err; } @@ -318,9 +323,9 @@ int addr_bind(evutil_socket_t fd, const ioa_addr *addr, int reusable, int debug, int addr_get_from_sock(evutil_socket_t fd, ioa_addr *addr) { - if (fd < 0 || !addr) + if (fd < 0 || !addr) { return -1; - else { + } else { ioa_addr a; a.ss.sa_family = AF_INET6; @@ -559,14 +564,16 @@ int set_socket_df(evutil_socket_t fd, int family, int value) { /* kernel sets DF bit on outgoing IP packets */ if (family == AF_INET) { int val = IP_PMTUDISC_DO; - if (!value) + if (!value) { val = IP_PMTUDISC_DONT; + } ret = setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val)); } else { #if defined(IPPROTO_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT) int val = IPV6_PMTUDISC_DO; - if (!value) + if (!value) { val = IPV6_PMTUDISC_DONT; + } ret = setsockopt(fd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &val, sizeof(val)); #else #error CANNOT SET IPV6 SOCKET DF FLAG (2) @@ -590,8 +597,9 @@ int set_socket_df(evutil_socket_t fd, int family, int value) { static int get_mtu_from_ssl(SSL *ssl) { int ret = SOSO_MTU; #if DTLS_SUPPORTED - if (ssl) + if (ssl) { ret = BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL); + } #else UNUSED_ARG(ssl); #endif @@ -610,29 +618,35 @@ static void set_query_mtu(SSL *ssl) { int decrease_mtu(SSL *ssl, int mtu, int verbose) { - if (!ssl) + if (!ssl) { return mtu; + } int new_mtu = get_mtu_from_ssl(ssl); - if (new_mtu < 1) + if (new_mtu < 1) { new_mtu = mtu; + } - if (new_mtu > MAX_MTU) + if (new_mtu > MAX_MTU) { mtu = MAX_MTU; - if (new_mtu > 0 && new_mtu < MIN_MTU) + } + if (new_mtu > 0 && new_mtu < MIN_MTU) { mtu = MIN_MTU; - else if (new_mtu < mtu) + } else if (new_mtu < mtu) { mtu = new_mtu; - else + } else { mtu -= MTU_STEP; + } - if (mtu < MIN_MTU) + if (mtu < MIN_MTU) { mtu = MIN_MTU; + } set_query_mtu(ssl); - if (verbose) + if (verbose) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "1. mtu to use: %d\n", mtu); + } #if DTLS_SUPPORTED SSL_set_mtu(ssl, mtu); @@ -644,21 +658,24 @@ int decrease_mtu(SSL *ssl, int mtu, int verbose) { int set_mtu_df(SSL *ssl, evutil_socket_t fd, int family, int mtu, int df_value, int verbose) { - if (!ssl || fd < 0) + if (!ssl || fd < 0) { return 0; + } int ret = set_socket_df(fd, family, df_value); - if (!mtu) + if (!mtu) { mtu = SOSO_MTU; - else if (mtu < MIN_MTU) + } else if (mtu < MIN_MTU) { mtu = MIN_MTU; - else if (mtu > MAX_MTU) + } else if (mtu > MAX_MTU) { mtu = MAX_MTU; + } set_query_mtu(ssl); - if (verbose) + if (verbose) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "3. mtu to use: %d\n", mtu); + } #if DTLS_SUPPORTED @@ -668,8 +685,9 @@ int set_mtu_df(SSL *ssl, evutil_socket_t fd, int family, int mtu, int df_value, #endif - if (verbose) + if (verbose) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "4. new mtu: %d\n", get_mtu_from_ssl(ssl)); + } return ret; } @@ -697,8 +715,9 @@ int get_socket_mtu(evutil_socket_t fd, int family, int verbose) { ret = val; #endif - if (verbose) + if (verbose) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: final=%d\n", __FUNCTION__, ret); + } return ret; } @@ -762,8 +781,9 @@ int handle_socket_error(void) { //////////////////// Misc utils ////////////////////////////// char *skip_blanks(char *s) { - while (*s == ' ' || *s == '\t' || *s == '\n') + while (*s == ' ' || *s == '\t' || *s == '\n') { ++s; + } return s; } @@ -810,9 +830,9 @@ int clock_gettime(int X, struct timeval *tv) { frequencyToMicroseconds = 10.; } } - if (usePerformanceCounter) + if (usePerformanceCounter) { QueryPerformanceCounter(&t); - else { + } else { GetSystemTimeAsFileTime(&f); t.QuadPart = f.dwHighDateTime; t.QuadPart <<= 32; @@ -858,10 +878,11 @@ char *dirname(char *path) { } int n = strlen(drive) + strlen(dir); - if (n > 0) + if (n > 0) { path[n] = 0; - else + } else { return NULL; + } return path; } #endif @@ -889,8 +910,9 @@ int getdomainname(char *name, size_t len) { strncpy(name, pszOut, n); name[n] = 0; TURN_LOG_FUNC(TURN_LOG_LEVEL_DEBUG, "DomainForestName: %s\n", pszOut); - } else + } else { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "wchar convert to char fail"); + } free(pszOut); break; @@ -909,8 +931,9 @@ int getdomainname(char *name, size_t len) { strncpy(name, pszOut, n); name[n] = 0; TURN_LOG_FUNC(TURN_LOG_LEVEL_DEBUG, "DomainNameDns: %s\n", pszOut); - } else + } else { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "wchar convert to char fail"); + } free(pszOut); break; @@ -929,8 +952,9 @@ int getdomainname(char *name, size_t len) { strncpy(name, pszOut, n); name[n] = 0; TURN_LOG_FUNC(TURN_LOG_LEVEL_DEBUG, "DomainNameFlat: %s\n", pszOut); - } else + } else { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "wchar convert to char fail"); + } free(pszOut); } else { @@ -953,20 +977,23 @@ int getdomainname(char *name, size_t len) { * \return */ wchar_t *_ATW(__in char *pszInBuf, __in int nInSize, __out wchar_t **pszOutBuf, __out int *pnOutSize) { - if (!pszInBuf || !pszOutBuf || !pnOutSize || nInSize <= 0) + if (!pszInBuf || !pszOutBuf || !pnOutSize || nInSize <= 0) { return NULL; + } // Get buffer size *pnOutSize = MultiByteToWideChar(NULL, NULL, pszInBuf, nInSize, *pszOutBuf, 0); - if (*pnOutSize == 0) + if (*pnOutSize == 0) { return NULL; + } (*pnOutSize)++; *pszOutBuf = malloc((*pnOutSize) * sizeof(wchar_t)); memset((void *)*pszOutBuf, 0, sizeof(wchar_t) * (*pnOutSize)); if (MultiByteToWideChar(NULL, NULL, pszInBuf, nInSize, *pszOutBuf, *pnOutSize) == 0) { free(*pszOutBuf); return NULL; - } else + } else { return *pszOutBuf; + } } /*! @@ -979,19 +1006,22 @@ wchar_t *_ATW(__in char *pszInBuf, __in int nInSize, __out wchar_t **pszOutBuf, * \return */ char *_WTA(__in wchar_t *pszInBuf, __in int nInSize, __out char **pszOutBuf, __out int *pnOutSize) { - if (!pszInBuf || !pszOutBuf || !pnOutSize || nInSize <= 0) + if (!pszInBuf || !pszOutBuf || !pnOutSize || nInSize <= 0) { return NULL; + } *pnOutSize = WideCharToMultiByte(NULL, NULL, pszInBuf, nInSize, *pszOutBuf, 0, NULL, NULL); - if (*pnOutSize == 0) + if (*pnOutSize == 0) { return NULL; + } (*pnOutSize)++; *pszOutBuf = malloc(*pnOutSize * sizeof(char)); memset((void *)*pszOutBuf, 0, sizeof(char) * (*pnOutSize)); if (WideCharToMultiByte(NULL, NULL, pszInBuf, nInSize, *pszOutBuf, *pnOutSize, NULL, NULL) == 0) { free(*pszOutBuf); return NULL; - } else + } else { return *pszOutBuf; + } } #endif @@ -1047,14 +1077,17 @@ void set_execdir(void) { if (_var && *_var) { _var = strdup(_var); char *edir = _var; - if (edir[0] != '.') + if (edir[0] != '.') { edir = strstr(edir, "/"); - if (edir && *edir) + } + if (edir && *edir) { edir = dirname(edir); - else + } else { edir = dirname(_var); - if (c_execdir) + } + if (c_execdir) { free(c_execdir); + } c_execdir = strdup(edir); free(_var); } @@ -1065,16 +1098,19 @@ void print_abs_file_name(const char *msg1, const char *msg2, const char *fn) { absfn[0] = 0; if (fn) { - while (fn[0] && fn[0] == ' ') + while (fn[0] && fn[0] == ' ') { ++fn; + } if (fn[0]) { if (fn[0] == '/') { STRCPY(absfn, fn); } else { - if (fn[0] == '.' && fn[1] && fn[1] == '/') + if (fn[0] == '.' && fn[1] && fn[1] == '/') { fn += 2; - if (!getcwd(absfn, sizeof(absfn) - 1)) + } + if (!getcwd(absfn, sizeof(absfn) - 1)) { absfn[0] = 0; + } size_t blen = strlen(absfn); if (blen < sizeof(absfn) - 1) { strncpy(absfn + blen, "/", sizeof(absfn) - blen); @@ -1286,8 +1322,9 @@ char *base64_encode(const unsigned char *data, size_t input_length, size_t *outp *output_length = 4 * ((input_length + 2) / 3); char *encoded_data = (char *)malloc(*output_length + 1); - if (encoded_data == NULL) + if (encoded_data == NULL) { return NULL; + } size_t i, j; for (i = 0, j = 0; i < input_length;) { @@ -1304,8 +1341,9 @@ char *base64_encode(const unsigned char *data, size_t input_length, size_t *outp encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F]; } - for (i = 0; i < mod_table[input_length % 3]; i++) + for (i = 0; i < mod_table[input_length % 3]; i++) { encoded_data[*output_length - 1 - i] = '='; + } encoded_data[*output_length] = 0; @@ -1318,27 +1356,33 @@ void build_base64_decoding_table(void) { memset(decoding_table, 0, 256); int i; - for (i = 0; i < 64; i++) + for (i = 0; i < 64; i++) { decoding_table[(unsigned char)encoding_table[i]] = (char)i; + } } unsigned char *base64_decode(const char *data, size_t input_length, size_t *output_length) { - if (decoding_table == NULL) + if (decoding_table == NULL) { build_base64_decoding_table(); + } - if (input_length % 4 != 0) + if (input_length % 4 != 0) { return NULL; + } *output_length = input_length / 4 * 3; - if (data[input_length - 1] == '=') + if (data[input_length - 1] == '=') { (*output_length)--; - if (data[input_length - 2] == '=') + } + if (data[input_length - 2] == '=') { (*output_length)--; + } unsigned char *decoded_data = (unsigned char *)malloc(*output_length); - if (decoded_data == NULL) + if (decoded_data == NULL) { return NULL; + } int i; size_t j; @@ -1351,12 +1395,15 @@ unsigned char *base64_decode(const char *data, size_t input_length, size_t *outp uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6); - if (j < *output_length) + if (j < *output_length) { decoded_data[j++] = (triple >> 2 * 8) & 0xFF; - if (j < *output_length) + } + if (j < *output_length) { decoded_data[j++] = (triple >> 1 * 8) & 0xFF; - if (j < *output_length) + } + if (j < *output_length) { decoded_data[j++] = (triple >> 0 * 8) & 0xFF; + } } return decoded_data; diff --git a/src/apps/common/hiredis_libevent2.c b/src/apps/common/hiredis_libevent2.c index 0fe5856..2bcad8e 100644 --- a/src/apps/common/hiredis_libevent2.c +++ b/src/apps/common/hiredis_libevent2.c @@ -143,15 +143,17 @@ static void redisLibeventCleanup(void *privdata) { struct redisLibeventEvents *e = (struct redisLibeventEvents *)privdata; if (e->allocated) { if (e->rev) { - if (e->rev_set) + if (e->rev_set) { event_del(e->rev); + } event_free(e->rev); e->rev = NULL; } e->rev_set = 0; if (e->wev) { - if (e->wev_set) + if (e->wev_set) { event_del(e->wev); + } event_free(e->wev); e->wev = NULL; } @@ -166,8 +168,9 @@ static void redisLibeventCleanup(void *privdata) { int is_redis_asyncconn_good(redis_context_handle rch) { if (rch) { struct redisLibeventEvents *e = (struct redisLibeventEvents *)rch; - if (redis_le_valid(e)) + if (redis_le_valid(e)) { return 1; + } } return 0; } @@ -216,14 +219,16 @@ redis_context_handle redisLibeventAttach(struct event_base *base, char *ip0, int redisAsyncContext *ac = NULL; char ip[256]; - if (ip0 && ip0[0]) + if (ip0 && ip0[0]) { STRCPY(ip, ip0); - else + } else { strncpy(ip, "127.0.0.1", sizeof(ip)); + } int port = DEFAULT_REDIS_PORT; - if (port0 > 0) + if (port0 > 0) { port = port0; + } ac = redisAsyncConnect(ip, port); if (!ac) { @@ -243,8 +248,9 @@ redis_context_handle redisLibeventAttach(struct event_base *base, char *ip0, int e->base = base; e->ip = strdup(ip); e->port = port; - if (pwd) + if (pwd) { e->pwd = strdup(pwd); + } e->db = db; /* Register functions to start/stop listening for events */ @@ -286,20 +292,23 @@ redis_context_handle redisLibeventAttach(struct event_base *base, char *ip0, int } static void redis_reconnect(struct redisLibeventEvents *e) { - if (!e || !(e->allocated)) + if (!e || !(e->allocated)) { return; + } if (e->rev) { - if (e->rev_set) + if (e->rev_set) { event_del(e->rev); + } event_free(e->rev); e->rev = NULL; } e->rev_set = 0; if (e->wev) { - if (e->wev_set) + if (e->wev_set) { event_del(e->wev); + } event_free(e->wev); e->wev = NULL; } diff --git a/src/apps/common/ns_turn_utils.c b/src/apps/common/ns_turn_utils.c index 4bc80e1..3f3c4a5 100644 --- a/src/apps/common/ns_turn_utils.c +++ b/src/apps/common/ns_turn_utils.c @@ -66,11 +66,13 @@ volatile int _log_time_value_set = 0; volatile turn_time_t _log_time_value = 0; static inline turn_time_t log_time(void) { - if (!log_start_time) + if (!log_start_time) { log_start_time = turn_time(); + } - if (_log_time_value_set) + if (_log_time_value_set) { return (_log_time_value - log_start_time); + } return (turn_time() - log_start_time); } @@ -175,8 +177,9 @@ static int syslog_facility = 0; static int str_to_syslog_facility(char *s) { int i; for (i = 0; str_fac[i]; i++) { - if (!strcasecmp(s, str_fac[i])) + if (!strcasecmp(s, str_fac[i])) { return int_fac[i]; + } } return -1; } @@ -218,8 +221,9 @@ void addr_debug_print(int verbose, const ioa_addr *addr, const char *s) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: EMPTY\n", s); } else { char addrbuf[INET6_ADDRSTRLEN]; - if (!s) + if (!s) { s = ""; + } if (addr->ss.sa_family == AF_INET) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "IPv4. %s: %s:%d\n", s, inet_ntop(AF_INET, &addr->s4.sin_addr, addrbuf, INET6_ADDRSTRLEN), nswap16(addr->s4.sin_port)); @@ -287,8 +291,9 @@ void set_log_file_line(int set) { _log_file_line_set = set; } void reset_rtpprintf(void) { log_lock(); if (_rtpfile) { - if (_rtpfile != stdout) + if (_rtpfile != stdout) { fclose(_rtpfile); + } _rtpfile = NULL; } log_unlock(); @@ -323,9 +328,9 @@ static void set_log_file_name_func(char *base, char *f, size_t fsz) { len = (int)strlen(base1); while (len >= 0) { - if (base1[len] == '/') + if (base1[len] == '/') { break; - else if (base1[len] == '.') { + } else if (base1[len] == '.') { free(tail); tail = strdup(base1 + len); base1[len] = 0; @@ -382,8 +387,9 @@ static void set_rtpfile(void) { } else { set_log_file_name(log_fn_base, log_fn); _rtpfile = fopen(log_fn, "a"); - if (_rtpfile) + if (_rtpfile) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "log file opened: %s\n", log_fn); + } } if (!_rtpfile) { fprintf(stderr, "ERROR: Cannot open log file for writing: %s\n", log_fn); @@ -399,49 +405,54 @@ static void set_rtpfile(void) { char logtail[FILE_STR_LEN]; char logf[FILE_STR_LEN]; - if (simple_log) + if (simple_log) { snprintf(logtail, FILE_STR_LEN, "turn.log"); - else + } else { snprintf(logtail, FILE_STR_LEN, "turn_%d_", (int)getpid()); + } - if (snprintf(logbase, FILE_STR_LEN, "/var/log/turnserver/%s", logtail) < 0) + if (snprintf(logbase, FILE_STR_LEN, "/var/log/turnserver/%s", logtail) < 0) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "String truncation occured.\n"); + } set_log_file_name(logbase, logf); _rtpfile = fopen(logf, "a"); - if (_rtpfile) + if (_rtpfile) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "log file opened: %s\n", logf); - else { - if (snprintf(logbase, FILE_STR_LEN, "/var/log/%s", logtail) < 0) + } else { + if (snprintf(logbase, FILE_STR_LEN, "/var/log/%s", logtail) < 0) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "String truncation occured.\n"); + } set_log_file_name(logbase, logf); _rtpfile = fopen(logf, "a"); - if (_rtpfile) + if (_rtpfile) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "log file opened: %s\n", logf); - else { - if (snprintf(logbase, FILE_STR_LEN, "/var/tmp/%s", logtail) < 0) + } else { + if (snprintf(logbase, FILE_STR_LEN, "/var/tmp/%s", logtail) < 0) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "String truncation occured.\n"); + } set_log_file_name(logbase, logf); _rtpfile = fopen(logf, "a"); - if (_rtpfile) + if (_rtpfile) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "log file opened: %s\n", logf); - else { - if (snprintf(logbase, FILE_STR_LEN, "/tmp/%s", logtail) < 0) + } else { + if (snprintf(logbase, FILE_STR_LEN, "/tmp/%s", logtail) < 0) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "String truncation occured.\n"); + } set_log_file_name(logbase, logf); _rtpfile = fopen(logf, "a"); - if (_rtpfile) + if (_rtpfile) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "log file opened: %s\n", logf); - else { + } else { snprintf(logbase, FILE_STR_LEN, "%s", logtail); set_log_file_name(logbase, logf); _rtpfile = fopen(logf, "a"); - if (_rtpfile) + if (_rtpfile) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "log file opened: %s\n", logf); - else { + } else { _rtpfile = stdout; return; } @@ -463,8 +474,9 @@ void set_simple_log(int val) { simple_log = val; } #define QUOTE(x) Q(x) void rollover_logfile(void) { - if (to_syslog || !(log_fn[0])) + if (to_syslog || !(log_fn[0])) { return; + } { FILE *f = fopen(log_fn, "r"); @@ -478,8 +490,9 @@ void rollover_logfile(void) { } } - if (simple_log) + if (simple_log) { return; + } log_lock(); if (_rtpfile && log_fn[0] && (_rtpfile != stdout)) { @@ -547,8 +560,9 @@ void turn_log_func_default(char *file, int line, TURN_LOG_LEVEL level, const cha so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "(%lu): ", (unsigned long)gettid()); #endif - if (_log_file_line_set) + if (_log_file_line_set) { so_far += snprintf(s + so_far, MAX_RTPPRINTF_BUFFER_SIZE - (so_far + 1), "%s(%d):", file, line); + } switch (level) { case TURN_LOG_LEVEL_DEBUG: @@ -572,8 +586,9 @@ void turn_log_func_default(char *file, int line, TURN_LOG_LEVEL level, const cha if (so_far > MAX_RTPPRINTF_BUFFER_SIZE + 1) { so_far = MAX_RTPPRINTF_BUFFER_SIZE + 1; } - if (!no_stdout_log) + if (!no_stdout_log) { fwrite(s, so_far, 1, stdout); + } /* write to syslog or to log file */ if (to_syslog) { @@ -605,46 +620,62 @@ int get_default_protocol_port(const char *scheme, size_t slen) { if (scheme && (slen > 0)) { switch (slen) { case 3: - if (!memcmp("ftp", scheme, 3)) + if (!memcmp("ftp", scheme, 3)) { return 21; - if (!memcmp("svn", scheme, 3)) + } + if (!memcmp("svn", scheme, 3)) { return 3690; - if (!memcmp("ssh", scheme, 3)) + } + if (!memcmp("ssh", scheme, 3)) { return 22; - if (!memcmp("sip", scheme, 3)) + } + if (!memcmp("sip", scheme, 3)) { return 5060; + } break; case 4: - if (!memcmp("http", scheme, 4)) + if (!memcmp("http", scheme, 4)) { return 80; - if (!memcmp("ldap", scheme, 4)) + } + if (!memcmp("ldap", scheme, 4)) { return 389; - if (!memcmp("sips", scheme, 4)) + } + if (!memcmp("sips", scheme, 4)) { return 5061; - if (!memcmp("turn", scheme, 4)) + } + if (!memcmp("turn", scheme, 4)) { return 3478; - if (!memcmp("stun", scheme, 4)) + } + if (!memcmp("stun", scheme, 4)) { return 3478; + } break; case 5: - if (!memcmp("https", scheme, 5)) + if (!memcmp("https", scheme, 5)) { return 443; - if (!memcmp("ldaps", scheme, 5)) + } + if (!memcmp("ldaps", scheme, 5)) { return 636; - if (!memcmp("turns", scheme, 5)) + } + if (!memcmp("turns", scheme, 5)) { return 5349; - if (!memcmp("stuns", scheme, 5)) + } + if (!memcmp("stuns", scheme, 5)) { return 5349; + } break; case 6: - if (!memcmp("telnet", scheme, 6)) + if (!memcmp("telnet", scheme, 6)) { return 23; - if (!memcmp("radius", scheme, 6)) + } + if (!memcmp("radius", scheme, 6)) { return 1645; + } break; case 7: - if (!memcmp("svn+ssh", scheme, 7)) + if (!memcmp("svn+ssh", scheme, 7)) { return 22; + } break; default: return 0; @@ -682,10 +713,11 @@ int get_canonic_origin(const char *o, char *co, int sz) { if (port < 1) { port = get_default_protocol_port(otmp, schlen); } - if (port > 0) + if (port > 0) { snprintf(otmp + schlen, sizeof(otmp) - schlen - 1, "://%s:%d", host, port); - else + } else { snprintf(otmp + schlen, sizeof(otmp) - schlen - 1, "://%s", host); + } { unsigned char *s = (unsigned char *)otmp + schlen + 3; diff --git a/src/apps/common/stun_buffer.c b/src/apps/common/stun_buffer.c index 513bdb5..9e5e24a 100644 --- a/src/apps/common/stun_buffer.c +++ b/src/apps/common/stun_buffer.c @@ -33,8 +33,9 @@ ////////////////////// BUFFERS /////////////////////////// int stun_init_buffer(stun_buffer *buf) { - if (!buf) + if (!buf) { return -1; + } memset(buf->buf, 0, sizeof(buf->buf)); buf->len = 0; buf->offset = 0; @@ -43,8 +44,9 @@ int stun_init_buffer(stun_buffer *buf) { } int stun_get_size(const stun_buffer *buf) { - if (!buf) + if (!buf) { return 0; + } return sizeof(buf->buf); } @@ -70,10 +72,11 @@ static inline int is_channel_msg(const stun_buffer *buf) { } int stun_is_command_message(const stun_buffer *buf) { - if (!buf || buf->len <= 0) + if (!buf || buf->len <= 0) { return 0; - else + } 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); } @@ -89,16 +92,18 @@ int stun_is_error_response(const stun_buffer *buf, int *err_code, uint8_t *err_m int 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) { - if (is_channel_msg(buf)) + if (is_channel_msg(buf)) { return 0; + } return IS_STUN_INDICATION(stun_get_msg_type(buf)); } uint16_t stun_get_method(const stun_buffer *buf) { return stun_get_method_str(buf->buf, (size_t)(buf->len)); } uint16_t stun_get_msg_type(const stun_buffer *buf) { - if (!buf) + if (!buf) { return (uint16_t)-1; + } return stun_get_msg_type_str(buf->buf, (size_t)buf->len); } @@ -137,8 +142,9 @@ int stun_init_channel_message(uint16_t chnumber, stun_buffer *buf, int length, i } int stun_is_channel_message(stun_buffer *buf, uint16_t *chnumber, int is_padding_mandatory) { - if (!buf) + if (!buf) { return 0; + } size_t blen = (size_t)buf->len; int ret = stun_is_channel_message_str(buf->buf, &blen, chnumber, is_padding_mandatory); if (ret) { @@ -209,8 +215,9 @@ int stun_attr_get_first_addr(const stun_buffer *buf, uint16_t attr_type, ioa_add } int stun_attr_add_even_port(stun_buffer *buf, uint8_t value) { - if (value) + if (value) { value = 0x80; + } return stun_attr_add(buf, STUN_ATTRIBUTE_EVEN_PORT, (const char *)&value, 1); } diff --git a/src/apps/common/win/getopt.c b/src/apps/common/win/getopt.c index 285d5a4..db65cc2 100644 --- a/src/apps/common/win/getopt.c +++ b/src/apps/common/win/getopt.c @@ -113,8 +113,9 @@ static const char illoptstring[] = "unknown option -- %s"; static void _vwarnx(const char *fmt, va_list ap) { (void)fprintf(stderr, "%s: ", __progname); - if (fmt != NULL) + if (fmt != NULL) { (void)vfprintf(stderr, fmt, ap); + } (void)fprintf(stderr, "\n"); } @@ -162,10 +163,11 @@ static void permute_args(int panonopt_start, int panonopt_end, int opt_end, char cstart = panonopt_end + i; pos = cstart; for (j = 0; j < cyclelen; j++) { - if (pos >= panonopt_end) + if (pos >= panonopt_end) { pos -= nnonopts; - else + } else { pos += nopts; + } swap = nargv[pos]; /* LINTED const cast */ ((char **)nargv)[pos] = nargv[cstart]; @@ -200,13 +202,15 @@ static int parse_long_options(char *const *nargv, const char *options, const str /* argument found (--option=arg) */ current_argv_len = has_equal - current_argv; has_equal++; - } else + } else { current_argv_len = strlen(current_argv); + } for (i = 0; long_options[i].name; i++) { /* find matching long option */ - if (strncmp(current_argv, long_options[i].name, current_argv_len)) + if (strncmp(current_argv, long_options[i].name, current_argv_len)) { continue; + } if (strlen(long_options[i].name) == current_argv_len) { /* exact match */ @@ -218,38 +222,43 @@ static int parse_long_options(char *const *nargv, const char *options, const str * If this is a known short option, don't allow * a partial match of a single character. */ - if (short_too && current_argv_len == 1) + if (short_too && current_argv_len == 1) { continue; + } - if (match == -1) /* partial match */ + if (match == -1) { /* partial match */ match = i; - else if (!IDENTICAL_INTERPRETATION(i, match)) + } else if (!IDENTICAL_INTERPRETATION(i, match)) { ambiguous = 1; + } } if (ambiguous) { /* ambiguous abbreviation */ - if (PRINT_ERROR) + if (PRINT_ERROR) { warnx(ambig, (int)current_argv_len, current_argv); + } optopt = 0; return (BADCH); } if (match != -1) { /* option found */ if (long_options[match].has_arg == no_argument && has_equal) { - if (PRINT_ERROR) + if (PRINT_ERROR) { warnx(noarg, (int)current_argv_len, current_argv); + } /* * XXX: GNU sets optopt to val regardless of flag */ - if (long_options[match].flag == NULL) + if (long_options[match].flag == NULL) { optopt = long_options[match].val; - else + } else { optopt = 0; + } return (BADARG); } if (long_options[match].has_arg == required_argument || long_options[match].has_arg == optional_argument) { - if (has_equal) + if (has_equal) { optarg = has_equal; - else if (long_options[match].has_arg == required_argument) { + } else if (long_options[match].has_arg == required_argument) { /* * optional argument doesn't use next nargv */ @@ -261,15 +270,17 @@ static int parse_long_options(char *const *nargv, const char *options, const str * Missing argument; leading ':' indicates no error * should be generated. */ - if (PRINT_ERROR) + if (PRINT_ERROR) { warnx(recargstring, current_argv); + } /* * XXX: GNU sets optopt to val regardless of flag */ - if (long_options[match].flag == NULL) + if (long_options[match].flag == NULL) { optopt = long_options[match].val; - else + } else { optopt = 0; + } --optind; return (BADARG); } @@ -278,18 +289,21 @@ static int parse_long_options(char *const *nargv, const char *options, const str --optind; return (-1); } - if (PRINT_ERROR) + if (PRINT_ERROR) { warnx(illoptstring, current_argv); + } optopt = 0; return (BADCH); } - if (idx) + if (idx) { *idx = match; + } if (long_options[match].flag) { *long_options[match].flag = long_options[match].val; return (0); - } else + } else { return (long_options[match].val); + } #undef IDENTICAL_INTERPRETATION } @@ -303,15 +317,17 @@ static int getopt_internal(int nargc, char *const *nargv, const char *options, c int optchar, short_too; static int posixly_correct = -1; - if (options == NULL) + if (options == NULL) { return (-1); + } /* * XXX Some GNU programs (like cvs) set optind to 0 instead of * XXX using optreset. Work around this braindamage. */ - if (optind == 0) + if (optind == 0) { optind = optreset = 1; + } /* * Disable GNU extensions if POSIXLY_CORRECT is set or options @@ -320,18 +336,22 @@ static int getopt_internal(int nargc, char *const *nargv, const char *options, c * CV, 2009-12-14: Check POSIXLY_CORRECT anew if optind == 0 or * optreset != 0 for GNU compatibility. */ - if (posixly_correct == -1 || optreset != 0) + if (posixly_correct == -1 || optreset != 0) { posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); - if (*options == '-') + } + if (*options == '-') { flags |= FLAG_ALLARGS; - else if (posixly_correct || *options == '+') + } else if (posixly_correct || *options == '+') { flags &= ~FLAG_PERMUTE; - if (*options == '+' || *options == '-') + } + if (*options == '+' || *options == '-') { options++; + } optarg = NULL; - if (optreset) + if (optreset) { nonopt_start = nonopt_end = -1; + } start: if (optreset || !*place) { /* update scanning pointer */ optreset = 0; @@ -369,9 +389,9 @@ start: return (-1); } /* do permutation */ - if (nonopt_start == -1) + if (nonopt_start == -1) { nonopt_start = optind; - else if (nonopt_end != -1) { + } else if (nonopt_end != -1) { permute_args(nonopt_start, nonopt_end, optind, nargv); nonopt_start = optind - (nonopt_end - nonopt_start); nonopt_end = -1; @@ -380,8 +400,9 @@ start: /* process next argument */ goto start; } - if (nonopt_start != -1 && nonopt_end == -1) + if (nonopt_start != -1 && nonopt_end == -1) { nonopt_end = optind; + } /* * If we have "-" do nothing, if "--" we are done. @@ -410,10 +431,11 @@ start: */ if (long_options != NULL && place != nargv[optind] && (*place == '-' || (flags & FLAG_LONGONLY))) { short_too = 0; - if (*place == '-') + if (*place == '-') { place++; /* --foo long option */ - else if (*place != ':' && strchr(options, *place) != NULL) + } else if (*place != ':' && strchr(options, *place) != NULL) { short_too = 1; /* could be short option too */ + } optchar = parse_long_options(nargv, options, long_options, idx, short_too); if (optchar != -1) { @@ -429,47 +451,55 @@ start: * options, return -1 (non-option) as per POSIX. * Otherwise, it is an unknown option character (or ':'). */ - if (optchar == (int)'-' && *place == '\0') + if (optchar == (int)'-' && *place == '\0') { return (-1); - if (!*place) + } + if (!*place) { ++optind; - if (PRINT_ERROR) + } + if (PRINT_ERROR) { warnx(illoptchar, optchar); + } optopt = optchar; return (BADCH); } if (long_options != NULL && optchar == 'W' && oli[1] == ';') { /* -W long-option */ - if (*place) /* no space */ + if (*place) /* no space */ { /* NOTHING */; - else if (++optind >= nargc) { /* no arg */ + } else if (++optind >= nargc) { /* no arg */ place = EMSG; - if (PRINT_ERROR) + if (PRINT_ERROR) { warnx(recargchar, optchar); + } optopt = optchar; return (BADARG); - } else /* white space */ + } else { /* white space */ place = nargv[optind]; + } optchar = parse_long_options(nargv, options, long_options, idx, 0); place = EMSG; return (optchar); } if (*++oli != ':') { /* doesn't take argument */ - if (!*place) + if (!*place) { ++optind; + } } else { /* takes (optional) argument */ optarg = NULL; - if (*place) /* no white space */ + if (*place) { /* no white space */ optarg = place; - else if (oli[1] != ':') { /* arg not optional */ - if (++optind >= nargc) { /* no arg */ + } else if (oli[1] != ':') { /* arg not optional */ + if (++optind >= nargc) { /* no arg */ place = EMSG; - if (PRINT_ERROR) + if (PRINT_ERROR) { warnx(recargchar, optchar); + } optopt = optchar; return (BADARG); - } else + } else { optarg = nargv[optind]; + } } place = EMSG; ++optind; diff --git a/src/apps/natdiscovery/natdiscovery.c b/src/apps/natdiscovery/natdiscovery.c index bf0e0ab..d4e6290 100644 --- a/src/apps/natdiscovery/natdiscovery.c +++ b/src/apps/natdiscovery/natdiscovery.c @@ -63,12 +63,14 @@ static int init_socket(int *socketfd, ioa_addr *local_addr, int local_port, ioa_ } *socketfd = socket(remote_addr->ss.sa_family, SOCK_DGRAM, 0); - if (udp_fd < 0) + if (udp_fd < 0) { err(-1, NULL); + } if (!addr_any(local_addr)) { - if (addr_bind(*socketfd, local_addr, 0, 1, UDP_SOCKET) < 0) + if (addr_bind(*socketfd, local_addr, 0, 1, UDP_SOCKET) < 0) { err(-1, NULL); + } } return ret; @@ -140,8 +142,9 @@ static int stunclient_send(int sockfd, ioa_addr *local_addr, int *local_port, io len = sendto(sockfd, req.getRawBuffer(), req.getSize(), 0, (struct sockaddr *)remote_addr, (socklen_t)slen); } while (len < 0 && (socket_eintr() || socket_enobufs() || socket_eagain())); - if (len < 0) + if (len < 0) { err(-1, NULL); + } } if (addr_get_from_sock(sockfd, local_addr) < 0) { @@ -184,8 +187,9 @@ static int stunclient_receive(int sockfd, ioa_addr *local_addr, ioa_addr *reflex ret = 1; return ret; } - if (recvd > 0) + if (recvd > 0) { len = recvd; + } buf.len = len; try { @@ -336,8 +340,9 @@ static int init_socket(int *socketfd, ioa_addr *local_addr, int local_port, ioa_ int ret = 0; *socketfd = socket(remote_addr->ss.sa_family, CLIENT_DGRAM_SOCKET_TYPE, CLIENT_DGRAM_SOCKET_PROTOCOL); - if (udp_fd < 0) + if (udp_fd < 0) { err(-1, NULL); + } if (local_port >= 0) { addr_set_port(local_addr, local_port); @@ -377,8 +382,9 @@ static int stunclient_send(stun_buffer *buf, int sockfd, ioa_addr *local_addr, i len = sendto(sockfd, buf->buf, buf->len, 0, (struct sockaddr *)remote_addr, (socklen_t)slen); } while (len < 0 && (socket_eintr() || socket_enobufs() || socket_eagain())); - if (len < 0) + if (len < 0) { err(-1, NULL); + } } if (addr_get_from_sock(sockfd, local_addr) < 0) { @@ -415,8 +421,9 @@ static int stunclient_receive(stun_buffer *buf, int sockfd, ioa_addr *local_addr } } while (len < 0 && socket_eintr()); - if (recvd > 0) + if (recvd > 0) { len = recvd; + } buf->len = len; if (stun_is_command_message(buf)) { @@ -601,12 +608,14 @@ static void init(int first, ioa_addr *local_addr, ioa_addr *remote_addr, int *lo err(-1, NULL); } } - if (!first) + if (!first) { *local_port = -1; + } *rfc5780 = 0; - if (make_ioa_addr((const uint8_t *)remote_param, port, remote_addr) < 0) + if (make_ioa_addr((const uint8_t *)remote_param, port, remote_addr) < 0) { err(-1, NULL); + } } static void discoveryresult(const char *decision) { @@ -632,8 +641,9 @@ int main(int argc, char **argv) { int first = 1; ioa_addr other_addr, reflexive_addr, tmp_addr, remote_addr, local_addr, local2_addr; - if (socket_init()) + if (socket_init()) { return -1; + } set_logfile("stdout"); set_no_stdout_log(1); diff --git a/src/apps/oauth/oauth.c b/src/apps/oauth/oauth.c index 40e315d..e44b963 100644 --- a/src/apps/oauth/oauth.c +++ b/src/apps/oauth/oauth.c @@ -105,8 +105,9 @@ static int encode_token(const char *server_name, const char *gcm_nonce, const ch memset(&etoken, 0, sizeof(etoken)); // TODO: avoid this hack - if (!*gcm_nonce) + if (!*gcm_nonce) { gcm_nonce = NULL; + } if (encode_oauth_token((const uint8_t *)server_name, &etoken, &key, &ot, (const uint8_t *)gcm_nonce) < 0) { fprintf(stderr, "%s: cannot encode oauth token\n", __FUNCTION__); @@ -370,8 +371,9 @@ int main(int argc, char **argv) { } } - for (i = optind; i < argc; i++) + for (i = optind; i < argc; i++) { printf("Non-option argument %s\n", argv[i]); + } if (optind > argc) { fprintf(stderr, "%s\n", Usage); @@ -456,8 +458,9 @@ int main(int argc, char **argv) { oauth_token dot; if (validate_decode_token(server_name, key, base64encoded_etoken, &dot) == 0) { printf("-=Valid token!=-\n"); - if (verbose_flag) + if (verbose_flag) { print_token_body(&dot); + } } else { fprintf(stderr, "Error during token validation and decoding\n"); exit(-1); diff --git a/src/apps/peer/mainudpserver.c b/src/apps/peer/mainudpserver.c index c8ef9f3..f872615 100644 --- a/src/apps/peer/mainudpserver.c +++ b/src/apps/peer/mainudpserver.c @@ -61,8 +61,9 @@ int main(int argc, char **argv) { int c; char ifname[1025] = "\0"; - if (socket_init()) + if (socket_init()) { return -1; + } IS_TURN_SERVER = 1; @@ -70,7 +71,7 @@ int main(int argc, char **argv) { set_no_stdout_log(1); set_system_parameters(0); - while ((c = getopt(argc, argv, "d:p:L:v")) != -1) + while ((c = getopt(argc, argv, "d:p:L:v")) != -1) { switch (c) { case 'd': STRCPY(ifname, optarg); @@ -89,6 +90,7 @@ int main(int argc, char **argv) { fprintf(stderr, "%s\n", Usage); exit(1); } + } if (las < 1) { local_addr_list = (char **)realloc(local_addr_list, ++las * sizeof(char *)); diff --git a/src/apps/peer/udpserver.c b/src/apps/peer/udpserver.c index bcc5b56..bac4472 100644 --- a/src/apps/peer/udpserver.c +++ b/src/apps/peer/udpserver.c @@ -36,8 +36,9 @@ static void udp_server_input_handler(evutil_socket_t fd, short what, void *arg) { - if (!(what & EV_READ)) + if (!(what & EV_READ)) { return; + } ioa_addr *addr = (ioa_addr *)arg; @@ -63,19 +64,22 @@ static void udp_server_input_handler(evutil_socket_t fd, short what, void *arg) static int udp_create_server_socket(server_type *server, const char *ifname, const char *local_address, int port) { - if (server && server->verbose) + if (server && server->verbose) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Start\n"); + } - if (!server) + if (!server) { return -1; + } evutil_socket_t udp_fd = -1; ioa_addr *server_addr = (ioa_addr *)malloc(sizeof(ioa_addr)); STRCPY(server->ifname, ifname); - if (make_ioa_addr((const uint8_t *)local_address, port, server_addr) < 0) + if (make_ioa_addr((const uint8_t *)local_address, port, server_addr) < 0) { return -1; + } udp_fd = socket(server_addr->ss.sa_family, RELAY_DGRAM_SOCKET_TYPE, RELAY_DGRAM_SOCKET_PROTOCOL); if (udp_fd < 0) { @@ -90,8 +94,9 @@ static int udp_create_server_socket(server_type *server, const char *ifname, con set_sock_buf_size(udp_fd, UR_SERVER_SOCK_BUF_SIZE); - if (addr_bind(udp_fd, server_addr, 1, 1, UDP_SOCKET) < 0) + if (addr_bind(udp_fd, server_addr, 1, 1, UDP_SOCKET) < 0) { return -1; + } socket_set_nonblocking(udp_fd); @@ -100,8 +105,9 @@ static int udp_create_server_socket(server_type *server, const char *ifname, con event_add(udp_ev, NULL); - if (server && server->verbose) + if (server && server->verbose) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "End\n"); + } return 0; } @@ -110,8 +116,9 @@ static server_type *init_server(int verbose, const char *ifname, char **local_ad server_type *server = (server_type *)malloc(sizeof(server_type)); - if (!server) + if (!server) { return server; + } memset(server, 0, sizeof(server_type)); @@ -129,8 +136,9 @@ static server_type *init_server(int verbose, const char *ifname, char **local_ad static int clean_server(server_type *server) { if (server) { - if (server->event_base) + if (server->event_base) { event_base_free(server->event_base); + } free(server); } return 0; @@ -140,8 +148,9 @@ static int clean_server(server_type *server) { static void run_events(server_type *server) { - if (!server) + if (!server) { return; + } struct timeval timeout; @@ -174,8 +183,9 @@ void run_udp_server(server_type *server) { } void clean_udp_server(server_type *server) { - if (server) + if (server) { clean_server(server); + } } ////////////////////////////////////////////////////////////////// diff --git a/src/apps/relay/acme.c b/src/apps/relay/acme.c index 160eb88..f2170df 100644 --- a/src/apps/relay/acme.c +++ b/src/apps/relay/acme.c @@ -17,26 +17,31 @@ static int is_acme_req(char *req, size_t len) { int c, i, k; // Check first request line. Should be like: GET path HTTP/1.x - if (strncmp(req, GET_ACME_PREFIX, GET_ACME_PREFIX_LEN)) + if (strncmp(req, GET_ACME_PREFIX, GET_ACME_PREFIX_LEN)) { return -1; + } // Usually (for LE) the "method path" is 32 + 43 = 55 chars. But other // implementations may choose longer pathes. We define PATHMAX = 127 chars // to be prepared for "DoS" attacks (STUN msg size max. is ~ 64K). len -= 21; // min size of trailing headers - if (len > 131) + if (len > 131) { len = 131; + } for (i = GET_ACME_PREFIX_LEN; i < (int)len; i++) { // find the end of the path - if (req[i] != ' ') + if (req[i] != ' ') { continue; + } // consider path < 10 chars invalid. Also we wanna see a "trailer". - if (i < (GET_ACME_PREFIX_LEN + 10) || strncmp(req + i, " HTTP/1.", 8)) + if (i < (GET_ACME_PREFIX_LEN + 10) || strncmp(req + i, " HTTP/1.", 8)) { return -2; + } // finally check for allowed chars for (k = GET_ACME_PREFIX_LEN; k < i; k++) { c = req[k]; - if ((c > 127) || (A[c] == ' ')) + if ((c > 127) || (A[c] == ' ')) { return -3; + } } // all checks passed: sufficient for us to answer with a redirect return i; @@ -50,11 +55,13 @@ int try_acme_redirect(char *req, size_t len, const char *url, ioa_socket_handle char http_response[1024]; size_t plen, rlen; - if (url == NULL || url[0] == '\0' || req == NULL || s == 0) + if (url == NULL || url[0] == '\0' || req == NULL || s == 0) { return 1; + } if (len < (GET_ACME_PREFIX_LEN + 32) || len > (512 - GET_ACME_PREFIX_LEN) || - (plen = is_acme_req(req, len)) < (GET_ACME_PREFIX_LEN + 1)) + (plen = is_acme_req(req, len)) < (GET_ACME_PREFIX_LEN + 1)) { return 2; + } req[plen] = '\0'; diff --git a/src/apps/relay/dbdrivers/dbd_mongo.c b/src/apps/relay/dbdrivers/dbd_mongo.c index d12884b..1f1ccbf 100644 --- a/src/apps/relay/dbdrivers/dbd_mongo.c +++ b/src/apps/relay/dbdrivers/dbd_mongo.c @@ -72,10 +72,12 @@ static void mongo_logger(mongoc_log_level_t log_level, const char *log_domain, c static void MongoFree(MONGO *info) { if (info) { - if (info->uri) + if (info->uri) { mongoc_uri_destroy(info->uri); - if (info->client) + } + if (info->client) { mongoc_client_destroy(info->client); + } free(info); } } @@ -108,8 +110,9 @@ static MONGO *get_mongodb_connection(void) { mydbconnection = NULL; } else { mydbconnection->database = mongoc_uri_get_database(mydbconnection->uri); - if (!mydbconnection->database) + if (!mydbconnection->database) { mydbconnection->database = MONGO_DEFAULT_DB; + } if (mydbconnection) { (void)pthread_setspecific(connection_key, mydbconnection); } @@ -143,8 +146,9 @@ static mongoc_collection_t *mongo_get_collection(const char *name) { static int mongo_get_auth_secrets(secrets_list_t *sl, uint8_t *realm) { mongoc_collection_t *collection = mongo_get_collection("turn_secret"); - if (!collection) + if (!collection) { return -1; + } bson_t query; bson_init(&query); @@ -185,8 +189,9 @@ static int mongo_get_auth_secrets(secrets_list_t *sl, uint8_t *realm) { static int mongo_get_user_key(uint8_t *usname, uint8_t *realm, hmackey_t key) { mongoc_collection_t *collection = mongo_get_collection("turnusers_lt"); - if (!collection) + if (!collection) { return -1; + } bson_t query; bson_init(&query); @@ -240,8 +245,9 @@ static int mongo_get_oauth_key(const uint8_t *kid, oauth_key_data_raw *key) { mongoc_collection_t *collection = mongo_get_collection("oauth_key"); - if (!collection) + if (!collection) { return -1; + } bson_t query; bson_init(&query); @@ -298,8 +304,9 @@ static int mongo_get_oauth_key(const uint8_t *kid, oauth_key_data_raw *key) { static int mongo_set_user_key(uint8_t *usname, uint8_t *realm, const char *key) { mongoc_collection_t *collection = mongo_get_collection("turnusers_lt"); - if (!collection) + if (!collection) { return -1; + } bson_t query; bson_init(&query); @@ -329,8 +336,9 @@ static int mongo_set_oauth_key(oauth_key_data_raw *key) { mongoc_collection_t *collection = mongo_get_collection("oauth_key"); - if (!collection) + if (!collection) { return -1; + } bson_t query; bson_init(&query); @@ -361,8 +369,9 @@ static int mongo_set_oauth_key(oauth_key_data_raw *key) { static int mongo_del_user(uint8_t *usname, uint8_t *realm) { mongoc_collection_t *collection = mongo_get_collection("turnusers_lt"); - if (!collection) + if (!collection) { return -1; + } bson_t query; bson_init(&query); @@ -385,8 +394,9 @@ static int mongo_del_oauth_key(const uint8_t *kid) { mongoc_collection_t *collection = mongo_get_collection("oauth_key"); - if (!collection) + if (!collection) { return -1; + } bson_t query; bson_init(&query); @@ -409,11 +419,13 @@ static int mongo_list_users(uint8_t *realm, secrets_list_t *users, secrets_list_ mongoc_collection_t *collection = mongo_get_collection(collection_name); uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } - if (!collection) + if (!collection) { return -1; + } bson_t query, child; bson_init(&query); @@ -484,8 +496,9 @@ static int mongo_list_oauth_keys(secrets_list_t *kids, secrets_list_t *teas, sec const char *collection_name = "oauth_key"; mongoc_collection_t *collection = mongo_get_collection(collection_name); - if (!collection) + if (!collection) { return -1; + } bson_t query; bson_init(&query); @@ -572,11 +585,13 @@ static int mongo_list_secrets(uint8_t *realm, secrets_list_t *secrets, secrets_l mongoc_collection_t *collection = mongo_get_collection("turn_secret"); uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } - if (!collection) + if (!collection) { return -1; + } bson_t query, child; bson_init(&query); @@ -644,8 +659,9 @@ static int mongo_list_secrets(uint8_t *realm, secrets_list_t *secrets, secrets_l static int mongo_del_secret(uint8_t *secret, uint8_t *realm) { mongoc_collection_t *collection = mongo_get_collection("turn_secret"); - if (!collection) + if (!collection) { return -1; + } bson_t query; bson_init(&query); @@ -663,8 +679,9 @@ static int mongo_del_secret(uint8_t *secret, uint8_t *realm) { static int mongo_set_secret(uint8_t *secret, uint8_t *realm) { mongoc_collection_t *collection = mongo_get_collection("turn_secret"); - if (!collection) + if (!collection) { return -1; + } bson_t query; bson_init(&query); @@ -689,14 +706,16 @@ static int mongo_set_permission_ip(const char *kind, uint8_t *realm, const char mongoc_collection_t *collection = mongo_get_collection("realm"); - if (!collection) + if (!collection) { return -1; + } int ret = -1; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } bson_t query, doc, child; bson_init(&query); @@ -732,14 +751,16 @@ static int mongo_set_permission_ip(const char *kind, uint8_t *realm, const char static int mongo_add_origin(uint8_t *origin, uint8_t *realm) { mongoc_collection_t *collection = mongo_get_collection("realm"); - if (!collection) + if (!collection) { return -1; + } int ret = -1; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } bson_t query, doc, child; bson_init(&query); @@ -763,8 +784,9 @@ static int mongo_add_origin(uint8_t *origin, uint8_t *realm) { static int mongo_del_origin(uint8_t *origin) { mongoc_collection_t *collection = mongo_get_collection("realm"); - if (!collection) + if (!collection) { return -1; + } int ret = -1; @@ -789,12 +811,14 @@ static int mongo_del_origin(uint8_t *origin) { static int mongo_list_origins(uint8_t *realm, secrets_list_t *origins, secrets_list_t *realms) { mongoc_collection_t *collection = mongo_get_collection("realm"); - if (!collection) + if (!collection) { return -1; + } uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } bson_t query, child; bson_init(&query); @@ -867,8 +891,9 @@ static int mongo_list_origins(uint8_t *realm, secrets_list_t *origins, secrets_l static int mongo_set_realm_option_one(uint8_t *realm, unsigned long value, const char *opt) { mongoc_collection_t *collection = mongo_get_collection("realm"); - if (!collection) + if (!collection) { return -1; + } bson_t query, doc, child; bson_init(&query); @@ -907,8 +932,9 @@ static int mongo_set_realm_option_one(uint8_t *realm, unsigned long value, const static int mongo_list_realm_options(uint8_t *realm) { mongoc_collection_t *collection = mongo_get_collection("realm"); - if (!collection) + if (!collection) { return -1; + } bson_t query, child; bson_init(&query); @@ -991,8 +1017,9 @@ static int mongo_read_realms_ip_lists(const char *kind, ip_range_list_t *list) { mongoc_collection_t *collection = mongo_get_collection("realm"); - if (!collection) + if (!collection) { return ret; + } bson_t query; bson_init(&query); @@ -1057,8 +1084,9 @@ static void mongo_reread_realms(secrets_list_t *realms_list) { mongoc_collection_t *collection = mongo_get_collection("realm"); - if (!collection) + if (!collection) { return; + } bson_t query; bson_init(&query); @@ -1139,13 +1167,13 @@ static void mongo_reread_realms(secrets_list_t *realms_list) { _v = (uint64_t)bson_iter_int64(&options_iter); } if (_v) { - if (!strcmp(_k, "max-bps")) + if (!strcmp(_k, "max-bps")) { rp->options.perf_options.max_bps = (band_limit_t)_v; - else if (!strcmp(_k, "total-quota")) + } else if (!strcmp(_k, "total-quota")) { rp->options.perf_options.total_quota = (vint)_v; - else if (!strcmp(_k, "user-quota")) + } else if (!strcmp(_k, "user-quota")) { rp->options.perf_options.user_quota = (vint)_v; - else { + } else { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unknown realm option: %s\n", _k); } } @@ -1168,8 +1196,9 @@ static void mongo_reread_realms(secrets_list_t *realms_list) { static int mongo_get_admin_user(const uint8_t *usname, uint8_t *realm, password_t pwd) { mongoc_collection_t *collection = mongo_get_collection("admin_user"); - if (!collection) + if (!collection) { return -1; + } realm[0] = 0; pwd[0] = 0; @@ -1215,8 +1244,9 @@ static int mongo_get_admin_user(const uint8_t *usname, uint8_t *realm, password_ static int mongo_set_admin_user(const uint8_t *usname, const uint8_t *realm, const password_t pwd) { mongoc_collection_t *collection = mongo_get_collection("admin_user"); - if (!collection) + if (!collection) { return -1; + } bson_t query; bson_init(&query); @@ -1244,8 +1274,9 @@ static int mongo_set_admin_user(const uint8_t *usname, const uint8_t *realm, con static int mongo_del_admin_user(const uint8_t *usname) { mongoc_collection_t *collection = mongo_get_collection("admin_user"); - if (!collection) + if (!collection) { return -1; + } bson_t query; bson_init(&query); @@ -1267,8 +1298,9 @@ static int mongo_list_admin_users(int no_print) { const char *collection_name = "admin_user"; mongoc_collection_t *collection = mongo_get_collection(collection_name); - if (!collection) + if (!collection) { return -1; + } bson_t query, child; bson_init(&query); diff --git a/src/apps/relay/dbdrivers/dbd_mysql.c b/src/apps/relay/dbdrivers/dbd_mysql.c index c1d2778..615ff31 100644 --- a/src/apps/relay/dbdrivers/dbd_mysql.c +++ b/src/apps/relay/dbdrivers/dbd_mysql.c @@ -60,24 +60,33 @@ typedef struct _Myconninfo Myconninfo; static void MyconninfoFree(Myconninfo *co) { if (co) { - if (co->host) + if (co->host) { free(co->host); - if (co->dbname) + } + if (co->dbname) { free(co->dbname); - if (co->user) + } + if (co->user) { free(co->user); - if (co->password) + } + if (co->password) { free(co->password); - if (co->key) + } + if (co->key) { free(co->key); - if (co->ca) + } + if (co->ca) { free(co->ca); - if (co->cert) + } + if (co->cert) { free(co->cert); - if (co->capath) + } + if (co->capath) { free(co->capath); - if (co->cipher) + } + if (co->cipher) { free(co->cipher); + } memset(co, 0, sizeof(Myconninfo)); free(co); } @@ -120,8 +129,9 @@ static Myconninfo *MyconninfoParse(char *userdb, char **errmsg) { while (s && *s) { - while (*s && (*s == ' ')) + while (*s && (*s == ' ')) { ++s; + } char *snext = strstr(s, " "); if (snext) { *snext = 0; @@ -139,69 +149,69 @@ static Myconninfo *MyconninfoParse(char *userdb, char **errmsg) { } *seq = 0; - if (!strcmp(s, "host")) + if (!strcmp(s, "host")) { co->host = strdup(seq + 1); - else if (!strcmp(s, "ip")) + } else if (!strcmp(s, "ip")) { co->host = strdup(seq + 1); - else if (!strcmp(s, "addr")) + } else if (!strcmp(s, "addr")) { co->host = strdup(seq + 1); - else if (!strcmp(s, "ipaddr")) + } else if (!strcmp(s, "ipaddr")) { co->host = strdup(seq + 1); - else if (!strcmp(s, "hostaddr")) + } else if (!strcmp(s, "hostaddr")) { co->host = strdup(seq + 1); - else if (!strcmp(s, "dbname")) + } else if (!strcmp(s, "dbname")) { co->dbname = strdup(seq + 1); - else if (!strcmp(s, "db")) + } else if (!strcmp(s, "db")) { co->dbname = strdup(seq + 1); - else if (!strcmp(s, "database")) + } else if (!strcmp(s, "database")) { co->dbname = strdup(seq + 1); - else if (!strcmp(s, "user")) + } else if (!strcmp(s, "user")) { co->user = strdup(seq + 1); - else if (!strcmp(s, "uname")) + } else if (!strcmp(s, "uname")) { co->user = strdup(seq + 1); - else if (!strcmp(s, "name")) + } else if (!strcmp(s, "name")) { co->user = strdup(seq + 1); - else if (!strcmp(s, "username")) + } else if (!strcmp(s, "username")) { co->user = strdup(seq + 1); - else if (!strcmp(s, "password")) + } else if (!strcmp(s, "password")) { co->password = strdup(seq + 1); - else if (!strcmp(s, "pwd")) + } else if (!strcmp(s, "pwd")) { co->password = strdup(seq + 1); - else if (!strcmp(s, "passwd")) + } else if (!strcmp(s, "passwd")) { co->password = strdup(seq + 1); - else if (!strcmp(s, "secret")) + } else if (!strcmp(s, "secret")) { co->password = strdup(seq + 1); - else if (!strcmp(s, "port")) + } else if (!strcmp(s, "port")) { co->port = (unsigned int)atoi(seq + 1); - else if (!strcmp(s, "p")) + } else if (!strcmp(s, "p")) { co->port = (unsigned int)atoi(seq + 1); - else if (!strcmp(s, "connect_timeout")) + } else if (!strcmp(s, "connect_timeout")) { co->connect_timeout = (unsigned int)atoi(seq + 1); - else if (!strcmp(s, "timeout")) + } else if (!strcmp(s, "timeout")) { co->connect_timeout = (unsigned int)atoi(seq + 1); - else if (!strcmp(s, "read_timeout")) + } else if (!strcmp(s, "read_timeout")) { co->read_timeout = (unsigned int)atoi(seq + 1); - else if (!strcmp(s, "key")) + } else if (!strcmp(s, "key")) { co->key = strdup(seq + 1); - else if (!strcmp(s, "ssl-key")) + } else if (!strcmp(s, "ssl-key")) { co->key = strdup(seq + 1); - else if (!strcmp(s, "ca")) + } else if (!strcmp(s, "ca")) { co->ca = strdup(seq + 1); - else if (!strcmp(s, "ssl-ca")) + } else if (!strcmp(s, "ssl-ca")) { co->ca = strdup(seq + 1); - else if (!strcmp(s, "capath")) + } else if (!strcmp(s, "capath")) { co->capath = strdup(seq + 1); - else if (!strcmp(s, "ssl-capath")) + } else if (!strcmp(s, "ssl-capath")) { co->capath = strdup(seq + 1); - else if (!strcmp(s, "cert")) + } else if (!strcmp(s, "cert")) { co->cert = strdup(seq + 1); - else if (!strcmp(s, "ssl-cert")) + } else if (!strcmp(s, "ssl-cert")) { co->cert = strdup(seq + 1); - else if (!strcmp(s, "cipher")) + } else if (!strcmp(s, "cipher")) { co->cipher = strdup(seq + 1); - else if (!strcmp(s, "ssl-cipher")) + } else if (!strcmp(s, "ssl-cipher")) { co->cipher = strdup(seq + 1); - else { + } else { MyconninfoFree(co); co = NULL; if (errmsg) { @@ -217,14 +227,18 @@ static Myconninfo *MyconninfoParse(char *userdb, char **errmsg) { } if (co) { - if (!(co->dbname)) + if (!(co->dbname)) { co->dbname = strdup("0"); - if (!(co->host)) + } + if (!(co->host)) { co->host = strdup("127.0.0.1"); - if (!(co->user)) + } + if (!(co->user)) { co->user = strdup(""); - if (!(co->password)) + } + if (!(co->password)) { co->password = strdup(""); + } } return co; @@ -270,10 +284,12 @@ static MYSQL *get_mydb_connection(void) { if (!mydbconnection) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot initialize MySQL DB connection\n"); } else { - if (co->connect_timeout) + if (co->connect_timeout) { mysql_options(mydbconnection, MYSQL_OPT_CONNECT_TIMEOUT, &(co->connect_timeout)); - if (co->read_timeout) + } + if (co->read_timeout) { mysql_options(mydbconnection, MYSQL_OPT_READ_TIMEOUT, &(co->read_timeout)); + } if (co->ca || co->capath || co->cert || co->cipher || co->key) { mysql_ssl_set(mydbconnection, co->key, co->cert, co->ca, co->capath, co->cipher); } @@ -298,8 +314,9 @@ static MYSQL *get_mydb_connection(void) { if (turn_params.secret_key_file[0]) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Encryption with AES is activated.\n"); TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Connection is secure.\n"); - } else + } else { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Connection is not secure.\n"); + } donot_print_connection_success = 1; } } @@ -348,8 +365,9 @@ static int mysql_get_auth_secrets(secrets_list_t *sl, uint8_t *realm) { ret = 0; } - if (mres) + if (mres) { mysql_free_result(mres); + } } } return ret; @@ -395,8 +413,9 @@ static int mysql_get_user_key(uint8_t *usname, uint8_t *realm, hmackey_t key) { } } - if (mres) + if (mres) { mysql_free_result(mres); + } } } return ret; @@ -451,8 +470,9 @@ static int mysql_get_oauth_key(const uint8_t *kid, oauth_key_data_raw *key) { } } - if (mres) + if (mres) { mysql_free_result(mres); + } } } return ret; @@ -531,8 +551,9 @@ static int mysql_list_oauth_keys(secrets_list_t *kids, secrets_list_t *teas, sec } } - if (mres) + if (mres) { mysql_free_result(mres); + } } } @@ -630,8 +651,9 @@ static int mysql_list_users(uint8_t *realm, secrets_list_t *users, secrets_list_ char statement[TURN_LONG_STRING_SIZE]; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } MYSQL *myc = get_mydb_connection(); if (myc) { @@ -675,8 +697,9 @@ static int mysql_list_users(uint8_t *realm, secrets_list_t *users, secrets_list_ ret = 0; } - if (mres) + if (mres) { mysql_free_result(mres); + } } } return ret; @@ -686,8 +709,9 @@ static int mysql_list_secrets(uint8_t *realm, secrets_list_t *secrets, secrets_l int ret = -1; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } char statement[TURN_LONG_STRING_SIZE]; if (realm[0]) { @@ -737,8 +761,9 @@ static int mysql_list_secrets(uint8_t *realm, secrets_list_t *secrets, secrets_l ret = 0; } - if (mres) + if (mres) { mysql_free_result(mres); + } } } return ret; @@ -750,10 +775,11 @@ static int mysql_del_secret(uint8_t *secret, uint8_t *realm) { char statement[TURN_LONG_STRING_SIZE]; MYSQL *myc = get_mydb_connection(); if (myc) { - if (!secret || (secret[0] == 0)) + if (!secret || (secret[0] == 0)) { snprintf(statement, sizeof(statement), "delete from turn_secret where realm='%s'", realm); - else + } else { snprintf(statement, sizeof(statement), "delete from turn_secret where value='%s' and realm='%s'", secret, realm); + } mysql_query(myc, statement); ret = 0; } @@ -781,8 +807,9 @@ static int mysql_set_permission_ip(const char *kind, uint8_t *realm, const char int ret = -1; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } donot_print_connection_success = 1; @@ -844,8 +871,9 @@ static int mysql_list_origins(uint8_t *realm, secrets_list_t *origins, secrets_l int ret = -1; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } donot_print_connection_success = 1; @@ -894,8 +922,9 @@ static int mysql_list_origins(uint8_t *realm, secrets_list_t *origins, secrets_l ret = 0; } - if (mres) + if (mres) { mysql_free_result(mres); + } } } return ret; @@ -959,8 +988,9 @@ static int mysql_list_realm_options(uint8_t *realm) { ret = 0; } - if (mres) + if (mres) { mysql_free_result(mres); + } } } return ret; @@ -1033,8 +1063,9 @@ static int mysql_get_ip_list(const char *kind, ip_range_list_t *list) { ret = 0; } - if (mres) + if (mres) { mysql_free_result(mres); + } } } return ret; @@ -1077,8 +1108,9 @@ static void mysql_reread_realms(secrets_list_t *realms_list) { update_o_to_realm(o_to_realm_new); } - if (mres) + if (mres) { mysql_free_result(mres); + } } } { @@ -1136,13 +1168,13 @@ static void mysql_reread_realms(secrets_list_t *realms_list) { memcpy(vval, row[2], sz); vval[sz] = 0; realm_params_t *rp = get_realm(rval); - if (!strcmp(oval, "max-bps")) + if (!strcmp(oval, "max-bps")) { rp->options.perf_options.max_bps = (band_limit_t)strtoul(vval, NULL, 10); - else if (!strcmp(oval, "total-quota")) + } else if (!strcmp(oval, "total-quota")) { rp->options.perf_options.total_quota = (vint)atoi(vval); - else if (!strcmp(oval, "user-quota")) + } else if (!strcmp(oval, "user-quota")) { rp->options.perf_options.user_quota = (vint)atoi(vval); - else { + } else { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unknown realm option: %s\n", oval); } } @@ -1151,8 +1183,9 @@ static void mysql_reread_realms(secrets_list_t *realms_list) { } } - if (mres) + if (mres) { mysql_free_result(mres); + } } } } @@ -1187,8 +1220,9 @@ static int mysql_get_admin_user(const uint8_t *usname, uint8_t *realm, password_ } } - if (mres) + if (mres) { mysql_free_result(mres); + } } } return ret; @@ -1271,8 +1305,9 @@ static int mysql_list_admin_users(int no_print) { } } - if (mres) + if (mres) { mysql_free_result(mres); + } } } return ret; diff --git a/src/apps/relay/dbdrivers/dbd_pgsql.c b/src/apps/relay/dbdrivers/dbd_pgsql.c index 29e4207..2027d42 100644 --- a/src/apps/relay/dbdrivers/dbd_pgsql.c +++ b/src/apps/relay/dbdrivers/dbd_pgsql.c @@ -68,8 +68,9 @@ static PGconn *get_pqdb_connection(void) { } } else { PQconninfoFree(co); - if (errmsg) + if (errmsg) { free(errmsg); + } pqdbconnection = PQconnectdb(pud->userdb); if (!pqdbconnection) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot open PostgreSQL DB connection: <%s>, runtime error\n", @@ -154,8 +155,9 @@ static int pgsql_get_user_key(uint8_t *usname, uint8_t *realm, hmackey_t key) { } } - if (res) + if (res) { PQclear(res); + } } return ret; } @@ -363,8 +365,9 @@ static int pgsql_list_users(uint8_t *realm, secrets_list_t *users, secrets_list_ char statement[TURN_LONG_STRING_SIZE]; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } PGconn *pqc = get_pqdb_connection(); if (pqc) { @@ -412,8 +415,9 @@ static int pgsql_list_secrets(uint8_t *realm, secrets_list_t *secrets, secrets_l int ret = -1; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } char statement[TURN_LONG_STRING_SIZE]; if (realm[0]) { @@ -465,10 +469,11 @@ static int pgsql_del_secret(uint8_t *secret, uint8_t *realm) { char statement[TURN_LONG_STRING_SIZE]; PGconn *pqc = get_pqdb_connection(); if (pqc) { - if (!secret || (secret[0] == 0)) + if (!secret || (secret[0] == 0)) { snprintf(statement, sizeof(statement), "delete from turn_secret where realm='%s'", realm); - else + } else { snprintf(statement, sizeof(statement), "delete from turn_secret where value='%s' and realm='%s'", secret, realm); + } PGresult *res = PQexec(pqc, statement); if (res) { @@ -504,8 +509,9 @@ static int pgsql_set_permission_ip(const char *kind, uint8_t *realm, const char int ret = -1; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } donot_print_connection_success = 1; @@ -580,8 +586,9 @@ static int pgsql_list_origins(uint8_t *realm, secrets_list_t *origins, secrets_l int ret = -1; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } donot_print_connection_success = 1; @@ -834,13 +841,13 @@ static void pgsql_reread_realms(secrets_list_t *realms_list) { char *vval = PQgetvalue(res, i, 2); if (rval && oval && vval) { realm_params_t *rp = get_realm(rval); - if (!strcmp(oval, "max-bps")) + if (!strcmp(oval, "max-bps")) { rp->options.perf_options.max_bps = (band_limit_t)strtoul(vval, NULL, 10); - else if (!strcmp(oval, "total-quota")) + } else if (!strcmp(oval, "total-quota")) { rp->options.perf_options.total_quota = (vint)atoi(vval); - else if (!strcmp(oval, "user-quota")) + } else if (!strcmp(oval, "user-quota")) { rp->options.perf_options.user_quota = (vint)atoi(vval); - else { + } else { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unknown realm option: %s\n", oval); } } @@ -882,8 +889,9 @@ static int pgsql_get_admin_user(const uint8_t *usname, uint8_t *realm, password_ ret = 0; } - if (res) + if (res) { PQclear(res); + } } return ret; } diff --git a/src/apps/relay/dbdrivers/dbd_redis.c b/src/apps/relay/dbdrivers/dbd_redis.c index f901496..e67bf37 100644 --- a/src/apps/relay/dbdrivers/dbd_redis.c +++ b/src/apps/relay/dbdrivers/dbd_redis.c @@ -58,12 +58,15 @@ typedef struct _Ryconninfo Ryconninfo; static void RyconninfoFree(Ryconninfo *co) { if (co) { - if (co->host) + if (co->host) { free(co->host); - if (co->dbname) + } + if (co->dbname) { free(co->dbname); - if (co->password) + } + if (co->password) { free(co->password); + } memset(co, 0, sizeof(Ryconninfo)); free(co); } @@ -78,8 +81,9 @@ static Ryconninfo *RyconninfoParse(const char *userdb, char **errmsg) { while (s && *s) { - while (*s && (*s == ' ')) + while (*s && (*s == ' ')) { ++s; + } char *snext = strstr(s, " "); if (snext) { *snext = 0; @@ -97,47 +101,47 @@ static Ryconninfo *RyconninfoParse(const char *userdb, char **errmsg) { } *seq = 0; - if (!strcmp(s, "host")) + if (!strcmp(s, "host")) { co->host = strdup(seq + 1); - else if (!strcmp(s, "ip")) + } else if (!strcmp(s, "ip")) { co->host = strdup(seq + 1); - else if (!strcmp(s, "addr")) + } else if (!strcmp(s, "addr")) { co->host = strdup(seq + 1); - else if (!strcmp(s, "ipaddr")) + } else if (!strcmp(s, "ipaddr")) { co->host = strdup(seq + 1); - else if (!strcmp(s, "hostaddr")) + } else if (!strcmp(s, "hostaddr")) { co->host = strdup(seq + 1); - else if (!strcmp(s, "dbname")) + } else if (!strcmp(s, "dbname")) { co->dbname = strdup(seq + 1); - else if (!strcmp(s, "db")) + } else if (!strcmp(s, "db")) { co->dbname = strdup(seq + 1); - else if (!strcmp(s, "database")) + } else if (!strcmp(s, "database")) { co->dbname = strdup(seq + 1); - else if (!strcmp(s, "user")) + } else if (!strcmp(s, "user")) { ; - else if (!strcmp(s, "uname")) + } else if (!strcmp(s, "uname")) { ; - else if (!strcmp(s, "name")) + } else if (!strcmp(s, "name")) { ; - else if (!strcmp(s, "username")) + } else if (!strcmp(s, "username")) { ; - else if (!strcmp(s, "password")) + } else if (!strcmp(s, "password")) { co->password = strdup(seq + 1); - else if (!strcmp(s, "pwd")) + } else if (!strcmp(s, "pwd")) { co->password = strdup(seq + 1); - else if (!strcmp(s, "passwd")) + } else if (!strcmp(s, "passwd")) { co->password = strdup(seq + 1); - else if (!strcmp(s, "secret")) + } else if (!strcmp(s, "secret")) { co->password = strdup(seq + 1); - else if (!strcmp(s, "port")) + } else if (!strcmp(s, "port")) { co->port = (unsigned int)atoi(seq + 1); - else if (!strcmp(s, "p")) + } else if (!strcmp(s, "p")) { co->port = (unsigned int)atoi(seq + 1); - else if (!strcmp(s, "connect_timeout")) + } else if (!strcmp(s, "connect_timeout")) { co->connect_timeout = (unsigned int)atoi(seq + 1); - else if (!strcmp(s, "timeout")) + } else if (!strcmp(s, "timeout")) { co->connect_timeout = (unsigned int)atoi(seq + 1); - else { + } else { RyconninfoFree(co); co = NULL; if (errmsg) { @@ -153,12 +157,15 @@ static Ryconninfo *RyconninfoParse(const char *userdb, char **errmsg) { } if (co) { - if (!(co->dbname)) + if (!(co->dbname)) { co->dbname = strdup("0"); - if (!(co->host)) + } + if (!(co->host)) { co->host = strdup("127.0.0.1"); - if (!(co->password)) + } + if (!(co->password)) { co->password = strdup(""); + } } return co; @@ -195,13 +202,16 @@ redis_context_handle get_redis_async_connection(struct event_base *base, redis_s char ip[256] = "\0"; int port = DEFAULT_REDIS_PORT; - if (co->host) + if (co->host) { STRCPY(ip, co->host); - if (!ip[0]) + } + if (!ip[0]) { strncpy(ip, "127.0.0.1", sizeof(ip)); + } - if (co->port) + if (co->port) { port = (int)(co->port); + } if (co->connect_timeout) { struct timeval tv; @@ -233,8 +243,9 @@ redis_context_handle get_redis_async_connection(struct event_base *base, redis_s if (reply->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str); } else if (reply->type != REDIS_REPLY_ARRAY) { - if (reply->type != REDIS_REPLY_NIL) + if (reply->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type); + } } else { size_t i; for (i = 0; i < reply->elements; ++i) { @@ -310,13 +321,16 @@ static redisContext *get_redis_connection(void) { } else { char ip[256] = "\0"; int port = DEFAULT_REDIS_PORT; - if (co->host) + if (co->host) { STRCPY(ip, co->host); - if (!ip[0]) + } + if (!ip[0]) { strncpy(ip, "127.0.0.1", sizeof(ip)); + } - if (co->port) + if (co->port) { port = (int)(co->port); + } if (co->connect_timeout) { struct timeval tv; @@ -391,11 +405,12 @@ static int set_redis_realm_opt(char *realm, const char *key, unsigned long *valu rget = (redisReply *)redisCommand(rc, s); if (rget) { - if (rget->type == REDIS_REPLY_ERROR) + if (rget->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", rget->str); - else if (rget->type != REDIS_REPLY_STRING) { - if (rget->type != REDIS_REPLY_NIL) + } else if (rget->type != REDIS_REPLY_STRING) { + if (rget->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", rget->type); + } } else { lock_realms(); *value = (unsigned long)atol(rget->str); @@ -418,11 +433,12 @@ static int redis_get_auth_secrets(secrets_list_t *sl, uint8_t *realm) { redisReply *reply = (redisReply *)redisCommand(rc, "smembers turn/realm/%s/secret", (char *)realm); if (reply) { - if (reply->type == REDIS_REPLY_ERROR) + if (reply->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str); - else if (reply->type != REDIS_REPLY_ARRAY) { - if (reply->type != REDIS_REPLY_NIL) + } else if (reply->type != REDIS_REPLY_ARRAY) { + if (reply->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type); + } } else { size_t i; for (i = 0; i < reply->elements; ++i) { @@ -446,11 +462,12 @@ static int redis_get_user_key(uint8_t *usname, uint8_t *realm, hmackey_t key) { snprintf(s, sizeof(s), "get turn/realm/%s/user/%s/key", (char *)realm, usname); redisReply *rget = (redisReply *)redisCommand(rc, s); if (rget) { - if (rget->type == REDIS_REPLY_ERROR) + if (rget->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", rget->str); - else if (rget->type != REDIS_REPLY_STRING) { - if (rget->type != REDIS_REPLY_NIL) + } else if (rget->type != REDIS_REPLY_STRING) { + if (rget->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", rget->type); + } } else { size_t sz = get_hmackey_size(SHATYPE_DEFAULT); if (strlen(rget->str) < sz * 2) { @@ -477,11 +494,12 @@ static int redis_get_oauth_key(const uint8_t *kid, oauth_key_data_raw *key) { snprintf(s, sizeof(s), "hgetall turn/oauth/kid/%s", (const char *)kid); redisReply *reply = (redisReply *)redisCommand(rc, s); if (reply) { - if (reply->type == REDIS_REPLY_ERROR) + if (reply->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str); - else if (reply->type != REDIS_REPLY_ARRAY) { - if (reply->type != REDIS_REPLY_NIL) + } else if (reply->type != REDIS_REPLY_ARRAY) { + if (reply->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type); + } } else if (reply->elements > 1) { size_t i; for (i = 0; i < (reply->elements) / 2; ++i) { @@ -572,8 +590,9 @@ static int redis_list_users(uint8_t *realm, secrets_list_t *users, secrets_list_ redisContext *rc = get_redis_connection(); uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } if (rc) { secrets_list_t keys; @@ -592,11 +611,12 @@ static int redis_list_users(uint8_t *realm, secrets_list_t *users, secrets_list_ if (reply) { - if (reply->type == REDIS_REPLY_ERROR) + if (reply->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str); - else if (reply->type != REDIS_REPLY_ARRAY) { - if (reply->type != REDIS_REPLY_NIL) + } else if (reply->type != REDIS_REPLY_ARRAY) { + if (reply->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type); + } } else { size_t i; for (i = 0; i < reply->elements; ++i) { @@ -614,23 +634,27 @@ static int redis_list_users(uint8_t *realm, secrets_list_t *users, secrets_list_ char *s = keys.secrets[isz]; char *sh = strstr(s, "turn/realm/"); - if (sh != s) + if (sh != s) { continue; + } sh += rhsz; char *st = strchr(sh, '/'); - if (!st) + if (!st) { continue; + } *st = 0; char *sr = sh; ++st; sh = strstr(st, "user/"); - if (sh != st) + if (sh != st) { continue; + } sh += uhsz; st = strchr(sh, '/'); - if (!st) + if (!st) { continue; + } *st = 0; char *su = sh; @@ -718,8 +742,9 @@ static int redis_list_secrets(uint8_t *realm, secrets_list_t *secrets, secrets_l int ret = -1; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } donot_print_connection_success = 1; redisContext *rc = get_redis_connection(); @@ -737,11 +762,12 @@ static int redis_list_secrets(uint8_t *realm, secrets_list_t *secrets, secrets_l init_secrets_list(&keys); - if (reply->type == REDIS_REPLY_ERROR) + if (reply->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str); - else if (reply->type != REDIS_REPLY_ARRAY) { - if (reply->type != REDIS_REPLY_NIL) + } else if (reply->type != REDIS_REPLY_ARRAY) { + if (reply->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type); + } } else { size_t i; for (i = 0; i < reply->elements; ++i) { @@ -760,19 +786,22 @@ static int redis_list_secrets(uint8_t *realm, secrets_list_t *secrets, secrets_l } else if (rget->type == REDIS_REPLY_STRING) { printf("%s\n", rget->str); } else if (rget->type != REDIS_REPLY_ARRAY) { - if (rget->type != REDIS_REPLY_NIL) + if (rget->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", rget->type); + } } else { char *s = keys.secrets[isz]; char *sh = strstr(s, "turn/realm/"); - if (sh != s) + if (sh != s) { continue; + } sh += rhsz; char *st = strchr(sh, '/'); - if (!st) + if (!st) { continue; + } *st = 0; const char *rval = sh; @@ -840,8 +869,9 @@ static int redis_set_permission_ip(const char *kind, uint8_t *realm, const char int ret = -1; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } donot_print_connection_success = 1; @@ -896,8 +926,9 @@ static int redis_list_origins(uint8_t *realm, secrets_list_t *origins, secrets_l int ret = -1; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } donot_print_connection_success = 1; @@ -914,11 +945,12 @@ static int redis_list_origins(uint8_t *realm, secrets_list_t *origins, secrets_l reply = (redisReply *)redisCommand(rc, "keys turn/origin/*"); if (reply) { - if (reply->type == REDIS_REPLY_ERROR) + if (reply->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str); - else if (reply->type != REDIS_REPLY_ARRAY) { - if (reply->type != REDIS_REPLY_NIL) + } else if (reply->type != REDIS_REPLY_ARRAY) { + if (reply->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type); + } } else { size_t i; size_t offset = strlen("turn/origin/"); @@ -937,11 +969,12 @@ static int redis_list_origins(uint8_t *realm, secrets_list_t *origins, secrets_l reply = (redisReply *)redisCommand(rc, "get turn/origin/%s", o); if (reply) { - if (reply->type == REDIS_REPLY_ERROR) + if (reply->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str); - else if (reply->type != REDIS_REPLY_STRING) { - if (reply->type != REDIS_REPLY_NIL) + } else if (reply->type != REDIS_REPLY_STRING) { + if (reply->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type); + } } else { if (!(realm && realm[0] && strcmp((char *)realm, reply->str))) { if (origins) { @@ -970,10 +1003,11 @@ static int redis_set_realm_option_one(uint8_t *realm, unsigned long value, const if (rc) { char s[TURN_LONG_STRING_SIZE]; - if (value > 0) + if (value > 0) { snprintf(s, sizeof(s), "set turn/realm/%s/%s %lu", (char *)realm, opt, (unsigned long)value); - else + } else { snprintf(s, sizeof(s), "del turn/realm/%s/%s", (char *)realm, opt); + } turnFreeRedisReply(redisCommand(rc, s)); turnFreeRedisReply(redisCommand(rc, "save")); @@ -1002,11 +1036,12 @@ static int redis_list_realm_options(uint8_t *realm) { } if (reply) { - if (reply->type == REDIS_REPLY_ERROR) + if (reply->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str); - else if (reply->type != REDIS_REPLY_ARRAY) { - if (reply->type != REDIS_REPLY_NIL) + } else if (reply->type != REDIS_REPLY_ARRAY) { + if (reply->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type); + } } else { size_t i; for (i = 0; i < reply->elements; ++i) { @@ -1028,11 +1063,12 @@ static int redis_list_realm_options(uint8_t *realm) { reply = (redisReply *)redisCommand(rc, "get %s", o); if (reply) { - if (reply->type == REDIS_REPLY_ERROR) + if (reply->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str); - else if (reply->type != REDIS_REPLY_STRING) { - if (reply->type != REDIS_REPLY_NIL) + } else if (reply->type != REDIS_REPLY_STRING) { + if (reply->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type); + } } else { printf("%s = %s\n", o + offset, reply->str); } @@ -1070,11 +1106,12 @@ static int redis_get_ip_list(const char *kind, ip_range_list_t *list) { init_secrets_list(&keys); - if (reply->type == REDIS_REPLY_ERROR) + if (reply->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str); - else if (reply->type != REDIS_REPLY_ARRAY) { - if (reply->type != REDIS_REPLY_NIL) + } else if (reply->type != REDIS_REPLY_ARRAY) { + if (reply->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type); + } } else { size_t i; for (i = 0; i < reply->elements; ++i) { @@ -1103,8 +1140,9 @@ static int redis_get_ip_list(const char *kind, ip_range_list_t *list) { } else if (rget->type == REDIS_REPLY_STRING) { add_ip_list_range(rget->str, realm, list); } else if (rget->type != REDIS_REPLY_ARRAY) { - if (rget->type != REDIS_REPLY_NIL) + if (rget->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", rget->type); + } } else { size_t i; for (i = 0; i < rget->elements; ++i) { @@ -1145,11 +1183,12 @@ static void redis_reread_realms(secrets_list_t *realms_list) { char s[1025]; - if (reply->type == REDIS_REPLY_ERROR) + if (reply->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str); - else if (reply->type != REDIS_REPLY_ARRAY) { - if (reply->type != REDIS_REPLY_NIL) + } else if (reply->type != REDIS_REPLY_ARRAY) { + if (reply->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type); + } } else { size_t i; for (i = 0; i < reply->elements; ++i) { @@ -1164,11 +1203,12 @@ static void redis_reread_realms(secrets_list_t *realms_list) { snprintf(s, sizeof(s), "get %s", keys.secrets[isz]); redisReply *rget = (redisReply *)redisCommand(rc, s); if (rget) { - if (rget->type == REDIS_REPLY_ERROR) + if (rget->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", rget->str); - else if (rget->type != REDIS_REPLY_STRING) { - if (rget->type != REDIS_REPLY_NIL) + } else if (rget->type != REDIS_REPLY_STRING) { + if (rget->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", rget->type); + } } else { get_realm(rget->str); ur_string_map_value_type value = strdup(rget->str); @@ -1243,11 +1283,12 @@ static int redis_get_admin_user(const uint8_t *usname, uint8_t *realm, password_ snprintf(s, sizeof(s), "hgetall turn/admin_user/%s", (const char *)usname); redisReply *reply = (redisReply *)redisCommand(rc, s); if (reply) { - if (reply->type == REDIS_REPLY_ERROR) + if (reply->type == REDIS_REPLY_ERROR) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error: %s\n", reply->str); - else if (reply->type != REDIS_REPLY_ARRAY) { - if (reply->type != REDIS_REPLY_NIL) + } else if (reply->type != REDIS_REPLY_ARRAY) { + if (reply->type != REDIS_REPLY_NIL) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unexpected type: %d\n", reply->type); + } } else if (reply->elements > 1) { size_t i; for (i = 0; i < (reply->elements) / 2; ++i) { diff --git a/src/apps/relay/dbdrivers/dbd_sqlite.c b/src/apps/relay/dbdrivers/dbd_sqlite.c index e76e873..23592b7 100644 --- a/src/apps/relay/dbdrivers/dbd_sqlite.c +++ b/src/apps/relay/dbdrivers/dbd_sqlite.c @@ -147,8 +147,9 @@ static int donot_print_connection_success = 0; static void fix_user_directory(char *dir0) { char *dir = dir0; - while (*dir == ' ') + while (*dir == ' ') { ++dir; + } #if defined(__unix__) || defined(unix) || defined(__APPLE__) if (*dir == '~') { char *home = getenv("HOME"); @@ -268,8 +269,9 @@ static int sqlite_get_auth_secrets(secrets_list_t *sl, uint8_t *realm) { if (res == SQLITE_ROW) { int type = sqlite3_column_type(st, 0); - if (type != SQLITE_NULL) + if (type != SQLITE_NULL) { add_to_secrets_list(sl, (const char *)sqlite3_column_text(st, 0)); + } } else if (res == SQLITE_DONE) { break; @@ -574,8 +576,9 @@ static int sqlite_list_users(uint8_t *realm, secrets_list_t *users, secrets_list int rc = 0; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } donot_print_connection_success = 1; @@ -638,8 +641,9 @@ static int sqlite_list_secrets(uint8_t *realm, secrets_list_t *secrets, secrets_ char statement[TURN_LONG_STRING_SIZE]; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } sqlite3_stmt *st = NULL; int rc = 0; @@ -707,10 +711,11 @@ static int sqlite_del_secret(uint8_t *secret, uint8_t *realm) { sqlite3 *sqliteconnection = get_sqlite_connection(); if (sqliteconnection) { - if (!secret || (secret[0] == 0)) + if (!secret || (secret[0] == 0)) { snprintf(statement, sizeof(statement), "delete from turn_secret where realm='%s'", realm); - else + } else { snprintf(statement, sizeof(statement), "delete from turn_secret where value='%s' and realm='%s'", secret, realm); + } sqlite_lock(1); @@ -816,8 +821,9 @@ static int sqlite_list_origins(uint8_t *realm, secrets_list_t *origins, secrets_ int ret = -1; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } donot_print_connection_success = 1; @@ -1012,8 +1018,9 @@ static int sqlite_set_permission_ip(const char *kind, uint8_t *realm, const char int ret = -1; uint8_t realm0[STUN_MAX_REALM_SIZE + 1] = "\0"; - if (!realm) + if (!realm) { realm = realm0; + } char statement[TURN_LONG_STRING_SIZE]; @@ -1140,13 +1147,13 @@ static void sqlite_reread_realms(secrets_list_t *realms_list) { const char *vval = (const char *)sqlite3_column_text(st, 2); realm_params_t *rp = get_realm(rval); - if (!strcmp(oval, "max-bps")) + if (!strcmp(oval, "max-bps")) { rp->options.perf_options.max_bps = (band_limit_t)strtoul(vval, NULL, 10); - else if (!strcmp(oval, "total-quota")) + } else if (!strcmp(oval, "total-quota")) { rp->options.perf_options.total_quota = (vint)atoi(vval); - else if (!strcmp(oval, "user-quota")) + } else if (!strcmp(oval, "user-quota")) { rp->options.perf_options.user_quota = (vint)atoi(vval); - else { + } else { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unknown realm option: %s\n", oval); } diff --git a/src/apps/relay/dbdrivers/dbdriver.c b/src/apps/relay/dbdrivers/dbdriver.c index 5c567fe..7d7a156 100644 --- a/src/apps/relay/dbdrivers/dbdriver.c +++ b/src/apps/relay/dbdrivers/dbdriver.c @@ -62,8 +62,9 @@ int convert_string_key_to_binary(char *keysource, hmackey_t key, size_t sz) { persistent_users_db_t *get_persistent_users_db(void) { return &(turn_params.default_users_db.persistent_users_db); } const turn_dbdriver_t *get_dbdriver(void) { - if (turn_params.default_users_db.userdb_type == TURN_USERDB_TYPE_UNKNOWN) + if (turn_params.default_users_db.userdb_type == TURN_USERDB_TYPE_UNKNOWN) { return NULL; + } (void)pthread_once(&connection_key_once, make_connection_key); diff --git a/src/apps/relay/dtls_listener.c b/src/apps/relay/dtls_listener.c index 5a9005b..2435fe2 100644 --- a/src/apps/relay/dtls_listener.c +++ b/src/apps/relay/dtls_listener.c @@ -120,8 +120,9 @@ int is_dtls_message(const unsigned char *buf, int len) { /* 0 - 1.0, 1 - 1.2 */ int get_dtls_version(const unsigned char *buf, int len) { - if (buf && (len > 3) && (buf[2] == 0xfd)) + if (buf && (len > 3) && (buf[2] == 0xfd)) { return 1; + } return 0; } @@ -134,8 +135,9 @@ static void calculate_cookie(SSL *ssl, unsigned char *cookie_secret, unsigned in long inum = (cookie_length - (((long)cookie_secret) % sizeof(long))) / sizeof(long); long i = 0; long *ip = (long *)cookie_secret; - for (i = 0; i < inum; ++i, ++ip) + for (i = 0; i < inum; ++i, ++ip) { *ip = rv; + } } static int generate_cookie(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len) { @@ -226,13 +228,15 @@ static ioa_socket_handle dtls_accept_client_connection(dtls_listener_relay_serve ioa_network_buffer_handle nbh) { FUNCSTART; - if (!ssl) + if (!ssl) { return NULL; + } int rc = ssl_read(sock->fd, ssl, nbh, server->verbose); - if (rc < 0) + if (rc < 0) { return NULL; + } addr_debug_print(server->verbose, remote_addr, "Accepted connection from"); @@ -730,8 +734,9 @@ start_udp_cycle: ioa_network_buffer_delete(server->e, server->sm.m.sm.nd.nbh); server->sm.m.sm.nd.nbh = NULL; - if ((bsize > 0) && (cycle++ < MAX_SINGLE_UDP_BATCH)) + if ((bsize > 0) && (cycle++ < MAX_SINGLE_UDP_BATCH)) { goto start_udp_cycle; + } FUNCEND; } @@ -742,8 +747,9 @@ static int create_server_socket(dtls_listener_relay_server_type *server, int rep FUNCSTART; - if (!server) + if (!server) { return -1; + } clean_server(server); @@ -796,12 +802,13 @@ static int create_server_socket(dtls_listener_relay_server_type *server, int rep } if (report_creation) { - if (!turn_params.no_udp && !turn_params.no_dtls) + if (!turn_params.no_udp && !turn_params.no_dtls) { addr_debug_print(server->verbose, &server->addr, "DTLS/UDP listener opened on"); - else if (!turn_params.no_dtls) + } else if (!turn_params.no_dtls) { addr_debug_print(server->verbose, &server->addr, "DTLS listener opened on"); - else if (!turn_params.no_udp) + } else if (!turn_params.no_udp) { addr_debug_print(server->verbose, &server->addr, "UDP listener opened on"); + } } FUNCEND; @@ -812,8 +819,9 @@ static int create_server_socket(dtls_listener_relay_server_type *server, int rep static int reopen_server_socket(dtls_listener_relay_server_type *server, evutil_socket_t fd) { UNUSED_ARG(fd); - if (!server) + if (!server) { return 0; + } FUNCSTART; @@ -863,12 +871,13 @@ static int reopen_server_socket(dtls_listener_relay_server_type *server, evutil_ event_add(server->udp_listen_ev, NULL); } - if (!turn_params.no_udp && !turn_params.no_dtls) + if (!turn_params.no_udp && !turn_params.no_dtls) { addr_debug_print(server->verbose, &server->addr, "DTLS/UDP listener opened on "); - else if (!turn_params.no_dtls) + } else if (!turn_params.no_dtls) { addr_debug_print(server->verbose, &server->addr, "DTLS listener opened on "); - else if (!turn_params.no_udp) + } else if (!turn_params.no_udp) { addr_debug_print(server->verbose, &server->addr, "UDP listener opened on "); + } FUNCEND; @@ -882,8 +891,9 @@ static int dtls_verify_callback(int ok, X509_STORE_CTX *ctx) { * if he trusts the received certificate. * Here we always trust. */ - if (ok && ctx) + if (ok && ctx) { return 1; + } return -1; } @@ -893,14 +903,16 @@ static int init_server(dtls_listener_relay_server_type *server, const char *ifna int verbose, ioa_engine_handle e, turn_turnserver *ts, int report_creation, ioa_engine_new_connection_event_handler send_socket) { - if (!server) + if (!server) { return -1; + } server->ts = ts; server->connect_cb = send_socket; - if (ifname) + if (ifname) { STRCPY(server->ifname, ifname); + } if (make_ioa_addr((const uint8_t *)local_address, port, &server->addr) < 0) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot create a DTLS/UDP listener for address: %s\n", local_address); @@ -929,8 +941,9 @@ static int clean_server(dtls_listener_relay_server_type *server) { #if DTLS_SUPPORTED void setup_dtls_callbacks(SSL_CTX *ctx) { - if (!ctx) + if (!ctx) { return; + } #if defined(REQUEST_CLIENT_CERT) /* If client has to authenticate, then */ @@ -958,16 +971,18 @@ dtls_listener_relay_server_type *create_dtls_listener_server(const char *ifname, } ioa_engine_handle get_engine(dtls_listener_relay_server_type *server) { - if (server) + if (server) { return server->e; + } return NULL; } //////////// UDP send //////////////// void udp_send_message(dtls_listener_relay_server_type *server, ioa_network_buffer_handle nbh, ioa_addr *dest) { - if (server && dest && nbh && (server->udp_listen_s)) + if (server && dest && nbh && (server->udp_listen_s)) { udp_send(server->udp_listen_s, dest, (char *)ioa_network_buffer_data(nbh), (int)ioa_network_buffer_get_size(nbh)); + } } ////////////////////////////////////////////////////////////////// diff --git a/src/apps/relay/http_server.c b/src/apps/relay/http_server.c index 2ba9f21..b921a7c 100644 --- a/src/apps/relay/http_server.c +++ b/src/apps/relay/http_server.c @@ -119,9 +119,9 @@ static struct headers_list *post_parse(char *data, size_t data_len) { while (fsplit != NULL) { char *vmarker = NULL; char *key = strtok_r(fsplit, "=", &vmarker); - if (key == NULL) + if (key == NULL) { break; - else { + } else { char *value = strtok_r(NULL, "=", &vmarker); char empty[1]; empty[0] = 0; @@ -129,8 +129,9 @@ static struct headers_list *post_parse(char *data, size_t data_len) { value = evhttp_decode_uri(value); char *p = value; while (*p) { - if (*p == '+') + if (*p == '+') { *p = ' '; + } p++; } list->keys = (char **)realloc(list->keys, sizeof(char *) * (list->n + 1)); @@ -178,8 +179,9 @@ static struct http_request *parse_http_request_1(struct http_request *ret, char } const char *path = evhttp_uri_get_path(uri); - if (path && ret) + if (path && ret) { ret->path = strdup(path); + } evhttp_uri_free(uri); diff --git a/src/apps/relay/libtelnet.c b/src/apps/relay/libtelnet.c index 9caccf8..875aea9 100644 --- a/src/apps/relay/libtelnet.c +++ b/src/apps/relay/libtelnet.c @@ -170,12 +170,14 @@ telnet_error_t _init_zlib(telnet_t *telnet, int deflate, int err_fatal) { int rs; /* if compression is already enabled, fail loudly */ - if (telnet->z != 0) + if (telnet->z != 0) { return telnet_error(telnet, __LINE__, __func__, TELNET_EBADVAL, err_fatal, "cannot initialize compression twice"); + } /* allocate zstream box */ - if ((z = (z_stream *)calloc(1, sizeof(z_stream))) == 0) + if ((z = (z_stream *)calloc(1, sizeof(z_stream))) == 0) { return telnet_error(telnet, __LINE__, __func__, TELNET_ENOMEM, err_fatal, "malloc() failed: %s", strerror(errno)); + } /* initialize */ if (deflate) { @@ -260,18 +262,20 @@ static INLINE int _check_telopt(telnet_t *telnet, unsigned char telopt, int us) int i; /* if we have no telopts table, we obviously don't support it */ - if (telnet->telopts == 0) + if (telnet->telopts == 0) { return 0; + } /* loop until found or end marker (us and him both 0) */ for (i = 0; telnet->telopts[i].telopt != -1; ++i) { if (telnet->telopts[i].telopt == telopt) { - if (us && telnet->telopts[i].us == TELNET_WILL) + if (us && telnet->telopts[i].us == TELNET_WILL) { return 1; - else if (!us && telnet->telopts[i].him == TELNET_DO) + } else if (!us && telnet->telopts[i].him == TELNET_DO) { return 1; - else + } else { return 0; + } } } @@ -306,13 +310,16 @@ static INLINE void _set_rfc1143(telnet_t *telnet, unsigned char telopt, char us, for (i = 0; i != telnet->q_cnt; ++i) { if (telnet->q[i].telopt == telopt) { telnet->q[i].state = Q_MAKE(us, him); - if (telopt != TELNET_TELOPT_BINARY) + if (telopt != TELNET_TELOPT_BINARY) { return; + } telnet->flags &= ~(TELNET_FLAG_TRANSMIT_BINARY | TELNET_FLAG_RECEIVE_BINARY); - if (us == Q_YES) + if (us == Q_YES) { telnet->flags |= TELNET_FLAG_TRANSMIT_BINARY; - if (him == Q_YES) + } + if (him == Q_YES) { telnet->flags |= TELNET_FLAG_RECEIVE_BINARY; + } return; } } @@ -388,8 +395,9 @@ static void _negotiate(telnet_t *telnet, unsigned char telopt) { _set_rfc1143(telnet, telopt, Q_US(q), Q_YES); _send_negotiate(telnet, TELNET_DO, telopt); NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt); - } else + } else { _send_negotiate(telnet, TELNET_DONT, telopt); + } break; case Q_WANTNO: _set_rfc1143(telnet, telopt, Q_US(q), Q_NO); @@ -444,8 +452,9 @@ static void _negotiate(telnet_t *telnet, unsigned char telopt) { _set_rfc1143(telnet, telopt, Q_YES, Q_HIM(q)); _send_negotiate(telnet, TELNET_WILL, telopt); NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt); - } else + } else { _send_negotiate(telnet, TELNET_WONT, telopt); + } break; case Q_WANTNO: _set_rfc1143(telnet, telopt, Q_NO, Q_HIM(q)); @@ -726,8 +735,9 @@ static int _zmp_telnet(telnet_t *telnet, const char *buffer, size_t size) { } /* count arguments */ - for (argc = 0, c = buffer; c != buffer + size; ++argc) + for (argc = 0, c = buffer; c != buffer + size; ++argc) { c += strlen(c) + 1; + } /* allocate argument array, bail on error */ if ((argv = (const char **)calloc(argc, sizeof(const char *))) == 0) { @@ -817,8 +827,9 @@ static int _subnegotiate(telnet_t *telnet) { */ case TELNET_TELOPT_COMPRESS2: if (telnet->sb_telopt == TELNET_TELOPT_COMPRESS2) { - if (_init_zlib(telnet, 0, 1) != TELNET_EOK) + if (_init_zlib(telnet, 0, 1) != TELNET_EOK) { return 0; + } /* notify app that compression was enabled */ ev.type = TELNET_EV_COMPRESS; @@ -848,8 +859,9 @@ static int _subnegotiate(telnet_t *telnet) { telnet_t *telnet_init(const telnet_telopt_t *telopts, telnet_event_handler_t eh, unsigned char flags, void *user_data) { /* allocate structure */ struct telnet_t *telnet = (telnet_t *)calloc(1, sizeof(telnet_t)); - if (telnet == 0) + if (telnet == 0) { return 0; + } /* initialize data */ telnet->ud = user_data; @@ -873,10 +885,11 @@ void telnet_free(telnet_t *telnet) { #if defined(HAVE_ZLIB) /* free zlib box */ if (telnet->z != 0) { - if (telnet->flags & TELNET_PFLAG_DEFLATE) + if (telnet->flags & TELNET_PFLAG_DEFLATE) { deflateEnd(telnet->z); - else + } else { inflateEnd(telnet->z); + } free(telnet->z); telnet->z = 0; } @@ -974,8 +987,9 @@ static void _process(telnet_t *telnet, const char *buffer, size_t size) { // any byte following '\r' other than '\n' or '\0' is invalid, // so pass both \r and the byte start = i; - if (byte == '\0') + if (byte == '\0') { ++start; + } /* state update */ telnet->state = TELNET_STATE_DATA; break; @@ -1155,10 +1169,11 @@ void telnet_recv(telnet_t *telnet, const char *buffer, size_t size) { rs = inflate(telnet->z, Z_SYNC_FLUSH); /* process the decompressed bytes on success */ - if (rs == Z_OK || rs == Z_STREAM_END) + if (rs == Z_OK || rs == Z_STREAM_END) { _process(telnet, inflate_buffer, sizeof(inflate_buffer) - telnet->z->avail_out); - else + } else { telnet_error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1, "inflate() failed: %s", zError(rs)); + } /* prepare output buffer for next run */ telnet->z->next_out = (unsigned char *)inflate_buffer; @@ -1374,8 +1389,9 @@ void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt, const char *b if (telnet->flags & TELNET_FLAG_PROXY && telopt == TELNET_TELOPT_COMPRESS2) { telnet_event_t ev; - if (_init_zlib(telnet, 1, 1) != TELNET_EOK) + if (_init_zlib(telnet, 1, 1) != TELNET_EOK) { return; + } /* notify app that compression was enabled */ ev.type = TELNET_EV_COMPRESS; @@ -1392,8 +1408,9 @@ void telnet_begin_compress2(telnet_t *telnet) { telnet_event_t ev; /* attempt to create output stream first, bail if we can't */ - if (_init_zlib(telnet, 1, 0) != TELNET_EOK) + if (_init_zlib(telnet, 1, 0) != TELNET_EOK) { return; + } /* send compression marker. we send directly to the event handler * instead of passing through _send because _send would result in @@ -1439,19 +1456,23 @@ int telnet_vprintf(telnet_t *telnet, const char *fmt, va_list va) { /* special characters */ if (output[i] == (char)TELNET_IAC || output[i] == '\r' || output[i] == '\n') { /* dump prior portion of text */ - if (i != l) + if (i != l) { _send(telnet, output + l, i - l); + } l = i + 1; /* IAC -> IAC IAC */ - if (output[i] == (char)TELNET_IAC) + if (output[i] == (char)TELNET_IAC) { telnet_iac(telnet, TELNET_IAC); + } /* automatic translation of \r -> CRNUL */ - else if (output[i] == '\r') + else if (output[i] == '\r') { _send(telnet, CRNUL, 2); + } /* automatic translation of \n -> CRLF */ - else if (output[i] == '\n') + else if (output[i] == '\n') { _send(telnet, CRLF, 2); + } } } @@ -1562,8 +1583,9 @@ void telnet_send_zmp(telnet_t *telnet, size_t argc, const char **argv) { telnet_begin_zmp(telnet, argv[0]); /* send out each argument, including trailing NUL byte */ - for (i = 1; i != argc; ++i) + for (i = 1; i != argc; ++i) { telnet_zmp_arg(telnet, argv[i]); + } /* ZMP footer */ telnet_finish_zmp(telnet); @@ -1577,8 +1599,9 @@ void telnet_send_vzmpv(telnet_t *telnet, va_list va) { telnet_begin_sb(telnet, TELNET_TELOPT_ZMP); /* send out each argument, including trailing NUL byte */ - while ((arg = va_arg(va, const char *)) != 0) + while ((arg = va_arg(va, const char *)) != 0) { telnet_zmp_arg(telnet, arg); + } /* ZMP footer */ telnet_finish_zmp(telnet); diff --git a/src/apps/relay/mainrelay.c b/src/apps/relay/mainrelay.c index 269d64c..9cda426 100644 --- a/src/apps/relay/mainrelay.c +++ b/src/apps/relay/mainrelay.c @@ -334,30 +334,38 @@ static int make_local_listeners_list(void) { if (AF_INET == pUnicast->Address.lpSockaddr->sa_family) // IPV4 { if (!inet_ntop(PF_INET, &((struct sockaddr_in *)pUnicast->Address.lpSockaddr)->sin_addr, saddr, - INET6_ADDRSTRLEN)) + INET6_ADDRSTRLEN)) { continue; - if (strstr(saddr, "169.254.") == saddr) + } + if (strstr(saddr, "169.254.") == saddr) { continue; - if (!strcmp(saddr, "0.0.0.0")) + } + if (!strcmp(saddr, "0.0.0.0")) { continue; + } } else if (AF_INET6 == pUnicast->Address.lpSockaddr->sa_family) // IPV6 { if (!inet_ntop(PF_INET6, &((struct sockaddr_in6 *)pUnicast->Address.lpSockaddr)->sin6_addr, saddr, - INET6_ADDRSTRLEN)) + INET6_ADDRSTRLEN)) { continue; - if (strstr(saddr, "fe80") == saddr) + } + if (strstr(saddr, "fe80") == saddr) { continue; - if (!strcmp(saddr, "::")) + } + if (!strcmp(saddr, "::")) { continue; - } else + } + } else { continue; + } // printf("\t\tIP: %s\n", saddr); add_listener_addr(saddr); - if (MIB_IF_TYPE_LOOPBACK != pCurrAddresses->IfType) + if (MIB_IF_TYPE_LOOPBACK != pCurrAddresses->IfType) { ret++; + } } } /* @@ -456,9 +464,9 @@ static int make_local_listeners_list(void) { } } else { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Call to GetAdaptersAddresses failed with error: %d\n", dwRetVal); - if (dwRetVal == ERROR_NO_DATA) + if (dwRetVal == ERROR_NO_DATA) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "\tNo addresses were found for the requested parameters\n"); - else { + } else { if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), @@ -466,8 +474,9 @@ static int make_local_listeners_list(void) { (LPTSTR)&lpMsgBuf, 0, NULL)) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "\tError: %s", lpMsgBuf); LocalFree(lpMsgBuf); - if (pAddresses) + if (pAddresses) { free(pAddresses); + } return -2; } } @@ -487,34 +496,43 @@ static int make_local_listeners_list(void) { for (ifa = ifs; ifa != NULL; ifa = ifa->ifa_next) { - if (!(ifa->ifa_flags & IFF_UP)) + if (!(ifa->ifa_flags & IFF_UP)) { continue; + } - if (!(ifa->ifa_addr)) + if (!(ifa->ifa_addr)) { continue; + } if (ifa->ifa_addr->sa_family == AF_INET) { - if (!inet_ntop(AF_INET, &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, saddr, INET_ADDRSTRLEN)) + if (!inet_ntop(AF_INET, &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, saddr, INET_ADDRSTRLEN)) { continue; - if (strstr(saddr, "169.254.") == saddr) + } + if (strstr(saddr, "169.254.") == saddr) { continue; - if (!strcmp(saddr, "0.0.0.0")) + } + if (!strcmp(saddr, "0.0.0.0")) { continue; + } } else if (ifa->ifa_addr->sa_family == AF_INET6) { - if (!inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr, saddr, INET6_ADDRSTRLEN)) + if (!inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr, saddr, INET6_ADDRSTRLEN)) { continue; - if (strstr(saddr, "fe80") == saddr) + } + if (strstr(saddr, "fe80") == saddr) { continue; - if (!strcmp(saddr, "::")) + } + if (!strcmp(saddr, "::")) { continue; + } } else { continue; } add_listener_addr(saddr); - if (!(ifa->ifa_flags & IFF_LOOPBACK)) + if (!(ifa->ifa_flags & IFF_LOOPBACK)) { ret++; + } } freeifaddrs(ifs); } @@ -581,35 +599,45 @@ static int make_local_relays_list(int allow_local, int family) { if (pUnicast != NULL) { // printf("\tNumber of Unicast Addresses:\n"); for (; pUnicast != NULL; pUnicast = pUnicast->Next) { - if (!allow_local && (MIB_IF_TYPE_LOOPBACK == pCurrAddresses->IfType)) + if (!allow_local && (MIB_IF_TYPE_LOOPBACK == pCurrAddresses->IfType)) { continue; + } char saddr[INET6_ADDRSTRLEN] = ""; if (AF_INET == pUnicast->Address.lpSockaddr->sa_family) // IPV4 { - if (family != AF_INET) + if (family != AF_INET) { continue; + } if (!inet_ntop(PF_INET, &((struct sockaddr_in *)pUnicast->Address.lpSockaddr)->sin_addr, saddr, - INET6_ADDRSTRLEN)) + INET6_ADDRSTRLEN)) { continue; - if (strstr(saddr, "169.254.") == saddr) + } + if (strstr(saddr, "169.254.") == saddr) { continue; - if (!strcmp(saddr, "0.0.0.0")) + } + if (!strcmp(saddr, "0.0.0.0")) { continue; + } } else if (AF_INET6 == pUnicast->Address.lpSockaddr->sa_family) // IPV6 { - if (family != AF_INET6) + if (family != AF_INET6) { continue; + } if (!inet_ntop(PF_INET6, &((struct sockaddr_in6 *)pUnicast->Address.lpSockaddr)->sin6_addr, saddr, - INET6_ADDRSTRLEN)) + INET6_ADDRSTRLEN)) { continue; - if (strstr(saddr, "fe80") == saddr) + } + if (strstr(saddr, "fe80") == saddr) { continue; - if (!strcmp(saddr, "::")) + } + if (!strcmp(saddr, "::")) { continue; - } else + } + } else { continue; + } if (add_relay_addr(saddr) > 0) { counter += 1; @@ -634,41 +662,54 @@ static int make_local_relays_list(int allow_local, int family) { if (ifs) { for (ifa = ifs; ifa != NULL; ifa = ifa->ifa_next) { - if (!(ifa->ifa_flags & IFF_UP)) + if (!(ifa->ifa_flags & IFF_UP)) { continue; + } - if (!(ifa->ifa_name)) + if (!(ifa->ifa_name)) { continue; - if (!(ifa->ifa_addr)) + } + if (!(ifa->ifa_addr)) { continue; + } - if (!allow_local && (ifa->ifa_flags & IFF_LOOPBACK)) + if (!allow_local && (ifa->ifa_flags & IFF_LOOPBACK)) { continue; + } if (ifa->ifa_addr->sa_family == AF_INET) { - if (family != AF_INET) + if (family != AF_INET) { continue; + } - if (!inet_ntop(AF_INET, &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, saddr, INET_ADDRSTRLEN)) + if (!inet_ntop(AF_INET, &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, saddr, INET_ADDRSTRLEN)) { continue; - if (strstr(saddr, "169.254.") == saddr) + } + if (strstr(saddr, "169.254.") == saddr) { continue; - if (!strcmp(saddr, "0.0.0.0")) + } + if (!strcmp(saddr, "0.0.0.0")) { continue; + } } else if (ifa->ifa_addr->sa_family == AF_INET6) { - if (family != AF_INET6) + if (family != AF_INET6) { continue; + } - if (!inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr, saddr, INET6_ADDRSTRLEN)) + if (!inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr, saddr, INET6_ADDRSTRLEN)) { continue; - if (strstr(saddr, "fe80") == saddr) + } + if (strstr(saddr, "fe80") == saddr) { continue; - if (!strcmp(saddr, "::")) + } + if (!strcmp(saddr, "::")) { continue; - } else + } + } else { continue; + } if (add_relay_addr(saddr) > 0) { counter += 1; @@ -734,35 +775,45 @@ int get_a_local_relay(int family, ioa_addr *relay_addr) { if (pUnicast != NULL) { // printf("\tNumber of Unicast Addresses:\n"); for (; pUnicast != NULL; pUnicast = pUnicast->Next) { - if (!allow_local && (MIB_IF_TYPE_LOOPBACK == pCurrAddresses->IfType)) + if (!allow_local && (MIB_IF_TYPE_LOOPBACK == pCurrAddresses->IfType)) { continue; + } char saddr[INET6_ADDRSTRLEN] = ""; if (AF_INET == pUnicast->Address.lpSockaddr->sa_family) // IPV4 { - if (family != AF_INET) + if (family != AF_INET) { continue; + } if (!inet_ntop(PF_INET, &((struct sockaddr_in *)pUnicast->Address.lpSockaddr)->sin_addr, saddr, - INET6_ADDRSTRLEN)) + INET6_ADDRSTRLEN)) { continue; - if (strstr(saddr, "169.254.") == saddr) + } + if (strstr(saddr, "169.254.") == saddr) { continue; - if (!strcmp(saddr, "0.0.0.0")) + } + if (!strcmp(saddr, "0.0.0.0")) { continue; + } } else if (AF_INET6 == pUnicast->Address.lpSockaddr->sa_family) // IPV6 { - if (family != AF_INET6) + if (family != AF_INET6) { continue; + } if (!inet_ntop(PF_INET6, &((struct sockaddr_in6 *)pUnicast->Address.lpSockaddr)->sin6_addr, saddr, - INET6_ADDRSTRLEN)) + INET6_ADDRSTRLEN)) { continue; - if (strstr(saddr, "fe80") == saddr) + } + if (strstr(saddr, "fe80") == saddr) { continue; - if (!strcmp(saddr, "::")) + } + if (!strcmp(saddr, "::")) { continue; - } else + } + } else { continue; + } if (make_ioa_addr((const uint8_t *)saddr, 0, relay_addr) < 0) { continue; @@ -793,48 +844,57 @@ int get_a_local_relay(int family, ioa_addr *relay_addr) { if (ifs) { - galr_start : + galr_start: + for (struct ifaddrs *ifa = ifs; ifa != NULL; ifa = ifa->ifa_next) { - { - struct ifaddrs *ifa = NULL; - - for (ifa = ifs; ifa != NULL; ifa = ifa->ifa_next) { - - if (!(ifa->ifa_flags & IFF_UP)) + if (!(ifa->ifa_flags & IFF_UP)) { continue; + } - if (!(ifa->ifa_name)) + if (!(ifa->ifa_name)) { continue; - if (!(ifa->ifa_addr)) + } + if (!(ifa->ifa_addr)) { continue; + } - if (!allow_local && (ifa->ifa_flags & IFF_LOOPBACK)) + if (!allow_local && (ifa->ifa_flags & IFF_LOOPBACK)) { continue; + } if (ifa->ifa_addr->sa_family == AF_INET) { - if (family != AF_INET) + if (family != AF_INET) { continue; + } - if (!inet_ntop(AF_INET, &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, saddr, INET_ADDRSTRLEN)) + if (!inet_ntop(AF_INET, &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, saddr, INET_ADDRSTRLEN)) { continue; - if (strstr(saddr, "169.254.") == saddr) + } + if (strstr(saddr, "169.254.") == saddr) { continue; - if (!strcmp(saddr, "0.0.0.0")) + } + if (!strcmp(saddr, "0.0.0.0")) { continue; + } } else if (ifa->ifa_addr->sa_family == AF_INET6) { - if (family != AF_INET6) + if (family != AF_INET6) { continue; + } - if (!inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr, saddr, INET6_ADDRSTRLEN)) + if (!inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr, saddr, INET6_ADDRSTRLEN)) { continue; - if (strstr(saddr, "fe80") == saddr) + } + if (strstr(saddr, "fe80") == saddr) { continue; - if (!strcmp(saddr, "::")) + } + if (!strcmp(saddr, "::")) { continue; - } else + } + } else { continue; + } if (make_ioa_addr((const uint8_t *)saddr, 0, relay_addr) < 0) { continue; @@ -843,7 +903,6 @@ int get_a_local_relay(int family, ioa_addr *relay_addr) { break; } } - } if (ret < 0 && !allow_local) { allow_local = 1; @@ -1761,24 +1820,31 @@ void decrypt_aes_128(char *in, const unsigned char *mykey) { } static int get_int_value(const char *s, int default_value) { - if (!s || !(s[0])) + if (!s || !(s[0])) { return default_value; + } return atoi(s); } static int get_bool_value(const char *s) { - if (!s || !(s[0])) + if (!s || !(s[0])) { return 1; - if (s[0] == '0' || s[0] == 'n' || s[0] == 'N' || s[0] == 'f' || s[0] == 'F') + } + if (s[0] == '0' || s[0] == 'n' || s[0] == 'N' || s[0] == 'f' || s[0] == 'F') { return 0; - if (s[0] == 'y' || s[0] == 'Y' || s[0] == 't' || s[0] == 'T') + } + if (s[0] == 'y' || s[0] == 'Y' || s[0] == 't' || s[0] == 'T') { return 1; - if (s[0] > '0' && s[0] <= '9') + } + if (s[0] > '0' && s[0] <= '9') { return 1; - if (!strcmp(s, "off") || !strcmp(s, "OFF") || !strcmp(s, "Off")) + } + if (!strcmp(s, "off") || !strcmp(s, "OFF") || !strcmp(s, "Off")) { return 0; - if (!strcmp(s, "on") || !strcmp(s, "ON") || !strcmp(s, "On")) + } + if (!strcmp(s, "on") || !strcmp(s, "ON") || !strcmp(s, "On")) { return 1; + } TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unknown boolean value: %s. You can use on/off, yes/no, 1/0, true/false.\n", s); exit(-1); } @@ -1794,13 +1860,15 @@ static void set_option(int c, char *value) { switch (c) { case 'K': - if (get_bool_value(value)) + if (get_bool_value(value)) { turn_params.allocation_default_address_family = ALLOCATION_DEFAULT_ADDRESS_FAMILY_KEEP; + } break; case 'A': if (value && strlen(value) > 0) { - if (*value == '=') + if (*value == '=') { ++value; + } if (!strcmp(value, "ipv6")) { turn_params.allocation_default_address_family = ALLOCATION_DEFAULT_ADDRESS_FAMILY_IPV6; } else if (!strcmp(value, "keep")) { @@ -1847,12 +1915,14 @@ static void set_option(int c, char *value) { turn_params.net_engine_version = (NET_ENG_VERSION)ne; } break; case DH566_OPT: - if (get_bool_value(value)) + if (get_bool_value(value)) { turn_params.dh_key_size = DH_566; + } break; case DH1066_OPT: - if (get_bool_value(value)) + if (get_bool_value(value)) { turn_params.dh_key_size = DH_1066; + } break; case EC_CURVE_NAME_OPT: STRCPY(turn_params.ec_curve_name, value); @@ -2015,8 +2085,9 @@ static void set_option(int c, char *value) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "-X : Wrong address format: %s\n", div); } else { ioa_addr_add_mapping(&apub, &apriv); - if (add_ip_list_range((const char *)div, NULL, &turn_params.ip_whitelist) == 0) + if (add_ip_list_range((const char *)div, NULL, &turn_params.ip_whitelist) == 0) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Whitelisting external-ip private part: %s\n", div); + } } } free(nval); @@ -2234,12 +2305,14 @@ static void set_option(int c, char *value) { add_tls_alternate_server(value); break; case ALLOWED_PEER_IPS: - if (add_ip_list_range(value, NULL, &turn_params.ip_whitelist) == 0) + if (add_ip_list_range(value, NULL, &turn_params.ip_whitelist) == 0) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "White listing: %s\n", value); + } break; case DENIED_PEER_IPS: - if (add_ip_list_range(value, NULL, &turn_params.ip_blacklist) == 0) + if (add_ip_list_range(value, NULL, &turn_params.ip_blacklist) == 0) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Black listing: %s\n", value); + } break; case CIPHER_LIST_OPT: STRCPY(turn_params.cipher_list, value); @@ -2375,8 +2448,9 @@ static void read_config_file(int argc, char **argv, int pass) { char *full_path_to_config_file = NULL; full_path_to_config_file = find_config_file(config_file, pass); - if (full_path_to_config_file) + if (full_path_to_config_file) { f = fopen(full_path_to_config_file, "r"); + } if (f) { @@ -2385,18 +2459,22 @@ static void read_config_file(int argc, char **argv, int pass) { for (;;) { char *s = fgets(sbuf, sizeof(sbuf) - 1, f); - if (!s) + if (!s) { break; + } s = skip_blanks(s); - if (s[0] == '#') + if (s[0] == '#') { continue; - if (!s[0]) + } + if (!s[0]) { continue; + } size_t slen = strlen(s); // strip white-spaces from config file lines end - while (slen && isspace(s[slen - 1])) + while (slen && isspace(s[slen - 1])) { s[--slen] = 0; + } if (slen) { int c = 0; char *value = NULL; @@ -2547,8 +2625,9 @@ static int adminmain(int argc, char **argv) { break; case 'X': ct = TA_DEL_SECRET; - if (optarg) + if (optarg) { STRCPY(secret, optarg); + } break; case DEL_ALL_AUTH_SECRETS_OPT: ct = TA_DEL_SECRET; @@ -2655,8 +2734,9 @@ static int adminmain(int argc, char **argv) { #if !defined(TURN_NO_SQLITE) if (!strlen(turn_params.default_users_db.persistent_users_db.userdb) && - (turn_params.default_users_db.userdb_type == TURN_USERDB_TYPE_SQLITE)) + (turn_params.default_users_db.userdb_type == TURN_USERDB_TYPE_SQLITE)) { strncpy(turn_params.default_users_db.persistent_users_db.userdb, DEFAULT_USERDB_FILE, TURN_LONG_STRING_SIZE); + } #endif if (ct == TA_COMMAND_UNKNOWN) { @@ -2682,13 +2762,15 @@ static int adminmain(int argc, char **argv) { static void print_features(unsigned long mfn) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Coturn Version %s\n", TURN_SOFTWARE); TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Max number of open files/sockets allowed for this process: %lu\n", mfn); - if (turn_params.net_engine_version == NEV_UDP_SOCKET_PER_ENDPOINT) + if (turn_params.net_engine_version == NEV_UDP_SOCKET_PER_ENDPOINT) { mfn = mfn / 3; - else + } else { mfn = mfn / 2; + } mfn = ((unsigned long)(mfn / 500)) * 500; - if (mfn < 500) + if (mfn < 500) { mfn = 500; + } TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Due to the open files/sockets limitation, max supported number of TURN Sessions possible is: %lu " "(approximately)\n", @@ -2787,8 +2869,9 @@ static void print_features(unsigned long mfn) { #endif static void set_network_engine(void) { - if (turn_params.net_engine_version != NEV_UNKNOWN) + if (turn_params.net_engine_version != NEV_UNKNOWN) { return; + } turn_params.net_engine_version = NEV_UDP_SOCKET_PER_ENDPOINT; #if defined(SO_REUSEPORT) #if defined(__linux__) || defined(__LINUX__) || defined(__linux) || defined(linux__) || defined(LINUX) || \ @@ -2922,12 +3005,14 @@ int main(int argc, char **argv) { { int cpus = get_system_number_of_cpus(); - if (0 < cpus) + if (0 < cpus) { turn_params.cpus = get_system_number_of_cpus(); - if (turn_params.cpus < DEFAULT_CPUS_NUMBER) + } + if (turn_params.cpus < DEFAULT_CPUS_NUMBER) { turn_params.cpus = DEFAULT_CPUS_NUMBER; - else if (turn_params.cpus > MAX_NUMBER_OF_GENERAL_RELAY_SERVERS) + } else if (turn_params.cpus > MAX_NUMBER_OF_GENERAL_RELAY_SERVERS) { turn_params.cpus = MAX_NUMBER_OF_GENERAL_RELAY_SERVERS; + } turn_params.general_relay_servers_number = (turnserver_id)turn_params.cpus; @@ -2938,8 +3023,9 @@ int main(int argc, char **argv) { memset(&turn_params.default_users_db, 0, sizeof(default_users_db_t)); turn_params.default_users_db.ram_db.static_accounts = ur_string_map_create(free); - if (strstr(argv[0], "turnadmin")) + if (strstr(argv[0], "turnadmin")) { return adminmain(argc, argv); + } // Zero pass apply the log options. read_config_file(argc, argv, 0); // First pass read other config options @@ -2949,8 +3035,9 @@ int main(int argc, char **argv) { uo.u.m = long_options; while (((c = getopt_long(argc, argv, OPTIONS, uo.u.o, NULL)) != -1)) { - if (c != 'u') + if (c != 'u') { set_option(c, optarg); + } } // Second pass read -u options @@ -3010,8 +3097,9 @@ int main(int argc, char **argv) { #if !defined(TURN_NO_SQLITE) if (!strlen(turn_params.default_users_db.persistent_users_db.userdb) && - (turn_params.default_users_db.userdb_type == TURN_USERDB_TYPE_SQLITE)) + (turn_params.default_users_db.userdb_type == TURN_USERDB_TYPE_SQLITE)) { strncpy(turn_params.default_users_db.persistent_users_db.userdb, DEFAULT_USERDB_FILE, TURN_LONG_STRING_SIZE); + } #endif argc -= optind; @@ -3152,18 +3240,20 @@ int main(int argc, char **argv) { } } - if (socket_init()) + if (socket_init()) { return -1; + } #if defined(WINDOWS) - // TODO: implement deamon!!! use windows server + // TODO: implement deamon!!! use windows server #else if (turn_params.turn_daemon) { #if !defined(TURN_HAS_DAEMON) pid_t pid = fork(); - if (pid > 0) + if (pid > 0) { exit(0); + } if (pid < 0) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot start daemon process\n"); exit(-1); @@ -3253,10 +3343,11 @@ void coturn_locking_function(int mode, int n, const char *file, int line) { UNUSED_ARG(file); UNUSED_ARG(line); if (mutex_buf_initialized && (n < CRYPTO_num_locks())) { - if (mode & CRYPTO_LOCK) + if (mode & CRYPTO_LOCK) { TURN_MUTEX_LOCK(&(mutex_buf[n])); - else + } else { TURN_MUTEX_UNLOCK(&(mutex_buf[n])); + } } } @@ -3281,8 +3372,9 @@ static int THREAD_setup(void) { int THREAD_cleanup(void) { int i; - if (!mutex_buf_initialized) + if (!mutex_buf_initialized) { return 0; + } CRYPTO_THREADID_set_callback(NULL); CRYPTO_set_locking_callback(NULL); @@ -3332,26 +3424,28 @@ static void adjust_key_file_name(char *fn, const char *file_title, int critical) return; } -keyerr : { +keyerr: if (critical) { turn_params.no_tls = 1; turn_params.no_dtls = 1; TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "cannot start TLS and DTLS listeners because %s file is not set properly\n", file_title); } - if (full_path_to_file) + if (full_path_to_file) { free(full_path_to_file); + } return; } -} static void adjust_key_file_names(void) { - if (turn_params.ca_cert_file[0]) + if (turn_params.ca_cert_file[0]) { adjust_key_file_name(turn_params.ca_cert_file, "CA", 1); + } adjust_key_file_name(turn_params.cert_file, "certificate", 1); adjust_key_file_name(turn_params.pkey_file, "private key", 1); - if (turn_params.dh_file[0]) + if (turn_params.dh_file[0]) { adjust_key_file_name(turn_params.dh_file, "DH key", 0); + } } static DH *get_dh566(void) { @@ -3369,8 +3463,9 @@ static DH *get_dh566(void) { unsigned char dh566_g[] = {0x05}; DH *dh; - if ((dh = DH_new()) == NULL) + if ((dh = DH_new()) == NULL) { return (NULL); + } #if OPENSSL_VERSION_NUMBER < 0x10100000L dh->p = BN_bin2bn(dh566_p, sizeof(dh566_p), NULL); dh->g = BN_bin2bn(dh566_g, sizeof(dh566_g), NULL); @@ -3405,8 +3500,9 @@ static DH *get_dh1066(void) { unsigned char dh1066_g[] = {0x02}; DH *dh; - if ((dh = DH_new()) == NULL) + if ((dh = DH_new()) == NULL) { return (NULL); + } #if OPENSSL_VERSION_NUMBER < 0x10100000L dh->p = BN_bin2bn(dh1066_p, sizeof(dh1066_p), NULL); dh->g = BN_bin2bn(dh1066_g, sizeof(dh1066_g), NULL); @@ -3450,8 +3546,9 @@ static DH *get_dh2066(void) { unsigned char dh2066_g[] = {0x05}; DH *dh; - if ((dh = DH_new()) == NULL) + if ((dh = DH_new()) == NULL) { return (NULL); + } #if OPENSSL_VERSION_NUMBER < 0x10100000L dh->p = BN_bin2bn(dh2066_p, sizeof(dh2066_p), NULL); dh->g = BN_bin2bn(dh2066_g, sizeof(dh2066_g), NULL); @@ -3490,8 +3587,9 @@ static int ServerALPNCallback(SSL *ssl, const unsigned char **out, unsigned char const unsigned char *ptr = in; while (ptr < (in + inlen)) { unsigned char current_len = *ptr; - if (ptr + 1 + current_len > in + inlen) + if (ptr + 1 + current_len > in + inlen) { break; + } if ((!turn_params.no_stun) && (current_len == sa_len) && (memcmp(ptr + 1, STUN_ALPN, sa_len) == 0)) { *out = ptr + 1; *outlen = sa_len; @@ -3513,8 +3611,9 @@ static int ServerALPNCallback(SSL *ssl, const unsigned char **out, unsigned char ptr += 1 + current_len; } - if (found_http) + if (found_http) { return SSL_TLSEXT_ERR_OK; + } return SSL_TLSEXT_ERR_NOACK; //??? } @@ -3649,12 +3748,13 @@ static void set_ctx(SSL_CTX **out, const char *protocol, const SSL_METHOD *metho } if (!dh) { - if (turn_params.dh_key_size == DH_566) + if (turn_params.dh_key_size == DH_566) { dh = get_dh566(); - else if (turn_params.dh_key_size == DH_1066) + } else if (turn_params.dh_key_size == DH_1066) { dh = get_dh1066(); - else + } else { dh = get_dh2066(); + } } if (!dh) { @@ -3838,8 +3938,9 @@ static void openssl_load_certificates(void) { static void reload_ssl_certs(evutil_socket_t sock, short events, void *args) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Reloading TLS certificates and keys\n"); openssl_load_certificates(); - if (turn_params.tls_ctx_update_ev != NULL) + if (turn_params.tls_ctx_update_ev != NULL) { event_active(turn_params.tls_ctx_update_ev, EV_READ, 0); + } UNUSED_ARG(sock); UNUSED_ARG(events); diff --git a/src/apps/relay/mainrelay.h b/src/apps/relay/mainrelay.h index f08e1c6..11161f1 100644 --- a/src/apps/relay/mainrelay.h +++ b/src/apps/relay/mainrelay.h @@ -338,14 +338,16 @@ extern turn_params_t turn_params; //////////////// Listener server ///////////////// static inline int get_alt_listener_port(void) { - if (turn_params.alt_listener_port < 1) + if (turn_params.alt_listener_port < 1) { return turn_params.listener_port + 1; + } return turn_params.alt_listener_port; } static inline int get_alt_tls_listener_port(void) { - if (turn_params.alt_tls_listener_port < 1) + if (turn_params.alt_tls_listener_port < 1) { return turn_params.tls_listener_port + 1; + } return turn_params.alt_tls_listener_port; } diff --git a/src/apps/relay/netengine.c b/src/apps/relay/netengine.c index 5df4cfc..e33a020 100644 --- a/src/apps/relay/netengine.c +++ b/src/apps/relay/netengine.c @@ -299,11 +299,13 @@ typedef struct update_ssl_ctx_cb_args { * Copy SSL context at "from", which may be NULL if no context in use */ static void replace_one_ssl_ctx(SSL_CTX **to, SSL_CTX *from) { - if (*to) + if (*to) { SSL_CTX_free(*to); + } - if (from != NULL) + if (from != NULL) { SSL_CTX_up_ref(from); + } *to = from; } @@ -324,8 +326,9 @@ static void update_ssl_ctx(evutil_socket_t sock, short events, update_ssl_ctx_cb struct event *next = args->next; TURN_MUTEX_UNLOCK(&turn_params.tls_mutex); - if (next != NULL) + if (next != NULL) { event_active(next, EV_READ, 0); + } UNUSED_ARG(sock); UNUSED_ARG(events); @@ -459,10 +462,11 @@ static authserver_id auth_message_counter = 1; void send_auth_message_to_auth_server(struct auth_message *am) { TURN_MUTEX_LOCK(&auth_message_counter_mutex); - if (auth_message_counter >= authserver_number) + if (auth_message_counter >= authserver_number) { auth_message_counter = 1; - else if (auth_message_counter < 1) + } else if (auth_message_counter < 1) { auth_message_counter = 1; + } authserver_id sn = auth_message_counter++; TURN_MUTEX_UNLOCK(&auth_message_counter_mutex); @@ -504,9 +508,9 @@ static void auth_server_receive_message(struct bufferevent *bev, void *ptr) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: can't find relay for turn_server_id: %d\n", __FUNCTION__, (int)am.id); } - if (output) + if (output) { evbuffer_add(output, &am, sizeof(struct auth_message)); - else { + } else { ioa_network_buffer_delete(NULL, am.in_buffer.nbh); am.in_buffer.nbh = NULL; } @@ -974,8 +978,9 @@ static void setup_listener(void) { #endif ); - if (!turn_params.listener.ioa_eng) + if (!turn_params.listener.ioa_eng) { exit(-1); + } set_ssl_ctx(turn_params.listener.ioa_eng, &turn_params); turn_params.listener.rtcpmap = rtcp_map_create(turn_params.listener.ioa_eng); @@ -1046,8 +1051,9 @@ static void setup_barriers(void) { #if !defined(TURN_NO_THREAD_BARRIERS) { - if (pthread_barrier_init(&barrier, NULL, barrier_count) < 0) + if (pthread_barrier_init(&barrier, NULL, barrier_count) < 0) { perror("barrier init"); + } } #endif @@ -1092,8 +1098,9 @@ static void setup_socket_per_endpoint_udp_listener_servers(void) { int is_5780 = turn_params.rfc5780; if (turn_params.general_relay_servers_number <= 1) { - while (!(general_relay_servers[0]->ioa_eng)) + while (!(general_relay_servers[0]->ioa_eng)) { sched_yield(); + } udp_relay_servers[i] = general_relay_servers[0]; continue; } else if (turn_params.general_relay_servers_number > 1) { @@ -1194,8 +1201,9 @@ static void setup_socket_per_endpoint_udp_listener_servers(void) { } } else { turn_params.listener.udp_services[index] = NULL; - if (turn_params.rfc5780) + if (turn_params.rfc5780) { turn_params.listener.udp_services[index + 1] = NULL; + } } if (!turn_params.no_dtls && (turn_params.no_udp || (turn_params.listener_port != turn_params.tls_listener_port))) { @@ -1238,8 +1246,9 @@ static void setup_socket_per_endpoint_udp_listener_servers(void) { } } else { turn_params.listener.dtls_services[index] = NULL; - if (turn_params.rfc5780) + if (turn_params.rfc5780) { turn_params.listener.dtls_services[index + 1] = NULL; + } } } } @@ -1251,8 +1260,9 @@ static void setup_socket_per_thread_udp_listener_servers(void) { /* Create listeners */ for (relayindex = 0; relayindex < get_real_general_relay_servers_number(); relayindex++) { - while (!(general_relay_servers[relayindex]->ioa_eng) || !(general_relay_servers[relayindex]->server.e)) + while (!(general_relay_servers[relayindex]->ioa_eng) || !(general_relay_servers[relayindex]->server.e)) { sched_yield(); + } } /* Aux UDP servers */ @@ -1314,8 +1324,9 @@ static void setup_socket_per_thread_udp_listener_servers(void) { } } else { turn_params.listener.udp_services[index] = NULL; - if (turn_params.rfc5780) + if (turn_params.rfc5780) { turn_params.listener.udp_services[index + 1] = NULL; + } } if (!turn_params.no_dtls && (turn_params.no_udp || (turn_params.listener_port != turn_params.tls_listener_port))) { @@ -1346,8 +1357,9 @@ static void setup_socket_per_thread_udp_listener_servers(void) { } } else { turn_params.listener.dtls_services[index] = NULL; - if (turn_params.rfc5780) + if (turn_params.rfc5780) { turn_params.listener.dtls_services[index + 1] = NULL; + } } } } @@ -1403,8 +1415,9 @@ static void setup_socket_per_session_udp_listener_servers(void) { } } else { turn_params.listener.udp_services[index] = NULL; - if (turn_params.rfc5780) + if (turn_params.rfc5780) { turn_params.listener.udp_services[index + 1] = NULL; + } } if (!turn_params.no_dtls && (turn_params.no_udp || (turn_params.listener_port != turn_params.tls_listener_port))) { @@ -1427,8 +1440,9 @@ static void setup_socket_per_session_udp_listener_servers(void) { } } else { turn_params.listener.dtls_services[index] = NULL; - if (turn_params.rfc5780) + if (turn_params.rfc5780) { turn_params.listener.dtls_services[index + 1] = NULL; + } } } } @@ -1473,54 +1487,58 @@ static void setup_tcp_listener_servers(ioa_engine_handle e, struct relay_server create_tls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], turn_params.tcp_use_proxy ? turn_params.tcp_proxy_port : turn_params.listener_port, turn_params.verbose, e, send_socket_to_general_relay, relay_server); - if (turn_params.rfc5780) + if (turn_params.rfc5780) { tcp_services[index + 1] = turn_params.tcp_use_proxy ? NULL : create_tls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], get_alt_listener_port(), turn_params.verbose, e, send_socket_to_general_relay, relay_server); + } } else { tcp_services[index] = NULL; - if (turn_params.rfc5780) + if (turn_params.rfc5780) { tcp_services[index + 1] = NULL; + } } if (!turn_params.no_tls && !turn_params.tcp_use_proxy && (turn_params.no_tcp || (turn_params.listener_port != turn_params.tls_listener_port))) { tls_services[index] = create_tls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], turn_params.tls_listener_port, turn_params.verbose, e, send_socket_to_general_relay, relay_server); - if (turn_params.rfc5780) + if (turn_params.rfc5780) { tls_services[index + 1] = create_tls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], get_alt_tls_listener_port(), turn_params.verbose, e, send_socket_to_general_relay, relay_server); + } } else { tls_services[index] = NULL; - if (turn_params.rfc5780) + if (turn_params.rfc5780) { tls_services[index + 1] = NULL; + } } } } static int get_alt_addr(ioa_addr *addr, ioa_addr *alt_addr) { - if (!addr || !turn_params.rfc5780 || (turn_params.listener.addrs_number < 2)) - ; - else { + if (!addr || !turn_params.rfc5780 || (turn_params.listener.addrs_number < 2)) { + } else { size_t index = 0xffff; size_t i = 0; int alt_port = -1; int port = addr_get_port(addr); - if (port == turn_params.listener_port) + if (port == turn_params.listener_port) { alt_port = get_alt_listener_port(); - else if (port == get_alt_listener_port()) + } else if (port == get_alt_listener_port()) { alt_port = turn_params.listener_port; - else if (port == turn_params.tls_listener_port) + } else if (port == turn_params.tls_listener_port) { alt_port = get_alt_tls_listener_port(); - else if (port == get_alt_tls_listener_port()) + } else if (port == get_alt_tls_listener_port()) { alt_port = turn_params.tls_listener_port; - else + } else { return -1; + } for (i = 0; i < turn_params.listener.addrs_number; i++) { if (turn_params.listener.encaddrs && turn_params.listener.encaddrs[i]) { @@ -1549,11 +1567,13 @@ static int get_alt_addr(ioa_addr *addr, ioa_addr *alt_addr) { } static void run_events(struct event_base *eb, ioa_engine_handle e) { - if (!eb && e) + if (!eb && e) { eb = e->event_base; + } - if (!eb) + if (!eb) { return; + } struct timeval timeout; @@ -1804,8 +1824,9 @@ void setup_server(void) { authserver_number = 1 + (authserver_id)(turn_params.cpus / 2); - if (authserver_number < MIN_AUTHSERVER_NUMBER) + if (authserver_number < MIN_AUTHSERVER_NUMBER) { authserver_number = MIN_AUTHSERVER_NUMBER; + } #if !defined(TURN_NO_THREAD_BARRIERS) @@ -1822,12 +1843,13 @@ void setup_server(void) { setup_general_relay_servers(); TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Total General servers: %d\n", (int)get_real_general_relay_servers_number()); - if (turn_params.net_engine_version == NEV_UDP_SOCKET_PER_THREAD) + if (turn_params.net_engine_version == NEV_UDP_SOCKET_PER_THREAD) { setup_socket_per_thread_udp_listener_servers(); - else if (turn_params.net_engine_version == NEV_UDP_SOCKET_PER_ENDPOINT) + } else if (turn_params.net_engine_version == NEV_UDP_SOCKET_PER_ENDPOINT) { setup_socket_per_endpoint_udp_listener_servers(); - else if (turn_params.net_engine_version == NEV_UDP_SOCKET_PER_SESSION) + } else if (turn_params.net_engine_version == NEV_UDP_SOCKET_PER_SESSION) { setup_socket_per_session_udp_listener_servers(); + } if (turn_params.net_engine_version != NEV_UDP_SOCKET_PER_THREAD) { setup_tcp_listener_servers(turn_params.listener.ioa_eng, NULL); diff --git a/src/apps/relay/ns_ioalib_engine_impl.c b/src/apps/relay/ns_ioalib_engine_impl.c index c62d92e..65d2656 100644 --- a/src/apps/relay/ns_ioalib_engine_impl.c +++ b/src/apps/relay/ns_ioalib_engine_impl.c @@ -119,8 +119,9 @@ static int is_socket_writeable(ioa_socket_handle s, size_t sz, const char *msg, UNUSED_ARG(msg); UNUSED_ARG(option); - if (!s) + if (!s) { return 0; + } if (!(s->done) && !(s->broken) && !(s->tobeclosed)) { @@ -180,8 +181,9 @@ static int is_socket_writeable(ioa_socket_handle s, size_t sz, const char *msg, static void log_socket_event(ioa_socket_handle s, const char *msg, int error) { if (s && (error || (s->e && s->e->verbose))) { - if (!msg) + if (!msg) { msg = "General socket event"; + } turnsession_id id = 0; { ts_ur_super_session *ss = s->session; @@ -193,8 +195,9 @@ static void log_socket_event(ioa_socket_handle s, const char *msg, int error) { } TURN_LOG_LEVEL ll = TURN_LOG_LEVEL_INFO; - if (error) + if (error) { ll = TURN_LOG_LEVEL_ERROR; + } UNUSED_ARG(ll); @@ -216,14 +219,17 @@ static void log_socket_event(ioa_socket_handle s, const char *msg, int error) { } int set_df_on_ioa_socket(ioa_socket_handle s, int value) { - if (!s) + if (!s) { return 0; + } - if (s->parent_s) + if (s->parent_s) { return 0; + } - if (s->do_not_use_df) + if (s->do_not_use_df) { value = 0; + } if (s->current_df_relay_flag != value) { s->current_df_relay_flag = value; @@ -234,8 +240,9 @@ int set_df_on_ioa_socket(ioa_socket_handle s, int value) { } void set_do_not_use_df(ioa_socket_handle s) { - if (s->parent_s) + if (s->parent_s) { return; + } s->do_not_use_df = 1; s->current_df_relay_flag = 1; @@ -245,8 +252,9 @@ void set_do_not_use_df(ioa_socket_handle s) { /************** Buffer List ********************/ static int buffer_list_empty(stun_buffer_list *bufs) { - if (bufs && bufs->head && bufs->tsz) + if (bufs && bufs->head && bufs->tsz) { return 0; + } return 1; } @@ -422,8 +430,9 @@ ioa_engine_handle create_ioa_engine(super_memory_t *sm, struct event_base *eb, t } } - if (relay_ifname) + if (relay_ifname) { STRCPY(e->relay_ifname, relay_ifname); + } { size_t i = 0; e->relay_addrs = (ioa_addr *)allocate_super_memory_region(sm, relays_number * sizeof(ioa_addr) + 8); @@ -442,8 +451,9 @@ ioa_engine_handle create_ioa_engine(super_memory_t *sm, struct event_base *eb, t } void ioa_engine_set_rtcp_map(ioa_engine_handle e, rtcp_map *rtcpmap) { - if (e) + if (e) { e->map_rtcp = rtcpmap; + } } static const ioa_addr *ioa_engine_get_relay_addr(ioa_engine_handle e, ioa_socket_handle client_s, int address_family, @@ -451,8 +461,9 @@ static const ioa_addr *ioa_engine_get_relay_addr(ioa_engine_handle e, ioa_socket if (e) { int family = AF_INET; - if (address_family == STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV6) + if (address_family == STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV6) { family = AF_INET6; + } if (e->default_relays) { @@ -462,12 +473,14 @@ static const ioa_addr *ioa_engine_get_relay_addr(ioa_engine_handle e, ioa_socket if (client_addr) { switch (address_family) { case STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV4: - if (client_addr->ss.sa_family == AF_INET) + if (client_addr->ss.sa_family == AF_INET) { return client_addr; + } break; case STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV6: - if (client_addr->ss.sa_family == AF_INET6) + if (client_addr->ss.sa_family == AF_INET6) { return client_addr; + } break; default: return client_addr; @@ -483,22 +496,26 @@ static const ioa_addr *ioa_engine_get_relay_addr(ioa_engine_handle e, ioa_socket for (i = 0; i < e->relays_number; i++) { - if (e->relay_addr_counter >= e->relays_number) + if (e->relay_addr_counter >= e->relays_number) { e->relay_addr_counter = 0; + } ioa_addr *relay_addr = &(e->relay_addrs[e->relay_addr_counter++]); - if (addr_any_no_port(relay_addr)) + if (addr_any_no_port(relay_addr)) { get_a_local_relay(family, relay_addr); + } switch (address_family) { case STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_DEFAULT: case STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV4: - if (relay_addr->ss.sa_family == AF_INET) + if (relay_addr->ss.sa_family == AF_INET) { return relay_addr; + } break; case STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY_VALUE_IPV6: - if (relay_addr->ss.sa_family == AF_INET6) + if (relay_addr->ss.sa_family == AF_INET6) { return relay_addr; + } break; default:; }; @@ -508,8 +525,9 @@ static const ioa_addr *ioa_engine_get_relay_addr(ioa_engine_handle e, ioa_socket // Fallback to "find whatever is available": - if (e->relay_addr_counter >= e->relays_number) + if (e->relay_addr_counter >= e->relays_number) { e->relay_addr_counter = 0; + } const ioa_addr *relay_addr = &(e->relay_addrs[e->relay_addr_counter++]); return relay_addr; } @@ -525,16 +543,19 @@ static const ioa_addr *ioa_engine_get_relay_addr(ioa_engine_handle e, ioa_socket static void timer_event_handler(evutil_socket_t fd, short what, void *arg) { timer_event *te = (timer_event *)arg; - if (!te) + if (!te) { return; + } UNUSED_ARG(fd); - if (!(what & EV_TIMEOUT)) + if (!(what & EV_TIMEOUT)) { return; + } - if (te->e && eve(te->e->verbose)) + if (te->e && eve(te->e->verbose)) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: timeout 0x%lx: %s\n", __FUNCTION__, (long)te, te->txt); + } ioa_timer_event_handler cb = te->cb; ioa_engine_handle e = te->e; @@ -551,8 +572,9 @@ ioa_timer_handle set_ioa_timer(ioa_engine_handle e, int secs, int ms, ioa_timer_ timer_event *te = (timer_event *)malloc(sizeof(timer_event)); int flags = EV_TIMEOUT; - if (persist) + if (persist) { flags |= EV_PERSIST; + } struct event *ev = event_new(e->event_base, -1, flags, timer_event_handler, te); struct timeval tv; @@ -618,8 +640,9 @@ int ioa_socket_check_bandwidth(ioa_socket_handle s, ioa_network_buffer_handle nb band_limit_t max_bps = s->session->bps; - if (max_bps < 1) + if (max_bps < 1) { return 1; + } struct traffic_bytes *traffic = &(s->data_traffic); @@ -644,25 +667,28 @@ int ioa_socket_check_bandwidth(ioa_socket_handle s, ioa_network_buffer_handle nb if (bsz > max_bps) { return 0; } else { - if (read) + if (read) { traffic->jiffie_bytes_read = bsz; - else + } else { traffic->jiffie_bytes_write = bsz; + } return 1; } } else { band_limit_t nsz; - if (read) + if (read) { nsz = traffic->jiffie_bytes_read + bsz; - else + } else { nsz = traffic->jiffie_bytes_write + bsz; + } if (nsz > max_bps) { return 0; } else { - if (read) + if (read) { traffic->jiffie_bytes_read = nsz; - else + } else { traffic->jiffie_bytes_write = nsz; + } return 1; } } @@ -684,16 +710,19 @@ int get_ioa_socket_from_reservation(ioa_engine_handle e, uint64_t in_reservation /* Socket options helpers ==>> */ static int set_socket_ttl(ioa_socket_handle s, int ttl) { - if (s->default_ttl < 0) // Unsupported + if (s->default_ttl < 0) { // Unsupported return -1; + } - if (ttl < 0) + if (ttl < 0) { ttl = s->default_ttl; + } CORRECT_RAW_TTL(ttl); - if (ttl > s->default_ttl) + if (ttl > s->default_ttl) { ttl = s->default_ttl; + } if (s->current_ttl != ttl) { int ret = set_raw_socket_ttl(s->fd, s->family, ttl); @@ -705,11 +734,13 @@ static int set_socket_ttl(ioa_socket_handle s, int ttl) { } static int set_socket_tos(ioa_socket_handle s, int tos) { - if (s->default_tos < 0) // Unsupported + if (s->default_tos < 0) { // Unsupported return -1; + } - if (tos < 0) + if (tos < 0) { tos = s->default_tos; + } CORRECT_RAW_TOS(tos); @@ -771,8 +802,9 @@ int set_raw_socket_tos_options(evutil_socket_t fd, int family) { } int set_socket_options_fd(evutil_socket_t fd, SOCKET_TYPE st, int family) { - if (fd < 0) + if (fd < 0) { return 0; + } set_sock_buf_size(fd, UR_CLIENT_SOCK_BUF_SIZE); @@ -798,8 +830,9 @@ int set_socket_options_fd(evutil_socket_t fd, SOCKET_TYPE st, int family) { #ifdef TURN_IP_RECVERR on = 1; #endif - if (setsockopt(fd, IPPROTO_IP, IP_RECVERR, (void *)&on, sizeof(on)) < 0) + if (setsockopt(fd, IPPROTO_IP, IP_RECVERR, (void *)&on, sizeof(on)) < 0) { perror("IP_RECVERR"); + } } #endif @@ -809,8 +842,9 @@ int set_socket_options_fd(evutil_socket_t fd, SOCKET_TYPE st, int family) { #ifdef TURN_IP_RECVERR on = 1; #endif - if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVERR, (void *)&on, sizeof(on)) < 0) + if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVERR, (void *)&on, sizeof(on)) < 0) { perror("IPV6_RECVERR"); + } } #endif @@ -841,8 +875,9 @@ int set_socket_options_fd(evutil_socket_t fd, SOCKET_TYPE st, int family) { } int set_socket_options(ioa_socket_handle s) { - if (!s || (s->parent_s)) + if (!s || (s->parent_s)) { return 0; + } set_socket_options_fd(s->fd, s->st, s->family); @@ -900,8 +935,9 @@ ioa_socket_handle create_unbound_relay_ioa_socket(ioa_engine_handle e, int famil } static int bind_ioa_socket(ioa_socket_handle s, const ioa_addr *local_addr, int reusable) { - if (!s || (s->parent_s)) + if (!s || (s->parent_s)) { return 0; + } if (s && s->fd >= 0 && s->e && local_addr) { @@ -931,8 +967,9 @@ int create_relay_ioa_sockets(ioa_engine_handle e, ioa_socket_handle client_s, in void *acbarg) { *rtp_s = NULL; - if (rtcp_s) + if (rtcp_s) { *rtcp_s = NULL; + } turnipports *tp = e->tp; @@ -942,20 +979,23 @@ int create_relay_ioa_sockets(ioa_engine_handle e, ioa_socket_handle client_s, in ioa_addr relay_addr; const ioa_addr *ra = ioa_engine_get_relay_addr(e, client_s, address_family, err_code); - if (ra) + if (ra) { addr_cpy(&relay_addr, ra); + } if (*err_code) { - if (*err_code == 440) + if (*err_code == 440) { *reason = (const uint8_t *)"Unsupported address family"; + } return -1; } int rtcp_port = -1; IOA_CLOSE_SOCKET(*rtp_s); - if (rtcp_s) + if (rtcp_s) { IOA_CLOSE_SOCKET(*rtcp_s); + } ioa_addr rtcp_local_addr; addr_cpy(&rtcp_local_addr, &relay_addr); @@ -1002,8 +1042,9 @@ int create_relay_ioa_sockets(ioa_engine_handle e, ioa_socket_handle client_s, in } if (port < 0) { IOA_CLOSE_SOCKET(*rtp_s); - if (rtcp_s) + if (rtcp_s) { IOA_CLOSE_SOCKET(*rtcp_s); + } rtcp_port = -1; break; } else { @@ -1054,8 +1095,9 @@ int create_relay_ioa_sockets(ioa_engine_handle e, ioa_socket_handle client_s, in if (i >= 0xFFFF) { IOA_CLOSE_SOCKET(*rtp_s); - if (rtcp_s) + if (rtcp_s) { IOA_CLOSE_SOCKET(*rtcp_s); + } } if (*rtp_s) { @@ -1072,8 +1114,9 @@ int create_relay_ioa_sockets(ioa_engine_handle e, ioa_socket_handle client_s, in if (!(*rtp_s)) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: no available ports 3\n", __FUNCTION__); IOA_CLOSE_SOCKET(*rtp_s); - if (rtcp_s) + if (rtcp_s) { IOA_CLOSE_SOCKET(*rtcp_s); + } return -1; } @@ -1083,8 +1126,9 @@ int create_relay_ioa_sockets(ioa_engine_handle e, ioa_socket_handle client_s, in if (rtcp_map_put(e->map_rtcp, *out_reservation_token, *rtcp_s) < 0) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: cannot update RTCP map\n", __FUNCTION__); IOA_CLOSE_SOCKET(*rtp_s); - if (rtcp_s) + if (rtcp_s) { IOA_CLOSE_SOCKET(*rtcp_s); + } return -1; } } @@ -1122,8 +1166,9 @@ static void tcp_listener_input_handler(struct evconnlistener *l, evutil_socket_t } static int set_accept_cb(ioa_socket_handle s, accept_cb acb, void *arg) { - if (!s || s->parent_s) + if (!s || s->parent_s) { return -1; + } if (s->st == TCP_SOCKET) { s->list_ev = evconnlistener_new(s->e->event_base, tcp_listener_input_handler, s, LEV_OPT_REUSEABLE, 1024, s->fd); @@ -1310,8 +1355,9 @@ ioa_socket_handle create_ioa_socket_from_fd(ioa_engine_handle e, ioa_socket_raw if (remote_addr) { ret->connected = 1; - if (!(ret->family)) + if (!(ret->family)) { ret->family = remote_addr->ss.sa_family; + } addr_cpy(&(ret->remote_addr), remote_addr); } @@ -1380,8 +1426,9 @@ static void set_socket_ssl(ioa_socket_handle s, SSL *ssl) { ioa_socket_handle create_ioa_socket_from_ssl(ioa_engine_handle e, ioa_socket_handle parent_s, SSL *ssl, SOCKET_TYPE st, SOCKET_APP_TYPE sat, const ioa_addr *remote_addr, const ioa_addr *local_addr) { - if (!parent_s) + if (!parent_s) { return NULL; + } ioa_socket_handle ret = create_ioa_socket_from_fd(e, parent_s->fd, parent_s, st, sat, remote_addr, local_addr); @@ -1468,8 +1515,9 @@ void close_ioa_socket(ioa_socket_handle s) { s->done = 1; - while (!buffer_list_empty(&(s->bufs))) + while (!buffer_list_empty(&(s->bufs))) { pop_elem_from_buffer_list(&(s->bufs)); + } ioa_network_buffer_delete(s->e, s->defer_nbh); @@ -1569,16 +1617,18 @@ ioa_socket_handle detach_ioa_socket(ioa_socket_handle s) { detach_socket_net_data(s); - while (!buffer_list_empty(&(s->bufs))) + while (!buffer_list_empty(&(s->bufs))) { pop_elem_from_buffer_list(&(s->bufs)); + } ioa_network_buffer_delete(s->e, s->defer_nbh); ret = (ioa_socket *)calloc(sizeof(ioa_socket), 1); if (!ret) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: Cannot allocate new socket structure\n", __FUNCTION__); - if (udp_fd >= 0) + if (udp_fd >= 0) { socket_closesocket(udp_fd); + } return ret; } @@ -1624,14 +1674,16 @@ ioa_socket_handle detach_ioa_socket(ioa_socket_handle s) { } ts_ur_super_session *get_ioa_socket_session(ioa_socket_handle s) { - if (s) + if (s) { return s->session; + } return NULL; } void set_ioa_socket_session(ioa_socket_handle s, ts_ur_super_session *ss) { - if (s) + if (s) { s->session = ss; + } } void clear_ioa_socket_session_if(ioa_socket_handle s, void *ss) { @@ -1641,14 +1693,16 @@ void clear_ioa_socket_session_if(ioa_socket_handle s, void *ss) { } tcp_connection *get_ioa_socket_sub_session(ioa_socket_handle s) { - if (s) + if (s) { return s->sub_session; + } return NULL; } void set_ioa_socket_sub_session(ioa_socket_handle s, tcp_connection *tc) { - if (s) + if (s) { s->sub_session = tc; + } } int get_ioa_socket_address_family(ioa_socket_handle s) { @@ -1667,21 +1721,24 @@ beg: } SOCKET_TYPE get_ioa_socket_type(ioa_socket_handle s) { - if (s) + if (s) { return s->st; + } return UNKNOWN_SOCKET; } SOCKET_APP_TYPE get_ioa_socket_app_type(ioa_socket_handle s) { - if (s) + if (s) { return s->sat; + } return UNKNOWN_APP_SOCKET; } void set_ioa_socket_app_type(ioa_socket_handle s, SOCKET_APP_TYPE sat) { - if (s) + if (s) { s->sat = sat; + } } ioa_addr *get_local_addr_from_ioa_socket(ioa_socket_handle s) { @@ -1733,8 +1790,9 @@ ioa_addr *get_remote_addr_from_ioa_socket(ioa_socket_handle s) { int get_local_mtu_ioa_socket(ioa_socket_handle s) { if (s) { - if (s->parent_s) + if (s->parent_s) { s = s->parent_s; + } return get_socket_mtu(s->fd, s->family, (s->e && eve(s->e->verbose))); } @@ -1748,15 +1806,17 @@ int get_local_mtu_ioa_socket(ioa_socket_handle s) { int ssl_read(evutil_socket_t fd, SSL *ssl, ioa_network_buffer_handle nbh, int verbose) { int ret = 0; - if (!ssl || !nbh) + if (!ssl || !nbh) { return -1; + } char *buffer = (char *)ioa_network_buffer_data(nbh); int buf_size = (int)ioa_network_buffer_get_capacity_udp(); int read_len = (int)ioa_network_buffer_get_size(nbh); - if (read_len < 1) + if (read_len < 1) { return -1; + } char *new_buffer = buffer + buf_size; int old_buffer_len = read_len; @@ -1857,8 +1917,9 @@ int ssl_read(evutil_socket_t fd, SSL *ssl, ioa_network_buffer_handle nbh, int ve TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s (%d)\n", ERR_error_string(ERR_get_error(), buf), SSL_get_error(ssl, len)); } - if (verbose) + if (verbose) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "SSL connection closed.\n"); + } ret = -1; break; default: @@ -1884,8 +1945,9 @@ int ssl_read(evutil_socket_t fd, SSL *ssl, ioa_network_buffer_handle nbh, int ve } static int socket_readerr(evutil_socket_t fd, ioa_addr *orig_addr) { - if ((fd < 0) || !orig_addr) + if ((fd < 0) || !orig_addr) { return -1; + } #if defined(CMSG_SPACE) && defined(MSG_ERRQUEUE) && defined(IP_RECVERR) #ifdef _MSC_VER @@ -1937,11 +1999,13 @@ int udp_recvfrom(evutil_socket_t fd, ioa_addr *orig_addr, const ioa_addr *like_a int *ttl, int *tos, char *ecmsg, int flags, uint32_t *errcode) { int len = 0; - if (fd < 0 || !orig_addr || !like_addr || !buffer) + if (fd < 0 || !orig_addr || !like_addr || !buffer) { return -1; + } - if (errcode) + if (errcode) { *errcode = 0; + } int slen = get_ioa_addr_len(like_addr); recv_ttl_t recv_ttl = TTL_DEFAULT; @@ -1951,8 +2015,9 @@ int udp_recvfrom(evutil_socket_t fd, ioa_addr *orig_addr, const ioa_addr *like_a do { len = recvfrom(fd, buffer, buf_size, flags, (struct sockaddr *)orig_addr, (socklen_t *)&slen); } while (len < 0 && socket_eintr()); - if (len < 0 && errcode) + if (len < 0 && errcode) { *errcode = (uint32_t)socket_errno(); + } #else struct msghdr msg; struct iovec iov; @@ -1983,8 +2048,9 @@ try_again: #if defined(MSG_ERRQUEUE) if (flags & MSG_ERRQUEUE) { - if ((len > 0) && (try_cycle++ < MAX_ERRORS_IN_UDP_BATCH)) + if ((len > 0) && (try_cycle++ < MAX_ERRORS_IN_UDP_BATCH)) { goto try_again; + } } if ((len < 0) && (!(flags & MSG_ERRQUEUE))) { @@ -2026,8 +2092,9 @@ try_again: #if defined(IP_RECVERR) case IP_RECVERR: { struct turn_sock_extended_err *e = (struct turn_sock_extended_err *)CMSG_DATA(cmsgh); - if (errcode) + if (errcode) { *errcode = e->ee_errno; + } } break; #endif default:; @@ -2051,8 +2118,9 @@ try_again: #if defined(IPV6_RECVERR) case IPV6_RECVERR: { struct turn_sock_extended_err *e = (struct turn_sock_extended_err *)CMSG_DATA(cmsgh); - if (errcode) + if (errcode) { *errcode = e->ee_errno; + } } break; #endif default:; @@ -2093,10 +2161,11 @@ static TURN_TLS_TYPE check_tentative_tls(ioa_socket_raw fd) { if (len > 0 && ((size_t)len == sizeof(s))) { if ((s[0] == 22) && (s[1] == 3) && (s[5] == 1) && (s[9] == 3)) { char max_supported = (char)(TURN_TLS_TOTAL - 2); - if (s[10] > max_supported) + if (s[10] > max_supported) { ret = TURN_TLS_SSL23; /* compatibility mode */ - else + } else { ret = (TURN_TLS_TYPE)(s[10] + 1); + } } else if ((s[2] == 1) && (s[3] == 3)) { ret = TURN_TLS_SSL23; /* compatibility mode */ } @@ -2149,44 +2218,53 @@ static ssize_t socket_parse_proxy_v1(ioa_socket_handle s, uint8_t *buf, size_t l size_t tlen = 11; /* Read source address */ tlen = proxy_string_field(saddr, sizeof(saddr), buf, tlen, len); - if (0 == tlen) + if (0 == tlen) { return -1; + } /* Read dest address */ tlen = proxy_string_field(daddr, sizeof(daddr), buf, tlen, len); - if (0 == tlen) + if (0 == tlen) { return -1; + } /* Read source port */ tlen = proxy_string_field(sport, sizeof(sport), buf, tlen, len); - if (0 == tlen) + if (0 == tlen) { return -1; + } /* Read dest port */ tlen = proxy_string_field(dport, sizeof(dport), buf, tlen, len); - if (0 == tlen) + if (0 == tlen) { return -1; + } /* Final line feed */ - if ((len <= tlen) || (0x0A != buf[tlen])) + 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)) + if ((sport_int < 0) || (0xFFFF < sport_int)) { return -1; - if ((dport_int < 0) || (0xFFFF < dport_int)) + } + 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)) + 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)) + } + 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); @@ -2196,10 +2274,12 @@ static ssize_t socket_parse_proxy_v1(ioa_socket_handle s, uint8_t *buf, size_t l } 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)) + 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)) + } + 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); @@ -2222,8 +2302,9 @@ static ssize_t socket_parse_proxy_v2(ioa_socket_handle s, uint8_t *buf, size_t l /* Check version */ uint8_t version = buf[12] >> 4; - if (version != 2) + if (version != 2) { return -1; + } /* Read data */ uint8_t command = buf[12] & 0xF; @@ -2232,16 +2313,19 @@ static ssize_t socket_parse_proxy_v2(ioa_socket_handle s, uint8_t *buf, size_t l size_t plen = ((size_t)buf[14] << 8) | buf[15]; size_t tlen = 16 + plen; - if (len < tlen) + if (len < tlen) { return 0; + } /* A local connection is used by the proxy itself and does not carry a valid address */ - if (command == 0) + if (command == 0) { return tlen; + } /* Accept only proxied TCP connections */ - if (command != 1 || proto != 1) + if (command != 1 || proto != 1) { return -1; + } /* Read the address */ if (family == 1 && plen >= 12) { /* IPv4 */ @@ -2295,8 +2379,9 @@ static int socket_input_worker(ioa_socket_handle s) { int try_cycle = 0; const int MAX_TRIES = 16; - if (!s) + if (!s) { return 0; + } if ((s->magic != SOCKET_MAGIC) || (s->done)) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "!!!%s on socket: 0x%lx, st=%d, sat=%d\n", __FUNCTION__, (long)s, s->st, s->sat); @@ -2304,14 +2389,17 @@ static int socket_input_worker(ioa_socket_handle s) { return -1; } - if (!(s->e)) + if (!(s->e)) { return 0; + } - if (s->tobeclosed) + if (s->tobeclosed) { return 0; + } - if (s->connected) + if (s->connected) { addr_cpy(&remote_addr, &(s->remote_addr)); + } if (tcp_congestion_control && s->sub_session && s->bev) { if (s == s->sub_session->client_s && (s->sub_session->peer_s)) { @@ -2344,8 +2432,9 @@ static int socket_input_worker(ioa_socket_handle s) { } } - if (!(s->e)) + if (!(s->e)) { return 0; + } if (s->st == TENTATIVE_TCP_SOCKET) { EVENT_DEL(s->read_event); @@ -2427,8 +2516,9 @@ static int socket_input_worker(ioa_socket_handle s) { try_start: - if (!(s->e)) + if (!(s->e)) { return 0; + } try_again = 0; try_ok = 0; @@ -2444,8 +2534,9 @@ try_start: if (blen > 0) { int mlen = 0; - if (blen > (ev_ssize_t)STUN_BUFFER_SIZE) + if (blen > (ev_ssize_t)STUN_BUFFER_SIZE) { blen = (ev_ssize_t)STUN_BUFFER_SIZE; + } if (s->st == TCP_SOCKET_PROXY) { ssize_t tlen = socket_parse_proxy(s, buf_elem->buf.buf, blen); @@ -2504,8 +2595,9 @@ try_start: log_socket_event(s, "socket input failed, socket to be closed", 1); } - if (len == 0) + if (len == 0) { len = -1; + } } else if (s->fd >= 0) { /* UDP and DTLS */ ret = udp_recvfrom(s->fd, &remote_addr, &(s->local_addr), (char *)(buf_elem->buf.buf), UDP_STUN_BUFFER_SIZE, &ttl, &tos, s->e->cmsg, 0, NULL); @@ -2523,11 +2615,13 @@ try_start: } else { len = (int)ioa_network_buffer_get_size((ioa_network_buffer_handle)buf_elem); } - if ((ret != -1) && (len > 0)) + if ((ret != -1) && (len > 0)) { try_again = 1; + } } else { /* UDP */ - if (ret >= 0) + if (ret >= 0) { try_again = 1; + } } } else { s->tobeclosed = 1; @@ -2538,10 +2632,11 @@ try_start: if ((ret != -1) && (len >= 0)) { - if (app_msg_len) + if (app_msg_len) { buf_elem->buf.len = app_msg_len; - else + } else { buf_elem->buf.len = len; + } if (ioa_socket_check_bandwidth(s, buf_elem, 1)) { @@ -2556,8 +2651,9 @@ try_start: s->read_cb(s, IOA_EV_READ, &nd, s->read_ctx, 1); - if (nd.nbh) + if (nd.nbh) { free_blist_elem(s->e, buf_elem); + } buf_elem = NULL; @@ -2585,8 +2681,9 @@ try_start: static void socket_input_handler(evutil_socket_t fd, short what, void *arg) { - if (!(what & EV_READ)) + if (!(what & EV_READ)) { return; + } if (!arg) { read_spare_buffer(fd); @@ -2613,10 +2710,11 @@ static void socket_input_handler(evutil_socket_t fd, short what, void *arg) { return; } - if (!ioa_socket_tobeclosed(s)) + if (!ioa_socket_tobeclosed(s)) { socket_input_worker(s); - else + } else { read_spare_buffer(fd); + } if ((s->magic != SOCKET_MAGIC) || (s->done)) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "!!!%s (1) on socket, ev=%d: 0x%lx, st=%d, sat=%d\n", __FUNCTION__, (int)what, @@ -2677,8 +2775,9 @@ static void socket_output_handler_bev(struct bufferevent *bev, void *arg) { ioa_socket_handle s = (ioa_socket_handle)arg; - if (s->in_write) + if (s->in_write) { return; + } if ((s->magic != SOCKET_MAGIC) || (s->done) || (bev != s->bev)) { return; @@ -2757,8 +2856,9 @@ static void socket_input_handler_bev(struct bufferevent *bev, void *arg) { read_spare_buffer_bev(s->bev); break; } - if (socket_input_worker(s) <= 0) + if (socket_input_worker(s) <= 0) { break; + } } while ((cycle++ < 128) && (s->bev)); } @@ -2802,8 +2902,9 @@ static void eventcb_bev(struct bufferevent *bev, short events, void *arg) { return; } - if (events & BEV_EVENT_ERROR) + if (events & BEV_EVENT_ERROR) { s->broken = 1; + } s->tobeclosed = 1; @@ -2841,9 +2942,10 @@ static void eventcb_bev(struct bufferevent *bev, short events, void *arg) { char sraddr[129] = "\0"; addr_to_string(&(s->remote_addr), (uint8_t *)sraddr); if (events & BEV_EVENT_EOF) { - if (server->verbose) + if (server->verbose) { TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "session %018llu: %s socket closed remotely %s\n", (unsigned long long)(ss->id), socket_type_name(s->st), sraddr); + } if (s == ss->client_socket) { char msg[256]; snprintf(msg, sizeof(msg) - 1, "%s connection closed by client (callback)", socket_type_name(s->st)); @@ -2888,8 +2990,9 @@ static void eventcb_bev(struct bufferevent *bev, short events, void *arg) { static int ssl_send(ioa_socket_handle s, const char *buffer, int len, int verbose) { - if (!s || !(s->ssl) || !buffer || (s->fd < 0)) + if (!s || !(s->ssl) || !buffer || (s->fd < 0)) { return -1; + } SSL *ssl = s->ssl; @@ -2900,8 +3003,9 @@ static int ssl_send(ioa_socket_handle s, const char *buffer, int len, int verbos if (s->parent_s) { /* Trick only for "children" sockets: */ BIO *wbio = SSL_get_wbio(ssl); - if (!wbio) + if (!wbio) { return -1; + } int fd = BIO_get_fd(wbio, 0); int sfd = s->parent_s->fd; if (sfd >= 0) { @@ -2911,8 +3015,9 @@ static int ssl_send(ioa_socket_handle s, const char *buffer, int len, int verbos } } else { BIO *wbio = SSL_get_wbio(ssl); - if (!wbio) + if (!wbio) { return -1; + } int fd = BIO_get_fd(wbio, 0); if (fd != s->fd) { BIO_set_fd(wbio, s->fd, BIO_NOCLOSE); @@ -3022,8 +3127,9 @@ static int send_ssl_backlog_buffers(ioa_socket_handle s) { while (buf_elem) { int rc = ssl_send(s, (char *)buf_elem->buf.buf + buf_elem->buf.offset - buf_elem->buf.coffset, (size_t)buf_elem->buf.len, (s->e ? s->e->verbose : TURN_VERBOSE_NONE)); - if (rc < 1) + if (rc < 1) { break; + } ++ret; pop_elem_from_buffer_list(&(s->bufs)); buf_elem = s->bufs.head; @@ -3046,13 +3152,15 @@ int udp_send(ioa_socket_handle s, const ioa_addr *dest_addr, const char *buffer, int rc = 0; evutil_socket_t fd = -1; - if (!s) + if (!s) { return -1; + } - if (s->parent_s) + if (s->parent_s) { fd = s->parent_s->fd; - else + } else { fd = s->fd; + } if (fd >= 0) { @@ -3121,8 +3229,9 @@ int send_data_from_ioa_socket_nbh(ioa_socket_handle s, ioa_addr *dest_addr, ioa_ if (!ioa_socket_check_bandwidth(s, nbh, 0)) { /* Bandwidth exhausted, we pretend everything is fine: */ ret = (int)(ioa_network_buffer_get_size(nbh)); - if (skip) + if (skip) { *skip = 1; + } } else { if (!ioa_socket_tobeclosed(s) && s->e) { @@ -3169,11 +3278,12 @@ int send_data_from_ioa_socket_nbh(ioa_socket_handle s, ioa_addr *dest_addr, ioa_ send_ssl_backlog_buffers(s); ret = ssl_send(s, (char *)ioa_network_buffer_data(nbh), ioa_network_buffer_get_size(nbh), (s->e ? s->e->verbose : TURN_VERBOSE_NONE)); - if (ret < 0) + if (ret < 0) { s->tobeclosed = 1; - else if (ret == 0) + } else if (ret == 0) { add_buffer_to_buffer_list(&(s->bufs), (char *)ioa_network_buffer_data(nbh), ioa_network_buffer_get_size(nbh)); + } } else if (s->fd >= 0) { if (s->connected && !(s->parent_s)) { @@ -3274,8 +3384,9 @@ int register_callback_on_ioa_socket(ioa_engine_handle e, ioa_socket_handle s, in if (event_type & IOA_EV_READ) { - if (e) + if (e) { s->e = e; + } if (s->e && !(s->parent_s)) { @@ -3407,8 +3518,9 @@ int ioa_socket_tobeclosed(ioa_socket_handle s) { } void set_ioa_socket_tobeclosed(ioa_socket_handle s) { - if (s) + if (s) { s->tobeclosed = 1; + } } /* @@ -3431,18 +3543,18 @@ uint8_t *ioa_network_buffer_data(ioa_network_buffer_handle nbh) { } size_t ioa_network_buffer_get_size(ioa_network_buffer_handle nbh) { - if (!nbh) + if (!nbh) { return 0; - else { + } else { stun_buffer_list_elem *buf_elem = (stun_buffer_list_elem *)nbh; return (size_t)(buf_elem->buf.len); } } size_t ioa_network_buffer_get_capacity(ioa_network_buffer_handle nbh) { - if (!nbh) + if (!nbh) { return 0; - else { + } else { stun_buffer_list_elem *buf_elem = (stun_buffer_list_elem *)nbh; if (buf_elem->buf.offset < STUN_BUFFER_SIZE) { return (STUN_BUFFER_SIZE - buf_elem->buf.offset); @@ -3530,8 +3642,9 @@ void turn_report_allocation_set(void *a, turn_time_t lifetime, int refresh) { ts_ur_super_session *ss = (ts_ur_super_session *)(((allocation *)a)->owner); if (ss) { const char *status = "new"; - if (refresh) + if (refresh) { status = "refreshed"; + } turn_turnserver *server = (turn_turnserver *)ss->server; if (server) { ioa_engine_handle e = turn_server_get_engine(server); @@ -3573,8 +3686,9 @@ void turn_report_allocation_set(void *a, turn_time_t lifetime, int refresh) { } #endif { - if (!refresh) + if (!refresh) { prom_inc_allocation(get_ioa_socket_type(ss->client_socket)); + } } } } @@ -3742,14 +3856,16 @@ void turn_report_session_usage(void *session, int force_invalid) { /////////////// SSL /////////////////// const char *get_ioa_socket_tls_cipher(ioa_socket_handle s) { - if (s && (s->ssl)) + if (s && (s->ssl)) { return SSL_get_cipher(s->ssl); + } return ""; } const char *get_ioa_socket_tls_method(ioa_socket_handle s) { - if (s && (s->ssl)) + if (s && (s->ssl)) { return turn_get_ssl_method(s->ssl, "UNKNOWN"); + } return ""; } @@ -3779,8 +3895,9 @@ static void init_super_memory_region(super_memory_t *r) { r->sm_total_sz = TURN_SM_SIZE; r->sm_chunk = 0; - while (r->id == 0) + while (r->id == 0) { r->id = (uint32_t)turn_random(); + } TURN_MUTEX_INIT(&r->mutex_sm); } @@ -3868,8 +3985,9 @@ void *allocate_super_memory_region_func(super_memory_t *r, size_t size, const ch void *allocate_super_memory_engine_func(ioa_engine_handle e, size_t size, const char *file, const char *func, int line) { - if (e) + if (e) { return allocate_super_memory_region_func(e->sm, size, file, func, line); + } return allocate_super_memory_region_func(NULL, size, file, func, line); } diff --git a/src/apps/relay/tls_listener.c b/src/apps/relay/tls_listener.c index 437c4cd..326775a 100644 --- a/src/apps/relay/tls_listener.c +++ b/src/apps/relay/tls_listener.c @@ -85,12 +85,13 @@ static void server_input_handler(struct evconnlistener *l, evutil_socket_t fd, s SOCKET_TYPE st = TENTATIVE_TCP_SOCKET; - if (turn_params.tcp_use_proxy) + if (turn_params.tcp_use_proxy) { st = TCP_SOCKET_PROXY; - else if (turn_params.no_tls) + } else if (turn_params.no_tls) { st = TCP_SOCKET; - else if (turn_params.no_tcp) + } else if (turn_params.no_tcp) { st = TLS_SOCKET; + } ioa_socket_handle ioas = create_ioa_socket_from_fd(server->e, fd, NULL, st, CLIENT_SOCKET, &(server->sm.m.sm.nd.src_addr), &(server->addr)); @@ -143,10 +144,11 @@ static void sctp_server_input_handler(struct evconnlistener *l, evutil_socket_t SOCKET_TYPE st = TENTATIVE_SCTP_SOCKET; - if (turn_params.no_tls) + if (turn_params.no_tls) { st = SCTP_SOCKET; - else if (turn_params.no_tcp) + } else if (turn_params.no_tcp) { st = TLS_SCTP_SOCKET; + } ioa_socket_handle ioas = create_ioa_socket_from_fd(server->e, fd, NULL, st, CLIENT_SOCKET, &(server->sm.m.sm.nd.src_addr), &(server->addr)); @@ -181,8 +183,9 @@ static int create_server_listener(tls_listener_relay_server_type *server) { FUNCSTART; - if (!server) + if (!server) { return -1; + } evutil_socket_t tls_listen_fd = -1; @@ -230,12 +233,13 @@ static int create_server_listener(tls_listener_relay_server_type *server) { return -1; } - if (!turn_params.no_tcp && !turn_params.no_tls) + if (!turn_params.no_tcp && !turn_params.no_tls) { addr_debug_print(server->verbose, &server->addr, "TLS/TCP listener opened on "); - else if (!turn_params.no_tls) + } else if (!turn_params.no_tls) { addr_debug_print(server->verbose, &server->addr, "TLS listener opened on "); - else if (!turn_params.no_tcp) + } else if (!turn_params.no_tcp) { addr_debug_print(server->verbose, &server->addr, "TCP listener opened on "); + } FUNCEND; @@ -248,8 +252,9 @@ static int sctp_create_server_listener(tls_listener_relay_server_type *server) { FUNCSTART; - if (!server) + if (!server) { return -1; + } evutil_socket_t tls_listen_fd = -1; @@ -280,10 +285,11 @@ static int sctp_create_server_listener(tls_listener_relay_server_type *server) { return -1; } - if (!turn_params.no_tls) + if (!turn_params.no_tls) { addr_debug_print(server->verbose, &server->addr, "TLS/SCTP listener opened on "); - else + } else { addr_debug_print(server->verbose, &server->addr, "SCTP listener opened on "); + } FUNCEND; @@ -296,14 +302,16 @@ static int init_server(tls_listener_relay_server_type *server, const char *ifnam int verbose, ioa_engine_handle e, ioa_engine_new_connection_event_handler send_socket, struct relay_server *relay_server) { - if (!server) + if (!server) { return -1; + } server->connect_cb = send_socket; server->relay_server = relay_server; - if (ifname) + if (ifname) { STRCPY(server->ifname, ifname); + } if (make_ioa_addr((const uint8_t *)local_address, port, &server->addr) < 0) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot create a TCP/TLS listener for address: %s\n", local_address); diff --git a/src/apps/relay/turn_admin_server.c b/src/apps/relay/turn_admin_server.c index 9967c73..9d0a5f8 100644 --- a/src/apps/relay/turn_admin_server.c +++ b/src/apps/relay/turn_admin_server.c @@ -223,16 +223,18 @@ static void print_str_array(struct cli_session *cs, const char **sa) { } static const char *get_flag(int val) { - if (val) + if (val) { return "ON"; + } return "OFF"; } static void cli_print_flag(struct cli_session *cs, int flag, const char *name, int changeable) { if (cs && cs->ts && name) { const char *sc = ""; - if (changeable) + if (changeable) { sc = " (*)"; + } myprintf(cs, " %s: %s%s\n", name, get_flag(flag), sc); } } @@ -240,23 +242,26 @@ static void cli_print_flag(struct cli_session *cs, int flag, const char *name, i static void cli_print_uint(struct cli_session *cs, unsigned long value, const char *name, int changeable) { if (cs && cs->ts && name) { const char *sc = ""; - if (changeable == 1) + if (changeable == 1) { sc = " (*)"; - else if (changeable == 2) + } else if (changeable == 2) { sc = " (**)"; + } myprintf(cs, " %s: %lu%s\n", name, value, sc); } } static void cli_print_str(struct cli_session *cs, const char *value, const char *name, int changeable) { if (cs && cs->ts && name && value) { - if (value[0] == 0) + if (value[0] == 0) { value = "empty"; + } const char *sc = ""; - if (changeable == 1) + if (changeable == 1) { sc = " (*)"; - else if (changeable == 2) + } else if (changeable == 2) { sc = " (**)"; + } myprintf(cs, " %s: %s%s\n", name, value, sc); } } @@ -264,15 +269,17 @@ static void cli_print_str(struct cli_session *cs, const char *value, const char static void cli_print_addr(struct cli_session *cs, ioa_addr *value, int use_port, const char *name, int changeable) { if (cs && cs->ts && name && value) { const char *sc = ""; - if (changeable == 1) + if (changeable == 1) { sc = " (*)"; - else if (changeable == 2) + } else if (changeable == 2) { sc = " (**)"; + } char s[256]; - if (!use_port) + if (!use_port) { addr_to_string_no_port(value, (uint8_t *)s); - else + } else { addr_to_string(value, (uint8_t *)s); + } myprintf(cs, " %s: %s%s\n", name, s, sc); } } @@ -281,17 +288,19 @@ static void cli_print_addr_list(struct cli_session *cs, turn_server_addrs_list_t int changeable) { if (cs && cs->ts && name && value && value->size && value->addrs) { const char *sc = ""; - if (changeable == 1) + if (changeable == 1) { sc = " (*)"; - else if (changeable == 2) + } else if (changeable == 2) { sc = " (**)"; + } char s[256]; size_t i; for (i = 0; i < value->size; i++) { - if (!use_port) + if (!use_port) { addr_to_string_no_port(&(value->addrs[i]), (uint8_t *)s); - else + } else { addr_to_string(&(value->addrs[i]), (uint8_t *)s); + } myprintf(cs, " %s: %s%s\n", name, s, sc); } } @@ -300,14 +309,16 @@ static void cli_print_addr_list(struct cli_session *cs, turn_server_addrs_list_t static void cli_print_str_array(struct cli_session *cs, char **value, size_t sz, const char *name, int changeable) { if (cs && cs->ts && name && value && sz) { const char *sc = ""; - if (changeable == 1) + if (changeable == 1) { sc = " (*)"; - else if (changeable == 2) + } else if (changeable == 2) { sc = " (**)"; + } size_t i; for (i = 0; i < sz; i++) { - if (value[i]) + if (value[i]) { myprintf(cs, " %s: %s%s\n", name, value[i], sc); + } } } } @@ -315,10 +326,11 @@ static void cli_print_str_array(struct cli_session *cs, char **value, size_t sz, static void cli_print_ip_range_list(struct cli_session *cs, ip_range_list_t *value, const char *name, int changeable) { if (cs && cs->ts && name && value && value->ranges_number && value->rs) { const char *sc = ""; - if (changeable == 1) + if (changeable == 1) { sc = " (*)"; - else if (changeable == 2) + } else if (changeable == 2) { sc = " (**)"; + } size_t i; for (i = 0; i < value->ranges_number; ++i) { if (value->rs[i].realm[0]) { @@ -414,28 +426,34 @@ static int print_session(ur_map_key_type key, ur_map_value_type value, void *arg struct cli_session *cs = csarg->cs; struct turn_session_info *tsi = (struct turn_session_info *)value; - if (cs->realm[0] && strcmp(cs->realm, tsi->realm)) + if (cs->realm[0] && strcmp(cs->realm, tsi->realm)) { return 0; + } - if (cs->origin[0] && strcmp(cs->origin, tsi->origin)) + if (cs->origin[0] && strcmp(cs->origin, tsi->origin)) { return 0; + } if (csarg->users) { const char *pn = csarg->pname; if (pn[0]) { if (!strcmp(pn, "TLS") || !strcmp(pn, "tls") || !strcmp(pn, "Tls")) { - if ((tsi->client_protocol != TLS_SOCKET) && (tsi->client_protocol != TLS_SCTP_SOCKET)) + if ((tsi->client_protocol != TLS_SOCKET) && (tsi->client_protocol != TLS_SCTP_SOCKET)) { return 0; + } } else if (!strcmp(pn, "DTLS") || !strcmp(pn, "dtls") || !strcmp(pn, "Dtls")) { - if (tsi->client_protocol != DTLS_SOCKET) + if (tsi->client_protocol != DTLS_SOCKET) { return 0; + } } else if (!strcmp(pn, "TCP") || !strcmp(pn, "tcp") || !strcmp(pn, "Tcp")) { - if ((tsi->client_protocol != TCP_SOCKET) && (tsi->client_protocol != SCTP_SOCKET)) + if ((tsi->client_protocol != TCP_SOCKET) && (tsi->client_protocol != SCTP_SOCKET)) { return 0; + } } else if (!strcmp(pn, "UDP") || !strcmp(pn, "udp") || !strcmp(pn, "Udp")) { - if (tsi->client_protocol != UDP_SOCKET) + if (tsi->client_protocol != UDP_SOCKET) { return 0; + } } else { return 0; } @@ -455,21 +473,25 @@ static int print_session(ur_map_key_type key, ur_map_value_type value, void *arg } else { if (csarg->username[0]) { if (csarg->exact_match) { - if (strcmp((char *)tsi->username, csarg->username)) + if (strcmp((char *)tsi->username, csarg->username)) { return 0; + } } else { - if (!strstr((char *)tsi->username, csarg->username)) + if (!strstr((char *)tsi->username, csarg->username)) { return 0; + } } } if (cs->f || (unsigned long)csarg->counter < (unsigned long)cli_max_output_sessions) { myprintf(cs, "\n"); myprintf(cs, " %lu) id=%018llu, user <%s>:\n", (unsigned long)(csarg->counter + 1), (unsigned long long)tsi->id, tsi->username); - if (tsi->realm[0]) + if (tsi->realm[0]) { myprintf(cs, " realm: %s\n", tsi->realm); - if (tsi->origin[0]) + } + if (tsi->origin[0]) { myprintf(cs, " origin: %s\n", tsi->origin); + } if (turn_time_before(csarg->ct, tsi->start_time)) { myprintf(cs, " started: undefined time\n"); } else { @@ -483,14 +505,18 @@ static int print_session(ur_map_key_type key, ur_map_value_type value, void *arg myprintf(cs, " client protocol %s, relay protocol %s\n", socket_type_name(tsi->client_protocol), socket_type_name(tsi->peer_protocol)); { - if (!tsi->local_addr_data.saddr[0]) + if (!tsi->local_addr_data.saddr[0]) { addr_to_string(&(tsi->local_addr_data.addr), (uint8_t *)tsi->local_addr_data.saddr); - if (!tsi->remote_addr_data.saddr[0]) + } + if (!tsi->remote_addr_data.saddr[0]) { addr_to_string(&(tsi->remote_addr_data.addr), (uint8_t *)tsi->remote_addr_data.saddr); - if (!tsi->relay_addr_data_ipv4.saddr[0]) + } + if (!tsi->relay_addr_data_ipv4.saddr[0]) { addr_to_string(&(tsi->relay_addr_data_ipv4.addr), (uint8_t *)tsi->relay_addr_data_ipv4.saddr); - if (!tsi->relay_addr_data_ipv6.saddr[0]) + } + if (!tsi->relay_addr_data_ipv6.saddr[0]) { addr_to_string(&(tsi->relay_addr_data_ipv6.addr), (uint8_t *)tsi->relay_addr_data_ipv6.saddr); + } myprintf(cs, " client addr %s, server addr %s\n", tsi->remote_addr_data.saddr, tsi->local_addr_data.saddr); if (tsi->relay_addr_data_ipv4.saddr[0]) { @@ -506,8 +532,9 @@ static int print_session(ur_map_key_type key, ur_map_value_type value, void *arg myprintf(cs, " TLS method: %s\n", tsi->tls_method); myprintf(cs, " TLS cipher: %s\n", tsi->tls_cipher); } - if (tsi->bps) + if (tsi->bps) { myprintf(cs, " Max throughput: %lu bytes per second\n", (unsigned long)tsi->bps); + } myprintf(cs, " usage: rp=%lu, rb=%lu, sp=%lu, sb=%lu\n", (unsigned long)(tsi->received_packets), (unsigned long)(tsi->received_bytes), (unsigned long)(tsi->sent_packets), (unsigned long)(tsi->sent_bytes)); @@ -517,14 +544,16 @@ static int print_session(ur_map_key_type key, ur_map_value_type value, void *arg myprintf(cs, " peers:\n"); size_t i; for (i = 0; i < tsi->main_peers_size; ++i) { - if (!(tsi->main_peers_data[i].saddr[0])) + if (!(tsi->main_peers_data[i].saddr[0])) { addr_to_string(&(tsi->main_peers_data[i].addr), (uint8_t *)tsi->main_peers_data[i].saddr); + } myprintf(cs, " %s\n", tsi->main_peers_data[i].saddr); } if (tsi->extra_peers_size && tsi->extra_peers_data) { for (i = 0; i < tsi->extra_peers_size; ++i) { - if (!(tsi->extra_peers_data[i].saddr[0])) + if (!(tsi->extra_peers_data[i].saddr[0])) { addr_to_string(&(tsi->extra_peers_data[i].addr), (uint8_t *)tsi->extra_peers_data[i].saddr); + } myprintf(cs, " %s\n", tsi->extra_peers_data[i].saddr); } } @@ -547,10 +576,12 @@ static void cancel_session(struct cli_session *cs, const char *ssid) { static void print_sessions(struct cli_session *cs, const char *pn, int exact_match, int print_users) { if (cs && cs->ts && pn) { - while (pn[0] == ' ') + while (pn[0] == ' ') { ++pn; - if (pn[0] == '*') + } + if (pn[0] == '*') { ++pn; + } const char *uname = ""; if (!print_users) { @@ -590,11 +621,13 @@ static void print_sessions(struct cli_session *cs, const char *pn, int exact_mat snprintf(ts, sizeof(ts), " Total sessions"); if (cs->realm[0]) { snprintf(ts + strlen(ts), sizeof(ts) - strlen(ts), " for realm %s", cs->realm); - if (cs->origin[0]) + if (cs->origin[0]) { snprintf(ts + strlen(ts), sizeof(ts) - strlen(ts), " and for origin %s", cs->origin); + } } else { - if (cs->origin[0]) + if (cs->origin[0]) { snprintf(ts + strlen(ts), sizeof(ts) - strlen(ts), " for origin %s", cs->origin); + } } snprintf(ts + strlen(ts), sizeof(ts) - strlen(ts), ": %lu", (unsigned long)arg.counter); myprintf(cs, "%s\n", ts); @@ -610,18 +643,21 @@ static void print_sessions(struct cli_session *cs, const char *pn, int exact_mat } } - if (arg.user_counters) + if (arg.user_counters) { free(arg.user_counters); + } if (arg.user_names) { size_t i; for (i = 0; i < arg.users_number; ++i) { - if (arg.user_names[i]) + if (arg.user_names[i]) { free(arg.user_names[i]); + } } free(arg.user_names); } - if (arg.users) + if (arg.users) { ur_string_map_free(&arg.users); + } } } @@ -659,21 +695,23 @@ static void cli_print_configuration(struct cli_session *cs) { myprintf(cs, "\n"); - if (turn_params.cipher_list[0]) + if (turn_params.cipher_list[0]) { cli_print_str(cs, turn_params.cipher_list, "cipher-list", 0); - else + } else { cli_print_str(cs, DEFAULT_CIPHER_LIST, "cipher-list", 0); + } cli_print_str(cs, turn_params.ec_curve_name, "ec-curve-name", 0); { - if (turn_params.dh_key_size == DH_CUSTOM) + if (turn_params.dh_key_size == DH_CUSTOM) { cli_print_str(cs, turn_params.dh_file, "dh-file", 0); - else { + } else { unsigned int dh_key_length = 1066; - if (turn_params.dh_key_size == DH_566) + if (turn_params.dh_key_size == DH_566) { dh_key_length = 566; - else if (turn_params.dh_key_size == DH_2066) + } else if (turn_params.dh_key_size == DH_2066) { dh_key_length = 2066; + } cli_print_uint(cs, (unsigned long)dh_key_length, "DH-key-length", 0); } } @@ -684,8 +722,9 @@ static void cli_print_configuration(struct cli_session *cs) { cli_print_str_array(cs, turn_params.listener.addrs, turn_params.listener.addrs_number, "Listener addr", 0); - if (turn_params.listener_ifname[0]) + if (turn_params.listener_ifname[0]) { cli_print_str(cs, turn_params.listener_ifname, "listener-ifname", 0); + } cli_print_flag(cs, turn_params.no_udp, "no-udp", 0); cli_print_flag(cs, turn_params.no_tcp, "no-tcp", 0); @@ -713,8 +752,9 @@ static void cli_print_configuration(struct cli_session *cs) { cli_print_str_array(cs, turn_params.relay_addrs, turn_params.relays_number, "Relay addr", 0); - if (turn_params.relay_ifname[0]) + if (turn_params.relay_ifname[0]) { cli_print_str(cs, turn_params.relay_ifname, "relay-ifname", 0); + } cli_print_flag(cs, turn_params.server_relay, "server-relay", 0); @@ -752,30 +792,36 @@ static void cli_print_configuration(struct cli_session *cs) { } #if !defined(TURN_NO_HIREDIS) - if (turn_params.use_redis_statsdb && turn_params.redis_statsdb.connection_string[0]) + if (turn_params.use_redis_statsdb && turn_params.redis_statsdb.connection_string[0]) { cli_print_str(cs, turn_params.redis_statsdb.connection_string, "Redis Statistics DB", 0); + } #endif myprintf(cs, "\n"); { char *rn = get_realm(NULL)->options.name; - if (rn[0]) + if (rn[0]) { cli_print_str(cs, rn, "Default realm", 0); + } } - if (cs->realm[0]) + if (cs->realm[0]) { cli_print_str(cs, cs->realm, "CLI session realm", 0); - else + } else { cli_print_str(cs, get_realm(NULL)->options.name, "CLI session realm", 0); - if (cs->origin[0]) + } + if (cs->origin[0]) { cli_print_str(cs, cs->origin, "CLI session origin", 0); - if (turn_params.ct == TURN_CREDENTIALS_LONG_TERM) + } + if (turn_params.ct == TURN_CREDENTIALS_LONG_TERM) { cli_print_flag(cs, 1, "Long-term authorization mechanism", 0); - else + } else { cli_print_flag(cs, 1, "Anonymous credentials", 0); + } cli_print_flag(cs, turn_params.use_auth_secret_with_timestamp, "TURN REST API support", 0); - if (turn_params.use_auth_secret_with_timestamp && turn_params.rest_api_separator) + if (turn_params.use_auth_secret_with_timestamp && turn_params.rest_api_separator) { cli_print_uint(cs, turn_params.rest_api_separator, "TURN REST API separator ASCII number", 0); + } myprintf(cs, "\n"); @@ -885,8 +931,9 @@ static int run_cli_input(struct cli_session *cs, const char *buf0, unsigned int char *cmd = buf; - while ((cmd[0] == ' ') || (cmd[0] == '\t')) + while ((cmd[0] == ' ') || (cmd[0] == '\t')) { ++cmd; + } size_t sl = strlen(cmd); @@ -962,8 +1009,9 @@ static int run_cli_input(struct cli_session *cs, const char *buf0, unsigned int type_cli_cursor(cs); } else if (strstr(cmd, "psd") == cmd) { cmd += 3; - while (cmd[0] == ' ') + while (cmd[0] == ' ') { ++cmd; + } if (!(cmd[0])) { const char *str = "You have to provide file name for ps dump\n"; myprintf(cs, "%s\n", str); @@ -1032,8 +1080,9 @@ static void cli_socket_input_handler_bev(struct bufferevent *bev, void *arg) { struct cli_session *cs = (struct cli_session *)arg; - if (!(cs->ts)) + if (!(cs->ts)) { return; + } stun_buffer buf; @@ -1535,10 +1584,10 @@ static char *current_realm(void) { static char *current_eff_realm(void) { char *r = current_realm(); - if (r && r[0]) + if (r && r[0]) { return r; - else if (current_socket && current_socket->special_session && - ((struct admin_session *)current_socket->special_session)->as_ok) { + } else if (current_socket && current_socket->special_session && + ((struct admin_session *)current_socket->special_session)->as_ok) { return ((struct admin_session *)current_socket->special_session)->as_eff_realm; } else { static char bad_eff_realm[1025] = "_ERROR:UNKNOWN_REALM__"; @@ -1613,8 +1662,9 @@ static AS_FORM get_form(const char *path) { if (path) { size_t i = 0; while (form_names[i].name) { - if (!strcmp(form_names[i].name, path)) + if (!strcmp(form_names[i].name, path)) { return form_names[i].form; + } ++i; } } @@ -1765,8 +1815,9 @@ static void sbprintf(struct str_buffer *sb, const char *format, ...) { static void https_print_flag(struct str_buffer *sb, int flag, const char *name, const char *param_name) { if (sb && name) { - if (!is_superuser()) + if (!is_superuser()) { param_name = 0; + } if (!param_name) { sbprintf(sb, "%s%s\r\n", name, get_flag(flag)); } else { @@ -1778,8 +1829,9 @@ static void https_print_flag(struct str_buffer *sb, int flag, const char *name, static void https_print_uint(struct str_buffer *sb, unsigned long value, const char *name, const char *param_name) { if (sb && name) { - if (!is_superuser()) + if (!is_superuser()) { param_name = 0; + } if (!param_name) { if (value) { sbprintf(sb, "%s%lu\r\n", name, value); @@ -1804,8 +1856,9 @@ static void https_print_uint(struct str_buffer *sb, unsigned long value, const c static void https_print_str(struct str_buffer *sb, const char *value, const char *name, const char *param_name) { if (sb && name && value) { - if (!is_superuser()) + if (!is_superuser()) { param_name = 0; + } if (!param_name) { sbprintf(sb, "%s%s\r\n", name, value); } else { @@ -1831,10 +1884,11 @@ static void https_print_str_array(struct str_buffer *sb, char **value, size_t sz static void https_print_addr(struct str_buffer *sb, ioa_addr *value, int use_port, const char *name) { if (sb && name && value) { char s[256]; - if (!use_port) + if (!use_port) { addr_to_string_no_port(value, (uint8_t *)s); - else + } else { addr_to_string(value, (uint8_t *)s); + } sbprintf(sb, " %s %s\r\n", name, s); } } @@ -1845,10 +1899,11 @@ static size_t https_print_addr_list(struct str_buffer *sb, turn_server_addrs_lis char s[256]; size_t i; for (i = 0; i < value->size; i++) { - if (!use_port) + if (!use_port) { addr_to_string_no_port(&(value->addrs[i]), (uint8_t *)s); - else + } else { addr_to_string(&(value->addrs[i]), (uint8_t *)s); + } sbprintf(sb, " %s %s\r\n", name, s); } return i; @@ -1864,8 +1919,9 @@ static const char *change_ip_addr_html(int dynamic, const char *kind, const char buffer[0] = 0; if (dynamic && kind && ip) { - if (!realm) + if (!realm) { realm = ""; + } if (current_realm()[0] && strcmp(current_realm(), realm)) { // delete forbidden @@ -1934,8 +1990,9 @@ static void toggle_param(const char *pn) { static void update_param(const char *pn, const char *value) { if (pn) { - if (!value) + if (!value) { value = "0"; + } if (is_superuser()) { if (strstr(pn, "total-quota") == pn) { turn_params.total_quota = atoi(value); @@ -1949,8 +2006,9 @@ static void update_param(const char *pn, const char *value) { } { realm_params_t *rp = get_realm(current_eff_realm()); - if (!rp) + if (!rp) { rp = get_realm(NULL); + } const turn_dbdriver_t *dbd = get_dbdriver(); if (dbd && dbd->set_realm_option_one) { @@ -2020,21 +2078,23 @@ static void write_pc_page(ioa_socket_handle s) { https_print_empty_row(sb, 2); - if (turn_params.cipher_list[0]) + if (turn_params.cipher_list[0]) { https_print_str(sb, turn_params.cipher_list, "cipher-list", 0); - else + } else { https_print_str(sb, DEFAULT_CIPHER_LIST, "cipher-list", 0); + } https_print_str(sb, turn_params.ec_curve_name, "ec-curve-name", 0); { - if (turn_params.dh_key_size == DH_CUSTOM) + if (turn_params.dh_key_size == DH_CUSTOM) { https_print_str(sb, turn_params.dh_file, "dh-file", 0); - else { + } else { unsigned int dh_key_length = 1066; - if (turn_params.dh_key_size == DH_566) + if (turn_params.dh_key_size == DH_566) { dh_key_length = 566; - else if (turn_params.dh_key_size == DH_2066) + } else if (turn_params.dh_key_size == DH_2066) { dh_key_length = 2066; + } https_print_uint(sb, (unsigned long)dh_key_length, "DH-key-length", 0); } } @@ -2047,8 +2107,9 @@ static void write_pc_page(ioa_socket_handle s) { https_print_str_array(sb, turn_params.listener.addrs, turn_params.listener.addrs_number, "Listener addr"); - if (turn_params.listener_ifname[0]) + if (turn_params.listener_ifname[0]) { https_print_str(sb, turn_params.listener_ifname, "listener-ifname", 0); + } https_print_flag(sb, turn_params.no_udp, "no-udp", 0); https_print_flag(sb, turn_params.no_tcp, "no-tcp", 0); @@ -2080,8 +2141,9 @@ static void write_pc_page(ioa_socket_handle s) { https_print_str_array(sb, turn_params.relay_addrs, turn_params.relays_number, "Relay addr"); - if (turn_params.relay_ifname[0]) + if (turn_params.relay_ifname[0]) { https_print_str(sb, turn_params.relay_ifname, "relay-ifname", 0); + } https_print_flag(sb, turn_params.server_relay, "server-relay", 0); @@ -2116,10 +2178,11 @@ static void write_pc_page(ioa_socket_handle s) { https_print_empty_row(sb, 2); - if (turn_params.ct == TURN_CREDENTIALS_LONG_TERM) + if (turn_params.ct == TURN_CREDENTIALS_LONG_TERM) { https_print_flag(sb, 1, "Long-term authorization mechanism", 0); - else + } else { https_print_flag(sb, 1, "Anonymous credentials", 0); + } https_print_flag(sb, turn_params.use_auth_secret_with_timestamp, "TURN REST API support", 0); if (turn_params.use_auth_secret_with_timestamp) { @@ -2134,13 +2197,15 @@ static void write_pc_page(ioa_socket_handle s) { if (is_superuser()) { char *rn = get_realm(NULL)->options.name; - if (rn[0]) + if (rn[0]) { https_print_str(sb, rn, "Default realm", 0); + } } realm_params_t *rp = get_realm(current_eff_realm()); - if (!rp) + if (!rp) { rp = get_realm(NULL); + } https_print_str(sb, rp->options.name, "Admin session (current) realm", 0); @@ -2205,8 +2270,9 @@ static int https_print_session(ur_map_key_type key, ur_map_value_type value, voi struct str_buffer *sb = csarg->sb; struct turn_session_info *tsi = (struct turn_session_info *)value; - if (current_eff_realm()[0] && strcmp(current_eff_realm(), tsi->realm)) + if (current_eff_realm()[0] && strcmp(current_eff_realm(), tsi->realm)) { return 0; + } if (csarg->user_pattern[0]) { if (!strstr((char *)tsi->username, csarg->user_pattern)) { @@ -2222,17 +2288,21 @@ static int https_print_session(ur_map_key_type key, ur_map_value_type value, voi const char *pn = csarg->client_protocol; if (pn[0]) { if (!strcmp(pn, "TLS") || !strcmp(pn, "tls") || !strcmp(pn, "Tls")) { - if ((tsi->client_protocol != TLS_SOCKET) && (tsi->client_protocol != TLS_SCTP_SOCKET)) + if ((tsi->client_protocol != TLS_SOCKET) && (tsi->client_protocol != TLS_SCTP_SOCKET)) { return 0; + } } else if (!strcmp(pn, "DTLS") || !strcmp(pn, "dtls") || !strcmp(pn, "Dtls")) { - if (tsi->client_protocol != DTLS_SOCKET) + if (tsi->client_protocol != DTLS_SOCKET) { return 0; + } } else if (!strcmp(pn, "TCP") || !strcmp(pn, "tcp") || !strcmp(pn, "Tcp")) { - if ((tsi->client_protocol != TCP_SOCKET) && (tsi->client_protocol != SCTP_SOCKET)) + if ((tsi->client_protocol != TCP_SOCKET) && (tsi->client_protocol != SCTP_SOCKET)) { return 0; + } } else if (!strcmp(pn, "UDP") || !strcmp(pn, "udp") || !strcmp(pn, "Udp")) { - if (tsi->client_protocol != UDP_SOCKET) + if (tsi->client_protocol != UDP_SOCKET) { return 0; + } } else { return 0; } @@ -2273,14 +2343,18 @@ static int https_print_session(ur_map_key_type key, ur_map_value_type value, voi str_buffer_append(sb, socket_type_name(tsi->peer_protocol)); str_buffer_append(sb, ""); { - if (!tsi->local_addr_data.saddr[0]) + if (!tsi->local_addr_data.saddr[0]) { addr_to_string(&(tsi->local_addr_data.addr), (uint8_t *)tsi->local_addr_data.saddr); - if (!tsi->remote_addr_data.saddr[0]) + } + if (!tsi->remote_addr_data.saddr[0]) { addr_to_string(&(tsi->remote_addr_data.addr), (uint8_t *)tsi->remote_addr_data.saddr); - if (!tsi->relay_addr_data_ipv4.saddr[0]) + } + if (!tsi->relay_addr_data_ipv4.saddr[0]) { addr_to_string(&(tsi->relay_addr_data_ipv4.addr), (uint8_t *)tsi->relay_addr_data_ipv4.saddr); - if (!tsi->relay_addr_data_ipv6.saddr[0]) + } + if (!tsi->relay_addr_data_ipv6.saddr[0]) { addr_to_string(&(tsi->relay_addr_data_ipv6.addr), (uint8_t *)tsi->relay_addr_data_ipv6.saddr); + } str_buffer_append(sb, tsi->remote_addr_data.saddr); str_buffer_append(sb, ""); str_buffer_append(sb, tsi->local_addr_data.saddr); @@ -2319,16 +2393,18 @@ static int https_print_session(ur_map_key_type key, ur_map_value_type value, voi if (tsi->main_peers_size) { size_t i; for (i = 0; i < tsi->main_peers_size; ++i) { - if (!(tsi->main_peers_data[i].saddr[0])) + if (!(tsi->main_peers_data[i].saddr[0])) { addr_to_string(&(tsi->main_peers_data[i].addr), (uint8_t *)tsi->main_peers_data[i].saddr); + } str_buffer_append(sb, " "); str_buffer_append(sb, tsi->main_peers_data[i].saddr); str_buffer_append(sb, " "); } if (tsi->extra_peers_size && tsi->extra_peers_data) { for (i = 0; i < tsi->extra_peers_size; ++i) { - if (!(tsi->extra_peers_data[i].saddr[0])) + if (!(tsi->extra_peers_data[i].saddr[0])) { addr_to_string(&(tsi->extra_peers_data[i].addr), (uint8_t *)tsi->extra_peers_data[i].saddr); + } str_buffer_append(sb, " "); str_buffer_append(sb, tsi->extra_peers_data[i].saddr); str_buffer_append(sb, " "); @@ -2994,8 +3070,9 @@ static void write_https_oauth_page(ioa_socket_handle s, const char *add_kid, con str_buffer_append(sb, "
"); { - if (!add_kid) + if (!add_kid) { add_kid = ""; + } str_buffer_append(sb, "
KID (required):
"); { - if (!add_ts) + if (!add_ts) { add_ts = ""; + } str_buffer_append(sb, "
Timestamp, secs (optional):
"); { - if (!add_lt) + if (!add_lt) { add_lt = ""; + } str_buffer_append(sb, "
Lifetime, secs (optional):
"); { - if (!add_ikm) + if (!add_ikm) { add_ikm = ""; + } str_buffer_append(sb, "
Base64-encoded input keying material (required):