|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 // Original author: ekr@rtfm.com |
|
8 |
|
9 #ifndef stunserver_h__ |
|
10 #define stunserver_h__ |
|
11 |
|
12 #include <map> |
|
13 #include <string> |
|
14 #include "prio.h" |
|
15 #include "nsError.h" |
|
16 |
|
17 typedef struct nr_stun_server_ctx_ nr_stun_server_ctx; |
|
18 typedef struct nr_socket_ nr_socket; |
|
19 typedef struct nr_local_addr_ nr_local_addr; |
|
20 |
|
21 namespace mozilla { |
|
22 |
|
23 class TestStunServer { |
|
24 public: |
|
25 // Generally, you should only call API in this class from the same thread that |
|
26 // the initial |GetInstance| call was made from. |
|
27 static TestStunServer *GetInstance(); |
|
28 static void ShutdownInstance(); |
|
29 // |ConfigurePort| will only have an effect if called before the first call |
|
30 // to |GetInstance| (possibly following a |ShutdownInstance| call) |
|
31 static void ConfigurePort(uint16_t port); |
|
32 static TestStunServer *Create(); |
|
33 |
|
34 ~TestStunServer(); |
|
35 |
|
36 void SetActive(bool active); |
|
37 void SetDelay(uint32_t delay_ms); |
|
38 void SetDropInitialPackets(uint32_t count); |
|
39 const std::string& addr() const { return listen_addr_; } |
|
40 uint16_t port() const { return listen_port_; } |
|
41 |
|
42 // These should only be called from the same thread as the initial |
|
43 // |GetInstance| call. |
|
44 nsresult SetResponseAddr(nr_transport_addr *addr); |
|
45 nsresult SetResponseAddr(const std::string& addr, uint16_t port); |
|
46 |
|
47 void Reset(); |
|
48 |
|
49 private: |
|
50 TestStunServer() |
|
51 : listen_sock_(nullptr), |
|
52 send_sock_(nullptr), |
|
53 stun_server_(nullptr), |
|
54 active_(true), |
|
55 delay_ms_(0), |
|
56 initial_ct_(0), |
|
57 response_addr_(nullptr), |
|
58 timer_handle_(nullptr), |
|
59 listen_port_(0) {} |
|
60 |
|
61 void Process(const uint8_t *msg, size_t len, nr_transport_addr *addr_in); |
|
62 int TryOpenListenSocket(nr_local_addr* addr, uint16_t port); |
|
63 |
|
64 static void readable_cb(NR_SOCKET sock, int how, void *cb_arg); |
|
65 static void process_cb(NR_SOCKET sock, int how, void *cb_arg); |
|
66 |
|
67 nr_socket *listen_sock_; |
|
68 nr_socket *send_sock_; |
|
69 nr_stun_server_ctx *stun_server_; |
|
70 bool active_; |
|
71 uint32_t delay_ms_; |
|
72 uint32_t initial_ct_; |
|
73 nr_transport_addr *response_addr_; |
|
74 void *timer_handle_; |
|
75 std::map<std::string, uint32_t> received_ct_; |
|
76 std::string listen_addr_; |
|
77 uint16_t listen_port_; |
|
78 |
|
79 static TestStunServer* instance; |
|
80 static uint16_t instance_port; |
|
81 }; |
|
82 |
|
83 } // End of namespace mozilla |
|
84 |
|
85 #endif |