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/. */
7 // Original author: ekr@rtfm.com
10 #define stunserver_h__
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
;
23 class TestStunServer
{
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();
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_
; }
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
);
51 : listen_sock_(nullptr),
53 stun_server_(nullptr),
57 response_addr_(nullptr),
58 timer_handle_(nullptr),
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
);
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
);
67 nr_socket
*listen_sock_
;
68 nr_socket
*send_sock_
;
69 nr_stun_server_ctx
*stun_server_
;
73 nr_transport_addr
*response_addr_
;
75 std::map
<std::string
, uint32_t> received_ct_
;
76 std::string listen_addr_
;
77 uint16_t listen_port_
;
79 static TestStunServer
* instance
;
80 static uint16_t instance_port
;
83 } // End of namespace mozilla