ns_turn_server.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2011, 2012, 2013 Citrix Systems
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #ifndef __TURN_SERVER__
  31. #define __TURN_SERVER__
  32. #include "ns_turn_utils.h"
  33. #include "ns_turn_session.h"
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. //////////// defines //////////////
  38. #define TURN_SESSION_ID_FACTOR (1000000000000000LL)
  39. //////////// ALTERNATE-SERVER /////////////
  40. struct _turn_server_addrs_list {
  41. ioa_addr *addrs;
  42. volatile size_t size;
  43. turn_mutex m;
  44. };
  45. typedef struct _turn_server_addrs_list turn_server_addrs_list_t;
  46. void init_turn_server_addrs_list(turn_server_addrs_list_t *l);
  47. ////////// RFC 5780 ///////////////////////
  48. typedef int (*get_alt_addr_cb)(ioa_addr *addr, ioa_addr *alt_addr);
  49. typedef int (*send_message_cb)(ioa_engine_handle e, ioa_network_buffer_handle nbh, ioa_addr *origin, ioa_addr *destination);
  50. //////////////////////////////////////////
  51. extern int TURN_MAX_ALLOCATE_TIMEOUT;
  52. extern int TURN_MAX_ALLOCATE_TIMEOUT_STUN_ONLY;
  53. typedef uint8_t turnserver_id;
  54. enum _MESSAGE_TO_RELAY_TYPE {
  55. RMT_UNKNOWN = 0,
  56. RMT_SOCKET,
  57. RMT_CB_SOCKET,
  58. RMT_MOBILE_SOCKET,
  59. RMT_CANCEL_SESSION
  60. };
  61. typedef enum _MESSAGE_TO_RELAY_TYPE MESSAGE_TO_RELAY_TYPE;
  62. struct socket_message {
  63. ioa_socket_handle s;
  64. ioa_net_data nd;
  65. int can_resume;
  66. };
  67. typedef enum {
  68. DONT_FRAGMENT_UNSUPPORTED=0,
  69. DONT_FRAGMENT_SUPPORTED,
  70. DONT_FRAGMENT_SUPPORT_EMULATED
  71. } dont_fragment_option_t;
  72. struct _turn_turnserver;
  73. typedef struct _turn_turnserver turn_turnserver;
  74. typedef void (*get_username_resume_cb)(int success, int oauth, int max_session_time, hmackey_t hmackey, password_t pwd, turn_turnserver *server, uint64_t ctxkey, ioa_net_data *in_buffer, uint8_t* realm);
  75. typedef uint8_t *(*get_user_key_cb)(turnserver_id id, turn_credential_type ct, int in_oauth, int *out_oauth, uint8_t *uname, uint8_t *realm, get_username_resume_cb resume, ioa_net_data *in_buffer, uint64_t ctxkey, int *postpone_reply);
  76. typedef int (*check_new_allocation_quota_cb)(uint8_t *username, int oauth, uint8_t *realm);
  77. typedef void (*release_allocation_quota_cb)(uint8_t *username, int oauth, uint8_t *realm);
  78. typedef int (*send_socket_to_relay_cb)(turnserver_id id, uint64_t cid, stun_tid *tid, ioa_socket_handle s, int message_integrity, MESSAGE_TO_RELAY_TYPE rmt, ioa_net_data *nd, int can_resume);
  79. typedef int (*send_turn_session_info_cb)(struct turn_session_info *tsi);
  80. typedef void (*send_https_socket_cb)(ioa_socket_handle s);
  81. typedef band_limit_t (*allocate_bps_cb)(band_limit_t bps, int positive);
  82. struct _turn_turnserver {
  83. turnserver_id id;
  84. turnsession_id session_id_counter;
  85. ur_map *sessions_map;
  86. turn_time_t ctime;
  87. ioa_engine_handle e;
  88. int verbose;
  89. int fingerprint;
  90. int rfc5780;
  91. vintp check_origin;
  92. vintp stale_nonce;
  93. vintp max_allocate_lifetime;
  94. vintp channel_lifetime;
  95. vintp permission_lifetime;
  96. vintp stun_only;
  97. vintp no_stun;
  98. vintp no_software_attribute;
  99. vintp web_admin_listen_on_workers;
  100. vintp secure_stun;
  101. turn_credential_type ct;
  102. get_alt_addr_cb alt_addr_cb;
  103. send_message_cb sm_cb;
  104. dont_fragment_option_t dont_fragment;
  105. int (*disconnect)(ts_ur_super_session*);
  106. get_user_key_cb userkeycb;
  107. check_new_allocation_quota_cb chquotacb;
  108. release_allocation_quota_cb raqcb;
  109. int external_ip_set;
  110. ioa_addr external_ip;
  111. vintp allow_loopback_peers;
  112. vintp no_multicast_peers;
  113. send_turn_session_info_cb send_turn_session_info;
  114. send_https_socket_cb send_https_socket;
  115. /* RFC 6062 ==>> */
  116. vintp no_udp_relay;
  117. vintp no_tcp_relay;
  118. ur_map *tcp_relay_connections;
  119. send_socket_to_relay_cb send_socket_to_relay;
  120. /* <<== RFC 6062 */
  121. /* Alternate servers ==>> */
  122. turn_server_addrs_list_t *alternate_servers_list;
  123. size_t as_counter;
  124. turn_server_addrs_list_t *tls_alternate_servers_list;
  125. size_t tls_as_counter;
  126. turn_server_addrs_list_t *aux_servers_list;
  127. int self_udp_balance;
  128. /* White/black listing of address ranges */
  129. ip_range_list_t* ip_whitelist;
  130. ip_range_list_t* ip_blacklist;
  131. /* Mobility */
  132. vintp mobility;
  133. ur_map *mobile_connections_map;
  134. /* Server relay */
  135. int server_relay;
  136. /* Bandwidth draft: */
  137. allocate_bps_cb allocate_bps_func;
  138. /* oAuth: */
  139. int oauth;
  140. const char* oauth_server_name;
  141. /* ACME redirect URL */
  142. const char* acme_redirect;
  143. /* Keep Address Family */
  144. int keep_address_family;
  145. /* Log Binding Requrest */
  146. vintp log_binding;
  147. };
  148. const char * get_version(turn_turnserver *server);
  149. ///////////////////////////////////////////
  150. void init_turn_server(turn_turnserver* server,
  151. turnserver_id id, int verbose,
  152. ioa_engine_handle e,
  153. turn_credential_type ct,
  154. int stun_port,
  155. int fingerprint,
  156. dont_fragment_option_t dont_fragment,
  157. get_user_key_cb userkeycb,
  158. check_new_allocation_quota_cb chquotacb,
  159. release_allocation_quota_cb raqcb,
  160. ioa_addr *external_addr,
  161. vintp check_origin,
  162. vintp no_tcp_relay,
  163. vintp no_udp_relay,
  164. vintp stale_nonce,
  165. vintp max_allocate_lifetime,
  166. vintp channel_lifetime,
  167. vintp permission_lifetime,
  168. vintp stun_only,
  169. vintp no_stun,
  170. vintp no_software_attribute,
  171. vintp web_admin_listen_on_workers,
  172. turn_server_addrs_list_t *alternate_servers_list,
  173. turn_server_addrs_list_t *tls_alternate_servers_list,
  174. turn_server_addrs_list_t *aux_servers_list,
  175. int self_udp_balance,
  176. vintp no_multicast_peers,
  177. vintp allow_loopback_peers,
  178. ip_range_list_t* ip_whitelist,
  179. ip_range_list_t* ip_blacklist,
  180. send_socket_to_relay_cb send_socket_to_relay,
  181. vintp secure_stun,
  182. vintp mobility,
  183. int server_relay,
  184. send_turn_session_info_cb send_turn_session_info,
  185. send_https_socket_cb send_https_socket,
  186. allocate_bps_cb allocate_bps_func,
  187. int oauth,
  188. const char* oauth_server_name,
  189. const char* acme_redirect,
  190. int keep_address_family,
  191. vintp log_binding);
  192. ioa_engine_handle turn_server_get_engine(turn_turnserver *s);
  193. ////////// RFC 5780 ///////////////////////
  194. void set_rfc5780(turn_turnserver *server, get_alt_addr_cb cb, send_message_cb smcb);
  195. ///////////////////////////////////////////
  196. int open_client_connection_session(turn_turnserver* server, struct socket_message *sm);
  197. int shutdown_client_connection(turn_turnserver *server, ts_ur_super_session *ss, int force, const char* reason);
  198. void set_disconnect_cb(turn_turnserver* server, int (*disconnect)(ts_ur_super_session*));
  199. int turnserver_accept_tcp_client_data_connection(turn_turnserver *server, tcp_connection_id tcid, stun_tid *tid, ioa_socket_handle s, int message_integrity, ioa_net_data *nd, int can_resume);
  200. int report_turn_session_info(turn_turnserver *server, ts_ur_super_session *ss, int force_invalid);
  201. turn_time_t get_turn_server_time(turn_turnserver *server);
  202. void turn_cancel_session(turn_turnserver *server, turnsession_id sid);
  203. ///////////////////////////////////////////
  204. #ifdef __cplusplus
  205. }
  206. #endif
  207. #endif //__TURN_SERVER__