Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* |
michael@0 | 2 | Copyright (c) 2007, Adobe Systems, Incorporated |
michael@0 | 3 | All rights reserved. |
michael@0 | 4 | |
michael@0 | 5 | Redistribution and use in source and binary forms, with or without |
michael@0 | 6 | modification, are permitted provided that the following conditions are |
michael@0 | 7 | met: |
michael@0 | 8 | |
michael@0 | 9 | * Redistributions of source code must retain the above copyright |
michael@0 | 10 | notice, this list of conditions and the following disclaimer. |
michael@0 | 11 | |
michael@0 | 12 | * Redistributions in binary form must reproduce the above copyright |
michael@0 | 13 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 14 | documentation and/or other materials provided with the distribution. |
michael@0 | 15 | |
michael@0 | 16 | * Neither the name of Adobe Systems, Network Resonance nor the names of its |
michael@0 | 17 | contributors may be used to endorse or promote products derived from |
michael@0 | 18 | this software without specific prior written permission. |
michael@0 | 19 | |
michael@0 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 24 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 31 | */ |
michael@0 | 32 | |
michael@0 | 33 | |
michael@0 | 34 | |
michael@0 | 35 | static char *RCSSTRING __UNUSED__="$Id: ice_ctx.c,v 1.2 2008/04/28 17:59:01 ekr Exp $"; |
michael@0 | 36 | |
michael@0 | 37 | #include <csi_platform.h> |
michael@0 | 38 | #include <assert.h> |
michael@0 | 39 | #include <sys/types.h> |
michael@0 | 40 | #ifdef WIN32 |
michael@0 | 41 | #include <winsock2.h> |
michael@0 | 42 | #else |
michael@0 | 43 | #include <sys/socket.h> |
michael@0 | 44 | #include <netinet/in.h> |
michael@0 | 45 | #include <arpa/inet.h> |
michael@0 | 46 | #endif |
michael@0 | 47 | #include <sys/queue.h> |
michael@0 | 48 | #include <string.h> |
michael@0 | 49 | #include <nr_api.h> |
michael@0 | 50 | #include <registry.h> |
michael@0 | 51 | #include "stun.h" |
michael@0 | 52 | #include "ice_ctx.h" |
michael@0 | 53 | #include "ice_reg.h" |
michael@0 | 54 | #include "nr_crypto.h" |
michael@0 | 55 | #include "async_timer.h" |
michael@0 | 56 | #include "util.h" |
michael@0 | 57 | |
michael@0 | 58 | |
michael@0 | 59 | int LOG_ICE = 0; |
michael@0 | 60 | |
michael@0 | 61 | static int nr_ice_random_string(char *str, int len); |
michael@0 | 62 | static int nr_ice_fetch_stun_servers(int ct, nr_ice_stun_server **out); |
michael@0 | 63 | #ifdef USE_TURN |
michael@0 | 64 | static int nr_ice_fetch_turn_servers(int ct, nr_ice_turn_server **out); |
michael@0 | 65 | #endif /* USE_TURN */ |
michael@0 | 66 | static void nr_ice_ctx_destroy_cb(NR_SOCKET s, int how, void *cb_arg); |
michael@0 | 67 | static int nr_ice_ctx_pair_new_trickle_candidates(nr_ice_ctx *ctx, nr_ice_candidate *cand); |
michael@0 | 68 | |
michael@0 | 69 | int nr_ice_fetch_stun_servers(int ct, nr_ice_stun_server **out) |
michael@0 | 70 | { |
michael@0 | 71 | int r,_status; |
michael@0 | 72 | nr_ice_stun_server *servers = 0; |
michael@0 | 73 | int i; |
michael@0 | 74 | NR_registry child; |
michael@0 | 75 | char *addr=0; |
michael@0 | 76 | UINT2 port; |
michael@0 | 77 | in_addr_t addr_int; |
michael@0 | 78 | |
michael@0 | 79 | if(!(servers=RCALLOC(sizeof(nr_ice_stun_server)*ct))) |
michael@0 | 80 | ABORT(R_NO_MEMORY); |
michael@0 | 81 | |
michael@0 | 82 | for(i=0;i<ct;i++){ |
michael@0 | 83 | if(r=NR_reg_get_child_registry(NR_ICE_REG_STUN_SRV_PRFX,i,child)) |
michael@0 | 84 | ABORT(r); |
michael@0 | 85 | /* Assume we have a v4 addr for now */ |
michael@0 | 86 | if(r=NR_reg_alloc2_string(child,"addr",&addr)) |
michael@0 | 87 | ABORT(r); |
michael@0 | 88 | addr_int=inet_addr(addr); |
michael@0 | 89 | if(addr_int==INADDR_NONE){ |
michael@0 | 90 | r_log(LOG_ICE,LOG_ERR,"Invalid address %s;",addr); |
michael@0 | 91 | ABORT(R_BAD_ARGS); |
michael@0 | 92 | } |
michael@0 | 93 | if(r=NR_reg_get2_uint2(child,"port",&port)) { |
michael@0 | 94 | if (r != R_NOT_FOUND) |
michael@0 | 95 | ABORT(r); |
michael@0 | 96 | port = 3478; |
michael@0 | 97 | } |
michael@0 | 98 | if(r=nr_ip4_port_to_transport_addr(ntohl(addr_int), port, IPPROTO_UDP, |
michael@0 | 99 | &servers[i].u.addr)) |
michael@0 | 100 | ABORT(r); |
michael@0 | 101 | servers[i].index=i; |
michael@0 | 102 | servers[i].type = NR_ICE_STUN_SERVER_TYPE_ADDR; |
michael@0 | 103 | RFREE(addr); |
michael@0 | 104 | addr=0; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | *out = servers; |
michael@0 | 108 | |
michael@0 | 109 | _status=0; |
michael@0 | 110 | abort: |
michael@0 | 111 | RFREE(addr); |
michael@0 | 112 | if (_status) RFREE(servers); |
michael@0 | 113 | return(_status); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | int nr_ice_ctx_set_stun_servers(nr_ice_ctx *ctx,nr_ice_stun_server *servers,int ct) |
michael@0 | 117 | { |
michael@0 | 118 | int _status; |
michael@0 | 119 | |
michael@0 | 120 | if(ctx->stun_servers){ |
michael@0 | 121 | RFREE(ctx->stun_servers); |
michael@0 | 122 | ctx->stun_server_ct=0; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | if (ct) { |
michael@0 | 126 | if(!(ctx->stun_servers=RCALLOC(sizeof(nr_ice_stun_server)*ct))) |
michael@0 | 127 | ABORT(R_NO_MEMORY); |
michael@0 | 128 | |
michael@0 | 129 | memcpy(ctx->stun_servers,servers,sizeof(nr_ice_stun_server)*ct); |
michael@0 | 130 | ctx->stun_server_ct = ct; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | _status=0; |
michael@0 | 134 | abort: |
michael@0 | 135 | return(_status); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | int nr_ice_ctx_set_turn_servers(nr_ice_ctx *ctx,nr_ice_turn_server *servers,int ct) |
michael@0 | 139 | { |
michael@0 | 140 | int _status; |
michael@0 | 141 | |
michael@0 | 142 | if(ctx->turn_servers){ |
michael@0 | 143 | RFREE(ctx->turn_servers); |
michael@0 | 144 | ctx->turn_server_ct=0; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | if(ct) { |
michael@0 | 148 | if(!(ctx->turn_servers=RCALLOC(sizeof(nr_ice_turn_server)*ct))) |
michael@0 | 149 | ABORT(R_NO_MEMORY); |
michael@0 | 150 | |
michael@0 | 151 | memcpy(ctx->turn_servers,servers,sizeof(nr_ice_turn_server)*ct); |
michael@0 | 152 | ctx->turn_server_ct = ct; |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | _status=0; |
michael@0 | 156 | abort: |
michael@0 | 157 | return(_status); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | int nr_ice_ctx_set_local_addrs(nr_ice_ctx *ctx,nr_local_addr *addrs,int ct) |
michael@0 | 161 | { |
michael@0 | 162 | int _status,i,r; |
michael@0 | 163 | |
michael@0 | 164 | if(ctx->local_addrs) { |
michael@0 | 165 | RFREE(ctx->local_addrs); |
michael@0 | 166 | ctx->local_addr_ct=0; |
michael@0 | 167 | ctx->local_addrs=0; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | if (ct) { |
michael@0 | 171 | if(!(ctx->local_addrs=RCALLOC(sizeof(nr_local_addr)*ct))) |
michael@0 | 172 | ABORT(R_NO_MEMORY); |
michael@0 | 173 | |
michael@0 | 174 | for (i=0;i<ct;++i) { |
michael@0 | 175 | if (r=nr_local_addr_copy(ctx->local_addrs+i,addrs+i)) { |
michael@0 | 176 | ABORT(r); |
michael@0 | 177 | } |
michael@0 | 178 | } |
michael@0 | 179 | ctx->local_addr_ct = ct; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | _status=0; |
michael@0 | 183 | abort: |
michael@0 | 184 | return(_status); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | int nr_ice_ctx_set_resolver(nr_ice_ctx *ctx, nr_resolver *resolver) |
michael@0 | 188 | { |
michael@0 | 189 | int _status; |
michael@0 | 190 | |
michael@0 | 191 | if (ctx->resolver) { |
michael@0 | 192 | ABORT(R_ALREADY); |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | ctx->resolver = resolver; |
michael@0 | 196 | |
michael@0 | 197 | _status=0; |
michael@0 | 198 | abort: |
michael@0 | 199 | return(_status); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | int nr_ice_ctx_set_interface_prioritizer(nr_ice_ctx *ctx, nr_interface_prioritizer *ip) |
michael@0 | 203 | { |
michael@0 | 204 | int _status; |
michael@0 | 205 | |
michael@0 | 206 | if (ctx->interface_prioritizer) { |
michael@0 | 207 | ABORT(R_ALREADY); |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | ctx->interface_prioritizer = ip; |
michael@0 | 211 | |
michael@0 | 212 | _status=0; |
michael@0 | 213 | abort: |
michael@0 | 214 | return(_status); |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | #ifdef USE_TURN |
michael@0 | 218 | int nr_ice_fetch_turn_servers(int ct, nr_ice_turn_server **out) |
michael@0 | 219 | { |
michael@0 | 220 | int r,_status; |
michael@0 | 221 | nr_ice_turn_server *servers = 0; |
michael@0 | 222 | int i; |
michael@0 | 223 | NR_registry child; |
michael@0 | 224 | char *addr=0; |
michael@0 | 225 | UINT2 port; |
michael@0 | 226 | in_addr_t addr_int; |
michael@0 | 227 | Data data={0}; |
michael@0 | 228 | |
michael@0 | 229 | if(!(servers=RCALLOC(sizeof(nr_ice_turn_server)*ct))) |
michael@0 | 230 | ABORT(R_NO_MEMORY); |
michael@0 | 231 | |
michael@0 | 232 | for(i=0;i<ct;i++){ |
michael@0 | 233 | if(r=NR_reg_get_child_registry(NR_ICE_REG_TURN_SRV_PRFX,i,child)) |
michael@0 | 234 | ABORT(r); |
michael@0 | 235 | /* Assume we have a v4 addr for now */ |
michael@0 | 236 | if(r=NR_reg_alloc2_string(child,"addr",&addr)) |
michael@0 | 237 | ABORT(r); |
michael@0 | 238 | addr_int=inet_addr(addr); |
michael@0 | 239 | if(addr_int==INADDR_NONE){ |
michael@0 | 240 | r_log(LOG_ICE,LOG_ERR,"Invalid address %s",addr); |
michael@0 | 241 | ABORT(R_BAD_ARGS); |
michael@0 | 242 | } |
michael@0 | 243 | if(r=NR_reg_get2_uint2(child,"port",&port)) { |
michael@0 | 244 | if (r != R_NOT_FOUND) |
michael@0 | 245 | ABORT(r); |
michael@0 | 246 | port = 3478; |
michael@0 | 247 | } |
michael@0 | 248 | if(r=nr_ip4_port_to_transport_addr(ntohl(addr_int), port, IPPROTO_UDP, |
michael@0 | 249 | &servers[i].turn_server.u.addr)) |
michael@0 | 250 | ABORT(r); |
michael@0 | 251 | |
michael@0 | 252 | |
michael@0 | 253 | if(r=NR_reg_alloc2_string(child,NR_ICE_REG_TURN_SRV_USERNAME,&servers[i].username)){ |
michael@0 | 254 | if(r!=R_NOT_FOUND) |
michael@0 | 255 | ABORT(r); |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | if(r=NR_reg_alloc2_data(child,NR_ICE_REG_TURN_SRV_PASSWORD,&data)){ |
michael@0 | 259 | if(r!=R_NOT_FOUND) |
michael@0 | 260 | ABORT(r); |
michael@0 | 261 | } |
michael@0 | 262 | else { |
michael@0 | 263 | servers[i].password=RCALLOC(sizeof(*servers[i].password)); |
michael@0 | 264 | if(!servers[i].password) |
michael@0 | 265 | ABORT(R_NO_MEMORY); |
michael@0 | 266 | servers[i].password->data = data.data; |
michael@0 | 267 | servers[i].password->len = data.len; |
michael@0 | 268 | data.data=0; |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | servers[i].turn_server.index=i; |
michael@0 | 272 | |
michael@0 | 273 | RFREE(addr); |
michael@0 | 274 | addr=0; |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | *out = servers; |
michael@0 | 278 | |
michael@0 | 279 | _status=0; |
michael@0 | 280 | abort: |
michael@0 | 281 | RFREE(data.data); |
michael@0 | 282 | RFREE(addr); |
michael@0 | 283 | if (_status) RFREE(servers); |
michael@0 | 284 | return(_status); |
michael@0 | 285 | } |
michael@0 | 286 | #endif /* USE_TURN */ |
michael@0 | 287 | |
michael@0 | 288 | int nr_ice_ctx_create(char *label, UINT4 flags, nr_ice_ctx **ctxp) |
michael@0 | 289 | { |
michael@0 | 290 | nr_ice_ctx *ctx=0; |
michael@0 | 291 | int r,_status; |
michael@0 | 292 | char buf[100]; |
michael@0 | 293 | |
michael@0 | 294 | if(r=r_log_register("ice", &LOG_ICE)) |
michael@0 | 295 | ABORT(r); |
michael@0 | 296 | |
michael@0 | 297 | if(!(ctx=RCALLOC(sizeof(nr_ice_ctx)))) |
michael@0 | 298 | ABORT(R_NO_MEMORY); |
michael@0 | 299 | |
michael@0 | 300 | ctx->flags=flags; |
michael@0 | 301 | |
michael@0 | 302 | if(!(ctx->label=r_strdup(label))) |
michael@0 | 303 | ABORT(R_NO_MEMORY); |
michael@0 | 304 | |
michael@0 | 305 | if(r=nr_ice_random_string(buf,8)) |
michael@0 | 306 | ABORT(r); |
michael@0 | 307 | if(!(ctx->ufrag=r_strdup(buf))) |
michael@0 | 308 | ABORT(r); |
michael@0 | 309 | if(r=nr_ice_random_string(buf,32)) |
michael@0 | 310 | ABORT(r); |
michael@0 | 311 | if(!(ctx->pwd=r_strdup(buf))) |
michael@0 | 312 | ABORT(r); |
michael@0 | 313 | |
michael@0 | 314 | /* Get the STUN servers */ |
michael@0 | 315 | if(r=NR_reg_get_child_count(NR_ICE_REG_STUN_SRV_PRFX, |
michael@0 | 316 | (unsigned int *)&ctx->stun_server_ct)||ctx->stun_server_ct==0) { |
michael@0 | 317 | r_log(LOG_ICE,LOG_WARNING,"ICE(%s): No STUN servers specified", ctx->label); |
michael@0 | 318 | ctx->stun_server_ct=0; |
michael@0 | 319 | } |
michael@0 | 320 | |
michael@0 | 321 | /* 255 is the max for our priority algorithm */ |
michael@0 | 322 | if(ctx->stun_server_ct>255){ |
michael@0 | 323 | r_log(LOG_ICE,LOG_WARNING,"ICE(%s): Too many STUN servers specified: max=255", ctx->label); |
michael@0 | 324 | ctx->stun_server_ct=255; |
michael@0 | 325 | } |
michael@0 | 326 | |
michael@0 | 327 | if(ctx->stun_server_ct>0){ |
michael@0 | 328 | if(r=nr_ice_fetch_stun_servers(ctx->stun_server_ct,&ctx->stun_servers)){ |
michael@0 | 329 | r_log(LOG_ICE,LOG_ERR,"ICE(%s): Couldn't load STUN servers from registry", ctx->label); |
michael@0 | 330 | ctx->stun_server_ct=0; |
michael@0 | 331 | ABORT(r); |
michael@0 | 332 | } |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | #ifdef USE_TURN |
michael@0 | 336 | /* Get the TURN servers */ |
michael@0 | 337 | if(r=NR_reg_get_child_count(NR_ICE_REG_TURN_SRV_PRFX, |
michael@0 | 338 | (unsigned int *)&ctx->turn_server_ct)||ctx->turn_server_ct==0) { |
michael@0 | 339 | r_log(LOG_ICE,LOG_NOTICE,"ICE(%s): No TURN servers specified", ctx->label); |
michael@0 | 340 | ctx->turn_server_ct=0; |
michael@0 | 341 | } |
michael@0 | 342 | #else |
michael@0 | 343 | ctx->turn_server_ct=0; |
michael@0 | 344 | #endif /* USE_TURN */ |
michael@0 | 345 | |
michael@0 | 346 | ctx->local_addrs=0; |
michael@0 | 347 | ctx->local_addr_ct=0; |
michael@0 | 348 | |
michael@0 | 349 | /* 255 is the max for our priority algorithm */ |
michael@0 | 350 | if((ctx->stun_server_ct+ctx->turn_server_ct)>255){ |
michael@0 | 351 | r_log(LOG_ICE,LOG_WARNING,"ICE(%s): Too many STUN/TURN servers specified: max=255", ctx->label); |
michael@0 | 352 | ctx->turn_server_ct=255-ctx->stun_server_ct; |
michael@0 | 353 | } |
michael@0 | 354 | |
michael@0 | 355 | #ifdef USE_TURN |
michael@0 | 356 | if(ctx->turn_server_ct>0){ |
michael@0 | 357 | if(r=nr_ice_fetch_turn_servers(ctx->turn_server_ct,&ctx->turn_servers)){ |
michael@0 | 358 | ctx->turn_server_ct=0; |
michael@0 | 359 | r_log(LOG_ICE,LOG_ERR,"ICE(%s): Couldn't load TURN servers from registry", ctx->label); |
michael@0 | 360 | ABORT(r); |
michael@0 | 361 | } |
michael@0 | 362 | } |
michael@0 | 363 | #endif /* USE_TURN */ |
michael@0 | 364 | |
michael@0 | 365 | |
michael@0 | 366 | ctx->Ta = 20; |
michael@0 | 367 | |
michael@0 | 368 | STAILQ_INIT(&ctx->streams); |
michael@0 | 369 | STAILQ_INIT(&ctx->sockets); |
michael@0 | 370 | STAILQ_INIT(&ctx->foundations); |
michael@0 | 371 | STAILQ_INIT(&ctx->peers); |
michael@0 | 372 | STAILQ_INIT(&ctx->ids); |
michael@0 | 373 | |
michael@0 | 374 | *ctxp=ctx; |
michael@0 | 375 | |
michael@0 | 376 | _status=0; |
michael@0 | 377 | abort: |
michael@0 | 378 | if(_status) |
michael@0 | 379 | nr_ice_ctx_destroy_cb(0,0,ctx); |
michael@0 | 380 | |
michael@0 | 381 | return(_status); |
michael@0 | 382 | } |
michael@0 | 383 | |
michael@0 | 384 | static void nr_ice_ctx_destroy_cb(NR_SOCKET s, int how, void *cb_arg) |
michael@0 | 385 | { |
michael@0 | 386 | nr_ice_ctx *ctx=cb_arg; |
michael@0 | 387 | nr_ice_foundation *f1,*f2; |
michael@0 | 388 | nr_ice_media_stream *s1,*s2; |
michael@0 | 389 | int i; |
michael@0 | 390 | nr_ice_stun_id *id1,*id2; |
michael@0 | 391 | |
michael@0 | 392 | RFREE(ctx->label); |
michael@0 | 393 | |
michael@0 | 394 | RFREE(ctx->stun_servers); |
michael@0 | 395 | |
michael@0 | 396 | RFREE(ctx->local_addrs); |
michael@0 | 397 | |
michael@0 | 398 | for (i = 0; i < ctx->turn_server_ct; i++) { |
michael@0 | 399 | RFREE(ctx->turn_servers[i].username); |
michael@0 | 400 | r_data_destroy(&ctx->turn_servers[i].password); |
michael@0 | 401 | } |
michael@0 | 402 | RFREE(ctx->turn_servers); |
michael@0 | 403 | |
michael@0 | 404 | f1=STAILQ_FIRST(&ctx->foundations); |
michael@0 | 405 | while(f1){ |
michael@0 | 406 | f2=STAILQ_NEXT(f1,entry); |
michael@0 | 407 | RFREE(f1); |
michael@0 | 408 | f1=f2; |
michael@0 | 409 | } |
michael@0 | 410 | RFREE(ctx->pwd); |
michael@0 | 411 | RFREE(ctx->ufrag); |
michael@0 | 412 | |
michael@0 | 413 | STAILQ_FOREACH_SAFE(s1, &ctx->streams, entry, s2){ |
michael@0 | 414 | STAILQ_REMOVE(&ctx->streams,s1,nr_ice_media_stream_,entry); |
michael@0 | 415 | nr_ice_media_stream_destroy(&s1); |
michael@0 | 416 | } |
michael@0 | 417 | |
michael@0 | 418 | STAILQ_FOREACH_SAFE(id1, &ctx->ids, entry, id2){ |
michael@0 | 419 | STAILQ_REMOVE(&ctx->ids,id1,nr_ice_stun_id_,entry); |
michael@0 | 420 | RFREE(id1); |
michael@0 | 421 | } |
michael@0 | 422 | |
michael@0 | 423 | nr_resolver_destroy(&ctx->resolver); |
michael@0 | 424 | nr_interface_prioritizer_destroy(&ctx->interface_prioritizer); |
michael@0 | 425 | |
michael@0 | 426 | RFREE(ctx); |
michael@0 | 427 | } |
michael@0 | 428 | |
michael@0 | 429 | int nr_ice_ctx_destroy(nr_ice_ctx **ctxp) |
michael@0 | 430 | { |
michael@0 | 431 | if(!ctxp || !*ctxp) |
michael@0 | 432 | return(0); |
michael@0 | 433 | |
michael@0 | 434 | (*ctxp)->done_cb=0; |
michael@0 | 435 | (*ctxp)->trickle_cb=0; |
michael@0 | 436 | |
michael@0 | 437 | NR_ASYNC_SCHEDULE(nr_ice_ctx_destroy_cb,*ctxp); |
michael@0 | 438 | |
michael@0 | 439 | *ctxp=0; |
michael@0 | 440 | |
michael@0 | 441 | return(0); |
michael@0 | 442 | } |
michael@0 | 443 | |
michael@0 | 444 | void nr_ice_initialize_finished_cb(NR_SOCKET s, int h, void *cb_arg) |
michael@0 | 445 | { |
michael@0 | 446 | int r,_status; |
michael@0 | 447 | nr_ice_candidate *cand=cb_arg; |
michael@0 | 448 | nr_ice_ctx *ctx; |
michael@0 | 449 | |
michael@0 | 450 | |
michael@0 | 451 | assert(cb_arg); |
michael@0 | 452 | if (!cb_arg) |
michael@0 | 453 | return; |
michael@0 | 454 | ctx = cand->ctx; |
michael@0 | 455 | |
michael@0 | 456 | ctx->uninitialized_candidates--; |
michael@0 | 457 | |
michael@0 | 458 | if (cand->state == NR_ICE_CAND_STATE_INITIALIZED) { |
michael@0 | 459 | int was_pruned = 0; |
michael@0 | 460 | |
michael@0 | 461 | if (r=nr_ice_component_maybe_prune_candidate(ctx, cand->component, |
michael@0 | 462 | cand, &was_pruned)) { |
michael@0 | 463 | r_log(LOG_ICE, LOG_NOTICE, "ICE(%s): Problem pruning candidates",ctx->label); |
michael@0 | 464 | } |
michael@0 | 465 | |
michael@0 | 466 | /* If we are initialized, the candidate wasn't pruned, |
michael@0 | 467 | and we have a trickle ICE callback fire the callback */ |
michael@0 | 468 | if (ctx->trickle_cb && !was_pruned) { |
michael@0 | 469 | ctx->trickle_cb(ctx->trickle_cb_arg, ctx, cand->stream, cand->component_id, cand); |
michael@0 | 470 | |
michael@0 | 471 | if (r=nr_ice_ctx_pair_new_trickle_candidates(ctx, cand)) { |
michael@0 | 472 | r_log(LOG_ICE,LOG_ERR, "ICE(%s): All could not pair new trickle candidate",ctx->label); |
michael@0 | 473 | /* But continue */ |
michael@0 | 474 | } |
michael@0 | 475 | } |
michael@0 | 476 | } |
michael@0 | 477 | |
michael@0 | 478 | if(ctx->uninitialized_candidates==0){ |
michael@0 | 479 | r_log(LOG_ICE,LOG_DEBUG,"ICE(%s): All candidates initialized",ctx->label); |
michael@0 | 480 | ctx->state=NR_ICE_STATE_INITIALIZED; |
michael@0 | 481 | if (ctx->done_cb) { |
michael@0 | 482 | ctx->done_cb(0,0,ctx->cb_arg); |
michael@0 | 483 | } |
michael@0 | 484 | else { |
michael@0 | 485 | r_log(LOG_ICE,LOG_DEBUG,"ICE(%s): No done_cb. We were probably destroyed.",ctx->label); |
michael@0 | 486 | } |
michael@0 | 487 | } |
michael@0 | 488 | else { |
michael@0 | 489 | r_log(LOG_ICE,LOG_DEBUG,"ICE(%s): Waiting for %d candidates to be initialized",ctx->label, ctx->uninitialized_candidates); |
michael@0 | 490 | } |
michael@0 | 491 | } |
michael@0 | 492 | |
michael@0 | 493 | static int nr_ice_ctx_pair_new_trickle_candidates(nr_ice_ctx *ctx, nr_ice_candidate *cand) |
michael@0 | 494 | { |
michael@0 | 495 | int r,_status; |
michael@0 | 496 | nr_ice_peer_ctx *pctx; |
michael@0 | 497 | |
michael@0 | 498 | pctx=STAILQ_FIRST(&ctx->peers); |
michael@0 | 499 | while(pctx){ |
michael@0 | 500 | if (pctx->state == NR_ICE_PEER_STATE_PAIRED) { |
michael@0 | 501 | r = nr_ice_peer_ctx_pair_new_trickle_candidate(ctx, pctx, cand); |
michael@0 | 502 | if (r) |
michael@0 | 503 | ABORT(r); |
michael@0 | 504 | } |
michael@0 | 505 | |
michael@0 | 506 | pctx=STAILQ_NEXT(pctx,entry); |
michael@0 | 507 | } |
michael@0 | 508 | |
michael@0 | 509 | _status=0; |
michael@0 | 510 | abort: |
michael@0 | 511 | return(_status); |
michael@0 | 512 | } |
michael@0 | 513 | |
michael@0 | 514 | |
michael@0 | 515 | #define MAXADDRS 100 // Ridiculously high |
michael@0 | 516 | int nr_ice_initialize(nr_ice_ctx *ctx, NR_async_cb done_cb, void *cb_arg) |
michael@0 | 517 | { |
michael@0 | 518 | int r,_status; |
michael@0 | 519 | nr_ice_media_stream *stream; |
michael@0 | 520 | nr_local_addr addrs[MAXADDRS]; |
michael@0 | 521 | int i,addr_ct; |
michael@0 | 522 | |
michael@0 | 523 | r_log(LOG_ICE,LOG_DEBUG,"ICE(%s): Initializing candidates",ctx->label); |
michael@0 | 524 | ctx->state=NR_ICE_STATE_INITIALIZING; |
michael@0 | 525 | ctx->done_cb=done_cb; |
michael@0 | 526 | ctx->cb_arg=cb_arg; |
michael@0 | 527 | |
michael@0 | 528 | if(STAILQ_EMPTY(&ctx->streams)) { |
michael@0 | 529 | r_log(LOG_ICE,LOG_ERR,"ICE(%s): Missing streams to initialize",ctx->label); |
michael@0 | 530 | ABORT(R_BAD_ARGS); |
michael@0 | 531 | } |
michael@0 | 532 | |
michael@0 | 533 | /* First, gather all the local addresses we have */ |
michael@0 | 534 | if(r=nr_stun_find_local_addresses(addrs,MAXADDRS,&addr_ct)) { |
michael@0 | 535 | r_log(LOG_ICE,LOG_ERR,"ICE(%s): unable to find local addresses",ctx->label); |
michael@0 | 536 | ABORT(r); |
michael@0 | 537 | } |
michael@0 | 538 | |
michael@0 | 539 | /* Sort interfaces by preference */ |
michael@0 | 540 | if(ctx->interface_prioritizer) { |
michael@0 | 541 | for(i=0;i<addr_ct;i++){ |
michael@0 | 542 | if(r=nr_interface_prioritizer_add_interface(ctx->interface_prioritizer,addrs+i)) { |
michael@0 | 543 | r_log(LOG_ICE,LOG_ERR,"ICE(%s): unable to add interface ",ctx->label); |
michael@0 | 544 | ABORT(r); |
michael@0 | 545 | } |
michael@0 | 546 | } |
michael@0 | 547 | if(r=nr_interface_prioritizer_sort_preference(ctx->interface_prioritizer)) { |
michael@0 | 548 | r_log(LOG_ICE,LOG_ERR,"ICE(%s): unable to sort interface by preference",ctx->label); |
michael@0 | 549 | ABORT(r); |
michael@0 | 550 | } |
michael@0 | 551 | } |
michael@0 | 552 | |
michael@0 | 553 | if (r=nr_ice_ctx_set_local_addrs(ctx,addrs,addr_ct)) { |
michael@0 | 554 | ABORT(r); |
michael@0 | 555 | } |
michael@0 | 556 | |
michael@0 | 557 | /* Initialize all the media stream/component pairs */ |
michael@0 | 558 | stream=STAILQ_FIRST(&ctx->streams); |
michael@0 | 559 | while(stream){ |
michael@0 | 560 | if(r=nr_ice_media_stream_initialize(ctx,stream)) |
michael@0 | 561 | ABORT(r); |
michael@0 | 562 | |
michael@0 | 563 | stream=STAILQ_NEXT(stream,entry); |
michael@0 | 564 | } |
michael@0 | 565 | |
michael@0 | 566 | if(ctx->uninitialized_candidates) |
michael@0 | 567 | ABORT(R_WOULDBLOCK); |
michael@0 | 568 | |
michael@0 | 569 | |
michael@0 | 570 | _status=0; |
michael@0 | 571 | abort: |
michael@0 | 572 | return(_status); |
michael@0 | 573 | } |
michael@0 | 574 | |
michael@0 | 575 | int nr_ice_add_media_stream(nr_ice_ctx *ctx,char *label,int components, nr_ice_media_stream **streamp) |
michael@0 | 576 | { |
michael@0 | 577 | int r,_status; |
michael@0 | 578 | |
michael@0 | 579 | if(r=nr_ice_media_stream_create(ctx,label,components,streamp)) |
michael@0 | 580 | ABORT(r); |
michael@0 | 581 | |
michael@0 | 582 | STAILQ_INSERT_TAIL(&ctx->streams,*streamp,entry); |
michael@0 | 583 | |
michael@0 | 584 | _status=0; |
michael@0 | 585 | abort: |
michael@0 | 586 | return(_status); |
michael@0 | 587 | } |
michael@0 | 588 | |
michael@0 | 589 | int nr_ice_get_global_attributes(nr_ice_ctx *ctx,char ***attrsp, int *attrctp) |
michael@0 | 590 | { |
michael@0 | 591 | char **attrs=0; |
michael@0 | 592 | int _status; |
michael@0 | 593 | char *tmp=0; |
michael@0 | 594 | |
michael@0 | 595 | if(!(attrs=RCALLOC(sizeof(char *)*2))) |
michael@0 | 596 | ABORT(R_NO_MEMORY); |
michael@0 | 597 | |
michael@0 | 598 | if(!(tmp=RMALLOC(100))) |
michael@0 | 599 | ABORT(R_NO_MEMORY); |
michael@0 | 600 | snprintf(tmp,100,"ice-ufrag:%s",ctx->ufrag); |
michael@0 | 601 | attrs[0]=tmp; |
michael@0 | 602 | |
michael@0 | 603 | if(!(tmp=RMALLOC(100))) |
michael@0 | 604 | ABORT(R_NO_MEMORY); |
michael@0 | 605 | snprintf(tmp,100,"ice-pwd:%s",ctx->pwd); |
michael@0 | 606 | attrs[1]=tmp; |
michael@0 | 607 | |
michael@0 | 608 | *attrctp=2; |
michael@0 | 609 | *attrsp=attrs; |
michael@0 | 610 | |
michael@0 | 611 | _status=0; |
michael@0 | 612 | abort: |
michael@0 | 613 | if (_status){ |
michael@0 | 614 | if (attrs){ |
michael@0 | 615 | RFREE(attrs[0]); |
michael@0 | 616 | RFREE(attrs[1]); |
michael@0 | 617 | } |
michael@0 | 618 | RFREE(attrs); |
michael@0 | 619 | } |
michael@0 | 620 | return(_status); |
michael@0 | 621 | } |
michael@0 | 622 | |
michael@0 | 623 | static int nr_ice_random_string(char *str, int len) |
michael@0 | 624 | { |
michael@0 | 625 | unsigned char bytes[100]; |
michael@0 | 626 | int needed; |
michael@0 | 627 | int r,_status; |
michael@0 | 628 | |
michael@0 | 629 | if(len%2) ABORT(R_BAD_ARGS); |
michael@0 | 630 | needed=len/2; |
michael@0 | 631 | |
michael@0 | 632 | if(needed>sizeof(bytes)) ABORT(R_BAD_ARGS); |
michael@0 | 633 | |
michael@0 | 634 | //memset(bytes,0,needed); |
michael@0 | 635 | |
michael@0 | 636 | if(r=nr_crypto_random_bytes(bytes,needed)) |
michael@0 | 637 | ABORT(r); |
michael@0 | 638 | |
michael@0 | 639 | if(r=nr_bin2hex(bytes,needed,(unsigned char *)str)) |
michael@0 | 640 | ABORT(r); |
michael@0 | 641 | |
michael@0 | 642 | _status=0; |
michael@0 | 643 | abort: |
michael@0 | 644 | return(_status); |
michael@0 | 645 | } |
michael@0 | 646 | |
michael@0 | 647 | /* This is incredibly annoying: we now have a datagram but we don't |
michael@0 | 648 | know which peer it's from, and we need to be able to tell the |
michael@0 | 649 | API user. So, offer it to each peer and if one bites, assume |
michael@0 | 650 | the others don't want it |
michael@0 | 651 | */ |
michael@0 | 652 | int nr_ice_ctx_deliver_packet(nr_ice_ctx *ctx, nr_ice_component *comp, nr_transport_addr *source_addr, UCHAR *data, int len) |
michael@0 | 653 | { |
michael@0 | 654 | nr_ice_peer_ctx *pctx; |
michael@0 | 655 | int r; |
michael@0 | 656 | |
michael@0 | 657 | pctx=STAILQ_FIRST(&ctx->peers); |
michael@0 | 658 | while(pctx){ |
michael@0 | 659 | r=nr_ice_peer_ctx_deliver_packet_maybe(pctx, comp, source_addr, data, len); |
michael@0 | 660 | if(!r) |
michael@0 | 661 | break; |
michael@0 | 662 | |
michael@0 | 663 | pctx=STAILQ_NEXT(pctx,entry); |
michael@0 | 664 | } |
michael@0 | 665 | |
michael@0 | 666 | if(!pctx) |
michael@0 | 667 | r_log(LOG_ICE,LOG_WARNING,"ICE(%s): Packet received from %s which doesn't match any known peer",ctx->label,source_addr->as_string); |
michael@0 | 668 | |
michael@0 | 669 | return(0); |
michael@0 | 670 | } |
michael@0 | 671 | |
michael@0 | 672 | int nr_ice_ctx_is_known_id(nr_ice_ctx *ctx, UCHAR id[12]) |
michael@0 | 673 | { |
michael@0 | 674 | nr_ice_stun_id *xid; |
michael@0 | 675 | |
michael@0 | 676 | xid=STAILQ_FIRST(&ctx->ids); |
michael@0 | 677 | while(xid){ |
michael@0 | 678 | if (!memcmp(xid->id, id, 12)) |
michael@0 | 679 | return 1; |
michael@0 | 680 | |
michael@0 | 681 | xid=STAILQ_NEXT(xid,entry); |
michael@0 | 682 | } |
michael@0 | 683 | |
michael@0 | 684 | return 0; |
michael@0 | 685 | } |
michael@0 | 686 | |
michael@0 | 687 | int nr_ice_ctx_remember_id(nr_ice_ctx *ctx, nr_stun_message *msg) |
michael@0 | 688 | { |
michael@0 | 689 | int _status; |
michael@0 | 690 | nr_ice_stun_id *xid; |
michael@0 | 691 | |
michael@0 | 692 | xid = RCALLOC(sizeof(*xid)); |
michael@0 | 693 | if (!xid) |
michael@0 | 694 | ABORT(R_NO_MEMORY); |
michael@0 | 695 | |
michael@0 | 696 | assert(sizeof(xid->id) == sizeof(msg->header.id)); |
michael@0 | 697 | #if __STDC_VERSION__ >= 201112L |
michael@0 | 698 | _Static_assert(sizeof(xid->id) == sizeof(msg->header.id),"Message ID Size Mismatch"); |
michael@0 | 699 | #endif |
michael@0 | 700 | memcpy(xid->id, &msg->header.id, sizeof(xid->id)); |
michael@0 | 701 | |
michael@0 | 702 | STAILQ_INSERT_TAIL(&ctx->ids,xid,entry); |
michael@0 | 703 | |
michael@0 | 704 | _status=0; |
michael@0 | 705 | abort: |
michael@0 | 706 | return(_status); |
michael@0 | 707 | } |
michael@0 | 708 | |
michael@0 | 709 | |
michael@0 | 710 | /* Clean up some of the resources (mostly file descriptors) used |
michael@0 | 711 | by candidates we didn't choose. Note that this still leaves |
michael@0 | 712 | a fair amount of non-system stuff floating around. This gets |
michael@0 | 713 | cleaned up when you destroy the ICE ctx */ |
michael@0 | 714 | int nr_ice_ctx_finalize(nr_ice_ctx *ctx, nr_ice_peer_ctx *pctx) |
michael@0 | 715 | { |
michael@0 | 716 | nr_ice_media_stream *lstr,*rstr; |
michael@0 | 717 | |
michael@0 | 718 | r_log(LOG_ICE,LOG_DEBUG,"Finalizing ICE ctx %s, peer=%s",ctx->label,pctx->label); |
michael@0 | 719 | /* |
michael@0 | 720 | First find the peer stream, if any |
michael@0 | 721 | */ |
michael@0 | 722 | lstr=STAILQ_FIRST(&ctx->streams); |
michael@0 | 723 | while(lstr){ |
michael@0 | 724 | rstr=STAILQ_FIRST(&pctx->peer_streams); |
michael@0 | 725 | |
michael@0 | 726 | while(rstr){ |
michael@0 | 727 | if(rstr->local_stream==lstr) |
michael@0 | 728 | break; |
michael@0 | 729 | |
michael@0 | 730 | rstr=STAILQ_NEXT(rstr,entry); |
michael@0 | 731 | } |
michael@0 | 732 | |
michael@0 | 733 | nr_ice_media_stream_finalize(lstr,rstr); |
michael@0 | 734 | |
michael@0 | 735 | lstr=STAILQ_NEXT(lstr,entry); |
michael@0 | 736 | } |
michael@0 | 737 | |
michael@0 | 738 | return(0); |
michael@0 | 739 | } |
michael@0 | 740 | |
michael@0 | 741 | |
michael@0 | 742 | int nr_ice_ctx_set_trickle_cb(nr_ice_ctx *ctx, nr_ice_trickle_candidate_cb cb, void *cb_arg) |
michael@0 | 743 | { |
michael@0 | 744 | ctx->trickle_cb = cb; |
michael@0 | 745 | ctx->trickle_cb_arg = cb_arg; |
michael@0 | 746 | |
michael@0 | 747 | return 0; |
michael@0 | 748 | } |