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