reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
TurnUdpSocket.cxx
Go to the documentation of this file.
1#ifdef HAVE_CONFIG_H
2#include "config.h"
3#endif
4
5#include "TurnUdpSocket.hxx"
6#include "../AsyncSocketBase.hxx"
7
8using namespace std;
9
10namespace reTurn {
11
12TurnUdpSocket::TurnUdpSocket(const asio::ip::address& address, unsigned short port) :
13 TurnSocket(address,port),
14 mSocket(mIOService)
15{
17
18 asio::error_code errorCode;
19 mSocket.open(address.is_v6() ? asio::ip::udp::v6() : asio::ip::udp::v4(), errorCode);
20 if(!errorCode)
21 {
23 mSocket.bind(asio::ip::udp::endpoint(mLocalBinding.getAddress(), mLocalBinding.getPort()), errorCode);
24 }
25}
26
27asio::error_code
28TurnUdpSocket::connect(const std::string& address, unsigned short port)
29{
30 asio::error_code errorCode;
31
32 // Get a list of endpoints corresponding to the server name.
33 asio::ip::udp::resolver resolver(mIOService);
34 resip::Data service(port);
35 asio::ip::udp::resolver::results_type results = resolver.resolve(mSocket.local_endpoint().protocol(), address, service.c_str());
36 asio::ip::udp::resolver::results_type::const_iterator endpoint_iterator = results.begin();
37 asio::ip::udp::resolver::results_type::const_iterator end = results.end();
38
39 // Use first endpoint in list
40 if(endpoint_iterator == end)
41 {
42 return asio::error::host_not_found;
43 }
44
45 // Nothing to do for UDP except store the remote endpoint
46 mRemoteEndpoint = endpoint_iterator->endpoint();
47
48 mConnected = true;
50 mConnectedTuple.setAddress(mRemoteEndpoint.address());
51 mConnectedTuple.setPort(mRemoteEndpoint.port());
52
53 return errorCode;
54}
55
56asio::error_code
57TurnUdpSocket::rawWrite(const char* buffer, unsigned int size)
58{
59 asio::error_code errorCode;
60 mSocket.send_to(asio::buffer(buffer, size), mRemoteEndpoint, 0, errorCode);
61 return errorCode;
62}
63
64asio::error_code
65TurnUdpSocket::rawWrite(const std::vector<asio::const_buffer>& buffers)
66{
67 asio::error_code errorCode;
68 mSocket.send_to(buffers, mRemoteEndpoint, 0, errorCode);
69 return errorCode;
70}
71
72asio::error_code
73TurnUdpSocket::rawRead(unsigned int timeout, unsigned int* bytesRead, asio::ip::address* sourceAddress, unsigned short* sourcePort)
74{
75 startReadTimer(timeout);
76
77 mSocket.async_receive_from(asio::buffer(mReadBuffer, sizeof(mReadBuffer)), mSenderEndpoint, 0, std::bind(&TurnUdpSocket::handleRawRead, this, std::placeholders::_1, std::placeholders::_2));
78
79 // Wait for timer and read to end
80 mIOService.run();
81 mIOService.restart();
82
83 *bytesRead = (unsigned int)mBytesRead;
84
86 {
87 if(sourceAddress)
88 {
89 *sourceAddress = mSenderEndpoint.address();
90 }
91 if(sourcePort)
92 {
93 *sourcePort = mSenderEndpoint.port();
94 }
95 }
96 return mReadErrorCode;
97}
98
99void
101{
102 asio::error_code ec;
103 mSocket.cancel(ec);
104}
105
106} // namespace
107
108
109/* ====================================================================
110
111 Copyright (c) 2008-2026 SIP Spectrum, Inc. https://www.sipspectrum.com
112 Copyright (c) 2007-2008, Plantronics, Inc.
113 All rights reserved.
114
115 Redistribution and use in source and binary forms, with or without
116 modification, are permitted provided that the following conditions are
117 met:
118
119 1. Redistributions of source code must retain the above copyright
120 notice, this list of conditions and the following disclaimer.
121
122 2. Redistributions in binary form must reproduce the above copyright
123 notice, this list of conditions and the following disclaimer in the
124 documentation and/or other materials provided with the distribution.
125
126 3. Neither the name of Plantronics nor the names of its contributors
127 may be used to endorse or promote products derived from this
128 software without specific prior written permission.
129
130 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
131 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
132 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
133 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
134 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
135 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
136 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
137 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
138 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
139 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
140 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
141
142 ==================================================================== */
static void setExclusiveAddressUse(asio::ip::udp::socket &socket)
For a UDP socket, between open() and bind().
const asio::ip::address & getAddress() const
Definition StunTuple.hxx:44
unsigned int getPort() const
Definition StunTuple.hxx:47
void setPort(unsigned int port)
Definition StunTuple.hxx:48
void setAddress(const asio::ip::address &address)
Definition StunTuple.hxx:45
void setTransportType(TransportType transport)
Definition StunTuple.hxx:42
void handleRawRead(const asio::error_code &errorCode, size_t bytesRead)
asio::io_context mIOService
StunTuple mLocalBinding
asio::error_code mReadErrorCode
char mReadBuffer[8192]
void startReadTimer(unsigned int timeout)
StunTuple mConnectedTuple
virtual void cancelSocket()
TurnUdpSocket(const asio::ip::address &address, unsigned short port)
virtual asio::error_code rawRead(unsigned int timeout, unsigned int *bytesRead, asio::ip::address *sourceAddress=0, unsigned short *sourcePort=0)
virtual asio::error_code rawWrite(const char *buffer, unsigned int size)
virtual asio::error_code connect(const std::string &address, unsigned short port)
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
const char * c_str() const
Returns a null-terminated string representing.
Definition Data.cxx:1097