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

Contents of /main/reTurn/reTurnServer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8529 - (show annotations) (download)
Sat Jul 25 17:40:10 2009 UTC (10 years, 4 months ago) by sgodin
File MIME type: text/plain
File size: 8475 byte(s)
-pad the content of the SOFTWARE header so that we are on a 4 byte boundary for size 
  - this prevents STUN messages from reTurn showing up in Wireshark as malformed packets 
  - Note: Wireshark is assuming older RFC3489 message sizing rules
-fixed some typos in command line parameters help text
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 reTurn::ReTurnConfig reTurnConfig;
48
49 try
50 {
51 // Check command line arguments.
52 if (argc != 1 && argc != 6)
53 {
54 std::cerr << "Usage: reTurnServer <address> <turnPort> <tlsPort> <altAddress> \n";
55 std::cerr << " <altPort>\n";
56 std::cerr << " IPv4 Example (with RFC3489 support):\n";
57 std::cerr << " reTurnServer 192.168.1.10 3478 5349 192.168.1.11 3479\n\n";
58 std::cerr << " IPv6 Example (with RFC3489 support):\n";
59 std::cerr << " reTurnServer 3ffe:0501:0008:0000:0260:97ff:fe40:efab 3478\n";
60 std::cerr << " 5349 3ffe:0501:0008:0000:0260:97ff:fe40:efac 3479\n\n";
61 std::cerr << " Note: For RFC3489 legacy support define altPort as non-zero and\n";
62 std::cerr << " ensure you don't use INADDR_ANY for the IP addresses.\n";
63 std::cerr << " Both addresses should terminate on the machine running\n";
64 std::cerr << " reTurn.";
65 return 1;
66 }
67
68 // Initialize Logging
69 resip::Log::initialize(reTurnConfig.mLoggingType, reTurnConfig.mLoggingLevel, "reTurnServer", reTurnConfig.mLoggingFilename.c_str());
70 resip::GenericLogImpl::MaxLineCount = reTurnConfig.mLoggingFileMaxLineCount;
71
72 // Initialize server.
73 asio::io_service ioService; // The one and only ioService for the stunServer
74 reTurn::TurnManager turnManager(ioService, reTurnConfig); // The one and only Turn Manager
75
76 if(argc == 6)
77 {
78 reTurnConfig.mTurnPort = (unsigned short)resip::Data(argv[2]).convertUnsignedLong();
79 reTurnConfig.mTlsTurnPort = (unsigned short)resip::Data(argv[3]).convertUnsignedLong();
80 reTurnConfig.mAltStunPort = (unsigned short)resip::Data(argv[5]).convertUnsignedLong();
81 reTurnConfig.mTurnAddress = asio::ip::address::from_string(argv[1]);
82 reTurnConfig.mAltStunAddress = asio::ip::address::from_string(argv[4]);
83 }
84
85 boost::shared_ptr<reTurn::UdpServer> udpTurnServer; // also a1p1StunUdpServer
86 boost::shared_ptr<reTurn::TcpServer> tcpTurnServer;
87 boost::shared_ptr<reTurn::TlsServer> tlsTurnServer;
88 boost::shared_ptr<reTurn::UdpServer> a1p2StunUdpServer;
89 boost::shared_ptr<reTurn::UdpServer> a2p1StunUdpServer;
90 boost::shared_ptr<reTurn::UdpServer> a2p2StunUdpServer;
91
92 // The one and only RequestHandler - if altStunPort is non-zero, then assume RFC3489 support is enabled and pass settings to request handler
93 reTurn::RequestHandler requestHandler(turnManager,
94 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mTurnAddress : 0,
95 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mTurnPort : 0,
96 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mAltStunAddress : 0,
97 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mAltStunPort : 0);
98
99 udpTurnServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTurnPort));
100 tcpTurnServer.reset(new reTurn::TcpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTurnPort));
101 tlsTurnServer.reset(new reTurn::TlsServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTlsTurnPort));
102
103 if(reTurnConfig.mAltStunPort != 0) // if alt stun port is non-zero, then RFC3489 support is enabled
104 {
105 a1p2StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mAltStunPort));
106 a2p1StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mAltStunAddress, reTurnConfig.mTurnPort));
107 a2p2StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mAltStunAddress, reTurnConfig.mAltStunPort));
108 udpTurnServer->setAlternateUdpServers(a1p2StunUdpServer.get(), a2p1StunUdpServer.get(), a2p2StunUdpServer.get());
109 a1p2StunUdpServer->setAlternateUdpServers(udpTurnServer.get(), a2p2StunUdpServer.get(), a2p1StunUdpServer.get());
110 a2p1StunUdpServer->setAlternateUdpServers(a2p2StunUdpServer.get(), udpTurnServer.get(), a1p2StunUdpServer.get());
111 a2p2StunUdpServer->setAlternateUdpServers(a2p1StunUdpServer.get(), a1p2StunUdpServer.get(), udpTurnServer.get());
112 a1p2StunUdpServer->start();
113 a2p1StunUdpServer->start();
114 a2p2StunUdpServer->start();
115 }
116
117 udpTurnServer->start();
118 tcpTurnServer->start();
119 tlsTurnServer->start();
120
121 #ifdef _WIN32
122 // Set console control handler to allow server to be stopped.
123 console_ctrl_function = boost::bind(&asio::io_service::stop, &ioService);
124 SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
125 #else
126 // Block all signals for background thread.
127 sigset_t new_mask;
128 sigfillset(&new_mask);
129 sigset_t old_mask;
130 pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);
131 #endif
132
133 // Run the ioService until stopped.
134 // Create a pool of threads to run all of the io_services.
135 boost::shared_ptr<asio::thread> thread(new asio::thread(
136 boost::bind(&asio::io_service::run, &ioService)));
137
138 #ifndef _WIN32
139 // Restore previous signals.
140 pthread_sigmask(SIG_SETMASK, &old_mask, 0);
141
142 // Wait for signal indicating time to shut down.
143 sigset_t wait_mask;
144 sigemptyset(&wait_mask);
145 sigaddset(&wait_mask, SIGINT);
146 sigaddset(&wait_mask, SIGQUIT);
147 sigaddset(&wait_mask, SIGTERM);
148 pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
149 int sig = 0;
150 sigwait(&wait_mask, &sig);
151 ioService.stop();
152 #endif
153
154 // Wait for thread to exit
155 thread->join();
156 }
157 catch (std::exception& e)
158 {
159 ErrLog(<< "exception: " << e.what());
160 }
161
162 return 0;
163 }
164
165
166 /* ====================================================================
167
168 Copyright (c) 2007-2008, Plantronics, Inc.
169 All rights reserved.
170
171 Redistribution and use in source and binary forms, with or without
172 modification, are permitted provided that the following conditions are
173 met:
174
175 1. Redistributions of source code must retain the above copyright
176 notice, this list of conditions and the following disclaimer.
177
178 2. Redistributions in binary form must reproduce the above copyright
179 notice, this list of conditions and the following disclaimer in the
180 documentation and/or other materials provided with the distribution.
181
182 3. Neither the name of Plantronics nor the names of its contributors
183 may be used to endorse or promote products derived from this
184 software without specific prior written permission.
185
186 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
187 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
188 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
189 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
190 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
191 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
192 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
193 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
194 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
195 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
196 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
197
198 ==================================================================== */

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