-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod_rayo.c
5414 lines (4864 loc) · 176 KB
/
mod_rayo.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
/*
* mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2013-2018, Grasshopper
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is Grasshopper
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Chris Rienzo <[email protected]>
*
* mod_rayo.c -- Rayo server / node implementation. Allows MxN clustering of FreeSWITCH and Rayo Clients (like Adhearsion)
*
*/
#include <switch.h>
#include <iksemel.h>
#include "mod_rayo.h"
#include "rayo_components.h"
#include "rayo_elements.h"
#include "xmpp_streams.h"
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_rayo_shutdown);
SWITCH_MODULE_LOAD_FUNCTION(mod_rayo_load);
SWITCH_MODULE_RUNTIME_FUNCTION(mod_rayo_runtime);
SWITCH_MODULE_DEFINITION(mod_rayo, mod_rayo_load, mod_rayo_shutdown, mod_rayo_runtime);
#define RAYO_CAUSE_HANGUP SWITCH_CAUSE_NORMAL_CLEARING
#define RAYO_CAUSE_DECLINE SWITCH_CAUSE_CALL_REJECTED
#define RAYO_CAUSE_BUSY SWITCH_CAUSE_USER_BUSY
#define RAYO_CAUSE_ERROR SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE
#define RAYO_END_REASON_HANGUP "hungup"
#define RAYO_END_REASON_HANGUP_LOCAL "hangup-command"
#define RAYO_END_REASON_TIMEOUT "timeout"
#define RAYO_END_REASON_BUSY "busy"
#define RAYO_END_REASON_REJECT "rejected"
#define RAYO_END_REASON_ERROR "error"
#define RAYO_SIP_REQUEST_HEADER "sip_h_"
#define RAYO_SIP_RESPONSE_HEADER "sip_rh_"
#define RAYO_SIP_PROVISIONAL_RESPONSE_HEADER "sip_ph_"
#define RAYO_SIP_BYE_RESPONSE_HEADER "sip_bye_h_"
#define RAYO_CONFIG_FILE "rayo.conf"
#define JOINED_CALL 1
#define JOINED_MIXER 2
#define OFFER_ALL 0
#define OFFER_FIRST 1
#define OFFER_RANDOM 2
struct rayo_actor;
struct rayo_client;
struct rayo_call;
#define rayo_call_get_uuid(call) RAYO_ID(call)
/**
* Function pointer wrapper for the handlers hash
*/
struct rayo_xmpp_handler {
const char *from_type;
const char *from_subtype;
const char *to_type;
const char *to_subtype;
rayo_actor_xmpp_handler fn;
};
/**
* Client availability
*/
enum presence_status {
PS_UNKNOWN = -1,
PS_OFFLINE = 0,
PS_ONLINE = 1
};
/**
* A xmpp peer server that routes messages to/from clients
*/
struct rayo_peer_server {
/** base class */
struct rayo_actor base;
/** clients connected via this server */
switch_hash_t *clients;
};
#define RAYO_PEER_SERVER(x) ((struct rayo_peer_server *)x)
/**
* A Rayo client that controls calls
*/
struct rayo_client {
/** base class */
struct rayo_actor base;
/** availability */
enum presence_status availability;
/** set if reachable via s2s */
struct rayo_peer_server *peer_server;
/** domain or full JID to route to */
const char *route;
/** time when last probe was sent */
switch_time_t last_probe;
};
#define RAYO_CLIENT(x) ((struct rayo_client *)x)
/**
* A call controlled by a Rayo client
*/
struct rayo_call {
/** actor base class */
struct rayo_actor base;
/** Definitive controlling party JID */
char *dcp_jid;
/** Potential controlling parties (have sent offers to) */
switch_hash_t *pcps;
/** Available controlling parties (not sent offers to) */
switch_hash_t *acps;
/** Number of available controlling parties */
int num_acps;
/** current idle start time */
switch_time_t idle_start_time;
/** true if fax is in progress */
int faxing;
/** 1 if joined to call, 2 if joined to mixer */
int joined;
/** pending join */
iks *pending_join_request;
/** ID of joined party TODO this will be many mixers / calls */
const char *joined_id;
/** set if response needs to be sent to IQ request */
const char *dial_request_id;
/** channel destroy event */
switch_event_t *end_event;
/** True if ringing event sent to client */
int ringing_sent;
/** true if rayo app has started */
int rayo_app_started;
/** delayed delivery of answer event because rayo APP wasn't started yet */
switch_event_t *answer_event;
/** True if request to create this call failed */
int dial_request_failed;
};
/**
* A conference
*/
struct rayo_mixer {
/** actor base class */
struct rayo_actor base;
/** member JIDs */
switch_hash_t *members;
/** subscriber JIDs */
switch_hash_t *subscribers;
};
/**
* A member of a mixer
*/
struct rayo_mixer_member {
/** JID of member */
const char *jid;
/** Controlling party JID */
const char *dcp_jid;
};
/**
* A subscriber to mixer events
*/
struct rayo_mixer_subscriber {
/** JID of client */
const char *jid;
/** Number of client's calls in mixer */
int ref_count;
};
/**
* Module state
*/
static struct {
/** module memory pool */
switch_memory_pool_t *pool;
/** Rayo <iq> set commands mapped to functions */
switch_hash_t *command_handlers;
/** Rayo <presence> events mapped to functions */
switch_hash_t *event_handlers;
/** Active Rayo actors mapped by JID */
switch_hash_t *actors;
/** Rayo actors pending destruction */
switch_hash_t *destroy_actors;
/** Active Rayo actors mapped by internal ID */
switch_hash_t *actors_by_id;
/** synchronizes access to actors */
switch_mutex_t *actors_mutex;
/** map of DCP JID to client */
switch_hash_t *clients_roster;
/** synchronizes access to available clients */
switch_mutex_t *clients_mutex;
/** server for calls/mixers/etc */
struct rayo_actor *server;
/** Maximum idle time before call is considered abandoned */
int max_idle_ms;
/** Conference profile to use for mixers */
char *mixer_conf_profile;
/** to URI prefixes mapped to gateways */
switch_hash_t *dial_gateways;
/** synchronizes access to dial gateways */
switch_mutex_t *dial_gateways_mutex;
/** console command aliases */
switch_hash_t *cmd_aliases;
/** global console */
struct rayo_client *console;
/** XMPP context */
struct xmpp_stream_context *xmpp_context;
/** number of message threads */
int num_message_threads;
/** message delivery queue */
switch_queue_t *msg_queue;
/** in progress offer queue */
switch_queue_t *offer_queue;
/** shutdown flag */
int shutdown;
/** prevents context shutdown until all threads are finished */
switch_thread_rwlock_t *shutdown_rwlock;
/** if true, URI is put in from/to of offer if available */
int offer_uri;
/** if true, pause inbound calling if all clients are offline */
int pause_when_offline;
/** flag to reduce log noise */
int offline_logged;
/** if true, channel variables are added to offer */
int add_variables_to_offer;
/** if true, channel variables are added to answered, ringing, end events */
int add_variables_to_events;
/** How to distribute offers to clients */
int offer_algorithm;
/** How long to wait for offer response before retrying */
int offer_timeout_us;
} globals;
/**
* An outbound dial gateway
*/
struct dial_gateway {
/** URI prefix to match */
const char *uri_prefix;
/** dial prefix to match */
const char *dial_prefix;
/** number of digits to strip from dialstring */
int strip;
};
static void rayo_call_send(struct rayo_actor *call, struct rayo_message *msg);
static void rayo_server_send(struct rayo_actor *server, struct rayo_message *msg);
static void rayo_mixer_send(struct rayo_actor *mixer, struct rayo_message *msg);
static void rayo_component_send(struct rayo_actor *component, struct rayo_message *msg);
static void rayo_client_send(struct rayo_actor *client, struct rayo_message *msg);
static void rayo_console_client_send(struct rayo_actor *client, struct rayo_message *msg);
static void on_client_presence(struct rayo_client *rclient, iks *node);
typedef switch_bool_t (* rayo_actor_match_fn)(struct rayo_actor *);
static switch_bool_t is_call_actor(struct rayo_actor *actor);
static void rayo_call_send_end(struct rayo_call *call, switch_event_t *event, int local_hangup, const char *cause_str, const char *cause_q850_str);
/**
* Entity features returned by service discovery
*/
struct entity_identity {
/** identity category */
const char *category;
/** identity type */
const char *type;
};
static struct entity_identity rayo_server_identity = { "server", "im" };
static const char *rayo_server_features[] = { IKS_NS_XMPP_ENTITY_CAPABILITIES, IKS_NS_XMPP_DISCO, RAYO_NS, RAYO_CPA_NS, RAYO_FAX_NS, 0 };
static struct entity_identity rayo_mixer_identity = { "client", "rayo_mixer" };
static const char *rayo_mixer_features[] = { 0 };
static struct entity_identity rayo_call_identity = { "client", "rayo_call" };
static const char *rayo_call_features[] = { 0 };
/**
* Calculate SHA-1 hash of entity capabilities
* @param identity of entity
* @param features of identity (NULL terminated)
* @return base64 hash (free when done)
*/
static char *calculate_entity_sha1_ver(struct entity_identity *identity, const char **features)
{
int i;
const char *feature;
char ver[SHA_1_HASH_BUF_SIZE + 1] = { 0 };
iksha *sha;
sha = iks_sha_new();
iks_sha_hash(sha, (const unsigned char *)identity->category, strlen(identity->category), 0);
iks_sha_hash(sha, (const unsigned char *)"/", 1, 0);
iks_sha_hash(sha, (const unsigned char *)identity->type, strlen(identity->type), 0);
iks_sha_hash(sha, (const unsigned char *)"//", 2, 0);
i = 0;
while ((feature = features[i++])) {
iks_sha_hash(sha, (const unsigned char *)"<", 1, 0);
iks_sha_hash(sha, (const unsigned char *)feature, strlen(feature), 0);
}
iks_sha_hash(sha, (const unsigned char *)"<", 1, 1);
iks_sha_print_base64(sha, ver);
iks_sha_delete(sha);
return strdup(ver);
}
/**
* @param msg to check
* @return true if message was sent by admin client (console)
*/
static int is_admin_client_message(struct rayo_message *msg)
{
return !zstr(msg->from_jid) && !strcmp(RAYO_JID(globals.console), msg->from_jid);
}
/**
* @param msg to check
* @return true if from/to bare JIDs match
*/
static int is_internal_message(struct rayo_message *msg)
{
return msg->from && msg->to && (iks_id_cmp(msg->from, msg->to, IKS_ID_PARTIAL) == 0);
}
/**
* Presence status
* @param status the presence status
* @return the string value of status
*/
static const char *presence_status_to_string(enum presence_status status)
{
switch(status) {
case PS_OFFLINE: return "OFFLINE";
case PS_ONLINE: return "ONLINE";
case PS_UNKNOWN: return "UNKNOWN";
}
return "UNKNOWN";
}
/**
* Get rayo cause code from FS hangup cause
* @param cause FS hangup cause
* @return rayo end cause
*/
static const char *switch_cause_to_rayo_cause(switch_call_cause_t cause)
{
switch (cause) {
case SWITCH_CAUSE_NONE:
case SWITCH_CAUSE_NORMAL_CLEARING:
return RAYO_END_REASON_HANGUP;
case SWITCH_CAUSE_UNALLOCATED_NUMBER:
case SWITCH_CAUSE_NO_ROUTE_TRANSIT_NET:
case SWITCH_CAUSE_NO_ROUTE_DESTINATION:
case SWITCH_CAUSE_CHANNEL_UNACCEPTABLE:
return RAYO_END_REASON_ERROR;
case SWITCH_CAUSE_CALL_AWARDED_DELIVERED:
return RAYO_END_REASON_HANGUP;
case SWITCH_CAUSE_USER_BUSY:
return RAYO_END_REASON_BUSY;
case SWITCH_CAUSE_NO_USER_RESPONSE:
case SWITCH_CAUSE_NO_ANSWER:
return RAYO_END_REASON_TIMEOUT;
case SWITCH_CAUSE_SUBSCRIBER_ABSENT:
return RAYO_END_REASON_ERROR;
case SWITCH_CAUSE_CALL_REJECTED:
return RAYO_END_REASON_REJECT;
case SWITCH_CAUSE_NUMBER_CHANGED:
case SWITCH_CAUSE_REDIRECTION_TO_NEW_DESTINATION:
case SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR:
case SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER:
case SWITCH_CAUSE_INVALID_NUMBER_FORMAT:
return RAYO_END_REASON_ERROR;
case SWITCH_CAUSE_FACILITY_REJECTED:
return RAYO_END_REASON_REJECT;
case SWITCH_CAUSE_RESPONSE_TO_STATUS_ENQUIRY:
case SWITCH_CAUSE_NORMAL_UNSPECIFIED:
return RAYO_END_REASON_HANGUP;
case SWITCH_CAUSE_NORMAL_CIRCUIT_CONGESTION:
case SWITCH_CAUSE_NETWORK_OUT_OF_ORDER:
case SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE:
case SWITCH_CAUSE_SWITCH_CONGESTION:
case SWITCH_CAUSE_ACCESS_INFO_DISCARDED:
case SWITCH_CAUSE_REQUESTED_CHAN_UNAVAIL:
case SWITCH_CAUSE_PRE_EMPTED:
case SWITCH_CAUSE_FACILITY_NOT_SUBSCRIBED:
case SWITCH_CAUSE_OUTGOING_CALL_BARRED:
case SWITCH_CAUSE_INCOMING_CALL_BARRED:
case SWITCH_CAUSE_BEARERCAPABILITY_NOTAUTH:
case SWITCH_CAUSE_BEARERCAPABILITY_NOTAVAIL:
case SWITCH_CAUSE_SERVICE_UNAVAILABLE:
case SWITCH_CAUSE_BEARERCAPABILITY_NOTIMPL:
case SWITCH_CAUSE_CHAN_NOT_IMPLEMENTED:
case SWITCH_CAUSE_FACILITY_NOT_IMPLEMENTED:
case SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED:
case SWITCH_CAUSE_INVALID_CALL_REFERENCE:
case SWITCH_CAUSE_INCOMPATIBLE_DESTINATION:
case SWITCH_CAUSE_INVALID_MSG_UNSPECIFIED:
case SWITCH_CAUSE_MANDATORY_IE_MISSING:
return RAYO_END_REASON_ERROR;
case SWITCH_CAUSE_MESSAGE_TYPE_NONEXIST:
case SWITCH_CAUSE_WRONG_MESSAGE:
case SWITCH_CAUSE_IE_NONEXIST:
case SWITCH_CAUSE_INVALID_IE_CONTENTS:
case SWITCH_CAUSE_WRONG_CALL_STATE:
case SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE:
case SWITCH_CAUSE_MANDATORY_IE_LENGTH_ERROR:
case SWITCH_CAUSE_PROTOCOL_ERROR:
return RAYO_END_REASON_ERROR;
case SWITCH_CAUSE_INTERWORKING:
case SWITCH_CAUSE_SUCCESS:
case SWITCH_CAUSE_ORIGINATOR_CANCEL:
return RAYO_END_REASON_HANGUP;
case SWITCH_CAUSE_CRASH:
case SWITCH_CAUSE_SYSTEM_SHUTDOWN:
case SWITCH_CAUSE_LOSE_RACE:
case SWITCH_CAUSE_MANAGER_REQUEST:
case SWITCH_CAUSE_BLIND_TRANSFER:
case SWITCH_CAUSE_ATTENDED_TRANSFER:
case SWITCH_CAUSE_ALLOTTED_TIMEOUT:
case SWITCH_CAUSE_USER_CHALLENGE:
case SWITCH_CAUSE_MEDIA_TIMEOUT:
case SWITCH_CAUSE_PICKED_OFF:
case SWITCH_CAUSE_USER_NOT_REGISTERED:
case SWITCH_CAUSE_PROGRESS_TIMEOUT:
case SWITCH_CAUSE_INVALID_GATEWAY:
case SWITCH_CAUSE_GATEWAY_DOWN:
case SWITCH_CAUSE_INVALID_URL:
case SWITCH_CAUSE_INVALID_PROFILE:
case SWITCH_CAUSE_NO_PICKUP:
case SWITCH_CAUSE_SRTP_READ_ERROR:
return RAYO_END_REASON_ERROR;
default:
break;
}
return RAYO_END_REASON_HANGUP;
}
/**
* Add <header> to node
* @param node to add <header> to
* @param name of header
* @param value of header
*/
static void add_header(iks *node, const char *name, const char *value)
{
if (!zstr(name) && !zstr(value)) {
iks *header = iks_insert(node, "header");
iks_insert_attrib(header, "name", name);
iks_insert_attrib(header, "value", value);
}
}
/**
* Add SIP <header>s to node
* @param node to add <header> to
* @param event source
* @param add_variables true if channel variables should be added
*/
static void add_headers_to_event(iks *node, switch_event_t *event, int add_variables)
{
switch_event_header_t *header;
/* get all variables prefixed with sip_h_ */
for (header = event->headers; header; header = header->next) {
if (!strncmp("variable_sip_h_", header->name, 15)) {
if (!zstr(header->name)) {
add_header(node, header->name + 15, header->value);
}
} else if (add_variables && !strncmp("variable_", header->name, 9)) {
if (!zstr(header->name)) {
char header_name[1024];
snprintf(header_name, 1024, "variable-%s", header->name + 9);
add_header(node, header_name, header->value);
}
}
}
}
/**
* Add SIP <header>s to node
* @param node to add <header> to
* @param add_variables true if channel variables should be added
*/
static void add_channel_headers_to_event(iks *node, switch_channel_t *channel, int add_variables)
{
switch_event_header_t *var;
/* add all SIP header variables and (if configured) all other variables */
if ((var = switch_channel_variable_first(channel))) {
for (; var; var = var->next) {
if (!strncmp("sip_h_", var->name, 6)) {
add_header(node, var->name + 6, var->value);
}
if (add_variables) {
char var_name[1024];
snprintf(var_name, 1024, "variable-%s", var->name);
add_header(node, var_name, var->value);
}
}
switch_channel_variable_last(channel);
}
}
static void pause_inbound_calling(void)
{
int32_t arg = 1;
switch_mutex_lock(globals.clients_mutex);
switch_core_session_ctl(SCSC_PAUSE_INBOUND, &arg);
if (!globals.offline_logged) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Pausing inbound calling\n");
globals.offline_logged = 1;
}
switch_mutex_unlock(globals.clients_mutex);
}
static void resume_inbound_calling(void)
{
int32_t arg = 0;
switch_mutex_lock(globals.clients_mutex);
switch_core_session_ctl(SCSC_PAUSE_INBOUND, &arg);
if (globals.offline_logged) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Resuming inbound calling\n");
globals.offline_logged = 0;
}
switch_mutex_unlock(globals.clients_mutex);
}
/**
* Check online status of rayo client(s) and pause/resume the server
*/
static void pause_when_offline(void)
{
if (globals.pause_when_offline) {
int is_online = 0;
switch_hash_index_t *hi;
switch_mutex_lock(globals.clients_mutex);
for (hi = switch_core_hash_first(globals.clients_roster); hi; hi = switch_core_hash_next(&hi)) {
const void *key;
void *client;
switch_core_hash_this(hi, &key, NULL, &client);
switch_assert(client);
if (RAYO_CLIENT(client)->availability == PS_ONLINE) {
is_online = 1;
break;
}
}
switch_safe_free(hi);
if (is_online) {
resume_inbound_calling();
} else {
pause_inbound_calling();
}
switch_mutex_unlock(globals.clients_mutex);
}
}
/**
* Send event to clients
* @param from event sender
* @param rayo_event the event to send
* @param online_only only send to online clients
*/
static void broadcast_event(struct rayo_actor *from, iks *rayo_event, int online_only)
{
switch_hash_index_t *hi = NULL;
switch_mutex_lock(globals.clients_mutex);
for (hi = switch_core_hash_first(globals.clients_roster); hi; hi = switch_core_hash_next(&hi)) {
struct rayo_client *rclient;
const void *key;
void *val;
switch_core_hash_this(hi, &key, NULL, &val);
rclient = (struct rayo_client *)val;
switch_assert(rclient);
if (!online_only || rclient->availability == PS_ONLINE) {
iks_insert_attrib(rayo_event, "to", RAYO_JID(rclient));
RAYO_SEND_MESSAGE_DUP(from, RAYO_JID(rclient), rayo_event);
}
}
switch_mutex_unlock(globals.clients_mutex);
}
/**
* Add an outbound dialing gateway
* @param uri_prefix to match
* @param dial_prefix to use
* @param strip number of digits to strip from dialstring
*/
static void dial_gateway_add(const char *uri_prefix, const char *dial_prefix, int strip)
{
struct dial_gateway *gateway = switch_core_alloc(globals.pool, sizeof(*gateway));
gateway->uri_prefix = uri_prefix ? switch_core_strdup(globals.pool, uri_prefix) : "";
gateway->dial_prefix = dial_prefix ? switch_core_strdup(globals.pool, dial_prefix) : "";
gateway->strip = strip > 0 ? strip : 0;
switch_core_hash_insert(globals.dial_gateways, uri_prefix, gateway);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "dial-gateway uriprefix = %s, dialprefix = %s, strip = %i\n", uri_prefix, dial_prefix, strip);
}
/**
* Find outbound dial gateway for the specified dialstring
*/
static struct dial_gateway *dial_gateway_find(const char *uri)
{
switch_hash_index_t *hi = NULL;
int match_len = 0;
struct dial_gateway *gateway = (struct dial_gateway *)switch_core_hash_find(globals.dial_gateways, "default");
/* find longest prefix match */
switch_mutex_lock(globals.dial_gateways_mutex);
for (hi = switch_core_hash_first(globals.dial_gateways); hi; hi = switch_core_hash_next(&hi)) {
struct dial_gateway *candidate = NULL;
const void *prefix;
int prefix_len = 0;
void *val;
switch_core_hash_this(hi, &prefix, NULL, &val);
candidate = (struct dial_gateway *)val;
switch_assert(candidate);
prefix_len = strlen(prefix);
if (!zstr(prefix) && !strncmp(prefix, uri, prefix_len) && prefix_len > match_len) {
match_len = prefix_len;
gateway = candidate;
}
}
switch_mutex_unlock(globals.dial_gateways_mutex);
return gateway;
}
/**
* Add command handler function
* @param name the command name
* @param handler the command handler function
*/
static void rayo_command_handler_add(const char *name, struct rayo_xmpp_handler *handler)
{
char full_name[1024];
full_name[1023] = '\0';
snprintf(full_name, sizeof(full_name) - 1, "%s:%s:%s", handler->to_type, handler->to_subtype, name);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding command: %s\n", full_name);
switch_core_hash_insert(globals.command_handlers, full_name, handler);
}
/**
* Add command handler function
* @param type the actor type
* @param subtype the actor subtype
* @param name the command name
* @param fn the command callback function
*/
void rayo_actor_command_handler_add(const char *type, const char *subtype, const char *name, rayo_actor_xmpp_handler fn)
{
struct rayo_xmpp_handler *handler = switch_core_alloc(globals.pool, sizeof (*handler));
handler->to_type = zstr(type) ? "" : switch_core_strdup(globals.pool, type);
handler->to_subtype = zstr(subtype) ? "" : switch_core_strdup(globals.pool, subtype);
handler->fn = fn;
rayo_command_handler_add(name, handler);
}
/**
* Get command handler function from hash
* @param hash the hash to search
* @param msg the command
* @return the command handler function or NULL
*/
rayo_actor_xmpp_handler rayo_actor_command_handler_find(struct rayo_actor *actor, struct rayo_message *msg)
{
iks *iq = msg->payload;
const char *iq_type = iks_find_attrib_soft(iq, "type");
iks *command = iks_first_tag(iq);
const char *name = "";
const char *namespace = "";
struct rayo_xmpp_handler *handler = NULL;
char full_name[1024];
full_name[1023] = '\0';
if (command) {
name = iks_name(command);
namespace = iks_find_attrib_soft(command, "xmlns");
if (zstr(name)) {
name = "";
}
}
snprintf(full_name, sizeof(full_name) - 1, "%s:%s:%s:%s:%s", actor->type, actor->subtype, iq_type, namespace, name);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s, looking for %s command\n", RAYO_JID(actor), full_name);
handler = (struct rayo_xmpp_handler *)switch_core_hash_find(globals.command_handlers, full_name);
if (handler) {
return handler->fn;
}
return NULL;
}
/**
* Add event handler function
* @param name the event name
* @param handler the event handler function
*/
static void rayo_event_handler_add(const char *name, struct rayo_xmpp_handler *handler)
{
char full_name[1024];
full_name[1023] = '\0';
snprintf(full_name, sizeof(full_name) - 1, "%s:%s:%s:%s:%s", handler->from_type, handler->from_subtype, handler->to_type, handler->to_subtype, name);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding event: %s\n", full_name);
switch_core_hash_insert(globals.event_handlers, full_name, handler);
}
/**
* Add event handler function
* @param from_type the source actor type
* @param from_subtype the source actor subtype
* @param to_type the destination actor type
* @param to_subtype the destination actor subtype
* @param name the event name
* @param fn the event callback function
*/
void rayo_actor_event_handler_add(const char *from_type, const char *from_subtype, const char *to_type, const char *to_subtype, const char *name, rayo_actor_xmpp_handler fn)
{
struct rayo_xmpp_handler *handler = switch_core_alloc(globals.pool, sizeof (*handler));
handler->from_type = zstr(from_type) ? "" : switch_core_strdup(globals.pool, from_type);
handler->from_subtype = zstr(from_subtype) ? "" : switch_core_strdup(globals.pool, from_subtype);
handler->to_type = zstr(to_type) ? "" : switch_core_strdup(globals.pool, to_type);
handler->to_subtype = zstr(to_subtype) ? "" : switch_core_strdup(globals.pool, to_subtype);
handler->fn = fn;
rayo_event_handler_add(name, handler);
}
/**
* Get event handler function from hash
* @param actor the event destination
* @param msg the event
* @return the event handler function or NULL
*/
rayo_actor_xmpp_handler rayo_actor_event_handler_find(struct rayo_actor *actor, struct rayo_message *msg)
{
iks *presence = msg->payload;
iks *event = iks_first_tag(presence);
if (event) {
struct rayo_xmpp_handler *handler = NULL;
const char *presence_type = iks_find_attrib_soft(presence, "type");
const char *event_name = iks_name(event);
const char *event_namespace = iks_find_attrib_soft(event, "xmlns");
char full_name[1024];
full_name[1023] = '\0';
if (zstr(event_name)) {
return NULL;
}
snprintf(full_name, sizeof(full_name) - 1, "%s:%s:%s:%s:%s:%s:%s", msg->from_type, msg->from_subtype, actor->type, actor->subtype, presence_type, event_namespace, event_name);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s => %s, looking for %s event handler\n", msg->from_jid, RAYO_JID(actor), full_name);
handler = (struct rayo_xmpp_handler *)switch_core_hash_find(globals.event_handlers, full_name);
if (handler) {
return handler->fn;
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s => %s, event missing child element\n", msg->from_jid, RAYO_JID(actor));
}
return NULL;
}
/**
* Clean up a message
* @param msg to destroy
*/
void rayo_message_destroy(struct rayo_message *msg)
{
if (msg) {
if (msg->payload) {
iks_delete(msg->payload);
}
switch_safe_free(msg->to_jid);
switch_safe_free(msg->from_jid);
switch_safe_free(msg->from_type);
switch_safe_free(msg->from_subtype);
switch_safe_free(msg->file);
free(msg);
}
}
/**
* Remove payload from message
*/
iks *rayo_message_remove_payload(struct rayo_message *msg)
{
iks *payload = msg->payload;
msg->payload = NULL;
msg->from = NULL;
msg->to = NULL;
return payload;
}
/**
* Thread that delivers internal XMPP messages
* @param thread this thread
* @param obj unused
* @return NULL
*/
static void *SWITCH_THREAD_FUNC deliver_message_thread(switch_thread_t *thread, void *obj)
{
struct rayo_message *msg = NULL;
switch_thread_rwlock_rdlock(globals.shutdown_rwlock);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "New message delivery thread\n");
while (!globals.shutdown) {
if (switch_queue_pop(globals.msg_queue, (void *)&msg) == SWITCH_STATUS_SUCCESS) {
struct rayo_actor *actor = RAYO_LOCATE(msg->to_jid);
if (actor) {
/* deliver to actor */
switch_mutex_lock(actor->mutex);
switch_log_printf(SWITCH_CHANNEL_ID_LOG, msg->file, "", msg->line, "", SWITCH_LOG_DEBUG, "Deliver %s => %s %s\n", msg->from_jid, msg->to_jid, iks_string(iks_stack(msg->payload), msg->payload));
actor->send_fn(actor, msg);
switch_mutex_unlock(actor->mutex);
RAYO_RELEASE(actor);
} else if (!msg->is_reply) {
/* unknown actor */
RAYO_SEND_REPLY(globals.server, msg->from_jid, iks_new_error(msg->payload, STANZA_ERROR_ITEM_NOT_FOUND));
}
rayo_message_destroy(msg);
}
}
/* clean up remaining messages */
while(switch_queue_trypop(globals.msg_queue, (void *)&msg) == SWITCH_STATUS_SUCCESS) {
rayo_message_destroy(msg);
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Message delivery thread finished\n");
switch_thread_rwlock_unlock(globals.shutdown_rwlock);
return NULL;
}
/**
* Create a new message thread
* @param pool to use
*/
static void start_deliver_message_thread(switch_memory_pool_t *pool)
{
switch_thread_t *thread;
switch_threadattr_t *thd_attr = NULL;
switch_threadattr_create(&thd_attr, pool);
switch_threadattr_detach_set(thd_attr, 1);
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
switch_thread_create(&thread, thd_attr, deliver_message_thread, NULL, pool);
}
/**
* Stop all threads
*/
static void stop_all_threads(void)
{
globals.shutdown = 1;
if (globals.msg_queue) {
switch_queue_interrupt_all(globals.msg_queue);
}
if (globals.offer_queue) {
switch_queue_interrupt_all(globals.offer_queue);
}
if (globals.shutdown_rwlock) {
switch_thread_rwlock_wrlock(globals.shutdown_rwlock);
}
}
/**
* Send message to actor addressed by JID
* @param from actor sending the message
* @param to destination JID
* @param payload the message payload to deliver
* @param dup true if payload is to be copied
* @param reply true if a reply
* @param file file name
* @param line line number
*/
void rayo_message_send(struct rayo_actor *from, const char *to, iks *payload, int dup, int reply, const char *file, int line)
{
const char *msg_name;
struct rayo_message *msg = malloc(sizeof(*msg));
switch_assert(msg);
if (dup) {
msg->payload = iks_copy(payload);
} else {
msg->payload = payload;
}
msg->is_reply = reply;
msg->to_jid = strdup(zstr(to) ? "" : to);
if (!zstr(msg->to_jid)) {
msg->to = iks_id_new(iks_stack(msg->payload), msg->to_jid);
}
msg->from_jid = strdup(RAYO_JID(from));
if (!zstr(msg->from_jid)) {
msg->from = iks_id_new(iks_stack(msg->payload), msg->from_jid);
}
msg->from_type = strdup(zstr(from->type) ? "" : from->type);
msg->from_subtype = strdup(zstr(from->subtype) ? "" : from->subtype);
msg->file = strdup(file);
msg->line = line;
/* add timestamp to presence events */
msg_name = iks_name(msg->payload);
if (!zstr(msg_name) && !strcmp("presence", msg_name)) {
/* don't add timestamp if there already is one */
iks *delay = iks_find(msg->payload, "delay");
if (!delay || strcmp("urn:xmpp:delay", iks_find_attrib_soft(delay, "xmlns"))) {
switch_time_exp_t tm;
char timestamp[80];
switch_size_t retsize;
delay = iks_insert(msg->payload, "delay");
iks_insert_attrib(delay, "xmlns", "urn:xmpp:delay");
switch_time_exp_tz(&tm, switch_time_now(), 0);
switch_strftime_nocheck(timestamp, &retsize, sizeof(timestamp), "%Y-%m-%dT%TZ", &tm);
iks_insert_attrib_printf(delay, "stamp", "%s", timestamp);
}
}
if (switch_queue_trypush(globals.msg_queue, msg) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "failed to queue message!\n");
rayo_message_destroy(msg);
}
}
/**
* Get access to Rayo actor with JID.
* @param jid the JID
* @return the actor or NULL. Call RAYO_RELEASE() when done with pointer.
*/
struct rayo_actor *rayo_actor_locate(const char *jid, const char *file, int line)
{
struct rayo_actor *actor = NULL;
switch_mutex_lock(globals.actors_mutex);
if (!strncmp("xmpp:", jid, 5)) {
jid = jid + 5;
}
actor = (struct rayo_actor *)switch_core_hash_find(globals.actors, jid);
if (actor) {
if (!actor->destroy) {
actor->ref_count++;
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, "", line, "", SWITCH_LOG_DEBUG, "Locate (jid) %s: ref count = %i\n", RAYO_JID(actor), actor->ref_count);
} else {
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, "", line, "", SWITCH_LOG_WARNING, "Locate (jid) %s: already marked for destruction!\n", jid);
actor = NULL;
}
}
switch_mutex_unlock(globals.actors_mutex);
return actor;
}
/**
* Get exclusive access to Rayo actor with internal ID
* @param id the internal ID
* @return the actor or NULL. Call RAYO_RELEASE() when done with pointer.
*/
struct rayo_actor *rayo_actor_locate_by_id(const char *id, const char *file, int line)
{
struct rayo_actor *actor = NULL;
if (!zstr(id)) {
switch_mutex_lock(globals.actors_mutex);
actor = (struct rayo_actor *)switch_core_hash_find(globals.actors_by_id, id);
if (actor) {
if (!actor->destroy) {
actor->ref_count++;