| 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 |
#include "engine-internal.h" |
| 23 |
#include "framing.h" |
| 24 |
#include "core/frame_generators.h" |
| 25 |
#include "core/frame_consumers.h" |
| 26 |
#include "memory.h" |
| 27 |
#include "platform/platform.h" |
| 28 |
#include "platform/platform_fmt.h" |
| 29 |
#include "sasl/sasl-internal.h" |
| 30 |
#include "ssl/ssl-internal.h" |
| 31 |
|
| 32 |
#include "autodetect.h" |
| 33 |
#include "protocol.h" |
| 34 |
#include "dispatch_actions.h" |
| 35 |
#include "config.h" |
| 36 |
#include "logger_private.h" |
| 37 |
|
| 38 |
#include "proton/event.h" |
| 39 |
|
| 40 |
#include <stddef.h> |
| 41 |
#include <string.h> |
| 42 |
#include <assert.h> |
| 43 |
#include <stdarg.h> |
| 44 |
#include <stdio.h> |
| 45 |
|
| 46 |
static ssize_t transport_consume(pn_transport_t *transport); |
| 47 |
|
| 48 |
// delivery buffers |
| 49 |
|
| 50 |
/* |
| 51 |
* Call this any time anything happens that may affect channel_max: |
| 52 |
* i.e. when the app indicates a preference, or when we receive the |
| 53 |
* OPEN frame from the remote peer. And call it to do the final |
| 54 |
* calculation just before we communicate our limit to the remote |
| 55 |
* peer by sending our OPEN frame. |
| 56 |
*/ |
| 57 |
static void pni_calculate_channel_max(pn_transport_t *transport) { |
| 58 |
/* |
| 59 |
* The application cannot make the limit larger than |
| 60 |
* what this library will allow. |
| 61 |
*/ |
| 62 |
transport->channel_max = (PN_IMPL_CHANNEL_MAX < transport->local_channel_max) |
| 63 |
? PN_IMPL_CHANNEL_MAX |
| 64 |
: transport->local_channel_max; |
| 65 |
|
| 66 |
/* |
| 67 |
* The remote peer's constraint is not valid until the |
| 68 |
* peer's open frame has been received. |
| 69 |
*/ |
| 70 |
if(transport->open_rcvd) { |
| 71 |
transport->channel_max = (transport->channel_max < transport->remote_channel_max) |
| 72 |
? transport->channel_max |
| 73 |
: transport->remote_channel_max; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
void pn_delivery_map_init(pn_delivery_map_t *db, pn_sequence_t next) |
| 78 |
{ |
| 79 |
db->deliveries = pn_hash(PN_WEAKREF, 0, 0.75); |
| 80 |
db->next = next; |
| 81 |
} |
| 82 |
|
| 83 |
void pn_delivery_map_free(pn_delivery_map_t *db) |
| 84 |
{ |
| 85 |
pn_free(db->deliveries); |
| 86 |
} |
| 87 |
|
| 88 |
static inline uintptr_t pni_sequence_make_hash ( pn_sequence_t i ) |
| 89 |
{ |
| 90 |
return i & 0x00000000FFFFFFFFUL; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
static pn_delivery_t *pni_delivery_map_get(pn_delivery_map_t *db, pn_sequence_t id) |
| 95 |
{ |
| 96 |
return (pn_delivery_t *) pn_hash_get(db->deliveries, pni_sequence_make_hash(id) ); |
| 97 |
} |
| 98 |
|
| 99 |
static void pn_delivery_state_init(pn_delivery_state_t *ds, pn_delivery_t *delivery, pn_sequence_t id) |
| 100 |
{ |
| 101 |
ds->id = id; |
| 102 |
ds->sending = false; |
| 103 |
ds->sent = false; |
| 104 |
ds->init = true; |
| 105 |
} |
| 106 |
|
| 107 |
static pn_delivery_state_t *pni_delivery_map_push(pn_delivery_map_t *db, pn_delivery_t *delivery) |
| 108 |
{ |
| 109 |
pn_delivery_state_t *ds = &delivery->state; |
| 110 |
pn_delivery_state_init(ds, delivery, db->next++); |
| 111 |
pn_hash_put(db->deliveries, pni_sequence_make_hash(ds->id), delivery); |
| 112 |
return ds; |
| 113 |
} |
| 114 |
|
| 115 |
void pn_delivery_map_del(pn_delivery_map_t *db, pn_delivery_t *delivery) |
| 116 |
{ |
| 117 |
if (delivery->state.init) { |
| 118 |
delivery->state.init = false; |
| 119 |
delivery->state.sending = false; |
| 120 |
delivery->state.sent = false; |
| 121 |
pn_hash_del(db->deliveries, pni_sequence_make_hash(delivery->state.id) ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
static void pni_delivery_map_clear(pn_delivery_map_t *dm) |
| 126 |
{ |
| 127 |
pn_hash_t *hash = dm->deliveries; |
| 128 |
for (pn_handle_t entry = pn_hash_head(hash); |
| 129 |
entry; |
| 130 |
entry = pn_hash_next(hash, entry)) |
| 131 |
{ |
| 132 |
pn_delivery_t *dlv = (pn_delivery_t *) pn_hash_value(hash, entry); |
| 133 |
pn_delivery_map_del(dm, dlv); |
| 134 |
} |
| 135 |
dm->next = 0; |
| 136 |
} |
| 137 |
|
| 138 |
static ssize_t pn_io_layer_input_passthru(pn_transport_t *, unsigned int, const char *, size_t ); |
| 139 |
static ssize_t pn_io_layer_output_passthru(pn_transport_t *, unsigned int, char *, size_t ); |
| 140 |
|
| 141 |
static ssize_t pn_io_layer_input_error(pn_transport_t *, unsigned int, const char *, size_t ); |
| 142 |
static ssize_t pn_io_layer_output_error(pn_transport_t *, unsigned int, char *, size_t ); |
| 143 |
|
| 144 |
static ssize_t pn_io_layer_input_setup(pn_transport_t *transport, unsigned int layer, const char *bytes, size_t available); |
| 145 |
static ssize_t pn_io_layer_output_setup(pn_transport_t *transport, unsigned int layer, char *bytes, size_t available); |
| 146 |
|
| 147 |
static ssize_t pn_input_read_amqp_header(pn_transport_t *transport, unsigned int layer, const char *bytes, size_t available); |
| 148 |
static ssize_t pn_input_read_amqp(pn_transport_t *transport, unsigned int layer, const char *bytes, size_t available); |
| 149 |
static ssize_t pn_output_write_amqp_header(pn_transport_t *transport, unsigned int layer, char *bytes, size_t available); |
| 150 |
static ssize_t pn_output_write_amqp(pn_transport_t *transport, unsigned int layer, char *bytes, size_t available); |
| 151 |
static void pn_error_amqp(pn_transport_t *transport, unsigned int layer); |
| 152 |
static int64_t pn_tick_amqp(pn_transport_t *transport, unsigned int layer, int64_t now); |
| 153 |
|
| 154 |
static ssize_t pn_io_layer_input_autodetect(pn_transport_t *transport, unsigned int layer, const char *bytes, size_t available); |
| 155 |
static ssize_t pn_io_layer_output_null(pn_transport_t *transport, unsigned int layer, char *bytes, size_t available); |
| 156 |
|
| 157 |
const pn_io_layer_t amqp_header_layer = { |
| 158 |
pn_input_read_amqp_header, |
| 159 |
pn_output_write_amqp_header, |
| 160 |
NULL, |
| 161 |
pn_tick_amqp, |
| 162 |
NULL |
| 163 |
}; |
| 164 |
|
| 165 |
const pn_io_layer_t amqp_write_header_layer = { |
| 166 |
pn_input_read_amqp, |
| 167 |
pn_output_write_amqp_header, |
| 168 |
NULL, |
| 169 |
pn_tick_amqp, |
| 170 |
NULL |
| 171 |
}; |
| 172 |
|
| 173 |
const pn_io_layer_t amqp_read_header_layer = { |
| 174 |
pn_input_read_amqp_header, |
| 175 |
pn_output_write_amqp, |
| 176 |
pn_error_amqp, |
| 177 |
pn_tick_amqp, |
| 178 |
NULL |
| 179 |
}; |
| 180 |
|
| 181 |
const pn_io_layer_t amqp_layer = { |
| 182 |
pn_input_read_amqp, |
| 183 |
pn_output_write_amqp, |
| 184 |
pn_error_amqp, |
| 185 |
pn_tick_amqp, |
| 186 |
NULL |
| 187 |
}; |
| 188 |
|
| 189 |
const pn_io_layer_t pni_setup_layer = { |
| 190 |
pn_io_layer_input_setup, |
| 191 |
pn_io_layer_output_setup, |
| 192 |
NULL, |
| 193 |
NULL, |
| 194 |
NULL |
| 195 |
}; |
| 196 |
|
| 197 |
const pn_io_layer_t pni_autodetect_layer = { |
| 198 |
pn_io_layer_input_autodetect, |
| 199 |
pn_io_layer_output_null, |
| 200 |
NULL, |
| 201 |
NULL, |
| 202 |
NULL |
| 203 |
}; |
| 204 |
|
| 205 |
const pn_io_layer_t pni_passthru_layer = { |
| 206 |
pn_io_layer_input_passthru, |
| 207 |
pn_io_layer_output_passthru, |
| 208 |
NULL, |
| 209 |
NULL, |
| 210 |
NULL |
| 211 |
}; |
| 212 |
|
| 213 |
const pn_io_layer_t pni_header_error_layer = { |
| 214 |
pn_io_layer_input_error, |
| 215 |
pn_output_write_amqp_header, |
| 216 |
NULL, |
| 217 |
NULL, |
| 218 |
NULL |
| 219 |
}; |
| 220 |
|
| 221 |
const pn_io_layer_t pni_error_layer = { |
| 222 |
pn_io_layer_input_error, |
| 223 |
pn_io_layer_output_error, |
| 224 |
pn_error_amqp, |
| 225 |
NULL, |
| 226 |
NULL |
| 227 |
}; |
| 228 |
|
| 229 |
/* Set up the transport protocol layers depending on what is configured */ |
| 230 |
static void pn_io_layer_setup(pn_transport_t *transport, unsigned int layer) |
| 231 |
{ |
| 232 |
assert(layer == 0); |
| 233 |
// Figure out if we are server or not |
| 234 |
if (transport->server) { |
| 235 |
transport->io_layers[layer++] = &pni_autodetect_layer; |
| 236 |
return; |
| 237 |
} |
| 238 |
if (transport->ssl) { |
| 239 |
transport->io_layers[layer++] = &ssl_layer; |
| 240 |
} |
| 241 |
if (transport->sasl) { |
| 242 |
transport->io_layers[layer++] = &sasl_header_layer; |
| 243 |
} |
| 244 |
transport->io_layers[layer++] = &amqp_header_layer; |
| 245 |
} |
| 246 |
|
| 247 |
ssize_t pn_io_layer_input_setup(pn_transport_t *transport, unsigned int layer, const char *bytes, size_t available) |
| 248 |
{ |
| 249 |
pn_io_layer_setup(transport, layer); |
| 250 |
return transport->io_layers[layer]->process_input(transport, layer, bytes, available); |
| 251 |
} |
| 252 |
|
| 253 |
ssize_t pn_io_layer_output_setup(pn_transport_t *transport, unsigned int layer, char *bytes, size_t available) |
| 254 |
{ |
| 255 |
pn_io_layer_setup(transport, layer); |
| 256 |
return transport->io_layers[layer]->process_output(transport, layer, bytes, available); |
| 257 |
} |
| 258 |
|
| 259 |
void pn_set_error_layer(pn_transport_t *transport) |
| 260 |
{ |
| 261 |
// Set every layer to the error layer in case we manually |
| 262 |
// pass through (happens from SASL to AMQP) |
| 263 |
for (int layer=0; layer<PN_IO_LAYER_CT; ++layer) { |
| 264 |
transport->io_layers[layer] = &pni_error_layer; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
// Autodetect the layer by reading the protocol header |
| 269 |
ssize_t pn_io_layer_input_autodetect(pn_transport_t *transport, unsigned int layer, const char *bytes, size_t available) |
| 270 |
{ |
| 271 |
const char* error; |
| 272 |
bool eos = transport->tail_closed; |
| 273 |
if (eos && available==0) { |
| 274 |
pn_do_error(transport, "amqp:connection:framing-error", "No protocol header found (connection aborted)"); |
| 275 |
pn_set_error_layer(transport); |
| 276 |
return PN_EOS; |
| 277 |
} |
| 278 |
pni_protocol_type_t protocol = pni_sniff_header(bytes, available); |
| 279 |
PN_LOG(&transport->logger, PN_SUBSYSTEM_IO, PN_LEVEL_DEBUG, "%s detected", pni_protocol_name(protocol)); |
| 280 |
switch (protocol) { |
| 281 |
case PNI_PROTOCOL_SSL: |
| 282 |
if (!(transport->allowed_layers & LAYER_SSL)) { |
| 283 |
error = "SSL protocol header not allowed (maybe detected twice)"; |
| 284 |
break; |
| 285 |
} |
| 286 |
transport->present_layers |= LAYER_SSL; |
| 287 |
transport->allowed_layers &= LAYER_AMQP1 | LAYER_AMQPSASL; |
| 288 |
if (!transport->ssl) { |
| 289 |
pn_ssl(transport); |
| 290 |
} |
| 291 |
transport->io_layers[layer] = &ssl_layer; |
| 292 |
transport->io_layers[layer+1] = &pni_autodetect_layer; |
| 293 |
return ssl_layer.process_input(transport, layer, bytes, available); |
| 294 |
case PNI_PROTOCOL_AMQP_SSL: |
| 295 |
if (!(transport->allowed_layers & LAYER_AMQPSSL)) { |
| 296 |
error = "AMQP SSL protocol header not allowed (maybe detected twice)"; |
| 297 |
break; |
| 298 |
} |
| 299 |
transport->present_layers |= LAYER_AMQPSSL; |
| 300 |
transport->allowed_layers &= LAYER_AMQP1 | LAYER_AMQPSASL; |
| 301 |
if (!transport->ssl) { |
| 302 |
pn_ssl(transport); |
| 303 |
} |
| 304 |
transport->io_layers[layer] = &ssl_layer; |
| 305 |
transport->io_layers[layer+1] = &pni_autodetect_layer; |
| 306 |
return 8; |
| 307 |
case PNI_PROTOCOL_AMQP_SASL: |
| 308 |
if (!(transport->allowed_layers & LAYER_AMQPSASL)) { |
| 309 |
error = "AMQP SASL protocol header not allowed (maybe detected twice)"; |
| 310 |
break; |
| 311 |
} |
| 312 |
transport->present_layers |= LAYER_AMQPSASL; |
| 313 |
transport->allowed_layers &= LAYER_AMQP1 | LAYER_AMQPSSL; |
| 314 |
if (!transport->sasl) { |
| 315 |
pn_sasl(transport); |
| 316 |
} |
| 317 |
transport->io_layers[layer] = &sasl_write_header_layer; |
| 318 |
transport->io_layers[layer+1] = &pni_autodetect_layer; |
| 319 |
PN_LOG(&transport->logger, PN_SUBSYSTEM_SASL, PN_LEVEL_FRAME, " <- %s", "SASL"); |
| 320 |
pni_sasl_set_external_security(transport, pn_ssl_get_ssf((pn_ssl_t*)transport), pn_ssl_get_remote_subject((pn_ssl_t*)transport)); |
| 321 |
return 8; |
| 322 |
case PNI_PROTOCOL_AMQP1: |
| 323 |
if (!(transport->allowed_layers & LAYER_AMQP1)) { |
| 324 |
error = "AMQP1.0 protocol header not allowed (maybe detected twice)"; |
| 325 |
break; |
| 326 |
} |
| 327 |
transport->present_layers |= LAYER_AMQP1; |
| 328 |
transport->allowed_layers = LAYER_NONE; |
| 329 |
if (transport->auth_required && !pn_transport_is_authenticated(transport)) { |
| 330 |
pn_do_error(transport, "amqp:connection:policy-error", |
| 331 |
"Client skipped authentication - forbidden"); |
| 332 |
pn_set_error_layer(transport); |
| 333 |
return 8; |
| 334 |
} |
| 335 |
if (transport->encryption_required && !pn_transport_is_encrypted(transport)) { |
| 336 |
pn_do_error(transport, "amqp:connection:policy-error", |
| 337 |
"Client connection unencrypted - forbidden"); |
| 338 |
pn_set_error_layer(transport); |
| 339 |
return 8; |
| 340 |
} |
| 341 |
transport->io_layers[layer] = &amqp_write_header_layer; |
| 342 |
PN_LOG(&transport->logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_FRAME, " <- %s", "AMQP"); |
| 343 |
return 8; |
| 344 |
case PNI_PROTOCOL_INSUFFICIENT: |
| 345 |
if (!eos) return 0; |
| 346 |
error = "End of input stream before protocol detection"; |
| 347 |
break; |
| 348 |
case PNI_PROTOCOL_AMQP_OTHER: |
| 349 |
error = "Incompatible AMQP connection detected"; |
| 350 |
break; |
| 351 |
case PNI_PROTOCOL_UNKNOWN: |
| 352 |
default: |
| 353 |
error = "Unknown protocol detected"; |
| 354 |
break; |
| 355 |
} |
| 356 |
transport->io_layers[layer] = &pni_header_error_layer; |
| 357 |
char quoted[1024]; |
| 358 |
pn_quote_data(quoted, 1024, bytes, available); |
| 359 |
pn_do_error(transport, "amqp:connection:framing-error", |
| 360 |
"%s: '%s'%s", error, quoted, |
| 361 |
!eos ? "" : " (connection aborted)"); |
| 362 |
return 0; |
| 363 |
} |
| 364 |
|
| 365 |
// We don't know what the output should be - do nothing |
| 366 |
ssize_t pn_io_layer_output_null(pn_transport_t *transport, unsigned int layer, char *bytes, size_t available) |
| 367 |
{ |
| 368 |
return 0; |
| 369 |
} |
| 370 |
|
| 371 |
/** Pass through input handler */ |
| 372 |
ssize_t pn_io_layer_input_passthru(pn_transport_t *transport, unsigned int layer, const char *data, size_t available) |
| 373 |
{ |
| 374 |
if (layer+1<PN_IO_LAYER_CT) |
| 375 |
return transport->io_layers[layer+1]->process_input(transport, layer+1, data, available); |
| 376 |
return PN_EOS; |
| 377 |
} |
| 378 |
|
| 379 |
/** Pass through output handler */ |
| 380 |
ssize_t pn_io_layer_output_passthru(pn_transport_t *transport, unsigned int layer, char *data, size_t available) |
| 381 |
{ |
| 382 |
if (layer+1<PN_IO_LAYER_CT) |
| 383 |
return transport->io_layers[layer+1]->process_output(transport, layer+1, data, available); |
| 384 |
return PN_EOS; |
| 385 |
} |
| 386 |
|
| 387 |
/** Input handler after detected error */ |
| 388 |
ssize_t pn_io_layer_input_error(pn_transport_t *transport, unsigned int layer, const char *data, size_t available) |
| 389 |
{ |
| 390 |
return PN_EOS; |
| 391 |
} |
| 392 |
|
| 393 |
/** Output handler after detected error */ |
| 394 |
ssize_t pn_io_layer_output_error(pn_transport_t *transport, unsigned int layer, char *data, size_t available) |
| 395 |
{ |
| 396 |
return PN_EOS; |
| 397 |
} |
| 398 |
|
| 399 |
static void pn_transport_initialize(void *object) |
| 400 |
{ |
| 401 |
pn_transport_t *transport = (pn_transport_t *)object; |
| 402 |
transport->freed = false; |
| 403 |
transport->output_buf = NULL; |
| 404 |
transport->output_size = PN_TRANSPORT_INITIAL_BUFFER_SIZE; |
| 405 |
transport->input_buf = NULL; |
| 406 |
transport->input_size = PN_TRANSPORT_INITIAL_BUFFER_SIZE; |
| 407 |
pni_logger_init(&transport->logger); |
| 408 |
transport->tracer = NULL; |
| 409 |
transport->sasl = NULL; |
| 410 |
transport->ssl = NULL; |
| 411 |
transport->frame = pn_buffer(PN_TRANSPORT_INITIAL_FRAME_SIZE); |
| 412 |
transport->input_frames_ct = 0; |
| 413 |
transport->output_frames_ct = 0; |
| 414 |
|
| 415 |
transport->connection = NULL; |
| 416 |
transport->context = pn_record(); |
| 417 |
|
| 418 |
for (int layer=0; layer<PN_IO_LAYER_CT; ++layer) { |
| 419 |
transport->io_layers[layer] = NULL; |
| 420 |
} |
| 421 |
|
| 422 |
transport->allowed_layers = LAYER_AMQP1 | LAYER_AMQPSASL | LAYER_AMQPSSL | LAYER_SSL; |
| 423 |
transport->present_layers = LAYER_NONE; |
| 424 |
|
| 425 |
// Defer setting up the layers until the first data arrives or is sent |
| 426 |
transport->io_layers[0] = &pni_setup_layer; |
| 427 |
|
| 428 |
transport->open_sent = false; |
| 429 |
transport->open_rcvd = false; |
| 430 |
transport->close_sent = false; |
| 431 |
transport->close_rcvd = false; |
| 432 |
transport->tail_closed = false; |
| 433 |
transport->head_closed = false; |
| 434 |
transport->remote_container = NULL; |
| 435 |
transport->remote_hostname = NULL; |
| 436 |
transport->local_max_frame = PN_DEFAULT_MAX_FRAME_SIZE; |
| 437 |
transport->remote_max_frame = OPEN_MAX_FRAME_SIZE_DEFAULT; |
| 438 |
|
| 439 |
/* |
| 440 |
* We set the local limit on channels to 2^15, because |
| 441 |
* parts of the code use the topmost bit (of a short) |
| 442 |
* as a flag. |
| 443 |
* The peer that this transport connects to may also |
| 444 |
* place its own limit on max channel number, and the |
| 445 |
* application may also set a limit. |
| 446 |
* The maximum that we use will be the minimum of all |
| 447 |
* these constraints. |
| 448 |
*/ |
| 449 |
// There is no constraint yet from remote peer, |
| 450 |
// so set to max possible. |
| 451 |
transport->remote_channel_max = OPEN_CHANNEL_MAX_DEFAULT; |
| 452 |
transport->local_channel_max = PN_IMPL_CHANNEL_MAX; |
| 453 |
transport->channel_max = transport->local_channel_max; |
| 454 |
|
| 455 |
transport->local_idle_timeout = 0; |
| 456 |
transport->dead_remote_deadline = 0; |
| 457 |
transport->last_bytes_input = 0; |
| 458 |
transport->remote_idle_timeout = 0; |
| 459 |
transport->keepalive_deadline = 0; |
| 460 |
transport->last_bytes_output = 0; |
| 461 |
transport->remote_offered_capabilities = pn_data(0); |
| 462 |
transport->remote_desired_capabilities = pn_data(0); |
| 463 |
transport->remote_properties = pn_data(0); |
| 464 |
transport->disp_data = pn_data(0); |
| 465 |
pn_condition_init(&transport->remote_condition); |
| 466 |
pn_condition_init(&transport->condition); |
| 467 |
transport->error = pn_error(); |
| 468 |
|
| 469 |
transport->local_channels = pn_hash(PN_WEAKREF, 0, 0.75); |
| 470 |
transport->remote_channels = pn_hash(PN_WEAKREF, 0, 0.75); |
| 471 |
|
| 472 |
transport->bytes_input = 0; |
| 473 |
transport->bytes_output = 0; |
| 474 |
|
| 475 |
transport->input_pending = 0; |
| 476 |
transport->output_pending = 0; |
| 477 |
|
| 478 |
transport->done_processing = false; |
| 479 |
|
| 480 |
transport->posted_idle_timeout = false; |
| 481 |
|
| 482 |
transport->server = false; |
| 483 |
transport->halt = false; |
| 484 |
transport->auth_required = false; |
| 485 |
transport->authenticated = false; |
| 486 |
transport->encryption_required = false; |
| 487 |
|
| 488 |
transport->referenced = true; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
static pn_session_t *pni_channel_state(pn_transport_t *transport, uint16_t channel) |
| 493 |
{ |
| 494 |
return (pn_session_t *) pn_hash_get(transport->remote_channels, channel); |
| 495 |
} |
| 496 |
|
| 497 |
static void pni_map_remote_channel(pn_session_t *session, uint16_t channel) |
| 498 |
{ |
| 499 |
pn_transport_t *transport = session->connection->transport; |
| 500 |
pn_hash_put(transport->remote_channels, channel, session); |
| 501 |
session->state.remote_channel = channel; |
| 502 |
pn_ep_incref(&session->endpoint); |
| 503 |
} |
| 504 |
|
| 505 |
void pni_transport_unbind_handles(pn_hash_t *handles, bool reset_state); |
| 506 |
|
| 507 |
static void pni_unmap_remote_channel(pn_session_t *ssn) |
| 508 |
{ |
| 509 |
// XXX: should really update link state also |
| 510 |
pni_delivery_map_clear(&ssn->state.incoming); |
| 511 |
pni_transport_unbind_handles(ssn->state.remote_handles, false); |
| 512 |
pn_transport_t *transport = ssn->connection->transport; |
| 513 |
uint16_t channel = ssn->state.remote_channel; |
| 514 |
ssn->state.remote_channel = -2; |
| 515 |
if (pn_hash_get(transport->remote_channels, channel)) { |
| 516 |
pn_ep_decref(&ssn->endpoint); |
| 517 |
} |
| 518 |
// note: may free the session: |
| 519 |
pn_hash_del(transport->remote_channels, channel); |
| 520 |
} |
| 521 |
|
| 522 |
static void pn_transport_incref(void *object) |
| 523 |
{ |
| 524 |
pn_transport_t *transport = (pn_transport_t *) object; |
| 525 |
if (!transport->referenced) { |
| 526 |
transport->referenced = true; |
| 527 |
if (transport->connection) { |
| 528 |
pn_incref(transport->connection); |
| 529 |
} else { |
| 530 |
pn_object_incref(object); |
| 531 |
} |
| 532 |
} else { |
| 533 |
pn_object_incref(object); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
static void pn_transport_finalize(void *object); |
| 538 |
#define pn_transport_new pn_object_new |
| 539 |
#define pn_transport_refcount pn_object_refcount |
| 540 |
#define pn_transport_decref pn_object_decref |
| 541 |
#define pn_transport_reify pn_object_reify |
| 542 |
#define pn_transport_hashcode NULL |
| 543 |
#define pn_transport_compare NULL |
| 544 |
#define pn_transport_inspect NULL |
| 545 |
|
| 546 |
pn_transport_t *pn_transport(void) |
| 547 |
{ |
| 548 |
#define pn_transport_free pn_object_free |
| 549 |
static const pn_class_t clazz = PN_METACLASS(pn_transport); |
| 550 |
#undef pn_transport_free |
| 551 |
pn_transport_t *transport = |
| 552 |
(pn_transport_t *) pn_class_new(&clazz, sizeof(pn_transport_t)); |
| 553 |
if (!transport) return NULL; |
| 554 |
|
| 555 |
transport->output_buf = (char *) pni_mem_suballocate(&clazz, transport, transport->output_size); |
| 556 |
if (!transport->output_buf) { |
| 557 |
pn_transport_free(transport); |
| 558 |
return NULL; |
| 559 |
} |
| 560 |
|
| 561 |
transport->input_buf = (char *) pni_mem_suballocate(&clazz, transport, transport->input_size); |
| 562 |
if (!transport->input_buf) { |
| 563 |
pn_transport_free(transport); |
| 564 |
return NULL; |
| 565 |
} |
| 566 |
|
| 567 |
transport->output_buffer = pn_buffer(4*1024); |
| 568 |
if (!transport->output_buffer) { |
| 569 |
pn_transport_free(transport); |
| 570 |
return NULL; |
| 571 |
} |
| 572 |
|
| 573 |
return transport; |
| 574 |
} |
| 575 |
|
| 576 |
void pn_transport_set_server(pn_transport_t *transport) |
| 577 |
{ |
| 578 |
assert(transport); |
| 579 |
transport->server = true; |
| 580 |
} |
| 581 |
|
| 582 |
const char *pn_transport_get_user(pn_transport_t *transport) |
| 583 |
{ |
| 584 |
assert(transport); |
| 585 |
// Client - just return whatever we gave to sasl |
| 586 |
if (!transport->server) { |
| 587 |
if (transport->sasl) return pn_sasl_get_user((pn_sasl_t *)transport); |
| 588 |
return "anonymous"; |
| 589 |
} |
| 590 |
|
| 591 |
// Server |
| 592 |
// Not finished authentication yet |
| 593 |
if (!(transport->present_layers & LAYER_AMQP1)) return 0; |
| 594 |
// We have SASL so it takes precedence |
| 595 |
if (transport->present_layers & LAYER_AMQPSASL) return pn_sasl_get_user((pn_sasl_t *)transport); |
| 596 |
// No SASL but we may have a SSL remote_subject |
| 597 |
if (transport->present_layers & (LAYER_AMQPSSL | LAYER_SSL)) return pn_ssl_get_remote_subject((pn_ssl_t *)transport); |
| 598 |
// otherwise it's just an unauthenticated anonymous connection |
| 599 |
return "anonymous"; |
| 600 |
} |
| 601 |
|
| 602 |
void pn_transport_require_auth(pn_transport_t *transport, bool required) |
| 603 |
{ |
| 604 |
assert(transport); |
| 605 |
transport->auth_required = required; |
| 606 |
} |
| 607 |
|
| 608 |
bool pn_transport_is_authenticated(pn_transport_t *transport) |
| 609 |
{ |
| 610 |
return transport && transport->authenticated; |
| 611 |
} |
| 612 |
|
| 613 |
void pn_transport_require_encryption(pn_transport_t *transport, bool required) |
| 614 |
{ |
| 615 |
assert(transport); |
| 616 |
transport->encryption_required = required; |
| 617 |
} |
| 618 |
|
| 619 |
bool pn_transport_is_encrypted(pn_transport_t *transport) |
| 620 |
{ |
| 621 |
return transport && transport->ssl && pn_ssl_get_ssf((pn_ssl_t*)transport)>0; |
| 622 |
} |
| 623 |
|
| 624 |
void pn_transport_free(pn_transport_t *transport) |
| 625 |
{ |
| 626 |
if (!transport) return; |
| 627 |
assert(!transport->freed); |
| 628 |
transport->freed = true; |
| 629 |
pn_decref(transport); |
| 630 |
} |
| 631 |
|
| 632 |
static void pn_transport_finalize(void *object) |
| 633 |
{ |
| 634 |
pn_transport_t *transport = (pn_transport_t *) object; |
| 635 |
|
| 636 |
if (transport->referenced && transport->connection && pn_refcount(transport->connection) > 1) { |
| 637 |
pn_object_incref(transport); |
| 638 |
transport->referenced = false; |
| 639 |
pn_decref(transport->connection); |
| 640 |
return; |
| 641 |
} |
| 642 |
|
| 643 |
// once the application frees the transport, no further I/O |
| 644 |
// processing can be done to the connection: |
| 645 |
pn_transport_unbind(transport); |
| 646 |
// we may have posted events, so stay alive until they are processed |
| 647 |
if (pn_refcount(transport) > 0) return; |
| 648 |
|
| 649 |
pn_ssl_free(transport); |
| 650 |
pn_sasl_free(transport); |
| 651 |
pni_mem_deallocate(PN_CLASSCLASS(pn_strdup), transport->remote_container); |
| 652 |
pni_mem_deallocate(PN_CLASSCLASS(pn_strdup), transport->remote_hostname); |
| 653 |
pn_free(transport->remote_offered_capabilities); |
| 654 |
pn_free(transport->remote_desired_capabilities); |
| 655 |
pn_free(transport->remote_properties); |
| 656 |
pn_free(transport->disp_data); |
| 657 |
pn_condition_tini(&transport->remote_condition); |
| 658 |
pn_condition_tini(&transport->condition); |
| 659 |
pn_error_free(transport->error); |
| 660 |
pn_free(transport->local_channels); |
| 661 |
pn_free(transport->remote_channels); |
| 662 |
pni_mem_subdeallocate(pn_class(transport), transport, transport->input_buf); |
| 663 |
pni_mem_subdeallocate(pn_class(transport), transport, transport->output_buf); |
| 664 |
pn_buffer_free(transport->frame); |
| 665 |
pn_free(transport->context); |
| 666 |
pn_buffer_free(transport->output_buffer); |
| 667 |
pni_logger_fini(&transport->logger); |
| 668 |
} |
| 669 |
|
| 670 |
static void pni_post_remote_open_events(pn_transport_t *transport, pn_connection_t *connection) { |
| 671 |
pn_collector_put(connection->collector, PN_OBJECT, connection, PN_CONNECTION_REMOTE_OPEN); |
| 672 |
if (transport->remote_idle_timeout) { |
| 673 |
pn_collector_put(connection->collector, PN_OBJECT, transport, PN_TRANSPORT); |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
int pn_transport_bind(pn_transport_t *transport, pn_connection_t *connection) |
| 678 |
{ |
| 679 |
assert(transport); |
| 680 |
assert(connection); |
| 681 |
|
| 682 |
if (transport->connection) return PN_STATE_ERR; |
| 683 |
if (connection->transport) return PN_STATE_ERR; |
| 684 |
|
| 685 |
transport->connection = connection; |
| 686 |
connection->transport = transport; |
| 687 |
|
| 688 |
pn_incref(connection); |
| 689 |
|
| 690 |
pn_connection_bound(connection); |
| 691 |
|
| 692 |
// set the hostname/user/password |
| 693 |
if (pn_string_size(connection->auth_user) || pn_string_size(connection->authzid)) { |
| 694 |
pn_sasl(transport); |
| 695 |
pni_sasl_set_user_password(transport, |
| 696 |
pn_string_get(connection->auth_user), |
| 697 |
pn_string_get(connection->authzid), |
| 698 |
pn_string_get(connection->auth_password)); |
| 699 |
} |
| 700 |
|
| 701 |
if (pn_string_size(connection->hostname)) { |
| 702 |
if (transport->sasl) { |
| 703 |
pni_sasl_set_remote_hostname(transport, pn_string_get(connection->hostname)); |
| 704 |
} |
| 705 |
|
| 706 |
// be sure not to overwrite a hostname already set by the user via |
| 707 |
// pn_ssl_set_peer_hostname() called before the bind |
| 708 |
if (transport->ssl) { |
| 709 |
size_t name_len = 0; |
| 710 |
pn_ssl_get_peer_hostname((pn_ssl_t*) transport, NULL, &name_len); |
| 711 |
if (name_len == 0) { |
| 712 |
pn_ssl_set_peer_hostname((pn_ssl_t*) transport, pn_string_get(connection->hostname)); |
| 713 |
} |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
if (transport->open_rcvd) { |
| 718 |
PN_SET_REMOTE(connection->endpoint.state, PN_REMOTE_ACTIVE); |
| 719 |
pni_post_remote_open_events(transport, connection); |
| 720 |
transport->halt = false; |
| 721 |
transport_consume(transport); // blech - testBindAfterOpen |
| 722 |
} |
| 723 |
|
| 724 |
return 0; |
| 725 |
} |
| 726 |
|
| 727 |
void pni_transport_unbind_handles(pn_hash_t *handles, bool reset_state) |
| 728 |
{ |
| 729 |
for (pn_handle_t h = pn_hash_head(handles); h; h = pn_hash_next(handles, h)) { |
| 730 |
uintptr_t key = pn_hash_key(handles, h); |
| 731 |
pn_link_t *link = (pn_link_t *) pn_hash_value(handles, h); |
| 732 |
if (reset_state) { |
| 733 |
pn_link_unbound(link); |
| 734 |
} |
| 735 |
pn_ep_decref(&link->endpoint); |
| 736 |
pn_hash_del(handles, key); |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
void pni_transport_unbind_channels(pn_hash_t *channels) |
| 741 |
{ |
| 742 |
for (pn_handle_t h = pn_hash_head(channels); h; h = pn_hash_next(channels, h)) { |
| 743 |
uintptr_t key = pn_hash_key(channels, h); |
| 744 |
pn_session_t *ssn = (pn_session_t *) pn_hash_value(channels, h); |
| 745 |
pni_delivery_map_clear(&ssn->state.incoming); |
| 746 |
pni_delivery_map_clear(&ssn->state.outgoing); |
| 747 |
pni_transport_unbind_handles(ssn->state.local_handles, true); |
| 748 |
pni_transport_unbind_handles(ssn->state.remote_handles, true); |
| 749 |
pn_session_unbound(ssn); |
| 750 |
pn_ep_decref(&ssn->endpoint); |
| 751 |
pn_hash_del(channels, key); |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
int pn_transport_unbind(pn_transport_t *transport) |
| 756 |
{ |
| 757 |
assert(transport); |
| 758 |
if (!transport->connection) return 0; |
| 759 |
|
| 760 |
|
| 761 |
pn_connection_t *conn = transport->connection; |
| 762 |
transport->connection = NULL; |
| 763 |
bool was_referenced = transport->referenced; |
| 764 |
|
| 765 |
pn_collector_put(conn->collector, PN_OBJECT, conn, PN_CONNECTION_UNBOUND); |
| 766 |
|
| 767 |
// XXX: what happens if the endpoints are freed before we get here? |
| 768 |
pn_session_t *ssn = pn_session_head(conn, 0); |
| 769 |
while (ssn) { |
| 770 |
pni_delivery_map_clear(&ssn->state.incoming); |
| 771 |
pni_delivery_map_clear(&ssn->state.outgoing); |
| 772 |
ssn = pn_session_next(ssn, 0); |
| 773 |
} |
| 774 |
|
| 775 |
pn_endpoint_t *endpoint = conn->endpoint_head; |
| 776 |
while (endpoint) { |
| 777 |
pn_condition_clear(&endpoint->remote_condition); |
| 778 |
pn_modified(conn, endpoint, true); |
| 779 |
endpoint = endpoint->endpoint_next; |
| 780 |
} |
| 781 |
|
| 782 |
pni_transport_unbind_channels(transport->local_channels); |
| 783 |
pni_transport_unbind_channels(transport->remote_channels); |
| 784 |
|
| 785 |
pn_connection_unbound(conn); |
| 786 |
if (was_referenced) { |
| 787 |
pn_decref(conn); |
| 788 |
} |
| 789 |
return 0; |
| 790 |
} |
| 791 |
|
| 792 |
pn_error_t *pn_transport_error(pn_transport_t *transport) |
| 793 |
{ |
| 794 |
assert(transport); |
| 795 |
if (pn_condition_is_set(&transport->condition)) { |
| 796 |
pn_error_format(transport->error, PN_ERR, "%s: %s", |
| 797 |
pn_condition_get_name(&transport->condition), |
| 798 |
pn_condition_get_description(&transport->condition)); |
| 799 |
} else { |
| 800 |
pn_error_clear(transport->error); |
| 801 |
} |
| 802 |
return transport->error; |
| 803 |
} |
| 804 |
|
| 805 |
pn_condition_t *pn_transport_condition(pn_transport_t *transport) |
| 806 |
{ |
| 807 |
assert(transport); |
| 808 |
return &transport->condition; |
| 809 |
} |
| 810 |
|
| 811 |
pn_logger_t *pn_transport_logger(pn_transport_t *transport) |
| 812 |
{ |
| 813 |
assert(transport); |
| 814 |
return &transport->logger; |
| 815 |
} |
| 816 |
|
| 817 |
static void pni_map_remote_handle(pn_link_t *link, uint32_t handle) |
| 818 |
{ |
| 819 |
link->state.remote_handle = handle; |
| 820 |
pn_hash_put(link->session->state.remote_handles, handle, link); |
| 821 |
pn_ep_incref(&link->endpoint); |
| 822 |
} |
| 823 |
|
| 824 |
static void pni_unmap_remote_handle(pn_link_t *link) |
| 825 |
{ |
| 826 |
uintptr_t handle = link->state.remote_handle; |
| 827 |
link->state.remote_handle = -2; |
| 828 |
if (pn_hash_get(link->session->state.remote_handles, handle)) { |
| 829 |
pn_ep_decref(&link->endpoint); |
| 830 |
} |
| 831 |
// may delete link: |
| 832 |
pn_hash_del(link->session->state.remote_handles, handle); |
| 833 |
} |
| 834 |
|
| 835 |
static pn_link_t *pni_handle_state(pn_session_t *ssn, uint32_t handle) |
| 836 |
{ |
| 837 |
return (pn_link_t *) pn_hash_get(ssn->state.remote_handles, handle); |
| 838 |
} |
| 839 |
|
| 840 |
bool pni_disposition_batchable(pn_disposition_t *disposition) |
| 841 |
{ |
| 842 |
switch (disposition->type) { |
| 843 |
case PN_ACCEPTED: |
| 844 |
return true; |
| 845 |
case PN_RELEASED: |
| 846 |
return true; |
| 847 |
default: |
| 848 |
return false; |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
static int pni_disposition_encode(pn_disposition_t *disposition, pn_data_t *data) |
| 853 |
{ |
| 854 |
pn_condition_t *cond = &disposition->condition; |
| 855 |
switch (disposition->type) { |
| 856 |
case PN_RECEIVED: |
| 857 |
PN_RETURN_IF_ERROR(pn_data_put_list(data)); |
| 858 |
pn_data_enter(data); |
| 859 |
PN_RETURN_IF_ERROR(pn_data_put_uint(data, disposition->section_number)); |
| 860 |
PN_RETURN_IF_ERROR(pn_data_put_ulong(data, disposition->section_offset)); |
| 861 |
pn_data_exit(data); |
| 862 |
return 0; |
| 863 |
case PN_ACCEPTED: |
| 864 |
case PN_RELEASED: |
| 865 |
return 0; |
| 866 |
case PN_REJECTED: |
| 867 |
return pn_data_fill(data, "[?DL[sSC]]", pn_condition_is_set(cond), ERROR, |
| 868 |
pn_condition_get_name(cond), |
| 869 |
pn_condition_get_description(cond), |
| 870 |
pn_condition_info(cond)); |
| 871 |
case PN_MODIFIED: |
| 872 |
return pn_data_fill(data, "[ooC]", |
| 873 |
disposition->failed, |
| 874 |
disposition->undeliverable, |
| 875 |
disposition->annotations); |
| 876 |
default: |
| 877 |
return pn_data_copy(data, disposition->data); |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
static int pni_post_amqp_transfer_frame(pn_transport_t *transport, uint16_t ch, |
| 882 |
uint32_t handle, |
| 883 |
pn_sequence_t id, |
| 884 |
pn_bytes_t *full_payload, |
| 885 |
const pn_bytes_t *tag, |
| 886 |
uint32_t message_format, |
| 887 |
bool settled, |
| 888 |
bool more, |
| 889 |
pn_sequence_t frame_limit, |
| 890 |
uint64_t code, |
| 891 |
pn_data_t* state, |
| 892 |
bool resume, |
| 893 |
bool aborted, |
| 894 |
bool batchable) |
| 895 |
{ |
| 896 |
bool more_flag = more; |
| 897 |
unsigned framecount = 0; |
| 898 |
|
| 899 |
// create performative, assuming 'more' flag need not change |
| 900 |
compute_performatives:; |
| 901 |
/* "DL[IIzI?o?on?DLC?o?o?o]" */ |
| 902 |
pn_bytes_t performative = |
| 903 |
pn_amqp_encode_DLEIIzIQoQonQDLCQoQoQoe(transport->frame, TRANSFER, |
| 904 |
handle, |
| 905 |
id, |
| 906 |
tag->size, tag->start, |
| 907 |
message_format, |
| 908 |
settled, settled, |
| 909 |
more_flag, more_flag, |
| 910 |
(bool)code, code, state, |
| 911 |
resume, resume, |
| 912 |
aborted, aborted, |
| 913 |
batchable, batchable); |
| 914 |
if (!performative.start) { |
| 915 |
return PN_ERR; |
| 916 |
} |
| 917 |
|
| 918 |
// At this point the side affect of the fill is to encode the performative into transport->frame |
| 919 |
|
| 920 |
do { // send as many frames as possible without changing the 'more' flag... |
| 921 |
|
| 922 |
// check if we need to break up the outbound frame |
| 923 |
size_t available = full_payload->size; |
| 924 |
if (transport->remote_max_frame) { |
| 925 |
if ((available + performative.size) > transport->remote_max_frame - AMQP_HEADER_SIZE) { |
| 926 |
available = transport->remote_max_frame - AMQP_HEADER_SIZE - performative.size; |
| 927 |
if (more_flag == false) { |
| 928 |
more_flag = true; |
| 929 |
goto compute_performatives; // deal with flag change |
| 930 |
} |
| 931 |
} else if (more_flag == true && more == false) { |
| 932 |
// caller has no more, and this is the last frame |
| 933 |
more_flag = false; |
| 934 |
goto compute_performatives; |
| 935 |
} |
| 936 |
} |
| 937 |
pn_bytes_t payload = {.size = available, .start = full_payload->start}; |
| 938 |
pn_framing_send_amqp_with_payload(transport, ch, performative, payload); |
| 939 |
|
| 940 |
full_payload->start += available; |
| 941 |
full_payload->size -= available; |
| 942 |
framecount++; |
| 943 |
} while (full_payload->size > 0 && framecount < frame_limit); |
| 944 |
|
| 945 |
return framecount; |
| 946 |
} |
| 947 |
|
| 948 |
static int pni_post_close(pn_transport_t *transport, pn_condition_t *cond) |
| 949 |
{ |
| 950 |
if (!cond && transport->connection) { |
| 951 |
cond = pn_connection_condition(transport->connection); |
| 952 |
} |
| 953 |
const char *condition = NULL; |
| 954 |
const char *description = NULL; |
| 955 |
pn_data_t *info = NULL; |
| 956 |
if (pn_condition_is_set(cond)) { |
| 957 |
condition = pn_condition_get_name(cond); |
| 958 |
description = pn_condition_get_description(cond); |
| 959 |
info = pn_condition_info(cond); |
| 960 |
} |
| 961 |
/* "DL[?DL[sSC]]" */ |
| 962 |
pn_bytes_t buf = pn_amqp_encode_DLEQDLEsSCee(transport->frame, CLOSE, |
| 963 |
(bool) condition, ERROR, condition, description, info); |
| 964 |
return pn_framing_send_amqp(transport, 0, buf); |
| 965 |
} |
| 966 |
|
| 967 |
static pn_collector_t *pni_transport_collector(pn_transport_t *transport) |
| 968 |
{ |
| 969 |
if (transport->connection && transport->connection->collector) { |
| 970 |
return transport->connection->collector; |
| 971 |
} else { |
| 972 |
return NULL; |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
static void pni_maybe_post_closed(pn_transport_t *transport) |
| 977 |
{ |
| 978 |
pn_collector_t *collector = pni_transport_collector(transport); |
| 979 |
if (transport->head_closed && transport->tail_closed) { |
| 980 |
pn_collector_put(collector, PN_OBJECT, transport, PN_TRANSPORT_CLOSED); |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
static void pni_close_tail(pn_transport_t *transport) |
| 985 |
{ |
| 986 |
if (!transport->tail_closed) { |
| 987 |
transport->tail_closed = true; |
| 988 |
pn_collector_t *collector = pni_transport_collector(transport); |
| 989 |
pn_collector_put(collector, PN_OBJECT, transport, PN_TRANSPORT_TAIL_CLOSED); |
| 990 |
pni_maybe_post_closed(transport); |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
int pn_do_error(pn_transport_t *transport, const char *condition, const char *fmt, ...) |
| 995 |
{ |
| 996 |
va_list ap; |
| 997 |
va_start(ap, fmt); |
| 998 |
char buf[1024]; |
| 999 |
if (fmt) { |
| 1000 |
// XXX: result |
| 1001 |
vsnprintf(buf, 1024, fmt, ap); |
| 1002 |
} else { |
| 1003 |
buf[0] = '\0'; |
| 1004 |
} |
| 1005 |
va_end(ap); |
| 1006 |
pn_condition_t *cond = &transport->condition; |
| 1007 |
if (!pn_condition_is_set(cond)) { |
| 1008 |
pn_condition_set_name(cond, condition); |
| 1009 |
if (fmt) { |
| 1010 |
pn_condition_set_description(cond, buf); |
| 1011 |
} |
| 1012 |
} else { |
| 1013 |
const char *first = pn_condition_get_description(cond); |
| 1014 |
if (first && fmt) { |
| 1015 |
char extended[2048]; |
| 1016 |
snprintf(extended, 2048, "%s (%s)", first, buf); |
| 1017 |
pn_condition_set_description(cond, extended); |
| 1018 |
} else if (fmt) { |
| 1019 |
pn_condition_set_description(cond, buf); |
| 1020 |
} |
| 1021 |
} |
| 1022 |
pn_collector_t *collector = pni_transport_collector(transport); |
| 1023 |
pn_collector_put(collector, PN_OBJECT, transport, PN_TRANSPORT_ERROR); |
| 1024 |
// Special case being called with no condition and no fmt to log the existing error condition |
| 1025 |
if (fmt && condition) { |
| 1026 |
PN_LOG(&transport->logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_ERROR, "%s %s", condition, buf); |
| 1027 |
} else { |
| 1028 |
PN_LOG(&transport->logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_ERROR, "%s %s", |
| 1029 |
pn_condition_get_name(cond), pn_condition_get_description(cond)); |
| 1030 |
} |
| 1031 |
|
| 1032 |
for (int i = 0; i<PN_IO_LAYER_CT; ++i) { |
| 1033 |
if (transport->io_layers[i] && transport->io_layers[i]->handle_error) |
| 1034 |
transport->io_layers[i]->handle_error(transport, i); |
| 1035 |
} |
| 1036 |
|
| 1037 |
pni_close_tail(transport); |
| 1038 |
return PN_ERR; |
| 1039 |
} |
| 1040 |
|
| 1041 |
static char *pn_bytes_strdup(pn_bytes_t str) |
| 1042 |
{ |
| 1043 |
return pn_strndup(str.start, str.size); |
| 1044 |
} |
| 1045 |
|
| 1046 |
int pn_do_open(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload) |
| 1047 |
{ |
| 1048 |
bool container_q, hostname_q, remote_channel_max_q, remote_max_frame_q; |
| 1049 |
uint16_t remote_channel_max; |
| 1050 |
uint32_t remote_max_frame; |
| 1051 |
pn_bytes_t remote_container, remote_hostname; |
| 1052 |
pn_data_clear(transport->remote_offered_capabilities); |
| 1053 |
pn_data_clear(transport->remote_desired_capabilities); |
| 1054 |
pn_data_clear(transport->remote_properties); |
| 1055 |
|
| 1056 |
pn_amqp_decode_DqEQSQSQIQHIqqCCCe(payload, |
| 1057 |
&container_q, &remote_container, |
| 1058 |
&hostname_q, &remote_hostname, |
| 1059 |
&remote_max_frame_q, &remote_max_frame, |
| 1060 |
&remote_channel_max_q, &remote_channel_max, |
| 1061 |
&transport->remote_idle_timeout, |
| 1062 |
transport->remote_offered_capabilities, |
| 1063 |
transport->remote_desired_capabilities, |
| 1064 |
transport->remote_properties); |
| 1065 |
/* |
| 1066 |
* The default value is already stored in the variable. |
| 1067 |
* But the scanner zeroes out values if it does not |
| 1068 |
* find them in the args, so don't give the variable |
| 1069 |
* directly to the scanner. |
| 1070 |
*/ |
| 1071 |
transport->remote_channel_max = remote_channel_max_q ? remote_channel_max : OPEN_CHANNEL_MAX_DEFAULT; |
| 1072 |
transport->remote_max_frame = remote_max_frame_q ? remote_max_frame : OPEN_MAX_FRAME_SIZE_DEFAULT; |
| 1073 |
|
| 1074 |
if (transport->remote_max_frame > 0) { |
| 1075 |
if (transport->remote_max_frame < AMQP_MIN_MAX_FRAME_SIZE) { |
| 1076 |
pn_logger_logf(&transport->logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_WARNING, |
| 1077 |
"Peer advertised bad max-frame (%u), forcing to %u", |
| 1078 |
transport->remote_max_frame, AMQP_MIN_MAX_FRAME_SIZE); |
| 1079 |
transport->remote_max_frame = AMQP_MIN_MAX_FRAME_SIZE; |
| 1080 |
} |
| 1081 |
} |
| 1082 |
pni_mem_deallocate(PN_CLASSCLASS(pn_strdup), transport->remote_container); |
| 1083 |
transport->remote_container = container_q ? pn_bytes_strdup(remote_container) : NULL; |
| 1084 |
pni_mem_deallocate(PN_CLASSCLASS(pn_strdup), transport->remote_hostname); |
| 1085 |
transport->remote_hostname = hostname_q ? pn_bytes_strdup(remote_hostname) : NULL; |
| 1086 |
|
| 1087 |
pn_connection_t *conn = transport->connection; |
| 1088 |
if (conn) { |
| 1089 |
PN_SET_REMOTE(conn->endpoint.state, PN_REMOTE_ACTIVE); |
| 1090 |
pni_post_remote_open_events(transport, conn); |
| 1091 |
} else { |
| 1092 |
transport->halt = true; |
| 1093 |
} |
| 1094 |
transport->open_rcvd = true; |
| 1095 |
pni_calculate_channel_max(transport); |
| 1096 |
return 0; |
| 1097 |
} |
| 1098 |
|
| 1099 |
int pn_do_begin(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload) |
| 1100 |
{ |
| 1101 |
bool reply; |
| 1102 |
uint16_t remote_channel; |
| 1103 |
pn_sequence_t next; |
| 1104 |
|
| 1105 |
pn_amqp_decode_DqEQHIe(payload, &reply, &remote_channel, &next); |
| 1106 |
|
| 1107 |
// AMQP 1.0 section 2.7.1 - if the peer doesn't honor our channel_max -- |
| 1108 |
// express our displeasure by closing the connection with a framing error. |
| 1109 |
if (channel > transport->channel_max) { |
| 1110 |
pn_do_error(transport, |
| 1111 |
"amqp:connection:framing-error", |
| 1112 |
"remote channel %d is above negotiated channel_max %d.", |
| 1113 |
channel, |
| 1114 |
transport->channel_max |
| 1115 |
); |
| 1116 |
return PN_ARG_ERR; |
| 1117 |
} |
| 1118 |
|
| 1119 |
pn_session_t *ssn; |
| 1120 |
if (reply) { |
| 1121 |
ssn = (pn_session_t *) pn_hash_get(transport->local_channels, remote_channel); |
| 1122 |
if (ssn == 0) { |
| 1123 |
pn_do_error(transport, |
| 1124 |
"amqp:invalid-field", |
| 1125 |
"begin reply to unknown channel %d.", |
| 1126 |
remote_channel |
| 1127 |
); |
| 1128 |
return PN_ARG_ERR; |
| 1129 |
} |
| 1130 |
} else { |
| 1131 |
ssn = pn_session(transport->connection); |
| 1132 |
} |
| 1133 |
ssn->state.incoming_transfer_count = next; |
| 1134 |
pni_map_remote_channel(ssn, channel); |
| 1135 |
PN_SET_REMOTE(ssn->endpoint.state, PN_REMOTE_ACTIVE); |
| 1136 |
pn_collector_put(transport->connection->collector, PN_OBJECT, ssn, PN_SESSION_REMOTE_OPEN); |
| 1137 |
return 0; |
| 1138 |
} |
| 1139 |
|
| 1140 |
static pn_link_t *pni_find_link(pn_session_t *ssn, pn_bytes_t name, bool is_sender) |
| 1141 |
{ |
| 1142 |
pn_endpoint_type_t type = is_sender ? SENDER : RECEIVER; |
| 1143 |
|
| 1144 |
for (size_t i = 0; i < pn_list_size(ssn->links); i++) |
| 1145 |
{ |
| 1146 |
pn_link_t *link = (pn_link_t *) pn_list_get(ssn->links, i); |
| 1147 |
if (link->endpoint.type == type && |
| 1148 |
// This function is used to locate the link object for an |
| 1149 |
// incoming attach. If a link object of the same name is found |
| 1150 |
// which is remotely closed or detached, assume that is |
| 1151 |
// no longer in use and a new link is intended. |
| 1152 |
(!(link->endpoint.state & PN_REMOTE_CLOSED) && ((int32_t) link->state.remote_handle != -2)) && |
| 1153 |
pn_bytes_equal(name, pn_string_bytes(link->name))) |
| 1154 |
{ |
| 1155 |
return link; |
| 1156 |
} |
| 1157 |
} |
| 1158 |
return NULL; |
| 1159 |
} |
| 1160 |
|
| 1161 |
static void set_expiry_policy_from_symbol(pn_terminus_t* terminus, pn_bytes_t symbol) |
| 1162 |
{ |
| 1163 |
if (symbol.start) { |
| 1164 |
if (pn_bytes_equal(symbol, PN_BYTES_LITERAL(link-detach))) |
| 1165 |
pn_terminus_set_expiry_policy(terminus, PN_EXPIRE_WITH_LINK); |
| 1166 |
if (pn_bytes_equal(symbol, PN_BYTES_LITERAL(session-end))) |
| 1167 |
pn_terminus_set_expiry_policy(terminus, PN_EXPIRE_WITH_SESSION); |
| 1168 |
if (pn_bytes_equal(symbol, PN_BYTES_LITERAL(connection-close))) |
| 1169 |
pn_terminus_set_expiry_policy(terminus, PN_EXPIRE_WITH_CONNECTION); |
| 1170 |
if (pn_bytes_equal(symbol, PN_BYTES_LITERAL(never))) |
| 1171 |
pn_terminus_set_expiry_policy(terminus, PN_EXPIRE_NEVER); |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
static pn_distribution_mode_t symbol2dist_mode(const pn_bytes_t symbol) |
| 1176 |
{ |
| 1177 |
if (!symbol.start) |
| 1178 |
return PN_DIST_MODE_UNSPECIFIED; |
| 1179 |
|
| 1180 |
if (pn_bytes_equal(symbol, PN_BYTES_LITERAL(move))) |
| 1181 |
return PN_DIST_MODE_MOVE; |
| 1182 |
if (pn_bytes_equal(symbol, PN_BYTES_LITERAL(copy))) |
| 1183 |
return PN_DIST_MODE_COPY; |
| 1184 |
|
| 1185 |
return PN_DIST_MODE_UNSPECIFIED; |
| 1186 |
} |
| 1187 |
|
| 1188 |
static const char *dist_mode2symbol(const pn_distribution_mode_t mode) |
| 1189 |
{ |
| 1190 |
switch (mode) |
| 1191 |
{ |
| 1192 |
case PN_DIST_MODE_COPY: |
| 1193 |
return "copy"; |
| 1194 |
case PN_DIST_MODE_MOVE: |
| 1195 |
return "move"; |
| 1196 |
default: |
| 1197 |
return NULL; |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
int pn_terminus_set_address_bytes(pn_terminus_t *terminus, pn_bytes_t address) |
| 1202 |
{ |
| 1203 |
assert(terminus); |
| 1204 |
return pn_string_setn(terminus->address, address.start, address.size); |
| 1205 |
} |
| 1206 |
|
| 1207 |
int pn_do_attach(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload) |
| 1208 |
{ |
| 1209 |
pn_bytes_t name; |
| 1210 |
uint32_t handle; |
| 1211 |
bool is_sender; |
| 1212 |
pn_bytes_t source, target; |
| 1213 |
pn_durability_t src_dr, tgt_dr; |
| 1214 |
pn_bytes_t src_exp, tgt_exp; |
| 1215 |
pn_seconds_t src_timeout, tgt_timeout; |
| 1216 |
bool src_dynamic, tgt_dynamic; |
| 1217 |
pn_sequence_t idc; |
| 1218 |
pn_bytes_t dist_mode; |
| 1219 |
bool snd_settle, rcv_settle; |
| 1220 |
uint8_t snd_settle_mode, rcv_settle_mode; |
| 1221 |
uint64_t max_msgsz; |
| 1222 |
bool has_props; |
| 1223 |
pn_data_t *rem_props = pn_data(0); |
| 1224 |
pn_amqp_decode_DqESIoQBQBDqESIsIoqseDqESIsIoeqqILqqQCe(payload, |
| 1225 |
&name, &handle, |
| 1226 |
&is_sender, |
| 1227 |
&snd_settle, &snd_settle_mode, |
| 1228 |
&rcv_settle, &rcv_settle_mode, |
| 1229 |
&source, &src_dr, &src_exp, &src_timeout, &src_dynamic, &dist_mode, |
| 1230 |
&target, &tgt_dr, &tgt_exp, &tgt_timeout, &tgt_dynamic, |
| 1231 |
&idc, &max_msgsz, &has_props, rem_props); |
| 1232 |
char strbuf[128]; // avoid malloc for most link names |
| 1233 |
char *strheap = (name.size >= sizeof(strbuf)) ? (char *) malloc(name.size + 1) : NULL; |
| 1234 |
char *strname = strheap ? strheap : strbuf; |
| 1235 |
if (name.size > 0) strncpy(strname, name.start, name.size); |
| 1236 |
strname[name.size] = '\0'; |
| 1237 |
|
| 1238 |
pn_session_t *ssn = pni_channel_state(transport, channel); |
| 1239 |
if (!ssn) { |
| 1240 |
pn_do_error(transport, "amqp:not-allowed", "no such channel: %u", channel); |
| 1241 |
if (strheap) free(strheap); |
| 1242 |
pn_free(rem_props); |
| 1243 |
return PN_EOS; |
| 1244 |
} |
| 1245 |
pn_link_t *link = pni_find_link(ssn, name, is_sender); |
| 1246 |
if (link && (int32_t)link->state.remote_handle >= 0) { |
| 1247 |
pn_do_error(transport, "amqp:invalid-field", "link name already attached: %s", strname); |
| 1248 |
if (strheap) free(strheap); |
| 1249 |
pn_free(rem_props); |
| 1250 |
return PN_EOS; |
| 1251 |
} |
| 1252 |
if (!link) { /* Make a new link for the attach */ |
| 1253 |
if (is_sender) { |
| 1254 |
link = (pn_link_t *) pn_sender(ssn, strname); |
| 1255 |
} else { |
| 1256 |
link = (pn_link_t *) pn_receiver(ssn, strname); |
| 1257 |
} |
| 1258 |
} |
| 1259 |
|
| 1260 |
if (strheap) { |
| 1261 |
free(strheap); |
| 1262 |
} |
| 1263 |
|
| 1264 |
if (has_props) { |
| 1265 |
link->remote_properties = rem_props; |
| 1266 |
} else { |
| 1267 |
pn_free(rem_props); |
| 1268 |
} |
| 1269 |
|
| 1270 |
pni_map_remote_handle(link, handle); |
| 1271 |
PN_SET_REMOTE(link->endpoint.state, PN_REMOTE_ACTIVE); |
| 1272 |
pn_terminus_t *rsrc = &link->remote_source; |
| 1273 |
if (source.start || src_dynamic) { |
| 1274 |
pn_terminus_set_type(rsrc, PN_SOURCE); |
| 1275 |
pn_terminus_set_address_bytes(rsrc, source); |
| 1276 |
pn_terminus_set_durability(rsrc, src_dr); |
| 1277 |
set_expiry_policy_from_symbol(rsrc, src_exp); |
| 1278 |
pn_terminus_set_timeout(rsrc, src_timeout); |
| 1279 |
pn_terminus_set_dynamic(rsrc, src_dynamic); |
| 1280 |
pn_terminus_set_distribution_mode(rsrc, symbol2dist_mode(dist_mode)); |
| 1281 |
} else { |
| 1282 |
pn_terminus_set_type(rsrc, PN_UNSPECIFIED); |
| 1283 |
} |
| 1284 |
pn_terminus_t *rtgt = &link->remote_target; |
| 1285 |
if (target.start || tgt_dynamic) { |
| 1286 |
pn_terminus_set_type(rtgt, PN_TARGET); |
| 1287 |
pn_terminus_set_address_bytes(rtgt, target); |
| 1288 |
pn_terminus_set_durability(rtgt, tgt_dr); |
| 1289 |
set_expiry_policy_from_symbol(rtgt, tgt_exp); |
| 1290 |
pn_terminus_set_timeout(rtgt, tgt_timeout); |
| 1291 |
pn_terminus_set_dynamic(rtgt, tgt_dynamic); |
| 1292 |
} else { |
| 1293 |
uint64_t code = 0; |
| 1294 |
pn_amqp_decode_DqEqqqqqDqqDLqqqqe(payload, &code); |
| 1295 |
if (code == COORDINATOR) { |
| 1296 |
pn_terminus_set_type(rtgt, PN_COORDINATOR); |
| 1297 |
} else if (code == TARGET) { |
| 1298 |
pn_terminus_set_type(rtgt, PN_TARGET); |
| 1299 |
} else { |
| 1300 |
pn_terminus_set_type(rtgt, PN_UNSPECIFIED); |
| 1301 |
} |
| 1302 |
} |
| 1303 |
|
| 1304 |
if (snd_settle) |
| 1305 |
link->remote_snd_settle_mode = snd_settle_mode; |
| 1306 |
if (rcv_settle) |
| 1307 |
link->remote_rcv_settle_mode = rcv_settle_mode; |
| 1308 |
|
| 1309 |
pn_data_clear(link->remote_source.properties); |
| 1310 |
pn_data_clear(link->remote_source.filter); |
| 1311 |
pn_data_clear(link->remote_source.outcomes); |
| 1312 |
pn_data_clear(link->remote_source.capabilities); |
| 1313 |
|
| 1314 |
pn_amqp_decode_DqEqqqqqDqEqqqqqCqCqCCee(payload, |
| 1315 |
link->remote_source.properties, |
| 1316 |
link->remote_source.filter, |
| 1317 |
link->remote_source.outcomes, |
| 1318 |
link->remote_source.capabilities); |
| 1319 |
|
| 1320 |
pn_data_rewind(link->remote_source.properties); |
| 1321 |
pn_data_rewind(link->remote_source.filter); |
| 1322 |
pn_data_rewind(link->remote_source.outcomes); |
| 1323 |
pn_data_rewind(link->remote_source.capabilities); |
| 1324 |
|
| 1325 |
pn_data_clear(link->remote_target.properties); |
| 1326 |
pn_data_clear(link->remote_target.capabilities); |
| 1327 |
|
| 1328 |
if (pn_terminus_get_type(&link->remote_target) == PN_COORDINATOR) { |
| 1329 |
// coordinator target only has a capabilities field |
| 1330 |
pn_amqp_decode_DqEqqqqqDqqDqECeqqqe(payload, |
| 1331 |
link->remote_target.capabilities); |
| 1332 |
} else { |
| 1333 |
pn_amqp_decode_DqEqqqqqDqqDqEqqqqqCCee(payload, |
| 1334 |
link->remote_target.properties, |
| 1335 |
link->remote_target.capabilities); |
| 1336 |
} |
| 1337 |
|
| 1338 |
pn_data_rewind(link->remote_target.properties); |
| 1339 |
pn_data_rewind(link->remote_target.capabilities); |
| 1340 |
|
| 1341 |
if (!is_sender) { |
| 1342 |
link->state.delivery_count = idc; |
| 1343 |
} |
| 1344 |
|
| 1345 |
if (max_msgsz) { |
| 1346 |
link->remote_max_message_size = max_msgsz; |
| 1347 |
} |
| 1348 |
|
| 1349 |
pn_collector_put(transport->connection->collector, PN_OBJECT, link, PN_LINK_REMOTE_OPEN); |
| 1350 |
return 0; |
| 1351 |
} |
| 1352 |
|
| 1353 |
static int pni_post_flow(pn_transport_t *transport, pn_session_t *ssn, pn_link_t *link); |
| 1354 |
|
| 1355 |
// free the delivery |
| 1356 |
static void pn_full_settle(pn_delivery_map_t *db, pn_delivery_t *delivery) |
| 1357 |
{ |
| 1358 |
assert(!delivery->work); |
| 1359 |
pn_clear_tpwork(delivery); |
| 1360 |
pn_delivery_map_del(db, delivery); |
| 1361 |
pn_incref(delivery); |
| 1362 |
pn_decref(delivery); |
| 1363 |
} |
| 1364 |
|
| 1365 |
int pn_do_transfer(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload) |
| 1366 |
{ |
| 1367 |
// XXX: multi transfer |
| 1368 |
uint32_t handle; |
| 1369 |
pn_bytes_t tag; |
| 1370 |
bool id_present; |
| 1371 |
pn_sequence_t id; |
| 1372 |
bool settled; |
| 1373 |
bool more; |
| 1374 |
bool has_type, settled_set; |
| 1375 |
bool resume, aborted, batchable; |
| 1376 |
uint64_t type; |
| 1377 |
|
| 1378 |
pn_bytes_t disp_data; |
| 1379 |
size_t dsize = |
| 1380 |
pn_amqp_decode_DqEIQIzqQooqDQLRoooe(payload, &handle, &id_present, &id, &tag, |
| 1381 |
&settled_set, &settled, &more, &has_type, &type, &disp_data, |
| 1382 |
&resume, &aborted, &batchable); |
| 1383 |
payload.size -= dsize; |
| 1384 |
payload.start += dsize; |
| 1385 |
|
| 1386 |
pn_session_t *ssn = pni_channel_state(transport, channel); |
| 1387 |
if (!ssn) { |
| 1388 |
return pn_do_error(transport, "amqp:not-allowed", "no such channel: %u", channel); |
| 1389 |
} |
| 1390 |
|
| 1391 |
if (!ssn->state.incoming_window) { |
| 1392 |
return pn_do_error(transport, "amqp:session:window-violation", "incoming session window exceeded"); |
| 1393 |
} |
| 1394 |
|
| 1395 |
pn_link_t *link = pni_handle_state(ssn, handle); |
| 1396 |
if (!link) { |
| 1397 |
return pn_do_error(transport, "amqp:invalid-field", "no such handle: %u", handle); |
| 1398 |
} |
| 1399 |
pn_delivery_t *delivery = NULL; |
| 1400 |
bool new_delivery = false; |
| 1401 |
if (link->more_pending) { |
| 1402 |
// Ongoing multiframe delivery. |
| 1403 |
if (link->unsettled_tail && !link->unsettled_tail->done) { |
| 1404 |
delivery = link->unsettled_tail; |
| 1405 |
if (settled_set && !settled && delivery->remote.settled) |
| 1406 |
return pn_do_error(transport, "amqp:invalid-field", "invalid transition from settled to unsettled"); |
| 1407 |
if (id_present && id != delivery->state.id) |
| 1408 |
return pn_do_error(transport, "amqp:invalid-field", "invalid delivery-id for a continuation transfer"); |
| 1409 |
} else { |
| 1410 |
// Application has already settled. Delivery is no more. |
| 1411 |
// Ignore content and look for transition to a new delivery. |
| 1412 |
if (!id_present || id == link->more_id) { |
| 1413 |
// Still old delivery. |
| 1414 |
if (!more || aborted) |
| 1415 |
link->more_pending = false; |
| 1416 |
} else { |
| 1417 |
// New id. |
| 1418 |
new_delivery = true; |
| 1419 |
link->more_pending = false; |
| 1420 |
} |
| 1421 |
} |
| 1422 |
} else { |
| 1423 |
new_delivery = true; |
| 1424 |
} |
| 1425 |
|
| 1426 |
if (new_delivery) { |
| 1427 |
assert(!link->more_pending); |
| 1428 |
assert(delivery == NULL); |
| 1429 |
pn_delivery_map_t *incoming = &ssn->state.incoming; |
| 1430 |
|
| 1431 |
if (!ssn->state.incoming_init) { |
| 1432 |
incoming->next = id; |
| 1433 |
ssn->state.incoming_init = true; |
| 1434 |
ssn->incoming_deliveries++; |
| 1435 |
} |
| 1436 |
|
| 1437 |
delivery = pn_delivery(link, pn_dtag(tag.start, tag.size)); |
| 1438 |
pn_delivery_state_t *state = pni_delivery_map_push(incoming, delivery); |
| 1439 |
if (id_present && id != state->id) { |
| 1440 |
return pn_do_error(transport, "amqp:session:invalid-field", |
| 1441 |
"sequencing error, expected delivery-id %u, got %u", |
| 1442 |
state->id, id); |
| 1443 |
} |
| 1444 |
if (has_type) { |
| 1445 |
delivery->remote.type = type; |
| 1446 |
pn_data_clear(delivery->remote.data); |
| 1447 |
pn_data_decode(delivery->remote.data, disp_data.start, disp_data.size); |
| 1448 |
} |
| 1449 |
link->state.delivery_count++; |
| 1450 |
link->state.link_credit--; |
| 1451 |
link->queued++; |
| 1452 |
} |
| 1453 |
|
| 1454 |
if (delivery) { |
| 1455 |
pn_buffer_append(delivery->bytes, payload.start, payload.size); |
| 1456 |
if (more) { |
| 1457 |
if (!link->more_pending) { |
| 1458 |
// First frame of a multi-frame transfer. Remember at link level. |
| 1459 |
link->more_pending = true; |
| 1460 |
assert(id_present); // Id MUST be set on first frame, and already checked above. |
| 1461 |
link->more_id = id; |
| 1462 |
} |
| 1463 |
delivery->done = false; |
| 1464 |
} |
| 1465 |
else |
| 1466 |
delivery->done = true; |
| 1467 |
|
| 1468 |
// XXX: need to fill in remote state: delivery->remote.state = ...; |
| 1469 |
if (settled && !delivery->remote.settled) { |
| 1470 |
delivery->remote.settled = settled; |
| 1471 |
delivery->updated = true; |
| 1472 |
pn_work_update(transport->connection, delivery); |
| 1473 |
} |
| 1474 |
|
| 1475 |
if ((delivery->aborted = aborted)) { |
| 1476 |
delivery->remote.settled = true; |
| 1477 |
delivery->done = true; |
| 1478 |
delivery->updated = true; |
| 1479 |
link->more_pending = false; |
| 1480 |
pn_work_update(transport->connection, delivery); |
| 1481 |
} |
| 1482 |
pn_collector_put(transport->connection->collector, PN_OBJECT, delivery, PN_DELIVERY); |
| 1483 |
} |
| 1484 |
|
| 1485 |
ssn->incoming_bytes += payload.size; |
| 1486 |
ssn->state.incoming_transfer_count++; |
| 1487 |
ssn->state.incoming_window--; |
| 1488 |
|
| 1489 |
// XXX: need better policy for when to refresh window |
| 1490 |
if (!ssn->state.incoming_window && (int32_t) link->state.local_handle >= 0) { |
| 1491 |
pni_post_flow(transport, ssn, link); |
| 1492 |
} |
| 1493 |
|
| 1494 |
return 0; |
| 1495 |
} |
| 1496 |
|
| 1497 |
int pn_do_flow(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload) |
| 1498 |
{ |
| 1499 |
pn_sequence_t onext, inext, delivery_count; |
| 1500 |
uint32_t iwin, owin, link_credit; |
| 1501 |
uint32_t handle; |
| 1502 |
bool inext_init, handle_init, dcount_init, drain; |
| 1503 |
|
| 1504 |
pn_amqp_decode_DqEQIIIIQIQIIqoe(payload, &inext_init, &inext, &iwin, |
| 1505 |
&onext, &owin, &handle_init, &handle, &dcount_init, |
| 1506 |
&delivery_count, &link_credit, &drain); |
| 1507 |
|
| 1508 |
pn_session_t *ssn = pni_channel_state(transport, channel); |
| 1509 |
if (!ssn) { |
| 1510 |
return pn_do_error(transport, "amqp:not-allowed", "no such channel: %u", channel); |
| 1511 |
} |
| 1512 |
|
| 1513 |
if (inext_init) { |
| 1514 |
ssn->state.remote_incoming_window = inext + iwin - ssn->state.outgoing_transfer_count; |
| 1515 |
} else { |
| 1516 |
ssn->state.remote_incoming_window = iwin; |
| 1517 |
} |
| 1518 |
|
| 1519 |
if (handle_init) { |
| 1520 |
pn_link_t *link = pni_handle_state(ssn, handle); |
| 1521 |
if (!link) { |
| 1522 |
return pn_do_error(transport, "amqp:invalid-field", "no such handle: %u", handle); |
| 1523 |
} |
| 1524 |
if (link->endpoint.type == SENDER) { |
| 1525 |
pn_sequence_t receiver_count; |
| 1526 |
if (dcount_init) { |
| 1527 |
receiver_count = delivery_count; |
| 1528 |
} else { |
| 1529 |
// our initial delivery count |
| 1530 |
receiver_count = 0; |
| 1531 |
} |
| 1532 |
pn_sequence_t old = link->state.link_credit; |
| 1533 |
link->state.link_credit = receiver_count + link_credit - link->state.delivery_count; |
| 1534 |
link->credit += link->state.link_credit - old; |
| 1535 |
link->drain = drain; |
| 1536 |
pn_delivery_t *delivery = pn_link_current(link); |
| 1537 |
if (delivery) pn_work_update(transport->connection, delivery); |
| 1538 |
} else { |
| 1539 |
pn_sequence_t delta = delivery_count - link->state.delivery_count; |
| 1540 |
if (delta > 0) { |
| 1541 |
link->state.delivery_count += delta; |
| 1542 |
link->state.link_credit -= delta; |
| 1543 |
link->credit -= delta; |
| 1544 |
link->drained += delta; |
| 1545 |
} |
| 1546 |
} |
| 1547 |
|
| 1548 |
pn_collector_put(transport->connection->collector, PN_OBJECT, link, PN_LINK_FLOW); |
| 1549 |
} |
| 1550 |
|
| 1551 |
return 0; |
| 1552 |
} |
| 1553 |
|
| 1554 |
static void pn_condition_set(pn_condition_t *condition, pn_bytes_t cond, pn_bytes_t desc) |
| 1555 |
{ |
| 1556 |
if (condition->name == NULL) { |
| 1557 |
condition->name = pn_string(NULL); |
| 1558 |
} |
| 1559 |
pn_string_setn(condition->name, cond.start, cond.size); |
| 1560 |
if (condition->description == NULL) { |
| 1561 |
condition->description = pn_string(NULL); |
| 1562 |
} |
| 1563 |
pn_string_setn(condition->description, desc.start, desc.size); |
| 1564 |
} |
| 1565 |
|
| 1566 |
static inline bool sequence_lte(pn_sequence_t a, pn_sequence_t b) { |
| 1567 |
return b-a <= INT32_MAX; |
| 1568 |
} |
| 1569 |
|
| 1570 |
static int pni_do_delivery_disposition(pn_transport_t * transport, pn_delivery_t *delivery, bool settled, bool remote_data, bool type_init, uint64_t type, pn_bytes_t disp_data) { |
| 1571 |
pn_disposition_t *remote = &delivery->remote; |
| 1572 |
|
| 1573 |
if (type_init) remote->type = type; |
| 1574 |
|
| 1575 |
if (remote_data) { |
| 1576 |
switch (type) { |
| 1577 |
case PN_RECEIVED: { |
| 1578 |
bool qnumber; |
| 1579 |
uint32_t number; |
| 1580 |
bool qoffset; |
| 1581 |
uint64_t offset; |
| 1582 |
pn_amqp_decode_DqEQIQLe(disp_data, &qnumber, &number, &qoffset, &offset); |
| 1583 |
|
| 1584 |
if (qnumber) { |
| 1585 |
remote->section_number = number; |
| 1586 |
} |
| 1587 |
if (qoffset) { |
| 1588 |
remote->section_offset = offset; |
| 1589 |
} |
| 1590 |
break; |
| 1591 |
} |
| 1592 |
case PN_ACCEPTED: |
| 1593 |
break; |
| 1594 |
|
| 1595 |
case PN_REJECTED: { |
| 1596 |
pn_bytes_t cond; |
| 1597 |
pn_bytes_t desc; |
| 1598 |
pn_data_t *info = pn_condition_info(&remote->condition); |
| 1599 |
pn_data_clear(info); |
| 1600 |
pn_amqp_decode_DqEDqEsSCee(disp_data, &cond, &desc, info); |
| 1601 |
pn_data_rewind(info); |
| 1602 |
pn_condition_set(&remote->condition, cond, desc); |
| 1603 |
|
| 1604 |
break; |
| 1605 |
} |
| 1606 |
case PN_RELEASED: |
| 1607 |
break; |
| 1608 |
|
| 1609 |
case PN_MODIFIED: { |
| 1610 |
bool qfailed; |
| 1611 |
bool failed; |
| 1612 |
bool qundeliverable; |
| 1613 |
bool undeliverable; |
| 1614 |
pn_data_clear(remote->annotations); |
| 1615 |
pn_amqp_decode_DqEQoQoCe(disp_data, &qfailed, &failed, &qundeliverable, &undeliverable, remote->annotations); |
| 1616 |
pn_data_rewind(remote->annotations); |
| 1617 |
|
| 1618 |
if (qfailed) { |
| 1619 |
remote->failed = failed; |
| 1620 |
} |
| 1621 |
if (qundeliverable) { |
| 1622 |
remote->undeliverable = undeliverable; |
| 1623 |
} |
| 1624 |
break; |
| 1625 |
} |
| 1626 |
default: |
| 1627 |
pn_data_clear(remote->data); |
| 1628 |
pn_amqp_decode_DqC(disp_data, remote->data); |
| 1629 |
pn_data_rewind(remote->data); |
| 1630 |
break; |
| 1631 |
} |
| 1632 |
} |
| 1633 |
|
| 1634 |
remote->settled = settled; |
| 1635 |
delivery->updated = true; |
| 1636 |
pn_work_update(transport->connection, delivery); |
| 1637 |
|
| 1638 |
pn_collector_put(transport->connection->collector, PN_OBJECT, delivery, PN_DELIVERY); |
| 1639 |
return 0; |
| 1640 |
} |
| 1641 |
|
| 1642 |
int pn_do_disposition(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload) |
| 1643 |
{ |
| 1644 |
bool role; |
| 1645 |
pn_sequence_t first, last; |
| 1646 |
bool last_init, settled; |
| 1647 |
pn_bytes_t disp_data; |
| 1648 |
pn_amqp_decode_DqEoIQIoRe(payload, &role, &first, &last_init, |
| 1649 |
&last, &settled, &disp_data); |
| 1650 |
if (!last_init) last = first; |
| 1651 |
|
| 1652 |
pn_session_t *ssn = pni_channel_state(transport, channel); |
| 1653 |
if (!ssn) { |
| 1654 |
return pn_do_error(transport, "amqp:not-allowed", "no such channel: %u", channel); |
| 1655 |
} |
| 1656 |
|
| 1657 |
if (!sequence_lte(first, last)) { |
| 1658 |
return pn_do_error(transport, "amqp:not allowed", "illegal delivery range: %x-%x", first, last); |
| 1659 |
} |
| 1660 |
|
| 1661 |
pn_delivery_map_t *deliveries; |
| 1662 |
if (role) { |
| 1663 |
deliveries = &ssn->state.outgoing; |
| 1664 |
} else { |
| 1665 |
deliveries = &ssn->state.incoming; |
| 1666 |
} |
| 1667 |
|
| 1668 |
bool type_init; |
| 1669 |
uint64_t type; |
| 1670 |
bool remote_data; |
| 1671 |
pn_amqp_decode_DQLQq(disp_data, &type_init, &type, &remote_data); |
| 1672 |
|
| 1673 |
|
| 1674 |
// Do some validation of received first and last values |
| 1675 |
// TODO: We should really also clamp the first value here, but we're not keeping track of the earliest |
| 1676 |
// unsettled delivery sequence no |
| 1677 |
last = sequence_lte(last, deliveries->next) ? last : deliveries->next; |
| 1678 |
|
| 1679 |
// If there are fewer deliveries in the session than the range then look at every delivery in the session |
| 1680 |
// otherwise look at every delivery_id in the disposition performative |
| 1681 |
pn_hash_t *dh = deliveries->deliveries; |
| 1682 |
if (last-first+1 >= pn_hash_size(dh)) { |
| 1683 |
for (pn_handle_t entry = pn_hash_head(dh); entry!=0 ; entry = pn_hash_next(dh, entry)) { |
| 1684 |
pn_sequence_t key = pn_hash_key(dh, entry); |
| 1685 |
if (sequence_lte(first, key) && sequence_lte(key, last)) { |
| 1686 |
pn_delivery_t *delivery = (pn_delivery_t*) pn_hash_value(dh, entry); |
| 1687 |
pni_do_delivery_disposition(transport, delivery, settled, remote_data, type_init, type, disp_data); |
| 1688 |
} |
| 1689 |
} |
| 1690 |
} else { |
| 1691 |
for (pn_sequence_t id = first; sequence_lte(id, last); ++id) { |
| 1692 |
pn_delivery_t *delivery = pni_delivery_map_get(deliveries, id); |
| 1693 |
if (delivery) { |
| 1694 |
pni_do_delivery_disposition(transport, delivery, settled, remote_data, type_init, type, disp_data); |
| 1695 |
} |
| 1696 |
} |
| 1697 |
} |
| 1698 |
return 0; |
| 1699 |
} |
| 1700 |
|
| 1701 |
int pn_do_detach(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload) |
| 1702 |
{ |
| 1703 |
uint32_t handle; |
| 1704 |
bool closed; |
| 1705 |
|
| 1706 |
pn_bytes_t error_condition; |
| 1707 |
pn_amqp_decode_DqEIoRe(payload, &handle, &closed, &error_condition); |
| 1708 |
|
| 1709 |
pn_session_t *ssn = pni_channel_state(transport, channel); |
| 1710 |
if (!ssn) { |
| 1711 |
return pn_do_error(transport, "amqp:not-allowed", "no such channel: %u", channel); |
| 1712 |
} |
| 1713 |
pn_link_t *link = pni_handle_state(ssn, handle); |
| 1714 |
if (!link) { |
| 1715 |
return pn_do_error(transport, "amqp:invalid-field", "no such handle: %u", handle); |
| 1716 |
} |
| 1717 |
|
| 1718 |
pn_bytes_t cond; |
| 1719 |
pn_bytes_t desc; |
| 1720 |
pn_condition_t* condition = &link->endpoint.remote_condition; |
| 1721 |
pn_condition_clear(condition); |
| 1722 |
pn_amqp_decode_DqEsSCe(error_condition, &cond, &desc, pn_condition_info(condition)); |
| 1723 |
pn_condition_set(condition, cond, desc); |
| 1724 |
pn_data_rewind(pn_condition_info(condition)); |
| 1725 |
|
| 1726 |
if (closed) |
| 1727 |
{ |
| 1728 |
PN_SET_REMOTE(link->endpoint.state, PN_REMOTE_CLOSED); |
| 1729 |
pn_collector_put(transport->connection->collector, PN_OBJECT, link, PN_LINK_REMOTE_CLOSE); |
| 1730 |
} else { |
| 1731 |
pn_collector_put(transport->connection->collector, PN_OBJECT, link, PN_LINK_REMOTE_DETACH); |
| 1732 |
} |
| 1733 |
|
| 1734 |
pni_unmap_remote_handle(link); |
| 1735 |
return 0; |
| 1736 |
} |
| 1737 |
|
| 1738 |
int pn_do_end(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload) |
| 1739 |
{ |
| 1740 |
pn_session_t *ssn = pni_channel_state(transport, channel); |
| 1741 |
if (!ssn) { |
| 1742 |
return pn_do_error(transport, "amqp:not-allowed", "no such channel: %u", channel); |
| 1743 |
} |
| 1744 |
|
| 1745 |
pn_bytes_t cond; |
| 1746 |
pn_bytes_t desc; |
| 1747 |
pn_condition_t* condition = &ssn->endpoint.remote_condition; |
| 1748 |
pn_condition_clear(condition); |
| 1749 |
pn_amqp_decode_DqEDqEsSCee(payload, &cond, &desc, pn_condition_info(condition)); |
| 1750 |
pn_condition_set(condition, cond, desc); |
| 1751 |
pn_data_rewind(pn_condition_info(condition)); |
| 1752 |
PN_SET_REMOTE(ssn->endpoint.state, PN_REMOTE_CLOSED); |
| 1753 |
pn_collector_put(transport->connection->collector, PN_OBJECT, ssn, PN_SESSION_REMOTE_CLOSE); |
| 1754 |
pni_unmap_remote_channel(ssn); |
| 1755 |
return 0; |
| 1756 |
} |
| 1757 |
|
| 1758 |
int pn_do_close(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload) |
| 1759 |
{ |
| 1760 |
pn_connection_t *conn = transport->connection; |
| 1761 |
|
| 1762 |
pn_bytes_t cond; |
| 1763 |
pn_bytes_t desc; |
| 1764 |
pn_condition_t* condition = &transport->remote_condition; |
| 1765 |
pn_condition_clear(condition); |
| 1766 |
pn_amqp_decode_DqEDqEsSCee(payload, &cond, &desc, pn_condition_info(condition)); |
| 1767 |
pn_condition_set(condition, cond, desc); |
| 1768 |
pn_data_rewind(pn_condition_info(condition)); |
| 1769 |
transport->close_rcvd = true; |
| 1770 |
PN_SET_REMOTE(conn->endpoint.state, PN_REMOTE_CLOSED); |
| 1771 |
pn_collector_put(transport->connection->collector, PN_OBJECT, conn, PN_CONNECTION_REMOTE_CLOSE); |
| 1772 |
return 0; |
| 1773 |
} |
| 1774 |
|
| 1775 |
// deprecated |
| 1776 |
ssize_t pn_transport_input(pn_transport_t *transport, const char *bytes, size_t available) |
| 1777 |
{ |
| 1778 |
if (!transport) return PN_ARG_ERR; |
| 1779 |
if (available == 0) { |
| 1780 |
return pn_transport_close_tail(transport); |
| 1781 |
} |
| 1782 |
const size_t original = available; |
| 1783 |
ssize_t capacity = pn_transport_capacity(transport); |
| 1784 |
if (capacity < 0) return capacity; |
| 1785 |
while (available && capacity) { |
| 1786 |
char *dest = pn_transport_tail(transport); |
| 1787 |
assert(dest); |
| 1788 |
size_t count = pn_min( (size_t)capacity, available ); |
| 1789 |
memmove( dest, bytes, count ); |
| 1790 |
available -= count; |
| 1791 |
bytes += count; |
| 1792 |
int rc = pn_transport_process( transport, count ); |
| 1793 |
if (rc < 0) return rc; |
| 1794 |
capacity = pn_transport_capacity(transport); |
| 1795 |
if (capacity < 0) return capacity; |
| 1796 |
} |
| 1797 |
|
| 1798 |
return original - available; |
| 1799 |
} |
| 1800 |
|
| 1801 |
// process pending input until none remaining or EOS |
| 1802 |
static ssize_t transport_consume(pn_transport_t *transport) |
| 1803 |
{ |
| 1804 |
// This allows whatever is driving the I/O to set the error |
| 1805 |
// condition on the transport before doing pn_transport_close_head() |
| 1806 |
// or pn_transport_close_tail(). This allows all transport errors to |
| 1807 |
// flow to the app the same way, but provides cleaner error messages |
| 1808 |
// since we don't try to look for a protocol header when, e.g. the |
| 1809 |
// connection was refused. |
| 1810 |
if (!(transport->present_layers & LAYER_AMQP1) && transport->tail_closed && |
| 1811 |
pn_condition_is_set(&transport->condition)) { |
| 1812 |
pn_do_error(transport, NULL, NULL); |
| 1813 |
return PN_EOS; |
| 1814 |
} |
| 1815 |
|
| 1816 |
size_t consumed = 0; |
| 1817 |
|
| 1818 |
while (transport->input_pending || transport->tail_closed) { |
| 1819 |
ssize_t n; |
| 1820 |
n = transport->io_layers[0]-> |
| 1821 |
process_input( transport, 0, |
| 1822 |
transport->input_buf + consumed, |
| 1823 |
transport->input_pending ); |
| 1824 |
if (n > 0) { |
| 1825 |
consumed += n; |
| 1826 |
transport->input_pending -= n; |
| 1827 |
} else if (n == 0) { |
| 1828 |
break; |
| 1829 |
} else { |
| 1830 |
assert(n == PN_EOS); |
| 1831 |
PN_LOG(&transport->logger, PN_SUBSYSTEM_AMQP | PN_SUBSYSTEM_IO, PN_LEVEL_FRAME | PN_LEVEL_RAW, " <- EOS"); |
| 1832 |
transport->input_pending = 0; // XXX ??? |
| 1833 |
return n; |
| 1834 |
} |
| 1835 |
} |
| 1836 |
|
| 1837 |
if (transport->input_pending && consumed) { |
| 1838 |
memmove( transport->input_buf, &transport->input_buf[consumed], transport->input_pending ); |
| 1839 |
} |
| 1840 |
|
| 1841 |
return consumed; |
| 1842 |
} |
| 1843 |
|
| 1844 |
static int pni_process_conn_setup(pn_transport_t *transport, pn_endpoint_t *endpoint) |
| 1845 |
{ |
| 1846 |
if (endpoint->type == CONNECTION) |
| 1847 |
{ |
| 1848 |
if (!(endpoint->state & PN_LOCAL_UNINIT) && !transport->open_sent) |
| 1849 |
{ |
| 1850 |
// as per the recommendation in the spec, advertise half our |
| 1851 |
// actual timeout to the remote |
| 1852 |
const pn_millis_t idle_timeout = transport->local_idle_timeout |
| 1853 |
? (transport->local_idle_timeout/2) |
| 1854 |
: 0; |
| 1855 |
pn_connection_t *connection = (pn_connection_t *) endpoint; |
| 1856 |
const char *cid = pn_string_get(connection->container); |
| 1857 |
pni_calculate_channel_max(transport); |
| 1858 |
/* "DL[SS?I?H?InnMMC]" */ |
| 1859 |
pn_bytes_t buf = pn_amqp_encode_DLESSQIQHQInnMMCe(transport->frame, OPEN, |
| 1860 |
cid ? cid : "", |
| 1861 |
pn_string_get(connection->hostname), |
| 1862 |
// TODO: This is messy, because we also have to allow local_max_frame_ to be 0 to mean unlimited |
| 1863 |
// otherwise flow control goes wrong |
| 1864 |
transport->local_max_frame!=0 && transport->local_max_frame!=OPEN_MAX_FRAME_SIZE_DEFAULT, |
| 1865 |
transport->local_max_frame, |
| 1866 |
transport->channel_max!=OPEN_CHANNEL_MAX_DEFAULT, transport->channel_max, |
| 1867 |
(bool)idle_timeout, idle_timeout, |
| 1868 |
connection->offered_capabilities, |
| 1869 |
connection->desired_capabilities, |
| 1870 |
connection->properties); |
| 1871 |
int err = pn_framing_send_amqp(transport, 0, buf); |
| 1872 |
if (err) return err; |
| 1873 |
transport->open_sent = true; |
| 1874 |
} |
| 1875 |
} |
| 1876 |
|
| 1877 |
return 0; |
| 1878 |
} |
| 1879 |
|
| 1880 |
static uint16_t allocate_alias(pn_hash_t *aliases, uint32_t max_index, int * valid) |
| 1881 |
{ |
| 1882 |
for (uint32_t i = 0; i <= max_index; i++) { |
| 1883 |
if (!pn_hash_get(aliases, i)) { |
| 1884 |
* valid = 1; |
| 1885 |
return i; |
| 1886 |
} |
| 1887 |
} |
| 1888 |
|
| 1889 |
* valid = 0; |
| 1890 |
return 0; |
| 1891 |
} |
| 1892 |
|
| 1893 |
static size_t pni_session_outgoing_window(pn_session_t *ssn) |
| 1894 |
{ |
| 1895 |
return ssn->outgoing_window; |
| 1896 |
} |
| 1897 |
|
| 1898 |
static size_t pni_session_incoming_window(pn_session_t *ssn) |
| 1899 |
{ |
| 1900 |
pn_transport_t *t = ssn->connection->transport; |
| 1901 |
uint32_t size = t->local_max_frame; |
| 1902 |
size_t capacity = ssn->incoming_capacity; |
| 1903 |
if (!size || !capacity) { /* session flow control is not enabled */ |
| 1904 |
return AMQP_MAX_WINDOW_SIZE; |
| 1905 |
} else if (capacity >= size) { /* precondition */ |
| 1906 |
return (capacity - ssn->incoming_bytes) / size; |
| 1907 |
} else { /* error: we will never have a non-zero window */ |
| 1908 |
pn_condition_format( |
| 1909 |
pn_transport_condition(t), |
| 1910 |
"amqp:internal-error", |
| 1911 |
"session capacity %zu is less than frame size %" PRIu32, |
| 1912 |
capacity, size); |
| 1913 |
pn_transport_close_tail(t); |
| 1914 |
return 0; |
| 1915 |
} |
| 1916 |
} |
| 1917 |
|
| 1918 |
static int pni_map_local_channel(pn_session_t *ssn) |
| 1919 |
{ |
| 1920 |
pn_transport_t *transport = ssn->connection->transport; |
| 1921 |
pn_session_state_t *state = &ssn->state; |
| 1922 |
int valid; |
| 1923 |
uint16_t channel = allocate_alias(transport->local_channels, transport->channel_max, & valid); |
| 1924 |
if (!valid) { |
| 1925 |
return 0; |
| 1926 |
} |
| 1927 |
state->local_channel = channel; |
| 1928 |
pn_hash_put(transport->local_channels, channel, ssn); |
| 1929 |
pn_ep_incref(&ssn->endpoint); |
| 1930 |
return 1; |
| 1931 |
} |
| 1932 |
|
| 1933 |
static int pni_process_ssn_setup(pn_transport_t *transport, pn_endpoint_t *endpoint) |
| 1934 |
{ |
| 1935 |
if (endpoint->type == SESSION && transport->open_sent) |
| 1936 |
{ |
| 1937 |
pn_session_t *ssn = (pn_session_t *) endpoint; |
| 1938 |
pn_session_state_t *state = &ssn->state; |
| 1939 |
if (!(endpoint->state & PN_LOCAL_UNINIT) && state->local_channel == (uint16_t) -1) |
| 1940 |
{ |
| 1941 |
if (! pni_map_local_channel(ssn)) { |
| 1942 |
pn_logger_logf(&transport->logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_WARNING, "unable to find an open available channel within limit of %d", transport->channel_max ); |
| 1943 |
return PN_ERR; |
| 1944 |
} |
| 1945 |
state->incoming_window = pni_session_incoming_window(ssn); |
| 1946 |
state->outgoing_window = pni_session_outgoing_window(ssn); |
| 1947 |
/* "DL[?HIII]" */ |
| 1948 |
pn_bytes_t buf = pn_amqp_encode_DLEQHIIIe(transport->frame, BEGIN, |
| 1949 |
((int16_t) state->remote_channel >= 0), state->remote_channel, |
| 1950 |
state->outgoing_transfer_count, |
| 1951 |
state->incoming_window, |
| 1952 |
state->outgoing_window); |
| 1953 |
pn_framing_send_amqp(transport, state->local_channel, buf); |
| 1954 |
} |
| 1955 |
} |
| 1956 |
|
| 1957 |
return 0; |
| 1958 |
} |
| 1959 |
|
| 1960 |
static const char *expiry_symbol(pn_terminus_t* terminus) |
| 1961 |
{ |
| 1962 |
if (!terminus->has_expiry_policy) return NULL; |
| 1963 |
|
| 1964 |
switch (terminus->expiry_policy) |
| 1965 |
{ |
| 1966 |
case PN_EXPIRE_WITH_LINK: |
| 1967 |
return "link-detach"; |
| 1968 |
case PN_EXPIRE_WITH_SESSION: |
| 1969 |
return "session-end"; |
| 1970 |
case PN_EXPIRE_WITH_CONNECTION: |
| 1971 |
return "connection-close"; |
| 1972 |
case PN_EXPIRE_NEVER: |
| 1973 |
return "never"; |
| 1974 |
} |
| 1975 |
return NULL; |
| 1976 |
} |
| 1977 |
|
| 1978 |
static int pni_map_local_handle(pn_link_t *link) { |
| 1979 |
pn_link_state_t *state = &link->state; |
| 1980 |
pn_session_state_t *ssn_state = &link->session->state; |
| 1981 |
int valid; |
| 1982 |
// XXX TODO MICK: once changes are made to handle_max, change this hardcoded value to something reasonable. |
| 1983 |
state->local_handle = allocate_alias(ssn_state->local_handles, 65536, & valid); |
| 1984 |
if ( ! valid ) |
| 1985 |
return 0; |
| 1986 |
pn_hash_put(ssn_state->local_handles, state->local_handle, link); |
| 1987 |
pn_ep_incref(&link->endpoint); |
| 1988 |
return 1; |
| 1989 |
} |
| 1990 |
|
| 1991 |
static int pni_process_link_setup(pn_transport_t *transport, pn_endpoint_t *endpoint) |
| 1992 |
{ |
| 1993 |
if (transport->open_sent && (endpoint->type == SENDER || |
| 1994 |
endpoint->type == RECEIVER)) |
| 1995 |
{ |
| 1996 |
pn_link_t *link = (pn_link_t *) endpoint; |
| 1997 |
pn_session_state_t *ssn_state = &link->session->state; |
| 1998 |
pn_link_state_t *state = &link->state; |
| 1999 |
if (((int16_t) ssn_state->local_channel >= 0) && |
| 2000 |
!(endpoint->state & PN_LOCAL_UNINIT) && state->local_handle == (uint32_t) -1) |
| 2001 |
{ |
| 2002 |
pni_map_local_handle(link); |
| 2003 |
const pn_distribution_mode_t dist_mode = (pn_distribution_mode_t) link->source.distribution_mode; |
| 2004 |
if (link->target.type == PN_COORDINATOR) { |
| 2005 |
/* "DL[SIoBB?DL[SIsIoC?sCnCC]DL[C]nnI]" */ |
| 2006 |
pn_bytes_t buf = pn_amqp_encode_DLESIoBBQDLESIsIoCQsCnCCeDLECennIe(transport->frame, ATTACH, |
| 2007 |
pn_string_get(link->name), |
| 2008 |
state->local_handle, |
| 2009 |
endpoint->type == RECEIVER, |
| 2010 |
link->snd_settle_mode, |
| 2011 |
link->rcv_settle_mode, |
| 2012 |
(bool) link->source.type, SOURCE, |
| 2013 |
pn_string_get(link->source.address), |
| 2014 |
link->source.durability, |
| 2015 |
expiry_symbol(&link->source), |
| 2016 |
link->source.timeout, |
| 2017 |
link->source.dynamic, |
| 2018 |
link->source.properties, |
| 2019 |
(dist_mode != PN_DIST_MODE_UNSPECIFIED), dist_mode2symbol(dist_mode), |
| 2020 |
link->source.filter, |
| 2021 |
link->source.outcomes, |
| 2022 |
link->source.capabilities, |
| 2023 |
COORDINATOR, link->target.capabilities, |
| 2024 |
0); |
| 2025 |
int err = pn_framing_send_amqp(transport, ssn_state->local_channel, buf); |
| 2026 |
if (err) return err; |
| 2027 |
} else { |
| 2028 |
/* "DL[SIoBB?DL[SIsIoC?sCnMM]?DL[SIsIoCM]nnILnnC]" */ |
| 2029 |
pn_bytes_t buf = pn_amqp_encode_DLESIoBBQDLESIsIoCQsCnMMeQDLESIsIoCMennILnnCe(transport->frame, ATTACH, |
| 2030 |
pn_string_get(link->name), |
| 2031 |
state->local_handle, |
| 2032 |
endpoint->type == RECEIVER, |
| 2033 |
link->snd_settle_mode, |
| 2034 |
link->rcv_settle_mode, |
| 2035 |
|
| 2036 |
(bool) link->source.type, SOURCE, |
| 2037 |
pn_string_get(link->source.address), |
| 2038 |
link->source.durability, |
| 2039 |
expiry_symbol(&link->source), |
| 2040 |
link->source.timeout, |
| 2041 |
link->source.dynamic, |
| 2042 |
link->source.properties, |
| 2043 |
(dist_mode != PN_DIST_MODE_UNSPECIFIED), dist_mode2symbol(dist_mode), |
| 2044 |
link->source.filter, |
| 2045 |
link->source.outcomes, |
| 2046 |
link->source.capabilities, |
| 2047 |
|
| 2048 |
(bool) link->target.type, TARGET, |
| 2049 |
pn_string_get(link->target.address), |
| 2050 |
link->target.durability, |
| 2051 |
expiry_symbol(&link->target), |
| 2052 |
link->target.timeout, |
| 2053 |
link->target.dynamic, |
| 2054 |
link->target.properties, |
| 2055 |
link->target.capabilities, |
| 2056 |
|
| 2057 |
0, |
| 2058 |
link->max_message_size, |
| 2059 |
link->properties); |
| 2060 |
int err = pn_framing_send_amqp(transport, ssn_state->local_channel, buf); |
| 2061 |
if (err) return err; |
| 2062 |
} |
| 2063 |
} |
| 2064 |
} |
| 2065 |
|
| 2066 |
return 0; |
| 2067 |
} |
| 2068 |
|
| 2069 |
static int pni_post_flow(pn_transport_t *transport, pn_session_t *ssn, pn_link_t *link) |
| 2070 |
{ |
| 2071 |
ssn->state.incoming_window = pni_session_incoming_window(ssn); |
| 2072 |
ssn->state.outgoing_window = pni_session_outgoing_window(ssn); |
| 2073 |
bool linkq = (bool) link; |
| 2074 |
pn_link_state_t *state = &link->state; |
| 2075 |
/* "DL[?IIII?I?I?In?o]" */ |
| 2076 |
pn_bytes_t buf = pn_amqp_encode_DLEQIIIIQIQIQInQoe(transport->frame, FLOW, |
| 2077 |
(int16_t) ssn->state.remote_channel >= 0, ssn->state.incoming_transfer_count, |
| 2078 |
ssn->state.incoming_window, |
| 2079 |
ssn->state.outgoing_transfer_count, |
| 2080 |
ssn->state.outgoing_window, |
| 2081 |
linkq, linkq ? state->local_handle : 0, |
| 2082 |
linkq, linkq ? state->delivery_count : 0, |
| 2083 |
linkq, linkq ? state->link_credit : 0, |
| 2084 |
linkq, linkq ? link->drain : false); |
| 2085 |
return pn_framing_send_amqp(transport, ssn->state.local_channel, buf); |
| 2086 |
} |
| 2087 |
|
| 2088 |
static int pni_process_flow_receiver(pn_transport_t *transport, pn_endpoint_t *endpoint) |
| 2089 |
{ |
| 2090 |
if (endpoint->type == RECEIVER && endpoint->state & PN_LOCAL_ACTIVE) |
| 2091 |
{ |
| 2092 |
pn_link_t *rcv = (pn_link_t *) endpoint; |
| 2093 |
pn_session_t *ssn = rcv->session; |
| 2094 |
pn_link_state_t *state = &rcv->state; |
| 2095 |
if ((int16_t) ssn->state.local_channel >= 0 && |
| 2096 |
(int32_t) state->local_handle >= 0 && |
| 2097 |
((rcv->drain || state->link_credit != rcv->credit - rcv->queued) || !ssn->state.incoming_window)) { |
| 2098 |
state->link_credit = rcv->credit - rcv->queued; |
| 2099 |
return pni_post_flow(transport, ssn, rcv); |
| 2100 |
} |
| 2101 |
} |
| 2102 |
|
| 2103 |
return 0; |
| 2104 |
} |
| 2105 |
|
| 2106 |
static int pni_flush_disp(pn_transport_t *transport, pn_session_t *ssn) |
| 2107 |
{ |
| 2108 |
uint64_t code = ssn->state.disp_code; |
| 2109 |
bool settled = ssn->state.disp_settled; |
| 2110 |
if (ssn->state.disp) { |
| 2111 |
/* "DL[oI?I?o?DL[]]" */ |
| 2112 |
pn_bytes_t buf = pn_amqp_encode_DLEoIQIQoQDLEee(transport->frame, DISPOSITION, |
| 2113 |
ssn->state.disp_type, |
| 2114 |
ssn->state.disp_first, |
| 2115 |
ssn->state.disp_last!=ssn->state.disp_first, ssn->state.disp_last, |
| 2116 |
settled, settled, |
| 2117 |
(bool)code, code); |
| 2118 |
int err = pn_framing_send_amqp(transport, ssn->state.local_channel, buf); |
| 2119 |
if (err) return err; |
| 2120 |
ssn->state.disp_type = 0; |
| 2121 |
ssn->state.disp_code = 0; |
| 2122 |
ssn->state.disp_settled = 0; |
| 2123 |
ssn->state.disp_first = 0; |
| 2124 |
ssn->state.disp_last = 0; |
| 2125 |
ssn->state.disp = false; |
| 2126 |
} |
| 2127 |
return 0; |
| 2128 |
} |
| 2129 |
|
| 2130 |
static int pni_post_disp(pn_transport_t *transport, pn_delivery_t *delivery) |
| 2131 |
{ |
| 2132 |
pn_link_t *link = delivery->link; |
| 2133 |
pn_session_t *ssn = link->session; |
| 2134 |
pn_session_state_t *ssn_state = &ssn->state; |
| 2135 |
pn_modified(transport->connection, &link->session->endpoint, false); |
| 2136 |
pn_delivery_state_t *state = &delivery->state; |
| 2137 |
assert(state->init); |
| 2138 |
bool role = (link->endpoint.type == RECEIVER); |
| 2139 |
uint64_t code = delivery->local.type; |
| 2140 |
|
| 2141 |
if (!code && !delivery->local.settled) { |
| 2142 |
return 0; |
| 2143 |
} |
| 2144 |
|
| 2145 |
if (!pni_disposition_batchable(&delivery->local)) { |
| 2146 |
pn_data_clear(transport->disp_data); |
| 2147 |
PN_RETURN_IF_ERROR(pni_disposition_encode(&delivery->local, transport->disp_data)); |
| 2148 |
/* "DL[oIn?o?DLC]" */ |
| 2149 |
pn_bytes_t buf = pn_amqp_encode_DLEoInQoQDLCe(transport->frame,DISPOSITION, |
| 2150 |
role, state->id, |
| 2151 |
delivery->local.settled, delivery->local.settled, |
| 2152 |
(bool)code, code, transport->disp_data); |
| 2153 |
return pn_framing_send_amqp(transport, ssn->state.local_channel, buf); |
| 2154 |
} |
| 2155 |
|
| 2156 |
if (ssn_state->disp && code == ssn_state->disp_code && |
| 2157 |
delivery->local.settled == ssn_state->disp_settled && |
| 2158 |
ssn_state->disp_type == role) { |
| 2159 |
if (state->id == ssn_state->disp_first - 1) { |
| 2160 |
ssn_state->disp_first = state->id; |
| 2161 |
return 0; |
| 2162 |
} else if (state->id == ssn_state->disp_last + 1) { |
| 2163 |
ssn_state->disp_last = state->id; |
| 2164 |
return 0; |
| 2165 |
} |
| 2166 |
} |
| 2167 |
|
| 2168 |
if (ssn_state->disp) { |
| 2169 |
int err = pni_flush_disp(transport, ssn); |
| 2170 |
if (err) return err; |
| 2171 |
} |
| 2172 |
|
| 2173 |
ssn_state->disp_type = role; |
| 2174 |
ssn_state->disp_code = code; |
| 2175 |
ssn_state->disp_settled = delivery->local.settled; |
| 2176 |
ssn_state->disp_first = state->id; |
| 2177 |
ssn_state->disp_last = state->id; |
| 2178 |
ssn_state->disp = true; |
| 2179 |
|
| 2180 |
return 0; |
| 2181 |
} |
| 2182 |
|
| 2183 |
static int pni_process_tpwork_sender(pn_transport_t *transport, pn_delivery_t *delivery, bool *settle) |
| 2184 |
{ |
| 2185 |
pn_link_t *link = delivery->link; |
| 2186 |
pn_delivery_state_t *state = &delivery->state; |
| 2187 |
if (delivery->aborted && !delivery->state.sending) { |
| 2188 |
// Aborted delivery with no data yet sent, drop it and issue a FLOW as we may have credit. |
| 2189 |
*settle = true; |
| 2190 |
state->sent = true; |
| 2191 |
pn_collector_put(transport->connection->collector, PN_OBJECT, link, PN_LINK_FLOW); |
| 2192 |
return 0; |
| 2193 |
} |
| 2194 |
*settle = false; |
| 2195 |
pn_session_state_t *ssn_state = &link->session->state; |
| 2196 |
pn_link_state_t *link_state = &link->state; |
| 2197 |
bool xfr_posted = false; |
| 2198 |
if ((int16_t) ssn_state->local_channel >= 0 && (int32_t) link_state->local_handle >= 0) { |
| 2199 |
if (!state->sent && (delivery->done || pn_buffer_size(delivery->bytes) > 0) && |
| 2200 |
ssn_state->remote_incoming_window > 0 && link_state->link_credit > 0) { |
| 2201 |
if (!state->init) { |
| 2202 |
state = pni_delivery_map_push(&ssn_state->outgoing, delivery); |
| 2203 |
} |
| 2204 |
|
| 2205 |
pn_bytes_t bytes = pn_buffer_bytes(delivery->bytes); |
| 2206 |
size_t full_size = bytes.size; |
| 2207 |
pn_bytes_t tag = pn_buffer_bytes(delivery->tag); |
| 2208 |
pn_data_clear(transport->disp_data); |
| 2209 |
PN_RETURN_IF_ERROR(pni_disposition_encode(&delivery->local, transport->disp_data)); |
| 2210 |
int count = pni_post_amqp_transfer_frame(transport, |
| 2211 |
ssn_state->local_channel, |
| 2212 |
link_state->local_handle, |
| 2213 |
state->id, &bytes, &tag, |
| 2214 |
0, // message-format |
| 2215 |
delivery->local.settled, |
| 2216 |
!delivery->done, |
| 2217 |
ssn_state->remote_incoming_window, |
| 2218 |
delivery->local.type, |
| 2219 |
transport->disp_data, |
| 2220 |
false, /* Resume */ |
| 2221 |
delivery->aborted, |
| 2222 |
false /* Batchable */ |
| 2223 |
); |
| 2224 |
if (count < 0) return count; |
| 2225 |
state->sending = true; |
| 2226 |
xfr_posted = true; |
| 2227 |
ssn_state->outgoing_transfer_count += count; |
| 2228 |
ssn_state->remote_incoming_window -= count; |
| 2229 |
|
| 2230 |
int sent = full_size - bytes.size; |
| 2231 |
pn_buffer_trim(delivery->bytes, sent, 0); |
| 2232 |
link->session->outgoing_bytes -= sent; |
| 2233 |
if (!pn_buffer_size(delivery->bytes) && delivery->done) { |
| 2234 |
state->sent = true; |
| 2235 |
link_state->delivery_count++; |
| 2236 |
link_state->link_credit--; |
| 2237 |
link->queued--; |
| 2238 |
link->session->outgoing_deliveries--; |
| 2239 |
} |
| 2240 |
|
| 2241 |
pn_collector_put(transport->connection->collector, PN_OBJECT, link, PN_LINK_FLOW); |
| 2242 |
} |
| 2243 |
} |
| 2244 |
|
| 2245 |
if (!state->init) state = NULL; |
| 2246 |
if ((int16_t) ssn_state->local_channel >= 0 && !delivery->remote.settled |
| 2247 |
&& state && state->sent && !xfr_posted) { |
| 2248 |
int err = pni_post_disp(transport, delivery); |
| 2249 |
if (err) return err; |
| 2250 |
} |
| 2251 |
|
| 2252 |
*settle = delivery->local.settled && state && state->sent; |
| 2253 |
return 0; |
| 2254 |
} |
| 2255 |
|
| 2256 |
static int pni_process_tpwork_receiver(pn_transport_t *transport, pn_delivery_t *delivery, bool *settle) |
| 2257 |
{ |
| 2258 |
*settle = false; |
| 2259 |
pn_link_t *link = delivery->link; |
| 2260 |
// XXX: need to prevent duplicate disposition sending |
| 2261 |
pn_session_t *ssn = link->session; |
| 2262 |
if ((int16_t) ssn->state.local_channel >= 0 && !delivery->remote.settled && delivery->state.init) { |
| 2263 |
int err = pni_post_disp(transport, delivery); |
| 2264 |
if (err) return err; |
| 2265 |
} |
| 2266 |
|
| 2267 |
// XXX: need to centralize this policy and improve it |
| 2268 |
if (!ssn->state.incoming_window) { |
| 2269 |
int err = pni_post_flow(transport, ssn, link); |
| 2270 |
if (err) return err; |
| 2271 |
} |
| 2272 |
|
| 2273 |
*settle = delivery->local.settled; |
| 2274 |
return 0; |
| 2275 |
} |
| 2276 |
|
| 2277 |
static int pni_process_tpwork(pn_transport_t *transport, pn_endpoint_t *endpoint) |
| 2278 |
{ |
| 2279 |
if (endpoint->type == CONNECTION && !transport->close_sent) |
| 2280 |
{ |
| 2281 |
pn_connection_t *conn = (pn_connection_t *) endpoint; |
| 2282 |
pn_delivery_t *delivery = conn->tpwork_head; |
| 2283 |
while (delivery) |
| 2284 |
{ |
| 2285 |
pn_delivery_t *tp_next = delivery->tpwork_next; |
| 2286 |
bool settle = false; |
| 2287 |
|
| 2288 |
pn_link_t *link = delivery->link; |
| 2289 |
pn_delivery_map_t *dm = NULL; |
| 2290 |
if (pn_link_is_sender(link)) { |
| 2291 |
dm = &link->session->state.outgoing; |
| 2292 |
int err = pni_process_tpwork_sender(transport, delivery, &settle); |
| 2293 |
if (err) return err; |
| 2294 |
} else { |
| 2295 |
dm = &link->session->state.incoming; |
| 2296 |
int err = pni_process_tpwork_receiver(transport, delivery, &settle); |
| 2297 |
if (err) return err; |
| 2298 |
} |
| 2299 |
|
| 2300 |
if (settle) { |
| 2301 |
pn_full_settle(dm, delivery); |
| 2302 |
} else if (!pn_delivery_buffered(delivery)) { |
| 2303 |
pn_clear_tpwork(delivery); |
| 2304 |
} |
| 2305 |
|
| 2306 |
delivery = tp_next; |
| 2307 |
} |
| 2308 |
} |
| 2309 |
|
| 2310 |
return 0; |
| 2311 |
} |
| 2312 |
|
| 2313 |
static int pni_process_flush_disp(pn_transport_t *transport, pn_endpoint_t *endpoint) |
| 2314 |
{ |
| 2315 |
if (endpoint->type == SESSION) { |
| 2316 |
pn_session_t *session = (pn_session_t *) endpoint; |
| 2317 |
pn_session_state_t *state = &session->state; |
| 2318 |
if ((int16_t) state->local_channel >= 0 && !transport->close_sent) |
| 2319 |
{ |
| 2320 |
int err = pni_flush_disp(transport, session); |
| 2321 |
if (err) return err; |
| 2322 |
} |
| 2323 |
} |
| 2324 |
|
| 2325 |
return 0; |
| 2326 |
} |
| 2327 |
|
| 2328 |
static int pni_process_flow_sender(pn_transport_t *transport, pn_endpoint_t *endpoint) |
| 2329 |
{ |
| 2330 |
if (endpoint->type == SENDER && endpoint->state & PN_LOCAL_ACTIVE) |
| 2331 |
{ |
| 2332 |
pn_link_t *snd = (pn_link_t *) endpoint; |
| 2333 |
pn_session_t *ssn = snd->session; |
| 2334 |
pn_link_state_t *state = &snd->state; |
| 2335 |
if ((int16_t) ssn->state.local_channel >= 0 && |
| 2336 |
(int32_t) state->local_handle >= 0 && |
| 2337 |
snd->drain && snd->drained) { |
| 2338 |
pn_delivery_t *tail = snd->unsettled_tail; |
| 2339 |
if (!tail || !pn_delivery_buffered(tail)) { |
| 2340 |
state->delivery_count += state->link_credit; |
| 2341 |
state->link_credit = 0; |
| 2342 |
snd->drained = 0; |
| 2343 |
return pni_post_flow(transport, ssn, snd); |
| 2344 |
} |
| 2345 |
} |
| 2346 |
} |
| 2347 |
|
| 2348 |
return 0; |
| 2349 |
} |
| 2350 |
|
| 2351 |
static void pni_unmap_local_handle(pn_link_t *link) { |
| 2352 |
pn_link_state_t *state = &link->state; |
| 2353 |
uintptr_t handle = state->local_handle; |
| 2354 |
state->local_handle = -2; |
| 2355 |
if (pn_hash_get(link->session->state.local_handles, handle)) { |
| 2356 |
pn_ep_decref(&link->endpoint); |
| 2357 |
} |
| 2358 |
// may delete link |
| 2359 |
pn_hash_del(link->session->state.local_handles, handle); |
| 2360 |
} |
| 2361 |
|
| 2362 |
static int pni_process_link_teardown(pn_transport_t *transport, pn_endpoint_t *endpoint) |
| 2363 |
{ |
| 2364 |
if (endpoint->type == SENDER || endpoint->type == RECEIVER) |
| 2365 |
{ |
| 2366 |
pn_link_t *link = (pn_link_t *) endpoint; |
| 2367 |
pn_session_t *session = link->session; |
| 2368 |
pn_session_state_t *ssn_state = &session->state; |
| 2369 |
pn_link_state_t *state = &link->state; |
| 2370 |
if (((endpoint->state & PN_LOCAL_CLOSED) || link->detached) && (int32_t) state->local_handle >= 0 && |
| 2371 |
(int16_t) ssn_state->local_channel >= 0 && !transport->close_sent) { |
| 2372 |
if (pn_link_is_sender(link) && pn_link_queued(link) && |
| 2373 |
(int32_t) state->remote_handle != -2 && |
| 2374 |
(int16_t) ssn_state->remote_channel != -2 && |
| 2375 |
!transport->close_rcvd) return 0; |
| 2376 |
|
| 2377 |
const char *name = NULL; |
| 2378 |
const char *description = NULL; |
| 2379 |
pn_data_t *info = NULL; |
| 2380 |
|
| 2381 |
if (pn_condition_is_set(&endpoint->condition)) { |
| 2382 |
name = pn_condition_get_name(&endpoint->condition); |
| 2383 |
description = pn_condition_get_description(&endpoint->condition); |
| 2384 |
info = pn_condition_info(&endpoint->condition); |
| 2385 |
} |
| 2386 |
/* "DL[I?o?DL[sSC]]" */ |
| 2387 |
pn_bytes_t buf = pn_amqp_encode_DLEIQoQDLEsSCee(transport->frame, DETACH, |
| 2388 |
state->local_handle, |
| 2389 |
!link->detached, !link->detached, |
| 2390 |
(bool)name, ERROR, name, description, info); |
| 2391 |
int err = pn_framing_send_amqp(transport, ssn_state->local_channel, buf); |
| 2392 |
if (err) return err; |
| 2393 |
pni_unmap_local_handle(link); |
| 2394 |
} |
| 2395 |
|
| 2396 |
pn_clear_modified(transport->connection, endpoint); |
| 2397 |
} |
| 2398 |
|
| 2399 |
return 0; |
| 2400 |
} |
| 2401 |
|
| 2402 |
static bool pni_pointful_buffering(pn_transport_t *transport, pn_session_t *session) |
| 2403 |
{ |
| 2404 |
if (transport->close_rcvd) return false; |
| 2405 |
if (!transport->open_rcvd) return true; |
| 2406 |
|
| 2407 |
pn_connection_t *conn = transport->connection; |
| 2408 |
pn_link_t *link = pn_link_head(conn, 0); |
| 2409 |
while (link) { |
| 2410 |
if (pn_link_is_sender(link) && pn_link_queued(link) > 0) { |
| 2411 |
pn_session_t *ssn = link->session; |
| 2412 |
if (session && session == ssn) { |
| 2413 |
if ((int32_t) link->state.remote_handle != -2 && |
| 2414 |
(int16_t) session->state.remote_channel != -2) { |
| 2415 |
return true; |
| 2416 |
} |
| 2417 |
} |
| 2418 |
} |
| 2419 |
link = pn_link_next(link, 0); |
| 2420 |
} |
| 2421 |
|
| 2422 |
return false; |
| 2423 |
} |
| 2424 |
|
| 2425 |
static void pni_unmap_local_channel(pn_session_t *ssn) { |
| 2426 |
// XXX: should really update link state also |
| 2427 |
pni_delivery_map_clear(&ssn->state.outgoing); |
| 2428 |
pni_transport_unbind_handles(ssn->state.local_handles, false); |
| 2429 |
pn_transport_t *transport = ssn->connection->transport; |
| 2430 |
pn_session_state_t *state = &ssn->state; |
| 2431 |
uintptr_t channel = state->local_channel; |
| 2432 |
state->local_channel = -2; |
| 2433 |
if (pn_hash_get(transport->local_channels, channel)) { |
| 2434 |
pn_ep_decref(&ssn->endpoint); |
| 2435 |
} |
| 2436 |
// may delete session |
| 2437 |
pn_hash_del(transport->local_channels, channel); |
| 2438 |
} |
| 2439 |
|
| 2440 |
static int pni_process_ssn_teardown(pn_transport_t *transport, pn_endpoint_t *endpoint) |
| 2441 |
{ |
| 2442 |
if (endpoint->type == SESSION) |
| 2443 |
{ |
| 2444 |
pn_session_t *session = (pn_session_t *) endpoint; |
| 2445 |
pn_session_state_t *state = &session->state; |
| 2446 |
if (endpoint->state & PN_LOCAL_CLOSED && (int16_t) state->local_channel >= 0 |
| 2447 |
&& !transport->close_sent) |
| 2448 |
{ |
| 2449 |
if (pni_pointful_buffering(transport, session)) { |
| 2450 |
return 0; |
| 2451 |
} |
| 2452 |
|
| 2453 |
const char *name = NULL; |
| 2454 |
const char *description = NULL; |
| 2455 |
pn_data_t *info = NULL; |
| 2456 |
|
| 2457 |
if (pn_condition_is_set(&endpoint->condition)) { |
| 2458 |
name = pn_condition_get_name(&endpoint->condition); |
| 2459 |
description = pn_condition_get_description(&endpoint->condition); |
| 2460 |
info = pn_condition_info(&endpoint->condition); |
| 2461 |
} |
| 2462 |
/* "DL[?DL[sSC]]" */ |
| 2463 |
pn_bytes_t buf = pn_amqp_encode_DLEQDLEsSCee(transport->frame, END, |
| 2464 |
(bool) name, ERROR, name, description, info); |
| 2465 |
int err = pn_framing_send_amqp(transport, state->local_channel, buf); |
| 2466 |
if (err) return err; |
| 2467 |
pni_unmap_local_channel(session); |
| 2468 |
} |
| 2469 |
|
| 2470 |
pn_clear_modified(transport->connection, endpoint); |
| 2471 |
} |
| 2472 |
return 0; |
| 2473 |
} |
| 2474 |
|
| 2475 |
static int pni_process_conn_teardown(pn_transport_t *transport, pn_endpoint_t *endpoint) |
| 2476 |
{ |
| 2477 |
if (endpoint->type == CONNECTION) |
| 2478 |
{ |
| 2479 |
if (endpoint->state & PN_LOCAL_CLOSED && !transport->close_sent) { |
| 2480 |
if (pni_pointful_buffering(transport, NULL)) return 0; |
| 2481 |
int err = pni_post_close(transport, NULL); |
| 2482 |
if (err) return err; |
| 2483 |
transport->close_sent = true; |
| 2484 |
} |
| 2485 |
|
| 2486 |
pn_clear_modified(transport->connection, endpoint); |
| 2487 |
} |
| 2488 |
return 0; |
| 2489 |
} |
| 2490 |
|
| 2491 |
static int pni_phase(pn_transport_t *transport, int (*phase)(pn_transport_t *, pn_endpoint_t *)) |
| 2492 |
{ |
| 2493 |
pn_connection_t *conn = transport->connection; |
| 2494 |
pn_endpoint_t *endpoint = conn->transport_head; |
| 2495 |
while (endpoint) |
| 2496 |
{ |
| 2497 |
pn_endpoint_t *next = endpoint->transport_next; |
| 2498 |
int err = phase(transport, endpoint); |
| 2499 |
if (err) return err; |
| 2500 |
endpoint = next; |
| 2501 |
} |
| 2502 |
return 0; |
| 2503 |
} |
| 2504 |
|
| 2505 |
static int pni_process(pn_transport_t *transport) |
| 2506 |
{ |
| 2507 |
int err; |
| 2508 |
if ((err = pni_phase(transport, pni_process_conn_setup))) return err; |
| 2509 |
if ((err = pni_phase(transport, pni_process_ssn_setup))) return err; |
| 2510 |
if ((err = pni_phase(transport, pni_process_link_setup))) return err; |
| 2511 |
if ((err = pni_phase(transport, pni_process_flow_receiver))) return err; |
| 2512 |
|
| 2513 |
// XXX: this has to happen two times because we might settle stuff |
| 2514 |
// on the first pass and create space for more work to be done on the |
| 2515 |
// second pass |
| 2516 |
if ((err = pni_phase(transport, pni_process_tpwork))) return err; |
| 2517 |
if ((err = pni_phase(transport, pni_process_tpwork))) return err; |
| 2518 |
|
| 2519 |
if ((err = pni_phase(transport, pni_process_flush_disp))) return err; |
| 2520 |
|
| 2521 |
if ((err = pni_phase(transport, pni_process_flow_sender))) return err; |
| 2522 |
if ((err = pni_phase(transport, pni_process_link_teardown))) return err; |
| 2523 |
if ((err = pni_phase(transport, pni_process_ssn_teardown))) return err; |
| 2524 |
if ((err = pni_phase(transport, pni_process_conn_teardown))) return err; |
| 2525 |
|
| 2526 |
if (transport->connection->tpwork_head) { |
| 2527 |
pn_modified(transport->connection, &transport->connection->endpoint, false); |
| 2528 |
} |
| 2529 |
|
| 2530 |
return 0; |
| 2531 |
} |
| 2532 |
|
| 2533 |
#define AMQP_HEADER ("AMQP\x00\x01\x00\x00") |
| 2534 |
|
| 2535 |
static void pn_error_amqp(pn_transport_t* transport, unsigned int layer) |
| 2536 |
{ |
| 2537 |
if (!transport->close_sent) { |
| 2538 |
if (!transport->open_sent) { |
| 2539 |
/* "DL[S]" */ |
| 2540 |
pn_bytes_t buf = pn_amqp_encode_DLESe(transport->frame, OPEN, ""); |
| 2541 |
pn_framing_send_amqp(transport, 0, buf); |
| 2542 |
} |
| 2543 |
|
| 2544 |
pni_post_close(transport, &transport->condition); |
| 2545 |
transport->close_sent = true; |
| 2546 |
} |
| 2547 |
transport->halt = true; |
| 2548 |
transport->done_processing = true; |
| 2549 |
} |
| 2550 |
|
| 2551 |
static ssize_t pn_input_read_amqp_header(pn_transport_t* transport, unsigned int layer, const char* bytes, size_t available) |
| 2552 |
{ |
| 2553 |
bool eos = transport->tail_closed; |
| 2554 |
if (eos && available==0) { |
| 2555 |
pn_do_error(transport, "amqp:connection:framing-error", |
| 2556 |
"Expected AMQP protocol header: no protocol header found (connection aborted)"); |
| 2557 |
return PN_EOS; |
| 2558 |
} |
| 2559 |
pni_protocol_type_t protocol = pni_sniff_header(bytes, available); |
| 2560 |
switch (protocol) { |
| 2561 |
case PNI_PROTOCOL_AMQP1: |
| 2562 |
transport->present_layers |= LAYER_AMQP1; |
| 2563 |
if (transport->io_layers[layer] == &amqp_read_header_layer) { |
| 2564 |
transport->io_layers[layer] = &amqp_layer; |
| 2565 |
} else { |
| 2566 |
transport->io_layers[layer] = &amqp_write_header_layer; |
| 2567 |
} |
| 2568 |
PN_LOG(&transport->logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_FRAME, " <- %s", "AMQP"); |
| 2569 |
return 8; |
| 2570 |
case PNI_PROTOCOL_INSUFFICIENT: |
| 2571 |
if (!eos) return 0; |
| 2572 |
/* Fallthru */ |
| 2573 |
default: |
| 2574 |
break; |
| 2575 |
} |
| 2576 |
char quoted[1024]; |
| 2577 |
pn_quote_data(quoted, 1024, bytes, available); |
| 2578 |
pn_do_error(transport, "amqp:connection:framing-error", |
| 2579 |
"Expected AMQP protocol header got: %s ['%s']%s", pni_protocol_name(protocol), quoted, |
| 2580 |
!eos ? "" : " (connection aborted)"); |
| 2581 |
return PN_EOS; |
| 2582 |
} |
| 2583 |
|
| 2584 |
static ssize_t pn_input_read_amqp(pn_transport_t* transport, unsigned int layer, const char* bytes, size_t available) |
| 2585 |
{ |
| 2586 |
if (transport->close_rcvd) { |
| 2587 |
if (available > 0) { |
| 2588 |
pn_do_error(transport, "amqp:connection:framing-error", "data after close"); |
| 2589 |
return PN_EOS; |
| 2590 |
} |
| 2591 |
} |
| 2592 |
|
| 2593 |
if (!transport->close_rcvd && !available) { |
| 2594 |
pn_do_error(transport, "amqp:connection:framing-error", "connection aborted"); |
| 2595 |
return PN_EOS; |
| 2596 |
} |
| 2597 |
|
| 2598 |
|
| 2599 |
ssize_t n = pn_dispatcher_input(transport, bytes, available, true, &transport->halt); |
| 2600 |
if (n < 0 || transport->close_rcvd) { |
| 2601 |
return PN_EOS; |
| 2602 |
} else { |
| 2603 |
return n; |
| 2604 |
} |
| 2605 |
} |
| 2606 |
|
| 2607 |
/* process AMQP related timer events */ |
| 2608 |
static int64_t pn_tick_amqp(pn_transport_t* transport, unsigned int layer, int64_t now) |
| 2609 |
{ |
| 2610 |
pn_timestamp_t timeout = 0; |
| 2611 |
|
| 2612 |
if (transport->local_idle_timeout) { |
| 2613 |
if (transport->dead_remote_deadline == 0 || |
| 2614 |
transport->last_bytes_input != transport->bytes_input) { |
| 2615 |
transport->dead_remote_deadline = now + transport->local_idle_timeout; |
| 2616 |
transport->last_bytes_input = transport->bytes_input; |
| 2617 |
} else if (transport->dead_remote_deadline <= now) { |
| 2618 |
transport->dead_remote_deadline = now + transport->local_idle_timeout; |
| 2619 |
if (!transport->posted_idle_timeout) { |
| 2620 |
transport->posted_idle_timeout = true; |
| 2621 |
// Note: AMQP-1.0 really should define a generic "timeout" error, but does not. |
| 2622 |
pn_do_error(transport, "amqp:resource-limit-exceeded", "local-idle-timeout expired"); |
| 2623 |
} |
| 2624 |
} |
| 2625 |
timeout = transport->dead_remote_deadline; |
| 2626 |
} |
| 2627 |
|
| 2628 |
// Prevent remote idle timeout as describe by AMQP 1.0: |
| 2629 |
if (transport->remote_idle_timeout && !transport->close_sent) { |
| 2630 |
if (transport->keepalive_deadline == 0 || |
| 2631 |
transport->last_bytes_output != transport->bytes_output) { |
| 2632 |
transport->keepalive_deadline = now + (pn_timestamp_t)(transport->remote_idle_timeout/2.0); |
| 2633 |
transport->last_bytes_output = transport->bytes_output; |
| 2634 |
} else if (transport->keepalive_deadline <= now) { |
| 2635 |
transport->keepalive_deadline = now + (pn_timestamp_t)(transport->remote_idle_timeout/2.0); |
| 2636 |
if (pn_buffer_size(transport->output_buffer) == 0) { // no outbound data pending |
| 2637 |
// so send empty frame (and account for it!) |
| 2638 |
pn_bytes_t buf = pn_bytes(0,""); |
| 2639 |
pn_framing_send_amqp(transport, 0, buf); |
| 2640 |
transport->last_bytes_output += pn_buffer_size(transport->output_buffer); |
| 2641 |
} |
| 2642 |
} |
| 2643 |
timeout = pn_timestamp_min( timeout, transport->keepalive_deadline ); |
| 2644 |
} |
| 2645 |
|
| 2646 |
return timeout; |
| 2647 |
} |
| 2648 |
|
| 2649 |
static ssize_t pn_output_write_amqp_header(pn_transport_t* transport, unsigned int layer, char* bytes, size_t available) |
| 2650 |
{ |
| 2651 |
PN_LOG(&transport->logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_FRAME, " -> %s", "AMQP"); |
| 2652 |
assert(available >= 8); |
| 2653 |
memmove(bytes, AMQP_HEADER, 8); |
| 2654 |
if (pn_condition_is_set(&transport->condition)) { |
| 2655 |
pn_error_amqp(transport, layer); |
| 2656 |
transport->io_layers[layer] = &pni_error_layer; |
| 2657 |
return pn_dispatcher_output(transport, bytes+8, available-8) + 8; |
| 2658 |
} |
| 2659 |
|
| 2660 |
if (transport->io_layers[layer] == &amqp_write_header_layer) { |
| 2661 |
transport->io_layers[layer] = &amqp_layer; |
| 2662 |
} else { |
| 2663 |
transport->io_layers[layer] = &amqp_read_header_layer; |
| 2664 |
} |
| 2665 |
return 8; |
| 2666 |
} |
| 2667 |
|
| 2668 |
static ssize_t pn_output_write_amqp(pn_transport_t* transport, unsigned int layer, char* bytes, size_t available) |
| 2669 |
{ |
| 2670 |
if (transport->connection && !transport->done_processing) { |
| 2671 |
int err = pni_process(transport); |
| 2672 |
if (err) { |
| 2673 |
pn_logger_logf(&transport->logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_ERROR, "process error %i", err); |
| 2674 |
transport->done_processing = true; |
| 2675 |
} |
| 2676 |
} |
| 2677 |
|
| 2678 |
// write out any buffered data _before_ returning PN_EOS, else we |
| 2679 |
// could truncate an outgoing Close frame containing a useful error |
| 2680 |
// status |
| 2681 |
if (!pn_buffer_size(transport->output_buffer) && transport->close_sent) { |
| 2682 |
return PN_EOS; |
| 2683 |
} |
| 2684 |
|
| 2685 |
return pn_dispatcher_output(transport, bytes, available); |
| 2686 |
} |
| 2687 |
|
| 2688 |
// Mark transport output as closed and send event |
| 2689 |
static void pni_close_head(pn_transport_t *transport) |
| 2690 |
{ |
| 2691 |
if (!transport->head_closed) { |
| 2692 |
transport->head_closed = true; |
| 2693 |
pn_collector_t *collector = pni_transport_collector(transport); |
| 2694 |
pn_collector_put(collector, PN_OBJECT, transport, PN_TRANSPORT_HEAD_CLOSED); |
| 2695 |
pni_maybe_post_closed(transport); |
| 2696 |
} |
| 2697 |
} |
| 2698 |
|
| 2699 |
// generate outbound data, return amount of pending output else error |
| 2700 |
static ssize_t transport_produce(pn_transport_t *transport) |
| 2701 |
{ |
| 2702 |
if (transport->head_closed) return PN_EOS; |
| 2703 |
|
| 2704 |
ssize_t space = transport->output_size - transport->output_pending; |
| 2705 |
|
| 2706 |
if (space <= 0) { // can we expand the buffer? |
| 2707 |
int more = 0; |
| 2708 |
if (!transport->remote_max_frame) // no limit, so double it |
| 2709 |
more = transport->output_size; |
| 2710 |
else if (transport->remote_max_frame > transport->output_size) |
| 2711 |
more = pn_min(transport->output_size, transport->remote_max_frame - transport->output_size); |
| 2712 |
if (more) { |
| 2713 |
char *newbuf = (char *)pni_mem_subreallocate(pn_class(transport), transport, transport->output_buf, transport->output_size + more ); |
| 2714 |
if (newbuf) { |
| 2715 |
transport->output_buf = newbuf; |
| 2716 |
transport->output_size += more; |
| 2717 |
space += more; |
| 2718 |
} |
| 2719 |
} |
| 2720 |
} |
| 2721 |
|
| 2722 |
while (space > 0) { |
| 2723 |
ssize_t n; |
| 2724 |
n = transport->io_layers[0]-> |
| 2725 |
process_output( transport, 0, |
| 2726 |
&transport->output_buf[transport->output_pending], |
| 2727 |
space ); |
| 2728 |
if (n > 0) { |
| 2729 |
space -= n; |
| 2730 |
transport->output_pending += n; |
| 2731 |
} else if (n == 0) { |
| 2732 |
break; |
| 2733 |
} else { |
| 2734 |
if (transport->output_pending) |
| 2735 |
break; // return what is available |
| 2736 |
PN_LOG(&transport->logger, PN_SUBSYSTEM_AMQP | PN_SUBSYSTEM_IO, PN_LEVEL_FRAME | PN_LEVEL_RAW, " -> EOS"); |
| 2737 |
pni_close_head(transport); |
| 2738 |
return n; |
| 2739 |
} |
| 2740 |
} |
| 2741 |
|
| 2742 |
return transport->output_pending; |
| 2743 |
} |
| 2744 |
|
| 2745 |
// deprecated |
| 2746 |
ssize_t pn_transport_output(pn_transport_t *transport, char *bytes, size_t size) |
| 2747 |
{ |
| 2748 |
if (!transport) return PN_ARG_ERR; |
| 2749 |
ssize_t available = pn_transport_pending(transport); |
| 2750 |
if (available > 0) { |
| 2751 |
available = (ssize_t) pn_min( (size_t)available, size ); |
| 2752 |
memmove( bytes, pn_transport_head(transport), available ); |
| 2753 |
pn_transport_pop( transport, (size_t) available ); |
| 2754 |
} |
| 2755 |
return available; |
| 2756 |
} |
| 2757 |
|
| 2758 |
|
| 2759 |
void pn_transport_trace(pn_transport_t *transport, pn_trace_t trace) |
| 2760 |
{ |
| 2761 |
unsigned int level_mask = 0; |
| 2762 |
if (trace & PN_TRACE_FRM) level_mask = level_mask | PN_LEVEL_FRAME; |
| 2763 |
if (trace & PN_TRACE_RAW) level_mask = level_mask | PN_LEVEL_RAW; |
| 2764 |
|
| 2765 |
// Don't change the subsystem bits, but forcibly set the level bits (back compat behaviour) |
| 2766 |
pn_logger_reset_mask(&transport->logger, PN_SUBSYSTEM_NONE, PN_LEVEL_ALL); |
| 2767 |
pn_logger_set_mask(&transport->logger, PN_SUBSYSTEM_NONE, (pn_log_level_t)level_mask); |
| 2768 |
} |
| 2769 |
|
| 2770 |
static void pni_tracer_to_log_sink(intptr_t context, pn_log_subsystem_t subsystem, pn_log_level_t sev, const char* msg) |
| 2771 |
{ |
| 2772 |
pn_transport_t *transport = (pn_transport_t*)context; |
| 2773 |
// can't use either logger or transport scratch string as we could be called with either |
| 2774 |
char buf[2048]; // Arbitrarily large |
| 2775 |
strcpy(buf, pn_logger_level_name(sev)); |
| 2776 |
strcat(buf, ": "); |
| 2777 |
strncat(buf, msg, 2037); // Up to 11 more characters than msg in buf |
| 2778 |
transport->tracer(transport, buf); |
| 2779 |
} |
| 2780 |
|
| 2781 |
void pn_transport_set_tracer(pn_transport_t *transport, pn_tracer_t tracer) |
| 2782 |
{ |
| 2783 |
assert(transport); |
| 2784 |
assert(tracer); |
| 2785 |
transport->tracer = tracer; |
| 2786 |
pn_logger_set_log_sink(&transport->logger, pni_tracer_to_log_sink, (intptr_t)transport); |
| 2787 |
} |
| 2788 |
|
| 2789 |
pn_tracer_t pn_transport_get_tracer(pn_transport_t *transport) |
| 2790 |
{ |
| 2791 |
assert(transport); |
| 2792 |
return transport->tracer; |
| 2793 |
} |
| 2794 |
|
| 2795 |
void pn_transport_set_context(pn_transport_t *transport, void *context) |
| 2796 |
{ |
| 2797 |
assert(transport); |
| 2798 |
pn_record_set(transport->context, PN_LEGCTX, context); |
| 2799 |
} |
| 2800 |
|
| 2801 |
void *pn_transport_get_context(pn_transport_t *transport) |
| 2802 |
{ |
| 2803 |
assert(transport); |
| 2804 |
return pn_record_get(transport->context, PN_LEGCTX); |
| 2805 |
} |
| 2806 |
|
| 2807 |
pn_record_t *pn_transport_attachments(pn_transport_t *transport) |
| 2808 |
{ |
| 2809 |
assert(transport); |
| 2810 |
return transport->context; |
| 2811 |
} |
| 2812 |
|
| 2813 |
void pn_transport_log(pn_transport_t *transport, const char *message) |
| 2814 |
{ |
| 2815 |
pn_logger_t *logger = transport ? &transport->logger : pn_default_logger(); |
| 2816 |
pni_logger_log(logger, PN_SUBSYSTEM_ALL, PN_LEVEL_TRACE, message); |
| 2817 |
} |
| 2818 |
|
| 2819 |
void pn_transport_vlogf(pn_transport_t *transport, const char *fmt, va_list ap) |
| 2820 |
{ |
| 2821 |
pn_logger_t *logger = transport ? &transport->logger : pn_default_logger(); |
| 2822 |
pni_logger_vlogf(logger, PN_SUBSYSTEM_ALL, PN_LEVEL_TRACE, fmt, ap); |
| 2823 |
} |
| 2824 |
|
| 2825 |
void pn_transport_logf(pn_transport_t *transport, const char *fmt, ...) |
| 2826 |
{ |
| 2827 |
va_list ap; |
| 2828 |
|
| 2829 |
va_start(ap, fmt); |
| 2830 |
pn_transport_vlogf(transport, fmt, ap); |
| 2831 |
va_end(ap); |
| 2832 |
} |
| 2833 |
|
| 2834 |
uint16_t pn_transport_get_channel_max(pn_transport_t *transport) |
| 2835 |
{ |
| 2836 |
return transport->channel_max; |
| 2837 |
} |
| 2838 |
|
| 2839 |
int pn_transport_set_channel_max(pn_transport_t *transport, uint16_t requested_channel_max) |
| 2840 |
{ |
| 2841 |
/* |
| 2842 |
* Once the OPEN frame has been sent, we have communicated our |
| 2843 |
* wishes to the remote client and there is no way to renegotiate. |
| 2844 |
* After that point, we do not allow the application to make changes. |
| 2845 |
* Before that point, however, the app is free to either raise or |
| 2846 |
* lower our local limit. (But the app cannot raise it above the |
| 2847 |
* limit imposed by this library.) |
| 2848 |
* The channel-max value will be finalized just before the OPEN frame |
| 2849 |
* is sent. |
| 2850 |
*/ |
| 2851 |
if(transport->open_sent) { |
| 2852 |
pn_logger_logf(&transport->logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_WARNING, "Cannot change local channel-max after OPEN frame sent."); |
| 2853 |
return PN_STATE_ERR; |
| 2854 |
} |
| 2855 |
|
| 2856 |
transport->local_channel_max = (requested_channel_max < PN_IMPL_CHANNEL_MAX) |
| 2857 |
? requested_channel_max |
| 2858 |
: PN_IMPL_CHANNEL_MAX; |
| 2859 |
pni_calculate_channel_max(transport); |
| 2860 |
|
| 2861 |
return PN_OK; |
| 2862 |
} |
| 2863 |
|
| 2864 |
uint16_t pn_transport_remote_channel_max(pn_transport_t *transport) |
| 2865 |
{ |
| 2866 |
return transport->remote_channel_max; |
| 2867 |
} |
| 2868 |
|
| 2869 |
uint32_t pn_transport_get_max_frame(pn_transport_t *transport) |
| 2870 |
{ |
| 2871 |
return transport->local_max_frame; |
| 2872 |
} |
| 2873 |
|
| 2874 |
void pn_transport_set_max_frame(pn_transport_t *transport, uint32_t size) |
| 2875 |
{ |
| 2876 |
// if size == 0, no advertised limit to input frame size. |
| 2877 |
if (size && size < AMQP_MIN_MAX_FRAME_SIZE) |
| 2878 |
size = AMQP_MIN_MAX_FRAME_SIZE; |
| 2879 |
transport->local_max_frame = size; |
| 2880 |
} |
| 2881 |
|
| 2882 |
uint32_t pn_transport_get_remote_max_frame(pn_transport_t *transport) |
| 2883 |
{ |
| 2884 |
return transport->remote_max_frame; |
| 2885 |
} |
| 2886 |
|
| 2887 |
pn_millis_t pn_transport_get_idle_timeout(pn_transport_t *transport) |
| 2888 |
{ |
| 2889 |
return transport->local_idle_timeout; |
| 2890 |
} |
| 2891 |
|
| 2892 |
void pn_transport_set_idle_timeout(pn_transport_t *transport, pn_millis_t timeout) |
| 2893 |
{ |
| 2894 |
transport->local_idle_timeout = timeout; |
| 2895 |
} |
| 2896 |
|
| 2897 |
pn_millis_t pn_transport_get_remote_idle_timeout(pn_transport_t *transport) |
| 2898 |
{ |
| 2899 |
return transport->remote_idle_timeout; |
| 2900 |
} |
| 2901 |
|
| 2902 |
int64_t pn_transport_tick(pn_transport_t *transport, int64_t now) |
| 2903 |
{ |
| 2904 |
int64_t r = 0; |
| 2905 |
for (int i = 0; i<PN_IO_LAYER_CT; ++i) { |
| 2906 |
if (transport->io_layers[i] && transport->io_layers[i]->process_tick) |
| 2907 |
r = pn_timestamp_min(r, transport->io_layers[i]->process_tick(transport, i, now)); |
| 2908 |
} |
| 2909 |
return r; |
| 2910 |
} |
| 2911 |
|
| 2912 |
uint64_t pn_transport_get_frames_output(const pn_transport_t *transport) |
| 2913 |
{ |
| 2914 |
if (transport) |
| 2915 |
return transport->output_frames_ct; |
| 2916 |
return 0; |
| 2917 |
} |
| 2918 |
|
| 2919 |
uint64_t pn_transport_get_frames_input(const pn_transport_t *transport) |
| 2920 |
{ |
| 2921 |
if (transport) |
| 2922 |
return transport->input_frames_ct; |
| 2923 |
return 0; |
| 2924 |
} |
| 2925 |
|
| 2926 |
ssize_t pni_transport_grow_capacity(pn_transport_t *transport, size_t n) { |
| 2927 |
// can we expand the size of the input buffer? |
| 2928 |
size_t size = pn_max(n, transport->input_size); |
| 2929 |
if (transport->local_max_frame) { // there is a limit to buffer size |
| 2930 |
size = pn_min(size, transport->local_max_frame); |
| 2931 |
} |
| 2932 |
if (size > transport->input_size) { |
| 2933 |
char *newbuf = (char *) pni_mem_subreallocate(pn_class(transport), transport, transport->input_buf, size ); |
| 2934 |
if (newbuf) { |
| 2935 |
transport->input_buf = newbuf; |
| 2936 |
transport->input_size = size; |
| 2937 |
} |
| 2938 |
} |
| 2939 |
return transport->input_size-transport->input_pending; |
| 2940 |
} |
| 2941 |
|
| 2942 |
// input |
| 2943 |
ssize_t pn_transport_capacity(pn_transport_t *transport) /* <0 == done */ |
| 2944 |
{ |
| 2945 |
if (transport->tail_closed) return PN_EOS; |
| 2946 |
//if (pn_error_code(transport->error)) return pn_error_code(transport->error); |
| 2947 |
|
| 2948 |
ssize_t capacity = transport->input_size - transport->input_pending; |
| 2949 |
if ( capacity<=0 ) { |
| 2950 |
capacity = pni_transport_grow_capacity(transport, 2*transport->input_size); |
| 2951 |
} |
| 2952 |
return capacity; |
| 2953 |
} |
| 2954 |
|
| 2955 |
|
| 2956 |
char *pn_transport_tail(pn_transport_t *transport) |
| 2957 |
{ |
| 2958 |
if (transport && transport->input_pending < transport->input_size) { |
| 2959 |
return &transport->input_buf[transport->input_pending]; |
| 2960 |
} |
| 2961 |
return NULL; |
| 2962 |
} |
| 2963 |
|
| 2964 |
ssize_t pn_transport_push(pn_transport_t *transport, const char *src, size_t size) |
| 2965 |
{ |
| 2966 |
assert(transport); |
| 2967 |
|
| 2968 |
ssize_t capacity = pn_transport_capacity(transport); |
| 2969 |
if (capacity < 0) { |
| 2970 |
return capacity; |
| 2971 |
} else if (size > (size_t) capacity) { |
| 2972 |
size = capacity; |
| 2973 |
} |
| 2974 |
|
| 2975 |
char *dst = pn_transport_tail(transport); |
| 2976 |
assert(dst); |
| 2977 |
memmove(dst, src, size); |
| 2978 |
|
| 2979 |
int n = pn_transport_process(transport, size); |
| 2980 |
if (n < 0) { |
| 2981 |
return n; |
| 2982 |
} else { |
| 2983 |
return size; |
| 2984 |
} |
| 2985 |
} |
| 2986 |
|
| 2987 |
int pn_transport_process(pn_transport_t *transport, size_t size) |
| 2988 |
{ |
| 2989 |
assert(transport); |
| 2990 |
size = pn_min( size, (transport->input_size - transport->input_pending) ); |
| 2991 |
transport->input_pending += size; |
| 2992 |
transport->bytes_input += size; |
| 2993 |
|
| 2994 |
ssize_t n = transport_consume( transport ); |
| 2995 |
if (n == PN_EOS) { |
| 2996 |
pni_close_tail(transport); |
| 2997 |
} |
| 2998 |
|
| 2999 |
if (n < 0 && n != PN_EOS) return n; |
| 3000 |
return 0; |
| 3001 |
} |
| 3002 |
|
| 3003 |
// input stream has closed |
| 3004 |
int pn_transport_close_tail(pn_transport_t *transport) |
| 3005 |
{ |
| 3006 |
pni_close_tail(transport); |
| 3007 |
transport_consume( transport ); |
| 3008 |
return 0; |
| 3009 |
// XXX: what if not all input processed at this point? do we care??? |
| 3010 |
} |
| 3011 |
|
| 3012 |
// output |
| 3013 |
ssize_t pn_transport_pending(pn_transport_t *transport) /* <0 == done */ |
| 3014 |
{ |
| 3015 |
assert(transport); |
| 3016 |
return transport_produce( transport ); |
| 3017 |
} |
| 3018 |
|
| 3019 |
const char *pn_transport_head(pn_transport_t *transport) |
| 3020 |
{ |
| 3021 |
if (transport && transport->output_pending) { |
| 3022 |
return transport->output_buf; |
| 3023 |
} |
| 3024 |
return NULL; |
| 3025 |
} |
| 3026 |
|
| 3027 |
ssize_t pn_transport_peek(pn_transport_t *transport, char *dst, size_t size) |
| 3028 |
{ |
| 3029 |
assert(transport); |
| 3030 |
|
| 3031 |
ssize_t pending = pn_transport_pending(transport); |
| 3032 |
if (pending < 0) { |
| 3033 |
return pending; |
| 3034 |
} else if (size > (size_t) pending) { |
| 3035 |
size = pending; |
| 3036 |
} |
| 3037 |
|
| 3038 |
if (pending > 0) { |
| 3039 |
const char *src = pn_transport_head(transport); |
| 3040 |
assert(src); |
| 3041 |
memmove(dst, src, size); |
| 3042 |
} |
| 3043 |
|
| 3044 |
return size; |
| 3045 |
} |
| 3046 |
|
| 3047 |
void pn_transport_pop(pn_transport_t *transport, size_t size) |
| 3048 |
{ |
| 3049 |
if (transport) { |
| 3050 |
assert( transport->output_pending >= size ); |
| 3051 |
transport->output_pending -= size; |
| 3052 |
transport->bytes_output += size; |
| 3053 |
if (transport->output_pending) { |
| 3054 |
// TODO: This could be potentially inefficient if we often pop the output without emptying it |
| 3055 |
// TODO: as we rotate the buffer here if we have any bytes left to write. |
| 3056 |
memmove( transport->output_buf, &transport->output_buf[size], |
| 3057 |
transport->output_pending ); |
| 3058 |
} else { |
| 3059 |
// If we emptied the output buffer then see if there's more output pending |
| 3060 |
pn_transport_pending(transport); |
| 3061 |
} |
| 3062 |
} |
| 3063 |
} |
| 3064 |
|
| 3065 |
int pn_transport_close_head(pn_transport_t *transport) |
| 3066 |
{ |
| 3067 |
ssize_t pending = pn_transport_pending(transport); |
| 3068 |
pni_close_head(transport); |
| 3069 |
if (pending > 0) |
| 3070 |
pn_transport_pop(transport, pending); |
| 3071 |
return 0; |
| 3072 |
} |
| 3073 |
|
| 3074 |
// true if the transport will not generate further output |
| 3075 |
bool pn_transport_quiesced(pn_transport_t *transport) |
| 3076 |
{ |
| 3077 |
if (!transport) return true; |
| 3078 |
ssize_t pending = pn_transport_pending(transport); |
| 3079 |
if (pending < 0) return true; // output done |
| 3080 |
else if (pending > 0) return false; |
| 3081 |
// no pending at transport, but check if data is buffered in I/O layers |
| 3082 |
for (int layer = 0; layer<PN_IO_LAYER_CT; ++layer) { |
| 3083 |
if (transport->io_layers[layer] && |
| 3084 |
transport->io_layers[layer]->buffered_output && |
| 3085 |
transport->io_layers[layer]->buffered_output( transport )) |
| 3086 |
return false; |
| 3087 |
} |
| 3088 |
return true; |
| 3089 |
} |
| 3090 |
|
| 3091 |
bool pn_transport_head_closed(pn_transport_t *transport) { return transport->head_closed; } |
| 3092 |
|
| 3093 |
bool pn_transport_tail_closed(pn_transport_t *transport) { return transport->tail_closed; } |
| 3094 |
|
| 3095 |
bool pn_transport_closed(pn_transport_t *transport) { |
| 3096 |
return transport->head_closed && transport->tail_closed; |
| 3097 |
} |
| 3098 |
|
| 3099 |
pn_connection_t *pn_transport_connection(pn_transport_t *transport) |
| 3100 |
{ |
| 3101 |
assert(transport); |
| 3102 |
return transport->connection; |
| 3103 |
} |