mainrelay.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 <limits.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <time.h>
  37. #include <locale.h>
  38. #include <pthread.h>
  39. #include <sched.h>
  40. #include <signal.h>
  41. #include <sys/stat.h>
  42. #include <sys/types.h>
  43. #include <getopt.h>
  44. #if defined(__unix__) || defined(unix) || defined(__APPLE__) || defined(__DARWIN__) || defined(__MACH__)
  45. #include <ifaddrs.h>
  46. #include <libgen.h>
  47. #include <sys/resource.h>
  48. #include <sys/time.h>
  49. #include <sys/utsname.h>
  50. #include <grp.h>
  51. #include <pwd.h>
  52. #endif
  53. #include <event2/buffer.h>
  54. #include <event2/bufferevent.h>
  55. #include "ns_turn_openssl.h"
  56. #include "ns_turn_khash.h"
  57. #include "ns_turn_utils.h"
  58. #include "turn_admin_server.h"
  59. #include "userdb.h"
  60. #include "dtls_listener.h"
  61. #include "tls_listener.h"
  62. #include "ns_turn_maps.h"
  63. #include "ns_turn_server.h"
  64. #include "apputils.h"
  65. #include "ns_ioalib_impl.h"
  66. #include <openssl/aes.h>
  67. #include <openssl/err.h>
  68. #include <openssl/pem.h>
  69. #include <openssl/ssl.h>
  70. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  71. #include <openssl/modes.h>
  72. #endif
  73. #if !defined(TURN_NO_SYSTEMD)
  74. #include <systemd/sd-daemon.h>
  75. #endif
  76. #ifdef __cplusplus
  77. extern "C" {
  78. #endif
  79. ////////////// DEFINES ////////////////////////////
  80. #define DEFAULT_CONFIG_FILE "turnserver.conf"
  81. #define DEFAULT_CIPHER_LIST "DEFAULT"
  82. /* "ALL:eNULL:aNULL:NULL" */
  83. #define DEFAULT_EC_CURVE_NAME "prime256v1"
  84. #define MAX_NUMBER_OF_GENERAL_RELAY_SERVERS ((uint8_t)(0x80))
  85. #define TURNSERVER_ID_BOUNDARY_BETWEEN_TCP_AND_UDP MAX_NUMBER_OF_GENERAL_RELAY_SERVERS
  86. #define TURNSERVER_ID_BOUNDARY_BETWEEN_UDP_AND_TCP TURNSERVER_ID_BOUNDARY_BETWEEN_TCP_AND_UDP
  87. #define DEFAULT_CPUS_NUMBER (2)
  88. /////////// TYPES ///////////////////////////////////
  89. enum _DH_KEY_SIZE { DH_566, DH_1066, DH_2066, DH_CUSTOM };
  90. typedef enum _DH_KEY_SIZE DH_KEY_SIZE;
  91. ///////// LISTENER SERVER TYPES /////////////////////
  92. struct message_to_listener_to_client {
  93. ioa_addr origin;
  94. ioa_addr destination;
  95. ioa_network_buffer_handle nbh;
  96. };
  97. enum _MESSAGE_TO_LISTENER_TYPE { LMT_UNKNOWN, LMT_TO_CLIENT };
  98. typedef enum _MESSAGE_TO_LISTENER_TYPE MESSAGE_TO_LISTENER_TYPE;
  99. struct message_to_listener {
  100. MESSAGE_TO_LISTENER_TYPE t;
  101. union {
  102. struct message_to_listener_to_client tc;
  103. } m;
  104. };
  105. struct listener_server {
  106. rtcp_map *rtcpmap;
  107. turnipports *tp;
  108. struct event_base *event_base;
  109. ioa_engine_handle ioa_eng;
  110. struct bufferevent *in_buf;
  111. struct bufferevent *out_buf;
  112. char **addrs;
  113. ioa_addr **encaddrs;
  114. size_t addrs_number;
  115. size_t services_number;
  116. dtls_listener_relay_server_type ***udp_services;
  117. dtls_listener_relay_server_type ***dtls_services;
  118. dtls_listener_relay_server_type ***aux_udp_services;
  119. };
  120. enum _NET_ENG_VERSION {
  121. NEV_UNKNOWN = 0,
  122. NEV_MIN,
  123. NEV_UDP_SOCKET_PER_SESSION = NEV_MIN,
  124. NEV_UDP_SOCKET_PER_ENDPOINT,
  125. NEV_UDP_SOCKET_PER_THREAD,
  126. NEV_MAX = NEV_UDP_SOCKET_PER_THREAD,
  127. NEV_TOTAL
  128. };
  129. typedef enum _NET_ENG_VERSION NET_ENG_VERSION;
  130. /////////// PARAMS //////////////////////////////////
  131. typedef struct _turn_params_ {
  132. //////////////// OpenSSL group //////////////////////
  133. SSL_CTX *tls_ctx;
  134. SSL_CTX *dtls_ctx;
  135. DH_KEY_SIZE dh_key_size;
  136. char cipher_list[1025];
  137. char ec_curve_name[33];
  138. char ca_cert_file[1025];
  139. char cert_file[1025];
  140. char pkey_file[1025];
  141. char tls_password[513];
  142. char dh_file[1025];
  143. int no_tlsv1;
  144. int no_tlsv1_1;
  145. int no_tlsv1_2;
  146. int no_tls;
  147. int no_dtls;
  148. struct event *tls_ctx_update_ev;
  149. TURN_MUTEX_DECLARE(tls_mutex)
  150. //////////////// Common params ////////////////////
  151. int verbose;
  152. int turn_daemon;
  153. int no_software_attribute;
  154. int web_admin_listen_on_workers;
  155. int do_not_use_config_file;
  156. char pidfile[1025];
  157. char acme_redirect[1025];
  158. //////////////// Listener server /////////////////
  159. int listener_port;
  160. int tls_listener_port;
  161. int alt_listener_port;
  162. int alt_tls_listener_port;
  163. int tcp_proxy_port;
  164. int rfc5780;
  165. int no_udp;
  166. int no_tcp;
  167. int tcp_use_proxy;
  168. vint no_tcp_relay;
  169. vint no_udp_relay;
  170. char listener_ifname[1025];
  171. redis_stats_db_t redis_statsdb;
  172. int use_redis_statsdb;
  173. struct listener_server listener;
  174. ip_range_list_t ip_whitelist;
  175. ip_range_list_t ip_blacklist;
  176. NET_ENG_VERSION net_engine_version;
  177. const char *net_engine_version_txt[NEV_TOTAL];
  178. //////////////// Relay servers /////////////
  179. uint16_t min_port;
  180. uint16_t max_port;
  181. vint check_origin;
  182. vint no_multicast_peers;
  183. vint allow_loopback_peers;
  184. char relay_ifname[1025];
  185. size_t relays_number;
  186. char **relay_addrs;
  187. int default_relays;
  188. // Single global public IP.
  189. // If multiple public IPs are used
  190. // then ioa_addr mapping must be used.
  191. ioa_addr *external_ip;
  192. turnserver_id general_relay_servers_number;
  193. turnserver_id udp_relay_servers_number;
  194. ////////////// Auth server ////////////////
  195. char oauth_server_name[1025];
  196. char domain[1025];
  197. int oauth;
  198. /////////////// AUX SERVERS ////////////////
  199. turn_server_addrs_list_t aux_servers_list;
  200. int udp_self_balance;
  201. /////////////// ALTERNATE SERVERS ////////////////
  202. turn_server_addrs_list_t alternate_servers_list;
  203. turn_server_addrs_list_t tls_alternate_servers_list;
  204. /////////////// stop server ////////////////
  205. int stop_turn_server;
  206. ////////////// MISC PARAMS ////////////////
  207. vint stun_only;
  208. vint no_stun;
  209. vint secure_stun;
  210. int server_relay;
  211. int fingerprint;
  212. char rest_api_separator;
  213. vint stale_nonce;
  214. vint max_allocate_lifetime;
  215. vint channel_lifetime;
  216. vint permission_lifetime;
  217. vint mobility;
  218. turn_credential_type ct;
  219. int use_auth_secret_with_timestamp;
  220. band_limit_t max_bps;
  221. band_limit_t bps_capacity;
  222. band_limit_t bps_capacity_allocated;
  223. vint total_quota;
  224. vint user_quota;
  225. int prometheus;
  226. int prometheus_port;
  227. int prometheus_username_labels;
  228. /////// Users DB ///////////
  229. default_users_db_t default_users_db;
  230. /////// CPUs //////////////
  231. unsigned long cpus;
  232. ///////// Encryption /////////
  233. char secret_key_file[1025];
  234. unsigned char secret_key[1025];
  235. ALLOCATION_DEFAULT_ADDRESS_FAMILY allocation_default_address_family;
  236. int no_auth_pings;
  237. int no_dynamic_ip_list;
  238. int no_dynamic_realms;
  239. vint log_binding;
  240. vint no_stun_backward_compatibility;
  241. vint response_origin_only_with_rfc5780;
  242. } turn_params_t;
  243. extern turn_params_t turn_params;
  244. //////////////// Listener server /////////////////
  245. static inline int get_alt_listener_port(void) {
  246. if (turn_params.alt_listener_port < 1)
  247. return turn_params.listener_port + 1;
  248. return turn_params.alt_listener_port;
  249. }
  250. static inline int get_alt_tls_listener_port(void) {
  251. if (turn_params.alt_tls_listener_port < 1)
  252. return turn_params.tls_listener_port + 1;
  253. return turn_params.alt_tls_listener_port;
  254. }
  255. void add_aux_server(const char *saddr);
  256. void add_alternate_server(const char *saddr);
  257. void del_alternate_server(const char *saddr);
  258. void add_tls_alternate_server(const char *saddr);
  259. void del_tls_alternate_server(const char *saddr);
  260. ////////// Addrs ////////////////////
  261. void add_listener_addr(const char *addr);
  262. int add_relay_addr(const char *addr);
  263. ////////// SSL CTX ////////////////////
  264. void set_ssl_ctx(ioa_engine_handle e, turn_params_t *params);
  265. ///////// Auth ////////////////
  266. void send_auth_message_to_auth_server(struct auth_message *am);
  267. /////////// Setup server ////////
  268. void init_listener(void);
  269. void setup_server(void);
  270. void run_listener_server(struct listener_server *ls);
  271. ////////// BPS ////////////////
  272. band_limit_t get_bps_capacity_allocated(void);
  273. band_limit_t get_bps_capacity(void);
  274. void set_bps_capacity(band_limit_t value);
  275. band_limit_t get_max_bps(void);
  276. void set_max_bps(band_limit_t value);
  277. ///////// AES ENCRYPTION AND DECRYPTION ////////
  278. struct ctr_state {
  279. unsigned char ivec[16];
  280. unsigned int num;
  281. unsigned char ecount[16];
  282. };
  283. void generate_aes_128_key(char *filePath, unsigned char *returnedKey);
  284. unsigned char *base64encode(const void *b64_encode_this, int encode_this_many_bytes);
  285. void encrypt_aes_128(unsigned char *in, const unsigned char *mykey);
  286. unsigned char *base64decode(const void *b64_decode_this, int decode_this_many_bytes);
  287. void decrypt_aes_128(char *in, const unsigned char *mykey);
  288. int decodedTextSize(char *input);
  289. char *decryptPassword(char *in, const unsigned char *mykey);
  290. int init_ctr(struct ctr_state *state, const unsigned char iv[8]);
  291. ///////////////////////////////
  292. #ifdef __cplusplus
  293. }
  294. #endif
  295. #endif //__MAIN_RELAY__