a-oRTP  5.2.0
turn.h
1 /*
2  The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) stack.
3  Copyright (C) 2011-2012 Aymeric MOIZARD amoizard_at_osip.org
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
20 /* ====================================================================
21  * The Vovida Software License, Version 1.0
22  *
23  * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  *
29  * 1. Redistributions of source code must retain the above copyright
30  * notice, this list of conditions and the following disclaimer.
31  *
32  * 2. Redistributions in binary form must reproduce the above copyright
33  * notice, this list of conditions and the following disclaimer in
34  * the documentation and/or other materials provided with the
35  * distribution.
36  *
37  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
38  * and "Vovida Open Communication Application Library (VOCAL)" must
39  * not be used to endorse or promote products derived from this
40  * software without prior written permission. For written
41  * permission, please contact vocal@vovida.org.
42  *
43  * 4. Products derived from this software may not be called "VOCAL", nor
44  * may "VOCAL" appear in their name, without prior written
45  * permission of Vovida Networks, Inc.
46  *
47  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
48  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
50  * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
51  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
52  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
56  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
58  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
59  * DAMAGE.
60  *
61  * ====================================================================
62  *
63  * This software consists of voluntary contributions made by Vovida
64  * Networks, Inc. and many individuals on behalf of Vovida Networks,
65  * Inc. For more information on Vovida Networks, Inc., please see
66  * <http://www.vovida.org/>.
67  *
68  */
69 
70 #pragma once
71 
72 #include <ortp/rtpsession.h>
73 
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77 
79  StunTrId tid;
80  char request[STUN_MAX_MESSAGE_SIZE];
81  int requestlen;
82  int state;
83  int nbretransmission;
84  uint64_t timeout;
85 };
86 
87 typedef enum {
88  TurnSessionInit = 0,
89  TurnSessionConnected,
90  TurnSessionSentAllocate,
91  TurnSessionSentChanBind,
92  TurnSessionStable,
93  TurnSessionSentRefresh,
94  TurnSessionSentBindRefresh,
95  TurnSessionSentDeleteAlloc,
96  TurnSessionTerminated,
97  TurnSessionFailed
98 } TurnSessionState;
99 
101  int stun_indication_received;
102  ortp_socket_t fd;
103  int proto;
104  int srcport;
105  int use_relay;
106  bool_t disable_ice;
107  StunIPAddress peerAddress;
108 
109  TurnSessionState state;
110  queue_t send_q;
111  int send_remain;
112  mblk_t *recv_m;
113  int recv_size;
114  int recv_need;
115  int recv_left;
116  int skipping;
117  int have_turn_msg;
118 
119  /* TURN auth data */
120  char auth_username[514]; // MAX 513 bytes
121  char auth_password[514];
122  char auth_realm[763]; // 128 bytes in UTF8
123  char auth_nonce[763]; // 128 bytes in UTF8
124 
125  int bad_nonce_count;
126 
127  struct StunTransaction refresh;
128  struct StunTransaction createpermission;
129  uint16_t channelbind_number;
130  int channelbind_state;
131  uint16_t max_channelbind_number;
132 
133  StunIPAddress relay_addr;
134  StunIPAddress mapped_addr;
135  StunIPAddress bind_addr;
136 
137  time_t alloc_expires;
138  time_t channel_expires;
139 };
140 
141 /* list of state for STUN connectivity check */
142 #define ICE_PRUNED -1
143 #define ICE_FROZEN 0
144 #define ICE_WAITING 1
145 #define ICE_IN_PROGRESS 2 /* STUN request was sent */
146 #define ICE_SUCCEEDED 3
147 #define ICE_FAILED 4 /* no answer or unrecoverable failure */
148 
149 struct SdpCandidate {
150  /* mandatory attributes: draft 19 */
151  char foundation[33];
152  int component_id;
153  char transport[20];
154  int priority;
155  char conn_addr[64];
156  int conn_port;
157  char cand_type[20];
158  char rel_addr[64];
159  int rel_port;
160 
161  /* optionnal attributes: draft 19 */
162  char extension_attr[512]; /* *(SP extension-att-name SP extension-att-value) */
163 };
164 
166  struct SdpCandidate local_candidate;
167  struct SdpCandidate remote_candidate;
168  long long pair_priority;
169  /* additionnal information */
170  int rem_controlling;
171 
172  struct StunTransaction connectivitycheck;
173  struct StunTransaction createpermission;
174 
175  int nominated_flag;
176  unsigned int channelbind_number;
177  int channelbind_state;
178  time_t channel_expires;
179 
180  bool_t sccwuc_received; /* sccwuc: stun connectivity check with use-candidate received */
181  uint16_t num_icc_req_received;
182  uint16_t num_icc_resp_sent;
183  uint16_t num_occ_req_sent;
184  uint16_t num_occ_resp_received;
185  bool_t occ_is_wrong_pair;
186  int rtt;
187  int keepalive_pending;
188 };
189 
190 #define MAX_ICE_CANDIDATES 10
191 
192 struct IceCheckList {
193  struct _OList *cand_pairs;
194  int nominated_pair_index;
195 
196  char loc_ice_ufrag[256];
197  char loc_ice_pwd[256];
198  char rem_ice_ufrag[256];
199  char rem_ice_pwd[256];
200 
201  int rem_controlling;
202  uint64_t tiebreak_value;
203 
204 #define ICE_CL_RUNNING 0
205 #define ICE_CL_COMPLETED 1
206 #define ICE_CL_FAILED 2
207  int state;
208 
209  int RTO;
210  int Ta;
211  uint64_t keepalive_time;
212  char remote_conn_ip[64];
213  int remote_conn_port;
214  char local_conn_ip[64];
215  int local_conn_port;
216 };
217 
218 struct _RtpTurnData {
219  struct TurnSocketData rtp;
220  struct TurnSocketData rtcp;
221  uint64_t rtp_cur_time;
222  uint64_t rtcp_cur_time;
223 };
224 
225 typedef struct _RtpTurnData RtpTurnData;
226 
227 RtpTurnData *rtp_turn_data_new(const char *relay_server, char *username, char *password, int srcport, bool_t prefer_tcp);
228 void rtp_turn_data_destroy(RtpTurnData *data);
229 
230 int rtp_turn_get_relay_addr(RtpTurnData *data, StunIPAddress *relay_addr);
231 
232 int turnBuildAllocate(char *buf, int buf_len, const StunAtrString *username, const StunAtrString *password, const StunAtrString *realm, const StunAtrString *nonce, const TurnAtrReservationToken *resToken, bool_t reservePortPair, int requestIpv6,
233  StunTrId *out_tr_id);
234 
235 int turnBuildChannelBindOrPermission(int use_channelbind, char *buf, int buf_len, const StunAtrString *username, const StunAtrString *password, unsigned int chanNumber, const StunIPAddress *peerAddress, const StunAtrString *realm,
236  const StunAtrString *nonce, StunTrId *tid);
237 
238 int turnBuildRefresh(char *buf, int buf_size, const StunAtrString *username, const StunAtrString *password, const StunAtrString *realm, const StunAtrString *nonce, int lifetime, StunTrId *tid);
239 
241  int opt; /* 0x01: stun, 0x02: turn */
242  char turn_name[256];
243  int turn_port;
244  char turn_login[256];
245  char turn_pwd[256];
246 };
247 
248 #define XX_MAX_TURN_SERVERS 6
249 #define XX_MAX_TURN_SERVERS_2 12 // XX_MAX_TURN_SERVERS * 2 (ipv4 + ipv6)
250 #define XX_MAX_TURN_SERVERS_4 24 // XX_MAX_TURN_SERVERS * 2 * 2 (ipv4 + ipv6 + rtp + rtcp)
251 #define XX_MAX_MEDIA 20
252 #define XX_MAX_SOCKET 40 // XX_MAX_MEDIA * 2
253 
255  StunTrId out_tr_id;
256  StunAtrString username;
257  StunAtrString password;
258  StunAtrString realm;
259  StunAtrString nonce;
260  char stun_buffer[256];
261  int len;
262  uint64_t timeout;
263  int nbretransmission;
264 };
265 
267  StunTrId out_tr_id;
268  StunAtrString username;
269  StunAtrString password;
270  StunAtrString realm;
271  StunAtrString nonce;
272  TurnAtrReservationToken resToken;
273  bool_t reservePortPair;
274  int requestIpv6;
275  int bad_nonce_count;
276  uint32_t lifetime;
277  char stun_buffer[256];
278  int len;
279  uint64_t timeout;
280  int nbretransmission;
281 };
282 
283 #define STUN_STATUS_NONE 0
284 #define STUN_STATUS_INIT 1
285 #define STUN_STATUS_SENT 2
286 #define STUN_STATUS_SUCCESS 3
287 #define STUN_STATUS_FAILURE 4
288 
289 struct stun_result {
290  ortp_socket_t stun_rtp_socket;
291  ortp_socket_t stun_rtcp_socket;
292  struct tmp_stun_buffer stun_rtp_buffer;
293  struct tmp_stun_buffer stun_rtcp_buffer;
294  int stun_rtp_status;
295  int stun_rtcp_status;
296 };
297 
298 struct turn_result {
299  ortp_socket_t turn_rtp_socket;
300  ortp_socket_t turn_rtcp_socket;
301  struct tmp_turn_buffer turn_rtp_buffer;
302  struct tmp_turn_buffer turn_rtcp_buffer;
303  int turn_rtp_status;
304  int turn_rtcp_status;
305  struct SdpCandidate turn_candidate[2];
306 };
307 
309  char local_conn_addr[64];
310  int local_conn_port;
311  struct SdpCandidate stun_candidate[4];
312 
313  struct stun_result stun_results[XX_MAX_TURN_SERVERS_2];
314  struct turn_result turn_results[XX_MAX_TURN_SERVERS_4];
315 
316  int result; /* 0x01 STUN 0x02 TURN */
317  ortp_socket_t turn_socket_rtp_selected;
318 };
319 
321  struct timeval tv;
322  struct ortp_turn_server turn_server[XX_MAX_TURN_SERVERS];
323  struct addrinfo *addrinfo[XX_MAX_TURN_SERVERS_2];
324  int addrinfo_dbg[XX_MAX_TURN_SERVERS_2];
325  struct ortp_candidate_result candidate_result[XX_MAX_MEDIA];
326  int successful;
327 };
328 
329 int ortp_turn_gathering_process_init(struct ortp_turn_gathering_process *tgp);
330 int ortp_turn_gathering_process_add_server(struct ortp_turn_gathering_process *tgp, struct ortp_turn_server *turn_server, int idx_server);
331 int ortp_turn_gathering_process_add_media(struct ortp_turn_gathering_process *tgp, const char *local_conn_addr, int local_conn_port, int stun_src_port, int turn_src_port);
332 int ortp_turn_gathering_process_start(struct ortp_turn_gathering_process *tgp);
333 int ortp_turn_gathering_process_stun_result(struct ortp_turn_gathering_process *tgp, int idx_media, struct SdpCandidate *sc_rtp, struct SdpCandidate *sc_rtcp);
334 struct _RtpTurnData *ortp_turn_gathering_process_turn_result(struct ortp_turn_gathering_process *tgp, int idx_media, struct SdpCandidate *sc_rtp, struct SdpCandidate *sc_rtcp);
335 
336 #ifdef __cplusplus
337 }
338 #endif
The RtpSession api.
Definition: turn.h:165
Definition: turn.h:192
Definition: turn.h:149
Definition: stun.h:267
Definition: stun_udp.h:95
Definition: stun_udp.h:103
Definition: turn.h:78
Definition: stun.h:259
Definition: turn.h:100
Definition: ortp.h:167
Definition: turn.h:218
Definition: str_utils.h:47
Definition: str_utils.h:25
Definition: turn.h:308
Definition: turn.h:320
Definition: turn.h:240
Definition: turn.h:289
Definition: turn.h:254
Definition: turn.h:266
Definition: turn.h:298