From f3b73f60d0eb10863aa2a1d00aa3fa53c551ccd8 Mon Sep 17 00:00:00 2001 From: Michael Jones Date: Sun, 26 May 2024 19:45:18 -0500 Subject: [PATCH] Change the various map functions to return bool instead of inconsistantly return 0, 1, or -1 (#1502) --- src/apps/relay/dbdrivers/dbd_pgsql.c | 3 +- src/apps/relay/ns_ioalib_engine_impl.c | 2 +- src/apps/relay/turn_admin_server.c | 42 +- src/server/ns_turn_allocation.c | 6 +- src/server/ns_turn_maps.c | 854 ++++++++++--------------- src/server/ns_turn_maps.h | 157 +++-- src/server/ns_turn_maps_rtcp.c | 59 +- src/server/ns_turn_maps_rtcp.h | 11 +- 8 files changed, 486 insertions(+), 648 deletions(-) diff --git a/src/apps/relay/dbdrivers/dbd_pgsql.c b/src/apps/relay/dbdrivers/dbd_pgsql.c index 2027d42..9a5a301 100644 --- a/src/apps/relay/dbdrivers/dbd_pgsql.c +++ b/src/apps/relay/dbdrivers/dbd_pgsql.c @@ -784,8 +784,7 @@ static void pgsql_reread_realms(secrets_list_t *realms_list) { if (rval) { get_realm(rval); ur_string_map_value_type value = strdup(rval); - int ret = ur_string_map_put(o_to_realm_new, (ur_string_map_key_type)oval, value); - if (ret == -1) { + if (!ur_string_map_put(o_to_realm_new, (ur_string_map_key_type)oval, value)) { free(value); } } diff --git a/src/apps/relay/ns_ioalib_engine_impl.c b/src/apps/relay/ns_ioalib_engine_impl.c index 65d2656..18a371a 100644 --- a/src/apps/relay/ns_ioalib_engine_impl.c +++ b/src/apps/relay/ns_ioalib_engine_impl.c @@ -1123,7 +1123,7 @@ int create_relay_ioa_sockets(ioa_engine_handle e, ioa_socket_handle client_s, in set_accept_cb(*rtp_s, acb, acbarg); if (rtcp_s && *rtcp_s && out_reservation_token && *out_reservation_token) { - if (rtcp_map_put(e->map_rtcp, *out_reservation_token, *rtcp_s) < 0) { + if (!rtcp_map_put(e->map_rtcp, *out_reservation_token, *rtcp_s)) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: cannot update RTCP map\n", __FUNCTION__); IOA_CLOSE_SOCKET(*rtp_s); if (rtcp_s) { diff --git a/src/apps/relay/turn_admin_server.c b/src/apps/relay/turn_admin_server.c index 9d0a5f8..4a81f05 100644 --- a/src/apps/relay/turn_admin_server.c +++ b/src/apps/relay/turn_admin_server.c @@ -420,18 +420,18 @@ struct ps_arg { size_t users_number; }; -static int print_session(ur_map_key_type key, ur_map_value_type value, void *arg) { +static bool print_session(ur_map_key_type key, ur_map_value_type value, void *arg) { if (key && value && arg) { struct ps_arg *csarg = (struct ps_arg *)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)) { - return 0; + return false; } if (cs->origin[0] && strcmp(cs->origin, tsi->origin)) { - return 0; + return false; } if (csarg->users) { @@ -440,22 +440,22 @@ static int print_session(ur_map_key_type key, ur_map_value_type value, void *arg 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)) { - return 0; + return false; } } else if (!strcmp(pn, "DTLS") || !strcmp(pn, "dtls") || !strcmp(pn, "Dtls")) { if (tsi->client_protocol != DTLS_SOCKET) { - return 0; + return false; } } else if (!strcmp(pn, "TCP") || !strcmp(pn, "tcp") || !strcmp(pn, "Tcp")) { if ((tsi->client_protocol != TCP_SOCKET) && (tsi->client_protocol != SCTP_SOCKET)) { - return 0; + return false; } } else if (!strcmp(pn, "UDP") || !strcmp(pn, "udp") || !strcmp(pn, "Udp")) { if (tsi->client_protocol != UDP_SOCKET) { - return 0; + return false; } } else { - return 0; + return false; } } @@ -474,11 +474,11 @@ static int print_session(ur_map_key_type key, ur_map_value_type value, void *arg if (csarg->username[0]) { if (csarg->exact_match) { if (strcmp((char *)tsi->username, csarg->username)) { - return 0; + return false; } } else { if (!strstr((char *)tsi->username, csarg->username)) { - return 0; + return false; } } } @@ -563,7 +563,7 @@ static int print_session(ur_map_key_type key, ur_map_value_type value, void *arg csarg->counter += 1; } - return 0; + return false; } static void cancel_session(struct cli_session *cs, const char *ssid) { @@ -2264,24 +2264,24 @@ struct https_ps_arg { turnsession_id cs; }; -static int https_print_session(ur_map_key_type key, ur_map_value_type value, void *arg) { +static bool https_print_session(ur_map_key_type key, ur_map_value_type value, void *arg) { if (key && value && arg) { struct https_ps_arg *csarg = (struct https_ps_arg *)arg; 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)) { - return 0; + return false; } if (csarg->user_pattern[0]) { if (!strstr((char *)tsi->username, csarg->user_pattern)) { - return 0; + return false; } } if (csarg->cs == tsi->id) { - return 0; + return false; } { @@ -2289,22 +2289,22 @@ static int https_print_session(ur_map_key_type key, ur_map_value_type value, voi 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)) { - return 0; + return false; } } else if (!strcmp(pn, "DTLS") || !strcmp(pn, "dtls") || !strcmp(pn, "Dtls")) { if (tsi->client_protocol != DTLS_SOCKET) { - return 0; + return false; } } else if (!strcmp(pn, "TCP") || !strcmp(pn, "tcp") || !strcmp(pn, "Tcp")) { if ((tsi->client_protocol != TCP_SOCKET) && (tsi->client_protocol != SCTP_SOCKET)) { - return 0; + return false; } } else if (!strcmp(pn, "UDP") || !strcmp(pn, "udp") || !strcmp(pn, "Udp")) { if (tsi->client_protocol != UDP_SOCKET) { - return 0; + return false; } } else { - return 0; + return false; } } } @@ -2417,7 +2417,7 @@ static int https_print_session(ur_map_key_type key, ur_map_value_type value, voi csarg->counter += 1; } - return 0; + return false; } static size_t https_print_sessions(struct str_buffer *sb, const char *client_protocol, const char *user_pattern, diff --git a/src/server/ns_turn_allocation.c b/src/server/ns_turn_allocation.c index dc7e9e7..adf7141 100644 --- a/src/server/ns_turn_allocation.c +++ b/src/server/ns_turn_allocation.c @@ -170,7 +170,7 @@ turn_permission_info *allocation_get_permission(allocation *a, const ioa_addr *a ///////////////////////////// TURN_PERMISSION ///////////////////////////////// -static int delete_channel_info_from_allocation_map(ur_map_key_type key, ur_map_value_type value); +static bool delete_channel_info_from_allocation_map(ur_map_key_type key, ur_map_value_type value); void turn_permission_clean(turn_permission_info *tinfo) { if (tinfo && tinfo->allocated) { @@ -279,7 +279,7 @@ static void ch_info_clean(ch_info *c) { } } -static int delete_channel_info_from_allocation_map(ur_map_key_type key, ur_map_value_type value) { +static bool delete_channel_info_from_allocation_map(ur_map_key_type key, ur_map_value_type value) { UNUSED_ARG(key); if (value) { @@ -292,7 +292,7 @@ static int delete_channel_info_from_allocation_map(ur_map_key_type key, ur_map_v ch_info_clean(chn); } - return 0; + return false; } void turn_channel_delete(ch_info *chn) { diff --git a/src/server/ns_turn_maps.c b/src/server/ns_turn_maps.c index af25aa5..19d13d2 100644 --- a/src/server/ns_turn_maps.c +++ b/src/server/ns_turn_maps.c @@ -44,128 +44,96 @@ struct _ur_map { TURN_MUTEX_DECLARE(mutex) }; -static int ur_map_init(ur_map *map) { +static bool ur_map_init(ur_map *map) { if (map) { map->h = kh_init(3); if (map->h) { map->magic = MAGIC_HASH; TURN_MUTEX_INIT_RECURSIVE(&(map->mutex)); - return 0; + return true; } } - return -1; + return false; } #define ur_map_valid(map) ((map) && ((map)->h) && ((map)->magic == MAGIC_HASH)) ur_map *ur_map_create(void) { ur_map *map = (ur_map *)malloc(sizeof(ur_map)); - if (ur_map_init(map) < 0) { + if (!ur_map_init(map)) { free(map); return NULL; } return map; } -/** - * @ret: - * 0 - success - * -1 - error - */ -int ur_map_put(ur_map *map, ur_map_key_type key, ur_map_value_type value) { +bool ur_map_put(ur_map *map, ur_map_key_type key, ur_map_value_type value) { if (!ur_map_valid(map)) { - return -1; - } else { - - int ret = 0; - khiter_t k; - - k = kh_get(3, map->h, key); - if (k != kh_end(map->h)) { - kh_del(3, map->h, k); - } - - k = kh_put(3, map->h, key, &ret); - - if (!ret) { - kh_del(3, map->h, k); - return -1; - } - - kh_value(map->h, k) = value; - - return 0; + return false; } + + khiter_t k = kh_get(3, map->h, key); + if (k != kh_end(map->h)) { + kh_del(3, map->h, k); + } + + int ret = 0; + k = kh_put(3, map->h, key, &ret); + + if (!ret) { + kh_del(3, map->h, k); + return false; + } + + kh_value(map->h, k) = value; + + return true; } -/** - * @ret: - * 1 - success - * 0 - not found - */ -int ur_map_get(const ur_map *map, ur_map_key_type key, ur_map_value_type *value) { +bool ur_map_get(const ur_map *map, ur_map_key_type key, ur_map_value_type *value) { if (!ur_map_valid(map)) { - return 0; - } else { - - khiter_t k; - - k = kh_get(3, map->h, key); - if ((k != kh_end(map->h)) && kh_exist(map->h, k)) { - if (value) { - *value = kh_value(map->h, k); - } - return 1; - } - - return 0; + return false; } + + khiter_t k = kh_get(3, map->h, key); + if ((k != kh_end(map->h)) && kh_exist(map->h, k)) { + if (value) { + *value = kh_value(map->h, k); + } + return true; + } + + return false; } -/** - * @ret: - * 1 - success - * 0 - not found - */ -int ur_map_del(ur_map *map, ur_map_key_type key, ur_map_del_func delfunc) { +bool ur_map_del(ur_map *map, ur_map_key_type key, ur_map_del_func delfunc) { if (!ur_map_valid(map)) { - return 0; - } else { - - khiter_t k; - - k = kh_get(3, map->h, key); - if ((k != kh_end(map->h)) && kh_exist(map->h, k)) { - if (delfunc) { - delfunc(kh_value(map->h, k)); - } - kh_del(3, map->h, k); - return 1; - } - - return 0; + return false; } + + khiter_t k = kh_get(3, map->h, key); + if ((k != kh_end(map->h)) && kh_exist(map->h, k)) { + if (delfunc) { + delfunc(kh_value(map->h, k)); + } + kh_del(3, map->h, k); + return true; + } + + return false; } -/** - * @ret: - * 1 - success - * 0 - not found - */ -int ur_map_exist(const ur_map *map, ur_map_key_type key) { +bool ur_map_exist(const ur_map *map, ur_map_key_type key) { if (!ur_map_valid(map)) { - return 0; - } else { - - khiter_t k; - - k = kh_get(3, map->h, key); - if ((k != kh_end(map->h)) && kh_exist(map->h, k)) { - return 1; - } - - return 0; + return false; } + + khiter_t k = kh_get(3, map->h, key); + if ((k != kh_end(map->h)) && kh_exist(map->h, k)) { + return true; + } + + return 0; } void ur_map_free(ur_map **map) { @@ -193,48 +161,46 @@ size_t ur_map_size(const ur_map *map) { } } -int ur_map_foreach(ur_map *map, foreachcb_type func) { +bool ur_map_foreach(ur_map *map, foreachcb_type func) { if (map && func && ur_map_valid(map)) { - khiter_t k; - for (k = kh_begin((*map)->h); k != kh_end(map->h); ++k) { + for (khiter_t k = kh_begin((*map)->h); k != kh_end(map->h); ++k) { if (kh_exist(map->h, k)) { if (func((ur_map_key_type)(kh_key(map->h, k)), (ur_map_value_type)(kh_value(map->h, k)))) { - return 1; + return true; } } } } - return 0; + return false; } -int ur_map_foreach_arg(const ur_map *map, foreachcb_arg_type func, void *arg) { +bool ur_map_foreach_arg(const ur_map *map, foreachcb_arg_type func, void *arg) { if (map && func && ur_map_valid(map)) { - khiter_t k; - for (k = kh_begin((*map)->h); k != kh_end(map->h); ++k) { + for (khiter_t k = kh_begin((*map)->h); k != kh_end(map->h); ++k) { if (kh_exist(map->h, k)) { if (func((ur_map_key_type)(kh_key(map->h, k)), (ur_map_value_type)(kh_value(map->h, k)), arg)) { - return 1; + return true; } } } } - return 0; + return false; } -int ur_map_lock(const ur_map *map) { +bool ur_map_lock(const ur_map *map) { if (ur_map_valid(map)) { TURN_MUTEX_LOCK((const turn_mutex *)&(map->mutex)); - return 0; + return true; } - return -1; + return false; } -int ur_map_unlock(const ur_map *map) { +bool ur_map_unlock(const ur_map *map) { if (ur_map_valid(map)) { TURN_MUTEX_UNLOCK((const turn_mutex *)&(map->mutex)); - return 0; + return true; } - return -1; + return false; } //////////////////// LOCAL MAPS //////////////////////////////////// @@ -245,199 +211,161 @@ void lm_map_init(lm_map *map) { } } -/** - * @ret: - * 0 - success - * -1 - error - */ +bool lm_map_put(lm_map *map, ur_map_key_type key, ur_map_value_type value) { + if (!map || !key || !value) { + return false; + } -int lm_map_put(lm_map *map, ur_map_key_type key, ur_map_value_type value) { - int ret = -1; - if (map && key && value) { + size_t index = (size_t)(key & (LM_MAP_HASH_SIZE - 1)); + lm_map_array *a = &(map->table[index]); - size_t index = (size_t)(key & (LM_MAP_HASH_SIZE - 1)); - lm_map_array *a = &(map->table[index]); + for (size_t i = 0; i < LM_MAP_ARRAY_SIZE; ++i) { + ur_map_key_type key0 = a->main_keys[i]; + ur_map_value_type value0 = a->main_values[i]; - size_t i; - - for (i = 0; i < LM_MAP_ARRAY_SIZE; ++i) { - - ur_map_key_type key0 = a->main_keys[i]; - ur_map_value_type value0 = a->main_values[i]; - - if (key0 == key) { - if (value0 == value) { - return 0; - } else { - return -1; - } - } - - if (!key0 || !value0) { - a->main_keys[i] = key; - a->main_values[i] = value; - return 0; - } + if (key0 == key) { + return value0 == value; } - size_t esz = a->extra_sz; - if (esz && a->extra_keys && a->extra_values) { - for (i = 0; i < esz; ++i) { - ur_map_key_type *keyp = a->extra_keys[i]; - ur_map_value_type *valuep = a->extra_values[i]; - if (keyp && valuep) { - if (!(*keyp) || !(*valuep)) { - *keyp = key; - *valuep = value; - return 0; - } - } else { - if (!(*keyp)) { - a->extra_keys[i] = (ur_map_key_type *)malloc(sizeof(ur_map_key_type)); - keyp = a->extra_keys[i]; - } - if (!(*valuep)) { - a->extra_values[i] = (ur_map_value_type *)malloc(sizeof(ur_map_value_type)); - valuep = a->extra_values[i]; - } + if (!key0 || !value0) { + a->main_keys[i] = key; + a->main_values[i] = value; + return true; + } + } + + size_t esz = a->extra_sz; + if (esz && a->extra_keys && a->extra_values) { + for (size_t i = 0; i < esz; ++i) { + ur_map_key_type *keyp = a->extra_keys[i]; + ur_map_value_type *valuep = a->extra_values[i]; + if (keyp && valuep) { + if (!(*keyp) || !(*valuep)) { *keyp = key; *valuep = value; - return 0; + return true; } + } else { + if (!(*keyp)) { + a->extra_keys[i] = (ur_map_key_type *)malloc(sizeof(ur_map_key_type)); + keyp = a->extra_keys[i]; + } + if (!(*valuep)) { + a->extra_values[i] = (ur_map_value_type *)malloc(sizeof(ur_map_value_type)); + valuep = a->extra_values[i]; + } + *keyp = key; + *valuep = value; + return false; } } - - size_t old_sz = esz; - size_t old_sz_mem = esz * sizeof(ur_map_key_type *); - a->extra_keys = (ur_map_key_type **)realloc(a->extra_keys, old_sz_mem + sizeof(ur_map_key_type *)); - a->extra_keys[old_sz] = (ur_map_key_type *)malloc(sizeof(ur_map_key_type)); - *(a->extra_keys[old_sz]) = key; - - old_sz_mem = esz * sizeof(ur_map_value_type *); - a->extra_values = (ur_map_value_type **)realloc(a->extra_values, old_sz_mem + sizeof(ur_map_value_type *)); - a->extra_values[old_sz] = (ur_map_value_type *)malloc(sizeof(ur_map_value_type)); - *(a->extra_values[old_sz]) = value; - - a->extra_sz += 1; - - return 0; } - return ret; + + size_t old_sz = esz; + size_t old_sz_mem = esz * sizeof(ur_map_key_type *); + a->extra_keys = (ur_map_key_type **)realloc(a->extra_keys, old_sz_mem + sizeof(ur_map_key_type *)); + a->extra_keys[old_sz] = (ur_map_key_type *)malloc(sizeof(ur_map_key_type)); + *(a->extra_keys[old_sz]) = key; + + old_sz_mem = esz * sizeof(ur_map_value_type *); + a->extra_values = (ur_map_value_type **)realloc(a->extra_values, old_sz_mem + sizeof(ur_map_value_type *)); + a->extra_values[old_sz] = (ur_map_value_type *)malloc(sizeof(ur_map_value_type)); + *(a->extra_values[old_sz]) = value; + + a->extra_sz += 1; + + return true; } -/** - * @ret: - * 1 - success - * 0 - not found - */ +bool lm_map_get(const lm_map *map, ur_map_key_type key, ur_map_value_type *value) { + if (!map || !key) { + return false; + } -int lm_map_get(const lm_map *map, ur_map_key_type key, ur_map_value_type *value) { - int ret = 0; - if (map && key) { - size_t index = (size_t)(key & (LM_MAP_HASH_SIZE - 1)); - const lm_map_array *a = &(map->table[index]); + size_t index = (size_t)(key & (LM_MAP_HASH_SIZE - 1)); + const lm_map_array *a = &(map->table[index]); - size_t i; - - for (i = 0; i < LM_MAP_ARRAY_SIZE; ++i) { - - ur_map_key_type key0 = a->main_keys[i]; - if ((key0 == key) && a->main_values[i]) { - if (value) { - *value = a->main_values[i]; - } - return 1; + for (size_t i = 0; i < LM_MAP_ARRAY_SIZE; ++i) { + ur_map_key_type key0 = a->main_keys[i]; + if ((key0 == key) && a->main_values[i]) { + if (value) { + *value = a->main_values[i]; } + return true; } + } - size_t esz = a->extra_sz; - if (esz && a->extra_keys && a->extra_values) { - for (i = 0; i < esz; ++i) { - ur_map_key_type *keyp = a->extra_keys[i]; - ur_map_value_type *valuep = a->extra_values[i]; - if (keyp && valuep) { - if (*keyp == key) { - if (value) { - *value = *valuep; - } - return 1; + size_t esz = a->extra_sz; + if (esz && a->extra_keys && a->extra_values) { + for (size_t i = 0; i < esz; ++i) { + ur_map_key_type *keyp = a->extra_keys[i]; + ur_map_value_type *valuep = a->extra_values[i]; + if (keyp && valuep) { + if (*keyp == key) { + if (value) { + *value = *valuep; } + return true; } } } } - return ret; + return false; } -/** - * @ret: - * 1 - success - * 0 - not found - */ -int lm_map_del(lm_map *map, ur_map_key_type key, ur_map_del_func delfunc) { - int ret = 0; +bool lm_map_del(lm_map *map, ur_map_key_type key, ur_map_del_func delfunc) { + if (!map || !key) { + return false; + } - if (map && key) { - size_t index = (size_t)(key & (LM_MAP_HASH_SIZE - 1)); - lm_map_array *a = &(map->table[index]); + size_t index = (size_t)(key & (LM_MAP_HASH_SIZE - 1)); + lm_map_array *a = &(map->table[index]); - size_t i; + for (size_t i = 0; i < LM_MAP_ARRAY_SIZE; ++i) { + ur_map_key_type key0 = a->main_keys[i]; - for (i = 0; i < LM_MAP_ARRAY_SIZE; ++i) { - - ur_map_key_type key0 = a->main_keys[i]; - - if ((key0 == key) && a->main_values[i]) { - if (delfunc) { - delfunc(a->main_values[i]); - } - a->main_keys[i] = 0; - a->main_values[i] = 0; - return 1; + if ((key0 == key) && a->main_values[i]) { + if (delfunc) { + delfunc(a->main_values[i]); } + a->main_keys[i] = 0; + a->main_values[i] = 0; + return true; } + } - size_t esz = a->extra_sz; - if (esz && a->extra_keys && a->extra_values) { - for (i = 0; i < esz; ++i) { - ur_map_key_type *keyp = a->extra_keys[i]; - ur_map_value_type *valuep = a->extra_values[i]; - if (keyp && valuep) { - if (*keyp == key) { - if (delfunc) { - delfunc(*valuep); - } - *keyp = 0; - *valuep = 0; - return 1; + size_t esz = a->extra_sz; + if (esz && a->extra_keys && a->extra_values) { + for (size_t i = 0; i < esz; ++i) { + ur_map_key_type *keyp = a->extra_keys[i]; + ur_map_value_type *valuep = a->extra_values[i]; + if (keyp && valuep) { + if (*keyp == key) { + if (delfunc) { + delfunc(*valuep); } + *keyp = 0; + *valuep = 0; + return true; } } } } - return ret; + return false; } -/** - * @ret: - * 1 - success - * 0 - not found - */ -int lm_map_exist(const lm_map *map, ur_map_key_type key) { return lm_map_get(map, key, NULL); } +bool lm_map_exist(const lm_map *map, ur_map_key_type key) { return lm_map_get(map, key, NULL); } void lm_map_clean(lm_map *map) { - size_t j; - for (j = 0; j < LM_MAP_HASH_SIZE; ++j) { - + for (size_t j = 0; j < LM_MAP_HASH_SIZE; ++j) { lm_map_array *a = &(map->table[j]); size_t esz = a->extra_sz; if (esz) { - size_t i; if (a->extra_keys) { - for (i = 0; i < esz; ++i) { + for (size_t i = 0; i < esz; ++i) { ur_map_key_type *keyp = a->extra_keys[i]; if (keyp) { *keyp = 0; @@ -448,7 +376,7 @@ void lm_map_clean(lm_map *map) { a->extra_keys = NULL; } if (a->extra_values) { - for (i = 0; i < esz; ++i) { + for (size_t i = 0; i < esz; ++i) { ur_map_value_type *valuep = a->extra_values[i]; if (valuep) { *valuep = 0; @@ -465,110 +393,94 @@ void lm_map_clean(lm_map *map) { } size_t lm_map_size(const lm_map *map) { + + if (!map) { + return 0; + } + size_t ret = 0; + for (size_t i = 0; i < LM_MAP_HASH_SIZE; ++i) { + const lm_map_array *a = &(map->table[i]); - if (map) { + for (size_t j = 0; j < LM_MAP_ARRAY_SIZE; ++j) { + if (a->main_keys[j] && a->main_values[j]) { + ++ret; + } + } - size_t i; - - for (i = 0; i < LM_MAP_HASH_SIZE; ++i) { - - const lm_map_array *a = &(map->table[i]); - - size_t j; - - for (j = 0; j < LM_MAP_ARRAY_SIZE; ++j) { - if (a->main_keys[j] && a->main_values[j]) { + size_t esz = a->extra_sz; + if (esz && a->extra_values && a->extra_keys) { + for (size_t j = 0; j < esz; ++j) { + if (*(a->extra_keys[j]) && *(a->extra_values[j])) { ++ret; } } - - size_t esz = a->extra_sz; - if (esz && a->extra_values && a->extra_keys) { - for (j = 0; j < esz; ++j) { - if (*(a->extra_keys[j]) && *(a->extra_values[j])) { - ++ret; - } - } - } } } return ret; } -int lm_map_foreach(lm_map *map, foreachcb_type func) { - size_t ret = 0; +bool lm_map_foreach(lm_map *map, foreachcb_type func) { + if (!map) { + return false; + } - if (map) { + for (size_t i = 0; i < LM_MAP_HASH_SIZE; ++i) { + lm_map_array *a = &(map->table[i]); - size_t i; - - for (i = 0; i < LM_MAP_HASH_SIZE; ++i) { - - lm_map_array *a = &(map->table[i]); - - size_t j; - - for (j = 0; j < LM_MAP_ARRAY_SIZE; ++j) { - if (a->main_keys[j] && a->main_values[j]) { - if (func((ur_map_key_type)a->main_keys[j], (ur_map_value_type)a->main_values[j])) { - return 1; - } + for (size_t j = 0; j < LM_MAP_ARRAY_SIZE; ++j) { + if (a->main_keys[j] && a->main_values[j]) { + if (func((ur_map_key_type)a->main_keys[j], (ur_map_value_type)a->main_values[j])) { + return true; } } + } - size_t esz = a->extra_sz; - if (esz && a->extra_values && a->extra_keys) { - for (j = 0; j < esz; ++j) { - if (*(a->extra_keys[j]) && *(a->extra_values[j])) { - if (func((ur_map_key_type) * (a->extra_keys[j]), (ur_map_value_type) * (a->extra_values[j]))) { - return 1; - } + size_t esz = a->extra_sz; + if (esz && a->extra_values && a->extra_keys) { + for (size_t j = 0; j < esz; ++j) { + if (*(a->extra_keys[j]) && *(a->extra_values[j])) { + if (func((ur_map_key_type) * (a->extra_keys[j]), (ur_map_value_type) * (a->extra_values[j]))) { + return true; } } } } } - return ret; + return false; } -int lm_map_foreach_arg(lm_map *map, foreachcb_arg_type func, void *arg) { - size_t ret = 0; +bool lm_map_foreach_arg(lm_map *map, foreachcb_arg_type func, void *arg) { + if (!map) { + return false; + } - if (map) { + for (size_t i = 0; i < LM_MAP_HASH_SIZE; ++i) { + lm_map_array *a = &(map->table[i]); - size_t i; - - for (i = 0; i < LM_MAP_HASH_SIZE; ++i) { - - lm_map_array *a = &(map->table[i]); - - size_t j; - - for (j = 0; j < LM_MAP_ARRAY_SIZE; ++j) { - if (a->main_keys[j] && a->main_values[j]) { - if (func((ur_map_key_type)a->main_keys[j], (ur_map_value_type)a->main_values[j], arg)) { - return 1; - } + for (size_t j = 0; j < LM_MAP_ARRAY_SIZE; ++j) { + if (a->main_keys[j] && a->main_values[j]) { + if (func((ur_map_key_type)a->main_keys[j], (ur_map_value_type)a->main_values[j], arg)) { + return true; } } + } - size_t esz = a->extra_sz; - if (esz && a->extra_values && a->extra_keys) { - for (j = 0; j < esz; ++j) { - if (*(a->extra_keys[j]) && *(a->extra_values[j])) { - if (func((ur_map_key_type) * (a->extra_keys[j]), (ur_map_value_type) * (a->extra_values[j]), arg)) { - return 1; - } + size_t esz = a->extra_sz; + if (esz && a->extra_values && a->extra_keys) { + for (size_t j = 0; j < esz; ++j) { + if (*(a->extra_keys[j]) && *(a->extra_values[j])) { + if (func((ur_map_key_type) * (a->extra_keys[j]), (ur_map_value_type) * (a->extra_values[j]), arg)) { + return true; } } } } } - return ret; + return false; } //////////////////// ADDR LISTS /////////////////////////////////// @@ -583,15 +495,13 @@ static void addr_list_free(addr_list_header *slh) { } static void addr_list_add(addr_list_header *slh, const ioa_addr *key, ur_addr_map_value_type value) { - if (!key || !value) { return; } addr_elem *elem = NULL; - size_t i; - for (i = 0; i < ADDR_ARRAY_SIZE; ++i) { + for (size_t i = 0; i < ADDR_ARRAY_SIZE; ++i) { if (!(slh->main_list[i].value)) { elem = &(slh->main_list[i]); break; @@ -599,7 +509,7 @@ static void addr_list_add(addr_list_header *slh, const ioa_addr *key, ur_addr_ma } if (!elem && slh->extra_list) { - for (i = 0; i < slh->extra_sz; ++i) { + for (size_t i = 0; i < slh->extra_sz; ++i) { if (!(slh->extra_list[i].value)) { elem = &(slh->extra_list[i]); break; @@ -628,9 +538,7 @@ static void addr_list_remove(addr_list_header *slh, const ioa_addr *key, ur_addr *counter = 0; } - size_t i; - - for (i = 0; i < ADDR_ARRAY_SIZE; ++i) { + for (size_t i = 0; i < ADDR_ARRAY_SIZE; ++i) { addr_elem *elem = &(slh->main_list[i]); if (elem->value) { if (addr_eq(&(elem->key), key)) { @@ -646,7 +554,7 @@ static void addr_list_remove(addr_list_header *slh, const ioa_addr *key, ur_addr } if (slh->extra_list) { - for (i = 0; i < slh->extra_sz; ++i) { + for (size_t i = 0; i < slh->extra_sz; ++i) { addr_elem *elem = &(slh->extra_list[i]); if (elem->value) { if (addr_eq(&(elem->key), key)) { @@ -666,9 +574,7 @@ static void addr_list_remove(addr_list_header *slh, const ioa_addr *key, ur_addr static void addr_list_foreach(addr_list_header *slh, ur_addr_map_func func) { if (slh && func) { - size_t i; - - for (i = 0; i < ADDR_ARRAY_SIZE; ++i) { + for (size_t i = 0; i < ADDR_ARRAY_SIZE; ++i) { addr_elem *elem = &(slh->main_list[i]); if (elem->value) { func(elem->value); @@ -676,7 +582,7 @@ static void addr_list_foreach(addr_list_header *slh, ur_addr_map_func func) { } if (slh->extra_list) { - for (i = 0; i < slh->extra_sz; ++i) { + for (size_t i = 0; i < slh->extra_sz; ++i) { addr_elem *elem = &(slh->extra_list[i]); if (elem->value) { func(elem->value); @@ -687,14 +593,10 @@ static void addr_list_foreach(addr_list_header *slh, ur_addr_map_func func) { } static size_t addr_list_num_elements(const addr_list_header *slh) { - size_t ret = 0; if (slh) { - - size_t i; - - for (i = 0; i < ADDR_ARRAY_SIZE; ++i) { + for (size_t i = 0; i < ADDR_ARRAY_SIZE; ++i) { const addr_elem *elem = &(slh->main_list[i]); if (elem->value) { ++ret; @@ -702,7 +604,7 @@ static size_t addr_list_num_elements(const addr_list_header *slh) { } if (slh->extra_list) { - for (i = 0; i < slh->extra_sz; ++i) { + for (size_t i = 0; i < slh->extra_sz; ++i) { addr_elem *elem = &(slh->extra_list[i]); if (elem->value) { ++ret; @@ -715,28 +617,19 @@ static size_t addr_list_num_elements(const addr_list_header *slh) { } static size_t addr_list_size(const addr_list_header *slh) { - - size_t ret = 0; - if (slh) { - - ret += ADDR_ARRAY_SIZE; - - ret += slh->extra_sz; + return ADDR_ARRAY_SIZE + slh->extra_sz; } - return ret; + return 0; } static addr_elem *addr_list_get(addr_list_header *slh, const ioa_addr *key) { - if (!slh || !key) { return NULL; } - size_t i; - - for (i = 0; i < ADDR_ARRAY_SIZE; ++i) { + for (size_t i = 0; i < ADDR_ARRAY_SIZE; ++i) { addr_elem *elem = &(slh->main_list[i]); if (elem->value) { if (addr_eq(&(elem->key), key)) { @@ -746,7 +639,7 @@ static addr_elem *addr_list_get(addr_list_header *slh, const ioa_addr *key) { } if (slh->extra_list) { - for (i = 0; i < slh->extra_sz; ++i) { + for (size_t i = 0; i < slh->extra_sz; ++i) { addr_elem *elem = &(slh->extra_list[i]); if (elem->value) { if (addr_eq(&(elem->key), key)) { @@ -760,14 +653,11 @@ static addr_elem *addr_list_get(addr_list_header *slh, const ioa_addr *key) { } static const addr_elem *addr_list_get_const(const addr_list_header *slh, const ioa_addr *key) { - if (!slh || !key) { return NULL; } - size_t i; - - for (i = 0; i < ADDR_ARRAY_SIZE; ++i) { + for (size_t i = 0; i < ADDR_ARRAY_SIZE; ++i) { const addr_elem *elem = &(slh->main_list[i]); if (elem->value) { if (addr_eq(&(elem->key), key)) { @@ -777,7 +667,7 @@ static const addr_elem *addr_list_get_const(const addr_list_header *slh, const i } if (slh->extra_list) { - for (i = 0; i < slh->extra_sz; ++i) { + for (size_t i = 0; i < slh->extra_sz; ++i) { const addr_elem *elem = &(slh->extra_list[i]); if (elem->value) { if (addr_eq(&(elem->key), key)) { @@ -807,24 +697,16 @@ void ur_addr_map_init(ur_addr_map *map) { void ur_addr_map_clean(ur_addr_map *map) { if (map && ur_addr_map_valid(map)) { - uint32_t i = 0; - for (i = 0; i < ADDR_MAP_SIZE; i++) { + for (size_t i = 0; i < ADDR_MAP_SIZE; i++) { addr_list_free(&(map->lists[i])); } memset(map, 0, sizeof(ur_addr_map)); } } -/** - * @ret: - * 0 - success - * -1 - error - * if the addr key exists, the value is updated. - */ -int ur_addr_map_put(ur_addr_map *map, ioa_addr *key, ur_addr_map_value_type value) { - +bool ur_addr_map_put(ur_addr_map *map, ioa_addr *key, ur_addr_map_value_type value) { if (!ur_addr_map_valid(map)) { - return -1; + return false; } else { @@ -838,108 +720,71 @@ int ur_addr_map_put(ur_addr_map *map, ioa_addr *key, ur_addr_map_value_type valu addr_list_add(slh, key, value); } - return 0; + return true; } } -/** - * @ret: - * 1 - success - * 0 - not found - */ -int ur_addr_map_get(const ur_addr_map *map, ioa_addr *key, ur_addr_map_value_type *value) { - +bool ur_addr_map_get(const ur_addr_map *map, ioa_addr *key, ur_addr_map_value_type *value) { if (!ur_addr_map_valid(map)) { - return 0; + return false; } - else { - - const addr_list_header *slh = get_addr_list_header(map, key); - - const addr_elem *elem = addr_list_get_const(slh, key); - if (elem) { - if (value) { - *value = elem->value; - } - return 1; + const addr_list_header *slh = get_addr_list_header(map, key); + const addr_elem *elem = addr_list_get_const(slh, key); + if (elem) { + if (value) { + *value = elem->value; } - - return 0; + return true; } + return false; } -/** - * @ret: - * 1 - success - * 0 - not found - */ -int ur_addr_map_del(ur_addr_map *map, ioa_addr *key, ur_addr_map_func delfunc) { - +bool ur_addr_map_del(ur_addr_map *map, ioa_addr *key, ur_addr_map_func delfunc) { if (!ur_addr_map_valid(map)) { - return 0; + return false; } - else { + addr_list_header *slh = get_addr_list_header(map, key); - addr_list_header *slh = get_addr_list_header(map, key); + int counter = 0; + addr_list_remove(slh, key, delfunc, &counter); - int counter = 0; - - addr_list_remove(slh, key, delfunc, &counter); - - return (counter > 0); - } + return (counter > 0); } -/** - * @ret: - * 1 - success - * 0 - not found - */ void ur_addr_map_foreach(ur_addr_map *map, ur_addr_map_func func) { - if (ur_addr_map_valid(map)) { - - uint32_t i = 0; - for (i = 0; i < ADDR_MAP_SIZE; i++) { - + for (size_t i = 0; i < ADDR_MAP_SIZE; i++) { addr_list_header *slh = &(map->lists[i]); - addr_list_foreach(slh, func); } } } size_t ur_addr_map_num_elements(const ur_addr_map *map) { + if (!ur_addr_map_valid(map)) { + return 0; + } size_t ret = 0; - - if (ur_addr_map_valid(map)) { - uint32_t i = 0; - for (i = 0; i < ADDR_MAP_SIZE; i++) { - - const addr_list_header *slh = &(map->lists[i]); - - ret += addr_list_num_elements(slh); - } + for (size_t i = 0; i < ADDR_MAP_SIZE; i++) { + const addr_list_header *slh = &(map->lists[i]); + ret += addr_list_num_elements(slh); } return ret; } size_t ur_addr_map_size(const ur_addr_map *map) { + if (!ur_addr_map_valid(map)) { + return 0; + } size_t ret = 0; - - if (ur_addr_map_valid(map)) { - uint32_t i = 0; - for (i = 0; i < ADDR_MAP_SIZE; i++) { - - const addr_list_header *slh = &(map->lists[i]); - - ret += addr_list_size(slh); - } + for (size_t i = 0; i < ADDR_MAP_SIZE; i++) { + const addr_list_header *slh = &(map->lists[i]); + ret += addr_list_size(slh); } return ret; @@ -1025,7 +870,6 @@ static string_list *string_list_remove(string_list *sl, const ur_string_map_key_ } static string_elem *string_list_get(string_list *sl, const ur_string_map_key_type key) { - if (!sl || !key) { return NULL; } @@ -1063,25 +907,25 @@ static uint32_t string_hash(const ur_string_map_key_type key) { return hash; } -static int string_map_index(const ur_string_map_key_type key) { return (int)(string_hash(key) % STRING_MAP_SIZE); } +static size_t string_map_index(const ur_string_map_key_type key) { return string_hash(key) % STRING_MAP_SIZE; } static string_list_header *get_string_list_header(ur_string_map *map, const ur_string_map_key_type key) { return &(map->lists[string_map_index(key)]); } -static int ur_string_map_init(ur_string_map *map) { +static bool ur_string_map_init(ur_string_map *map) { if (map) { memset(map, 0, sizeof(ur_string_map)); map->magic = MAGIC_HASH; TURN_MUTEX_INIT_RECURSIVE(&(map->mutex)); - return 0; + return true; } - return -1; + return false; } -static int ur_string_map_valid(const ur_string_map *map) { return (map && map->magic == MAGIC_HASH); } +#define ur_string_map_valid(map) ((map) && ((map)->magic == MAGIC_HASH)) ur_string_map *ur_string_map_create(ur_string_map_func del_value_func) { ur_string_map *map = (ur_string_map *)malloc(sizeof(ur_string_map)); @@ -1093,92 +937,62 @@ ur_string_map *ur_string_map_create(ur_string_map_func del_value_func) { return map; } -/** - * @ret: - * 0 - success - * -1 - error - * if the string key exists, and the value is different, return error. - */ -int ur_string_map_put(ur_string_map *map, const ur_string_map_key_type key, ur_string_map_value_type value) { +bool ur_string_map_put(ur_string_map *map, const ur_string_map_key_type key, ur_string_map_value_type value) { if (!ur_string_map_valid(map)) { - return -1; + return false; } - else { - - string_list_header *slh = get_string_list_header(map, key); - - string_elem *elem = string_list_get(slh->list, key); - if (elem) { - if (elem->value != value) { - if (map->del_value_func) { - map->del_value_func(elem->value); - } - elem->value = value; + string_list_header *slh = get_string_list_header(map, key); + string_elem *elem = string_list_get(slh->list, key); + if (elem) { + if (elem->value != value) { + if (map->del_value_func) { + map->del_value_func(elem->value); } - return 0; + elem->value = value; } + return true; + } - slh->list = string_list_add(slh->list, key, value); + slh->list = string_list_add(slh->list, key, value); + return true; +} - return 0; +bool ur_string_map_get(ur_string_map *map, const ur_string_map_key_type key, ur_string_map_value_type *value) { + + if (!ur_string_map_valid(map)) { + return false; + } + + string_list_header *slh = get_string_list_header(map, key); + string_elem *elem = string_list_get(slh->list, key); + if (elem) { + if (value) { + *value = elem->value; + } + return true; + } else { + return false; } } -/** - * @ret: - * 1 - success - * 0 - not found - */ -int ur_string_map_get(ur_string_map *map, const ur_string_map_key_type key, ur_string_map_value_type *value) { +bool ur_string_map_del(ur_string_map *map, const ur_string_map_key_type key) { if (!ur_string_map_valid(map)) { - return 0; + return false; } - else { + string_list_header *slh = get_string_list_header(map, key); - string_list_header *slh = get_string_list_header(map, key); - string_elem *elem = string_list_get(slh->list, key); - if (elem) { - if (value) { - *value = elem->value; - } - return 1; - } else { - return 0; - } - } -} - -/** - * @ret: - * 1 - success - * 0 - not found - */ -int ur_string_map_del(ur_string_map *map, const ur_string_map_key_type key) { - - if (!ur_string_map_valid(map)) { - return 0; - } - - else { - - string_list_header *slh = get_string_list_header(map, key); - - int counter = 0; - - slh->list = string_list_remove(slh->list, key, map->del_value_func, &counter); - - return (counter > 0); - } + int counter = 0; + slh->list = string_list_remove(slh->list, key, map->del_value_func, &counter); + return (counter > 0); } void ur_string_map_clean(ur_string_map *map) { if (ur_string_map_valid(map)) { - int i = 0; - for (i = 0; i < STRING_MAP_SIZE; i++) { + for (size_t i = 0; i < STRING_MAP_SIZE; ++i) { string_list_free(&(map->lists[i]), map->del_value_func); } } @@ -1186,8 +1000,7 @@ void ur_string_map_clean(ur_string_map *map) { void ur_string_map_free(ur_string_map **map) { if (map && ur_string_map_valid(*map)) { - int i = 0; - for (i = 0; i < STRING_MAP_SIZE; i++) { + for (size_t i = 0; i < STRING_MAP_SIZE; ++i) { string_list_free(&((*map)->lists[i]), (*map)->del_value_func); } (*map)->magic = 0; @@ -1198,32 +1011,31 @@ void ur_string_map_free(ur_string_map **map) { } size_t ur_string_map_size(const ur_string_map *map) { - if (ur_string_map_valid(map)) { - size_t ret = 0; - int i = 0; - for (i = 0; i < STRING_MAP_SIZE; i++) { - ret += string_list_size(map->lists[i].list); - } - return ret; - } else { + if (!ur_string_map_valid(map)) { return 0; } + + size_t ret = 0; + for (size_t i = 0; i < STRING_MAP_SIZE; ++i) { + ret += string_list_size(map->lists[i].list); + } + return ret; } -int ur_string_map_lock(const ur_string_map *map) { +bool ur_string_map_lock(const ur_string_map *map) { if (ur_string_map_valid(map)) { TURN_MUTEX_LOCK((const turn_mutex *)&(map->mutex)); - return 0; + return true; } - return -1; + return false; } -int ur_string_map_unlock(const ur_string_map *map) { +bool ur_string_map_unlock(const ur_string_map *map) { if (ur_string_map_valid(map)) { TURN_MUTEX_UNLOCK((const turn_mutex *)&(map->mutex)); - return 0; + return true; } - return -1; + return false; } //////////////////////////////////////////////////////////////// diff --git a/src/server/ns_turn_maps.h b/src/server/ns_turn_maps.h index cab2dbf..88cfe37 100644 --- a/src/server/ns_turn_maps.h +++ b/src/server/ns_turn_maps.h @@ -33,6 +33,8 @@ #include "ns_turn_ioaddr.h" +#include + #ifdef __cplusplus extern "C" { #endif @@ -49,8 +51,8 @@ typedef uintptr_t ur_map_value_type; typedef void (*ur_map_del_func)(ur_map_value_type); -typedef int (*foreachcb_type)(ur_map_key_type key, ur_map_value_type value); -typedef int (*foreachcb_arg_type)(ur_map_key_type key, ur_map_value_type value, void *arg); +typedef bool (*foreachcb_type)(ur_map_key_type key, ur_map_value_type value); +typedef bool (*foreachcb_arg_type)(ur_map_key_type key, ur_map_value_type value, void *arg); ///////////// non-local map ///////////////////// @@ -58,44 +60,63 @@ ur_map *ur_map_create(void); /** * @ret: - * 0 - success - * -1 - error + * true - success + * fakse - error */ - -int ur_map_put(ur_map *map, ur_map_key_type key, ur_map_value_type value); +bool ur_map_put(ur_map *map, ur_map_key_type key, ur_map_value_type value); /** * @ret: - * 1 - success - * 0 - not found + * true - success + * false - not found */ +bool ur_map_get(const ur_map *map, ur_map_key_type key, ur_map_value_type *value); -int ur_map_get(const ur_map *map, ur_map_key_type key, ur_map_value_type *value); /** * @ret: - * 1 - success - * 0 - not found + * true - success + * false - not found */ +bool ur_map_del(ur_map *map, ur_map_key_type key, ur_map_del_func delfunc); -int ur_map_del(ur_map *map, ur_map_key_type key, ur_map_del_func delfunc); /** * @ret: - * 1 - success - * 0 - not found + * true - success + * false - not found */ - -int ur_map_exist(const ur_map *map, ur_map_key_type key); +bool ur_map_exist(const ur_map *map, ur_map_key_type key); void ur_map_free(ur_map **map); size_t ur_map_size(const ur_map *map); -int ur_map_foreach(ur_map *map, foreachcb_type func); +/** + * @ret: + * true - func is called and returns true + * false - func is not called, or is called and returns false + */ +bool ur_map_foreach(ur_map *map, foreachcb_type func); -int ur_map_foreach_arg(const ur_map *map, foreachcb_arg_type func, void *arg); +/** + * @ret: + * true - func is called and returns true + * false - func is not called, or is called and returns false + */ +bool ur_map_foreach_arg(const ur_map *map, foreachcb_arg_type func, void *arg); -int ur_map_lock(const ur_map *map); -int ur_map_unlock(const ur_map *map); +/** + * @ret: + * true - success + * false - failure + */ +bool ur_map_lock(const ur_map *map); + +/** + * @ret: + * true - success + * false - failure + */ +bool ur_map_unlock(const ur_map *map); ///////////// "local" map ///////////////////// @@ -118,41 +139,49 @@ void lm_map_init(lm_map *map); /** * @ret: - * 0 - success - * -1 - error + * true - success + * false - error */ - -int lm_map_put(lm_map *map, ur_map_key_type key, ur_map_value_type value); +bool lm_map_put(lm_map *map, ur_map_key_type key, ur_map_value_type value); /** * @ret: - * 1 - success - * 0 - not found + * true - success + * false - not found */ +bool lm_map_get(const lm_map *map, ur_map_key_type key, ur_map_value_type *value); -int lm_map_get(const lm_map *map, ur_map_key_type key, ur_map_value_type *value); /** * @ret: - * 1 - success - * 0 - not found + * true - success + * false - not found */ +bool lm_map_del(lm_map *map, ur_map_key_type key, ur_map_del_func delfunc); -int lm_map_del(lm_map *map, ur_map_key_type key, ur_map_del_func delfunc); /** * @ret: - * 1 - success - * 0 - not found + * true - success + * false - not found */ - -int lm_map_exist(const lm_map *map, ur_map_key_type key); +bool lm_map_exist(const lm_map *map, ur_map_key_type key); void lm_map_clean(lm_map *map); size_t lm_map_size(const lm_map *map); -int lm_map_foreach(lm_map *map, foreachcb_type func); +/** + * @ret: + * true - func is called and returns true + * false - func is not called, or is called and returns false + */ +bool lm_map_foreach(lm_map *map, foreachcb_type func); -int lm_map_foreach_arg(lm_map *map, foreachcb_arg_type func, void *arg); +/** + * @ret: + * true - func is called and returns true + * false - func is not called, or is called and returns false + */ +bool lm_map_foreach_arg(lm_map *map, foreachcb_arg_type func, void *arg); //////////////// UR ADDR MAP ////////////////// @@ -187,31 +216,26 @@ void ur_addr_map_clean(ur_addr_map *map); /** * @ret: - * 0 - success - * -1 - error + * true - success + * false - error * if the addr key exists, the value is updated. */ -int ur_addr_map_put(ur_addr_map *map, ioa_addr *key, ur_addr_map_value_type value); +bool ur_addr_map_put(ur_addr_map *map, ioa_addr *key, ur_addr_map_value_type value); /** * @ret: - * 1 - success - * 0 - not found + * true - success + * false - not found */ -int ur_addr_map_get(const ur_addr_map *map, ioa_addr *key, ur_addr_map_value_type *value); +bool ur_addr_map_get(const ur_addr_map *map, ioa_addr *key, ur_addr_map_value_type *value); /** * @ret: - * 1 - success - * 0 - not found + * true - success + * false - not found */ -int ur_addr_map_del(ur_addr_map *map, ioa_addr *key, ur_addr_map_func func); +bool ur_addr_map_del(ur_addr_map *map, ioa_addr *key, ur_addr_map_func func); -/** - * @ret: - * 1 - success - * 0 - not found - */ void ur_addr_map_foreach(ur_addr_map *map, ur_addr_map_func func); size_t ur_addr_map_num_elements(const ur_addr_map *map); @@ -230,33 +254,44 @@ ur_string_map *ur_string_map_create(ur_string_map_func del_value_func); /** * @ret: - * 0 - success - * -1 - error + * true - success + * false - error * if the string key exists, and the value is different, return error. */ -int ur_string_map_put(ur_string_map *map, const ur_string_map_key_type key, ur_string_map_value_type value); +bool ur_string_map_put(ur_string_map *map, const ur_string_map_key_type key, ur_string_map_value_type value); /** * @ret: - * 1 - success - * 0 - not found + * true - success + * false - not found */ -int ur_string_map_get(ur_string_map *map, const ur_string_map_key_type key, ur_string_map_value_type *value); +bool ur_string_map_get(ur_string_map *map, const ur_string_map_key_type key, ur_string_map_value_type *value); /** * @ret: - * 1 - success - * 0 - not found + * true - success + * false - not found */ -int ur_string_map_del(ur_string_map *map, const ur_string_map_key_type key); +bool ur_string_map_del(ur_string_map *map, const ur_string_map_key_type key); void ur_string_map_clean(ur_string_map *map); void ur_string_map_free(ur_string_map **map); size_t ur_string_map_size(const ur_string_map *map); -int ur_string_map_lock(const ur_string_map *map); -int ur_string_map_unlock(const ur_string_map *map); +/** + * @ret: + * true - success + * false - failure + */ +bool ur_string_map_lock(const ur_string_map *map); + +/** + * @ret: + * true - success + * false - failure + */ +bool ur_string_map_unlock(const ur_string_map *map); //////////////////////////////////////////// diff --git a/src/server/ns_turn_maps_rtcp.c b/src/server/ns_turn_maps_rtcp.c index 5e65bc4..4b86437 100644 --- a/src/server/ns_turn_maps_rtcp.c +++ b/src/server/ns_turn_maps_rtcp.c @@ -55,7 +55,7 @@ typedef struct { //////////////////////////////////////////// -static int rtcp_map_valid(const rtcp_map *map) { return (map && (map->magic == MAGIC_RTCP_MAP) && map->map); } +static bool rtcp_map_valid(const rtcp_map *map) { return (map && (map->magic == MAGIC_RTCP_MAP) && map->map); } typedef struct { rtcp_token_type tokens[MAX_TOKEN_DEL]; @@ -63,7 +63,7 @@ typedef struct { turn_time_t t; } timeout_check_arg_type; -static int timeout_check(ur_map_key_type key, ur_map_value_type value, void *arg) { +static bool timeout_check(ur_map_key_type key, ur_map_value_type value, void *arg) { if (value && arg) { @@ -76,7 +76,7 @@ static int timeout_check(ur_map_key_type key, ur_map_value_type value, void *arg } } - return 0; + return false; } static void rtcp_alloc_free(ur_map_value_type value) { @@ -94,37 +94,35 @@ static void rtcp_alloc_free_savefd(ur_map_value_type value) { } } -static int foreachcb_free(ur_map_key_type key, ur_map_value_type value) { +static bool foreachcb_free(ur_map_key_type key, ur_map_value_type value) { UNUSED_ARG(key); if (value) { rtcp_alloc_free(value); } - return 0; + return false; } /** * @ret: - * 1 - success - * 0 - not found + * true - success + * false - not found */ -static int rtcp_map_del(rtcp_map *map, rtcp_token_type token) { +static bool rtcp_map_del(rtcp_map *map, rtcp_token_type token) { if (!rtcp_map_valid(map)) { - return 0; - } else { - TURN_MUTEX_LOCK(&map->mutex); - int ret = ur_map_del(map->map, token, rtcp_alloc_free); - TURN_MUTEX_UNLOCK(&map->mutex); - return ret; + return false; } + + TURN_MUTEX_LOCK(&map->mutex); + bool ret = ur_map_del(map->map, token, rtcp_alloc_free); + TURN_MUTEX_UNLOCK(&map->mutex); + return ret; } -static int rtcp_map_del_savefd(rtcp_map *map, rtcp_token_type token) { +static bool rtcp_map_del_savefd(rtcp_map *map, rtcp_token_type token) { if (!rtcp_map_valid(map)) { - return 0; - } else { - int ret = ur_map_del(map->map, token, rtcp_alloc_free_savefd); - return ret; + return false; } + return ur_map_del(map->map, token, rtcp_alloc_free_savefd); } static void rtcp_map_timeout_handler(ioa_engine_handle e, void *arg) { @@ -149,14 +147,13 @@ static void rtcp_map_timeout_handler(ioa_engine_handle e, void *arg) { TURN_MUTEX_UNLOCK(&map->mutex); - int i = 0; - for (i = 0; i < tcat.tn; i++) { + for (size_t i = 0; i < tcat.tn; i++) { rtcp_map_del(map, tcat.tokens[i]); } } } -static int rtcp_map_init(rtcp_map *map, ioa_engine_handle e) { +static bool rtcp_map_init(rtcp_map *map, ioa_engine_handle e) { if (map) { if (map->magic != MAGIC_RTCP_MAP) { map->magic = MAGIC_RTCP_MAP; @@ -166,11 +163,11 @@ static int rtcp_map_init(rtcp_map *map, ioa_engine_handle e) { } TURN_MUTEX_INIT(&map->mutex); if (rtcp_map_valid(map)) { - return 0; + return true; } } } - return -1; + return false; } rtcp_map *rtcp_map_create(ioa_engine_handle e) { @@ -184,26 +181,26 @@ rtcp_map *rtcp_map_create(ioa_engine_handle e) { /** * @ret: - * 0 - success - * -1 - error + * true - success + * false - error */ -int rtcp_map_put(rtcp_map *map, rtcp_token_type token, ioa_socket_handle s) { +bool rtcp_map_put(rtcp_map *map, rtcp_token_type token, ioa_socket_handle s) { if (!rtcp_map_valid(map)) { - return -1; + return false; } else { rtcp_alloc_type *value = (rtcp_alloc_type *)calloc(sizeof(rtcp_alloc_type), 1); if (!value) { - return -1; + return false; } value->s = s; value->t = turn_time() + RTCP_TIMEOUT; value->token = token; TURN_MUTEX_LOCK(&map->mutex); - int ret = ur_map_put(map->map, token, (ur_map_value_type)value); + bool ret = ur_map_put(map->map, token, (ur_map_value_type)value); // TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,"%s: 111.111: ret=%d, token=%llu\n",__FUNCTION__,ret,token); TURN_MUTEX_UNLOCK(&map->mutex); - if (ret < 0) { + if (!ret) { free(value); } return ret; diff --git a/src/server/ns_turn_maps_rtcp.h b/src/server/ns_turn_maps_rtcp.h index 45ff5fe..1593125 100644 --- a/src/server/ns_turn_maps_rtcp.h +++ b/src/server/ns_turn_maps_rtcp.h @@ -51,10 +51,10 @@ rtcp_map *rtcp_map_create(ioa_engine_handle e); /** * @ret: - * 0 - success - * -1 - error + * true - success + * false - error */ -int rtcp_map_put(rtcp_map *map, rtcp_token_type key, ioa_socket_handle s); +bool rtcp_map_put(rtcp_map *map, rtcp_token_type key, ioa_socket_handle s); /** * @ret: @@ -63,11 +63,6 @@ int rtcp_map_put(rtcp_map *map, rtcp_token_type key, ioa_socket_handle s); */ ioa_socket_handle rtcp_map_get(rtcp_map *map, rtcp_token_type token); -/** - * @ret: - * 1 - success - * 0 - not found - */ void rtcp_map_free(rtcp_map **map); size_t rtcp_map_size(const rtcp_map *map);