summaryrefslogtreecommitdiff
path: root/pjnath/include/pjnath/ice.h
blob: f42a793badec17959bbff1161f1c6e0e78de7034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* $Id$ */
/* 
 * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */
#ifndef __PJNATH_ICE_SOCK_H__
#define __PJNATH_ICE_SOCK_H__

/**
 * @file ice_sock.h
 * @brief ICE socket.
 */
#include <pjnath/types.h>
#include <pjnath/stun_session.h>
#include <pjlib-util/resolver.h>
#include <pj/sock.h>

PJ_BEGIN_DECL


/* **************************************************************************/
/**
 * @defgroup PJNATH_ICE_SOCK ICE Socket
 * @brief High level ICE socket abstraction.
 * @ingroup PJNATH
 * @{
 */

/**
 * This enumeration describes the type of an ICE candidate.
 */
typedef enum pj_ice_cand_type
{
    PJ_ICE_CAND_TYPE_HOST,
    PJ_ICE_CAND_TYPE_MAPPED,
    PJ_ICE_CAND_TYPE_PEER_MAPPED,
    PJ_ICE_CAND_TYPE_RELAYED
} pj_ice_cand_type;

/**
 *
 */
enum pj_ice_type_pref
{
    PJ_ICE_HOST_PREF	    = 126,
    PJ_ICE_MAPPED_PREF	    = 100,
    PJ_ICE_PEER_MAPPED_PREF = 110,
    PJ_ICE_RELAYED_PREF	    = 0
};

typedef struct pj_ice pj_ice;


#define PJ_ICE_MAX_CAND	    16
#define PJ_ICE_MAX_COMP	    8
#define PJ_ICE_MAX_CHECKS   32

/**
 * ICE component
 */
typedef struct pj_ice_comp
{
    unsigned	    comp_id;
    pj_sock_t	    sock;
} pj_ice_comp;


/**
 * This structure describes an ICE candidate.
 */
typedef struct pj_ice_cand
{
    pj_uint32_t		 comp_id;
    pj_ice_cand_type	 type;
    pj_str_t		 foundation;
    pj_uint32_t		 prio;
    pj_sockaddr		 addr;
    pj_sockaddr		 base_addr;
    pj_sockaddr		 srv_addr;
} pj_ice_cand;

typedef enum pj_ice_check_state
{
    PJ_ICE_CHECK_STATE_FROZEN,
    PJ_ICE_CHECK_STATE_WAITING,
    PJ_ICE_CHECK_STATE_IN_PROGRESS,
    PJ_ICE_CHECK_STATE_SUCCEEDED,
    PJ_ICE_CHECK_STATE_FAILED
} pj_ice_check_state;


typedef struct pj_ice_check
{
    unsigned		cand_id;
    pj_uint32_t		comp_id;
    pj_str_t		foundation;

    pj_uint64_t		check_prio;
    pj_ice_check_state	check_state;

    pj_ice_cand_type	rem_type;
    pj_str_t		rem_foundation;
    pj_uint32_t		rem_prio;
    pj_sockaddr		rem_addr;
    pj_sockaddr		rem_base_addr;
} pj_ice_check;


typedef enum pj_ice_checklist_state
{
    PJ_ICE_CHECKLIST_ST_IDLE,
    PJ_ICE_CHECKLIST_ST_RUNNING,
    PJ_ICE_CHECKLIST_ST_COMPLETED
} pj_ice_checklist_state;

typedef struct pj_ice_checklist
{
    pj_ice_checklist_state   state;
    unsigned		     count;
    pj_ice_check	     checks[PJ_ICE_MAX_CHECKS];
} pj_ice_checklist;


/**
 * ICE sock callback.
 */
typedef struct pj_ice_cb
{
    pj_status_t (*on_send_pkt)(pj_ice *ice, 
			       const void *pkt, pj_size_t size,
			       const pj_sockaddr_t *dst_addr,
			       unsigned addr_len);
} pj_ice_cb;


typedef enum pj_ice_state
{
    PJ_ICE_STATE_INIT,
    PJ_ICE_STATE_GATHERING,
    PJ_ICE_STATE_CAND_COMPLETE,
    PJ_ICE_STATE_CHECKING,
    PJ_ICE_STATE_COMPLETE,
    PJ_ICE_STATE_RESV_ERROR
} pj_ice_state;

typedef enum pj_ice_role
{
    PJ_ICE_ROLE_CONTROLLED,
    PJ_ICE_ROLE_CONTROLLING
} pj_ice_role;

/**
 * ICE structure.
 */
struct pj_ice
{
    char		obj_name[PJ_MAX_OBJ_NAME];

    pj_pool_t		*pool;
    pj_mutex_t		*mutex;
    int			 af;
    int			 sock_type;
    pj_ice_role		 role;
    pj_ice_state	 state;

    pj_ice_cb		 cb;

    /* STUN credentials */
    pj_str_t		 tx_uname;
    pj_str_t		 tx_pass;
    pj_str_t		 rx_uname;
    pj_str_t		 rx_pass;

    /* Components */
    unsigned		 comp_cnt;
    pj_ice_comp		 comp[PJ_ICE_MAX_COMP];

    /* Local candidates */
    unsigned		 lcand_cnt;
    pj_ice_cand		 lcand[PJ_ICE_MAX_CAND];

    /* Remote candidates */
    unsigned		 rcand_cnt;
    pj_ice_cand		 rcand[PJ_ICE_MAX_CAND];

    /* Checklists */
    pj_ice_checklist	 cklist;
    pj_ice_checklist	 valid_list;

    /* STUN servers */
    pj_dns_resolver	*resv;
    pj_dns_async_query	*resv_q;
    pj_bool_t		 relay_enabled;
    pj_sockaddr		 stun_srv;

    /* STUN sessions */
    pj_stun_session	*tx_sess;
    pj_stun_session	*rx_sess;
};


PJ_DECL(pj_status_t) pj_ice_create(pj_stun_config *cfg,
				   const char *name,
				   pj_ice_role role,
				   const pj_ice_cb *cb,
				   int af,
				   int sock_type,
				   pj_ice **p_ice);
PJ_DECL(pj_status_t) pj_ice_destroy(pj_ice *ice);
PJ_DECL(pj_status_t) pj_ice_set_srv(pj_ice *ice,
				    pj_bool_t enable_relay,
				    pj_dns_resolver *resolver,
				    const pj_str_t *domain);
PJ_DECL(pj_status_t) pj_ice_set_srv_addr(pj_ice *ice,
					 pj_bool_t enable_relay,
					 const pj_sockaddr_t *srv_addr,
					 unsigned addr_len);
PJ_DECL(pj_status_t) pj_ice_add_comp(pj_ice *ice,
				     unsigned comp_id,
				     const pj_sockaddr_t *local_addr,
				     unsigned addr_len);
PJ_DECL(pj_status_t) pj_ice_add_sock_comp(pj_ice *ice,
					  unsigned comp_id,
					  pj_sock_t sock);
PJ_DECL(pj_status_t) pj_ice_set_credentials(pj_ice *ice,
					    const pj_str_t *local_ufrag,
					    const pj_str_t *local_pass,
					    const pj_str_t *remote_ufrag,
					    const pj_str_t *remote_pass);
PJ_DECL(pj_status_t) pj_ice_start_gather(pj_ice *ice,
					 unsigned flags);

PJ_DECL(pj_status_t) pj_ice_add_cand(pj_ice *ice,
				     unsigned comp_id,
				     pj_ice_cand_type type,
				     pj_uint16_t local_pref,
				     const pj_str_t *foundation,
				     const pj_sockaddr_t *addr,
				     const pj_sockaddr_t *base_addr,
				     const pj_sockaddr_t *srv_addr,
				     int addr_len,
				     unsigned *cand_id);

PJ_DECL(unsigned) pj_ice_get_cand_cnt(pj_ice *ice);
PJ_DECL(pj_status_t) pj_ice_enum_cands(pj_ice *ice,
				       unsigned sort_by,
				       unsigned *p_count,
				       unsigned cand_ids[]);
PJ_DECL(unsigned) pj_ice_get_default_cand(pj_ice *ice,
					  int *cand_id);
PJ_DECL(pj_status_t) pj_ice_get_cand(pj_ice *ice,
				     unsigned cand_id,
				     pj_ice_cand **p_cand);

PJ_DECL(pj_status_t) pj_ice_create_check_list(pj_ice *ice,
					      pj_bool_t is_remote_offer,
					      unsigned rem_cand_cnt,
					      const pj_ice_cand rem_cand[]);

PJ_DECL(pj_status_t) pj_ice_start_check(pj_ice *ice);


/**
 * @}
 */


PJ_END_DECL


#endif	/* __PJNATH_ICE_SOCK_H__ */