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
|
/* $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.h
* @brief ICE.
*/
#include <pjnath/types.h>
#include <pjnath/stun_session.h>
#include <pj/sock.h>
#include <pj/timer.h>
/**
* @defgroup PJNATH_ICE Interactive Connectivity Establishment (ICE)
* @brief Interactive Connectivity Establishment (ICE)
* @ingroup PJNATH
*/
PJ_BEGIN_DECL
/**
* @defgroup PJNATH_ICE_STREAM Transport Independent ICE Media Stream
* @brief Transport Independent ICE Media Stream
* @ingroup PJNATH_ICE
* @{
*/
/**
* This enumeration describes the type of an ICE candidate.
*/
typedef enum pj_ice_cand_type
{
PJ_ICE_CAND_TYPE_HOST,
PJ_ICE_CAND_TYPE_SRFLX,
PJ_ICE_CAND_TYPE_PRFLX,
PJ_ICE_CAND_TYPE_RELAYED
} pj_ice_cand_type;
/**
*
*/
enum pj_ice_type_pref
{
PJ_ICE_HOST_PREF = 126,
PJ_ICE_SRFLX_PREF = 100,
PJ_ICE_PRFLX_PREF = 110,
PJ_ICE_RELAYED_PREF = 0
};
typedef struct pj_ice pj_ice;
typedef struct pj_ice_check pj_ice_check;
#define PJ_ICE_MAX_CAND 16
#define PJ_ICE_MAX_COMP 8
#define PJ_ICE_MAX_CHECKS 32
#define PJ_ICE_TA_VAL 20
/**
* ICE component
*/
typedef struct pj_ice_comp
{
pj_ice_check *valid_check;
} 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 rel_addr;
pj_stun_session *stun_sess;
} 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;
struct pj_ice_check
{
pj_ice_cand *lcand;
pj_ice_cand *rcand;
pj_uint64_t prio;
pj_ice_check_state state;
pj_stun_tx_data *tdata;
pj_bool_t nominated;
pj_status_t err_code;
};
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_timer_entry timer;
} pj_ice_checklist;
/**
* ICE sock callback.
*/
typedef struct pj_ice_cb
{
void (*on_ice_complete)(pj_ice *ice, pj_status_t status);
pj_status_t (*on_tx_pkt)(pj_ice *ice, unsigned comp_id,
unsigned cand_id,
const void *pkt, pj_size_t size,
const pj_sockaddr_t *dst_addr,
unsigned dst_addr_len);
void (*on_rx_data)(pj_ice *ice, unsigned comp_id,
void *pkt, pj_size_t size,
const pj_sockaddr_t *src_addr,
unsigned src_addr_len);
} pj_ice_cb;
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;
void *user_data;
pj_mutex_t *mutex;
pj_ice_role role;
pj_bool_t is_complete;
pj_status_t ice_status;
pj_ice_cb cb;
pj_stun_config stun_cfg;
/* STUN credentials */
pj_str_t tx_ufrag;
pj_str_t tx_uname;
pj_str_t tx_pass;
pj_str_t rx_ufrag;
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];
/* Checklist */
pj_ice_checklist clist;
/* Valid list */
pj_ice_checklist valid_list;
};
PJ_DECL(const char*) pj_ice_get_cand_type_name(pj_ice_cand_type type);
PJ_DECL(pj_status_t) pj_ice_create(pj_stun_config *stun_cfg,
const char *name,
pj_ice_role role,
unsigned comp_cnt,
const pj_ice_cb *cb,
const pj_str_t *local_ufrag,
const pj_str_t *local_passwd,
pj_ice **p_ice);
PJ_DECL(pj_status_t) pj_ice_destroy(pj_ice *ice);
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 *rel_addr,
int addr_len,
unsigned *cand_id);
PJ_DECL(pj_status_t) pj_ice_find_default_cand(pj_ice *ice,
unsigned comp_id,
int *cand_id);
PJ_DECL(pj_status_t) pj_ice_create_check_list(pj_ice *ice,
const pj_str_t *rem_ufrag,
const pj_str_t *rem_passwd,
unsigned rem_cand_cnt,
const pj_ice_cand rem_cand[]);
PJ_DECL(pj_status_t) pj_ice_start_check(pj_ice *ice);
PJ_DECL(pj_status_t) pj_ice_send_data(pj_ice *ice,
unsigned comp_id,
const void *data,
pj_size_t data_len);
PJ_DECL(pj_status_t) pj_ice_on_rx_pkt(pj_ice *ice,
unsigned comp_id,
unsigned cand_id,
void *pkt,
pj_size_t pkt_size,
const pj_sockaddr_t *src_addr,
int src_addr_len);
/**
* @}
*/
PJ_END_DECL
#endif /* __PJNATH_ICE_SOCK_H__ */
|