mainrelay.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #if !defined(__MAIN_RELAY__)
  31. #define __MAIN_RELAY__
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <time.h>
  36. #include <unistd.h>
  37. #include <limits.h>
  38. #include <ifaddrs.h>
  39. #include <getopt.h>
  40. #include <locale.h>
  41. #include <libgen.h>
  42. #include <pthread.h>
  43. #include <sched.h>
  44. #include <signal.h>
  45. #include <sys/types.h>
  46. #include <sys/time.h>
  47. #include <sys/stat.h>
  48. #include <sys/resource.h>
  49. #include <sys/utsname.h>
  50. #include <pwd.h>
  51. #include <grp.h>
  52. #include <event2/bufferevent.h>
  53. #include <event2/buffer.h>
  54. #include <openssl/ssl.h>
  55. #include <openssl/bio.h>
  56. #include <openssl/err.h>
  57. #include <openssl/rand.h>
  58. #include <openssl/crypto.h>
  59. #include <openssl/opensslv.h>
  60. #include "ns_turn_utils.h"
  61. #include "ns_turn_khash.h"
  62. #include "userdb.h"
  63. #include "turncli.h"
  64. #include "tls_listener.h"
  65. #include "dtls_listener.h"
  66. #include "ns_turn_server.h"
  67. #include "ns_turn_maps.h"
  68. #include "apputils.h"
  69. #include "ns_ioalib_impl.h"
  70. #ifdef __cplusplus
  71. extern "C" {
  72. #endif
  73. ////////////// DEFINES ////////////////////////////
  74. #define DEFAULT_CONFIG_FILE "turnserver.conf"
  75. #define DEFAULT_CIPHER_LIST "DEFAULT"
  76. /* "ALL:eNULL:aNULL:NULL" */
  77. #define DEFAULT_EC_CURVE_NAME "prime256v1"
  78. #define MAX_NUMBER_OF_GENERAL_RELAY_SERVERS ((u08bits)(0x80))
  79. #define TURNSERVER_ID_BOUNDARY_BETWEEN_TCP_AND_UDP MAX_NUMBER_OF_GENERAL_RELAY_SERVERS
  80. #define TURNSERVER_ID_BOUNDARY_BETWEEN_UDP_AND_TCP TURNSERVER_ID_BOUNDARY_BETWEEN_TCP_AND_UDP
  81. /////////// TYPES ///////////////////////////////////
  82. enum _DH_KEY_SIZE {
  83. DH_566,
  84. DH_1066,
  85. DH_2066,
  86. DH_CUSTOM
  87. };
  88. typedef enum _DH_KEY_SIZE DH_KEY_SIZE;
  89. ///////// LISTENER SERVER TYPES /////////////////////
  90. struct message_to_listener_to_client {
  91. ioa_addr origin;
  92. ioa_addr destination;
  93. ioa_network_buffer_handle nbh;
  94. };
  95. enum _MESSAGE_TO_LISTENER_TYPE {
  96. LMT_UNKNOWN,
  97. LMT_TO_CLIENT
  98. };
  99. typedef enum _MESSAGE_TO_LISTENER_TYPE MESSAGE_TO_LISTENER_TYPE;
  100. struct message_to_listener {
  101. MESSAGE_TO_LISTENER_TYPE t;
  102. union {
  103. struct message_to_listener_to_client tc;
  104. } m;
  105. };
  106. struct listener_server {
  107. rtcp_map* rtcpmap;
  108. turnipports* tp;
  109. struct event_base* event_base;
  110. ioa_engine_handle ioa_eng;
  111. struct bufferevent *in_buf;
  112. struct bufferevent *out_buf;
  113. char **addrs;
  114. ioa_addr **encaddrs;
  115. size_t addrs_number;
  116. size_t services_number;
  117. dtls_listener_relay_server_type ***udp_services;
  118. dtls_listener_relay_server_type ***dtls_services;
  119. dtls_listener_relay_server_type ***aux_udp_services;
  120. };
  121. enum _NET_ENG_VERSION {
  122. NEV_UNKNOWN=0,
  123. NEV_MIN,
  124. NEV_UDP_SOCKET_PER_SESSION=NEV_MIN,
  125. NEV_UDP_SOCKET_PER_ENDPOINT,
  126. NEV_UDP_SOCKET_PER_THREAD,
  127. NEV_MAX=NEV_UDP_SOCKET_PER_THREAD,
  128. NEV_TOTAL
  129. };
  130. typedef enum _NET_ENG_VERSION NET_ENG_VERSION;
  131. ////////////// Auth Server Types ////////////////
  132. struct auth_server {
  133. struct event_base* event_base;
  134. struct bufferevent *in_buf;
  135. struct bufferevent *out_buf;
  136. pthread_t thr;
  137. redis_context_handle rch;
  138. };
  139. /////////// PARAMS //////////////////////////////////
  140. typedef struct _turn_params_ {
  141. //////////////// OpenSSL group //////////////////////
  142. SSL_CTX *tls_ctx_ssl23;
  143. SSL_CTX *tls_ctx_v1_0;
  144. #if defined(SSL_TXT_TLSV1_1)
  145. SSL_CTX *tls_ctx_v1_1;
  146. #if defined(SSL_TXT_TLSV1_2)
  147. SSL_CTX *tls_ctx_v1_2;
  148. #endif
  149. #endif
  150. SSL_CTX *dtls_ctx;
  151. DH_KEY_SIZE dh_key_size;
  152. char cipher_list[1025];
  153. char ec_curve_name[33];
  154. char ca_cert_file[1025];
  155. char cert_file[1025];
  156. char pkey_file[1025];
  157. char tls_password[513];
  158. char dh_file[1025];
  159. int no_sslv2;
  160. int no_sslv3;
  161. int no_tlsv1;
  162. int no_tlsv1_1;
  163. int no_tlsv1_2;
  164. int no_tls;
  165. int no_dtls;
  166. //////////////// Common params ////////////////////
  167. int verbose;
  168. int turn_daemon;
  169. int do_not_use_config_file;
  170. char pidfile[1025];
  171. //////////////// Listener server /////////////////
  172. int listener_port;
  173. int tls_listener_port;
  174. int alt_listener_port;
  175. int alt_tls_listener_port;
  176. int rfc5780;
  177. int no_udp;
  178. int no_tcp;
  179. vint no_tcp_relay;
  180. vint no_udp_relay;
  181. char listener_ifname[1025];
  182. char redis_statsdb[1025];
  183. int use_redis_statsdb;
  184. struct listener_server listener;
  185. ip_range_list_t ip_whitelist;
  186. ip_range_list_t ip_blacklist;
  187. NET_ENG_VERSION net_engine_version;
  188. const char* net_engine_version_txt[NEV_TOTAL];
  189. //////////////// Relay servers /////////////
  190. u16bits min_port;
  191. u16bits max_port;
  192. vint check_origin;
  193. vint no_multicast_peers;
  194. vint no_loopback_peers;
  195. char relay_ifname[1025];
  196. size_t relays_number;
  197. char **relay_addrs;
  198. int default_relays;
  199. // Single global public IP.
  200. // If multiple public IPs are used
  201. // then ioa_addr mapping must be used.
  202. ioa_addr *external_ip;
  203. turnserver_id general_relay_servers_number;
  204. turnserver_id udp_relay_servers_number;
  205. ////////////// Auth server ////////////////
  206. struct auth_server authserver;
  207. char oauth_server_name[1025];
  208. char domain[1025];
  209. int oauth;
  210. /////////////// AUX SERVERS ////////////////
  211. turn_server_addrs_list_t aux_servers_list;
  212. int udp_self_balance;
  213. /////////////// ALTERNATE SERVERS ////////////////
  214. turn_server_addrs_list_t alternate_servers_list;
  215. turn_server_addrs_list_t tls_alternate_servers_list;
  216. int stop_turn_server;
  217. ////////////// MISC PARAMS ////////////////
  218. vint stun_only;
  219. vint no_stun;
  220. vint secure_stun;
  221. int server_relay;
  222. int fingerprint;
  223. SHATYPE shatype;
  224. char rest_api_separator;
  225. vint stale_nonce;
  226. vint mobility;
  227. turn_credential_type ct;
  228. int use_auth_secret_with_timestamp;
  229. band_limit_t max_bps;
  230. band_limit_t bps_capacity;
  231. band_limit_t bps_capacity_allocated;
  232. vint total_quota;
  233. vint user_quota;
  234. /////// Users DB ///////////
  235. default_users_db_t default_users_db;
  236. } turn_params_t;
  237. extern turn_params_t turn_params;
  238. //////////////// Listener server /////////////////
  239. static inline int get_alt_listener_port(void) {
  240. if(turn_params.alt_listener_port<1)
  241. return turn_params.listener_port + 1;
  242. return turn_params.alt_listener_port;
  243. }
  244. static inline int get_alt_tls_listener_port(void) {
  245. if(turn_params.alt_tls_listener_port<1)
  246. return turn_params.tls_listener_port + 1;
  247. return turn_params.alt_tls_listener_port;
  248. }
  249. void add_aux_server(const char *saddr);
  250. void add_alternate_server(const char *saddr);
  251. void del_alternate_server(const char *saddr);
  252. void add_tls_alternate_server(const char *saddr);
  253. void del_tls_alternate_server(const char *saddr);
  254. ////////// Addrs ////////////////////
  255. void add_listener_addr(const char* addr);
  256. int add_relay_addr(const char* addr);
  257. ///////// Auth ////////////////
  258. void send_auth_message_to_auth_server(struct auth_message *am);
  259. /////////// Setup server ////////
  260. void init_listener(void);
  261. void setup_server(void);
  262. void run_listener_server(struct listener_server *ls);
  263. ////////// BPS ////////////////
  264. band_limit_t get_bps_capacity_allocated(void);
  265. band_limit_t get_bps_capacity(void);
  266. void set_bps_capacity(band_limit_t value);
  267. band_limit_t get_max_bps(void);
  268. void set_max_bps(band_limit_t value);
  269. ///////////////////////////////
  270. #ifdef __cplusplus
  271. }
  272. #endif
  273. #endif //__MAIN_RELAY__