-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathre_redis_mod.c
1339 lines (1078 loc) · 44.3 KB
/
re_redis_mod.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
#include <stdio.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include "./rtpengine/daemon/sdp.h"
#include "./rtpengine/daemon/call.h"
#include "re_redis_mod.h"
#include "redis_storage.h"
char *__module_version = "redis/9";
#define DECL_STRUCT_SIZE(_s_) unsigned long __size_struct_ ## _s_ = sizeof(struct _s_);
#define MEMBER_SIZE(type, member) sizeof(((type *)0)->member)
#define MEMBER_OFFSET(type, member) &(((type *)0)->member)
#define DECL_MEMBER_OFFSET(_s_, _m_) unsigned long __offset_struct_ ## _s_ ## _ ## _m_ = (unsigned long) MEMBER_OFFSET(struct _s_, _m_);
#define DECL_MEMBER_SIZE(_s_, _m_) unsigned long __size_struct_ ## _s_ ## _ ## _m_ = MEMBER_SIZE(struct _s_, _m_);
#define DECL_MEMBER_OFFSET_SIZE(_s_, _m_) DECL_MEMBER_OFFSET(_s_, _m_) \
DECL_MEMBER_SIZE(_s_, _m_)
/* REDIS call keys */
#define CALL_TT_KEY 0
#define CALL_FT_KEY 1
#define CALL_LASTPORT_KEY 2
#define CALL_TOS_KEY 3
#define CALL_STREAM_COUNT_KEY 4
#define CALL_CREATED_FROM_KEY 5
#define CALL_MAX_KEY 6
/* REDIS stream keys */
#define STREAM_PAYLOAD_TYPE_KEY 0
#define STREAM_PAYLOAD_COUNT_KEY 1
#define STREAM_FLAGS_KEY 2
#define STREAM_RTP_BRIDGE_PORT_KEY 3
#define STREAM_RTCP_BRIDGE_PORT_KEY 4
#define STREAM_INDEX_KEY 5
#define STREAM_FINGER_DIGEST_KEY 6
#define STREAM_FINGER_NAME_KEY 7
#define STREAM_CRYPTO_NAME_KEY 8
#define STREAM_CRYPTO_KEY 9
#define STREAM_CRYPTO_SALT_KEY 10
#define STREAM_CRYPTO_MKI_KEY 11
#define STREAM_CRYPTO_MKI_LEN_KEY 12
#define STREAM_TYPE_KEY 13
#define STREAM_DIRECTION0_KEY 14
#define STREAM_DIRECTION1_KEY 15
#define STREAM_FAMILY_KEY 16
#define STREAM_TRANSPORT_KEY 17
#define STREAM_SDES_KEY 18
#define STREAM_RTP_ENDPOINT_KEY 19
#define STREAM_RTCP_ENDPOINT_KEY 20
#define STREAM_CONSECUTIVE_PORTS_KEY 21
#define STREAM_MAX_KEY 22
/* REDIS call key array */
const char *redis_call_keys[CALL_MAX_KEY] = {
"tt",
"ft",
"lastport",
"tos",
"stream-count",
"created-from",
};
/* REDIS stream key array */
const char *redis_stream_keys[STREAM_MAX_KEY] = {
"payload-type",
"payload-count",
"flags",
"rtp-bridge-port",
"rtcp-bridge-port",
"index",
"fingerprint-digest",
"fingerprint-name",
"crypto-name",
"crypto-key",
"crypto-salt",
"crypto-mki",
"crypto-mki-len",
"type",
"direction0",
"direction1",
"desired_family",
"transport-index",
"sdes-tag",
"rtp-endpoint",
"rtcp-endpoint",
"consecutive_ports",
};
DECL_STRUCT_SIZE(call);
DECL_STRUCT_SIZE(packet_stream);
DECL_STRUCT_SIZE(call_media);
DECL_STRUCT_SIZE(call_monologue);
DECL_STRUCT_SIZE(crypto_suite);
DECL_STRUCT_SIZE(crypto_context);
DECL_MEMBER_OFFSET_SIZE(call, callmaster);
DECL_MEMBER_OFFSET_SIZE(call, master_lock);
DECL_MEMBER_OFFSET_SIZE(call, monologues);
DECL_MEMBER_OFFSET_SIZE(call, tags);
DECL_MEMBER_OFFSET_SIZE(call, streams);
DECL_MEMBER_OFFSET_SIZE(call, stream_fds);
DECL_MEMBER_OFFSET_SIZE(call, dtls_cert);
DECL_MEMBER_OFFSET_SIZE(call, callid);
DECL_MEMBER_OFFSET_SIZE(call, last_signal);
DECL_MEMBER_OFFSET_SIZE(packet_stream, media);
DECL_MEMBER_OFFSET_SIZE(packet_stream, call);
DECL_MEMBER_OFFSET_SIZE(packet_stream, rtcp_sibling);
DECL_MEMBER_OFFSET_SIZE(packet_stream, handler);
DECL_MEMBER_OFFSET_SIZE(packet_stream, crypto);
DECL_MEMBER_OFFSET_SIZE(packet_stream, dtls_cert);
DECL_MEMBER_OFFSET_SIZE(packet_stream, ps_flags);
DECL_MEMBER_OFFSET_SIZE(call_media, monologue);
DECL_MEMBER_OFFSET_SIZE(call_media, call);
DECL_MEMBER_OFFSET_SIZE(call_media, protocol);
DECL_MEMBER_OFFSET_SIZE(call_media, fingerprint);
DECL_MEMBER_OFFSET_SIZE(call_media, streams);
DECL_MEMBER_OFFSET_SIZE(call_media, media_flags);
DECL_MEMBER_OFFSET_SIZE(call_monologue, call);
DECL_MEMBER_OFFSET_SIZE(call_monologue, tag);
DECL_MEMBER_OFFSET_SIZE(call_monologue, created);
DECL_MEMBER_OFFSET_SIZE(call_monologue, other_tags);
DECL_MEMBER_OFFSET_SIZE(call_monologue, active_dialogue);
DECL_MEMBER_OFFSET_SIZE(call_monologue, medias);
DECL_MEMBER_OFFSET_SIZE(stream_fd, fd);
DECL_MEMBER_OFFSET_SIZE(stream_fd, call);
DECL_MEMBER_OFFSET_SIZE(stream_fd, stream);
DECL_MEMBER_OFFSET_SIZE(stream_fd, dtls);
extern const struct transport_protocol transport_protocols[];
extern const struct crypto_suite crypto_suites[];
extern const int num_crypto_suites;
//static void __streams_free(GQueue *q);
static void __print_flags(const char *title, struct stream_params *sp);
static int __insert_stream_params(struct redis *redis, str* callid, int stream_id,
struct stream_params *sp, unsigned int rtp_bridge_port, unsigned int rtcp_bridge_port);
static int __retrieve_stream_params(struct redis *redis, str* callid, int stream_id,
struct stream_params *sp, unsigned int *rtp_bridge_port, unsigned int *rtcp_bridge_port);
static int __register_callid(struct redis *redis, struct call *call);
static int __retrieve_call_list(struct redis *redis, struct callmaster *cm, str ***list, int **length);
static const char *__crypto_find_name(const struct crypto_suite *ptr);
static int __process_call(str *callid, struct callmaster *cm, struct stream_params *sp, str *ft, str *tt, str *proxy, struct sdp_ng_flags *flags, enum call_opmode op_mode,
unsigned int *rtp_bridge_ports, unsigned int *rtcp_bridge_ports, unsigned int stream_count);
static void __fill_flag(struct sdp_ng_flags *flags, struct stream_params *sp);
static const char* redis_get_call_key(unsigned int key) {
if (key >= CALL_MAX_KEY) {
return NULL;
}
return redis_call_keys[key];
}
static const char* redis_get_stream_key(unsigned int key) {
if (key >= STREAM_MAX_KEY) {
return NULL;
}
return redis_stream_keys[key];
}
double time_mili_diff(struct timeval x , struct timeval y)
{
double x_ms , y_ms , diff;
x_ms = (double)x.tv_sec * 1000 + (double)x.tv_usec / 1000;
y_ms = (double)y.tv_sec * 1000 + (double)y.tv_usec / 1000;
diff = (double)y_ms - (double)x_ms;
return diff;
}
void mod_redis_update(struct call *call, struct redis *redis) {
struct packet_stream *ps;
GSList *monologue_iter;
GList *ps_iter;
GList *ml_media_iter, *ml_other_media_iter;
struct call_monologue *monologue, *other_monologue;
struct call_media *media, *other_media;
struct interface_address *ifa, *other_ifa;
int media_cnt = 0, other_media_cnt = 0;
int sp_counter = 0;
struct sdp_ng_flags flags;
struct stream_params sp;
int set = 0;
unsigned int rtp_bridge_port = 0, rtcp_bridge_port = 0;
struct rtp_payload_type *pt;
GList *values, *iter;
str proxy = {0, 0};
memset(&flags, 0, sizeof(flags));
memset(&sp, 0, sizeof(sp));
__register_callid(redis, call);
for (monologue_iter = call->monologues; monologue_iter; monologue_iter = monologue_iter->next) {
monologue = monologue_iter->data;
other_monologue = monologue->active_dialogue;
if (!set) {
if (redis_insert_str_value_async(redis, &call->callid, redis_get_call_key(CALL_TT_KEY), &monologue->tag) < 0) {
plog(LOG_ERR, "couldn't insert ps counter into database\n");
return;
}
if (monologue->active_dialogue && redis_insert_str_value_async(redis, &call->callid, redis_get_call_key(CALL_FT_KEY), &monologue->active_dialogue->tag) < 0) {
plog(LOG_ERR, "couldn't insert ps counter into database\n");
return;
}
set = 1;
}
media_cnt = 0;
for (ml_media_iter = monologue->medias.head; ml_media_iter; ml_media_iter = ml_media_iter->next) {
media = ml_media_iter->data;
ifa = (struct interface_address *)media->local_address;
// push payload types in temporary sp structure which will be written into redis database
values = g_hash_table_get_values(media->rtp_payload_types);
for (iter = values; iter; iter = iter->next) {
pt = iter->data;
g_queue_push_tail(&sp.rtp_payload_types, pt);
plog(LOG_DEBUG, "insert payload_type = %d", pt->payload_type);
}
g_list_free(values);
sp.index = media->index;
sp.protocol = media->protocol;
if (MEDIA_ISSET(media, RTCP_MUX))
SP_SET((&sp), RTCP_MUX);
sp.crypto = media->sdes_in.params;
crypto_params_copy(&sp.crypto, &media->sdes_in.params, (flags.opmode == OP_OFFER) ? 1 : 0);
sp.sdes_tag = media->sdes_in.tag;
if (MEDIA_ISSET(media, ASYMMETRIC)) {
SP_SET((&sp), ASYMMETRIC);
flags.asymmetric = 1;
}
if (MEDIA_ISSET(media, SEND))
SP_SET((&sp), SEND);
if (MEDIA_ISSET(media, RECV))
SP_SET((&sp), RECV);
if (MEDIA_ISSET(media, SETUP_ACTIVE))
SP_SET((&sp), SETUP_ACTIVE);
if (MEDIA_ISSET(media, SETUP_PASSIVE))
SP_SET((&sp), SETUP_PASSIVE);
if (MEDIA_ISSET(media, ICE))
SP_SET((&sp), ICE);
sp.fingerprint = media->fingerprint;
sp.desired_family = media->desired_family;
// FIXME
sp.consecutive_ports = 1;
// SP_SET((&sp), IMPLICIT_RTCP);
call_str_cpy(call, &sp.type, &media->type);
// match other_media for the given media in order to get the right direction
other_media_cnt = 0;
for (ml_other_media_iter = other_monologue->medias.head; ml_other_media_iter; ml_other_media_iter = ml_other_media_iter->next) {
if (other_media_cnt == media_cnt) {
break;
}
other_media_cnt++;
}
other_media = ml_other_media_iter->data;
other_ifa = (struct interface_address *)other_media->local_address;
// now we can complete the right direction for sp
sp.direction[0].s = g_malloc0(ifa->interface_name.len);
sp.direction[0].len = ifa->interface_name.len;
memcpy(sp.direction[0].s, ifa->interface_name.s, ifa->interface_name.len);
sp.direction[1].s = g_malloc0(other_ifa->interface_name.len);
sp.direction[1].len = other_ifa->interface_name.len;
memcpy(sp.direction[1].s, other_ifa->interface_name.s, other_ifa->interface_name.len);
memcpy(flags.direction, sp.direction, sizeof(sp.direction));
flags.transport_protocol = media->protocol;
SP_SET((&sp), NO_RTCP);
for (ps_iter = media->streams.head; ps_iter; ps_iter = ps_iter->next) {
ps = ps_iter->data;
if (PS_ISSET(ps, RTCP)) {
rtcp_bridge_port = ps->sfd->fd.localport;
sp.rtcp_endpoint = ps->endpoint;
SP_CLEAR((&sp), NO_RTCP);
}
else {
rtp_bridge_port = ps->sfd->fd.localport;
sp.rtp_endpoint = ps->endpoint;
}
if (PS_ISSET(ps, IMPLICIT_RTCP))
SP_SET((&sp), IMPLICIT_RTCP);
if (PS_ISSET(ps, STRICT_SOURCE))
SP_SET((&sp), STRICT_SOURCE);
if (PS_ISSET(ps, MEDIA_HANDOVER))
SP_SET((&sp), MEDIA_HANDOVER);
}
// the OFFER streams are inserted firstly and the ANSWER streams are inserted lastly in the database
// this is the behaviour due to rtpengine's call_offer_answer_ng()
__insert_stream_params(redis, &call->callid, sp_counter++, &sp, rtp_bridge_port, rtcp_bridge_port);
// debugs
char addr_buff[64];
smart_ntop_p(addr_buff, &sp.rtp_endpoint.ip46, sizeof(addr_buff));
plog(LOG_INFO, "RTP: %s:%d", addr_buff, sp.rtp_endpoint.port);
smart_ntop_p(addr_buff, &sp.rtcp_endpoint.ip46, sizeof(addr_buff));
plog(LOG_INFO, "RTCP: %s:%d", addr_buff, sp.rtcp_endpoint.port);
smart_ntop_p(addr_buff, &ifa->addr, sizeof(addr_buff));
plog(LOG_INFO, "Media interface name: \"%.*s\" addr: %s:%d",
ifa->interface_name.len, ifa->interface_name.s, addr_buff, sp.rtcp_endpoint.port);
// count the medias for this monologue
media_cnt ++;
}
}
// not used anymore - bridgeporrts are used instead
if (redis_insert_int_value_async(redis, &call->callid, redis_get_call_key(CALL_LASTPORT_KEY), call->callmaster->lastport) < 0) {
plog(LOG_ERR, "couldn't insert callmaster lastport into database\n");
return;
}
// keep the number of stream params
if (redis_insert_int_value_async(redis, &call->callid, redis_get_call_key(CALL_STREAM_COUNT_KEY), sp_counter) < 0) {
plog(LOG_ERR, "couldn't insert streams count into database\n");
return;
}
// keep call tos in order to fill flags.tos when redis restore
if (redis_insert_uint_value_async(redis, &call->callid, redis_get_call_key(CALL_TOS_KEY), call->tos) < 0) {
plog(LOG_ERR, "couldn't insert callmaster lastport into database\n");
return;
}
// keep call created_from to identify details about control proxy
proxy.s = call->created_from;
proxy.len = strlen(call->created_from);
if (redis_insert_str_value_async(redis, &call->callid, redis_get_call_key(CALL_CREATED_FROM_KEY), &proxy) < 0) {
plog(LOG_ERR, "couldn't insert proxy string into database\n");
return;
}
// process async events
event_base_loop(redis->eb, EVLOOP_NONBLOCK | EVLOOP_ONCE);
}
void mod_redis_delete(struct call *c, struct redis *redis) {
if (redis_remove_mp_entry(redis, c) < 0)
plog(LOG_ERR, "Error removing key from hash table\n");
if (redis_remove_member(redis, c) < 0)
plog(LOG_ERR, "Error removing member from list\n");
}
int redis_remove_mp_entry(struct redis *redis, struct call *call) {
redisReply *rpl = NULL;
char cmd_buffer[1024];
int ret;
snprintf(cmd_buffer, sizeof(cmd_buffer), "DEL mp:%.*s", call->callid.len, call->callid.s);
ret = redis_exec(redis, cmd_buffer, &rpl);
if (ret > 0)
freeReplyObject(rpl);
return ret;
}
void mod_redis_wipe(struct redis *r) {
}
#ifdef obsolete_dtls
static int __restore_dtls_params(struct redis *redis, struct dtls_cert *cert) {
int ret;
str aux;
const str default_hash_function = {"sha-1", sizeof("sha-1") - 1};
ret = redis_restore_cert(redis, &cert->x509);
if (ret < 0) {
plog(LOG_ERR, "error restoring certificate\n");
return -1;
}
else if (ret == 0 && redis_insert_cert(redis, cert->x509) < 0) {
plog(LOG_ERR, "error inserting certificate\n");
return -1;
}
else if (ret == 1)
plog(LOG_INFO, "certificate was restored from previous instance\n");
ret = redis_restore_pkey(redis, &cert->pkey);
if (ret < 0) {
plog(LOG_ERR, "error restoring pkey\n");
return -1;
}
else if (ret == 0 && redis_insert_pkey(redis, cert->pkey) < 0) {
plog(LOG_ERR, "error inserting pkey\n");
return -1;
}
else if (ret == 1)
plog(LOG_INFO, "pkey was restored from previous instance\n");
ret = redis_restore_expires(redis, &cert->expires);
if (ret < 0) {
plog(LOG_ERR, "error restoring expires\n");
return -1;
}
else if (ret == 0 && redis_insert_expires(redis, &cert->expires) < 0) {
plog(LOG_ERR, "error inserting expires\n");
return -1;
}
else if (ret == 1)
plog(LOG_INFO, "expires was restored from previous instance\n");
cert->fingerprint.hash_func = dtls_find_hash_func(&default_hash_function);
if (!cert->fingerprint.hash_func) {
plog(LOG_ERR, "error finding hash function\n");
return -1;
}
ret = redis_restore_fingerprint(redis, &aux);
if (ret < 0) {
plog(LOG_ERR, "error restoring fingerprint\n");
return -1;
}
else if (ret == 0 && redis_insert_fingerprint(redis, cert->fingerprint.digest, sizeof(cert->fingerprint.digest)) < 0) {
plog(LOG_ERR, "error inserting fingerprint\n");
return -1;
}
else if (ret == 1) {
memcpy(cert->fingerprint.digest, aux.s, sizeof(cert->fingerprint.digest));
g_free(aux.s);
plog(LOG_INFO, "fingerprint was restored from previous instance\n");
}
if (!X509_set_pubkey(cert->x509, cert->pkey))
return -1;
return 1;
/* if (!X509_set_version(cert->x509, 0L))
return -1;
if (!X509_gmtime_adj(X509_get_notBefore(cert->x509), -60*60*24))
return -1;
if (!X509_gmtime_adj(X509_get_notAfter(cert->x509), (60*60*24*30)))
return -1;
*/
/* sign it */
// if (!X509_sign(cert->x509, cert->pkey, EVP_sha1()))
// return -1;
//dtls_fingerprint_hash(&cert->fingerprint, cert->x509);
/*
BIGNUM *exponent = NULL, *serial_number = NULL;
RSA *rsa = NULL;
ASN1_INTEGER *asn1_serial_number;
X509_NAME *name;
EVP_PKEY *pkey = NULL;
pkey = EVP_PKEY_new();
exponent = BN_new();
rsa = RSA_new();
serial_number = BN_new();
name = X509_NAME_new();
if (!BN_set_word(exponent, 0x10001))
goto err;
if (!RSA_generate_key_ex(rsa, 1024, exponent, NULL))
goto err;
if (!EVP_PKEY_assign_RSA(pkey, rsa))
goto err;
if (!X509_set_pubkey(cert->x509, pkey))
goto err;
if (!BN_pseudo_rand(serial_number, 64, 0, 0))
goto err;
asn1_serial_number = X509_get_serialNumber(cert->x509);
if (!asn1_serial_number)
goto err;
if (!BN_to_ASN1_INTEGER(serial_number, asn1_serial_number))
goto err;
if (!X509_set_version(cert->x509, 0L))
goto err;
if (!X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
(unsigned char *) "rtpengine", -1, -1, 0))
goto err;
if (!X509_set_subject_name(cert->x509, name))
goto err;
if (!X509_set_issuer_name(cert->x509, name))
goto err;
if (!X509_gmtime_adj(X509_get_notBefore(cert->x509), -60*60*24))
goto err;
mod_redis_restore
if (!X509_gmtime_adj(X509_get_notAfter(cert->x509), (60*60*24*30)))
goto err;
if (!X509_sign(cert->x509, pkey, EVP_sha1()))
goto err;
//new_cert = obj_alloc0("dtls_cert", sizeof(*new_cert), cert_free);
cert->fingerprint.hash_func = dtls_find_hash_func(&default_hash_function);
dtls_fingerprint_hash(&cert->fingerprint, cert->x509);
cert->pkey = pkey;
return 1;
err:
return -1;
*/
}
#endif
/* substitutes the first half with the second half of the array */
static void swap_bridgeports(unsigned int *rtp_bridge_ports, unsigned int len) {
unsigned int iter;
unsigned int aux;
for (iter = 0; iter < len / 2; iter++) {
aux = rtp_bridge_ports[iter];
rtp_bridge_ports[iter] = rtp_bridge_ports[iter + len / 2];
rtp_bridge_ports[iter + len / 2] = aux;
}
}
int mod_redis_restore(struct callmaster *cm, struct redis *redis) {
str **list = NULL;
str ft = {0,0};
str tt = {0,0};
str proxy = {0,0};
str *callid = NULL;
int *length = NULL, stream_count = 0, stream_iter = 0;
int i, j;
unsigned int tos = 0;
// useful arrays
struct stream_params *sp;
struct sdp_ng_flags *flags;
unsigned int *rtp_bridge_ports;
unsigned int *rtcp_bridge_ports;
struct timeval total_start, total_end;
struct timeval redis_start, redis_end;
double total_diff = 0, redis_diff = 0;
// start redis and total timers
gettimeofday(&total_start, NULL);
gettimeofday(&redis_start, NULL);
#ifdef obsolete_dtls
// get the certificate newly generated
if (__restore_dtls_params(redis, dtls_cert()) < 0)
return -1;
#endif
// get list of calls
if (__retrieve_call_list(redis, cm, &list, &length) < 0)
return -1;
// stop redis timer
gettimeofday(&redis_end, NULL);
redis_diff += time_mili_diff(redis_start, redis_end);
// get stream details binded on each interface
for (i = 0; i < g_queue_get_length(cm->conf.interfaces); i++) {
for (j = 0; j < length[i]; j++) {
// start redis timer
gettimeofday(&redis_start, NULL);
// get call
callid = &list[i][j];
plog(LOG_INFO, "Retrieve call ID [%.*s]", callid->len, callid->s);
// get call from tag
if (redis_get_str(redis, "HGET", callid, redis_get_call_key(CALL_FT_KEY), &ft) < 0)
goto next;
plog(LOG_INFO, "Retrieve call %s [%.*s]", redis_get_call_key(CALL_FT_KEY), ft.len, ft.s);
// get call to tag
if (redis_get_str(redis, "HGET", callid, redis_get_call_key(CALL_TT_KEY), &tt) < 0)
goto next;
plog(LOG_INFO, "Retrieve call %s [%.*s]", redis_get_call_key(CALL_TT_KEY), tt.len, tt.s);
// get call tos
if (redis_get_uint(redis, "HGET", callid, redis_get_call_key(CALL_TOS_KEY), &tos) < 0)
goto next;
plog(LOG_INFO, "Retrieve call %s [%u]", redis_get_call_key(CALL_TOS_KEY), tos);
// get call control proxy
if (redis_get_str(redis, "HGET", callid, redis_get_call_key(CALL_CREATED_FROM_KEY), &proxy) < 0)
goto next;
plog(LOG_INFO, "Retrieve call %s [%.*s]", redis_get_call_key(CALL_CREATED_FROM_KEY), proxy.len, proxy.s);
// get number of stream params; 2 for audio only, 4 for audio + video
if ((redis_get_int(redis, "HGET", callid, redis_get_call_key(CALL_STREAM_COUNT_KEY), &stream_count) < 0))
goto next;
plog(LOG_INFO, "Retrieve call %s [%u]", redis_get_call_key(CALL_STREAM_COUNT_KEY), stream_count);
// skip if no streams found for the call
if (stream_count == 0) {
plog(LOG_INFO, "No streams found for call ID [%.*s]", callid->len, callid->s);
goto next;
}
// skip if odd number of streams found for the call
if (stream_count % 2) {
plog(LOG_INFO, "Odd number of streams found for call ID [%.*s]", callid->len, callid->s);
goto next;
}
// stop redis timer
gettimeofday(&redis_end, NULL);
redis_diff += time_mili_diff(redis_start, redis_end);
// alloc stream params and bridgeport vector
sp = (struct stream_params *) calloc (stream_count, sizeof(struct stream_params));
flags = (struct sdp_ng_flags *) calloc (stream_count, sizeof(struct sdp_ng_flags));
rtp_bridge_ports = (unsigned int *) calloc (stream_count, sizeof(unsigned int));
rtcp_bridge_ports = (unsigned int *) calloc (stream_count, sizeof(unsigned int));
// retrieve call stream params and r(c)tp bridgeports
// due to insertion order, the OFFER stream params are retrieved first and the ANSWER last
for (stream_iter = 0; stream_iter < stream_count; stream_iter++) {
// start redis timer
gettimeofday(&redis_start, NULL);
if (__retrieve_stream_params(redis, callid, stream_iter, &sp[stream_iter], &rtp_bridge_ports[stream_iter], &rtcp_bridge_ports[stream_iter]) < 0) {
// stop redis timer
gettimeofday(&redis_end, NULL);
redis_diff += time_mili_diff(redis_start, redis_end);
goto next_free;
}
// stop redis timer
gettimeofday(&redis_end, NULL);
redis_diff += time_mili_diff(redis_start, redis_end);
plog(LOG_INFO, "Retrieve rtp bridge port [%u], rtcp bridge port [%u]",
rtp_bridge_ports[stream_iter], rtcp_bridge_ports[stream_iter]);
#ifdef obsolete_dtls
plog(LOG_INFO, "%d rtp %d rtcp %d fingerprint %p bp %d", stream_iter, sp[stream_iter].rtp_endpoint.port, sp[stream_iter].rtcp_endpoint.port,
sp[stream_iter].fingerprint.hash_func, rtp_bridge_ports[stream_iter]);
ZERO(sp[stream_iter].fingerprint);
#endif
sp[stream_iter].index = 1;
// only the OFFER and ANSWER flags are needed but we keep per stream_params flags
__fill_flag(&flags[stream_iter], &sp[stream_iter]);
flags[stream_iter].tos = tos;
if (stream_iter % 2 == 0) {
flags[stream_iter].opmode = OP_OFFER;
} else {
flags[stream_iter].opmode = OP_ANSWER;
}
}
// switch the first half of the bridgeports with the second half
// this is necessary due to rtpengine commit 16b42fbd62d930f8a38283c5086fe7ac026e80e6 which swaps the OFFER/ANSWER stream logic
swap_bridgeports(rtp_bridge_ports, stream_count);
swap_bridgeports(rtcp_bridge_ports, stream_count);
// process offer
if (__process_call(callid, cm, sp, &ft, NULL, &proxy, flags, OP_OFFER, rtp_bridge_ports, rtcp_bridge_ports, stream_count) < 0) {
plog(LOG_ERR, "error processing call [%.*s]\n", callid->len, callid->s);
goto next_free;
}
// process answer
if (__process_call(callid, cm, sp, &ft, &tt, &proxy, flags, OP_ANSWER, rtp_bridge_ports, rtcp_bridge_ports, stream_count) < 0) {
plog(LOG_ERR, "error processing call [%.*s]\n", callid->len, callid->s);
goto next_free;
}
next_free:
// if (callid->s)
// g_free(callid->s);
// TODO: don't like this alloc and free; maybe in the future we make static like sp[8] => 4 media per call
free(sp);
free(flags);
free(rtp_bridge_ports);
free(rtcp_bridge_ports);
next:
;
}
}
gettimeofday(&total_end, NULL);
total_diff += time_mili_diff(total_start, total_end);
plog(LOG_INFO, "Total restore time = %.0lf ms; Redis restore time = %.0lf ms", total_diff, redis_diff);
// if (list)
// g_free(list);
return 0;
}
static void __fill_flag(struct sdp_ng_flags *flags, struct stream_params *sp) {
flags->address_family = sp->desired_family;
flags->transport_protocol = sp->protocol;
if (SP_ISSET(sp, ASYMMETRIC))
flags->asymmetric = 1;
if (SP_ISSET(sp, STRICT_SOURCE))
flags->strict_source = 1;
if (SP_ISSET(sp, MEDIA_HANDOVER))
flags->media_handover = 1;
}
static int __process_call(str *callid, struct callmaster *cm, struct stream_params *sp,
str *ft, str *tt, str *proxy, struct sdp_ng_flags *flags, enum call_opmode op_mode,
unsigned int *rtp_bridge_ports, unsigned int *rtcp_bridge_ports, unsigned int stream_count) {
unsigned int stream_iter = 0;
struct call *call = NULL;
GQueue streams = G_QUEUE_INIT;
struct call_monologue *monologue = NULL;
if (!(call = call_get_opmode(callid, cm, op_mode))) {
plog(LOG_ERR, "error obtaining call using op_mode=%d\n", op_mode);
goto error;
}
// keep control proxy info
call->created_from = call_strdup_len(call, proxy->s, proxy->len);
// clear the bridgeports queue
g_queue_clear(&call->rtp_bridge_ports);
switch (op_mode) {
case OP_OFFER:
// get/create monologue
if (!(monologue = call_get_mono_dialogue(call, ft, tt, NULL))) {
plog(LOG_ERR, "error allocating monologue\n");
goto error;
}
// fix the tag-type displayed at "rtpengine-ctl sessions"
monologue->tagtype = FROM_TAG;
// add the OFFER stream params present in the first half of the sp array (e.g. sp1, sp2)
// push the correspondent bridge ports (e.g [sp1_bp, sp3_bp], [sp2_bp, sp4_bp])
for (stream_iter = 0; stream_iter < stream_count / 2; stream_iter++) {
g_queue_push_tail(&streams, &sp[stream_iter]);
g_queue_push_tail(&call->rtp_bridge_ports, GUINT_TO_POINTER(rtp_bridge_ports[stream_iter]));
g_queue_push_tail(&call->rtp_bridge_ports, GUINT_TO_POINTER(rtp_bridge_ports[stream_iter + stream_count / 2]));
}
// call rtpengine main logic with OFFER flags
if (monologue_offer_answer(monologue, &streams, &flags[stream_count / 2 - 1]) < 0) {
plog(LOG_ERR, "error processing monologue\n");
goto error;
}
break;
case OP_ANSWER:
// fix the call_get_mono_dialogue() for 4.1 (this is not needed for 4.0 or 3.3.0)
// -> see also call_offer_answer_ng()->str_swap() in rtpengine's call_interfaces.c
str_swap(tt, ft);
// get/create monologue
if (!(monologue = call_get_mono_dialogue(call, ft, tt, NULL))) {
plog(LOG_ERR, "error allocating monologue\n");
goto error;
}
// fix the tag-type displayed at "rtpengine-ctl sessions"
monologue->tagtype = TO_TAG;
// add the ANSWER stream params present in the second half of the sp array (e.g. sp3, sp4)
// no bridge ports needed (were allocated at the OFFER)
for (stream_iter = stream_count / 2; stream_iter < stream_count; stream_iter++) {
g_queue_push_tail(&streams, &sp[stream_iter]);
}
// call rtpengine main logic with ANSWER flags
if (monologue_offer_answer(monologue, &streams, &flags[stream_count / 2]) < 0) {
plog(LOG_ERR, "error processing monologue\n");
goto error;
}
break;
default:
plog(LOG_ERR, "op_mode %d not found; need either OP_OFFER or OP_ANSWER\n", op_mode);
break;
}
rwlock_unlock_w(&call->master_lock);
obj_put(call);
// __streams_free(&streams);
return 1;
error:
if (call)
rwlock_unlock_w(&call->master_lock);
return-1;
}
struct redis *mod_redis_new(struct in_addr ip, u_int16_t port, int db) {
return redis_connect(ip, port, db);
}
#ifdef obsolete_dtls
static void __dtls_connection_init(struct packet_stream *ps, struct dtls_cert *cert) {
char *p;
char *ciphers_str;
int i;
struct dtls_connection *d = &ps->sfd->dtls;
p = ciphers_str;
for (i = 0; i < num_crypto_suites; i++) {
if (!crypto_suites[i].dtls_name)
continue;
p += sprintf(p, "%s:", crypto_suites[i].dtls_name);
}
p[-1] = '\0';
//d->ssl_ctx = SSL_CTX_new(active ? DTLSv1_client_method() : DTLSv1_server_method());
d->ssl_ctx = SSL_CTX_new(DTLSv1_client_method());
if (!d->ssl_ctx)
goto error;
if (SSL_CTX_use_certificate(d->ssl_ctx, cert->x509) != 1)
goto error;
if (SSL_CTX_use_PrivateKey(d->ssl_ctx, cert->pkey) != 1)
goto error;
// SSL_CTX_set_verify(d->ssl_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
// verify_callback);
SSL_CTX_set_verify_depth(d->ssl_ctx, 4);
SSL_CTX_set_cipher_list(d->ssl_ctx, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
if (SSL_CTX_set_tlsext_use_srtp(d->ssl_ctx, ciphers_str))
goto error;
d->ssl = SSL_new(d->ssl_ctx);
if (!d->ssl)
goto error;
d->r_bio = BIO_new(BIO_s_mem());
d->w_bio = BIO_new(BIO_s_mem());
if (!d->r_bio || !d->w_bio)
goto error;
SSL_set_app_data(d->ssl, ps->sfd); /* XXX obj reference here? */
SSL_set_bio(d->ssl, d->r_bio, d->w_bio);
SSL_set_mode(d->ssl, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
d->init = 1;
d->active = 1;
error:
plog(LOG_ERR, "error");
return;
}
#endif
/*
static void __streams_free(GQueue *q) {
struct stream_params *s;
while ((s = g_queue_pop_head(q))) {
if (s->crypto.mki)
free(s->crypto.mki);
g_slice_free1(sizeof(*s), s);
}
}
*/
#define VAL_SIZE(_x_) (char *)&(_x_), sizeof((_x_))
static int __retrieve_call_list(struct redis *redis, struct callmaster *cm, str ***list, int **length) {
GList *l;
redisReply *rpl = NULL;
int i,j;
struct interface_address *ifa;
char cmd_buff[CMD_BUFFER_SIZE];
char addr_buff[64];
if (g_queue_get_length(cm->conf.interfaces) > 0) {
*list = g_malloc0(sizeof(str) * g_queue_get_length(cm->conf.interfaces));
*length = g_malloc0(sizeof(int) * g_queue_get_length(cm->conf.interfaces));
} else {
plog(LOG_ERR, "Found empty rtpengine interfaces list");
return -1;
}
/* build primary lists first */
for (l = cm->conf.interfaces->head, i = 0; l; l = l->next, i++) {
ifa = l->data;
smart_ntop_p(addr_buff, &ifa->addr, sizeof(addr_buff));
snprintf(cmd_buff, sizeof(cmd_buff), "SMEMBERS %.*s:%s",
ifa->interface_name.len, ifa->interface_name.s,
addr_buff);
if (redis_exec(redis, cmd_buff, &rpl) < 0) {
plog(LOG_ERR, "Failed command %s for set %.*s", cmd_buff,
ifa->interface_name.len, ifa->interface_name.s);
return -1;
}
if (!rpl || rpl->type == REDIS_REPLY_ERROR) {
if (!rpl)
plog(LOG_ERR, "%s", redis->ctxt->errstr);
else {
plog(LOG_ERR, "%.*s", rpl->len, rpl->str);
freeReplyObject(rpl);
}
// reconnect on error
redis_connect_all(redis);
continue;
}
if (rpl->elements <= 0) {
plog(LOG_INFO, "Found empty set: %.*s\n", ifa->interface_name.len, ifa->interface_name.s);
(*length)[i] = 0;
continue;
} else {
plog(LOG_INFO, "Found elements in set: %.*s\n", ifa->interface_name.len, ifa->interface_name.s);
}
(*list)[i] = g_malloc0(sizeof(str) * rpl->elements);
(*length)[i] = rpl->elements;
for (j = 0; j < rpl->elements; j++) {
if (rpl->element[j]->len <= 0) {
plog(LOG_INFO, "Found empty element in set: %.*s\n", ifa->interface_name.len, ifa->interface_name.s);
continue;
}
(*list)[i][j].s = g_malloc0(rpl->element[j]->len);
(*list)[i][j].len = rpl->element[j]->len;
memcpy((*list)[i][j].s, rpl->element[j]->str, (*list)[i][j].len);
}
freeReplyObject(rpl);
}
return 1;
}
static const char *__crypto_find_name(const struct crypto_suite *ptr) {
int i;
const struct crypto_suite *cs;
for (i = 0; i < num_crypto_suites; i++) {
cs = &crypto_suites[i];
if (cs == ptr)
return cs->name;
}
return NULL;