~ubuntu-branches/ubuntu/saucy/resiprocate/saucy-proposed

« back to all changes in this revision

Viewing changes to reTurn/reTurnServer.cxx

  • Committer: Package Import Robot
  • Author(s): Daniel Pocock
  • Date: 2012-05-17 19:29:59 UTC
  • Revision ID: package-import@ubuntu.com-20120517192959-vv00m77isztdy64q
Tags: upstream-1.8.2
ImportĀ upstreamĀ versionĀ 1.8.2

Show diffs side-by-side

added added

removed removed

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