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 #ifndef __TURN_SESSION__
32 #define __TURN_SESSION__
33 
34 #include "ns_turn_utils.h"
35 #include "ns_turn_maps.h"
36 #include "ns_turn_ioalib.h"
37 #include "ns_turn_allocation.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 ////////// REALM ////////////
44 
45 typedef struct _perf_options_t {
46 
47 	volatile band_limit_t max_bps;
48 	vint total_quota;
49 	vint user_quota;
50 
51 } perf_options_t;
52 
53 struct _realm_options_t {
54 
55 	char name[STUN_MAX_REALM_SIZE + 1];
56 
57 	perf_options_t perf_options;
58 };
59 
60 //////////////// session info //////////////////////
61 
62 typedef uint64_t turnsession_id;
63 
64 #define NONCE_MAX_SIZE (NONCE_LENGTH_32BITS*4+1)
65 
66 typedef uint64_t mobile_id_t;
67 
68 struct _ts_ur_super_session {
69   void* server;
70   turnsession_id id;
71   turn_time_t start_time;
72   ioa_socket_handle client_socket;
73   allocation alloc;
74   ioa_timer_handle to_be_allocated_timeout_ev;
75   int enforce_fingerprints;
76   int is_tcp_relay;
77   int to_be_closed;
78   /* Auth */
79   uint8_t nonce[NONCE_MAX_SIZE];
80   turn_time_t nonce_expiration_time;
81   uint8_t username[STUN_MAX_USERNAME_SIZE+1];
82   hmackey_t hmackey;
83   int hmackey_set;
84   password_t pwd;
85   int quota_used;
86   int oauth;
87   turn_time_t max_session_time_auth;
88   /* Realm */
89   realm_options_t realm_options;
90   int origin_set;
91   char origin[STUN_MAX_ORIGIN_SIZE + 1];
92   /* Stats */
93   uint32_t received_packets;
94   uint32_t sent_packets;
95   uint32_t received_bytes;
96   uint32_t sent_bytes;
97   uint64_t t_received_packets;
98   uint64_t t_sent_packets;
99   uint64_t t_received_bytes;
100   uint64_t t_sent_bytes;
101   uint64_t received_rate;
102   size_t sent_rate;
103   size_t total_rate;
104   uint32_t peer_received_packets;
105   uint32_t peer_sent_packets;
106   uint32_t peer_received_bytes;
107   uint32_t peer_sent_bytes;
108   uint32_t t_peer_received_packets;
109   uint32_t t_peer_sent_packets;
110   uint32_t t_peer_received_bytes;
111   uint32_t t_peer_sent_bytes;
112   uint64_t peer_received_rate;
113   size_t peer_sent_rate;
114   size_t peer_total_rate;
115   /* Mobile */
116   int is_mobile;
117   mobile_id_t mobile_id;
118   mobile_id_t old_mobile_id;
119   char s_mobile_id[33];
120   /* Bandwidth */
121   band_limit_t bps;
122 };
123 
124 ////// Session info for statistics //////
125 
126 #define TURN_ADDR_STR_SIZE (65)
127 #define TURN_MAIN_PEERS_ARRAY_SIZE (5)
128 
129 typedef struct _addr_data {
130 	ioa_addr addr;
131 	char saddr[TURN_ADDR_STR_SIZE];
132 } addr_data;
133 
134 struct turn_session_info {
135 	turnsession_id id;
136 	int valid;
137 	turn_time_t start_time;
138 	turn_time_t expiration_time;
139 	SOCKET_TYPE client_protocol;
140 	SOCKET_TYPE peer_protocol;
141 	char tls_method[17];
142 	char tls_cipher[65];
143 	addr_data local_addr_data;
144 	addr_data remote_addr_data;
145 	addr_data relay_addr_data_ipv4;
146 	addr_data relay_addr_data_ipv6;
147 	uint8_t username[STUN_MAX_USERNAME_SIZE+1];
148 	int enforce_fingerprints;
149 /* Stats */
150 	uint64_t received_packets;
151 	uint64_t sent_packets;
152 	uint64_t received_bytes;
153 	uint64_t sent_bytes;
154 	uint32_t received_rate;
155 	uint32_t sent_rate;
156 	uint32_t total_rate;
157 	uint64_t peer_received_packets;
158 	uint64_t peer_sent_packets;
159 	uint64_t peer_received_bytes;
160 	uint64_t peer_sent_bytes;
161 	uint32_t peer_received_rate;
162 	uint32_t peer_sent_rate;
163 	uint32_t peer_total_rate;
164 /* Mobile */
165 	int is_mobile;
166 /* Peers */
167 	addr_data main_peers_data[TURN_MAIN_PEERS_ARRAY_SIZE];
168 	size_t main_peers_size;
169 	addr_data *extra_peers_data;
170 	size_t extra_peers_size;
171 /* Realm */
172 	char realm[STUN_MAX_REALM_SIZE + 1];
173 	char origin[STUN_MAX_ORIGIN_SIZE + 1];
174 /* Bandwidth */
175 	band_limit_t bps;
176 };
177 
178 void turn_session_info_init(struct turn_session_info* tsi);
179 void turn_session_info_clean(struct turn_session_info* tsi);
180 void turn_session_info_add_peer(struct turn_session_info* tsi, ioa_addr *peer);
181 
182 int turn_session_info_copy_from(struct turn_session_info* tsi, ts_ur_super_session *ss);
183 
184 ////////////// ss /////////////////////
185 
186 allocation* get_allocation_ss(ts_ur_super_session *ss);
187 
188 ///////////////////////////////////////////////////////
189 
190 #ifdef __cplusplus
191 }
192 #endif
193 
194 #endif //__TURN_SESSION__
195