media/mtransport/third_party/nICEr/src/ice/ice_candidate.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/mtransport/third_party/nICEr/src/ice/ice_candidate.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,871 @@
     1.4 +/*
     1.5 +Copyright (c) 2007, Adobe Systems, Incorporated
     1.6 +All rights reserved.
     1.7 +
     1.8 +Redistribution and use in source and binary forms, with or without
     1.9 +modification, are permitted provided that the following conditions are
    1.10 +met:
    1.11 +
    1.12 +* Redistributions of source code must retain the above copyright
    1.13 +  notice, this list of conditions and the following disclaimer.
    1.14 +
    1.15 +* Redistributions in binary form must reproduce the above copyright
    1.16 +  notice, this list of conditions and the following disclaimer in the
    1.17 +  documentation and/or other materials provided with the distribution.
    1.18 +
    1.19 +* Neither the name of Adobe Systems, Network Resonance nor the names of its
    1.20 +  contributors may be used to endorse or promote products derived from
    1.21 +  this software without specific prior written permission.
    1.22 +
    1.23 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.24 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.25 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.26 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.27 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.28 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.29 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.30 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.31 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.32 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.33 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.34 +*/
    1.35 +
    1.36 +
    1.37 +
    1.38 +static char *RCSSTRING __UNUSED__="$Id: ice_candidate.c,v 1.2 2008/04/28 17:59:01 ekr Exp $";
    1.39 +
    1.40 +#include <csi_platform.h>
    1.41 +#include <assert.h>
    1.42 +#include <stdio.h>
    1.43 +#include <string.h>
    1.44 +#include <sys/queue.h>
    1.45 +#include <sys/types.h>
    1.46 +#ifdef WIN32
    1.47 +#include <winsock2.h>
    1.48 +#else
    1.49 +#include <sys/socket.h>
    1.50 +#include <netinet/in.h>
    1.51 +#include <arpa/inet.h>
    1.52 +#endif
    1.53 +#include "nr_api.h"
    1.54 +#include "registry.h"
    1.55 +#include "nr_socket.h"
    1.56 +#include "async_timer.h"
    1.57 +
    1.58 +#include "stun_client_ctx.h"
    1.59 +#include "stun_server_ctx.h"
    1.60 +#include "turn_client_ctx.h"
    1.61 +#include "ice_ctx.h"
    1.62 +#include "ice_candidate.h"
    1.63 +#include "ice_codeword.h"
    1.64 +#include "ice_reg.h"
    1.65 +#include "ice_util.h"
    1.66 +#include "nr_socket_turn.h"
    1.67 +#include "nr_socket.h"
    1.68 +
    1.69 +static int next_automatic_preference = 224;
    1.70 +
    1.71 +static int nr_ice_candidate_initialize2(nr_ice_candidate *cand);
    1.72 +static int nr_ice_get_foundation(nr_ice_ctx *ctx,nr_ice_candidate *cand);
    1.73 +static int nr_ice_srvrflx_start_stun(nr_ice_candidate *cand);
    1.74 +static void nr_ice_srvrflx_stun_finished_cb(NR_SOCKET sock, int how, void *cb_arg);
    1.75 +#ifdef USE_TURN
    1.76 +static int nr_ice_start_relay_turn(nr_ice_candidate *cand);
    1.77 +static void nr_ice_turn_allocated_cb(NR_SOCKET sock, int how, void *cb_arg);
    1.78 +static int nr_ice_candidate_resolved_cb(void *cb_arg, nr_transport_addr *addr);
    1.79 +#endif /* USE_TURN */
    1.80 +
    1.81 +void nr_ice_candidate_compute_codeword(nr_ice_candidate *cand)
    1.82 +  {
    1.83 +    char as_string[1024];
    1.84 +
    1.85 +    snprintf(as_string,
    1.86 +             sizeof(as_string),
    1.87 +             "%s(%s)",
    1.88 +             cand->addr.as_string,
    1.89 +             cand->label);
    1.90 +
    1.91 +    nr_ice_compute_codeword(as_string,strlen(as_string),cand->codeword);
    1.92 +  }
    1.93 +
    1.94 +char *nr_ice_candidate_type_names[]={0,"host","srflx","prflx","relay",0};
    1.95 +
    1.96 +static const char *nr_ctype_name(nr_ice_candidate_type ctype) {
    1.97 +  assert(ctype<CTYPE_MAX && ctype>0);
    1.98 +  if (ctype <= 0 || ctype >= CTYPE_MAX) {
    1.99 +    return "ERROR";
   1.100 +  }
   1.101 +  return nr_ice_candidate_type_names[ctype];
   1.102 +}
   1.103 +
   1.104 +static int nr_ice_candidate_format_stun_label(char *label, size_t size, nr_ice_candidate *cand)
   1.105 +  {
   1.106 +    int _status;
   1.107 +
   1.108 +    *label = 0;
   1.109 +    switch(cand->stun_server->type) {
   1.110 +      case NR_ICE_STUN_SERVER_TYPE_ADDR:
   1.111 +        snprintf(label, size, "%s(%s|%s)", nr_ctype_name(cand->type), cand->base.as_string,
   1.112 +                 cand->stun_server->u.addr.as_string);
   1.113 +        break;
   1.114 +      case NR_ICE_STUN_SERVER_TYPE_DNSNAME:
   1.115 +        snprintf(label, size, "%s(%s|%s:%u)", nr_ctype_name(cand->type), cand->base.as_string,
   1.116 +                 cand->stun_server->u.dnsname.host, cand->stun_server->u.dnsname.port);
   1.117 +        break;
   1.118 +      default:
   1.119 +        assert(0);
   1.120 +        ABORT(R_BAD_ARGS);
   1.121 +    }
   1.122 +
   1.123 +    _status=0;
   1.124 +   abort:
   1.125 +    return(_status);
   1.126 +  }
   1.127 +
   1.128 +int nr_ice_candidate_create(nr_ice_ctx *ctx,nr_ice_component *comp,nr_ice_socket *isock, nr_socket *osock, nr_ice_candidate_type ctype, nr_ice_stun_server *stun_server, UCHAR component_id, nr_ice_candidate **candp)
   1.129 +  {
   1.130 +    nr_ice_candidate *cand=0;
   1.131 +    nr_ice_candidate *tmp=0;
   1.132 +    int r,_status;
   1.133 +    char label[512];
   1.134 +
   1.135 +    if(!(cand=RCALLOC(sizeof(nr_ice_candidate))))
   1.136 +      ABORT(R_NO_MEMORY);
   1.137 +    cand->state=NR_ICE_CAND_STATE_CREATED;
   1.138 +    cand->ctx=ctx;
   1.139 +    cand->isock=isock;
   1.140 +    cand->osock=osock;
   1.141 +    cand->type=ctype;
   1.142 +    cand->stun_server=stun_server;
   1.143 +    cand->component_id=component_id;
   1.144 +    cand->component=comp;
   1.145 +    cand->stream=comp->stream;
   1.146 +
   1.147 +    /* Extract the addr as the base */
   1.148 +    if(r=nr_socket_getaddr(cand->isock->sock,&cand->base))
   1.149 +      ABORT(r);
   1.150 +
   1.151 +    switch(ctype) {
   1.152 +      case HOST:
   1.153 +        snprintf(label, sizeof(label), "host(%s)", cand->base.as_string);
   1.154 +        break;
   1.155 +
   1.156 +      case SERVER_REFLEXIVE:
   1.157 +        if(r=nr_ice_candidate_format_stun_label(label, sizeof(label),cand))
   1.158 +          ABORT(r);
   1.159 +        break;
   1.160 +
   1.161 +      case RELAYED:
   1.162 +        if(r=nr_ice_candidate_format_stun_label(label, sizeof(label),cand))
   1.163 +          ABORT(r);
   1.164 +        break;
   1.165 +
   1.166 +      case PEER_REFLEXIVE:
   1.167 +        snprintf(label, sizeof(label), "prflx");
   1.168 +        break;
   1.169 +
   1.170 +      default:
   1.171 +        assert(0); /* Can't happen */
   1.172 +        ABORT(R_BAD_ARGS);
   1.173 +    }
   1.174 +    if(!(cand->label=r_strdup(label)))
   1.175 +      ABORT(R_NO_MEMORY);
   1.176 +
   1.177 +    if(r=nr_ice_get_foundation(ctx,cand))
   1.178 +      ABORT(r);
   1.179 +    if(r=nr_ice_candidate_compute_priority(cand))
   1.180 +      ABORT(r);
   1.181 +
   1.182 +    TAILQ_FOREACH(tmp,&isock->candidates,entry_sock){
   1.183 +      if(cand->priority==tmp->priority){
   1.184 +        r_log(LOG_ICE,LOG_ERR,"ICE(%s): duplicate priority %u candidate %s and candidate %s",
   1.185 +          ctx->label,cand->priority,cand->label,tmp->label);
   1.186 +      }
   1.187 +    }
   1.188 +
   1.189 +    if(ctype==RELAYED)
   1.190 +      cand->u.relayed.turn_sock=osock;
   1.191 +
   1.192 +
   1.193 +    /* Add the candidate to the isock list*/
   1.194 +    TAILQ_INSERT_TAIL(&isock->candidates,cand,entry_sock);
   1.195 +
   1.196 +    nr_ice_candidate_compute_codeword(cand);
   1.197 +
   1.198 +    r_log(LOG_ICE,LOG_DEBUG,"ICE(%s): created candidate %s with type %s",
   1.199 +      ctx->label,cand->label,nr_ctype_name(ctype));
   1.200 +
   1.201 +    *candp=cand;
   1.202 +
   1.203 +    _status=0;
   1.204 +  abort:
   1.205 +    if (_status){
   1.206 +      r_log(LOG_ICE,LOG_ERR,"ICE(%s): Failed to create candidate of type %s", ctx->label,nr_ctype_name(ctype));
   1.207 +      nr_ice_candidate_destroy(&cand);
   1.208 +    }
   1.209 +    return(_status);
   1.210 +  }
   1.211 +
   1.212 +
   1.213 +/* Create a peer reflexive candidate */
   1.214 +int nr_ice_peer_peer_rflx_candidate_create(nr_ice_ctx *ctx,char *label, nr_ice_component *comp,nr_transport_addr *addr, nr_ice_candidate **candp)
   1.215 +  {
   1.216 +    nr_ice_candidate *cand=0;
   1.217 +    nr_ice_candidate_type ctype=PEER_REFLEXIVE;
   1.218 +    int r,_status;
   1.219 +
   1.220 +    if(!(cand=RCALLOC(sizeof(nr_ice_candidate))))
   1.221 +      ABORT(R_NO_MEMORY);
   1.222 +    if(!(cand->label=r_strdup(label)))
   1.223 +      ABORT(R_NO_MEMORY);
   1.224 +
   1.225 +    cand->state=NR_ICE_CAND_STATE_INITIALIZED;
   1.226 +    cand->ctx=ctx;
   1.227 +    cand->type=ctype;
   1.228 +    cand->component_id=comp->component_id;
   1.229 +    cand->component=comp;
   1.230 +    cand->stream=comp->stream;
   1.231 +
   1.232 +
   1.233 +    r_log(LOG_ICE,LOG_DEBUG,"ICE(%s)/CAND(%s): creating candidate with type %s",
   1.234 +      ctx->label,label,nr_ctype_name(ctype));
   1.235 +
   1.236 +    if(r=nr_transport_addr_copy(&cand->base,addr))
   1.237 +      ABORT(r);
   1.238 +    if(r=nr_transport_addr_copy(&cand->addr,addr))
   1.239 +      ABORT(r);
   1.240 +    /* Bogus foundation */
   1.241 +    if(!(cand->foundation=r_strdup(cand->addr.as_string)))
   1.242 +      ABORT(R_NO_MEMORY);
   1.243 +
   1.244 +    nr_ice_candidate_compute_codeword(cand);
   1.245 +
   1.246 +    *candp=cand;
   1.247 +
   1.248 +    _status=0;
   1.249 +  abort:
   1.250 +    if (_status){
   1.251 +      nr_ice_candidate_destroy(&cand);
   1.252 +    }
   1.253 +    return(_status);
   1.254 +  }
   1.255 +
   1.256 +int nr_ice_candidate_destroy(nr_ice_candidate **candp)
   1.257 +  {
   1.258 +    nr_ice_candidate *cand=0;
   1.259 +
   1.260 +    if(!candp || !*candp)
   1.261 +      return(0);
   1.262 +
   1.263 +    cand=*candp;
   1.264 +
   1.265 +    switch(cand->type){
   1.266 +      case HOST:
   1.267 +        break;
   1.268 +#ifdef USE_TURN
   1.269 +      case RELAYED:
   1.270 +        if (cand->u.relayed.turn_handle)
   1.271 +          nr_ice_socket_deregister(cand->isock, cand->u.relayed.turn_handle);
   1.272 +        nr_turn_client_ctx_destroy(&cand->u.relayed.turn);
   1.273 +        nr_socket_destroy(&cand->u.relayed.turn_sock);
   1.274 +        break;
   1.275 +#endif /* USE_TURN */
   1.276 +      case SERVER_REFLEXIVE:
   1.277 +        if (cand->u.srvrflx.stun_handle)
   1.278 +          nr_ice_socket_deregister(cand->isock, cand->u.srvrflx.stun_handle);
   1.279 +        nr_stun_client_ctx_destroy(&cand->u.srvrflx.stun);
   1.280 +        break;
   1.281 +      default:
   1.282 +        break;
   1.283 +    }
   1.284 +
   1.285 +    NR_async_timer_cancel(cand->delay_timer);
   1.286 +    NR_async_timer_cancel(cand->ready_cb_timer);
   1.287 +    if(cand->resolver_handle){
   1.288 +      nr_resolver_cancel(cand->ctx->resolver,cand->resolver_handle);
   1.289 +    }
   1.290 +
   1.291 +    RFREE(cand->foundation);
   1.292 +    RFREE(cand->label);
   1.293 +    RFREE(cand);
   1.294 +
   1.295 +    return(0);
   1.296 +  }
   1.297 +
   1.298 +void nr_ice_candidate_destroy_cb(NR_SOCKET s, int h, void *cb_arg)
   1.299 +  {
   1.300 +    nr_ice_candidate *cand=cb_arg;
   1.301 +    nr_ice_candidate_destroy(&cand);
   1.302 +  }
   1.303 +
   1.304 +/* This algorithm is not super-fast, but I don't think we need a hash
   1.305 +   table just yet and it produces a small foundation string */
   1.306 +static int nr_ice_get_foundation(nr_ice_ctx *ctx,nr_ice_candidate *cand)
   1.307 +  {
   1.308 +    nr_ice_foundation *foundation;
   1.309 +    int i=0;
   1.310 +    char fnd[20];
   1.311 +    int _status;
   1.312 +
   1.313 +    foundation=STAILQ_FIRST(&ctx->foundations);
   1.314 +    while(foundation){
   1.315 +      if(nr_transport_addr_cmp(&cand->base,&foundation->addr,NR_TRANSPORT_ADDR_CMP_MODE_ADDR))
   1.316 +        goto next;
   1.317 +      if(cand->type != foundation->type)
   1.318 +        goto next;
   1.319 +      if(cand->stun_server != foundation->stun_server)
   1.320 +        goto next;
   1.321 +
   1.322 +      snprintf(fnd,sizeof(fnd),"%d",i);
   1.323 +      if(!(cand->foundation=r_strdup(fnd)))
   1.324 +        ABORT(R_NO_MEMORY);
   1.325 +      return(0);
   1.326 +
   1.327 +    next:
   1.328 +      foundation=STAILQ_NEXT(foundation,entry);
   1.329 +      i++;
   1.330 +    }
   1.331 +
   1.332 +    if(!(foundation=RCALLOC(sizeof(nr_ice_foundation))))
   1.333 +      ABORT(R_NO_MEMORY);
   1.334 +    nr_transport_addr_copy(&foundation->addr,&cand->base);
   1.335 +    foundation->type=cand->type;
   1.336 +    foundation->stun_server=cand->stun_server;
   1.337 +    STAILQ_INSERT_TAIL(&ctx->foundations,foundation,entry);
   1.338 +
   1.339 +    snprintf(fnd,sizeof(fnd),"%d",i);
   1.340 +    if(!(cand->foundation=r_strdup(fnd)))
   1.341 +      ABORT(R_NO_MEMORY);
   1.342 +
   1.343 +    _status=0;
   1.344 +  abort:
   1.345 +    return(_status);
   1.346 +  }
   1.347 +
   1.348 +int nr_ice_candidate_compute_priority(nr_ice_candidate *cand)
   1.349 +  {
   1.350 +    UCHAR type_preference;
   1.351 +    UCHAR interface_preference;
   1.352 +    UCHAR stun_priority;
   1.353 +    int r,_status;
   1.354 +
   1.355 +    switch(cand->type){
   1.356 +      case HOST:
   1.357 +        if(r=NR_reg_get_uchar(NR_ICE_REG_PREF_TYPE_HOST,&type_preference))
   1.358 +          ABORT(r);
   1.359 +        stun_priority=0;
   1.360 +        break;
   1.361 +      case RELAYED:
   1.362 +        if(cand->base.protocol == IPPROTO_UDP) {
   1.363 +          if(r=NR_reg_get_uchar(NR_ICE_REG_PREF_TYPE_RELAYED,&type_preference))
   1.364 +            ABORT(r);
   1.365 +        }
   1.366 +        else if(cand->base.protocol == IPPROTO_TCP) {
   1.367 +          if(r=NR_reg_get_uchar(NR_ICE_REG_PREF_TYPE_RELAYED_TCP,&type_preference))
   1.368 +            ABORT(r);
   1.369 +
   1.370 +        }
   1.371 +        else {
   1.372 +          r_log(LOG_ICE,LOG_ERR,"Unknown protocol type %u",
   1.373 +                (unsigned int)cand->base.protocol);
   1.374 +          ABORT(R_INTERNAL);
   1.375 +        }
   1.376 +        stun_priority=255-cand->stun_server->index;
   1.377 +        break;
   1.378 +      case SERVER_REFLEXIVE:
   1.379 +        if(r=NR_reg_get_uchar(NR_ICE_REG_PREF_TYPE_SRV_RFLX,&type_preference))
   1.380 +          ABORT(r);
   1.381 +        stun_priority=255-cand->stun_server->index;
   1.382 +        break;
   1.383 +      case PEER_REFLEXIVE:
   1.384 +        if(r=NR_reg_get_uchar(NR_ICE_REG_PREF_TYPE_PEER_RFLX,&type_preference))
   1.385 +          ABORT(r);
   1.386 +        stun_priority=0;
   1.387 +        break;
   1.388 +      default:
   1.389 +        ABORT(R_INTERNAL);
   1.390 +    }
   1.391 +
   1.392 +    if(type_preference > 126)
   1.393 +      r_log(LOG_ICE,LOG_ERR,"Illegal type preference %d",type_preference);
   1.394 +
   1.395 +    if(!cand->ctx->interface_prioritizer) {
   1.396 +      /* Prioritizer is not set, read from registry */
   1.397 +      if(r=NR_reg_get2_uchar(NR_ICE_REG_PREF_INTERFACE_PRFX,cand->base.ifname,
   1.398 +        &interface_preference)) {
   1.399 +        if (r==R_NOT_FOUND) {
   1.400 +          if (next_automatic_preference == 1) {
   1.401 +            r_log(LOG_ICE,LOG_ERR,"Out of preference values. Can't assign one for interface %s",cand->base.ifname);
   1.402 +            ABORT(R_NOT_FOUND);
   1.403 +          }
   1.404 +          r_log(LOG_ICE,LOG_DEBUG,"Automatically assigning preference for interface %s->%d",cand->base.ifname,
   1.405 +            next_automatic_preference);
   1.406 +          if (r=NR_reg_set2_uchar(NR_ICE_REG_PREF_INTERFACE_PRFX,cand->base.ifname,next_automatic_preference)){
   1.407 +            ABORT(r);
   1.408 +          }
   1.409 +          interface_preference=next_automatic_preference;
   1.410 +          next_automatic_preference--;
   1.411 +        }
   1.412 +        else {
   1.413 +          ABORT(r);
   1.414 +        }
   1.415 +      }
   1.416 +    }
   1.417 +    else {
   1.418 +      char key_of_interface[MAXIFNAME + 41];
   1.419 +      nr_transport_addr addr;
   1.420 +
   1.421 +      if(r=nr_socket_getaddr(cand->isock->sock, &addr))
   1.422 +        ABORT(r);
   1.423 +
   1.424 +      if(r=nr_transport_addr_fmt_ifname_addr_string(&addr,key_of_interface,
   1.425 +         sizeof(key_of_interface))) {
   1.426 +        ABORT(r);
   1.427 +      }
   1.428 +      if(r=nr_interface_prioritizer_get_priority(cand->ctx->interface_prioritizer,
   1.429 +         key_of_interface,&interface_preference)) {
   1.430 +        ABORT(r);
   1.431 +      }
   1.432 +    }
   1.433 +
   1.434 +    cand->priority=
   1.435 +      (type_preference << 24) |
   1.436 +      (interface_preference << 16) |
   1.437 +      (stun_priority << 8) |
   1.438 +      (256 - cand->component_id);
   1.439 +
   1.440 +    /* S 4.1.2 */
   1.441 +    assert(cand->priority>=1&&cand->priority<=2147483647);
   1.442 +
   1.443 +    _status=0;
   1.444 +  abort:
   1.445 +    return(_status);
   1.446 +  }
   1.447 +
   1.448 +static void nr_ice_candidate_fire_ready_cb(NR_SOCKET s, int how, void *cb_arg)
   1.449 +  {
   1.450 +    nr_ice_candidate *cand = cb_arg;
   1.451 +
   1.452 +    cand->ready_cb_timer = 0;
   1.453 +    cand->ready_cb(0, 0, cand->ready_cb_arg);
   1.454 +  }
   1.455 +
   1.456 +int nr_ice_candidate_initialize(nr_ice_candidate *cand, NR_async_cb ready_cb, void *cb_arg)
   1.457 +  {
   1.458 +    int r,_status;
   1.459 +    int protocol=NR_RESOLVE_PROTOCOL_STUN;
   1.460 +    int transport=IPPROTO_UDP;
   1.461 +    cand->done_cb=ready_cb;
   1.462 +    cand->cb_arg=cb_arg;
   1.463 +
   1.464 +    switch(cand->type){
   1.465 +      case HOST:
   1.466 +        if(r=nr_socket_getaddr(cand->isock->sock,&cand->addr))
   1.467 +          ABORT(r);
   1.468 +        cand->osock=cand->isock->sock;
   1.469 +        cand->state=NR_ICE_CAND_STATE_INITIALIZED;
   1.470 +        // Post this so that it doesn't happen in-line
   1.471 +        cand->ready_cb = ready_cb;
   1.472 +        cand->ready_cb_arg = cb_arg;
   1.473 +        NR_ASYNC_TIMER_SET(0, nr_ice_candidate_fire_ready_cb, (void *)cand, &cand->ready_cb_timer);
   1.474 +        break;
   1.475 +#ifdef USE_TURN
   1.476 +      case RELAYED:
   1.477 +        protocol=NR_RESOLVE_PROTOCOL_TURN;
   1.478 +        transport=cand->u.relayed.server->transport;
   1.479 +        /* Fall through */
   1.480 +#endif
   1.481 +      case SERVER_REFLEXIVE:
   1.482 +        cand->state=NR_ICE_CAND_STATE_INITIALIZING;
   1.483 +
   1.484 +        if(cand->stun_server->type == NR_ICE_STUN_SERVER_TYPE_ADDR) {
   1.485 +          /* Just copy the address */
   1.486 +          if (r=nr_transport_addr_copy(&cand->stun_server_addr,
   1.487 +                                       &cand->stun_server->u.addr)) {
   1.488 +            r_log(LOG_ICE,LOG_ERR,"ICE-CANDIDATE(%s): Could not copy STUN server addr", cand->label);
   1.489 +            ABORT(r);
   1.490 +          }
   1.491 +
   1.492 +          if(r=nr_ice_candidate_initialize2(cand))
   1.493 +            ABORT(r);
   1.494 +        }
   1.495 +        else {
   1.496 +          nr_resolver_resource resource;
   1.497 +          resource.domain_name=cand->stun_server->u.dnsname.host;
   1.498 +          resource.port=cand->stun_server->u.dnsname.port;
   1.499 +          resource.stun_turn=protocol;
   1.500 +          resource.transport_protocol=transport;
   1.501 +
   1.502 +          /* Try to resolve */
   1.503 +          if(!cand->ctx->resolver) {
   1.504 +            r_log(LOG_ICE, LOG_ERR, "ICE-CANDIDATE(%s): Can't use DNS names without a resolver", cand->label);
   1.505 +            ABORT(R_BAD_ARGS);
   1.506 +          }
   1.507 +
   1.508 +          if(r=nr_resolver_resolve(cand->ctx->resolver,
   1.509 +                                   &resource,
   1.510 +                                   nr_ice_candidate_resolved_cb,
   1.511 +                                   (void *)cand,
   1.512 +                                   &cand->resolver_handle)){
   1.513 +            r_log(LOG_ICE,LOG_ERR,"ICE-CANDIDATE(%s): Could not invoke DNS resolver",cand->label);
   1.514 +            ABORT(r);
   1.515 +          }
   1.516 +        }
   1.517 +        break;
   1.518 +      default:
   1.519 +        ABORT(R_INTERNAL);
   1.520 +    }
   1.521 +
   1.522 +    nr_ice_candidate_compute_codeword(cand);
   1.523 +
   1.524 +    _status=0;
   1.525 +  abort:
   1.526 +    if(_status && _status!=R_WOULDBLOCK)
   1.527 +      cand->state=NR_ICE_CAND_STATE_FAILED;
   1.528 +    return(_status);
   1.529 +  }
   1.530 +
   1.531 +
   1.532 +static int nr_ice_candidate_resolved_cb(void *cb_arg, nr_transport_addr *addr)
   1.533 +  {
   1.534 +    nr_ice_candidate *cand=cb_arg;
   1.535 +    int r,_status;
   1.536 +
   1.537 +    cand->resolver_handle=0;
   1.538 +
   1.539 +    if(addr){
   1.540 +      r_log(LOG_ICE,LOG_DEBUG,"ICE(%s): resolved candidate %s. addr=%s",
   1.541 +            cand->ctx->label,cand->label,addr->as_string);
   1.542 +    }
   1.543 +    else {
   1.544 +      r_log(LOG_ICE,LOG_WARNING,"ICE(%s): failed to resolve candidate %s.",
   1.545 +            cand->ctx->label,cand->label);
   1.546 +      ABORT(R_NOT_FOUND);
   1.547 +    }
   1.548 +
   1.549 +    /* Copy the address */
   1.550 +    if(r=nr_transport_addr_copy(&cand->stun_server_addr,addr))
   1.551 +      ABORT(r);
   1.552 +
   1.553 +    /* Now start initializing */
   1.554 +    if(r=nr_ice_candidate_initialize2(cand))
   1.555 +      ABORT(r);
   1.556 +
   1.557 +    _status=0;
   1.558 +  abort:
   1.559 +    if(_status && _status!=R_WOULDBLOCK) {
   1.560 +      cand->state=NR_ICE_CAND_STATE_FAILED;
   1.561 +      cand->done_cb(0,0,cand->cb_arg);
   1.562 +    }
   1.563 +    return(_status);
   1.564 +  }
   1.565 +
   1.566 +static int nr_ice_candidate_initialize2(nr_ice_candidate *cand)
   1.567 +  {
   1.568 +    int r,_status;
   1.569 +
   1.570 +    switch(cand->type){
   1.571 +      case HOST:
   1.572 +        assert(0); /* Can't happen */
   1.573 +        ABORT(R_INTERNAL);
   1.574 +        break;
   1.575 +#ifdef USE_TURN
   1.576 +      case RELAYED:
   1.577 +        if(r=nr_ice_start_relay_turn(cand))
   1.578 +          ABORT(r);
   1.579 +        ABORT(R_WOULDBLOCK);
   1.580 +        break;
   1.581 +#endif /* USE_TURN */
   1.582 +      case SERVER_REFLEXIVE:
   1.583 +        /* Need to start stun */
   1.584 +        if(r=nr_ice_srvrflx_start_stun(cand))
   1.585 +          ABORT(r);
   1.586 +        cand->osock=cand->isock->sock;
   1.587 +        ABORT(R_WOULDBLOCK);
   1.588 +        break;
   1.589 +      default:
   1.590 +        ABORT(R_INTERNAL);
   1.591 +    }
   1.592 +
   1.593 +    _status=0;
   1.594 +  abort:
   1.595 +    if(_status && _status!=R_WOULDBLOCK)
   1.596 +      cand->state=NR_ICE_CAND_STATE_FAILED;
   1.597 +    return(_status);
   1.598 +  }
   1.599 +
   1.600 +static void nr_ice_srvrflx_start_stun_timer_cb(NR_SOCKET s, int how, void *cb_arg)
   1.601 +  {
   1.602 +    nr_ice_candidate *cand=cb_arg;
   1.603 +    int r,_status;
   1.604 +
   1.605 +    cand->delay_timer=0;
   1.606 +
   1.607 +/* TODO: if the response is a BINDING-ERROR-RESPONSE, then restart
   1.608 + * TODO: using NR_STUN_CLIENT_MODE_BINDING_REQUEST because the
   1.609 + * TODO: server may not have understood the 0.96-style request */
   1.610 +    if(r=nr_stun_client_start(cand->u.srvrflx.stun, NR_STUN_CLIENT_MODE_BINDING_REQUEST_NO_AUTH, nr_ice_srvrflx_stun_finished_cb, cand))
   1.611 +      ABORT(r);
   1.612 +
   1.613 +    if(r=nr_ice_ctx_remember_id(cand->ctx, cand->u.srvrflx.stun->request))
   1.614 +      ABORT(r);
   1.615 +
   1.616 +    if(r=nr_ice_socket_register_stun_client(cand->isock,cand->u.srvrflx.stun,&cand->u.srvrflx.stun_handle))
   1.617 +      ABORT(r);
   1.618 +
   1.619 +    _status=0;
   1.620 +  abort:
   1.621 +    if(_status){
   1.622 +      cand->state=NR_ICE_CAND_STATE_FAILED;
   1.623 +    }
   1.624 +
   1.625 +    return;
   1.626 +  }
   1.627 +
   1.628 +static int nr_ice_srvrflx_start_stun(nr_ice_candidate *cand)
   1.629 +  {
   1.630 +    int r,_status;
   1.631 +
   1.632 +    assert(!cand->delay_timer);
   1.633 +    if(r=nr_stun_client_ctx_create(cand->label, cand->isock->sock,
   1.634 +      &cand->stun_server_addr, cand->stream->ctx->gather_rto,
   1.635 +      &cand->u.srvrflx.stun))
   1.636 +      ABORT(r);
   1.637 +
   1.638 +    NR_ASYNC_TIMER_SET(cand->stream->ctx->stun_delay,nr_ice_srvrflx_start_stun_timer_cb,cand,&cand->delay_timer);
   1.639 +    cand->stream->ctx->stun_delay += cand->stream->ctx->Ta;
   1.640 +
   1.641 +    _status=0;
   1.642 +  abort:
   1.643 +    if(_status){
   1.644 +      cand->state=NR_ICE_CAND_STATE_FAILED;
   1.645 +    }
   1.646 +    return(_status);
   1.647 +  }
   1.648 +
   1.649 +#ifdef USE_TURN
   1.650 +static void nr_ice_start_relay_turn_timer_cb(NR_SOCKET s, int how, void *cb_arg)
   1.651 +  {
   1.652 +    nr_ice_candidate *cand=cb_arg;
   1.653 +    int r,_status;
   1.654 +
   1.655 +    cand->delay_timer=0;
   1.656 +
   1.657 +    if(r=nr_turn_client_allocate(cand->u.relayed.turn, nr_ice_turn_allocated_cb, cb_arg))
   1.658 +      ABORT(r);
   1.659 +
   1.660 +    if(r=nr_ice_socket_register_turn_client(cand->isock, cand->u.relayed.turn,
   1.661 +                                            cand->osock, &cand->u.relayed.turn_handle))
   1.662 +      ABORT(r);
   1.663 +
   1.664 +    _status=0;
   1.665 +  abort:
   1.666 +    if(_status){
   1.667 +      cand->state=NR_ICE_CAND_STATE_FAILED;
   1.668 +    }
   1.669 +    return;
   1.670 +  }
   1.671 +
   1.672 +static int nr_ice_start_relay_turn(nr_ice_candidate *cand)
   1.673 +  {
   1.674 +    int r,_status;
   1.675 +    assert(!cand->delay_timer);
   1.676 +    if(r=nr_turn_client_ctx_create(cand->label, cand->isock->sock,
   1.677 +                                   cand->u.relayed.server->username,
   1.678 +                                   cand->u.relayed.server->password,
   1.679 +                                   &cand->stun_server_addr,
   1.680 +                                   &cand->u.relayed.turn))
   1.681 +      ABORT(r);
   1.682 +
   1.683 +    if(r=nr_socket_turn_set_ctx(cand->osock, cand->u.relayed.turn))
   1.684 +      ABORT(r);
   1.685 +
   1.686 +    NR_ASYNC_TIMER_SET(cand->stream->ctx->stun_delay,nr_ice_start_relay_turn_timer_cb,cand,&cand->delay_timer);
   1.687 +    cand->stream->ctx->stun_delay += cand->stream->ctx->Ta;
   1.688 +
   1.689 +    _status=0;
   1.690 +  abort:
   1.691 +    if(_status){
   1.692 +      cand->state=NR_ICE_CAND_STATE_FAILED;
   1.693 +    }
   1.694 +    return(_status);
   1.695 +  }
   1.696 +#endif /* USE_TURN */
   1.697 +
   1.698 +static void nr_ice_srvrflx_stun_finished_cb(NR_SOCKET sock, int how, void *cb_arg)
   1.699 +  {
   1.700 +    int _status;
   1.701 +    nr_ice_candidate *cand=cb_arg;
   1.702 +
   1.703 +    r_log(LOG_ICE,LOG_DEBUG,"ICE(%s)/CAND(%s): %s",cand->ctx->label,cand->label,__FUNCTION__);
   1.704 +
   1.705 +    /* Deregister to suppress duplicates */
   1.706 +    if(cand->u.srvrflx.stun_handle){ /* This test because we might have failed before CB registered */
   1.707 +      nr_ice_socket_deregister(cand->isock,cand->u.srvrflx.stun_handle);
   1.708 +      cand->u.srvrflx.stun_handle=0;
   1.709 +    }
   1.710 +
   1.711 +    switch(cand->u.srvrflx.stun->state){
   1.712 +      /* OK, we should have a mapped address */
   1.713 +      case NR_STUN_CLIENT_STATE_DONE:
   1.714 +        /* Copy the address */
   1.715 +        nr_transport_addr_copy(&cand->addr, &cand->u.srvrflx.stun->results.stun_binding_response.mapped_addr);
   1.716 +        nr_stun_client_ctx_destroy(&cand->u.srvrflx.stun);
   1.717 +        cand->state=NR_ICE_CAND_STATE_INITIALIZED;
   1.718 +        /* Execute the ready callback */
   1.719 +        cand->done_cb(0,0,cand->cb_arg);
   1.720 +        break;
   1.721 +
   1.722 +      /* This failed, so go to the next STUN server if there is one */
   1.723 +      case NR_STUN_CLIENT_STATE_FAILED:
   1.724 +        ABORT(R_NOT_FOUND);
   1.725 +        break;
   1.726 +      default:
   1.727 +        ABORT(R_INTERNAL);
   1.728 +    }
   1.729 +    _status = 0;
   1.730 +  abort:
   1.731 +    if(_status){
   1.732 +      cand->state=NR_ICE_CAND_STATE_FAILED;
   1.733 +      cand->done_cb(0,0,cand->cb_arg);
   1.734 +    }
   1.735 +  }
   1.736 +
   1.737 +#ifdef USE_TURN
   1.738 +static void nr_ice_turn_allocated_cb(NR_SOCKET s, int how, void *cb_arg)
   1.739 +  {
   1.740 +    int r,_status;
   1.741 +    nr_ice_candidate *cand=cb_arg;
   1.742 +    nr_turn_client_ctx *turn=cand->u.relayed.turn;
   1.743 +    char *label;
   1.744 +    nr_transport_addr relay_addr;
   1.745 +
   1.746 +    switch(turn->state){
   1.747 +      /* OK, we should have a mapped address */
   1.748 +      case NR_TURN_CLIENT_STATE_ALLOCATED:
   1.749 +        if (r=nr_turn_client_get_relayed_address(turn, &relay_addr))
   1.750 +          ABORT(r);
   1.751 +
   1.752 +        if(r=nr_concat_strings(&label,"turn-relay(",cand->base.as_string,"|",
   1.753 +                               relay_addr.as_string,")",NULL))
   1.754 +          ABORT(r);
   1.755 +
   1.756 +        r_log(LOG_ICE,LOG_DEBUG,"TURN-CLIENT(%s)/CAND(%s): Switching from TURN to RELAY (%s)",cand->u.relayed.turn->label,cand->label,label);
   1.757 +
   1.758 +        /* Copy the relayed address into the candidate addr and
   1.759 +           into the candidate base. Note that we need to keep the
   1.760 +           ifname in the base. */
   1.761 +        if (r=nr_transport_addr_copy(&cand->addr, &relay_addr))
   1.762 +          ABORT(r);
   1.763 +        if (r=nr_transport_addr_copy_keep_ifname(&cand->base, &relay_addr))  /* Need to keep interface for priority calculation */
   1.764 +          ABORT(r);
   1.765 +
   1.766 +        r_log(LOG_ICE,LOG_DEBUG,"ICE(%s)/CAND(%s): new relay base=%s addr=%s", cand->ctx->label, cand->label, cand->base.as_string, cand->addr.as_string);
   1.767 +
   1.768 +        RFREE(cand->label);
   1.769 +        cand->label=label;
   1.770 +        cand->state=NR_ICE_CAND_STATE_INITIALIZED;
   1.771 +
   1.772 +        /* We also need to activate the associated STUN candidate */
   1.773 +        if(cand->u.relayed.srvflx_candidate){
   1.774 +          nr_ice_candidate *cand2=cand->u.relayed.srvflx_candidate;
   1.775 +
   1.776 +          if (r=nr_turn_client_get_mapped_address(cand->u.relayed.turn, &cand2->addr))
   1.777 +            ABORT(r);
   1.778 +
   1.779 +          cand2->state=NR_ICE_CAND_STATE_INITIALIZED;
   1.780 +          cand2->done_cb(0,0,cand2->cb_arg);
   1.781 +        }
   1.782 +
   1.783 +        /* Execute the ready callback */
   1.784 +        cand->done_cb(0,0,cand->cb_arg);
   1.785 +        cand = 0;
   1.786 +
   1.787 +        break;
   1.788 +
   1.789 +    case NR_TURN_CLIENT_STATE_FAILED:
   1.790 +    case NR_TURN_CLIENT_STATE_CANCELLED:
   1.791 +      r_log(NR_LOG_TURN, LOG_WARNING,
   1.792 +            "ICE-CANDIDATE(%s): nr_turn_allocated_cb called with state %d",
   1.793 +            cand->label, turn->state);
   1.794 +      /* This failed, so go to the next TURN server if there is one */
   1.795 +      ABORT(R_NOT_FOUND);
   1.796 +      break;
   1.797 +    default:
   1.798 +      assert(0); /* should never happen */
   1.799 +      ABORT(R_INTERNAL);
   1.800 +    }
   1.801 +
   1.802 +    _status=0;
   1.803 +  abort:
   1.804 +    if(_status){
   1.805 +      if (cand) {
   1.806 +        r_log(NR_LOG_TURN, LOG_WARNING,
   1.807 +              "ICE-CANDIDATE(%s): nr_turn_allocated_cb failed", cand->label);
   1.808 +        cand->state=NR_ICE_CAND_STATE_FAILED;
   1.809 +        cand->done_cb(0,0,cand->cb_arg);
   1.810 +
   1.811 +        if(cand->u.relayed.srvflx_candidate){
   1.812 +          nr_ice_candidate *cand2=cand->u.relayed.srvflx_candidate;
   1.813 +
   1.814 +          cand2->state=NR_ICE_CAND_STATE_FAILED;
   1.815 +          cand2->done_cb(0,0,cand2->cb_arg);
   1.816 +        }
   1.817 +      }
   1.818 +    }
   1.819 +  }
   1.820 +#endif /* USE_TURN */
   1.821 +
   1.822 +/* Format the candidate attribute as per ICE S 15.1 */
   1.823 +int nr_ice_format_candidate_attribute(nr_ice_candidate *cand, char *attr, int maxlen)
   1.824 +  {
   1.825 +    int r,_status;
   1.826 +    char addr[64];
   1.827 +    int port;
   1.828 +    int len;
   1.829 +
   1.830 +    assert(!strcmp(nr_ice_candidate_type_names[HOST], "host"));
   1.831 +    assert(!strcmp(nr_ice_candidate_type_names[RELAYED], "relay"));
   1.832 +
   1.833 +    if(r=nr_transport_addr_get_addrstring(&cand->addr,addr,sizeof(addr)))
   1.834 +      ABORT(r);
   1.835 +    if(r=nr_transport_addr_get_port(&cand->addr,&port))
   1.836 +      ABORT(r);
   1.837 +    snprintf(attr,maxlen,"candidate:%s %d UDP %u %s %d typ %s",
   1.838 +      cand->foundation, cand->component_id, cand->priority, addr, port,
   1.839 +      nr_ctype_name(cand->type));
   1.840 +
   1.841 +    len=strlen(attr); attr+=len; maxlen-=len;
   1.842 +
   1.843 +    /* raddr, rport */
   1.844 +    switch(cand->type){
   1.845 +      case HOST:
   1.846 +        break;
   1.847 +      case SERVER_REFLEXIVE:
   1.848 +      case PEER_REFLEXIVE:
   1.849 +        if(r=nr_transport_addr_get_addrstring(&cand->base,addr,sizeof(addr)))
   1.850 +          ABORT(r);
   1.851 +        if(r=nr_transport_addr_get_port(&cand->base,&port))
   1.852 +          ABORT(r);
   1.853 +
   1.854 +        snprintf(attr,maxlen," raddr %s rport %d",addr,port);
   1.855 +        break;
   1.856 +      case RELAYED:
   1.857 +        // comes from XorMappedAddress via AllocateResponse
   1.858 +        if(r=nr_transport_addr_get_addrstring(&cand->base,addr,sizeof(addr)))
   1.859 +          ABORT(r);
   1.860 +        if(r=nr_transport_addr_get_port(&cand->base,&port))
   1.861 +          ABORT(r);
   1.862 +
   1.863 +        snprintf(attr,maxlen," raddr %s rport %d",addr,port);
   1.864 +        break;
   1.865 +      default:
   1.866 +        assert(0);
   1.867 +        ABORT(R_INTERNAL);
   1.868 +        break;
   1.869 +    }
   1.870 +    _status=0;
   1.871 +  abort:
   1.872 +    return(_status);
   1.873 +  }
   1.874 +

mercurial