Exemplo n.º 1
0
int main(int argc, char **argv)
  {
    int r;
    nr_transport_addr my_addr;
    nr_socket *my_sock;
    int fd;
    struct timeval tv;

    if (argc != 1) 
        usage();

    nr_app_startup("turn_server",NR_APP_STARTUP_INIT_LOGGING|NR_APP_STARTUP_REGISTRY_LOCAL,&LOG_TURN_SERVER,0,0);
NR_reg_set_char("logging.stderr.enabled", 1);
NR_reg_set_char("logging.syslog.enabled", 1);
NR_reg_set_string("logging.syslog.facility.turn_client.level", "debug");
NR_reg_set_string("logging.stderr.facility.turn_client.level", "debug");
NR_reg_set_string("logging.syslog.facility.stun.level", "debug");
NR_reg_set_string("logging.stderr.facility.stun.level", "debug");
NR_reg_set_string("logging.syslog.facility.turn.level", "debug");
NR_reg_set_string("logging.stderr.facility.turn.level", "debug");
 
    NR_async_timer_init();
    gettimeofday(&tv,0);
    NR_async_timer_update_time(&tv);

    /* Set up the TURN client */
    nr_ip4_port_to_transport_addr(ntohl(0), 3478, IPPROTO_UDP, &my_addr);
    if(r=nr_socket_local_create(&my_addr,&my_sock)){
      fprintf(stderr,"Couldn't create socket\n");
      exit(1);
    }

    /* Now set an async cb */
    nr_socket_getfd(my_sock,&fd);
    NR_ASYNC_WAIT(fd,NR_ASYNC_WAIT_READ,s_cb,my_sock);
 
    while(1){
      int events;
      struct timeval towait={0,50};

      if(r=NR_async_event_wait2(&events,&towait)){
#if 0
        if(r==R_EOD)
          break;
        
        if(r!=R_WOULDBLOCK){
          fprintf(stderr,"Error in event wait\n");
          exit(1);
        }
#endif
      }

      gettimeofday(&tv,0);
      NR_async_timer_update_time(&tv);
    }

    printf("Success!\n");

    exit(0);
  }
Exemplo n.º 2
0
static int nr_turn_client_connect(nr_turn_client_ctx *ctx)
{
  int r,_status;

  if (ctx->turn_server_addr.protocol != IPPROTO_TCP)
    ABORT(R_INTERNAL);

  r = nr_socket_connect(ctx->sock, &ctx->turn_server_addr);

  if (r == R_WOULDBLOCK) {
    NR_SOCKET fd;

    if (r=nr_socket_getfd(ctx->sock, &fd))
      ABORT(r);

    NR_ASYNC_WAIT(fd, NR_ASYNC_WAIT_WRITE, nr_turn_client_connected_cb, ctx);
    NR_ASYNC_TIMER_SET(TURN_CONNECT_TIMEOUT,
                       nr_turn_client_connect_timeout_cb, ctx,
                       &ctx->connected_timer_handle);
    ABORT(R_WOULDBLOCK);
  }
  if (r) {
    ABORT(R_IO_ERROR);
  }

  ctx->state = NR_TURN_CLIENT_STATE_CONNECTED;

  _status = 0;
abort:
  return(_status);
}
Exemplo n.º 3
0
static int nr_tcp_socket_ctx_initialize(nr_tcp_socket_ctx *tcpsock,
  nr_transport_addr *addr, void* cb_arg)
  {
    int r, _status;
    NR_SOCKET fd;

    if ((r=nr_transport_addr_copy(&tcpsock->remote_addr, addr)))
      ABORT(r);
    if ((r=nr_socket_getfd(tcpsock->inner, &fd)))
      ABORT(r);
    NR_ASYNC_WAIT(fd, NR_ASYNC_WAIT_READ, nr_tcp_socket_readable_cb, cb_arg);

    _status=0;
  abort:
    if (_status)
      r_log(LOG_ICE,LOG_DEBUG,"%s:%d function %s(addr:%s) failed with error %d",__FILE__,__LINE__,__FUNCTION__,addr->as_string,_status);
    return(_status);
  }
Exemplo n.º 4
0
int nr_turn_client_cancel(nr_turn_client_ctx *ctx)
{
  nr_turn_stun_ctx *stun = 0;

  if (ctx->state == NR_TURN_CLIENT_STATE_CANCELLED ||
      ctx->state == NR_TURN_CLIENT_STATE_FAILED)
    return 0;

  if (ctx->label)
    r_log(NR_LOG_TURN, LOG_INFO, "TURN(%s): cancelling", ctx->label);

  /* If we are waiting for connect, we need to stop
     waiting for writability */
  if (ctx->state == NR_TURN_CLIENT_STATE_INITTED ||
      ctx->state == NR_TURN_CLIENT_STATE_ALLOCATION_WAIT) {
    NR_SOCKET fd;
    int r;

    r = nr_socket_getfd(ctx->sock, &fd);
    if (r) {
      /* We should be able to get the fd, but if we get an
         error, we shouldn't cancel. */
      r_log(NR_LOG_TURN, LOG_ERR, "TURN: Couldn't get internal fd");
    }
    else {
      NR_ASYNC_CANCEL(fd, NR_ASYNC_WAIT_WRITE);
    }
  }

  /* Cancel the STUN client ctxs */
  stun = STAILQ_FIRST(&ctx->stun_ctxs);
  while (stun) {
    nr_stun_client_cancel(stun->stun);
    stun = STAILQ_NEXT(stun, entry);
  }

  /* Cancel the timers, if not already cancelled */
  NR_async_timer_cancel(ctx->connected_timer_handle);
  NR_async_timer_cancel(ctx->refresh_timer_handle);

  ctx->state = NR_TURN_CLIENT_STATE_CANCELLED;

  return(0);
}
Exemplo n.º 5
0
int main(int argc, char **argv)
  {
    int r;
    nr_turn_client_ctx *c_turn;
    nr_transport_addr my_addr;
    nr_socket *my_sock;
    int fd;
    struct timeval tv;

    nr_crypto_openssl_set();

    if (argc != 2) 
        usage();

    nr_app_startup("turn_client",NR_APP_STARTUP_INIT_LOGGING|NR_APP_STARTUP_REGISTRY_LOCAL,&LOG_TURN_CLIENT,0,0);
NR_reg_set_char("logging.stderr.enabled", 1);
NR_reg_set_char("logging.syslog.enabled", 1);
NR_reg_set_string("logging.syslog.facility.turn_client.level", "debug");
NR_reg_set_string("logging.stderr.facility.turn_client.level", "debug");
NR_reg_set_string("logging.syslog.facility.stun.level", "debug");
NR_reg_set_string("logging.stderr.facility.stun.level", "debug");
NR_reg_set_string("logging.syslog.facility.turn.level", "debug");
NR_reg_set_string("logging.stderr.facility.turn.level", "debug");
 
    NR_async_timer_init();
    gettimeofday(&tv,0);
    NR_async_timer_update_time(&tv);

    nr_ip4_port_to_transport_addr(ntohl(inet_addr(argv[1])),
      3478,IPPROTO_UDP,&remote_addr);

    /* Set up the TURN client */
    nr_ip4_port_to_transport_addr(ntohl(0), 0, IPPROTO_UDP, &my_addr);
    if(r=nr_socket_local_create(&my_addr,&my_sock)){
      fprintf(stderr,"Couldn't create socket\n");
      exit(1);
    }

    if(r=nr_turn_client_ctx_create("TEST",my_sock, 0, &remote_addr,0, &c_turn)){
      fprintf(stderr,"Couldn't create TURN ctx\n");
      exit(1);
    }

    /* Start TURN */
    if(r=nr_turn_client_allocate(c_turn, user, &pass, 123456, 654321, done_cb, c_turn)){
      fprintf(stderr,"Couldn't start TURN\n");
      exit(1);
    }

    /* Now set an async cb */
    nr_socket_getfd(my_sock,&fd);
    NR_ASYNC_WAIT(fd,NR_ASYNC_WAIT_READ,c_cb,c_turn);
 
    while(1){
      int events;
      struct timeval towait={0,50};

      if(r=NR_async_event_wait2(&events,&towait)){
#if 0
        if(r==R_EOD)
          break;
        
        if(r!=R_WOULDBLOCK){
          fprintf(stderr,"Error in event wait\n");
          exit(1);
        }
#endif
      }

      gettimeofday(&tv,0);
      NR_async_timer_update_time(&tv);
    }

    printf("Success!\n");

    exit(0);
  }