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

Contents of /main/reTurn/reTurnServer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8383 - (show annotations) (download)
Thu Jan 8 02:36:06 2009 UTC (10 years, 11 months ago) by sgodin
File MIME type: text/plain
File size: 8372 byte(s)
-more updates to move reTurn towards draft-behave-turn-12
-modified channel number range to 0x4000-0x7fff
-handle channel bind errors in async client
-remove allocation if 437 to Refresh request in clients
-implemented CreatePremission method
-return error if Don't Fragment bit is set (at least until DF is implemented)
-ensure reservation tokens can only allocate reserved ports numbers
-removed bandwidth attribute from responses
-verify channel number in channel bind request is in valid range
-allow starting server with no command line parameters



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 "RequestHandler.hxx"
12 #include "TurnManager.hxx"
13 #include <rutil/WinLeakCheck.hxx>
14 #include <rutil/Log.hxx>
15 #include <rutil/Logger.hxx>
16 #include "ReTurnSubsystem.hxx"
17
18 #define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
19
20 #if defined(_WIN32)
21
22 boost::function0<void> console_ctrl_function;
23
24 BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
25 {
26 switch (ctrl_type)
27 {
28 case CTRL_C_EVENT:
29 case CTRL_BREAK_EVENT:
30 case CTRL_CLOSE_EVENT:
31 case CTRL_SHUTDOWN_EVENT:
32 console_ctrl_function();
33 return TRUE;
34 default:
35 return FALSE;
36 }
37 }
38 #endif // defined(_WIN32)
39
40
41 class reTurnConfig
42 {
43 public:
44 // TODO
45 };
46
47 int main(int argc, char* argv[])
48 {
49 #if defined(WIN32) && defined(_DEBUG) && defined(LEAK_CHECK)
50 resip::FindMemoryLeaks fml;
51 #endif
52
53 try
54 {
55 unsigned short turnPort = 3478;
56 unsigned short tlsTurnPort = 5349;
57 unsigned short altStunPort = 0; // Note: The default is to disable RFC3489 binding support
58 asio::ip::address turnAddress = asio::ip::address::from_string("0.0.0.0");
59 asio::ip::address altStunAddress = asio::ip::address::from_string("0.0.0.0");
60
61 // Check command line arguments.
62 if (argc != 1 && argc != 6)
63 {
64 std::cerr << "Usage: reTurnServer <address> <turnPort> <tlsPort> <altAddress> \n";
65 std::cerr << " <altPort>\n";
66 std::cerr << " IPv4 Example (with RFC3478 support):\n";
67 std::cerr << " reTurnServer 192.168.1.10 3478 5349 192.168.1.11 3479\n\n";
68 std::cerr << " IPv6 Example (with RFC3478 support):\n";
69 std::cerr << " reTurnServer 3ffe:0501:0008:0000:0260:97ff:fe40:efab 3478\n";
70 std::cerr << " 5349 3ffe:0501:0008:0000:0260:97ff:fe40:efac 3479\n\n";
71 std::cerr << " Note: For RFC3489 legacy support define altPort as non-zero and\n";
72 std::cerr << " ensure you don't use INADDR_ANY for the IP addresses.\n";
73 std::cerr << " Both addresses should terminate on the machine running\n";
74 std::cerr << " reTurn.";
75 return 1;
76 }
77
78 // Initialize Logging - TODO make configurable
79 resip::Log::initialize(resip::Log::Cout, resip::Log::Info, argv[0]);
80 //resip::Log::initialize(resip::Log::Cout, resip::Log::Stack, argv[0]);
81
82 // Initialize server.
83 asio::io_service ioService; // The one and only ioService for the stunServer
84 reTurn::TurnManager turnManager(ioService); // The one and only Turn Manager
85
86 if(argc == 6)
87 {
88 turnPort = (unsigned short)resip::Data(argv[2]).convertUnsignedLong();
89 tlsTurnPort = (unsigned short)resip::Data(argv[3]).convertUnsignedLong();
90 altStunPort = (unsigned short)resip::Data(argv[5]).convertUnsignedLong();
91 turnAddress = asio::ip::address::from_string(argv[1]);
92 altStunAddress = asio::ip::address::from_string(argv[4]);
93 }
94
95 boost::shared_ptr<reTurn::UdpServer> udpTurnServer; // also a1p1StunUdpServer
96 boost::shared_ptr<reTurn::TcpServer> tcpTurnServer;
97 boost::shared_ptr<reTurn::TlsServer> tlsTurnServer;
98 boost::shared_ptr<reTurn::UdpServer> a1p2StunUdpServer;
99 boost::shared_ptr<reTurn::UdpServer> a2p1StunUdpServer;
100 boost::shared_ptr<reTurn::UdpServer> a2p2StunUdpServer;
101
102 // The one and only RequestHandler - if altStunPort is non-zero, then assume RFC3489 support is enabled and pass settings to request handler
103 reTurn::RequestHandler requestHandler(turnManager,
104 altStunPort != 0 ? &turnAddress : 0,
105 altStunPort != 0 ? &turnPort : 0,
106 altStunPort != 0 ? &altStunAddress : 0,
107 altStunPort != 0 ? &altStunPort : 0);
108
109 udpTurnServer.reset(new reTurn::UdpServer(ioService, requestHandler, turnAddress, turnPort));
110 tcpTurnServer.reset(new reTurn::TcpServer(ioService, requestHandler, turnAddress, turnPort));
111 tlsTurnServer.reset(new reTurn::TlsServer(ioService, requestHandler, turnAddress, tlsTurnPort));
112
113 if(altStunPort != 0) // if alt stun port is non-zero, then RFC3489 support is enabled
114 {
115 a1p2StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, turnAddress, altStunPort));
116 a2p1StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, altStunAddress, turnPort));
117 a2p2StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, altStunAddress, altStunPort));
118 udpTurnServer->setAlternateUdpServers(a1p2StunUdpServer.get(), a2p1StunUdpServer.get(), a2p2StunUdpServer.get());
119 a1p2StunUdpServer->setAlternateUdpServers(udpTurnServer.get(), a2p2StunUdpServer.get(), a2p1StunUdpServer.get());
120 a2p1StunUdpServer->setAlternateUdpServers(a2p2StunUdpServer.get(), udpTurnServer.get(), a1p2StunUdpServer.get());
121 a2p2StunUdpServer->setAlternateUdpServers(a2p1StunUdpServer.get(), a1p2StunUdpServer.get(), udpTurnServer.get());
122 a1p2StunUdpServer->start();
123 a2p1StunUdpServer->start();
124 a2p2StunUdpServer->start();
125 }
126
127 udpTurnServer->start();
128 tcpTurnServer->start();
129 tlsTurnServer->start();
130
131 #ifdef _WIN32
132 // Set console control handler to allow server to be stopped.
133 console_ctrl_function = boost::bind(&asio::io_service::stop, &ioService);
134 SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
135 #else
136 // Block all signals for background thread.
137 sigset_t new_mask;
138 sigfillset(&new_mask);
139 sigset_t old_mask;
140 pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);
141 #endif
142
143 // Run the ioService until stopped.
144 // Create a pool of threads to run all of the io_services.
145 boost::shared_ptr<asio::thread> thread(new asio::thread(
146 boost::bind(&asio::io_service::run, &ioService)));
147
148 #ifndef _WIN32
149 // Restore previous signals.
150 pthread_sigmask(SIG_SETMASK, &old_mask, 0);
151
152 // Wait for signal indicating time to shut down.
153 sigset_t wait_mask;
154 sigemptyset(&wait_mask);
155 sigaddset(&wait_mask, SIGINT);
156 sigaddset(&wait_mask, SIGQUIT);
157 sigaddset(&wait_mask, SIGTERM);
158 pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
159 int sig = 0;
160 sigwait(&wait_mask, &sig);
161 ioService.stop();
162 #endif
163
164 // Wait for thread to exit
165 thread->join();
166 }
167 catch (std::exception& e)
168 {
169 ErrLog(<< "exception: " << e.what());
170 }
171
172 return 0;
173 }
174
175
176 /* ====================================================================
177
178 Copyright (c) 2007-2008, Plantronics, Inc.
179 All rights reserved.
180
181 Redistribution and use in source and binary forms, with or without
182 modification, are permitted provided that the following conditions are
183 met:
184
185 1. Redistributions of source code must retain the above copyright
186 notice, this list of conditions and the following disclaimer.
187
188 2. Redistributions in binary form must reproduce the above copyright
189 notice, this list of conditions and the following disclaimer in the
190 documentation and/or other materials provided with the distribution.
191
192 3. Neither the name of Plantronics nor the names of its contributors
193 may be used to endorse or promote products derived from this
194 software without specific prior written permission.
195
196 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
197 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
198 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
199 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
200 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
201 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
202 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
203 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
204 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
205 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
206 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
207
208 ==================================================================== */

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