include "logger.h" and errno.h from asterisk.h - usage shows that they
[asterisk/asterisk.git] / channels / chan_gtalk.c
index e3a71c6..e28684e 100644 (file)
 
 /*** MODULEINFO
        <depend>iksemel</depend>
+       <depend>res_jabber</depend>
+       <use>openssl</use>
  ***/
 
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
 #include <sys/socket.h>
-#include <errno.h>
-#include <stdlib.h>
 #include <fcntl.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <sys/signal.h>
 #include <iksemel.h>
+#include <pthread.h>
 
 #include "asterisk/lock.h"
 #include "asterisk/channel.h"
 #include "asterisk/config.h"
-#include "asterisk/logger.h"
 #include "asterisk/module.h"
 #include "asterisk/pbx.h"
 #include "asterisk/options.h"
-#include "asterisk/lock.h"
 #include "asterisk/sched.h"
 #include "asterisk/io.h"
 #include "asterisk/rtp.h"
@@ -103,8 +98,8 @@ struct gtalk_pvt {
        time_t laststun;
        struct gtalk *parent;            /*!< Parent client */
        char sid[100];
-       char us[100];
-       char them[100];
+       char us[AJI_MAX_JIDLEN];
+       char them[AJI_MAX_JIDLEN];
        char ring[10];                   /*!< Message ID of ring */
        iksrule *ringrule;               /*!< Rule for matching RING request */
        int initiator;                   /*!< If we're the initiator */
@@ -146,8 +141,8 @@ struct gtalk {
        struct gtalk_pvt *p;
        struct ast_codec_pref prefs;
        int amaflags;                   /*!< AMA Flags */
-       char user[100];
-       char context[100];
+       char user[AJI_MAX_JIDLEN];
+       char context[AST_MAX_CONTEXT];
        char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
        int capability;
        ast_group_t callgroup;  /*!< Call group */
@@ -163,7 +158,6 @@ struct gtalk_container {
 };
 
 static const char desc[] = "Gtalk Channel";
-static const char type[] = "Gtalk";
 
 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
 
@@ -171,7 +165,9 @@ AST_MUTEX_DEFINE_STATIC(gtalklock); /*!< Protect the interface list (of gtalk_pv
 
 /* Forward declarations */
 static struct ast_channel *gtalk_request(const char *type, int format, void *data, int *cause);
-static int gtalk_digit(struct ast_channel *ast, char digit);
+static int gtalk_digit(struct ast_channel *ast, char digit, unsigned int duration);
+static int gtalk_digit_begin(struct ast_channel *ast, char digit);
+static int gtalk_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
 static int gtalk_call(struct ast_channel *ast, char *dest, int timeout);
 static int gtalk_hangup(struct ast_channel *ast);
 static int gtalk_answer(struct ast_channel *ast);
@@ -182,22 +178,22 @@ static int gtalk_indicate(struct ast_channel *ast, int condition, const void *da
 static int gtalk_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
 static int gtalk_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
 static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const char *them, const char *sid);
-static int gtalk_do_reload(int fd, int argc, char **argv);
-static int gtalk_show_channels(int fd, int argc, char **argv);
+static char *gtalk_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
+static char *gtalk_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 /*----- RTP interface functions */
 static int gtalk_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp,
-                                                          struct ast_rtp *vrtp, int codecs, int nat_active);
+                                                          struct ast_rtp *vrtp, struct ast_rtp *trtp, int codecs, int nat_active);
 static enum ast_rtp_get_result gtalk_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
 static int gtalk_get_codec(struct ast_channel *chan);
 
 /*! \brief PBX interface structure for channel registration */
 static const struct ast_channel_tech gtalk_tech = {
-       .type = type,
+       .type = "Gtalk",
        .description = "Gtalk Channel Driver",
-       .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
+       .capabilities = AST_FORMAT_AUDIO_MASK,
        .requester = gtalk_request,
-       .send_digit_begin = gtalk_digit,
-       .send_digit_end = gtalk_digit,
+       .send_digit_begin = gtalk_digit_begin,
+       .send_digit_end = gtalk_digit_end,
        .bridge = ast_rtp_bridge,
        .call = gtalk_call,
        .hangup = gtalk_hangup,
@@ -217,30 +213,18 @@ static struct sched_context *sched;       /*!< The scheduling context */
 static struct io_context *io;  /*!< The IO context */
 static struct in_addr __ourip;
 
-
 /*! \brief RTP driver interface */
 static struct ast_rtp_protocol gtalk_rtp = {
-       type: "gtalk",
+       type: "Gtalk",
        get_rtp_info: gtalk_get_rtp_peer,
        set_rtp_peer: gtalk_set_rtp_peer,
        get_codec: gtalk_get_codec,
 };
 
-static char debug_usage[] = 
-"Usage: gtalk show channels\n" 
-"       Shows current state of the Gtalk channels.\n";
-
-static char reload_usage[] = 
-"Usage: gtalk reload\n" 
-"       Reload gtalk channel driver.\n";
-
-
 static struct ast_cli_entry gtalk_cli[] = {
-       {{ "gtalk", "reload", NULL}, gtalk_do_reload, "Enable Jabber debugging", reload_usage },
-       {{ "gtalk", "show", "channels", NULL}, gtalk_show_channels, "Show GoogleTalk Channels", debug_usage },
- };
-
-
+       AST_CLI_DEFINE(gtalk_do_reload, "Reload GoogleTalk configuration"),
+       AST_CLI_DEFINE(gtalk_show_channels, "Show GoogleTalk channels"),
+};
 
 static char externip[16];
 
@@ -248,18 +232,18 @@ static struct gtalk_container gtalk_list;
 
 static void gtalk_member_destroy(struct gtalk *obj)
 {
-       free(obj);
+       ast_free(obj);
 }
 
 static struct gtalk *find_gtalk(char *name, char *connection)
 {
        struct gtalk *gtalk = NULL;
        char *domain = NULL , *s = NULL;
-       if(strchr(connection, '@')) {
-               s = ast_strdupa((char *) connection);
+
+       if (strchr(connection, '@')) {
+               s = ast_strdupa(connection);
                domain = strsep(&s, "@");
                ast_verbose("OOOOH domain = %s\n", domain);
-               free(s);
        }
        gtalk = ASTOBJ_CONTAINER_FIND(&gtalk_list, name);
        if (!gtalk && strchr(name, '@'))
@@ -267,20 +251,20 @@ static struct gtalk *find_gtalk(char *name, char *connection)
 
        if (!gtalk) {                           /* guest call */
                ASTOBJ_CONTAINER_TRAVERSE(&gtalk_list, 1, {
-                       ASTOBJ_WRLOCK(iterator);
+                       ASTOBJ_RDLOCK(iterator);
                        if (!strcasecmp(iterator->name, "guest")) {
                                if (!strcasecmp(iterator->connection->jid->partial, connection)) {
                                        gtalk = iterator;
-                                       break;
                                } else if (!strcasecmp(iterator->connection->name, connection)) {
                                        gtalk = iterator;
-                                       break;
                                } else if (iterator->connection->component && !strcasecmp(iterator->connection->user,domain)) {
                                        gtalk = iterator;
-                                       break;
                                }
                        }
                        ASTOBJ_UNLOCK(iterator);
+
+                       if (gtalk)
+                               break;
                });
 
        }
@@ -346,7 +330,7 @@ static int add_codec_to_answer(const struct gtalk_pvt *p, int codec, iks *dcodec
                        ast_log(LOG_WARNING,"Failed to allocate iks node");
                        return -1;
                }
-               iks_insert_attrib(payload_ilbc, "id", "102");
+               iks_insert_attrib(payload_ilbc, "id", "97");
                iks_insert_attrib(payload_ilbc, "name", "iLBC");
                iks_insert_attrib(payload_ilbc, "clockrate","8000");
                iks_insert_attrib(payload_ilbc, "bitrate","13300");
@@ -370,7 +354,7 @@ static int add_codec_to_answer(const struct gtalk_pvt *p, int codec, iks *dcodec
                        ast_log(LOG_WARNING,"Failed to allocate iks node");
                        return -1;
                }
-               iks_insert_attrib(payload_speex, "id", "98");
+               iks_insert_attrib(payload_speex, "id", "110");
                iks_insert_attrib(payload_speex, "name", "speex");
                iks_insert_attrib(payload_speex, "clockrate","8000");
                iks_insert_attrib(payload_speex, "bitrate","11000");
@@ -444,7 +428,7 @@ static int gtalk_invite(struct gtalk_pvt *p, char *to, char *from, char *sid, in
        iks_insert_node(gtalk, transport);
        iks_insert_node(dcodecs, payload_telephone);
 
-       iks_send(client->connection->p, iq);
+       ast_aji_send(client->connection, iq);
        iks_delete(payload_telephone);
        iks_delete(transport);
        iks_delete(dcodecs);
@@ -481,7 +465,7 @@ static int gtalk_invite_response(struct gtalk_pvt *p, char *to , char *from, cha
        iks_insert_attrib(transport, "xmlns", "http://www.google.com/transport/p2p");
        iks_insert_node(iq,session);
        iks_insert_node(session,transport);
-       iks_send(p->parent->connection->p, iq);
+       ast_aji_send(p->parent->connection, iq);
        iks_delete(transport);
        iks_delete(session);
        iks_delete(iq);
@@ -506,8 +490,7 @@ static int gtalk_answer(struct ast_channel *ast)
        struct gtalk_pvt *p = ast->tech_pvt;
        int res = 0;
        
-       if (option_debug)
-               ast_log(LOG_DEBUG, "Answer!\n");
+       ast_debug(1, "Answer!\n");
        ast_mutex_lock(&p->lock);
        gtalk_invite(p, p->them, p->us,p->sid, 0);
        ast_mutex_unlock(&p->lock);
@@ -525,7 +508,7 @@ static enum ast_rtp_get_result gtalk_get_rtp_peer(struct ast_channel *chan, stru
        ast_mutex_lock(&p->lock);
        if (p->rtp){
                *rtp = p->rtp;
-               res = AST_RTP_TRY_NATIVE;
+               res = AST_RTP_TRY_PARTIAL;
        }
        ast_mutex_unlock(&p->lock);
 
@@ -538,7 +521,7 @@ static int gtalk_get_codec(struct ast_channel *chan)
        return p->peercapability;
 }
 
-static int gtalk_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active)
+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)
 {
        struct gtalk_pvt *p;
 
@@ -579,7 +562,7 @@ static int gtalk_response(struct gtalk *client, char *from, ikspak *pak, const c
                                iks_insert_node(response, error);
                        }
                }
-               iks_send(client->connection->p, response);
+               ast_aji_send(client->connection, response);
                if (reason)
                        iks_delete(reason);
                if (error)
@@ -594,8 +577,7 @@ static int gtalk_is_answered(struct gtalk *client, ikspak *pak)
 {
        struct gtalk_pvt *tmp;
        char *from;
-       if (option_debug)
-               ast_log(LOG_DEBUG, "The client is %s\n", client->name);
+       ast_debug(1, "The client is %s\n", client->name);
        /* Make sure our new call doesn't exist yet */
        for (tmp = client->p; tmp; tmp = tmp->next) {
                if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid))
@@ -615,15 +597,39 @@ static int gtalk_is_answered(struct gtalk *client, ikspak *pak)
        return 1;
 }
 
+static int gtalk_is_accepted(struct gtalk *client, ikspak *pak)
+{
+       struct gtalk_pvt *tmp;
+       char *from;
+
+       ast_log(LOG_DEBUG, "The client is %s\n", client->name);
+       /* find corresponding call */
+       for (tmp = client->p; tmp; tmp = tmp->next) {
+               if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid))
+                       break;
+       }
+
+       from = iks_find_attrib(pak->x, "to");
+       if(!from)
+               from = client->connection->jid->full;
+
+       if (!tmp)
+               ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
+
+       /* answer 'iq' packet to let the remote peer know that we're alive */
+       gtalk_response(client, from, pak, NULL, NULL);
+       return 1;
+}
+
 static int gtalk_handle_dtmf(struct gtalk *client, ikspak *pak)
 {
        struct gtalk_pvt *tmp;
-       iks *dtmfnode = NULL;
+       iks *dtmfnode = NULL, *dtmfchild = NULL;
        char *dtmf;
        char *from;
        /* Make sure our new call doesn't exist yet */
        for (tmp = client->p; tmp; tmp = tmp->next) {
-               if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid))
+               if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid) || iks_find_with_attrib(pak->x, "gtalk", "sid", tmp->sid))
                        break;
        }
        from = iks_find_attrib(pak->x, "to");
@@ -657,6 +663,22 @@ static int gtalk_handle_dtmf(struct gtalk *client, ikspak *pak)
                                        ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass);
                                }
                        }
+               } else if ((dtmfnode = iks_find_with_attrib(pak->x, "gtalk", "action", "session-info"))) {
+                       if((dtmfchild = iks_find(dtmfnode, "dtmf"))) {
+                               if((dtmf = iks_find_attrib(dtmfchild, "code"))) {
+                                       if(iks_find_with_attrib(dtmfnode, "dtmf", "action", "button-up")) {
+                                               struct ast_frame f = {AST_FRAME_DTMF_END, };
+                                               f.subclass = dtmf[0];
+                                               ast_queue_frame(tmp->owner, &f);
+                                               ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass);
+                                       } else if(iks_find_with_attrib(dtmfnode, "dtmf", "action", "button-down")) {
+                                               struct ast_frame f = {AST_FRAME_DTMF_BEGIN, };
+                                               f.subclass = dtmf[0];
+                                               ast_queue_frame(tmp->owner, &f);
+                                               ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass);
+                                       }
+                               }
+                       }
                }
                gtalk_response(client, from, pak, NULL, NULL);
                return 1;
@@ -667,14 +689,12 @@ static int gtalk_handle_dtmf(struct gtalk *client, ikspak *pak)
        return 1;
 }
 
-
 static int gtalk_hangup_farend(struct gtalk *client, ikspak *pak)
 {
        struct gtalk_pvt *tmp;
        char *from;
 
-       if (option_debug)
-               ast_log(LOG_DEBUG, "The client is %s\n", client->name);
+       ast_debug(1, "The client is %s\n", client->name);
        /* Make sure our new call doesn't exist yet */
        for (tmp = client->p; tmp; tmp = tmp->next) {
                if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid))
@@ -684,10 +704,10 @@ static int gtalk_hangup_farend(struct gtalk *client, ikspak *pak)
        if(!from)
                from = client->connection->jid->full;
 
-
        if (tmp) {
                tmp->alreadygone = 1;
-               ast_queue_hangup(tmp->owner);
+               if (tmp->owner)
+                       ast_queue_hangup(tmp->owner);
        } else
                ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
        gtalk_response(client, from, pak, NULL, NULL);
@@ -802,15 +822,15 @@ static int gtalk_create_candidates(struct gtalk *client, struct gtalk_pvt *p, ch
                        iks_insert_attrib(candidate, "type", "relay");
                iks_insert_attrib(candidate, "network", "0");
                iks_insert_attrib(candidate, "generation", "0");
-               iks_send(c->p, iq);
+               ast_aji_send(c, iq);
        }
        p->laststun = 0;
 
 safeout:
        if (ours1)
-               free(ours1);
+               ast_free(ours1);
        if (ours2)
-               free(ours2);
+               ast_free(ours2);
        if (iq)
                iks_delete(iq);
        if (gtalk)
@@ -830,14 +850,13 @@ static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const
        char idroster[200];
        char *data, *exten = NULL;
 
-       if (option_debug)
-               ast_log(LOG_DEBUG, "The client is %s for alloc\n", client->name);
+       ast_debug(1, "The client is %s for alloc\n", client->name);
        if (!sid && !strchr(them, '/')) {       /* I started call! */
                if (!strcasecmp(client->name, "guest")) {
                        buddy = ASTOBJ_CONTAINER_FIND(&client->connection->buddies, them);
                        if (buddy)
                                resources = buddy->resources;
-               } else 
+               } else if (client->buddy)
                        resources = client->buddy->resources;
                while (resources) {
                        if (resources->cap->jingle) {
@@ -855,6 +874,9 @@ static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const
        if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
                return NULL;
        }
+
+       memcpy(&tmp->prefs, &client->prefs, sizeof(struct ast_codec_pref));
+
        if (sid) {
                ast_copy_string(tmp->sid, sid, sizeof(tmp->sid));
                ast_copy_string(tmp->them, them, sizeof(tmp->them));
@@ -869,18 +891,16 @@ static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const
        tmp->parent = client;
        if (!tmp->rtp) {
                ast_log(LOG_WARNING, "Out of RTP sessions?\n");
-               free(tmp);
+               ast_free(tmp);
                return NULL;
        }
 
        if(strchr(tmp->us, '/')) {
-               data = ast_strdupa((char *) tmp->us);
+               data = ast_strdupa(tmp->us);
                exten = strsep(&data, "/");
        } else
                exten = tmp->us;
        ast_copy_string(tmp->exten,  exten, sizeof(tmp->exten));
-       if(data)
-               free(data);
        ast_mutex_init(&tmp->lock);
        ast_mutex_lock(&gtalklock);
        tmp->next = client->p;
@@ -895,13 +915,14 @@ static struct ast_channel *gtalk_new(struct gtalk *client, struct gtalk_pvt *i,
        struct ast_channel *tmp;
        int fmt;
        int what;
-       char *n2;
+       const char *n2;
+       char *data = NULL, *cid = NULL;
 
        if (title)
                n2 = title;
        else
                n2 = i->us;
-       tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, "Gtalk/%s-%04lx", n2, ast_random() & 0xffff);
+       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);
        if (!tmp) {
                ast_log(LOG_WARNING, "Unable to allocate Gtalk channel structure!\n");
                return NULL;
@@ -917,16 +938,23 @@ static struct ast_channel *gtalk_new(struct gtalk *client, struct gtalk_pvt *i,
                what = i->capability;
        else
                what = global_capability;
+
+       /* Set Frame packetization */
+       if (i->rtp)
+               ast_rtp_codec_setpref(i->rtp, &i->prefs);
+
        tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | (i->jointcapability & AST_FORMAT_VIDEO_MASK);
        fmt = ast_best_codec(tmp->nativeformats);
 
        if (i->rtp) {
-               tmp->fds[0] = ast_rtp_fd(i->rtp);
-               tmp->fds[1] = ast_rtcp_fd(i->rtp);
+               ast_rtp_setstun(i->rtp, 1);
+               ast_channel_set_fd(tmp, 0, ast_rtp_fd(i->rtp));
+               ast_channel_set_fd(tmp, 1, ast_rtcp_fd(i->rtp));
        }
        if (i->vrtp) {
-               tmp->fds[2] = ast_rtp_fd(i->vrtp);
-               tmp->fds[3] = ast_rtcp_fd(i->vrtp);
+               ast_rtp_setstun(i->rtp, 1);
+               ast_channel_set_fd(tmp, 2, ast_rtp_fd(i->vrtp));
+               ast_channel_set_fd(tmp, 3, ast_rtcp_fd(i->vrtp));
        }
        if (state == AST_STATE_RING)
                tmp->rings = 1;
@@ -949,13 +977,23 @@ static struct ast_channel *gtalk_new(struct gtalk *client, struct gtalk_pvt *i,
        if (!ast_strlen_zero(client->musicclass))
                ast_string_field_set(tmp, musicclass, client->musicclass);
        i->owner = tmp;
+       ast_module_ref(ast_module_info->self);
        ast_copy_string(tmp->context, client->context, sizeof(tmp->context));
        ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
        /* Don't use ast_set_callerid() here because it will
         * generate a needless NewCallerID event */
-       tmp->cid.cid_num = ast_strdup(l->cid_num);
-       tmp->cid.cid_ani = ast_strdup(l->cid_num);
-       tmp->cid.cid_name = ast_strdup(l->cid_name);
+       if (!strcasecmp(client->name, "guest")) {
+               data = ast_strdupa(i->them);
+               if (strchr(data, '/')) {
+                       cid = strsep(&data, "/");
+               } else
+                       cid = data;
+       } else {
+               data =  ast_strdupa(client->user);
+               cid = data;
+       }
+       cid = strsep(&cid, "@");
+       tmp->cid.cid_ani = ast_strdup(cid);
        if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
                tmp->cid.cid_dnid = ast_strdup(i->exten);
        tmp->priority = 1;
@@ -990,7 +1028,7 @@ static int gtalk_action(struct gtalk *client, struct gtalk_pvt *p, const char *a
                        iks_insert_attrib(session, "initiator", p->initiator ? p->us : p->them);
                        iks_insert_attrib(session, "xmlns", "http://www.google.com/session");
                        iks_insert_node(request, session);
-                       iks_send(client->connection->p, request);
+                       ast_aji_send(client->connection, request);
                        iks_delete(session);
                        res = 0;
                }
@@ -1005,7 +1043,7 @@ static void gtalk_free_candidates(struct gtalk_candidate *candidate)
        while (candidate) {
                last = candidate;
                candidate = candidate->next;
-               free(last);
+               ast_free(last);
        }
 }
 
@@ -1033,7 +1071,7 @@ static void gtalk_free_pvt(struct gtalk *client, struct gtalk_pvt *p)
        if (p->vrtp)
                ast_rtp_destroy(p->vrtp);
        gtalk_free_candidates(p->theircandidates);
-       free(p);
+       ast_free(p);
 }
 
 
@@ -1112,6 +1150,7 @@ static int gtalk_update_stun(struct gtalk *client, struct gtalk_pvt *p)
        struct hostent *hp;
        struct ast_hostent ahp;
        struct sockaddr_in sin;
+       struct sockaddr_in aux;
 
        if (time(NULL) == p->laststun)
                return 0;
@@ -1120,14 +1159,32 @@ static int gtalk_update_stun(struct gtalk *client, struct gtalk_pvt *p)
        p->laststun = time(NULL);
        while (tmp) {
                char username[256];
+
+               /* Find the IP address of the host */
                hp = ast_gethostbyname(tmp->ip, &ahp);
                sin.sin_family = AF_INET;
                memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
                sin.sin_port = htons(tmp->port);
                snprintf(username, sizeof(username), "%s%s", tmp->username,
-                                p->ourcandidates->username);
+                        p->ourcandidates->username);
+               
+               /* Find out the result of the STUN */
+               ast_rtp_get_peer(p->rtp, &aux);
+
+               /* If the STUN result is different from the IP of the hostname,
+                       lock on the stun IP of the hostname advertised by the
+                       remote client */
+               if (aux.sin_addr.s_addr && 
+                   aux.sin_addr.s_addr != sin.sin_addr.s_addr)
+                       ast_rtp_stun_request(p->rtp, &aux, username);
+               else 
+                       ast_rtp_stun_request(p->rtp, &sin, username);
+               
+               if (aux.sin_addr.s_addr) {
+                       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);
+                       ast_debug(4, "Sending STUN request to %s\n", tmp->ip);
+               }
 
-               ast_rtp_stun_request(p->rtp, &sin, username);
                tmp = tmp->next;
        }
        return 1;
@@ -1145,9 +1202,6 @@ static int gtalk_add_candidate(struct gtalk *client, ikspak *pak)
        if(!from)
                from = c->jid->full;
 
-       newcandidate = ast_calloc(1, sizeof(*newcandidate));
-       if (!newcandidate)
-               return 0;
        for (tmp = client->p; tmp; tmp = tmp->next) {
                if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) {
                        p = tmp;
@@ -1212,7 +1266,7 @@ static int gtalk_add_candidate(struct gtalk *client, ikspak *pak)
        iks_insert_attrib(receipt, "from", from);
        iks_insert_attrib(receipt, "to", iks_find_attrib(pak->x, "from"));
        iks_insert_attrib(receipt, "id", iks_find_attrib(pak->x, "id"));
-       iks_send(c->p, receipt);
+       ast_aji_send(c, receipt);
        iks_delete(receipt);
 
        return 1;
@@ -1230,8 +1284,7 @@ static struct ast_frame *gtalk_rtp_read(struct ast_channel *ast, struct gtalk_pv
                /* We already hold the channel lock */
                if (f->frametype == AST_FRAME_VOICE) {
                        if (f->subclass != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
+                               ast_debug(1, "Oooh, format changed to %d\n", f->subclass);
                                p->owner->nativeformats =
                                        (p->owner->nativeformats & AST_FORMAT_VIDEO_MASK) | f->subclass;
                                ast_set_read_format(p->owner, p->owner->readformat);
@@ -1240,8 +1293,7 @@ static struct ast_frame *gtalk_rtp_read(struct ast_channel *ast, struct gtalk_pv
 /*                     if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
                                f = ast_dsp_process(p->owner, p->vad, f);
                                if (option_debug && f && (f->frametype == AST_FRAME_DTMF))
-                                       if (option_debug)
-                                               ast_log(LOG_DEBUG, "* Detected inband DTMF '%c'\n", f->subclass);
+                                       ast_debug(1, "* Detected inband DTMF '%c'\n", f->subclass);
                        } */
                }
        }
@@ -1337,7 +1389,17 @@ static int gtalk_indicate(struct ast_channel *ast, int condition, const void *da
        return res;
 }
 
-static int gtalk_digit(struct ast_channel *ast, char digit)
+static int gtalk_digit_begin(struct ast_channel *chan, char digit)
+{
+       return gtalk_digit(chan, digit, 0);
+}
+
+static int gtalk_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
+{
+       return gtalk_digit(chan, digit, duration);
+}
+
+static int gtalk_digit(struct ast_channel *ast, char digit, unsigned int duration)
 {
        struct gtalk_pvt *p = ast->tech_pvt;
        struct gtalk *client = p->parent;
@@ -1363,7 +1425,7 @@ static int gtalk_digit(struct ast_channel *ast, char digit)
        iks_insert_attrib(iq, "id", client->connection->mid);
        ast_aji_increment_mid(client->connection->mid);
        iks_insert_attrib(gtalk, "xmlns", "http://jabber.org/protocol/gtalk");
-       iks_insert_attrib(gtalk, "action", "content-info");
+       iks_insert_attrib(gtalk, "action", "session-info");
        iks_insert_attrib(gtalk, "initiator", p->initiator ? p->us: p->them);
        iks_insert_attrib(gtalk, "sid", p->sid);
        iks_insert_attrib(dtmf, "xmlns", "http://jabber.org/protocol/gtalk/info/dtmf");
@@ -1372,14 +1434,12 @@ static int gtalk_digit(struct ast_channel *ast, char digit)
        iks_insert_node(gtalk, dtmf);
 
        ast_mutex_lock(&p->lock);
-       if(ast->dtmff.frametype == AST_FRAME_DTMF) {
-               ast_verbose("Sending 250ms dtmf!\n");
-       } else if (ast->dtmff.frametype == AST_FRAME_DTMF_BEGIN) {
+       if (ast->dtmff.frametype == AST_FRAME_DTMF_BEGIN || duration == 0) {
                iks_insert_attrib(dtmf, "action", "button-down");
-       } else if (ast->dtmff.frametype == AST_FRAME_DTMF_END) {
+       } else if (ast->dtmff.frametype == AST_FRAME_DTMF_END || duration != 0) {
                iks_insert_attrib(dtmf, "action", "button-up");
        }
-       iks_send(client->connection->p, iq);
+       ast_aji_send(client->connection, iq);
        iks_delete(iq);
        iks_delete(gtalk);
        iks_delete(dtmf);
@@ -1453,6 +1513,7 @@ static int gtalk_hangup(struct ast_channel *ast)
        ast_mutex_unlock(&p->lock);
 
        gtalk_free_pvt(client, p);
+       ast_module_unref(ast_module_info->self);
 
        return 0;
 }
@@ -1466,15 +1527,13 @@ static struct ast_channel *gtalk_request(const char *type, int format, void *dat
        struct ast_channel *chan = NULL;
 
        if (data) {
-               s = ast_strdupa((char *) data);
+               s = ast_strdupa(data);
                if (s) {
                        sender = strsep(&s, "/");
                        if (sender && (sender[0] != '\0'))
                                to = strsep(&s, "/");
                        if (!to) {
                                ast_log(LOG_ERROR, "Bad arguments in Gtalk Dialstring: %s\n", (char*) data);
-                               if (s)
-                                       free(s);
                                return NULL;
                        }
                }
@@ -1482,34 +1541,97 @@ static struct ast_channel *gtalk_request(const char *type, int format, void *dat
        client = find_gtalk(to, sender);
        if (!client) {
                ast_log(LOG_WARNING, "Could not find recipient.\n");
-               if (s)
-                       free(s);
                return NULL;
        }
+       ASTOBJ_WRLOCK(client);
        p = gtalk_alloc(client, strchr(sender, '@') ? sender : client->connection->jid->full, strchr(to, '@') ? to : client->user, NULL);
        if (p)
                chan = gtalk_new(client, p, AST_STATE_DOWN, to);
 
+       ASTOBJ_UNLOCK(client);
        return chan;
 }
 
 /*! \brief CLI command "gtalk show channels" */
-static int gtalk_show_channels(int fd, int argc, char **argv)
+static char *gtalk_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-       if (argc != 3)
-               return RESULT_SHOWUSAGE;
+#define FORMAT  "%-30.30s  %-30.30s  %-15.15s  %-5.5s %-5.5s \n"
+       struct gtalk_pvt *p;
+       struct ast_channel *chan;
+       int numchans = 0;
+       char them[AJI_MAX_JIDLEN];
+       char *jid = NULL;
+       char *resource = NULL;
+
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "gtalk show channels";
+               e->usage =
+                       "Usage: gtalk show channels\n"
+                       "       Shows current state of the Gtalk channels.\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
+       }
+
+       if (a->argc != 3)
+               return CLI_SHOWUSAGE;
+
        ast_mutex_lock(&gtalklock);
-//     if (!gtalk_list->p)
-               ast_cli(fd, "No gtalk channels in use\n");
+       ast_cli(a->fd, FORMAT, "Channel", "Jabber ID", "Resource", "Read", "Write");
+       ASTOBJ_CONTAINER_TRAVERSE(&gtalk_list, 1, {
+               ASTOBJ_WRLOCK(iterator);
+               p = iterator->p;
+               while(p) {
+                       chan = p->owner;
+                       ast_copy_string(them, p->them, sizeof(them));
+                       jid = them;
+                       resource = strchr(them, '/');
+                       if (!resource)
+                               resource = "None";
+                       else {
+                               *resource = '\0';
+                               resource ++;
+                       }
+                       if (chan)
+                               ast_cli(a->fd, FORMAT, 
+                                       chan->name,
+                                       jid,
+                                       resource,
+                                       ast_getformatname(chan->readformat),
+                                       ast_getformatname(chan->writeformat)                                    
+                                       );
+                       else 
+                               ast_log(LOG_WARNING, "No available channel\n");
+                       numchans ++;
+                       p = p->next;
+               }
+               ASTOBJ_UNLOCK(iterator);
+       });
+
        ast_mutex_unlock(&gtalklock);
-       return RESULT_SUCCESS;
+
+       ast_cli(a->fd, "%d active gtalk channel%s\n", numchans, (numchans != 1) ? "s" : "");
+       return CLI_SUCCESS;
+#undef FORMAT
 }
 
-/*! \brief CLI command "gtalk show channels" */
-static int gtalk_do_reload(int fd, int argc, char **argv)
+/*! \brief CLI command "gtalk reload" */
+static char *gtalk_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "gtalk reload";
+               e->usage =
+                       "Usage: gtalk reload\n"
+                       "       Reload gtalk channel driver.\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
+       }       
+       
        ast_verbose("IT DOES WORK!\n");
-       return RESULT_SUCCESS;
+       return CLI_SUCCESS;
 }
 
 static int gtalk_parser(void *data, ikspak *pak)
@@ -1519,15 +1641,15 @@ static int gtalk_parser(void *data, ikspak *pak)
        if (iks_find_with_attrib(pak->x, "session", "type", "initiate")) {
                /* New call */
                gtalk_newcall(client, pak);
-       } else if (iks_find_with_attrib(pak->x, "session", "type", "candidates") || iks_find_with_attrib(pak->x, "session", "type", "transport-info") ) {
-               if (option_debug > 2)
-                       ast_log(LOG_DEBUG, "About to add candidate!\n");
+       } else if (iks_find_with_attrib(pak->x, "session", "type", "candidates") || iks_find_with_attrib(pak->x, "session", "type", "transport-info")) {
+               ast_debug(3, "About to add candidate!\n");
                gtalk_add_candidate(client, pak);
-               if (option_debug > 2)
-                       ast_log(LOG_DEBUG, "Candidate Added!\n");
+               ast_debug(3, "Candidate Added!\n");
        } else if (iks_find_with_attrib(pak->x, "session", "type", "accept")) {
                gtalk_is_answered(client, pak);
-       } else if (iks_find_with_attrib(pak->x, "session", "type", "content-info")) {
+       } else if (iks_find_with_attrib(pak->x, "session", "type", "transport-accept")) {
+               gtalk_is_accepted(client, pak);
+       } else if (iks_find_with_attrib(pak->x, "session", "type", "content-info") || iks_find_with_attrib(pak->x, "gtalk", "action", "session-info")) {
                gtalk_handle_dtmf(client, pak);
        } else if (iks_find_with_attrib(pak->x, "session", "type", "terminate")) {
                gtalk_hangup_farend(client, pak);
@@ -1543,8 +1665,7 @@ static struct gtalk_candidate *gtalk_create_candidate(char *args)
 {
        char *name, *type, *preference, *protocol;
        struct gtalk_candidate *res;
-       res = malloc(sizeof(struct gtalk_candidate));
-       memset(res, 0, sizeof(struct gtalk_candidate));
+       res = ast_calloc(1, sizeof(*res));
        if (args)
                name = args;
        if ((args = strchr(args, ','))) {
@@ -1624,10 +1745,11 @@ static int gtalk_create_member(char *label, struct ast_variable *var, int allowg
                else if (!strcasecmp(var->name, "connection")) {
                        if ((client = ast_aji_get_client(var->value))) {
                                member->connection = client;
-                               iks_filter_add_rule(client->f, gtalk_parser, member, IKS_RULE_TYPE,
-                                                                       IKS_PAK_IQ, IKS_RULE_FROM_PARTIAL, member->user,
-                                                                       IKS_RULE_NS, "http://www.google.com/session",
-                                                                       IKS_RULE_DONE);
+                               iks_filter_add_rule(client->f, gtalk_parser, member, 
+                                                   IKS_RULE_TYPE, IKS_PAK_IQ, 
+                                                   IKS_RULE_FROM_PARTIAL, member->user,
+                                                   IKS_RULE_NS, "http://www.google.com/session",
+                                                   IKS_RULE_DONE);
 
                        } else {
                                ast_log(LOG_ERROR, "connection referenced not found!\n");
@@ -1648,7 +1770,7 @@ static int gtalk_load_config(void)
 {
        char *cat = NULL;
        struct ast_config *cfg = NULL;
-       char context[100];
+       char context[AST_MAX_CONTEXT];
        int allowguest = 1;
        struct ast_variable *var;
        struct gtalk *member;
@@ -1657,8 +1779,9 @@ static int gtalk_load_config(void)
        struct gtalk_candidate *global_candidates = NULL;
        struct hostent *hp;
        struct ast_hostent ahp;
+       struct ast_flags config_flags = { 0 };
 
-       cfg = ast_config_load(GOOGLE_CONFIG);
+       cfg = ast_config_load(GOOGLE_CONFIG, config_flags);
        if (!cfg)
                return 0;
 
@@ -1701,8 +1824,7 @@ static int gtalk_load_config(void)
        while (cat) {
                if (strcasecmp(cat, "general")) {
                        var = ast_variable_browse(cfg, cat);
-                       member = (struct gtalk *) malloc(sizeof(struct gtalk));
-                       memset(member, 0, sizeof(struct gtalk));
+                       member = ast_calloc(1, sizeof(*member));
                        ASTOBJ_INIT(member);
                        ASTOBJ_WRLOCK(member);
                        if (!strcasecmp(cat, "guest")) {
@@ -1740,8 +1862,8 @@ static int gtalk_load_config(void)
                                                ASTOBJ_WRLOCK(iterator);
                                                ASTOBJ_WRLOCK(member);
                                                member->connection = iterator;
-                                               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);
+                                               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);
+                                               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);
                                                ASTOBJ_UNLOCK(member);
                                                ASTOBJ_CONTAINER_LINK(&gtalk_list, member);
                                                ASTOBJ_UNLOCK(iterator);
@@ -1790,7 +1912,7 @@ static int load_module(void)
 
        /* Make sure we can register our channel type */
        if (ast_channel_register(&gtalk_tech)) {
-               ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
+               ast_log(LOG_ERROR, "Unable to register channel class %s\n", gtalk_tech.type);
                return -1;
        }
        return 0;