Debian Codegraph @

cpp/src/connection_options.cpp

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21 #include "proton/connection_options.hpp"
22
23 #include "proton/connection.hpp"
24 #include <proton/codec/map.hpp>
25 #include "proton/codec/vector.hpp"
26 #include "proton/fwd.hpp"
27 #include "proton/messaging_handler.hpp"
28 #include "proton/reconnect_options.hpp"
29 #include "proton/transport.hpp"
30 #include "proton/ssl.hpp"
31 #include "proton/sasl.hpp"
32
33 #include "contexts.hpp"
34 #include "messaging_adapter.hpp"
35 #include "msg.hpp"
36 #include "proton_bits.hpp"
37 #include "reconnect_options_impl.hpp"
38 #include "ssl_options_impl.hpp"
39
40 #include <proton/connection.h>
41 #include <proton/proactor.h>
42 #include <proton/transport.h>
43
44 namespace proton {
45
46 template <class T> struct option {
47 T value;
48 bool set;
49
50 option() : value(), set(false) {}
51 option& operator=(const T& x) { value = x; set = true; return *this; }
52 void update(const option<T>& x) { if (x.set) *this = x.value; }
53 };
54
55 class connection_options::impl {
56 public:
57 option<messaging_handler*> handler;
58 option<uint32_t> max_frame_size;
59 option<uint16_t> max_sessions;
60 option<duration> idle_timeout;
61 option<std::string> container_id;
62 option<std::string> virtual_host;
63 option<std::string> user;
64 option<std::string> password;
65 option<std::vector<symbol> > offered_capabilities;
66 option<std::vector<symbol> > desired_capabilities;
67 option<std::map<symbol, value> > properties;
68 option<reconnect_options_base> reconnect;
69 option<std::string> reconnect_url;
70 option<std::vector<std::string> > failover_urls;
71 option<class ssl_client_options> ssl_client_options;
72 option<class ssl_server_options> ssl_server_options;
73 option<bool> sasl_enabled;
74 option<std::string> sasl_allowed_mechs;
75 option<bool> sasl_allow_insecure_mechs;
76 option<std::string> sasl_config_name;
77 option<std::string> sasl_config_path;
78
79 /*
80 * There are three types of connection options: the handler
81 * (required at creation, so too late to apply here), open frame
82 * options (that never change after the original open), and
83 * transport options (set once per transport over the life of the
84 * connection).
85 */
86 void apply_unbound(connection& c) {
87 pn_connection_t *pnc = unwrap(c);
88
89 // Only apply connection options if uninit.
90 bool uninit = c.uninitialized();
91 if (!uninit) return;
92
93 if (reconnect.set || reconnect_url.set || failover_urls.set) {
94 // Transfer reconnect options from connection options to reconnect contexts
95 // to stop the reconnect context being reset every retry unless there are new options
96 connection_context::get(pnc).reconnect_context_.reset(
97 new reconnect_context(
98 reconnect.set ? reconnect.value : reconnect_options_base()));
99 reconnect.set = false;
100 }
101 if (container_id.set)
102 pn_connection_set_container(pnc, container_id.value.c_str());
103 if (virtual_host.set)
104 pn_connection_set_hostname(pnc, virtual_host.value.c_str());
105 if (user.set)
106 pn_connection_set_user(pnc, user.value.c_str());
107 if (password.set)
108 pn_connection_set_password(pnc, password.value.c_str());
109 if (offered_capabilities.set)
110 value(pn_connection_offered_capabilities(pnc)) = offered_capabilities.value;
111 if (desired_capabilities.set)
112 value(pn_connection_desired_capabilities(pnc)) = desired_capabilities.value;
113 if (properties.set)
114 value(pn_connection_properties(pnc)) = properties.value;
115 }
116
117 void apply_reconnect_urls(pn_connection_t* pnc) {
118 connection_context& cc = connection_context::get(pnc);
119 // If there no reconnect delay parameters create default
120 if ((reconnect_url.set || failover_urls.set) && !cc.reconnect_context_)
121 cc.reconnect_context_.reset(new reconnect_context(reconnect_options_base()));
122 if (reconnect_url.set) {
123 reconnect_url.set = false;
124 cc.reconnect_url_ = reconnect_url.value;
125 cc.reconnect_context_->current_url_ = -1;
126 }
127 if (failover_urls.set && !failover_urls.value.empty()) {
128 failover_urls.set = false;
129 cc.failover_urls_ = failover_urls.value;
130 cc.reconnect_context_->current_url_ = 0;
131 }
132
133 }
134
135 void apply_transport(pn_transport_t* pnt) {
136 if (max_frame_size.set)
137 pn_transport_set_max_frame(pnt, max_frame_size.value);
138 if (max_sessions.set)
139 pn_transport_set_channel_max(pnt, max_sessions.value);
140 if (idle_timeout.set)
141 pn_transport_set_idle_timeout(pnt, idle_timeout.value.milliseconds());
142 }
143
144 void apply_sasl(pn_transport_t* pnt) {
145 // Transport options. pnt is NULL between reconnect attempts
146 // and if there is a pipelined open frame.
147 if (!pnt) return;
148
149 // Skip entirely if SASL explicitly disabled
150 if (!sasl_enabled.set || sasl_enabled.value) {
151 // We now default to enabling SASL even without specific SASL configuration
152 // This gives better interoperability and consistency across bindings
153 pn_sasl(pnt);
154 if (sasl_allow_insecure_mechs.set)
155 pn_sasl_set_allow_insecure_mechs(pn_sasl(pnt), sasl_allow_insecure_mechs.value);
156 if (sasl_allowed_mechs.set)
157 pn_sasl_allowed_mechs(pn_sasl(pnt), sasl_allowed_mechs.value.c_str());
158 if (sasl_config_name.set)
159 pn_sasl_config_name(pn_sasl(pnt), sasl_config_name.value.c_str());
160 if (sasl_config_path.set)
161 pn_sasl_config_path(pn_sasl(pnt), sasl_config_path.value.c_str());
162 }
163
164 }
165
166 void apply_ssl(pn_transport_t* pnt, bool client) {
167 // Transport options. pnt is NULL between reconnect attempts
168 // and if there is a pipelined open frame.
169 if (!pnt) return;
170
171 if (client && ssl_client_options.set) {
172 // A side effect of pn_ssl() is to set the ssl peer
173 // hostname to the connection hostname, which has
174 // already been adjusted for the virtual_host option.
175 pn_ssl_t *ssl = pn_ssl(pnt);
176 pn_ssl_domain_t* ssl_domain = ssl_client_options.value.impl_ ? ssl_client_options.value.impl_->pn_domain() : NULL;
177 if (pn_ssl_init(ssl, ssl_domain, NULL)) {
178 throw error(MSG("client SSL/TLS initialization error"));
179 }
180 } else if (!client && ssl_server_options.set) {
181 pn_ssl_t *ssl = pn_ssl(pnt);
182 pn_ssl_domain_t* ssl_domain = ssl_server_options.value.impl_ ? ssl_server_options.value.impl_->pn_domain() : pn_ssl_domain(PN_SSL_MODE_SERVER);
183 if (pn_ssl_init(ssl, ssl_domain, NULL)) {
184 throw error(MSG("server SSL/TLS initialization error"));
185 }
186 }
187
188 }
189
190 void update(const impl& x) {
191 handler.update(x.handler);
192 max_frame_size.update(x.max_frame_size);
193 max_sessions.update(x.max_sessions);
194 idle_timeout.update(x.idle_timeout);
195 container_id.update(x.container_id);
196 virtual_host.update(x.virtual_host);
197 user.update(x.user);
198 password.update(x.password);
199 offered_capabilities.update(x.offered_capabilities);
200 desired_capabilities.update(x.desired_capabilities);
201 properties.update(x.properties);
202 reconnect.update(x.reconnect);
203 reconnect_url.update(x.reconnect_url);
204 failover_urls.update(x.failover_urls);
205 ssl_client_options.update(x.ssl_client_options);
206 ssl_server_options.update(x.ssl_server_options);
207 sasl_enabled.update(x.sasl_enabled);
208 sasl_allow_insecure_mechs.update(x.sasl_allow_insecure_mechs);
209 sasl_allowed_mechs.update(x.sasl_allowed_mechs);
210 sasl_config_name.update(x.sasl_config_name);
211 sasl_config_path.update(x.sasl_config_path);
212 }
213
214 };
215
216 connection_options::connection_options() : impl_(new impl()) {}
217
218 connection_options::connection_options(class messaging_handler& h) : impl_(new impl()) { handler(h); }
219
220 connection_options::connection_options(const connection_options& x) : impl_(new impl()) {
221 *this = x;
222 }
223
224 connection_options::~connection_options() = default;
225
226 connection_options& connection_options::operator=(const connection_options& x) {
227 *impl_ = *x.impl_;
228 return *this;
229 }
230
231 connection_options& connection_options::update(const connection_options& x) {
232 impl_->update(*x.impl_);
233 return *this;
234 }
235
236 connection_options& connection_options::handler(class messaging_handler &h) { impl_->handler = &h; return *this; }
237 connection_options& connection_options::max_frame_size(uint32_t n) { impl_->max_frame_size = n; return *this; }
238 connection_options& connection_options::max_sessions(uint16_t n) { impl_->max_sessions = n; return *this; }
239 connection_options& connection_options::idle_timeout(duration t) { impl_->idle_timeout = t; return *this; }
240 connection_options& connection_options::container_id(const std::string &id) { impl_->container_id = id; return *this; }
241 connection_options& connection_options::virtual_host(const std::string &id) { impl_->virtual_host = id; return *this; }
242 connection_options& connection_options::user(const std::string &user) { impl_->user = user; return *this; }
243 connection_options& connection_options::password(const std::string &password) { impl_->password = password; return *this; }
244 connection_options& connection_options::offered_capabilities(const std::vector<symbol> &caps) { impl_->offered_capabilities = caps; return *this; }
245 connection_options& connection_options::desired_capabilities(const std::vector<symbol> &caps) { impl_->desired_capabilities = caps; return *this; }
246 connection_options& connection_options::properties(const std::map<symbol, value> &props) { impl_->properties = props; return *this; }
247 connection_options& connection_options::reconnect(const reconnect_options &r) {
248 if (!r.impl_->failover_urls.empty()) {
249 impl_->failover_urls = r.impl_->failover_urls;
250 }
251 impl_->reconnect = *r.impl_;
252 return *this;
253 }
254 connection_options& connection_options::reconnect_url(const std::string& u) { impl_->reconnect_url = u; return *this; }
255 connection_options &
256 connection_options::failover_urls(const std::vector<std::string> &us) {
257 if (!us.empty()) {
258 impl_->failover_urls = us;
259 }
260 return *this;
261 }
262 connection_options& connection_options::ssl_client_options(const class ssl_client_options &c) { impl_->ssl_client_options = c; return *this; }
263 connection_options& connection_options::ssl_server_options(const class ssl_server_options &c) { impl_->ssl_server_options = c; return *this; }
264 connection_options& connection_options::sasl_enabled(bool b) { impl_->sasl_enabled = b; return *this; }
265 connection_options& connection_options::sasl_allow_insecure_mechs(bool b) { impl_->sasl_allow_insecure_mechs = b; return *this; }
266 connection_options& connection_options::sasl_allowed_mechs(const std::string &s) { impl_->sasl_allowed_mechs = s; return *this; }
267 connection_options& connection_options::sasl_config_name(const std::string &n) { impl_->sasl_config_name = n; return *this; }
268 connection_options& connection_options::sasl_config_path(const std::string &p) { impl_->sasl_config_path = p; return *this; }
269
270 void connection_options::apply_unbound(connection& c) const { impl_->apply_unbound(c); }
271 void connection_options::apply_reconnect_urls(pn_connection_t *c) const { impl_->apply_reconnect_urls(c); }
272 void connection_options::apply_unbound_client(pn_transport_t *t) const { impl_->apply_sasl(t); impl_->apply_ssl(t, true); impl_->apply_transport(t); }
273 void connection_options::apply_unbound_server(pn_transport_t *t) const { impl_->apply_sasl(t); impl_->apply_ssl(t, false); impl_->apply_transport(t); }
274
275 messaging_handler* connection_options::handler() const { return impl_->handler.value; }
276
277 } // namespace proton