| 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 |
|
| 22 |
#define PN_USE_DEPRECATED_API 1 |
| 23 |
|
| 24 |
#include <proton/connection.h> |
| 25 |
#include <proton/object.h> |
| 26 |
#include <proton/sasl.h> |
| 27 |
#include <proton/ssl.h> |
| 28 |
#include <proton/transport.h> |
| 29 |
#include <proton/url.h> |
| 30 |
#include <assert.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <string.h> |
| 33 |
#include "io.h" |
| 34 |
#include "selectable.h" |
| 35 |
#include "reactor.h" |
| 36 |
|
| 37 |
// XXX: overloaded for both directions |
| 38 |
PN_HANDLE(PN_TRANCTX) |
| 39 |
PN_HANDLE(PNI_CONN_PEER_ADDRESS) |
| 40 |
|
| 41 |
void pni_reactor_set_connection_peer_address(pn_connection_t *connection, |
| 42 |
const char *host, |
| 43 |
const char *port) |
| 44 |
{ |
| 45 |
pn_url_t *url = pn_url(); |
| 46 |
pn_url_set_host(url, host); |
| 47 |
pn_url_set_port(url, port); |
| 48 |
pn_record_t *record = pn_connection_attachments(connection); |
| 49 |
if (!pn_record_has(record, PNI_CONN_PEER_ADDRESS)) { |
| 50 |
pn_record_def(record, PNI_CONN_PEER_ADDRESS, PN_OBJECT); |
| 51 |
} |
| 52 |
pn_record_set(record, PNI_CONN_PEER_ADDRESS, url); |
| 53 |
pn_decref(url); |
| 54 |
} |
| 55 |
|
| 56 |
static pn_transport_t *pni_transport(pn_selectable_t *sel) { |
| 57 |
pn_record_t *record = pn_selectable_attachments(sel); |
| 58 |
return (pn_transport_t *) pn_record_get(record, PN_TRANCTX); |
| 59 |
} |
| 60 |
|
| 61 |
static ssize_t pni_connection_capacity(pn_selectable_t *sel) |
| 62 |
{ |
| 63 |
pn_transport_t *transport = pni_transport(sel); |
| 64 |
ssize_t capacity = pn_transport_capacity(transport); |
| 65 |
if (capacity < 0) { |
| 66 |
if (pn_transport_closed(transport)) { |
| 67 |
pn_selectable_terminate(sel); |
| 68 |
} |
| 69 |
} |
| 70 |
return capacity; |
| 71 |
} |
| 72 |
|
| 73 |
static ssize_t pni_connection_pending(pn_selectable_t *sel) |
| 74 |
{ |
| 75 |
pn_transport_t *transport = pni_transport(sel); |
| 76 |
ssize_t pending = pn_transport_pending(transport); |
| 77 |
if (pending < 0) { |
| 78 |
if (pn_transport_closed(transport)) { |
| 79 |
pn_selectable_terminate(sel); |
| 80 |
} |
| 81 |
} |
| 82 |
return pending; |
| 83 |
} |
| 84 |
|
| 85 |
static pn_timestamp_t pni_connection_deadline(pn_selectable_t *sel) |
| 86 |
{ |
| 87 |
pn_reactor_t *reactor = (pn_reactor_t *) pni_selectable_get_context(sel); |
| 88 |
pn_transport_t *transport = pni_transport(sel); |
| 89 |
pn_timestamp_t deadline = pn_transport_tick(transport, pn_reactor_now(reactor)); |
| 90 |
return deadline; |
| 91 |
} |
| 92 |
|
| 93 |
static void pni_connection_update(pn_selectable_t *sel) { |
| 94 |
ssize_t c = pni_connection_capacity(sel); |
| 95 |
ssize_t p = pni_connection_pending(sel); |
| 96 |
pn_selectable_set_reading(sel, c > 0); |
| 97 |
pn_selectable_set_writing(sel, p > 0); |
| 98 |
pn_selectable_set_deadline(sel, pni_connection_deadline(sel)); |
| 99 |
} |
| 100 |
|
| 101 |
void pni_handle_transport(pn_reactor_t *reactor, pn_event_t *event) { |
| 102 |
assert(reactor); |
| 103 |
pn_transport_t *transport = pn_event_transport(event); |
| 104 |
pn_record_t *record = pn_transport_attachments(transport); |
| 105 |
pn_selectable_t *sel = (pn_selectable_t *) pn_record_get(record, PN_TRANCTX); |
| 106 |
if (sel && !pn_selectable_is_terminal(sel)) { |
| 107 |
pni_connection_update(sel); |
| 108 |
pn_reactor_update(reactor, sel); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
pn_selectable_t *pn_reactor_selectable_transport(pn_reactor_t *reactor, pn_socket_t sock, pn_transport_t *transport); |
| 113 |
|
| 114 |
void pni_handle_open(pn_reactor_t *reactor, pn_event_t *event) { |
| 115 |
assert(reactor); |
| 116 |
assert(event); |
| 117 |
|
| 118 |
pn_connection_t *conn = pn_event_connection(event); |
| 119 |
if (!(pn_connection_state(conn) & PN_REMOTE_UNINIT)) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
pn_transport_t *transport = pn_transport(); |
| 124 |
pn_transport_bind(transport, conn); |
| 125 |
pn_decref(transport); |
| 126 |
} |
| 127 |
|
| 128 |
void pni_handle_bound(pn_reactor_t *reactor, pn_event_t *event) { |
| 129 |
assert(reactor); |
| 130 |
assert(event); |
| 131 |
|
| 132 |
pn_connection_t *conn = pn_event_connection(event); |
| 133 |
pn_transport_t *transport = pn_event_transport(event); |
| 134 |
pn_record_t *record = pn_connection_attachments(conn); |
| 135 |
pn_url_t *url = (pn_url_t *)pn_record_get(record, PNI_CONN_PEER_ADDRESS); |
| 136 |
const char *host = NULL; |
| 137 |
const char *port = "5672"; |
| 138 |
pn_string_t *str = NULL; |
| 139 |
|
| 140 |
// link the new transport to its reactor: |
| 141 |
pni_record_init_reactor(pn_transport_attachments(transport), reactor); |
| 142 |
|
| 143 |
if (pn_connection_acceptor(conn) != NULL) { |
| 144 |
// this connection was created by the acceptor. There is already a |
| 145 |
// socket assigned to this connection. Nothing needs to be done. |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
if (url) { |
| 150 |
host = pn_url_get_host(url); |
| 151 |
const char *uport = pn_url_get_port(url); |
| 152 |
if (uport) { |
| 153 |
port = uport; |
| 154 |
} else { |
| 155 |
const char *scheme = pn_url_get_scheme(url); |
| 156 |
if (scheme && strcmp(scheme, "amqps") == 0) { |
| 157 |
port = "5671"; |
| 158 |
} |
| 159 |
} |
| 160 |
if (!pn_connection_get_user(conn)) { |
| 161 |
// user did not manually set auth info |
| 162 |
const char *user = pn_url_get_username(url); |
| 163 |
if (user) pn_connection_set_user(conn, user); |
| 164 |
const char *passwd = pn_url_get_password(url); |
| 165 |
if (passwd) pn_connection_set_password(conn, passwd); |
| 166 |
} |
| 167 |
} else { |
| 168 |
// for backward compatibility, see if the connection's hostname can be |
| 169 |
// used for the remote address. See JIRA PROTON-1133 |
| 170 |
const char *hostname = pn_connection_get_hostname(conn); |
| 171 |
if (hostname) { |
| 172 |
str = pn_string(hostname); |
| 173 |
char *h = pn_string_buffer(str); |
| 174 |
// see if a port has been included in the hostname. This is not |
| 175 |
// allowed by the spec, but the old reactor interface allowed it. |
| 176 |
char *colon = strrchr(h, ':'); |
| 177 |
if (colon) { |
| 178 |
*colon = '\0'; |
| 179 |
port = colon + 1; |
| 180 |
} |
| 181 |
host = h; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
if (!host) { |
| 186 |
// error: no address configured |
| 187 |
pn_condition_t *cond = pn_transport_condition(transport); |
| 188 |
pn_condition_set_name(cond, "proton:io"); |
| 189 |
pn_condition_set_description(cond, "Connection failed: no address configured"); |
| 190 |
pn_transport_close_tail(transport); |
| 191 |
pn_transport_close_head(transport); |
| 192 |
} else { |
| 193 |
pn_socket_t sock = pn_connect(pni_reactor_io(reactor), host, port); |
| 194 |
// invalid sockets are ignored by poll, so we need to do this manually |
| 195 |
if (sock == PN_INVALID_SOCKET) { |
| 196 |
pn_condition_t *cond = pn_transport_condition(transport); |
| 197 |
pn_condition_set_name(cond, "proton:io"); |
| 198 |
pn_condition_set_description(cond, pn_error_text(pn_reactor_error(reactor))); |
| 199 |
pn_transport_close_tail(transport); |
| 200 |
pn_transport_close_head(transport); |
| 201 |
} else { |
| 202 |
pn_reactor_selectable_transport(reactor, sock, transport); |
| 203 |
} |
| 204 |
} |
| 205 |
pn_free(str); |
| 206 |
} |
| 207 |
|
| 208 |
void pni_handle_final(pn_reactor_t *reactor, pn_event_t *event) { |
| 209 |
assert(reactor); |
| 210 |
assert(event); |
| 211 |
pn_connection_t *conn = pn_event_connection(event); |
| 212 |
pn_list_remove(pn_reactor_children(reactor), conn); |
| 213 |
} |
| 214 |
|
| 215 |
static void pni_connection_readable(pn_selectable_t *sel) |
| 216 |
{ |
| 217 |
pn_reactor_t *reactor = (pn_reactor_t *) pni_selectable_get_context(sel); |
| 218 |
pn_transport_t *transport = pni_transport(sel); |
| 219 |
ssize_t capacity = pn_transport_capacity(transport); |
| 220 |
if (capacity > 0) { |
| 221 |
ssize_t n = pn_recv(pni_reactor_io(reactor), pn_selectable_get_fd(sel), |
| 222 |
pn_transport_tail(transport), capacity); |
| 223 |
if (n <= 0) { |
| 224 |
if (n == 0 || !pn_wouldblock(pni_reactor_io(reactor))) { |
| 225 |
if (n < 0) { |
| 226 |
pn_condition_t *cond = pn_transport_condition(transport); |
| 227 |
pn_condition_set_name(cond, "proton:io"); |
| 228 |
pn_condition_set_description(cond, pn_error_text(pn_reactor_error(reactor))); |
| 229 |
} |
| 230 |
pn_transport_close_tail(transport); |
| 231 |
} |
| 232 |
} else { |
| 233 |
pn_transport_process(transport, (size_t)n); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
ssize_t newcap = pn_transport_capacity(transport); |
| 238 |
//occasionally transport events aren't generated when expected, so |
| 239 |
//the following hack ensures we always update the selector |
| 240 |
if (1 || newcap != capacity) { |
| 241 |
pni_connection_update(sel); |
| 242 |
pn_reactor_update(reactor, sel); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
static void pni_connection_writable(pn_selectable_t *sel) |
| 247 |
{ |
| 248 |
pn_reactor_t *reactor = (pn_reactor_t *) pni_selectable_get_context(sel); |
| 249 |
pn_transport_t *transport = pni_transport(sel); |
| 250 |
ssize_t pending = pn_transport_pending(transport); |
| 251 |
if (pending > 0) { |
| 252 |
ssize_t n = pn_send(pni_reactor_io(reactor), pn_selectable_get_fd(sel), |
| 253 |
pn_transport_head(transport), pending); |
| 254 |
if (n < 0) { |
| 255 |
if (!pn_wouldblock(pni_reactor_io(reactor))) { |
| 256 |
pn_condition_t *cond = pn_transport_condition(transport); |
| 257 |
if (!pn_condition_is_set(cond)) { |
| 258 |
pn_condition_set_name(cond, "proton:io"); |
| 259 |
pn_condition_set_description(cond, pn_error_text(pn_reactor_error(reactor))); |
| 260 |
} |
| 261 |
pn_transport_close_head(transport); |
| 262 |
} |
| 263 |
} else { |
| 264 |
pn_transport_pop(transport, n); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
ssize_t newpending = pn_transport_pending(transport); |
| 269 |
if (newpending != pending) { |
| 270 |
pni_connection_update(sel); |
| 271 |
pn_reactor_update(reactor, sel); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
static void pni_connection_error(pn_selectable_t *sel) { |
| 276 |
pn_reactor_t *reactor = (pn_reactor_t *) pni_selectable_get_context(sel); |
| 277 |
pn_transport_t *transport = pni_transport(sel); |
| 278 |
pn_transport_close_head(transport); |
| 279 |
pn_transport_close_tail(transport); |
| 280 |
pn_selectable_terminate(sel); |
| 281 |
pn_reactor_update(reactor, sel); |
| 282 |
} |
| 283 |
|
| 284 |
static void pni_connection_expired(pn_selectable_t *sel) { |
| 285 |
pn_reactor_t *reactor = (pn_reactor_t *) pni_selectable_get_context(sel); |
| 286 |
pn_transport_t *transport = pni_transport(sel); |
| 287 |
pn_timestamp_t deadline = pn_transport_tick(transport, pn_reactor_now(reactor)); |
| 288 |
pn_selectable_set_deadline(sel, deadline); |
| 289 |
ssize_t c = pni_connection_capacity(sel); |
| 290 |
ssize_t p = pni_connection_pending(sel); |
| 291 |
pn_selectable_set_reading(sel, c > 0); |
| 292 |
pn_selectable_set_writing(sel, p > 0); |
| 293 |
pn_reactor_update(reactor, sel); |
| 294 |
} |
| 295 |
|
| 296 |
static void pni_connection_finalize(pn_selectable_t *sel) { |
| 297 |
pn_reactor_t *reactor = (pn_reactor_t *) pni_selectable_get_context(sel); |
| 298 |
pn_transport_t *transport = pni_transport(sel); |
| 299 |
pn_record_t *record = pn_transport_attachments(transport); |
| 300 |
pn_record_set(record, PN_TRANCTX, NULL); |
| 301 |
pn_socket_t fd = pn_selectable_get_fd(sel); |
| 302 |
pn_close(pni_reactor_io(reactor), fd); |
| 303 |
} |
| 304 |
|
| 305 |
pn_selectable_t *pn_reactor_selectable_transport(pn_reactor_t *reactor, pn_socket_t sock, pn_transport_t *transport) { |
| 306 |
pn_selectable_t *sel = pn_reactor_selectable(reactor); |
| 307 |
pn_selectable_set_fd(sel, sock); |
| 308 |
pn_selectable_on_readable(sel, pni_connection_readable); |
| 309 |
pn_selectable_on_writable(sel, pni_connection_writable); |
| 310 |
pn_selectable_on_error(sel, pni_connection_error); |
| 311 |
pn_selectable_on_expired(sel, pni_connection_expired); |
| 312 |
pn_selectable_on_finalize(sel, pni_connection_finalize); |
| 313 |
pn_record_t *record = pn_selectable_attachments(sel); |
| 314 |
pn_record_def(record, PN_TRANCTX, PN_OBJECT); |
| 315 |
pn_record_set(record, PN_TRANCTX, transport); |
| 316 |
pn_record_t *tr = pn_transport_attachments(transport); |
| 317 |
pn_record_def(tr, PN_TRANCTX, PN_WEAKREF); |
| 318 |
pn_record_set(tr, PN_TRANCTX, sel); |
| 319 |
pni_connection_update(sel); |
| 320 |
pn_reactor_update(reactor, sel); |
| 321 |
return sel; |
| 322 |
} |
| 323 |
|
| 324 |
pn_connection_t *pn_reactor_connection(pn_reactor_t *reactor, pn_handler_t *handler) { |
| 325 |
assert(reactor); |
| 326 |
pn_connection_t *connection = pn_connection(); |
| 327 |
pn_record_t *record = pn_connection_attachments(connection); |
| 328 |
pn_record_set_handler(record, handler); |
| 329 |
pn_connection_collect(connection, pn_reactor_collector(reactor)); |
| 330 |
pn_list_add(pn_reactor_children(reactor), connection); |
| 331 |
pni_record_init_reactor(record, reactor); |
| 332 |
pn_decref(connection); |
| 333 |
return connection; |
| 334 |
} |
| 335 |
|
| 336 |
pn_connection_t *pn_reactor_connection_to_host(pn_reactor_t *reactor, |
| 337 |
const char *host, |
| 338 |
const char *port, |
| 339 |
pn_handler_t *handler) { |
| 340 |
pn_connection_t *connection = pn_reactor_connection(reactor, handler); |
| 341 |
pn_reactor_set_connection_host(reactor, connection, host, port); |
| 342 |
return connection; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
void pn_reactor_set_connection_host(pn_reactor_t *reactor, |
| 347 |
pn_connection_t *connection, |
| 348 |
const char *host, |
| 349 |
const char *port) |
| 350 |
{ |
| 351 |
(void)reactor; // ignored |
| 352 |
if (pn_connection_acceptor(connection) != NULL) { |
| 353 |
// this is an inbound connection created by the acceptor. The peer |
| 354 |
// address cannot be modified. |
| 355 |
return; |
| 356 |
} |
| 357 |
pni_reactor_set_connection_peer_address(connection, host, port); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
const char *pn_reactor_get_connection_address(pn_reactor_t *reactor, |
| 362 |
pn_connection_t *connection) |
| 363 |
{ |
| 364 |
(void)reactor; // ignored |
| 365 |
if (!connection) return NULL; |
| 366 |
pn_record_t *record = pn_connection_attachments(connection); |
| 367 |
pn_url_t *url = (pn_url_t *)pn_record_get(record, PNI_CONN_PEER_ADDRESS); |
| 368 |
if (url) { |
| 369 |
return pn_url_str(url); |
| 370 |
} |
| 371 |
return NULL; |
| 372 |
} |
| 373 |
|
| 374 |
#undef PN_USE_DEPRECATED_API |