| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
> |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "asterisk.h" |
| 20 |
|
| 21 |
#include "asterisk/iostream.h" |
| 22 |
|
| 23 |
#include <fcntl.h> |
| 24 |
#ifdef DO_SSL |
| 25 |
#include <openssl/err.h> |
| 26 |
#include <openssl/opensslv.h> |
| 27 |
#include <openssl/ssl.h> |
| 28 |
#endif |
| 29 |
#include <sys/socket.h> |
| 30 |
#include <sys/time.h> |
| 31 |
|
| 32 |
#include "asterisk/astobj2.h" |
| 33 |
#include "asterisk/logger.h" |
| 34 |
#include "asterisk/strings.h" |
| 35 |
#include "asterisk/threadstorage.h" |
| 36 |
#include "asterisk/time.h" |
| 37 |
#include "asterisk/utils.h" |
| 38 |
|
| 39 |
struct ast_iostream { |
| 40 |
SSL *ssl; |
| 41 |
struct timeval start; |
| 42 |
int fd; |
| 43 |
int timeout; |
| 44 |
int timeout_reset; |
| 45 |
int exclusive_input; |
| 46 |
int rbuflen; |
| 47 |
char *rbufhead; |
| 48 |
char rbuf[2048]; |
| 49 |
char *sni_hostname; |
| 50 |
}; |
| 51 |
|
| 52 |
#if defined(DO_SSL) |
| 53 |
AST_THREADSTORAGE(err2str_threadbuf); |
| 54 |
#define ERR2STR_BUFSIZE 128 |
| 55 |
|
| 56 |
static const char *ssl_error_to_string(int sslerr, int ret) |
| 57 |
{ |
| 58 |
switch (sslerr) { |
| 59 |
case SSL_ERROR_SSL: |
| 60 |
return "Internal SSL error"; |
| 61 |
case SSL_ERROR_SYSCALL: |
| 62 |
if (!ret) { |
| 63 |
return "System call EOF"; |
| 64 |
} else if (ret == -1) { |
| 65 |
char *buf; |
| 66 |
|
| 67 |
buf = ast_threadstorage_get(&err2str_threadbuf, ERR2STR_BUFSIZE); |
| 68 |
if (!buf) { |
| 69 |
return "Unknown"; |
| 70 |
} |
| 71 |
|
| 72 |
snprintf(buf, ERR2STR_BUFSIZE, "Underlying BIO error: %s", strerror(errno)); |
| 73 |
return buf; |
| 74 |
} else { |
| 75 |
return "System call other"; |
| 76 |
} |
| 77 |
default: |
| 78 |
break; |
| 79 |
} |
| 80 |
|
| 81 |
return "Unknown"; |
| 82 |
} |
| 83 |
#endif |
| 84 |
|
| 85 |
int ast_iostream_get_fd(struct ast_iostream *stream) |
| 86 |
{ |
| 87 |
return stream->fd; |
| 88 |
} |
| 89 |
|
| 90 |
int ast_iostream_wait_for_input(struct ast_iostream *stream, int timeout) |
| 91 |
{ |
| 92 |
#if defined(DO_SSL) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
if (stream->ssl && SSL_pending(stream->ssl)) { |
| 98 |
return 1; |
| 99 |
} |
| 100 |
#endif |
| 101 |
return ast_wait_for_input(stream->fd, timeout); |
| 102 |
} |
| 103 |
|
| 104 |
void ast_iostream_nonblock(struct ast_iostream *stream) |
| 105 |
{ |
| 106 |
ast_fd_set_flags(stream->fd, O_NONBLOCK); |
| 107 |
} |
| 108 |
|
| 109 |
SSL *ast_iostream_get_ssl(struct ast_iostream *stream) |
| 110 |
{ |
| 111 |
return stream->ssl; |
| 112 |
} |
| 113 |
|
| 114 |
void ast_iostream_set_timeout_disable(struct ast_iostream *stream) |
| 115 |
{ |
| 116 |
ast_assert(stream != NULL); |
| 117 |
|
| 118 |
stream->timeout = -1; |
| 119 |
stream->timeout_reset = -1; |
| 120 |
} |
| 121 |
|
| 122 |
void ast_iostream_set_timeout_inactivity(struct ast_iostream *stream, int timeout) |
| 123 |
{ |
| 124 |
ast_assert(stream != NULL); |
| 125 |
|
| 126 |
stream->start.tv_sec = 0; |
| 127 |
stream->timeout = timeout; |
| 128 |
stream->timeout_reset = timeout; |
| 129 |
} |
| 130 |
|
| 131 |
void ast_iostream_set_timeout_idle_inactivity(struct ast_iostream *stream, int timeout, int timeout_reset) |
| 132 |
{ |
| 133 |
ast_assert(stream != NULL); |
| 134 |
|
| 135 |
stream->start.tv_sec = 0; |
| 136 |
stream->timeout = timeout; |
| 137 |
stream->timeout_reset = timeout_reset; |
| 138 |
} |
| 139 |
|
| 140 |
void ast_iostream_set_timeout_sequence(struct ast_iostream *stream, struct timeval start, int timeout) |
| 141 |
{ |
| 142 |
ast_assert(stream != NULL); |
| 143 |
|
| 144 |
stream->start = start; |
| 145 |
stream->timeout = timeout; |
| 146 |
stream->timeout_reset = timeout; |
| 147 |
} |
| 148 |
|
| 149 |
void ast_iostream_set_exclusive_input(struct ast_iostream *stream, int exclusive_input) |
| 150 |
{ |
| 151 |
ast_assert(stream != NULL); |
| 152 |
|
| 153 |
stream->exclusive_input = exclusive_input; |
| 154 |
} |
| 155 |
|
| 156 |
int ast_iostream_set_sni_hostname(struct ast_iostream *stream, const char *sni_hostname) |
| 157 |
{ |
| 158 |
ast_assert(stream != NULL); |
| 159 |
|
| 160 |
ast_free(stream->sni_hostname); |
| 161 |
stream->sni_hostname = ast_strdup(sni_hostname); |
| 162 |
|
| 163 |
return stream->sni_hostname ? 0 : -1; |
| 164 |
} |
| 165 |
|
| 166 |
static ssize_t iostream_read(struct ast_iostream *stream, void *buf, size_t size) |
| 167 |
{ |
| 168 |
struct timeval start; |
| 169 |
int ms; |
| 170 |
int res; |
| 171 |
|
| 172 |
if (stream->start.tv_sec) { |
| 173 |
start = stream->start; |
| 174 |
} else { |
| 175 |
start = ast_tvnow(); |
| 176 |
} |
| 177 |
|
| 178 |
#if defined(DO_SSL) |
| 179 |
if (stream->ssl) { |
| 180 |
for (;;) { |
| 181 |
int sslerr; |
| 182 |
char err[256]; |
| 183 |
res = SSL_read(stream->ssl, buf, size); |
| 184 |
if (0 < res) { |
| 185 |
|
| 186 |
stream->timeout = stream->timeout_reset; |
| 187 |
return res; |
| 188 |
} |
| 189 |
sslerr = SSL_get_error(stream->ssl, res); |
| 190 |
switch (sslerr) { |
| 191 |
case SSL_ERROR_ZERO_RETURN: |
| 192 |
|
| 193 |
ast_debug(1, "TLS clean shutdown alert reading data\n"); |
| 194 |
return 0; |
| 195 |
case SSL_ERROR_WANT_READ: |
| 196 |
if (!stream->exclusive_input) { |
| 197 |
|
| 198 |
errno = EAGAIN; |
| 199 |
return -1; |
| 200 |
} |
| 201 |
while ((ms = ast_remaining_ms(start, stream->timeout))) { |
| 202 |
res = ast_wait_for_input(stream->fd, ms); |
| 203 |
if (0 < res) { |
| 204 |
|
| 205 |
break; |
| 206 |
} |
| 207 |
if (res < 0) { |
| 208 |
if (errno == EINTR || errno == EAGAIN) { |
| 209 |
|
| 210 |
continue; |
| 211 |
} |
| 212 |
ast_debug(1, "TLS socket error waiting for read data: %s\n", |
| 213 |
strerror(errno)); |
| 214 |
return -1; |
| 215 |
} |
| 216 |
} |
| 217 |
break; |
| 218 |
case SSL_ERROR_WANT_WRITE: |
| 219 |
while ((ms = ast_remaining_ms(start, stream->timeout))) { |
| 220 |
res = ast_wait_for_output(stream->fd, ms); |
| 221 |
if (0 < res) { |
| 222 |
|
| 223 |
break; |
| 224 |
} |
| 225 |
if (res < 0) { |
| 226 |
if (errno == EINTR || errno == EAGAIN) { |
| 227 |
|
| 228 |
continue; |
| 229 |
} |
| 230 |
ast_debug(1, "TLS socket error waiting for write space: %s\n", |
| 231 |
strerror(errno)); |
| 232 |
return -1; |
| 233 |
} |
| 234 |
} |
| 235 |
break; |
| 236 |
case SSL_ERROR_SYSCALL: |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
ast_debug(1, "TLS non-recoverable I/O error occurred: %s, %s\n", ERR_error_string(sslerr, err), |
| 241 |
ssl_error_to_string(sslerr, res)); |
| 242 |
return -1; |
| 243 |
default: |
| 244 |
|
| 245 |
ast_debug(1, "TLS transport or SSL error reading data: %s, %s\n", ERR_error_string(sslerr, err), |
| 246 |
ssl_error_to_string(sslerr, res)); |
| 247 |
return -1; |
| 248 |
} |
| 249 |
if (!ms) { |
| 250 |
|
| 251 |
ast_debug(1, "TLS timeout reading data\n"); |
| 252 |
return 0; |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
#endif |
| 257 |
|
| 258 |
for (;;) { |
| 259 |
res = read(stream->fd, buf, size); |
| 260 |
if (0 <= res) { |
| 261 |
|
| 262 |
stream->timeout = stream->timeout_reset; |
| 263 |
return res; |
| 264 |
} |
| 265 |
if (!stream->exclusive_input) { |
| 266 |
return res; |
| 267 |
} |
| 268 |
if (errno != EINTR && errno != EAGAIN) { |
| 269 |
|
| 270 |
ast_debug(1, "TCP socket error reading data: %s\n", |
| 271 |
strerror(errno)); |
| 272 |
return -1; |
| 273 |
} |
| 274 |
ms = ast_remaining_ms(start, stream->timeout); |
| 275 |
if (!ms) { |
| 276 |
|
| 277 |
ast_debug(1, "TCP timeout reading data\n"); |
| 278 |
return 0; |
| 279 |
} |
| 280 |
ast_wait_for_input(stream->fd, ms); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
ssize_t ast_iostream_read(struct ast_iostream *stream, void *buffer, size_t count) |
| 285 |
{ |
| 286 |
if (!count) { |
| 287 |
|
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
if (!stream || stream->fd == -1) { |
| 292 |
errno = EBADF; |
| 293 |
return -1; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
if (stream->rbuflen) { |
| 298 |
size_t r = count; |
| 299 |
if (r > stream->rbuflen) { |
| 300 |
r = stream->rbuflen; |
| 301 |
} |
| 302 |
memcpy(buffer, stream->rbufhead, r); |
| 303 |
stream->rbuflen -= r; |
| 304 |
stream->rbufhead += r; |
| 305 |
return r; |
| 306 |
} |
| 307 |
|
| 308 |
return iostream_read(stream, buffer, count); |
| 309 |
} |
| 310 |
|
| 311 |
ssize_t ast_iostream_gets(struct ast_iostream *stream, char *buffer, size_t size) |
| 312 |
{ |
| 313 |
size_t remaining = size; |
| 314 |
ssize_t accum_size = 0; |
| 315 |
ssize_t len; |
| 316 |
char *newline; |
| 317 |
|
| 318 |
for (;;) { |
| 319 |
|
| 320 |
newline = memchr(stream->rbufhead, '\n', stream->rbuflen); |
| 321 |
if (newline) { |
| 322 |
len = newline - stream->rbufhead + 1; |
| 323 |
if (len > remaining - 1) { |
| 324 |
len = remaining - 1; |
| 325 |
} |
| 326 |
break; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
if (stream->rbuflen >= remaining - 1) { |
| 331 |
len = remaining - 1; |
| 332 |
break; |
| 333 |
} |
| 334 |
if (stream->rbuflen) { |
| 335 |
|
| 336 |
memcpy(buffer + accum_size, stream->rbufhead, stream->rbuflen); |
| 337 |
remaining -= stream->rbuflen; |
| 338 |
accum_size += stream->rbuflen; |
| 339 |
stream->rbuflen = 0; |
| 340 |
} |
| 341 |
stream->rbufhead = stream->rbuf; |
| 342 |
|
| 343 |
len = iostream_read(stream, stream->rbuf, sizeof(stream->rbuf)); |
| 344 |
if (len == 0) { |
| 345 |
|
| 346 |
break; |
| 347 |
} |
| 348 |
if (len < 0) { |
| 349 |
if (accum_size) { |
| 350 |
|
| 351 |
len = 0; |
| 352 |
break; |
| 353 |
} |
| 354 |
return len; |
| 355 |
} |
| 356 |
stream->rbuflen += len; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
memcpy(buffer + accum_size, stream->rbufhead, len); |
| 361 |
buffer[accum_size + len] = 0; |
| 362 |
stream->rbuflen -= len; |
| 363 |
stream->rbufhead += len; |
| 364 |
|
| 365 |
return accum_size + len; |
| 366 |
} |
| 367 |
|
| 368 |
ssize_t ast_iostream_discard(struct ast_iostream *stream, size_t size) |
| 369 |
{ |
| 370 |
char buf[1024]; |
| 371 |
size_t remaining = size; |
| 372 |
ssize_t ret; |
| 373 |
|
| 374 |
while (remaining) { |
| 375 |
ret = ast_iostream_read(stream, buf, remaining > sizeof(buf) ? sizeof(buf) : remaining); |
| 376 |
if (ret <= 0) { |
| 377 |
return ret; |
| 378 |
} |
| 379 |
remaining -= ret; |
| 380 |
} |
| 381 |
|
| 382 |
return size; |
| 383 |
} |
| 384 |
|
| 385 |
ssize_t ast_iostream_write(struct ast_iostream *stream, const void *buffer, size_t size) |
| 386 |
{ |
| 387 |
struct timeval start; |
| 388 |
int ms; |
| 389 |
int res; |
| 390 |
int written; |
| 391 |
int remaining; |
| 392 |
|
| 393 |
if (!size) { |
| 394 |
|
| 395 |
return 0; |
| 396 |
} |
| 397 |
|
| 398 |
if (!stream || stream->fd == -1) { |
| 399 |
errno = EBADF; |
| 400 |
return -1; |
| 401 |
} |
| 402 |
|
| 403 |
if (stream->start.tv_sec) { |
| 404 |
start = stream->start; |
| 405 |
} else { |
| 406 |
start = ast_tvnow(); |
| 407 |
} |
| 408 |
|
| 409 |
#if defined(DO_SSL) |
| 410 |
if (stream->ssl) { |
| 411 |
written = 0; |
| 412 |
remaining = size; |
| 413 |
for (;;) { |
| 414 |
int sslerr; |
| 415 |
char err[256]; |
| 416 |
res = SSL_write(stream->ssl, buffer + written, remaining); |
| 417 |
if (res == remaining) { |
| 418 |
|
| 419 |
return size; |
| 420 |
} |
| 421 |
if (0 < res) { |
| 422 |
|
| 423 |
written += res; |
| 424 |
remaining -= res; |
| 425 |
continue; |
| 426 |
} |
| 427 |
sslerr = SSL_get_error(stream->ssl, res); |
| 428 |
switch (sslerr) { |
| 429 |
case SSL_ERROR_ZERO_RETURN: |
| 430 |
ast_debug(1, "TLS clean shutdown alert writing data\n"); |
| 431 |
if (written) { |
| 432 |
|
| 433 |
return written; |
| 434 |
} |
| 435 |
errno = EBADF; |
| 436 |
return -1; |
| 437 |
case SSL_ERROR_WANT_READ: |
| 438 |
ms = ast_remaining_ms(start, stream->timeout); |
| 439 |
if (!ms) { |
| 440 |
|
| 441 |
ast_debug(1, "TLS timeout writing data (want read)\n"); |
| 442 |
return written; |
| 443 |
} |
| 444 |
ast_wait_for_input(stream->fd, ms); |
| 445 |
break; |
| 446 |
case SSL_ERROR_WANT_WRITE: |
| 447 |
ms = ast_remaining_ms(start, stream->timeout); |
| 448 |
if (!ms) { |
| 449 |
|
| 450 |
ast_debug(1, "TLS timeout writing data (want write)\n"); |
| 451 |
return written; |
| 452 |
} |
| 453 |
ast_wait_for_output(stream->fd, ms); |
| 454 |
break; |
| 455 |
default: |
| 456 |
|
| 457 |
ast_debug(1, "TLS transport or SSL error writing data: %s, %s\n", ERR_error_string(sslerr, err), |
| 458 |
ssl_error_to_string(sslerr, res)); |
| 459 |
if (written) { |
| 460 |
|
| 461 |
return written; |
| 462 |
} |
| 463 |
errno = EBADF; |
| 464 |
return -1; |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
#endif |
| 469 |
|
| 470 |
written = 0; |
| 471 |
remaining = size; |
| 472 |
for (;;) { |
| 473 |
res = write(stream->fd, buffer + written, remaining); |
| 474 |
if (res == remaining) { |
| 475 |
|
| 476 |
return size; |
| 477 |
} |
| 478 |
if (0 < res) { |
| 479 |
|
| 480 |
written += res; |
| 481 |
remaining -= res; |
| 482 |
continue; |
| 483 |
} |
| 484 |
if (errno != EINTR && errno != EAGAIN) { |
| 485 |
|
| 486 |
ast_debug(1, "TCP socket error writing: %s\n", strerror(errno)); |
| 487 |
if (written) { |
| 488 |
return written; |
| 489 |
} |
| 490 |
return -1; |
| 491 |
} |
| 492 |
ms = ast_remaining_ms(start, stream->timeout); |
| 493 |
if (!ms) { |
| 494 |
|
| 495 |
ast_debug(1, "TCP timeout writing data\n"); |
| 496 |
return written; |
| 497 |
} |
| 498 |
ast_wait_for_output(stream->fd, ms); |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
ssize_t ast_iostream_printf(struct ast_iostream *stream, const char *format, ...) |
| 503 |
{ |
| 504 |
char sbuf[512], *buf = sbuf; |
| 505 |
int len, len2, ret = -1; |
| 506 |
va_list va; |
| 507 |
|
| 508 |
va_start(va, format); |
| 509 |
len = vsnprintf(buf, sizeof(sbuf), format, va); |
| 510 |
va_end(va); |
| 511 |
|
| 512 |
if (len > sizeof(sbuf) - 1) { |
| 513 |
|
| 514 |
size_t buf_len = len + 1; |
| 515 |
|
| 516 |
buf = ast_malloc(buf_len); |
| 517 |
if (!buf) { |
| 518 |
return -1; |
| 519 |
} |
| 520 |
va_start(va, format); |
| 521 |
len2 = vsnprintf(buf, buf_len, format, va); |
| 522 |
va_end(va); |
| 523 |
if (len2 != len) { |
| 524 |
goto error; |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
if (ast_iostream_write(stream, buf, len) == len) |
| 529 |
ret = len; |
| 530 |
|
| 531 |
error: |
| 532 |
if (buf != sbuf) { |
| 533 |
ast_free(buf); |
| 534 |
} |
| 535 |
|
| 536 |
return ret; |
| 537 |
} |
| 538 |
|
| 539 |
int ast_iostream_close(struct ast_iostream *stream) |
| 540 |
{ |
| 541 |
if (!stream) { |
| 542 |
errno = EBADF; |
| 543 |
return -1; |
| 544 |
} |
| 545 |
|
| 546 |
if (stream->fd != -1) { |
| 547 |
#if defined(DO_SSL) |
| 548 |
if (stream->ssl) { |
| 549 |
int res; |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
res = SSL_shutdown(stream->ssl); |
| 560 |
if (res < 0) { |
| 561 |
int sslerr = SSL_get_error(stream->ssl, res); |
| 562 |
char err[256]; |
| 563 |
ast_log(LOG_ERROR, "SSL_shutdown() failed: %s, %s\n", |
| 564 |
ERR_error_string(sslerr, err), ssl_error_to_string(sslerr, res)); |
| 565 |
} |
| 566 |
|
| 567 |
#if !(defined(LIBRESSL_VERSION_NUMBER) && (LIBRESSL_VERSION_NUMBER < 0x2070000L)) && (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
| 568 |
if (!SSL_is_server(stream->ssl)) { |
| 569 |
#else |
| 570 |
if (!stream->ssl->server) { |
| 571 |
#endif |
| 572 |
|
| 573 |
#if defined(LIBRESSL_VERSION_NUMBER) || (OPENSSL_VERSION_NUMBER < 0x10100000L) |
| 574 |
#if OPENSSL_VERSION_NUMBER >= 0x10000000L |
| 575 |
ERR_remove_thread_state(NULL); |
| 576 |
#else |
| 577 |
ERR_remove_state(0); |
| 578 |
#endif |
| 579 |
#endif |
| 580 |
} |
| 581 |
|
| 582 |
SSL_free(stream->ssl); |
| 583 |
stream->ssl = NULL; |
| 584 |
} |
| 585 |
#endif |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
shutdown(stream->fd, SHUT_RDWR); |
| 593 |
if (close(stream->fd)) { |
| 594 |
ast_log(LOG_ERROR, "close() failed: %s\n", strerror(errno)); |
| 595 |
} |
| 596 |
stream->fd = -1; |
| 597 |
} |
| 598 |
ao2_t_ref(stream, -1, "Closed ast_iostream"); |
| 599 |
|
| 600 |
return 0; |
| 601 |
} |
| 602 |
|
| 603 |
static void iostream_dtor(void *cookie) |
| 604 |
{ |
| 605 |
struct ast_iostream *stream = cookie; |
| 606 |
|
| 607 |
ast_free(stream->sni_hostname); |
| 608 |
ast_assert(stream->fd == -1); |
| 609 |
} |
| 610 |
|
| 611 |
struct ast_iostream *ast_iostream_from_fd(int *fd) |
| 612 |
{ |
| 613 |
struct ast_iostream *stream; |
| 614 |
|
| 615 |
stream = ao2_alloc_options(sizeof(*stream), iostream_dtor, |
| 616 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 617 |
if (stream) { |
| 618 |
stream->timeout = -1; |
| 619 |
stream->timeout_reset = -1; |
| 620 |
stream->fd = *fd; |
| 621 |
*fd = -1; |
| 622 |
} |
| 623 |
|
| 624 |
return stream; |
| 625 |
} |
| 626 |
|
| 627 |
int ast_iostream_start_tls(struct ast_iostream **pstream, SSL_CTX *ssl_ctx, int client) |
| 628 |
{ |
| 629 |
#ifdef DO_SSL |
| 630 |
struct ast_iostream *stream = *pstream; |
| 631 |
int (*ssl_setup)(SSL *) = client ? SSL_connect : SSL_accept; |
| 632 |
int res; |
| 633 |
struct timeval rcv_timeout, snd_timeout; |
| 634 |
struct timeval timeout; |
| 635 |
socklen_t len; |
| 636 |
|
| 637 |
stream->ssl = SSL_new(ssl_ctx); |
| 638 |
if (!stream->ssl) { |
| 639 |
ast_log(LOG_ERROR, "Unable to create new SSL connection\n"); |
| 640 |
errno = ENOMEM; |
| 641 |
return -1; |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
SSL_set_fd(stream->ssl, stream->fd); |
| 651 |
|
| 652 |
if (client && !ast_strlen_zero(stream->sni_hostname)) { |
| 653 |
if (!SSL_set_tlsext_host_name(stream->ssl, stream->sni_hostname)) { |
| 654 |
ast_log(LOG_ERROR, "Unable to set SNI hostname '%s'\n", |
| 655 |
stream->sni_hostname); |
| 656 |
errno = EIO; |
| 657 |
return -1; |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
len = sizeof(rcv_timeout); |
| 663 |
getsockopt(stream->fd, SOL_SOCKET, SO_RCVTIMEO, &rcv_timeout, &len); |
| 664 |
len = sizeof(snd_timeout); |
| 665 |
getsockopt(stream->fd, SOL_SOCKET, SO_SNDTIMEO, &snd_timeout, &len); |
| 666 |
|
| 667 |
|
| 668 |
timeout.tv_sec = 30; |
| 669 |
timeout.tv_usec = 0; |
| 670 |
setsockopt(stream->fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); |
| 671 |
setsockopt(stream->fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); |
| 672 |
|
| 673 |
res = ssl_setup(stream->ssl); |
| 674 |
if (res <= 0) { |
| 675 |
int sslerr = SSL_get_error(stream->ssl, res); |
| 676 |
char err[256]; |
| 677 |
|
| 678 |
ast_log(LOG_ERROR, "Problem setting up ssl connection: %s, %s\n", |
| 679 |
ERR_error_string(sslerr, err), ssl_error_to_string(sslerr, res)); |
| 680 |
errno = EIO; |
| 681 |
return -1; |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
setsockopt(stream->fd, SOL_SOCKET, SO_RCVTIMEO, &rcv_timeout, sizeof(rcv_timeout)); |
| 686 |
setsockopt(stream->fd, SOL_SOCKET, SO_SNDTIMEO, &snd_timeout, sizeof(snd_timeout)); |
| 687 |
|
| 688 |
return 0; |
| 689 |
#else |
| 690 |
ast_log(LOG_ERROR, "SSL not enabled in this build\n"); |
| 691 |
errno = ENOTSUP; |
| 692 |
return -1; |
| 693 |
#endif |
| 694 |
} |