/[resiprocate]/main/reTurn/reTurnServer.cxx
ViewVC logotype

Contents of /main/reTurn/reTurnServer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11221 - (show annotations) (download)
Sat Aug 30 14:15:44 2014 UTC (5 years, 2 months ago) by dpocock
File MIME type: text/plain
File size: 9504 byte(s)
reTurn: Logging: configuring Syslog facility
1 #include <iostream>
2 #include <csignal>
3 #include <string>
4 #include <asio.hpp>
5 #ifdef USE_SSL
6 #include <asio/ssl.hpp>
7 #endif
8 #include <boost/bind.hpp>
9 #include <boost/function.hpp>
10 #include <boost/lexical_cast.hpp>
11 #include <rutil/Data.hxx>
12 #include "reTurnServer.hxx"
13 #include "TcpServer.hxx"
14 #include "TlsServer.hxx"
15 #include "UdpServer.hxx"
16 #include "ReTurnConfig.hxx"
17 #include "RequestHandler.hxx"
18 #include "TurnManager.hxx"
19 #include <rutil/WinLeakCheck.hxx>
20 #include <rutil/Log.hxx>
21 #include <rutil/Logger.hxx>
22 #include "ReTurnSubsystem.hxx"
23
24 #define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
25
26 #if defined(_WIN32)
27
28 boost::function0<void> console_ctrl_function;
29
30 BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
31 {
32 switch (ctrl_type)
33 {
34 case CTRL_C_EVENT:
35 case CTRL_BREAK_EVENT:
36 case CTRL_CLOSE_EVENT:
37 case CTRL_SHUTDOWN_EVENT:
38 console_ctrl_function();
39 return TRUE;
40 default:
41 return FALSE;
42 }
43 }
44 #endif // defined(_WIN32)
45
46 int main(int argc, char* argv[])
47 {
48 reTurn::ReTurnServerProcess proc;
49 return proc.main(argc, argv);
50 }
51
52 reTurn::ReTurnServerProcess::ReTurnServerProcess()
53 {
54 }
55
56 reTurn::ReTurnServerProcess::~ReTurnServerProcess()
57 {
58 }
59
60 int
61 reTurn::ReTurnServerProcess::main(int argc, char* argv[])
62 {
63 #if defined(WIN32) && defined(_DEBUG) && defined(LEAK_CHECK)
64 resip::FindMemoryLeaks fml;
65 #endif
66
67 resip::Data defaultConfig("reTurnServer.config");
68 reTurn::ReTurnConfig reTurnConfig;
69 try
70 {
71 reTurnConfig.parseConfig(argc, argv, defaultConfig);
72 }
73 catch(std::exception& e)
74 {
75 ErrLog(<< "Exception parsing configuration: " << e.what());
76 exit(-1);
77 }
78
79 setPidFile(reTurnConfig.mPidFile);
80 if(isAlreadyRunning())
81 {
82 std::cerr << "Already running, will not start two instances. Please stop existing process and/or delete PID file.";
83 #ifndef WIN32
84 syslog(LOG_DAEMON | LOG_CRIT, "Already running, will not start two instances. Please stop existing process and/or delete PID file.");
85 #endif
86 return false;
87 }
88
89 // Daemonize if necessary
90 if(reTurnConfig.mDaemonize)
91 {
92 daemonize();
93 }
94
95 try
96 {
97 // Initialize Logging
98 resip::Log::initialize(reTurnConfig.mLoggingType, reTurnConfig.mLoggingLevel, "reTurnServer", reTurnConfig.mLoggingFilename.c_str(), 0, reTurnConfig.mSyslogFacility);
99 resip::GenericLogImpl::MaxLineCount = reTurnConfig.mLoggingFileMaxLineCount;
100
101 // Initialize server.
102 asio::io_service ioService; // The one and only ioService for the stunServer
103 reTurn::TurnManager turnManager(ioService, reTurnConfig); // The one and only Turn Manager
104
105 boost::shared_ptr<reTurn::UdpServer> udpTurnServer; // also a1p1StunUdpServer
106 boost::shared_ptr<reTurn::TcpServer> tcpTurnServer;
107 boost::shared_ptr<reTurn::TlsServer> tlsTurnServer;
108 boost::shared_ptr<reTurn::UdpServer> a1p2StunUdpServer;
109 boost::shared_ptr<reTurn::UdpServer> a2p1StunUdpServer;
110 boost::shared_ptr<reTurn::UdpServer> a2p2StunUdpServer;
111
112 #ifdef USE_IPV6
113 boost::shared_ptr<reTurn::UdpServer> udpV6TurnServer;
114 boost::shared_ptr<reTurn::TcpServer> tcpV6TurnServer;
115 boost::shared_ptr<reTurn::TlsServer> tlsV6TurnServer;
116 #endif
117
118 // The one and only RequestHandler - if altStunPort is non-zero, then assume RFC3489 support is enabled and pass settings to request handler
119 reTurn::RequestHandler requestHandler(turnManager,
120 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mTurnAddress : 0,
121 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mTurnPort : 0,
122 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mAltStunAddress : 0,
123 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mAltStunPort : 0);
124
125 udpTurnServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTurnPort));
126 tcpTurnServer.reset(new reTurn::TcpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTurnPort));
127 if(reTurnConfig.mTlsTurnPort != 0)
128 {
129 tlsTurnServer.reset(new reTurn::TlsServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTlsTurnPort));
130 }
131
132 #ifdef USE_IPV6
133 udpV6TurnServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mTurnV6Address, reTurnConfig.mTurnPort));
134 tcpV6TurnServer.reset(new reTurn::TcpServer(ioService, requestHandler, reTurnConfig.mTurnV6Address, reTurnConfig.mTurnPort));
135 if(reTurnConfig.mTlsTurnPort != 0)
136 {
137 tlsV6TurnServer.reset(new reTurn::TlsServer(ioService, requestHandler, reTurnConfig.mTurnV6Address, reTurnConfig.mTlsTurnPort));
138 }
139 #endif
140
141 if(reTurnConfig.mAltStunPort != 0) // if alt stun port is non-zero, then RFC3489 support is enabled
142 {
143 a1p2StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mAltStunPort));
144 a2p1StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mAltStunAddress, reTurnConfig.mTurnPort));
145 a2p2StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mAltStunAddress, reTurnConfig.mAltStunPort));
146 udpTurnServer->setAlternateUdpServers(a1p2StunUdpServer.get(), a2p1StunUdpServer.get(), a2p2StunUdpServer.get());
147 a1p2StunUdpServer->setAlternateUdpServers(udpTurnServer.get(), a2p2StunUdpServer.get(), a2p1StunUdpServer.get());
148 a2p1StunUdpServer->setAlternateUdpServers(a2p2StunUdpServer.get(), udpTurnServer.get(), a1p2StunUdpServer.get());
149 a2p2StunUdpServer->setAlternateUdpServers(a2p1StunUdpServer.get(), a1p2StunUdpServer.get(), udpTurnServer.get());
150 a1p2StunUdpServer->start();
151 a2p1StunUdpServer->start();
152 a2p2StunUdpServer->start();
153 }
154
155 udpTurnServer->start();
156 tcpTurnServer->start();
157 if(tlsTurnServer)
158 {
159 tlsTurnServer->start();
160 }
161
162 #ifdef USE_IPV6
163 udpV6TurnServer->start();
164 tcpV6TurnServer->start();
165 if(tlsV6TurnServer)
166 {
167 tlsV6TurnServer->start();
168 }
169 #endif
170
171 // Drop privileges (can do this now that sockets are bound)
172 if(!reTurnConfig.mRunAsUser.empty())
173 {
174 InfoLog( << "Trying to drop privileges, configured uid = " << reTurnConfig.mRunAsUser << " gid = " << reTurnConfig.mRunAsGroup);
175 dropPrivileges(reTurnConfig.mRunAsUser, reTurnConfig.mRunAsGroup);
176 }
177
178 ReTurnUserFileScanner userFileScanner(ioService, reTurnConfig);
179 userFileScanner.start();
180
181 #ifdef _WIN32
182 // Set console control handler to allow server to be stopped.
183 console_ctrl_function = boost::bind(&asio::io_service::stop, &ioService);
184 SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
185 #else
186 // Block all signals for background thread.
187 sigset_t new_mask;
188 sigfillset(&new_mask);
189 sigset_t old_mask;
190 pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);
191 #endif
192
193 // Run the ioService until stopped.
194 // Create a pool of threads to run all of the io_services.
195 boost::shared_ptr<asio::thread> thread(new asio::thread(
196 boost::bind(&asio::io_service::run, &ioService)));
197
198 #ifndef _WIN32
199 // Restore previous signals.
200 pthread_sigmask(SIG_SETMASK, &old_mask, 0);
201
202 // Wait for signal indicating time to shut down.
203 sigset_t wait_mask;
204 sigemptyset(&wait_mask);
205 sigaddset(&wait_mask, SIGINT);
206 sigaddset(&wait_mask, SIGQUIT);
207 sigaddset(&wait_mask, SIGTERM);
208 pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
209 int sig = 0;
210 sigwait(&wait_mask, &sig);
211 ioService.stop();
212 #endif
213
214 // Wait for thread to exit
215 thread->join();
216 }
217 catch (std::exception& e)
218 {
219 ErrLog(<< "exception: " << e.what());
220 }
221
222 return 0;
223 }
224
225
226 /* ====================================================================
227
228 Copyright (c) 2007-2008, Plantronics, Inc.
229 All rights reserved.
230
231 Redistribution and use in source and binary forms, with or without
232 modification, are permitted provided that the following conditions are
233 met:
234
235 1. Redistributions of source code must retain the above copyright
236 notice, this list of conditions and the following disclaimer.
237
238 2. Redistributions in binary form must reproduce the above copyright
239 notice, this list of conditions and the following disclaimer in the
240 documentation and/or other materials provided with the distribution.
241
242 3. Neither the name of Plantronics nor the names of its contributors
243 may be used to endorse or promote products derived from this
244 software without specific prior written permission.
245
246 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
247 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
248 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
249 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
250 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
251 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
252 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
253 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
254 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
256 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
257
258 ==================================================================== */

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27