Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
tcpserver.h
Go to the documentation of this file.
1 /*
2  Copyright 2011 John Selbie
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 
18 #ifndef STUN_TCP_SERVER_H
19 #define STUN_TCP_SERVER_H
20 
21 #include "stuncore.h"
22 #include "stunauth.h"
23 #include "server.h"
24 #include "fasthash.h"
25 #include "messagehandler.h"
26 #include "stunconnection.h"
27 #include "polling.h"
28 #include "ratelimiter.h"
29 
30 
31 
33 {
34  static const int c_sweepTimeoutSeconds = 60;
35 
36 
37  int _pipe[2];
40  void ClosePipes();
41 
44 
45  boost::shared_ptr<RateLimiter> _spLimiter;
46 
47  // epoll helpers
48  HRESULT SetListenSocketsOnEpoll(bool fEnable);
49 
50  TransportAddressSet _tsaListen; // this is not what gets passed to CStunRequestHandler, see _tsa below
52  int _socketTable[4]; // same as _socketListenArray,but for quick lookup
55  void CloseListenSockets();
56  CStunSocket* GetListenSocket(int sock);
57 
58 
62 
65 
66  pthread_t _pthread;
68 
70 
71  // this is the function that runs in a thread
72  void Run();
73 
74  void Reset();
75 
76  static void* ThreadFunction(void* pThis);
77 
78  // ---------------------------------------------------------------
79  // thread data
80 
81  // maps socket back to connection
83 
84  StunThreadConnectionMap _hashConnections1;
85  StunThreadConnectionMap _hashConnections2;
86 
87  StunThreadConnectionMap* _pNewConnList;
88  StunThreadConnectionMap* _pOldConnList;
90 
91 
92 
94 
95  void ProcessConnectionEvent(int sock, uint32_t eventflags);
96 
99 
100  void CloseAllConnections(StunThreadConnectionMap* pConnMap);
101  void SweepDeadConnections();
102  void ThreadCleanup();
103  int GetTimeoutSeconds();
104  bool IsConnectionCountAtMax();
105  void CloseConnection(StunConnection* pConn);
106  bool RateCheck(const CSocketAddress& addr);
107 
108  // thread members
109 
110  // ---------------------------------------------------------------
111 
112 public:
113  CTCPStunThread();
114  ~CTCPStunThread();
115 
116  // tsaListen are the set of addresses we listen to connections on (either 1 address or 4 addresses)
117  // tsaHandler is what gets passed to the CStunRequestHandler for formation of the "other-address" attribute
118  HRESULT Init(const TransportAddressSet& tsaListen, const TransportAddressSet& tsaHandler, IStunAuth* pAuth, int maxConnections, boost::shared_ptr<RateLimiter>& spLimiter);
119  HRESULT Start();
120  HRESULT Stop();
121 };
122 
123 class CTCPServer :
124  public CBasicRefCount,
125  public CObjectFactory<CTCPServer>,
126  public IRefCounted
127 {
128 private:
129 
130  CTCPStunThread* _threads[4];
131 
133 
134  void InitTSA(TransportAddressSet* pTSA, SocketRole role, bool fValid, const CSocketAddress& addrListen, const CSocketAddress& addrAdvertise);
135 
136 public:
137 
138  CTCPServer();
139  virtual ~CTCPServer();
140 
141 
142  HRESULT Initialize(const CStunServerConfig& config);
143  HRESULT Shutdown();
144  HRESULT Start();
145  HRESULT Stop();
146 
148 
149 };
150 
151 
152 
153 
154 
155 
156 #endif /* SERVER_H */
157 
int GetTimeoutSeconds()
Definition: tcpserver.cpp:347
void CloseAllConnections(StunThreadConnectionMap *pConnMap)
Definition: tcpserver.cpp:743
void CloseConnection(StunConnection *pConn)
Definition: tcpserver.cpp:713
bool _fNeedToExit
Definition: tcpserver.h:59
HRESULT Start()
Definition: tcpserver.cpp:298
HRESULT NotifyThreadViaPipe()
Definition: tcpserver.cpp:124
HRESULT Stop()
Definition: tcpserver.cpp:318
void ThreadCleanup()
Definition: tcpserver.cpp:779
static const int c_sweepTimeoutSeconds
Definition: tcpserver.h:34
HRESULT CreatePipes()
Definition: tcpserver.cpp:93
StunThreadConnectionMap _hashConnections2
Definition: tcpserver.h:85
int _pipe[2]
Definition: tcpserver.h:37
void ClosePipes()
Definition: tcpserver.cpp:109
TransportAddressSet _tsaListen
Definition: tcpserver.h:50
void ProcessConnectionEvent(int sock, uint32_t eventflags)
Definition: tcpserver.cpp:430
pthread_t _pthread
Definition: tcpserver.h:66
CRefCountedPtr< IStunAuth > _spAuth
Definition: tcpserver.h:132
bool _fListenSocketsOnEpoll
Definition: tcpserver.h:43
SocketRole _role
Definition: tcpserver.h:61
void SweepDeadConnections()
Definition: tcpserver.cpp:753
StunThreadConnectionMap * _pOldConnList
Definition: tcpserver.h:88
HRESULT WriteBytesForConnection(StunConnection *pConn)
Definition: tcpserver.cpp:645
boost::shared_ptr< RateLimiter > _spLimiter
Definition: tcpserver.h:45
#define ADDREF_AND_RELEASE_IMPL()
void CloseListenSockets()
Definition: tcpserver.cpp:200
StunConnection * AcceptConnection(CStunSocket *pListenSocket)
Definition: tcpserver.cpp:484
CStunSocket _socketListenArray[4]
Definition: tcpserver.h:51
FastHashDynamic< int, StunConnection * > StunThreadConnectionMap
Definition: tcpserver.h:82
TransportAddressSet _tsa
Definition: tcpserver.h:63
StunThreadConnectionMap * _pNewConnList
Definition: tcpserver.h:87
static void * ThreadFunction(void *pThis)
Definition: tcpserver.cpp:341
StunThreadConnectionMap _hashConnections1
Definition: tcpserver.h:84
SocketRole
Definition: socketrole.h:22
int32_t HRESULT
Definition: hresult.h:22
int _maxConnections
Definition: tcpserver.h:64
HRESULT SetListenSocketsOnEpoll(bool fEnable)
Definition: tcpserver.cpp:138
int _socketTable[4]
Definition: tcpserver.h:52
HRESULT ReceiveBytesForConnection(StunConnection *pConn)
Definition: tcpserver.cpp:555
CStunSocket * GetListenSocket(int sock)
Definition: tcpserver.cpp:211
HRESULT CreateListenSockets()
Definition: tcpserver.cpp:171
bool IsConnectionCountAtMax()
Definition: tcpserver.cpp:353
bool _fThreadIsValid
Definition: tcpserver.h:67
HRESULT Init(const TransportAddressSet &tsaListen, const TransportAddressSet &tsaHandler, IStunAuth *pAuth, int maxConnections, boost::shared_ptr< RateLimiter > &spLimiter)
Definition: tcpserver.cpp:230
CRefCountedPtr< IPolling > _spPolling
Definition: tcpserver.h:42
time_t _timeLastSweep
Definition: tcpserver.h:89
CConnectionPool _connectionpool
Definition: tcpserver.h:69
CRefCountedPtr< IStunAuth > _spAuth
Definition: tcpserver.h:60
bool RateCheck(const CSocketAddress &addr)
Definition: tcpserver.cpp:463