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 /*
32  * IO Abstraction library
33  */
34 
35 #ifndef __IOA_LIB__
36 #define __IOA_LIB__
37 
38 #include "ns_turn_ioaddr.h"
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 ////////////// forward declarations ////////
45 
46 struct _ts_ur_super_session;
47 typedef struct _ts_ur_super_session ts_ur_super_session;
48 
49 struct _tcp_connection;
50 typedef struct _tcp_connection tcp_connection;
51 
52 
53 ////////////// Mutexes /////////////////////
54 
55 struct _turn_mutex {
56   uint32_t data;
57   void* mutex;
58 };
59 
60 typedef struct _turn_mutex turn_mutex;
61 
62 int turn_mutex_init(turn_mutex* mutex);
63 int turn_mutex_init_recursive(turn_mutex* mutex);
64 
65 int turn_mutex_lock(const turn_mutex *mutex);
66 int turn_mutex_unlock(const turn_mutex *mutex);
67 
68 int turn_mutex_destroy(turn_mutex* mutex);
69 
70 #define TURN_MUTEX_DECLARE(mutex) turn_mutex mutex;
71 #define TURN_MUTEX_INIT(mutex) turn_mutex_init(mutex)
72 #define TURN_MUTEX_INIT_RECURSIVE(mutex) turn_mutex_init_recursive(mutex)
73 #define TURN_MUTEX_LOCK(mutex) turn_mutex_lock(mutex)
74 #define TURN_MUTEX_UNLOCK(mutex) turn_mutex_unlock(mutex)
75 #define TURN_MUTEX_DESTROY(mutex) turn_mutex_destroy(mutex)
76 
77 /////// Sockets //////////////////////////////
78 
79 #define IOA_EV_TIMEOUT	0x01
80 #define IOA_EV_READ		0x02
81 #define IOA_EV_WRITE	0x04
82 #define IOA_EV_SIGNAL	0x08
83 #define IOA_EV_CLOSE	0x10
84 
85 enum _SOCKET_TYPE {
86 	UNKNOWN_SOCKET=0,
87 	TCP_SOCKET=6,
88 	UDP_SOCKET=17,
89 	TLS_SOCKET=56,
90 	SCTP_SOCKET=132,
91 	TLS_SCTP_SOCKET=133,
92 	DTLS_SOCKET=250,
93 	TCP_SOCKET_PROXY=253,
94 	TENTATIVE_SCTP_SOCKET=254,
95 	TENTATIVE_TCP_SOCKET=255
96 };
97 
98 typedef enum _SOCKET_TYPE SOCKET_TYPE;
99 
100 enum _SOCKET_APP_TYPE {
101 	UNKNOWN_APP_SOCKET,
102 	CLIENT_SOCKET,
103 	HTTP_CLIENT_SOCKET,
104 	HTTPS_CLIENT_SOCKET,
105 	RELAY_SOCKET,
106 	RELAY_RTCP_SOCKET,
107 	TCP_CLIENT_DATA_SOCKET,
108 	TCP_RELAY_DATA_SOCKET,
109 	LISTENER_SOCKET
110 };
111 
112 typedef enum _SOCKET_APP_TYPE SOCKET_APP_TYPE;
113 
114 struct _ioa_socket;
115 typedef struct _ioa_socket ioa_socket;
116 typedef ioa_socket *ioa_socket_handle;
117 
118 struct _ioa_engine;
119 typedef struct _ioa_engine ioa_engine;
120 typedef ioa_engine *ioa_engine_handle;
121 
122 typedef void *ioa_timer_handle;
123 
124 typedef void *ioa_network_buffer_handle;
125 
126 /* event data for net event */
127 typedef struct _ioa_net_data {
128 	ioa_addr			src_addr;
129 	ioa_network_buffer_handle	nbh;
130 	int				recv_ttl;
131 	int				recv_tos;
132 } ioa_net_data;
133 
134 /* Callback on TCP connection completion */
135 typedef void (*connect_cb)(int success, void *arg);
136 /* Callback on accepted socket from TCP relay endpoint */
137 typedef void (*accept_cb)(ioa_socket_handle s, void *arg);
138 
139 ////////// REALM ////////////
140 
141 struct _realm_options_t;
142 typedef struct _realm_options_t realm_options_t;
143 
144 //////// IP White/black listing ///////////
145 
146 struct _ip_range {
147 	char str[257];
148 	char realm[513];
149 	ioa_addr_range enc;
150 };
151 
152 typedef struct _ip_range ip_range_t;
153 
154 struct _ip_range_list {
155 	ip_range_t *rs;
156 	size_t ranges_number;
157 };
158 
159 typedef struct _ip_range_list ip_range_list_t;
160 
161 void ioa_lock_whitelist(ioa_engine_handle e);
162 void ioa_unlock_whitelist(ioa_engine_handle e);
163 const ip_range_list_t* ioa_get_whitelist(ioa_engine_handle e);
164 
165 void ioa_lock_blacklist(ioa_engine_handle e);
166 void ioa_unlock_blacklist(ioa_engine_handle e);
167 const ip_range_list_t* ioa_get_blacklist(ioa_engine_handle e);
168 
169 ////////////////////////////////////////////
170 
171 /*
172  * Network buffer functions
173  */
174 ioa_network_buffer_handle ioa_network_buffer_allocate(ioa_engine_handle e);
175 void ioa_network_buffer_header_init(ioa_network_buffer_handle nbh);
176 uint8_t *ioa_network_buffer_data(ioa_network_buffer_handle nbh);
177 size_t ioa_network_buffer_get_size(ioa_network_buffer_handle nbh);
178 size_t ioa_network_buffer_get_capacity(ioa_network_buffer_handle nbh);
179 size_t ioa_network_buffer_get_capacity_udp(void);
180 void ioa_network_buffer_set_size(ioa_network_buffer_handle nbh, size_t len);
181 void ioa_network_buffer_add_offset_size(ioa_network_buffer_handle nbh, uint16_t offset, uint8_t coffset, size_t len);
182 uint16_t ioa_network_buffer_get_offset(ioa_network_buffer_handle nbh);
183 uint8_t ioa_network_buffer_get_coffset(ioa_network_buffer_handle nbh);
184 void ioa_network_buffer_delete(ioa_engine_handle e, ioa_network_buffer_handle nbh);
185 
186 /*
187  * Status reporting functions
188  */
189 void turn_report_allocation_set(void *a, turn_time_t lifetime, int refresh);
190 void turn_report_allocation_delete(void *a);
191 void turn_report_session_usage(void *session, int force_invalid);
192 
193 /*
194  * Network event handler callback
195  * chnum parameter is just an optimisation hint -
196  * the function must work correctly when chnum=0
197  * (when no hint information is available).
198  */
199 typedef void (*ioa_net_event_handler)(ioa_socket_handle s, int event_type, ioa_net_data *data, void *ctx, int can_resume);
200 
201 /*
202  * Timer callback
203  */
204 typedef void (*ioa_timer_event_handler)(ioa_engine_handle e, void *ctx);
205 
206 /* timers */
207 
208 ioa_timer_handle set_ioa_timer(ioa_engine_handle e, int secs, int ms, ioa_timer_event_handler cb, void *ctx, int persist, const char *txt);
209 void stop_ioa_timer(ioa_timer_handle th);
210 void delete_ioa_timer(ioa_timer_handle th);
211 #define IOA_EVENT_DEL(E) do { if(E) { delete_ioa_timer(E); E = NULL; } } while(0)
212 
213 ioa_socket_handle create_unbound_relay_ioa_socket(ioa_engine_handle e, int family, SOCKET_TYPE st, SOCKET_APP_TYPE sat);
214 
215 void inc_ioa_socket_ref_counter(ioa_socket_handle s);
216 
217 /* Relay socket handling */
218 /*
219  * event_port == -1: no rtcp;
220  * event_port == 0: reserve rtcp;
221  * even_port == +1: reserve and bind rtcp.
222  */
223 int create_relay_ioa_sockets(ioa_engine_handle e, ioa_socket_handle client_s,
224 				int address_family, uint8_t transport,
225 				int even_port, ioa_socket_handle *rtp_s, ioa_socket_handle *rtcp_s,
226 				uint64_t *out_reservation_token, int *err_code, const uint8_t **reason,
227 				accept_cb acb, void *acbarg);
228 
229 ioa_socket_handle  ioa_create_connecting_tcp_relay_socket(ioa_socket_handle s, ioa_addr *peer_addr, connect_cb cb, void *arg);
230 
231 int get_ioa_socket_from_reservation(ioa_engine_handle e, uint64_t in_reservation_token, ioa_socket_handle *s);
232 
233 int get_ioa_socket_address_family(ioa_socket_handle s);
234 int is_stream_socket(int st);
235 int is_tcp_socket(int st);
236 int is_sctp_socket(int st);
237 const char* socket_type_name(SOCKET_TYPE st);
238 const char* get_ioa_socket_cipher(ioa_socket_handle s);
239 const char* get_ioa_socket_ssl_method(ioa_socket_handle s);
240 SOCKET_TYPE get_ioa_socket_type(ioa_socket_handle s);
241 SOCKET_APP_TYPE get_ioa_socket_app_type(ioa_socket_handle s);
242 const char* get_ioa_socket_tls_method(ioa_socket_handle s);
243 const char* get_ioa_socket_tls_cipher(ioa_socket_handle s);
244 void set_ioa_socket_app_type(ioa_socket_handle s, SOCKET_APP_TYPE sat);
245 ioa_addr* get_local_addr_from_ioa_socket(ioa_socket_handle s);
246 ioa_addr* get_remote_addr_from_ioa_socket(ioa_socket_handle s);
247 int get_local_mtu_ioa_socket(ioa_socket_handle s);
248 ts_ur_super_session *get_ioa_socket_session(ioa_socket_handle s);
249 void set_ioa_socket_session(ioa_socket_handle s, ts_ur_super_session *ss);
250 void clear_ioa_socket_session_if(ioa_socket_handle s, void *ss);
251 tcp_connection *get_ioa_socket_sub_session(ioa_socket_handle s);
252 void set_ioa_socket_sub_session(ioa_socket_handle s, tcp_connection *tc);
253 int register_callback_on_ioa_socket(ioa_engine_handle e, ioa_socket_handle s, int event_type, ioa_net_event_handler cb, void *ctx, int clean_preexisting);
254 int send_data_from_ioa_socket_nbh(ioa_socket_handle s, ioa_addr* dest_addr, ioa_network_buffer_handle nbh, int ttl, int tos, int *skip);
255 void close_ioa_socket(ioa_socket_handle s);
256 #define IOA_CLOSE_SOCKET(S) do { if(S) { close_ioa_socket(S); S = NULL; } } while(0)
257 ioa_socket_handle detach_ioa_socket(ioa_socket_handle s);
258 void detach_socket_net_data(ioa_socket_handle s);
259 int set_df_on_ioa_socket(ioa_socket_handle s, int value);
260 void set_do_not_use_df(ioa_socket_handle s);
261 int ioa_socket_tobeclosed(ioa_socket_handle s);
262 void set_ioa_socket_tobeclosed(ioa_socket_handle s);
263 void close_ioa_socket_after_processing_if_necessary(ioa_socket_handle s);
264 
265 ////////////////// Base64 /////////////////////////////
266 
267 char *base64_encode(const unsigned char *data,
268                     size_t input_length,
269                     size_t *output_length);
270 
271 void build_base64_decoding_table(void);
272 
273 unsigned char *base64_decode(const char *data,
274                              size_t input_length,
275                              size_t *output_length);
276 
277 ///////////// Realm ///////////////////
278 
279 void get_default_realm_options(realm_options_t* ro);
280 int get_realm_options_by_origin(char *origin, realm_options_t* ro);
281 void get_realm_options_by_name(char *realm, realm_options_t* ro);
282 int get_canonic_origin(const char* o, char *co, int sz);
283 int get_default_protocol_port(const char* scheme, size_t slen);
284 
285 ///////////// HTTP ////////////////////
286 
287 void handle_http_echo(ioa_socket_handle s);
288 
289 ///////////// ACME /////////////////////
290 
291 int try_acme_redirect(char *req, size_t len, const char *url, ioa_socket_handle s);
292 
293 ///////////// ACME /////////////////////
294 
295 int try_acme_redirect(char *req, size_t len, const char *url, ioa_socket_handle s);
296 
297 ///////////////////////////////////////
298 
299 #ifdef __cplusplus
300 }
301 #endif
302 
303 #endif /* __IOA_LIB__ */
304