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

Annotation of /main/reTurn/reTurnServer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


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

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