stunserver.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef stunserver_h__
  7. #define stunserver_h__
  8. #include <map>
  9. #include <string>
  10. #include "prio.h"
  11. #include "nsError.h"
  12. #include "mozilla/UniquePtr.h"
  13. typedef struct nr_stun_server_ctx_ nr_stun_server_ctx;
  14. typedef struct nr_socket_ nr_socket;
  15. typedef struct nr_local_addr_ nr_local_addr;
  16. namespace mozilla {
  17. class TestStunServer {
  18. public:
  19. // Generally, you should only call API in this class from the same thread that
  20. // the initial |GetInstance| call was made from.
  21. static TestStunServer *GetInstance(int address_family = AF_INET);
  22. static void ShutdownInstance();
  23. // |ConfigurePort| will only have an effect if called before the first call
  24. // to |GetInstance| (possibly following a |ShutdownInstance| call)
  25. static void ConfigurePort(uint16_t port);
  26. // AF_INET, AF_INET6
  27. static UniquePtr<TestStunServer> Create(int address_family);
  28. virtual ~TestStunServer();
  29. void SetActive(bool active);
  30. void SetDelay(uint32_t delay_ms);
  31. void SetDropInitialPackets(uint32_t count);
  32. const std::string& addr() const { return listen_addr_; }
  33. uint16_t port() const { return listen_port_; }
  34. // These should only be called from the same thread as the initial
  35. // |GetInstance| call.
  36. nsresult SetResponseAddr(nr_transport_addr *addr);
  37. nsresult SetResponseAddr(const std::string& addr, uint16_t port);
  38. void Reset();
  39. static const size_t max_stun_message_size = 4096;
  40. virtual nr_socket* GetReceivingSocket(NR_SOCKET s);
  41. virtual nr_socket* GetSendingSocket(nr_socket *sock);
  42. protected:
  43. TestStunServer()
  44. : listen_port_(0),
  45. listen_sock_(nullptr),
  46. send_sock_(nullptr),
  47. stun_server_(nullptr),
  48. active_(true),
  49. delay_ms_(0),
  50. initial_ct_(0),
  51. response_addr_(nullptr),
  52. timer_handle_(nullptr) {}
  53. int SetInternalPort(nr_local_addr *addr, uint16_t port);
  54. int Initialize(int address_family);
  55. static void readable_cb(NR_SOCKET sock, int how, void *cb_arg);
  56. private:
  57. void Process(const uint8_t *msg, size_t len, nr_transport_addr *addr_in, nr_socket *sock);
  58. virtual int TryOpenListenSocket(nr_local_addr *addr, uint16_t port);
  59. static void process_cb(NR_SOCKET sock, int how, void *cb_arg);
  60. protected:
  61. std::string listen_addr_;
  62. uint16_t listen_port_;
  63. nr_socket *listen_sock_;
  64. nr_socket *send_sock_;
  65. nr_stun_server_ctx *stun_server_;
  66. private:
  67. bool active_;
  68. uint32_t delay_ms_;
  69. uint32_t initial_ct_;
  70. nr_transport_addr *response_addr_;
  71. void *timer_handle_;
  72. std::map<std::string, uint32_t> received_ct_;
  73. static TestStunServer *instance;
  74. static TestStunServer *instance6;
  75. static uint16_t instance_port;
  76. };
  77. class TestStunTcpServer: public TestStunServer {
  78. public:
  79. static TestStunTcpServer *GetInstance(int address_family);
  80. static void ShutdownInstance();
  81. static void ConfigurePort(uint16_t port);
  82. virtual ~TestStunTcpServer();
  83. virtual nr_socket* GetReceivingSocket(NR_SOCKET s);
  84. virtual nr_socket* GetSendingSocket(nr_socket *sock);
  85. protected:
  86. TestStunTcpServer() {}
  87. static void accept_cb(NR_SOCKET sock, int how, void *cb_arg);
  88. private:
  89. virtual int TryOpenListenSocket(nr_local_addr *addr, uint16_t port);
  90. static UniquePtr<TestStunTcpServer> Create(int address_family);
  91. static TestStunTcpServer *instance;
  92. static TestStunTcpServer *instance6;
  93. static uint16_t instance_port;
  94. std::map<NR_SOCKET, nr_socket*> connections_;
  95. };
  96. } // End of namespace mozilla
  97. #endif