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

Annotation of /main/reTurn/reTurnServer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7501 - (hide annotations) (download)
Wed Dec 19 21:42:02 2007 UTC (11 years, 11 months 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 sgodin 7321 #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 sgodin 7423 #include "TcpServer.hxx"
9     #include "TlsServer.hxx"
10 sgodin 7321 #include "UdpServer.hxx"
11     #include "RequestHandler.hxx"
12     #include "TurnManager.hxx"
13 sgodin 7476 #include <rutil/WinLeakCheck.hxx>
14 sgodin 7501 #include <rutil/Log.hxx>
15     #include <rutil/Logger.hxx>
16     #include "ReTurnSubsystem.hxx"
17 sgodin 7321
18 sgodin 7501 #define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
19    
20 sgodin 7321 #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 sgodin 7322 #endif // defined(_WIN32)
39 sgodin 7321
40 sgodin 7322
41 sgodin 7321 class reTurnConfig
42     {
43     public:
44     // TODO
45     };
46    
47     int main(int argc, char* argv[])
48     {
49 sgodin 7476 #ifdef WIN32
50     resip::FindMemoryLeaks fml;
51     #endif
52    
53 sgodin 7321 try
54     {
55     // Check command line arguments.
56 sgodin 7348 if (argc != 6)
57 sgodin 7321 {
58 sgodin 7348 std::cerr << "Usage: reTurnServer <address> <turnPort> <stunPort> <altAddress> <altPort>\n";
59 sgodin 7321 std::cerr << " For IPv4, try:\n";
60 sgodin 7423 std::cerr << " reTurnServer 0.0.0.0 8777 3489 0.0.0.0 3589\n";
61 sgodin 7321 std::cerr << " For IPv6, try:\n";
62 sgodin 7423 std::cerr << " reTurnServer 0::0 8777 3489 0::0 3589\n";
63 sgodin 7321 return 1;
64     }
65    
66 sgodin 7501 // Initialize Logging - TODO make configurable
67     resip::Log::initialize(resip::Log::Cout, resip::Log::Info, argv[0]);
68    
69 sgodin 7321 // 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 sgodin 7365 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 sgodin 7423 unsigned short tlsTurnPort = turnPort + 1;
77     unsigned short tlsStunPort = stunPort + 1;
78 sgodin 7365 asio::ip::address turnAddress = asio::ip::address::from_string(argv[1]);
79     asio::ip::address altStunAddress = asio::ip::address::from_string(argv[4]);
80 sgodin 7321
81 sgodin 7418 boost::shared_ptr<reTurn::UdpServer> udpTurnServer;
82 sgodin 7423 boost::shared_ptr<reTurn::TcpServer> tcpTurnServer;
83     boost::shared_ptr<reTurn::TlsServer> tlsTurnServer;
84 sgodin 7418 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 sgodin 7423 boost::shared_ptr<reTurn::TcpServer> tcpStunServer;
89     boost::shared_ptr<reTurn::TlsServer> tlsStunServer;
90 sgodin 7348
91 sgodin 7365 // The one and only RequestHandler - if stun port is non-zero, then assume RFC3489 support is enabled and pass settings to request handler
92 sgodin 7501 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 sgodin 7365
98 sgodin 7348 if(turnPort != 0)
99     {
100 sgodin 7423 udpTurnServer.reset(new reTurn::UdpServer(ioService, requestHandler, turnAddress, turnPort, true /*turnFraming?*/));
101 sgodin 7418 udpTurnServer->start();
102 sgodin 7423 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 sgodin 7348 }
107    
108 sgodin 7365 if(stunPort != 0) // if stun port is non-zero, then assume RFC3489 support is enabled
109 sgodin 7348 {
110 sgodin 7423 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 sgodin 7418 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 sgodin 7423 tcpStunServer.reset(new reTurn::TcpServer(ioService, requestHandler, turnAddress, stunPort, false /*turnFraming?*/));
123     tlsStunServer.reset(new reTurn::TlsServer(ioService, requestHandler, turnAddress, tlsStunPort, false /*turnFraming?*/));
124 sgodin 7348 }
125    
126 sgodin 7322 #ifdef _WIN32
127 sgodin 7321 // 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 sgodin 7322 #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 sgodin 7321
138     // Run the ioService until stopped.
139     // Create a pool of threads to run all of the io_services.
140 sgodin 7501 boost::shared_ptr<asio::thread> thread(new asio::thread(
141 sgodin 7321 boost::bind(&asio::io_service::run, &ioService)));
142    
143 sgodin 7322 #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 sgodin 7501 // Wait for thread to exit
159     thread->join();
160 sgodin 7321 }
161     catch (std::exception& e)
162     {
163 sgodin 7501 ErrLog(<< "exception: " << e.what());
164 sgodin 7321 }
165    
166     return 0;
167     }
168    
169    
170 sgodin 7366 /* ====================================================================
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