| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "asterisk/module.h" |
| 33 |
#include "asterisk/http.h" |
| 34 |
#include "asterisk/astobj2.h" |
| 35 |
#include "asterisk/strings.h" |
| 36 |
#include "asterisk/file.h" |
| 37 |
#include "asterisk/unaligned.h" |
| 38 |
#include "asterisk/uri.h" |
| 39 |
#include "asterisk/uuid.h" |
| 40 |
|
| 41 |
#define AST_API_MODULE |
| 42 |
#include "asterisk/http_websocket.h" |
| 43 |
|
| 44 |
|
| 45 |
#define WEBSOCKET_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" |
| 46 |
|
| 47 |
|
| 48 |
#define CLIENT_KEY_SIZE 16 |
| 49 |
|
| 50 |
|
| 51 |
#define MAX_PROTOCOL_BUCKETS 7 |
| 52 |
|
| 53 |
#ifdef LOW_MEMORY |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
#define DEFAULT_RECONSTRUCTION_CEILING AST_WEBSOCKET_MAX_RX_PAYLOAD_SIZE |
| 58 |
|
| 59 |
|
| 60 |
#define MAXIMUM_RECONSTRUCTION_CEILING AST_WEBSOCKET_MAX_RX_PAYLOAD_SIZE |
| 61 |
#else |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#define DEFAULT_RECONSTRUCTION_CEILING AST_WEBSOCKET_MAX_RX_PAYLOAD_SIZE |
| 66 |
|
| 67 |
|
| 68 |
#define MAXIMUM_RECONSTRUCTION_CEILING AST_WEBSOCKET_MAX_RX_PAYLOAD_SIZE |
| 69 |
#endif |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
#define MAX_WS_HDR_SZ 14 |
| 79 |
#define MIN_WS_HDR_SZ 2 |
| 80 |
|
| 81 |
|
| 82 |
struct ast_websocket { |
| 83 |
struct ast_iostream *stream; |
| 84 |
struct ast_sockaddr remote_address; |
| 85 |
struct ast_sockaddr local_address; |
| 86 |
enum ast_websocket_opcode opcode; |
| 87 |
size_t payload_len; |
| 88 |
char *payload; |
| 89 |
size_t reconstruct; |
| 90 |
int timeout; |
| 91 |
unsigned int secure:1; |
| 92 |
unsigned int closing:1; |
| 93 |
unsigned int close_sent:1; |
| 94 |
struct websocket_client *client; |
| 95 |
char session_id[AST_UUID_STR_LEN]; |
| 96 |
uint16_t close_status_code; |
| 97 |
char buf[AST_WEBSOCKET_MAX_RX_PAYLOAD_SIZE]; |
| 98 |
}; |
| 99 |
|
| 100 |
const char *ast_websocket_type_to_str(enum ast_websocket_type type) |
| 101 |
{ |
| 102 |
switch (type) { |
| 103 |
case AST_WS_TYPE_CLIENT_PERSISTENT: |
| 104 |
return "persistent"; |
| 105 |
case AST_WS_TYPE_CLIENT_PER_CALL: |
| 106 |
return "per_call"; |
| 107 |
case AST_WS_TYPE_CLIENT_PER_CALL_CONFIG: |
| 108 |
return "per_call_config"; |
| 109 |
case AST_WS_TYPE_CLIENT: |
| 110 |
return "client"; |
| 111 |
case AST_WS_TYPE_INBOUND: |
| 112 |
return "inbound"; |
| 113 |
case AST_WS_TYPE_SERVER: |
| 114 |
return "server"; |
| 115 |
case AST_WS_TYPE_ANY: |
| 116 |
return "any"; |
| 117 |
default: |
| 118 |
return "unknown"; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
static int protocol_hash_fn(const void *obj, const int flags) |
| 124 |
{ |
| 125 |
const struct ast_websocket_protocol *protocol = obj; |
| 126 |
const char *name = obj; |
| 127 |
|
| 128 |
return ast_str_case_hash(flags & OBJ_KEY ? name : protocol->name); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
static int protocol_cmp_fn(void *obj, void *arg, int flags) |
| 133 |
{ |
| 134 |
const struct ast_websocket_protocol *protocol1 = obj, *protocol2 = arg; |
| 135 |
const char *protocol = arg; |
| 136 |
|
| 137 |
return !strcasecmp(protocol1->name, flags & OBJ_KEY ? protocol : protocol2->name) ? CMP_MATCH | CMP_STOP : 0; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
static void protocol_destroy_fn(void *obj) |
| 142 |
{ |
| 143 |
struct ast_websocket_protocol *protocol = obj; |
| 144 |
ast_free(protocol->name); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
struct ast_websocket_server { |
| 149 |
struct ao2_container *protocols; |
| 150 |
}; |
| 151 |
|
| 152 |
static void websocket_server_dtor(void *obj) |
| 153 |
{ |
| 154 |
struct ast_websocket_server *server = obj; |
| 155 |
ao2_cleanup(server->protocols); |
| 156 |
server->protocols = NULL; |
| 157 |
} |
| 158 |
|
| 159 |
static struct ast_websocket_server *websocket_server_create_impl(void) |
| 160 |
{ |
| 161 |
RAII_VAR(struct ast_websocket_server *, server, NULL, ao2_cleanup); |
| 162 |
|
| 163 |
server = ao2_alloc(sizeof(*server), websocket_server_dtor); |
| 164 |
if (!server) { |
| 165 |
return NULL; |
| 166 |
} |
| 167 |
|
| 168 |
server->protocols = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 169 |
MAX_PROTOCOL_BUCKETS, protocol_hash_fn, NULL, protocol_cmp_fn); |
| 170 |
if (!server->protocols) { |
| 171 |
return NULL; |
| 172 |
} |
| 173 |
|
| 174 |
ao2_ref(server, +1); |
| 175 |
return server; |
| 176 |
} |
| 177 |
|
| 178 |
static struct ast_websocket_server *websocket_server_internal_create(void) |
| 179 |
{ |
| 180 |
return websocket_server_create_impl(); |
| 181 |
} |
| 182 |
|
| 183 |
struct ast_websocket_server *AST_OPTIONAL_API_NAME(ast_websocket_server_create)(void) |
| 184 |
{ |
| 185 |
return websocket_server_create_impl(); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
static void session_destroy_fn(void *obj) |
| 190 |
{ |
| 191 |
struct ast_websocket *session = obj; |
| 192 |
|
| 193 |
if (session->stream) { |
| 194 |
ast_websocket_close(session, session->close_status_code); |
| 195 |
if (session->stream) { |
| 196 |
ast_iostream_close(session->stream); |
| 197 |
session->stream = NULL; |
| 198 |
ast_debug(3, "WebSocket connection %s '%s' closed\n", session->client ? "to" : "from", |
| 199 |
ast_sockaddr_stringify(&session->remote_address)); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
ao2_cleanup(session->client); |
| 204 |
ast_free(session->payload); |
| 205 |
} |
| 206 |
|
| 207 |
struct ast_websocket_protocol *AST_OPTIONAL_API_NAME(ast_websocket_sub_protocol_alloc)(const char *name) |
| 208 |
{ |
| 209 |
struct ast_websocket_protocol *protocol; |
| 210 |
|
| 211 |
protocol = ao2_alloc(sizeof(*protocol), protocol_destroy_fn); |
| 212 |
if (!protocol) { |
| 213 |
return NULL; |
| 214 |
} |
| 215 |
|
| 216 |
protocol->name = ast_strdup(name); |
| 217 |
if (!protocol->name) { |
| 218 |
ao2_ref(protocol, -1); |
| 219 |
return NULL; |
| 220 |
} |
| 221 |
protocol->version = AST_WEBSOCKET_PROTOCOL_VERSION; |
| 222 |
|
| 223 |
return protocol; |
| 224 |
} |
| 225 |
|
| 226 |
int AST_OPTIONAL_API_NAME(ast_websocket_server_add_protocol)(struct ast_websocket_server *server, const char *name, ast_websocket_callback callback) |
| 227 |
{ |
| 228 |
struct ast_websocket_protocol *protocol; |
| 229 |
|
| 230 |
if (!server->protocols) { |
| 231 |
return -1; |
| 232 |
} |
| 233 |
|
| 234 |
protocol = ast_websocket_sub_protocol_alloc(name); |
| 235 |
if (!protocol) { |
| 236 |
return -1; |
| 237 |
} |
| 238 |
protocol->session_established = callback; |
| 239 |
|
| 240 |
if (ast_websocket_server_add_protocol2(server, protocol)) { |
| 241 |
ao2_ref(protocol, -1); |
| 242 |
return -1; |
| 243 |
} |
| 244 |
|
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
int AST_OPTIONAL_API_NAME(ast_websocket_server_add_protocol2)(struct ast_websocket_server *server, struct ast_websocket_protocol *protocol) |
| 249 |
{ |
| 250 |
struct ast_websocket_protocol *existing; |
| 251 |
|
| 252 |
if (!server->protocols) { |
| 253 |
return -1; |
| 254 |
} |
| 255 |
|
| 256 |
if (protocol->version != AST_WEBSOCKET_PROTOCOL_VERSION) { |
| 257 |
ast_log(LOG_WARNING, "WebSocket could not register sub-protocol '%s': " |
| 258 |
"expected version '%u', got version '%u'\n", |
| 259 |
protocol->name, AST_WEBSOCKET_PROTOCOL_VERSION, protocol->version); |
| 260 |
return -1; |
| 261 |
} |
| 262 |
|
| 263 |
ao2_lock(server->protocols); |
| 264 |
|
| 265 |
|
| 266 |
existing = ao2_find(server->protocols, protocol->name, OBJ_KEY | OBJ_NOLOCK); |
| 267 |
if (existing) { |
| 268 |
ao2_ref(existing, -1); |
| 269 |
ao2_unlock(server->protocols); |
| 270 |
return -1; |
| 271 |
} |
| 272 |
|
| 273 |
ao2_link_flags(server->protocols, protocol, OBJ_NOLOCK); |
| 274 |
ao2_unlock(server->protocols); |
| 275 |
|
| 276 |
ast_debug(1, "WebSocket registered sub-protocol '%s'\n", protocol->name); |
| 277 |
ao2_ref(protocol, -1); |
| 278 |
|
| 279 |
return 0; |
| 280 |
} |
| 281 |
|
| 282 |
int AST_OPTIONAL_API_NAME(ast_websocket_server_remove_protocol)(struct ast_websocket_server *server, const char *name, ast_websocket_callback callback) |
| 283 |
{ |
| 284 |
struct ast_websocket_protocol *protocol; |
| 285 |
|
| 286 |
if (!(protocol = ao2_find(server->protocols, name, OBJ_KEY))) { |
| 287 |
return -1; |
| 288 |
} |
| 289 |
|
| 290 |
if (protocol->session_established != callback) { |
| 291 |
ao2_ref(protocol, -1); |
| 292 |
return -1; |
| 293 |
} |
| 294 |
|
| 295 |
ao2_unlink(server->protocols, protocol); |
| 296 |
ao2_ref(protocol, -1); |
| 297 |
|
| 298 |
ast_debug(1, "WebSocket unregistered sub-protocol '%s'\n", name); |
| 299 |
|
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
static void websocket_mask_payload(struct ast_websocket *session, char *frame, char *payload, uint64_t payload_size) |
| 305 |
{ |
| 306 |
|
| 307 |
if (session->client) { |
| 308 |
uint64_t i; |
| 309 |
uint8_t mask_key_idx; |
| 310 |
uint32_t mask_key = ast_random(); |
| 311 |
uint8_t length = frame[1] & 0x7f; |
| 312 |
frame[1] |= 0x80; |
| 313 |
|
| 314 |
mask_key_idx = length == 126 ? 4 : length == 127 ? 10 : 2; |
| 315 |
put_unaligned_uint32(&frame[mask_key_idx], mask_key); |
| 316 |
for (i = 0; i < payload_size; i++) { |
| 317 |
payload[i] ^= ((char *)&mask_key)[i % 4]; |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
int AST_OPTIONAL_API_NAME(ast_websocket_close)(struct ast_websocket *session, uint16_t reason) |
| 325 |
{ |
| 326 |
enum ast_websocket_opcode opcode = AST_WEBSOCKET_OPCODE_CLOSE; |
| 327 |
|
| 328 |
|
| 329 |
char frame[8] = { 0, }; |
| 330 |
int header_size, fsize, res; |
| 331 |
|
| 332 |
if (session->close_sent) { |
| 333 |
return 0; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
header_size = session->client ? 6 : 2; |
| 338 |
fsize = header_size + 2; |
| 339 |
|
| 340 |
frame[0] = opcode | 0x80; |
| 341 |
frame[1] = 2; |
| 342 |
|
| 343 |
|
| 344 |
put_unaligned_uint16(&frame[header_size], htons(reason ? reason : 1000)); |
| 345 |
|
| 346 |
websocket_mask_payload(session, frame, &frame[header_size], 2); |
| 347 |
|
| 348 |
session->closing = 1; |
| 349 |
session->close_sent = 1; |
| 350 |
|
| 351 |
ao2_lock(session); |
| 352 |
ast_iostream_set_timeout_inactivity(session->stream, session->timeout); |
| 353 |
res = ast_iostream_write(session->stream, frame, fsize); |
| 354 |
ast_iostream_set_timeout_disable(session->stream); |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
if (res != fsize) { |
| 360 |
ast_iostream_close(session->stream); |
| 361 |
session->stream = NULL; |
| 362 |
ast_verb(2, "WebSocket connection %s '%s' forcefully closed due to fatal write error\n", |
| 363 |
session->client ? "to" : "from", ast_sockaddr_stringify(&session->remote_address)); |
| 364 |
} |
| 365 |
|
| 366 |
ao2_unlock(session); |
| 367 |
return res == sizeof(frame); |
| 368 |
} |
| 369 |
|
| 370 |
static const char *opcode_map[] = { |
| 371 |
[AST_WEBSOCKET_OPCODE_CONTINUATION] = "continuation", |
| 372 |
[AST_WEBSOCKET_OPCODE_TEXT] = "text", |
| 373 |
[AST_WEBSOCKET_OPCODE_BINARY] = "binary", |
| 374 |
[AST_WEBSOCKET_OPCODE_CLOSE] = "close", |
| 375 |
[AST_WEBSOCKET_OPCODE_PING] = "ping", |
| 376 |
[AST_WEBSOCKET_OPCODE_PONG] = "pong", |
| 377 |
}; |
| 378 |
|
| 379 |
static const char *websocket_opcode2str(enum ast_websocket_opcode opcode) |
| 380 |
{ |
| 381 |
if (opcode < AST_WEBSOCKET_OPCODE_CONTINUATION || |
| 382 |
opcode > AST_WEBSOCKET_OPCODE_PONG) { |
| 383 |
return "<unknown>"; |
| 384 |
} else { |
| 385 |
return opcode_map[opcode]; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
int AST_OPTIONAL_API_NAME(ast_websocket_write)(struct ast_websocket *session, enum ast_websocket_opcode opcode, char *payload, uint64_t payload_size) |
| 391 |
{ |
| 392 |
size_t header_size = 2; |
| 393 |
char *frame; |
| 394 |
uint64_t length; |
| 395 |
uint64_t frame_size; |
| 396 |
|
| 397 |
ast_debug(3, "Writing websocket %s frame, length %" PRIu64 "\n", |
| 398 |
websocket_opcode2str(opcode), payload_size); |
| 399 |
|
| 400 |
if (payload_size < 126) { |
| 401 |
length = payload_size; |
| 402 |
} else if (payload_size < (1 << 16)) { |
| 403 |
length = 126; |
| 404 |
|
| 405 |
header_size += 2; |
| 406 |
} else { |
| 407 |
length = 127; |
| 408 |
|
| 409 |
header_size += 8; |
| 410 |
} |
| 411 |
|
| 412 |
if (session->client) { |
| 413 |
|
| 414 |
header_size += 4; |
| 415 |
} |
| 416 |
|
| 417 |
frame_size = header_size + payload_size; |
| 418 |
|
| 419 |
frame = ast_alloca(frame_size + 1); |
| 420 |
memset(frame, 0, frame_size + 1); |
| 421 |
|
| 422 |
frame[0] = opcode | 0x80; |
| 423 |
frame[1] = length; |
| 424 |
|
| 425 |
|
| 426 |
if (length == 126) { |
| 427 |
put_unaligned_uint16(&frame[2], htons(payload_size)); |
| 428 |
} else if (length == 127) { |
| 429 |
put_unaligned_uint64(&frame[2], htonll(payload_size)); |
| 430 |
} |
| 431 |
|
| 432 |
memcpy(&frame[header_size], payload, payload_size); |
| 433 |
|
| 434 |
websocket_mask_payload(session, frame, &frame[header_size], payload_size); |
| 435 |
|
| 436 |
ao2_lock(session); |
| 437 |
if (session->closing) { |
| 438 |
ao2_unlock(session); |
| 439 |
return -1; |
| 440 |
} |
| 441 |
|
| 442 |
ast_iostream_set_timeout_sequence(session->stream, ast_tvnow(), session->timeout); |
| 443 |
if (ast_iostream_write(session->stream, frame, frame_size) != frame_size) { |
| 444 |
ao2_unlock(session); |
| 445 |
|
| 446 |
ast_debug(1, "Closing WS with 1011 because we can't fulfill a write request\n"); |
| 447 |
ast_websocket_close(session, 1011); |
| 448 |
return -1; |
| 449 |
} |
| 450 |
|
| 451 |
ast_iostream_set_timeout_disable(session->stream); |
| 452 |
ao2_unlock(session); |
| 453 |
|
| 454 |
return 0; |
| 455 |
} |
| 456 |
|
| 457 |
void AST_OPTIONAL_API_NAME(ast_websocket_reconstruct_enable)(struct ast_websocket *session, size_t bytes) |
| 458 |
{ |
| 459 |
session->reconstruct = MIN(bytes, MAXIMUM_RECONSTRUCTION_CEILING); |
| 460 |
} |
| 461 |
|
| 462 |
void AST_OPTIONAL_API_NAME(ast_websocket_reconstruct_disable)(struct ast_websocket *session) |
| 463 |
{ |
| 464 |
session->reconstruct = 0; |
| 465 |
} |
| 466 |
|
| 467 |
void AST_OPTIONAL_API_NAME(ast_websocket_ref)(struct ast_websocket *session) |
| 468 |
{ |
| 469 |
ao2_ref(session, +1); |
| 470 |
} |
| 471 |
|
| 472 |
void AST_OPTIONAL_API_NAME(ast_websocket_unref)(struct ast_websocket *session) |
| 473 |
{ |
| 474 |
ao2_cleanup(session); |
| 475 |
} |
| 476 |
|
| 477 |
int AST_OPTIONAL_API_NAME(ast_websocket_fd)(struct ast_websocket *session) |
| 478 |
{ |
| 479 |
return session->closing ? -1 : ast_iostream_get_fd(session->stream); |
| 480 |
} |
| 481 |
|
| 482 |
int AST_OPTIONAL_API_NAME(ast_websocket_wait_for_input)(struct ast_websocket *session, int timeout) |
| 483 |
{ |
| 484 |
return session->closing ? -1 : ast_iostream_wait_for_input(session->stream, timeout); |
| 485 |
} |
| 486 |
|
| 487 |
struct ast_sockaddr * AST_OPTIONAL_API_NAME(ast_websocket_remote_address)(struct ast_websocket *session) |
| 488 |
{ |
| 489 |
return &session->remote_address; |
| 490 |
} |
| 491 |
|
| 492 |
struct ast_sockaddr * AST_OPTIONAL_API_NAME(ast_websocket_local_address)(struct ast_websocket *session) |
| 493 |
{ |
| 494 |
return &session->local_address; |
| 495 |
} |
| 496 |
|
| 497 |
int AST_OPTIONAL_API_NAME(ast_websocket_is_secure)(struct ast_websocket *session) |
| 498 |
{ |
| 499 |
return session->secure; |
| 500 |
} |
| 501 |
|
| 502 |
int AST_OPTIONAL_API_NAME(ast_websocket_set_nonblock)(struct ast_websocket *session) |
| 503 |
{ |
| 504 |
ast_iostream_nonblock(session->stream); |
| 505 |
ast_iostream_set_exclusive_input(session->stream, 0); |
| 506 |
return 0; |
| 507 |
} |
| 508 |
|
| 509 |
int AST_OPTIONAL_API_NAME(ast_websocket_set_timeout)(struct ast_websocket *session, int timeout) |
| 510 |
{ |
| 511 |
session->timeout = timeout; |
| 512 |
|
| 513 |
return 0; |
| 514 |
} |
| 515 |
|
| 516 |
const char * AST_OPTIONAL_API_NAME(ast_websocket_session_id)(struct ast_websocket *session) |
| 517 |
{ |
| 518 |
return session->session_id; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
static inline int ws_safe_read(struct ast_websocket *session, char *buf, size_t len, enum ast_websocket_opcode *opcode) |
| 551 |
{ |
| 552 |
ssize_t rlen; |
| 553 |
int xlen = len; |
| 554 |
char *rbuf = buf; |
| 555 |
int sanity = 10; |
| 556 |
|
| 557 |
ast_assert(len > 0); |
| 558 |
|
| 559 |
if (!len) { |
| 560 |
errno = EINVAL; |
| 561 |
return -1; |
| 562 |
} |
| 563 |
|
| 564 |
ao2_lock(session); |
| 565 |
if (!session->stream) { |
| 566 |
ao2_unlock(session); |
| 567 |
errno = ECONNABORTED; |
| 568 |
return -1; |
| 569 |
} |
| 570 |
|
| 571 |
for (;;) { |
| 572 |
rlen = ast_iostream_read(session->stream, rbuf, xlen); |
| 573 |
if (rlen != xlen) { |
| 574 |
if (rlen == 0) { |
| 575 |
ast_log(LOG_WARNING, "Web socket closed abruptly\n"); |
| 576 |
*opcode = AST_WEBSOCKET_OPCODE_CLOSE; |
| 577 |
session->closing = 1; |
| 578 |
ao2_unlock(session); |
| 579 |
return -1; |
| 580 |
} |
| 581 |
|
| 582 |
if (rlen < 0 && errno != EAGAIN) { |
| 583 |
ast_log(LOG_ERROR, "Error reading from web socket: %s\n", strerror(errno)); |
| 584 |
*opcode = AST_WEBSOCKET_OPCODE_CLOSE; |
| 585 |
session->closing = 1; |
| 586 |
ao2_unlock(session); |
| 587 |
return -1; |
| 588 |
} |
| 589 |
|
| 590 |
if (!--sanity) { |
| 591 |
ast_log(LOG_WARNING, "Websocket seems unresponsive, disconnecting ...\n"); |
| 592 |
*opcode = AST_WEBSOCKET_OPCODE_CLOSE; |
| 593 |
session->closing = 1; |
| 594 |
ao2_unlock(session); |
| 595 |
return -1; |
| 596 |
} |
| 597 |
} |
| 598 |
if (rlen > 0) { |
| 599 |
xlen = xlen - rlen; |
| 600 |
rbuf = rbuf + rlen; |
| 601 |
if (!xlen) { |
| 602 |
break; |
| 603 |
} |
| 604 |
} |
| 605 |
if (ast_iostream_wait_for_input(session->stream, 1000) < 0) { |
| 606 |
ast_log(LOG_ERROR, "ast_iostream_wait_for_input returned err: %s\n", strerror(errno)); |
| 607 |
*opcode = AST_WEBSOCKET_OPCODE_CLOSE; |
| 608 |
session->closing = 1; |
| 609 |
ao2_unlock(session); |
| 610 |
return -1; |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
ao2_unlock(session); |
| 615 |
return 0; |
| 616 |
} |
| 617 |
|
| 618 |
int AST_OPTIONAL_API_NAME(ast_websocket_read)(struct ast_websocket *session, char **payload, uint64_t *payload_len, enum ast_websocket_opcode *opcode, int *fragmented) |
| 619 |
{ |
| 620 |
int fin = 0; |
| 621 |
int mask_present = 0; |
| 622 |
char *mask = NULL, *new_payload = NULL; |
| 623 |
size_t options_len = 0, frame_size = 0; |
| 624 |
|
| 625 |
*payload = NULL; |
| 626 |
*payload_len = 0; |
| 627 |
*fragmented = 0; |
| 628 |
|
| 629 |
if (ws_safe_read(session, &session->buf[0], MIN_WS_HDR_SZ, opcode)) { |
| 630 |
return -1; |
| 631 |
} |
| 632 |
frame_size += MIN_WS_HDR_SZ; |
| 633 |
|
| 634 |
|
| 635 |
*opcode = session->buf[0] & 0xf; |
| 636 |
*payload_len = session->buf[1] & 0x7f; |
| 637 |
if (*opcode == AST_WEBSOCKET_OPCODE_TEXT || *opcode == AST_WEBSOCKET_OPCODE_BINARY || *opcode == AST_WEBSOCKET_OPCODE_CONTINUATION || |
| 638 |
*opcode == AST_WEBSOCKET_OPCODE_PING || *opcode == AST_WEBSOCKET_OPCODE_PONG || *opcode == AST_WEBSOCKET_OPCODE_CLOSE) { |
| 639 |
fin = (session->buf[0] >> 7) & 1; |
| 640 |
mask_present = (session->buf[1] >> 7) & 1; |
| 641 |
|
| 642 |
|
| 643 |
options_len += mask_present ? 4 : 0; |
| 644 |
options_len += (*payload_len == 126) ? 2 : (*payload_len == 127) ? 8 : 0; |
| 645 |
if (options_len) { |
| 646 |
|
| 647 |
if (ws_safe_read(session, &session->buf[frame_size], options_len, opcode)) { |
| 648 |
return -1; |
| 649 |
} |
| 650 |
frame_size += options_len; |
| 651 |
} |
| 652 |
|
| 653 |
if (*payload_len == 126) { |
| 654 |
|
| 655 |
*payload_len = ntohs(get_unaligned_uint16(&session->buf[2])); |
| 656 |
mask = &session->buf[4]; |
| 657 |
} else if (*payload_len == 127) { |
| 658 |
|
| 659 |
*payload_len = ntohll(get_unaligned_uint64(&session->buf[2])); |
| 660 |
mask = &session->buf[10]; |
| 661 |
} else { |
| 662 |
|
| 663 |
mask = &session->buf[2]; |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
*payload = &session->buf[frame_size]; |
| 668 |
frame_size = frame_size + (*payload_len); |
| 669 |
if (frame_size > AST_WEBSOCKET_MAX_RX_PAYLOAD_SIZE) { |
| 670 |
ast_log(LOG_WARNING, "Cannot fit huge websocket frame of %zu bytes\n", frame_size); |
| 671 |
|
| 672 |
ast_websocket_close(session, 1009); |
| 673 |
return -1; |
| 674 |
} |
| 675 |
|
| 676 |
if (*payload_len) { |
| 677 |
if (ws_safe_read(session, *payload, *payload_len, opcode)) { |
| 678 |
return -1; |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
if (mask_present) { |
| 684 |
unsigned int pos; |
| 685 |
for (pos = 0; pos < *payload_len; pos++) { |
| 686 |
(*payload)[pos] ^= mask[pos % 4]; |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
if (*opcode == AST_WEBSOCKET_OPCODE_PING) { |
| 692 |
if (ast_websocket_write(session, AST_WEBSOCKET_OPCODE_PONG, *payload, *payload_len)) { |
| 693 |
ast_websocket_close(session, 1009); |
| 694 |
} |
| 695 |
*payload_len = 0; |
| 696 |
return 0; |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
if (*opcode == AST_WEBSOCKET_OPCODE_PONG) { |
| 701 |
*payload_len = 0; |
| 702 |
return 0; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
if (*opcode == AST_WEBSOCKET_OPCODE_CLOSE) { |
| 707 |
session->closing = 1; |
| 708 |
if (*payload_len >= 2) { |
| 709 |
session->close_status_code = ntohs(get_unaligned_uint16(*payload)); |
| 710 |
} |
| 711 |
*payload_len = 0; |
| 712 |
return 0; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
if (*payload_len) { |
| 717 |
if (!(new_payload = ast_realloc(session->payload, (session->payload_len + *payload_len)))) { |
| 718 |
ast_log(LOG_WARNING, "Failed allocation: %p, %zu, %"PRIu64"\n", |
| 719 |
session->payload, session->payload_len, *payload_len); |
| 720 |
*payload_len = 0; |
| 721 |
ast_websocket_close(session, 1009); |
| 722 |
return -1; |
| 723 |
} |
| 724 |
|
| 725 |
session->payload = new_payload; |
| 726 |
memcpy((session->payload + session->payload_len), (*payload), (*payload_len)); |
| 727 |
session->payload_len += *payload_len; |
| 728 |
} else if (!session->payload_len && session->payload) { |
| 729 |
ast_free(session->payload); |
| 730 |
session->payload = NULL; |
| 731 |
} |
| 732 |
|
| 733 |
if (!fin && session->reconstruct && (session->payload_len < session->reconstruct)) { |
| 734 |
|
| 735 |
if (*opcode != AST_WEBSOCKET_OPCODE_CONTINUATION) { |
| 736 |
session->opcode = *opcode; |
| 737 |
} |
| 738 |
*opcode = AST_WEBSOCKET_OPCODE_CONTINUATION; |
| 739 |
*payload_len = 0; |
| 740 |
*payload = NULL; |
| 741 |
} else { |
| 742 |
if (*opcode == AST_WEBSOCKET_OPCODE_CONTINUATION) { |
| 743 |
if (!fin) { |
| 744 |
|
| 745 |
*fragmented = 1; |
| 746 |
} else { |
| 747 |
|
| 748 |
*opcode = session->opcode; |
| 749 |
} |
| 750 |
} |
| 751 |
*payload_len = session->payload_len; |
| 752 |
*payload = session->payload; |
| 753 |
session->payload_len = 0; |
| 754 |
} |
| 755 |
} else { |
| 756 |
ast_log(LOG_WARNING, "WebSocket unknown opcode %u\n", *opcode); |
| 757 |
|
| 758 |
|
| 759 |
ast_websocket_close(session, 1003); |
| 760 |
} |
| 761 |
|
| 762 |
return 0; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
static struct ast_websocket_protocol *one_protocol( |
| 769 |
struct ast_websocket_server *server) |
| 770 |
{ |
| 771 |
SCOPED_AO2LOCK(lock, server->protocols); |
| 772 |
|
| 773 |
if (ao2_container_count(server->protocols) != 1) { |
| 774 |
return NULL; |
| 775 |
} |
| 776 |
|
| 777 |
return ao2_callback(server->protocols, OBJ_NOLOCK, NULL, NULL); |
| 778 |
} |
| 779 |
|
| 780 |
static char *websocket_combine_key(const char *key, char *res, int res_size) |
| 781 |
{ |
| 782 |
char *combined; |
| 783 |
unsigned combined_length = strlen(key) + strlen(WEBSOCKET_GUID) + 1; |
| 784 |
uint8_t sha[20]; |
| 785 |
|
| 786 |
combined = ast_alloca(combined_length); |
| 787 |
snprintf(combined, combined_length, "%s%s", key, WEBSOCKET_GUID); |
| 788 |
ast_sha1_hash_uint(sha, combined); |
| 789 |
ast_base64encode(res, (const unsigned char*)sha, 20, res_size); |
| 790 |
return res; |
| 791 |
} |
| 792 |
|
| 793 |
static void websocket_bad_request(struct ast_tcptls_session_instance *ser) |
| 794 |
{ |
| 795 |
struct ast_str *http_header = ast_str_create(64); |
| 796 |
|
| 797 |
if (!http_header) { |
| 798 |
ast_http_request_close_on_completion(ser); |
| 799 |
ast_http_error(ser, 500, "Server Error", "Out of memory"); |
| 800 |
return; |
| 801 |
} |
| 802 |
ast_str_set(&http_header, 0, "Sec-WebSocket-Version: 7, 8, 13\r\n"); |
| 803 |
ast_http_send(ser, AST_HTTP_UNKNOWN, 400, "Bad Request", http_header, NULL, 0, 0); |
| 804 |
} |
| 805 |
|
| 806 |
int AST_OPTIONAL_API_NAME(ast_websocket_uri_cb)(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_vars, struct ast_variable *headers) |
| 807 |
{ |
| 808 |
struct ast_variable *v; |
| 809 |
const char *upgrade = NULL, *key = NULL, *key1 = NULL, *key2 = NULL, *protos = NULL; |
| 810 |
char *requested_protocols = NULL, *protocol = NULL; |
| 811 |
int version = 0, flags = 1; |
| 812 |
struct ast_websocket_protocol *protocol_handler = NULL; |
| 813 |
struct ast_websocket *session; |
| 814 |
struct ast_websocket_server *server; |
| 815 |
|
| 816 |
SCOPED_MODULE_USE(ast_module_info->self); |
| 817 |
|
| 818 |
|
| 819 |
if (method != AST_HTTP_GET) { |
| 820 |
ast_http_error(ser, 501, "Not Implemented", "Attempt to use unimplemented / unsupported method"); |
| 821 |
return 0; |
| 822 |
} |
| 823 |
|
| 824 |
server = urih->data; |
| 825 |
|
| 826 |
|
| 827 |
for (v = headers; v; v = v->next) { |
| 828 |
if (!strcasecmp(v->name, "Upgrade")) { |
| 829 |
upgrade = v->value; |
| 830 |
} else if (!strcasecmp(v->name, "Sec-WebSocket-Key")) { |
| 831 |
key = v->value; |
| 832 |
} else if (!strcasecmp(v->name, "Sec-WebSocket-Key1")) { |
| 833 |
key1 = v->value; |
| 834 |
} else if (!strcasecmp(v->name, "Sec-WebSocket-Key2")) { |
| 835 |
key2 = v->value; |
| 836 |
} else if (!strcasecmp(v->name, "Sec-WebSocket-Protocol")) { |
| 837 |
protos = v->value; |
| 838 |
} else if (!strcasecmp(v->name, "Sec-WebSocket-Version")) { |
| 839 |
if (sscanf(v->value, "%30d", &version) != 1) { |
| 840 |
version = 0; |
| 841 |
} |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
if (!upgrade || strcasecmp(upgrade, "websocket")) { |
| 847 |
ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - did not request WebSocket\n", |
| 848 |
ast_sockaddr_stringify(&ser->remote_address)); |
| 849 |
ast_http_error(ser, 426, "Upgrade Required", NULL); |
| 850 |
return 0; |
| 851 |
} else if (ast_strlen_zero(protos)) { |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
protocol_handler = one_protocol(server); |
| 856 |
if (!protocol_handler) { |
| 857 |
|
| 858 |
ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - no protocols requested\n", |
| 859 |
ast_sockaddr_stringify(&ser->remote_address)); |
| 860 |
websocket_bad_request(ser); |
| 861 |
return 0; |
| 862 |
} |
| 863 |
} else if (key1 && key2) { |
| 864 |
|
| 865 |
|
| 866 |
ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - unsupported version '00/76' chosen\n", |
| 867 |
ast_sockaddr_stringify(&ser->remote_address)); |
| 868 |
websocket_bad_request(ser); |
| 869 |
return 0; |
| 870 |
} |
| 871 |
|
| 872 |
if (!protocol_handler && protos) { |
| 873 |
requested_protocols = ast_strdupa(protos); |
| 874 |
|
| 875 |
while (!protocol_handler && (protocol = strsep(&requested_protocols, ","))) { |
| 876 |
protocol_handler = ao2_find(server->protocols, ast_strip(protocol), OBJ_KEY); |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
if (!protocol_handler) { |
| 882 |
ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - no protocols out of '%s' supported\n", |
| 883 |
ast_sockaddr_stringify(&ser->remote_address), protos); |
| 884 |
websocket_bad_request(ser); |
| 885 |
return 0; |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
if (version == 7 || version == 8 || version == 13) { |
| 890 |
char base64[64]; |
| 891 |
|
| 892 |
if (!key || strlen(key) + strlen(WEBSOCKET_GUID) + 1 > 8192) { |
| 893 |
websocket_bad_request(ser); |
| 894 |
ao2_ref(protocol_handler, -1); |
| 895 |
return 0; |
| 896 |
} |
| 897 |
|
| 898 |
if (ast_http_body_discard(ser)) { |
| 899 |
websocket_bad_request(ser); |
| 900 |
ao2_ref(protocol_handler, -1); |
| 901 |
return 0; |
| 902 |
} |
| 903 |
|
| 904 |
if (!(session = ao2_alloc(sizeof(*session) + AST_UUID_STR_LEN + 1, session_destroy_fn))) { |
| 905 |
ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted\n", |
| 906 |
ast_sockaddr_stringify(&ser->remote_address)); |
| 907 |
websocket_bad_request(ser); |
| 908 |
ao2_ref(protocol_handler, -1); |
| 909 |
return 0; |
| 910 |
} |
| 911 |
session->timeout = AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT; |
| 912 |
|
| 913 |
|
| 914 |
if (!ast_uuid_generate_str(session->session_id, sizeof(session->session_id))) { |
| 915 |
ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - failed to generate a session id\n", |
| 916 |
ast_sockaddr_stringify(&ser->remote_address)); |
| 917 |
ast_http_error(ser, 500, "Internal Server Error", "Allocation failed"); |
| 918 |
ao2_ref(protocol_handler, -1); |
| 919 |
return 0; |
| 920 |
} |
| 921 |
|
| 922 |
if (protocol_handler->session_attempted |
| 923 |
&& protocol_handler->session_attempted(ser, get_vars, headers, session->session_id)) { |
| 924 |
ast_debug(3, "WebSocket connection from '%s' rejected by protocol handler '%s'\n", |
| 925 |
ast_sockaddr_stringify(&ser->remote_address), protocol_handler->name); |
| 926 |
websocket_bad_request(ser); |
| 927 |
ao2_ref(protocol_handler, -1); |
| 928 |
return 0; |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
if (protocol) { |
| 941 |
ast_iostream_printf(ser->stream, |
| 942 |
"HTTP/1.1 101 Switching Protocols\r\n" |
| 943 |
"Upgrade: %s\r\n" |
| 944 |
"Connection: Upgrade\r\n" |
| 945 |
"Sec-WebSocket-Accept: %s\r\n" |
| 946 |
"Sec-WebSocket-Protocol: %s\r\n\r\n", |
| 947 |
upgrade, |
| 948 |
websocket_combine_key(key, base64, sizeof(base64)), |
| 949 |
protocol); |
| 950 |
} else { |
| 951 |
ast_iostream_printf(ser->stream, |
| 952 |
"HTTP/1.1 101 Switching Protocols\r\n" |
| 953 |
"Upgrade: %s\r\n" |
| 954 |
"Connection: Upgrade\r\n" |
| 955 |
"Sec-WebSocket-Accept: %s\r\n\r\n", |
| 956 |
upgrade, |
| 957 |
websocket_combine_key(key, base64, sizeof(base64))); |
| 958 |
} |
| 959 |
} else { |
| 960 |
|
| 961 |
|
| 962 |
ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - unsupported version '%d' chosen\n", |
| 963 |
ast_sockaddr_stringify(&ser->remote_address), version ? version : 75); |
| 964 |
websocket_bad_request(ser); |
| 965 |
ao2_ref(protocol_handler, -1); |
| 966 |
return 0; |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
if (setsockopt(ast_iostream_get_fd(ser->stream), SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof(flags))) { |
| 971 |
ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - failed to enable keepalive\n", |
| 972 |
ast_sockaddr_stringify(&ser->remote_address)); |
| 973 |
websocket_bad_request(ser); |
| 974 |
ao2_ref(session, -1); |
| 975 |
ao2_ref(protocol_handler, -1); |
| 976 |
return 0; |
| 977 |
} |
| 978 |
|
| 979 |
|
| 980 |
if (ast_getsockname(ast_iostream_get_fd(ser->stream), &session->local_address)) { |
| 981 |
ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - failed to get local address\n", |
| 982 |
ast_sockaddr_stringify(&ser->remote_address)); |
| 983 |
websocket_bad_request(ser); |
| 984 |
ao2_ref(session, -1); |
| 985 |
ao2_ref(protocol_handler, -1); |
| 986 |
return 0; |
| 987 |
} |
| 988 |
|
| 989 |
ast_debug(3, "WebSocket connection from '%s' for protocol '%s' accepted using version '%d'\n", ast_sockaddr_stringify(&ser->remote_address), protocol ? : "", version); |
| 990 |
|
| 991 |
|
| 992 |
session->stream = ser->stream; |
| 993 |
ast_sockaddr_copy(&session->remote_address, &ser->remote_address); |
| 994 |
session->opcode = -1; |
| 995 |
session->reconstruct = DEFAULT_RECONSTRUCTION_CEILING; |
| 996 |
session->secure = ast_iostream_get_ssl(ser->stream) ? 1 : 0; |
| 997 |
|
| 998 |
|
| 999 |
ast_iostream_set_exclusive_input(session->stream, 0); |
| 1000 |
protocol_handler->session_established(session, get_vars, headers); |
| 1001 |
ao2_ref(protocol_handler, -1); |
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
ser->stream = NULL; |
| 1009 |
|
| 1010 |
return 0; |
| 1011 |
} |
| 1012 |
|
| 1013 |
static struct ast_http_uri websocketuri = { |
| 1014 |
.callback = AST_OPTIONAL_API_NAME(ast_websocket_uri_cb), |
| 1015 |
.description = "Asterisk HTTP WebSocket", |
| 1016 |
.uri = "ws", |
| 1017 |
.has_subtree = 0, |
| 1018 |
.data = NULL, |
| 1019 |
.key = __FILE__, |
| 1020 |
}; |
| 1021 |
|
| 1022 |
|
| 1023 |
static void websocket_echo_callback(struct ast_websocket *session, struct ast_variable *parameters, struct ast_variable *headers) |
| 1024 |
{ |
| 1025 |
int res; |
| 1026 |
|
| 1027 |
ast_debug(1, "Entering WebSocket echo loop\n"); |
| 1028 |
|
| 1029 |
if (ast_fd_set_flags(ast_websocket_fd(session), O_NONBLOCK)) { |
| 1030 |
goto end; |
| 1031 |
} |
| 1032 |
|
| 1033 |
while ((res = ast_websocket_wait_for_input(session, -1)) > 0) { |
| 1034 |
char *payload; |
| 1035 |
uint64_t payload_len; |
| 1036 |
enum ast_websocket_opcode opcode; |
| 1037 |
int fragmented; |
| 1038 |
|
| 1039 |
if (ast_websocket_read(session, &payload, &payload_len, &opcode, &fragmented)) { |
| 1040 |
|
| 1041 |
ast_log(LOG_WARNING, "Read failure during WebSocket echo loop\n"); |
| 1042 |
break; |
| 1043 |
} |
| 1044 |
|
| 1045 |
if (opcode == AST_WEBSOCKET_OPCODE_TEXT || opcode == AST_WEBSOCKET_OPCODE_BINARY) { |
| 1046 |
ast_websocket_write(session, opcode, payload, payload_len); |
| 1047 |
} else if (opcode == AST_WEBSOCKET_OPCODE_CLOSE) { |
| 1048 |
break; |
| 1049 |
} else { |
| 1050 |
ast_debug(1, "Ignored WebSocket opcode %u\n", opcode); |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
end: |
| 1055 |
ast_debug(1, "Exiting WebSocket echo loop\n"); |
| 1056 |
ast_websocket_unref(session); |
| 1057 |
} |
| 1058 |
|
| 1059 |
static int websocket_add_protocol_internal(const char *name, ast_websocket_callback callback) |
| 1060 |
{ |
| 1061 |
struct ast_websocket_server *ws_server = websocketuri.data; |
| 1062 |
if (!ws_server) { |
| 1063 |
return -1; |
| 1064 |
} |
| 1065 |
return ast_websocket_server_add_protocol(ws_server, name, callback); |
| 1066 |
} |
| 1067 |
|
| 1068 |
int AST_OPTIONAL_API_NAME(ast_websocket_add_protocol)(const char *name, ast_websocket_callback callback) |
| 1069 |
{ |
| 1070 |
return websocket_add_protocol_internal(name, callback); |
| 1071 |
} |
| 1072 |
|
| 1073 |
int AST_OPTIONAL_API_NAME(ast_websocket_add_protocol2)(struct ast_websocket_protocol *protocol) |
| 1074 |
{ |
| 1075 |
struct ast_websocket_server *ws_server = websocketuri.data; |
| 1076 |
|
| 1077 |
if (!ws_server) { |
| 1078 |
return -1; |
| 1079 |
} |
| 1080 |
|
| 1081 |
if (ast_websocket_server_add_protocol2(ws_server, protocol)) { |
| 1082 |
return -1; |
| 1083 |
} |
| 1084 |
|
| 1085 |
return 0; |
| 1086 |
} |
| 1087 |
|
| 1088 |
static int websocket_remove_protocol_internal(const char *name, ast_websocket_callback callback) |
| 1089 |
{ |
| 1090 |
struct ast_websocket_server *ws_server = websocketuri.data; |
| 1091 |
if (!ws_server) { |
| 1092 |
return -1; |
| 1093 |
} |
| 1094 |
return ast_websocket_server_remove_protocol(ws_server, name, callback); |
| 1095 |
} |
| 1096 |
|
| 1097 |
int AST_OPTIONAL_API_NAME(ast_websocket_remove_protocol)(const char *name, ast_websocket_callback callback) |
| 1098 |
{ |
| 1099 |
return websocket_remove_protocol_internal(name, callback); |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
static int websocket_client_parse_uri(const char *uri, char **host, |
| 1111 |
struct ast_str **path, char **userinfo) |
| 1112 |
{ |
| 1113 |
struct ast_uri *parsed_uri = ast_uri_parse_websocket(uri); |
| 1114 |
|
| 1115 |
if (!parsed_uri) { |
| 1116 |
return -1; |
| 1117 |
} |
| 1118 |
|
| 1119 |
*host = ast_uri_make_host_with_port(parsed_uri); |
| 1120 |
*userinfo = ast_strdup(ast_uri_user_info(parsed_uri)); |
| 1121 |
|
| 1122 |
if (ast_uri_path(parsed_uri) || ast_uri_query(parsed_uri)) { |
| 1123 |
*path = ast_str_create(64); |
| 1124 |
if (!*path) { |
| 1125 |
ao2_ref(parsed_uri, -1); |
| 1126 |
return -1; |
| 1127 |
} |
| 1128 |
|
| 1129 |
if (ast_uri_path(parsed_uri)) { |
| 1130 |
ast_str_set(path, 0, "%s", ast_uri_path(parsed_uri)); |
| 1131 |
} |
| 1132 |
|
| 1133 |
if (ast_uri_query(parsed_uri)) { |
| 1134 |
ast_str_append(path, 0, "?%s", ast_uri_query(parsed_uri)); |
| 1135 |
} |
| 1136 |
} |
| 1137 |
|
| 1138 |
ao2_ref(parsed_uri, -1); |
| 1139 |
return 0; |
| 1140 |
} |
| 1141 |
|
| 1142 |
static void websocket_client_args_destroy(void *obj) |
| 1143 |
{ |
| 1144 |
struct ast_tcptls_session_args *args = obj; |
| 1145 |
|
| 1146 |
if (args->tls_cfg) { |
| 1147 |
ast_free(args->tls_cfg->certfile); |
| 1148 |
ast_free(args->tls_cfg->pvtfile); |
| 1149 |
ast_free(args->tls_cfg->cipher); |
| 1150 |
ast_free(args->tls_cfg->cafile); |
| 1151 |
ast_free(args->tls_cfg->capath); |
| 1152 |
|
| 1153 |
ast_ssl_teardown(args->tls_cfg); |
| 1154 |
} |
| 1155 |
ast_free(args->tls_cfg); |
| 1156 |
} |
| 1157 |
|
| 1158 |
static struct ast_tcptls_session_args *websocket_client_args_create( |
| 1159 |
const char *host, struct ast_tls_config *tls_cfg, |
| 1160 |
enum ast_websocket_result *result) |
| 1161 |
{ |
| 1162 |
struct ast_sockaddr *addr; |
| 1163 |
struct ast_tcptls_session_args *args = ao2_alloc( |
| 1164 |
sizeof(*args), websocket_client_args_destroy); |
| 1165 |
|
| 1166 |
if (!args) { |
| 1167 |
*result = WS_ALLOCATE_ERROR; |
| 1168 |
return NULL; |
| 1169 |
} |
| 1170 |
|
| 1171 |
args->accept_fd = -1; |
| 1172 |
args->tls_cfg = tls_cfg; |
| 1173 |
args->name = "websocket client"; |
| 1174 |
|
| 1175 |
if (!ast_sockaddr_resolve(&addr, host, 0, 0)) { |
| 1176 |
ast_log(LOG_ERROR, "Unable to resolve address %s\n", |
| 1177 |
host); |
| 1178 |
ao2_ref(args, -1); |
| 1179 |
*result = WS_URI_RESOLVE_ERROR; |
| 1180 |
return NULL; |
| 1181 |
} |
| 1182 |
ast_sockaddr_copy(&args->remote_address, addr); |
| 1183 |
ast_free(addr); |
| 1184 |
|
| 1185 |
|
| 1186 |
snprintf(args->hostname, sizeof(args->hostname), |
| 1187 |
"%.*s", |
| 1188 |
(int) strcspn(host, ":"), host); |
| 1189 |
|
| 1190 |
return args; |
| 1191 |
} |
| 1192 |
|
| 1193 |
static char *websocket_client_create_key(void) |
| 1194 |
{ |
| 1195 |
static int encoded_size = CLIENT_KEY_SIZE * 2 * sizeof(char) + 1; |
| 1196 |
|
| 1197 |
unsigned char key[CLIENT_KEY_SIZE + sizeof(long) - 1]; |
| 1198 |
char *encoded = ast_malloc(encoded_size); |
| 1199 |
long i = 0; |
| 1200 |
|
| 1201 |
if (!encoded) { |
| 1202 |
ast_log(LOG_ERROR, "Unable to allocate client websocket key\n"); |
| 1203 |
return NULL; |
| 1204 |
} |
| 1205 |
|
| 1206 |
while (i < CLIENT_KEY_SIZE) { |
| 1207 |
long num = ast_random(); |
| 1208 |
memcpy(key + i, &num, sizeof(long)); |
| 1209 |
i += sizeof(long); |
| 1210 |
} |
| 1211 |
|
| 1212 |
ast_base64encode(encoded, key, CLIENT_KEY_SIZE, encoded_size); |
| 1213 |
return encoded; |
| 1214 |
} |
| 1215 |
|
| 1216 |
struct websocket_client { |
| 1217 |
|
| 1218 |
char *host; |
| 1219 |
|
| 1220 |
struct ast_str *resource_name; |
| 1221 |
|
| 1222 |
char *key; |
| 1223 |
|
| 1224 |
char *protocols; |
| 1225 |
|
| 1226 |
char *accept_protocol; |
| 1227 |
|
| 1228 |
int version; |
| 1229 |
|
| 1230 |
struct ast_tcptls_session_args *args; |
| 1231 |
|
| 1232 |
struct ast_tcptls_session_instance *ser; |
| 1233 |
|
| 1234 |
char *userinfo; |
| 1235 |
|
| 1236 |
int suppress_connection_msgs; |
| 1237 |
}; |
| 1238 |
|
| 1239 |
static void websocket_client_destroy(void *obj) |
| 1240 |
{ |
| 1241 |
struct websocket_client *client = obj; |
| 1242 |
|
| 1243 |
ao2_cleanup(client->ser); |
| 1244 |
ao2_cleanup(client->args); |
| 1245 |
|
| 1246 |
ast_free(client->accept_protocol); |
| 1247 |
ast_free(client->protocols); |
| 1248 |
ast_free(client->key); |
| 1249 |
ast_free(client->resource_name); |
| 1250 |
ast_free(client->host); |
| 1251 |
ast_free(client->userinfo); |
| 1252 |
} |
| 1253 |
|
| 1254 |
static struct ast_websocket * websocket_client_create( |
| 1255 |
struct ast_websocket_client_options *options, enum ast_websocket_result *result) |
| 1256 |
{ |
| 1257 |
struct ast_websocket *ws = ao2_alloc(sizeof(*ws), session_destroy_fn); |
| 1258 |
|
| 1259 |
if (!ws) { |
| 1260 |
ast_log(LOG_ERROR, "Unable to allocate websocket\n"); |
| 1261 |
*result = WS_ALLOCATE_ERROR; |
| 1262 |
return NULL; |
| 1263 |
} |
| 1264 |
|
| 1265 |
if (!ast_uuid_generate_str(ws->session_id, sizeof(ws->session_id))) { |
| 1266 |
ast_log(LOG_ERROR, "Unable to allocate websocket session_id\n"); |
| 1267 |
ao2_ref(ws, -1); |
| 1268 |
*result = WS_ALLOCATE_ERROR; |
| 1269 |
return NULL; |
| 1270 |
} |
| 1271 |
|
| 1272 |
if (!(ws->client = ao2_alloc( |
| 1273 |
sizeof(*ws->client), websocket_client_destroy))) { |
| 1274 |
ast_log(LOG_ERROR, "Unable to allocate websocket client\n"); |
| 1275 |
ao2_ref(ws, -1); |
| 1276 |
*result = WS_ALLOCATE_ERROR; |
| 1277 |
return NULL; |
| 1278 |
} |
| 1279 |
|
| 1280 |
if (!(ws->client->key = websocket_client_create_key())) { |
| 1281 |
ao2_ref(ws, -1); |
| 1282 |
*result = WS_KEY_ERROR; |
| 1283 |
return NULL; |
| 1284 |
} |
| 1285 |
|
| 1286 |
if (websocket_client_parse_uri( |
| 1287 |
options->uri, &ws->client->host, &ws->client->resource_name, |
| 1288 |
&ws->client->userinfo)) { |
| 1289 |
ao2_ref(ws, -1); |
| 1290 |
*result = WS_URI_PARSE_ERROR; |
| 1291 |
return NULL; |
| 1292 |
} |
| 1293 |
|
| 1294 |
if (ast_strlen_zero(ws->client->userinfo) |
| 1295 |
&& !ast_strlen_zero(options->username) |
| 1296 |
&& !ast_strlen_zero(options->password)) { |
| 1297 |
ast_asprintf(&ws->client->userinfo, "%s:%s", options->username, |
| 1298 |
options->password); |
| 1299 |
} |
| 1300 |
|
| 1301 |
if (!(ws->client->args = websocket_client_args_create( |
| 1302 |
ws->client->host, options->tls_cfg, result))) { |
| 1303 |
ao2_ref(ws, -1); |
| 1304 |
return NULL; |
| 1305 |
} |
| 1306 |
|
| 1307 |
ws->client->suppress_connection_msgs = options->suppress_connection_msgs; |
| 1308 |
ws->client->args->suppress_connection_msgs = options->suppress_connection_msgs; |
| 1309 |
ws->client->protocols = ast_strdup(options->protocols); |
| 1310 |
ws->client->version = 13; |
| 1311 |
ws->opcode = -1; |
| 1312 |
ws->reconstruct = DEFAULT_RECONSTRUCTION_CEILING; |
| 1313 |
ws->timeout = options->timeout; |
| 1314 |
|
| 1315 |
return ws; |
| 1316 |
} |
| 1317 |
|
| 1318 |
const char * AST_OPTIONAL_API_NAME( |
| 1319 |
ast_websocket_client_accept_protocol)(struct ast_websocket *ws) |
| 1320 |
{ |
| 1321 |
return ws->client->accept_protocol; |
| 1322 |
} |
| 1323 |
|
| 1324 |
static enum ast_websocket_result websocket_client_handle_response_code( |
| 1325 |
struct websocket_client *client, int response_code) |
| 1326 |
{ |
| 1327 |
if (response_code <= 0) { |
| 1328 |
return WS_INVALID_RESPONSE; |
| 1329 |
} |
| 1330 |
|
| 1331 |
switch (response_code) { |
| 1332 |
case 101: |
| 1333 |
return 0; |
| 1334 |
case 400: |
| 1335 |
if (!client->suppress_connection_msgs) { |
| 1336 |
ast_log(LOG_ERROR, "Received response 400 - Bad Request " |
| 1337 |
"- from %s\n", client->host); |
| 1338 |
} |
| 1339 |
return WS_BAD_REQUEST; |
| 1340 |
case 401: |
| 1341 |
if (!client->suppress_connection_msgs) { |
| 1342 |
ast_log(LOG_ERROR, "Received response 401 - Unauthorized " |
| 1343 |
"- from %s\n", client->host); |
| 1344 |
} |
| 1345 |
return WS_UNAUTHORIZED; |
| 1346 |
case 404: |
| 1347 |
if (!client->suppress_connection_msgs) { |
| 1348 |
ast_log(LOG_ERROR, "Received response 404 - Request URL not " |
| 1349 |
"found - from %s\n", client->host); |
| 1350 |
} |
| 1351 |
return WS_URL_NOT_FOUND; |
| 1352 |
} |
| 1353 |
|
| 1354 |
if (!client->suppress_connection_msgs) { |
| 1355 |
ast_log(LOG_ERROR, "Invalid HTTP response code %d from %s\n", |
| 1356 |
response_code, client->host); |
| 1357 |
} |
| 1358 |
return WS_INVALID_RESPONSE; |
| 1359 |
} |
| 1360 |
|
| 1361 |
static enum ast_websocket_result websocket_client_handshake_get_response( |
| 1362 |
struct websocket_client *client) |
| 1363 |
{ |
| 1364 |
enum ast_websocket_result res; |
| 1365 |
char buf[4096]; |
| 1366 |
char base64[64]; |
| 1367 |
int has_upgrade = 0; |
| 1368 |
int has_connection = 0; |
| 1369 |
int has_accept = 0; |
| 1370 |
int has_protocol = 0; |
| 1371 |
|
| 1372 |
while (ast_iostream_gets(client->ser->stream, buf, sizeof(buf)) <= 0) { |
| 1373 |
if (errno == EINTR || errno == EAGAIN) { |
| 1374 |
continue; |
| 1375 |
} |
| 1376 |
|
| 1377 |
ast_log(LOG_ERROR, "Unable to retrieve HTTP status line."); |
| 1378 |
return WS_BAD_STATUS; |
| 1379 |
} |
| 1380 |
|
| 1381 |
if ((res = websocket_client_handle_response_code(client, |
| 1382 |
ast_http_response_status_line( |
| 1383 |
buf, "HTTP/1.1", 101))) != WS_OK) { |
| 1384 |
return res; |
| 1385 |
} |
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
while (1) { |
| 1390 |
ssize_t len = ast_iostream_gets(client->ser->stream, buf, sizeof(buf)); |
| 1391 |
char *name, *value; |
| 1392 |
int parsed; |
| 1393 |
|
| 1394 |
if (len <= 0) { |
| 1395 |
if (errno == EINTR || errno == EAGAIN) { |
| 1396 |
continue; |
| 1397 |
} |
| 1398 |
break; |
| 1399 |
} |
| 1400 |
|
| 1401 |
parsed = ast_http_header_parse(buf, &name, &value); |
| 1402 |
if (parsed < 0) { |
| 1403 |
break; |
| 1404 |
} |
| 1405 |
|
| 1406 |
if (parsed > 0) { |
| 1407 |
continue; |
| 1408 |
} |
| 1409 |
|
| 1410 |
if (!has_upgrade && |
| 1411 |
(has_upgrade = ast_http_header_match( |
| 1412 |
name, "upgrade", value, "websocket")) < 0) { |
| 1413 |
return WS_HEADER_MISMATCH; |
| 1414 |
} else if (!has_connection && |
| 1415 |
(has_connection = ast_http_header_match( |
| 1416 |
name, "connection", value, "upgrade")) < 0) { |
| 1417 |
return WS_HEADER_MISMATCH; |
| 1418 |
} else if (!has_accept && |
| 1419 |
(has_accept = ast_http_header_match( |
| 1420 |
name, "sec-websocket-accept", value, |
| 1421 |
websocket_combine_key( |
| 1422 |
client->key, base64, sizeof(base64)))) < 0) { |
| 1423 |
return WS_HEADER_MISMATCH; |
| 1424 |
} else if (!has_protocol && |
| 1425 |
(has_protocol = ast_http_header_match_in( |
| 1426 |
name, "sec-websocket-protocol", value, client->protocols))) { |
| 1427 |
if (has_protocol < 0) { |
| 1428 |
return WS_HEADER_MISMATCH; |
| 1429 |
} |
| 1430 |
client->accept_protocol = ast_strdup(value); |
| 1431 |
} else if (!strcasecmp(name, "sec-websocket-extensions")) { |
| 1432 |
ast_log(LOG_ERROR, "Extensions received, but not " |
| 1433 |
"supported by client\n"); |
| 1434 |
return WS_NOT_SUPPORTED; |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
return has_upgrade && has_connection && has_accept ? |
| 1439 |
WS_OK : WS_HEADER_MISSING; |
| 1440 |
} |
| 1441 |
|
| 1442 |
#define optional_header_spec "%s%s%s" |
| 1443 |
#define print_optional_header(test, name, value) \ |
| 1444 |
test ? name : "", \ |
| 1445 |
test ? value : "", \ |
| 1446 |
test ? "\r\n" : "" |
| 1447 |
|
| 1448 |
static enum ast_websocket_result websocket_client_handshake( |
| 1449 |
struct websocket_client *client) |
| 1450 |
{ |
| 1451 |
size_t protocols_len = 0; |
| 1452 |
struct ast_variable *auth_header = NULL; |
| 1453 |
size_t res; |
| 1454 |
|
| 1455 |
if (!ast_strlen_zero(client->userinfo)) { |
| 1456 |
auth_header = ast_http_create_basic_auth_header(client->userinfo, NULL); |
| 1457 |
if (!auth_header) { |
| 1458 |
ast_log(LOG_ERROR, "Unable to allocate client websocket userinfo\n"); |
| 1459 |
return WS_ALLOCATE_ERROR; |
| 1460 |
} |
| 1461 |
} |
| 1462 |
|
| 1463 |
protocols_len = client->protocols ? strlen(client->protocols) : 0; |
| 1464 |
|
| 1465 |
res = ast_iostream_printf(client->ser->stream, |
| 1466 |
"GET /%s HTTP/1.1\r\n" |
| 1467 |
"Sec-WebSocket-Version: %d\r\n" |
| 1468 |
"Upgrade: websocket\r\n" |
| 1469 |
"Connection: Upgrade\r\n" |
| 1470 |
"Host: %s\r\n" |
| 1471 |
optional_header_spec |
| 1472 |
optional_header_spec |
| 1473 |
"Sec-WebSocket-Key: %s\r\n" |
| 1474 |
"\r\n", |
| 1475 |
client->resource_name ? ast_str_buffer(client->resource_name) : "", |
| 1476 |
client->version, |
| 1477 |
client->host, |
| 1478 |
print_optional_header(auth_header, "Authorization: ", auth_header->value), |
| 1479 |
print_optional_header(protocols_len, "Sec-WebSocket-Protocol: ", client->protocols), |
| 1480 |
client->key |
| 1481 |
); |
| 1482 |
|
| 1483 |
ast_variables_destroy(auth_header); |
| 1484 |
if (res < 0) { |
| 1485 |
ast_log(LOG_ERROR, "Failed to send handshake.\n"); |
| 1486 |
return WS_WRITE_ERROR; |
| 1487 |
} |
| 1488 |
|
| 1489 |
return websocket_client_handshake_get_response(client); |
| 1490 |
} |
| 1491 |
|
| 1492 |
static enum ast_websocket_result websocket_client_connect(struct ast_websocket *ws, int timeout) |
| 1493 |
{ |
| 1494 |
enum ast_websocket_result res; |
| 1495 |
|
| 1496 |
|
| 1497 |
if (!(ws->client->ser = ast_tcptls_client_start_timeout( |
| 1498 |
ast_tcptls_client_create(ws->client->args), timeout))) { |
| 1499 |
return WS_CLIENT_START_ERROR; |
| 1500 |
} |
| 1501 |
|
| 1502 |
if ((res = websocket_client_handshake(ws->client)) != WS_OK) { |
| 1503 |
ao2_ref(ws->client->ser, -1); |
| 1504 |
ws->client->ser = NULL; |
| 1505 |
return res; |
| 1506 |
} |
| 1507 |
|
| 1508 |
ws->stream = ws->client->ser->stream; |
| 1509 |
ws->secure = ast_iostream_get_ssl(ws->stream) ? 1 : 0; |
| 1510 |
ws->client->ser->stream = NULL; |
| 1511 |
ast_sockaddr_copy(&ws->remote_address, &ws->client->ser->remote_address); |
| 1512 |
return WS_OK; |
| 1513 |
} |
| 1514 |
|
| 1515 |
struct ast_websocket *AST_OPTIONAL_API_NAME(ast_websocket_client_create) |
| 1516 |
(const char *uri, const char *protocols, struct ast_tls_config *tls_cfg, |
| 1517 |
enum ast_websocket_result *result) |
| 1518 |
{ |
| 1519 |
struct ast_websocket_client_options options = { |
| 1520 |
.uri = uri, |
| 1521 |
.protocols = protocols, |
| 1522 |
.timeout = -1, |
| 1523 |
.tls_cfg = tls_cfg, |
| 1524 |
}; |
| 1525 |
|
| 1526 |
return ast_websocket_client_create_with_options(&options, result); |
| 1527 |
} |
| 1528 |
|
| 1529 |
struct ast_websocket *AST_OPTIONAL_API_NAME(ast_websocket_client_create_with_options) |
| 1530 |
(struct ast_websocket_client_options *options, enum ast_websocket_result *result) |
| 1531 |
{ |
| 1532 |
struct ast_websocket *ws = websocket_client_create(options, result); |
| 1533 |
|
| 1534 |
if (!ws) { |
| 1535 |
return NULL; |
| 1536 |
} |
| 1537 |
|
| 1538 |
if ((*result = websocket_client_connect(ws, options->timeout)) != WS_OK) { |
| 1539 |
ao2_ref(ws, -1); |
| 1540 |
return NULL; |
| 1541 |
} |
| 1542 |
|
| 1543 |
return ws; |
| 1544 |
} |
| 1545 |
|
| 1546 |
int AST_OPTIONAL_API_NAME(ast_websocket_read_string) |
| 1547 |
(struct ast_websocket *ws, char **buf) |
| 1548 |
{ |
| 1549 |
char *payload; |
| 1550 |
uint64_t payload_len; |
| 1551 |
enum ast_websocket_opcode opcode; |
| 1552 |
int fragmented = 1; |
| 1553 |
|
| 1554 |
while (fragmented) { |
| 1555 |
if (ast_websocket_read(ws, &payload, &payload_len, |
| 1556 |
&opcode, &fragmented)) { |
| 1557 |
ast_log(LOG_ERROR, "Client WebSocket string read - " |
| 1558 |
"error reading string data\n"); |
| 1559 |
return -1; |
| 1560 |
} |
| 1561 |
|
| 1562 |
if (opcode == AST_WEBSOCKET_OPCODE_PING) { |
| 1563 |
|
| 1564 |
fragmented = 1; |
| 1565 |
continue; |
| 1566 |
} |
| 1567 |
|
| 1568 |
if (opcode == AST_WEBSOCKET_OPCODE_CONTINUATION) { |
| 1569 |
continue; |
| 1570 |
} |
| 1571 |
|
| 1572 |
if (opcode == AST_WEBSOCKET_OPCODE_CLOSE) { |
| 1573 |
return -1; |
| 1574 |
} |
| 1575 |
|
| 1576 |
if (opcode != AST_WEBSOCKET_OPCODE_TEXT) { |
| 1577 |
ast_log(LOG_ERROR, "Client WebSocket string read - " |
| 1578 |
"non string data received\n"); |
| 1579 |
return -1; |
| 1580 |
} |
| 1581 |
} |
| 1582 |
|
| 1583 |
if (!(*buf = ast_strndup(payload, payload_len))) { |
| 1584 |
return -1; |
| 1585 |
} |
| 1586 |
|
| 1587 |
return payload_len + 1; |
| 1588 |
} |
| 1589 |
|
| 1590 |
int AST_OPTIONAL_API_NAME(ast_websocket_write_string) |
| 1591 |
(struct ast_websocket *ws, const char *buf) |
| 1592 |
{ |
| 1593 |
uint64_t len = strlen(buf); |
| 1594 |
|
| 1595 |
ast_debug(3, "Writing websocket string of length %" PRIu64 "\n", len); |
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
return ast_websocket_write(ws, AST_WEBSOCKET_OPCODE_TEXT, |
| 1605 |
(char *)buf, len); |
| 1606 |
} |
| 1607 |
|
| 1608 |
const char *websocket_result_string_map[] = { |
| 1609 |
[WS_OK] = "OK", |
| 1610 |
[WS_ALLOCATE_ERROR] = "Allocation error", |
| 1611 |
[WS_KEY_ERROR] = "Key error", |
| 1612 |
[WS_URI_PARSE_ERROR] = "URI parse error", |
| 1613 |
[WS_URI_RESOLVE_ERROR] = "URI resolve error", |
| 1614 |
[WS_BAD_STATUS] = "Bad status line", |
| 1615 |
[WS_INVALID_RESPONSE] = "Invalid response code", |
| 1616 |
[WS_BAD_REQUEST] = "Bad request", |
| 1617 |
[WS_URL_NOT_FOUND] = "URL not found", |
| 1618 |
[WS_HEADER_MISMATCH] = "Header mismatch", |
| 1619 |
[WS_HEADER_MISSING] = "Header missing", |
| 1620 |
[WS_NOT_SUPPORTED] = "Not supported", |
| 1621 |
[WS_WRITE_ERROR] = "Write error", |
| 1622 |
[WS_CLIENT_START_ERROR] = "Client start error", |
| 1623 |
[WS_UNAUTHORIZED] = "Unauthorized" |
| 1624 |
}; |
| 1625 |
|
| 1626 |
const char *AST_OPTIONAL_API_NAME(ast_websocket_result_to_str) |
| 1627 |
(enum ast_websocket_result result) |
| 1628 |
{ |
| 1629 |
if (!ARRAY_IN_BOUNDS(result, websocket_result_string_map)) { |
| 1630 |
return "unknown"; |
| 1631 |
} |
| 1632 |
return websocket_result_string_map[result]; |
| 1633 |
} |
| 1634 |
|
| 1635 |
struct status_map { |
| 1636 |
enum ast_websocket_status_code code; |
| 1637 |
const char *desc; |
| 1638 |
}; |
| 1639 |
|
| 1640 |
static const struct status_map websocket_status_map[] = { |
| 1641 |
{ AST_WEBSOCKET_STATUS_NORMAL, "Normal" }, |
| 1642 |
{ AST_WEBSOCKET_STATUS_GOING_AWAY, "Going away" }, |
| 1643 |
{ AST_WEBSOCKET_STATUS_PROTOCOL_ERROR, "Protocol error" }, |
| 1644 |
{ AST_WEBSOCKET_STATUS_UNSUPPORTED_DATA, "Unsupported data" }, |
| 1645 |
{ AST_WEBSOCKET_STATUS_RESERVED_1004, "reserved 1004" }, |
| 1646 |
{ AST_WEBSOCKET_STATUS_RESERVED_1005, "reserved 1005" }, |
| 1647 |
{ AST_WEBSOCKET_STATUS_RESERVED_1006, "reserved 1006" }, |
| 1648 |
{ AST_WEBSOCKET_STATUS_INVALID_FRAME, "Invalid frame" }, |
| 1649 |
{ AST_WEBSOCKET_STATUS_POLICY_VIOLATION, "Policy violation" }, |
| 1650 |
{ AST_WEBSOCKET_STATUS_TOO_BIG, "Data too big" }, |
| 1651 |
{ AST_WEBSOCKET_STATUS_MANDATORY_EXT, "Mandatory extension" }, |
| 1652 |
{ AST_WEBSOCKET_STATUS_INTERNAL_ERROR, "Internal error" }, |
| 1653 |
{ AST_WEBSOCKET_STATUS_RESERVED_1012, "reserved 1012" }, |
| 1654 |
{ AST_WEBSOCKET_STATUS_RESERVED_1013, "reserved 1013" }, |
| 1655 |
{ AST_WEBSOCKET_STATUS_BAD_GATEWAY, "Bad gateway" }, |
| 1656 |
{ AST_WEBSOCKET_STATUS_RESERVED_1015, "reserved 1015" }, |
| 1657 |
}; |
| 1658 |
|
| 1659 |
const char *AST_OPTIONAL_API_NAME(ast_websocket_status_to_str) |
| 1660 |
(enum ast_websocket_status_code code) |
| 1661 |
{ |
| 1662 |
int i; |
| 1663 |
|
| 1664 |
for (i = 0; i < ARRAY_LEN(websocket_status_map); i++) { |
| 1665 |
if (websocket_status_map[i].code == code) |
| 1666 |
return websocket_status_map[i].desc; |
| 1667 |
} |
| 1668 |
|
| 1669 |
return "Unknown"; |
| 1670 |
} |
| 1671 |
|
| 1672 |
static int load_module(void) |
| 1673 |
{ |
| 1674 |
websocketuri.data = websocket_server_internal_create(); |
| 1675 |
if (!websocketuri.data) { |
| 1676 |
return AST_MODULE_LOAD_DECLINE; |
| 1677 |
} |
| 1678 |
ast_http_uri_link(&websocketuri); |
| 1679 |
websocket_add_protocol_internal("echo", websocket_echo_callback); |
| 1680 |
|
| 1681 |
return 0; |
| 1682 |
} |
| 1683 |
|
| 1684 |
static int unload_module(void) |
| 1685 |
{ |
| 1686 |
websocket_remove_protocol_internal("echo", websocket_echo_callback); |
| 1687 |
ast_http_uri_unlink(&websocketuri); |
| 1688 |
ao2_ref(websocketuri.data, -1); |
| 1689 |
websocketuri.data = NULL; |
| 1690 |
|
| 1691 |
return 0; |
| 1692 |
} |
| 1693 |
|
| 1694 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "HTTP WebSocket Support", |
| 1695 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1696 |
.load = load_module, |
| 1697 |
.unload = unload_module, |
| 1698 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND, |
| 1699 |
.requires = "http", |
| 1700 |
); |