Sat Mar 26 11:15:18 2011

Asterisk developer's documentation


chan_gtalk.c File Reference

Gtalk Channel Driver, until google/libjingle works with jingle spec. More...

#include "asterisk.h"
#include <sys/socket.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/signal.h>
#include <iksemel.h>
#include <pthread.h>
#include <ctype.h>
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/sched.h"
#include "asterisk/io.h"
#include "asterisk/rtp_engine.h"
#include "asterisk/stun.h"
#include "asterisk/acl.h"
#include "asterisk/callerid.h"
#include "asterisk/file.h"
#include "asterisk/cli.h"
#include "asterisk/app.h"
#include "asterisk/musiconhold.h"
#include "asterisk/manager.h"
#include "asterisk/stringfields.h"
#include "asterisk/utils.h"
#include "asterisk/causes.h"
#include "asterisk/astobj.h"
#include "asterisk/abstract_jb.h"
#include "asterisk/jabber.h"
#include "asterisk/jingle.h"

Include dependency graph for chan_gtalk.c:

Go to the source code of this file.

Data Structures

struct  gtalk
struct  gtalk_candidate
struct  gtalk_container
struct  gtalk_pvt

Defines

#define FORMAT   "%-30.30s %-30.30s %-15.15s %-5.5s %-5.5s \n"
#define GOOGLE_CONFIG   "gtalk.conf"

Enumerations

enum  gtalk_connect_type { AJI_CONNECT_STUN = 1, AJI_CONNECT_LOCAL = 2, AJI_CONNECT_RELAY = 3 }
enum  gtalk_protocol { AJI_PROTOCOL_UDP = 1, AJI_PROTOCOL_SSLTCP = 2 }

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int add_codec_to_answer (const struct gtalk_pvt *p, int codec, iks *dcodecs)
static struct gtalk * find_gtalk (char *name, char *connection)
static int gtalk_action (struct gtalk *client, struct gtalk_pvt *p, const char *action)
static int gtalk_add_candidate (struct gtalk *client, ikspak *pak)
static struct gtalk_pvt * gtalk_alloc (struct gtalk *client, const char *us, const char *them, const char *sid)
static int gtalk_answer (struct ast_channel *ast)
static int gtalk_call (struct ast_channel *ast, char *dest, int timeout)
 Initiate new call, part of PBX interface dest is the dial string.
static int gtalk_create_candidates (struct gtalk *client, struct gtalk_pvt *p, char *sid, char *from, char *to)
static int gtalk_create_member (char *label, struct ast_variable *var, int allowguest, struct ast_codec_pref prefs, char *context, struct gtalk *member)
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_fixup (struct ast_channel *oldchan, struct ast_channel *newchan)
static void gtalk_free_candidates (struct gtalk_candidate *candidate)
static void gtalk_free_pvt (struct gtalk *client, struct gtalk_pvt *p)
static format_t gtalk_get_codec (struct ast_channel *chan)
static int gtalk_get_local_ip (struct ast_sockaddr *ourip)
static enum ast_rtp_glue_result gtalk_get_rtp_peer (struct ast_channel *chan, struct ast_rtp_instance **instance)
static int gtalk_handle_dtmf (struct gtalk *client, ikspak *pak)
static int gtalk_hangup (struct ast_channel *ast)
 Hangup a call through the gtalk proxy channel.
static int gtalk_hangup_farend (struct gtalk *client, ikspak *pak)
static int gtalk_indicate (struct ast_channel *ast, int condition, const void *data, size_t datalen)
static int gtalk_invite (struct gtalk_pvt *p, char *to, char *from, char *sid, int initiator)
static int gtalk_is_accepted (struct gtalk *client, ikspak *pak)
static int gtalk_is_answered (struct gtalk *client, ikspak *pak)
static int gtalk_load_config (void)
static void gtalk_member_destroy (struct gtalk *obj)
static struct ast_channel * gtalk_new (struct gtalk *client, struct gtalk_pvt *i, int state, const char *title, const char *linkedid)
 Start new gtalk channel.
static int gtalk_newcall (struct gtalk *client, ikspak *pak)
static int gtalk_parser (void *data, ikspak *pak)
 CLI command "gtalk reload".
static struct ast_frame * gtalk_read (struct ast_channel *ast)
static struct ast_channel * gtalk_request (const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
 Part of PBX interface.
static int gtalk_response (struct gtalk *client, char *from, ikspak *pak, const char *reasonstr, const char *reasonstr2)
static int gtalk_ringing_ack (void *data, ikspak *pak)
static struct ast_frame * gtalk_rtp_read (struct ast_channel *ast, struct gtalk_pvt *p)
static int gtalk_sendhtml (struct ast_channel *ast, int subclass, const char *data, int datalen)
static int gtalk_sendtext (struct ast_channel *ast, const char *text)
static int gtalk_set_rtp_peer (struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, format_t codecs, int nat_active)
static char * gtalk_show_channels (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 CLI command "gtalk show channels".
static int gtalk_update_externip (void)
static int gtalk_update_stun (struct gtalk *client, struct gtalk_pvt *p)
static int gtalk_write (struct ast_channel *ast, struct ast_frame *frame)
 Send frame to media channel (rtp).
static int load_module (void)
 Load module into PBX, register channel.
static int unload_module (void)
 Reload module.

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Gtalk Channel Driver" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CHANNEL_DRIVER, }
static struct in_addr __ourip
static struct ast_module_info * ast_module_info = &__mod_info
static struct sockaddr_in bindaddr = { 0, }
static struct ast_jb_conf default_jbconf
static const char desc [] = "Gtalk Channel"
static char externip [16]
static format_t global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263
static struct ast_jb_conf global_jbconf
static struct ast_cli_entry gtalk_cli []
static struct gtalk_container gtalk_list
static struct ast_rtp_glue gtalk_rtp_glue
static struct ast_channel_tech gtalk_tech
 PBX interface structure for channel registration.
static ast_mutex_t gtalklock = { { { NULL }, { 0 }, 0, { NULL }, { 0 }, {{{ 0 }}}, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP } , 1, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP }
static struct io_context * io
static struct sched_context * sched
static struct sockaddr_in stunaddr


Detailed Description

Gtalk Channel Driver, until google/libjingle works with jingle spec.

Author:
Matt O'Gorman <mogorman@digium.com>

Philippe Sultan <philippe.sultan@gmail.com>

********** General TODO:s
Todo:
Support config reloading.

Fix native bridging.

Definition in file chan_gtalk.c.


Define Documentation

#define FORMAT   "%-30.30s %-30.30s %-15.15s %-5.5s %-5.5s \n"

#define GOOGLE_CONFIG   "gtalk.conf"

Definition at line 79 of file chan_gtalk.c.

Referenced by gtalk_load_config(), and load_module().


Enumeration Type Documentation

Enumerator:
AJI_CONNECT_STUN 
AJI_CONNECT_LOCAL 
AJI_CONNECT_RELAY 

Definition at line 97 of file chan_gtalk.c.

00097                         {
00098    AJI_CONNECT_STUN = 1,
00099    AJI_CONNECT_LOCAL = 2,
00100    AJI_CONNECT_RELAY = 3,
00101 };

Enumerator:
AJI_PROTOCOL_UDP 
AJI_PROTOCOL_SSLTCP 

Definition at line 92 of file chan_gtalk.c.

00092                     {
00093    AJI_PROTOCOL_UDP = 1,
00094    AJI_PROTOCOL_SSLTCP = 2,
00095 };


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 2241 of file chan_gtalk.c.

static void __unreg_module ( void   )  [static]

Definition at line 2241 of file chan_gtalk.c.

static int add_codec_to_answer ( const struct gtalk_pvt *  p,
int  codec,
iks *  dcodecs 
) [static]

Definition at line 275 of file chan_gtalk.c.

References ast_getformatname(), ast_log(), format, and LOG_WARNING.

Referenced by gtalk_invite().

00276 {
00277    int res = 0;
00278    char *format = ast_getformatname(codec);
00279 
00280    if (!strcasecmp("ulaw", format)) {
00281       iks *payload_eg711u, *payload_pcmu;
00282       payload_pcmu = iks_new("payload-type");
00283       payload_eg711u = iks_new("payload-type");
00284 
00285       if(!payload_eg711u || !payload_pcmu) {
00286          iks_delete(payload_pcmu);
00287          iks_delete(payload_eg711u);
00288          ast_log(LOG_WARNING,"Failed to allocate iks node");
00289          return -1;
00290       }
00291       iks_insert_attrib(payload_pcmu, "id", "0");
00292       iks_insert_attrib(payload_pcmu, "name", "PCMU");
00293       iks_insert_attrib(payload_pcmu, "clockrate","8000");
00294       iks_insert_attrib(payload_pcmu, "bitrate","64000");
00295       iks_insert_attrib(payload_eg711u, "id", "100");
00296       iks_insert_attrib(payload_eg711u, "name", "EG711U");
00297       iks_insert_attrib(payload_eg711u, "clockrate","8000");
00298       iks_insert_attrib(payload_eg711u, "bitrate","64000");
00299       iks_insert_node(dcodecs, payload_pcmu);
00300       iks_insert_node(dcodecs, payload_eg711u);
00301       res ++;
00302    }
00303    if (!strcasecmp("alaw", format)) {
00304       iks *payload_eg711a, *payload_pcma;
00305       payload_pcma = iks_new("payload-type");
00306       payload_eg711a = iks_new("payload-type");
00307       if(!payload_eg711a || !payload_pcma) {
00308          iks_delete(payload_eg711a);
00309          iks_delete(payload_pcma);
00310          ast_log(LOG_WARNING,"Failed to allocate iks node");
00311          return -1;
00312       }
00313       iks_insert_attrib(payload_pcma, "id", "8");
00314       iks_insert_attrib(payload_pcma, "name", "PCMA");
00315       iks_insert_attrib(payload_pcma, "clockrate","8000");
00316       iks_insert_attrib(payload_pcma, "bitrate","64000");
00317       payload_eg711a = iks_new("payload-type");
00318       iks_insert_attrib(payload_eg711a, "id", "101");
00319       iks_insert_attrib(payload_eg711a, "name", "EG711A");
00320       iks_insert_attrib(payload_eg711a, "clockrate","8000");
00321       iks_insert_attrib(payload_eg711a, "bitrate","64000");
00322       iks_insert_node(dcodecs, payload_pcma);
00323       iks_insert_node(dcodecs, payload_eg711a);
00324       res ++;
00325    }
00326    if (!strcasecmp("ilbc", format)) {
00327       iks *payload_ilbc = iks_new("payload-type");
00328       if(!payload_ilbc) {
00329          ast_log(LOG_WARNING,"Failed to allocate iks node");
00330          return -1;
00331       }
00332       iks_insert_attrib(payload_ilbc, "id", "97");
00333       iks_insert_attrib(payload_ilbc, "name", "iLBC");
00334       iks_insert_attrib(payload_ilbc, "clockrate","8000");
00335       iks_insert_attrib(payload_ilbc, "bitrate","13300");
00336       iks_insert_node(dcodecs, payload_ilbc);
00337       res ++;
00338    }
00339    if (!strcasecmp("g723", format)) {
00340       iks *payload_g723 = iks_new("payload-type");
00341       if(!payload_g723) {
00342          ast_log(LOG_WARNING,"Failed to allocate iks node");
00343          return -1;
00344       }
00345       iks_insert_attrib(payload_g723, "id", "4");
00346       iks_insert_attrib(payload_g723, "name", "G723");
00347       iks_insert_attrib(payload_g723, "clockrate","8000");
00348       iks_insert_attrib(payload_g723, "bitrate","6300");
00349       iks_insert_node(dcodecs, payload_g723);
00350       res ++;
00351    }
00352    if (!strcasecmp("speex", format)) {
00353       iks *payload_speex = iks_new("payload-type");
00354       if(!payload_speex) {
00355          ast_log(LOG_WARNING,"Failed to allocate iks node");
00356          return -1;
00357       }
00358       iks_insert_attrib(payload_speex, "id", "110");
00359       iks_insert_attrib(payload_speex, "name", "speex");
00360       iks_insert_attrib(payload_speex, "clockrate","8000");
00361       iks_insert_attrib(payload_speex, "bitrate","11000");
00362       iks_insert_node(dcodecs, payload_speex);
00363       res++;
00364    }
00365    if (!strcasecmp("gsm", format)) {
00366       iks *payload_gsm = iks_new("payload-type");
00367       if(!payload_gsm) {
00368          ast_log(LOG_WARNING,"Failed to allocate iks node");
00369          return -1;
00370       }
00371       iks_insert_attrib(payload_gsm, "id", "103");
00372       iks_insert_attrib(payload_gsm, "name", "gsm");
00373       iks_insert_node(dcodecs, payload_gsm);
00374       res++;
00375    }
00376 
00377    return res;
00378 }

static struct gtalk* find_gtalk ( char *  name,
char *  connection 
) [static, read]

Definition at line 243 of file chan_gtalk.c.

References ast_strdupa, ast_verbose, ASTOBJ_CONTAINER_FIND, ASTOBJ_CONTAINER_FIND_FULL, ASTOBJ_CONTAINER_TRAVERSE, ASTOBJ_RDLOCK, ASTOBJ_UNLOCK, gtalk_list, and strsep().

Referenced by gtalk_request().

00244 {
00245    struct gtalk *gtalk = NULL;
00246    char *domain = NULL , *s = NULL;
00247 
00248    if (strchr(connection, '@')) {
00249       s = ast_strdupa(connection);
00250       domain = strsep(&s, "@");
00251       ast_verbose("OOOOH domain = %s\n", domain);
00252    }
00253    gtalk = ASTOBJ_CONTAINER_FIND(&gtalk_list, name);
00254    if (!gtalk && strchr(name, '@'))
00255       gtalk = ASTOBJ_CONTAINER_FIND_FULL(&gtalk_list, name, user,,, strcasecmp);
00256 
00257    if (!gtalk) {
00258       /* guest call */
00259       ASTOBJ_CONTAINER_TRAVERSE(&gtalk_list, 1, {
00260          ASTOBJ_RDLOCK(iterator);
00261          if (!strcasecmp(iterator->name, "guest")) {
00262             gtalk = iterator;
00263          }
00264          ASTOBJ_UNLOCK(iterator);
00265 
00266          if (gtalk)
00267             break;
00268       });
00269 
00270    }
00271    return gtalk;
00272 }

static int gtalk_action ( struct gtalk *  client,
struct gtalk_pvt *  p,
const char *  action 
) [static]

Definition at line 1127 of file chan_gtalk.c.

References ast_aji_increment_mid(), ast_aji_send(), ast_strdupa, gtalk::connection, GOOGLE_NS, gtalk_pvt::initiator, aji_client::mid, gtalk_pvt::sid, gtalk_pvt::them, and gtalk_pvt::us.

Referenced by gtalk_hangup(), and gtalk_newcall().

01128 {
01129    iks *request, *session = NULL;
01130    int res = -1;
01131    char *lowerthem = NULL;
01132 
01133    request = iks_new("iq");
01134    if (request) {
01135       iks_insert_attrib(request, "type", "set");
01136       iks_insert_attrib(request, "from", p->us);
01137       iks_insert_attrib(request, "to", p->them);
01138       iks_insert_attrib(request, "id", client->connection->mid);
01139       ast_aji_increment_mid(client->connection->mid);
01140       session = iks_new("session");
01141       if (session) {
01142          iks_insert_attrib(session, "type", action);
01143          iks_insert_attrib(session, "id", p->sid);
01144          /* put the initiator attribute to lower case if we receive the call
01145           * otherwise GoogleTalk won't establish the session */
01146          if (!p->initiator) {
01147                  char c;
01148             char *t = lowerthem = ast_strdupa(p->them);
01149             while (((c = *t) != '/') && (*t++ = tolower(c)));
01150          }
01151          iks_insert_attrib(session, "initiator", p->initiator ? p->us : lowerthem);
01152          iks_insert_attrib(session, "xmlns", GOOGLE_NS);
01153          iks_insert_node(request, session);
01154          ast_aji_send(client->connection, request);
01155          res = 0;
01156       }
01157    }
01158 
01159    iks_delete(session);
01160    iks_delete(request);
01161 
01162    return res;
01163 }

static int gtalk_add_candidate ( struct gtalk *  client,
ikspak *  pak 
) [static]

Definition at line 1429 of file chan_gtalk.c.

References AJI_CONNECT_LOCAL, AJI_CONNECT_RELAY, AJI_CONNECT_STUN, AJI_PROTOCOL_SSLTCP, AJI_PROTOCOL_UDP, ast_aji_send(), ast_calloc, ast_copy_string(), gtalk::connection, gtalk_candidate::generation, gtalk_update_stun(), gtalk_candidate::ip, aji_client::jid, gtalk_pvt::laststun, gtalk_candidate::name, gtalk_candidate::network, gtalk_candidate::next, gtalk_pvt::next, gtalk::p, gtalk_pvt::parent, gtalk_candidate::password, gtalk_candidate::port, gtalk_candidate::preference, gtalk_candidate::protocol, gtalk_candidate::receipt, S_OR, gtalk_pvt::theircandidates, gtalk_candidate::type, and gtalk_candidate::username.

Referenced by gtalk_parser().

01430 {
01431    struct gtalk_pvt *p = NULL, *tmp = NULL;
01432    struct aji_client *c = client->connection;
01433    struct gtalk_candidate *newcandidate = NULL;
01434    iks *traversenodes = NULL, *receipt = NULL;
01435    char *from;
01436 
01437    from = iks_find_attrib(pak->x,"to");
01438    if (!from) {
01439       from = c->jid->full;
01440    }
01441 
01442    for (tmp = client->p; tmp; tmp = tmp->next) {
01443       if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid) ||
01444          (iks_find_attrib(pak->query, "id") && !strcmp(iks_find_attrib(pak->query, "id"), tmp->sid))) {
01445          p = tmp;
01446          break;
01447       }
01448    }
01449 
01450    if (!p) {
01451       return -1;
01452    }
01453    traversenodes = pak->query;
01454    while(traversenodes) {
01455       if(!strcasecmp(S_OR(iks_name(traversenodes), ""), "session")) {
01456          traversenodes = iks_first_tag(traversenodes);
01457          continue;
01458       }
01459       if(!strcasecmp(S_OR(iks_name(traversenodes), ""), "ses:session")) {
01460          traversenodes = iks_child(traversenodes);
01461          continue;
01462       }
01463       if(!strcasecmp(S_OR(iks_name(traversenodes), ""), "candidate") || !strcasecmp(S_OR(iks_name(traversenodes), ""), "ses:candidate")) {
01464          newcandidate = ast_calloc(1, sizeof(*newcandidate));
01465          if (!newcandidate)
01466             return 0;
01467          ast_copy_string(newcandidate->name,
01468             S_OR(iks_find_attrib(traversenodes, "name"), ""),
01469             sizeof(newcandidate->name));
01470          ast_copy_string(newcandidate->ip,
01471             S_OR(iks_find_attrib(traversenodes, "address"), ""),
01472             sizeof(newcandidate->ip));
01473          newcandidate->port = atoi(iks_find_attrib(traversenodes, "port"));
01474          ast_copy_string(newcandidate->username,
01475             S_OR(iks_find_attrib(traversenodes, "username"), ""),
01476             sizeof(newcandidate->username));
01477          ast_copy_string(newcandidate->password,
01478             S_OR(iks_find_attrib(traversenodes, "password"), ""),
01479             sizeof(newcandidate->password));
01480          newcandidate->preference = atof(iks_find_attrib(traversenodes, "preference"));
01481          if (!strcasecmp(S_OR(iks_find_attrib(traversenodes, "protocol"), ""), "udp"))
01482             newcandidate->protocol = AJI_PROTOCOL_UDP;
01483          if (!strcasecmp(S_OR(iks_find_attrib(traversenodes, "protocol"), ""), "ssltcp"))
01484             newcandidate->protocol = AJI_PROTOCOL_SSLTCP;
01485 
01486          if (!strcasecmp(S_OR(iks_find_attrib(traversenodes, "type"), ""), "stun"))
01487             newcandidate->type = AJI_CONNECT_STUN;
01488          if (!strcasecmp(S_OR(iks_find_attrib(traversenodes, "type"), ""), "local"))
01489             newcandidate->type = AJI_CONNECT_LOCAL;
01490          if (!strcasecmp(S_OR(iks_find_attrib(traversenodes, "type"), ""), "relay"))
01491             newcandidate->type = AJI_CONNECT_RELAY;
01492          ast_copy_string(newcandidate->network,
01493             S_OR(iks_find_attrib(traversenodes, "network"), ""),
01494             sizeof(newcandidate->network));
01495          newcandidate->generation = atoi(S_OR(iks_find_attrib(traversenodes, "generation"), "0"));
01496          newcandidate->next = NULL;
01497 
01498          newcandidate->next = p->theircandidates;
01499          p->theircandidates = newcandidate;
01500          p->laststun = 0;
01501          gtalk_update_stun(p->parent, p);
01502          newcandidate = NULL;
01503       }
01504       traversenodes = iks_next_tag(traversenodes);
01505    }
01506 
01507    receipt = iks_new("iq");
01508    iks_insert_attrib(receipt, "type", "result");
01509    iks_insert_attrib(receipt, "from", from);
01510    iks_insert_attrib(receipt, "to", S_OR(iks_find_attrib(pak->x, "from"), ""));
01511    iks_insert_attrib(receipt, "id", S_OR(iks_find_attrib(pak->x, "id"), ""));
01512    ast_aji_send(c, receipt);
01513 
01514    iks_delete(receipt);
01515 
01516    return 1;
01517 }

static struct gtalk_pvt * gtalk_alloc ( struct gtalk *  client,
const char *  us,
const char *  them,
const char *  sid 
) [static, read]

Definition at line 939 of file chan_gtalk.c.

References ast_calloc, ast_copy_string(), ast_debug, ast_free, ast_log(), ast_mutex_init, ast_mutex_lock, ast_mutex_unlock, ast_random(), ast_rtp_codecs_payloads_clear(), AST_RTP_DTMF_MODE_RFC2833, ast_rtp_instance_dtmf_mode_set(), ast_rtp_instance_get_codecs(), ast_rtp_instance_new(), ast_rtp_instance_set_prop(), AST_RTP_PROPERTY_DTMF, AST_RTP_PROPERTY_RTCP, AST_RTP_PROPERTY_STUN, ast_sockaddr_from_sin, ast_strdupa, ASTOBJ_CONTAINER_FIND, aji_client::buddies, gtalk::buddy, aji_resource::cap, gtalk_pvt::capability, gtalk::capability, gtalk_pvt::cid_name, gtalk::connection, gtalk_pvt::exten, exten, gtalklock, gtalk_pvt::initiator, aji_version::jingle, gtalk_pvt::lock, LOG_ERROR, LOG_WARNING, gtalk::name, gtalk_pvt::next, aji_resource::next, gtalk::p, gtalk_pvt::parent, gtalk::prefs, gtalk_pvt::prefs, aji_resource::resource, aji_buddy::resources, gtalk_pvt::rtp, gtalk_pvt::sid, strsep(), gtalk_pvt::them, and gtalk_pvt::us.

Referenced by gtalk_newcall(), and gtalk_request().

00940 {
00941    struct gtalk_pvt *tmp = NULL;
00942    struct aji_resource *resources = NULL;
00943    struct aji_buddy *buddy;
00944    char idroster[200];
00945    char *data, *exten = NULL;
00946    struct ast_sockaddr bindaddr_tmp;
00947 
00948    ast_debug(1, "The client is %s for alloc\n", client->name);
00949    if (!sid && !strchr(them, '/')) {   /* I started call! */
00950       if (!strcasecmp(client->name, "guest")) {
00951          buddy = ASTOBJ_CONTAINER_FIND(&client->connection->buddies, them);
00952          if (buddy) {
00953             resources = buddy->resources;
00954          }
00955       } else if (client->buddy) {
00956          resources = client->buddy->resources;
00957       }
00958 
00959       while (resources) {
00960          if (resources->cap->jingle) {
00961             break;
00962          }
00963          resources = resources->next;
00964       }
00965       if (resources) {
00966          snprintf(idroster, sizeof(idroster), "%s/%s", them, resources->resource);
00967       } else if ((*them == '+') || (strstr(them, "@voice.google.com"))) {
00968          snprintf(idroster, sizeof(idroster), "%s/srvres", them);
00969       } else {
00970          ast_log(LOG_ERROR, "no gtalk capable clients to talk to.\n");
00971          return NULL;
00972       }
00973    }
00974    if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
00975       return NULL;
00976    }
00977 
00978    memcpy(&tmp->prefs, &client->prefs, sizeof(struct ast_codec_pref));
00979 
00980    if (sid) {
00981       ast_copy_string(tmp->sid, sid, sizeof(tmp->sid));
00982       ast_copy_string(tmp->them, them, sizeof(tmp->them));
00983       ast_copy_string(tmp->us, us, sizeof(tmp->us));
00984    } else {
00985       snprintf(tmp->sid, sizeof(tmp->sid), "%08lx%08lx", ast_random(), ast_random());
00986       ast_copy_string(tmp->them, idroster, sizeof(tmp->them));
00987       ast_copy_string(tmp->us, us, sizeof(tmp->us));
00988       tmp->initiator = 1;
00989    }
00990    /* clear codecs */
00991    bindaddr.sin_family = AF_INET;
00992    ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
00993    if (!(tmp->rtp = ast_rtp_instance_new("asterisk", sched, &bindaddr_tmp, NULL))) {
00994      ast_log(LOG_ERROR, "Failed to create a new RTP instance (possibly an invalid bindaddr?)\n");
00995      ast_free(tmp);
00996      return NULL;
00997    }
00998    ast_rtp_instance_set_prop(tmp->rtp, AST_RTP_PROPERTY_RTCP, 1);
00999    ast_rtp_instance_set_prop(tmp->rtp, AST_RTP_PROPERTY_STUN, 1);
01000    ast_rtp_instance_set_prop(tmp->rtp, AST_RTP_PROPERTY_DTMF, 1);
01001    ast_rtp_instance_dtmf_mode_set(tmp->rtp, AST_RTP_DTMF_MODE_RFC2833);
01002    ast_rtp_codecs_payloads_clear(ast_rtp_instance_get_codecs(tmp->rtp), tmp->rtp);
01003 
01004    /* add user configured codec capabilites */
01005    if (client->capability) {
01006       tmp->capability = client->capability;
01007    } else if (global_capability) {
01008       tmp->capability = global_capability;
01009    }
01010 
01011    tmp->parent = client;
01012    if (!tmp->rtp) {
01013       ast_log(LOG_WARNING, "Out of RTP sessions?\n");
01014       ast_free(tmp);
01015       return NULL;
01016    }
01017 
01018    /* Set CALLERID(name) to the full JID of the remote peer */
01019    ast_copy_string(tmp->cid_name, tmp->them, sizeof(tmp->cid_name));
01020 
01021    if(strchr(tmp->us, '/')) {
01022       data = ast_strdupa(tmp->us);
01023       exten = strsep(&data, "/");
01024    } else {
01025       exten = tmp->us;
01026    }
01027    ast_copy_string(tmp->exten,  exten, sizeof(tmp->exten));
01028    ast_mutex_init(&tmp->lock);
01029    ast_mutex_lock(&gtalklock);
01030    tmp->next = client->p;
01031    client->p = tmp;
01032    ast_mutex_unlock(&gtalklock);
01033    return tmp;
01034 }

static int gtalk_answer ( struct ast_channel *  ast  )  [static]

Definition at line 470 of file chan_gtalk.c.

References ast_debug, ast_mutex_lock, ast_mutex_unlock, EVENT_FLAG_SYSTEM, gtalk_invite(), gtalk_pvt::lock, manager_event, ast_channel::name, gtalk_pvt::sid, ast_channel::tech_pvt, gtalk_pvt::them, and gtalk_pvt::us.

00471 {
00472    struct gtalk_pvt *p = ast->tech_pvt;
00473    int res = 0;
00474 
00475    ast_debug(1, "Answer!\n");
00476    ast_mutex_lock(&p->lock);
00477    gtalk_invite(p, p->them, p->us,p->sid, 0);
00478    manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate", "Channel: %s\r\nChanneltype: %s\r\nGtalk-SID: %s\r\n",
00479       ast->name, "GTALK", p->sid);
00480    ast_mutex_unlock(&p->lock);
00481    return res;
00482 }

static int gtalk_call ( struct ast_channel *  ast,
char *  dest,
int  timeout 
) [static]

Initiate new call, part of PBX interface dest is the dial string.

Definition at line 1759 of file chan_gtalk.c.

References ast_channel::_state, ast_copy_string(), ast_log(), ast_setstate(), AST_STATE_DOWN, AST_STATE_RESERVED, AST_STATE_RING, gtalk::connection, aji_client::f, gtalk_create_candidates(), gtalk_invite(), gtalk_ringing_ack(), LOG_WARNING, aji_client::mid, ast_channel::name, gtalk_pvt::parent, gtalk_pvt::ring, gtalk_pvt::ringrule, gtalk_pvt::sid, ast_channel::tech_pvt, gtalk_pvt::them, and gtalk_pvt::us.

01760 {
01761    struct gtalk_pvt *p = ast->tech_pvt;
01762 
01763    if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
01764       ast_log(LOG_WARNING, "gtalk_call called on %s, neither down nor reserved\n", ast->name);
01765       return -1;
01766    }
01767 
01768    ast_setstate(ast, AST_STATE_RING);
01769    if (!p->ringrule) {
01770       ast_copy_string(p->ring, p->parent->connection->mid, sizeof(p->ring));
01771       p->ringrule = iks_filter_add_rule(p->parent->connection->f, gtalk_ringing_ack, p,
01772                      IKS_RULE_ID, p->ring, IKS_RULE_DONE);
01773    } else {
01774       ast_log(LOG_WARNING, "Whoa, already have a ring rule!\n");
01775    }
01776 
01777    gtalk_invite(p, p->them, p->us, p->sid, 1);
01778    gtalk_create_candidates(p->parent, p, p->sid, p->them, p->us);
01779 
01780    return 0;
01781 }

static int gtalk_create_candidates ( struct gtalk *  client,
struct gtalk_pvt *  p,
char *  sid,
char *  from,
char *  to 
) [static]

Definition at line 802 of file chan_gtalk.c.

References AJI_CONNECT_LOCAL, AJI_CONNECT_RELAY, AJI_CONNECT_STUN, AJI_PROTOCOL_SSLTCP, AJI_PROTOCOL_UDP, ast_aji_increment_mid(), ast_aji_send(), ast_calloc, ast_copy_string(), ast_free, ast_log(), ast_random(), ast_rtp_instance_get_local_address(), ast_sockaddr_stringify_addr(), ast_sockaddr_to_sin, ast_strdupa, ast_strlen_zero(), gtalk::connection, gtalk_candidate::generation, GOOGLE_NS, GOOGLE_TRANSPORT_NS, gtalk_get_local_ip(), gtalk_update_externip(), gtalk_pvt::initiator, gtalk_candidate::ip, gtalk_pvt::laststun, LOG_ERROR, LOG_NOTICE, LOG_WARNING, aji_client::mid, gtalk_candidate::name, gtalk_candidate::next, gtalk_pvt::next, gtalk_pvt::ourcandidates, pass, gtalk_candidate::password, gtalk_candidate::port, gtalk_candidate::preference, gtalk_candidate::protocol, gtalk_pvt::rtp, gtalk_pvt::sid, gtalk_candidate::type, and gtalk_candidate::username.

Referenced by gtalk_call(), and gtalk_newcall().

00803 {
00804    struct gtalk_candidate *tmp;
00805    struct aji_client *c = client->connection;
00806    struct gtalk_candidate *ours1 = NULL, *ours2 = NULL;
00807    struct sockaddr_in sin = { 0, };
00808    struct ast_sockaddr sin_tmp;
00809    struct ast_sockaddr us;
00810    iks *iq, *gtalk, *candidate, *transport;
00811    char user[17], pass[17], preference[5], port[7];
00812    char *lowerfrom = NULL;
00813 
00814    iq = iks_new("iq");
00815    gtalk = iks_new("session");
00816    candidate = iks_new("candidate");
00817    transport = iks_new("transport");
00818    if (!iq || !gtalk || !candidate || !transport) {
00819       ast_log(LOG_ERROR, "Memory allocation error\n");
00820       goto safeout;
00821    }
00822    ours1 = ast_calloc(1, sizeof(*ours1));
00823    ours2 = ast_calloc(1, sizeof(*ours2));
00824    if (!ours1 || !ours2)
00825       goto safeout;
00826 
00827    iks_insert_attrib(transport, "xmlns",GOOGLE_TRANSPORT_NS);
00828    iks_insert_node(iq, gtalk);
00829    iks_insert_node(gtalk,candidate);
00830    iks_insert_node(gtalk,transport);
00831 
00832    for (; p; p = p->next) {
00833       if (!strcasecmp(p->sid, sid))
00834          break;
00835    }
00836 
00837    if (!p) {
00838       ast_log(LOG_NOTICE, "No matching gtalk session - SID %s!\n", sid);
00839       goto safeout;
00840    }
00841 
00842    ast_rtp_instance_get_local_address(p->rtp, &sin_tmp);
00843    ast_sockaddr_to_sin(&sin_tmp, &sin);
00844 
00845    gtalk_get_local_ip(&us);
00846 
00847    if (!strcmp(ast_sockaddr_stringify_addr(&us), "127.0.0.1")) {
00848       ast_log(LOG_WARNING, "Found a loopback IP on the system, check your network configuration or set the bindaddr attribute.");
00849    }
00850 
00851    /* Setup our gtalk candidates */
00852    ast_copy_string(ours1->name, "rtp", sizeof(ours1->name));
00853    ours1->port = ntohs(sin.sin_port);
00854    ours1->preference = 1;
00855    snprintf(user, sizeof(user), "%08lx%08lx", ast_random(), ast_random());
00856    snprintf(pass, sizeof(pass), "%08lx%08lx", ast_random(), ast_random());
00857    ast_copy_string(ours1->username, user, sizeof(ours1->username));
00858    ast_copy_string(ours1->password, pass, sizeof(ours1->password));
00859    ast_copy_string(ours1->ip, ast_sockaddr_stringify_addr(&us),
00860          sizeof(ours1->ip));
00861    ours1->protocol = AJI_PROTOCOL_UDP;
00862    ours1->type = AJI_CONNECT_LOCAL;
00863    ours1->generation = 0;
00864    p->ourcandidates = ours1;
00865 
00866    /* XXX this is a blocking action.  We send a STUN request to the server
00867     * and wait for the response.  If blocking here is a problem the STUN requests/responses
00868     * for the externip may need to be done differently. */
00869    gtalk_update_externip();
00870    if (!ast_strlen_zero(externip)) {
00871       ast_copy_string(ours2->username, user, sizeof(ours2->username));
00872       ast_copy_string(ours2->password, pass, sizeof(ours2->password));
00873       ast_copy_string(ours2->ip, externip, sizeof(ours2->ip));
00874       ast_copy_string(ours2->name, "rtp", sizeof(ours1->name));
00875       ours2->port = ntohs(sin.sin_port);
00876       ours2->preference = 0.9;
00877       ours2->protocol = AJI_PROTOCOL_UDP;
00878       ours2->type = AJI_CONNECT_STUN;
00879       ours2->generation = 0;
00880       ours1->next = ours2;
00881       ours2 = NULL;
00882    }
00883    ours1 = NULL;
00884 
00885    for (tmp = p->ourcandidates; tmp; tmp = tmp->next) {
00886       snprintf(port, sizeof(port), "%d", tmp->port);
00887       snprintf(preference, sizeof(preference), "%.2f", tmp->preference);
00888       iks_insert_attrib(iq, "from", to);
00889       iks_insert_attrib(iq, "to", from);
00890       iks_insert_attrib(iq, "type", "set");
00891       iks_insert_attrib(iq, "id", c->mid);
00892       ast_aji_increment_mid(c->mid);
00893       iks_insert_attrib(gtalk, "type", "candidates");
00894       iks_insert_attrib(gtalk, "id", sid);
00895       /* put the initiator attribute to lower case if we receive the call
00896        * otherwise GoogleTalk won't establish the session */
00897       if (!p->initiator) {
00898               char c;
00899          char *t = lowerfrom = ast_strdupa(from);
00900          while (((c = *t) != '/') && (*t++ = tolower(c)));
00901       }
00902       iks_insert_attrib(gtalk, "initiator", (p->initiator) ? to : lowerfrom);
00903       iks_insert_attrib(gtalk, "xmlns", GOOGLE_NS);
00904       iks_insert_attrib(candidate, "name", tmp->name);
00905       iks_insert_attrib(candidate, "address", tmp->ip);
00906       iks_insert_attrib(candidate, "port", port);
00907       iks_insert_attrib(candidate, "username", tmp->username);
00908       iks_insert_attrib(candidate, "password", tmp->password);
00909       iks_insert_attrib(candidate, "preference", preference);
00910       if (tmp->protocol == AJI_PROTOCOL_UDP)
00911          iks_insert_attrib(candidate, "protocol", "udp");
00912       if (tmp->protocol == AJI_PROTOCOL_SSLTCP)
00913          iks_insert_attrib(candidate, "protocol", "ssltcp");
00914       if (tmp->type == AJI_CONNECT_STUN)
00915          iks_insert_attrib(candidate, "type", "stun");
00916       if (tmp->type == AJI_CONNECT_LOCAL)
00917          iks_insert_attrib(candidate, "type", "local");
00918       if (tmp->type == AJI_CONNECT_RELAY)
00919          iks_insert_attrib(candidate, "type", "relay");
00920       iks_insert_attrib(candidate, "network", "0");
00921       iks_insert_attrib(candidate, "generation", "0");
00922       ast_aji_send(c, iq);
00923    }
00924    p->laststun = 0;
00925 
00926 safeout:
00927    if (ours1)
00928       ast_free(ours1);
00929    if (ours2)
00930       ast_free(ours2);
00931    iks_delete(iq);
00932    iks_delete(gtalk);
00933    iks_delete(candidate);
00934    iks_delete(transport);
00935 
00936    return 1;
00937 }

static int gtalk_create_member ( char *  label,
struct ast_variable *  var,
int  allowguest,
struct ast_codec_pref  prefs,
char *  context,
struct gtalk *  member 
) [static]

Definition at line 1973 of file chan_gtalk.c.

References gtalk::allowguest, ast_aji_get_client(), ast_copy_string(), ast_log(), ast_parse_allow_disallow(), ASTOBJ_CONTAINER_FIND, aji_client::buddies, gtalk::buddy, gtalk::capability, gtalk::connection, gtalk::context, aji_client::f, GOOGLE_NS, gtalk_parser(), LOG_ERROR, LOG_WARNING, ast_variable::name, gtalk::name, ast_variable::next, gtalk::parkinglot, gtalk::prefs, gtalk::user, and ast_variable::value.

Referenced by gtalk_load_config().

01976 {
01977    struct aji_client *client;
01978 
01979    if (!member)
01980       ast_log(LOG_WARNING, "Out of memory.\n");
01981 
01982    ast_copy_string(member->name, label, sizeof(member->name));
01983    ast_copy_string(member->user, label, sizeof(member->user));
01984    ast_copy_string(member->context, context, sizeof(member->context));
01985    member->allowguest = allowguest;
01986    member->prefs = prefs;
01987    while (var) {
01988       if (!strcasecmp(var->name, "username"))
01989          ast_copy_string(member->user, var->value, sizeof(member->user));
01990       else if (!strcasecmp(var->name, "disallow"))
01991          ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 0);
01992       else if (!strcasecmp(var->name, "allow"))
01993          ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 1);
01994       else if (!strcasecmp(var->name, "context"))
01995          ast_copy_string(member->context, var->value, sizeof(member->context));
01996       else if (!strcasecmp(var->name, "parkinglot"))
01997          ast_copy_string(member->parkinglot, var->value, sizeof(member->parkinglot));
01998       else if (!strcasecmp(var->name, "connection")) {
01999          if ((client = ast_aji_get_client(var->value))) {
02000             member->connection = client;
02001             iks_filter_add_rule(client->f, gtalk_parser, member,
02002                       IKS_RULE_TYPE, IKS_PAK_IQ,
02003                       IKS_RULE_FROM_PARTIAL, member->user,
02004                       IKS_RULE_NS, GOOGLE_NS,
02005                       IKS_RULE_DONE);
02006          } else {
02007             ast_log(LOG_ERROR, "connection referenced not found!\n");
02008             return 0;
02009          }
02010       }
02011       var = var->next;
02012    }
02013    if (member->connection && member->user)
02014       member->buddy = ASTOBJ_CONTAINER_FIND(&member->connection->buddies, member->user);
02015    else {
02016       ast_log(LOG_ERROR, "No Connection or Username!\n");
02017    }
02018    return 1;
02019 }

static int gtalk_digit_begin ( struct ast_channel *  ast,
char  digit 
) [static]

Definition at line 1659 of file chan_gtalk.c.

References ast_mutex_lock, ast_mutex_unlock, ast_rtp_instance_dtmf_begin(), gtalk_pvt::lock, gtalk_pvt::rtp, and ast_channel::tech_pvt.

01660 {
01661    struct gtalk_pvt *p = chan->tech_pvt;
01662    int res = 0;
01663 
01664    ast_mutex_lock(&p->lock);
01665    if (p->rtp) {
01666       ast_rtp_instance_dtmf_begin(p->rtp, digit);
01667    } else {
01668       res = -1;
01669    }
01670    ast_mutex_unlock(&p->lock);
01671 
01672    return res;
01673 }

static int gtalk_digit_end ( struct ast_channel *  ast,
char  digit,
unsigned int  duration 
) [static]

Definition at line 1675 of file chan_gtalk.c.

References ast_mutex_lock, ast_mutex_unlock, ast_rtp_instance_dtmf_end_with_duration(), gtalk_pvt::lock, gtalk_pvt::rtp, and ast_channel::tech_pvt.

01676 {
01677    struct gtalk_pvt *p = chan->tech_pvt;
01678    int res = 0;
01679 
01680    ast_mutex_lock(&p->lock);
01681    if (p->rtp) {
01682       ast_rtp_instance_dtmf_end_with_duration(p->rtp, digit, duration);
01683    } else {
01684       res = -1;
01685    }
01686    ast_mutex_unlock(&p->lock);
01687 
01688    return res;
01689 }

static int gtalk_fixup ( struct ast_channel *  oldchan,
struct ast_channel *  newchan 
) [static]

Definition at line 1606 of file chan_gtalk.c.

References ast_mutex_lock, ast_mutex_unlock, gtalk_pvt::lock, gtalk_pvt::owner, and ast_channel::tech_pvt.

01607 {
01608    struct gtalk_pvt *p = newchan->tech_pvt;
01609    ast_mutex_lock(&p->lock);
01610 
01611    if ((p->owner != oldchan)) {
01612       ast_mutex_unlock(&p->lock);
01613       return -1;
01614    }
01615    if (p->owner == oldchan)
01616       p->owner = newchan;
01617    ast_mutex_unlock(&p->lock);
01618    return 0;
01619 }

static void gtalk_free_candidates ( struct gtalk_candidate *  candidate  )  [static]

Definition at line 1165 of file chan_gtalk.c.

References ast_free, last, and gtalk_candidate::next.

Referenced by gtalk_free_pvt(), and gtalk_load_config().

01166 {
01167    struct gtalk_candidate *last;
01168    while (candidate) {
01169       last = candidate;
01170       candidate = candidate->next;
01171       ast_free(last);
01172    }
01173 }

static void gtalk_free_pvt ( struct gtalk *  client,
struct gtalk_pvt *  p 
) [static]

Definition at line 1175 of file chan_gtalk.c.

References ast_free, ast_log(), ast_rtp_instance_destroy(), gtalk::connection, aji_client::f, gtalk_free_candidates(), LOG_WARNING, gtalk_pvt::next, gtalk_pvt::owner, gtalk::p, gtalk_pvt::parent, gtalk_pvt::ringrule, gtalk_pvt::rtp, gtalk_pvt::theircandidates, and gtalk_pvt::vrtp.

Referenced by gtalk_hangup(), and gtalk_newcall().

01176 {
01177    struct gtalk_pvt *cur, *prev = NULL;
01178    cur = client->p;
01179    while (cur) {
01180       if (cur == p) {
01181          if (prev)
01182             prev->next = p->next;
01183          else
01184             client->p = p->next;
01185          break;
01186       }
01187       prev = cur;
01188       cur = cur->next;
01189    }
01190    if (p->ringrule)
01191       iks_filter_remove_rule(p->parent->connection->f, p->ringrule);
01192    if (p->owner)
01193       ast_log(LOG_WARNING, "Uh oh, there's an owner, this is going to be messy.\n");
01194    if (p->rtp)
01195       ast_rtp_instance_destroy(p->rtp);
01196    if (p->vrtp)
01197       ast_rtp_instance_destroy(p->vrtp);
01198    gtalk_free_candidates(p->theircandidates);
01199    ast_free(p);
01200 }

static format_t gtalk_get_codec ( struct ast_channel *  chan  )  [static]

Definition at line 503 of file chan_gtalk.c.

References gtalk_pvt::peercapability, and ast_channel::tech_pvt.

00504 {
00505    struct gtalk_pvt *p = chan->tech_pvt;
00506    return p->peercapability;
00507 }

static int gtalk_get_local_ip ( struct ast_sockaddr *  ourip  )  [static]

Definition at line 774 of file chan_gtalk.c.

References ast_find_ourip(), ast_free, ast_ouraddrfor(), ast_sockaddr_copy(), ast_sockaddr_from_sin, ast_sockaddr_is_any(), ast_sockaddr_resolve(), and PARSE_PORT_FORBID.

Referenced by gtalk_create_candidates(), and load_module().

00775 {
00776    struct ast_sockaddr root;
00777    struct ast_sockaddr bindaddr_tmp;
00778    struct ast_sockaddr *addrs;
00779    int addrs_cnt;
00780 
00781    /* If bind address is not 0.0.0.0, then bindaddr is our local ip. */
00782    ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
00783    if (!ast_sockaddr_is_any(&bindaddr_tmp)) {
00784       ast_sockaddr_copy(ourip, &bindaddr_tmp);
00785       return 0;
00786    }
00787 
00788    /* If no bind address was provided, lets see what ip we would use to connect to google.com and use that.
00789     * If you can't resolve google.com from your network, then this module is useless for you anyway. */
00790    if ((addrs_cnt = ast_sockaddr_resolve(&addrs, "google.com", PARSE_PORT_FORBID, AF_INET)) > 0) {
00791       ast_sockaddr_copy(&root, &addrs[0]);
00792       ast_free(addrs);
00793       if (!ast_ouraddrfor(&root, ourip)) {
00794          return 0;
00795       }
00796    }
00797 
00798    /* As a last resort, use this function to find our local address. */
00799    return ast_find_ourip(ourip, &bindaddr_tmp, AF_INET);
00800 }

static enum ast_rtp_glue_result gtalk_get_rtp_peer ( struct ast_channel *  chan,
struct ast_rtp_instance **  instance 
) [static]

Definition at line 484 of file chan_gtalk.c.

References ao2_ref, ast_mutex_lock, ast_mutex_unlock, AST_RTP_GLUE_RESULT_FORBID, AST_RTP_GLUE_RESULT_LOCAL, gtalk_pvt::lock, gtalk_pvt::rtp, and ast_channel::tech_pvt.

00485 {
00486    struct gtalk_pvt *p = chan->tech_pvt;
00487    enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_FORBID;
00488 
00489    if (!p)
00490       return res;
00491 
00492    ast_mutex_lock(&p->lock);
00493    if (p->rtp){
00494       ao2_ref(p->rtp, +1);
00495       *instance = p->rtp;
00496       res = AST_RTP_GLUE_RESULT_LOCAL;
00497    }
00498    ast_mutex_unlock(&p->lock);
00499 
00500    return res;
00501 }

static int gtalk_handle_dtmf ( struct gtalk *  client,
ikspak *  pak 
) [static]

Definition at line 675 of file chan_gtalk.c.

References AST_FRAME_DTMF, AST_FRAME_DTMF_BEGIN, AST_FRAME_DTMF_END, ast_log(), ast_queue_frame(), ast_verbose, gtalk::connection, gtalk_response(), ast_frame_subclass::integer, aji_client::jid, LOG_NOTICE, gtalk_pvt::next, gtalk_pvt::owner, gtalk::p, gtalk_pvt::sid, and ast_frame::subclass.

Referenced by gtalk_parser().

00676 {
00677    struct gtalk_pvt *tmp;
00678    iks *dtmfnode = NULL, *dtmfchild = NULL;
00679    char *dtmf;
00680    char *from;
00681    /* Make sure our new call doesn't exist yet */
00682    for (tmp = client->p; tmp; tmp = tmp->next) {
00683       if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid) || iks_find_with_attrib(pak->x, "gtalk", "sid", tmp->sid))
00684          break;
00685    }
00686    from = iks_find_attrib(pak->x, "to");
00687    if (!from) {
00688       from = client->connection->jid->full;
00689    }
00690 
00691    if (tmp) {
00692       if(iks_find_with_attrib(pak->x, "dtmf-method", "method", "rtp")) {
00693          gtalk_response(client, from, pak,
00694                "feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
00695                "unsupported-dtmf-method xmlns='http://jabber.org/protocol/gtalk/info/dtmf#errors'");
00696          return -1;
00697       }
00698       if ((dtmfnode = iks_find(pak->x, "dtmf"))) {
00699          if((dtmf = iks_find_attrib(dtmfnode, "code"))) {
00700             if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-up")) {
00701                struct ast_frame f = {AST_FRAME_DTMF_BEGIN, };
00702                f.subclass.integer = dtmf[0];
00703                ast_queue_frame(tmp->owner, &f);
00704                ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
00705             } else if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-down")) {
00706                struct ast_frame f = {AST_FRAME_DTMF_END, };
00707                f.subclass.integer = dtmf[0];
00708                ast_queue_frame(tmp->owner, &f);
00709                ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
00710             } else if(iks_find_attrib(pak->x, "dtmf")) { /* 250 millasecond default */
00711                struct ast_frame f = {AST_FRAME_DTMF, };
00712                f.subclass.integer = dtmf[0];
00713                ast_queue_frame(tmp->owner, &f);
00714                ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
00715             }
00716          }
00717       } else if ((dtmfnode = iks_find_with_attrib(pak->x, "gtalk", "action", "session-info"))) {
00718          if((dtmfchild = iks_find(dtmfnode, "dtmf"))) {
00719             if((dtmf = iks_find_attrib(dtmfchild, "code"))) {
00720                if(iks_find_with_attrib(dtmfnode, "dtmf", "action", "button-up")) {
00721                   struct ast_frame f = {AST_FRAME_DTMF_END, };
00722                   f.subclass.integer = dtmf[0];
00723                   ast_queue_frame(tmp->owner, &f);
00724                   ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
00725                } else if(iks_find_with_attrib(dtmfnode, "dtmf", "action", "button-down")) {
00726                   struct ast_frame f = {AST_FRAME_DTMF_BEGIN, };
00727                   f.subclass.integer = dtmf[0];
00728                   ast_queue_frame(tmp->owner, &f);
00729                   ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
00730                }
00731             }
00732          }
00733       }
00734       gtalk_response(client, from, pak, NULL, NULL);
00735       return 1;
00736    } else {
00737       ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
00738    }
00739 
00740    gtalk_response(client, from, pak, NULL, NULL);
00741    return 1;
00742 }

static int gtalk_hangup ( struct ast_channel *  ast  )  [static]

Hangup a call through the gtalk proxy channel.

Definition at line 1784 of file chan_gtalk.c.

References gtalk_pvt::alreadygone, ast_module_unref(), ast_mutex_lock, ast_mutex_unlock, gtalk_action(), gtalk_free_pvt(), gtalk_pvt::lock, gtalk_pvt::owner, gtalk_pvt::parent, and ast_channel::tech_pvt.

Referenced by gtalk_newcall().

01785 {
01786    struct gtalk_pvt *p = ast->tech_pvt;
01787    struct gtalk *client;
01788 
01789    ast_mutex_lock(&p->lock);
01790    client = p->parent;
01791    p->owner = NULL;
01792    ast->tech_pvt = NULL;
01793    if (!p->alreadygone) {
01794       gtalk_action(client, p, "terminate");
01795    }
01796    ast_mutex_unlock(&p->lock);
01797 
01798    gtalk_free_pvt(client, p);
01799    ast_module_unref(ast_module_info->self);
01800 
01801    return 0;
01802 }

static int gtalk_hangup_farend ( struct gtalk *  client,
ikspak *  pak 
) [static]

Definition at line 744 of file chan_gtalk.c.

References gtalk_pvt::alreadygone, ast_debug, ast_log(), ast_queue_hangup(), gtalk::connection, gtalk_response(), aji_client::jid, LOG_NOTICE, gtalk::name, gtalk_pvt::next, gtalk_pvt::owner, gtalk::p, and gtalk_pvt::sid.

Referenced by gtalk_parser().

00745 {
00746    struct gtalk_pvt *tmp;
00747    char *from;
00748 
00749    ast_debug(1, "The client is %s\n", client->name);
00750    /* Make sure our new call doesn't exist yet */
00751    for (tmp = client->p; tmp; tmp = tmp->next) {
00752       if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid) ||
00753          (iks_find_attrib(pak->query, "id") && !strcmp(iks_find_attrib(pak->query, "id"), tmp->sid))) {
00754          break;
00755       }
00756    }
00757    from = iks_find_attrib(pak->x, "to");
00758    if (!from) {
00759       from = client->connection->jid->full;
00760    }
00761 
00762    if (tmp) {
00763       tmp->alreadygone = 1;
00764       if (tmp->owner) {
00765          ast_queue_hangup(tmp->owner);
00766       }
00767    } else {
00768       ast_log(LOG_NOTICE, "Whoa, didn't find call during hangup!\n");
00769    }
00770    gtalk_response(client, from, pak, NULL, NULL);
00771    return 1;
00772 }

static int gtalk_indicate ( struct ast_channel *  ast,
int  condition,
const void *  data,
size_t  datalen 
) [static]

Definition at line 1621 of file chan_gtalk.c.

References AST_CONTROL_HOLD, AST_CONTROL_UNHOLD, ast_debug, ast_moh_start(), and ast_moh_stop().

01622 {
01623    int res = 0;
01624 
01625    switch (condition) {
01626    case AST_CONTROL_HOLD:
01627       ast_moh_start(ast, data, NULL);
01628       break;
01629    case AST_CONTROL_UNHOLD:
01630       ast_moh_stop(ast);
01631       break;
01632    default:
01633       ast_debug(3, "Don't know how to indicate condition '%d'\n", condition);
01634       res = -1;
01635    }
01636 
01637    return res;
01638 }

static int gtalk_invite ( struct gtalk_pvt *  p,
char *  to,
char *  from,
char *  sid,
int  initiator 
) [static]

Definition at line 380 of file chan_gtalk.c.

References add_codec_to_answer(), ast_aji_increment_mid(), ast_aji_send(), ast_codec_pref_index(), ast_log(), ast_strdupa, gtalk::capability, gtalk::connection, GOOGLE_AUDIO_NS, GOOGLE_NS, GOOGLE_TRANSPORT_NS, LOG_ERROR, aji_client::mid, gtalk_pvt::parent, and gtalk::prefs.

Referenced by gtalk_answer(), and gtalk_call().

00381 {
00382    struct gtalk *client = p->parent;
00383    iks *iq, *gtalk, *dcodecs, *payload_telephone, *transport;
00384    int x;
00385    int pref_codec = 0;
00386    int alreadysent = 0;
00387    int codecs_num = 0;
00388    char *lowerto = NULL;
00389 
00390    iq = iks_new("iq");
00391    gtalk = iks_new("session");
00392    dcodecs = iks_new("description");
00393    transport = iks_new("transport");
00394    payload_telephone = iks_new("payload-type");
00395    if (!(iq && gtalk && dcodecs && transport && payload_telephone)){
00396       iks_delete(iq);
00397       iks_delete(gtalk);
00398       iks_delete(dcodecs);
00399       iks_delete(transport);
00400       iks_delete(payload_telephone);
00401 
00402       ast_log(LOG_ERROR, "Could not allocate iksemel nodes\n");
00403       return 0;
00404    }
00405    iks_insert_attrib(dcodecs, "xmlns", GOOGLE_AUDIO_NS);
00406    iks_insert_attrib(dcodecs, "xml:lang", "en");
00407 
00408    for (x = 0; x < 64; x++) {
00409       if (!(pref_codec = ast_codec_pref_index(&client->prefs, x)))
00410          break;
00411       if (!(client->capability & pref_codec))
00412          continue;
00413       if (alreadysent & pref_codec)
00414          continue;
00415       codecs_num = add_codec_to_answer(p, pref_codec, dcodecs);
00416       alreadysent |= pref_codec;
00417    }
00418 
00419    if (codecs_num) {
00420       /* only propose DTMF within an audio session */
00421       iks_insert_attrib(payload_telephone, "id", "101");
00422       iks_insert_attrib(payload_telephone, "name", "telephone-event");
00423       iks_insert_attrib(payload_telephone, "clockrate", "8000");
00424    }
00425    iks_insert_attrib(transport,"xmlns",GOOGLE_TRANSPORT_NS);
00426 
00427    iks_insert_attrib(iq, "type", "set");
00428    iks_insert_attrib(iq, "to", to);
00429    iks_insert_attrib(iq, "from", from);
00430    iks_insert_attrib(iq, "id", client->connection->mid);
00431    ast_aji_increment_mid(client->connection->mid);
00432 
00433    iks_insert_attrib(gtalk, "xmlns", GOOGLE_NS);
00434    iks_insert_attrib(gtalk, "type",initiator ? "initiate": "accept");
00435    /* put the initiator attribute to lower case if we receive the call
00436     * otherwise GoogleTalk won't establish the session */
00437    if (!initiator) {
00438            char c;
00439            char *t = lowerto = ast_strdupa(to);
00440       while (((c = *t) != '/') && (*t++ = tolower(c)));
00441    }
00442    iks_insert_attrib(gtalk, "initiator", initiator ? from : lowerto);
00443    iks_insert_attrib(gtalk, "id", sid);
00444    iks_insert_node(iq, gtalk);
00445    iks_insert_node(gtalk, dcodecs);
00446    iks_insert_node(dcodecs, payload_telephone);
00447 
00448    ast_aji_send(client->connection, iq);
00449 
00450    iks_delete(payload_telephone);
00451    iks_delete(transport);
00452    iks_delete(dcodecs);
00453    iks_delete(gtalk);
00454    iks_delete(iq);
00455    return 1;
00456 }

static int gtalk_is_accepted ( struct gtalk *  client,
ikspak *  pak 
) [static]

Definition at line 646 of file chan_gtalk.c.

References ast_log(), gtalk::connection, gtalk_response(), gtalk_update_stun(), aji_client::jid, LOG_DEBUG, LOG_NOTICE, gtalk::name, gtalk_pvt::next, gtalk::p, gtalk_pvt::parent, and gtalk_pvt::sid.

Referenced by gtalk_parser().

00647 {
00648    struct gtalk_pvt *tmp;
00649    char *from;
00650 
00651    ast_log(LOG_DEBUG, "The client is %s\n", client->name);
00652    /* find corresponding call */
00653    for (tmp = client->p; tmp; tmp = tmp->next) {
00654       if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) {
00655          break;
00656       }
00657    }
00658 
00659    from = iks_find_attrib(pak->x, "to");
00660    if (!from) {
00661       from = client->connection->jid->full;
00662    }
00663 
00664    if (tmp) {
00665       gtalk_update_stun(tmp->parent, tmp);
00666    } else {
00667       ast_log(LOG_NOTICE, "Whoa, didn't find call during accept?!\n");
00668    }
00669 
00670    /* answer 'iq' packet to let the remote peer know that we're alive */
00671    gtalk_response(client, from, pak, NULL, NULL);
00672    return 1;
00673 }

static int gtalk_is_answered ( struct gtalk *  client,
ikspak *  pak 
) [static]

Definition at line 568 of file chan_gtalk.c.

References AST_CONTROL_ANSWER, ast_getformatname_multiple(), ast_log(), ast_queue_control(), ast_queue_hangup(), ast_rtp_codecs_payload_formats(), ast_rtp_codecs_payloads_set_m_type(), ast_rtp_codecs_payloads_set_rtpmap_type(), ast_rtp_instance_get_codecs(), gtalk_pvt::capability, gtalk::connection, gtalk_response(), gtalk_update_stun(), aji_client::jid, gtalk_pvt::jointcapability, LOG_DEBUG, LOG_WARNING, gtalk::name, gtalk_pvt::next, gtalk_pvt::owner, gtalk::p, gtalk_pvt::parent, gtalk_pvt::peercapability, gtalk_pvt::rtp, and gtalk_pvt::sid.

Referenced by gtalk_parser().

00569 {
00570    struct gtalk_pvt *tmp = NULL;
00571    char *from;
00572    iks *codec;
00573    char s1[BUFSIZ], s2[BUFSIZ], s3[BUFSIZ];
00574    int peernoncodeccapability;
00575 
00576    ast_log(LOG_DEBUG, "The client is %s\n", client->name);
00577 
00578    /* Make sure our new call does exist */
00579    for (tmp = client->p; tmp; tmp = tmp->next) {
00580       if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) {
00581          break;
00582       } else if (iks_find_with_attrib(pak->x, "ses:session", "id", tmp->sid)) {
00583          break;
00584       }
00585    }
00586 
00587    if (!tmp) {
00588       ast_log(LOG_WARNING, "Could not find session in iq\n");
00589       return -1;
00590    }
00591 
00592    /* codec points to the first <payload-type/> tag */
00593    codec = iks_first_tag(iks_first_tag(iks_first_tag(pak->x)));
00594    while (codec) {
00595       char *codec_id = iks_find_attrib(codec, "id");
00596       char *codec_name = iks_find_attrib(codec, "name");
00597       if (!codec_id || !codec_name) {
00598          codec = iks_next_tag(codec);
00599          continue;
00600       }
00601 
00602       ast_rtp_codecs_payloads_set_m_type(
00603          ast_rtp_instance_get_codecs(tmp->rtp),
00604          tmp->rtp,
00605          atoi(codec_id));
00606       ast_rtp_codecs_payloads_set_rtpmap_type(
00607          ast_rtp_instance_get_codecs(tmp->rtp),
00608          tmp->rtp,
00609          atoi(codec_id),
00610          "audio",
00611          codec_name,
00612          0);
00613       codec = iks_next_tag(codec);
00614    }
00615 
00616    /* Now gather all of the codecs that we are asked for */
00617    ast_rtp_codecs_payload_formats(ast_rtp_instance_get_codecs(tmp->rtp), &tmp->peercapability, &peernoncodeccapability);
00618 
00619    /* at this point, we received an awser from the remote Gtalk client,
00620       which allows us to compare capabilities */
00621    tmp->jointcapability = tmp->capability & tmp->peercapability;
00622    if (!tmp->jointcapability) {
00623       ast_log(LOG_WARNING, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1, BUFSIZ, tmp->capability),
00624          ast_getformatname_multiple(s2, BUFSIZ, tmp->peercapability),
00625          ast_getformatname_multiple(s3, BUFSIZ, tmp->jointcapability));
00626       /* close session if capabilities don't match */
00627       ast_queue_hangup(tmp->owner);
00628 
00629       return -1;
00630 
00631    }
00632 
00633    from = iks_find_attrib(pak->x, "to");
00634    if (!from) {
00635       from = client->connection->jid->full;
00636    }
00637 
00638    if (tmp->owner) {
00639       ast_queue_control(tmp->owner, AST_CONTROL_ANSWER);
00640    }
00641    gtalk_update_stun(tmp->parent, tmp);
00642    gtalk_response(client, from, pak, NULL, NULL);
00643    return 1;
00644 }

static int gtalk_load_config ( void   )  [static]

Definition at line 2021 of file chan_gtalk.c.

References gtalk::allowguest, ast_aji_get_clients(), ast_calloc, ast_category_browse(), ast_config_load, ast_copy_string(), ast_gethostbyname(), ast_jb_read_conf(), ast_log(), AST_MAX_CONTEXT, ast_parse_allow_disallow(), ast_parse_arg(), ast_true(), ast_variable_browse(), ast_variable_retrieve(), ASTOBJ_CONTAINER_LINK, ASTOBJ_CONTAINER_TRAVERSE, ASTOBJ_INIT, ASTOBJ_UNLOCK, ASTOBJ_UNREF, ASTOBJ_WRLOCK, gtalk::capability, clients, CONFIG_STATUS_FILEINVALID, gtalk::connection, gtalk::context, context, global_jbconf, GOOGLE_CONFIG, GOOGLE_NS, gtalk_create_member(), gtalk_free_candidates(), gtalk_list, gtalk_member_destroy(), gtalk_parser(), gtalk_update_externip(), hp, LOG_ERROR, LOG_WARNING, gtalk::name, ast_variable::name, ast_variable::next, gtalk::parkinglot, parkinglot, PARSE_INADDR, gtalk::prefs, STANDARD_STUN_PORT, stunaddr, gtalk::user, ast_variable::value, and var.

Referenced by load_module().

02022 {
02023    char *cat = NULL;
02024    struct ast_config *cfg = NULL;
02025    char context[AST_MAX_CONTEXT];
02026    char parkinglot[AST_MAX_CONTEXT];
02027    int allowguest = 1;
02028    struct ast_variable *var;
02029    struct gtalk *member;
02030    struct ast_codec_pref prefs;
02031    struct aji_client_container *clients;
02032    struct gtalk_candidate *global_candidates = NULL;
02033    struct hostent *hp;
02034    struct ast_hostent ahp;
02035    struct ast_flags config_flags = { 0 };
02036 
02037    cfg = ast_config_load(GOOGLE_CONFIG, config_flags);
02038    if (!cfg) {
02039       return 0;
02040    } else if (cfg == CONFIG_STATUS_FILEINVALID) {
02041       ast_log(LOG_ERROR, "Config file %s is in an invalid format.  Aborting.\n", GOOGLE_CONFIG);
02042       return 0;
02043    }
02044 
02045    /* Copy the default jb config over global_jbconf */
02046    memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
02047 
02048    /* set defaults */
02049    memset(&stunaddr, 0, sizeof(stunaddr));
02050 
02051    cat = ast_category_browse(cfg, NULL);
02052    for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
02053       /* handle jb conf */
02054       if (!ast_jb_read_conf(&global_jbconf, var->name, var->value))
02055          continue;
02056 
02057       if (!strcasecmp(var->name, "allowguest")) {
02058          allowguest = (ast_true(ast_variable_retrieve(cfg, "general", "allowguest"))) ? 1 : 0;
02059       } else if (!strcasecmp(var->name, "disallow")) {
02060          ast_parse_allow_disallow(&prefs, &global_capability, var->value, 0);
02061       } else if (!strcasecmp(var->name, "allow")) {
02062          ast_parse_allow_disallow(&prefs, &global_capability, var->value, 1);
02063       } else if (!strcasecmp(var->name, "context")) {
02064          ast_copy_string(context, var->value, sizeof(context));
02065       } else if (!strcasecmp(var->name, "externip")) {
02066          ast_copy_string(externip, var->value, sizeof(externip));
02067       } else if (!strcasecmp(var->name, "parkinglot")) {
02068          ast_copy_string(parkinglot, var->value, sizeof(parkinglot));
02069       } else if (!strcasecmp(var->name, "bindaddr")) {
02070          if (!(hp = ast_gethostbyname(var->value, &ahp))) {
02071             ast_log(LOG_WARNING, "Invalid address: %s\n", var->value);
02072          } else {
02073             memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
02074          }
02075       } else if (!strcasecmp(var->name, "stunaddr")) {
02076          stunaddr.sin_port = htons(STANDARD_STUN_PORT);
02077          if (ast_parse_arg(var->value, PARSE_INADDR, &stunaddr)) {
02078             ast_log(LOG_WARNING, "Invalid STUN server address: %s\n", var->value);
02079          }
02080       }
02081    }
02082    while (cat) {
02083       if (strcasecmp(cat, "general")) {
02084          var = ast_variable_browse(cfg, cat);
02085          member = ast_calloc(1, sizeof(*member));
02086          ASTOBJ_INIT(member);
02087          ASTOBJ_WRLOCK(member);
02088          if (!strcasecmp(cat, "guest")) {
02089             ast_copy_string(member->name, "guest", sizeof(member->name));
02090             ast_copy_string(member->user, "guest", sizeof(member->user));
02091             ast_copy_string(member->context, context, sizeof(member->context));
02092             ast_copy_string(member->parkinglot, parkinglot, sizeof(member->parkinglot));
02093             member->allowguest = allowguest;
02094             member->prefs = prefs;
02095             while (var) {
02096                if (!strcasecmp(var->name, "disallow")) {
02097                   ast_parse_allow_disallow(&member->prefs, &member->capability,
02098                                      var->value, 0);
02099                } else if (!strcasecmp(var->name, "allow")) {
02100                   ast_parse_allow_disallow(&member->prefs, &member->capability,
02101                                      var->value, 1);
02102                } else if (!strcasecmp(var->name, "context")) {
02103                   ast_copy_string(member->context, var->value,
02104                               sizeof(member->context));
02105                } else if (!strcasecmp(var->name, "parkinglot")) {
02106                   ast_copy_string(member->parkinglot, var->value,
02107                               sizeof(member->parkinglot));
02108                }
02109                var = var->next;
02110             }
02111             ASTOBJ_UNLOCK(member);
02112             clients = ast_aji_get_clients();
02113             if (clients) {
02114                ASTOBJ_CONTAINER_TRAVERSE(clients, 1, {
02115                   ASTOBJ_WRLOCK(iterator);
02116                   ASTOBJ_WRLOCK(member);
02117                   member->connection = NULL;
02118                   iks_filter_add_rule(iterator->f, gtalk_parser, member, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_NS, GOOGLE_NS, IKS_RULE_DONE);
02119                   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);
02120                   ASTOBJ_UNLOCK(member);
02121                   ASTOBJ_UNLOCK(iterator);
02122                });
02123                ASTOBJ_CONTAINER_LINK(&gtalk_list, member);
02124                ASTOBJ_UNREF(member, gtalk_member_destroy);
02125             } else {
02126                ASTOBJ_UNLOCK(member);
02127                ASTOBJ_UNREF(member, gtalk_member_destroy);
02128             }
02129          } else {
02130             ASTOBJ_UNLOCK(member);
02131             if (gtalk_create_member(cat, var, allowguest, prefs, context, member))
02132                ASTOBJ_CONTAINER_LINK(&gtalk_list, member);
02133             ASTOBJ_UNREF(member, gtalk_member_destroy);
02134          }
02135       }
02136       cat = ast_category_browse(cfg, cat);
02137    }
02138 
02139    gtalk_update_externip();
02140    gtalk_free_candidates(global_candidates);
02141    return 1;
02142 }

static void gtalk_member_destroy ( struct gtalk *  obj  )  [static]

Definition at line 238 of file chan_gtalk.c.

References ast_free.

Referenced by gtalk_load_config(), gtalk_parser(), gtalk_request(), and unload_module().

00239 {
00240    ast_free(obj);
00241 }

static struct ast_channel* gtalk_new ( struct gtalk *  client,
struct gtalk_pvt *  i,
int  state,
const char *  title,
const char *  linkedid 
) [static, read]

Start new gtalk channel.

Definition at line 1037 of file chan_gtalk.c.

References accountcode, gtalk::accountcode, ast_channel::adsicpe, ast_channel::amaflags, gtalk::amaflags, AST_ADSI_UNAVAILABLE, ast_best_codec(), AST_CAUSE_SWITCH_CONGESTION, ast_channel_alloc, ast_channel_set_fd(), ast_codec_choose(), ast_copy_string(), AST_FORMAT_VIDEO_MASK, ast_hangup(), ast_jb_configure(), ast_log(), ast_module_ref(), ast_pbx_start(), ast_random(), ast_rtp_codecs_packetization_set(), ast_rtp_instance_fd(), ast_rtp_instance_get_codecs(), AST_STATE_DOWN, AST_STATE_RING, ast_strdup, ast_string_field_set, ast_strlen_zero(), ast_channel::caller, gtalk::callgroup, ast_channel::callgroup, gtalk::callingpres, gtalk_pvt::capability, gtalk_pvt::cid_name, gtalk_pvt::cid_num, ast_channel::context, gtalk::context, ast_channel::dialed, EVENT_FLAG_SYSTEM, ast_channel::exten, gtalk_pvt::exten, global_jbconf, ast_channel::hangupcause, ast_party_caller::id, gtalk_pvt::jointcapability, language, gtalk::language, LOG_WARNING, manager_event, musicclass, gtalk::musicclass, ast_channel::name, ast_party_id::name, ast_channel::nativeformats, ast_party_dialed::number, ast_party_id::number, gtalk_pvt::owner, parkinglot, gtalk::parkinglot, gtalk::pickupgroup, ast_channel::pickupgroup, gtalk_pvt::prefs, ast_party_number::presentation, ast_party_name::presentation, ast_channel::priority, ast_channel::rawreadformat, ast_channel::rawwriteformat, ast_channel::readformat, ast_channel::rings, gtalk_pvt::rtp, gtalk_pvt::sid, ast_party_dialed::str, ast_channel::tech, ast_channel::tech_pvt, gtalk_pvt::us, gtalk_pvt::vrtp, and ast_channel::writeformat.

Referenced by gtalk_newcall(), and gtalk_request().

01038 {
01039    struct ast_channel *tmp;
01040    int fmt;
01041    int what;
01042    const char *n2;
01043 
01044    if (title)
01045       n2 = title;
01046    else
01047       n2 = i->us;
01048    tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, linkedid, client->accountcode, i->exten, client->context, client->amaflags, "Gtalk/%s-%04lx", n2, ast_random() & 0xffff);
01049    if (!tmp) {
01050       ast_log(LOG_WARNING, "Unable to allocate Gtalk channel structure!\n");
01051       return NULL;
01052    }
01053    tmp->tech = &gtalk_tech;
01054 
01055    /* Select our native format based on codec preference until we receive
01056       something from another device to the contrary. */
01057    if (i->jointcapability)
01058       what = i->jointcapability;
01059    else if (i->capability)
01060       what = i->capability;
01061    else
01062       what = global_capability;
01063 
01064    /* Set Frame packetization */
01065    if (i->rtp) {
01066       ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(i->rtp), i->rtp, &i->prefs);
01067    }
01068 
01069    tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | (i->jointcapability & AST_FORMAT_VIDEO_MASK);
01070    fmt = ast_best_codec(tmp->nativeformats);
01071 
01072    if (i->rtp) {
01073       ast_channel_set_fd(tmp, 0, ast_rtp_instance_fd(i->rtp, 0));
01074       ast_channel_set_fd(tmp, 1, ast_rtp_instance_fd(i->rtp, 1));
01075    }
01076    if (i->vrtp) {
01077       ast_channel_set_fd(tmp, 2, ast_rtp_instance_fd(i->vrtp, 0));
01078       ast_channel_set_fd(tmp, 3, ast_rtp_instance_fd(i->vrtp, 1));
01079    }
01080    if (state == AST_STATE_RING)
01081       tmp->rings = 1;
01082    tmp->adsicpe = AST_ADSI_UNAVAILABLE;
01083    tmp->writeformat = fmt;
01084    tmp->rawwriteformat = fmt;
01085    tmp->readformat = fmt;
01086    tmp->rawreadformat = fmt;
01087    tmp->tech_pvt = i;
01088 
01089    tmp->callgroup = client->callgroup;
01090    tmp->pickupgroup = client->pickupgroup;
01091    tmp->caller.id.name.presentation = client->callingpres;
01092    tmp->caller.id.number.presentation = client->callingpres;
01093    if (!ast_strlen_zero(client->accountcode))
01094       ast_string_field_set(tmp, accountcode, client->accountcode);
01095    if (client->amaflags)
01096       tmp->amaflags = client->amaflags;
01097    if (!ast_strlen_zero(client->language))
01098       ast_string_field_set(tmp, language, client->language);
01099    if (!ast_strlen_zero(client->musicclass))
01100       ast_string_field_set(tmp, musicclass, client->musicclass);
01101    if (!ast_strlen_zero(client->parkinglot))
01102       ast_string_field_set(tmp, parkinglot, client->parkinglot);
01103    i->owner = tmp;
01104    ast_module_ref(ast_module_info->self);
01105    ast_copy_string(tmp->context, client->context, sizeof(tmp->context));
01106    ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
01107 
01108    if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s")) {
01109       tmp->dialed.number.str = ast_strdup(i->exten);
01110    }
01111    tmp->priority = 1;
01112    if (i->rtp)
01113       ast_jb_configure(tmp, &global_jbconf);
01114    if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
01115       ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
01116       tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
01117       ast_hangup(tmp);
01118       tmp = NULL;
01119    } else {
01120       manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
01121          "Channel: %s\r\nChanneltype: %s\r\nGtalk-SID: %s\r\n",
01122          i->owner ? i->owner->name : "", "Gtalk", i->sid);
01123    }
01124    return tmp;
01125 }

static int gtalk_newcall ( struct gtalk *  client,
ikspak *  pak 
) [static]

Definition at line 1203 of file chan_gtalk.c.

References gtalk_pvt::alreadygone, ast_aji_get_client(), ast_channel_release(), ast_copy_string(), ast_getformatname_multiple(), ast_log(), ast_mutex_lock, ast_mutex_unlock, AST_PBX_CALL_LIMIT, AST_PBX_FAILED, ast_pbx_start(), AST_PBX_SUCCESS, ast_rtp_codecs_payload_formats(), ast_rtp_codecs_payloads_set_m_type(), ast_rtp_codecs_payloads_set_rtpmap_type(), ast_rtp_instance_get_codecs(), ast_setstate(), AST_STATE_DOWN, AST_STATE_RING, gtalk_pvt::capability, gtalk::connection, gtalk_action(), gtalk_alloc(), gtalk_create_candidates(), gtalk_free_pvt(), gtalk_hangup(), gtalk_new(), gtalk_response(), aji_client::jid, gtalk_pvt::jointcapability, gtalk_pvt::lock, LOG_ERROR, LOG_NOTICE, LOG_WARNING, gtalk::name, gtalk_pvt::next, gtalk::p, gtalk_pvt::peercapability, gtalk_pvt::rtp, S_OR, gtalk_pvt::sid, gtalk_pvt::them, gtalk_pvt::us, and gtalk_pvt::vrtp.

Referenced by gtalk_parser().

01204 {
01205    struct gtalk_pvt *p, *tmp = client->p;
01206    struct ast_channel *chan;
01207    int res;
01208    iks *codec;
01209    char *from = NULL;
01210    char s1[BUFSIZ], s2[BUFSIZ], s3[BUFSIZ];
01211    int peernoncodeccapability;
01212    char *sid;
01213 
01214    /* Make sure our new call doesn't exist yet */
01215    from = iks_find_attrib(pak->x,"to");
01216    if (!from) {
01217       from = client->connection->jid->full;
01218    }
01219 
01220    while (tmp) {
01221       if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid) ||
01222          (iks_find_attrib(pak->query, "id") && !strcmp(iks_find_attrib(pak->query, "id"), tmp->sid))) {
01223          ast_log(LOG_NOTICE, "Ignoring duplicate call setup on SID %s\n", tmp->sid);
01224          gtalk_response(client, from, pak, "out-of-order", NULL);
01225          return -1;
01226       }
01227       tmp = tmp->next;
01228    }
01229 
01230    if (!strcasecmp(client->name, "guest")){
01231       /* the guest account is not tied to any configured XMPP client,
01232          let's set it now */
01233       client->connection = ast_aji_get_client(from);
01234       if (!client->connection) {
01235          ast_log(LOG_ERROR, "No XMPP client to talk to, us (partial JID) : %s\n", from);
01236          return -1;
01237       }
01238    }
01239 
01240    if (!(sid = iks_find_attrib(pak->query, "id"))) {
01241       ast_log(LOG_WARNING, "Received Initiate without id attribute. Can not start call.\n");
01242       return -1;
01243    }
01244 
01245    p = gtalk_alloc(client, from, pak->from->full, sid);
01246    if (!p) {
01247       ast_log(LOG_WARNING, "Unable to allocate gtalk structure!\n");
01248       return -1;
01249    }
01250 
01251    chan = gtalk_new(client, p, AST_STATE_DOWN, pak->from->user, NULL);
01252    if (!chan) {
01253       gtalk_free_pvt(client, p);
01254       return -1;
01255    }
01256 
01257    ast_mutex_lock(&p->lock);
01258    ast_copy_string(p->them, pak->from->full, sizeof(p->them));
01259    ast_copy_string(p->sid, sid, sizeof(p->sid));
01260 
01261    /* codec points to the first <payload-type/> tag */
01262    codec = iks_first_tag(iks_first_tag(iks_first_tag(pak->x)));
01263 
01264    while (codec) {
01265       char *codec_id = iks_find_attrib(codec, "id");
01266       char *codec_name = iks_find_attrib(codec, "name");
01267       if (!codec_id || !codec_name) {
01268          codec = iks_next_tag(codec);
01269          continue;
01270       }
01271       if (!strcmp(S_OR(iks_name(codec), ""), "vid:payload-type") && p->vrtp) {
01272          ast_rtp_codecs_payloads_set_m_type(
01273             ast_rtp_instance_get_codecs(p->vrtp),
01274             p->vrtp,
01275             atoi(codec_id));
01276          ast_rtp_codecs_payloads_set_rtpmap_type(
01277             ast_rtp_instance_get_codecs(p->vrtp),
01278             p->vrtp,
01279             atoi(codec_id),
01280             "video",
01281             codec_name,
01282             0);
01283       } else {
01284          ast_rtp_codecs_payloads_set_m_type(
01285             ast_rtp_instance_get_codecs(p->rtp),
01286             p->rtp,
01287             atoi(codec_id));
01288          ast_rtp_codecs_payloads_set_rtpmap_type(
01289             ast_rtp_instance_get_codecs(p->rtp),
01290             p->rtp,
01291             atoi(codec_id),
01292             "audio",
01293             codec_name,
01294             0);
01295       }
01296       codec = iks_next_tag(codec);
01297    }
01298 
01299    /* Now gather all of the codecs that we are asked for */
01300    ast_rtp_codecs_payload_formats(ast_rtp_instance_get_codecs(p->rtp), &p->peercapability, &peernoncodeccapability);
01301    p->jointcapability = p->capability & p->peercapability;
01302    ast_mutex_unlock(&p->lock);
01303 
01304    ast_setstate(chan, AST_STATE_RING);
01305    if (!p->jointcapability) {
01306       ast_log(LOG_WARNING, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1, BUFSIZ, p->capability),
01307          ast_getformatname_multiple(s2, BUFSIZ, p->peercapability),
01308          ast_getformatname_multiple(s3, BUFSIZ, p->jointcapability));
01309       /* close session if capabilities don't match */
01310       gtalk_action(client, p, "reject");
01311       p->alreadygone = 1;
01312       gtalk_hangup(chan);
01313       ast_channel_release(chan);
01314       return -1;
01315    }
01316 
01317    res = ast_pbx_start(chan);
01318 
01319    switch (res) {
01320    case AST_PBX_FAILED:
01321       ast_log(LOG_WARNING, "Failed to start PBX :(\n");
01322       gtalk_response(client, from, pak, "service-unavailable", NULL);
01323       break;
01324    case AST_PBX_CALL_LIMIT:
01325       ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
01326       gtalk_response(client, from, pak, "service-unavailable", NULL);
01327       break;
01328    case AST_PBX_SUCCESS:
01329       gtalk_response(client, from, pak, NULL, NULL);
01330       gtalk_create_candidates(client, p, p->sid, p->them, p->us);
01331       /* nothing to do */
01332       break;
01333    }
01334 
01335    return 1;
01336 }

static int gtalk_parser ( void *  data,
ikspak *  pak 
) [static]

CLI command "gtalk reload".

Todo:
XXX TODO make this work.

Definition at line 1936 of file chan_gtalk.c.

References ast_debug, ast_log(), ast_strlen_zero(), ASTOBJ_REF, ASTOBJ_UNREF, gtalk_add_candidate(), gtalk_handle_dtmf(), gtalk_hangup_farend(), gtalk_is_accepted(), gtalk_is_answered(), gtalk_member_destroy(), gtalk_newcall(), LOG_NOTICE, LOG_WARNING, and S_OR.

Referenced by gtalk_create_member(), and gtalk_load_config().

01937 {
01938    struct gtalk *client = ASTOBJ_REF((struct gtalk *) data);
01939    int res;
01940 
01941    if (!strcmp(S_OR(iks_find_attrib(pak->x, "type"), ""), "error")) {
01942       ast_log(LOG_NOTICE, "Remote peer reported an error, trying to establish the call anyway\n");
01943    }
01944 
01945    if (ast_strlen_zero(iks_find_attrib(pak->query, "type"))) {
01946       ast_log(LOG_NOTICE, "No attribute \"type\" found.  Ignoring message.\n");
01947    } else if (!strcmp(iks_find_attrib(pak->query, "type"), "initiate")) {
01948       /* New call */
01949       gtalk_newcall(client, pak);
01950    } else if (!strcmp(iks_find_attrib(pak->query, "type"), "candidates") || !strcmp(iks_find_attrib(pak->query, "type"), "transport-info")) {
01951       ast_debug(3, "About to add candidate!\n");
01952       res = gtalk_add_candidate(client, pak);
01953       if (!res) {
01954          ast_log(LOG_WARNING, "Could not add any candidate\n");
01955       } else {
01956          ast_debug(3, "Candidate Added!\n");
01957       }
01958    } else if (!strcmp(iks_find_attrib(pak->query, "type"), "accept")) {
01959       gtalk_is_answered(client, pak);
01960    } else if (!strcmp(iks_find_attrib(pak->query, "type"), "transport-accept")) {
01961       gtalk_is_accepted(client, pak);
01962    } else if (!strcmp(iks_find_attrib(pak->query, "type"), "content-info") || iks_find_with_attrib(pak->x, "gtalk", "action", "session-info")) {
01963       gtalk_handle_dtmf(client, pak);
01964    } else if (!strcmp(iks_find_attrib(pak->query, "type"), "terminate")) {
01965       gtalk_hangup_farend(client, pak);
01966    } else if (!strcmp(iks_find_attrib(pak->query, "type"), "reject")) {
01967       gtalk_hangup_farend(client, pak);
01968    }
01969    ASTOBJ_UNREF(client, gtalk_member_destroy);
01970    return IKS_FILTER_EAT;
01971 }

static struct ast_frame * gtalk_read ( struct ast_channel *  ast  )  [static, read]

Definition at line 1548 of file chan_gtalk.c.

References ast_mutex_lock, ast_mutex_unlock, gtalk_rtp_read(), gtalk_pvt::lock, and ast_channel::tech_pvt.

01549 {
01550    struct ast_frame *fr;
01551    struct gtalk_pvt *p = ast->tech_pvt;
01552 
01553    ast_mutex_lock(&p->lock);
01554    fr = gtalk_rtp_read(ast, p);
01555    ast_mutex_unlock(&p->lock);
01556    return fr;
01557 }

static struct ast_channel * gtalk_request ( const char *  type,
format_t  format,
const struct ast_channel *  requestor,
void *  data,
int *  cause 
) [static, read]

Part of PBX interface.

Definition at line 1805 of file chan_gtalk.c.

References ast_aji_get_client(), ast_log(), AST_STATE_DOWN, ast_strdupa, ASTOBJ_UNLOCK, ASTOBJ_UNREF, ASTOBJ_WRLOCK, gtalk::connection, find_gtalk(), gtalk_alloc(), gtalk_member_destroy(), gtalk_new(), aji_client::jid, ast_channel::linkedid, LOG_ERROR, LOG_WARNING, gtalk::name, strsep(), and gtalk::user.

01806 {
01807    struct gtalk_pvt *p = NULL;
01808    struct gtalk *client = NULL;
01809    char *sender = NULL, *to = NULL, *s = NULL;
01810    struct ast_channel *chan = NULL;
01811 
01812    if (data) {
01813       s = ast_strdupa(data);
01814       if (s) {
01815          sender = strsep(&s, "/");
01816          if (sender && (sender[0] != '\0')) {
01817             to = strsep(&s, "/");
01818          }
01819          if (!to) {
01820             ast_log(LOG_ERROR, "Bad arguments in Gtalk Dialstring: %s\n", (char*) data);
01821             return NULL;
01822          }
01823       }
01824    }
01825 
01826    client = find_gtalk(to, sender);
01827    if (!client) {
01828       ast_log(LOG_WARNING, "Could not find recipient.\n");
01829       return NULL;
01830    }
01831    if (!strcasecmp(client->name, "guest")){
01832       /* the guest account is not tied to any configured XMPP client,
01833          let's set it now */
01834       client->connection = ast_aji_get_client(sender);
01835       if (!client->connection) {
01836          ast_log(LOG_ERROR, "No XMPP client to talk to, us (partial JID) : %s\n", sender);
01837          ASTOBJ_UNREF(client, gtalk_member_destroy);
01838          return NULL;
01839       }
01840    }
01841 
01842    ASTOBJ_WRLOCK(client);
01843    p = gtalk_alloc(client, strchr(sender, '@') ? sender : client->connection->jid->full, strchr(to, '@') ? to : client->user, NULL);
01844    if (p) {
01845       chan = gtalk_new(client, p, AST_STATE_DOWN, to, requestor ? requestor->linkedid : NULL);
01846    }
01847    ASTOBJ_UNLOCK(client);
01848    return chan;
01849 }

static int gtalk_response ( struct gtalk *  client,
char *  from,
ikspak *  pak,
const char *  reasonstr,
const char *  reasonstr2 
) [static]

Definition at line 536 of file chan_gtalk.c.

References ast_aji_send(), gtalk::connection, and S_OR.

Referenced by gtalk_handle_dtmf(), gtalk_hangup_farend(), gtalk_is_accepted(), gtalk_is_answered(), and gtalk_newcall().

00537 {
00538    iks *response = NULL, *error = NULL, *reason = NULL;
00539    int res = -1;
00540 
00541    response = iks_new("iq");
00542    if (response) {
00543       iks_insert_attrib(response, "type", "result");
00544       iks_insert_attrib(response, "from", from);
00545       iks_insert_attrib(response, "to", S_OR(iks_find_attrib(pak->x, "from"), ""));
00546       iks_insert_attrib(response, "id", S_OR(iks_find_attrib(pak->x, "id"), ""));
00547       if (reasonstr) {
00548          error = iks_new("error");
00549          if (error) {
00550             iks_insert_attrib(error, "type", "cancel");
00551             reason = iks_new(reasonstr);
00552             if (reason)
00553                iks_insert_node(error, reason);
00554             iks_insert_node(response, error);
00555          }
00556       }
00557       ast_aji_send(client->connection, response);
00558       res = 0;
00559    }
00560 
00561    iks_delete(reason);
00562    iks_delete(error);
00563    iks_delete(response);
00564 
00565    return res;
00566 }

static int gtalk_ringing_ack ( void *  data,
ikspak *  pak 
) [static]

Definition at line 458 of file chan_gtalk.c.

References AST_CONTROL_RINGING, ast_queue_control(), gtalk::connection, aji_client::f, gtalk_pvt::owner, gtalk_pvt::parent, and gtalk_pvt::ringrule.

Referenced by gtalk_call().

00459 {
00460    struct gtalk_pvt *p = data;
00461 
00462    if (p->ringrule)
00463       iks_filter_remove_rule(p->parent->connection->f, p->ringrule);
00464    p->ringrule = NULL;
00465    if (p->owner)
00466       ast_queue_control(p->owner, AST_CONTROL_RINGING);
00467    return IKS_FILTER_EAT;
00468 }

static struct ast_frame* gtalk_rtp_read ( struct ast_channel *  ast,
struct gtalk_pvt *  p 
) [static, read]

Definition at line 1519 of file chan_gtalk.c.

References ast_debug, AST_FORMAT_AUDIO_MASK, AST_FORMAT_VIDEO_MASK, AST_FRAME_VOICE, ast_getformatname(), ast_null_frame, ast_rtp_instance_read(), ast_set_read_format(), ast_set_write_format(), ast_frame_subclass::codec, f, ast_frame::frametype, gtalk_update_stun(), ast_channel::nativeformats, gtalk_pvt::owner, gtalk_pvt::parent, ast_channel::readformat, gtalk_pvt::rtp, ast_frame::subclass, and ast_channel::writeformat.

Referenced by gtalk_read().

01520 {
01521    struct ast_frame *f;
01522 
01523    if (!p->rtp) {
01524       return &ast_null_frame;
01525    }
01526    f = ast_rtp_instance_read(p->rtp, 0);
01527    gtalk_update_stun(p->parent, p);
01528    if (p->owner) {
01529       /* We already hold the channel lock */
01530       if (f->frametype == AST_FRAME_VOICE) {
01531          if (f->subclass.codec != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
01532             ast_debug(1, "Oooh, format changed to %s\n", ast_getformatname(f->subclass.codec));
01533             p->owner->nativeformats =
01534                (p->owner->nativeformats & AST_FORMAT_VIDEO_MASK) | f->subclass.codec;
01535             ast_set_read_format(p->owner, p->owner->readformat);
01536             ast_set_write_format(p->owner, p->owner->writeformat);
01537          }
01538          /* if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
01539             f = ast_dsp_process(p->owner, p->vad, f);
01540             if (option_debug && f && (f->frametype == AST_FRAME_DTMF))
01541                ast_debug(1, "* Detected inband DTMF '%c'\n", f->subclass);
01542            } */
01543       }
01544    }
01545    return f;
01546 }

static int gtalk_sendhtml ( struct ast_channel *  ast,
int  subclass,
const char *  data,
int  datalen 
) [static]

Definition at line 1750 of file chan_gtalk.c.

References ast_log(), and LOG_NOTICE.

01751 {
01752    ast_log(LOG_NOTICE, "XXX Implement gtalk sendhtml XXX\n");
01753 
01754    return -1;
01755 }

static int gtalk_sendtext ( struct ast_channel *  ast,
const char *  text 
) [static]

Definition at line 1640 of file chan_gtalk.c.

References ast_aji_send_chat(), ast_log(), gtalk::connection, LOG_ERROR, gtalk_pvt::parent, ast_channel::tech_pvt, and gtalk_pvt::them.

01641 {
01642    int res = 0;
01643    struct aji_client *client = NULL;
01644    struct gtalk_pvt *p = chan->tech_pvt;
01645 
01646    if (!p->parent) {
01647       ast_log(LOG_ERROR, "Parent channel not found\n");
01648       return -1;
01649    }
01650    if (!p->parent->connection) {
01651       ast_log(LOG_ERROR, "XMPP client not found\n");
01652       return -1;
01653    }
01654    client = p->parent->connection;
01655    res = ast_aji_send_chat(client, p->them, text);
01656    return res;
01657 }

static int gtalk_set_rtp_peer ( struct ast_channel *  chan,
struct ast_rtp_instance *  rtp,
struct ast_rtp_instance *  vrtp,
struct ast_rtp_instance *  trtp,
format_t  codecs,
int  nat_active 
) [static]

Definition at line 509 of file chan_gtalk.c.

References ast_mutex_lock, ast_mutex_unlock, gtalk_pvt::lock, and ast_channel::tech_pvt.

00510 {
00511    struct gtalk_pvt *p;
00512 
00513    p = chan->tech_pvt;
00514    if (!p)
00515       return -1;
00516    ast_mutex_lock(&p->lock);
00517 
00518 /* if (rtp)
00519       ast_rtp_get_peer(rtp, &p->redirip);
00520    else
00521       memset(&p->redirip, 0, sizeof(p->redirip));
00522    p->redircodecs = codecs; */
00523 
00524    /* Reset lastrtprx timer */
00525    ast_mutex_unlock(&p->lock);
00526    return 0;
00527 }

static char * gtalk_show_channels ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

CLI command "gtalk show channels".

Definition at line 1852 of file chan_gtalk.c.

References AJI_MAX_JIDLEN, ast_cli_args::argc, ast_cli(), ast_copy_string(), ast_getformatname(), ast_log(), ast_mutex_lock, ast_mutex_unlock, ASTOBJ_CONTAINER_TRAVERSE, ASTOBJ_UNLOCK, ASTOBJ_WRLOCK, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, FORMAT, gtalk_list, gtalklock, LOG_WARNING, ast_channel::name, gtalk_pvt::next, gtalk_pvt::owner, ast_channel::readformat, gtalk_pvt::them, ast_cli_entry::usage, and ast_channel::writeformat.

01853 {
01854 #define FORMAT  "%-30.30s  %-30.30s  %-15.15s  %-5.5s %-5.5s \n"
01855    struct gtalk_pvt *p;
01856    struct ast_channel *chan;
01857    int numchans = 0;
01858    char them[AJI_MAX_JIDLEN];
01859    char *jid = NULL;
01860    char *resource = NULL;
01861 
01862    switch (cmd) {
01863    case CLI_INIT:
01864       e->command = "gtalk show channels";
01865       e->usage =
01866          "Usage: gtalk show channels\n"
01867          "       Shows current state of the Gtalk channels.\n";
01868       return NULL;
01869    case CLI_GENERATE:
01870       return NULL;
01871    }
01872 
01873    if (a->argc != 3)
01874       return CLI_SHOWUSAGE;
01875 
01876    ast_mutex_lock(&gtalklock);
01877    ast_cli(a->fd, FORMAT, "Channel", "Jabber ID", "Resource", "Read", "Write");
01878    ASTOBJ_CONTAINER_TRAVERSE(&gtalk_list, 1, {
01879       ASTOBJ_WRLOCK(iterator);
01880       p = iterator->p;
01881       while(p) {
01882          chan = p->owner;
01883          ast_copy_string(them, p->them, sizeof(them));
01884          jid = them;
01885          resource = strchr(them, '/');
01886          if (!resource)
01887             resource = "None";
01888          else {
01889             *resource = '\0';
01890             resource ++;
01891          }
01892          if (chan)
01893             ast_cli(a->fd, FORMAT,
01894                chan->name,
01895                jid,
01896                resource,
01897                ast_getformatname(chan->readformat),
01898                ast_getformatname(chan->writeformat)
01899                );
01900          else
01901             ast_log(LOG_WARNING, "No available channel\n");
01902          numchans ++;
01903          p = p->next;
01904       }
01905       ASTOBJ_UNLOCK(iterator);
01906    });
01907 
01908    ast_mutex_unlock(&gtalklock);
01909 
01910    ast_cli(a->fd, "%d active gtalk channel%s\n", numchans, (numchans != 1) ? "s" : "");
01911    return CLI_SUCCESS;
01912 #undef FORMAT
01913 }

static int gtalk_update_externip ( void   )  [static]

Definition at line 1338 of file chan_gtalk.c.

References ast_connect(), ast_inet_ntoa(), ast_log(), ast_sockaddr_from_sin, ast_sockaddr_stringify(), ast_strdupa, ast_stun_request(), errno, LOG_WARNING, and stunaddr.

Referenced by gtalk_create_candidates(), and gtalk_load_config().

01339 {
01340    int sock;
01341    char *newaddr;
01342    struct sockaddr_in answer = { 0, };
01343    struct sockaddr_in *dst;
01344    struct ast_sockaddr tmp_dst;
01345 
01346    if (!stunaddr.sin_addr.s_addr) {
01347       return -1;
01348    }
01349    dst = &stunaddr;
01350 
01351    sock = socket(AF_INET, SOCK_DGRAM, 0);
01352    if (sock < 0) {
01353       ast_log(LOG_WARNING, "Unable to create STUN socket: %s\n", strerror(errno));
01354       return -1;
01355    }
01356 
01357    ast_sockaddr_from_sin(&tmp_dst, dst);
01358    if (ast_connect(sock, &tmp_dst) != 0) {
01359       ast_log(LOG_WARNING, "STUN Failed to connect to %s\n", ast_sockaddr_stringify(&tmp_dst));
01360       close(sock);
01361       return -1;
01362    }
01363 
01364    if ((ast_stun_request(sock, &stunaddr, NULL, &answer))) {
01365       close(sock);
01366       return -1;
01367    }
01368 
01369    newaddr = ast_strdupa(ast_inet_ntoa(answer.sin_addr));
01370    memcpy(externip, newaddr, sizeof(externip));
01371 
01372    close(sock);
01373    return 0;
01374 
01375 }

static int gtalk_update_stun ( struct gtalk *  client,
struct gtalk_pvt *  p 
) [static]

Definition at line 1377 of file chan_gtalk.c.

References ast_debug, ast_gethostbyname(), ast_inet_ntoa(), ast_log(), ast_rtp_instance_get_remote_address(), ast_rtp_instance_stun_request(), ast_sockaddr_from_sin, ast_sockaddr_to_sin, hp, gtalk_candidate::ip, gtalk_pvt::laststun, LOG_WARNING, gtalk_candidate::next, gtalk_pvt::ourcandidates, gtalk_candidate::port, gtalk_pvt::rtp, gtalk_pvt::theircandidates, and gtalk_candidate::username.

Referenced by gtalk_add_candidate(), gtalk_is_accepted(), gtalk_is_answered(), and gtalk_rtp_read().

01378 {
01379    struct gtalk_candidate *tmp;
01380    struct hostent *hp;
01381    struct ast_hostent ahp;
01382    struct sockaddr_in sin = { 0, };
01383    struct sockaddr_in aux = { 0, };
01384    struct ast_sockaddr sin_tmp;
01385    struct ast_sockaddr aux_tmp;
01386 
01387    if (time(NULL) == p->laststun)
01388       return 0;
01389 
01390    tmp = p->theircandidates;
01391    p->laststun = time(NULL);
01392    while (tmp) {
01393       char username[256];
01394 
01395       /* Find the IP address of the host */
01396       if (!(hp = ast_gethostbyname(tmp->ip, &ahp))) {
01397          ast_log(LOG_WARNING, "Could not get host by name for %s\n", tmp->ip);
01398          tmp = tmp->next;
01399          continue;
01400       }
01401       sin.sin_family = AF_INET;
01402       memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
01403       sin.sin_port = htons(tmp->port);
01404       snprintf(username, sizeof(username), "%s%s", tmp->username, p->ourcandidates->username);
01405 
01406       /* Find out the result of the STUN */
01407       ast_rtp_instance_get_remote_address(p->rtp, &aux_tmp);
01408       ast_sockaddr_to_sin(&aux_tmp, &aux);
01409 
01410       /* If the STUN result is different from the IP of the hostname,
01411        * lock on the stun IP of the hostname advertised by the
01412        * remote client */
01413       if (aux.sin_addr.s_addr && (aux.sin_addr.s_addr != sin.sin_addr.s_addr)) {
01414          ast_rtp_instance_stun_request(p->rtp, &aux_tmp, username);
01415       } else {
01416          ast_sockaddr_from_sin(&sin_tmp, &sin);
01417          ast_rtp_instance_stun_request(p->rtp, &sin_tmp, username);
01418       }
01419       if (aux.sin_addr.s_addr) {
01420          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);
01421          ast_debug(4, "Sending STUN request to %s\n", tmp->ip);
01422       }
01423 
01424       tmp = tmp->next;
01425    }
01426    return 1;
01427 }

static int gtalk_write ( struct ast_channel *  ast,
struct ast_frame *  f 
) [static]

Send frame to media channel (rtp).

Definition at line 1560 of file chan_gtalk.c.

References AST_FRAME_IMAGE, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_getformatname(), ast_getformatname_multiple(), ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_rtp_instance_write(), ast_frame_subclass::codec, ast_frame::frametype, gtalk_pvt::lock, LOG_WARNING, ast_channel::nativeformats, ast_channel::readformat, gtalk_pvt::rtp, ast_frame::subclass, ast_channel::tech_pvt, gtalk_pvt::vrtp, and ast_channel::writeformat.

01561 {
01562    struct gtalk_pvt *p = ast->tech_pvt;
01563    int res = 0;
01564    char buf[256];
01565 
01566    switch (frame->frametype) {
01567    case AST_FRAME_VOICE:
01568       if (!(frame->subclass.codec & ast->nativeformats)) {
01569          ast_log(LOG_WARNING,
01570                "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
01571                ast_getformatname(frame->subclass.codec),
01572                ast_getformatname_multiple(buf, sizeof(buf), ast->nativeformats),
01573                ast_getformatname(ast->readformat),
01574                ast_getformatname(ast->writeformat));
01575          return 0;
01576       }
01577       if (p) {
01578          ast_mutex_lock(&p->lock);
01579          if (p->rtp) {
01580             res = ast_rtp_instance_write(p->rtp, frame);
01581          }
01582          ast_mutex_unlock(&p->lock);
01583       }
01584       break;
01585    case AST_FRAME_VIDEO:
01586       if (p) {
01587          ast_mutex_lock(&p->lock);
01588          if (p->vrtp) {
01589             res = ast_rtp_instance_write(p->vrtp, frame);
01590          }
01591          ast_mutex_unlock(&p->lock);
01592       }
01593       break;
01594    case AST_FRAME_IMAGE:
01595       return 0;
01596       break;
01597    default:
01598       ast_log(LOG_WARNING, "Can't send %d type frames with Gtalk write\n",
01599             frame->frametype);
01600       return 0;
01601    }
01602 
01603    return res;
01604 }

static int load_module ( void   )  [static]

Load module into PBX, register channel.

Definition at line 2145 of file chan_gtalk.c.

References __ourip, ARRAY_LEN, ast_channel_register(), ast_cli_register_multiple(), ast_log(), ast_module_helper(), AST_MODULE_LOAD_DECLINE, ast_rtp_glue_register, ast_sockaddr_from_sin, ast_sockaddr_ipv4(), ASTOBJ_CONTAINER_INIT, free, GOOGLE_CONFIG, gtalk_get_local_ip(), gtalk_list, gtalk_load_config(), io_context_create(), LOG_ERROR, LOG_WARNING, sched_context_create(), and ast_channel_tech::type.

02146 {
02147    struct ast_sockaddr bindaddr_tmp;
02148    struct ast_sockaddr ourip_tmp;
02149 
02150    char *jabber_loaded = ast_module_helper("", "res_jabber.so", 0, 0, 0, 0);
02151    free(jabber_loaded);
02152    if (!jabber_loaded) {
02153       /* If embedded, check for a different module name */
02154       jabber_loaded = ast_module_helper("", "res_jabber", 0, 0, 0, 0);
02155       free(jabber_loaded);
02156       if (!jabber_loaded) {
02157          ast_log(LOG_ERROR, "chan_gtalk.so depends upon res_jabber.so\n");
02158          return AST_MODULE_LOAD_DECLINE;
02159       }
02160    }
02161 
02162    ASTOBJ_CONTAINER_INIT(&gtalk_list);
02163    if (!gtalk_load_config()) {
02164       ast_log(LOG_ERROR, "Unable to read config file %s. Not loading module.\n", GOOGLE_CONFIG);
02165       return 0;
02166    }
02167 
02168    sched = sched_context_create();
02169    if (!sched) {
02170       ast_log(LOG_WARNING, "Unable to create schedule context\n");
02171    }
02172 
02173    io = io_context_create();
02174    if (!io) {
02175       ast_log(LOG_WARNING, "Unable to create I/O context\n");
02176    }
02177 
02178    ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
02179    if (gtalk_get_local_ip(&ourip_tmp)) {
02180       ast_log(LOG_WARNING, "Unable to get own IP address, Gtalk disabled\n");
02181       return 0;
02182    }
02183    __ourip.s_addr = htonl(ast_sockaddr_ipv4(&ourip_tmp));
02184 
02185    ast_rtp_glue_register(&gtalk_rtp_glue);
02186    ast_cli_register_multiple(gtalk_cli, ARRAY_LEN(gtalk_cli));
02187 
02188    /* Make sure we can register our channel type */
02189    if (ast_channel_register(&gtalk_tech)) {
02190       ast_log(LOG_ERROR, "Unable to register channel class %s\n", gtalk_tech.type);
02191       return -1;
02192    }
02193    return 0;
02194 }

static int unload_module ( void   )  [static]

Reload module.

Todo:
XXX TODO make this work.

Unload the gtalk channel from Asterisk

Definition at line 2205 of file chan_gtalk.c.

References ARRAY_LEN, ast_channel_unregister(), ast_cli_unregister_multiple(), ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_rtp_glue_unregister(), ast_softhangup(), AST_SOFTHANGUP_APPUNLOAD, ASTOBJ_CONTAINER_DESTROY, ASTOBJ_CONTAINER_DESTROYALL, ASTOBJ_CONTAINER_TRAVERSE, ASTOBJ_UNLOCK, ASTOBJ_WRLOCK, gtalk_list, gtalk_member_destroy(), gtalklock, LOG_WARNING, gtalk_pvt::next, and gtalk_pvt::owner.

02206 {
02207    struct gtalk_pvt *privates = NULL;
02208    ast_cli_unregister_multiple(gtalk_cli, ARRAY_LEN(gtalk_cli));
02209    /* First, take us out of the channel loop */
02210    ast_channel_unregister(&gtalk_tech);
02211    ast_rtp_glue_unregister(&gtalk_rtp_glue);
02212 
02213    if (!ast_mutex_lock(&gtalklock)) {
02214       /* Hangup all interfaces if they have an owner */
02215       ASTOBJ_CONTAINER_TRAVERSE(&gtalk_list, 1, {
02216          ASTOBJ_WRLOCK(iterator);
02217          privates = iterator->p;
02218          while(privates) {
02219             if (privates->owner)
02220                ast_softhangup(privates->owner, AST_SOFTHANGUP_APPUNLOAD);
02221             privates = privates->next;
02222          }
02223          iterator->p = NULL;
02224          ASTOBJ_UNLOCK(iterator);
02225       });
02226       ast_mutex_unlock(&gtalklock);
02227    } else {
02228       ast_log(LOG_WARNING, "Unable to lock the monitor\n");
02229       return -1;
02230    }
02231    ASTOBJ_CONTAINER_DESTROYALL(&gtalk_list, gtalk_member_destroy);
02232    ASTOBJ_CONTAINER_DESTROY(&gtalk_list);
02233    return 0;
02234 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Gtalk Channel Driver" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CHANNEL_DRIVER, } [static]

Definition at line 2241 of file chan_gtalk.c.

struct in_addr __ourip [static]

Definition at line 226 of file chan_gtalk.c.

Referenced by load_module().

Definition at line 2241 of file chan_gtalk.c.

struct sockaddr_in bindaddr = { 0, } [static]

struct ast_jb_conf default_jbconf [static]

Global jitterbuffer configuration - by default, jb is disabled

Definition at line 82 of file chan_gtalk.c.

const char desc[] = "Gtalk Channel" [static]

Definition at line 168 of file chan_gtalk.c.

char externip[16] [static]

Definition at line 233 of file chan_gtalk.c.

format_t global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263 [static]

Definition at line 170 of file chan_gtalk.c.

struct ast_jb_conf global_jbconf [static]

Definition at line 90 of file chan_gtalk.c.

Referenced by gtalk_load_config(), and gtalk_new().

struct ast_cli_entry gtalk_cli[] [static]

Initial value:

 {

   AST_CLI_DEFINE(gtalk_show_channels, "Show GoogleTalk channels"),
}

Definition at line 228 of file chan_gtalk.c.

struct gtalk_container gtalk_list [static]

struct ast_rtp_glue gtalk_rtp_glue [static]

Definition at line 529 of file chan_gtalk.c.

struct ast_channel_tech gtalk_tech [static]

PBX interface structure for channel registration.

Definition at line 198 of file chan_gtalk.c.

ast_mutex_t gtalklock = { { { NULL }, { 0 }, 0, { NULL }, { 0 }, {{{ 0 }}}, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP } , 1, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP } [static]

Protect the interface list (of gtalk_pvt's)

Definition at line 172 of file chan_gtalk.c.

Referenced by gtalk_alloc(), gtalk_show_channels(), and unload_module().

struct io_context* io [static]

The IO context

Definition at line 225 of file chan_gtalk.c.

struct sched_context* sched [static]

The scheduling context

Definition at line 224 of file chan_gtalk.c.

struct sockaddr_in stunaddr [static]

the stun server we get the externip from

Definition at line 234 of file chan_gtalk.c.

Referenced by gtalk_load_config(), and gtalk_update_externip().


Generated on Sat Mar 26 11:15:23 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.9