stunserver.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. // Original author: ekr@rtfm.com
  6. /*
  7. Original code from nICEr and nrappkit.
  8. nICEr copyright:
  9. Copyright (c) 2007, Adobe Systems, Incorporated
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without
  12. modification, are permitted provided that the following conditions are
  13. met:
  14. * Redistributions of source code must retain the above copyright
  15. notice, this list of conditions and the following disclaimer.
  16. * Redistributions in binary form must reproduce the above copyright
  17. notice, this list of conditions and the following disclaimer in the
  18. documentation and/or other materials provided with the distribution.
  19. * Neither the name of Adobe Systems, Network Resonance nor the names of its
  20. contributors may be used to endorse or promote products derived from
  21. this software without specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. nrappkit copyright:
  34. Copyright (C) 2001-2003, Network Resonance, Inc.
  35. Copyright (C) 2006, Network Resonance, Inc.
  36. All Rights Reserved
  37. Redistribution and use in source and binary forms, with or without
  38. modification, are permitted provided that the following conditions
  39. are met:
  40. 1. Redistributions of source code must retain the above copyright
  41. notice, this list of conditions and the following disclaimer.
  42. 2. Redistributions in binary form must reproduce the above copyright
  43. notice, this list of conditions and the following disclaimer in the
  44. documentation and/or other materials provided with the distribution.
  45. 3. Neither the name of Network Resonance, Inc. nor the name of any
  46. contributors to this software may be used to endorse or promote
  47. products derived from this software without specific prior written
  48. permission.
  49. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
  50. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  52. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  53. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  54. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  55. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  56. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  57. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  58. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  59. POSSIBILITY OF SUCH DAMAGE.
  60. ekr@rtfm.com Thu Dec 20 20:14:49 2001
  61. */
  62. #include "logging.h"
  63. #include "mozilla/UniquePtr.h"
  64. #include "mozilla/Unused.h"
  65. #include "databuffer.h"
  66. extern "C" {
  67. #include "nr_api.h"
  68. #include "async_wait.h"
  69. #include "async_timer.h"
  70. #include "nr_socket.h"
  71. #include "nr_socket_local.h"
  72. #include "transport_addr.h"
  73. #include "addrs.h"
  74. #include "local_addr.h"
  75. #include "stun_util.h"
  76. #include "registry.h"
  77. #include "nr_socket_buffered_stun.h"
  78. }
  79. #include "stunserver.h"
  80. #include <string>
  81. MOZ_MTLOG_MODULE("stunserver");
  82. namespace mozilla {
  83. // Wrapper nr_socket which allows us to lie to the stun server about the
  84. // IP address.
  85. struct nr_socket_wrapped {
  86. nr_socket *sock_;
  87. nr_transport_addr addr_;
  88. };
  89. static int nr_socket_wrapped_destroy(void **objp) {
  90. if (!objp || !*objp)
  91. return 0;
  92. nr_socket_wrapped *wrapped = static_cast<nr_socket_wrapped *>(*objp);
  93. *objp = 0;
  94. delete wrapped;
  95. return 0;
  96. }
  97. static int nr_socket_wrapped_sendto(void *obj, const void *msg, size_t len, int flags,
  98. nr_transport_addr *addr) {
  99. nr_socket_wrapped *wrapped = static_cast<nr_socket_wrapped *>(obj);
  100. return nr_socket_sendto(wrapped->sock_, msg, len, flags, &wrapped->addr_);
  101. }
  102. static int nr_socket_wrapped_recvfrom(void *obj, void * restrict buf, size_t maxlen,
  103. size_t *len, int flags, nr_transport_addr *addr) {
  104. nr_socket_wrapped *wrapped = static_cast<nr_socket_wrapped *>(obj);
  105. return nr_socket_recvfrom(wrapped->sock_, buf, maxlen, len, flags, addr);
  106. }
  107. static int nr_socket_wrapped_getfd(void *obj, NR_SOCKET *fd) {
  108. nr_socket_wrapped *wrapped = static_cast<nr_socket_wrapped *>(obj);
  109. return nr_socket_getfd(wrapped->sock_, fd);
  110. }
  111. static int nr_socket_wrapped_getaddr(void *obj, nr_transport_addr *addrp) {
  112. nr_socket_wrapped *wrapped = static_cast<nr_socket_wrapped *>(obj);
  113. return nr_socket_getaddr(wrapped->sock_, addrp);
  114. }
  115. static int nr_socket_wrapped_close(void *obj) {
  116. MOZ_CRASH();
  117. }
  118. static int nr_socket_wrapped_set_send_addr(nr_socket *sock, nr_transport_addr *addr) {
  119. nr_socket_wrapped *wrapped = static_cast<nr_socket_wrapped *>(sock->obj);
  120. return nr_transport_addr_copy(&wrapped->addr_, addr);
  121. }
  122. static nr_socket_vtbl nr_socket_wrapped_vtbl = {
  123. 2,
  124. nr_socket_wrapped_destroy,
  125. nr_socket_wrapped_sendto,
  126. nr_socket_wrapped_recvfrom,
  127. nr_socket_wrapped_getfd,
  128. nr_socket_wrapped_getaddr,
  129. 0,
  130. 0,
  131. 0,
  132. nr_socket_wrapped_close,
  133. 0,
  134. 0
  135. };
  136. int nr_socket_wrapped_create(nr_socket *inner, nr_socket **outp) {
  137. auto wrapped = MakeUnique<nr_socket_wrapped>();
  138. wrapped->sock_ = inner;
  139. int r = nr_socket_create_int(wrapped.get(), &nr_socket_wrapped_vtbl, outp);
  140. if (r)
  141. return r;
  142. Unused << wrapped.release();
  143. return 0;
  144. }
  145. // Instance static.
  146. // Note: Calling Create() at static init time is not going to be safe, since
  147. // we have no reason to expect this will be initted to a nullptr yet.
  148. TestStunServer *TestStunServer::instance;
  149. TestStunTcpServer *TestStunTcpServer::instance;
  150. TestStunServer *TestStunServer::instance6;
  151. TestStunTcpServer *TestStunTcpServer::instance6;
  152. uint16_t TestStunServer::instance_port = 3478;
  153. uint16_t TestStunTcpServer::instance_port = 3478;
  154. TestStunServer::~TestStunServer() {
  155. // TODO(ekr@rtfm.com): Put this on the right thread.
  156. // Unhook callback from our listen socket.
  157. if (listen_sock_) {
  158. NR_SOCKET fd;
  159. if (!nr_socket_getfd(listen_sock_, &fd)) {
  160. NR_ASYNC_CANCEL(fd, NR_ASYNC_WAIT_READ);
  161. }
  162. }
  163. // Free up stun context and network resources
  164. nr_stun_server_ctx_destroy(&stun_server_);
  165. nr_socket_destroy(&listen_sock_);
  166. nr_socket_destroy(&send_sock_);
  167. // Make sure we aren't still waiting on a deferred response timer to pop
  168. if (timer_handle_)
  169. NR_async_timer_cancel(timer_handle_);
  170. delete response_addr_;
  171. }
  172. int TestStunServer::SetInternalPort(nr_local_addr *addr, uint16_t port) {
  173. if (nr_transport_addr_set_port(&addr->addr, port)) {
  174. MOZ_MTLOG(ML_ERROR, "Couldn't set port");
  175. return R_INTERNAL;
  176. }
  177. if (nr_transport_addr_fmt_addr_string(&addr->addr)) {
  178. MOZ_MTLOG(ML_ERROR, "Couldn't re-set addr string");
  179. return R_INTERNAL;
  180. }
  181. return 0;
  182. }
  183. int TestStunServer::TryOpenListenSocket(nr_local_addr *addr, uint16_t port) {
  184. int r = SetInternalPort(addr, port);
  185. if (r)
  186. return r;
  187. if (nr_socket_local_create(nullptr, &addr->addr, &listen_sock_)) {
  188. MOZ_MTLOG(ML_ERROR, "Couldn't create listen socket");
  189. return R_ALREADY;
  190. }
  191. return 0;
  192. }
  193. int TestStunServer::Initialize(int address_family) {
  194. static const size_t max_addrs = 100;
  195. nr_local_addr addrs[max_addrs];
  196. int addr_ct;
  197. int r;
  198. int i;
  199. r = nr_stun_find_local_addresses(addrs, max_addrs, &addr_ct);
  200. if (r) {
  201. MOZ_MTLOG(ML_ERROR, "Couldn't retrieve addresses");
  202. return R_INTERNAL;
  203. }
  204. if (addr_ct < 1) {
  205. MOZ_MTLOG(ML_ERROR, "No local addresses");
  206. return R_INTERNAL;
  207. }
  208. for (i = 0; i < addr_ct; ++i) {
  209. if (addrs[i].addr.addr->sa_family == address_family) {
  210. break;
  211. }
  212. }
  213. if (i == addr_ct) {
  214. MOZ_MTLOG(ML_ERROR, "No local addresses of the configured IP version");
  215. return R_INTERNAL;
  216. }
  217. int tries = 100;
  218. while (tries--) {
  219. // Bind on configured port (default 3478)
  220. r = TryOpenListenSocket(&addrs[i], instance_port);
  221. // We interpret R_ALREADY to mean the addr is probably in use. Try another.
  222. // Otherwise, it either worked or it didn't, and we check below.
  223. if (r != R_ALREADY) {
  224. break;
  225. }
  226. ++instance_port;
  227. }
  228. if (r) {
  229. return R_INTERNAL;
  230. }
  231. r = nr_socket_wrapped_create(listen_sock_, &send_sock_);
  232. if (r) {
  233. MOZ_MTLOG(ML_ERROR, "Couldn't create send socket");
  234. return R_INTERNAL;
  235. }
  236. r = nr_stun_server_ctx_create(const_cast<char *>("Test STUN server"),
  237. send_sock_,
  238. &stun_server_);
  239. if (r) {
  240. MOZ_MTLOG(ML_ERROR, "Couldn't create STUN server");
  241. return R_INTERNAL;
  242. }
  243. // Cache the address and port.
  244. char addr_string[INET6_ADDRSTRLEN];
  245. r = nr_transport_addr_get_addrstring(&addrs[i].addr, addr_string,
  246. sizeof(addr_string));
  247. if (r) {
  248. MOZ_MTLOG(ML_ERROR, "Failed to convert listen addr to a string representation");
  249. return R_INTERNAL;
  250. }
  251. listen_addr_ = addr_string;
  252. listen_port_ = instance_port;
  253. return 0;
  254. }
  255. UniquePtr<TestStunServer> TestStunServer::Create(int address_family) {
  256. NR_reg_init(NR_REG_MODE_LOCAL);
  257. UniquePtr<TestStunServer> server(new TestStunServer());
  258. if (server->Initialize(address_family))
  259. return nullptr;
  260. NR_SOCKET fd;
  261. int r = nr_socket_getfd(server->listen_sock_, &fd);
  262. if (r) {
  263. MOZ_MTLOG(ML_ERROR, "Couldn't get fd");
  264. return nullptr;
  265. }
  266. NR_ASYNC_WAIT(fd, NR_ASYNC_WAIT_READ, &TestStunServer::readable_cb, server.get());
  267. return server;
  268. }
  269. void TestStunServer::ConfigurePort(uint16_t port) {
  270. instance_port = port;
  271. }
  272. TestStunServer* TestStunServer::GetInstance(int address_family) {
  273. switch (address_family) {
  274. case AF_INET:
  275. if (!instance)
  276. instance = Create(address_family).release();
  277. MOZ_ASSERT(instance);
  278. return instance;
  279. case AF_INET6:
  280. if (!instance6)
  281. instance6 = Create(address_family).release();
  282. return instance6;
  283. default:
  284. MOZ_CRASH();
  285. }
  286. }
  287. void TestStunServer::ShutdownInstance() {
  288. delete instance;
  289. instance = nullptr;
  290. delete instance6;
  291. instance6 = nullptr;
  292. }
  293. struct DeferredStunOperation {
  294. DeferredStunOperation(TestStunServer *server,
  295. const char *data, size_t len,
  296. nr_transport_addr *addr,
  297. nr_socket *sock) :
  298. server_(server),
  299. buffer_(reinterpret_cast<const uint8_t *>(data), len),
  300. sock_(sock) {
  301. nr_transport_addr_copy(&addr_, addr);
  302. }
  303. TestStunServer *server_;
  304. DataBuffer buffer_;
  305. nr_transport_addr addr_;
  306. nr_socket *sock_;
  307. };
  308. void TestStunServer::Process(const uint8_t *msg, size_t len, nr_transport_addr *addr, nr_socket *sock) {
  309. if (!sock) {
  310. sock = send_sock_;
  311. }
  312. // Set the wrapped address so that the response goes to the right place.
  313. nr_socket_wrapped_set_send_addr(sock, addr);
  314. nr_stun_server_process_request(stun_server_, sock,
  315. const_cast<char *>(reinterpret_cast<const char *>(msg)),
  316. len,
  317. response_addr_ ?
  318. response_addr_ : addr,
  319. NR_STUN_AUTH_RULE_OPTIONAL);
  320. }
  321. void TestStunServer::process_cb(NR_SOCKET s, int how, void *cb_arg) {
  322. DeferredStunOperation *op = static_cast<DeferredStunOperation *>(cb_arg);
  323. op->server_->timer_handle_ = nullptr;
  324. op->server_->Process(op->buffer_.data(), op->buffer_.len(), &op->addr_, op->sock_);
  325. delete op;
  326. }
  327. nr_socket* TestStunServer::GetReceivingSocket(NR_SOCKET s) {
  328. return listen_sock_;
  329. }
  330. nr_socket* TestStunServer::GetSendingSocket(nr_socket *sock) {
  331. return send_sock_;
  332. }
  333. void TestStunServer::readable_cb(NR_SOCKET s, int how, void *cb_arg) {
  334. TestStunServer *server = static_cast<TestStunServer*>(cb_arg);
  335. char message[max_stun_message_size];
  336. size_t message_len;
  337. nr_transport_addr addr;
  338. nr_socket *recv_sock = server->GetReceivingSocket(s);
  339. if (!recv_sock) {
  340. MOZ_MTLOG(ML_ERROR, "Failed to lookup receiving socket");
  341. return;
  342. }
  343. nr_socket *send_sock = server->GetSendingSocket(recv_sock);
  344. /* Re-arm. */
  345. NR_ASYNC_WAIT(s, NR_ASYNC_WAIT_READ, &TestStunServer::readable_cb, server);
  346. if (nr_socket_recvfrom(recv_sock, message, sizeof(message),
  347. &message_len, 0, &addr)) {
  348. MOZ_MTLOG(ML_ERROR, "Couldn't read STUN message");
  349. return;
  350. }
  351. MOZ_MTLOG(ML_DEBUG, "Received data of length " << message_len);
  352. // If we have initial dropping set, check at this point.
  353. std::string key(addr.as_string);
  354. if (server->received_ct_.count(key) == 0) {
  355. server->received_ct_[key] = 0;
  356. }
  357. ++server->received_ct_[key];
  358. if (!server->active_ || (server->received_ct_[key] <= server->initial_ct_)) {
  359. MOZ_MTLOG(ML_DEBUG, "Dropping message #"
  360. << server->received_ct_[key] << " from " << key);
  361. return;
  362. }
  363. if (server->delay_ms_) {
  364. NR_ASYNC_TIMER_SET(server->delay_ms_,
  365. process_cb,
  366. new DeferredStunOperation(
  367. server,
  368. message, message_len,
  369. &addr, send_sock),
  370. &server->timer_handle_);
  371. } else {
  372. server->Process(reinterpret_cast<const uint8_t *>(message), message_len,
  373. &addr, send_sock);
  374. }
  375. }
  376. void TestStunServer::SetActive(bool active) {
  377. active_ = active;
  378. }
  379. void TestStunServer::SetDelay(uint32_t delay_ms) {
  380. delay_ms_ = delay_ms;
  381. }
  382. void TestStunServer::SetDropInitialPackets(uint32_t count) {
  383. initial_ct_ = count;
  384. }
  385. nsresult TestStunServer::SetResponseAddr(nr_transport_addr *addr) {
  386. delete response_addr_;
  387. response_addr_ = new nr_transport_addr();
  388. int r = nr_transport_addr_copy(response_addr_, addr);
  389. if (r)
  390. return NS_ERROR_FAILURE;
  391. return NS_OK;
  392. }
  393. nsresult TestStunServer::SetResponseAddr(const std::string& addr,
  394. uint16_t port) {
  395. nr_transport_addr addr2;
  396. int r = nr_str_port_to_transport_addr(addr.c_str(),
  397. port, IPPROTO_UDP,
  398. &addr2);
  399. if (r)
  400. return NS_ERROR_FAILURE;
  401. return SetResponseAddr(&addr2);
  402. }
  403. void TestStunServer::Reset() {
  404. delay_ms_ = 0;
  405. if (timer_handle_) {
  406. NR_async_timer_cancel(timer_handle_);
  407. timer_handle_ = nullptr;
  408. }
  409. delete response_addr_;
  410. response_addr_ = nullptr;
  411. received_ct_.clear();
  412. }
  413. // TestStunTcpServer
  414. void TestStunTcpServer::ConfigurePort(uint16_t port) {
  415. instance_port = port;
  416. }
  417. TestStunTcpServer* TestStunTcpServer::GetInstance(int address_family) {
  418. switch (address_family) {
  419. case AF_INET:
  420. if (!instance)
  421. instance = Create(address_family).release();
  422. MOZ_ASSERT(instance);
  423. return instance;
  424. case AF_INET6:
  425. if (!instance6)
  426. instance6 = Create(address_family).release();
  427. return instance6;
  428. default:
  429. MOZ_CRASH();
  430. }
  431. }
  432. void TestStunTcpServer::ShutdownInstance() {
  433. delete instance;
  434. instance = nullptr;
  435. delete instance6;
  436. instance6 = nullptr;
  437. }
  438. int TestStunTcpServer::TryOpenListenSocket(nr_local_addr *addr, uint16_t port) {
  439. addr->addr.protocol=IPPROTO_TCP;
  440. int r = SetInternalPort(addr, port);
  441. if (r)
  442. return r;
  443. nr_socket *sock;
  444. if (nr_socket_local_create(nullptr, &addr->addr, &sock)) {
  445. MOZ_MTLOG(ML_ERROR, "Couldn't create listen tcp socket");
  446. return R_ALREADY;
  447. }
  448. if (nr_socket_buffered_stun_create(sock, 2048, TURN_TCP_FRAMING, &listen_sock_)) {
  449. MOZ_MTLOG(ML_ERROR, "Couldn't create listen tcp socket");
  450. return R_ALREADY;
  451. }
  452. if(nr_socket_listen(listen_sock_, 10)) {
  453. MOZ_MTLOG(ML_ERROR, "Couldn't listen on socket");
  454. return R_ALREADY;
  455. }
  456. return 0;
  457. }
  458. nr_socket* TestStunTcpServer::GetReceivingSocket(NR_SOCKET s) {
  459. return connections_[s];
  460. }
  461. nr_socket* TestStunTcpServer::GetSendingSocket(nr_socket *sock) {
  462. return sock;
  463. }
  464. void TestStunTcpServer::accept_cb(NR_SOCKET s, int how, void *cb_arg) {
  465. TestStunTcpServer *server = static_cast<TestStunTcpServer*>(cb_arg);
  466. nr_socket *newsock, *bufsock, *wrapsock;
  467. nr_transport_addr remote_addr;
  468. NR_SOCKET fd;
  469. /* rearm */
  470. NR_ASYNC_WAIT(s, NR_ASYNC_WAIT_READ, &TestStunTcpServer::accept_cb, cb_arg);
  471. /* accept */
  472. if (nr_socket_accept(server->listen_sock_, &remote_addr, &newsock)) {
  473. MOZ_MTLOG(ML_ERROR, "Couldn't accept incoming tcp connection");
  474. return;
  475. }
  476. if(nr_socket_buffered_stun_create(newsock, 2048, TURN_TCP_FRAMING, &bufsock)) {
  477. MOZ_MTLOG(ML_ERROR, "Couldn't create connected tcp socket");
  478. nr_socket_destroy(&newsock);
  479. return;
  480. }
  481. nr_socket_buffered_set_connected_to(bufsock, &remote_addr);
  482. if(nr_socket_wrapped_create(bufsock, &wrapsock)) {
  483. MOZ_MTLOG(ML_ERROR, "Couldn't wrap connected tcp socket");
  484. nr_socket_destroy(&bufsock);
  485. return;
  486. }
  487. if(nr_socket_getfd(wrapsock, &fd)) {
  488. MOZ_MTLOG(ML_ERROR, "Couldn't get fd from connected tcp socket");
  489. nr_socket_destroy(&wrapsock);
  490. return;
  491. }
  492. server->connections_[fd] = wrapsock;
  493. NR_ASYNC_WAIT(fd, NR_ASYNC_WAIT_READ, &TestStunServer::readable_cb, server);
  494. }
  495. UniquePtr<TestStunTcpServer> TestStunTcpServer::Create(int address_family) {
  496. NR_reg_init(NR_REG_MODE_LOCAL);
  497. UniquePtr<TestStunTcpServer> server(new TestStunTcpServer());
  498. if (server->Initialize(address_family)) {
  499. return nullptr;
  500. }
  501. NR_SOCKET fd;
  502. if(nr_socket_getfd(server->listen_sock_, &fd)) {
  503. MOZ_MTLOG(ML_ERROR, "Couldn't get tcp fd");
  504. return nullptr;
  505. }
  506. NR_ASYNC_WAIT(fd, NR_ASYNC_WAIT_READ, &TestStunTcpServer::accept_cb, server.get());
  507. return server;
  508. }
  509. TestStunTcpServer::~TestStunTcpServer() {
  510. for (auto it = connections_.begin(); it != connections_.end();) {
  511. NR_ASYNC_CANCEL(it->first, NR_ASYNC_WAIT_READ);
  512. nr_socket_destroy(&it->second);
  513. connections_.erase(it++);
  514. }
  515. }
  516. } // close namespace