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

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

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