2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Matt O'Gorman <mogorman@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \author Matt O'Gorman <mogorman@digium.com>
23 * \brief Gtalk Channel Driver, until google/libjingle works with jingle spec
25 * \ingroup channel_drivers
29 <depend>iksemel</depend>
30 <depend>res_jabber</depend>
36 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
42 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <sys/signal.h>
55 GCRY_THREAD_OPTION_PTHREAD_IMPL
;
56 #endif /* HAVE_GNUTLS */
58 #include "asterisk/lock.h"
59 #include "asterisk/channel.h"
60 #include "asterisk/config.h"
61 #include "asterisk/logger.h"
62 #include "asterisk/module.h"
63 #include "asterisk/pbx.h"
64 #include "asterisk/options.h"
65 #include "asterisk/lock.h"
66 #include "asterisk/sched.h"
67 #include "asterisk/io.h"
68 #include "asterisk/rtp.h"
69 #include "asterisk/acl.h"
70 #include "asterisk/callerid.h"
71 #include "asterisk/file.h"
72 #include "asterisk/cli.h"
73 #include "asterisk/app.h"
74 #include "asterisk/musiconhold.h"
75 #include "asterisk/manager.h"
76 #include "asterisk/stringfields.h"
77 #include "asterisk/utils.h"
78 #include "asterisk/causes.h"
79 #include "asterisk/astobj.h"
80 #include "asterisk/abstract_jb.h"
81 #include "asterisk/jabber.h"
83 #define GOOGLE_CONFIG "gtalk.conf"
85 #define GOOGLE_NS "http://www.google.com/session"
88 /*! Global jitterbuffer configuration - by default, jb is disabled */
89 static struct ast_jb_conf default_jbconf
=
93 .resync_threshold
= -1,
96 static struct ast_jb_conf global_jbconf
;
100 AJI_PROTOCOL_SSLTCP
= 2,
103 enum gtalk_connect_type
{
104 AJI_CONNECT_STUN
= 1,
105 AJI_CONNECT_LOCAL
= 2,
106 AJI_CONNECT_RELAY
= 3,
110 ast_mutex_t lock
; /*!< Channel private lock */
112 struct gtalk
*parent
; /*!< Parent client */
114 char us
[AJI_MAX_JIDLEN
];
115 char them
[AJI_MAX_JIDLEN
];
116 char ring
[10]; /*!< Message ID of ring */
117 iksrule
*ringrule
; /*!< Rule for matching RING request */
118 int initiator
; /*!< If we're the initiator */
121 struct ast_codec_pref prefs
;
122 struct gtalk_candidate
*theircandidates
;
123 struct gtalk_candidate
*ourcandidates
;
124 char cid_num
[80]; /*!< Caller ID num */
125 char cid_name
[80]; /*!< Caller ID name */
126 char exten
[80]; /*!< Called extension */
127 struct ast_channel
*owner
; /*!< Master Channel */
128 struct ast_rtp
*rtp
; /*!< RTP audio session */
129 struct ast_rtp
*vrtp
; /*!< RTP video session */
130 int jointcapability
; /*!< Supported capability at both ends (codecs ) */
132 struct gtalk_pvt
*next
; /* Next entity */
135 struct gtalk_candidate
{
137 enum gtalk_protocol protocol
;
141 enum gtalk_connect_type type
;
147 struct gtalk_candidate
*next
;
151 ASTOBJ_COMPONENTS(struct gtalk
);
152 struct aji_client
*connection
;
153 struct aji_buddy
*buddy
;
155 struct ast_codec_pref prefs
;
156 int amaflags
; /*!< AMA Flags */
157 char user
[AJI_MAX_JIDLEN
];
158 char context
[AST_MAX_CONTEXT
];
159 char accountcode
[AST_MAX_ACCOUNT_CODE
]; /*!< Account code */
161 ast_group_t callgroup
; /*!< Call group */
162 ast_group_t pickupgroup
; /*!< Pickup group */
163 int callingpres
; /*!< Calling presentation */
165 char language
[MAX_LANGUAGE
]; /*!< Default language for prompts */
166 char musicclass
[MAX_MUSICCLASS
]; /*!< Music on Hold class */
169 struct gtalk_container
{
170 ASTOBJ_CONTAINER_COMPONENTS(struct gtalk
);
173 static const char desc
[] = "Gtalk Channel";
175 static int global_capability
= AST_FORMAT_ULAW
| AST_FORMAT_ALAW
| AST_FORMAT_GSM
| AST_FORMAT_H263
;
177 AST_MUTEX_DEFINE_STATIC(gtalklock
); /*!< Protect the interface list (of gtalk_pvt's) */
179 /* Forward declarations */
180 static struct ast_channel
*gtalk_request(const char *type
, int format
, void *data
, int *cause
);
181 static int gtalk_digit(struct ast_channel
*ast
, char digit
, unsigned int duration
);
182 static int gtalk_digit_begin(struct ast_channel
*ast
, char digit
);
183 static int gtalk_digit_end(struct ast_channel
*ast
, char digit
, unsigned int duration
);
184 static int gtalk_call(struct ast_channel
*ast
, char *dest
, int timeout
);
185 static int gtalk_hangup(struct ast_channel
*ast
);
186 static int gtalk_answer(struct ast_channel
*ast
);
187 static int gtalk_action(struct gtalk
*client
, struct gtalk_pvt
*p
, const char *action
);
188 static void gtalk_free_pvt(struct gtalk
*client
, struct gtalk_pvt
*p
);
189 static int gtalk_newcall(struct gtalk
*client
, ikspak
*pak
);
190 static struct ast_frame
*gtalk_read(struct ast_channel
*ast
);
191 static int gtalk_write(struct ast_channel
*ast
, struct ast_frame
*f
);
192 static int gtalk_indicate(struct ast_channel
*ast
, int condition
, const void *data
, size_t datalen
);
193 static int gtalk_fixup(struct ast_channel
*oldchan
, struct ast_channel
*newchan
);
194 static int gtalk_sendhtml(struct ast_channel
*ast
, int subclass
, const char *data
, int datalen
);
195 static struct gtalk_pvt
*gtalk_alloc(struct gtalk
*client
, const char *us
, const char *them
, const char *sid
);
196 static int gtalk_do_reload(int fd
, int argc
, char **argv
);
197 static int gtalk_show_channels(int fd
, int argc
, char **argv
);
198 /*----- RTP interface functions */
199 static int gtalk_set_rtp_peer(struct ast_channel
*chan
, struct ast_rtp
*rtp
,
200 struct ast_rtp
*vrtp
, int codecs
, int nat_active
);
201 static enum ast_rtp_get_result
gtalk_get_rtp_peer(struct ast_channel
*chan
, struct ast_rtp
**rtp
);
202 static int gtalk_get_codec(struct ast_channel
*chan
);
204 /*! \brief PBX interface structure for channel registration */
205 static const struct ast_channel_tech gtalk_tech
= {
207 .description
= "Gtalk Channel Driver",
208 .capabilities
= ((AST_FORMAT_MAX_AUDIO
<< 1) - 1),
209 .requester
= gtalk_request
,
210 .send_digit_begin
= gtalk_digit_begin
,
211 .send_digit_end
= gtalk_digit_end
,
212 .bridge
= ast_rtp_bridge
,
214 .hangup
= gtalk_hangup
,
215 .answer
= gtalk_answer
,
217 .write
= gtalk_write
,
218 .exception
= gtalk_read
,
219 .indicate
= gtalk_indicate
,
220 .fixup
= gtalk_fixup
,
221 .send_html
= gtalk_sendhtml
,
222 .properties
= AST_CHAN_TP_WANTSJITTER
| AST_CHAN_TP_CREATESJITTER
225 static struct sockaddr_in bindaddr
= { 0, }; /*!< The address we bind to */
227 static struct sched_context
*sched
; /*!< The scheduling context */
228 static struct io_context
*io
; /*!< The IO context */
229 static struct in_addr __ourip
;
232 /*! \brief RTP driver interface */
233 static struct ast_rtp_protocol gtalk_rtp
= {
235 get_rtp_info
: gtalk_get_rtp_peer
,
236 set_rtp_peer
: gtalk_set_rtp_peer
,
237 get_codec
: gtalk_get_codec
,
240 static char show_channels_usage
[] =
241 "Usage: gtalk show channels\n"
242 " Shows current state of the Gtalk channels.\n";
244 static char reload_usage
[] =
245 "Usage: gtalk reload\n"
246 " Reload gtalk channel driver.\n";
249 static struct ast_cli_entry gtalk_cli
[] = {
250 {{ "gtalk", "reload", NULL
}, gtalk_do_reload
, "Reload GoogleTalk configuration", reload_usage
},
251 {{ "gtalk", "show", "channels", NULL
}, gtalk_show_channels
, "Show GoogleTalk channels", show_channels_usage
},
256 static char externip
[16];
258 static struct gtalk_container gtalk_list
;
260 static void gtalk_member_destroy(struct gtalk
*obj
)
265 static struct gtalk
*find_gtalk(char *name
, char *connection
)
267 struct gtalk
*gtalk
= NULL
;
268 char *domain
= NULL
, *s
= NULL
;
270 if(strchr(connection
, '@')) {
271 s
= ast_strdupa(connection
);
272 domain
= strsep(&s
, "@");
273 ast_verbose("OOOOH domain = %s\n", domain
);
275 gtalk
= ASTOBJ_CONTAINER_FIND(>alk_list
, name
);
276 if (!gtalk
&& strchr(name
, '@'))
277 gtalk
= ASTOBJ_CONTAINER_FIND_FULL(>alk_list
, name
, user
,,, strcasecmp
);
279 if (!gtalk
) { /* guest call */
280 ASTOBJ_CONTAINER_TRAVERSE(>alk_list
, 1, {
281 ASTOBJ_RDLOCK(iterator
);
282 if (!strcasecmp(iterator
->name
, "guest")) {
283 if (!strcasecmp(iterator
->connection
->jid
->partial
, connection
)) {
285 } else if (!strcasecmp(iterator
->connection
->name
, connection
)) {
287 } else if (iterator
->connection
->component
&& !strcasecmp(iterator
->connection
->user
,domain
)) {
291 ASTOBJ_UNLOCK(iterator
);
302 static int add_codec_to_answer(const struct gtalk_pvt
*p
, int codec
, iks
*dcodecs
)
305 char *format
= ast_getformatname(codec
);
307 if (!strcasecmp("ulaw", format
)) {
308 iks
*payload_eg711u
, *payload_pcmu
;
309 payload_pcmu
= iks_new("payload-type");
310 payload_eg711u
= iks_new("payload-type");
312 if(!payload_eg711u
|| !payload_pcmu
) {
314 iks_delete(payload_pcmu
);
316 iks_delete(payload_eg711u
);
317 ast_log(LOG_WARNING
,"Failed to allocate iks node");
320 iks_insert_attrib(payload_pcmu
, "id", "0");
321 iks_insert_attrib(payload_pcmu
, "name", "PCMU");
322 iks_insert_attrib(payload_pcmu
, "clockrate","8000");
323 iks_insert_attrib(payload_pcmu
, "bitrate","64000");
324 iks_insert_attrib(payload_eg711u
, "id", "100");
325 iks_insert_attrib(payload_eg711u
, "name", "EG711U");
326 iks_insert_attrib(payload_eg711u
, "clockrate","8000");
327 iks_insert_attrib(payload_eg711u
, "bitrate","64000");
328 iks_insert_node(dcodecs
, payload_pcmu
);
329 iks_insert_node(dcodecs
, payload_eg711u
);
332 if (!strcasecmp("alaw", format
)) {
333 iks
*payload_eg711a
, *payload_pcma
;
334 payload_pcma
= iks_new("payload-type");
335 payload_eg711a
= iks_new("payload-type");
336 if(!payload_eg711a
|| !payload_pcma
) {
338 iks_delete(payload_eg711a
);
340 iks_delete(payload_pcma
);
341 ast_log(LOG_WARNING
,"Failed to allocate iks node");
344 iks_insert_attrib(payload_pcma
, "id", "8");
345 iks_insert_attrib(payload_pcma
, "name", "PCMA");
346 iks_insert_attrib(payload_pcma
, "clockrate","8000");
347 iks_insert_attrib(payload_pcma
, "bitrate","64000");
348 payload_eg711a
= iks_new("payload-type");
349 iks_insert_attrib(payload_eg711a
, "id", "101");
350 iks_insert_attrib(payload_eg711a
, "name", "EG711A");
351 iks_insert_attrib(payload_eg711a
, "clockrate","8000");
352 iks_insert_attrib(payload_eg711a
, "bitrate","64000");
353 iks_insert_node(dcodecs
, payload_pcma
);
354 iks_insert_node(dcodecs
, payload_eg711a
);
357 if (!strcasecmp("ilbc", format
)) {
358 iks
*payload_ilbc
= iks_new("payload-type");
360 ast_log(LOG_WARNING
,"Failed to allocate iks node");
363 iks_insert_attrib(payload_ilbc
, "id", "97");
364 iks_insert_attrib(payload_ilbc
, "name", "iLBC");
365 iks_insert_attrib(payload_ilbc
, "clockrate","8000");
366 iks_insert_attrib(payload_ilbc
, "bitrate","13300");
367 iks_insert_node(dcodecs
, payload_ilbc
);
370 if (!strcasecmp("g723", format
)) {
371 iks
*payload_g723
= iks_new("payload-type");
373 ast_log(LOG_WARNING
,"Failed to allocate iks node");
376 iks_insert_attrib(payload_g723
, "id", "4");
377 iks_insert_attrib(payload_g723
, "name", "G723");
378 iks_insert_attrib(payload_g723
, "clockrate","8000");
379 iks_insert_attrib(payload_g723
, "bitrate","6300");
380 iks_insert_node(dcodecs
, payload_g723
);
383 if (!strcasecmp("speex", format
)) {
384 iks
*payload_speex
= iks_new("payload-type");
386 ast_log(LOG_WARNING
,"Failed to allocate iks node");
389 iks_insert_attrib(payload_speex
, "id", "110");
390 iks_insert_attrib(payload_speex
, "name", "speex");
391 iks_insert_attrib(payload_speex
, "clockrate","8000");
392 iks_insert_attrib(payload_speex
, "bitrate","11000");
393 iks_insert_node(dcodecs
, payload_speex
);
396 if (!strcasecmp("gsm", format
)) {
397 iks
*payload_gsm
= iks_new("payload-type");
399 ast_log(LOG_WARNING
,"Failed to allocate iks node");
402 iks_insert_attrib(payload_gsm
, "id", "103");
403 iks_insert_attrib(payload_gsm
, "name", "gsm");
404 iks_insert_node(dcodecs
, payload_gsm
);
407 ast_rtp_lookup_code(p
->rtp
, 1, codec
);
411 static int gtalk_invite(struct gtalk_pvt
*p
, char *to
, char *from
, char *sid
, int initiator
)
413 struct gtalk
*client
= p
->parent
;
414 iks
*iq
, *gtalk
, *dcodecs
, *payload_telephone
, *transport
;
421 gtalk
= iks_new("session");
422 dcodecs
= iks_new("description");
423 transport
= iks_new("transport");
424 payload_telephone
= iks_new("payload-type");
425 if (!(iq
&& gtalk
&& dcodecs
&& transport
&& payload_telephone
)){
433 iks_delete(transport
);
434 if(payload_telephone
)
435 iks_delete(payload_telephone
);
437 ast_log(LOG_ERROR
, "Could not allocate iksemel nodes\n");
440 iks_insert_attrib(dcodecs
, "xmlns", "http://www.google.com/session/phone");
441 iks_insert_attrib(dcodecs
, "xml:lang", "en");
443 for (x
= 0; x
< 32; x
++) {
444 if (!(pref_codec
= ast_codec_pref_index(&client
->prefs
, x
)))
446 if (!(client
->capability
& pref_codec
))
448 if (alreadysent
& pref_codec
)
450 codecs_num
= add_codec_to_answer(p
, pref_codec
, dcodecs
);
451 alreadysent
|= pref_codec
;
455 /* only propose DTMF within an audio session */
456 iks_insert_attrib(payload_telephone
, "id", "106");
457 iks_insert_attrib(payload_telephone
, "name", "telephone-event");
458 iks_insert_attrib(payload_telephone
, "clockrate", "8000");
460 iks_insert_attrib(transport
,"xmlns","http://www.google.com/transport/p2p");
462 iks_insert_attrib(iq
, "type", "set");
463 iks_insert_attrib(iq
, "to", to
);
464 iks_insert_attrib(iq
, "from", from
);
465 iks_insert_attrib(iq
, "id", client
->connection
->mid
);
466 ast_aji_increment_mid(client
->connection
->mid
);
468 iks_insert_attrib(gtalk
, "xmlns", "http://www.google.com/session");
469 iks_insert_attrib(gtalk
, "type",initiator
? "initiate": "accept");
470 iks_insert_attrib(gtalk
, "initiator", initiator
? from
: to
);
471 iks_insert_attrib(gtalk
, "id", sid
);
472 iks_insert_node(iq
, gtalk
);
473 iks_insert_node(gtalk
, dcodecs
);
474 iks_insert_node(gtalk
, transport
);
475 iks_insert_node(dcodecs
, payload_telephone
);
477 iks_send(client
->connection
->p
, iq
);
478 iks_delete(payload_telephone
);
479 iks_delete(transport
);
486 static int gtalk_invite_response(struct gtalk_pvt
*p
, char *to
, char *from
, char *sid
, int initiator
)
488 iks
*iq
, *session
, *transport
;
490 session
= iks_new("session");
491 transport
= iks_new("transport");
492 if(!(iq
&& session
&& transport
)) {
498 iks_delete(transport
);
499 ast_log(LOG_ERROR
, " Unable to allocate IKS node\n");
502 iks_insert_attrib(iq
, "from", from
);
503 iks_insert_attrib(iq
, "to", to
);
504 iks_insert_attrib(iq
, "type", "set");
505 iks_insert_attrib(iq
, "id",p
->parent
->connection
->mid
);
506 ast_aji_increment_mid(p
->parent
->connection
->mid
);
507 iks_insert_attrib(session
, "type", "transport-accept");
508 iks_insert_attrib(session
, "id", sid
);
509 iks_insert_attrib(session
, "initiator", initiator
? from
: to
);
510 iks_insert_attrib(session
, "xmlns", "http://www.google.com/session");
511 iks_insert_attrib(transport
, "xmlns", "http://www.google.com/transport/p2p");
512 iks_insert_node(iq
,session
);
513 iks_insert_node(session
,transport
);
514 iks_send(p
->parent
->connection
->p
, iq
);
515 iks_delete(transport
);
522 static int gtalk_ringing_ack(void *data
, ikspak
*pak
)
524 struct gtalk_pvt
*p
= data
;
527 iks_filter_remove_rule(p
->parent
->connection
->f
, p
->ringrule
);
530 ast_queue_control(p
->owner
, AST_CONTROL_RINGING
);
531 return IKS_FILTER_EAT
;
534 static int gtalk_answer(struct ast_channel
*ast
)
536 struct gtalk_pvt
*p
= ast
->tech_pvt
;
540 ast_log(LOG_DEBUG
, "Answer!\n");
541 ast_mutex_lock(&p
->lock
);
542 gtalk_invite(p
, p
->them
, p
->us
,p
->sid
, 0);
543 ast_mutex_unlock(&p
->lock
);
547 static enum ast_rtp_get_result
gtalk_get_rtp_peer(struct ast_channel
*chan
, struct ast_rtp
**rtp
)
549 struct gtalk_pvt
*p
= chan
->tech_pvt
;
550 enum ast_rtp_get_result res
= AST_RTP_GET_FAILED
;
555 ast_mutex_lock(&p
->lock
);
558 res
= AST_RTP_TRY_PARTIAL
;
560 ast_mutex_unlock(&p
->lock
);
565 static int gtalk_get_codec(struct ast_channel
*chan
)
567 struct gtalk_pvt
*p
= chan
->tech_pvt
;
568 return p
->peercapability
;
571 static int gtalk_set_rtp_peer(struct ast_channel
*chan
, struct ast_rtp
*rtp
, struct ast_rtp
*vrtp
, int codecs
, int nat_active
)
578 ast_mutex_lock(&p
->lock
);
581 ast_rtp_get_peer(rtp, &p->redirip);
583 memset(&p->redirip, 0, sizeof(p->redirip));
584 p->redircodecs = codecs; */
586 /* Reset lastrtprx timer */
587 ast_mutex_unlock(&p
->lock
);
591 static int gtalk_response(struct gtalk
*client
, char *from
, ikspak
*pak
, const char *reasonstr
, const char *reasonstr2
)
593 iks
*response
= NULL
, *error
= NULL
, *reason
= NULL
;
596 response
= iks_new("iq");
598 iks_insert_attrib(response
, "type", "result");
599 iks_insert_attrib(response
, "from", from
);
600 iks_insert_attrib(response
, "to", iks_find_attrib(pak
->x
, "from"));
601 iks_insert_attrib(response
, "id", iks_find_attrib(pak
->x
, "id"));
603 error
= iks_new("error");
605 iks_insert_attrib(error
, "type", "cancel");
606 reason
= iks_new(reasonstr
);
608 iks_insert_node(error
, reason
);
609 iks_insert_node(response
, error
);
612 iks_send(client
->connection
->p
, response
);
617 iks_delete(response
);
623 static int gtalk_is_answered(struct gtalk
*client
, ikspak
*pak
)
625 struct gtalk_pvt
*tmp
;
628 char s1
[BUFSIZ
], s2
[BUFSIZ
], s3
[BUFSIZ
];
629 int peernoncodeccapability
;
631 ast_log(LOG_DEBUG
, "The client is %s\n", client
->name
);
632 /* Make sure our new call doesn't exist yet */
633 for (tmp
= client
->p
; tmp
; tmp
= tmp
->next
) {
634 if (iks_find_with_attrib(pak
->x
, "session", "id", tmp
->sid
))
638 /* codec points to the first <payload-type/> tag */
639 codec
= iks_child(iks_child(iks_child(pak
->x
)));
641 ast_rtp_set_m_type(tmp
->rtp
, atoi(iks_find_attrib(codec
, "id")));
642 ast_rtp_set_rtpmap_type(tmp
->rtp
, atoi(iks_find_attrib(codec
, "id")), "audio", iks_find_attrib(codec
, "name"), 0);
643 codec
= iks_next(codec
);
646 /* Now gather all of the codecs that we are asked for */
647 ast_rtp_get_current_formats(tmp
->rtp
, &tmp
->peercapability
, &peernoncodeccapability
);
649 /* at this point, we received an awser from the remote Gtalk client,
650 which allows us to compare capabilities */
651 tmp
->jointcapability
= tmp
->capability
& tmp
->peercapability
;
652 if (!tmp
->jointcapability
) {
653 ast_log(LOG_WARNING
, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1
, BUFSIZ
, tmp
->capability
),
654 ast_getformatname_multiple(s2
, BUFSIZ
, tmp
->peercapability
),
655 ast_getformatname_multiple(s3
, BUFSIZ
, tmp
->jointcapability
));
656 /* close session if capabilities don't match */
657 ast_queue_hangup(tmp
->owner
);
663 from
= iks_find_attrib(pak
->x
, "to");
665 from
= client
->connection
->jid
->full
;
669 ast_queue_control(tmp
->owner
, AST_CONTROL_ANSWER
);
671 ast_log(LOG_NOTICE
, "Whoa, didn't find call!\n");
672 gtalk_response(client
, from
, pak
, NULL
, NULL
);
676 static int gtalk_is_accepted(struct gtalk
*client
, ikspak
*pak
)
678 struct gtalk_pvt
*tmp
;
681 ast_log(LOG_DEBUG
, "The client is %s\n", client
->name
);
682 /* find corresponding call */
683 for (tmp
= client
->p
; tmp
; tmp
= tmp
->next
) {
684 if (iks_find_with_attrib(pak
->x
, "session", "id", tmp
->sid
))
688 from
= iks_find_attrib(pak
->x
, "to");
690 from
= client
->connection
->jid
->full
;
693 ast_log(LOG_NOTICE
, "Whoa, didn't find call!\n");
695 /* answer 'iq' packet to let the remote peer know that we're alive */
696 gtalk_response(client
, from
, pak
, NULL
, NULL
);
700 static int gtalk_handle_dtmf(struct gtalk
*client
, ikspak
*pak
)
702 struct gtalk_pvt
*tmp
;
703 iks
*dtmfnode
= NULL
, *dtmfchild
= NULL
;
706 /* Make sure our new call doesn't exist yet */
707 for (tmp
= client
->p
; tmp
; tmp
= tmp
->next
) {
708 if (iks_find_with_attrib(pak
->x
, "session", "id", tmp
->sid
) || iks_find_with_attrib(pak
->x
, "gtalk", "sid", tmp
->sid
))
711 from
= iks_find_attrib(pak
->x
, "to");
713 from
= client
->connection
->jid
->full
;
717 if(iks_find_with_attrib(pak
->x
, "dtmf-method", "method", "rtp")) {
718 gtalk_response(client
, from
, pak
,
719 "feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
720 "unsupported-dtmf-method xmlns='http://jabber.org/protocol/gtalk/info/dtmf#errors'");
723 if ((dtmfnode
= iks_find(pak
->x
, "dtmf"))) {
724 if((dtmf
= iks_find_attrib(dtmfnode
, "code"))) {
725 if(iks_find_with_attrib(pak
->x
, "dtmf", "action", "button-up")) {
726 struct ast_frame f
= {AST_FRAME_DTMF_BEGIN
, };
727 f
.subclass
= dtmf
[0];
728 ast_queue_frame(tmp
->owner
, &f
);
729 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f
.subclass
);
730 } else if(iks_find_with_attrib(pak
->x
, "dtmf", "action", "button-down")) {
731 struct ast_frame f
= {AST_FRAME_DTMF_END
, };
732 f
.subclass
= dtmf
[0];
733 ast_queue_frame(tmp
->owner
, &f
);
734 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f
.subclass
);
735 } else if(iks_find_attrib(pak
->x
, "dtmf")) { /* 250 millasecond default */
736 struct ast_frame f
= {AST_FRAME_DTMF
, };
737 f
.subclass
= dtmf
[0];
738 ast_queue_frame(tmp
->owner
, &f
);
739 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f
.subclass
);
742 } else if ((dtmfnode
= iks_find_with_attrib(pak
->x
, "gtalk", "action", "session-info"))) {
743 if((dtmfchild
= iks_find(dtmfnode
, "dtmf"))) {
744 if((dtmf
= iks_find_attrib(dtmfchild
, "code"))) {
745 if(iks_find_with_attrib(dtmfnode
, "dtmf", "action", "button-up")) {
746 struct ast_frame f
= {AST_FRAME_DTMF_END
, };
747 f
.subclass
= dtmf
[0];
748 ast_queue_frame(tmp
->owner
, &f
);
749 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f
.subclass
);
750 } else if(iks_find_with_attrib(dtmfnode
, "dtmf", "action", "button-down")) {
751 struct ast_frame f
= {AST_FRAME_DTMF_BEGIN
, };
752 f
.subclass
= dtmf
[0];
753 ast_queue_frame(tmp
->owner
, &f
);
754 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f
.subclass
);
759 gtalk_response(client
, from
, pak
, NULL
, NULL
);
762 ast_log(LOG_NOTICE
, "Whoa, didn't find call!\n");
764 gtalk_response(client
, from
, pak
, NULL
, NULL
);
768 static int gtalk_hangup_farend(struct gtalk
*client
, ikspak
*pak
)
770 struct gtalk_pvt
*tmp
;
773 ast_log(LOG_DEBUG
, "The client is %s\n", client
->name
);
774 /* Make sure our new call doesn't exist yet */
775 for (tmp
= client
->p
; tmp
; tmp
= tmp
->next
) {
776 if (iks_find_with_attrib(pak
->x
, "session", "id", tmp
->sid
))
779 from
= iks_find_attrib(pak
->x
, "to");
781 from
= client
->connection
->jid
->full
;
784 tmp
->alreadygone
= 1;
786 ast_queue_hangup(tmp
->owner
);
788 ast_log(LOG_NOTICE
, "Whoa, didn't find call!\n");
789 gtalk_response(client
, from
, pak
, NULL
, NULL
);
793 static int gtalk_create_candidates(struct gtalk
*client
, struct gtalk_pvt
*p
, char *sid
, char *from
, char *to
)
795 struct gtalk_candidate
*tmp
;
796 struct aji_client
*c
= client
->connection
;
797 struct gtalk_candidate
*ours1
= NULL
, *ours2
= NULL
;
798 struct sockaddr_in sin
;
799 struct sockaddr_in dest
;
801 iks
*iq
, *gtalk
, *candidate
, *transport
;
802 char user
[17], pass
[17], preference
[5], port
[7];
806 gtalk
= iks_new("session");
807 candidate
= iks_new("candidate");
808 transport
= iks_new("transport");
809 if (!iq
|| !gtalk
|| !candidate
|| !transport
) {
810 ast_log(LOG_ERROR
, "Memory allocation error\n");
813 ours1
= ast_calloc(1, sizeof(*ours1
));
814 ours2
= ast_calloc(1, sizeof(*ours2
));
815 if (!ours1
|| !ours2
)
818 iks_insert_attrib(transport
, "xmlns","http://www.google.com/transport/p2p");
819 iks_insert_node(iq
, gtalk
);
820 iks_insert_node(gtalk
,transport
);
821 iks_insert_node(transport
, candidate
);
823 for (; p
; p
= p
->next
) {
824 if (!strcasecmp(p
->sid
, sid
))
829 ast_log(LOG_NOTICE
, "No matching gtalk session - SID %s!\n", sid
);
833 ast_rtp_get_us(p
->rtp
, &sin
);
834 ast_find_ourip(&us
, bindaddr
);
836 /* Setup our gtalk candidates */
837 ast_copy_string(ours1
->name
, "rtp", sizeof(ours1
->name
));
838 ours1
->port
= ntohs(sin
.sin_port
);
839 ours1
->preference
= 1;
840 snprintf(user
, sizeof(user
), "%08lx%08lx", ast_random(), ast_random());
841 snprintf(pass
, sizeof(pass
), "%08lx%08lx", ast_random(), ast_random());
842 ast_copy_string(ours1
->username
, user
, sizeof(ours1
->username
));
843 ast_copy_string(ours1
->password
, pass
, sizeof(ours1
->password
));
844 ast_copy_string(ours1
->ip
, ast_inet_ntoa(us
), sizeof(ours1
->ip
));
845 ours1
->protocol
= AJI_PROTOCOL_UDP
;
846 ours1
->type
= AJI_CONNECT_LOCAL
;
847 ours1
->generation
= 0;
848 p
->ourcandidates
= ours1
;
850 if (!ast_strlen_zero(externip
)) {
851 /* XXX We should really stun for this one not just go with externip XXX */
852 snprintf(user
, sizeof(user
), "%08lx%08lx", ast_random(), ast_random());
853 snprintf(pass
, sizeof(pass
), "%08lx%08lx", ast_random(), ast_random());
854 ast_copy_string(ours2
->username
, user
, sizeof(ours2
->username
));
855 ast_copy_string(ours2
->password
, pass
, sizeof(ours2
->password
));
856 ast_copy_string(ours2
->ip
, externip
, sizeof(ours2
->ip
));
857 ast_copy_string(ours2
->name
, "rtp", sizeof(ours1
->name
));
858 ours2
->port
= ntohs(sin
.sin_port
);
859 ours2
->preference
= 0.9;
860 ours2
->protocol
= AJI_PROTOCOL_UDP
;
861 ours2
->type
= AJI_CONNECT_STUN
;
862 ours2
->generation
= 0;
867 dest
.sin_addr
= __ourip
;
868 dest
.sin_port
= sin
.sin_port
;
871 for (tmp
= p
->ourcandidates
; tmp
; tmp
= tmp
->next
) {
872 snprintf(port
, sizeof(port
), "%d", tmp
->port
);
873 snprintf(preference
, sizeof(preference
), "%.2f", tmp
->preference
);
874 iks_insert_attrib(iq
, "from", to
);
875 iks_insert_attrib(iq
, "to", from
);
876 iks_insert_attrib(iq
, "type", "set");
877 iks_insert_attrib(iq
, "id", c
->mid
);
878 ast_aji_increment_mid(c
->mid
);
879 iks_insert_attrib(gtalk
, "type", "transport-info");
880 iks_insert_attrib(gtalk
, "id", sid
);
881 iks_insert_attrib(gtalk
, "initiator", (p
->initiator
) ? to
: from
);
882 iks_insert_attrib(gtalk
, "xmlns", GOOGLE_NS
);
883 iks_insert_attrib(candidate
, "name", tmp
->name
);
884 iks_insert_attrib(candidate
, "address", tmp
->ip
);
885 iks_insert_attrib(candidate
, "port", port
);
886 iks_insert_attrib(candidate
, "username", tmp
->username
);
887 iks_insert_attrib(candidate
, "password", tmp
->password
);
888 iks_insert_attrib(candidate
, "preference", preference
);
889 if (tmp
->protocol
== AJI_PROTOCOL_UDP
)
890 iks_insert_attrib(candidate
, "protocol", "udp");
891 if (tmp
->protocol
== AJI_PROTOCOL_SSLTCP
)
892 iks_insert_attrib(candidate
, "protocol", "ssltcp");
893 if (tmp
->type
== AJI_CONNECT_STUN
)
894 iks_insert_attrib(candidate
, "type", "stun");
895 if (tmp
->type
== AJI_CONNECT_LOCAL
)
896 iks_insert_attrib(candidate
, "type", "local");
897 if (tmp
->type
== AJI_CONNECT_RELAY
)
898 iks_insert_attrib(candidate
, "type", "relay");
899 iks_insert_attrib(candidate
, "network", "0");
900 iks_insert_attrib(candidate
, "generation", "0");
915 iks_delete(candidate
);
917 iks_delete(transport
);
921 static struct gtalk_pvt
*gtalk_alloc(struct gtalk
*client
, const char *us
, const char *them
, const char *sid
)
923 struct gtalk_pvt
*tmp
= NULL
;
924 struct aji_resource
*resources
= NULL
;
925 struct aji_buddy
*buddy
;
927 char *data
, *exten
= NULL
;
930 ast_log(LOG_DEBUG
, "The client is %s for alloc\n", client
->name
);
931 if (!sid
&& !strchr(them
, '/')) { /* I started call! */
932 if (!strcasecmp(client
->name
, "guest")) {
933 buddy
= ASTOBJ_CONTAINER_FIND(&client
->connection
->buddies
, them
);
935 resources
= buddy
->resources
;
936 } else if (client
->buddy
)
937 resources
= client
->buddy
->resources
;
939 if (resources
->cap
->jingle
) {
942 resources
= resources
->next
;
945 snprintf(idroster
, sizeof(idroster
), "%s/%s", them
, resources
->resource
);
947 ast_log(LOG_ERROR
, "no gtalk capable clients to talk to.\n");
951 if (!(tmp
= ast_calloc(1, sizeof(*tmp
)))) {
955 ast_copy_string(tmp
->sid
, sid
, sizeof(tmp
->sid
));
956 ast_copy_string(tmp
->them
, them
, sizeof(tmp
->them
));
957 ast_copy_string(tmp
->us
, us
, sizeof(tmp
->us
));
959 snprintf(tmp
->sid
, sizeof(tmp
->sid
), "%08lx%08lx", ast_random(), ast_random());
960 ast_copy_string(tmp
->them
, idroster
, sizeof(tmp
->them
));
961 ast_copy_string(tmp
->us
, us
, sizeof(tmp
->us
));
965 tmp
->rtp
= ast_rtp_new_with_bindaddr(sched
, io
, 1, 0, bindaddr
.sin_addr
);
966 ast_rtp_pt_clear(tmp
->rtp
);
968 /* add user configured codec capabilites */
969 if (client
->capability
)
970 tmp
->capability
= client
->capability
;
971 else if (global_capability
)
972 tmp
->capability
= global_capability
;
974 tmp
->parent
= client
;
976 ast_log(LOG_WARNING
, "Out of RTP sessions?\n");
981 /* Set CALLERID(name) to the full JID of the remote peer */
982 ast_copy_string(tmp
->cid_name
, tmp
->them
, sizeof(tmp
->cid_name
));
984 if(strchr(tmp
->us
, '/')) {
985 data
= ast_strdupa(tmp
->us
);
986 exten
= strsep(&data
, "/");
989 ast_copy_string(tmp
->exten
, exten
, sizeof(tmp
->exten
));
990 ast_mutex_init(&tmp
->lock
);
991 ast_mutex_lock(>alklock
);
992 tmp
->next
= client
->p
;
994 ast_mutex_unlock(>alklock
);
998 /*! \brief Start new gtalk channel */
999 static struct ast_channel
*gtalk_new(struct gtalk
*client
, struct gtalk_pvt
*i
, int state
, const char *title
)
1001 struct ast_channel
*tmp
;
1010 tmp
= ast_channel_alloc(1, state
, i
->cid_num
, i
->cid_name
, client
->accountcode
, i
->exten
, client
->context
, client
->amaflags
, "Gtalk/%s-%04lx", n2
, ast_random() & 0xffff);
1012 ast_log(LOG_WARNING
, "Unable to allocate Gtalk channel structure!\n");
1015 tmp
->tech
= >alk_tech
;
1017 /* Select our native format based on codec preference until we receive
1018 something from another device to the contrary. */
1019 if (i
->jointcapability
)
1020 what
= i
->jointcapability
;
1021 else if (i
->capability
)
1022 what
= i
->capability
;
1024 what
= global_capability
;
1025 tmp
->nativeformats
= ast_codec_choose(&i
->prefs
, what
, 1) | (i
->jointcapability
& AST_FORMAT_VIDEO_MASK
);
1026 fmt
= ast_best_codec(tmp
->nativeformats
);
1029 ast_rtp_setstun(i
->rtp
, 1);
1030 tmp
->fds
[0] = ast_rtp_fd(i
->rtp
);
1031 tmp
->fds
[1] = ast_rtcp_fd(i
->rtp
);
1034 ast_rtp_setstun(i
->rtp
, 1);
1035 tmp
->fds
[2] = ast_rtp_fd(i
->vrtp
);
1036 tmp
->fds
[3] = ast_rtcp_fd(i
->vrtp
);
1038 if (state
== AST_STATE_RING
)
1040 tmp
->adsicpe
= AST_ADSI_UNAVAILABLE
;
1041 tmp
->writeformat
= fmt
;
1042 tmp
->rawwriteformat
= fmt
;
1043 tmp
->readformat
= fmt
;
1044 tmp
->rawreadformat
= fmt
;
1047 tmp
->callgroup
= client
->callgroup
;
1048 tmp
->pickupgroup
= client
->pickupgroup
;
1049 tmp
->cid
.cid_pres
= client
->callingpres
;
1050 if (!ast_strlen_zero(client
->accountcode
))
1051 ast_string_field_set(tmp
, accountcode
, client
->accountcode
);
1052 if (client
->amaflags
)
1053 tmp
->amaflags
= client
->amaflags
;
1054 if (!ast_strlen_zero(client
->language
))
1055 ast_string_field_set(tmp
, language
, client
->language
);
1056 if (!ast_strlen_zero(client
->musicclass
))
1057 ast_string_field_set(tmp
, musicclass
, client
->musicclass
);
1059 ast_module_ref(ast_module_info
->self
);
1060 ast_copy_string(tmp
->context
, client
->context
, sizeof(tmp
->context
));
1061 ast_copy_string(tmp
->exten
, i
->exten
, sizeof(tmp
->exten
));
1063 if (!ast_strlen_zero(i
->exten
) && strcmp(i
->exten
, "s"))
1064 tmp
->cid
.cid_dnid
= ast_strdup(i
->exten
);
1067 ast_jb_configure(tmp
, &global_jbconf
);
1068 if (state
!= AST_STATE_DOWN
&& ast_pbx_start(tmp
)) {
1069 ast_log(LOG_WARNING
, "Unable to start PBX on %s\n", tmp
->name
);
1070 tmp
->hangupcause
= AST_CAUSE_SWITCH_CONGESTION
;
1078 static int gtalk_action(struct gtalk
*client
, struct gtalk_pvt
*p
, const char *action
)
1080 iks
*request
, *session
= NULL
;
1083 request
= iks_new("iq");
1085 iks_insert_attrib(request
, "type", "set");
1086 iks_insert_attrib(request
, "from", p
->us
);
1087 iks_insert_attrib(request
, "to", p
->them
);
1088 iks_insert_attrib(request
, "id", client
->connection
->mid
);
1089 ast_aji_increment_mid(client
->connection
->mid
);
1090 session
= iks_new("session");
1092 iks_insert_attrib(session
, "type", action
);
1093 iks_insert_attrib(session
, "id", p
->sid
);
1094 iks_insert_attrib(session
, "initiator", p
->initiator
? p
->us
: p
->them
);
1095 iks_insert_attrib(session
, "xmlns", "http://www.google.com/session");
1096 iks_insert_node(request
, session
);
1097 iks_send(client
->connection
->p
, request
);
1098 iks_delete(session
);
1101 iks_delete(request
);
1106 static void gtalk_free_candidates(struct gtalk_candidate
*candidate
)
1108 struct gtalk_candidate
*last
;
1111 candidate
= candidate
->next
;
1116 static void gtalk_free_pvt(struct gtalk
*client
, struct gtalk_pvt
*p
)
1118 struct gtalk_pvt
*cur
, *prev
= NULL
;
1123 prev
->next
= p
->next
;
1125 client
->p
= p
->next
;
1132 iks_filter_remove_rule(p
->parent
->connection
->f
, p
->ringrule
);
1134 ast_log(LOG_WARNING
, "Uh oh, there's an owner, this is going to be messy.\n");
1136 ast_rtp_destroy(p
->rtp
);
1138 ast_rtp_destroy(p
->vrtp
);
1139 gtalk_free_candidates(p
->theircandidates
);
1144 static int gtalk_newcall(struct gtalk
*client
, ikspak
*pak
)
1146 struct gtalk_pvt
*p
, *tmp
= client
->p
;
1147 struct ast_channel
*chan
;
1151 char s1
[BUFSIZ
], s2
[BUFSIZ
], s3
[BUFSIZ
];
1152 int peernoncodeccapability
;
1154 /* Make sure our new call doesn't exist yet */
1155 from
= iks_find_attrib(pak
->x
,"to");
1157 from
= client
->connection
->jid
->full
;
1160 if (iks_find_with_attrib(pak
->x
, "session", "id", tmp
->sid
)) {
1161 ast_log(LOG_NOTICE
, "Ignoring duplicate call setup on SID %s\n", tmp
->sid
);
1162 gtalk_response(client
, from
, pak
, "out-of-order", NULL
);
1168 p
= gtalk_alloc(client
, from
, pak
->from
->full
, iks_find_attrib(pak
->query
, "id"));
1170 ast_log(LOG_WARNING
, "Unable to allocate gtalk structure!\n");
1174 chan
= gtalk_new(client
, p
, AST_STATE_DOWN
, pak
->from
->user
);
1176 gtalk_free_pvt(client
, p
);
1180 ast_mutex_lock(&p
->lock
);
1181 ast_copy_string(p
->them
, pak
->from
->full
, sizeof(p
->them
));
1182 if (iks_find_attrib(pak
->query
, "id")) {
1183 ast_copy_string(p
->sid
, iks_find_attrib(pak
->query
, "id"),
1187 /* codec points to the first <payload-type/> tag */
1188 codec
= iks_child(iks_child(iks_child(pak
->x
)));
1191 ast_rtp_set_m_type(p
->rtp
, atoi(iks_find_attrib(codec
, "id")));
1192 ast_rtp_set_rtpmap_type(p
->rtp
, atoi(iks_find_attrib(codec
, "id")), "audio", iks_find_attrib(codec
, "name"), 0);
1193 codec
= iks_next(codec
);
1196 /* Now gather all of the codecs that we are asked for */
1197 ast_rtp_get_current_formats(p
->rtp
, &p
->peercapability
, &peernoncodeccapability
);
1198 p
->jointcapability
= p
->capability
& p
->peercapability
;
1199 ast_mutex_unlock(&p
->lock
);
1201 ast_setstate(chan
, AST_STATE_RING
);
1202 if (!p
->jointcapability
) {
1203 ast_log(LOG_WARNING
, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1
, BUFSIZ
, p
->capability
),
1204 ast_getformatname_multiple(s2
, BUFSIZ
, p
->peercapability
),
1205 ast_getformatname_multiple(s3
, BUFSIZ
, p
->jointcapability
));
1206 /* close session if capabilities don't match */
1207 gtalk_action(client
, p
, "reject");
1210 ast_channel_free(chan
);
1214 res
= ast_pbx_start(chan
);
1217 case AST_PBX_FAILED
:
1218 ast_log(LOG_WARNING
, "Failed to start PBX :(\n");
1219 gtalk_response(client
, from
, pak
, "service-unavailable", NULL
);
1221 case AST_PBX_CALL_LIMIT
:
1222 ast_log(LOG_WARNING
, "Failed to start PBX (call limit reached) \n");
1223 gtalk_response(client
, from
, pak
, "service-unavailable", NULL
);
1225 case AST_PBX_SUCCESS
:
1226 gtalk_response(client
, from
, pak
, NULL
, NULL
);
1227 gtalk_invite_response(p
, p
->them
, p
->us
,p
->sid
, 0);
1228 gtalk_create_candidates(client
, p
, p
->sid
, p
->them
, p
->us
);
1236 static int gtalk_update_stun(struct gtalk
*client
, struct gtalk_pvt
*p
)
1238 struct gtalk_candidate
*tmp
;
1240 struct ast_hostent ahp
;
1241 struct sockaddr_in sin
;
1242 struct sockaddr_in aux
;
1244 if (time(NULL
) == p
->laststun
)
1247 tmp
= p
->theircandidates
;
1248 p
->laststun
= time(NULL
);
1252 /* Find the IP address of the host */
1253 hp
= ast_gethostbyname(tmp
->ip
, &ahp
);
1254 sin
.sin_family
= AF_INET
;
1255 memcpy(&sin
.sin_addr
, hp
->h_addr
, sizeof(sin
.sin_addr
));
1256 sin
.sin_port
= htons(tmp
->port
);
1257 snprintf(username
, sizeof(username
), "%s%s", tmp
->username
,
1258 p
->ourcandidates
->username
);
1260 /* Find out the result of the STUN */
1261 ast_rtp_get_peer(p
->rtp
, &aux
);
1263 /* If the STUN result is different from the IP of the hostname,
1264 lock on the stun IP of the hostname advertised by the
1266 if (aux
.sin_addr
.s_addr
&&
1267 aux
.sin_addr
.s_addr
!= sin
.sin_addr
.s_addr
)
1268 ast_rtp_stun_request(p
->rtp
, &aux
, username
);
1270 ast_rtp_stun_request(p
->rtp
, &sin
, username
);
1272 if (aux
.sin_addr
.s_addr
&& option_debug
> 3) {
1273 ast_log(LOG_DEBUG
, "Receiving RTP traffic from IP %s, matches with remote candidate's IP %s\n", ast_inet_ntoa(aux
.sin_addr
), tmp
->ip
);
1274 ast_log(LOG_DEBUG
, "Sending STUN request to %s\n", tmp
->ip
);
1282 static int gtalk_add_candidate(struct gtalk
*client
, ikspak
*pak
)
1284 struct gtalk_pvt
*p
= NULL
, *tmp
= NULL
;
1285 struct aji_client
*c
= client
->connection
;
1286 struct gtalk_candidate
*newcandidate
= NULL
;
1287 iks
*traversenodes
= NULL
, *receipt
= NULL
;
1290 from
= iks_find_attrib(pak
->x
,"to");
1292 from
= c
->jid
->full
;
1294 for (tmp
= client
->p
; tmp
; tmp
= tmp
->next
) {
1295 if (iks_find_with_attrib(pak
->x
, "session", "id", tmp
->sid
)) {
1304 traversenodes
= pak
->query
;
1305 while(traversenodes
) {
1306 if(!strcasecmp(iks_name(traversenodes
), "session")) {
1307 traversenodes
= iks_child(traversenodes
);
1310 if(!strcasecmp(iks_name(traversenodes
), "transport")) {
1311 traversenodes
= iks_child(traversenodes
);
1314 if(!strcasecmp(iks_name(traversenodes
), "candidate")) {
1315 newcandidate
= ast_calloc(1, sizeof(*newcandidate
));
1318 ast_copy_string(newcandidate
->name
, iks_find_attrib(traversenodes
, "name"),
1319 sizeof(newcandidate
->name
));
1320 ast_copy_string(newcandidate
->ip
, iks_find_attrib(traversenodes
, "address"),
1321 sizeof(newcandidate
->ip
));
1322 newcandidate
->port
= atoi(iks_find_attrib(traversenodes
, "port"));
1323 ast_copy_string(newcandidate
->username
, iks_find_attrib(traversenodes
, "username"),
1324 sizeof(newcandidate
->username
));
1325 ast_copy_string(newcandidate
->password
, iks_find_attrib(traversenodes
, "password"),
1326 sizeof(newcandidate
->password
));
1327 newcandidate
->preference
= atof(iks_find_attrib(traversenodes
, "preference"));
1328 if (!strcasecmp(iks_find_attrib(traversenodes
, "protocol"), "udp"))
1329 newcandidate
->protocol
= AJI_PROTOCOL_UDP
;
1330 if (!strcasecmp(iks_find_attrib(traversenodes
, "protocol"), "ssltcp"))
1331 newcandidate
->protocol
= AJI_PROTOCOL_SSLTCP
;
1333 if (!strcasecmp(iks_find_attrib(traversenodes
, "type"), "stun"))
1334 newcandidate
->type
= AJI_CONNECT_STUN
;
1335 if (!strcasecmp(iks_find_attrib(traversenodes
, "type"), "local"))
1336 newcandidate
->type
= AJI_CONNECT_LOCAL
;
1337 if (!strcasecmp(iks_find_attrib(traversenodes
, "type"), "relay"))
1338 newcandidate
->type
= AJI_CONNECT_RELAY
;
1339 ast_copy_string(newcandidate
->network
, iks_find_attrib(traversenodes
, "network"),
1340 sizeof(newcandidate
->network
));
1341 newcandidate
->generation
= atoi(iks_find_attrib(traversenodes
, "generation"));
1342 newcandidate
->next
= NULL
;
1344 newcandidate
->next
= p
->theircandidates
;
1345 p
->theircandidates
= newcandidate
;
1347 gtalk_update_stun(p
->parent
, p
);
1348 newcandidate
= NULL
;
1350 traversenodes
= iks_next(traversenodes
);
1353 receipt
= iks_new("iq");
1354 iks_insert_attrib(receipt
, "type", "result");
1355 iks_insert_attrib(receipt
, "from", from
);
1356 iks_insert_attrib(receipt
, "to", iks_find_attrib(pak
->x
, "from"));
1357 iks_insert_attrib(receipt
, "id", iks_find_attrib(pak
->x
, "id"));
1358 iks_send(c
->p
, receipt
);
1359 iks_delete(receipt
);
1364 static struct ast_frame
*gtalk_rtp_read(struct ast_channel
*ast
, struct gtalk_pvt
*p
)
1366 struct ast_frame
*f
;
1369 return &ast_null_frame
;
1370 f
= ast_rtp_read(p
->rtp
);
1371 gtalk_update_stun(p
->parent
, p
);
1373 /* We already hold the channel lock */
1374 if (f
->frametype
== AST_FRAME_VOICE
) {
1375 if (f
->subclass
!= (p
->owner
->nativeformats
& AST_FORMAT_AUDIO_MASK
)) {
1377 ast_log(LOG_DEBUG
, "Oooh, format changed to %d\n", f
->subclass
);
1378 p
->owner
->nativeformats
=
1379 (p
->owner
->nativeformats
& AST_FORMAT_VIDEO_MASK
) | f
->subclass
;
1380 ast_set_read_format(p
->owner
, p
->owner
->readformat
);
1381 ast_set_write_format(p
->owner
, p
->owner
->writeformat
);
1383 /* if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
1384 f = ast_dsp_process(p->owner, p->vad, f);
1385 if (option_debug && f && (f->frametype == AST_FRAME_DTMF))
1386 ast_log(LOG_DEBUG, "* Detected inband DTMF '%c'\n", f->subclass);
1393 static struct ast_frame
*gtalk_read(struct ast_channel
*ast
)
1395 struct ast_frame
*fr
;
1396 struct gtalk_pvt
*p
= ast
->tech_pvt
;
1398 ast_mutex_lock(&p
->lock
);
1399 fr
= gtalk_rtp_read(ast
, p
);
1400 ast_mutex_unlock(&p
->lock
);
1404 /*! \brief Send frame to media channel (rtp) */
1405 static int gtalk_write(struct ast_channel
*ast
, struct ast_frame
*frame
)
1407 struct gtalk_pvt
*p
= ast
->tech_pvt
;
1410 switch (frame
->frametype
) {
1411 case AST_FRAME_VOICE
:
1412 if (!(frame
->subclass
& ast
->nativeformats
)) {
1413 ast_log(LOG_WARNING
,
1414 "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
1415 frame
->subclass
, ast
->nativeformats
, ast
->readformat
,
1420 ast_mutex_lock(&p
->lock
);
1422 res
= ast_rtp_write(p
->rtp
, frame
);
1424 ast_mutex_unlock(&p
->lock
);
1427 case AST_FRAME_VIDEO
:
1429 ast_mutex_lock(&p
->lock
);
1431 res
= ast_rtp_write(p
->vrtp
, frame
);
1433 ast_mutex_unlock(&p
->lock
);
1436 case AST_FRAME_IMAGE
:
1440 ast_log(LOG_WARNING
, "Can't send %d type frames with Gtalk write\n",
1448 static int gtalk_fixup(struct ast_channel
*oldchan
, struct ast_channel
*newchan
)
1450 struct gtalk_pvt
*p
= newchan
->tech_pvt
;
1451 ast_mutex_lock(&p
->lock
);
1453 if ((p
->owner
!= oldchan
)) {
1454 ast_mutex_unlock(&p
->lock
);
1457 if (p
->owner
== oldchan
)
1459 ast_mutex_unlock(&p
->lock
);
1463 static int gtalk_indicate(struct ast_channel
*ast
, int condition
, const void *data
, size_t datalen
)
1467 switch (condition
) {
1468 case AST_CONTROL_HOLD
:
1469 ast_moh_start(ast
, data
, NULL
);
1471 case AST_CONTROL_UNHOLD
:
1475 ast_log(LOG_NOTICE
, "Don't know how to indicate condition '%d'\n", condition
);
1482 static int gtalk_digit_begin(struct ast_channel
*chan
, char digit
)
1484 return gtalk_digit(chan
, digit
, 0);
1487 static int gtalk_digit_end(struct ast_channel
*chan
, char digit
, unsigned int duration
)
1489 return gtalk_digit(chan
, digit
, duration
);
1492 static int gtalk_digit(struct ast_channel
*ast
, char digit
, unsigned int duration
)
1494 struct gtalk_pvt
*p
= ast
->tech_pvt
;
1495 struct gtalk
*client
= p
->parent
;
1496 iks
*iq
, *gtalk
, *dtmf
;
1497 char buffer
[2] = {digit
, '\0'};
1499 gtalk
= iks_new("gtalk");
1500 dtmf
= iks_new("dtmf");
1501 if(!iq
|| !gtalk
|| !dtmf
) {
1508 ast_log(LOG_ERROR
, "Did not send dtmf do to memory issue\n");
1512 iks_insert_attrib(iq
, "type", "set");
1513 iks_insert_attrib(iq
, "to", p
->them
);
1514 iks_insert_attrib(iq
, "from", p
->us
);
1515 iks_insert_attrib(iq
, "id", client
->connection
->mid
);
1516 ast_aji_increment_mid(client
->connection
->mid
);
1517 iks_insert_attrib(gtalk
, "xmlns", "http://jabber.org/protocol/gtalk");
1518 iks_insert_attrib(gtalk
, "action", "session-info");
1519 iks_insert_attrib(gtalk
, "initiator", p
->initiator
? p
->us
: p
->them
);
1520 iks_insert_attrib(gtalk
, "sid", p
->sid
);
1521 iks_insert_attrib(dtmf
, "xmlns", "http://jabber.org/protocol/gtalk/info/dtmf");
1522 iks_insert_attrib(dtmf
, "code", buffer
);
1523 iks_insert_node(iq
, gtalk
);
1524 iks_insert_node(gtalk
, dtmf
);
1526 ast_mutex_lock(&p
->lock
);
1527 if (ast
->dtmff
.frametype
== AST_FRAME_DTMF_BEGIN
|| duration
== 0) {
1528 iks_insert_attrib(dtmf
, "action", "button-down");
1529 } else if (ast
->dtmff
.frametype
== AST_FRAME_DTMF_END
|| duration
!= 0) {
1530 iks_insert_attrib(dtmf
, "action", "button-up");
1532 iks_send(client
->connection
->p
, iq
);
1536 ast_mutex_unlock(&p
->lock
);
1540 static int gtalk_sendhtml(struct ast_channel
*ast
, int subclass
, const char *data
, int datalen
)
1542 ast_log(LOG_NOTICE
, "XXX Implement gtalk sendhtml XXX\n");
1547 /* Not in use right now.
1548 static int gtalk_auto_congest(void *nothing)
1550 struct gtalk_pvt *p = nothing;
1552 ast_mutex_lock(&p->lock);
1554 if (!ast_channel_trylock(p->owner)) {
1555 ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
1556 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
1557 ast_channel_unlock(p->owner);
1560 ast_mutex_unlock(&p->lock);
1565 /*! \brief Initiate new call, part of PBX interface
1566 * dest is the dial string */
1567 static int gtalk_call(struct ast_channel
*ast
, char *dest
, int timeout
)
1569 struct gtalk_pvt
*p
= ast
->tech_pvt
;
1571 if ((ast
->_state
!= AST_STATE_DOWN
) && (ast
->_state
!= AST_STATE_RESERVED
)) {
1572 ast_log(LOG_WARNING
, "gtalk_call called on %s, neither down nor reserved\n", ast
->name
);
1576 ast_setstate(ast
, AST_STATE_RING
);
1578 ast_copy_string(p
->ring
, p
->parent
->connection
->mid
, sizeof(p
->ring
));
1579 p
->ringrule
= iks_filter_add_rule(p
->parent
->connection
->f
, gtalk_ringing_ack
, p
,
1580 IKS_RULE_ID
, p
->ring
, IKS_RULE_DONE
);
1582 ast_log(LOG_WARNING
, "Whoa, already have a ring rule!\n");
1584 gtalk_invite(p
, p
->them
, p
->us
, p
->sid
, 1);
1585 gtalk_create_candidates(p
->parent
, p
, p
->sid
, p
->them
, p
->us
);
1590 /*! \brief Hangup a call through the gtalk proxy channel */
1591 static int gtalk_hangup(struct ast_channel
*ast
)
1593 struct gtalk_pvt
*p
= ast
->tech_pvt
;
1594 struct gtalk
*client
;
1596 ast_mutex_lock(&p
->lock
);
1599 ast
->tech_pvt
= NULL
;
1600 if (!p
->alreadygone
)
1601 gtalk_action(client
, p
, "terminate");
1602 ast_mutex_unlock(&p
->lock
);
1604 gtalk_free_pvt(client
, p
);
1605 ast_module_unref(ast_module_info
->self
);
1610 /*! \brief Part of PBX interface */
1611 static struct ast_channel
*gtalk_request(const char *type
, int format
, void *data
, int *cause
)
1613 struct gtalk_pvt
*p
= NULL
;
1614 struct gtalk
*client
= NULL
;
1615 char *sender
= NULL
, *to
= NULL
, *s
= NULL
;
1616 struct ast_channel
*chan
= NULL
;
1619 s
= ast_strdupa(data
);
1621 sender
= strsep(&s
, "/");
1622 if (sender
&& (sender
[0] != '\0'))
1623 to
= strsep(&s
, "/");
1625 ast_log(LOG_ERROR
, "Bad arguments in Gtalk Dialstring: %s\n", (char*) data
);
1630 client
= find_gtalk(to
, sender
);
1632 ast_log(LOG_WARNING
, "Could not find recipient.\n");
1635 ASTOBJ_WRLOCK(client
);
1636 p
= gtalk_alloc(client
, strchr(sender
, '@') ? sender
: client
->connection
->jid
->full
, strchr(to
, '@') ? to
: client
->user
, NULL
);
1638 chan
= gtalk_new(client
, p
, AST_STATE_DOWN
, to
);
1640 ASTOBJ_UNLOCK(client
);
1644 /*! \brief CLI command "gtalk show channels" */
1645 static int gtalk_show_channels(int fd
, int argc
, char **argv
)
1647 #define FORMAT "%-30.30s %-30.30s %-15.15s %-5.5s %-5.5s \n"
1648 struct gtalk_pvt
*p
;
1649 struct ast_channel
*chan
;
1651 char them
[AJI_MAX_JIDLEN
];
1653 char *resource
= NULL
;
1656 return RESULT_SHOWUSAGE
;
1658 ast_mutex_lock(>alklock
);
1659 ast_cli(fd
, FORMAT
, "Channel", "Jabber ID", "Resource", "Read", "Write");
1660 ASTOBJ_CONTAINER_TRAVERSE(>alk_list
, 1, {
1661 ASTOBJ_WRLOCK(iterator
);
1665 ast_copy_string(them
, p
->them
, sizeof(them
));
1667 resource
= strchr(them
, '/');
1679 ast_getformatname(chan
->readformat
),
1680 ast_getformatname(chan
->writeformat
)
1683 ast_log(LOG_WARNING
, "No available channel\n");
1687 ASTOBJ_UNLOCK(iterator
);
1690 ast_mutex_unlock(>alklock
);
1692 ast_cli(fd
, "%d active gtalk channel%s\n", numchans
, (numchans
!= 1) ? "s" : "");
1693 return RESULT_SUCCESS
;
1697 /*! \brief CLI command "gtalk show channels" */
1698 static int gtalk_do_reload(int fd
, int argc
, char **argv
)
1700 ast_verbose("IT DOES WORK!\n");
1701 return RESULT_SUCCESS
;
1704 static int gtalk_parser(void *data
, ikspak
*pak
)
1706 struct gtalk
*client
= ASTOBJ_REF((struct gtalk
*) data
);
1708 if (iks_find_with_attrib(pak
->x
, "session", "type", "initiate")) {
1710 gtalk_newcall(client
, pak
);
1711 } else if (iks_find_with_attrib(pak
->x
, "session", "type", "candidates") || iks_find_with_attrib(pak
->x
, "session", "type", "transport-info")) {
1712 if (option_debug
> 2)
1713 ast_log(LOG_DEBUG
, "About to add candidate!\n");
1714 gtalk_add_candidate(client
, pak
);
1715 if (option_debug
> 2)
1716 ast_log(LOG_DEBUG
, "Candidate Added!\n");
1717 } else if (iks_find_with_attrib(pak
->x
, "session", "type", "accept")) {
1718 gtalk_is_answered(client
, pak
);
1719 } else if (iks_find_with_attrib(pak
->x
, "session", "type", "transport-accept")) {
1720 gtalk_is_accepted(client
, pak
);
1721 } else if (iks_find_with_attrib(pak
->x
, "session", "type", "content-info") || iks_find_with_attrib(pak
->x
, "gtalk", "action", "session-info")) {
1722 gtalk_handle_dtmf(client
, pak
);
1723 } else if (iks_find_with_attrib(pak
->x
, "session", "type", "terminate")) {
1724 gtalk_hangup_farend(client
, pak
);
1725 } else if (iks_find_with_attrib(pak
->x
, "session", "type", "reject")) {
1726 gtalk_hangup_farend(client
, pak
);
1728 ASTOBJ_UNREF(client
, gtalk_member_destroy
);
1729 return IKS_FILTER_EAT
;
1732 /* Not using this anymore probably take out soon
1733 static struct gtalk_candidate *gtalk_create_candidate(char *args)
1735 char *name, *type, *preference, *protocol;
1736 struct gtalk_candidate *res;
1737 res = malloc(sizeof(struct gtalk_candidate));
1738 memset(res, 0, sizeof(struct gtalk_candidate));
1741 if ((args = strchr(args, ','))) {
1746 if ((args = strchr(args, ','))) {
1751 if ((args = strchr(args, ','))) {
1757 ast_copy_string(res->name, name, sizeof(res->name));
1759 res->preference = atof(preference);
1762 if (!strcasecmp("udp", protocol))
1763 res->protocol = AJI_PROTOCOL_UDP;
1764 if (!strcasecmp("ssltcp", protocol))
1765 res->protocol = AJI_PROTOCOL_SSLTCP;
1768 if (!strcasecmp("stun", type))
1769 res->type = AJI_CONNECT_STUN;
1770 if (!strcasecmp("local", type))
1771 res->type = AJI_CONNECT_LOCAL;
1772 if (!strcasecmp("relay", type))
1773 res->type = AJI_CONNECT_RELAY;
1780 static int gtalk_create_member(char *label
, struct ast_variable
*var
, int allowguest
,
1781 struct ast_codec_pref prefs
, char *context
,
1782 struct gtalk
*member
)
1784 struct aji_client
*client
;
1787 ast_log(LOG_WARNING
, "Out of memory.\n");
1789 ast_copy_string(member
->name
, label
, sizeof(member
->name
));
1790 ast_copy_string(member
->user
, label
, sizeof(member
->user
));
1791 ast_copy_string(member
->context
, context
, sizeof(member
->context
));
1792 member
->allowguest
= allowguest
;
1793 member
->prefs
= prefs
;
1796 struct gtalk_candidate
*candidate
= NULL
;
1798 if (!strcasecmp(var
->name
, "username"))
1799 ast_copy_string(member
->user
, var
->value
, sizeof(member
->user
));
1800 else if (!strcasecmp(var
->name
, "disallow"))
1801 ast_parse_allow_disallow(&member
->prefs
, &member
->capability
, var
->value
, 0);
1802 else if (!strcasecmp(var
->name
, "allow"))
1803 ast_parse_allow_disallow(&member
->prefs
, &member
->capability
, var
->value
, 1);
1804 else if (!strcasecmp(var
->name
, "context"))
1805 ast_copy_string(member
->context
, var
->value
, sizeof(member
->context
));
1807 else if (!strcasecmp(var
->name
, "candidate")) {
1808 candidate
= gtalk_create_candidate(var
->value
);
1810 candidate
->next
= member
->ourcandidates
;
1811 member
->ourcandidates
= candidate
;
1815 else if (!strcasecmp(var
->name
, "connection")) {
1816 if ((client
= ast_aji_get_client(var
->value
))) {
1817 member
->connection
= client
;
1818 iks_filter_add_rule(client
->f
, gtalk_parser
, member
,
1819 IKS_RULE_TYPE
, IKS_PAK_IQ
,
1820 IKS_RULE_FROM_PARTIAL
, member
->user
,
1821 IKS_RULE_NS
, "http://www.google.com/session",
1825 ast_log(LOG_ERROR
, "connection referenced not found!\n");
1831 if (member
->connection
&& member
->user
)
1832 member
->buddy
= ASTOBJ_CONTAINER_FIND(&member
->connection
->buddies
, member
->user
);
1834 ast_log(LOG_ERROR
, "No Connection or Username!\n");
1839 static int gtalk_load_config(void)
1842 struct ast_config
*cfg
= NULL
;
1843 char context
[AST_MAX_CONTEXT
];
1845 struct ast_variable
*var
;
1846 struct gtalk
*member
;
1847 struct ast_codec_pref prefs
;
1848 struct aji_client_container
*clients
;
1849 struct gtalk_candidate
*global_candidates
= NULL
;
1851 struct ast_hostent ahp
;
1853 cfg
= ast_config_load(GOOGLE_CONFIG
);
1857 /* Copy the default jb config over global_jbconf */
1858 memcpy(&global_jbconf
, &default_jbconf
, sizeof(struct ast_jb_conf
));
1860 cat
= ast_category_browse(cfg
, NULL
);
1861 for (var
= ast_variable_browse(cfg
, "general"); var
; var
= var
->next
) {
1862 /* handle jb conf */
1863 if (!ast_jb_read_conf(&global_jbconf
, var
->name
, var
->value
))
1866 if (!strcasecmp(var
->name
, "allowguest"))
1868 (ast_true(ast_variable_retrieve(cfg
, "general", "allowguest"))) ? 1 : 0;
1869 else if (!strcasecmp(var
->name
, "disallow"))
1870 ast_parse_allow_disallow(&prefs
, &global_capability
, var
->value
, 0);
1871 else if (!strcasecmp(var
->name
, "allow"))
1872 ast_parse_allow_disallow(&prefs
, &global_capability
, var
->value
, 1);
1873 else if (!strcasecmp(var
->name
, "context"))
1874 ast_copy_string(context
, var
->value
, sizeof(context
));
1875 else if (!strcasecmp(var
->name
, "bindaddr")) {
1876 if (!(hp
= ast_gethostbyname(var
->value
, &ahp
))) {
1877 ast_log(LOG_WARNING
, "Invalid address: %s\n", var
->value
);
1879 memcpy(&bindaddr
.sin_addr
, hp
->h_addr
, sizeof(bindaddr
.sin_addr
));
1882 /* Idea to allow for custom candidates */
1884 else if (!strcasecmp(var->name, "candidate")) {
1885 candidate = gtalk_create_candidate(var->value);
1887 candidate->next = global_candidates;
1888 global_candidates = candidate;
1894 if (strcasecmp(cat
, "general")) {
1895 var
= ast_variable_browse(cfg
, cat
);
1896 member
= (struct gtalk
*) malloc(sizeof(struct gtalk
));
1897 memset(member
, 0, sizeof(struct gtalk
));
1898 ASTOBJ_INIT(member
);
1899 ASTOBJ_WRLOCK(member
);
1900 if (!strcasecmp(cat
, "guest")) {
1901 ast_copy_string(member
->name
, "guest", sizeof(member
->name
));
1902 ast_copy_string(member
->user
, "guest", sizeof(member
->user
));
1903 ast_copy_string(member
->context
, context
, sizeof(member
->context
));
1904 member
->allowguest
= allowguest
;
1905 member
->prefs
= prefs
;
1907 if (!strcasecmp(var
->name
, "disallow"))
1908 ast_parse_allow_disallow(&member
->prefs
, &member
->capability
,
1910 else if (!strcasecmp(var
->name
, "allow"))
1911 ast_parse_allow_disallow(&member
->prefs
, &member
->capability
,
1913 else if (!strcasecmp(var
->name
, "context"))
1914 ast_copy_string(member
->context
, var
->value
,
1915 sizeof(member
->context
));
1916 /* Idea to allow for custom candidates */
1918 else if (!strcasecmp(var->name, "candidate")) {
1919 candidate = gtalk_create_candidate(var->value);
1921 candidate->next = member->ourcandidates;
1922 member->ourcandidates = candidate;
1928 ASTOBJ_UNLOCK(member
);
1929 clients
= ast_aji_get_clients();
1931 ASTOBJ_CONTAINER_TRAVERSE(clients
, 1, {
1932 ASTOBJ_WRLOCK(iterator
);
1933 ASTOBJ_WRLOCK(member
);
1934 member
->connection
= iterator
;
1935 iks_filter_add_rule(iterator
->f
, gtalk_parser
, member
, IKS_RULE_TYPE
, IKS_PAK_IQ
, IKS_RULE_NS
, "http://www.google.com/session", IKS_RULE_DONE
);
1936 iks_filter_add_rule(iterator
->f
, gtalk_parser
, member
, IKS_RULE_TYPE
, IKS_PAK_IQ
, IKS_RULE_NS
, "http://jabber.org/protocol/gtalk", IKS_RULE_DONE
);
1937 ASTOBJ_UNLOCK(member
);
1938 ASTOBJ_CONTAINER_LINK(>alk_list
, member
);
1939 ASTOBJ_UNLOCK(iterator
);
1942 ASTOBJ_UNLOCK(member
);
1943 ASTOBJ_UNREF(member
, gtalk_member_destroy
);
1946 ASTOBJ_UNLOCK(member
);
1947 if (gtalk_create_member(cat
, var
, allowguest
, prefs
, context
, member
))
1948 ASTOBJ_CONTAINER_LINK(>alk_list
, member
);
1949 ASTOBJ_UNREF(member
, gtalk_member_destroy
);
1952 cat
= ast_category_browse(cfg
, cat
);
1954 gtalk_free_candidates(global_candidates
);
1958 /*! \brief Load module into PBX, register channel */
1959 static int load_module(void)
1961 char *jabber_loaded
= ast_module_helper("", "res_jabber.so", 0, 0, 0, 0);
1962 free(jabber_loaded
);
1963 if (!jabber_loaded
) {
1964 /* If embedded, check for a different module name */
1965 jabber_loaded
= ast_module_helper("", "res_jabber", 0, 0, 0, 0);
1966 free(jabber_loaded
);
1967 if (!jabber_loaded
) {
1968 ast_log(LOG_ERROR
, "chan_gtalk.so depends upon res_jabber.so\n");
1969 return AST_MODULE_LOAD_DECLINE
;
1974 gcry_control (GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
1975 #endif /* HAVE_GNUTLS */
1977 ASTOBJ_CONTAINER_INIT(>alk_list
);
1978 if (!gtalk_load_config()) {
1979 ast_log(LOG_ERROR
, "Unable to read config file %s. Not loading module.\n", GOOGLE_CONFIG
);
1983 sched
= sched_context_create();
1985 ast_log(LOG_WARNING
, "Unable to create schedule context\n");
1987 io
= io_context_create();
1989 ast_log(LOG_WARNING
, "Unable to create I/O context\n");
1991 if (ast_find_ourip(&__ourip
, bindaddr
)) {
1992 ast_log(LOG_WARNING
, "Unable to get own IP address, Gtalk disabled\n");
1996 ast_rtp_proto_register(>alk_rtp
);
1997 ast_cli_register_multiple(gtalk_cli
, sizeof(gtalk_cli
) / sizeof(gtalk_cli
[0]));
1999 /* Make sure we can register our channel type */
2000 if (ast_channel_register(>alk_tech
)) {
2001 ast_log(LOG_ERROR
, "Unable to register channel class %s\n", gtalk_tech
.type
);
2007 /*! \brief Reload module */
2008 static int reload(void)
2013 /*! \brief Unload the gtalk channel from Asterisk */
2014 static int unload_module(void)
2016 struct gtalk_pvt
*privates
= NULL
;
2017 ast_cli_unregister_multiple(gtalk_cli
, sizeof(gtalk_cli
) / sizeof(gtalk_cli
[0]));
2018 /* First, take us out of the channel loop */
2019 ast_channel_unregister(>alk_tech
);
2020 ast_rtp_proto_unregister(>alk_rtp
);
2022 if (!ast_mutex_lock(>alklock
)) {
2023 /* Hangup all interfaces if they have an owner */
2024 ASTOBJ_CONTAINER_TRAVERSE(>alk_list
, 1, {
2025 ASTOBJ_WRLOCK(iterator
);
2026 privates
= iterator
->p
;
2028 if (privates
->owner
)
2029 ast_softhangup(privates
->owner
, AST_SOFTHANGUP_APPUNLOAD
);
2030 privates
= privates
->next
;
2033 ASTOBJ_UNLOCK(iterator
);
2035 ast_mutex_unlock(>alklock
);
2037 ast_log(LOG_WARNING
, "Unable to lock the monitor\n");
2040 ASTOBJ_CONTAINER_DESTROYALL(>alk_list
, gtalk_member_destroy
);
2041 ASTOBJ_CONTAINER_DESTROY(>alk_list
);
2045 AST_MODULE_INFO(ASTERISK_GPL_KEY
, AST_MODFLAG_DEFAULT
, "Gtalk Channel Driver",
2046 .load
= load_module
,
2047 .unload
= unload_module
,