ns_ioalib_impl.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. /*
  31. * IO Abstraction library
  32. */
  33. #ifndef __IOA_LIBIMPL__
  34. #define __IOA_LIBIMPL__
  35. #include <event2/event.h>
  36. #include <event2/bufferevent.h>
  37. #include <event2/buffer.h>
  38. #include <event2/thread.h>
  39. #include <openssl/ssl.h>
  40. #include "ns_turn_ioalib.h"
  41. #include "turn_ports.h"
  42. #include "ns_turn_maps_rtcp.h"
  43. #include "ns_turn_maps.h"
  44. #include "ns_turn_server.h"
  45. #include "apputils.h"
  46. #include "stun_buffer.h"
  47. #include "userdb.h"
  48. #include "ns_sm.h"
  49. #include <pthread.h>
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. //////////////////////////////////////////////////////
  54. #define MAX_BUFFER_QUEUE_SIZE_PER_ENGINE (64)
  55. #define MAX_SOCKET_BUFFER_BACKLOG (16)
  56. #define BUFFEREVENT_HIGH_WATERMARK (128<<10)
  57. #define BUFFEREVENT_MAX_UDP_TO_TCP_WRITE (64<<9)
  58. #define BUFFEREVENT_MAX_TCP_TO_TCP_WRITE (192<<10)
  59. typedef struct _stun_buffer_list_elem {
  60. struct _stun_buffer_list_elem *next;
  61. stun_buffer buf;
  62. } stun_buffer_list_elem;
  63. typedef struct _stun_buffer_list {
  64. stun_buffer_list_elem *head;
  65. size_t tsz;
  66. } stun_buffer_list;
  67. /*
  68. * New connection callback
  69. */
  70. struct cb_socket_message {
  71. turnserver_id id;
  72. tcp_connection_id connection_id;
  73. stun_tid tid;
  74. ioa_socket_handle s;
  75. int message_integrity;
  76. ioa_net_data nd;
  77. int can_resume;
  78. };
  79. struct cancelled_session_message {
  80. turnsession_id id;
  81. };
  82. struct relay_server {
  83. turnserver_id id;
  84. super_memory_t* sm;
  85. struct event_base* event_base;
  86. struct bufferevent *in_buf;
  87. struct bufferevent *out_buf;
  88. struct bufferevent *auth_in_buf;
  89. struct bufferevent *auth_out_buf;
  90. ioa_engine_handle ioa_eng;
  91. turn_turnserver server;
  92. pthread_t thr;
  93. };
  94. struct message_to_relay {
  95. MESSAGE_TO_RELAY_TYPE t;
  96. struct relay_server *relay_server;
  97. union {
  98. struct socket_message sm;
  99. struct cb_socket_message cb_sm;
  100. struct cancelled_session_message csm;
  101. } m;
  102. };
  103. struct relay_server;
  104. typedef struct relay_server *relay_server_handle;
  105. typedef int (*ioa_engine_new_connection_event_handler)(ioa_engine_handle e, struct message_to_relay *sm);
  106. typedef int (*ioa_engine_udp_event_handler)(relay_server_handle rs, struct message_to_relay *sm);
  107. #define TURN_CMSG_SZ (65536)
  108. #define PREDEF_TIMERS_NUM (14)
  109. extern const int predef_timer_intervals[PREDEF_TIMERS_NUM];
  110. struct _ioa_engine
  111. {
  112. super_memory_t *sm;
  113. struct event_base *event_base;
  114. int deallocate_eb;
  115. int verbose;
  116. turnipports* tp;
  117. rtcp_map *map_rtcp;
  118. stun_buffer_list bufs;
  119. SSL_CTX *tls_ctx_ssl23;
  120. SSL_CTX *tls_ctx_v1_0;
  121. #if defined(SSL_TXT_TLSV1_1)
  122. SSL_CTX *tls_ctx_v1_1;
  123. #if defined(SSL_TXT_TLSV1_2)
  124. SSL_CTX *tls_ctx_v1_2;
  125. #endif
  126. #endif
  127. SSL_CTX *dtls_ctx;
  128. turn_time_t jiffie; /* bandwidth check interval */
  129. ioa_timer_handle timer_ev;
  130. s08bits cmsg[TURN_CMSG_SZ+1];
  131. int predef_timer_intervals[PREDEF_TIMERS_NUM];
  132. struct timeval predef_timers[PREDEF_TIMERS_NUM];
  133. /* Relays */
  134. s08bits relay_ifname[1025];
  135. int default_relays;
  136. size_t relays_number;
  137. size_t relay_addr_counter;
  138. ioa_addr *relay_addrs;
  139. redis_context_handle rch;
  140. };
  141. #define SOCKET_MAGIC (0xABACADEF)
  142. struct _ioa_socket
  143. {
  144. evutil_socket_t fd;
  145. struct _ioa_socket *parent_s;
  146. void *listener_server;
  147. u32bits magic;
  148. ur_addr_map *sockets_container; /* relay container for UDP sockets */
  149. struct bufferevent *bev;
  150. ioa_network_buffer_handle defer_nbh;
  151. int family;
  152. SOCKET_TYPE st;
  153. SOCKET_APP_TYPE sat;
  154. SSL* ssl;
  155. int in_write;
  156. char orig_ctx_type[16];
  157. int bound;
  158. int local_addr_known;
  159. ioa_addr local_addr;
  160. int connected;
  161. ioa_addr remote_addr;
  162. ioa_engine_handle e;
  163. struct event *read_event;
  164. ioa_net_event_handler read_cb;
  165. void *read_ctx;
  166. int done;
  167. ts_ur_super_session* session;
  168. int current_df_relay_flag;
  169. /* RFC6156: if IPv6 is involved, do not use DF: */
  170. int do_not_use_df;
  171. int tobeclosed;
  172. int broken;
  173. int default_ttl;
  174. int current_ttl;
  175. int default_tos;
  176. int current_tos;
  177. stun_buffer_list bufs;
  178. turn_time_t jiffie; /* bandwidth check interval */
  179. band_limit_t jiffie_bytes_read;
  180. band_limit_t jiffie_bytes_write;
  181. /* RFC 6062 ==>> */
  182. //Connection session:
  183. tcp_connection *sub_session;
  184. //Connect:
  185. struct bufferevent *conn_bev;
  186. connect_cb conn_cb;
  187. void *conn_arg;
  188. //Transferable sockets user data
  189. u32bits username_hash;
  190. u32bits realm_hash;
  191. //Accept:
  192. struct evconnlistener *list_ev;
  193. accept_cb acb;
  194. void *acbarg;
  195. /* <<== RFC 6062 */
  196. };
  197. typedef struct _timer_event
  198. {
  199. struct event *ev;
  200. ioa_engine_handle e;
  201. ioa_timer_event_handler cb;
  202. void *ctx;
  203. s08bits* txt;
  204. } timer_event;
  205. ///////////////////////////////////
  206. /* realm */
  207. void create_new_realm(char* name);
  208. int get_realm_data(char* name, realm_params_t* rp);
  209. /* engine handling */
  210. ioa_engine_handle create_ioa_engine(super_memory_t *sm,
  211. struct event_base *eb, turnipports* tp,
  212. const s08bits* relay_if,
  213. size_t relays_number, s08bits **relay_addrs, int default_relays,
  214. int verbose
  215. #if !defined(TURN_NO_HIREDIS)
  216. ,const char* redis_report_connection_string
  217. #endif
  218. );
  219. void set_ssl_ctx(ioa_engine_handle e,
  220. SSL_CTX *tls_ctx_ssl23,
  221. SSL_CTX *tls_ctx_v1_0,
  222. #if defined(SSL_TXT_TLSV1_1)
  223. SSL_CTX *tls_ctx_v1_1,
  224. #if defined(SSL_TXT_TLSV1_2)
  225. SSL_CTX *tls_ctx_v1_2,
  226. #endif
  227. #endif
  228. SSL_CTX *dtls_ctx);
  229. void ioa_engine_set_rtcp_map(ioa_engine_handle e, rtcp_map *rtcpmap);
  230. ioa_socket_handle create_ioa_socket_from_fd(ioa_engine_handle e, ioa_socket_raw fd, ioa_socket_handle parent_s, SOCKET_TYPE st, SOCKET_APP_TYPE sat, const ioa_addr *remote_addr, const ioa_addr *local_addr);
  231. ioa_socket_handle create_ioa_socket_from_ssl(ioa_engine_handle e, ioa_socket_handle parent_s, SSL* ssl, SOCKET_TYPE st, SOCKET_APP_TYPE sat, const ioa_addr *remote_addr, const ioa_addr *local_addr);
  232. int get_a_local_relay(int family, ioa_addr *relay_addr);
  233. void add_socket_to_parent(ioa_socket_handle parent_s, ioa_socket_handle s);
  234. void delete_socket_from_parent(ioa_socket_handle s);
  235. void add_socket_to_map(ioa_socket_handle s, ur_addr_map *amap);
  236. void delete_socket_from_map(ioa_socket_handle s);
  237. int is_connreset(void);
  238. int would_block(void);
  239. int udp_send(ioa_socket_handle s, const ioa_addr* dest_addr, const s08bits* buffer, int len);
  240. int udp_recvfrom(evutil_socket_t fd, ioa_addr* orig_addr, const ioa_addr *like_addr, s08bits* buffer, int buf_size, int *ttl, int *tos, s08bits *ecmsg, int flags, u32bits *errcode);
  241. int ssl_read(evutil_socket_t fd, SSL* ssl, ioa_network_buffer_handle nbh, int verbose);
  242. int set_raw_socket_ttl_options(evutil_socket_t fd, int family);
  243. int set_raw_socket_tos_options(evutil_socket_t fd, int family);
  244. int set_socket_options_fd(evutil_socket_t fd, int tcp, int family);
  245. int set_socket_options(ioa_socket_handle s);
  246. int send_session_cancellation_to_relay(turnsession_id sid);
  247. ///////////////////////// SUPER MEMORY ////////
  248. #define allocate_super_memory_engine(e,size) allocate_super_memory_engine_func(e, size, __FILE__, __FUNCTION__, __LINE__)
  249. void* allocate_super_memory_engine_func(ioa_engine_handle e, size_t size, const char* file, const char* func, int line);
  250. /////////////////////////////////////////////////
  251. #ifdef __cplusplus
  252. }
  253. #endif
  254. #endif /* __IOA_LIBIMPL__ */