d6c488c0ce409fd9bdfedd6b812102511befecf7
[platform/upstream/libnice.git] / agent / agent-priv.h
1 /*
2  * This file is part of the Nice GLib ICE library.
3  *
4  * (C) 2006-2009 Collabora Ltd.
5  *  Contact: Youness Alaoui
6  * (C) 2006-2009 Nokia Corporation. All rights reserved.
7  *  Contact: Kai Vehmanen
8  *
9  * The contents of this file are subject to the Mozilla Public License Version
10  * 1.1 (the "License"); you may not use this file except in compliance with
11  * the License. You may obtain a copy of the License at
12  * http://www.mozilla.org/MPL/
13  *
14  * Software distributed under the License is distributed on an "AS IS" basis,
15  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16  * for the specific language governing rights and limitations under the
17  * License.
18  *
19  * The Original Code is the Nice GLib ICE library.
20  *
21  * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22  * Corporation. All Rights Reserved.
23  *
24  * Contributors:
25  *   Dafydd Harries, Collabora Ltd.
26  *   Youness Alaoui, Collabora Ltd.
27  *   Kai Vehmanen, Nokia
28  *
29  * Alternatively, the contents of this file may be used under the terms of the
30  * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
31  * case the provisions of LGPL are applicable instead of those above. If you
32  * wish to allow use of your version of this file only under the terms of the
33  * LGPL and not to allow others to use your version of this file under the
34  * MPL, indicate your decision by deleting the provisions above and replace
35  * them with the notice and other provisions required by the LGPL. If you do
36  * not delete the provisions above, a recipient may use your version of this
37  * file under either the MPL or the LGPL.
38  */
39
40 #ifndef _NICE_AGENT_PRIV_H
41 #define _NICE_AGENT_PRIV_H
42
43 /* note: this is a private header part of agent.h */
44
45
46 #ifdef HAVE_CONFIG_H
47 # include <config.h>
48 #else
49 #define NICEAPI_EXPORT
50 #endif
51
52 #include <glib.h>
53
54 #include "agent.h"
55
56 /**
57  * NiceInputMessageIter:
58  * @message: index of the message currently being written into
59  * @buffer: index of the buffer currently being written into
60  * @offset: byte offset into the buffer
61  *
62  * Iterator for sequentially writing into an array of #NiceInputMessages,
63  * tracking the current write position (i.e. the index of the next byte to be
64  * written).
65  *
66  * If @message is equal to the number of messages in the associated
67  * #NiceInputMessage array, and @buffer and @offset are zero, the iterator is at
68  * the end of the messages array, and the array is (presumably) full.
69  *
70  * Since: 0.1.5
71  */
72 typedef struct {
73   guint message;
74   guint buffer;
75   gsize offset;
76 } NiceInputMessageIter;
77
78 void
79 nice_input_message_iter_reset (NiceInputMessageIter *iter);
80 gboolean
81 nice_input_message_iter_is_at_end (NiceInputMessageIter *iter,
82     NiceInputMessage *messages, guint n_messages);
83 guint
84 nice_input_message_iter_get_n_valid_messages (NiceInputMessageIter *iter);
85 gboolean
86 nice_input_message_iter_compare (const NiceInputMessageIter *a,
87     const NiceInputMessageIter *b);
88
89
90 #include "socket.h"
91 #include "candidate.h"
92 #include "stream.h"
93 #include "conncheck.h"
94 #include "component.h"
95 #include "random.h"
96 #include "stun/stunagent.h"
97 #include "stun/usages/turn.h"
98 #include "stun/usages/ice.h"
99
100 #ifdef HAVE_GUPNP
101 #include <libgupnp-igd/gupnp-simple-igd-thread.h>
102 #endif
103
104 /* XXX: starting from ICE ID-18, Ta SHOULD now be set according
105  *      to session bandwidth -> this is not yet implemented in NICE */
106
107 #define NICE_AGENT_TIMER_TA_DEFAULT 20      /* timer Ta, msecs (impl. defined) */
108 #define NICE_AGENT_TIMER_TR_DEFAULT 25000   /* timer Tr, msecs (impl. defined) */
109 #define NICE_AGENT_MAX_CONNECTIVITY_CHECKS_DEFAULT 100 /* see RFC 8445 6.1.2.5 */
110
111
112 /* An upper limit to size of STUN packets handled (based on Ethernet
113  * MTU and estimated typical sizes of ICE STUN packet */
114 #define MAX_STUN_DATAGRAM_PAYLOAD    1300
115
116 #define NICE_COMPONENT_MAX_VALID_CANDIDATES 50 /* maximum number of validates remote candidates to keep, the number is arbitrary but hopefully large enough */
117
118 /* A convenient macro to test if the agent is compatible with RFC5245
119  * or OC2007R2. Specifically these two modes share the support
120  * of the regular or aggressive nomination mode */
121 #define NICE_AGENT_IS_COMPATIBLE_WITH_RFC5245_OR_OC2007R2(obj) \
122   ((obj)->compatibility == NICE_COMPATIBILITY_RFC5245 || \
123   (obj)->compatibility == NICE_COMPATIBILITY_OC2007R2)
124
125 struct _NiceAgent
126 {
127   GObject parent;                 /* gobject pointer */
128
129   GMutex agent_mutex;             /* Mutex used for thread-safe lib */
130
131   gboolean full_mode;             /* property: full-mode */
132   gchar *stun_server_ip;          /* property: STUN server IP */
133   guint stun_server_port;         /* property: STUN server port */
134   gchar *proxy_ip;                /* property: Proxy server IP */
135   guint proxy_port;               /* property: Proxy server port */
136   NiceProxyType proxy_type;       /* property: Proxy type */
137   gchar *proxy_username;          /* property: Proxy username */
138   gchar *proxy_password;          /* property: Proxy password */
139   gboolean saved_controlling_mode;/* property: controlling-mode */
140   guint timer_ta;                 /* property: timer Ta */
141   guint max_conn_checks;          /* property: max connectivity checks */
142   gboolean force_relay;           /* property: force relay */
143   guint stun_max_retransmissions; /* property: stun max retransmissions, Rc */
144   guint stun_initial_timeout;     /* property: stun initial timeout, RTO */
145   guint stun_reliable_timeout;    /* property: stun reliable timeout */
146   NiceNominationMode nomination_mode; /* property: Nomination mode */
147   gboolean support_renomination;  /* property: support RENOMINATION STUN attribute */
148   guint idle_timeout;             /* property: conncheck timeout before stop */
149
150   GSList *local_addresses;        /* list of NiceAddresses for local
151                                      interfaces */
152   GSList *streams;                /* list of Stream objects */
153   GSList *pruning_streams;        /* list of Streams current being shut down */
154   GMainContext *main_context;     /* main context pointer */
155   guint next_candidate_id;        /* id of next created candidate */
156   guint next_stream_id;           /* id of next created candidate */
157   NiceRNG *rng;                   /* random number generator */
158   GSList *discovery_list;         /* list of CandidateDiscovery items */
159   GSList *triggered_check_queue;  /* pairs in the triggered check list */
160   guint discovery_unsched_items;  /* number of discovery items unscheduled */
161   GSource *discovery_timer_source; /* source of discovery timer */
162   GSource *conncheck_timer_source; /* source of conncheck timer */
163   GSource *keepalive_timer_source; /* source of keepalive timer */
164   GSList *refresh_list;         /* list of CandidateRefresh items */
165   guint64 tie_breaker;            /* tie breaker (ICE sect 5.2
166                                      "Determining Role" ID-19) */
167   NiceCompatibility compatibility; /* property: Compatibility mode */
168   gboolean media_after_tick;       /* Received media after keepalive tick */
169 #ifdef HAVE_GUPNP
170   GUPnPSimpleIgdThread* upnp;      /* GUPnP Single IGD agent */
171   gboolean upnp_enabled;           /* whether UPnP discovery is enabled */
172   guint upnp_timeout;              /* UPnP discovery timeout */
173   GSList *upnp_mapping;            /* NiceAddresses of cands being mapped */
174   GSource *upnp_timer_source;      /* source of upnp timeout timer */
175 #endif
176   gchar *software_attribute;       /* SOFTWARE attribute */
177   gboolean reliable;               /* property: reliable */
178   gboolean keepalive_conncheck;    /* property: keepalive_conncheck */
179
180   GQueue pending_signals;
181   guint16 rfc4571_expecting_length;
182   gboolean use_ice_udp;
183   gboolean use_ice_tcp;
184   gboolean use_ice_trickle;
185
186   guint conncheck_ongoing_idle_delay; /* ongoing delay before timer stop */
187   gboolean controlling_mode;          /* controlling mode used by the
188                                          conncheck */
189   /* XXX: add pointer to internal data struct for ABI-safe extensions */
190 };
191
192 gboolean
193 agent_find_component (
194   NiceAgent *agent,
195   guint stream_id,
196   guint component_id,
197   NiceStream **stream,
198   NiceComponent **component) G_GNUC_WARN_UNUSED_RESULT;
199
200 NiceStream *agent_find_stream (NiceAgent *agent, guint stream_id);
201
202 void agent_gathering_done (NiceAgent *agent);
203 void agent_signal_gathering_done (NiceAgent *agent);
204
205 void agent_lock (NiceAgent *agent);
206 void agent_unlock (NiceAgent *agent);
207 void agent_unlock_and_emit (NiceAgent *agent);
208
209 void agent_signal_new_selected_pair (
210   NiceAgent *agent,
211   guint stream_id,
212   guint component_id,
213   NiceCandidate *lcandidate,
214   NiceCandidate *rcandidate);
215
216 void agent_signal_component_state_change (
217   NiceAgent *agent,
218   guint stream_id,
219   guint component_id,
220   NiceComponentState state);
221
222 void agent_signal_new_candidate (
223   NiceAgent *agent,
224   NiceCandidate *candidate);
225
226 void agent_signal_new_remote_candidate (NiceAgent *agent, NiceCandidate *candidate);
227
228 void agent_signal_initial_binding_request_received (NiceAgent *agent, NiceStream *stream);
229
230 guint64 agent_candidate_pair_priority (NiceAgent *agent, NiceCandidate *local, NiceCandidate *remote);
231
232 NiceSocket * agent_create_tcp_turn_socket (NiceAgent *agent,
233     NiceStream *stream, NiceComponent *component, NiceSocket *nicesock,
234     NiceAddress *server, NiceRelayType type, gboolean reliable_tcp);
235
236 typedef gboolean (*NiceTimeoutLockedCallback)(NiceAgent *agent,
237     gpointer user_data);
238 void agent_timeout_add_with_context (NiceAgent *agent, GSource **out,
239     const gchar *name, guint interval, NiceTimeoutLockedCallback function,
240     gpointer data);
241 void agent_timeout_add_seconds_with_context (NiceAgent *agent, GSource **out,
242     const gchar *name, guint interval, NiceTimeoutLockedCallback function,
243     gpointer data);
244
245 StunUsageIceCompatibility agent_to_ice_compatibility (NiceAgent *agent);
246 StunUsageTurnCompatibility agent_to_turn_compatibility (NiceAgent *agent);
247 NiceTurnSocketCompatibility agent_to_turn_socket_compatibility (NiceAgent *agent);
248
249 void agent_remove_local_candidate (NiceAgent *agent,
250     NiceCandidate *candidate);
251
252 void nice_agent_init_stun_agent (NiceAgent *agent, StunAgent *stun_agent);
253
254 void _priv_set_socket_tos (NiceAgent *agent, NiceSocket *sock, gint tos);
255
256 void _tcp_sock_is_writable (NiceSocket *sock, gpointer user_data);
257
258 gboolean
259 component_io_cb (
260   GSocket *gsocket,
261   GIOCondition condition,
262   gpointer data);
263
264 gsize
265 memcpy_buffer_to_input_message (NiceInputMessage *message,
266     const guint8 *buffer, gsize buffer_length);
267 guint8 *
268 compact_input_message (const NiceInputMessage *message, gsize *buffer_length);
269
270 guint8 *
271 compact_output_message (const NiceOutputMessage *message, gsize *buffer_length);
272
273 gsize
274 output_message_get_size (const NiceOutputMessage *message);
275
276 gsize
277 input_message_get_size (const NiceInputMessage *message);
278
279 gssize agent_socket_send (NiceSocket *sock, const NiceAddress *addr, gsize len,
280     const gchar *buf);
281
282 guint32
283 nice_candidate_jingle_priority (NiceCandidate *candidate);
284
285 guint32
286 nice_candidate_msn_priority (NiceCandidate *candidate);
287
288 guint32
289 nice_candidate_ice_priority_full (guint type_pref, guint local_pref,
290     guint component_id);
291
292 guint32
293 nice_candidate_ice_priority (const NiceCandidate *candidate,
294     gboolean reliable, gboolean nat_assisted);
295
296 guint32
297 nice_candidate_ms_ice_priority (const NiceCandidate *candidate,
298     gboolean reliable, gboolean nat_assisted);
299
300 guint64
301 nice_candidate_pair_priority (guint32 o_prio, guint32 a_prio);
302
303 #define NICE_CANDIDATE_PAIR_PRIORITY_MAX_SIZE 32
304
305 void
306 nice_candidate_pair_priority_to_string (guint64 prio, gchar *string);
307
308 /*
309  * nice_debug_init:
310  *
311  * Initialize the debugging system. Uses the NICE_DEBUG environment variable
312  * to set the appropriate debugging flags
313  */
314 void nice_debug_init (void);
315
316
317 #ifdef NDEBUG
318 static inline gboolean nice_debug_is_enabled (void) { return FALSE; }
319 static inline gboolean nice_debug_is_verbose (void) { return FALSE; }
320 static inline void nice_debug (const char *fmt, ...) { }
321 static inline void nice_debug_verbose (const char *fmt, ...) { }
322 #else
323 gboolean nice_debug_is_enabled (void);
324 gboolean nice_debug_is_verbose (void);
325 void nice_debug (const char *fmt, ...) G_GNUC_PRINTF (1, 2);
326 void nice_debug_verbose (const char *fmt, ...) G_GNUC_PRINTF (1, 2);
327 #endif
328
329 #if !GLIB_CHECK_VERSION(2, 59, 0)
330 #if __GNUC__ > 6
331 #define G_GNUC_FALLTHROUGH __attribute__((fallthrough))
332 #else
333 #define G_GNUC_FALLTHROUGH
334 #endif /* __GNUC__ */
335 #endif
336
337 #endif /*_NICE_AGENT_PRIV_H */