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

Contents of /main/reTurn/reTurnServer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7501 - (show annotations) (download)
Wed Dec 19 21:42:02 2007 UTC (12 years, 1 month ago) by sgodin
File MIME type: text/plain
File size: 8120 byte(s)
-ServerRegistration.cxx - remove compiler warning
-BasicCall.cxx - fixed typo
-reTurn - use resip logging subsystem
-reTurn - code cleanup for converstion to/from StunAtrAddress and StunTuple
-OpenSSLInit - make sure locking function does not get called after de-initialization
 - this is possible on applications that use rutil with openSSL and openSSL from a different library

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 #ifdef WIN32
50 resip::FindMemoryLeaks fml;
51 #endif
52
53 try
54 {
55 // Check command line arguments.
56 if (argc != 6)
57 {
58 std::cerr << "Usage: reTurnServer <address> <turnPort> <stunPort> <altAddress> <altPort>\n";
59 std::cerr << " For IPv4, try:\n";
60 std::cerr << " reTurnServer 0.0.0.0 8777 3489 0.0.0.0 3589\n";
61 std::cerr << " For IPv6, try:\n";
62 std::cerr << " reTurnServer 0::0 8777 3489 0::0 3589\n";
63 return 1;
64 }
65
66 // Initialize Logging - TODO make configurable
67 resip::Log::initialize(resip::Log::Cout, resip::Log::Info, argv[0]);
68
69 // Initialize server.
70 asio::io_service ioService; // The one and only ioService for the stunServer
71 reTurn::TurnManager turnManager(ioService); // The one and only Turn Manager
72
73 unsigned short turnPort = (unsigned short)resip::Data(argv[2]).convertUnsignedLong();
74 unsigned short stunPort = (unsigned short)resip::Data(argv[3]).convertUnsignedLong();
75 unsigned short altStunPort = (unsigned short)resip::Data(argv[5]).convertUnsignedLong();
76 unsigned short tlsTurnPort = turnPort + 1;
77 unsigned short tlsStunPort = stunPort + 1;
78 asio::ip::address turnAddress = asio::ip::address::from_string(argv[1]);
79 asio::ip::address altStunAddress = asio::ip::address::from_string(argv[4]);
80
81 boost::shared_ptr<reTurn::UdpServer> udpTurnServer;
82 boost::shared_ptr<reTurn::TcpServer> tcpTurnServer;
83 boost::shared_ptr<reTurn::TlsServer> tlsTurnServer;
84 boost::shared_ptr<reTurn::UdpServer> a1p1StunUdpServer;
85 boost::shared_ptr<reTurn::UdpServer> a1p2StunUdpServer;
86 boost::shared_ptr<reTurn::UdpServer> a2p1StunUdpServer;
87 boost::shared_ptr<reTurn::UdpServer> a2p2StunUdpServer;
88 boost::shared_ptr<reTurn::TcpServer> tcpStunServer;
89 boost::shared_ptr<reTurn::TlsServer> tlsStunServer;
90
91 // The one and only RequestHandler - if stun port is non-zero, then assume RFC3489 support is enabled and pass settings to request handler
92 reTurn::RequestHandler requestHandler(turnManager,
93 stunPort != 0 ? &turnAddress : 0,
94 stunPort != 0 ? &turnPort : 0,
95 stunPort != 0 ? &altStunAddress : 0,
96 stunPort != 0 ? &altStunPort : 0);
97
98 if(turnPort != 0)
99 {
100 udpTurnServer.reset(new reTurn::UdpServer(ioService, requestHandler, turnAddress, turnPort, true /*turnFraming?*/));
101 udpTurnServer->start();
102 tcpTurnServer.reset(new reTurn::TcpServer(ioService, requestHandler, turnAddress, turnPort, true /*turnFraming?*/));
103 tcpTurnServer->start();
104 tlsTurnServer.reset(new reTurn::TlsServer(ioService, requestHandler, turnAddress, tlsTurnPort, true /*turnFraming?*/));
105 tlsTurnServer->start();
106 }
107
108 if(stunPort != 0) // if stun port is non-zero, then assume RFC3489 support is enabled
109 {
110 a1p1StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, turnAddress, stunPort, false /*turnFraming?*/));
111 a1p2StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, turnAddress, altStunPort, false /*turnFraming?*/));
112 a2p1StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, altStunAddress, stunPort, false /*turnFraming?*/));
113 a2p2StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, altStunAddress, altStunPort, false /*turnFraming?*/));
114 a1p1StunUdpServer->setAlternateUdpServers(a1p2StunUdpServer.get(), a2p1StunUdpServer.get(), a2p2StunUdpServer.get());
115 a1p2StunUdpServer->setAlternateUdpServers(a1p1StunUdpServer.get(), a2p2StunUdpServer.get(), a2p1StunUdpServer.get());
116 a2p1StunUdpServer->setAlternateUdpServers(a2p2StunUdpServer.get(), a1p1StunUdpServer.get(), a1p2StunUdpServer.get());
117 a2p2StunUdpServer->setAlternateUdpServers(a2p1StunUdpServer.get(), a1p2StunUdpServer.get(), a1p1StunUdpServer.get());
118 a1p1StunUdpServer->start();
119 a1p2StunUdpServer->start();
120 a2p1StunUdpServer->start();
121 a2p2StunUdpServer->start();
122 tcpStunServer.reset(new reTurn::TcpServer(ioService, requestHandler, turnAddress, stunPort, false /*turnFraming?*/));
123 tlsStunServer.reset(new reTurn::TlsServer(ioService, requestHandler, turnAddress, tlsStunPort, false /*turnFraming?*/));
124 }
125
126 #ifdef _WIN32
127 // Set console control handler to allow server to be stopped.
128 console_ctrl_function = boost::bind(&asio::io_service::stop, &ioService);
129 SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
130 #else
131 // Block all signals for background thread.
132 sigset_t new_mask;
133 sigfillset(&new_mask);
134 sigset_t old_mask;
135 pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);
136 #endif
137
138 // Run the ioService until stopped.
139 // Create a pool of threads to run all of the io_services.
140 boost::shared_ptr<asio::thread> thread(new asio::thread(
141 boost::bind(&asio::io_service::run, &ioService)));
142
143 #ifndef _WIN32
144 // Restore previous signals.
145 pthread_sigmask(SIG_SETMASK, &old_mask, 0);
146
147 // Wait for signal indicating time to shut down.
148 sigset_t wait_mask;
149 sigemptyset(&wait_mask);
150 sigaddset(&wait_mask, SIGINT);
151 sigaddset(&wait_mask, SIGQUIT);
152 sigaddset(&wait_mask, SIGTERM);
153 pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
154 int sig = 0;
155 sigwait(&wait_mask, &sig);
156 #endif
157
158 // Wait for thread to exit
159 thread->join();
160 }
161 catch (std::exception& e)
162 {
163 ErrLog(<< "exception: " << e.what());
164 }
165
166 return 0;
167 }
168
169
170 /* ====================================================================
171
172 Original contribution Copyright (C) 2007 Plantronics, Inc.
173 Provided under the terms of the Vovida Software License, Version 2.0.
174
175 The Vovida Software License, Version 2.0
176
177 Redistribution and use in source and binary forms, with or without
178 modification, are permitted provided that the following conditions
179 are met:
180
181 1. Redistributions of source code must retain the above copyright
182 notice, this list of conditions and the following disclaimer.
183
184 2. Redistributions in binary form must reproduce the above copyright
185 notice, this list of conditions and the following disclaimer in
186 the documentation and/or other materials provided with the
187 distribution.
188
189 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
190 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
191 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
192 NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
193 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
194 IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
195 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
196 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
197 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
198 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
199 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
200 USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
201 DAMAGE.
202
203 ==================================================================== */
204

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