forked from asterisk/asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchan_gtalk.c
2438 lines (2184 loc) · 75.2 KB
/
chan_gtalk.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
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2005, Digium, Inc.
*
* Matt O'Gorman <[email protected]>
* Philippe Sultan <[email protected]>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \author Matt O'Gorman <[email protected]>
* \author Philippe Sultan <[email protected]>
*
* \brief Gtalk Channel Driver, until google/libjingle works with jingle spec
*
* \ingroup channel_drivers
*
* ********** General TODO:s
* \todo Support config reloading.
* \todo Fix native bridging.
*/
/*! \li \ref chan_gtalk.c uses the configuration file \ref gtalk.conf
* \addtogroup configuration_file
*/
/*! \page gtalk.conf gtalk.conf
* \verbinclude gtalk.conf.sample
*/
/*** MODULEINFO
<defaultenabled>no</defaultenabled>
<depend>iksemel</depend>
<depend>res_jabber</depend>
<use type="external">openssl</use>
<support_level>deprecated</support_level>
<replacement>chan_motif</replacement>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <sys/socket.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/signal.h>
#include <iksemel.h>
#include <pthread.h>
#include <ctype.h>
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/sched.h"
#include "asterisk/io.h"
#include "asterisk/rtp_engine.h"
#include "asterisk/stun.h"
#include "asterisk/acl.h"
#include "asterisk/callerid.h"
#include "asterisk/file.h"
#include "asterisk/cli.h"
#include "asterisk/app.h"
#include "asterisk/musiconhold.h"
#include "asterisk/manager.h"
#include "asterisk/stringfields.h"
#include "asterisk/utils.h"
#include "asterisk/causes.h"
#include "asterisk/astobj.h"
#include "asterisk/abstract_jb.h"
#include "asterisk/jabber.h"
#include "asterisk/jingle.h"
#include "asterisk/features.h"
#include "asterisk/stasis_channels.h"
#define GOOGLE_CONFIG "gtalk.conf"
/*! Global jitterbuffer configuration - by default, jb is disabled */
static struct ast_jb_conf default_jbconf =
{
.flags = 0,
.max_size = -1,
.resync_threshold = -1,
.impl = "",
.target_extra = -1,
};
static struct ast_jb_conf global_jbconf;
enum gtalk_protocol {
AJI_PROTOCOL_UDP = 1,
AJI_PROTOCOL_SSLTCP = 2,
};
enum gtalk_connect_type {
AJI_CONNECT_STUN = 1,
AJI_CONNECT_LOCAL = 2,
AJI_CONNECT_RELAY = 3,
};
struct gtalk_pvt {
ast_mutex_t lock; /*!< Channel private lock */
time_t laststun;
struct gtalk *parent; /*!< Parent client */
char sid[100];
char us[AJI_MAX_JIDLEN];
char them[AJI_MAX_JIDLEN];
char ring[10]; /*!< Message ID of ring */
iksrule *ringrule; /*!< Rule for matching RING request */
int initiator; /*!< If we're the initiator */
int alreadygone;
struct ast_codec_pref prefs;
struct gtalk_candidate *theircandidates;
struct gtalk_candidate *ourcandidates;
char cid_num[80]; /*!< Caller ID num */
char cid_name[80]; /*!< Caller ID name */
char exten[80]; /*!< Called extension */
struct ast_channel *owner; /*!< Master Channel */
struct ast_rtp_instance *rtp; /*!< RTP audio session */
struct ast_rtp_instance *vrtp; /*!< RTP video session */
struct ast_format_cap *cap;
struct ast_format_cap *jointcap; /*!< Supported capability at both ends (codecs ) */
struct ast_format_cap *peercap;
struct gtalk_pvt *next; /* Next entity */
};
struct gtalk_candidate {
char name[100];
enum gtalk_protocol protocol;
double preference;
char username[100];
char password[100];
enum gtalk_connect_type type;
char network[6];
int generation;
char ip[16];
int port;
int receipt;
struct gtalk_candidate *next;
};
struct gtalk {
ASTOBJ_COMPONENTS(struct gtalk);
struct aji_client *connection;
struct aji_buddy *buddy;
struct gtalk_pvt *p;
struct ast_codec_pref prefs;
int amaflags; /*!< AMA Flags */
char user[AJI_MAX_JIDLEN];
char context[AST_MAX_CONTEXT];
char parkinglot[AST_MAX_CONTEXT]; /*!< Parkinglot */
char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
struct ast_format_cap *cap;
ast_group_t callgroup; /*!< Call group */
ast_group_t pickupgroup; /*!< Pickup group */
int callingpres; /*!< Calling presentation */
int allowguest;
char language[MAX_LANGUAGE]; /*!< Default language for prompts */
char musicclass[MAX_MUSICCLASS]; /*!< Music on Hold class */
};
struct gtalk_container {
ASTOBJ_CONTAINER_COMPONENTS(struct gtalk);
};
static const char desc[] = "Gtalk Channel";
static const char DEFAULT_CONTEXT[] = "default";
static const int DEFAULT_ALLOWGUEST = 1;
static struct ast_format_cap *global_capability;
AST_MUTEX_DEFINE_STATIC(gtalklock); /*!< Protect the interface list (of gtalk_pvt's) */
/* Forward declarations */
static struct ast_channel *gtalk_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause);
/*static int gtalk_digit(struct ast_channel *ast, char digit, unsigned int duration);*/
static int gtalk_sendtext(struct ast_channel *ast, const char *text);
static int gtalk_digit_begin(struct ast_channel *ast, char digit);
static int gtalk_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
static int gtalk_call(struct ast_channel *ast, const char *dest, int timeout);
static int gtalk_hangup(struct ast_channel *ast);
static int gtalk_answer(struct ast_channel *ast);
static int gtalk_action(struct gtalk *client, struct gtalk_pvt *p, const char *action);
static void gtalk_free_pvt(struct gtalk *client, struct gtalk_pvt *p);
static int gtalk_newcall(struct gtalk *client, ikspak *pak);
static struct ast_frame *gtalk_read(struct ast_channel *ast);
static int gtalk_write(struct ast_channel *ast, struct ast_frame *f);
static int gtalk_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
static int gtalk_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
static int gtalk_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const char *them, const char *sid);
static int gtalk_update_stun(struct gtalk *client, struct gtalk_pvt *p);
/* static char *gtalk_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a); */
static char *gtalk_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
static char *gtalk_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
static int gtalk_update_externip(void);
static int gtalk_parser(void *data, ikspak *pak);
static int gtalk_create_candidates(struct gtalk *client, struct gtalk_pvt *p, char *sid, char *from, char *to);
static void gtalk_set_owner(struct gtalk_pvt *p, struct ast_channel *chan);
/*! \brief PBX interface structure for channel registration */
static struct ast_channel_tech gtalk_tech = {
.type = "Gtalk",
.description = "Gtalk Channel Driver",
.requester = gtalk_request,
.send_text = gtalk_sendtext,
.send_digit_begin = gtalk_digit_begin,
.send_digit_end = gtalk_digit_end,
/* XXX TODO native bridging is causing odd problems with DTMF pass-through with
* the gtalk servers. Enable native bridging once the source of this problem has
* been identified.
.bridge = ast_rtp_instance_bridge, */
.call = gtalk_call,
.hangup = gtalk_hangup,
.answer = gtalk_answer,
.read = gtalk_read,
.write = gtalk_write,
.exception = gtalk_read,
.indicate = gtalk_indicate,
.fixup = gtalk_fixup,
.send_html = gtalk_sendhtml,
.properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER
};
static struct sockaddr_in bindaddr = { 0, }; /*!< The address we bind to */
static struct ast_sched_context *sched; /*!< The scheduling context */
static struct io_context *io; /*!< The IO context */
static struct in_addr __ourip;
static struct ast_cli_entry gtalk_cli[] = {
/* AST_CLI_DEFINE(gtalk_do_reload, "Reload GoogleTalk configuration"), XXX TODO reloads are not possible yet. */
AST_CLI_DEFINE(gtalk_show_channels, "Show GoogleTalk channels"),
AST_CLI_DEFINE(gtalk_show_settings, "Show GoogleTalk global settings"),
};
static char externip[16];
static char global_context[AST_MAX_CONTEXT];
static char global_parkinglot[AST_MAX_CONTEXT];
static int global_allowguest;
static struct sockaddr_in stunaddr; /*!< the stun server we get the externip from */
static int global_stunaddr;
static struct gtalk_container gtalk_list;
static void gtalk_member_destroy(struct gtalk *obj)
{
obj->cap = ast_format_cap_destroy(obj->cap);
if (obj->connection) {
ASTOBJ_UNREF(obj->connection, ast_aji_client_destroy);
}
if (obj->buddy) {
ASTOBJ_UNREF(obj->buddy, ast_aji_buddy_destroy);
}
ast_free(obj);
}
/* XXX This could be a source of reference leaks given that the CONTAINER_FIND
* macros bump the refcount while the traversal does not. */
static struct gtalk *find_gtalk(char *name, char *connection)
{
struct gtalk *gtalk = NULL;
char *domain = NULL , *s = NULL;
if (strchr(connection, '@')) {
s = ast_strdupa(connection);
domain = strsep(&s, "@");
ast_verbose("OOOOH domain = %s\n", domain);
}
gtalk = ASTOBJ_CONTAINER_FIND(>alk_list, name);
if (!gtalk && strchr(name, '@'))
gtalk = ASTOBJ_CONTAINER_FIND_FULL(>alk_list, name, user,,, strcasecmp);
if (!gtalk) {
/* guest call */
ASTOBJ_CONTAINER_TRAVERSE(>alk_list, 1, {
ASTOBJ_RDLOCK(iterator);
if (!strcasecmp(iterator->name, "guest")) {
gtalk = iterator;
}
ASTOBJ_UNLOCK(iterator);
if (gtalk)
break;
});
}
return gtalk;
}
static int add_codec_to_answer(const struct gtalk_pvt *p, struct ast_format *codec, iks *dcodecs)
{
int res = 0;
const char *format = ast_getformatname(codec);
if (!strcasecmp("ulaw", format)) {
iks *payload_eg711u, *payload_pcmu;
payload_pcmu = iks_new("payload-type");
payload_eg711u = iks_new("payload-type");
if(!payload_eg711u || !payload_pcmu) {
iks_delete(payload_pcmu);
iks_delete(payload_eg711u);
ast_log(LOG_WARNING,"Failed to allocate iks node\n");
return -1;
}
iks_insert_attrib(payload_pcmu, "id", "0");
iks_insert_attrib(payload_pcmu, "name", "PCMU");
iks_insert_attrib(payload_pcmu, "clockrate","8000");
iks_insert_attrib(payload_pcmu, "bitrate","64000");
iks_insert_attrib(payload_eg711u, "id", "100");
iks_insert_attrib(payload_eg711u, "name", "EG711U");
iks_insert_attrib(payload_eg711u, "clockrate","8000");
iks_insert_attrib(payload_eg711u, "bitrate","64000");
iks_insert_node(dcodecs, payload_pcmu);
iks_insert_node(dcodecs, payload_eg711u);
res ++;
}
if (!strcasecmp("alaw", format)) {
iks *payload_eg711a, *payload_pcma;
payload_pcma = iks_new("payload-type");
payload_eg711a = iks_new("payload-type");
if(!payload_eg711a || !payload_pcma) {
iks_delete(payload_eg711a);
iks_delete(payload_pcma);
ast_log(LOG_WARNING,"Failed to allocate iks node\n");
return -1;
}
iks_insert_attrib(payload_pcma, "id", "8");
iks_insert_attrib(payload_pcma, "name", "PCMA");
iks_insert_attrib(payload_pcma, "clockrate","8000");
iks_insert_attrib(payload_pcma, "bitrate","64000");
payload_eg711a = iks_new("payload-type");
iks_insert_attrib(payload_eg711a, "id", "101");
iks_insert_attrib(payload_eg711a, "name", "EG711A");
iks_insert_attrib(payload_eg711a, "clockrate","8000");
iks_insert_attrib(payload_eg711a, "bitrate","64000");
iks_insert_node(dcodecs, payload_pcma);
iks_insert_node(dcodecs, payload_eg711a);
res ++;
}
if (!strcasecmp("ilbc", format)) {
iks *payload_ilbc = iks_new("payload-type");
if(!payload_ilbc) {
ast_log(LOG_WARNING,"Failed to allocate iks node\n");
return -1;
}
iks_insert_attrib(payload_ilbc, "id", "97");
iks_insert_attrib(payload_ilbc, "name", "iLBC");
iks_insert_attrib(payload_ilbc, "clockrate","8000");
iks_insert_attrib(payload_ilbc, "bitrate","13300");
iks_insert_node(dcodecs, payload_ilbc);
res ++;
}
if (!strcasecmp("g723", format)) {
iks *payload_g723 = iks_new("payload-type");
if(!payload_g723) {
ast_log(LOG_WARNING,"Failed to allocate iks node\n");
return -1;
}
iks_insert_attrib(payload_g723, "id", "4");
iks_insert_attrib(payload_g723, "name", "G723");
iks_insert_attrib(payload_g723, "clockrate","8000");
iks_insert_attrib(payload_g723, "bitrate","6300");
iks_insert_node(dcodecs, payload_g723);
res ++;
}
if (!strcasecmp("speex", format)) {
iks *payload_speex = iks_new("payload-type");
if(!payload_speex) {
ast_log(LOG_WARNING,"Failed to allocate iks node\n");
return -1;
}
iks_insert_attrib(payload_speex, "id", "110");
iks_insert_attrib(payload_speex, "name", "speex");
iks_insert_attrib(payload_speex, "clockrate","8000");
iks_insert_attrib(payload_speex, "bitrate","11000");
iks_insert_node(dcodecs, payload_speex);
res++;
}
if (!strcasecmp("gsm", format)) {
iks *payload_gsm = iks_new("payload-type");
if(!payload_gsm) {
ast_log(LOG_WARNING,"Failed to allocate iks node\n");
return -1;
}
iks_insert_attrib(payload_gsm, "id", "103");
iks_insert_attrib(payload_gsm, "name", "gsm");
iks_insert_node(dcodecs, payload_gsm);
res++;
}
return res;
}
static int gtalk_invite(struct gtalk_pvt *p, char *to, char *from, char *sid, int initiator)
{
struct gtalk *client = p->parent;
iks *iq, *gtalk, *dcodecs, *payload_telephone, *transport;
int x;
struct ast_format_cap *alreadysent;
int codecs_num = 0;
char *lowerto = NULL;
struct ast_format tmpfmt;
iq = iks_new("iq");
gtalk = iks_new("session");
dcodecs = iks_new("description");
transport = iks_new("transport");
payload_telephone = iks_new("payload-type");
if (!(iq && gtalk && dcodecs && transport && payload_telephone)) {
iks_delete(iq);
iks_delete(gtalk);
iks_delete(dcodecs);
iks_delete(transport);
iks_delete(payload_telephone);
ast_log(LOG_ERROR, "Could not allocate iksemel nodes\n");
return 0;
}
iks_insert_attrib(dcodecs, "xmlns", GOOGLE_AUDIO_NS);
iks_insert_attrib(dcodecs, "xml:lang", "en");
if (!(alreadysent = ast_format_cap_alloc_nolock())) {
return 0;
}
for (x = 0; x < AST_CODEC_PREF_SIZE; x++) {
if (!(ast_codec_pref_index(&client->prefs, x, &tmpfmt))) {
break;
}
if (!(ast_format_cap_iscompatible(client->cap, &tmpfmt))) {
continue;
}
if (ast_format_cap_iscompatible(alreadysent, &tmpfmt)) {
continue;
}
codecs_num = add_codec_to_answer(p, &tmpfmt, dcodecs);
ast_format_cap_add(alreadysent, &tmpfmt);
}
alreadysent = ast_format_cap_destroy(alreadysent);
if (codecs_num) {
/* only propose DTMF within an audio session */
iks_insert_attrib(payload_telephone, "id", "101");
iks_insert_attrib(payload_telephone, "name", "telephone-event");
iks_insert_attrib(payload_telephone, "clockrate", "8000");
}
iks_insert_attrib(transport,"xmlns",GOOGLE_TRANSPORT_NS);
iks_insert_attrib(iq, "type", "set");
iks_insert_attrib(iq, "to", to);
iks_insert_attrib(iq, "from", from);
iks_insert_attrib(iq, "id", client->connection->mid);
ast_aji_increment_mid(client->connection->mid);
iks_insert_attrib(gtalk, "xmlns", GOOGLE_NS);
iks_insert_attrib(gtalk, "type",initiator ? "initiate": "accept");
/* put the initiator attribute to lower case if we receive the call
* otherwise GoogleTalk won't establish the session */
if (!initiator) {
char c;
char *t = lowerto = ast_strdupa(to);
while (((c = *t) != '/') && (*t++ = tolower(c)));
}
iks_insert_attrib(gtalk, "initiator", initiator ? from : lowerto);
iks_insert_attrib(gtalk, "id", sid);
iks_insert_node(iq, gtalk);
iks_insert_node(gtalk, dcodecs);
iks_insert_node(dcodecs, payload_telephone);
ast_aji_send(client->connection, iq);
iks_delete(payload_telephone);
iks_delete(transport);
iks_delete(dcodecs);
iks_delete(gtalk);
iks_delete(iq);
return 1;
}
static int gtalk_ringing_ack(void *data, ikspak *pak)
{
struct gtalk_pvt *p = data;
struct ast_channel *owner;
ast_mutex_lock(&p->lock);
if (p->ringrule) {
iks_filter_remove_rule(p->parent->connection->f, p->ringrule);
}
p->ringrule = NULL;
/* this may be a redirect */
if (!strcmp(S_OR(iks_find_attrib(pak->x, "type"), ""), "error")) {
char *name = NULL;
char *redirect = NULL;
iks *traversenodes = NULL;
traversenodes = pak->query;
while (traversenodes) {
if (!(name = iks_name(traversenodes))) {
break;
}
if (!strcasecmp(name, "error") &&
((redirect = iks_find_cdata(traversenodes, "redirect")) ||
(redirect = iks_find_cdata(traversenodes, "sta:redirect"))) &&
(redirect = strstr(redirect, "xmpp:"))) {
redirect += 5;
ast_debug(1, "redirect %s\n", redirect);
ast_copy_string(p->them, redirect, sizeof(p->them));
gtalk_invite(p, p->them, p->us, p->sid, 1);
break;
}
traversenodes = iks_next_tag(traversenodes);
}
}
gtalk_create_candidates(p->parent, p, p->sid, p->them, p->us);
owner = p->owner;
ast_mutex_unlock(&p->lock);
if (owner) {
ast_queue_control(owner, AST_CONTROL_RINGING);
}
return IKS_FILTER_EAT;
}
static int gtalk_answer(struct ast_channel *ast)
{
struct gtalk_pvt *p = ast_channel_tech_pvt(ast);
int res = 0;
ast_debug(1, "Answer!\n");
ast_mutex_lock(&p->lock);
gtalk_invite(p, p->them, p->us,p->sid, 0);
ast_mutex_unlock(&p->lock);
return res;
}
static enum ast_rtp_glue_result gtalk_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
{
struct gtalk_pvt *p = ast_channel_tech_pvt(chan);
enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_FORBID;
if (!p)
return res;
ast_mutex_lock(&p->lock);
if (p->rtp){
ao2_ref(p->rtp, +1);
*instance = p->rtp;
res = AST_RTP_GLUE_RESULT_LOCAL;
}
ast_mutex_unlock(&p->lock);
return res;
}
static void gtalk_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
{
struct gtalk_pvt *p = ast_channel_tech_pvt(chan);
ast_mutex_lock(&p->lock);
ast_format_cap_copy(result, p->peercap);
ast_mutex_unlock(&p->lock);
}
static int gtalk_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, const struct ast_format_cap *cap, int nat_active)
{
struct gtalk_pvt *p;
p = ast_channel_tech_pvt(chan);
if (!p)
return -1;
ast_mutex_lock(&p->lock);
/* if (rtp)
ast_rtp_get_peer(rtp, &p->redirip);
else
memset(&p->redirip, 0, sizeof(p->redirip));
p->redircodecs = codecs; */
/* Reset lastrtprx timer */
ast_mutex_unlock(&p->lock);
return 0;
}
static struct ast_rtp_glue gtalk_rtp_glue = {
.type = "Gtalk",
.get_rtp_info = gtalk_get_rtp_peer,
.get_codec = gtalk_get_codec,
.update_peer = gtalk_set_rtp_peer,
};
static int gtalk_response(struct gtalk *client, char *from, ikspak *pak, const char *reasonstr, const char *reasonstr2)
{
iks *response = NULL, *error = NULL, *reason = NULL;
int res = -1;
response = iks_new("iq");
if (response) {
iks_insert_attrib(response, "type", "result");
iks_insert_attrib(response, "from", from);
iks_insert_attrib(response, "to", S_OR(iks_find_attrib(pak->x, "from"), ""));
iks_insert_attrib(response, "id", S_OR(iks_find_attrib(pak->x, "id"), ""));
if (reasonstr) {
error = iks_new("error");
if (error) {
iks_insert_attrib(error, "type", "cancel");
reason = iks_new(reasonstr);
if (reason)
iks_insert_node(error, reason);
iks_insert_node(response, error);
}
}
ast_aji_send(client->connection, response);
res = 0;
}
iks_delete(reason);
iks_delete(error);
iks_delete(response);
return res;
}
static int gtalk_is_answered(struct gtalk *client, ikspak *pak)
{
struct gtalk_pvt *tmp = NULL;
char *from;
iks *codec;
char s1[BUFSIZ], s2[BUFSIZ], s3[BUFSIZ];
int peernoncodeccapability;
ast_debug(1, "The client is %s\n", client->name);
/* Make sure our new call does exist */
for (tmp = client->p; tmp; tmp = tmp->next) {
if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) {
break;
} else if (iks_find_with_attrib(pak->x, "ses:session", "id", tmp->sid)) {
break;
}
}
if (!tmp) {
ast_log(LOG_WARNING, "Could not find session in iq\n");
return -1;
}
/* codec points to the first <payload-type/> tag */
codec = iks_first_tag(iks_first_tag(iks_first_tag(pak->x)));
while (codec) {
char *codec_id = iks_find_attrib(codec, "id");
char *codec_name = iks_find_attrib(codec, "name");
if (!codec_id || !codec_name) {
codec = iks_next_tag(codec);
continue;
}
ast_rtp_codecs_payloads_set_m_type(
ast_rtp_instance_get_codecs(tmp->rtp),
tmp->rtp,
atoi(codec_id));
ast_rtp_codecs_payloads_set_rtpmap_type(
ast_rtp_instance_get_codecs(tmp->rtp),
tmp->rtp,
atoi(codec_id),
"audio",
codec_name,
0);
codec = iks_next_tag(codec);
}
/* Now gather all of the codecs that we are asked for */
ast_rtp_codecs_payload_formats(ast_rtp_instance_get_codecs(tmp->rtp), tmp->peercap, &peernoncodeccapability);
/* at this point, we received an answer from the remote Gtalk client,
which allows us to compare capabilities */
ast_format_cap_joint_copy(tmp->cap, tmp->peercap, tmp->jointcap);
if (ast_format_cap_is_empty(tmp->jointcap)) {
ast_log(LOG_WARNING, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1, BUFSIZ, tmp->cap),
ast_getformatname_multiple(s2, BUFSIZ, tmp->peercap),
ast_getformatname_multiple(s3, BUFSIZ, tmp->jointcap));
/* close session if capabilities don't match */
ast_queue_hangup(tmp->owner);
return -1;
}
from = iks_find_attrib(pak->x, "to");
if (!from) {
from = client->connection->jid->full;
}
if (tmp->owner) {
ast_queue_control(tmp->owner, AST_CONTROL_ANSWER);
}
gtalk_update_stun(tmp->parent, tmp);
gtalk_response(client, from, pak, NULL, NULL);
return 1;
}
static int gtalk_is_accepted(struct gtalk *client, ikspak *pak)
{
struct gtalk_pvt *tmp;
char *from;
ast_debug(1, "The client is %s\n", client->name);
/* find corresponding call */
for (tmp = client->p; tmp; tmp = tmp->next) {
if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) {
break;
}
}
from = iks_find_attrib(pak->x, "to");
if (!from) {
from = client->connection->jid->full;
}
if (tmp) {
gtalk_update_stun(tmp->parent, tmp);
} else {
ast_log(LOG_NOTICE, "Whoa, didn't find call during accept?!\n");
}
/* answer 'iq' packet to let the remote peer know that we're alive */
gtalk_response(client, from, pak, NULL, NULL);
return 1;
}
static int gtalk_handle_dtmf(struct gtalk *client, ikspak *pak)
{
struct gtalk_pvt *tmp;
iks *dtmfnode = NULL, *dtmfchild = NULL;
char *dtmf;
char *from;
/* Make sure our new call doesn't exist yet */
for (tmp = client->p; tmp; tmp = tmp->next) {
if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid) || iks_find_with_attrib(pak->x, "gtalk", "sid", tmp->sid))
break;
}
from = iks_find_attrib(pak->x, "to");
if (!from) {
from = client->connection->jid->full;
}
if (tmp) {
if(iks_find_with_attrib(pak->x, "dtmf-method", "method", "rtp")) {
gtalk_response(client, from, pak,
"feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
"unsupported-dtmf-method xmlns='http://jabber.org/protocol/gtalk/info/dtmf#errors'");
return -1;
}
if ((dtmfnode = iks_find(pak->x, "dtmf"))) {
if((dtmf = iks_find_attrib(dtmfnode, "code"))) {
if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-up")) {
struct ast_frame f = {AST_FRAME_DTMF_BEGIN, };
f.subclass.integer = dtmf[0];
ast_queue_frame(tmp->owner, &f);
ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
} else if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-down")) {
struct ast_frame f = {AST_FRAME_DTMF_END, };
f.subclass.integer = dtmf[0];
ast_queue_frame(tmp->owner, &f);
ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
} else if(iks_find_attrib(pak->x, "dtmf")) { /* 250 millasecond default */
struct ast_frame f = {AST_FRAME_DTMF, };
f.subclass.integer = dtmf[0];
ast_queue_frame(tmp->owner, &f);
ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
}
}
} else if ((dtmfnode = iks_find_with_attrib(pak->x, "gtalk", "action", "session-info"))) {
if((dtmfchild = iks_find(dtmfnode, "dtmf"))) {
if((dtmf = iks_find_attrib(dtmfchild, "code"))) {
if(iks_find_with_attrib(dtmfnode, "dtmf", "action", "button-up")) {
struct ast_frame f = {AST_FRAME_DTMF_END, };
f.subclass.integer = dtmf[0];
ast_queue_frame(tmp->owner, &f);
ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
} else if(iks_find_with_attrib(dtmfnode, "dtmf", "action", "button-down")) {
struct ast_frame f = {AST_FRAME_DTMF_BEGIN, };
f.subclass.integer = dtmf[0];
ast_queue_frame(tmp->owner, &f);
ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
}
}
}
}
gtalk_response(client, from, pak, NULL, NULL);
return 1;
} else {
ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
}
gtalk_response(client, from, pak, NULL, NULL);
return 1;
}
static int gtalk_hangup_farend(struct gtalk *client, ikspak *pak)
{
struct gtalk_pvt *tmp;
char *from;
ast_debug(1, "The client is %s\n", client->name);
/* Make sure our new call doesn't exist yet */
for (tmp = client->p; tmp; tmp = tmp->next) {
if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid) ||
(iks_find_attrib(pak->query, "id") && !strcmp(iks_find_attrib(pak->query, "id"), tmp->sid))) {
break;
}
}
from = iks_find_attrib(pak->x, "to");
if (!from) {
from = client->connection->jid->full;
}
if (tmp) {
tmp->alreadygone = 1;
if (tmp->owner) {
ast_queue_hangup(tmp->owner);
}
} else {
ast_log(LOG_NOTICE, "Whoa, didn't find call during hangup!\n");
}
gtalk_response(client, from, pak, NULL, NULL);
return 1;
}
static int gtalk_get_local_ip(struct ast_sockaddr *ourip)
{
struct ast_sockaddr root;
struct ast_sockaddr bindaddr_tmp;
struct ast_sockaddr *addrs;
/* If bind address is not 0.0.0.0, then bindaddr is our local ip. */
ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
if (!ast_sockaddr_is_any(&bindaddr_tmp)) {
ast_sockaddr_copy(ourip, &bindaddr_tmp);
return 0;
}
/* If no bind address was provided, lets see what ip we would use to connect to google.com and use that.
* If you can't resolve google.com from your network, then this module is useless for you anyway. */
if (ast_sockaddr_resolve(&addrs, "google.com", PARSE_PORT_FORBID, AF_INET) > 0) {
ast_sockaddr_copy(&root, &addrs[0]);
ast_free(addrs);
if (!ast_ouraddrfor(&root, ourip)) {
return 0;
}
}
/* As a last resort, use this function to find our local address. */
return ast_find_ourip(ourip, &bindaddr_tmp, AF_INET);
}
static int gtalk_create_candidates(struct gtalk *client, struct gtalk_pvt *p, char *sid, char *from, char *to)
{
struct gtalk_candidate *tmp;
struct aji_client *c = client->connection;
struct gtalk_candidate *ours1 = NULL, *ours2 = NULL;
struct sockaddr_in sin = { 0, };
struct ast_sockaddr sin_tmp;
struct ast_sockaddr us;
iks *iq, *gtalk, *candidate, *transport;
char user[17], pass[17], preference[5], port[7];
char *lowerfrom = NULL;
iq = iks_new("iq");
gtalk = iks_new("session");
candidate = iks_new("candidate");
transport = iks_new("transport");
if (!iq || !gtalk || !candidate || !transport) {
ast_log(LOG_ERROR, "Memory allocation error\n");
goto safeout;
}
ours1 = ast_calloc(1, sizeof(*ours1));
ours2 = ast_calloc(1, sizeof(*ours2));
if (!ours1 || !ours2)
goto safeout;
iks_insert_attrib(transport, "xmlns",GOOGLE_TRANSPORT_NS);
iks_insert_node(iq, gtalk);
iks_insert_node(gtalk,candidate);
iks_insert_node(gtalk,transport);
for (; p; p = p->next) {
if (!strcasecmp(p->sid, sid))
break;
}
if (!p) {
ast_log(LOG_NOTICE, "No matching gtalk session - SID %s!\n", sid);
goto safeout;
}
ast_rtp_instance_get_local_address(p->rtp, &sin_tmp);
ast_sockaddr_to_sin(&sin_tmp, &sin);
gtalk_get_local_ip(&us);
if (!strcmp(ast_sockaddr_stringify_addr(&us), "127.0.0.1")) {
ast_log(LOG_WARNING, "Found a loopback IP on the system, check your network configuration or set the bindaddr attribute.\n");
}
/* Setup our gtalk candidates */
ast_copy_string(ours1->name, "rtp", sizeof(ours1->name));
ours1->port = ntohs(sin.sin_port);
ours1->preference = 1;
snprintf(user, sizeof(user), "%08lx%08lx", ast_random(), ast_random());
snprintf(pass, sizeof(pass), "%08lx%08lx", ast_random(), ast_random());
ast_copy_string(ours1->username, user, sizeof(ours1->username));
ast_copy_string(ours1->password, pass, sizeof(ours1->password));
ast_copy_string(ours1->ip, ast_sockaddr_stringify_addr(&us),
sizeof(ours1->ip));
ours1->protocol = AJI_PROTOCOL_UDP;
ours1->type = AJI_CONNECT_LOCAL;
ours1->generation = 0;
p->ourcandidates = ours1;
/* XXX this is a blocking action. We send a STUN request to the server
* and wait for the response. If blocking here is a problem the STUN requests/responses
* for the externip may need to be done differently. */
gtalk_update_externip();
if (!ast_strlen_zero(externip)) {
ast_copy_string(ours2->username, user, sizeof(ours2->username));
ast_copy_string(ours2->password, pass, sizeof(ours2->password));
ast_copy_string(ours2->ip, externip, sizeof(ours2->ip));
ast_copy_string(ours2->name, "rtp", sizeof(ours1->name));
ours2->port = ntohs(sin.sin_port);
ours2->preference = 0.9;
ours2->protocol = AJI_PROTOCOL_UDP;
ours2->type = AJI_CONNECT_STUN;
ours2->generation = 0;
ours1->next = ours2;
ours2 = NULL;
}
ours1 = NULL;
for (tmp = p->ourcandidates; tmp; tmp = tmp->next) {
snprintf(port, sizeof(port), "%d", tmp->port);
snprintf(preference, sizeof(preference), "%.2f", tmp->preference);
iks_insert_attrib(iq, "from", to);
iks_insert_attrib(iq, "to", from);
iks_insert_attrib(iq, "type", "set");
iks_insert_attrib(iq, "id", c->mid);
ast_aji_increment_mid(c->mid);
iks_insert_attrib(gtalk, "type", "candidates");
iks_insert_attrib(gtalk, "id", sid);
/* put the initiator attribute to lower case if we receive the call
* otherwise GoogleTalk won't establish the session */
if (!p->initiator) {
char cur;
char *t = lowerfrom = ast_strdupa(from);
while (((cur = *t) != '/') && (*t++ = tolower(cur)));
}
iks_insert_attrib(gtalk, "initiator", (p->initiator) ? to : lowerfrom);
iks_insert_attrib(gtalk, "xmlns", GOOGLE_NS);
iks_insert_attrib(candidate, "name", tmp->name);
iks_insert_attrib(candidate, "address", tmp->ip);
iks_insert_attrib(candidate, "port", port);
iks_insert_attrib(candidate, "username", tmp->username);
iks_insert_attrib(candidate, "password", tmp->password);
iks_insert_attrib(candidate, "preference", preference);
if (tmp->protocol == AJI_PROTOCOL_UDP)
iks_insert_attrib(candidate, "protocol", "udp");
if (tmp->protocol == AJI_PROTOCOL_SSLTCP)
iks_insert_attrib(candidate, "protocol", "ssltcp");
if (tmp->type == AJI_CONNECT_STUN)
iks_insert_attrib(candidate, "type", "stun");
if (tmp->type == AJI_CONNECT_LOCAL)
iks_insert_attrib(candidate, "type", "local");
if (tmp->type == AJI_CONNECT_RELAY)
iks_insert_attrib(candidate, "type", "relay");
iks_insert_attrib(candidate, "network", "0");
iks_insert_attrib(candidate, "generation", "0");
ast_aji_send(c, iq);
}
p->laststun = 0;
safeout:
if (ours1)
ast_free(ours1);