4
#include <boost/bind.hpp>
5
#include <boost/function.hpp>
6
#include <boost/lexical_cast.hpp>
7
#include <rutil/Data.hxx>
8
#include "reTurnServer.hxx"
9
#include "TcpServer.hxx"
10
#include "TlsServer.hxx"
11
#include "UdpServer.hxx"
12
#include "ReTurnConfig.hxx"
13
#include "RequestHandler.hxx"
14
#include "TurnManager.hxx"
15
#include <rutil/WinLeakCheck.hxx>
16
#include <rutil/Log.hxx>
17
#include <rutil/Logger.hxx>
18
#include "ReTurnSubsystem.hxx"
20
#define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
24
boost::function0<void> console_ctrl_function;
26
BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
31
case CTRL_BREAK_EVENT:
32
case CTRL_CLOSE_EVENT:
33
case CTRL_SHUTDOWN_EVENT:
34
console_ctrl_function();
40
#endif // defined(_WIN32)
42
int main(int argc, char* argv[])
44
reTurn::ReTurnServerProcess proc;
45
return proc.main(argc, argv);
48
reTurn::ReTurnServerProcess::ReTurnServerProcess()
52
reTurn::ReTurnServerProcess::~ReTurnServerProcess()
57
reTurn::ReTurnServerProcess::main(int argc, char* argv[])
59
#if defined(WIN32) && defined(_DEBUG) && defined(LEAK_CHECK)
60
resip::FindMemoryLeaks fml;
63
resip::Data defaultConfig("reTurnServer.config");
64
reTurn::ReTurnConfig reTurnConfig(argc, argv, defaultConfig);
66
setPidFile(reTurnConfig.mPidFile);
67
// Daemonize if necessary
68
if(reTurnConfig.mDaemonize)
74
resip::Log::initialize(reTurnConfig.mLoggingType, reTurnConfig.mLoggingLevel, "reTurnServer", reTurnConfig.mLoggingFilename.c_str());
75
resip::GenericLogImpl::MaxLineCount = reTurnConfig.mLoggingFileMaxLineCount;
78
asio::io_service ioService; // The one and only ioService for the stunServer
79
reTurn::TurnManager turnManager(ioService, reTurnConfig); // The one and only Turn Manager
81
boost::shared_ptr<reTurn::UdpServer> udpTurnServer; // also a1p1StunUdpServer
82
boost::shared_ptr<reTurn::TcpServer> tcpTurnServer;
83
boost::shared_ptr<reTurn::TlsServer> tlsTurnServer;
84
boost::shared_ptr<reTurn::UdpServer> a1p2StunUdpServer;
85
boost::shared_ptr<reTurn::UdpServer> a2p1StunUdpServer;
86
boost::shared_ptr<reTurn::UdpServer> a2p2StunUdpServer;
88
// The one and only RequestHandler - if altStunPort is non-zero, then assume RFC3489 support is enabled and pass settings to request handler
89
reTurn::RequestHandler requestHandler(turnManager,
90
reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mTurnAddress : 0,
91
reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mTurnPort : 0,
92
reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mAltStunAddress : 0,
93
reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mAltStunPort : 0);
95
udpTurnServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTurnPort));
96
tcpTurnServer.reset(new reTurn::TcpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTurnPort));
97
tlsTurnServer.reset(new reTurn::TlsServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTlsTurnPort));
99
if(reTurnConfig.mAltStunPort != 0) // if alt stun port is non-zero, then RFC3489 support is enabled
101
a1p2StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mAltStunPort));
102
a2p1StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mAltStunAddress, reTurnConfig.mTurnPort));
103
a2p2StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mAltStunAddress, reTurnConfig.mAltStunPort));
104
udpTurnServer->setAlternateUdpServers(a1p2StunUdpServer.get(), a2p1StunUdpServer.get(), a2p2StunUdpServer.get());
105
a1p2StunUdpServer->setAlternateUdpServers(udpTurnServer.get(), a2p2StunUdpServer.get(), a2p1StunUdpServer.get());
106
a2p1StunUdpServer->setAlternateUdpServers(a2p2StunUdpServer.get(), udpTurnServer.get(), a1p2StunUdpServer.get());
107
a2p2StunUdpServer->setAlternateUdpServers(a2p1StunUdpServer.get(), a1p2StunUdpServer.get(), udpTurnServer.get());
108
a1p2StunUdpServer->start();
109
a2p1StunUdpServer->start();
110
a2p2StunUdpServer->start();
113
udpTurnServer->start();
114
tcpTurnServer->start();
115
tlsTurnServer->start();
118
// Set console control handler to allow server to be stopped.
119
console_ctrl_function = boost::bind(&asio::io_service::stop, &ioService);
120
SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
122
// Block all signals for background thread.
124
sigfillset(&new_mask);
126
pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);
129
// Run the ioService until stopped.
130
// Create a pool of threads to run all of the io_services.
131
boost::shared_ptr<asio::thread> thread(new asio::thread(
132
boost::bind(&asio::io_service::run, &ioService)));
135
// Restore previous signals.
136
pthread_sigmask(SIG_SETMASK, &old_mask, 0);
138
// Wait for signal indicating time to shut down.
140
sigemptyset(&wait_mask);
141
sigaddset(&wait_mask, SIGINT);
142
sigaddset(&wait_mask, SIGQUIT);
143
sigaddset(&wait_mask, SIGTERM);
144
pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
146
sigwait(&wait_mask, &sig);
150
// Wait for thread to exit
153
catch (std::exception& e)
155
ErrLog(<< "exception: " << e.what());
162
/* ====================================================================
164
Copyright (c) 2007-2008, Plantronics, Inc.
167
Redistribution and use in source and binary forms, with or without
168
modification, are permitted provided that the following conditions are
171
1. Redistributions of source code must retain the above copyright
172
notice, this list of conditions and the following disclaimer.
174
2. Redistributions in binary form must reproduce the above copyright
175
notice, this list of conditions and the following disclaimer in the
176
documentation and/or other materials provided with the distribution.
178
3. Neither the name of Plantronics nor the names of its contributors
179
may be used to endorse or promote products derived from this
180
software without specific prior written permission.
182
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
183
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
185
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
186
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
187
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
188
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
189
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
190
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
191
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
192
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
194
==================================================================== */