DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Line Code
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

// Original author: ekr@rtfm.com

/*
Original code from nICEr and nrappkit.

nICEr copyright:

Copyright (c) 2007, Adobe Systems, Incorporated
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

* Neither the name of Adobe Systems, Network Resonance nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


nrappkit copyright:

   Copyright (C) 2001-2003, Network Resonance, Inc.
   Copyright (C) 2006, Network Resonance, Inc.
   All Rights Reserved

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
   3. Neither the name of Network Resonance, Inc. nor the name of any
      contributors to this software may be used to endorse or promote
      products derived from this software without specific prior written
      permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.


   ekr@rtfm.com  Thu Dec 20 20:14:49 2001
*/
#include "logging.h"
#include "mozilla/Scoped.h"
#include "databuffer.h"

extern "C" {
#include "nr_api.h"
#include "async_wait.h"
#include "async_timer.h"
#include "nr_socket.h"
#include "nr_socket_local.h"
#include "transport_addr.h"
#include "addrs.h"
#include "local_addr.h"
#include "stun_util.h"
#include "registry.h"
#include "nr_socket_buffered_stun.h"
}

#include "stunserver.h"

#include <string>

MOZ_MTLOG_MODULE("stunserver");

namespace mozilla {

// Wrapper nr_socket which allows us to lie to the stun server about the
// IP address.
struct nr_socket_wrapped {
  nr_socket *sock_;
  nr_transport_addr addr_;
};

static int nr_socket_wrapped_destroy(void **objp) {
  if (!objp || !*objp)
    return 0;

  nr_socket_wrapped *wrapped = static_cast<nr_socket_wrapped *>(*objp);
  *objp = 0;

  delete wrapped;

  return 0;
}

static int nr_socket_wrapped_sendto(void *obj, const void *msg, size_t len, int flags,
    nr_transport_addr *addr) {
  nr_socket_wrapped *wrapped = static_cast<nr_socket_wrapped *>(obj);

  return nr_socket_sendto(wrapped->sock_, msg, len, flags, &wrapped->addr_);
}

static int nr_socket_wrapped_recvfrom(void *obj, void * restrict buf, size_t maxlen,
    size_t *len, int flags, nr_transport_addr *addr) {
  nr_socket_wrapped *wrapped = static_cast<nr_socket_wrapped *>(obj);

  return nr_socket_recvfrom(wrapped->sock_, buf, maxlen, len, flags, addr);
}

static int nr_socket_wrapped_getfd(void *obj, NR_SOCKET *fd) {
  nr_socket_wrapped *wrapped = static_cast<nr_socket_wrapped *>(obj);

  return nr_socket_getfd(wrapped->sock_, fd);
}

static int nr_socket_wrapped_getaddr(void *obj, nr_transport_addr *addrp) {
  nr_socket_wrapped *wrapped = static_cast<nr_socket_wrapped *>(obj);

  return nr_socket_getaddr(wrapped->sock_, addrp);
}

static int nr_socket_wrapped_close(void *obj) {
  MOZ_CRASH();
}

static int nr_socket_wrapped_set_send_addr(nr_socket *sock, nr_transport_addr *addr) {
  nr_socket_wrapped *wrapped = static_cast<nr_socket_wrapped *>(sock->obj);

  return nr_transport_addr_copy(&wrapped->addr_, addr);
}

static nr_socket_vtbl nr_socket_wrapped_vtbl = {
  2,
  nr_socket_wrapped_destroy,
  nr_socket_wrapped_sendto,
  nr_socket_wrapped_recvfrom,
  nr_socket_wrapped_getfd,
  nr_socket_wrapped_getaddr,
  0,
  0,
  0,
  nr_socket_wrapped_close,
  0,
  0
};

int nr_socket_wrapped_create(nr_socket *inner, nr_socket **outp) {
  ScopedDeletePtr<nr_socket_wrapped> wrapped(new nr_socket_wrapped());

  wrapped->sock_ = inner;

  int r = nr_socket_create_int(wrapped.get(), &nr_socket_wrapped_vtbl, outp);
  if (r)
    return r;

  wrapped.forget();
  return 0;
}


// Instance static.
// Note: Calling Create() at static init time is not going to be safe, since
// we have no reason to expect this will be initted to a nullptr yet.
TestStunServer *TestStunServer::instance;
TestStunTcpServer *TestStunTcpServer::instance;
TestStunServer *TestStunServer::instance6;
TestStunTcpServer *TestStunTcpServer::instance6;
uint16_t TestStunServer::instance_port = 3478;
uint16_t TestStunTcpServer::instance_port = 3478;

TestStunServer::~TestStunServer() {
  // TODO(ekr@rtfm.com): Put this on the right thread.

  // Unhook callback from our listen socket.
  if (listen_sock_) {
    NR_SOCKET fd;
    if (!nr_socket_getfd(listen_sock_, &fd)) {
      NR_ASYNC_CANCEL(fd, NR_ASYNC_WAIT_READ);
    }
  }

  // Free up stun context and network resources
  nr_stun_server_ctx_destroy(&stun_server_);
  nr_socket_destroy(&listen_sock_);
  nr_socket_destroy(&send_sock_);

  // Make sure we aren't still waiting on a deferred response timer to pop
  if (timer_handle_)
    NR_async_timer_cancel(timer_handle_);

  delete response_addr_;
}

int TestStunServer::SetInternalPort(nr_local_addr *addr, uint16_t port) {
  if (nr_transport_addr_set_port(&addr->addr, port)) {
    MOZ_MTLOG(ML_ERROR, "Couldn't set port");
    return R_INTERNAL;
  }

  if (nr_transport_addr_fmt_addr_string(&addr->addr)) {
    MOZ_MTLOG(ML_ERROR, "Couldn't re-set addr string");
    return R_INTERNAL;
  }

  return 0;
}

int TestStunServer::TryOpenListenSocket(nr_local_addr *addr, uint16_t port) {

  int r = SetInternalPort(addr, port);

  if (r)
    return r;

  if (nr_socket_local_create(nullptr, &addr->addr, &listen_sock_)) {
    MOZ_MTLOG(ML_ERROR, "Couldn't create listen socket");
    return R_ALREADY;
  }

  return 0;
}

int TestStunServer::Initialize(int address_family) {
  static const size_t max_addrs = 100;
  nr_local_addr addrs[max_addrs];
  int addr_ct;
  int r;
  int i;

  r = nr_stun_find_local_addresses(addrs, max_addrs, &addr_ct);
  if (r) {
    MOZ_MTLOG(ML_ERROR, "Couldn't retrieve addresses");
    return R_INTERNAL;
  }

  if (addr_ct < 1) {
    MOZ_MTLOG(ML_ERROR, "No local addresses");
    return R_INTERNAL;
  }

  for (i = 0; i < addr_ct; ++i) {
    if (addrs[i].addr.addr->sa_family == address_family) {
      break;
    }
  }

  if (i == addr_ct) {
    MOZ_MTLOG(ML_ERROR, "No local addresses of the configured IP version");
    return R_INTERNAL;
  }

  int tries = 100;
  while (tries--) {
    // Bind on configured port (default 3478)
    r = TryOpenListenSocket(&addrs[i], instance_port);
    // We interpret R_ALREADY to mean the addr is probably in use. Try another.
    // Otherwise, it either worked or it didn't, and we check below.
    if (r != R_ALREADY) {
      break;
    }
    ++instance_port;
  }

  if (r) {
    return R_INTERNAL;
  }

  r = nr_socket_wrapped_create(listen_sock_, &send_sock_);
  if (r) {
    MOZ_MTLOG(ML_ERROR, "Couldn't create send socket");
    return R_INTERNAL;
  }

  r = nr_stun_server_ctx_create(const_cast<char *>("Test STUN server"),
                                send_sock_,
                                &stun_server_);
  if (r) {
    MOZ_MTLOG(ML_ERROR, "Couldn't create STUN server");
    return R_INTERNAL;
  }

  // Cache the address and port.
  char addr_string[INET6_ADDRSTRLEN];
  r = nr_transport_addr_get_addrstring(&addrs[i].addr, addr_string,
                                       sizeof(addr_string));
  if (r) {
    MOZ_MTLOG(ML_ERROR, "Failed to convert listen addr to a string representation");
    return R_INTERNAL;
  }

  listen_addr_ = addr_string;
  listen_port_ = instance_port;

  return 0;
}

TestStunServer* TestStunServer::Create(int address_family) {
  NR_reg_init(NR_REG_MODE_LOCAL);

  ScopedDeletePtr<TestStunServer> server(new TestStunServer());

  if (server->Initialize(address_family))
    return nullptr;

  NR_SOCKET fd;
  int r = nr_socket_getfd(server->listen_sock_, &fd);
  if (r) {
    MOZ_MTLOG(ML_ERROR, "Couldn't get fd");
    return nullptr;
  }

  NR_ASYNC_WAIT(fd, NR_ASYNC_WAIT_READ, &TestStunServer::readable_cb, server.get());

  return server.forget();
}

void TestStunServer::ConfigurePort(uint16_t port) {
  instance_port = port;
}

TestStunServer* TestStunServer::GetInstance(int address_family) {
  switch (address_family) {
    case AF_INET:
      if (!instance)
        instance = Create(address_family);

      MOZ_ASSERT(instance);
      return instance;
    case AF_INET6:
      if (!instance6)
        instance6 = Create(address_family);

      return instance6;
    default:
      MOZ_CRASH();
  }
}

void TestStunServer::ShutdownInstance() {
  delete instance;
  instance = nullptr;
  delete instance6;
  instance6 = nullptr;
}


struct DeferredStunOperation {
  DeferredStunOperation(TestStunServer *server,
                        const char *data, size_t len,
                        nr_transport_addr *addr,
                        nr_socket *sock) :
      server_(server),
      buffer_(reinterpret_cast<const uint8_t *>(data), len),
      sock_(sock) {
    nr_transport_addr_copy(&addr_, addr);
  }

  TestStunServer *server_;
  DataBuffer buffer_;
  nr_transport_addr addr_;
  nr_socket *sock_;
};

void TestStunServer::Process(const uint8_t *msg, size_t len, nr_transport_addr *addr, nr_socket *sock) {

  if (!sock) {
    sock = send_sock_;
  }

  // Set the wrapped address so that the response goes to the right place.
  nr_socket_wrapped_set_send_addr(sock, addr);

  nr_stun_server_process_request(stun_server_, sock,
                                 const_cast<char *>(reinterpret_cast<const char *>(msg)),
                                 len,
                                 response_addr_ ?
                                 response_addr_ : addr,
                                 NR_STUN_AUTH_RULE_OPTIONAL);
}

void TestStunServer::process_cb(NR_SOCKET s, int how, void *cb_arg) {
  DeferredStunOperation *op = static_cast<DeferredStunOperation *>(cb_arg);
  op->server_->timer_handle_ = nullptr;
  op->server_->Process(op->buffer_.data(), op->buffer_.len(), &op->addr_, op->sock_);

  delete op;
}

nr_socket* TestStunServer::GetReceivingSocket(NR_SOCKET s) {
  return listen_sock_;
}

nr_socket* TestStunServer::GetSendingSocket(nr_socket *sock) {
  return send_sock_;
}

void TestStunServer::readable_cb(NR_SOCKET s, int how, void *cb_arg) {
  TestStunServer *server = static_cast<TestStunServer*>(cb_arg);

  char message[max_stun_message_size];
  size_t message_len;
  nr_transport_addr addr;
  nr_socket *recv_sock = server->GetReceivingSocket(s);
  if (!recv_sock) {
    MOZ_MTLOG(ML_ERROR, "Failed to lookup receiving socket");
    return;
  }
  nr_socket *send_sock = server->GetSendingSocket(recv_sock);

  /* Re-arm. */
  NR_ASYNC_WAIT(s, NR_ASYNC_WAIT_READ, &TestStunServer::readable_cb, server);

  if (nr_socket_recvfrom(recv_sock, message, sizeof(message),
      &message_len, 0, &addr)) {
    MOZ_MTLOG(ML_ERROR, "Couldn't read STUN message");
    return;
  }

  MOZ_MTLOG(ML_DEBUG, "Received data of length " << message_len);

  // If we have initial dropping set, check at this point.
  std::string key(addr.as_string);

  if (server->received_ct_.count(key) == 0) {
    server->received_ct_[key] = 0;
  }

  ++server->received_ct_[key];

  if (!server->active_ || (server->received_ct_[key] <= server->initial_ct_)) {
    MOZ_MTLOG(ML_DEBUG, "Dropping message #"
              << server->received_ct_[key] << " from " << key);
    return;
  }

  if (server->delay_ms_) {
    NR_ASYNC_TIMER_SET(server->delay_ms_,
                       process_cb,
                       new DeferredStunOperation(
                           server,
                           message, message_len,
                           &addr, send_sock),
                       &server->timer_handle_);
  } else {
    server->Process(reinterpret_cast<const uint8_t *>(message), message_len,
                    &addr, send_sock);
  }
}

void TestStunServer::SetActive(bool active) {
  active_ = active;
}

void TestStunServer::SetDelay(uint32_t delay_ms) {
  delay_ms_ = delay_ms;
}

void TestStunServer::SetDropInitialPackets(uint32_t count) {
  initial_ct_ = count;
}

nsresult TestStunServer::SetResponseAddr(nr_transport_addr *addr) {
  delete response_addr_;

  response_addr_ = new nr_transport_addr();

  int r = nr_transport_addr_copy(response_addr_, addr);
  if (r)
    return NS_ERROR_FAILURE;

  return NS_OK;
}

nsresult TestStunServer::SetResponseAddr(const std::string& addr,
                                         uint16_t port) {
  nr_transport_addr addr2;

  int r = nr_str_port_to_transport_addr(addr.c_str(),
                                        port, IPPROTO_UDP,
                                        &addr2);
  if (r)
    return NS_ERROR_FAILURE;

  return SetResponseAddr(&addr2);
}

void TestStunServer::Reset() {
  delay_ms_ = 0;
  if (timer_handle_) {
    NR_async_timer_cancel(timer_handle_);
    timer_handle_ = nullptr;
  }
  delete response_addr_;
  response_addr_ = nullptr;
}


// TestStunTcpServer

void TestStunTcpServer::ConfigurePort(uint16_t port) {
  instance_port = port;
}

TestStunTcpServer* TestStunTcpServer::GetInstance(int address_family) {
  switch (address_family) {
    case AF_INET:
      if (!instance)
        instance = Create(address_family);

      MOZ_ASSERT(instance);
      return instance;
    case AF_INET6:
      if (!instance6)
        instance6 = Create(address_family);

      return instance6;
    default:
      MOZ_CRASH();
  }
}

void TestStunTcpServer::ShutdownInstance() {
  delete instance;
  instance = nullptr;
  delete instance6;
  instance6 = nullptr;
}

int TestStunTcpServer::TryOpenListenSocket(nr_local_addr *addr, uint16_t port) {

  addr->addr.protocol=IPPROTO_TCP;

  int r = SetInternalPort(addr, port);

  if (r)
    return r;

  nr_socket *sock;
  if (nr_socket_local_create(nullptr, &addr->addr, &sock)) {
    MOZ_MTLOG(ML_ERROR, "Couldn't create listen tcp socket");
    return R_ALREADY;
  }

  if (nr_socket_buffered_stun_create(sock, 2048, TURN_TCP_FRAMING, &listen_sock_)) {
    MOZ_MTLOG(ML_ERROR, "Couldn't create listen tcp socket");
    return R_ALREADY;
  }

  if(nr_socket_listen(listen_sock_, 10)) {
    MOZ_MTLOG(ML_ERROR, "Couldn't listen on socket");
    return R_ALREADY;
  }

  return 0;
}

nr_socket* TestStunTcpServer::GetReceivingSocket(NR_SOCKET s) {
  return connections_[s];
}

nr_socket* TestStunTcpServer::GetSendingSocket(nr_socket *sock) {
  return sock;
}

void TestStunTcpServer::accept_cb(NR_SOCKET s, int how, void *cb_arg) {
  TestStunTcpServer *server = static_cast<TestStunTcpServer*>(cb_arg);
  nr_socket *newsock, *bufsock, *wrapsock;
  nr_transport_addr remote_addr;
  NR_SOCKET fd;

  /* rearm */
  NR_ASYNC_WAIT(s, NR_ASYNC_WAIT_READ, &TestStunTcpServer::accept_cb, cb_arg);

  /* accept */
  if (nr_socket_accept(server->listen_sock_, &remote_addr, &newsock)) {
    MOZ_MTLOG(ML_ERROR, "Couldn't accept incoming tcp connection");
    return;
  }

  if(nr_socket_buffered_stun_create(newsock, 2048, TURN_TCP_FRAMING, &bufsock)) {
    MOZ_MTLOG(ML_ERROR, "Couldn't create connected tcp socket");
    return;
  }

  nr_socket_buffered_set_connected_to(bufsock, &remote_addr);

  if(nr_socket_wrapped_create(bufsock, &wrapsock)) {
    MOZ_MTLOG(ML_ERROR, "Couldn't wrap connected tcp socket");
    return;
  }

  if(nr_socket_getfd(bufsock, &fd)) {
    MOZ_MTLOG(ML_ERROR, "Couldn't get fd from connected tcp socket");
    return;
  }

  server->connections_[fd] = wrapsock;

  NR_ASYNC_WAIT(fd, NR_ASYNC_WAIT_READ, &TestStunServer::readable_cb, server);
}

TestStunTcpServer* TestStunTcpServer::Create(int address_family) {
  NR_reg_init(NR_REG_MODE_LOCAL);

  ScopedDeletePtr<TestStunTcpServer> server(new TestStunTcpServer());

  if (server->Initialize(address_family)) {
    return nullptr;
  }

  NR_SOCKET fd;
  if(nr_socket_getfd(server->listen_sock_, &fd)) {
    MOZ_MTLOG(ML_ERROR, "Couldn't get tcp fd");
    return nullptr;
  }

  NR_ASYNC_WAIT(fd, NR_ASYNC_WAIT_READ, &TestStunTcpServer::accept_cb, server.get());

  return server.forget();
}

TestStunTcpServer::~TestStunTcpServer() {
  for (auto it = connections_.begin(); it != connections_.end();) {
    NR_ASYNC_CANCEL(it->first, NR_ASYNC_WAIT_READ);
    nr_socket_destroy(&it->second);
    connections_.erase(it++);
  }
}

}  // close namespace