| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "thread.h" |
| 21 |
|
| 22 |
#include <proton/raw_connection.h> |
| 23 |
#include <proton/tls.h> |
| 24 |
#include <proton/listener.h> |
| 25 |
#include <proton/netaddr.h> |
| 26 |
#include <proton/proactor.h> |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#include <time.h> |
| 32 |
#include <assert.h> |
| 33 |
#include <unistd.h> |
| 34 |
|
| 35 |
|
| 36 |
/* |
| 37 |
* Jabberwock raw connection example with and without TLS. |
| 38 |
* |
| 39 |
* One client and one server take turns sending some lines of the poem. |
| 40 |
* The simple "application" logic resides in some_jabber() and gobble_jabber(). |
| 41 |
* handle_outgoing() and handle_incoming() handle the application IO |
| 42 |
* plumbing to use Proton raw connections, with or without TLS. |
| 43 |
* |
| 44 |
* See the "no_tls" option to contrast the approach compared to TLS usage. |
| 45 |
* |
| 46 |
* In this example orderly termination of the connection is initiated by one side. |
| 47 |
* The initiator does not wait for any close handshake from the peer. |
| 48 |
* The peer looks for confirmation of orderly closure from the initiator. |
| 49 |
* |
| 50 |
* Based on the broker and raw connection examples. Must be able to find |
| 51 |
* the TLS certificates in the same location as the broker example. |
| 52 |
* |
| 53 |
* The astute reader will realize that the lack of any locking mechanisms |
| 54 |
* works only by luck of the simple nature of this "taking turns" example |
| 55 |
* and may not survive TSAN scrutiny. |
| 56 |
*/ |
| 57 |
|
| 58 |
|
| 59 |
/* The ssl-certs subdir must be in the current directory for TLS configuration */ |
| 60 |
#define SSL_FILE(NAME) "ssl-certs/" NAME |
| 61 |
#define SSL_PW "tserverpw" |
| 62 |
/* Windows vs. OpenSSL certificates */ |
| 63 |
#if defined(_WIN32) |
| 64 |
# define CERTIFICATE(NAME) SSL_FILE(NAME "-certificate.p12") |
| 65 |
# define SET_CREDENTIALS(DOMAIN, NAME) \ |
| 66 |
pn_tls_config_set_credentials(DOMAIN, SSL_FILE(NAME "-full.p12"), "", SSL_PW) |
| 67 |
#else |
| 68 |
# define CERTIFICATE(NAME) SSL_FILE(NAME "-certificate.pem") |
| 69 |
# define SET_CREDENTIALS(DOMAIN, NAME) \ |
| 70 |
pn_tls_config_set_credentials(DOMAIN, CERTIFICATE(NAME), SSL_FILE(NAME "-private-key.pem"), SSL_PW) |
| 71 |
#endif |
| 72 |
|
| 73 |
// A raw buffer "pool", in name only |
| 74 |
static void rbuf_pool_get(pn_raw_buffer_t *bufs, uint32_t num) { |
| 75 |
memset(bufs, 0, sizeof(pn_raw_buffer_t) * num); |
| 76 |
while (num--) { |
| 77 |
bufs->bytes = calloc(1, 4096); |
| 78 |
bufs->capacity = 4096; |
| 79 |
bufs++; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
static void rbuf_pool_return(pn_raw_buffer_t *buf) { |
| 84 |
free(buf->bytes); |
| 85 |
} |
| 86 |
|
| 87 |
static void rbuf_pool_multi_return(pn_raw_buffer_t *buf, size_t n) { |
| 88 |
while(n--) |
| 89 |
rbuf_pool_return(buf++); |
| 90 |
} |
| 91 |
|
| 92 |
static size_t size_t_min(size_t a, size_t b) { |
| 93 |
return (a < b) ? a : b; |
| 94 |
} |
| 95 |
|
| 96 |
typedef struct jabber_t { |
| 97 |
pn_proactor_t *proactor; |
| 98 |
size_t threads; |
| 99 |
pn_tls_config_t *srv_domain; |
| 100 |
pn_tls_config_t *cli_domain; |
| 101 |
const char *host; |
| 102 |
const char *port; |
| 103 |
pn_listener_t *listener; |
| 104 |
size_t current_jline; |
| 105 |
size_t total_bytes_sent; |
| 106 |
size_t total_bytes_recv; |
| 107 |
bool alpn_enabled; |
| 108 |
} jabber_t; |
| 109 |
|
| 110 |
typedef struct jabber_connection_t { |
| 111 |
jabber_t *parent; |
| 112 |
pn_raw_connection_t *rawc; |
| 113 |
pn_tls_t *tls; |
| 114 |
// Orderly close: |
| 115 |
// Non TLS: shutdown write side, read side detects EOF |
| 116 |
// TLS: same shutdown, but also an encrypted TLS EOF using pn_tls_close_output() |
| 117 |
// which sends the TLS protocol close_notify alert record. |
| 118 |
bool need_rawc_write_close; |
| 119 |
bool tls_has_output; |
| 120 |
|
| 121 |
bool connecting; |
| 122 |
bool tls_closing; |
| 123 |
bool orderly_close_initiated; |
| 124 |
bool orderly_close_detected; |
| 125 |
bool tls_error; |
| 126 |
|
| 127 |
bool is_server; |
| 128 |
bool jabber_turn; |
| 129 |
char *alpn_protocol; |
| 130 |
} jabber_connection_t; |
| 131 |
|
| 132 |
|
| 133 |
static inline uint32_t room(pn_raw_buffer_t const *rb) { |
| 134 |
if (rb) |
| 135 |
return rb->capacity - (rb->offset + rb->size); |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
static const char* jlines[] = { |
| 140 |
"Twas brillig, and the slithy toves", |
| 141 |
"Did gire and gymble in the wabe.", |
| 142 |
"All mimsy were the borogroves,", |
| 143 |
"And the mome raths outgrabe." |
| 144 |
}; |
| 145 |
|
| 146 |
static size_t jlines_count = sizeof(jlines) / sizeof(jlines[0]); |
| 147 |
|
| 148 |
static size_t some_jabber(jabber_connection_t *jc, pn_raw_buffer_t *rbufp, size_t nrbufs) { |
| 149 |
jabber_t *j = jc->parent; |
| 150 |
const char *self = jc->is_server ? "server" : "client"; |
| 151 |
size_t actual = 0; |
| 152 |
// Simuate varying output for each run by choosing a random number of lines. |
| 153 |
int desired = (rand() % nrbufs) + 1; |
| 154 |
while (desired && j->current_jline < jlines_count) { |
| 155 |
size_t len = strlen(jlines[j->current_jline]); |
| 156 |
assert(len < room(rbufp)); |
| 157 |
memcpy(rbufp->bytes + rbufp->offset, jlines[j->current_jline++], len); |
| 158 |
rbufp->size = len; |
| 159 |
j->total_bytes_sent += len; |
| 160 |
desired--; |
| 161 |
actual++; |
| 162 |
rbufp++; |
| 163 |
} |
| 164 |
printf("--> %s supplied %d lines\n", self, actual); |
| 165 |
return actual; |
| 166 |
} |
| 167 |
|
| 168 |
static void gobble_jabber(jabber_connection_t* jc, pn_raw_buffer_t* rbuf) { |
| 169 |
jabber_t *j = jc->parent; |
| 170 |
const char *self = jc->is_server ? "server" : "client"; |
| 171 |
assert(rbuf->size != 0); |
| 172 |
printf("<-- %s received: %.4096s\n", self, rbuf->bytes + rbuf->offset); |
| 173 |
|
| 174 |
j->total_bytes_recv += rbuf->size; |
| 175 |
if (j->total_bytes_recv == j->total_bytes_sent) { |
| 176 |
if (j->current_jline != jlines_count) { |
| 177 |
// This connection sends the next set of lines. |
| 178 |
jc->jabber_turn = true; |
| 179 |
pn_raw_connection_wake(jc->rawc); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
rbuf_pool_return(rbuf); |
| 184 |
} |
| 185 |
|
| 186 |
// Return false if TLS error encountered. |
| 187 |
static bool jabber_tls_process(jabber_connection_t* jc) { |
| 188 |
int err = pn_tls_process(jc->tls); |
| 189 |
if (err && !jc->tls_error) { |
| 190 |
jc->tls_error = true; |
| 191 |
// Stop all application data processing. |
| 192 |
// Close input. Continue non-application output in case we have a TLS protocol error to send to peer. |
| 193 |
char buf[256]; |
| 194 |
pn_tls_get_session_error_string(jc->tls, buf, sizeof(buf)); |
| 195 |
fprintf(stderr, "TLS processing error: %s\n", buf); |
| 196 |
fprintf(stderr, "Jabber %s connection terminated.\n", jc->is_server ? "server" : "client"); |
| 197 |
pn_raw_connection_read_close(jc->rawc); |
| 198 |
jc->tls_has_output = pn_tls_is_encrypt_output_pending(jc->tls) > 0; |
| 199 |
return false; |
| 200 |
} |
| 201 |
return true; |
| 202 |
} |
| 203 |
|
| 204 |
// TLS close requires best efforts to send final closure or error alert record. |
| 205 |
// Peer is not obligated to wait for it. |
| 206 |
static void jabber_tls_begin_close(jabber_connection_t* jc) { |
| 207 |
if (!jc->tls_closing) { |
| 208 |
jc->tls_closing = true; |
| 209 |
pn_tls_close_output(jc->tls); |
| 210 |
jc->need_rawc_write_close = true; // Remember to eventually close the raw connection. |
| 211 |
pn_tls_process(jc->tls); // Best efforts. No error check. |
| 212 |
pn_raw_connection_wake(jc->rawc); // Ensure handle_outgoing is called. |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
static void check_alpn(jabber_connection_t* jc) { |
| 217 |
const char *self = jc->is_server ? "server" : "client"; |
| 218 |
|
| 219 |
if (!jc->alpn_protocol) { |
| 220 |
const char *protocol_name; |
| 221 |
size_t len; |
| 222 |
if (pn_tls_get_alpn_protocol(jc->tls, &protocol_name, &len)) { |
| 223 |
jc->alpn_protocol = (char *) malloc(len+1); |
| 224 |
assert(jc->alpn_protocol); |
| 225 |
memmove(jc->alpn_protocol, protocol_name, len); |
| 226 |
jc->alpn_protocol[len] = 0; |
| 227 |
printf("**%s: using ALPN protocol %s\n", self, jc->alpn_protocol); |
| 228 |
} else { |
| 229 |
printf("**%s: no available ALPN protocol\n", self); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
static void load_alpn_strings(pn_tls_config_t *cli_domain, pn_tls_config_t *srv_domain) { |
| 235 |
const char *protos[4]; |
| 236 |
// server... |
| 237 |
protos[0] = "jibberjabber"; |
| 238 |
protos[1] = "jabber/v1"; // expected winner |
| 239 |
protos[2] = "piglatin"; |
| 240 |
pn_tls_config_set_alpn_protocols(srv_domain, protos, 3); |
| 241 |
//client |
| 242 |
protos[0] = "jabber/v2"; |
| 243 |
protos[1] = "piglatin"; |
| 244 |
protos[2] = "jabber/v1"; // expected winner |
| 245 |
protos[3] = "ghost"; |
| 246 |
pn_tls_config_set_alpn_protocols(cli_domain, protos, 4); |
| 247 |
} |
| 248 |
|
| 249 |
static void handle_outgoing(jabber_connection_t* jc) { |
| 250 |
// Handle here as much outgoing data as possible. Limits include how much |
| 251 |
// data is available to send, how much data the raw_connection can accept |
| 252 |
// before blocking, and if TLS is involved, how much TLS data can be produced |
| 253 |
// before running out of result buffers. |
| 254 |
// |
| 255 |
// For this simplified example, max 4 buffers of application output are dealt |
| 256 |
// with at a time and "enough" TLS result buffers are always available from |
| 257 |
// the buffer pool. |
| 258 |
|
| 259 |
// Do accounting for previous raw connection writes and make room for new ones. |
| 260 |
jabber_t *j = jc->parent; |
| 261 |
pn_raw_buffer_t rbuf; |
| 262 |
while (1 == pn_raw_connection_take_written_buffers(jc->rawc, &rbuf, 1)) |
| 263 |
rbuf_pool_return(&rbuf); |
| 264 |
size_t max_wire_bufs = pn_raw_connection_write_buffers_capacity(jc->rawc); |
| 265 |
if (max_wire_bufs == 0) |
| 266 |
return; // Nothing to do until notified of future raw connection write completion |
| 267 |
|
| 268 |
if (!jc->jabber_turn && !jc->tls_has_output && !jc->need_rawc_write_close) |
| 269 |
return; // nothing to send at this time |
| 270 |
|
| 271 |
pn_raw_buffer_t wire_buffers[4]; // An arbitrary chunking size for this example |
| 272 |
size_t wire_buf_count = 0; |
| 273 |
|
| 274 |
if (!jc->tls) { |
| 275 |
// Initialize wire_buffers from pool and insert available application data. |
| 276 |
// max 4 wanted. Arbitrary decision for example. |
| 277 |
size_t max_bufs = size_t_min(max_wire_bufs, 4); |
| 278 |
rbuf_pool_get(wire_buffers, max_bufs); |
| 279 |
wire_buf_count = some_jabber(jc, wire_buffers, max_bufs); |
| 280 |
for (size_t tail = 4; tail > wire_buf_count; tail--) |
| 281 |
rbuf_pool_return(wire_buffers + tail - 1); // not used, back to pool. |
| 282 |
jc->jabber_turn = false; // Peer gets to do next jabber. |
| 283 |
|
| 284 |
pn_raw_connection_write_buffers(jc->rawc, wire_buffers, wire_buf_count); |
| 285 |
|
| 286 |
// If no more application data to write, start orderly close. |
| 287 |
if (j->current_jline == jlines_count) { |
| 288 |
pn_raw_connection_write_close(jc->rawc); |
| 289 |
jc->orderly_close_initiated = true; |
| 290 |
} |
| 291 |
} else { |
| 292 |
// TLS |
| 293 |
|
| 294 |
if (pn_tls_is_secure(jc->tls) && jc->jabber_turn && !jc->tls_closing) { |
| 295 |
assert(jc->alpn_protocol || !jc->parent->alpn_enabled); |
| 296 |
// Add jabber data if there is room. |
| 297 |
size_t max_bufs_to_encrypt = size_t_min(4, pn_tls_get_encrypt_input_buffer_capacity(jc->tls)); |
| 298 |
if (max_bufs_to_encrypt) { |
| 299 |
pn_raw_buffer_t unencrypted_buffers[4]; |
| 300 |
rbuf_pool_get(unencrypted_buffers, max_bufs_to_encrypt); |
| 301 |
size_t unencrypted_buf_count = some_jabber(jc, unencrypted_buffers, max_bufs_to_encrypt); |
| 302 |
for (size_t tail = max_bufs_to_encrypt; tail > unencrypted_buf_count; tail--) |
| 303 |
rbuf_pool_return(unencrypted_buffers + tail - 1); // not used, back to pool. |
| 304 |
jc->jabber_turn = false; // Peer gets to do next jabber. |
| 305 |
size_t consumed = pn_tls_give_encrypt_input_buffers(jc->tls, unencrypted_buffers, unencrypted_buf_count); |
| 306 |
if (consumed != unencrypted_buf_count) abort(); // Our careful counting was meant to prevent this. |
| 307 |
|
| 308 |
// If no more application data to write, start orderly close. |
| 309 |
// For TLS, indicate we are done writing to generate the protocol's EOS (closure alert). |
| 310 |
if (j->current_jline == jlines_count) { |
| 311 |
jc->orderly_close_initiated = true; |
| 312 |
jabber_tls_begin_close(jc); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
// Even if no application output (i.e. call to pn_tls_give_encrypt_input_buffers()), |
| 318 |
// there may be encrypt result buffers. |
| 319 |
|
| 320 |
if (!jabber_tls_process(jc)) |
| 321 |
return; |
| 322 |
for ( ; wire_buf_count < 4 |
| 323 |
&& pn_raw_connection_write_buffers_capacity(jc->rawc) > (wire_buf_count) |
| 324 |
&& pn_tls_need_encrypt_output_buffers(jc->tls) ; |
| 325 |
wire_buf_count++ ) { |
| 326 |
|
| 327 |
rbuf_pool_get(&rbuf, 1); |
| 328 |
assert(pn_tls_get_encrypt_output_buffer_capacity(jc->tls) > 0); |
| 329 |
pn_tls_give_encrypt_output_buffers(jc->tls, &rbuf, 1); |
| 330 |
if (!jabber_tls_process(jc)) |
| 331 |
return; |
| 332 |
if (pn_tls_take_encrypt_output_buffers(jc->tls, wire_buffers + wire_buf_count, 1) != 1) |
| 333 |
abort(); |
| 334 |
} |
| 335 |
|
| 336 |
// Reacquire and release encryption buffers that TLS library is done with. |
| 337 |
while (pn_tls_take_encrypt_input_buffers(jc->tls, &rbuf, 1) == 1) |
| 338 |
rbuf_pool_return(&rbuf); |
| 339 |
|
| 340 |
// Write the application data we just encrypted. |
| 341 |
pn_raw_connection_write_buffers(jc->rawc, wire_buffers, wire_buf_count); |
| 342 |
|
| 343 |
// Remember whether we missed some output buffered in the TLS library and need to come back. |
| 344 |
jc->tls_has_output = pn_tls_is_encrypt_output_pending(jc->tls) > 0; |
| 345 |
|
| 346 |
if (jc->need_rawc_write_close && !jc->tls_has_output) { |
| 347 |
// We have written to the raw connection all the output the TLS library can give us. |
| 348 |
pn_raw_connection_write_close(jc->rawc); |
| 349 |
jc->need_rawc_write_close = false; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
} |
| 354 |
|
| 355 |
static void handle_incoming(jabber_connection_t* jc, bool rawc_input_closed_event) { |
| 356 |
bool done = false; |
| 357 |
pn_raw_buffer_t wire_buffers[4]; |
| 358 |
pn_raw_buffer_t rbuf; |
| 359 |
bool rawc_input_closed = rawc_input_closed_event; |
| 360 |
const char *self = jc->is_server ? "server" : "client"; |
| 361 |
|
| 362 |
while (!done) { |
| 363 |
int max_bufs = 4; |
| 364 |
if (jc->tls) { |
| 365 |
// Don't read more buffers than the TLS lib will accept. |
| 366 |
if (max_bufs > pn_tls_get_decrypt_input_buffer_capacity(jc->tls)) |
| 367 |
max_bufs = pn_tls_get_decrypt_input_buffer_capacity(jc->tls); |
| 368 |
// Since we are always extracting, there should always be room |
| 369 |
assert(max_bufs > 0); |
| 370 |
} |
| 371 |
|
| 372 |
size_t buf_count = pn_raw_connection_take_read_buffers(jc->rawc, wire_buffers, max_bufs); |
| 373 |
if (buf_count < 4) |
| 374 |
done = true; |
| 375 |
|
| 376 |
if (buf_count) { |
| 377 |
|
| 378 |
if (!jc->tls) { |
| 379 |
// Use wire data directly |
| 380 |
for (size_t i = 0; i < buf_count; i++) { |
| 381 |
if (wire_buffers[i].size > 0) |
| 382 |
gobble_jabber(jc, wire_buffers + i); // buffer ownership transferred to application |
| 383 |
else { |
| 384 |
// size == 0 implies orderly close on the raw connection |
| 385 |
rawc_input_closed = true; |
| 386 |
if (!jc->orderly_close_initiated && jc->parent->current_jline == jlines_count) |
| 387 |
jc->orderly_close_detected = true; |
| 388 |
rbuf_pool_return(wire_buffers + i); |
| 389 |
} |
| 390 |
} |
| 391 |
} else { |
| 392 |
// TLS case |
| 393 |
if (wire_buffers[buf_count-1].size == 0) { |
| 394 |
rawc_input_closed = true; |
| 395 |
rbuf_pool_return(&wire_buffers[buf_count-1]); |
| 396 |
buf_count -= 1; |
| 397 |
} |
| 398 |
|
| 399 |
if (buf_count) { |
| 400 |
size_t consumed = pn_tls_give_decrypt_input_buffers(jc->tls, wire_buffers, buf_count); |
| 401 |
if (consumed != buf_count) { |
| 402 |
if (consumed == 0 && pn_tls_is_input_closed(jc->tls)) { |
| 403 |
// Library will not process anything after TLS EOS and we are not expecting any. |
| 404 |
printf("data after TLS EOF\n"); |
| 405 |
} |
| 406 |
else // Should never happen if we counted correctly. |
| 407 |
printf("Decryption input buffer failure\n"); |
| 408 |
abort(); |
| 409 |
} |
| 410 |
|
| 411 |
if (!jabber_tls_process(jc)) |
| 412 |
return; |
| 413 |
|
| 414 |
if (jc->parent->alpn_enabled && !jc->alpn_protocol) { |
| 415 |
check_alpn(jc); |
| 416 |
} |
| 417 |
|
| 418 |
while (pn_tls_need_decrypt_output_buffers(jc->tls)) { |
| 419 |
rbuf_pool_get(&rbuf, 1); |
| 420 |
assert(pn_tls_get_decrypt_output_buffer_capacity(jc->tls) > 0); |
| 421 |
pn_tls_give_decrypt_output_buffers(jc->tls, &rbuf, 1); |
| 422 |
if (!jabber_tls_process(jc)) |
| 423 |
return; |
| 424 |
size_t decrypted_count = pn_tls_take_decrypt_output_buffers(jc->tls, &rbuf, 1); |
| 425 |
if (decrypted_count != 1) |
| 426 |
abort(); |
| 427 |
gobble_jabber(jc, &rbuf); // buffer ownership transferred to application |
| 428 |
} |
| 429 |
|
| 430 |
// Reclaim and recycle the TLS encoded input buffers (from the wire) processed by the TLS library |
| 431 |
while (pn_tls_take_decrypt_input_buffers(jc->tls, &rbuf, 1) == 1) |
| 432 |
rbuf_pool_return(&rbuf); |
| 433 |
} |
| 434 |
|
| 435 |
if (pn_tls_is_input_closed(jc->tls) && !jc->orderly_close_initiated) { |
| 436 |
printf("**%s: received TLS end of session notification from peer\n", self); |
| 437 |
jc->orderly_close_detected = true; |
| 438 |
jabber_tls_begin_close(jc); |
| 439 |
} |
| 440 |
// TLS input can generate non-application output. Note that here. |
| 441 |
jc->tls_has_output = pn_tls_is_encrypt_output_pending(jc->tls) > 0; |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
if (rawc_input_closed) { |
| 447 |
// Whether unexpected hangup or expected EOS, Jabber has no more data to send. |
| 448 |
if (!jc->tls) { |
| 449 |
pn_raw_connection_close(jc->rawc); |
| 450 |
} |
| 451 |
else { |
| 452 |
// TLS case |
| 453 |
jabber_tls_begin_close(jc); |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
static void create_client_connection(jabber_t *j) { |
| 460 |
char addr[PN_MAX_ADDR]; |
| 461 |
pn_raw_connection_t *c = pn_raw_connection(); |
| 462 |
jabber_connection_t *jc = calloc(sizeof(*jc), 1); |
| 463 |
jc->rawc = c; |
| 464 |
jc->is_server = false; |
| 465 |
jc->jabber_turn = true; // Client goes first for purposes of this example. Arbitrary choice. |
| 466 |
jc->parent = j; |
| 467 |
pn_raw_connection_set_context(c, (void *) jc); |
| 468 |
|
| 469 |
// Client side TLS can be set up anywhere between here and first write (TLS clienthello). |
| 470 |
if (j->cli_domain) { |
| 471 |
jc->tls = pn_tls(j->cli_domain); |
| 472 |
pn_tls_set_peer_hostname(jc->tls, "test_server"); |
| 473 |
pn_tls_start(jc->tls); |
| 474 |
jc->tls_has_output = true; // always true for initial client side TLS. |
| 475 |
} |
| 476 |
|
| 477 |
pn_proactor_addr(addr, sizeof(addr), j->host, j->port); |
| 478 |
pn_proactor_raw_connect(j->proactor, c, addr); |
| 479 |
} |
| 480 |
|
| 481 |
static void create_server_connection(jabber_t *j, pn_listener_t *listener) { |
| 482 |
pn_raw_connection_t *c = pn_raw_connection(); |
| 483 |
jabber_connection_t *jc = calloc(sizeof(*jc), 1); |
| 484 |
jc->rawc = c; |
| 485 |
jc->is_server = true; |
| 486 |
jc->parent = j; |
| 487 |
pn_raw_connection_set_context(c, (void *) jc); |
| 488 |
|
| 489 |
// Server side TLS can be set up anywhere between here and first read (TLS clienthello). |
| 490 |
if (j->srv_domain) { |
| 491 |
jc->tls = pn_tls(j->srv_domain); |
| 492 |
pn_tls_start(jc->tls); |
| 493 |
} |
| 494 |
pn_listener_raw_accept(listener, c); |
| 495 |
printf("**listener accepted %p\n", (void *)jc); |
| 496 |
pn_listener_close(j->listener); // Single client. |
| 497 |
} |
| 498 |
|
| 499 |
// Called on disconnect. No more raw connection IO in either direction. |
| 500 |
static void tls_cleanup(jabber_connection_t* jc) { |
| 501 |
if (jc->tls) { |
| 502 |
pn_tls_stop(jc->tls); |
| 503 |
pn_raw_buffer_t rb; |
| 504 |
// recycle unused result buffers, released by pn_tls_stop() |
| 505 |
while (pn_tls_take_encrypt_output_buffers(jc->tls, &rb, 1) == 1) |
| 506 |
rbuf_pool_return(&rb); |
| 507 |
while (pn_tls_take_decrypt_output_buffers(jc->tls, &rb, 1) == 1) |
| 508 |
rbuf_pool_return(&rb); |
| 509 |
pn_tls_free(jc->tls); |
| 510 |
jc->tls = NULL; |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
static bool handle_raw_connection(jabber_connection_t* jc, pn_event_t* event) { |
| 515 |
switch (pn_event_type(event)) { |
| 516 |
|
| 517 |
case PN_RAW_CONNECTION_CONNECTED: { |
| 518 |
printf("**raw connection established %p %s\n", (void *)jc, jc->is_server ? "server" : "client"); |
| 519 |
jc->connecting = false; |
| 520 |
} break; |
| 521 |
|
| 522 |
case PN_RAW_CONNECTION_NEED_READ_BUFFERS: { |
| 523 |
pn_raw_buffer_t buffers[4]; |
| 524 |
rbuf_pool_get(buffers, 4); |
| 525 |
size_t n = pn_raw_connection_give_read_buffers(jc->rawc, buffers, 4); |
| 526 |
if (n != 4) abort(); |
| 527 |
} break; |
| 528 |
|
| 529 |
case PN_RAW_CONNECTION_CLOSED_WRITE: { |
| 530 |
pn_raw_connection_close(jc->rawc); // In case not fully closed |
| 531 |
} break; |
| 532 |
|
| 533 |
case PN_RAW_CONNECTION_WRITTEN: |
| 534 |
case PN_RAW_CONNECTION_NEED_WRITE_BUFFERS: |
| 535 |
case PN_RAW_CONNECTION_WAKE: { |
| 536 |
handle_outgoing(jc); |
| 537 |
} break; |
| 538 |
|
| 539 |
case PN_RAW_CONNECTION_CLOSED_READ: |
| 540 |
case PN_RAW_CONNECTION_READ: { |
| 541 |
handle_incoming(jc, pn_event_type(event) == PN_RAW_CONNECTION_CLOSED_READ); |
| 542 |
if (jc->tls_has_output) |
| 543 |
handle_outgoing(jc); |
| 544 |
} break; |
| 545 |
|
| 546 |
case PN_RAW_CONNECTION_DRAIN_BUFFERS: { |
| 547 |
pn_raw_buffer_t rbuf; |
| 548 |
while (1 == pn_raw_connection_take_read_buffers(jc->rawc, &rbuf, 1)) { |
| 549 |
rbuf_pool_return(&rbuf); |
| 550 |
} |
| 551 |
while (1 == pn_raw_connection_take_written_buffers(jc->rawc, &rbuf, 1)) { |
| 552 |
rbuf_pool_return(&rbuf); |
| 553 |
} |
| 554 |
} break; |
| 555 |
|
| 556 |
case PN_RAW_CONNECTION_DISCONNECTED: { |
| 557 |
const char *self = jc->is_server ? "Server" : "Client"; |
| 558 |
pn_condition_t *cond = pn_raw_connection_condition(jc->rawc); |
| 559 |
if (jc->connecting) |
| 560 |
printf("**Connection failed\n"); |
| 561 |
else { |
| 562 |
if (!(jc->orderly_close_initiated || jc->orderly_close_detected) || pn_condition_is_set(cond)) |
| 563 |
printf("**%s connection terminated abnormally\n", self); |
| 564 |
else if (jc->orderly_close_detected && jc->parent->current_jline != jlines_count) |
| 565 |
printf("**Jabber completed successfully\n", self); |
| 566 |
} |
| 567 |
if (pn_condition_is_set(cond)) { |
| 568 |
fprintf(stderr, "raw connection failure: %s\n", pn_condition_get_name(cond), pn_condition_get_description(cond)); |
| 569 |
} |
| 570 |
|
| 571 |
if (jc->tls) { |
| 572 |
if (jc->need_rawc_write_close) |
| 573 |
printf("**%s connection terminated with unsent TLS data\n", self); |
| 574 |
tls_cleanup(jc); |
| 575 |
} |
| 576 |
} break; |
| 577 |
} |
| 578 |
return true; |
| 579 |
} |
| 580 |
|
| 581 |
static bool handle(jabber_t* j, pn_event_t* event) { |
| 582 |
switch (pn_event_type(event)) { |
| 583 |
|
| 584 |
case PN_LISTENER_OPEN: { |
| 585 |
create_client_connection(j); |
| 586 |
break; |
| 587 |
} |
| 588 |
case PN_LISTENER_ACCEPT: { |
| 589 |
pn_listener_t *listener = pn_event_listener(event); |
| 590 |
create_server_connection(j, listener); |
| 591 |
} break; |
| 592 |
|
| 593 |
case PN_PROACTOR_INACTIVE: |
| 594 |
printf("**proactor inactive: connections and listeners finalized\n"); |
| 595 |
// fall through |
| 596 |
case PN_PROACTOR_INTERRUPT: { |
| 597 |
pn_proactor_t *proactor = pn_event_proactor(event); |
| 598 |
pn_proactor_interrupt(proactor); |
| 599 |
return false; |
| 600 |
} break; |
| 601 |
|
| 602 |
default: { |
| 603 |
pn_raw_connection_t *c = pn_event_raw_connection(event); |
| 604 |
if (c) { |
| 605 |
jabber_connection_t *jc = (jabber_connection_t *) pn_raw_connection_get_context(c); |
| 606 |
handle_raw_connection(jc, event); |
| 607 |
} |
| 608 |
} |
| 609 |
} |
| 610 |
return true; |
| 611 |
} |
| 612 |
|
| 613 |
static void* j_thread(void *void_j) { |
| 614 |
jabber_t *j = (jabber_t*)void_j; |
| 615 |
bool finished = false; |
| 616 |
do { |
| 617 |
pn_event_batch_t *events = pn_proactor_wait(j->proactor); |
| 618 |
pn_event_t *e; |
| 619 |
while ((e = pn_event_batch_next(events))) { |
| 620 |
if (!handle(j, e)) finished = true; |
| 621 |
} |
| 622 |
pn_proactor_done(j->proactor, events); |
| 623 |
} while(!finished); |
| 624 |
return NULL; |
| 625 |
} |
| 626 |
|
| 627 |
// main is from broker.c with switch to raw connections versus AMQP connections. |
| 628 |
int main(int argc, char **argv) { |
| 629 |
int err; |
| 630 |
srand(time(NULL)); |
| 631 |
jabber_t j = {0}; |
| 632 |
j.host = (argc > 1) ? argv[1] : ""; |
| 633 |
j.port = (argc > 2) ? argv[2] : "15243"; // our default Jabberwock address |
| 634 |
j.listener = pn_listener(); |
| 635 |
bool use_tls = true; |
| 636 |
if (argc > 3 && strncmp(argv[3], "no_tls", 6) == 0) |
| 637 |
use_tls = false; |
| 638 |
j.alpn_enabled = use_tls; // make configurable |
| 639 |
|
| 640 |
j.proactor = pn_proactor(); |
| 641 |
j.threads = 4; |
| 642 |
if (use_tls) { |
| 643 |
j.srv_domain = pn_tls_config(PN_TLS_MODE_SERVER); |
| 644 |
if (SET_CREDENTIALS(j.srv_domain, "tserver") != 0) { |
| 645 |
printf("Failed to set up server certificate: %s, private key: %s\n", CERTIFICATE("tserver"), SSL_FILE("tserver-private-key.pem")); |
| 646 |
exit(1); |
| 647 |
} |
| 648 |
j.cli_domain = pn_tls_config(PN_TLS_MODE_CLIENT); // PN_TLS_VERIFY_PEER_NAME by default |
| 649 |
if (pn_tls_config_set_trusted_certs(j.cli_domain, CERTIFICATE("tserver")) != 0) { |
| 650 |
printf("CA failure\n"); |
| 651 |
exit(1); |
| 652 |
} |
| 653 |
|
| 654 |
if (j.alpn_enabled) { |
| 655 |
// Set some ALPN values for each peer. |
| 656 |
load_alpn_strings(j.cli_domain, j.srv_domain); |
| 657 |
} |
| 658 |
} |
| 659 |
{ |
| 660 |
/* Listen on addr */ |
| 661 |
char addr[PN_MAX_ADDR]; |
| 662 |
pn_proactor_addr(addr, sizeof(addr), j.host, j.port); |
| 663 |
pn_proactor_listen(j.proactor, j.listener, addr, 16); |
| 664 |
} |
| 665 |
|
| 666 |
{ |
| 667 |
/* Start n-1 threads */ |
| 668 |
pthread_t* threads = (pthread_t*)calloc(sizeof(pthread_t), j.threads); |
| 669 |
size_t i; |
| 670 |
for (i = 0; i < j.threads-1; ++i) { |
| 671 |
pthread_create(&threads[i], NULL, j_thread, &j); |
| 672 |
} |
| 673 |
j_thread(&j); /* Use the main thread too. */ |
| 674 |
/* Join the other threads */ |
| 675 |
for (i = 0; i < j.threads-1; ++i) { |
| 676 |
pthread_join(threads[i], NULL); |
| 677 |
} |
| 678 |
pn_proactor_free(j.proactor); |
| 679 |
free(threads); |
| 680 |
if (j.srv_domain) |
| 681 |
pn_tls_config_free(j.srv_domain); |
| 682 |
if (j.cli_domain) |
| 683 |
pn_tls_config_free(j.cli_domain); |
| 684 |
return 0; |
| 685 |
} |
| 686 |
} |