From 55c8e19424b3e7cc6be8e8f79e19b25d6dbfb339 Mon Sep 17 00:00:00 2001 From: mom040267 Date: Wed, 22 Oct 2014 05:34:50 +0000 Subject: [PATCH] working on rtcp sockets --- src/apps/relay/ns_ioalib_engine_impl.c | 17 ++-- src/server/ns_turn_ioalib.h | 2 +- src/server/ns_turn_maps_rtcp.c | 103 +++++++++++++------------ src/server/ns_turn_maps_rtcp.h | 10 +-- src/server/ns_turn_server.c | 14 ++-- 5 files changed, 71 insertions(+), 75 deletions(-) diff --git a/src/apps/relay/ns_ioalib_engine_impl.c b/src/apps/relay/ns_ioalib_engine_impl.c index d0ba4839..c64e361a 100644 --- a/src/apps/relay/ns_ioalib_engine_impl.c +++ b/src/apps/relay/ns_ioalib_engine_impl.c @@ -681,10 +681,10 @@ static int ioa_socket_check_bandwidth(ioa_socket_handle s, size_t sz, int read) return 1; } -int get_ioa_socket_from_reservation(ioa_engine_handle e, u64bits in_reservation_token, ioa_socket_handle *s) +int get_ioa_socket_from_reservation(ioa_engine_handle e, u64bits in_reservation_token, ioa_socket_handle *s, u08bits *realm) { if (e && in_reservation_token && s) { - *s = rtcp_map_get(e->map_rtcp, in_reservation_token); + *s = rtcp_map_get(e->map_rtcp, in_reservation_token, realm); if (*s) { return 0; } @@ -1823,12 +1823,15 @@ void set_ioa_socket_sub_session(ioa_socket_handle s, tcp_connection *tc) } int get_ioa_socket_address_family(ioa_socket_handle s) { - if(!s) { + + int first_time = 1; + beg: + if (!(s && (s->magic == SOCKET_MAGIC) && !(s->done))) { return AF_INET; - } else if(s->done) { - return s->family; - } else if(s->parent_s && (s != s->parent_s)) { - return get_ioa_socket_address_family(s->parent_s); + } else if(first_time && s->parent_s && (s != s->parent_s)) { + first_time = 0; + s = s->parent_s; + goto beg; } else { return s->family; } diff --git a/src/server/ns_turn_ioalib.h b/src/server/ns_turn_ioalib.h index 3a29e8dd..29b9cb37 100644 --- a/src/server/ns_turn_ioalib.h +++ b/src/server/ns_turn_ioalib.h @@ -215,7 +215,7 @@ int create_relay_ioa_sockets(ioa_engine_handle e, ioa_socket_handle client_s, ioa_socket_handle ioa_create_connecting_tcp_relay_socket(ioa_socket_handle s, ioa_addr *peer_addr, connect_cb cb, void *arg); -int get_ioa_socket_from_reservation(ioa_engine_handle e, u64bits in_reservation_token, ioa_socket_handle *s); +int get_ioa_socket_from_reservation(ioa_engine_handle e, u64bits in_reservation_token, ioa_socket_handle *s, u08bits *realm); int get_ioa_socket_address_family(ioa_socket_handle s); SOCKET_TYPE get_ioa_socket_type(ioa_socket_handle s); diff --git a/src/server/ns_turn_maps_rtcp.c b/src/server/ns_turn_maps_rtcp.c index 95266d41..34d7d75e 100644 --- a/src/server/ns_turn_maps_rtcp.c +++ b/src/server/ns_turn_maps_rtcp.c @@ -85,6 +85,54 @@ static int timeout_check(ur_map_key_type key, return 0; } +static void rtcp_alloc_free(ur_map_value_type value) +{ + rtcp_alloc_type *at = (rtcp_alloc_type *)value; + if (at) { + IOA_CLOSE_SOCKET(at->s); + turn_free(at,sizeof(rtcp_alloc_type)); + } +} + +static void rtcp_alloc_free_savefd(ur_map_value_type value) +{ + rtcp_alloc_type *at = (rtcp_alloc_type *) value; + if (at) { + turn_free(at,sizeof(rtcp_alloc_type)); + } +} + +static int foreachcb_free(ur_map_key_type key, ur_map_value_type value) { + UNUSED_ARG(key); + if(value) { + rtcp_alloc_free(value); + } + return 0; +} + +/** + * @ret: + * 1 - success + * 0 - not found + */ +static int 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; + } +} + +static int 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; + } +} + static void rtcp_map_timeout_handler(ioa_engine_handle e, void* arg) { UNUSED_ARG(e); @@ -166,7 +214,7 @@ int rtcp_map_put(rtcp_map* map, rtcp_token_type token, ioa_socket_handle s) { * >=0 - success * <0 - not found */ -ioa_socket_handle rtcp_map_get(rtcp_map* map, rtcp_token_type token) { +ioa_socket_handle rtcp_map_get(rtcp_map* map, rtcp_token_type token, u08bits *realm) { ioa_socket_handle s = NULL; if (rtcp_map_valid(map)) { ur_map_value_type value; @@ -176,61 +224,18 @@ ioa_socket_handle rtcp_map_get(rtcp_map* map, rtcp_token_type token) { rtcp_alloc_type* rval = (rtcp_alloc_type*) value; if (rval) { s = rval->s; + if(!check_realm_hash(s,realm)) { + s = NULL; + } else { + rtcp_map_del_savefd(map, token); + } } } - rtcp_map_del_savefd(map, token); TURN_MUTEX_UNLOCK(&map->mutex); } return s; } -static void rtcp_alloc_free(ur_map_value_type value) -{ - rtcp_alloc_type *at = (rtcp_alloc_type *)value; - if (at) { - IOA_CLOSE_SOCKET(at->s); - turn_free(at,sizeof(rtcp_alloc_type)); - } -} - -static void rtcp_alloc_free_savefd(ur_map_value_type value) -{ - rtcp_alloc_type *at = (rtcp_alloc_type *) value; - if (at) { - turn_free(at,sizeof(rtcp_alloc_type)); - } -} - -static int foreachcb_free(ur_map_key_type key, ur_map_value_type value) { - UNUSED_ARG(key); - if(value) { - rtcp_alloc_free(value); - } - return 0; -} - -/** - * @ret: - * 1 - success - * 0 - not found - */ -int 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; - } -} -int 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; - } -} - void rtcp_map_free(rtcp_map** map) { if(map && rtcp_map_valid(*map)) { TURN_MUTEX_LOCK(&((*map)->mutex)); diff --git a/src/server/ns_turn_maps_rtcp.h b/src/server/ns_turn_maps_rtcp.h index 44222ba3..e8c2a0f9 100644 --- a/src/server/ns_turn_maps_rtcp.h +++ b/src/server/ns_turn_maps_rtcp.h @@ -61,15 +61,7 @@ int rtcp_map_put(rtcp_map* map, rtcp_token_type key, ioa_socket_handle s); * >=0 - success * <0 - not found */ -ioa_socket_handle rtcp_map_get(rtcp_map* map, rtcp_token_type token); - -/** - * @ret: - * 1 - success - * 0 - not found - */ -int rtcp_map_del(rtcp_map* map, rtcp_token_type token); -int rtcp_map_del_savefd(rtcp_map* map, rtcp_token_type token); +ioa_socket_handle rtcp_map_get(rtcp_map* map, rtcp_token_type token, u08bits *realm); /** * @ret: diff --git a/src/server/ns_turn_server.c b/src/server/ns_turn_server.c index 502c6871..604efba6 100644 --- a/src/server/ns_turn_server.c +++ b/src/server/ns_turn_server.c @@ -4261,7 +4261,7 @@ static int create_relay_connection(turn_turnserver* server, ioa_socket_handle s = NULL; - if ((get_ioa_socket_from_reservation(server->e, in_reservation_token,&s) < 0)|| + if ((get_ioa_socket_from_reservation(server->e, in_reservation_token,&s,(u08bits*)ss->realm_options.name) < 0)|| !s || ioa_socket_tobeclosed(s)) { @@ -4275,16 +4275,12 @@ static int create_relay_connection(turn_turnserver* server, newelem = get_relay_session_ss(ss,family); - IOA_CLOSE_SOCKET(newelem->s); - - ns_bzero(newelem, sizeof(relay_endpoint_session)); - newelem->s = s; + if(newelem->s != s) { - if(!check_realm_hash(newelem->s,(u08bits*)ss->realm_options.name)) { IOA_CLOSE_SOCKET(newelem->s); - *err_code = 508; - *reason = (const u08bits *)"Cannot find a valid reserved socket for this realm"; - return -1; + + ns_bzero(newelem, sizeof(relay_endpoint_session)); + newelem->s = s; } addr_debug_print(server->verbose, get_local_addr_from_ioa_socket(newelem->s), "Local relay addr (RTCP)");