Coverage Report

Created: 2018-09-25 14:53

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