-
Notifications
You must be signed in to change notification settings - Fork 564
/
msrp_ua.c
2471 lines (2036 loc) · 59.4 KB
/
msrp_ua.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
/*
* Copyright (C) 2022 - OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../mem/mem.h"
#include "../../lib/hash.h"
#include "../../socket_info.h"
#include "../../timer.h"
#include "../../parser/sdp/sdp.h"
#include "../../parser/parse_from.h"
#include "../../msg_translator.h"
#include "../../evi/evi_modules.h"
#include "../../mi/mi.h"
#include "../uac_auth/uac_auth.h"
#include "../../lib/digest_auth/digest_auth.h"
#include "../proto_msrp/msrp_api.h"
#include "msrp_ua.h"
#define MSRP_DEFAULT_PORT 2855
#define append_string(_d,_s,_len) \
do{\
memcpy((_d),(_s),(_len));\
(_d) += (_len);\
}while(0)
static int mod_init(void);
static void destroy(void);
mi_response_t *msrpua_mi_end(const mi_params_t *params,
struct mi_handler *async_hdl);
mi_response_t *msrpua_mi_list(const mi_params_t *params,
struct mi_handler *async_hdl);
mi_response_t *msrpua_mi_send_msg(const mi_params_t *params,
struct mi_handler *async_hdl);
mi_response_t *msrpua_mi_start_session(const mi_params_t *params,
struct mi_handler *async_hdl);
void load_msrp_ua(struct msrp_ua_binds *binds);
/* proto_msrp binds */
struct msrp_binds msrp_api;
/* proto_msrp registration handler */
void *msrp_hdl;
b2b_api_t b2b_api;
str my_msrp_uri_str;
struct msrp_url my_msrp_uri;
struct socket_info *msrp_sock;
str adv_contact;
int msrpua_sessions_hsize = 10;
gen_hash_t *msrpua_sessions;
int cleanup_interval = 60;
int max_duration = 12*3600;
str relay_uri_str;
struct msrp_url relay_uri;
uac_auth_api_t uac_auth_api;
static str msrpua_mod_name = str_init("msrp_ua");
gen_lock_t *sdp_id_lock;
int *next_sdp_id;
static event_id_t evi_sess_new_id = EVI_ERROR;
static event_id_t evi_sess_end_id = EVI_ERROR;
static event_id_t evi_msg_rcv_id = EVI_ERROR;
static str evi_sess_new_name = str_init("E_MSRP_SESSION_NEW");
static str evi_sess_end_name = str_init("E_MSRP_SESSION_END");
static str evi_msg_rcv_name = str_init("E_MSRP_MSG_RECEIVED");
static evi_params_p evi_sess_params;
static evi_params_p evi_msg_rcv_params;
static evi_param_p evi_sess_from_p, evi_sess_to_p, evi_sess_ruri_p,
evi_sess_sid_p, evi_sess_types_p;
static evi_param_p evi_msg_rcv_sid_p, evi_msg_rcv_ctype_p, evi_msg_rcv_body_p;
static str evi_sess_from_pname = str_init("from_uri");
static str evi_sess_to_pname = str_init("to_uri");
static str evi_sess_ruri_pname = str_init("ruri");
static str evi_sess_sid_pname = str_init("session_id");
static str evi_sess_types_pname = str_init("content_types");
static str evi_msg_rcv_sid_pname = str_init("session_id");
static str evi_msg_rcv_ctype_pname = str_init("content_type");
static str evi_msg_rcv_body_pname = str_init("body");
static param_export_t params[] = {
{"hash_size", INT_PARAM, &msrpua_sessions_hsize},
{"cleanup_interval", INT_PARAM, &cleanup_interval},
{"max_duration", INT_PARAM, &max_duration},
{"my_uri", STR_PARAM, &my_msrp_uri_str},
{"advertised_contact", STR_PARAM, &adv_contact.s},
{"relay_uri", STR_PARAM, &relay_uri_str.s},
};
static mi_export_t mi_cmds[] = {
{ "msrp_ua_send_message", 0, 0, 0, {
{msrpua_mi_send_msg, {"session_id", 0}},
{msrpua_mi_send_msg, {"session_id", "mime", "body", 0}},
{EMPTY_MI_RECIPE}}
},
{ "msrp_ua_end_session", 0, 0, 0, {
{msrpua_mi_end, {"session_id", 0}},
{EMPTY_MI_RECIPE}}
},
{ "msrp_ua_list_sessions", 0, 0, 0, {
{msrpua_mi_list, {0}},
{EMPTY_MI_RECIPE}}
},
{ "msrp_ua_start_session", 0, 0, 0, {
{msrpua_mi_start_session, {"content_types", "from_uri", "to_uri", "ruri", 0}},
{EMPTY_MI_RECIPE}}
},
{EMPTY_MI_EXPORT}
};
static int msrpua_answer(struct sip_msg *msg, str *content_types);
static cmd_export_t cmds[]=
{
{"msrp_ua_answer", (cmd_function)msrpua_answer, {
{CMD_PARAM_STR, 0, 0},
{0,0,0}},
REQUEST_ROUTE},
{"load_msrp_ua", (cmd_function)load_msrp_ua, {{0,0,0}}, 0},
{0,0,{{0,0,0}},0}
};
static module_dependency_t *get_deps_relay_uri(param_export_t *param)
{
if (*(int *)param->param_pointer == 0)
return NULL;
return alloc_module_dep(MOD_TYPE_DEFAULT, "uac_auth", DEP_ABORT);
}
static dep_export_t deps = {
{ /* OpenSIPS module dependencies */
{ MOD_TYPE_DEFAULT, "proto_msrp" , DEP_ABORT },
{ MOD_TYPE_DEFAULT, "b2b_entities", DEP_ABORT },
{ MOD_TYPE_NULL, NULL, 0 },
},
{ /* modparam dependencies */
{ "relay_uri", get_deps_relay_uri },
{ NULL, NULL },
},
};
struct module_exports exports = {
"msrp_ua", /* module name*/
MOD_TYPE_DEFAULT,/* class of this module */
MODULE_VERSION,
DEFAULT_DLFLAGS, /* dlopen flags */
0, /* load function */
&deps, /* OpenSIPS module dependencies */
cmds, /* exported functions */
0, /* exported async functions */
params, /* module parameters */
0, /* exported statistics */
mi_cmds, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* exported transformations */
0, /* extra processes */
0, /* module pre-initialization function */
mod_init, /* module initialization function */
0, /* response function */
destroy, /* destroy function */
0, /* per-child init function */
0 /* reload confirm function */
};
static int handle_msrp_request(struct msrp_msg *req, void *hdl_param);
static int handle_msrp_reply(struct msrp_msg *rpl, struct msrp_cell *tran,
void *trans_param, void *hdl_param);
static void clean_msrpua_sessions(unsigned int ticks,void *param);
static int msrpua_evi_init(void)
{
evi_sess_new_id = evi_publish_event(evi_sess_new_name);
if (evi_sess_new_id == EVI_ERROR) {
LM_ERR("cannot register event\n");
return -1;
}
evi_sess_end_id = evi_publish_event(evi_sess_end_name);
if (evi_sess_end_id == EVI_ERROR) {
LM_ERR("cannot register event\n");
return -1;
}
evi_msg_rcv_id = evi_publish_event(evi_msg_rcv_name);
if (evi_msg_rcv_id == EVI_ERROR) {
LM_ERR("cannot register event\n");
return -1;
}
evi_sess_params = pkg_malloc(sizeof(evi_params_t));
if (evi_sess_params == NULL) {
LM_ERR("no more pkg mem\n");
return -1;
}
memset(evi_sess_params, 0, sizeof(evi_params_t));
evi_msg_rcv_params = pkg_malloc(sizeof(evi_params_t));
if (evi_msg_rcv_params == NULL) {
LM_ERR("no more pkg mem\n");
return -1;
}
memset(evi_msg_rcv_params, 0, sizeof(evi_params_t));
evi_sess_from_p = evi_param_create(evi_sess_params, &evi_sess_from_pname);
if (evi_sess_from_p == NULL)
goto error;
evi_sess_to_p = evi_param_create(evi_sess_params, &evi_sess_to_pname);
if (evi_sess_to_p == NULL)
goto error;
evi_sess_ruri_p = evi_param_create(evi_sess_params, &evi_sess_ruri_pname);
if (evi_sess_ruri_p == NULL)
goto error;
evi_sess_sid_p = evi_param_create(evi_sess_params, &evi_sess_sid_pname);
if (evi_sess_sid_p == NULL)
goto error;
evi_sess_types_p = evi_param_create(evi_sess_params, &evi_sess_types_pname);
if (evi_sess_types_p == NULL)
goto error;
evi_msg_rcv_sid_p = evi_param_create(evi_msg_rcv_params,
&evi_msg_rcv_sid_pname);
if (evi_msg_rcv_sid_p == NULL)
goto error;
evi_msg_rcv_ctype_p = evi_param_create(evi_msg_rcv_params,
&evi_msg_rcv_ctype_pname);
if (evi_msg_rcv_ctype_p == NULL)
goto error;
evi_msg_rcv_body_p = evi_param_create(evi_msg_rcv_params,
&evi_msg_rcv_body_pname);
if (evi_msg_rcv_body_p == NULL)
goto error;
return 0;
error:
LM_ERR("cannot create event parameter\n");
return -1;
}
static int mod_init(void)
{
LM_INFO("initializing...\n");
char *p, *end;
if (!my_msrp_uri_str.s) {
LM_ERR("'my_uri' parameter not set\n");
return -1;
}
if (adv_contact.s)
adv_contact.len = strlen(adv_contact.s);
my_msrp_uri_str.len = strlen(my_msrp_uri_str.s);
end = my_msrp_uri_str.s + my_msrp_uri_str.len;
p = parse_msrp_url(my_msrp_uri_str.s, end, &my_msrp_uri);
if (!p) {
LM_ERR("Failed to parse MSRP URI in 'my_uri' parameter\n");
return -1;
}
if (my_msrp_uri.port_no == 0) {
LM_INFO("Explicit port number not provided in 'my_uri',"
"using default %d\n", MSRP_DEFAULT_PORT);
my_msrp_uri.port_no = MSRP_DEFAULT_PORT;
}
msrp_sock = grep_sock_info(&my_msrp_uri.host, my_msrp_uri.port_no,
my_msrp_uri.secured ? PROTO_MSRPS:PROTO_MSRP);
if (!msrp_sock) {
LM_ERR("non-local socket <%.*s>\n",
my_msrp_uri.host.len, my_msrp_uri.host.s);
return 1;
}
if (relay_uri_str.s) {
relay_uri_str.len = strlen(relay_uri_str.s);
end = relay_uri_str.s + relay_uri_str.len;
p = parse_msrp_url(relay_uri_str.s, end, &relay_uri);
if (!p) {
LM_ERR("Failed to parse MSRP URI in 'relay_uri' parameter\n");
return -1;
}
if(load_uac_auth_api(&uac_auth_api)<0){
LM_ERR("can't load UAC_AUTH API, needed for uac_auth()\n");
return -1;
}
}
/* load MSRP API */
if(load_msrp_api(&msrp_api)< 0){
LM_ERR("can't load MSRP functions\n");
return -1;
}
msrp_hdl = msrp_api.register_msrp_handler(&my_msrp_uri.host,
my_msrp_uri.port_no, my_msrp_uri.secured, handle_msrp_request,
handle_msrp_reply, NULL);
if (!msrp_hdl) {
LM_ERR("Failed to register MSRP handler\n");
return -1;
}
if (load_b2b_api(&b2b_api) < 0) {
LM_ERR("Failed to load b2b api\n");
return -1;
}
if (msrpua_sessions_hsize < 1 || msrpua_sessions_hsize > 20) {
LM_ERR("hash size should be between 1 and 20\n");
return -1;
}
msrpua_sessions_hsize = 1 << msrpua_sessions_hsize;
msrpua_sessions = hash_init(msrpua_sessions_hsize);
if (!msrpua_sessions) {
LM_ERR("Failed to init MSRP sessions table\n");
return -1;
}
register_timer("msrpua-expire", clean_msrpua_sessions, NULL,
cleanup_interval, TIMER_FLAG_DELAY_ON_DELAY);
sdp_id_lock = lock_alloc();
if (!sdp_id_lock) {
LM_ERR("no more shm memory\n");
return -1;
}
if (!lock_init(sdp_id_lock)) {
LM_ERR("failed to init lock\n");
return -1;
}
next_sdp_id = shm_malloc(sizeof *next_sdp_id);
if (!next_sdp_id) {
LM_ERR("no more shm memory\n");
return -1;
}
*next_sdp_id = time(NULL);
if (msrpua_evi_init() < 0) {
LM_ERR("Failed to init events\n");
return -1;
}
return 0;
}
static void free_msrpua_session(void *val)
{
struct msrpua_session *sess = (struct msrpua_session *)val;
if (sess->use_path.s)
shm_free(sess->use_path.s);
if (sess->peer_path.s)
shm_free(sess->peer_path.s);
free_msrp_path_shm(sess->peer_path_parsed);
if (sess->peer_accept_types.s)
shm_free(sess->peer_accept_types.s);
if (sess->dlginfo)
shm_free(sess->dlginfo);
shm_free(sess);
}
static void msrpua_delete_session(struct msrpua_session *sess)
{
LM_DBG("Deleting session [%.*s]\n", sess->session_id.len, sess->session_id.s);
if (sess->b2b_key.s)
b2b_api.entity_delete(sess->b2b_type, &sess->b2b_key, sess->dlginfo, 1, 1);
hash_remove_key(msrpua_sessions, sess->session_id);
free_msrpua_session(sess);
}
void msrpua_evi_destroy(void)
{
evi_free_params(evi_sess_params);
}
static void destroy(void)
{
hash_destroy(msrpua_sessions, free_msrpua_session);
lock_destroy(sdp_id_lock);
lock_dealloc(sdp_id_lock);
msrpua_evi_destroy();
}
static inline int msrpua_b2b_reply(int et, str *b2b_key, int method,
int code, str *reason, str *body)
{
b2b_rpl_data_t rpl_data;
static str ct_type_sdp_str = str_init("Content-Type: application/sdp\r\n");
memset(&rpl_data, 0, sizeof(b2b_rpl_data_t));
rpl_data.et = et;
rpl_data.b2b_key = b2b_key;
rpl_data.method = method;
rpl_data.code = code;
rpl_data.text = reason;
rpl_data.body = body;
if (body)
rpl_data.extra_headers = &ct_type_sdp_str;
return b2b_api.send_reply(&rpl_data);
}
static inline int msrpua_b2b_request(int et, str *b2b_key, str *method)
{
b2b_req_data_t req_data;
memset(&req_data, 0, sizeof(b2b_req_data_t));
req_data.et = et;
req_data.b2b_key = b2b_key;
req_data.method = method;
return b2b_api.send_request(&req_data);
}
#define MESSAGE_STR "message"
#define TCP_MSRP_STR "TCP/MSRP"
#define TLS_MSRP_STR "TCP/TLS/MSRP"
static sdp_stream_cell_t *get_sdp_msrp_stream(sdp_info_t *sdp)
{
sdp_session_cell_t* sdp_session;
sdp_stream_cell_t* sdp_stream;
int sdp_session_num = 0, sdp_stream_num;
for (;;) {
sdp_session = get_sdp_session(sdp, sdp_session_num);
if (!sdp_session)
break;
sdp_stream_num = 0;
for (;;) {
sdp_stream = get_sdp_stream(sdp, sdp_session_num,
sdp_stream_num);
if (!sdp_stream)
break;
if (str_match(&sdp_stream->media, const_str(MESSAGE_STR)) &&
(str_match(&sdp_stream->transport, const_str(TCP_MSRP_STR)) ||
str_match(&sdp_stream->transport, const_str(TLS_MSRP_STR))))
return sdp_stream;
sdp_stream_num++;
}
sdp_session_num++;
}
return NULL;
}
static int get_sdp_peer_info(struct sip_msg *msg, str *peer_accept_types,
str *peer_path)
{
sdp_info_t *sdp;
sdp_stream_cell_t *msrp_stream;
if (!(sdp = parse_sdp(msg))) {
LM_DBG("failed to parse SDP\n");
return -1;
}
if (!(msrp_stream = get_sdp_msrp_stream(sdp))) {
LM_ERR("No MSRP media stream in SDP\n");
return -1;
}
*peer_accept_types = msrp_stream->accept_types;
*peer_path = msrp_stream->path;
return 0;
}
static inline int match_mimes(str *a_type, str *a_subtype,
str *b_type, str *b_subtype)
{
if (a_type->len != b_type->len || memcmp(a_type->s, b_type->s, b_type->len))
return 0;
if (a_subtype->s[0] == '*' || b_subtype->s[0] == '*')
return 1;
else if (a_subtype->len != b_subtype->len ||
memcmp(a_subtype->s, b_subtype->s, b_subtype->len))
return 0;
else
return 1;
}
static int match_mime_with_list(str *mime, str *list)
{
enum state {
MLIST_TYPE_ST,
MLIST_SUBTYPE_ST
};
enum state st;
str src_type = STR_NULL, src_subtype = STR_NULL;
str type = STR_NULL, subtype = STR_NULL;
char *p;
if (mime->len == 0)
goto err_mime;
if (mime->s[0] == '*') {
if (mime->len != 1)
goto err_mime;
return 1;
}
src_type.s = mime->s;
src_subtype.s = q_memchr(mime->s, '/', mime->len);
if (!src_subtype.s)
goto err_mime;
src_type.len = src_subtype.s - src_type.s;
src_subtype.s++;
if (mime->len - src_type.len - 1 == 0)
goto err_mime;
/* ignore type params */
p = q_memchr(src_subtype.s, ';', mime->len - src_type.len - 1);
if (p)
src_subtype.len = p - src_subtype.s;
else
src_subtype.len = mime->len - src_type.len - 1;
if (list->s[0] == '*') {
if (list->len != 1)
goto err_list;
return 1;
}
p = list->s;
st = MLIST_TYPE_ST;
type.s = p;
while (p < list->s + list->len) {
switch (st) {
case MLIST_TYPE_ST:
switch (*p) {
case '/':
type.len = p - type.s;
subtype.s = p+1;
st = MLIST_SUBTYPE_ST;
break;
case ' ':
case '*':
goto err_list;
}
break;
case MLIST_SUBTYPE_ST:
switch (*p) {
case '/':
goto err_list;
case ' ':
subtype.len = p - subtype.s;
if (match_mimes(&src_type, &src_subtype, &type, &subtype))
return 1;
type.s = p+1;
st = MLIST_TYPE_ST;
}
}
p++;
}
if (st != MLIST_SUBTYPE_ST)
goto err_list;
subtype.len = p - subtype.s;
if (match_mimes(&src_type, &src_subtype, &type, &subtype))
return 1;
return 0;
err_mime:
LM_ERR("Bad MIME format [%.*s]\n", mime->len, mime->s);
return 0;
err_list:
LM_ERR("Bad format for accept-types [%.*s]\n", list->len, list->s);
return 0;
}
static int check_offer_types(str *accept_types, str *peer_accept_types)
{
str mime = STR_NULL;
char *p = accept_types->s;
mime.s = accept_types->s;
while (p < accept_types->s + accept_types->len) {
if (*p == ' ') {
mime.len = p - mime.s;
if (match_mime_with_list(&mime, peer_accept_types))
return 1;
mime.s = p+1;
} else {
p++;
}
}
mime.len = p - mime.s;
if (match_mime_with_list(&mime, peer_accept_types))
return 1;
return 0;
}
#define SDP_V_LINE_STR "v=0\r\n"
#define SDP_V_LINE_STR_LEN (sizeof(SDP_V_LINE_STR) - 1)
#define SDP_S_LINE_STR "s= -\r\n"
#define SDP_S_LINE_STR_LEN (sizeof(SDP_S_LINE_STR) - 1)
#define SDP_T_LINE_STR "t=0 0\r\n"
#define SDP_T_LINE_STR_LEN (sizeof(SDP_T_LINE_STR) - 1)
#define SDP_O_STR "o=- "
#define SDP_O_STR_LEN (sizeof(SDP_O_STR) - 1)
#define SDP_IP4_STR "IN IP4 "
#define SDP_IP4_STR_LEN (sizeof(SDP_IP4_STR) - 1)
#define SDP_IP6_STR "IN IP6 "
#define SDP_IP6_STR_LEN (sizeof(SDP_IP6_STR) - 1)
#define SDP_C_STR "c="
#define SDP_C_STR_LEN (sizeof(SDP_C_STR) - 1)
#define SDP_M_STR "m=message "
#define SDP_M_STR_LEN (sizeof(SDP_M_STR) - 1)
#define SDP_M_TCP_STR " TCP/MSRP *\r\n"
#define SDP_M_TCP_STR_LEN (sizeof(SDP_M_TCP_STR) - 1)
#define SDP_M_TLS_STR " TCP/TLS/MSRP *\r\n"
#define SDP_M_TLS_STR_LEN (sizeof(SDP_M_TLS_STR) - 1)
#define SDP_A_TYPES_STR "a=accept-types:"
#define SDP_A_TYPES_STR_LEN (sizeof(SDP_A_TYPES_STR) - 1)
#define SDP_A_PATH_STR "a=path:"
#define SDP_A_PATH_STR_LEN (sizeof(SDP_A_PATH_STR) - 1)
static str *msrpua_build_sdp(struct msrpua_session *sess, str *accept_types)
{
static str buf;
char *p;
str id;
str vers;
id.s = int2str(sess->sdp_sess_id, &id.len);
vers.s = int2str(sess->sdp_sess_vers, &vers.len);
buf.len = SDP_V_LINE_STR_LEN + SDP_S_LINE_STR_LEN + SDP_T_LINE_STR_LEN;
buf.len += SDP_O_STR_LEN + id.len + 1 + vers.len + 1 +
(msrp_sock->address.af==AF_INET ? SDP_IP4_STR_LEN:SDP_IP6_STR_LEN) +
my_msrp_uri.host.len + CRLF_LEN;
buf.len += SDP_C_STR_LEN +
(msrp_sock->address.af==AF_INET ? SDP_IP4_STR_LEN:SDP_IP6_STR_LEN) +
my_msrp_uri.host.len + CRLF_LEN;
buf.len += SDP_M_STR_LEN + my_msrp_uri.port.len +
(my_msrp_uri.secured ? SDP_M_TLS_STR_LEN:SDP_M_TCP_STR_LEN);
buf.len += SDP_A_TYPES_STR_LEN + accept_types->len + CRLF_LEN +
SDP_A_PATH_STR_LEN + my_msrp_uri.whole.len + 1 + sess->session_id.len +
CRLF_LEN;
if (relay_uri_str.s)
buf.len += sess->use_path.len + 1;
buf.s = pkg_malloc(buf.len);
if (!buf.s) {
LM_ERR("no more pkg memory\n");
return NULL;
}
p = buf.s;
append_string(p, SDP_V_LINE_STR, SDP_V_LINE_STR_LEN);
append_string(p, SDP_O_STR, SDP_O_STR_LEN);
append_string(p, id.s, id.len);
*(p++) = ' ';
append_string(p, vers.s, vers.len);
*(p++) = ' ';
if (msrp_sock->address.af==AF_INET)
append_string(p, SDP_IP4_STR, SDP_IP4_STR_LEN);
else
append_string(p, SDP_IP6_STR, SDP_IP6_STR_LEN);
append_string(p, my_msrp_uri.host.s, my_msrp_uri.host.len);
append_string(p, CRLF, CRLF_LEN);
append_string(p, SDP_S_LINE_STR, SDP_S_LINE_STR_LEN);
append_string(p, SDP_C_STR, SDP_C_STR_LEN);
if (msrp_sock->address.af==AF_INET)
append_string(p, SDP_IP4_STR, SDP_IP4_STR_LEN);
else
append_string(p, SDP_IP6_STR, SDP_IP6_STR_LEN);
append_string(p, my_msrp_uri.host.s, my_msrp_uri.host.len);
append_string(p, CRLF, CRLF_LEN);
append_string(p, SDP_T_LINE_STR, SDP_T_LINE_STR_LEN);
append_string(p, SDP_M_STR, SDP_M_STR_LEN);
append_string(p, my_msrp_uri.port.s, my_msrp_uri.port.len);
if (my_msrp_uri.secured)
append_string(p, SDP_M_TLS_STR, SDP_M_TLS_STR_LEN);
else
append_string(p, SDP_M_TCP_STR, SDP_M_TCP_STR_LEN);
append_string(p, SDP_A_TYPES_STR, SDP_A_TYPES_STR_LEN);
append_string(p, accept_types->s, accept_types->len);
append_string(p, CRLF, CRLF_LEN);
append_string(p, SDP_A_PATH_STR, SDP_A_PATH_STR_LEN);
if (relay_uri_str.s) {
append_string(p, sess->use_path.s, sess->use_path.len);
*(p++) = ' ';
}
append_string(p, my_msrp_uri.whole.s,
my_msrp_uri.whole.len - my_msrp_uri.params.len - 1);
*(p++) = '/';
append_string(p, sess->session_id.s, sess->session_id.len);
*(p++) = ';';
append_string(p, my_msrp_uri.params.s, my_msrp_uri.params.len);
append_string(p, CRLF, CRLF_LEN);
if (p - buf.s != buf.len) {
LM_BUG("computed: %d, but wrote %d\n",buf.len,(int)(p-buf.s));
return NULL;
}
return &buf;
}
static int update_peer_path(struct msrpua_session *sess)
{
str tmp;
if (pkg_str_dup(&tmp, &sess->peer_path) < 0) {
LM_ERR("no more pkg memory\n");
return -1;
}
if (shm_str_extend(&sess->peer_path,
sess->peer_path.len + sess->use_path.len + 1) != 0) {
LM_ERR("no more shm memory\n");
goto error;
}
memcpy(sess->peer_path.s, sess->use_path.s, sess->use_path.len);
sess->peer_path.s[sess->use_path.len] = ' ';
memcpy(sess->peer_path.s + sess->use_path.len + 1, tmp.s, tmp.len);
pkg_free(tmp.s);
sess->peer_path_parsed = parse_msrp_path_shm(&sess->peer_path);
if (!sess->peer_path_parsed ) {
LM_ERR("Failed to parse MSRP peer path\n");
return -1;
}
return 0;
error:
pkg_free(tmp.s);
return -1;
}
#define REASON_488_STR "Not Acceptable Here"
static int msrpua_update_session(struct msrpua_session *sess,
struct sip_msg *msg, int etype)
{
str peer_accept_types;
str peer_path;
int code;
str reason;
str *sdp;
int del_sess = 0;
if (get_sdp_peer_info(msg, &peer_accept_types, &peer_path) < 0) {
LM_ERR("Failed to get peer info from SDP\n");
code = 488;
reason = str_init(REASON_488_STR);
goto err_reply;
}
/* match at least one content type from our accept_types
* with the peer's accept_types */
if (!check_offer_types(&sess->accept_types, &peer_accept_types)) {
LM_ERR("Cannot understand any content type received in the offer\n");
code = 488;
reason = str_init(REASON_488_STR);
goto err_reply;
}
if (relay_uri_str.s) {
if (update_peer_path(sess) < 0) {
LM_ERR("Failed to update peer path with the relay URI\n");
code = 500;
reason = str_init("Internal Server Error");
del_sess = 1;
goto err_reply;
}
} else {
if (shm_str_sync(&sess->peer_path, &peer_path) < 0) {
LM_ERR("No more shm memory\n");
code = 500;
reason = str_init("Internal Server Error");
del_sess = 1;
goto err_reply;
}
}
sess->peer_path_parsed = parse_msrp_path_shm(&sess->peer_path);
if (!sess->peer_path_parsed ) {
LM_ERR("Failed to parse MSRP peer path\n");
code = 500;
reason = str_init("Internal Server Error");
del_sess = 1;
goto err_reply;
}
memset(&sess->to_su, 0, sizeof sess->to_su);
if (shm_str_sync(&sess->peer_accept_types, &peer_accept_types) < 0) {
LM_ERR("No more shm memory\n");
code = 500;
reason = str_init("Internal Server Error");
del_sess = 1;
goto err_reply;
}
sess->sdp_sess_vers++;
sdp = msrpua_build_sdp(sess, &sess->accept_types);
if (!sdp) {
LM_ERR("Failed to build SDP answer\n");
code = 500;
reason = str_init("Internal Server Error");
del_sess = 1;
goto err_reply;
}
if (msrpua_b2b_reply(etype, &sess->b2b_key, METHOD_INVITE,
200, &str_init("OK"), sdp) < 0) {
LM_ERR("Failed to send 200 OK\n");
pkg_free(sdp->s);
del_sess = 1;
goto err;
}
sess->dlg_state = MSRPUA_DLG_CONF;
pkg_free(sdp->s);
return 0;
err_reply:
if (msrpua_b2b_reply(etype, &sess->b2b_key, METHOD_INVITE,
code, &reason, NULL) < 0) {
LM_ERR("Failed to send error reply\n");
del_sess = 1;
}
err:
if (del_sess) {
if (msrpua_b2b_request(etype, &sess->b2b_key, &str_init("BYE")) < 0)
LM_ERR("Failed to send BYE on error\n");
msrpua_delete_session(sess);
}
return -1;
}
static int raise_sess_new_event(struct sip_msg *msg, str *sess_id,
str *accept_types)
{
if (parse_from_header(msg) < 0) {
LM_ERR("cannot parse From header\n");
return -1;
}
if (parse_to_header(msg) < 0) {
LM_ERR("cannot parse To header\n");
return -1;
}
if (evi_param_set_str(evi_sess_from_p, &get_from(msg)->uri) < 0) {
LM_ERR("cannot set event parameter\n");
return -1;
}
if (evi_param_set_str(evi_sess_to_p, &get_to(msg)->uri) < 0) {
LM_ERR("cannot set event parameter\n");
return -1;
}
if (evi_param_set_str(evi_sess_ruri_p, GET_RURI(msg)) < 0) {
LM_ERR("cannot set event parameter\n");
return -1;
}
if (evi_param_set_str(evi_sess_sid_p, sess_id) < 0) {
LM_ERR("cannot set event parameter\n");
return -1;
}
if (evi_param_set_str(evi_sess_types_p, accept_types) < 0) {
LM_ERR("cannot set event parameter\n");
return -1;
}
if (evi_raise_event(evi_sess_new_id, evi_sess_params) < 0) {
LM_ERR("cannot raise event\n");
return -1;
}
return 0;
}
static int raise_sess_end_event(str *sess_id)
{
if (evi_param_set_str(evi_sess_sid_p, sess_id) < 0) {
LM_ERR("cannot set event parameter\n");
return -1;
}
if (evi_raise_event(evi_sess_end_id, evi_sess_params) < 0) {
LM_ERR("cannot raise event\n");
return -1;
}
return 0;
}
static int raise_msg_rcv_event(str *sess_id, str *ctype, str *body)
{
if (evi_param_set_str(evi_msg_rcv_sid_p, sess_id) < 0) {
LM_ERR("cannot set event parameter\n");
return -1;
}
if (evi_param_set_str(evi_msg_rcv_ctype_p, ctype) < 0) {
LM_ERR("cannot set event parameter\n");
return -1;
}
if (evi_param_set_str(evi_msg_rcv_body_p, body) < 0) {
LM_ERR("cannot set event parameter\n");
return -1;
}
if (evi_raise_event(evi_msg_rcv_id, evi_msg_rcv_params) < 0) {
LM_ERR("cannot raise event\n");
return -1;
}
return 0;
}
static int b2b_notify_request(int etype, struct sip_msg *msg, str *key,
void *param, int flags)
{