Stop ignoring .lastclean
[asterisk-bristuff.git] / channels / chan_gtalk.c
blobabe0d63b6d92890bbe96604967b837a9e080dfc5
1 /*
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.
19 /*! \file
21 * \author Matt O'Gorman <mogorman@digium.com>
23 * \brief Gtalk Channel Driver, until google/libjingle works with jingle spec
25 * \ingroup channel_drivers
28 /*** MODULEINFO
29 <depend>iksemel</depend>
30 <depend>res_jabber</depend>
31 <use>gnutls</use>
32 ***/
34 #include "asterisk.h"
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <sys/socket.h>
43 #include <errno.h>
44 #include <stdlib.h>
45 #include <fcntl.h>
46 #include <netdb.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <sys/signal.h>
50 #include <iksemel.h>
51 #include <pthread.h>
53 #ifdef HAVE_GNUTLS
54 #include <gcrypt.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 =
91 .flags = 0,
92 .max_size = -1,
93 .resync_threshold = -1,
94 .impl = ""
96 static struct ast_jb_conf global_jbconf;
98 enum gtalk_protocol {
99 AJI_PROTOCOL_UDP = 1,
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,
109 struct gtalk_pvt {
110 ast_mutex_t lock; /*!< Channel private lock */
111 time_t laststun;
112 struct gtalk *parent; /*!< Parent client */
113 char sid[100];
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 */
119 int alreadygone;
120 int capability;
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 ) */
131 int peercapability;
132 struct gtalk_pvt *next; /* Next entity */
135 struct gtalk_candidate {
136 char name[100];
137 enum gtalk_protocol protocol;
138 double preference;
139 char username[100];
140 char password[100];
141 enum gtalk_connect_type type;
142 char network[6];
143 int generation;
144 char ip[16];
145 int port;
146 int receipt;
147 struct gtalk_candidate *next;
150 struct gtalk {
151 ASTOBJ_COMPONENTS(struct gtalk);
152 struct aji_client *connection;
153 struct aji_buddy *buddy;
154 struct gtalk_pvt *p;
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 */
160 int capability;
161 ast_group_t callgroup; /*!< Call group */
162 ast_group_t pickupgroup; /*!< Pickup group */
163 int callingpres; /*!< Calling presentation */
164 int allowguest;
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 = {
206 .type = "Gtalk",
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,
213 .call = gtalk_call,
214 .hangup = gtalk_hangup,
215 .answer = gtalk_answer,
216 .read = gtalk_read,
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 = {
234 type: "Gtalk",
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)
262 free(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(&gtalk_list, name);
276 if (!gtalk && strchr(name, '@'))
277 gtalk = ASTOBJ_CONTAINER_FIND_FULL(&gtalk_list, name, user,,, strcasecmp);
279 if (!gtalk) {
280 /* guest call */
281 ASTOBJ_CONTAINER_TRAVERSE(&gtalk_list, 1, {
282 ASTOBJ_RDLOCK(iterator);
283 if (!strcasecmp(iterator->name, "guest")) {
284 gtalk = iterator;
286 ASTOBJ_UNLOCK(iterator);
288 if (gtalk)
289 break;
293 return gtalk;
297 static int add_codec_to_answer(const struct gtalk_pvt *p, int codec, iks *dcodecs)
299 int res = 0;
300 char *format = ast_getformatname(codec);
302 if (!strcasecmp("ulaw", format)) {
303 iks *payload_eg711u, *payload_pcmu;
304 payload_pcmu = iks_new("payload-type");
305 payload_eg711u = iks_new("payload-type");
307 if(!payload_eg711u || !payload_pcmu) {
308 if(payload_pcmu)
309 iks_delete(payload_pcmu);
310 if(payload_eg711u)
311 iks_delete(payload_eg711u);
312 ast_log(LOG_WARNING,"Failed to allocate iks node");
313 return -1;
315 iks_insert_attrib(payload_pcmu, "id", "0");
316 iks_insert_attrib(payload_pcmu, "name", "PCMU");
317 iks_insert_attrib(payload_pcmu, "clockrate","8000");
318 iks_insert_attrib(payload_pcmu, "bitrate","64000");
319 iks_insert_attrib(payload_eg711u, "id", "100");
320 iks_insert_attrib(payload_eg711u, "name", "EG711U");
321 iks_insert_attrib(payload_eg711u, "clockrate","8000");
322 iks_insert_attrib(payload_eg711u, "bitrate","64000");
323 iks_insert_node(dcodecs, payload_pcmu);
324 iks_insert_node(dcodecs, payload_eg711u);
325 res ++;
327 if (!strcasecmp("alaw", format)) {
328 iks *payload_eg711a, *payload_pcma;
329 payload_pcma = iks_new("payload-type");
330 payload_eg711a = iks_new("payload-type");
331 if(!payload_eg711a || !payload_pcma) {
332 if(payload_eg711a)
333 iks_delete(payload_eg711a);
334 if(payload_pcma)
335 iks_delete(payload_pcma);
336 ast_log(LOG_WARNING,"Failed to allocate iks node");
337 return -1;
339 iks_insert_attrib(payload_pcma, "id", "8");
340 iks_insert_attrib(payload_pcma, "name", "PCMA");
341 iks_insert_attrib(payload_pcma, "clockrate","8000");
342 iks_insert_attrib(payload_pcma, "bitrate","64000");
343 payload_eg711a = iks_new("payload-type");
344 iks_insert_attrib(payload_eg711a, "id", "101");
345 iks_insert_attrib(payload_eg711a, "name", "EG711A");
346 iks_insert_attrib(payload_eg711a, "clockrate","8000");
347 iks_insert_attrib(payload_eg711a, "bitrate","64000");
348 iks_insert_node(dcodecs, payload_pcma);
349 iks_insert_node(dcodecs, payload_eg711a);
350 res ++;
352 if (!strcasecmp("ilbc", format)) {
353 iks *payload_ilbc = iks_new("payload-type");
354 if(!payload_ilbc) {
355 ast_log(LOG_WARNING,"Failed to allocate iks node");
356 return -1;
358 iks_insert_attrib(payload_ilbc, "id", "97");
359 iks_insert_attrib(payload_ilbc, "name", "iLBC");
360 iks_insert_attrib(payload_ilbc, "clockrate","8000");
361 iks_insert_attrib(payload_ilbc, "bitrate","13300");
362 iks_insert_node(dcodecs, payload_ilbc);
363 res ++;
365 if (!strcasecmp("g723", format)) {
366 iks *payload_g723 = iks_new("payload-type");
367 if(!payload_g723) {
368 ast_log(LOG_WARNING,"Failed to allocate iks node");
369 return -1;
371 iks_insert_attrib(payload_g723, "id", "4");
372 iks_insert_attrib(payload_g723, "name", "G723");
373 iks_insert_attrib(payload_g723, "clockrate","8000");
374 iks_insert_attrib(payload_g723, "bitrate","6300");
375 iks_insert_node(dcodecs, payload_g723);
376 res ++;
378 if (!strcasecmp("speex", format)) {
379 iks *payload_speex = iks_new("payload-type");
380 if(!payload_speex) {
381 ast_log(LOG_WARNING,"Failed to allocate iks node");
382 return -1;
384 iks_insert_attrib(payload_speex, "id", "110");
385 iks_insert_attrib(payload_speex, "name", "speex");
386 iks_insert_attrib(payload_speex, "clockrate","8000");
387 iks_insert_attrib(payload_speex, "bitrate","11000");
388 iks_insert_node(dcodecs, payload_speex);
389 res++;
391 if (!strcasecmp("gsm", format)) {
392 iks *payload_gsm = iks_new("payload-type");
393 if(!payload_gsm) {
394 ast_log(LOG_WARNING,"Failed to allocate iks node");
395 return -1;
397 iks_insert_attrib(payload_gsm, "id", "103");
398 iks_insert_attrib(payload_gsm, "name", "gsm");
399 iks_insert_node(dcodecs, payload_gsm);
400 res++;
402 ast_rtp_lookup_code(p->rtp, 1, codec);
403 return res;
406 static int gtalk_invite(struct gtalk_pvt *p, char *to, char *from, char *sid, int initiator)
408 struct gtalk *client = p->parent;
409 iks *iq, *gtalk, *dcodecs, *payload_telephone, *transport;
410 int x;
411 int pref_codec = 0;
412 int alreadysent = 0;
413 int codecs_num = 0;
415 iq = iks_new("iq");
416 gtalk = iks_new("session");
417 dcodecs = iks_new("description");
418 transport = iks_new("transport");
419 payload_telephone = iks_new("payload-type");
420 if (!(iq && gtalk && dcodecs && transport && payload_telephone)){
421 if(iq)
422 iks_delete(iq);
423 if(gtalk)
424 iks_delete(gtalk);
425 if(dcodecs)
426 iks_delete(dcodecs);
427 if(transport)
428 iks_delete(transport);
429 if(payload_telephone)
430 iks_delete(payload_telephone);
432 ast_log(LOG_ERROR, "Could not allocate iksemel nodes\n");
433 return 0;
435 iks_insert_attrib(dcodecs, "xmlns", "http://www.google.com/session/phone");
436 iks_insert_attrib(dcodecs, "xml:lang", "en");
438 for (x = 0; x < 32; x++) {
439 if (!(pref_codec = ast_codec_pref_index(&client->prefs, x)))
440 break;
441 if (!(client->capability & pref_codec))
442 continue;
443 if (alreadysent & pref_codec)
444 continue;
445 codecs_num = add_codec_to_answer(p, pref_codec, dcodecs);
446 alreadysent |= pref_codec;
449 if (codecs_num) {
450 /* only propose DTMF within an audio session */
451 iks_insert_attrib(payload_telephone, "id", "106");
452 iks_insert_attrib(payload_telephone, "name", "telephone-event");
453 iks_insert_attrib(payload_telephone, "clockrate", "8000");
455 iks_insert_attrib(transport,"xmlns","http://www.google.com/transport/p2p");
457 iks_insert_attrib(iq, "type", "set");
458 iks_insert_attrib(iq, "to", to);
459 iks_insert_attrib(iq, "from", from);
460 iks_insert_attrib(iq, "id", client->connection->mid);
461 ast_aji_increment_mid(client->connection->mid);
463 iks_insert_attrib(gtalk, "xmlns", "http://www.google.com/session");
464 iks_insert_attrib(gtalk, "type",initiator ? "initiate": "accept");
465 iks_insert_attrib(gtalk, "initiator", initiator ? from : to);
466 iks_insert_attrib(gtalk, "id", sid);
467 iks_insert_node(iq, gtalk);
468 iks_insert_node(gtalk, dcodecs);
469 iks_insert_node(gtalk, transport);
470 iks_insert_node(dcodecs, payload_telephone);
472 iks_send(client->connection->p, iq);
473 iks_delete(payload_telephone);
474 iks_delete(transport);
475 iks_delete(dcodecs);
476 iks_delete(gtalk);
477 iks_delete(iq);
478 return 1;
481 static int gtalk_invite_response(struct gtalk_pvt *p, char *to , char *from, char *sid, int initiator)
483 iks *iq, *session, *transport;
484 iq = iks_new("iq");
485 session = iks_new("session");
486 transport = iks_new("transport");
487 if(!(iq && session && transport)) {
488 if(iq)
489 iks_delete(iq);
490 if(session)
491 iks_delete(session);
492 if(transport)
493 iks_delete(transport);
494 ast_log(LOG_ERROR, " Unable to allocate IKS node\n");
495 return -1;
497 iks_insert_attrib(iq, "from", from);
498 iks_insert_attrib(iq, "to", to);
499 iks_insert_attrib(iq, "type", "set");
500 iks_insert_attrib(iq, "id",p->parent->connection->mid);
501 ast_aji_increment_mid(p->parent->connection->mid);
502 iks_insert_attrib(session, "type", "transport-accept");
503 iks_insert_attrib(session, "id", sid);
504 iks_insert_attrib(session, "initiator", initiator ? from : to);
505 iks_insert_attrib(session, "xmlns", "http://www.google.com/session");
506 iks_insert_attrib(transport, "xmlns", "http://www.google.com/transport/p2p");
507 iks_insert_node(iq,session);
508 iks_insert_node(session,transport);
509 iks_send(p->parent->connection->p, iq);
510 iks_delete(transport);
511 iks_delete(session);
512 iks_delete(iq);
513 return 1;
517 static int gtalk_ringing_ack(void *data, ikspak *pak)
519 struct gtalk_pvt *p = data;
521 if (p->ringrule)
522 iks_filter_remove_rule(p->parent->connection->f, p->ringrule);
523 p->ringrule = NULL;
524 if (p->owner)
525 ast_queue_control(p->owner, AST_CONTROL_RINGING);
526 return IKS_FILTER_EAT;
529 static int gtalk_answer(struct ast_channel *ast)
531 struct gtalk_pvt *p = ast->tech_pvt;
532 int res = 0;
534 if (option_debug)
535 ast_log(LOG_DEBUG, "Answer!\n");
536 ast_mutex_lock(&p->lock);
537 gtalk_invite(p, p->them, p->us,p->sid, 0);
538 ast_mutex_unlock(&p->lock);
539 return res;
542 static enum ast_rtp_get_result gtalk_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
544 struct gtalk_pvt *p = chan->tech_pvt;
545 enum ast_rtp_get_result res = AST_RTP_GET_FAILED;
547 if (!p)
548 return res;
550 ast_mutex_lock(&p->lock);
551 if (p->rtp){
552 *rtp = p->rtp;
553 res = AST_RTP_TRY_PARTIAL;
555 ast_mutex_unlock(&p->lock);
557 return res;
560 static int gtalk_get_codec(struct ast_channel *chan)
562 struct gtalk_pvt *p = chan->tech_pvt;
563 return p->peercapability;
566 static int gtalk_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active)
568 struct gtalk_pvt *p;
570 p = chan->tech_pvt;
571 if (!p)
572 return -1;
573 ast_mutex_lock(&p->lock);
575 /* if (rtp)
576 ast_rtp_get_peer(rtp, &p->redirip);
577 else
578 memset(&p->redirip, 0, sizeof(p->redirip));
579 p->redircodecs = codecs; */
581 /* Reset lastrtprx timer */
582 ast_mutex_unlock(&p->lock);
583 return 0;
586 static int gtalk_response(struct gtalk *client, char *from, ikspak *pak, const char *reasonstr, const char *reasonstr2)
588 iks *response = NULL, *error = NULL, *reason = NULL;
589 int res = -1;
591 response = iks_new("iq");
592 if (response) {
593 iks_insert_attrib(response, "type", "result");
594 iks_insert_attrib(response, "from", from);
595 iks_insert_attrib(response, "to", iks_find_attrib(pak->x, "from"));
596 iks_insert_attrib(response, "id", iks_find_attrib(pak->x, "id"));
597 if (reasonstr) {
598 error = iks_new("error");
599 if (error) {
600 iks_insert_attrib(error, "type", "cancel");
601 reason = iks_new(reasonstr);
602 if (reason)
603 iks_insert_node(error, reason);
604 iks_insert_node(response, error);
607 iks_send(client->connection->p, response);
608 if (reason)
609 iks_delete(reason);
610 if (error)
611 iks_delete(error);
612 iks_delete(response);
613 res = 0;
615 return res;
618 static int gtalk_is_answered(struct gtalk *client, ikspak *pak)
620 struct gtalk_pvt *tmp;
621 char *from;
622 iks *codec;
623 char s1[BUFSIZ], s2[BUFSIZ], s3[BUFSIZ];
624 int peernoncodeccapability;
626 ast_log(LOG_DEBUG, "The client is %s\n", client->name);
627 /* Make sure our new call doesn't exist yet */
628 for (tmp = client->p; tmp; tmp = tmp->next) {
629 if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid))
630 break;
633 /* codec points to the first <payload-type/> tag */
634 codec = iks_child(iks_child(iks_child(pak->x)));
635 while (codec) {
636 ast_rtp_set_m_type(tmp->rtp, atoi(iks_find_attrib(codec, "id")));
637 ast_rtp_set_rtpmap_type(tmp->rtp, atoi(iks_find_attrib(codec, "id")), "audio", iks_find_attrib(codec, "name"), 0);
638 codec = iks_next(codec);
641 /* Now gather all of the codecs that we are asked for */
642 ast_rtp_get_current_formats(tmp->rtp, &tmp->peercapability, &peernoncodeccapability);
644 /* at this point, we received an awser from the remote Gtalk client,
645 which allows us to compare capabilities */
646 tmp->jointcapability = tmp->capability & tmp->peercapability;
647 if (!tmp->jointcapability) {
648 ast_log(LOG_WARNING, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1, BUFSIZ, tmp->capability),
649 ast_getformatname_multiple(s2, BUFSIZ, tmp->peercapability),
650 ast_getformatname_multiple(s3, BUFSIZ, tmp->jointcapability));
651 /* close session if capabilities don't match */
652 ast_queue_hangup(tmp->owner);
654 return -1;
658 from = iks_find_attrib(pak->x, "to");
659 if(!from)
660 from = client->connection->jid->full;
662 if (tmp) {
663 if (tmp->owner)
664 ast_queue_control(tmp->owner, AST_CONTROL_ANSWER);
665 } else
666 ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
667 gtalk_response(client, from, pak, NULL, NULL);
668 return 1;
671 static int gtalk_is_accepted(struct gtalk *client, ikspak *pak)
673 struct gtalk_pvt *tmp;
674 char *from;
676 ast_log(LOG_DEBUG, "The client is %s\n", client->name);
677 /* find corresponding call */
678 for (tmp = client->p; tmp; tmp = tmp->next) {
679 if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid))
680 break;
683 from = iks_find_attrib(pak->x, "to");
684 if(!from)
685 from = client->connection->jid->full;
687 if (!tmp)
688 ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
690 /* answer 'iq' packet to let the remote peer know that we're alive */
691 gtalk_response(client, from, pak, NULL, NULL);
692 return 1;
695 static int gtalk_handle_dtmf(struct gtalk *client, ikspak *pak)
697 struct gtalk_pvt *tmp;
698 iks *dtmfnode = NULL, *dtmfchild = NULL;
699 char *dtmf;
700 char *from;
701 /* Make sure our new call doesn't exist yet */
702 for (tmp = client->p; tmp; tmp = tmp->next) {
703 if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid) || iks_find_with_attrib(pak->x, "gtalk", "sid", tmp->sid))
704 break;
706 from = iks_find_attrib(pak->x, "to");
707 if(!from)
708 from = client->connection->jid->full;
711 if (tmp) {
712 if(iks_find_with_attrib(pak->x, "dtmf-method", "method", "rtp")) {
713 gtalk_response(client, from, pak,
714 "feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
715 "unsupported-dtmf-method xmlns='http://jabber.org/protocol/gtalk/info/dtmf#errors'");
716 return -1;
718 if ((dtmfnode = iks_find(pak->x, "dtmf"))) {
719 if((dtmf = iks_find_attrib(dtmfnode, "code"))) {
720 if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-up")) {
721 struct ast_frame f = {AST_FRAME_DTMF_BEGIN, };
722 f.subclass = dtmf[0];
723 ast_queue_frame(tmp->owner, &f);
724 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass);
725 } else if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-down")) {
726 struct ast_frame f = {AST_FRAME_DTMF_END, };
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_attrib(pak->x, "dtmf")) { /* 250 millasecond default */
731 struct ast_frame f = {AST_FRAME_DTMF, };
732 f.subclass = dtmf[0];
733 ast_queue_frame(tmp->owner, &f);
734 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass);
737 } else if ((dtmfnode = iks_find_with_attrib(pak->x, "gtalk", "action", "session-info"))) {
738 if((dtmfchild = iks_find(dtmfnode, "dtmf"))) {
739 if((dtmf = iks_find_attrib(dtmfchild, "code"))) {
740 if(iks_find_with_attrib(dtmfnode, "dtmf", "action", "button-up")) {
741 struct ast_frame f = {AST_FRAME_DTMF_END, };
742 f.subclass = dtmf[0];
743 ast_queue_frame(tmp->owner, &f);
744 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass);
745 } else if(iks_find_with_attrib(dtmfnode, "dtmf", "action", "button-down")) {
746 struct ast_frame f = {AST_FRAME_DTMF_BEGIN, };
747 f.subclass = dtmf[0];
748 ast_queue_frame(tmp->owner, &f);
749 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass);
754 gtalk_response(client, from, pak, NULL, NULL);
755 return 1;
756 } else
757 ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
759 gtalk_response(client, from, pak, NULL, NULL);
760 return 1;
763 static int gtalk_hangup_farend(struct gtalk *client, ikspak *pak)
765 struct gtalk_pvt *tmp;
766 char *from;
768 ast_log(LOG_DEBUG, "The client is %s\n", client->name);
769 /* Make sure our new call doesn't exist yet */
770 for (tmp = client->p; tmp; tmp = tmp->next) {
771 if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid))
772 break;
774 from = iks_find_attrib(pak->x, "to");
775 if(!from)
776 from = client->connection->jid->full;
778 if (tmp) {
779 tmp->alreadygone = 1;
780 if (tmp->owner)
781 ast_queue_hangup(tmp->owner);
782 } else
783 ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
784 gtalk_response(client, from, pak, NULL, NULL);
785 return 1;
788 static int gtalk_create_candidates(struct gtalk *client, struct gtalk_pvt *p, char *sid, char *from, char *to)
790 struct gtalk_candidate *tmp;
791 struct aji_client *c = client->connection;
792 struct gtalk_candidate *ours1 = NULL, *ours2 = NULL;
793 struct sockaddr_in sin;
794 struct sockaddr_in dest;
795 struct in_addr us;
796 iks *iq, *gtalk, *candidate, *transport;
797 char user[17], pass[17], preference[5], port[7];
800 iq = iks_new("iq");
801 gtalk = iks_new("session");
802 candidate = iks_new("candidate");
803 transport = iks_new("transport");
804 if (!iq || !gtalk || !candidate || !transport) {
805 ast_log(LOG_ERROR, "Memory allocation error\n");
806 goto safeout;
808 ours1 = ast_calloc(1, sizeof(*ours1));
809 ours2 = ast_calloc(1, sizeof(*ours2));
810 if (!ours1 || !ours2)
811 goto safeout;
813 iks_insert_attrib(transport, "xmlns","http://www.google.com/transport/p2p");
814 iks_insert_node(iq, gtalk);
815 iks_insert_node(gtalk,transport);
816 iks_insert_node(transport, candidate);
818 for (; p; p = p->next) {
819 if (!strcasecmp(p->sid, sid))
820 break;
823 if (!p) {
824 ast_log(LOG_NOTICE, "No matching gtalk session - SID %s!\n", sid);
825 goto safeout;
828 ast_rtp_get_us(p->rtp, &sin);
829 ast_find_ourip(&us, bindaddr);
831 /* Setup our gtalk candidates */
832 ast_copy_string(ours1->name, "rtp", sizeof(ours1->name));
833 ours1->port = ntohs(sin.sin_port);
834 ours1->preference = 1;
835 snprintf(user, sizeof(user), "%08lx%08lx", ast_random(), ast_random());
836 snprintf(pass, sizeof(pass), "%08lx%08lx", ast_random(), ast_random());
837 ast_copy_string(ours1->username, user, sizeof(ours1->username));
838 ast_copy_string(ours1->password, pass, sizeof(ours1->password));
839 ast_copy_string(ours1->ip, ast_inet_ntoa(us), sizeof(ours1->ip));
840 ours1->protocol = AJI_PROTOCOL_UDP;
841 ours1->type = AJI_CONNECT_LOCAL;
842 ours1->generation = 0;
843 p->ourcandidates = ours1;
845 if (!ast_strlen_zero(externip)) {
846 /* XXX We should really stun for this one not just go with externip XXX */
847 snprintf(user, sizeof(user), "%08lx%08lx", ast_random(), ast_random());
848 snprintf(pass, sizeof(pass), "%08lx%08lx", ast_random(), ast_random());
849 ast_copy_string(ours2->username, user, sizeof(ours2->username));
850 ast_copy_string(ours2->password, pass, sizeof(ours2->password));
851 ast_copy_string(ours2->ip, externip, sizeof(ours2->ip));
852 ast_copy_string(ours2->name, "rtp", sizeof(ours1->name));
853 ours2->port = ntohs(sin.sin_port);
854 ours2->preference = 0.9;
855 ours2->protocol = AJI_PROTOCOL_UDP;
856 ours2->type = AJI_CONNECT_STUN;
857 ours2->generation = 0;
858 ours1->next = ours2;
859 ours2 = NULL;
861 ours1 = NULL;
862 dest.sin_addr = __ourip;
863 dest.sin_port = sin.sin_port;
866 for (tmp = p->ourcandidates; tmp; tmp = tmp->next) {
867 snprintf(port, sizeof(port), "%d", tmp->port);
868 snprintf(preference, sizeof(preference), "%.2f", tmp->preference);
869 iks_insert_attrib(iq, "from", to);
870 iks_insert_attrib(iq, "to", from);
871 iks_insert_attrib(iq, "type", "set");
872 iks_insert_attrib(iq, "id", c->mid);
873 ast_aji_increment_mid(c->mid);
874 iks_insert_attrib(gtalk, "type", "transport-info");
875 iks_insert_attrib(gtalk, "id", sid);
876 iks_insert_attrib(gtalk, "initiator", (p->initiator) ? to : from);
877 iks_insert_attrib(gtalk, "xmlns", GOOGLE_NS);
878 iks_insert_attrib(candidate, "name", tmp->name);
879 iks_insert_attrib(candidate, "address", tmp->ip);
880 iks_insert_attrib(candidate, "port", port);
881 iks_insert_attrib(candidate, "username", tmp->username);
882 iks_insert_attrib(candidate, "password", tmp->password);
883 iks_insert_attrib(candidate, "preference", preference);
884 if (tmp->protocol == AJI_PROTOCOL_UDP)
885 iks_insert_attrib(candidate, "protocol", "udp");
886 if (tmp->protocol == AJI_PROTOCOL_SSLTCP)
887 iks_insert_attrib(candidate, "protocol", "ssltcp");
888 if (tmp->type == AJI_CONNECT_STUN)
889 iks_insert_attrib(candidate, "type", "stun");
890 if (tmp->type == AJI_CONNECT_LOCAL)
891 iks_insert_attrib(candidate, "type", "local");
892 if (tmp->type == AJI_CONNECT_RELAY)
893 iks_insert_attrib(candidate, "type", "relay");
894 iks_insert_attrib(candidate, "network", "0");
895 iks_insert_attrib(candidate, "generation", "0");
896 iks_send(c->p, iq);
898 p->laststun = 0;
900 safeout:
901 if (ours1)
902 free(ours1);
903 if (ours2)
904 free(ours2);
905 if (iq)
906 iks_delete(iq);
907 if (gtalk)
908 iks_delete(gtalk);
909 if (candidate)
910 iks_delete(candidate);
911 if(transport)
912 iks_delete(transport);
913 return 1;
916 static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const char *them, const char *sid)
918 struct gtalk_pvt *tmp = NULL;
919 struct aji_resource *resources = NULL;
920 struct aji_buddy *buddy;
921 char idroster[200];
922 char *data, *exten = NULL;
924 if (option_debug)
925 ast_log(LOG_DEBUG, "The client is %s for alloc\n", client->name);
926 if (!sid && !strchr(them, '/')) { /* I started call! */
927 if (!strcasecmp(client->name, "guest")) {
928 buddy = ASTOBJ_CONTAINER_FIND(&client->connection->buddies, them);
929 if (buddy)
930 resources = buddy->resources;
931 } else if (client->buddy)
932 resources = client->buddy->resources;
933 while (resources) {
934 if (resources->cap->jingle) {
935 break;
937 resources = resources->next;
939 if (resources)
940 snprintf(idroster, sizeof(idroster), "%s/%s", them, resources->resource);
941 else {
942 ast_log(LOG_ERROR, "no gtalk capable clients to talk to.\n");
943 return NULL;
946 if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
947 return NULL;
949 if (sid) {
950 ast_copy_string(tmp->sid, sid, sizeof(tmp->sid));
951 ast_copy_string(tmp->them, them, sizeof(tmp->them));
952 ast_copy_string(tmp->us, us, sizeof(tmp->us));
953 } else {
954 snprintf(tmp->sid, sizeof(tmp->sid), "%08lx%08lx", ast_random(), ast_random());
955 ast_copy_string(tmp->them, idroster, sizeof(tmp->them));
956 ast_copy_string(tmp->us, us, sizeof(tmp->us));
957 tmp->initiator = 1;
959 /* clear codecs */
960 tmp->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
961 ast_rtp_pt_clear(tmp->rtp);
963 /* add user configured codec capabilites */
964 if (client->capability)
965 tmp->capability = client->capability;
966 else if (global_capability)
967 tmp->capability = global_capability;
969 tmp->parent = client;
970 if (!tmp->rtp) {
971 ast_log(LOG_WARNING, "Out of RTP sessions?\n");
972 free(tmp);
973 return NULL;
976 /* Set CALLERID(name) to the full JID of the remote peer */
977 ast_copy_string(tmp->cid_name, tmp->them, sizeof(tmp->cid_name));
979 if(strchr(tmp->us, '/')) {
980 data = ast_strdupa(tmp->us);
981 exten = strsep(&data, "/");
982 } else
983 exten = tmp->us;
984 ast_copy_string(tmp->exten, exten, sizeof(tmp->exten));
985 ast_mutex_init(&tmp->lock);
986 ast_mutex_lock(&gtalklock);
987 tmp->next = client->p;
988 client->p = tmp;
989 ast_mutex_unlock(&gtalklock);
990 return tmp;
993 /*! \brief Start new gtalk channel */
994 static struct ast_channel *gtalk_new(struct gtalk *client, struct gtalk_pvt *i, int state, const char *title)
996 struct ast_channel *tmp;
997 int fmt;
998 int what;
999 const char *n2;
1001 if (title)
1002 n2 = title;
1003 else
1004 n2 = i->us;
1005 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);
1006 if (!tmp) {
1007 ast_log(LOG_WARNING, "Unable to allocate Gtalk channel structure!\n");
1008 return NULL;
1010 tmp->tech = &gtalk_tech;
1012 /* Select our native format based on codec preference until we receive
1013 something from another device to the contrary. */
1014 if (i->jointcapability)
1015 what = i->jointcapability;
1016 else if (i->capability)
1017 what = i->capability;
1018 else
1019 what = global_capability;
1020 tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | (i->jointcapability & AST_FORMAT_VIDEO_MASK);
1021 fmt = ast_best_codec(tmp->nativeformats);
1023 if (i->rtp) {
1024 ast_rtp_setstun(i->rtp, 1);
1025 tmp->fds[0] = ast_rtp_fd(i->rtp);
1026 tmp->fds[1] = ast_rtcp_fd(i->rtp);
1028 if (i->vrtp) {
1029 ast_rtp_setstun(i->rtp, 1);
1030 tmp->fds[2] = ast_rtp_fd(i->vrtp);
1031 tmp->fds[3] = ast_rtcp_fd(i->vrtp);
1033 if (state == AST_STATE_RING)
1034 tmp->rings = 1;
1035 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
1036 tmp->writeformat = fmt;
1037 tmp->rawwriteformat = fmt;
1038 tmp->readformat = fmt;
1039 tmp->rawreadformat = fmt;
1040 tmp->tech_pvt = i;
1042 tmp->callgroup = client->callgroup;
1043 tmp->pickupgroup = client->pickupgroup;
1044 tmp->cid.cid_pres = client->callingpres;
1045 if (!ast_strlen_zero(client->accountcode))
1046 ast_string_field_set(tmp, accountcode, client->accountcode);
1047 if (client->amaflags)
1048 tmp->amaflags = client->amaflags;
1049 if (!ast_strlen_zero(client->language))
1050 ast_string_field_set(tmp, language, client->language);
1051 if (!ast_strlen_zero(client->musicclass))
1052 ast_string_field_set(tmp, musicclass, client->musicclass);
1053 i->owner = tmp;
1054 ast_module_ref(ast_module_info->self);
1055 ast_copy_string(tmp->context, client->context, sizeof(tmp->context));
1056 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
1058 if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
1059 tmp->cid.cid_dnid = ast_strdup(i->exten);
1060 tmp->priority = 1;
1061 if (i->rtp)
1062 ast_jb_configure(tmp, &global_jbconf);
1063 if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
1064 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
1065 tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
1066 ast_hangup(tmp);
1067 tmp = NULL;
1070 return tmp;
1073 static int gtalk_action(struct gtalk *client, struct gtalk_pvt *p, const char *action)
1075 iks *request, *session = NULL;
1076 int res = -1;
1078 request = iks_new("iq");
1079 if (request) {
1080 iks_insert_attrib(request, "type", "set");
1081 iks_insert_attrib(request, "from", p->us);
1082 iks_insert_attrib(request, "to", p->them);
1083 iks_insert_attrib(request, "id", client->connection->mid);
1084 ast_aji_increment_mid(client->connection->mid);
1085 session = iks_new("session");
1086 if (session) {
1087 iks_insert_attrib(session, "type", action);
1088 iks_insert_attrib(session, "id", p->sid);
1089 iks_insert_attrib(session, "initiator", p->initiator ? p->us : p->them);
1090 iks_insert_attrib(session, "xmlns", "http://www.google.com/session");
1091 iks_insert_node(request, session);
1092 iks_send(client->connection->p, request);
1093 iks_delete(session);
1094 res = 0;
1096 iks_delete(request);
1098 return res;
1101 static void gtalk_free_candidates(struct gtalk_candidate *candidate)
1103 struct gtalk_candidate *last;
1104 while (candidate) {
1105 last = candidate;
1106 candidate = candidate->next;
1107 free(last);
1111 static void gtalk_free_pvt(struct gtalk *client, struct gtalk_pvt *p)
1113 struct gtalk_pvt *cur, *prev = NULL;
1114 cur = client->p;
1115 while (cur) {
1116 if (cur == p) {
1117 if (prev)
1118 prev->next = p->next;
1119 else
1120 client->p = p->next;
1121 break;
1123 prev = cur;
1124 cur = cur->next;
1126 if (p->ringrule)
1127 iks_filter_remove_rule(p->parent->connection->f, p->ringrule);
1128 if (p->owner)
1129 ast_log(LOG_WARNING, "Uh oh, there's an owner, this is going to be messy.\n");
1130 if (p->rtp)
1131 ast_rtp_destroy(p->rtp);
1132 if (p->vrtp)
1133 ast_rtp_destroy(p->vrtp);
1134 gtalk_free_candidates(p->theircandidates);
1135 free(p);
1139 static int gtalk_newcall(struct gtalk *client, ikspak *pak)
1141 struct gtalk_pvt *p, *tmp = client->p;
1142 struct ast_channel *chan;
1143 int res;
1144 iks *codec;
1145 char *from = NULL;
1146 char s1[BUFSIZ], s2[BUFSIZ], s3[BUFSIZ];
1147 int peernoncodeccapability;
1149 /* Make sure our new call doesn't exist yet */
1150 from = iks_find_attrib(pak->x,"to");
1151 if(!from)
1152 from = client->connection->jid->full;
1154 while (tmp) {
1155 if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) {
1156 ast_log(LOG_NOTICE, "Ignoring duplicate call setup on SID %s\n", tmp->sid);
1157 gtalk_response(client, from, pak, "out-of-order", NULL);
1158 return -1;
1160 tmp = tmp->next;
1163 if (!strcasecmp(client->name, "guest")){
1164 /* the guest account is not tied to any configured XMPP client,
1165 let's set it now */
1166 client->connection = ast_aji_get_client(from);
1167 if (!client->connection) {
1168 ast_log(LOG_ERROR, "No XMPP client to talk to, us (partial JID) : %s\n", from);
1169 return -1;
1173 p = gtalk_alloc(client, from, pak->from->full, iks_find_attrib(pak->query, "id"));
1174 if (!p) {
1175 ast_log(LOG_WARNING, "Unable to allocate gtalk structure!\n");
1176 return -1;
1179 chan = gtalk_new(client, p, AST_STATE_DOWN, pak->from->user);
1180 if (!chan) {
1181 gtalk_free_pvt(client, p);
1182 return -1;
1185 ast_mutex_lock(&p->lock);
1186 ast_copy_string(p->them, pak->from->full, sizeof(p->them));
1187 if (iks_find_attrib(pak->query, "id")) {
1188 ast_copy_string(p->sid, iks_find_attrib(pak->query, "id"),
1189 sizeof(p->sid));
1192 /* codec points to the first <payload-type/> tag */
1193 codec = iks_child(iks_child(iks_child(pak->x)));
1195 while (codec) {
1196 ast_rtp_set_m_type(p->rtp, atoi(iks_find_attrib(codec, "id")));
1197 ast_rtp_set_rtpmap_type(p->rtp, atoi(iks_find_attrib(codec, "id")), "audio", iks_find_attrib(codec, "name"), 0);
1198 codec = iks_next(codec);
1201 /* Now gather all of the codecs that we are asked for */
1202 ast_rtp_get_current_formats(p->rtp, &p->peercapability, &peernoncodeccapability);
1203 p->jointcapability = p->capability & p->peercapability;
1204 ast_mutex_unlock(&p->lock);
1206 ast_setstate(chan, AST_STATE_RING);
1207 if (!p->jointcapability) {
1208 ast_log(LOG_WARNING, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1, BUFSIZ, p->capability),
1209 ast_getformatname_multiple(s2, BUFSIZ, p->peercapability),
1210 ast_getformatname_multiple(s3, BUFSIZ, p->jointcapability));
1211 /* close session if capabilities don't match */
1212 gtalk_action(client, p, "reject");
1213 p->alreadygone = 1;
1214 gtalk_hangup(chan);
1215 ast_channel_free(chan);
1216 return -1;
1219 res = ast_pbx_start(chan);
1221 switch (res) {
1222 case AST_PBX_FAILED:
1223 ast_log(LOG_WARNING, "Failed to start PBX :(\n");
1224 gtalk_response(client, from, pak, "service-unavailable", NULL);
1225 break;
1226 case AST_PBX_CALL_LIMIT:
1227 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
1228 gtalk_response(client, from, pak, "service-unavailable", NULL);
1229 break;
1230 case AST_PBX_SUCCESS:
1231 gtalk_response(client, from, pak, NULL, NULL);
1232 gtalk_invite_response(p, p->them, p->us,p->sid, 0);
1233 gtalk_create_candidates(client, p, p->sid, p->them, p->us);
1234 /* nothing to do */
1235 break;
1238 return 1;
1241 static int gtalk_update_stun(struct gtalk *client, struct gtalk_pvt *p)
1243 struct gtalk_candidate *tmp;
1244 struct hostent *hp;
1245 struct ast_hostent ahp;
1246 struct sockaddr_in sin;
1247 struct sockaddr_in aux;
1249 if (time(NULL) == p->laststun)
1250 return 0;
1252 tmp = p->theircandidates;
1253 p->laststun = time(NULL);
1254 while (tmp) {
1255 char username[256];
1257 /* Find the IP address of the host */
1258 hp = ast_gethostbyname(tmp->ip, &ahp);
1259 sin.sin_family = AF_INET;
1260 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
1261 sin.sin_port = htons(tmp->port);
1262 snprintf(username, sizeof(username), "%s%s", tmp->username,
1263 p->ourcandidates->username);
1265 /* Find out the result of the STUN */
1266 ast_rtp_get_peer(p->rtp, &aux);
1268 /* If the STUN result is different from the IP of the hostname,
1269 lock on the stun IP of the hostname advertised by the
1270 remote client */
1271 if (aux.sin_addr.s_addr &&
1272 aux.sin_addr.s_addr != sin.sin_addr.s_addr)
1273 ast_rtp_stun_request(p->rtp, &aux, username);
1274 else
1275 ast_rtp_stun_request(p->rtp, &sin, username);
1277 if (aux.sin_addr.s_addr && option_debug > 3) {
1278 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);
1279 ast_log(LOG_DEBUG, "Sending STUN request to %s\n", tmp->ip);
1282 tmp = tmp->next;
1284 return 1;
1287 static int gtalk_add_candidate(struct gtalk *client, ikspak *pak)
1289 struct gtalk_pvt *p = NULL, *tmp = NULL;
1290 struct aji_client *c = client->connection;
1291 struct gtalk_candidate *newcandidate = NULL;
1292 iks *traversenodes = NULL, *receipt = NULL;
1293 char *from;
1295 from = iks_find_attrib(pak->x,"to");
1296 if(!from)
1297 from = c->jid->full;
1299 for (tmp = client->p; tmp; tmp = tmp->next) {
1300 if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) {
1301 p = tmp;
1302 break;
1306 if (!p)
1307 return -1;
1309 traversenodes = pak->query;
1310 while(traversenodes) {
1311 if(!strcasecmp(iks_name(traversenodes), "session")) {
1312 traversenodes = iks_child(traversenodes);
1313 continue;
1315 if(!strcasecmp(iks_name(traversenodes), "transport")) {
1316 traversenodes = iks_child(traversenodes);
1317 continue;
1319 if(!strcasecmp(iks_name(traversenodes), "candidate")) {
1320 newcandidate = ast_calloc(1, sizeof(*newcandidate));
1321 if (!newcandidate)
1322 return 0;
1323 ast_copy_string(newcandidate->name, iks_find_attrib(traversenodes, "name"),
1324 sizeof(newcandidate->name));
1325 ast_copy_string(newcandidate->ip, iks_find_attrib(traversenodes, "address"),
1326 sizeof(newcandidate->ip));
1327 newcandidate->port = atoi(iks_find_attrib(traversenodes, "port"));
1328 ast_copy_string(newcandidate->username, iks_find_attrib(traversenodes, "username"),
1329 sizeof(newcandidate->username));
1330 ast_copy_string(newcandidate->password, iks_find_attrib(traversenodes, "password"),
1331 sizeof(newcandidate->password));
1332 newcandidate->preference = atof(iks_find_attrib(traversenodes, "preference"));
1333 if (!strcasecmp(iks_find_attrib(traversenodes, "protocol"), "udp"))
1334 newcandidate->protocol = AJI_PROTOCOL_UDP;
1335 if (!strcasecmp(iks_find_attrib(traversenodes, "protocol"), "ssltcp"))
1336 newcandidate->protocol = AJI_PROTOCOL_SSLTCP;
1338 if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "stun"))
1339 newcandidate->type = AJI_CONNECT_STUN;
1340 if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "local"))
1341 newcandidate->type = AJI_CONNECT_LOCAL;
1342 if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "relay"))
1343 newcandidate->type = AJI_CONNECT_RELAY;
1344 ast_copy_string(newcandidate->network, iks_find_attrib(traversenodes, "network"),
1345 sizeof(newcandidate->network));
1346 newcandidate->generation = atoi(iks_find_attrib(traversenodes, "generation"));
1347 newcandidate->next = NULL;
1349 newcandidate->next = p->theircandidates;
1350 p->theircandidates = newcandidate;
1351 p->laststun = 0;
1352 gtalk_update_stun(p->parent, p);
1353 newcandidate = NULL;
1355 traversenodes = iks_next(traversenodes);
1358 receipt = iks_new("iq");
1359 iks_insert_attrib(receipt, "type", "result");
1360 iks_insert_attrib(receipt, "from", from);
1361 iks_insert_attrib(receipt, "to", iks_find_attrib(pak->x, "from"));
1362 iks_insert_attrib(receipt, "id", iks_find_attrib(pak->x, "id"));
1363 iks_send(c->p, receipt);
1364 iks_delete(receipt);
1366 return 1;
1369 static struct ast_frame *gtalk_rtp_read(struct ast_channel *ast, struct gtalk_pvt *p)
1371 struct ast_frame *f;
1373 if (!p->rtp)
1374 return &ast_null_frame;
1375 f = ast_rtp_read(p->rtp);
1376 gtalk_update_stun(p->parent, p);
1377 if (p->owner) {
1378 /* We already hold the channel lock */
1379 if (f->frametype == AST_FRAME_VOICE) {
1380 if (f->subclass != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
1381 if (option_debug)
1382 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
1383 p->owner->nativeformats =
1384 (p->owner->nativeformats & AST_FORMAT_VIDEO_MASK) | f->subclass;
1385 ast_set_read_format(p->owner, p->owner->readformat);
1386 ast_set_write_format(p->owner, p->owner->writeformat);
1388 /* if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
1389 f = ast_dsp_process(p->owner, p->vad, f);
1390 if (option_debug && f && (f->frametype == AST_FRAME_DTMF))
1391 ast_log(LOG_DEBUG, "* Detected inband DTMF '%c'\n", f->subclass);
1392 } */
1395 return f;
1398 static struct ast_frame *gtalk_read(struct ast_channel *ast)
1400 struct ast_frame *fr;
1401 struct gtalk_pvt *p = ast->tech_pvt;
1403 ast_mutex_lock(&p->lock);
1404 fr = gtalk_rtp_read(ast, p);
1405 ast_mutex_unlock(&p->lock);
1406 return fr;
1409 /*! \brief Send frame to media channel (rtp) */
1410 static int gtalk_write(struct ast_channel *ast, struct ast_frame *frame)
1412 struct gtalk_pvt *p = ast->tech_pvt;
1413 int res = 0;
1415 switch (frame->frametype) {
1416 case AST_FRAME_VOICE:
1417 if (!(frame->subclass & ast->nativeformats)) {
1418 ast_log(LOG_WARNING,
1419 "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
1420 frame->subclass, ast->nativeformats, ast->readformat,
1421 ast->writeformat);
1422 return 0;
1424 if (p) {
1425 ast_mutex_lock(&p->lock);
1426 if (p->rtp) {
1427 res = ast_rtp_write(p->rtp, frame);
1429 ast_mutex_unlock(&p->lock);
1431 break;
1432 case AST_FRAME_VIDEO:
1433 if (p) {
1434 ast_mutex_lock(&p->lock);
1435 if (p->vrtp) {
1436 res = ast_rtp_write(p->vrtp, frame);
1438 ast_mutex_unlock(&p->lock);
1440 break;
1441 case AST_FRAME_IMAGE:
1442 return 0;
1443 break;
1444 default:
1445 ast_log(LOG_WARNING, "Can't send %d type frames with Gtalk write\n",
1446 frame->frametype);
1447 return 0;
1450 return res;
1453 static int gtalk_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1455 struct gtalk_pvt *p = newchan->tech_pvt;
1456 ast_mutex_lock(&p->lock);
1458 if ((p->owner != oldchan)) {
1459 ast_mutex_unlock(&p->lock);
1460 return -1;
1462 if (p->owner == oldchan)
1463 p->owner = newchan;
1464 ast_mutex_unlock(&p->lock);
1465 return 0;
1468 static int gtalk_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
1470 int res = 0;
1472 switch (condition) {
1473 case AST_CONTROL_HOLD:
1474 ast_moh_start(ast, data, NULL);
1475 break;
1476 case AST_CONTROL_UNHOLD:
1477 ast_moh_stop(ast);
1478 break;
1479 default:
1480 ast_log(LOG_NOTICE, "Don't know how to indicate condition '%d'\n", condition);
1481 res = -1;
1484 return res;
1487 static int gtalk_digit_begin(struct ast_channel *chan, char digit)
1489 return gtalk_digit(chan, digit, 0);
1492 static int gtalk_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
1494 return gtalk_digit(chan, digit, duration);
1497 static int gtalk_digit(struct ast_channel *ast, char digit, unsigned int duration)
1499 struct gtalk_pvt *p = ast->tech_pvt;
1500 struct gtalk *client = p->parent;
1501 iks *iq, *gtalk, *dtmf;
1502 char buffer[2] = {digit, '\0'};
1503 iq = iks_new("iq");
1504 gtalk = iks_new("gtalk");
1505 dtmf = iks_new("dtmf");
1506 if(!iq || !gtalk || !dtmf) {
1507 if(iq)
1508 iks_delete(iq);
1509 if(gtalk)
1510 iks_delete(gtalk);
1511 if(dtmf)
1512 iks_delete(dtmf);
1513 ast_log(LOG_ERROR, "Did not send dtmf do to memory issue\n");
1514 return -1;
1517 iks_insert_attrib(iq, "type", "set");
1518 iks_insert_attrib(iq, "to", p->them);
1519 iks_insert_attrib(iq, "from", p->us);
1520 iks_insert_attrib(iq, "id", client->connection->mid);
1521 ast_aji_increment_mid(client->connection->mid);
1522 iks_insert_attrib(gtalk, "xmlns", "http://jabber.org/protocol/gtalk");
1523 iks_insert_attrib(gtalk, "action", "session-info");
1524 iks_insert_attrib(gtalk, "initiator", p->initiator ? p->us: p->them);
1525 iks_insert_attrib(gtalk, "sid", p->sid);
1526 iks_insert_attrib(dtmf, "xmlns", "http://jabber.org/protocol/gtalk/info/dtmf");
1527 iks_insert_attrib(dtmf, "code", buffer);
1528 iks_insert_node(iq, gtalk);
1529 iks_insert_node(gtalk, dtmf);
1531 ast_mutex_lock(&p->lock);
1532 if (ast->dtmff.frametype == AST_FRAME_DTMF_BEGIN || duration == 0) {
1533 iks_insert_attrib(dtmf, "action", "button-down");
1534 } else if (ast->dtmff.frametype == AST_FRAME_DTMF_END || duration != 0) {
1535 iks_insert_attrib(dtmf, "action", "button-up");
1537 iks_send(client->connection->p, iq);
1538 iks_delete(iq);
1539 iks_delete(gtalk);
1540 iks_delete(dtmf);
1541 ast_mutex_unlock(&p->lock);
1542 return 0;
1545 static int gtalk_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
1547 ast_log(LOG_NOTICE, "XXX Implement gtalk sendhtml XXX\n");
1549 return -1;
1552 /* Not in use right now.
1553 static int gtalk_auto_congest(void *nothing)
1555 struct gtalk_pvt *p = nothing;
1557 ast_mutex_lock(&p->lock);
1558 if (p->owner) {
1559 if (!ast_channel_trylock(p->owner)) {
1560 ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
1561 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
1562 ast_channel_unlock(p->owner);
1565 ast_mutex_unlock(&p->lock);
1566 return 0;
1570 /*! \brief Initiate new call, part of PBX interface
1571 * dest is the dial string */
1572 static int gtalk_call(struct ast_channel *ast, char *dest, int timeout)
1574 struct gtalk_pvt *p = ast->tech_pvt;
1576 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1577 ast_log(LOG_WARNING, "gtalk_call called on %s, neither down nor reserved\n", ast->name);
1578 return -1;
1581 ast_setstate(ast, AST_STATE_RING);
1582 if (!p->ringrule) {
1583 ast_copy_string(p->ring, p->parent->connection->mid, sizeof(p->ring));
1584 p->ringrule = iks_filter_add_rule(p->parent->connection->f, gtalk_ringing_ack, p,
1585 IKS_RULE_ID, p->ring, IKS_RULE_DONE);
1586 } else
1587 ast_log(LOG_WARNING, "Whoa, already have a ring rule!\n");
1589 gtalk_invite(p, p->them, p->us, p->sid, 1);
1590 gtalk_create_candidates(p->parent, p, p->sid, p->them, p->us);
1592 return 0;
1595 /*! \brief Hangup a call through the gtalk proxy channel */
1596 static int gtalk_hangup(struct ast_channel *ast)
1598 struct gtalk_pvt *p = ast->tech_pvt;
1599 struct gtalk *client;
1601 ast_mutex_lock(&p->lock);
1602 client = p->parent;
1603 p->owner = NULL;
1604 ast->tech_pvt = NULL;
1605 if (!p->alreadygone)
1606 gtalk_action(client, p, "terminate");
1607 ast_mutex_unlock(&p->lock);
1609 gtalk_free_pvt(client, p);
1610 ast_module_unref(ast_module_info->self);
1612 return 0;
1615 /*! \brief Part of PBX interface */
1616 static struct ast_channel *gtalk_request(const char *type, int format, void *data, int *cause)
1618 struct gtalk_pvt *p = NULL;
1619 struct gtalk *client = NULL;
1620 char *sender = NULL, *to = NULL, *s = NULL;
1621 struct ast_channel *chan = NULL;
1623 if (data) {
1624 s = ast_strdupa(data);
1625 if (s) {
1626 sender = strsep(&s, "/");
1627 if (sender && (sender[0] != '\0'))
1628 to = strsep(&s, "/");
1629 if (!to) {
1630 ast_log(LOG_ERROR, "Bad arguments in Gtalk Dialstring: %s\n", (char*) data);
1631 return NULL;
1636 client = find_gtalk(to, sender);
1637 if (!client) {
1638 ast_log(LOG_WARNING, "Could not find recipient.\n");
1639 return NULL;
1641 if (!strcasecmp(client->name, "guest")){
1642 /* the guest account is not tied to any configured XMPP client,
1643 let's set it now */
1644 client->connection = ast_aji_get_client(sender);
1645 if (!client->connection) {
1646 ast_log(LOG_ERROR, "No XMPP client to talk to, us (partial JID) : %s\n", sender);
1647 ASTOBJ_UNREF(client, gtalk_member_destroy);
1648 return NULL;
1652 ASTOBJ_WRLOCK(client);
1653 p = gtalk_alloc(client, strchr(sender, '@') ? sender : client->connection->jid->full, strchr(to, '@') ? to : client->user, NULL);
1654 if (p)
1655 chan = gtalk_new(client, p, AST_STATE_DOWN, to);
1657 ASTOBJ_UNLOCK(client);
1658 return chan;
1661 /*! \brief CLI command "gtalk show channels" */
1662 static int gtalk_show_channels(int fd, int argc, char **argv)
1664 #define FORMAT "%-30.30s %-30.30s %-15.15s %-5.5s %-5.5s \n"
1665 struct gtalk_pvt *p;
1666 struct ast_channel *chan;
1667 int numchans = 0;
1668 char them[AJI_MAX_JIDLEN];
1669 char *jid = NULL;
1670 char *resource = NULL;
1672 if (argc != 3)
1673 return RESULT_SHOWUSAGE;
1675 ast_mutex_lock(&gtalklock);
1676 ast_cli(fd, FORMAT, "Channel", "Jabber ID", "Resource", "Read", "Write");
1677 ASTOBJ_CONTAINER_TRAVERSE(&gtalk_list, 1, {
1678 ASTOBJ_WRLOCK(iterator);
1679 p = iterator->p;
1680 while(p) {
1681 chan = p->owner;
1682 ast_copy_string(them, p->them, sizeof(them));
1683 jid = them;
1684 resource = strchr(them, '/');
1685 if (!resource)
1686 resource = "None";
1687 else {
1688 *resource = '\0';
1689 resource ++;
1691 if (chan)
1692 ast_cli(fd, FORMAT,
1693 chan->name,
1694 jid,
1695 resource,
1696 ast_getformatname(chan->readformat),
1697 ast_getformatname(chan->writeformat)
1699 else
1700 ast_log(LOG_WARNING, "No available channel\n");
1701 numchans ++;
1702 p = p->next;
1704 ASTOBJ_UNLOCK(iterator);
1707 ast_mutex_unlock(&gtalklock);
1709 ast_cli(fd, "%d active gtalk channel%s\n", numchans, (numchans != 1) ? "s" : "");
1710 return RESULT_SUCCESS;
1711 #undef FORMAT
1714 /*! \brief CLI command "gtalk show channels" */
1715 static int gtalk_do_reload(int fd, int argc, char **argv)
1717 ast_verbose("IT DOES WORK!\n");
1718 return RESULT_SUCCESS;
1721 static int gtalk_parser(void *data, ikspak *pak)
1723 struct gtalk *client = ASTOBJ_REF((struct gtalk *) data);
1725 if (iks_find_with_attrib(pak->x, "session", "type", "initiate")) {
1726 /* New call */
1727 gtalk_newcall(client, pak);
1728 } else if (iks_find_with_attrib(pak->x, "session", "type", "candidates") || iks_find_with_attrib(pak->x, "session", "type", "transport-info")) {
1729 if (option_debug > 2)
1730 ast_log(LOG_DEBUG, "About to add candidate!\n");
1731 gtalk_add_candidate(client, pak);
1732 if (option_debug > 2)
1733 ast_log(LOG_DEBUG, "Candidate Added!\n");
1734 } else if (iks_find_with_attrib(pak->x, "session", "type", "accept")) {
1735 gtalk_is_answered(client, pak);
1736 } else if (iks_find_with_attrib(pak->x, "session", "type", "transport-accept")) {
1737 gtalk_is_accepted(client, pak);
1738 } else if (iks_find_with_attrib(pak->x, "session", "type", "content-info") || iks_find_with_attrib(pak->x, "gtalk", "action", "session-info")) {
1739 gtalk_handle_dtmf(client, pak);
1740 } else if (iks_find_with_attrib(pak->x, "session", "type", "terminate")) {
1741 gtalk_hangup_farend(client, pak);
1742 } else if (iks_find_with_attrib(pak->x, "session", "type", "reject")) {
1743 gtalk_hangup_farend(client, pak);
1745 ASTOBJ_UNREF(client, gtalk_member_destroy);
1746 return IKS_FILTER_EAT;
1749 /* Not using this anymore probably take out soon
1750 static struct gtalk_candidate *gtalk_create_candidate(char *args)
1752 char *name, *type, *preference, *protocol;
1753 struct gtalk_candidate *res;
1754 res = malloc(sizeof(struct gtalk_candidate));
1755 memset(res, 0, sizeof(struct gtalk_candidate));
1756 if (args)
1757 name = args;
1758 if ((args = strchr(args, ','))) {
1759 *args = '\0';
1760 args++;
1761 preference = args;
1763 if ((args = strchr(args, ','))) {
1764 *args = '\0';
1765 args++;
1766 protocol = args;
1768 if ((args = strchr(args, ','))) {
1769 *args = '\0';
1770 args++;
1771 type = args;
1773 if (name)
1774 ast_copy_string(res->name, name, sizeof(res->name));
1775 if (preference) {
1776 res->preference = atof(preference);
1778 if (protocol) {
1779 if (!strcasecmp("udp", protocol))
1780 res->protocol = AJI_PROTOCOL_UDP;
1781 if (!strcasecmp("ssltcp", protocol))
1782 res->protocol = AJI_PROTOCOL_SSLTCP;
1784 if (type) {
1785 if (!strcasecmp("stun", type))
1786 res->type = AJI_CONNECT_STUN;
1787 if (!strcasecmp("local", type))
1788 res->type = AJI_CONNECT_LOCAL;
1789 if (!strcasecmp("relay", type))
1790 res->type = AJI_CONNECT_RELAY;
1793 return res;
1797 static int gtalk_create_member(char *label, struct ast_variable *var, int allowguest,
1798 struct ast_codec_pref prefs, char *context,
1799 struct gtalk *member)
1801 struct aji_client *client;
1803 if (!member)
1804 ast_log(LOG_WARNING, "Out of memory.\n");
1806 ast_copy_string(member->name, label, sizeof(member->name));
1807 ast_copy_string(member->user, label, sizeof(member->user));
1808 ast_copy_string(member->context, context, sizeof(member->context));
1809 member->allowguest = allowguest;
1810 member->prefs = prefs;
1811 while (var) {
1812 #if 0
1813 struct gtalk_candidate *candidate = NULL;
1814 #endif
1815 if (!strcasecmp(var->name, "username"))
1816 ast_copy_string(member->user, var->value, sizeof(member->user));
1817 else if (!strcasecmp(var->name, "disallow"))
1818 ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 0);
1819 else if (!strcasecmp(var->name, "allow"))
1820 ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 1);
1821 else if (!strcasecmp(var->name, "context"))
1822 ast_copy_string(member->context, var->value, sizeof(member->context));
1823 #if 0
1824 else if (!strcasecmp(var->name, "candidate")) {
1825 candidate = gtalk_create_candidate(var->value);
1826 if (candidate) {
1827 candidate->next = member->ourcandidates;
1828 member->ourcandidates = candidate;
1831 #endif
1832 else if (!strcasecmp(var->name, "connection")) {
1833 if ((client = ast_aji_get_client(var->value))) {
1834 member->connection = client;
1835 iks_filter_add_rule(client->f, gtalk_parser, member,
1836 IKS_RULE_TYPE, IKS_PAK_IQ,
1837 IKS_RULE_FROM_PARTIAL, member->user,
1838 IKS_RULE_NS, "http://www.google.com/session",
1839 IKS_RULE_DONE);
1841 } else {
1842 ast_log(LOG_ERROR, "connection referenced not found!\n");
1843 return 0;
1846 var = var->next;
1848 if (member->connection && member->user)
1849 member->buddy = ASTOBJ_CONTAINER_FIND(&member->connection->buddies, member->user);
1850 else {
1851 ast_log(LOG_ERROR, "No Connection or Username!\n");
1853 return 1;
1856 static int gtalk_load_config(void)
1858 char *cat = NULL;
1859 struct ast_config *cfg = NULL;
1860 char context[AST_MAX_CONTEXT];
1861 int allowguest = 1;
1862 struct ast_variable *var;
1863 struct gtalk *member;
1864 struct ast_codec_pref prefs;
1865 struct aji_client_container *clients;
1866 struct gtalk_candidate *global_candidates = NULL;
1867 struct hostent *hp;
1868 struct ast_hostent ahp;
1870 cfg = ast_config_load(GOOGLE_CONFIG);
1871 if (!cfg)
1872 return 0;
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))
1881 continue;
1883 if (!strcasecmp(var->name, "allowguest"))
1884 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, "bindaddr")) {
1893 if (!(hp = ast_gethostbyname(var->value, &ahp))) {
1894 ast_log(LOG_WARNING, "Invalid address: %s\n", var->value);
1895 } else {
1896 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
1899 /* Idea to allow for custom candidates */
1901 else if (!strcasecmp(var->name, "candidate")) {
1902 candidate = gtalk_create_candidate(var->value);
1903 if (candidate) {
1904 candidate->next = global_candidates;
1905 global_candidates = candidate;
1910 while (cat) {
1911 if (strcasecmp(cat, "general")) {
1912 var = ast_variable_browse(cfg, cat);
1913 member = (struct gtalk *) malloc(sizeof(struct gtalk));
1914 memset(member, 0, sizeof(struct gtalk));
1915 ASTOBJ_INIT(member);
1916 ASTOBJ_WRLOCK(member);
1917 if (!strcasecmp(cat, "guest")) {
1918 ast_copy_string(member->name, "guest", sizeof(member->name));
1919 ast_copy_string(member->user, "guest", sizeof(member->user));
1920 ast_copy_string(member->context, context, sizeof(member->context));
1921 member->allowguest = allowguest;
1922 member->prefs = prefs;
1923 while (var) {
1924 if (!strcasecmp(var->name, "disallow"))
1925 ast_parse_allow_disallow(&member->prefs, &member->capability,
1926 var->value, 0);
1927 else if (!strcasecmp(var->name, "allow"))
1928 ast_parse_allow_disallow(&member->prefs, &member->capability,
1929 var->value, 1);
1930 else if (!strcasecmp(var->name, "context"))
1931 ast_copy_string(member->context, var->value,
1932 sizeof(member->context));
1933 /* Idea to allow for custom candidates */
1935 else if (!strcasecmp(var->name, "candidate")) {
1936 candidate = gtalk_create_candidate(var->value);
1937 if (candidate) {
1938 candidate->next = member->ourcandidates;
1939 member->ourcandidates = candidate;
1943 var = var->next;
1945 ASTOBJ_UNLOCK(member);
1946 clients = ast_aji_get_clients();
1947 if (clients) {
1948 ASTOBJ_CONTAINER_TRAVERSE(clients, 1, {
1949 ASTOBJ_WRLOCK(iterator);
1950 ASTOBJ_WRLOCK(member);
1951 member->connection = NULL;
1952 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);
1953 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);
1954 ASTOBJ_UNLOCK(member);
1955 ASTOBJ_UNLOCK(iterator);
1957 ASTOBJ_CONTAINER_LINK(&gtalk_list, member);
1958 ASTOBJ_UNREF(member, gtalk_member_destroy);
1959 } else {
1960 ASTOBJ_UNLOCK(member);
1961 ASTOBJ_UNREF(member, gtalk_member_destroy);
1963 } else {
1964 ASTOBJ_UNLOCK(member);
1965 if (gtalk_create_member(cat, var, allowguest, prefs, context, member))
1966 ASTOBJ_CONTAINER_LINK(&gtalk_list, member);
1967 ASTOBJ_UNREF(member, gtalk_member_destroy);
1970 cat = ast_category_browse(cfg, cat);
1972 gtalk_free_candidates(global_candidates);
1973 return 1;
1976 /*! \brief Load module into PBX, register channel */
1977 static int load_module(void)
1979 char *jabber_loaded = ast_module_helper("", "res_jabber.so", 0, 0, 0, 0);
1980 free(jabber_loaded);
1981 if (!jabber_loaded) {
1982 /* If embedded, check for a different module name */
1983 jabber_loaded = ast_module_helper("", "res_jabber", 0, 0, 0, 0);
1984 free(jabber_loaded);
1985 if (!jabber_loaded) {
1986 ast_log(LOG_ERROR, "chan_gtalk.so depends upon res_jabber.so\n");
1987 return AST_MODULE_LOAD_DECLINE;
1991 #ifdef HAVE_GNUTLS
1992 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
1993 #endif /* HAVE_GNUTLS */
1995 ASTOBJ_CONTAINER_INIT(&gtalk_list);
1996 if (!gtalk_load_config()) {
1997 ast_log(LOG_ERROR, "Unable to read config file %s. Not loading module.\n", GOOGLE_CONFIG);
1998 return 0;
2001 sched = sched_context_create();
2002 if (!sched)
2003 ast_log(LOG_WARNING, "Unable to create schedule context\n");
2005 io = io_context_create();
2006 if (!io)
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");
2011 return 0;
2014 ast_rtp_proto_register(&gtalk_rtp);
2015 ast_cli_register_multiple(gtalk_cli, sizeof(gtalk_cli) / sizeof(gtalk_cli[0]));
2017 /* Make sure we can register our channel type */
2018 if (ast_channel_register(&gtalk_tech)) {
2019 ast_log(LOG_ERROR, "Unable to register channel class %s\n", gtalk_tech.type);
2020 return -1;
2022 return 0;
2025 /*! \brief Reload module */
2026 static int reload(void)
2028 return 0;
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, sizeof(gtalk_cli) / sizeof(gtalk_cli[0]));
2036 /* First, take us out of the channel loop */
2037 ast_channel_unregister(&gtalk_tech);
2038 ast_rtp_proto_unregister(&gtalk_rtp);
2040 if (!ast_mutex_lock(&gtalklock)) {
2041 /* Hangup all interfaces if they have an owner */
2042 ASTOBJ_CONTAINER_TRAVERSE(&gtalk_list, 1, {
2043 ASTOBJ_WRLOCK(iterator);
2044 privates = iterator->p;
2045 while(privates) {
2046 if (privates->owner)
2047 ast_softhangup(privates->owner, AST_SOFTHANGUP_APPUNLOAD);
2048 privates = privates->next;
2050 iterator->p = NULL;
2051 ASTOBJ_UNLOCK(iterator);
2053 ast_mutex_unlock(&gtalklock);
2054 } else {
2055 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
2056 return -1;
2058 ASTOBJ_CONTAINER_DESTROYALL(&gtalk_list, gtalk_member_destroy);
2059 ASTOBJ_CONTAINER_DESTROY(&gtalk_list);
2060 return 0;
2063 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Gtalk Channel Driver",
2064 .load = load_module,
2065 .unload = unload_module,
2066 .reload = reload,