1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/mtransport/test/stunserver.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,85 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +// Original author: ekr@rtfm.com 1.11 + 1.12 +#ifndef stunserver_h__ 1.13 +#define stunserver_h__ 1.14 + 1.15 +#include <map> 1.16 +#include <string> 1.17 +#include "prio.h" 1.18 +#include "nsError.h" 1.19 + 1.20 +typedef struct nr_stun_server_ctx_ nr_stun_server_ctx; 1.21 +typedef struct nr_socket_ nr_socket; 1.22 +typedef struct nr_local_addr_ nr_local_addr; 1.23 + 1.24 +namespace mozilla { 1.25 + 1.26 +class TestStunServer { 1.27 + public: 1.28 + // Generally, you should only call API in this class from the same thread that 1.29 + // the initial |GetInstance| call was made from. 1.30 + static TestStunServer *GetInstance(); 1.31 + static void ShutdownInstance(); 1.32 + // |ConfigurePort| will only have an effect if called before the first call 1.33 + // to |GetInstance| (possibly following a |ShutdownInstance| call) 1.34 + static void ConfigurePort(uint16_t port); 1.35 + static TestStunServer *Create(); 1.36 + 1.37 + ~TestStunServer(); 1.38 + 1.39 + void SetActive(bool active); 1.40 + void SetDelay(uint32_t delay_ms); 1.41 + void SetDropInitialPackets(uint32_t count); 1.42 + const std::string& addr() const { return listen_addr_; } 1.43 + uint16_t port() const { return listen_port_; } 1.44 + 1.45 + // These should only be called from the same thread as the initial 1.46 + // |GetInstance| call. 1.47 + nsresult SetResponseAddr(nr_transport_addr *addr); 1.48 + nsresult SetResponseAddr(const std::string& addr, uint16_t port); 1.49 + 1.50 + void Reset(); 1.51 + 1.52 + private: 1.53 + TestStunServer() 1.54 + : listen_sock_(nullptr), 1.55 + send_sock_(nullptr), 1.56 + stun_server_(nullptr), 1.57 + active_(true), 1.58 + delay_ms_(0), 1.59 + initial_ct_(0), 1.60 + response_addr_(nullptr), 1.61 + timer_handle_(nullptr), 1.62 + listen_port_(0) {} 1.63 + 1.64 + void Process(const uint8_t *msg, size_t len, nr_transport_addr *addr_in); 1.65 + int TryOpenListenSocket(nr_local_addr* addr, uint16_t port); 1.66 + 1.67 + static void readable_cb(NR_SOCKET sock, int how, void *cb_arg); 1.68 + static void process_cb(NR_SOCKET sock, int how, void *cb_arg); 1.69 + 1.70 + nr_socket *listen_sock_; 1.71 + nr_socket *send_sock_; 1.72 + nr_stun_server_ctx *stun_server_; 1.73 + bool active_; 1.74 + uint32_t delay_ms_; 1.75 + uint32_t initial_ct_; 1.76 + nr_transport_addr *response_addr_; 1.77 + void *timer_handle_; 1.78 + std::map<std::string, uint32_t> received_ct_; 1.79 + std::string listen_addr_; 1.80 + uint16_t listen_port_; 1.81 + 1.82 + static TestStunServer* instance; 1.83 + static uint16_t instance_port; 1.84 +}; 1.85 + 1.86 +} // End of namespace mozilla 1.87 + 1.88 +#endif