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

Contents of /main/reTurn/TlsConnection.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10481 - (show annotations) (download)
Thu Sep 12 07:59:36 2013 UTC (6 years, 2 months ago) by dpocock
File MIME type: text/plain
File size: 7051 byte(s)
reTurn: revert commits 10472 and 10473, now on branch b-reTurn-async
1 #ifdef WIN32
2 #pragma warning(disable : 4267)
3 #endif
4
5 #include "TlsConnection.hxx"
6 #include <vector>
7 #include <boost/bind.hpp>
8 #include "ConnectionManager.hxx"
9 #include "RequestHandler.hxx"
10 #include <rutil/Logger.hxx>
11 #include "ReTurnSubsystem.hxx"
12
13 #define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
14
15 using namespace std;
16 using namespace resip;
17
18 namespace reTurn {
19
20 TlsConnection::TlsConnection(asio::io_service& ioService,
21 ConnectionManager& manager,
22 RequestHandler& handler,
23 asio::ssl::context& context)
24 : AsyncTlsSocketBase(ioService, context, false /* not needed in server */),
25 mConnectionManager(manager),
26 mRequestHandler(handler)
27 {
28 }
29
30 TlsConnection::~TlsConnection()
31 {
32 DebugLog(<< "TlsConnection destroyed.");
33 }
34
35 ssl_socket::lowest_layer_type&
36 TlsConnection::socket()
37 {
38 return mSocket.lowest_layer();
39 }
40
41 void
42 TlsConnection::start()
43 {
44 DebugLog(<< "TlsConnection started.");
45
46 doHandshake();
47 }
48
49 void
50 TlsConnection::stop()
51 {
52 asio::error_code ec;
53 //mSocket.shutdown(ec); // !slg! note: this fn gives a stack overflow since ASIO 1.0.0 for some reason
54 mSocket.lowest_layer().close(ec);
55 if(ec)
56 {
57 WarningLog(<< "TlsConnection shutdown, error=" << ec.value() << "-" << ec.message());
58 }
59 }
60
61 void
62 TlsConnection::close()
63 {
64 mConnectionManager.stop(shared_from_this());
65 }
66
67 void
68 TlsConnection::onServerHandshakeSuccess()
69 {
70 DebugLog(<< "TlsConnection handshake completed.");
71 asio::error_code ec;
72 mLocalAddress = mSocket.lowest_layer().local_endpoint().address();
73 mLocalPort = mSocket.lowest_layer().local_endpoint().port();
74 doFramedReceive();
75 }
76
77 void
78 TlsConnection::onServerHandshakeFailure(const asio::error_code& e)
79 {
80 WarningLog(<< "TlsConnection handshake failure, error=" << e.value() << "-" << e.message());
81 close();
82 }
83
84 void
85 TlsConnection::onReceiveSuccess(const asio::ip::address& address, unsigned short port, boost::shared_ptr<DataBuffer>& data)
86 {
87 if (data->size() > 4)
88 {
89 /*
90 std::cout << "Read " << bytesTransferred << " bytes from tls socket (" << address.to_string() << ":" << port << "): " << std::endl;
91 cout << std::hex;
92 for(int i = 0; i < data->size(); i++)
93 {
94 std::cout << (char)(*data)[i] << "(" << int((*data)[i]) << ") ";
95 }
96 std::cout << std::dec << std::endl;
97 */
98
99 if(((*data)[0] & 0xC0) == 0) // Stun/Turn Messages always have bits 0 and 1 as 00 - otherwise ChannelData message
100 {
101 // Try to parse stun message
102 StunMessage request(StunTuple(StunTuple::TLS, mLocalAddress, mLocalPort),
103 StunTuple(StunTuple::TLS, address, port),
104 (char*)&(*data)[0], data->size());
105 if(request.isValid())
106 {
107 StunMessage response;
108 RequestHandler::ProcessResult result = mRequestHandler.processStunMessage(this, mTurnAllocationManager, request, response);
109
110 switch(result)
111 {
112 case RequestHandler::NoResponseToSend:
113 // No response to send - just receive next message
114 doFramedReceive();
115 return;
116 case RequestHandler::RespondFromAlternatePort:
117 case RequestHandler::RespondFromAlternateIp:
118 case RequestHandler::RespondFromAlternateIpPort:
119 // These only happen for UDP server for RFC3489 backwards compatibility
120 assert(false);
121 break;
122 case RequestHandler::RespondFromReceiving:
123 default:
124 break;
125 }
126 #define RESPONSE_BUFFER_SIZE 1024
127 boost::shared_ptr<DataBuffer> buffer = allocateBuffer(RESPONSE_BUFFER_SIZE);
128 unsigned int responseSize;
129 responseSize = response.stunEncodeMessage((char*)buffer->data(), RESPONSE_BUFFER_SIZE);
130 buffer->truncate(responseSize); // set size to real size
131
132 doSend(response.mRemoteTuple, buffer);
133 }
134 else
135 {
136 WarningLog(<< "Received invalid StunMessage. Dropping.");
137 }
138 }
139 else // ChannelData message
140 {
141 unsigned short channelNumber;
142 memcpy(&channelNumber, &(*data)[0], 2);
143 channelNumber = ntohs(channelNumber);
144
145 mRequestHandler.processTurnData(mTurnAllocationManager,
146 channelNumber,
147 StunTuple(StunTuple::TLS, mLocalAddress, mLocalPort),
148 StunTuple(StunTuple::TLS, address, port),
149 data);
150 }
151 }
152 else
153 {
154 WarningLog(<< "Not enough data for stun message or framed message. Closing connection.");
155 close();
156 return;
157 }
158
159 doFramedReceive();
160 }
161
162 void
163 TlsConnection::onReceiveFailure(const asio::error_code& e)
164 {
165 if(e != asio::error::operation_aborted)
166 {
167 InfoLog(<< "TlsConnection::onReceiveFailure: " << e.value() << "-" << e.message());
168
169 close();
170 }
171 }
172
173 void
174 TlsConnection::onSendSuccess()
175 {
176 }
177
178 void
179 TlsConnection::onSendFailure(const asio::error_code& error)
180 {
181 if(error != asio::error::operation_aborted)
182 {
183 InfoLog(<< "TlsConnection::onSendFailure: " << error.value() << "-" << error.message());
184 close();
185 }
186 }
187
188 }
189
190
191 /* ====================================================================
192
193 Copyright (c) 2007-2008, Plantronics, Inc.
194 All rights reserved.
195
196 Redistribution and use in source and binary forms, with or without
197 modification, are permitted provided that the following conditions are
198 met:
199
200 1. Redistributions of source code must retain the above copyright
201 notice, this list of conditions and the following disclaimer.
202
203 2. Redistributions in binary form must reproduce the above copyright
204 notice, this list of conditions and the following disclaimer in the
205 documentation and/or other materials provided with the distribution.
206
207 3. Neither the name of Plantronics nor the names of its contributors
208 may be used to endorse or promote products derived from this
209 software without specific prior written permission.
210
211 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
212 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
213 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
214 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
215 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
217 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
218 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
219 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
220 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
221 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
222
223 ==================================================================== */

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