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$")
38 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #include <sys/signal.h>
47 #include "asterisk/lock.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/config.h"
50 #include "asterisk/module.h"
51 #include "asterisk/pbx.h"
52 #include "asterisk/sched.h"
53 #include "asterisk/io.h"
54 #include "asterisk/rtp.h"
55 #include "asterisk/acl.h"
56 #include "asterisk/callerid.h"
57 #include "asterisk/file.h"
58 #include "asterisk/cli.h"
59 #include "asterisk/app.h"
60 #include "asterisk/musiconhold.h"
61 #include "asterisk/manager.h"
62 #include "asterisk/stringfields.h"
63 #include "asterisk/utils.h"
64 #include "asterisk/causes.h"
65 #include "asterisk/astobj.h"
66 #include "asterisk/abstract_jb.h"
67 #include "asterisk/jabber.h"
69 #define GOOGLE_CONFIG "gtalk.conf"
71 #define GOOGLE_NS "http://www.google.com/session"
74 /*! Global jitterbuffer configuration - by default, jb is disabled */
75 static struct ast_jb_conf default_jbconf
=
79 .resync_threshold
= -1,
82 static struct ast_jb_conf global_jbconf
;
86 AJI_PROTOCOL_SSLTCP
= 2,
89 enum gtalk_connect_type
{
91 AJI_CONNECT_LOCAL
= 2,
92 AJI_CONNECT_RELAY
= 3,
96 ast_mutex_t lock
; /*!< Channel private lock */
98 struct gtalk
*parent
; /*!< Parent client */
100 char us
[AJI_MAX_JIDLEN
];
101 char them
[AJI_MAX_JIDLEN
];
102 char ring
[10]; /*!< Message ID of ring */
103 iksrule
*ringrule
; /*!< Rule for matching RING request */
104 int initiator
; /*!< If we're the initiator */
107 struct ast_codec_pref prefs
;
108 struct gtalk_candidate
*theircandidates
;
109 struct gtalk_candidate
*ourcandidates
;
110 char cid_num
[80]; /*!< Caller ID num */
111 char cid_name
[80]; /*!< Caller ID name */
112 char exten
[80]; /*!< Called extension */
113 struct ast_channel
*owner
; /*!< Master Channel */
114 struct ast_rtp
*rtp
; /*!< RTP audio session */
115 struct ast_rtp
*vrtp
; /*!< RTP video session */
116 int jointcapability
; /*!< Supported capability at both ends (codecs ) */
118 struct gtalk_pvt
*next
; /* Next entity */
121 struct gtalk_candidate
{
123 enum gtalk_protocol protocol
;
127 enum gtalk_connect_type type
;
133 struct gtalk_candidate
*next
;
137 ASTOBJ_COMPONENTS(struct gtalk
);
138 struct aji_client
*connection
;
139 struct aji_buddy
*buddy
;
141 struct ast_codec_pref prefs
;
142 int amaflags
; /*!< AMA Flags */
143 char user
[AJI_MAX_JIDLEN
];
144 char context
[AST_MAX_CONTEXT
];
145 char parkinglot
[AST_MAX_CONTEXT
]; /*!< Parkinglot */
146 char accountcode
[AST_MAX_ACCOUNT_CODE
]; /*!< Account code */
148 ast_group_t callgroup
; /*!< Call group */
149 ast_group_t pickupgroup
; /*!< Pickup group */
150 int callingpres
; /*!< Calling presentation */
152 char language
[MAX_LANGUAGE
]; /*!< Default language for prompts */
153 char musicclass
[MAX_MUSICCLASS
]; /*!< Music on Hold class */
156 struct gtalk_container
{
157 ASTOBJ_CONTAINER_COMPONENTS(struct gtalk
);
160 static const char desc
[] = "Gtalk Channel";
162 static int global_capability
= AST_FORMAT_ULAW
| AST_FORMAT_ALAW
| AST_FORMAT_GSM
| AST_FORMAT_H263
;
164 AST_MUTEX_DEFINE_STATIC(gtalklock
); /*!< Protect the interface list (of gtalk_pvt's) */
166 /* Forward declarations */
167 static struct ast_channel
*gtalk_request(const char *type
, int format
, void *data
, int *cause
);
168 static int gtalk_digit(struct ast_channel
*ast
, char digit
, unsigned int duration
);
169 static int gtalk_digit_begin(struct ast_channel
*ast
, char digit
);
170 static int gtalk_digit_end(struct ast_channel
*ast
, char digit
, unsigned int duration
);
171 static int gtalk_call(struct ast_channel
*ast
, char *dest
, int timeout
);
172 static int gtalk_hangup(struct ast_channel
*ast
);
173 static int gtalk_answer(struct ast_channel
*ast
);
174 static int gtalk_action(struct gtalk
*client
, struct gtalk_pvt
*p
, const char *action
);
175 static void gtalk_free_pvt(struct gtalk
*client
, struct gtalk_pvt
*p
);
176 static int gtalk_newcall(struct gtalk
*client
, ikspak
*pak
);
177 static struct ast_frame
*gtalk_read(struct ast_channel
*ast
);
178 static int gtalk_write(struct ast_channel
*ast
, struct ast_frame
*f
);
179 static int gtalk_indicate(struct ast_channel
*ast
, int condition
, const void *data
, size_t datalen
);
180 static int gtalk_fixup(struct ast_channel
*oldchan
, struct ast_channel
*newchan
);
181 static int gtalk_sendhtml(struct ast_channel
*ast
, int subclass
, const char *data
, int datalen
);
182 static struct gtalk_pvt
*gtalk_alloc(struct gtalk
*client
, const char *us
, const char *them
, const char *sid
);
183 static char *gtalk_do_reload(struct ast_cli_entry
*e
, int cmd
, struct ast_cli_args
*a
);
184 static char *gtalk_show_channels(struct ast_cli_entry
*e
, int cmd
, struct ast_cli_args
*a
);
185 /*----- RTP interface functions */
186 static int gtalk_set_rtp_peer(struct ast_channel
*chan
, struct ast_rtp
*rtp
,
187 struct ast_rtp
*vrtp
, struct ast_rtp
*trtp
, int codecs
, int nat_active
);
188 static enum ast_rtp_get_result
gtalk_get_rtp_peer(struct ast_channel
*chan
, struct ast_rtp
**rtp
);
189 static int gtalk_get_codec(struct ast_channel
*chan
);
191 /*! \brief PBX interface structure for channel registration */
192 static const struct ast_channel_tech gtalk_tech
= {
194 .description
= "Gtalk Channel Driver",
195 .capabilities
= AST_FORMAT_AUDIO_MASK
,
196 .requester
= gtalk_request
,
197 .send_digit_begin
= gtalk_digit_begin
,
198 .send_digit_end
= gtalk_digit_end
,
199 .bridge
= ast_rtp_bridge
,
201 .hangup
= gtalk_hangup
,
202 .answer
= gtalk_answer
,
204 .write
= gtalk_write
,
205 .exception
= gtalk_read
,
206 .indicate
= gtalk_indicate
,
207 .fixup
= gtalk_fixup
,
208 .send_html
= gtalk_sendhtml
,
209 .properties
= AST_CHAN_TP_WANTSJITTER
| AST_CHAN_TP_CREATESJITTER
212 static struct sockaddr_in bindaddr
= { 0, }; /*!< The address we bind to */
214 static struct sched_context
*sched
; /*!< The scheduling context */
215 static struct io_context
*io
; /*!< The IO context */
216 static struct in_addr __ourip
;
218 /*! \brief RTP driver interface */
219 static struct ast_rtp_protocol gtalk_rtp
= {
221 get_rtp_info
: gtalk_get_rtp_peer
,
222 set_rtp_peer
: gtalk_set_rtp_peer
,
223 get_codec
: gtalk_get_codec
,
226 static struct ast_cli_entry gtalk_cli
[] = {
227 AST_CLI_DEFINE(gtalk_do_reload
, "Reload GoogleTalk configuration"),
228 AST_CLI_DEFINE(gtalk_show_channels
, "Show GoogleTalk channels"),
231 static char externip
[16];
233 static struct gtalk_container gtalk_list
;
235 static void gtalk_member_destroy(struct gtalk
*obj
)
240 static struct gtalk
*find_gtalk(char *name
, char *connection
)
242 struct gtalk
*gtalk
= NULL
;
243 char *domain
= NULL
, *s
= NULL
;
245 if (strchr(connection
, '@')) {
246 s
= ast_strdupa(connection
);
247 domain
= strsep(&s
, "@");
248 ast_verbose("OOOOH domain = %s\n", domain
);
250 gtalk
= ASTOBJ_CONTAINER_FIND(>alk_list
, name
);
251 if (!gtalk
&& strchr(name
, '@'))
252 gtalk
= ASTOBJ_CONTAINER_FIND_FULL(>alk_list
, name
, user
,,, strcasecmp
);
256 ASTOBJ_CONTAINER_TRAVERSE(>alk_list
, 1, {
257 ASTOBJ_RDLOCK(iterator
);
258 if (!strcasecmp(iterator
->name
, "guest")) {
261 ASTOBJ_UNLOCK(iterator
);
272 static int add_codec_to_answer(const struct gtalk_pvt
*p
, int codec
, iks
*dcodecs
)
275 char *format
= ast_getformatname(codec
);
277 if (!strcasecmp("ulaw", format
)) {
278 iks
*payload_eg711u
, *payload_pcmu
;
279 payload_pcmu
= iks_new("payload-type");
280 payload_eg711u
= iks_new("payload-type");
282 if(!payload_eg711u
|| !payload_pcmu
) {
283 iks_delete(payload_pcmu
);
284 iks_delete(payload_eg711u
);
285 ast_log(LOG_WARNING
,"Failed to allocate iks node");
288 iks_insert_attrib(payload_pcmu
, "id", "0");
289 iks_insert_attrib(payload_pcmu
, "name", "PCMU");
290 iks_insert_attrib(payload_pcmu
, "clockrate","8000");
291 iks_insert_attrib(payload_pcmu
, "bitrate","64000");
292 iks_insert_attrib(payload_eg711u
, "id", "100");
293 iks_insert_attrib(payload_eg711u
, "name", "EG711U");
294 iks_insert_attrib(payload_eg711u
, "clockrate","8000");
295 iks_insert_attrib(payload_eg711u
, "bitrate","64000");
296 iks_insert_node(dcodecs
, payload_pcmu
);
297 iks_insert_node(dcodecs
, payload_eg711u
);
300 if (!strcasecmp("alaw", format
)) {
301 iks
*payload_eg711a
, *payload_pcma
;
302 payload_pcma
= iks_new("payload-type");
303 payload_eg711a
= iks_new("payload-type");
304 if(!payload_eg711a
|| !payload_pcma
) {
305 iks_delete(payload_eg711a
);
306 iks_delete(payload_pcma
);
307 ast_log(LOG_WARNING
,"Failed to allocate iks node");
310 iks_insert_attrib(payload_pcma
, "id", "8");
311 iks_insert_attrib(payload_pcma
, "name", "PCMA");
312 iks_insert_attrib(payload_pcma
, "clockrate","8000");
313 iks_insert_attrib(payload_pcma
, "bitrate","64000");
314 payload_eg711a
= iks_new("payload-type");
315 iks_insert_attrib(payload_eg711a
, "id", "101");
316 iks_insert_attrib(payload_eg711a
, "name", "EG711A");
317 iks_insert_attrib(payload_eg711a
, "clockrate","8000");
318 iks_insert_attrib(payload_eg711a
, "bitrate","64000");
319 iks_insert_node(dcodecs
, payload_pcma
);
320 iks_insert_node(dcodecs
, payload_eg711a
);
323 if (!strcasecmp("ilbc", format
)) {
324 iks
*payload_ilbc
= iks_new("payload-type");
326 ast_log(LOG_WARNING
,"Failed to allocate iks node");
329 iks_insert_attrib(payload_ilbc
, "id", "97");
330 iks_insert_attrib(payload_ilbc
, "name", "iLBC");
331 iks_insert_attrib(payload_ilbc
, "clockrate","8000");
332 iks_insert_attrib(payload_ilbc
, "bitrate","13300");
333 iks_insert_node(dcodecs
, payload_ilbc
);
336 if (!strcasecmp("g723", format
)) {
337 iks
*payload_g723
= iks_new("payload-type");
339 ast_log(LOG_WARNING
,"Failed to allocate iks node");
342 iks_insert_attrib(payload_g723
, "id", "4");
343 iks_insert_attrib(payload_g723
, "name", "G723");
344 iks_insert_attrib(payload_g723
, "clockrate","8000");
345 iks_insert_attrib(payload_g723
, "bitrate","6300");
346 iks_insert_node(dcodecs
, payload_g723
);
349 if (!strcasecmp("speex", format
)) {
350 iks
*payload_speex
= iks_new("payload-type");
352 ast_log(LOG_WARNING
,"Failed to allocate iks node");
355 iks_insert_attrib(payload_speex
, "id", "110");
356 iks_insert_attrib(payload_speex
, "name", "speex");
357 iks_insert_attrib(payload_speex
, "clockrate","8000");
358 iks_insert_attrib(payload_speex
, "bitrate","11000");
359 iks_insert_node(dcodecs
, payload_speex
);
362 if (!strcasecmp("gsm", format
)) {
363 iks
*payload_gsm
= iks_new("payload-type");
365 ast_log(LOG_WARNING
,"Failed to allocate iks node");
368 iks_insert_attrib(payload_gsm
, "id", "103");
369 iks_insert_attrib(payload_gsm
, "name", "gsm");
370 iks_insert_node(dcodecs
, payload_gsm
);
373 ast_rtp_lookup_code(p
->rtp
, 1, codec
);
377 static int gtalk_invite(struct gtalk_pvt
*p
, char *to
, char *from
, char *sid
, int initiator
)
379 struct gtalk
*client
= p
->parent
;
380 iks
*iq
, *gtalk
, *dcodecs
, *payload_telephone
, *transport
;
387 gtalk
= iks_new("session");
388 dcodecs
= iks_new("description");
389 transport
= iks_new("transport");
390 payload_telephone
= iks_new("payload-type");
391 if (!(iq
&& gtalk
&& dcodecs
&& transport
&& payload_telephone
)){
395 iks_delete(transport
);
396 iks_delete(payload_telephone
);
398 ast_log(LOG_ERROR
, "Could not allocate iksemel nodes\n");
401 iks_insert_attrib(dcodecs
, "xmlns", "http://www.google.com/session/phone");
402 iks_insert_attrib(dcodecs
, "xml:lang", "en");
404 for (x
= 0; x
< 32; x
++) {
405 if (!(pref_codec
= ast_codec_pref_index(&client
->prefs
, x
)))
407 if (!(client
->capability
& pref_codec
))
409 if (alreadysent
& pref_codec
)
411 codecs_num
= add_codec_to_answer(p
, pref_codec
, dcodecs
);
412 alreadysent
|= pref_codec
;
416 /* only propose DTMF within an audio session */
417 iks_insert_attrib(payload_telephone
, "id", "106");
418 iks_insert_attrib(payload_telephone
, "name", "telephone-event");
419 iks_insert_attrib(payload_telephone
, "clockrate", "8000");
421 iks_insert_attrib(transport
,"xmlns","http://www.google.com/transport/p2p");
423 iks_insert_attrib(iq
, "type", "set");
424 iks_insert_attrib(iq
, "to", to
);
425 iks_insert_attrib(iq
, "from", from
);
426 iks_insert_attrib(iq
, "id", client
->connection
->mid
);
427 ast_aji_increment_mid(client
->connection
->mid
);
429 iks_insert_attrib(gtalk
, "xmlns", "http://www.google.com/session");
430 iks_insert_attrib(gtalk
, "type",initiator
? "initiate": "accept");
431 iks_insert_attrib(gtalk
, "initiator", initiator
? from
: to
);
432 iks_insert_attrib(gtalk
, "id", sid
);
433 iks_insert_node(iq
, gtalk
);
434 iks_insert_node(gtalk
, dcodecs
);
435 iks_insert_node(gtalk
, transport
);
436 iks_insert_node(dcodecs
, payload_telephone
);
438 ast_aji_send(client
->connection
, iq
);
440 iks_delete(payload_telephone
);
441 iks_delete(transport
);
448 static int gtalk_invite_response(struct gtalk_pvt
*p
, char *to
, char *from
, char *sid
, int initiator
)
450 iks
*iq
, *session
, *transport
;
452 session
= iks_new("session");
453 transport
= iks_new("transport");
454 if(!(iq
&& session
&& transport
)) {
457 iks_delete(transport
);
458 ast_log(LOG_ERROR
, " Unable to allocate IKS node\n");
461 iks_insert_attrib(iq
, "from", from
);
462 iks_insert_attrib(iq
, "to", to
);
463 iks_insert_attrib(iq
, "type", "set");
464 iks_insert_attrib(iq
, "id",p
->parent
->connection
->mid
);
465 ast_aji_increment_mid(p
->parent
->connection
->mid
);
466 iks_insert_attrib(session
, "type", "transport-accept");
467 iks_insert_attrib(session
, "id", sid
);
468 iks_insert_attrib(session
, "initiator", initiator
? from
: to
);
469 iks_insert_attrib(session
, "xmlns", "http://www.google.com/session");
470 iks_insert_attrib(transport
, "xmlns", "http://www.google.com/transport/p2p");
471 iks_insert_node(iq
,session
);
472 iks_insert_node(session
,transport
);
473 ast_aji_send(p
->parent
->connection
, iq
);
475 iks_delete(transport
);
482 static int gtalk_ringing_ack(void *data
, ikspak
*pak
)
484 struct gtalk_pvt
*p
= data
;
487 iks_filter_remove_rule(p
->parent
->connection
->f
, p
->ringrule
);
490 ast_queue_control(p
->owner
, AST_CONTROL_RINGING
);
491 return IKS_FILTER_EAT
;
494 static int gtalk_answer(struct ast_channel
*ast
)
496 struct gtalk_pvt
*p
= ast
->tech_pvt
;
499 ast_debug(1, "Answer!\n");
500 ast_mutex_lock(&p
->lock
);
501 gtalk_invite(p
, p
->them
, p
->us
,p
->sid
, 0);
502 manager_event(EVENT_FLAG_SYSTEM
, "ChannelUpdate", "Channel: %s\r\nChanneltype: %s\r\nGtalk-SID: %s\r\n",
503 ast
->name
, "GTALK", p
->sid
);
504 ast_mutex_unlock(&p
->lock
);
508 static enum ast_rtp_get_result
gtalk_get_rtp_peer(struct ast_channel
*chan
, struct ast_rtp
**rtp
)
510 struct gtalk_pvt
*p
= chan
->tech_pvt
;
511 enum ast_rtp_get_result res
= AST_RTP_GET_FAILED
;
516 ast_mutex_lock(&p
->lock
);
519 res
= AST_RTP_TRY_PARTIAL
;
521 ast_mutex_unlock(&p
->lock
);
526 static int gtalk_get_codec(struct ast_channel
*chan
)
528 struct gtalk_pvt
*p
= chan
->tech_pvt
;
529 return p
->peercapability
;
532 static int gtalk_set_rtp_peer(struct ast_channel
*chan
, struct ast_rtp
*rtp
, struct ast_rtp
*vrtp
, struct ast_rtp
*trtp
, int codecs
, int nat_active
)
539 ast_mutex_lock(&p
->lock
);
542 ast_rtp_get_peer(rtp, &p->redirip);
544 memset(&p->redirip, 0, sizeof(p->redirip));
545 p->redircodecs = codecs; */
547 /* Reset lastrtprx timer */
548 ast_mutex_unlock(&p
->lock
);
552 static int gtalk_response(struct gtalk
*client
, char *from
, ikspak
*pak
, const char *reasonstr
, const char *reasonstr2
)
554 iks
*response
= NULL
, *error
= NULL
, *reason
= NULL
;
557 response
= iks_new("iq");
559 iks_insert_attrib(response
, "type", "result");
560 iks_insert_attrib(response
, "from", from
);
561 iks_insert_attrib(response
, "to", iks_find_attrib(pak
->x
, "from"));
562 iks_insert_attrib(response
, "id", iks_find_attrib(pak
->x
, "id"));
564 error
= iks_new("error");
566 iks_insert_attrib(error
, "type", "cancel");
567 reason
= iks_new(reasonstr
);
569 iks_insert_node(error
, reason
);
570 iks_insert_node(response
, error
);
573 ast_aji_send(client
->connection
, response
);
579 iks_delete(response
);
584 static int gtalk_is_answered(struct gtalk
*client
, ikspak
*pak
)
586 struct gtalk_pvt
*tmp
;
589 char s1
[BUFSIZ
], s2
[BUFSIZ
], s3
[BUFSIZ
];
590 int peernoncodeccapability
;
592 ast_log(LOG_DEBUG
, "The client is %s\n", client
->name
);
593 /* Make sure our new call doesn't exist yet */
594 for (tmp
= client
->p
; tmp
; tmp
= tmp
->next
) {
595 if (iks_find_with_attrib(pak
->x
, "session", "id", tmp
->sid
))
599 /* codec points to the first <payload-type/> tag */
600 codec
= iks_child(iks_child(iks_child(pak
->x
)));
602 ast_rtp_set_m_type(tmp
->rtp
, atoi(iks_find_attrib(codec
, "id")));
603 ast_rtp_set_rtpmap_type(tmp
->rtp
, atoi(iks_find_attrib(codec
, "id")), "audio", iks_find_attrib(codec
, "name"), 0);
604 codec
= iks_next(codec
);
607 /* Now gather all of the codecs that we are asked for */
608 ast_rtp_get_current_formats(tmp
->rtp
, &tmp
->peercapability
, &peernoncodeccapability
);
610 /* at this point, we received an awser from the remote Gtalk client,
611 which allows us to compare capabilities */
612 tmp
->jointcapability
= tmp
->capability
& tmp
->peercapability
;
613 if (!tmp
->jointcapability
) {
614 ast_log(LOG_WARNING
, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1
, BUFSIZ
, tmp
->capability
),
615 ast_getformatname_multiple(s2
, BUFSIZ
, tmp
->peercapability
),
616 ast_getformatname_multiple(s3
, BUFSIZ
, tmp
->jointcapability
));
617 /* close session if capabilities don't match */
618 ast_queue_hangup(tmp
->owner
);
624 from
= iks_find_attrib(pak
->x
, "to");
626 from
= client
->connection
->jid
->full
;
630 ast_queue_control(tmp
->owner
, AST_CONTROL_ANSWER
);
632 ast_log(LOG_NOTICE
, "Whoa, didn't find call!\n");
633 gtalk_response(client
, from
, pak
, NULL
, NULL
);
637 static int gtalk_is_accepted(struct gtalk
*client
, ikspak
*pak
)
639 struct gtalk_pvt
*tmp
;
642 ast_log(LOG_DEBUG
, "The client is %s\n", client
->name
);
643 /* find corresponding call */
644 for (tmp
= client
->p
; tmp
; tmp
= tmp
->next
) {
645 if (iks_find_with_attrib(pak
->x
, "session", "id", tmp
->sid
))
649 from
= iks_find_attrib(pak
->x
, "to");
651 from
= client
->connection
->jid
->full
;
654 ast_log(LOG_NOTICE
, "Whoa, didn't find call!\n");
656 /* answer 'iq' packet to let the remote peer know that we're alive */
657 gtalk_response(client
, from
, pak
, NULL
, NULL
);
661 static int gtalk_handle_dtmf(struct gtalk
*client
, ikspak
*pak
)
663 struct gtalk_pvt
*tmp
;
664 iks
*dtmfnode
= NULL
, *dtmfchild
= NULL
;
667 /* Make sure our new call doesn't exist yet */
668 for (tmp
= client
->p
; tmp
; tmp
= tmp
->next
) {
669 if (iks_find_with_attrib(pak
->x
, "session", "id", tmp
->sid
) || iks_find_with_attrib(pak
->x
, "gtalk", "sid", tmp
->sid
))
672 from
= iks_find_attrib(pak
->x
, "to");
674 from
= client
->connection
->jid
->full
;
678 if(iks_find_with_attrib(pak
->x
, "dtmf-method", "method", "rtp")) {
679 gtalk_response(client
, from
, pak
,
680 "feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
681 "unsupported-dtmf-method xmlns='http://jabber.org/protocol/gtalk/info/dtmf#errors'");
684 if ((dtmfnode
= iks_find(pak
->x
, "dtmf"))) {
685 if((dtmf
= iks_find_attrib(dtmfnode
, "code"))) {
686 if(iks_find_with_attrib(pak
->x
, "dtmf", "action", "button-up")) {
687 struct ast_frame f
= {AST_FRAME_DTMF_BEGIN
, };
688 f
.subclass
= dtmf
[0];
689 ast_queue_frame(tmp
->owner
, &f
);
690 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f
.subclass
);
691 } else if(iks_find_with_attrib(pak
->x
, "dtmf", "action", "button-down")) {
692 struct ast_frame f
= {AST_FRAME_DTMF_END
, };
693 f
.subclass
= dtmf
[0];
694 ast_queue_frame(tmp
->owner
, &f
);
695 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f
.subclass
);
696 } else if(iks_find_attrib(pak
->x
, "dtmf")) { /* 250 millasecond default */
697 struct ast_frame f
= {AST_FRAME_DTMF
, };
698 f
.subclass
= dtmf
[0];
699 ast_queue_frame(tmp
->owner
, &f
);
700 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f
.subclass
);
703 } else if ((dtmfnode
= iks_find_with_attrib(pak
->x
, "gtalk", "action", "session-info"))) {
704 if((dtmfchild
= iks_find(dtmfnode
, "dtmf"))) {
705 if((dtmf
= iks_find_attrib(dtmfchild
, "code"))) {
706 if(iks_find_with_attrib(dtmfnode
, "dtmf", "action", "button-up")) {
707 struct ast_frame f
= {AST_FRAME_DTMF_END
, };
708 f
.subclass
= dtmf
[0];
709 ast_queue_frame(tmp
->owner
, &f
);
710 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f
.subclass
);
711 } else if(iks_find_with_attrib(dtmfnode
, "dtmf", "action", "button-down")) {
712 struct ast_frame f
= {AST_FRAME_DTMF_BEGIN
, };
713 f
.subclass
= dtmf
[0];
714 ast_queue_frame(tmp
->owner
, &f
);
715 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f
.subclass
);
720 gtalk_response(client
, from
, pak
, NULL
, NULL
);
723 ast_log(LOG_NOTICE
, "Whoa, didn't find call!\n");
725 gtalk_response(client
, from
, pak
, NULL
, NULL
);
729 static int gtalk_hangup_farend(struct gtalk
*client
, ikspak
*pak
)
731 struct gtalk_pvt
*tmp
;
734 ast_debug(1, "The client is %s\n", client
->name
);
735 /* Make sure our new call doesn't exist yet */
736 for (tmp
= client
->p
; tmp
; tmp
= tmp
->next
) {
737 if (iks_find_with_attrib(pak
->x
, "session", "id", tmp
->sid
))
740 from
= iks_find_attrib(pak
->x
, "to");
742 from
= client
->connection
->jid
->full
;
745 tmp
->alreadygone
= 1;
747 ast_queue_hangup(tmp
->owner
);
749 ast_log(LOG_NOTICE
, "Whoa, didn't find call!\n");
750 gtalk_response(client
, from
, pak
, NULL
, NULL
);
754 static int gtalk_create_candidates(struct gtalk
*client
, struct gtalk_pvt
*p
, char *sid
, char *from
, char *to
)
756 struct gtalk_candidate
*tmp
;
757 struct aji_client
*c
= client
->connection
;
758 struct gtalk_candidate
*ours1
= NULL
, *ours2
= NULL
;
759 struct sockaddr_in sin
;
760 struct sockaddr_in dest
;
762 iks
*iq
, *gtalk
, *candidate
, *transport
;
763 char user
[17], pass
[17], preference
[5], port
[7];
767 gtalk
= iks_new("session");
768 candidate
= iks_new("candidate");
769 transport
= iks_new("transport");
770 if (!iq
|| !gtalk
|| !candidate
|| !transport
) {
771 ast_log(LOG_ERROR
, "Memory allocation error\n");
774 ours1
= ast_calloc(1, sizeof(*ours1
));
775 ours2
= ast_calloc(1, sizeof(*ours2
));
776 if (!ours1
|| !ours2
)
779 iks_insert_attrib(transport
, "xmlns","http://www.google.com/transport/p2p");
780 iks_insert_node(iq
, gtalk
);
781 iks_insert_node(gtalk
,transport
);
782 iks_insert_node(transport
, candidate
);
784 for (; p
; p
= p
->next
) {
785 if (!strcasecmp(p
->sid
, sid
))
790 ast_log(LOG_NOTICE
, "No matching gtalk session - SID %s!\n", sid
);
794 ast_rtp_get_us(p
->rtp
, &sin
);
795 ast_find_ourip(&us
, bindaddr
);
797 /* Setup our gtalk candidates */
798 ast_copy_string(ours1
->name
, "rtp", sizeof(ours1
->name
));
799 ours1
->port
= ntohs(sin
.sin_port
);
800 ours1
->preference
= 1;
801 snprintf(user
, sizeof(user
), "%08lx%08lx", ast_random(), ast_random());
802 snprintf(pass
, sizeof(pass
), "%08lx%08lx", ast_random(), ast_random());
803 ast_copy_string(ours1
->username
, user
, sizeof(ours1
->username
));
804 ast_copy_string(ours1
->password
, pass
, sizeof(ours1
->password
));
805 ast_copy_string(ours1
->ip
, ast_inet_ntoa(us
), sizeof(ours1
->ip
));
806 ours1
->protocol
= AJI_PROTOCOL_UDP
;
807 ours1
->type
= AJI_CONNECT_LOCAL
;
808 ours1
->generation
= 0;
809 p
->ourcandidates
= ours1
;
811 if (!ast_strlen_zero(externip
)) {
812 /* XXX We should really stun for this one not just go with externip XXX */
813 snprintf(user
, sizeof(user
), "%08lx%08lx", ast_random(), ast_random());
814 snprintf(pass
, sizeof(pass
), "%08lx%08lx", ast_random(), ast_random());
815 ast_copy_string(ours2
->username
, user
, sizeof(ours2
->username
));
816 ast_copy_string(ours2
->password
, pass
, sizeof(ours2
->password
));
817 ast_copy_string(ours2
->ip
, externip
, sizeof(ours2
->ip
));
818 ast_copy_string(ours2
->name
, "rtp", sizeof(ours1
->name
));
819 ours2
->port
= ntohs(sin
.sin_port
);
820 ours2
->preference
= 0.9;
821 ours2
->protocol
= AJI_PROTOCOL_UDP
;
822 ours2
->type
= AJI_CONNECT_STUN
;
823 ours2
->generation
= 0;
828 dest
.sin_addr
= __ourip
;
829 dest
.sin_port
= sin
.sin_port
;
832 for (tmp
= p
->ourcandidates
; tmp
; tmp
= tmp
->next
) {
833 snprintf(port
, sizeof(port
), "%d", tmp
->port
);
834 snprintf(preference
, sizeof(preference
), "%.2f", tmp
->preference
);
835 iks_insert_attrib(iq
, "from", to
);
836 iks_insert_attrib(iq
, "to", from
);
837 iks_insert_attrib(iq
, "type", "set");
838 iks_insert_attrib(iq
, "id", c
->mid
);
839 ast_aji_increment_mid(c
->mid
);
840 iks_insert_attrib(gtalk
, "type", "transport-info");
841 iks_insert_attrib(gtalk
, "id", sid
);
842 iks_insert_attrib(gtalk
, "initiator", (p
->initiator
) ? to
: from
);
843 iks_insert_attrib(gtalk
, "xmlns", GOOGLE_NS
);
844 iks_insert_attrib(candidate
, "name", tmp
->name
);
845 iks_insert_attrib(candidate
, "address", tmp
->ip
);
846 iks_insert_attrib(candidate
, "port", port
);
847 iks_insert_attrib(candidate
, "username", tmp
->username
);
848 iks_insert_attrib(candidate
, "password", tmp
->password
);
849 iks_insert_attrib(candidate
, "preference", preference
);
850 if (tmp
->protocol
== AJI_PROTOCOL_UDP
)
851 iks_insert_attrib(candidate
, "protocol", "udp");
852 if (tmp
->protocol
== AJI_PROTOCOL_SSLTCP
)
853 iks_insert_attrib(candidate
, "protocol", "ssltcp");
854 if (tmp
->type
== AJI_CONNECT_STUN
)
855 iks_insert_attrib(candidate
, "type", "stun");
856 if (tmp
->type
== AJI_CONNECT_LOCAL
)
857 iks_insert_attrib(candidate
, "type", "local");
858 if (tmp
->type
== AJI_CONNECT_RELAY
)
859 iks_insert_attrib(candidate
, "type", "relay");
860 iks_insert_attrib(candidate
, "network", "0");
861 iks_insert_attrib(candidate
, "generation", "0");
873 iks_delete(candidate
);
874 iks_delete(transport
);
879 static struct gtalk_pvt
*gtalk_alloc(struct gtalk
*client
, const char *us
, const char *them
, const char *sid
)
881 struct gtalk_pvt
*tmp
= NULL
;
882 struct aji_resource
*resources
= NULL
;
883 struct aji_buddy
*buddy
;
885 char *data
, *exten
= NULL
;
887 ast_debug(1, "The client is %s for alloc\n", client
->name
);
888 if (!sid
&& !strchr(them
, '/')) { /* I started call! */
889 if (!strcasecmp(client
->name
, "guest")) {
890 buddy
= ASTOBJ_CONTAINER_FIND(&client
->connection
->buddies
, them
);
892 resources
= buddy
->resources
;
893 } else if (client
->buddy
)
894 resources
= client
->buddy
->resources
;
896 if (resources
->cap
->jingle
) {
899 resources
= resources
->next
;
902 snprintf(idroster
, sizeof(idroster
), "%s/%s", them
, resources
->resource
);
904 ast_log(LOG_ERROR
, "no gtalk capable clients to talk to.\n");
908 if (!(tmp
= ast_calloc(1, sizeof(*tmp
)))) {
912 memcpy(&tmp
->prefs
, &client
->prefs
, sizeof(struct ast_codec_pref
));
915 ast_copy_string(tmp
->sid
, sid
, sizeof(tmp
->sid
));
916 ast_copy_string(tmp
->them
, them
, sizeof(tmp
->them
));
917 ast_copy_string(tmp
->us
, us
, sizeof(tmp
->us
));
919 snprintf(tmp
->sid
, sizeof(tmp
->sid
), "%08lx%08lx", ast_random(), ast_random());
920 ast_copy_string(tmp
->them
, idroster
, sizeof(tmp
->them
));
921 ast_copy_string(tmp
->us
, us
, sizeof(tmp
->us
));
925 tmp
->rtp
= ast_rtp_new_with_bindaddr(sched
, io
, 1, 0, bindaddr
.sin_addr
);
926 ast_rtp_pt_clear(tmp
->rtp
);
928 /* add user configured codec capabilites */
929 if (client
->capability
)
930 tmp
->capability
= client
->capability
;
931 else if (global_capability
)
932 tmp
->capability
= global_capability
;
934 tmp
->parent
= client
;
936 ast_log(LOG_WARNING
, "Out of RTP sessions?\n");
941 /* Set CALLERID(name) to the full JID of the remote peer */
942 ast_copy_string(tmp
->cid_name
, tmp
->them
, sizeof(tmp
->cid_name
));
944 if(strchr(tmp
->us
, '/')) {
945 data
= ast_strdupa(tmp
->us
);
946 exten
= strsep(&data
, "/");
949 ast_copy_string(tmp
->exten
, exten
, sizeof(tmp
->exten
));
950 ast_mutex_init(&tmp
->lock
);
951 ast_mutex_lock(>alklock
);
952 tmp
->next
= client
->p
;
954 ast_mutex_unlock(>alklock
);
958 /*! \brief Start new gtalk channel */
959 static struct ast_channel
*gtalk_new(struct gtalk
*client
, struct gtalk_pvt
*i
, int state
, const char *title
)
961 struct ast_channel
*tmp
;
970 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);
972 ast_log(LOG_WARNING
, "Unable to allocate Gtalk channel structure!\n");
975 tmp
->tech
= >alk_tech
;
977 /* Select our native format based on codec preference until we receive
978 something from another device to the contrary. */
979 if (i
->jointcapability
)
980 what
= i
->jointcapability
;
981 else if (i
->capability
)
982 what
= i
->capability
;
984 what
= global_capability
;
986 /* Set Frame packetization */
988 ast_rtp_codec_setpref(i
->rtp
, &i
->prefs
);
990 tmp
->nativeformats
= ast_codec_choose(&i
->prefs
, what
, 1) | (i
->jointcapability
& AST_FORMAT_VIDEO_MASK
);
991 fmt
= ast_best_codec(tmp
->nativeformats
);
994 ast_rtp_setstun(i
->rtp
, 1);
995 ast_channel_set_fd(tmp
, 0, ast_rtp_fd(i
->rtp
));
996 ast_channel_set_fd(tmp
, 1, ast_rtcp_fd(i
->rtp
));
999 ast_rtp_setstun(i
->rtp
, 1);
1000 ast_channel_set_fd(tmp
, 2, ast_rtp_fd(i
->vrtp
));
1001 ast_channel_set_fd(tmp
, 3, ast_rtcp_fd(i
->vrtp
));
1003 if (state
== AST_STATE_RING
)
1005 tmp
->adsicpe
= AST_ADSI_UNAVAILABLE
;
1006 tmp
->writeformat
= fmt
;
1007 tmp
->rawwriteformat
= fmt
;
1008 tmp
->readformat
= fmt
;
1009 tmp
->rawreadformat
= fmt
;
1012 tmp
->callgroup
= client
->callgroup
;
1013 tmp
->pickupgroup
= client
->pickupgroup
;
1014 tmp
->cid
.cid_pres
= client
->callingpres
;
1015 if (!ast_strlen_zero(client
->accountcode
))
1016 ast_string_field_set(tmp
, accountcode
, client
->accountcode
);
1017 if (client
->amaflags
)
1018 tmp
->amaflags
= client
->amaflags
;
1019 if (!ast_strlen_zero(client
->language
))
1020 ast_string_field_set(tmp
, language
, client
->language
);
1021 if (!ast_strlen_zero(client
->musicclass
))
1022 ast_string_field_set(tmp
, musicclass
, client
->musicclass
);
1023 if (!ast_strlen_zero(client
->parkinglot
))
1024 ast_string_field_set(tmp
, parkinglot
, client
->parkinglot
);
1026 ast_module_ref(ast_module_info
->self
);
1027 ast_copy_string(tmp
->context
, client
->context
, sizeof(tmp
->context
));
1028 ast_copy_string(tmp
->exten
, i
->exten
, sizeof(tmp
->exten
));
1030 if (!ast_strlen_zero(i
->exten
) && strcmp(i
->exten
, "s"))
1031 tmp
->cid
.cid_dnid
= ast_strdup(i
->exten
);
1034 ast_jb_configure(tmp
, &global_jbconf
);
1035 if (state
!= AST_STATE_DOWN
&& ast_pbx_start(tmp
)) {
1036 ast_log(LOG_WARNING
, "Unable to start PBX on %s\n", tmp
->name
);
1037 tmp
->hangupcause
= AST_CAUSE_SWITCH_CONGESTION
;
1041 manager_event(EVENT_FLAG_SYSTEM
, "ChannelUpdate",
1042 "Channel: %s\r\nChanneltype: %s\r\nGtalk-SID: %s\r\n",
1043 i
->owner
? i
->owner
->name
: "", "Gtalk", i
->sid
);
1048 static int gtalk_action(struct gtalk
*client
, struct gtalk_pvt
*p
, const char *action
)
1050 iks
*request
, *session
= NULL
;
1053 request
= iks_new("iq");
1055 iks_insert_attrib(request
, "type", "set");
1056 iks_insert_attrib(request
, "from", p
->us
);
1057 iks_insert_attrib(request
, "to", p
->them
);
1058 iks_insert_attrib(request
, "id", client
->connection
->mid
);
1059 ast_aji_increment_mid(client
->connection
->mid
);
1060 session
= iks_new("session");
1062 iks_insert_attrib(session
, "type", action
);
1063 iks_insert_attrib(session
, "id", p
->sid
);
1064 iks_insert_attrib(session
, "initiator", p
->initiator
? p
->us
: p
->them
);
1065 iks_insert_attrib(session
, "xmlns", "http://www.google.com/session");
1066 iks_insert_node(request
, session
);
1067 ast_aji_send(client
->connection
, request
);
1072 iks_delete(session
);
1073 iks_delete(request
);
1078 static void gtalk_free_candidates(struct gtalk_candidate
*candidate
)
1080 struct gtalk_candidate
*last
;
1083 candidate
= candidate
->next
;
1088 static void gtalk_free_pvt(struct gtalk
*client
, struct gtalk_pvt
*p
)
1090 struct gtalk_pvt
*cur
, *prev
= NULL
;
1095 prev
->next
= p
->next
;
1097 client
->p
= p
->next
;
1104 iks_filter_remove_rule(p
->parent
->connection
->f
, p
->ringrule
);
1106 ast_log(LOG_WARNING
, "Uh oh, there's an owner, this is going to be messy.\n");
1108 ast_rtp_destroy(p
->rtp
);
1110 ast_rtp_destroy(p
->vrtp
);
1111 gtalk_free_candidates(p
->theircandidates
);
1116 static int gtalk_newcall(struct gtalk
*client
, ikspak
*pak
)
1118 struct gtalk_pvt
*p
, *tmp
= client
->p
;
1119 struct ast_channel
*chan
;
1123 char s1
[BUFSIZ
], s2
[BUFSIZ
], s3
[BUFSIZ
];
1124 int peernoncodeccapability
;
1126 /* Make sure our new call doesn't exist yet */
1127 from
= iks_find_attrib(pak
->x
,"to");
1129 from
= client
->connection
->jid
->full
;
1132 if (iks_find_with_attrib(pak
->x
, "session", "id", tmp
->sid
)) {
1133 ast_log(LOG_NOTICE
, "Ignoring duplicate call setup on SID %s\n", tmp
->sid
);
1134 gtalk_response(client
, from
, pak
, "out-of-order", NULL
);
1140 if (!strcasecmp(client
->name
, "guest")){
1141 /* the guest account is not tied to any configured XMPP client,
1143 client
->connection
= ast_aji_get_client(from
);
1144 if (!client
->connection
) {
1145 ast_log(LOG_ERROR
, "No XMPP client to talk to, us (partial JID) : %s\n", from
);
1150 p
= gtalk_alloc(client
, from
, pak
->from
->full
, iks_find_attrib(pak
->query
, "id"));
1152 ast_log(LOG_WARNING
, "Unable to allocate gtalk structure!\n");
1156 chan
= gtalk_new(client
, p
, AST_STATE_DOWN
, pak
->from
->user
);
1158 gtalk_free_pvt(client
, p
);
1162 ast_mutex_lock(&p
->lock
);
1163 ast_copy_string(p
->them
, pak
->from
->full
, sizeof(p
->them
));
1164 if (iks_find_attrib(pak
->query
, "id")) {
1165 ast_copy_string(p
->sid
, iks_find_attrib(pak
->query
, "id"),
1169 /* codec points to the first <payload-type/> tag */
1170 codec
= iks_child(iks_child(iks_child(pak
->x
)));
1173 ast_rtp_set_m_type(p
->rtp
, atoi(iks_find_attrib(codec
, "id")));
1174 ast_rtp_set_rtpmap_type(p
->rtp
, atoi(iks_find_attrib(codec
, "id")), "audio", iks_find_attrib(codec
, "name"), 0);
1175 codec
= iks_next(codec
);
1178 /* Now gather all of the codecs that we are asked for */
1179 ast_rtp_get_current_formats(p
->rtp
, &p
->peercapability
, &peernoncodeccapability
);
1180 p
->jointcapability
= p
->capability
& p
->peercapability
;
1181 ast_mutex_unlock(&p
->lock
);
1183 ast_setstate(chan
, AST_STATE_RING
);
1184 if (!p
->jointcapability
) {
1185 ast_log(LOG_WARNING
, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1
, BUFSIZ
, p
->capability
),
1186 ast_getformatname_multiple(s2
, BUFSIZ
, p
->peercapability
),
1187 ast_getformatname_multiple(s3
, BUFSIZ
, p
->jointcapability
));
1188 /* close session if capabilities don't match */
1189 gtalk_action(client
, p
, "reject");
1192 ast_channel_free(chan
);
1196 res
= ast_pbx_start(chan
);
1199 case AST_PBX_FAILED
:
1200 ast_log(LOG_WARNING
, "Failed to start PBX :(\n");
1201 gtalk_response(client
, from
, pak
, "service-unavailable", NULL
);
1203 case AST_PBX_CALL_LIMIT
:
1204 ast_log(LOG_WARNING
, "Failed to start PBX (call limit reached) \n");
1205 gtalk_response(client
, from
, pak
, "service-unavailable", NULL
);
1207 case AST_PBX_SUCCESS
:
1208 gtalk_response(client
, from
, pak
, NULL
, NULL
);
1209 gtalk_invite_response(p
, p
->them
, p
->us
,p
->sid
, 0);
1210 gtalk_create_candidates(client
, p
, p
->sid
, p
->them
, p
->us
);
1218 static int gtalk_update_stun(struct gtalk
*client
, struct gtalk_pvt
*p
)
1220 struct gtalk_candidate
*tmp
;
1222 struct ast_hostent ahp
;
1223 struct sockaddr_in sin
;
1224 struct sockaddr_in aux
;
1226 if (time(NULL
) == p
->laststun
)
1229 tmp
= p
->theircandidates
;
1230 p
->laststun
= time(NULL
);
1234 /* Find the IP address of the host */
1235 hp
= ast_gethostbyname(tmp
->ip
, &ahp
);
1236 sin
.sin_family
= AF_INET
;
1237 memcpy(&sin
.sin_addr
, hp
->h_addr
, sizeof(sin
.sin_addr
));
1238 sin
.sin_port
= htons(tmp
->port
);
1239 snprintf(username
, sizeof(username
), "%s%s", tmp
->username
,
1240 p
->ourcandidates
->username
);
1242 /* Find out the result of the STUN */
1243 ast_rtp_get_peer(p
->rtp
, &aux
);
1245 /* If the STUN result is different from the IP of the hostname,
1246 lock on the stun IP of the hostname advertised by the
1248 if (aux
.sin_addr
.s_addr
&&
1249 aux
.sin_addr
.s_addr
!= sin
.sin_addr
.s_addr
)
1250 ast_rtp_stun_request(p
->rtp
, &aux
, username
);
1252 ast_rtp_stun_request(p
->rtp
, &sin
, username
);
1254 if (aux
.sin_addr
.s_addr
) {
1255 ast_debug(4, "Receiving RTP traffic from IP %s, matches with remote candidate's IP %s\n", ast_inet_ntoa(aux
.sin_addr
), tmp
->ip
);
1256 ast_debug(4, "Sending STUN request to %s\n", tmp
->ip
);
1264 static int gtalk_add_candidate(struct gtalk
*client
, ikspak
*pak
)
1266 struct gtalk_pvt
*p
= NULL
, *tmp
= NULL
;
1267 struct aji_client
*c
= client
->connection
;
1268 struct gtalk_candidate
*newcandidate
= NULL
;
1269 iks
*traversenodes
= NULL
, *receipt
= NULL
;
1272 from
= iks_find_attrib(pak
->x
,"to");
1274 from
= c
->jid
->full
;
1276 for (tmp
= client
->p
; tmp
; tmp
= tmp
->next
) {
1277 if (iks_find_with_attrib(pak
->x
, "session", "id", tmp
->sid
)) {
1286 traversenodes
= pak
->query
;
1287 while(traversenodes
) {
1288 if(!strcasecmp(iks_name(traversenodes
), "session")) {
1289 traversenodes
= iks_child(traversenodes
);
1292 if(!strcasecmp(iks_name(traversenodes
), "transport")) {
1293 traversenodes
= iks_child(traversenodes
);
1296 if(!strcasecmp(iks_name(traversenodes
), "candidate")) {
1297 newcandidate
= ast_calloc(1, sizeof(*newcandidate
));
1300 ast_copy_string(newcandidate
->name
, iks_find_attrib(traversenodes
, "name"),
1301 sizeof(newcandidate
->name
));
1302 ast_copy_string(newcandidate
->ip
, iks_find_attrib(traversenodes
, "address"),
1303 sizeof(newcandidate
->ip
));
1304 newcandidate
->port
= atoi(iks_find_attrib(traversenodes
, "port"));
1305 ast_copy_string(newcandidate
->username
, iks_find_attrib(traversenodes
, "username"),
1306 sizeof(newcandidate
->username
));
1307 ast_copy_string(newcandidate
->password
, iks_find_attrib(traversenodes
, "password"),
1308 sizeof(newcandidate
->password
));
1309 newcandidate
->preference
= atof(iks_find_attrib(traversenodes
, "preference"));
1310 if (!strcasecmp(iks_find_attrib(traversenodes
, "protocol"), "udp"))
1311 newcandidate
->protocol
= AJI_PROTOCOL_UDP
;
1312 if (!strcasecmp(iks_find_attrib(traversenodes
, "protocol"), "ssltcp"))
1313 newcandidate
->protocol
= AJI_PROTOCOL_SSLTCP
;
1315 if (!strcasecmp(iks_find_attrib(traversenodes
, "type"), "stun"))
1316 newcandidate
->type
= AJI_CONNECT_STUN
;
1317 if (!strcasecmp(iks_find_attrib(traversenodes
, "type"), "local"))
1318 newcandidate
->type
= AJI_CONNECT_LOCAL
;
1319 if (!strcasecmp(iks_find_attrib(traversenodes
, "type"), "relay"))
1320 newcandidate
->type
= AJI_CONNECT_RELAY
;
1321 ast_copy_string(newcandidate
->network
, iks_find_attrib(traversenodes
, "network"),
1322 sizeof(newcandidate
->network
));
1323 newcandidate
->generation
= atoi(iks_find_attrib(traversenodes
, "generation"));
1324 newcandidate
->next
= NULL
;
1326 newcandidate
->next
= p
->theircandidates
;
1327 p
->theircandidates
= newcandidate
;
1329 gtalk_update_stun(p
->parent
, p
);
1330 newcandidate
= NULL
;
1332 traversenodes
= iks_next(traversenodes
);
1335 receipt
= iks_new("iq");
1336 iks_insert_attrib(receipt
, "type", "result");
1337 iks_insert_attrib(receipt
, "from", from
);
1338 iks_insert_attrib(receipt
, "to", iks_find_attrib(pak
->x
, "from"));
1339 iks_insert_attrib(receipt
, "id", iks_find_attrib(pak
->x
, "id"));
1340 ast_aji_send(c
, receipt
);
1342 iks_delete(receipt
);
1347 static struct ast_frame
*gtalk_rtp_read(struct ast_channel
*ast
, struct gtalk_pvt
*p
)
1349 struct ast_frame
*f
;
1352 return &ast_null_frame
;
1353 f
= ast_rtp_read(p
->rtp
);
1354 gtalk_update_stun(p
->parent
, p
);
1356 /* We already hold the channel lock */
1357 if (f
->frametype
== AST_FRAME_VOICE
) {
1358 if (f
->subclass
!= (p
->owner
->nativeformats
& AST_FORMAT_AUDIO_MASK
)) {
1359 ast_debug(1, "Oooh, format changed to %d\n", f
->subclass
);
1360 p
->owner
->nativeformats
=
1361 (p
->owner
->nativeformats
& AST_FORMAT_VIDEO_MASK
) | f
->subclass
;
1362 ast_set_read_format(p
->owner
, p
->owner
->readformat
);
1363 ast_set_write_format(p
->owner
, p
->owner
->writeformat
);
1365 /* if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
1366 f = ast_dsp_process(p->owner, p->vad, f);
1367 if (option_debug && f && (f->frametype == AST_FRAME_DTMF))
1368 ast_debug(1, "* Detected inband DTMF '%c'\n", f->subclass);
1375 static struct ast_frame
*gtalk_read(struct ast_channel
*ast
)
1377 struct ast_frame
*fr
;
1378 struct gtalk_pvt
*p
= ast
->tech_pvt
;
1380 ast_mutex_lock(&p
->lock
);
1381 fr
= gtalk_rtp_read(ast
, p
);
1382 ast_mutex_unlock(&p
->lock
);
1386 /*! \brief Send frame to media channel (rtp) */
1387 static int gtalk_write(struct ast_channel
*ast
, struct ast_frame
*frame
)
1389 struct gtalk_pvt
*p
= ast
->tech_pvt
;
1392 switch (frame
->frametype
) {
1393 case AST_FRAME_VOICE
:
1394 if (!(frame
->subclass
& ast
->nativeformats
)) {
1395 ast_log(LOG_WARNING
,
1396 "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
1397 frame
->subclass
, ast
->nativeformats
, ast
->readformat
,
1402 ast_mutex_lock(&p
->lock
);
1404 res
= ast_rtp_write(p
->rtp
, frame
);
1406 ast_mutex_unlock(&p
->lock
);
1409 case AST_FRAME_VIDEO
:
1411 ast_mutex_lock(&p
->lock
);
1413 res
= ast_rtp_write(p
->vrtp
, frame
);
1415 ast_mutex_unlock(&p
->lock
);
1418 case AST_FRAME_IMAGE
:
1422 ast_log(LOG_WARNING
, "Can't send %d type frames with Gtalk write\n",
1430 static int gtalk_fixup(struct ast_channel
*oldchan
, struct ast_channel
*newchan
)
1432 struct gtalk_pvt
*p
= newchan
->tech_pvt
;
1433 ast_mutex_lock(&p
->lock
);
1435 if ((p
->owner
!= oldchan
)) {
1436 ast_mutex_unlock(&p
->lock
);
1439 if (p
->owner
== oldchan
)
1441 ast_mutex_unlock(&p
->lock
);
1445 static int gtalk_indicate(struct ast_channel
*ast
, int condition
, const void *data
, size_t datalen
)
1449 switch (condition
) {
1450 case AST_CONTROL_HOLD
:
1451 ast_moh_start(ast
, data
, NULL
);
1453 case AST_CONTROL_UNHOLD
:
1457 ast_log(LOG_NOTICE
, "Don't know how to indicate condition '%d'\n", condition
);
1464 static int gtalk_digit_begin(struct ast_channel
*chan
, char digit
)
1466 return gtalk_digit(chan
, digit
, 0);
1469 static int gtalk_digit_end(struct ast_channel
*chan
, char digit
, unsigned int duration
)
1471 return gtalk_digit(chan
, digit
, duration
);
1474 static int gtalk_digit(struct ast_channel
*ast
, char digit
, unsigned int duration
)
1476 struct gtalk_pvt
*p
= ast
->tech_pvt
;
1477 struct gtalk
*client
= p
->parent
;
1478 iks
*iq
, *gtalk
, *dtmf
;
1479 char buffer
[2] = {digit
, '\0'};
1481 gtalk
= iks_new("gtalk");
1482 dtmf
= iks_new("dtmf");
1483 if(!iq
|| !gtalk
|| !dtmf
) {
1487 ast_log(LOG_ERROR
, "Did not send dtmf do to memory issue\n");
1491 iks_insert_attrib(iq
, "type", "set");
1492 iks_insert_attrib(iq
, "to", p
->them
);
1493 iks_insert_attrib(iq
, "from", p
->us
);
1494 iks_insert_attrib(iq
, "id", client
->connection
->mid
);
1495 ast_aji_increment_mid(client
->connection
->mid
);
1496 iks_insert_attrib(gtalk
, "xmlns", "http://jabber.org/protocol/gtalk");
1497 iks_insert_attrib(gtalk
, "action", "session-info");
1498 iks_insert_attrib(gtalk
, "initiator", p
->initiator
? p
->us
: p
->them
);
1499 iks_insert_attrib(gtalk
, "sid", p
->sid
);
1500 iks_insert_attrib(dtmf
, "xmlns", "http://jabber.org/protocol/gtalk/info/dtmf");
1501 iks_insert_attrib(dtmf
, "code", buffer
);
1502 iks_insert_node(iq
, gtalk
);
1503 iks_insert_node(gtalk
, dtmf
);
1505 ast_mutex_lock(&p
->lock
);
1506 if (ast
->dtmff
.frametype
== AST_FRAME_DTMF_BEGIN
|| duration
== 0) {
1507 iks_insert_attrib(dtmf
, "action", "button-down");
1508 } else if (ast
->dtmff
.frametype
== AST_FRAME_DTMF_END
|| duration
!= 0) {
1509 iks_insert_attrib(dtmf
, "action", "button-up");
1511 ast_aji_send(client
->connection
, iq
);
1516 ast_mutex_unlock(&p
->lock
);
1520 static int gtalk_sendhtml(struct ast_channel
*ast
, int subclass
, const char *data
, int datalen
)
1522 ast_log(LOG_NOTICE
, "XXX Implement gtalk sendhtml XXX\n");
1527 /* Not in use right now.
1528 static int gtalk_auto_congest(void *nothing)
1530 struct gtalk_pvt *p = nothing;
1532 ast_mutex_lock(&p->lock);
1534 if (!ast_channel_trylock(p->owner)) {
1535 ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
1536 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
1537 ast_channel_unlock(p->owner);
1540 ast_mutex_unlock(&p->lock);
1545 /*! \brief Initiate new call, part of PBX interface
1546 * dest is the dial string */
1547 static int gtalk_call(struct ast_channel
*ast
, char *dest
, int timeout
)
1549 struct gtalk_pvt
*p
= ast
->tech_pvt
;
1551 if ((ast
->_state
!= AST_STATE_DOWN
) && (ast
->_state
!= AST_STATE_RESERVED
)) {
1552 ast_log(LOG_WARNING
, "gtalk_call called on %s, neither down nor reserved\n", ast
->name
);
1556 ast_setstate(ast
, AST_STATE_RING
);
1558 ast_copy_string(p
->ring
, p
->parent
->connection
->mid
, sizeof(p
->ring
));
1559 p
->ringrule
= iks_filter_add_rule(p
->parent
->connection
->f
, gtalk_ringing_ack
, p
,
1560 IKS_RULE_ID
, p
->ring
, IKS_RULE_DONE
);
1562 ast_log(LOG_WARNING
, "Whoa, already have a ring rule!\n");
1564 gtalk_invite(p
, p
->them
, p
->us
, p
->sid
, 1);
1565 gtalk_create_candidates(p
->parent
, p
, p
->sid
, p
->them
, p
->us
);
1570 /*! \brief Hangup a call through the gtalk proxy channel */
1571 static int gtalk_hangup(struct ast_channel
*ast
)
1573 struct gtalk_pvt
*p
= ast
->tech_pvt
;
1574 struct gtalk
*client
;
1576 ast_mutex_lock(&p
->lock
);
1579 ast
->tech_pvt
= NULL
;
1580 if (!p
->alreadygone
)
1581 gtalk_action(client
, p
, "terminate");
1582 ast_mutex_unlock(&p
->lock
);
1584 gtalk_free_pvt(client
, p
);
1585 ast_module_unref(ast_module_info
->self
);
1590 /*! \brief Part of PBX interface */
1591 static struct ast_channel
*gtalk_request(const char *type
, int format
, void *data
, int *cause
)
1593 struct gtalk_pvt
*p
= NULL
;
1594 struct gtalk
*client
= NULL
;
1595 char *sender
= NULL
, *to
= NULL
, *s
= NULL
;
1596 struct ast_channel
*chan
= NULL
;
1599 s
= ast_strdupa(data
);
1601 sender
= strsep(&s
, "/");
1602 if (sender
&& (sender
[0] != '\0'))
1603 to
= strsep(&s
, "/");
1605 ast_log(LOG_ERROR
, "Bad arguments in Gtalk Dialstring: %s\n", (char*) data
);
1611 client
= find_gtalk(to
, sender
);
1613 ast_log(LOG_WARNING
, "Could not find recipient.\n");
1616 if (!strcasecmp(client
->name
, "guest")){
1617 /* the guest account is not tied to any configured XMPP client,
1619 client
->connection
= ast_aji_get_client(sender
);
1620 if (!client
->connection
) {
1621 ast_log(LOG_ERROR
, "No XMPP client to talk to, us (partial JID) : %s\n", sender
);
1626 ASTOBJ_WRLOCK(client
);
1627 p
= gtalk_alloc(client
, strchr(sender
, '@') ? sender
: client
->connection
->jid
->full
, strchr(to
, '@') ? to
: client
->user
, NULL
);
1629 chan
= gtalk_new(client
, p
, AST_STATE_DOWN
, to
);
1631 ASTOBJ_UNLOCK(client
);
1635 /*! \brief CLI command "gtalk show channels" */
1636 static char *gtalk_show_channels(struct ast_cli_entry
*e
, int cmd
, struct ast_cli_args
*a
)
1638 #define FORMAT "%-30.30s %-30.30s %-15.15s %-5.5s %-5.5s \n"
1639 struct gtalk_pvt
*p
;
1640 struct ast_channel
*chan
;
1642 char them
[AJI_MAX_JIDLEN
];
1644 char *resource
= NULL
;
1648 e
->command
= "gtalk show channels";
1650 "Usage: gtalk show channels\n"
1651 " Shows current state of the Gtalk channels.\n";
1658 return CLI_SHOWUSAGE
;
1660 ast_mutex_lock(>alklock
);
1661 ast_cli(a
->fd
, FORMAT
, "Channel", "Jabber ID", "Resource", "Read", "Write");
1662 ASTOBJ_CONTAINER_TRAVERSE(>alk_list
, 1, {
1663 ASTOBJ_WRLOCK(iterator
);
1667 ast_copy_string(them
, p
->them
, sizeof(them
));
1669 resource
= strchr(them
, '/');
1677 ast_cli(a
->fd
, FORMAT
,
1681 ast_getformatname(chan
->readformat
),
1682 ast_getformatname(chan
->writeformat
)
1685 ast_log(LOG_WARNING
, "No available channel\n");
1689 ASTOBJ_UNLOCK(iterator
);
1692 ast_mutex_unlock(>alklock
);
1694 ast_cli(a
->fd
, "%d active gtalk channel%s\n", numchans
, (numchans
!= 1) ? "s" : "");
1699 /*! \brief CLI command "gtalk reload" */
1700 static char *gtalk_do_reload(struct ast_cli_entry
*e
, int cmd
, struct ast_cli_args
*a
)
1704 e
->command
= "gtalk reload";
1706 "Usage: gtalk reload\n"
1707 " Reload gtalk channel driver.\n";
1713 ast_verbose("IT DOES WORK!\n");
1717 static int gtalk_parser(void *data
, ikspak
*pak
)
1719 struct gtalk
*client
= ASTOBJ_REF((struct gtalk
*) data
);
1721 if (iks_find_attrib(pak
->x
, "type") && !strcmp(iks_find_attrib (pak
->x
, "type"),"error")) {
1722 ast_log(LOG_NOTICE
, "Remote peer reported an error, trying to establish the call anyway\n");
1724 else if (iks_find_with_attrib(pak
->x
, "session", "type", "initiate")) {
1726 gtalk_newcall(client
, pak
);
1727 } else if (iks_find_with_attrib(pak
->x
, "session", "type", "candidates") || iks_find_with_attrib(pak
->x
, "session", "type", "transport-info")) {
1728 ast_debug(3, "About to add candidate!\n");
1729 gtalk_add_candidate(client
, pak
);
1730 ast_debug(3, "Candidate Added!\n");
1731 } else if (iks_find_with_attrib(pak
->x
, "session", "type", "accept")) {
1732 gtalk_is_answered(client
, pak
);
1733 } else if (iks_find_with_attrib(pak
->x
, "session", "type", "transport-accept")) {
1734 gtalk_is_accepted(client
, pak
);
1735 } else if (iks_find_with_attrib(pak
->x
, "session", "type", "content-info") || iks_find_with_attrib(pak
->x
, "gtalk", "action", "session-info")) {
1736 gtalk_handle_dtmf(client
, pak
);
1737 } else if (iks_find_with_attrib(pak
->x
, "session", "type", "terminate")) {
1738 gtalk_hangup_farend(client
, pak
);
1739 } else if (iks_find_with_attrib(pak
->x
, "session", "type", "reject")) {
1740 gtalk_hangup_farend(client
, pak
);
1742 ASTOBJ_UNREF(client
, gtalk_member_destroy
);
1743 return IKS_FILTER_EAT
;
1746 /* Not using this anymore probably take out soon
1747 static struct gtalk_candidate *gtalk_create_candidate(char *args)
1749 char *name, *type, *preference, *protocol;
1750 struct gtalk_candidate *res;
1751 res = ast_calloc(1, sizeof(*res));
1754 if ((args = strchr(args, ','))) {
1759 if ((args = strchr(args, ','))) {
1764 if ((args = strchr(args, ','))) {
1770 ast_copy_string(res->name, name, sizeof(res->name));
1772 res->preference = atof(preference);
1775 if (!strcasecmp("udp", protocol))
1776 res->protocol = AJI_PROTOCOL_UDP;
1777 if (!strcasecmp("ssltcp", protocol))
1778 res->protocol = AJI_PROTOCOL_SSLTCP;
1781 if (!strcasecmp("stun", type))
1782 res->type = AJI_CONNECT_STUN;
1783 if (!strcasecmp("local", type))
1784 res->type = AJI_CONNECT_LOCAL;
1785 if (!strcasecmp("relay", type))
1786 res->type = AJI_CONNECT_RELAY;
1793 static int gtalk_create_member(char *label
, struct ast_variable
*var
, int allowguest
,
1794 struct ast_codec_pref prefs
, char *context
,
1795 struct gtalk
*member
)
1797 struct aji_client
*client
;
1800 ast_log(LOG_WARNING
, "Out of memory.\n");
1802 ast_copy_string(member
->name
, label
, sizeof(member
->name
));
1803 ast_copy_string(member
->user
, label
, sizeof(member
->user
));
1804 ast_copy_string(member
->context
, context
, sizeof(member
->context
));
1805 member
->allowguest
= allowguest
;
1806 member
->prefs
= prefs
;
1809 struct gtalk_candidate
*candidate
= NULL
;
1811 if (!strcasecmp(var
->name
, "username"))
1812 ast_copy_string(member
->user
, var
->value
, sizeof(member
->user
));
1813 else if (!strcasecmp(var
->name
, "disallow"))
1814 ast_parse_allow_disallow(&member
->prefs
, &member
->capability
, var
->value
, 0);
1815 else if (!strcasecmp(var
->name
, "allow"))
1816 ast_parse_allow_disallow(&member
->prefs
, &member
->capability
, var
->value
, 1);
1817 else if (!strcasecmp(var
->name
, "context"))
1818 ast_copy_string(member
->context
, var
->value
, sizeof(member
->context
));
1819 else if (!strcasecmp(var
->name
, "parkinglot"))
1820 ast_copy_string(member
->parkinglot
, var
->value
, sizeof(member
->parkinglot
));
1822 else if (!strcasecmp(var
->name
, "candidate")) {
1823 candidate
= gtalk_create_candidate(var
->value
);
1825 candidate
->next
= member
->ourcandidates
;
1826 member
->ourcandidates
= candidate
;
1830 else if (!strcasecmp(var
->name
, "connection")) {
1831 if ((client
= ast_aji_get_client(var
->value
))) {
1832 member
->connection
= client
;
1833 iks_filter_add_rule(client
->f
, gtalk_parser
, member
,
1834 IKS_RULE_TYPE
, IKS_PAK_IQ
,
1835 IKS_RULE_FROM_PARTIAL
, member
->user
,
1836 IKS_RULE_NS
, "http://www.google.com/session",
1840 ast_log(LOG_ERROR
, "connection referenced not found!\n");
1846 if (member
->connection
&& member
->user
)
1847 member
->buddy
= ASTOBJ_CONTAINER_FIND(&member
->connection
->buddies
, member
->user
);
1849 ast_log(LOG_ERROR
, "No Connection or Username!\n");
1854 static int gtalk_load_config(void)
1857 struct ast_config
*cfg
= NULL
;
1858 char context
[AST_MAX_CONTEXT
];
1859 char parkinglot
[AST_MAX_CONTEXT
];
1861 struct ast_variable
*var
;
1862 struct gtalk
*member
;
1863 struct ast_codec_pref prefs
;
1864 struct aji_client_container
*clients
;
1865 struct gtalk_candidate
*global_candidates
= NULL
;
1867 struct ast_hostent ahp
;
1868 struct ast_flags config_flags
= { 0 };
1870 cfg
= ast_config_load(GOOGLE_CONFIG
, config_flags
);
1874 /* Copy the default jb config over global_jbconf */
1875 memcpy(&global_jbconf
, &default_jbconf
, sizeof(struct ast_jb_conf
));
1877 cat
= ast_category_browse(cfg
, NULL
);
1878 for (var
= ast_variable_browse(cfg
, "general"); var
; var
= var
->next
) {
1879 /* handle jb conf */
1880 if (!ast_jb_read_conf(&global_jbconf
, var
->name
, var
->value
))
1883 if (!strcasecmp(var
->name
, "allowguest"))
1885 (ast_true(ast_variable_retrieve(cfg
, "general", "allowguest"))) ? 1 : 0;
1886 else if (!strcasecmp(var
->name
, "disallow"))
1887 ast_parse_allow_disallow(&prefs
, &global_capability
, var
->value
, 0);
1888 else if (!strcasecmp(var
->name
, "allow"))
1889 ast_parse_allow_disallow(&prefs
, &global_capability
, var
->value
, 1);
1890 else if (!strcasecmp(var
->name
, "context"))
1891 ast_copy_string(context
, var
->value
, sizeof(context
));
1892 else if (!strcasecmp(var
->name
, "parkinglot"))
1893 ast_copy_string(parkinglot
, var
->value
, sizeof(parkinglot
));
1894 else if (!strcasecmp(var
->name
, "bindaddr")) {
1895 if (!(hp
= ast_gethostbyname(var
->value
, &ahp
))) {
1896 ast_log(LOG_WARNING
, "Invalid address: %s\n", var
->value
);
1898 memcpy(&bindaddr
.sin_addr
, hp
->h_addr
, sizeof(bindaddr
.sin_addr
));
1901 /* Idea to allow for custom candidates */
1903 else if (!strcasecmp(var->name, "candidate")) {
1904 candidate = gtalk_create_candidate(var->value);
1906 candidate->next = global_candidates;
1907 global_candidates = candidate;
1913 if (strcasecmp(cat
, "general")) {
1914 var
= ast_variable_browse(cfg
, cat
);
1915 member
= ast_calloc(1, sizeof(*member
));
1916 ASTOBJ_INIT(member
);
1917 ASTOBJ_WRLOCK(member
);
1918 if (!strcasecmp(cat
, "guest")) {
1919 ast_copy_string(member
->name
, "guest", sizeof(member
->name
));
1920 ast_copy_string(member
->user
, "guest", sizeof(member
->user
));
1921 ast_copy_string(member
->context
, context
, sizeof(member
->context
));
1922 ast_copy_string(member
->parkinglot
, parkinglot
, sizeof(member
->parkinglot
));
1923 member
->allowguest
= allowguest
;
1924 member
->prefs
= prefs
;
1926 if (!strcasecmp(var
->name
, "disallow"))
1927 ast_parse_allow_disallow(&member
->prefs
, &member
->capability
,
1929 else if (!strcasecmp(var
->name
, "allow"))
1930 ast_parse_allow_disallow(&member
->prefs
, &member
->capability
,
1932 else if (!strcasecmp(var
->name
, "context"))
1933 ast_copy_string(member
->context
, var
->value
,
1934 sizeof(member
->context
));
1935 else if (!strcasecmp(var
->name
, "parkinglot"))
1936 ast_copy_string(member
->parkinglot
, var
->value
,
1937 sizeof(member
->parkinglot
));
1938 /* Idea to allow for custom candidates */
1940 else if (!strcasecmp(var->name, "candidate")) {
1941 candidate = gtalk_create_candidate(var->value);
1943 candidate->next = member->ourcandidates;
1944 member->ourcandidates = candidate;
1950 ASTOBJ_UNLOCK(member
);
1951 clients
= ast_aji_get_clients();
1953 ASTOBJ_CONTAINER_TRAVERSE(clients
, 1, {
1954 ASTOBJ_WRLOCK(iterator
);
1955 ASTOBJ_WRLOCK(member
);
1956 member
->connection
= NULL
;
1957 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
);
1958 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
);
1959 ASTOBJ_UNLOCK(member
);
1960 ASTOBJ_UNLOCK(iterator
);
1962 ASTOBJ_CONTAINER_LINK(>alk_list
, member
);
1964 ASTOBJ_UNLOCK(member
);
1965 ASTOBJ_UNREF(member
, gtalk_member_destroy
);
1968 ASTOBJ_UNLOCK(member
);
1969 if (gtalk_create_member(cat
, var
, allowguest
, prefs
, context
, member
))
1970 ASTOBJ_CONTAINER_LINK(>alk_list
, member
);
1971 ASTOBJ_UNREF(member
, gtalk_member_destroy
);
1974 cat
= ast_category_browse(cfg
, cat
);
1976 gtalk_free_candidates(global_candidates
);
1980 /*! \brief Load module into PBX, register channel */
1981 static int load_module(void)
1983 char *jabber_loaded
= ast_module_helper("", "res_jabber.so", 0, 0, 0, 0);
1984 free(jabber_loaded
);
1985 if (!jabber_loaded
) {
1986 /* If embedded, check for a different module name */
1987 jabber_loaded
= ast_module_helper("", "res_jabber", 0, 0, 0, 0);
1988 free(jabber_loaded
);
1989 if (!jabber_loaded
) {
1990 ast_log(LOG_ERROR
, "chan_gtalk.so depends upon res_jabber.so\n");
1991 return AST_MODULE_LOAD_DECLINE
;
1995 ASTOBJ_CONTAINER_INIT(>alk_list
);
1996 if (!gtalk_load_config()) {
1997 ast_log(LOG_ERROR
, "Unable to read config file %s. Not loading module.\n", GOOGLE_CONFIG
);
2001 sched
= sched_context_create();
2003 ast_log(LOG_WARNING
, "Unable to create schedule context\n");
2005 io
= io_context_create();
2007 ast_log(LOG_WARNING
, "Unable to create I/O context\n");
2009 if (ast_find_ourip(&__ourip
, bindaddr
)) {
2010 ast_log(LOG_WARNING
, "Unable to get own IP address, Gtalk disabled\n");
2014 ast_rtp_proto_register(>alk_rtp
);
2015 ast_cli_register_multiple(gtalk_cli
, ARRAY_LEN(gtalk_cli
));
2017 /* Make sure we can register our channel type */
2018 if (ast_channel_register(>alk_tech
)) {
2019 ast_log(LOG_ERROR
, "Unable to register channel class %s\n", gtalk_tech
.type
);
2025 /*! \brief Reload module */
2026 static int reload(void)
2031 /*! \brief Unload the gtalk channel from Asterisk */
2032 static int unload_module(void)
2034 struct gtalk_pvt
*privates
= NULL
;
2035 ast_cli_unregister_multiple(gtalk_cli
, ARRAY_LEN(gtalk_cli
));
2036 /* First, take us out of the channel loop */
2037 ast_channel_unregister(>alk_tech
);
2038 ast_rtp_proto_unregister(>alk_rtp
);
2040 if (!ast_mutex_lock(>alklock
)) {
2041 /* Hangup all interfaces if they have an owner */
2042 ASTOBJ_CONTAINER_TRAVERSE(>alk_list
, 1, {
2043 ASTOBJ_WRLOCK(iterator
);
2044 privates
= iterator
->p
;
2046 if (privates
->owner
)
2047 ast_softhangup(privates
->owner
, AST_SOFTHANGUP_APPUNLOAD
);
2048 privates
= privates
->next
;
2051 ASTOBJ_UNLOCK(iterator
);
2053 ast_mutex_unlock(>alklock
);
2055 ast_log(LOG_WARNING
, "Unable to lock the monitor\n");
2058 ASTOBJ_CONTAINER_DESTROYALL(>alk_list
, gtalk_member_destroy
);
2059 ASTOBJ_CONTAINER_DESTROY(>alk_list
);
2063 AST_MODULE_INFO(ASTERISK_GPL_KEY
, AST_MODFLAG_DEFAULT
, "Gtalk Channel Driver",
2064 .load
= load_module
,
2065 .unload
= unload_module
,