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

Contents of /main/reTurn/reTurnServer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9362 - (show annotations) (download)
Tue Jan 31 09:37:56 2012 UTC (7 years, 10 months ago) by dpocock
File MIME type: text/plain
File size: 7640 byte(s)
Add support for daemonizing on platforms supporting fork()
1 #include <iostream>
2 #include <string>
3 #include <asio.hpp>
4 #include <boost/bind.hpp>
5 #include <boost/function.hpp>
6 #include <boost/lexical_cast.hpp>
7 #include <rutil/Data.hxx>
8 #include "TcpServer.hxx"
9 #include "TlsServer.hxx"
10 #include "UdpServer.hxx"
11 #include "ReTurnConfig.hxx"
12 #include "RequestHandler.hxx"
13 #include "TurnManager.hxx"
14 #include <rutil/WinLeakCheck.hxx>
15 #include <rutil/Log.hxx>
16 #include <rutil/Logger.hxx>
17 #include "ReTurnSubsystem.hxx"
18
19 #define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
20
21 #if defined(_WIN32)
22
23 boost::function0<void> console_ctrl_function;
24
25 BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
26 {
27 switch (ctrl_type)
28 {
29 case CTRL_C_EVENT:
30 case CTRL_BREAK_EVENT:
31 case CTRL_CLOSE_EVENT:
32 case CTRL_SHUTDOWN_EVENT:
33 console_ctrl_function();
34 return TRUE;
35 default:
36 return FALSE;
37 }
38 }
39 #endif // defined(_WIN32)
40
41 int main(int argc, char* argv[])
42 {
43 #if defined(WIN32) && defined(_DEBUG) && defined(LEAK_CHECK)
44 resip::FindMemoryLeaks fml;
45 #endif
46
47 resip::Data defaultConfig("reTurnServer.config");
48 reTurn::ReTurnConfig reTurnConfig(argc, argv, defaultConfig);
49
50 #ifndef WIN32
51 // Daemonize if necessary
52 if(reTurnConfig.mDaemonize)
53 {
54 pid_t pid;
55 if ((pid = fork()) < 0)
56 {
57 // fork() failed
58 throw std::runtime_error(strerror(errno));
59 }
60 else if (pid != 0)
61 {
62 // parent process done
63 exit(0);
64 }
65 if(chdir("/") < 0)
66 throw std::runtime_error(strerror(errno));
67 // Nothing should be writing to stdout/stderr after this
68 close(STDIN_FILENO);
69 close(STDOUT_FILENO);
70 close(STDERR_FILENO);
71 }
72 #endif
73
74 try
75 {
76 // Initialize Logging
77 resip::Log::initialize(reTurnConfig.mLoggingType, reTurnConfig.mLoggingLevel, "reTurnServer", reTurnConfig.mLoggingFilename.c_str());
78 resip::GenericLogImpl::MaxLineCount = reTurnConfig.mLoggingFileMaxLineCount;
79
80 // Initialize server.
81 asio::io_service ioService; // The one and only ioService for the stunServer
82 reTurn::TurnManager turnManager(ioService, reTurnConfig); // The one and only Turn Manager
83
84 boost::shared_ptr<reTurn::UdpServer> udpTurnServer; // also a1p1StunUdpServer
85 boost::shared_ptr<reTurn::TcpServer> tcpTurnServer;
86 boost::shared_ptr<reTurn::TlsServer> tlsTurnServer;
87 boost::shared_ptr<reTurn::UdpServer> a1p2StunUdpServer;
88 boost::shared_ptr<reTurn::UdpServer> a2p1StunUdpServer;
89 boost::shared_ptr<reTurn::UdpServer> a2p2StunUdpServer;
90
91 // The one and only RequestHandler - if altStunPort is non-zero, then assume RFC3489 support is enabled and pass settings to request handler
92 reTurn::RequestHandler requestHandler(turnManager,
93 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mTurnAddress : 0,
94 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mTurnPort : 0,
95 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mAltStunAddress : 0,
96 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mAltStunPort : 0);
97
98 udpTurnServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTurnPort));
99 tcpTurnServer.reset(new reTurn::TcpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTurnPort));
100 tlsTurnServer.reset(new reTurn::TlsServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTlsTurnPort));
101
102 if(reTurnConfig.mAltStunPort != 0) // if alt stun port is non-zero, then RFC3489 support is enabled
103 {
104 a1p2StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mAltStunPort));
105 a2p1StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mAltStunAddress, reTurnConfig.mTurnPort));
106 a2p2StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mAltStunAddress, reTurnConfig.mAltStunPort));
107 udpTurnServer->setAlternateUdpServers(a1p2StunUdpServer.get(), a2p1StunUdpServer.get(), a2p2StunUdpServer.get());
108 a1p2StunUdpServer->setAlternateUdpServers(udpTurnServer.get(), a2p2StunUdpServer.get(), a2p1StunUdpServer.get());
109 a2p1StunUdpServer->setAlternateUdpServers(a2p2StunUdpServer.get(), udpTurnServer.get(), a1p2StunUdpServer.get());
110 a2p2StunUdpServer->setAlternateUdpServers(a2p1StunUdpServer.get(), a1p2StunUdpServer.get(), udpTurnServer.get());
111 a1p2StunUdpServer->start();
112 a2p1StunUdpServer->start();
113 a2p2StunUdpServer->start();
114 }
115
116 udpTurnServer->start();
117 tcpTurnServer->start();
118 tlsTurnServer->start();
119
120 #ifdef _WIN32
121 // Set console control handler to allow server to be stopped.
122 console_ctrl_function = boost::bind(&asio::io_service::stop, &ioService);
123 SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
124 #else
125 // Block all signals for background thread.
126 sigset_t new_mask;
127 sigfillset(&new_mask);
128 sigset_t old_mask;
129 pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);
130 #endif
131
132 // Run the ioService until stopped.
133 // Create a pool of threads to run all of the io_services.
134 boost::shared_ptr<asio::thread> thread(new asio::thread(
135 boost::bind(&asio::io_service::run, &ioService)));
136
137 #ifndef _WIN32
138 // Restore previous signals.
139 pthread_sigmask(SIG_SETMASK, &old_mask, 0);
140
141 // Wait for signal indicating time to shut down.
142 sigset_t wait_mask;
143 sigemptyset(&wait_mask);
144 sigaddset(&wait_mask, SIGINT);
145 sigaddset(&wait_mask, SIGQUIT);
146 sigaddset(&wait_mask, SIGTERM);
147 pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
148 int sig = 0;
149 sigwait(&wait_mask, &sig);
150 ioService.stop();
151 #endif
152
153 // Wait for thread to exit
154 thread->join();
155 }
156 catch (std::exception& e)
157 {
158 ErrLog(<< "exception: " << e.what());
159 }
160
161 return 0;
162 }
163
164
165 /* ====================================================================
166
167 Copyright (c) 2007-2008, Plantronics, Inc.
168 All rights reserved.
169
170 Redistribution and use in source and binary forms, with or without
171 modification, are permitted provided that the following conditions are
172 met:
173
174 1. Redistributions of source code must retain the above copyright
175 notice, this list of conditions and the following disclaimer.
176
177 2. Redistributions in binary form must reproduce the above copyright
178 notice, this list of conditions and the following disclaimer in the
179 documentation and/or other materials provided with the distribution.
180
181 3. Neither the name of Plantronics nor the names of its contributors
182 may be used to endorse or promote products derived from this
183 software without specific prior written permission.
184
185 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
187 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
188 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
189 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
190 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
191 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
192 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
193 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
194 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
195 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
196
197 ==================================================================== */

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