-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
mod_commands.c
7960 lines (6553 loc) · 226 KB
/
mod_commands.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
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
*
* 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 FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthm@freeswitch.org>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthm@freeswitch.org>
* Michael Jerris <mike@jerris.com>
* Johny Kadarisman <jkr888@gmail.com>
* Paul Tinsley <jackhammer@gmail.com>
* Marcel Barbulescu <marcelbarbulescu@gmail.com>
* Bret McDanel <trixter AT 0xdecafbad.com>
* Cesar Cepeda <cesar@auronix.com>
* Massimo Cetra <devel@navynet.it>
* Rupa Schomaker <rupa@rupa.com>
* Joseph Sullivan <jossulli@amazon.com>
* Raymond Chandler <intralanman@freeswitch.org>
* Seven Du <dujinfang@gmail.com>
* Garmt Boekholt <garmt@cimico.com>
*
* mod_commands.c -- Misc. Command Module
*
*/
#include <switch.h>
#include <switch_stun.h>
SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_commands_shutdown);
SWITCH_MODULE_DEFINITION(mod_commands, mod_commands_load, mod_commands_shutdown, NULL);
static switch_mutex_t *reload_mutex = NULL;
struct cb_helper {
uint32_t row_process;
switch_stream_handle_t *stream;
};
struct stream_format {
char *http; /* http cmd (from xmlrpc) */
char *query; /* http query (cmd args) */
switch_bool_t api; /* flag: define content type for http reply e.g. text/html or text/xml */
switch_bool_t html; /* flag: format as html */
char *nl; /* newline to use: html "<br>\n" or just "\n" */
};
typedef struct stream_format stream_format;
static int url_callback(void *pArg, int argc, char **argv, char **columnNames)
{
struct cb_helper *cb = (struct cb_helper *) pArg;
cb->row_process++;
if (!zstr(argv[0])) {
cb->stream->write_function(cb->stream, "%s,", argv[0]);
}
return 0;
}
static switch_status_t select_url(const char *user,
const char *domain,
const char *concat,
const char *exclude_contact,
switch_stream_handle_t *stream)
{
struct cb_helper cb;
char *sql, *errmsg = NULL;
switch_core_flag_t cflags = switch_core_flags();
switch_cache_db_handle_t *db = NULL;
if (!(cflags & SCF_USE_SQL)) {
stream->write_function(stream, "-ERR SQL disabled, no data available!\n");
return SWITCH_STATUS_SUCCESS;
}
if (switch_core_db_handle(&db) != SWITCH_STATUS_SUCCESS) {
stream->write_function(stream, "%s", "-ERR Database error!\n");
return SWITCH_STATUS_SUCCESS;
}
cb.row_process = 0;
cb.stream = stream;
if (exclude_contact) {
sql = switch_mprintf("select url, '%q' "
"from registrations where reg_user='%q' and realm='%q' "
"and url not like '%%%q%%'", (concat != NULL) ? concat : "", user, domain, exclude_contact);
} else {
sql = switch_mprintf("select url, '%q' "
"from registrations where reg_user='%q' and realm='%q'",
(concat != NULL) ? concat : "", user, domain);
}
switch_assert(sql);
switch_cache_db_execute_sql_callback(db, sql, url_callback, &cb, &errmsg);
if (errmsg) {
stream->write_function(stream, "-ERR SQL error [%s]\n", errmsg);
free(errmsg);
errmsg = NULL;
}
switch_safe_free(sql);
switch_cache_db_release_db_handle(&db);
return SWITCH_STATUS_SUCCESS;
}
static stream_format set_format(stream_format *format, switch_stream_handle_t *stream)
{
format->nl = "\n";
if (stream->param_event && (format->http = switch_event_get_header(stream->param_event, "HTTP-URI"))) {
format->query = switch_event_get_header(stream->param_event, "HTTP-QUERY");
if (switch_event_get_header(stream->param_event, "HTTP-API")) {
format->api = SWITCH_TRUE;
}
if (!strncasecmp(format->http, "/webapi/", 8)) {
format->nl = "<br>\n";
format->html = SWITCH_TRUE;
}
}
return *format;
}
#define SAY_STRING_SYNTAX "<module_name>[.<ext>] <lang>[.<ext>] <say_type> <say_method> [<say_gender>] <text>"
SWITCH_STANDARD_API(say_string_function)
{
char *argv[6] = { 0 };
int argc;
char *lbuf = NULL, *string = NULL;
int err = 1, par = 0;
char *p, *ext = "wav";
char *tosay = NULL;
int strip = 0;
if (cmd) {
lbuf = strdup(cmd);
}
if (lbuf && (argc = switch_separate_string(lbuf, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) && (argc == 5 || argc == 6)) {
if ((p = strchr(argv[0], '.'))) {
*p++ = '\0';
ext = p;
par++;
}
if (!par && (p = strchr(argv[1], '.'))) {
*p++ = '\0';
ext = p;
}
tosay = (argc == 5) ? argv[4] : argv[5];
if (*tosay == '~') {
tosay++;
strip++;
}
switch_ivr_say_string(session,
argv[1],
ext,
tosay,
argv[0],
argv[2],
argv[3],
(argc == 6) ? argv[4] : NULL ,
&string);
if (string) {
stream->write_function(stream, "%s", strip ? string + 14 : string);
free(string);
err = 0;
}
}
if (err) {
stream->write_function(stream, "-ERR Usage: %s\n", SAY_STRING_SYNTAX);
}
free(lbuf);
return SWITCH_STATUS_SUCCESS;
}
struct user_struct {
char *dname;
char *gname;
char *effective_caller_id_name;
char *effective_caller_id_number;
char *callgroup;
switch_xml_t x_user_tag;
switch_stream_handle_t *stream;
char *search_context;
char *context;
switch_xml_t x_domain_tag;
};
static void dump_user(struct user_struct *us)
{
switch_xml_t x_vars, x_var, ux, x_user_tag, x_domain_tag;
switch_status_t status;
switch_stream_handle_t apistream = { 0 }, *stream;
char *user_context = NULL, *search_context = NULL, *context = NULL;
char *effective_caller_id_name = NULL;
char *effective_caller_id_number = NULL;
char *dname = NULL, *gname = NULL, *callgroup = NULL;
char *utype = NULL, *uname = NULL;
char *apip = NULL;
x_user_tag = us->x_user_tag;
x_domain_tag = us->x_domain_tag;
effective_caller_id_name = us->effective_caller_id_name;
effective_caller_id_number = us->effective_caller_id_number;
callgroup = us->callgroup;
dname = us->dname;
gname = us->gname;
stream = us->stream;
context = us->context;
search_context = us->search_context;
if (!x_user_tag) {
return;
}
utype = (char *)switch_xml_attr_soft(us->x_user_tag, "type");
uname = (char *)switch_xml_attr_soft(us->x_user_tag, "id");
if (!strcasecmp(utype, "pointer")) {
if (switch_xml_locate_user_in_domain(uname, x_domain_tag, &ux, NULL) == SWITCH_STATUS_SUCCESS) {
x_user_tag = ux;
}
}
user_context = (char *)context;
if ((x_vars = switch_xml_child(x_user_tag, "variables"))) {
for (x_var = switch_xml_child(x_vars, "variable"); x_var; x_var = x_var->next) {
const char *key = switch_xml_attr_soft(x_var, "name");
const char *val = switch_xml_attr_soft(x_var, "value");
if (!strcasecmp(key, "user_context")) {
user_context = (char*) val;
} else if (!strcasecmp(key, "effective_caller_id_name")) {
effective_caller_id_name = (char*) val;
} else if (!strcasecmp(key, "effective_caller_id_number")) {
effective_caller_id_number = (char*) val;
} else if (!strcasecmp(key, "callgroup")) {
callgroup = (char*) val;
} else {
continue;
}
}
}
if (search_context) {
if (zstr(user_context) || strcasecmp(search_context, user_context)) {
return;
}
}
if(zstr(dname)) {
apip = switch_mprintf("*/%s",switch_xml_attr_soft(x_user_tag, "id"));
} else {
apip = switch_mprintf("*/%s@%s",switch_xml_attr_soft(x_user_tag, "id"), dname);
}
SWITCH_STANDARD_STREAM(apistream);
if ((status = switch_api_execute("sofia_contact", apip, NULL, &apistream)) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "sofia_contact '%s' failed. status: %d \n", apip, status );
goto end;
}
stream->write_function(stream, "%s|%s|%s|%s|%s|%s|%s|%s\n", switch_xml_attr_soft(x_user_tag, "id"), user_context, dname, gname, apistream.data, callgroup, effective_caller_id_name, effective_caller_id_number);
end:
switch_safe_free(apistream.data);
switch_safe_free(apip);
return;
}
char *find_channel_brackets(char *data, char start, char end, char **front, int *local_clobber)
{
char *p;
char *last_end = NULL;
*front = NULL;
p = data;
while ((p = switch_strchr_strict(p, start, " "))) {
char *next_end = switch_find_end_paren(p, start, end);
if (!next_end) {
break;
}
if (!*front) {
*front = p;
}
*p = '[';
last_end = next_end;
*last_end = ']';
p = last_end + 1;
}
if (!last_end) {
if (local_clobber) {
*local_clobber = 0;
}
return data;
}
*last_end = '\0';
if (local_clobber) {
/* Would be nice to use switch_true to account for other valid boolean
representations, but this is better than nothing: */
*local_clobber = strstr(data, "local_var_clobber=true") != NULL;
}
return last_end + 1;
}
char *find_channel_delim(char *p, const char **out)
{
*out = "";
for (; *p; p++) {
if (*p == ',') {
*out = ",";
break;
}
if (*p == '|') {
*out = "|";
break;
}
if (!strncmp(p, SWITCH_ENT_ORIGINATE_DELIM, strlen(SWITCH_ENT_ORIGINATE_DELIM))) {
*out = SWITCH_ENT_ORIGINATE_DELIM;
break;
}
}
return p;
}
/* Read <..> and {..}, and inserts [..] before every leg dial string. */
void output_flattened_dial_string(char *data, switch_stream_handle_t *stream)
{
char *p;
char *vars_start_ent;
char *vars_start_all;
char *vars_start_leg;
int local_clobber_ent;
int local_clobber_all;
const char *delim;
char *leg_dial_string;
/* -3 because ":_:" is the longest delimiter, of length 3. */
p = find_channel_delim(end_of_p(data) - 3, &delim);
*p = '\0';
p = data;
p = find_channel_brackets(p, '<', '>', &vars_start_ent, &local_clobber_ent);
p = find_channel_brackets(p, '{', '}', &vars_start_all, &local_clobber_all);
while (*p) {
p = find_channel_brackets(p, '[', ']', &vars_start_leg, NULL);
if (vars_start_leg) {
if (vars_start_ent && !local_clobber_ent) {
stream->write_function(stream, "%s]", vars_start_ent);
}
if (vars_start_all && !local_clobber_all) {
stream->write_function(stream, "%s]", vars_start_all);
}
stream->write_function(stream, "%s]", vars_start_leg);
}
while (*p == ' ') p++;
if (*p) {
if (vars_start_all && (!vars_start_leg || local_clobber_all)) {
stream->write_function(stream, "%s]", vars_start_all);
}
if (vars_start_ent && (!vars_start_leg || local_clobber_ent)) {
stream->write_function(stream, "%s]", vars_start_ent);
}
leg_dial_string = p;
p = find_channel_delim(p, &delim);
if (*p) {
*p = '\0';
p += strlen(delim);
if (!strcmp(delim, SWITCH_ENT_ORIGINATE_DELIM)) {
p = find_channel_brackets(p, '{', '}', &vars_start_all, &local_clobber_all);
}
}
stream->write_function(stream, "%s%s", leg_dial_string, delim);
}
}
}
#define LIST_USERS_SYNTAX "[group <group>] [domain <domain>] [user <user>] [context <context>]"
SWITCH_STANDARD_API(list_users_function)
{
int argc;
char *pdata = NULL, *argv[9];
int32_t arg = 0;
switch_xml_t xml_root, x_domains, x_domain_tag;
switch_xml_t gts, gt, uts, ut;
char *_user = NULL, *_search_context = NULL, *_group = NULL, *section = "directory";
char *tag_name = NULL, *key_name = NULL, *key_value = NULL;
char *_domain = NULL;
if (!zstr(cmd) && (pdata = strdup(cmd))) {
argc = switch_separate_string(pdata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
if (argc >= 9) {
stream->write_function(stream, "-USAGE: %s\n", LIST_USERS_SYNTAX);
goto done;
}
for (arg = 0; arg < argc; arg++) {
if (!strcasecmp(argv[arg], "user")) {
_user = argv[arg + 1];
}
if (!strcasecmp(argv[arg], "context")) {
_search_context = argv[arg + 1];
}
if (!strcasecmp(argv[arg], "domain")) {
_domain = argv[arg + 1];
}
if (!strcasecmp(argv[arg], "group")) {
_group = argv[arg + 1];
}
}
}
if (_domain) {
tag_name = "domain";
key_name = "name";
key_value = _domain;
}
stream->write_function(stream, "userid|context|domain|group|contact|callgroup|effective_caller_id_name|effective_caller_id_number\n");
if (switch_xml_locate(section, tag_name, key_name, key_value, &xml_root, &x_domains, NULL, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
struct user_struct us = { 0 };
for (x_domain_tag = _domain ? x_domains : switch_xml_child(x_domains, "domain"); x_domain_tag; x_domain_tag = x_domain_tag->next) {
switch_xml_t x_vars, x_var;
us.dname = (char*)switch_xml_attr_soft(x_domain_tag, "name");
if (_domain && strcasecmp(_domain, us.dname)) {
continue;
}
if ((x_vars = switch_xml_child(x_domain_tag, "variables"))) {
if ((x_var = switch_xml_find_child_multi(x_vars, "variable", "name", "user_context", NULL))) {
us.context = (char*)switch_xml_attr_soft(x_var, "value");
}
if ((x_var = switch_xml_find_child_multi(x_vars, "variable", "name", "callgroup", NULL))) {
us.callgroup = (char*)switch_xml_attr_soft(x_var, "value");
}
if ((x_var = switch_xml_find_child_multi(x_vars, "variable", "name", "effective_caller_id_name", NULL))) {
us.effective_caller_id_name = (char*)switch_xml_attr_soft(x_var, "value");
}
if ((x_var = switch_xml_find_child_multi(x_vars, "variable", "name", "effective_caller_id_number", NULL))) {
us.effective_caller_id_number = (char*)switch_xml_attr_soft(x_var, "value");
}
}
if ((gts = switch_xml_child(x_domain_tag, "groups"))) {
for (gt = switch_xml_child(gts, "group"); gt; gt = gt->next) {
us.gname = (char*)switch_xml_attr_soft(gt, "name");
if (_group && strcasecmp(_group, us.gname)) {
continue;
}
if ((x_vars = switch_xml_child(gt, "variables"))) {
if ((x_var = switch_xml_find_child_multi(x_vars, "variable", "name", "user_context", NULL))) {
us.context = (char*)switch_xml_attr_soft(x_var, "value");
}
if ((x_var = switch_xml_find_child_multi(x_vars, "variable", "name", "callgroup", NULL))) {
us.callgroup = (char*)switch_xml_attr_soft(x_var, "value");
}
if ((x_var = switch_xml_find_child_multi(x_vars, "variable", "name", "effective_caller_id_name", NULL))) {
us.effective_caller_id_name = (char*)switch_xml_attr_soft(x_var, "value");
}
if ((x_var = switch_xml_find_child_multi(x_vars, "variable", "name", "effective_caller_id_number", NULL))) {
us.effective_caller_id_number = (char*)switch_xml_attr_soft(x_var, "value");
}
}
for (uts = switch_xml_child(gt, "users"); uts; uts = uts->next) {
for (ut = switch_xml_child(uts, "user"); ut; ut = ut->next) {
if (_user && strcasecmp(_user, switch_xml_attr_soft(ut, "id"))) {
continue;
}
us.x_user_tag = ut;
us.x_domain_tag = x_domain_tag;
us.stream = stream;
us.search_context = _search_context;
dump_user(&us);
}
}
}
} else {
for (uts = switch_xml_child(x_domain_tag, "users"); uts; uts = uts->next) {
for (ut = switch_xml_child(uts, "user"); ut; ut = ut->next) {
if (_user && strcasecmp(_user, switch_xml_attr_soft(ut, "id"))) {
continue;
}
us.x_user_tag = ut;
us.x_domain_tag = x_domain_tag;
us.stream = stream;
us.search_context = _search_context;
dump_user(&us);
}
}
}
}
switch_xml_free(xml_root);
}
stream->write_function(stream, "\n+OK\n");
done:
switch_safe_free(pdata);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(reg_url_function)
{
char *data;
char *user = NULL;
char *domain = NULL, *dup_domain = NULL;
char *concat = NULL;
const char *exclude_contact = NULL;
char *reply = "error/facility_not_subscribed";
switch_stream_handle_t mystream = { 0 };
if (!cmd) {
stream->write_function(stream, "%s", "");
return SWITCH_STATUS_SUCCESS;
}
if (session) {
switch_channel_t *channel = switch_core_session_get_channel(session);
exclude_contact = switch_channel_get_variable(channel, "sip_exclude_contact");
}
data = strdup(cmd);
switch_assert(data);
user = data;
if ((domain = strchr(user, '@'))) {
*domain++ = '\0';
if ((concat = strchr(domain, '/'))) {
*concat++ = '\0';
}
} else {
if ((concat = strchr(user, '/'))) {
*concat++ = '\0';
}
}
if (zstr(domain)) {
dup_domain = switch_core_get_domain(SWITCH_TRUE);
domain = dup_domain;
}
SWITCH_STANDARD_STREAM(mystream);
switch_assert(mystream.data);
select_url(user, domain, concat, exclude_contact, &mystream);
reply = mystream.data;
if (zstr(reply)) {
reply = "error/user_not_registered";
} else if (end_of(reply) == ',') {
end_of(reply) = '\0';
}
stream->write_function(stream, "%s", reply);
reply = NULL;
switch_safe_free(mystream.data);
switch_safe_free(data);
switch_safe_free(dup_domain);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(banner_function)
{
stream->write_function(stream, "%s", switch_core_banner());
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(hostname_api_function)
{
stream->write_function(stream, "%s", switch_core_get_hostname());
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(switchname_api_function)
{
stream->write_function(stream, "%s", switch_core_get_switchname());
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(gethost_api_function)
{
struct sockaddr_in sa;
struct hostent *he;
const char *ip;
char buf[50] = "";
if (!zstr(cmd)) {
he = gethostbyname(cmd);
if (he) {
memcpy(&sa.sin_addr, he->h_addr, sizeof(struct in_addr));
ip = switch_inet_ntop(AF_INET, &sa.sin_addr, buf, sizeof(buf));
stream->write_function(stream, "%s", ip);
return SWITCH_STATUS_SUCCESS;
}
}
stream->write_function(stream, "-ERR");
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(shutdown_function)
{
switch_session_ctl_t command = SCSC_SHUTDOWN;
int arg = 0;
stream->write_function(stream, "+OK\n");
switch_core_session_ctl(command, &arg);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(version_function)
{
int argc;
char *mydata = NULL, *argv[2];
if (zstr(cmd)) {
stream->write_function(stream, "FreeSWITCH Version %s (%s)\n", switch_version_full(), switch_version_revision_human());
goto end;
}
mydata = strdup(cmd);
switch_assert(mydata);
argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
if (argc > 0 && switch_stristr("short", argv[0])) {
stream->write_function(stream, "%s.%s.%s\n", switch_version_major(),switch_version_minor(),switch_version_micro());
} else {
stream->write_function(stream, "FreeSWITCH Version %s (%s)\n", switch_version_full(), switch_version_full_human());
}
switch_safe_free(mydata);
end:
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(pool_stats_function)
{
switch_core_pool_stats(stream);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(db_cache_function)
{
int argc;
char *mydata = NULL, *argv[2];
if (zstr(cmd)) {
goto error;
}
mydata = strdup(cmd);
switch_assert(mydata);
argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
if (argc < 1) {
goto error;
}
if (argv[0] && switch_stristr("status", argv[0])) {
switch_cache_db_status(stream);
goto ok;
} else {
goto error;
}
error:
stream->write_function(stream, "%s", "parameter missing\n");
ok:
switch_safe_free(mydata);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(host_lookup_function)
{
char host[256] = "";
if (zstr(cmd)) {
stream->write_function(stream, "%s", "parameter missing\n");
} else {
if (switch_resolve_host(cmd, host, sizeof(host)) == SWITCH_STATUS_SUCCESS) {
stream->write_function(stream, "%s", host);
} else {
stream->write_function(stream, "%s", "!err!");
}
}
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(nat_map_function)
{
int argc;
char *mydata = NULL, *argv[5];
switch_nat_ip_proto_t proto = SWITCH_NAT_UDP;
switch_port_t external_port = 0;
char *tmp = NULL;
switch_bool_t sticky = SWITCH_FALSE;
switch_bool_t mapping = SWITCH_TRUE;
if (!cmd) {
goto usage;
}
if (!switch_nat_is_initialized()) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "nat_map API called while NAT not initialized\n");
goto error;
}
mydata = strdup(cmd);
switch_assert(mydata);
argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
if (argc < 1) {
goto usage;
}
if (argv[0] && switch_stristr("status", argv[0])) {
tmp = switch_nat_status();
stream->write_function(stream, tmp);
switch_safe_free(tmp);
goto ok;
} else if (argv[0] && switch_stristr("republish", argv[0])) {
switch_nat_republish();
stream->write_function(stream, "true");
goto ok;
} else if (argv[0] && switch_stristr("reinit", argv[0])) {
switch_nat_reinit();
tmp = switch_nat_status();
stream->write_function(stream, tmp);
switch_safe_free(tmp);
goto ok;
}
if (argc < 2) {
goto usage;
}
if (argv[0] && switch_stristr("mapping", argv[0])) {
if (argv[1] && switch_stristr("enable", argv[1])) {
mapping = SWITCH_TRUE;
} else if (argv[1] && switch_stristr("disable", argv[1])) {
mapping = SWITCH_FALSE;
}
switch_nat_set_mapping(mapping);
tmp = switch_nat_status();
stream->write_function(stream, tmp);
switch_safe_free(tmp);
goto ok;
}
if (argc < 3) {
goto error;
}
if (argv[2] && switch_stristr("tcp", argv[2])) {
proto = SWITCH_NAT_TCP;
} else if (argv[2] && switch_stristr("udp", argv[2])) {
proto = SWITCH_NAT_UDP;
}
if (argv[3] && switch_stristr("sticky", argv[3])) {
sticky = SWITCH_TRUE;
}
if (argv[0] && switch_stristr("add", argv[0])) {
if (switch_nat_add_mapping((switch_port_t) atoi(argv[1]), proto, &external_port, sticky) == SWITCH_STATUS_SUCCESS) {
stream->write_function(stream, "true"); /* still return true */
goto ok;
}
} else if (argv[0] && switch_stristr("del", argv[0])) {
if (switch_nat_del_mapping((switch_port_t) atoi(argv[1]), proto) == SWITCH_STATUS_SUCCESS) {
stream->write_function(stream, "true");
goto ok;
}
}
error:
stream->write_function(stream, "false");
goto ok;
usage:
stream->write_function(stream, "USAGE: nat_map [status|reinit|republish] | [add|del] <port> [tcp|udp] [sticky] | [mapping] <enable|disable>");
ok:
switch_safe_free(mydata);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(time_test_function)
{
switch_time_t now, then;
int x;
long mss;
uint32_t total = 0;
int diff;
int max = 10, a = 0;
char *p;
if (zstr(cmd)) {
stream->write_function(stream, "parameter missing\n");
return SWITCH_STATUS_SUCCESS;
}
mss = atol(cmd);
if (mss > 1000000) {
mss = 1000000;
}
if ((p = strchr(cmd, ' '))) {
if ((a = atoi(p + 1)) > 0) {
max = a;
if (max > 100) {
max = 100;
}
}
}
for (x = 1; x <= max; x++) {
then = switch_time_ref();
switch_yield(mss);
now = switch_time_ref();
diff = (int) (now - then);
stream->write_function(stream, "test %d sleep %ld %d\n", x, mss, diff);
total += diff;
}
stream->write_function(stream, "avg %d\n", total / (x - 1));
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(msleep_function)
{
if (cmd) {
long ms = atol(cmd);
switch_yield(ms * 1000);
}
stream->write_function(stream, "+OK");
return SWITCH_STATUS_SUCCESS;
}
#define TIMER_TEST_SYNTAX "<10|20|40|60|120> [<1..200>] [<timer_name>]"
SWITCH_STANDARD_API(timer_test_function)
{
switch_time_t now, then, start, end;
int x;
int mss = 20;
uint32_t total = 0;
int diff;
int max = 50;
switch_timer_t timer = { 0 };
int argc = 0;
char *argv[5] = { 0 };
const char *timer_name = "soft";
switch_memory_pool_t *pool;
char *mycmd = NULL;
switch_core_new_memory_pool(&pool);
if (zstr(cmd)) {
mycmd = "";
} else {
mycmd = switch_core_strdup(pool, cmd);
}
argc = switch_split(mycmd, ' ', argv);
if (argc > 0) {
mss = atoi(argv[0]);
}
if (argc > 1) {
int tmp = atoi(argv[1]);
if (tmp > 0 && tmp <= 400) {
max = tmp;
}
}
if (argc > 2) {
timer_name = argv[2];
}
if (mss != 10 && mss != 20 && mss != 30 && mss != 32 && mss != 40 && mss != 60 && mss != 120) {
stream->write_function(stream, "parameter missing: %s\n", TIMER_TEST_SYNTAX);
goto end;
}
if (switch_core_timer_init(&timer, timer_name, mss, 1, pool) != SWITCH_STATUS_SUCCESS) {
stream->write_function(stream, "Timer Error!\n");
goto end;
}
switch_core_timer_next(&timer); /* Step timer once before testing results below, to get first timestamp as accurate as possible */
start = then = switch_time_ref();
for (x = 1; x <= max; x++) {
switch_core_timer_next(&timer);
now = switch_time_ref();
diff = (int) (now - then);
total += diff;
then = now;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Timer Test: %d sleep %d %d\n", x, mss, diff);
}
end = then;
switch_yield(250000);
stream->write_function(stream, "Avg: %0.3fms Total Time: %0.3fms\n", (float) ((float) (total / (x - 1)) / 1000),
(float) ((float) (end - start) / 1000));
if (switch_core_timer_destroy(&timer) != SWITCH_STATUS_SUCCESS) {
stream->write_function(stream, "Timer Destroy Error!\n");
}
end:
switch_core_destroy_memory_pool(&pool);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(group_call_function)
{
char *domain, *dup_domain = NULL;
char *group_name = NULL;
char *flags;
int ok = 0;
switch_channel_t *channel = NULL;
char *fp = NULL;
const char *call_delim = ",";
if (zstr(cmd)) {
goto end;
}
if (session) {
channel = switch_core_session_get_channel(session);
}
group_name = strdup(cmd);