Skip to content

Commit 0803d8b

Browse files
committed
Merge pull request cpp-netlib#551 from DanielBujnik/0.11-devel-setting-source-port
Setting source port
2 parents 9e0cfaa + 87f6482 commit 0803d8b

File tree

7 files changed

+32
-20
lines changed

7 files changed

+32
-20
lines changed

boost/network/protocol/http/client/connection/async_normal.hpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,12 @@ struct http_async_connection
9595
this->method = method;
9696
boost::uint16_t port_ = port(request);
9797
string_type host_ = host(request);
98+
boost::uint16_t source_port = request.source_port();
99+
98100
resolve_(resolver_, host_, port_,
99101
request_strand_.wrap(boost::bind(
100102
&this_type::handle_resolved, this_type::shared_from_this(),
101-
host_, port_, get_body, callback,
103+
host_, port_, source_port, get_body, callback,
102104
generator, boost::arg<1>(), boost::arg<2>())));
103105
if (timeout_ > 0) {
104106
timer_.expires_from_now(boost::posix_time::seconds(timeout_));
@@ -129,7 +131,7 @@ struct http_async_connection
129131
is_timedout_ = true;
130132
}
131133

132-
void handle_resolved(string_type host, boost::uint16_t port, bool get_body,
134+
void handle_resolved(string_type host, boost::uint16_t port, boost::uint16_t source_port, bool get_body,
133135
body_callback_function_type callback,
134136
body_generator_function_type generator,
135137
boost::system::error_code const& ec,
@@ -141,10 +143,10 @@ struct http_async_connection
141143
resolver_iterator iter = boost::begin(endpoint_range);
142144
asio::ip::tcp::endpoint endpoint(iter->endpoint().address(), port);
143145
delegate_->connect(
144-
endpoint, host,
146+
endpoint, host, source_port,
145147
request_strand_.wrap(boost::bind(
146148
&this_type::handle_connected, this_type::shared_from_this(), host,
147-
port, get_body, callback, generator,
149+
port, source_port, get_body, callback, generator,
148150
std::make_pair(++iter, resolver_iterator()),
149151
placeholders::error)));
150152
} else {
@@ -154,7 +156,7 @@ struct http_async_connection
154156
}
155157
}
156158

157-
void handle_connected(string_type host, boost::uint16_t port, bool get_body,
159+
void handle_connected(string_type host, boost::uint16_t port, boost::uint16_t source_port, bool get_body,
158160
body_callback_function_type callback,
159161
body_generator_function_type generator,
160162
resolver_iterator_pair endpoint_range,
@@ -174,10 +176,10 @@ struct http_async_connection
174176
resolver_iterator iter = boost::begin(endpoint_range);
175177
asio::ip::tcp::endpoint endpoint(iter->endpoint().address(), port);
176178
delegate_->connect(
177-
endpoint, host,
179+
endpoint, host, source_port,
178180
request_strand_.wrap(boost::bind(
179181
&this_type::handle_connected, this_type::shared_from_this(),
180-
host, port, get_body, callback, generator,
182+
host, port, source_port, get_body, callback, generator,
181183
std::make_pair(++iter, resolver_iterator()),
182184
placeholders::error)));
183185
} else {

boost/network/protocol/http/client/connection/connection_delegate.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace http {
1313
namespace impl {
1414

1515
struct connection_delegate {
16-
virtual void connect(asio::ip::tcp::endpoint &endpoint, std::string host,
16+
virtual void connect(asio::ip::tcp::endpoint &endpoint, std::string host, boost::uint16_t source_port,
1717
function<void(system::error_code const &)> handler) = 0;
1818
virtual void write(
1919
asio::streambuf &command_streambuf,

boost/network/protocol/http/client/connection/normal_delegate.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace impl {
1919
struct normal_delegate : connection_delegate {
2020
normal_delegate(asio::io_service &service);
2121

22-
virtual void connect(asio::ip::tcp::endpoint &endpoint, std::string host,
22+
virtual void connect(asio::ip::tcp::endpoint &endpoint, std::string host, boost::uint16_t source_port,
2323
function<void(system::error_code const &)> handler);
2424
virtual void write(
2525
asio::streambuf &command_streambuf,

boost/network/protocol/http/client/connection/normal_delegate.ipp

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ boost::network::http::impl::normal_delegate::normal_delegate(
1919
: service_(service) {}
2020

2121
void boost::network::http::impl::normal_delegate::connect(
22-
asio::ip::tcp::endpoint &endpoint, std::string host,
22+
asio::ip::tcp::endpoint &endpoint, std::string host, boost::uint16_t source_port,
2323
function<void(system::error_code const &)> handler) {
2424

2525
// TODO(dberris): review parameter necessity.
2626
(void)host;
27-
28-
socket_.reset(new asio::ip::tcp::socket(service_));
27+
28+
socket_.reset(new asio::ip::tcp::socket(service_, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), source_port)));
2929
socket_->async_connect(endpoint, handler);
3030
}
3131

boost/network/protocol/http/client/connection/ssl_delegate.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct ssl_delegate : connection_delegate,
2929
optional<std::string> private_key_file,
3030
optional<std::string> ciphers, long ssl_options);
3131

32-
virtual void connect(asio::ip::tcp::endpoint &endpoint, std::string host,
32+
virtual void connect(asio::ip::tcp::endpoint &endpoint, std::string host, boost::uint16_t source_port,
3333
function<void(system::error_code const &)> handler);
3434
virtual void write(
3535
asio::streambuf &command_streambuf,
@@ -49,7 +49,8 @@ struct ssl_delegate : connection_delegate,
4949
optional<std::string> ciphers_;
5050
long ssl_options_;
5151
scoped_ptr<asio::ssl::context> context_;
52-
scoped_ptr<asio::ssl::stream<asio::ip::tcp::socket> > socket_;
52+
scoped_ptr<asio::ip::tcp::socket> tcp_socket_;
53+
scoped_ptr<asio::ssl::stream<asio::ip::tcp::socket&> > socket_;
5354
bool always_verify_peer_;
5455

5556
ssl_delegate(ssl_delegate const &); // = delete

boost/network/protocol/http/client/connection/ssl_delegate.ipp

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ boost::network::http::impl::ssl_delegate::ssl_delegate(
2929
always_verify_peer_(always_verify_peer) {}
3030

3131
void boost::network::http::impl::ssl_delegate::connect(
32-
asio::ip::tcp::endpoint &endpoint, std::string host,
32+
asio::ip::tcp::endpoint &endpoint, std::string host, boost::uint16_t source_port,
3333
function<void(system::error_code const &)> handler) {
3434
context_.reset(
3535
new asio::ssl::context(service_, asio::ssl::context::sslv23_client));
@@ -59,8 +59,11 @@ void boost::network::http::impl::ssl_delegate::connect(
5959
if (private_key_file_)
6060
context_->use_private_key_file(*private_key_file_,
6161
boost::asio::ssl::context::pem);
62+
63+
tcp_socket_.reset(new asio::ip::tcp::socket(service_, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), source_port)));
6264
socket_.reset(
63-
new asio::ssl::stream<asio::ip::tcp::socket>(service_, *context_));
65+
new asio::ssl::stream<asio::ip::tcp::socket&>(*(tcp_socket_.get()), *context_));
66+
6467
if (always_verify_peer_)
6568
socket_->set_verify_callback(boost::asio::ssl::rfc2818_verification(host));
6669
socket_->lowest_layer().async_connect(

boost/network/protocol/http/impl/request.hpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,26 @@ template <class Tag>
5555
struct basic_request : public basic_message<Tag> {
5656

5757
mutable boost::network::uri::uri uri_;
58+
boost::uint16_t source_port_;
5859
typedef basic_message<Tag> base_type;
5960

6061
public:
6162
typedef typename sync_only<Tag>::type tag;
6263
typedef typename string<tag>::type string_type;
6364
typedef boost::uint16_t port_type;
6465

65-
explicit basic_request(string_type const& uri_) : uri_(uri_) {}
66+
explicit basic_request(string_type const& uri_) : uri_(uri_), source_port_(0) {}
6667

67-
explicit basic_request(boost::network::uri::uri const& uri_) : uri_(uri_) {}
68+
explicit basic_request(boost::network::uri::uri const& uri_) : uri_(uri_), source_port_(0) {}
6869

6970
void uri(string_type const& new_uri) { uri_ = new_uri; }
7071

7172
void uri(boost::network::uri::uri const& new_uri) { uri_ = new_uri; }
7273

73-
basic_request() : base_type() {}
74+
basic_request() : base_type(), source_port_(0) {}
7475

7576
basic_request(basic_request const& other)
76-
: base_type(other), uri_(other.uri_) {}
77+
: base_type(other), uri_(other.uri_), source_port_(other.source_port_) {}
7778

7879
basic_request& operator=(basic_request rhs) {
7980
rhs.swap(*this);
@@ -85,6 +86,7 @@ struct basic_request : public basic_message<Tag> {
8586
basic_request<Tag>& this_ref(*this);
8687
base_ref.swap(this_ref);
8788
boost::swap(other.uri_, this->uri_);
89+
boost::swap(other.source_port_, this->source_port_);
8890
}
8991

9092
string_type const host() const { return uri_.host(); }
@@ -110,6 +112,10 @@ struct basic_request : public basic_message<Tag> {
110112
void uri(string_type const& new_uri) const { uri_ = new_uri; }
111113

112114
boost::network::uri::uri const& uri() const { return uri_; }
115+
116+
void source_port(const boost::uint16_t port) { source_port_ = port; }
117+
118+
boost::uint16_t source_port() const { return source_port_; }
113119
};
114120

115121
/** This is the implementation of a POD request type

0 commit comments

Comments
 (0)