James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame] | 1 | /** |
| 2 | * @file sip/transp.c SIP Transport |
| 3 | * |
| 4 | * Copyright (C) 2010 Creytiv.com |
| 5 | */ |
| 6 | #include <string.h> |
| 7 | #include <re_types.h> |
| 8 | #include <re_mem.h> |
| 9 | #include <re_mbuf.h> |
| 10 | #include <re_sa.h> |
| 11 | #include <re_list.h> |
| 12 | #include <re_hash.h> |
| 13 | #include <re_fmt.h> |
| 14 | #include <re_uri.h> |
| 15 | #include <re_sys.h> |
| 16 | #include <re_tmr.h> |
| 17 | #include <re_udp.h> |
| 18 | #include <re_stun.h> |
| 19 | #include <re_srtp.h> |
| 20 | #include <re_tcp.h> |
| 21 | #include <re_tls.h> |
| 22 | #include <re_msg.h> |
| 23 | #include <re_sip.h> |
| 24 | #include "sip.h" |
| 25 | |
| 26 | |
| 27 | enum { |
| 28 | TCP_ACCEPT_TIMEOUT = 32, |
| 29 | TCP_IDLE_TIMEOUT = 900, |
| 30 | TCP_KEEPALIVE_TIMEOUT = 10, |
| 31 | TCP_KEEPALIVE_INTVAL = 120, |
| 32 | TCP_BUFSIZE_MAX = 65536, |
| 33 | }; |
| 34 | |
| 35 | |
| 36 | struct sip_transport { |
| 37 | struct le le; |
| 38 | struct sa laddr; |
| 39 | struct sip *sip; |
| 40 | struct tls *tls; |
| 41 | void *sock; |
| 42 | enum sip_transp tp; |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | struct sip_conn { |
| 47 | struct le he; |
| 48 | struct list ql; |
| 49 | struct list kal; |
| 50 | struct tmr tmr; |
| 51 | struct tmr tmr_ka; |
| 52 | struct sa laddr; |
| 53 | struct sa paddr; |
| 54 | struct tls_conn *sc; |
| 55 | struct tcp_conn *tc; |
| 56 | struct mbuf *mb; |
| 57 | struct sip *sip; |
| 58 | uint32_t ka_interval; |
| 59 | bool established; |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | struct sip_connqent { |
| 64 | struct le le; |
| 65 | struct mbuf *mb; |
| 66 | struct sip_connqent **qentp; |
| 67 | sip_transp_h *transph; |
| 68 | void *arg; |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | static uint8_t crlfcrlf[4] = {0x0d, 0x0a, 0x0d, 0x0a}; |
| 73 | |
| 74 | |
| 75 | static void internal_transport_handler(int err, void *arg) |
| 76 | { |
| 77 | (void)err; |
| 78 | (void)arg; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | static void transp_destructor(void *arg) |
| 83 | { |
| 84 | struct sip_transport *transp = arg; |
| 85 | |
| 86 | if (transp->tp == SIP_TRANSP_UDP) |
| 87 | udp_handler_set(transp->sock, NULL, NULL); |
| 88 | |
| 89 | list_unlink(&transp->le); |
| 90 | mem_deref(transp->sock); |
| 91 | mem_deref(transp->tls); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | static void conn_destructor(void *arg) |
| 96 | { |
| 97 | struct sip_conn *conn = arg; |
| 98 | |
| 99 | tmr_cancel(&conn->tmr_ka); |
| 100 | tmr_cancel(&conn->tmr); |
| 101 | list_flush(&conn->kal); |
| 102 | list_flush(&conn->ql); |
| 103 | hash_unlink(&conn->he); |
| 104 | mem_deref(conn->sc); |
| 105 | mem_deref(conn->tc); |
| 106 | mem_deref(conn->mb); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | static void qent_destructor(void *arg) |
| 111 | { |
| 112 | struct sip_connqent *qent = arg; |
| 113 | |
| 114 | if (qent->qentp) |
| 115 | *qent->qentp = NULL; |
| 116 | |
| 117 | list_unlink(&qent->le); |
| 118 | mem_deref(qent->mb); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | static const struct sip_transport *transp_find(struct sip *sip, |
| 123 | enum sip_transp tp, |
| 124 | int af, const struct sa *dst) |
| 125 | { |
| 126 | struct le *le; |
| 127 | (void)dst; |
| 128 | |
| 129 | for (le = sip->transpl.head; le; le = le->next) { |
| 130 | |
| 131 | const struct sip_transport *transp = le->data; |
| 132 | |
| 133 | if (transp->tp != tp) |
| 134 | continue; |
| 135 | |
| 136 | if (af != AF_UNSPEC && sa_af(&transp->laddr) != af) |
| 137 | continue; |
| 138 | |
| 139 | return transp; |
| 140 | } |
| 141 | |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | static struct sip_conn *conn_find(struct sip *sip, const struct sa *paddr, |
| 147 | bool secure) |
| 148 | { |
| 149 | struct le *le; |
| 150 | |
| 151 | le = list_head(hash_list(sip->ht_conn, sa_hash(paddr, SA_ALL))); |
| 152 | |
| 153 | for (; le; le = le->next) { |
| 154 | |
| 155 | struct sip_conn *conn = le->data; |
| 156 | |
| 157 | if (!secure != (conn->sc == NULL)) |
| 158 | continue; |
| 159 | |
| 160 | if (!sa_cmp(&conn->paddr, paddr, SA_ALL)) |
| 161 | continue; |
| 162 | |
| 163 | return conn; |
| 164 | } |
| 165 | |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | static void conn_close(struct sip_conn *conn, int err) |
| 171 | { |
| 172 | struct le *le; |
| 173 | |
| 174 | conn->sc = mem_deref(conn->sc); |
| 175 | conn->tc = mem_deref(conn->tc); |
| 176 | tmr_cancel(&conn->tmr_ka); |
| 177 | tmr_cancel(&conn->tmr); |
| 178 | hash_unlink(&conn->he); |
| 179 | |
| 180 | le = list_head(&conn->ql); |
| 181 | |
| 182 | while (le) { |
| 183 | |
| 184 | struct sip_connqent *qent = le->data; |
| 185 | le = le->next; |
| 186 | |
| 187 | if (qent->qentp) { |
| 188 | *qent->qentp = NULL; |
| 189 | qent->qentp = NULL; |
| 190 | } |
| 191 | |
| 192 | qent->transph(err, qent->arg); |
| 193 | list_unlink(&qent->le); |
| 194 | mem_deref(qent); |
| 195 | } |
| 196 | |
| 197 | sip_keepalive_signal(&conn->kal, err); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | static void conn_tmr_handler(void *arg) |
| 202 | { |
| 203 | struct sip_conn *conn = arg; |
| 204 | |
| 205 | conn_close(conn, ETIMEDOUT); |
| 206 | mem_deref(conn); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | static void conn_keepalive_handler(void *arg) |
| 211 | { |
| 212 | struct sip_conn *conn = arg; |
| 213 | struct mbuf mb; |
| 214 | int err; |
| 215 | |
| 216 | mb.buf = crlfcrlf; |
| 217 | mb.size = sizeof(crlfcrlf); |
| 218 | mb.pos = 0; |
| 219 | mb.end = 4; |
| 220 | |
| 221 | err = tcp_send(conn->tc, &mb); |
| 222 | if (err) { |
| 223 | conn_close(conn, err); |
| 224 | mem_deref(conn); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | tmr_start(&conn->tmr, TCP_KEEPALIVE_TIMEOUT * 1000, |
| 229 | conn_tmr_handler, conn); |
| 230 | tmr_start(&conn->tmr_ka, sip_keepalive_wait(conn->ka_interval), |
| 231 | conn_keepalive_handler, conn); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | static void sip_recv(struct sip *sip, const struct sip_msg *msg) |
| 236 | { |
| 237 | struct le *le = sip->lsnrl.head; |
| 238 | |
| 239 | while (le) { |
| 240 | struct sip_lsnr *lsnr = le->data; |
| 241 | |
| 242 | le = le->next; |
| 243 | |
| 244 | if (msg->req != lsnr->req) |
| 245 | continue; |
| 246 | |
| 247 | if (lsnr->msgh(msg, lsnr->arg)) |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | if (msg->req) { |
| 252 | (void)re_fprintf(stderr, "unhandeled request from %J: %r %r\n", |
| 253 | &msg->src, &msg->met, &msg->ruri); |
| 254 | |
| 255 | if (!pl_strcmp(&msg->met, "CANCEL")) |
| 256 | (void)sip_reply(sip, msg, |
| 257 | 481, "Transaction Does Not Exist"); |
| 258 | else |
| 259 | (void)sip_reply(sip, msg, |
| 260 | 501, "Not Implemented"); |
| 261 | } |
| 262 | else { |
| 263 | (void)re_fprintf(stderr, "unhandeled response from %J:" |
| 264 | " %u %r (%r)\n", &msg->src, |
| 265 | msg->scode, &msg->reason, &msg->cseq.met); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | |
| 270 | static void udp_recv_handler(const struct sa *src, struct mbuf *mb, void *arg) |
| 271 | { |
| 272 | struct sip_transport *transp = arg; |
| 273 | struct stun_unknown_attr ua; |
| 274 | struct stun_msg *stun_msg; |
| 275 | struct sip_msg *msg; |
| 276 | int err; |
| 277 | |
| 278 | if (mb->end <= 4) |
| 279 | return; |
| 280 | |
| 281 | if (!stun_msg_decode(&stun_msg, mb, &ua)) { |
| 282 | |
| 283 | if (stun_msg_method(stun_msg) == STUN_METHOD_BINDING) { |
| 284 | |
| 285 | switch (stun_msg_class(stun_msg)) { |
| 286 | |
| 287 | case STUN_CLASS_REQUEST: |
| 288 | (void)stun_reply(IPPROTO_UDP, transp->sock, |
| 289 | src, 0, stun_msg, |
| 290 | NULL, 0, false, 2, |
| 291 | STUN_ATTR_XOR_MAPPED_ADDR, |
| 292 | src, |
| 293 | STUN_ATTR_SOFTWARE, |
| 294 | transp->sip->software); |
| 295 | break; |
| 296 | |
| 297 | default: |
| 298 | (void)stun_ctrans_recv(transp->sip->stun, |
| 299 | stun_msg, &ua); |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | mem_deref(stun_msg); |
| 305 | |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | err = sip_msg_decode(&msg, mb); |
| 310 | if (err) { |
| 311 | (void)re_fprintf(stderr, "sip: msg decode err: %m\n", err); |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | msg->sock = mem_ref(transp->sock); |
| 316 | msg->src = *src; |
| 317 | msg->dst = transp->laddr; |
| 318 | msg->tp = SIP_TRANSP_UDP; |
| 319 | |
| 320 | sip_recv(transp->sip, msg); |
| 321 | |
| 322 | mem_deref(msg); |
| 323 | } |
| 324 | |
| 325 | |
| 326 | static void tcp_recv_handler(struct mbuf *mb, void *arg) |
| 327 | { |
| 328 | struct sip_conn *conn = arg; |
| 329 | size_t pos; |
| 330 | int err = 0; |
| 331 | |
| 332 | if (conn->mb) { |
| 333 | pos = conn->mb->pos; |
| 334 | |
| 335 | conn->mb->pos = conn->mb->end; |
| 336 | |
| 337 | err = mbuf_write_mem(conn->mb, mbuf_buf(mb),mbuf_get_left(mb)); |
| 338 | if (err) |
| 339 | goto out; |
| 340 | |
| 341 | conn->mb->pos = pos; |
| 342 | |
| 343 | if (mbuf_get_left(conn->mb) > TCP_BUFSIZE_MAX) { |
| 344 | err = EOVERFLOW; |
| 345 | goto out; |
| 346 | } |
| 347 | } |
| 348 | else { |
| 349 | conn->mb = mem_ref(mb); |
| 350 | } |
| 351 | |
| 352 | for (;;) { |
| 353 | struct sip_msg *msg; |
| 354 | uint32_t clen; |
| 355 | size_t end; |
| 356 | |
| 357 | if (mbuf_get_left(conn->mb) < 2) |
| 358 | break; |
| 359 | |
| 360 | if (!memcmp(mbuf_buf(conn->mb), "\r\n", 2)) { |
| 361 | |
| 362 | tmr_start(&conn->tmr, TCP_IDLE_TIMEOUT * 1000, |
| 363 | conn_tmr_handler, conn); |
| 364 | |
| 365 | conn->mb->pos += 2; |
| 366 | |
| 367 | if (mbuf_get_left(conn->mb) >= 2 && |
| 368 | !memcmp(mbuf_buf(conn->mb), "\r\n", 2)) { |
| 369 | |
| 370 | struct mbuf mbr; |
| 371 | |
| 372 | conn->mb->pos += 2; |
| 373 | |
| 374 | mbr.buf = crlfcrlf; |
| 375 | mbr.size = sizeof(crlfcrlf); |
| 376 | mbr.pos = 0; |
| 377 | mbr.end = 2; |
| 378 | |
| 379 | err = tcp_send(conn->tc, &mbr); |
| 380 | if (err) |
| 381 | break; |
| 382 | } |
| 383 | |
| 384 | if (mbuf_get_left(conn->mb)) |
| 385 | continue; |
| 386 | |
| 387 | conn->mb = mem_deref(conn->mb); |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | pos = conn->mb->pos; |
| 392 | |
| 393 | err = sip_msg_decode(&msg, conn->mb); |
| 394 | if (err) { |
| 395 | if (err == ENODATA) |
| 396 | err = 0; |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | if (!msg->clen.p) { |
| 401 | mem_deref(msg); |
| 402 | err = EBADMSG; |
| 403 | break; |
| 404 | } |
| 405 | |
| 406 | clen = pl_u32(&msg->clen); |
| 407 | |
| 408 | if (mbuf_get_left(conn->mb) < clen) { |
| 409 | conn->mb->pos = pos; |
| 410 | mem_deref(msg); |
| 411 | break; |
| 412 | } |
| 413 | |
| 414 | tmr_start(&conn->tmr, TCP_IDLE_TIMEOUT * 1000, |
| 415 | conn_tmr_handler, conn); |
| 416 | |
| 417 | end = conn->mb->end; |
| 418 | |
| 419 | msg->mb->end = msg->mb->pos + clen; |
| 420 | msg->sock = mem_ref(conn); |
| 421 | msg->src = conn->paddr; |
| 422 | msg->dst = conn->laddr; |
| 423 | msg->tp = conn->sc ? SIP_TRANSP_TLS : SIP_TRANSP_TCP; |
| 424 | |
| 425 | sip_recv(conn->sip, msg); |
| 426 | mem_deref(msg); |
| 427 | |
| 428 | if (end <= conn->mb->end) { |
| 429 | conn->mb = mem_deref(conn->mb); |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | mb = mbuf_alloc(end - conn->mb->end); |
| 434 | if (!mb) { |
| 435 | err = ENOMEM; |
| 436 | goto out; |
| 437 | } |
| 438 | |
| 439 | (void)mbuf_write_mem(mb, &conn->mb->buf[conn->mb->end], |
| 440 | end - conn->mb->end); |
| 441 | |
| 442 | mb->pos = 0; |
| 443 | |
| 444 | mem_deref(conn->mb); |
| 445 | conn->mb = mb; |
| 446 | } |
| 447 | |
| 448 | out: |
| 449 | if (err) { |
| 450 | conn_close(conn, err); |
| 451 | mem_deref(conn); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | |
| 456 | static void tcp_estab_handler(void *arg) |
| 457 | { |
| 458 | struct sip_conn *conn = arg; |
| 459 | struct le *le; |
| 460 | int err; |
| 461 | |
| 462 | #ifdef WIN32 |
| 463 | tcp_conn_local_get(conn->tc, &conn->laddr); |
| 464 | #endif |
| 465 | |
| 466 | conn->established = true; |
| 467 | |
| 468 | le = list_head(&conn->ql); |
| 469 | |
| 470 | while (le) { |
| 471 | |
| 472 | struct sip_connqent *qent = le->data; |
| 473 | le = le->next; |
| 474 | |
| 475 | if (qent->qentp) { |
| 476 | *qent->qentp = NULL; |
| 477 | qent->qentp = NULL; |
| 478 | } |
| 479 | |
| 480 | err = tcp_send(conn->tc, qent->mb); |
| 481 | if (err) |
| 482 | qent->transph(err, qent->arg); |
| 483 | |
| 484 | list_unlink(&qent->le); |
| 485 | mem_deref(qent); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | |
| 490 | static void tcp_close_handler(int err, void *arg) |
| 491 | { |
| 492 | struct sip_conn *conn = arg; |
| 493 | |
| 494 | conn_close(conn, err ? err : ECONNRESET); |
| 495 | mem_deref(conn); |
| 496 | } |
| 497 | |
| 498 | |
| 499 | static void tcp_connect_handler(const struct sa *paddr, void *arg) |
| 500 | { |
| 501 | struct sip_transport *transp = arg; |
| 502 | struct sip_conn *conn; |
| 503 | int err; |
| 504 | |
| 505 | conn = mem_zalloc(sizeof(*conn), conn_destructor); |
| 506 | if (!conn) { |
| 507 | err = ENOMEM; |
| 508 | goto out; |
| 509 | } |
| 510 | |
| 511 | hash_append(transp->sip->ht_conn, sa_hash(paddr, SA_ALL), |
| 512 | &conn->he, conn); |
| 513 | |
| 514 | conn->paddr = *paddr; |
| 515 | conn->sip = transp->sip; |
| 516 | |
| 517 | err = tcp_accept(&conn->tc, transp->sock, tcp_estab_handler, |
| 518 | tcp_recv_handler, tcp_close_handler, conn); |
| 519 | if (err) |
| 520 | goto out; |
| 521 | |
| 522 | err = tcp_conn_local_get(conn->tc, &conn->laddr); |
| 523 | if (err) |
| 524 | goto out; |
| 525 | |
| 526 | #ifdef USE_TLS |
| 527 | if (transp->tls) { |
| 528 | err = tls_start_tcp(&conn->sc, transp->tls, conn->tc, 0); |
| 529 | if (err) |
| 530 | goto out; |
| 531 | } |
| 532 | #endif |
| 533 | |
| 534 | tmr_start(&conn->tmr, TCP_ACCEPT_TIMEOUT * 1000, |
| 535 | conn_tmr_handler, conn); |
| 536 | |
| 537 | out: |
| 538 | if (err) { |
| 539 | tcp_reject(transp->sock); |
| 540 | mem_deref(conn); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | |
| 545 | static int conn_send(struct sip_connqent **qentp, struct sip *sip, bool secure, |
| 546 | const struct sa *dst, struct mbuf *mb, |
| 547 | sip_transp_h *transph, void *arg) |
| 548 | { |
| 549 | struct sip_conn *conn, *new_conn = NULL; |
| 550 | struct sip_connqent *qent; |
| 551 | int err = 0; |
| 552 | |
| 553 | conn = conn_find(sip, dst, secure); |
| 554 | if (conn) { |
| 555 | if (!conn->established) |
| 556 | goto enqueue; |
| 557 | |
| 558 | return tcp_send(conn->tc, mb); |
| 559 | } |
| 560 | |
| 561 | new_conn = conn = mem_zalloc(sizeof(*conn), conn_destructor); |
| 562 | if (!conn) |
| 563 | return ENOMEM; |
| 564 | |
| 565 | hash_append(sip->ht_conn, sa_hash(dst, SA_ALL), &conn->he, conn); |
| 566 | conn->paddr = *dst; |
| 567 | conn->sip = sip; |
| 568 | |
| 569 | err = tcp_connect(&conn->tc, dst, tcp_estab_handler, tcp_recv_handler, |
| 570 | tcp_close_handler, conn); |
| 571 | if (err) |
| 572 | goto out; |
| 573 | |
| 574 | err = tcp_conn_local_get(conn->tc, &conn->laddr); |
| 575 | if (err) |
| 576 | goto out; |
| 577 | |
| 578 | #ifdef USE_TLS |
| 579 | if (secure) { |
| 580 | const struct sip_transport *transp; |
| 581 | |
| 582 | transp = transp_find(sip, SIP_TRANSP_TLS, sa_af(dst), dst); |
| 583 | if (!transp || !transp->tls) { |
| 584 | err = EPROTONOSUPPORT; |
| 585 | goto out; |
| 586 | } |
| 587 | |
| 588 | err = tls_start_tcp(&conn->sc, transp->tls, conn->tc, 0); |
| 589 | if (err) |
| 590 | goto out; |
| 591 | } |
| 592 | #endif |
| 593 | |
| 594 | tmr_start(&conn->tmr, TCP_IDLE_TIMEOUT * 1000, conn_tmr_handler, conn); |
| 595 | |
| 596 | enqueue: |
| 597 | qent = mem_zalloc(sizeof(*qent), qent_destructor); |
| 598 | if (!qent) { |
| 599 | err = ENOMEM; |
| 600 | goto out; |
| 601 | |
| 602 | } |
| 603 | |
| 604 | list_append(&conn->ql, &qent->le, qent); |
| 605 | qent->mb = mem_ref(mb); |
| 606 | qent->transph = transph ? transph : internal_transport_handler; |
| 607 | qent->arg = arg; |
| 608 | |
| 609 | if (qentp) { |
| 610 | qent->qentp = qentp; |
| 611 | *qentp = qent; |
| 612 | } |
| 613 | |
| 614 | out: |
| 615 | if (err) |
| 616 | mem_deref(new_conn); |
| 617 | |
| 618 | return err; |
| 619 | } |
| 620 | |
| 621 | |
| 622 | int sip_transp_init(struct sip *sip, uint32_t sz) |
| 623 | { |
| 624 | return hash_alloc(&sip->ht_conn, sz); |
| 625 | } |
| 626 | |
| 627 | |
| 628 | /** |
| 629 | * Add a SIP transport |
| 630 | * |
| 631 | * @param sip SIP stack instance |
| 632 | * @param tp SIP Transport |
| 633 | * @param laddr Local network address |
| 634 | * @param ... Optional transport parameters such as TLS context |
| 635 | * |
| 636 | * @return 0 if success, otherwise errorcode |
| 637 | */ |
| 638 | int sip_transp_add(struct sip *sip, enum sip_transp tp, |
| 639 | const struct sa *laddr, ...) |
| 640 | { |
| 641 | struct sip_transport *transp; |
| 642 | struct tls *tls; |
| 643 | va_list ap; |
| 644 | int err; |
| 645 | |
| 646 | if (!sip || !laddr || !sa_isset(laddr, SA_ADDR)) |
| 647 | return EINVAL; |
| 648 | |
| 649 | transp = mem_zalloc(sizeof(*transp), transp_destructor); |
| 650 | if (!transp) |
| 651 | return ENOMEM; |
| 652 | |
| 653 | list_append(&sip->transpl, &transp->le, transp); |
| 654 | transp->sip = sip; |
| 655 | transp->tp = tp; |
| 656 | |
| 657 | va_start(ap, laddr); |
| 658 | |
| 659 | switch (tp) { |
| 660 | |
| 661 | case SIP_TRANSP_UDP: |
| 662 | err = udp_listen((struct udp_sock **)&transp->sock, laddr, |
| 663 | udp_recv_handler, transp); |
| 664 | if (err) |
| 665 | break; |
| 666 | |
| 667 | err = udp_local_get(transp->sock, &transp->laddr); |
| 668 | break; |
| 669 | |
| 670 | case SIP_TRANSP_TLS: |
| 671 | tls = va_arg(ap, struct tls *); |
| 672 | if (!tls) { |
| 673 | err = EINVAL; |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | transp->tls = mem_ref(tls); |
| 678 | |
| 679 | /*@fallthrough@*/ |
| 680 | |
| 681 | case SIP_TRANSP_TCP: |
| 682 | err = tcp_listen((struct tcp_sock **)&transp->sock, laddr, |
| 683 | tcp_connect_handler, transp); |
| 684 | if (err) |
| 685 | break; |
| 686 | |
| 687 | err = tcp_sock_local_get(transp->sock, &transp->laddr); |
| 688 | break; |
| 689 | |
| 690 | default: |
| 691 | err = EPROTONOSUPPORT; |
| 692 | break; |
| 693 | } |
| 694 | |
| 695 | va_end(ap); |
| 696 | |
| 697 | if (err) |
| 698 | mem_deref(transp); |
| 699 | |
| 700 | return err; |
| 701 | } |
| 702 | |
| 703 | |
| 704 | /** |
| 705 | * Flush all transports of a SIP stack instance |
| 706 | * |
| 707 | * @param sip SIP stack instance |
| 708 | */ |
| 709 | void sip_transp_flush(struct sip *sip) |
| 710 | { |
| 711 | if (!sip) |
| 712 | return; |
| 713 | |
| 714 | hash_flush(sip->ht_conn); |
| 715 | list_flush(&sip->transpl); |
| 716 | } |
| 717 | |
| 718 | |
| 719 | int sip_transp_send(struct sip_connqent **qentp, struct sip *sip, void *sock, |
| 720 | enum sip_transp tp, const struct sa *dst, struct mbuf *mb, |
| 721 | sip_transp_h *transph, void *arg) |
| 722 | { |
| 723 | const struct sip_transport *transp; |
| 724 | struct sip_conn *conn; |
| 725 | bool secure = false; |
| 726 | int err; |
| 727 | |
| 728 | if (!sip || !dst || !mb) |
| 729 | return EINVAL; |
| 730 | |
| 731 | switch (tp) { |
| 732 | |
| 733 | case SIP_TRANSP_UDP: |
| 734 | if (!sock) { |
| 735 | transp = transp_find(sip, tp, sa_af(dst), dst); |
| 736 | if (!transp) |
| 737 | return EPROTONOSUPPORT; |
| 738 | |
| 739 | sock = transp->sock; |
| 740 | } |
| 741 | |
| 742 | err = udp_send(sock, dst, mb); |
| 743 | break; |
| 744 | |
| 745 | case SIP_TRANSP_TLS: |
| 746 | secure = true; |
| 747 | /*@fallthrough@*/ |
| 748 | |
| 749 | case SIP_TRANSP_TCP: |
| 750 | conn = sock; |
| 751 | |
| 752 | if (conn && conn->tc) |
| 753 | err = tcp_send(conn->tc, mb); |
| 754 | else |
| 755 | err = conn_send(qentp, sip, secure, dst, mb, |
| 756 | transph, arg); |
| 757 | break; |
| 758 | |
| 759 | default: |
| 760 | err = EPROTONOSUPPORT; |
| 761 | break; |
| 762 | } |
| 763 | |
| 764 | return err; |
| 765 | } |
| 766 | |
| 767 | |
| 768 | int sip_transp_laddr(struct sip *sip, struct sa *laddr, enum sip_transp tp, |
| 769 | const struct sa *dst) |
| 770 | { |
| 771 | const struct sip_transport *transp; |
| 772 | |
| 773 | if (!sip || !laddr) |
| 774 | return EINVAL; |
| 775 | |
| 776 | transp = transp_find(sip, tp, sa_af(dst), dst); |
| 777 | if (!transp) |
| 778 | return EPROTONOSUPPORT; |
| 779 | |
| 780 | *laddr = transp->laddr; |
| 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | |
| 786 | bool sip_transp_supported(struct sip *sip, enum sip_transp tp, int af) |
| 787 | { |
| 788 | if (!sip) |
| 789 | return false; |
| 790 | |
| 791 | return transp_find(sip, tp, af, NULL) != NULL; |
| 792 | } |
| 793 | |
| 794 | |
| 795 | /** |
| 796 | * Check if network address is part of SIP transports |
| 797 | * |
| 798 | * @param sip SIP stack instance |
| 799 | * @param tp SIP transport |
| 800 | * @param laddr Local network address to check |
| 801 | * |
| 802 | * @return True if part of SIP transports, otherwise false |
| 803 | */ |
| 804 | bool sip_transp_isladdr(const struct sip *sip, enum sip_transp tp, |
| 805 | const struct sa *laddr) |
| 806 | { |
| 807 | struct le *le; |
| 808 | |
| 809 | if (!sip || !laddr) |
| 810 | return false; |
| 811 | |
| 812 | for (le=sip->transpl.head; le; le=le->next) { |
| 813 | |
| 814 | const struct sip_transport *transp = le->data; |
| 815 | |
| 816 | if (tp != SIP_TRANSP_NONE && transp->tp != tp) |
| 817 | continue; |
| 818 | |
| 819 | if (!sa_cmp(&transp->laddr, laddr, SA_ALL)) |
| 820 | continue; |
| 821 | |
| 822 | return true; |
| 823 | } |
| 824 | |
| 825 | return false; |
| 826 | } |
| 827 | |
| 828 | |
| 829 | /** |
| 830 | * Get the name of a given SIP Transport |
| 831 | * |
| 832 | * @param tp SIP Transport |
| 833 | * |
| 834 | * @return Name of the corresponding SIP Transport |
| 835 | */ |
| 836 | const char *sip_transp_name(enum sip_transp tp) |
| 837 | { |
| 838 | switch (tp) { |
| 839 | |
| 840 | case SIP_TRANSP_UDP: return "UDP"; |
| 841 | case SIP_TRANSP_TCP: return "TCP"; |
| 842 | case SIP_TRANSP_TLS: return "TLS"; |
| 843 | default: return "???"; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | |
| 848 | const char *sip_transp_srvid(enum sip_transp tp) |
| 849 | { |
| 850 | switch (tp) { |
| 851 | |
| 852 | case SIP_TRANSP_UDP: return "_sip._udp"; |
| 853 | case SIP_TRANSP_TCP: return "_sip._tcp"; |
| 854 | case SIP_TRANSP_TLS: return "_sips._tcp"; |
| 855 | default: return "???"; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | |
| 860 | /** |
| 861 | * Get the transport parameters for a given SIP Transport |
| 862 | * |
| 863 | * @param tp SIP Transport |
| 864 | * |
| 865 | * @return Transport parameters of the corresponding SIP Transport |
| 866 | */ |
| 867 | const char *sip_transp_param(enum sip_transp tp) |
| 868 | { |
| 869 | switch (tp) { |
| 870 | |
| 871 | case SIP_TRANSP_UDP: return ""; |
| 872 | case SIP_TRANSP_TCP: return ";transport=tcp"; |
| 873 | case SIP_TRANSP_TLS: return ";transport=tls"; |
| 874 | default: return ""; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | |
| 879 | bool sip_transp_reliable(enum sip_transp tp) |
| 880 | { |
| 881 | switch (tp) { |
| 882 | |
| 883 | case SIP_TRANSP_UDP: return false; |
| 884 | case SIP_TRANSP_TCP: return true; |
| 885 | case SIP_TRANSP_TLS: return true; |
| 886 | default: return false; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | |
| 891 | /** |
| 892 | * Get the default port number for a given SIP Transport |
| 893 | * |
| 894 | * @param tp SIP Transport |
| 895 | * @param port Port number |
| 896 | * |
| 897 | * @return Corresponding port number |
| 898 | */ |
| 899 | uint16_t sip_transp_port(enum sip_transp tp, uint16_t port) |
| 900 | { |
| 901 | if (port) |
| 902 | return port; |
| 903 | |
| 904 | switch (tp) { |
| 905 | |
| 906 | case SIP_TRANSP_UDP: return SIP_PORT; |
| 907 | case SIP_TRANSP_TCP: return SIP_PORT; |
| 908 | case SIP_TRANSP_TLS: return SIP_PORT_TLS; |
| 909 | default: return 0; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | |
| 914 | static bool debug_handler(struct le *le, void *arg) |
| 915 | { |
| 916 | const struct sip_transport *transp = le->data; |
| 917 | struct re_printf *pf = arg; |
| 918 | |
| 919 | (void)re_hprintf(pf, " %J (%s)\n", |
| 920 | &transp->laddr, |
| 921 | sip_transp_name(transp->tp)); |
| 922 | |
| 923 | return false; |
| 924 | } |
| 925 | |
| 926 | |
| 927 | int sip_transp_debug(struct re_printf *pf, const struct sip *sip) |
| 928 | { |
| 929 | int err; |
| 930 | |
| 931 | err = re_hprintf(pf, "transports:\n"); |
| 932 | list_apply(&sip->transpl, true, debug_handler, pf); |
| 933 | |
| 934 | return err; |
| 935 | } |
| 936 | |
| 937 | |
| 938 | /** |
| 939 | * Get the TCP Connection from a SIP Message |
| 940 | * |
| 941 | * @param msg SIP Message |
| 942 | * |
| 943 | * @return TCP Connection if reliable transport, otherwise NULL |
| 944 | */ |
| 945 | struct tcp_conn *sip_msg_tcpconn(const struct sip_msg *msg) |
| 946 | { |
| 947 | if (!msg || !msg->sock) |
| 948 | return NULL; |
| 949 | |
| 950 | switch (msg->tp) { |
| 951 | |
| 952 | case SIP_TRANSP_TCP: |
| 953 | case SIP_TRANSP_TLS: |
| 954 | return ((struct sip_conn *)msg->sock)->tc; |
| 955 | |
| 956 | default: |
| 957 | return NULL; |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | |
| 962 | int sip_keepalive_tcp(struct sip_keepalive *ka, struct sip_conn *conn, |
| 963 | uint32_t interval) |
| 964 | { |
| 965 | if (!ka || !conn) |
| 966 | return EINVAL; |
| 967 | |
| 968 | if (!conn->tc || !conn->established) |
| 969 | return ENOTCONN; |
| 970 | |
| 971 | list_append(&conn->kal, &ka->le, ka); |
| 972 | |
| 973 | if (!tmr_isrunning(&conn->tmr_ka)) { |
| 974 | |
| 975 | interval = MAX(interval ? interval : TCP_KEEPALIVE_INTVAL, |
| 976 | TCP_KEEPALIVE_TIMEOUT * 2); |
| 977 | |
| 978 | conn->ka_interval = interval; |
| 979 | |
| 980 | tmr_start(&conn->tmr_ka, sip_keepalive_wait(conn->ka_interval), |
| 981 | conn_keepalive_handler, conn); |
| 982 | } |
| 983 | |
| 984 | return 0; |
| 985 | } |