3 Copyright (c) 2003, Arvid Norberg
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #ifndef TORRENT_SOCKET_HPP_INCLUDED
34 #define TORRENT_SOCKET_HPP_INCLUDED
37 #pragma warning(push, 1)
40 // if building as Objective C++, asio's template
41 // parameters Protocol has to be renamed to avoid
42 // colliding with keywords
45 #define Protocol Protocol_
48 #include <boost/version.hpp>
50 #if BOOST_VERSION < 103500
51 #include <asio/ip/tcp.hpp>
52 #include <asio/ip/udp.hpp>
53 #include <asio/io_service.hpp>
54 #include <asio/deadline_timer.hpp>
55 #include <asio/write.hpp>
56 #include <asio/read.hpp>
57 #include <asio/time_traits.hpp>
58 #include <asio/basic_deadline_timer.hpp>
60 #include <boost/asio/ip/tcp.hpp>
61 #include <boost/asio/ip/udp.hpp>
62 #include <boost/asio/io_service.hpp>
63 #include <boost/asio/deadline_timer.hpp>
64 #include <boost/asio/write.hpp>
65 #include <boost/asio/read.hpp>
66 #include <boost/asio/time_traits.hpp>
67 #include <boost/asio/basic_deadline_timer.hpp>
74 #include "libtorrent/io.hpp"
75 #include "libtorrent/time.hpp"
76 #include "libtorrent/error_code.hpp"
85 #if BOOST_VERSION < 103500
88 using asio::async_write
;
89 using asio::async_read
;
91 typedef asio::ip::tcp::socket stream_socket
;
92 typedef asio::ip::address address
;
93 typedef asio::ip::address_v4 address_v4
;
94 typedef asio::ip::address_v6 address_v6
;
95 typedef asio::ip::udp::socket datagram_socket
;
96 typedef asio::ip::tcp::acceptor socket_acceptor
;
97 typedef asio::io_service io_service
;
99 namespace asio
= ::asio
;
100 typedef asio::basic_deadline_timer
<libtorrent::ptime
> deadline_timer
;
102 using boost::asio::ip::tcp
;
103 using boost::asio::ip::udp
;
104 using boost::asio::async_write
;
105 using boost::asio::async_read
;
107 typedef boost::asio::ip::tcp::socket stream_socket
;
108 typedef boost::asio::ip::address address
;
109 typedef boost::asio::ip::address_v4 address_v4
;
110 typedef boost::asio::ip::address_v6 address_v6
;
111 typedef boost::asio::ip::udp::socket datagram_socket
;
112 typedef boost::asio::ip::tcp::acceptor socket_acceptor
;
113 typedef boost::asio::io_service io_service
;
115 namespace asio
= boost::asio
;
116 typedef boost::asio::basic_deadline_timer
<libtorrent::ptime
> deadline_timer
;
119 inline std::ostream
& print_address(std::ostream
& os
, address
const& addr
)
122 std::string a
= addr
.to_string(ec
);
128 inline std::ostream
& print_endpoint(std::ostream
& os
, tcp::endpoint
const& ep
)
130 address
const& addr
= ep
.address();
132 std::string a
= addr
.to_string(ec
);
136 os
<< "[" << a
<< "]:";
145 template<class OutIt
>
146 void write_address(address
const& a
, OutIt
& out
)
150 write_uint32(a
.to_v4().to_ulong(), out
);
154 address_v6::bytes_type bytes
155 = a
.to_v6().to_bytes();
156 std::copy(bytes
.begin(), bytes
.end(), out
);
161 address
read_v4_address(InIt
& in
)
163 unsigned long ip
= read_uint32(in
);
164 return address_v4(ip
);
168 address
read_v6_address(InIt
& in
)
170 typedef address_v6::bytes_type bytes_t
;
172 for (bytes_t::iterator i
= bytes
.begin()
173 , end(bytes
.end()); i
!= end
; ++i
)
175 return address_v6(bytes
);
178 template<class Endpoint
, class OutIt
>
179 void write_endpoint(Endpoint
const& e
, OutIt
& out
)
181 write_address(e
.address(), out
);
182 write_uint16(e
.port(), out
);
185 template<class Endpoint
, class InIt
>
186 Endpoint
read_v4_endpoint(InIt
& in
)
188 address addr
= read_v4_address(in
);
189 int port
= read_uint16(in
);
190 return Endpoint(addr
, port
);
193 template<class Endpoint
, class InIt
>
194 Endpoint
read_v6_endpoint(InIt
& in
)
196 address addr
= read_v6_address(in
);
197 int port
= read_uint16(in
);
198 return Endpoint(addr
, port
);
204 v6only(bool enable
): m_value(enable
) {}
205 template<class Protocol
>
206 int level(Protocol
const&) const { return IPPROTO_IPV6
; }
207 template<class Protocol
>
208 int name(Protocol
const&) const { return IPV6_V6ONLY
; }
209 template<class Protocol
>
210 int const* data(Protocol
const&) const { return &m_value
; }
211 template<class Protocol
>
212 size_t size(Protocol
const&) const { return sizeof(m_value
); }
216 struct type_of_service
218 type_of_service(char val
): m_value(val
) {}
219 template<class Protocol
>
220 int level(Protocol
const&) const { return IPPROTO_IP
; }
221 template<class Protocol
>
222 int name(Protocol
const&) const { return IP_TOS
; }
223 template<class Protocol
>
224 char const* data(Protocol
const&) const { return &m_value
; }
225 template<class Protocol
>
226 size_t size(Protocol
const&) const { return sizeof(m_value
); }
231 #endif // TORRENT_SOCKET_HPP_INCLUDED