![]() |
Home page |
Mailing list |
Docs
Asterisk developer's documentation :: Codename Pineapple
chan_gtalk.c
Go to the documentation of this file.
00001 /* 00002 * Asterisk -- An open source telephony toolkit. 00003 * 00004 * Copyright (C) 1999 - 2005, Digium, Inc. 00005 * 00006 * Matt O'Gorman <mogorman@digium.com> 00007 * 00008 * See http://www.asterisk.org for more information about 00009 * the Asterisk project. Please do not directly contact 00010 * any of the maintainers of this project for assistance; 00011 * the project provides a web site, mailing lists and IRC 00012 * channels for your use. 00013 * 00014 * This program is free software, distributed under the terms of 00015 * the GNU General Public License Version 2. See the LICENSE file 00016 * at the top of the source tree. 00017 */ 00018 00019 /*! \file 00020 * 00021 * \author Matt O'Gorman <mogorman@digium.com> 00022 * 00023 * \brief Gtalk Channel Driver, until google/libjingle works with jingle spec 00024 * 00025 * \ingroup channel_drivers 00026 */ 00027 00028 /*** MODULEINFO 00029 <depend>iksemel</depend> 00030 ***/ 00031 00032 #include "asterisk.h" 00033 00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 51806 $") 00035 00036 #include <stdlib.h> 00037 #include <stdio.h> 00038 #include <string.h> 00039 #include <unistd.h> 00040 #include <sys/socket.h> 00041 #include <errno.h> 00042 #include <stdlib.h> 00043 #include <fcntl.h> 00044 #include <netdb.h> 00045 #include <netinet/in.h> 00046 #include <arpa/inet.h> 00047 #include <sys/signal.h> 00048 #include <iksemel.h> 00049 00050 #include "asterisk/lock.h" 00051 #include "asterisk/channel.h" 00052 #include "asterisk/config.h" 00053 #include "asterisk/logger.h" 00054 #include "asterisk/module.h" 00055 #include "asterisk/pbx.h" 00056 #include "asterisk/options.h" 00057 #include "asterisk/lock.h" 00058 #include "asterisk/sched.h" 00059 #include "asterisk/io.h" 00060 #include "asterisk/rtp.h" 00061 #include "asterisk/acl.h" 00062 #include "asterisk/callerid.h" 00063 #include "asterisk/file.h" 00064 #include "asterisk/cli.h" 00065 #include "asterisk/app.h" 00066 #include "asterisk/musiconhold.h" 00067 #include "asterisk/manager.h" 00068 #include "asterisk/stringfields.h" 00069 #include "asterisk/utils.h" 00070 #include "asterisk/causes.h" 00071 #include "asterisk/astobj.h" 00072 #include "asterisk/abstract_jb.h" 00073 #include "asterisk/jabber.h" 00074 00075 #define GOOGLE_CONFIG "gtalk.conf" 00076 00077 #define GOOGLE_NS "http://www.google.com/session" 00078 00079 00080 /*! Global jitterbuffer configuration - by default, jb is disabled */ 00081 static struct ast_jb_conf default_jbconf = 00082 { 00083 .flags = 0, 00084 .max_size = -1, 00085 .resync_threshold = -1, 00086 .impl = "" 00087 }; 00088 static struct ast_jb_conf global_jbconf; 00089 00090 enum gtalk_protocol { 00091 AJI_PROTOCOL_UDP = 1, 00092 AJI_PROTOCOL_SSLTCP = 2, 00093 }; 00094 00095 enum gtalk_connect_type { 00096 AJI_CONNECT_STUN = 1, 00097 AJI_CONNECT_LOCAL = 2, 00098 AJI_CONNECT_RELAY = 3, 00099 }; 00100 00101 struct gtalk_pvt { 00102 ast_mutex_t lock; /*!< Channel private lock */ 00103 time_t laststun; 00104 struct gtalk *parent; /*!< Parent client */ 00105 char sid[100]; 00106 char us[100]; 00107 char them[100]; 00108 char ring[10]; /*!< Message ID of ring */ 00109 iksrule *ringrule; /*!< Rule for matching RING request */ 00110 int initiator; /*!< If we're the initiator */ 00111 int alreadygone; 00112 int capability; 00113 struct ast_codec_pref prefs; 00114 struct gtalk_candidate *theircandidates; 00115 struct gtalk_candidate *ourcandidates; 00116 char cid_num[80]; /*!< Caller ID num */ 00117 char cid_name[80]; /*!< Caller ID name */ 00118 char exten[80]; /*!< Called extension */ 00119 struct ast_channel *owner; /*!< Master Channel */ 00120 struct ast_rtp *rtp; /*!< RTP audio session */ 00121 struct ast_rtp *vrtp; /*!< RTP video session */ 00122 int jointcapability; /*!< Supported capability at both ends (codecs ) */ 00123 int peercapability; 00124 struct gtalk_pvt *next; /* Next entity */ 00125 }; 00126 00127 struct gtalk_candidate { 00128 char name[100]; 00129 enum gtalk_protocol protocol; 00130 double preference; 00131 char username[100]; 00132 char password[100]; 00133 enum gtalk_connect_type type; 00134 char network[6]; 00135 int generation; 00136 char ip[16]; 00137 int port; 00138 int receipt; 00139 struct gtalk_candidate *next; 00140 }; 00141 00142 struct gtalk { 00143 ASTOBJ_COMPONENTS(struct gtalk); 00144 struct aji_client *connection; 00145 struct aji_buddy *buddy; 00146 struct gtalk_pvt *p; 00147 struct ast_codec_pref prefs; 00148 int amaflags; /*!< AMA Flags */ 00149 char user[100]; 00150 char context[100]; 00151 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */ 00152 int capability; 00153 ast_group_t callgroup; /*!< Call group */ 00154 ast_group_t pickupgroup; /*!< Pickup group */ 00155 int callingpres; /*!< Calling presentation */ 00156 int allowguest; 00157 char language[MAX_LANGUAGE]; /*!< Default language for prompts */ 00158 char musicclass[MAX_MUSICCLASS]; /*!< Music on Hold class */ 00159 }; 00160 00161 struct gtalk_container { 00162 ASTOBJ_CONTAINER_COMPONENTS(struct gtalk); 00163 }; 00164 00165 static const char desc[] = "Gtalk Channel"; 00166 00167 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263; 00168 00169 AST_MUTEX_DEFINE_STATIC(gtalklock); /*!< Protect the interface list (of gtalk_pvt's) */ 00170 00171 /* Forward declarations */ 00172 static struct ast_channel *gtalk_request(const char *type, int format, void *data, int *cause); 00173 static int gtalk_digit(struct ast_channel *ast, char digit, unsigned int duration); 00174 static int gtalk_digit_begin(struct ast_channel *ast, char digit); 00175 static int gtalk_digit_end(struct ast_channel *ast, char digit, unsigned int duration); 00176 static int gtalk_call(struct ast_channel *ast, char *dest, int timeout); 00177 static int gtalk_hangup(struct ast_channel *ast); 00178 static int gtalk_answer(struct ast_channel *ast); 00179 static int gtalk_newcall(struct gtalk *client, ikspak *pak); 00180 static struct ast_frame *gtalk_read(struct ast_channel *ast); 00181 static int gtalk_write(struct ast_channel *ast, struct ast_frame *f); 00182 static int gtalk_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen); 00183 static int gtalk_fixup(struct ast_channel *oldchan, struct ast_channel *newchan); 00184 static int gtalk_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen); 00185 static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const char *them, const char *sid); 00186 static int gtalk_do_reload(int fd, int argc, char **argv); 00187 static int gtalk_show_channels(int fd, int argc, char **argv); 00188 /*----- RTP interface functions */ 00189 static int gtalk_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, 00190 struct ast_rtp *vrtp, int codecs, int nat_active); 00191 static enum ast_rtp_get_result gtalk_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp); 00192 static int gtalk_get_codec(struct ast_channel *chan); 00193 00194 /*! \brief PBX interface structure for channel registration */ 00195 static const struct ast_channel_tech gtalk_tech = { 00196 .type = "Gtalk", 00197 .description = "Gtalk Channel Driver", 00198 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1), 00199 .requester = gtalk_request, 00200 .send_digit_begin = gtalk_digit_begin, 00201 .send_digit_end = gtalk_digit_end, 00202 .bridge = ast_rtp_bridge, 00203 .call = gtalk_call, 00204 .hangup = gtalk_hangup, 00205 .answer = gtalk_answer, 00206 .read = gtalk_read, 00207 .write = gtalk_write, 00208 .exception = gtalk_read, 00209 .indicate = gtalk_indicate, 00210 .fixup = gtalk_fixup, 00211 .send_html = gtalk_sendhtml, 00212 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER 00213 }; 00214 00215 static struct sockaddr_in bindaddr = { 0, }; /*!< The address we bind to */ 00216 00217 static struct sched_context *sched; /*!< The scheduling context */ 00218 static struct io_context *io; /*!< The IO context */ 00219 static struct in_addr __ourip; 00220 00221 00222 /*! \brief RTP driver interface */ 00223 static struct ast_rtp_protocol gtalk_rtp = { 00224 type: "Gtalk", 00225 get_rtp_info: gtalk_get_rtp_peer, 00226 set_rtp_peer: gtalk_set_rtp_peer, 00227 get_codec: gtalk_get_codec, 00228 }; 00229 00230 static const char debug_usage[] = 00231 "Usage: gtalk show channels\n" 00232 " Shows current state of the Gtalk channels.\n"; 00233 00234 static const char reload_usage[] = 00235 "Usage: gtalk reload\n" 00236 " Reload gtalk channel driver.\n"; 00237 00238 00239 static struct ast_cli_entry gtalk_cli[] = { 00240 {{ "gtalk", "reload", NULL}, gtalk_do_reload, "Enable Jabber debugging", reload_usage }, 00241 {{ "gtalk", "show", "channels", NULL}, gtalk_show_channels, "Show GoogleTalk Channels", debug_usage }, 00242 }; 00243 00244 00245 00246 static char externip[16]; 00247 00248 static struct gtalk_container gtalk_list; 00249 00250 static void gtalk_member_destroy(struct gtalk *obj) 00251 { 00252 free(obj); 00253 } 00254 00255 static struct gtalk *find_gtalk(char *name, char *connection) 00256 { 00257 struct gtalk *gtalk = NULL; 00258 char *domain = NULL , *s = NULL; 00259 if(strchr(connection, '@')) { 00260 s = ast_strdupa((char *) connection); 00261 domain = strsep(&s, "@"); 00262 ast_verbose("OOOOH domain = %s\n", domain); 00263 free(s); 00264 } 00265 gtalk = ASTOBJ_CONTAINER_FIND(>alk_list, name); 00266 if (!gtalk && strchr(name, '@')) 00267 gtalk = ASTOBJ_CONTAINER_FIND_FULL(>alk_list, name, user,,, strcasecmp); 00268 00269 if (!gtalk) { /* guest call */ 00270 ASTOBJ_CONTAINER_TRAVERSE(>alk_list, 1, { 00271 ASTOBJ_WRLOCK(iterator); 00272 if (!strcasecmp(iterator->name, "guest")) { 00273 if (!strcasecmp(iterator->connection->jid->partial, connection)) { 00274 gtalk = iterator; 00275 break; 00276 } else if (!strcasecmp(iterator->connection->name, connection)) { 00277 gtalk = iterator; 00278 break; 00279 } else if (iterator->connection->component && !strcasecmp(iterator->connection->user,domain)) { 00280 gtalk = iterator; 00281 break; 00282 } 00283 } 00284 ASTOBJ_UNLOCK(iterator); 00285 }); 00286 00287 } 00288 return gtalk; 00289 } 00290 00291 00292 static int add_codec_to_answer(const struct gtalk_pvt *p, int codec, iks *dcodecs) 00293 { 00294 char *format = ast_getformatname(codec); 00295 00296 if (!strcasecmp("ulaw", format)) { 00297 iks *payload_eg711u, *payload_pcmu; 00298 payload_pcmu = iks_new("payload-type"); 00299 payload_eg711u = iks_new("payload-type"); 00300 00301 if(!payload_eg711u || !payload_pcmu) { 00302 if(payload_pcmu) 00303 iks_delete(payload_pcmu); 00304 if(payload_eg711u) 00305 iks_delete(payload_eg711u); 00306 ast_log(LOG_WARNING,"Failed to allocate iks node"); 00307 return -1; 00308 } 00309 iks_insert_attrib(payload_pcmu, "id", "0"); 00310 iks_insert_attrib(payload_pcmu, "name", "PCMU"); 00311 iks_insert_attrib(payload_pcmu, "clockrate","8000"); 00312 iks_insert_attrib(payload_pcmu, "bitrate","64000"); 00313 iks_insert_attrib(payload_eg711u, "id", "100"); 00314 iks_insert_attrib(payload_eg711u, "name", "EG711U"); 00315 iks_insert_attrib(payload_eg711u, "clockrate","8000"); 00316 iks_insert_attrib(payload_eg711u, "bitrate","64000"); 00317 iks_insert_node(dcodecs, payload_pcmu); 00318 iks_insert_node(dcodecs, payload_eg711u); 00319 } 00320 if (!strcasecmp("alaw", format)) { 00321 iks *payload_eg711a, *payload_pcma; 00322 payload_pcma = iks_new("payload-type"); 00323 payload_eg711a = iks_new("payload-type"); 00324 if(!payload_eg711a || !payload_pcma) { 00325 if(payload_eg711a) 00326 iks_delete(payload_eg711a); 00327 if(payload_pcma) 00328 iks_delete(payload_pcma); 00329 ast_log(LOG_WARNING,"Failed to allocate iks node"); 00330 return -1; 00331 } 00332 iks_insert_attrib(payload_pcma, "id", "8"); 00333 iks_insert_attrib(payload_pcma, "name", "PCMA"); 00334 iks_insert_attrib(payload_pcma, "clockrate","8000"); 00335 iks_insert_attrib(payload_pcma, "bitrate","64000"); 00336 payload_eg711a = iks_new("payload-type"); 00337 iks_insert_attrib(payload_eg711a, "id", "101"); 00338 iks_insert_attrib(payload_eg711a, "name", "EG711A"); 00339 iks_insert_attrib(payload_eg711a, "clockrate","8000"); 00340 iks_insert_attrib(payload_eg711a, "bitrate","64000"); 00341 iks_insert_node(dcodecs, payload_pcma); 00342 iks_insert_node(dcodecs, payload_eg711a); 00343 } 00344 if (!strcasecmp("ilbc", format)) { 00345 iks *payload_ilbc = iks_new("payload-type"); 00346 if(!payload_ilbc) { 00347 ast_log(LOG_WARNING,"Failed to allocate iks node"); 00348 return -1; 00349 } 00350 iks_insert_attrib(payload_ilbc, "id", "102"); 00351 iks_insert_attrib(payload_ilbc, "name", "iLBC"); 00352 iks_insert_attrib(payload_ilbc, "clockrate","8000"); 00353 iks_insert_attrib(payload_ilbc, "bitrate","13300"); 00354 iks_insert_node(dcodecs, payload_ilbc); 00355 } 00356 if (!strcasecmp("g723", format)) { 00357 iks *payload_g723 = iks_new("payload-type"); 00358 if(!payload_g723) { 00359 ast_log(LOG_WARNING,"Failed to allocate iks node"); 00360 return -1; 00361 } 00362 iks_insert_attrib(payload_g723, "id", "4"); 00363 iks_insert_attrib(payload_g723, "name", "G723"); 00364 iks_insert_attrib(payload_g723, "clockrate","8000"); 00365 iks_insert_attrib(payload_g723, "bitrate","6300"); 00366 iks_insert_node(dcodecs, payload_g723); 00367 } 00368 if (!strcasecmp("speex", format)) { 00369 iks *payload_speex = iks_new("payload-type"); 00370 if(!payload_speex) { 00371 ast_log(LOG_WARNING,"Failed to allocate iks node"); 00372 return -1; 00373 } 00374 iks_insert_attrib(payload_speex, "id", "98"); 00375 iks_insert_attrib(payload_speex, "name", "speex"); 00376 iks_insert_attrib(payload_speex, "clockrate","8000"); 00377 iks_insert_attrib(payload_speex, "bitrate","11000"); 00378 iks_insert_node(dcodecs, payload_speex); 00379 } 00380 ast_rtp_lookup_code(p->rtp, 1, codec); 00381 return 0; 00382 } 00383 00384 static int gtalk_invite(struct gtalk_pvt *p, char *to, char *from, char *sid, int initiator) 00385 { 00386 struct gtalk *client = p->parent; 00387 iks *iq, *gtalk, *dcodecs, *payload_telephone, *transport; 00388 int x; 00389 int pref_codec = 0; 00390 int alreadysent = 0; 00391 00392 00393 iq = iks_new("iq"); 00394 gtalk = iks_new("session"); 00395 dcodecs = iks_new("description"); 00396 transport = iks_new("transport"); 00397 payload_telephone = iks_new("payload-type"); 00398 if (!(iq && gtalk && dcodecs && transport && payload_telephone)){ 00399 if(iq) 00400 iks_delete(iq); 00401 if(gtalk) 00402 iks_delete(gtalk); 00403 if(dcodecs) 00404 iks_delete(dcodecs); 00405 if(transport) 00406 iks_delete(transport); 00407 if(payload_telephone) 00408 iks_delete(payload_telephone); 00409 00410 ast_log(LOG_ERROR, "Could not allocate iksemel nodes\n"); 00411 return 0; 00412 } 00413 iks_insert_attrib(dcodecs, "xmlns", "http://www.google.com/session/phone"); 00414 iks_insert_attrib(dcodecs, "xml:lang", "en"); 00415 00416 for (x = 0; x < 32; x++) { 00417 if (!(pref_codec = ast_codec_pref_index(&client->prefs, x))) 00418 break; 00419 if (!(client->capability & pref_codec)) 00420 continue; 00421 if (alreadysent & pref_codec) 00422 continue; 00423 add_codec_to_answer(p, pref_codec, dcodecs); 00424 alreadysent |= pref_codec; 00425 } 00426 00427 iks_insert_attrib(payload_telephone, "id", "106"); 00428 iks_insert_attrib(payload_telephone, "name", "telephone-event"); 00429 iks_insert_attrib(payload_telephone, "clockrate", "8000"); 00430 00431 iks_insert_attrib(transport,"xmlns","http://www.google.com/transport/p2p"); 00432 00433 iks_insert_attrib(iq, "type", "set"); 00434 iks_insert_attrib(iq, "to", to); 00435 iks_insert_attrib(iq, "from", from); 00436 iks_insert_attrib(iq, "id", client->connection->mid); 00437 ast_aji_increment_mid(client->connection->mid); 00438 00439 iks_insert_attrib(gtalk, "xmlns", "http://www.google.com/session"); 00440 iks_insert_attrib(gtalk, "type",initiator ? "initiate": "accept"); 00441 iks_insert_attrib(gtalk, "initiator", initiator ? from : to); 00442 iks_insert_attrib(gtalk, "id", sid); 00443 iks_insert_node(iq, gtalk); 00444 iks_insert_node(gtalk, dcodecs); 00445 iks_insert_node(gtalk, transport); 00446 iks_insert_node(dcodecs, payload_telephone); 00447 00448 iks_send(client->connection->p, iq); 00449 iks_delete(payload_telephone); 00450 iks_delete(transport); 00451 iks_delete(dcodecs); 00452 iks_delete(gtalk); 00453 iks_delete(iq); 00454 return 1; 00455 } 00456 00457 static int gtalk_invite_response(struct gtalk_pvt *p, char *to , char *from, char *sid, int initiator) 00458 { 00459 iks *iq, *session, *transport; 00460 iq = iks_new("iq"); 00461 session = iks_new("session"); 00462 transport = iks_new("transport"); 00463 if(!(iq && session && transport)) { 00464 if(iq) 00465 iks_delete(iq); 00466 if(session) 00467 iks_delete(session); 00468 if(transport) 00469 iks_delete(transport); 00470 ast_log(LOG_ERROR, " Unable to allocate IKS node\n"); 00471 return -1; 00472 } 00473 iks_insert_attrib(iq, "from", from); 00474 iks_insert_attrib(iq, "to", to); 00475 iks_insert_attrib(iq, "type", "set"); 00476 iks_insert_attrib(iq, "id",p->parent->connection->mid); 00477 ast_aji_increment_mid(p->parent->connection->mid); 00478 iks_insert_attrib(session, "type", "transport-accept"); 00479 iks_insert_attrib(session, "id", sid); 00480 iks_insert_attrib(session, "initiator", initiator ? from : to); 00481 iks_insert_attrib(session, "xmlns", "http://www.google.com/session"); 00482 iks_insert_attrib(transport, "xmlns", "http://www.google.com/transport/p2p"); 00483 iks_insert_node(iq,session); 00484 iks_insert_node(session,transport); 00485 iks_send(p->parent->connection->p, iq); 00486 iks_delete(transport); 00487 iks_delete(session); 00488 iks_delete(iq); 00489 return 1; 00490 00491 } 00492 00493 static int gtalk_ringing_ack(void *data, ikspak *pak) 00494 { 00495 struct gtalk_pvt *p = data; 00496 00497 if (p->ringrule) 00498 iks_filter_remove_rule(p->parent->connection->f, p->ringrule); 00499 p->ringrule = NULL; 00500 if (p->owner) 00501 ast_queue_control(p->owner, AST_CONTROL_RINGING); 00502 return IKS_FILTER_EAT; 00503 } 00504 00505 static int gtalk_answer(struct ast_channel *ast) 00506 { 00507 struct gtalk_pvt *p = ast->tech_pvt; 00508 int res = 0; 00509 00510 if (option_debug) 00511 ast_log(LOG_DEBUG, "Answer!\n"); 00512 ast_mutex_lock(&p->lock); 00513 gtalk_invite(p, p->them, p->us,p->sid, 0); 00514 ast_mutex_unlock(&p->lock); 00515 return res; 00516 } 00517 00518 static enum ast_rtp_get_result gtalk_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp) 00519 { 00520 struct gtalk_pvt *p = chan->tech_pvt; 00521 enum ast_rtp_get_result res = AST_RTP_GET_FAILED; 00522 00523 if (!p) 00524 return res; 00525 00526 ast_mutex_lock(&p->lock); 00527 if (p->rtp){ 00528 *rtp = p->rtp; 00529 res = AST_RTP_TRY_NATIVE; 00530 } 00531 ast_mutex_unlock(&p->lock); 00532 00533 return res; 00534 } 00535 00536 static int gtalk_get_codec(struct ast_channel *chan) 00537 { 00538 struct gtalk_pvt *p = chan->tech_pvt; 00539 return p->peercapability; 00540 } 00541 00542 static int gtalk_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active) 00543 { 00544 struct gtalk_pvt *p; 00545 00546 p = chan->tech_pvt; 00547 if (!p) 00548 return -1; 00549 ast_mutex_lock(&p->lock); 00550 00551 /* if (rtp) 00552 ast_rtp_get_peer(rtp, &p->redirip); 00553 else 00554 memset(&p->redirip, 0, sizeof(p->redirip)); 00555 p->redircodecs = codecs; */ 00556 00557 /* Reset lastrtprx timer */ 00558 ast_mutex_unlock(&p->lock); 00559 return 0; 00560 } 00561 00562 static int gtalk_response(struct gtalk *client, char *from, ikspak *pak, const char *reasonstr, const char *reasonstr2) 00563 { 00564 iks *response = NULL, *error = NULL, *reason = NULL; 00565 int res = -1; 00566 00567 response = iks_new("iq"); 00568 if (response) { 00569 iks_insert_attrib(response, "type", "result"); 00570 iks_insert_attrib(response, "from", from); 00571 iks_insert_attrib(response, "to", iks_find_attrib(pak->x, "from")); 00572 iks_insert_attrib(response, "id", iks_find_attrib(pak->x, "id")); 00573 if (reasonstr) { 00574 error = iks_new("error"); 00575 if (error) { 00576 iks_insert_attrib(error, "type", "cancel"); 00577 reason = iks_new(reasonstr); 00578 if (reason) 00579 iks_insert_node(error, reason); 00580 iks_insert_node(response, error); 00581 } 00582 } 00583 iks_send(client->connection->p, response); 00584 if (reason) 00585 iks_delete(reason); 00586 if (error) 00587 iks_delete(error); 00588 iks_delete(response); 00589 res = 0; 00590 } 00591 return res; 00592 } 00593 00594 static int gtalk_is_answered(struct gtalk *client, ikspak *pak) 00595 { 00596 struct gtalk_pvt *tmp; 00597 char *from; 00598 if (option_debug) 00599 ast_log(LOG_DEBUG, "The client is %s\n", client->name); 00600 /* Make sure our new call doesn't exist yet */ 00601 for (tmp = client->p; tmp; tmp = tmp->next) { 00602 if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) 00603 break; 00604 } 00605 00606 from = iks_find_attrib(pak->x, "to"); 00607 if(!from) 00608 from = client->connection->jid->full; 00609 00610 if (tmp) { 00611 if (tmp->owner) 00612 ast_queue_control(tmp->owner, AST_CONTROL_ANSWER); 00613 } else 00614 ast_log(LOG_NOTICE, "Whoa, didn't find call!\n"); 00615 gtalk_response(client, from, pak, NULL, NULL); 00616 return 1; 00617 } 00618 00619 static int gtalk_handle_dtmf(struct gtalk *client, ikspak *pak) 00620 { 00621 struct gtalk_pvt *tmp; 00622 iks *dtmfnode = NULL; 00623 char *dtmf; 00624 char *from; 00625 /* Make sure our new call doesn't exist yet */ 00626 for (tmp = client->p; tmp; tmp = tmp->next) { 00627 if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) 00628 break; 00629 } 00630 from = iks_find_attrib(pak->x, "to"); 00631 if(!from) 00632 from = client->connection->jid->full; 00633 00634 00635 if (tmp) { 00636 if(iks_find_with_attrib(pak->x, "dtmf-method", "method", "rtp")) { 00637 gtalk_response(client, from, pak, 00638 "feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'", 00639 "unsupported-dtmf-method xmlns='http://jabber.org/protocol/gtalk/info/dtmf#errors'"); 00640 return -1; 00641 } 00642 if ((dtmfnode = iks_find(pak->x, "dtmf"))) { 00643 if((dtmf = iks_find_attrib(dtmfnode, "code"))) { 00644 if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-up")) { 00645 struct ast_frame f = {AST_FRAME_DTMF_BEGIN, }; 00646 f.subclass = dtmf[0]; 00647 ast_queue_frame(tmp->owner, &f); 00648 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass); 00649 } else if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-down")) { 00650 struct ast_frame f = {AST_FRAME_DTMF_END, }; 00651 f.subclass = dtmf[0]; 00652 ast_queue_frame(tmp->owner, &f); 00653 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass); 00654 } else if(iks_find_attrib(pak->x, "dtmf")) { /* 250 millasecond default */ 00655 struct ast_frame f = {AST_FRAME_DTMF, }; 00656 f.subclass = dtmf[0]; 00657 ast_queue_frame(tmp->owner, &f); 00658 ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass); 00659 } 00660 } 00661 } 00662 gtalk_response(client, from, pak, NULL, NULL); 00663 return 1; 00664 } else 00665 ast_log(LOG_NOTICE, "Whoa, didn't find call!\n"); 00666 00667 gtalk_response(client, from, pak, NULL, NULL); 00668 return 1; 00669 } 00670 00671 00672 static int gtalk_hangup_farend(struct gtalk *client, ikspak *pak) 00673 { 00674 struct gtalk_pvt *tmp; 00675 char *from; 00676 00677 if (option_debug) 00678 ast_log(LOG_DEBUG, "The client is %s\n", client->name); 00679 /* Make sure our new call doesn't exist yet */ 00680 for (tmp = client->p; tmp; tmp = tmp->next) { 00681 if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) 00682 break; 00683 } 00684 from = iks_find_attrib(pak->x, "to"); 00685 if(!from) 00686 from = client->connection->jid->full; 00687 00688 00689 if (tmp) { 00690 tmp->alreadygone = 1; 00691 ast_queue_hangup(tmp->owner); 00692 } else 00693 ast_log(LOG_NOTICE, "Whoa, didn't find call!\n"); 00694 gtalk_response(client, from, pak, NULL, NULL); 00695 return 1; 00696 } 00697 00698 static int gtalk_create_candidates(struct gtalk *client, struct gtalk_pvt *p, char *sid, char *from, char *to) 00699 { 00700 struct gtalk_candidate *tmp; 00701 struct aji_client *c = client->connection; 00702 struct gtalk_candidate *ours1 = NULL, *ours2 = NULL; 00703 struct sockaddr_in sin; 00704 struct sockaddr_in dest; 00705 struct in_addr us; 00706 iks *iq, *gtalk, *candidate, *transport; 00707 char user[17], pass[17], preference[5], port[7]; 00708 00709 00710 iq = iks_new("iq"); 00711 gtalk = iks_new("session"); 00712 candidate = iks_new("candidate"); 00713 transport = iks_new("transport"); 00714 if (!iq || !gtalk || !candidate || !transport) { 00715 ast_log(LOG_ERROR, "Memory allocation error\n"); 00716 goto safeout; 00717 } 00718 ours1 = ast_calloc(1, sizeof(*ours1)); 00719 ours2 = ast_calloc(1, sizeof(*ours2)); 00720 if (!ours1 || !ours2) 00721 goto safeout; 00722 00723 iks_insert_attrib(transport, "xmlns","http://www.google.com/transport/p2p"); 00724 iks_insert_node(iq, gtalk); 00725 iks_insert_node(gtalk,transport); 00726 iks_insert_node(transport, candidate); 00727 00728 for (; p; p = p->next) { 00729 if (!strcasecmp(p->sid, sid)) 00730 break; 00731 } 00732 00733 if (!p) { 00734 ast_log(LOG_NOTICE, "No matching gtalk session - SID %s!\n", sid); 00735 goto safeout; 00736 } 00737 00738 ast_rtp_get_us(p->rtp, &sin); 00739 ast_find_ourip(&us, bindaddr); 00740 00741 /* Setup our gtalk candidates */ 00742 ast_copy_string(ours1->name, "rtp", sizeof(ours1->name)); 00743 ours1->port = ntohs(sin.sin_port); 00744 ours1->preference = 1; 00745 snprintf(user, sizeof(user), "%08lx%08lx", ast_random(), ast_random()); 00746 snprintf(pass, sizeof(pass), "%08lx%08lx", ast_random(), ast_random()); 00747 ast_copy_string(ours1->username, user, sizeof(ours1->username)); 00748 ast_copy_string(ours1->password, pass, sizeof(ours1->password)); 00749 ast_copy_string(ours1->ip, ast_inet_ntoa(us), sizeof(ours1->ip)); 00750 ours1->protocol = AJI_PROTOCOL_UDP; 00751 ours1->type = AJI_CONNECT_LOCAL; 00752 ours1->generation = 0; 00753 p->ourcandidates = ours1; 00754 00755 if (!ast_strlen_zero(externip)) { 00756 /* XXX We should really stun for this one not just go with externip XXX */ 00757 snprintf(user, sizeof(user), "%08lx%08lx", ast_random(), ast_random()); 00758 snprintf(pass, sizeof(pass), "%08lx%08lx", ast_random(), ast_random()); 00759 ast_copy_string(ours2->username, user, sizeof(ours2->username)); 00760 ast_copy_string(ours2->password, pass, sizeof(ours2->password)); 00761 ast_copy_string(ours2->ip, externip, sizeof(ours2->ip)); 00762 ast_copy_string(ours2->name, "rtp", sizeof(ours1->name)); 00763 ours2->port = ntohs(sin.sin_port); 00764 ours2->preference = 0.9; 00765 ours2->protocol = AJI_PROTOCOL_UDP; 00766 ours2->type = AJI_CONNECT_STUN; 00767 ours2->generation = 0; 00768 ours1->next = ours2; 00769 ours2 = NULL; 00770 } 00771 ours1 = NULL; 00772 dest.sin_addr = __ourip; 00773 dest.sin_port = sin.sin_port; 00774 00775 00776 for (tmp = p->ourcandidates; tmp; tmp = tmp->next) { 00777 snprintf(port, sizeof(port), "%d", tmp->port); 00778 snprintf(preference, sizeof(preference), "%.2f", tmp->preference); 00779 iks_insert_attrib(iq, "from", to); 00780 iks_insert_attrib(iq, "to", from); 00781 iks_insert_attrib(iq, "type", "set"); 00782 iks_insert_attrib(iq, "id", c->mid); 00783 ast_aji_increment_mid(c->mid); 00784 iks_insert_attrib(gtalk, "type", "transport-info"); 00785 iks_insert_attrib(gtalk, "id", sid); 00786 iks_insert_attrib(gtalk, "initiator", (p->initiator) ? to : from); 00787 iks_insert_attrib(gtalk, "xmlns", GOOGLE_NS); 00788 iks_insert_attrib(candidate, "name", tmp->name); 00789 iks_insert_attrib(candidate, "address", tmp->ip); 00790 iks_insert_attrib(candidate, "port", port); 00791 iks_insert_attrib(candidate, "username", tmp->username); 00792 iks_insert_attrib(candidate, "password", tmp->password); 00793 iks_insert_attrib(candidate, "preference", preference); 00794 if (tmp->protocol == AJI_PROTOCOL_UDP) 00795 iks_insert_attrib(candidate, "protocol", "udp"); 00796 if (tmp->protocol == AJI_PROTOCOL_SSLTCP) 00797 iks_insert_attrib(candidate, "protocol", "ssltcp"); 00798 if (tmp->type == AJI_CONNECT_STUN) 00799 iks_insert_attrib(candidate, "type", "stun"); 00800 if (tmp->type == AJI_CONNECT_LOCAL) 00801 iks_insert_attrib(candidate, "type", "local"); 00802 if (tmp->type == AJI_CONNECT_RELAY) 00803 iks_insert_attrib(candidate, "type", "relay"); 00804 iks_insert_attrib(candidate, "network", "0"); 00805 iks_insert_attrib(candidate, "generation", "0"); 00806 iks_send(c->p, iq); 00807 } 00808 p->laststun = 0; 00809 00810 safeout: 00811 if (ours1) 00812 free(ours1); 00813 if (ours2) 00814 free(ours2); 00815 if (iq) 00816 iks_delete(iq); 00817 if (gtalk) 00818 iks_delete(gtalk); 00819 if (candidate) 00820 iks_delete(candidate); 00821 if(transport) 00822 iks_delete(transport); 00823 return 1; 00824 } 00825 00826 static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const char *them, const char *sid) 00827 { 00828 struct gtalk_pvt *tmp = NULL; 00829 struct aji_resource *resources = NULL; 00830 struct aji_buddy *buddy; 00831 char idroster[200]; 00832 char *data, *exten = NULL; 00833 00834 if (option_debug) 00835 ast_log(LOG_DEBUG, "The client is %s for alloc\n", client->name); 00836 if (!sid && !strchr(them, '/')) { /* I started call! */ 00837 if (!strcasecmp(client->name, "guest")) { 00838 buddy = ASTOBJ_CONTAINER_FIND(&client->connection->buddies, them); 00839 if (buddy) 00840 resources = buddy->resources; 00841 } else 00842 resources = client->buddy->resources; 00843 while (resources) { 00844 if (resources->cap->jingle) { 00845 break; 00846 } 00847 resources = resources->next; 00848 } 00849 if (resources) 00850 snprintf(idroster, sizeof(idroster), "%s/%s", them, resources->resource); 00851 else { 00852 ast_log(LOG_ERROR, "no gtalk capable clients to talk to.\n"); 00853 return NULL; 00854 } 00855 } 00856 if (!(tmp = ast_calloc(1, sizeof(*tmp)))) { 00857 return NULL; 00858 } 00859 if (sid) { 00860 ast_copy_string(tmp->sid, sid, sizeof(tmp->sid)); 00861 ast_copy_string(tmp->them, them, sizeof(tmp->them)); 00862 ast_copy_string(tmp->us, us, sizeof(tmp->us)); 00863 } else { 00864 snprintf(tmp->sid, sizeof(tmp->sid), "%08lx%08lx", ast_random(), ast_random()); 00865 ast_copy_string(tmp->them, idroster, sizeof(tmp->them)); 00866 ast_copy_string(tmp->us, us, sizeof(tmp->us)); 00867 tmp->initiator = 1; 00868 } 00869 tmp->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr); 00870 tmp->parent = client; 00871 if (!tmp->rtp) { 00872 ast_log(LOG_WARNING, "Out of RTP sessions?\n"); 00873 free(tmp); 00874 return NULL; 00875 } 00876 00877 if(strchr(tmp->us, '/')) { 00878 data = ast_strdupa((char *) tmp->us); 00879 exten = strsep(&data, "/"); 00880 } else 00881 exten = tmp->us; 00882 ast_copy_string(tmp->exten, exten, sizeof(tmp->exten)); 00883 if(data) 00884 free(data); 00885 ast_mutex_init(&tmp->lock); 00886 ast_mutex_lock(>alklock); 00887 tmp->next = client->p; 00888 client->p = tmp; 00889 ast_mutex_unlock(>alklock); 00890 return tmp; 00891 } 00892 00893 /*! \brief Start new gtalk channel */ 00894 static struct ast_channel *gtalk_new(struct gtalk *client, struct gtalk_pvt *i, int state, const char *title) 00895 { 00896 struct ast_channel *tmp; 00897 int fmt; 00898 int what; 00899 const char *n2; 00900 00901 if (title) 00902 n2 = title; 00903 else 00904 n2 = i->us; 00905 tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, "Gtalk/%s-%04lx", n2, ast_random() & 0xffff); 00906 if (!tmp) { 00907 ast_log(LOG_WARNING, "Unable to allocate Gtalk channel structure!\n"); 00908 return NULL; 00909 } 00910 tmp->tech = >alk_tech; 00911 00912 /* Select our native format based on codec preference until we receive 00913 something from another device to the contrary. */ 00914 /* ast_verbose("XXXXXXXXXXXXX\nXXX i->jointcapability = %X\nXXX i->capability = %X\nXXX global_capability %X\n XXXXXXXXXXXX\n",i->jointcapability,i->capability,global_capability); */ 00915 if (i->jointcapability) 00916 what = i->jointcapability; 00917 else if (i->capability) 00918 what = i->capability; 00919 else 00920 what = global_capability; 00921 tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | (i->jointcapability & AST_FORMAT_VIDEO_MASK); 00922 fmt = ast_best_codec(tmp->nativeformats); 00923 00924 if (i->rtp) { 00925 ast_rtp_setstun(i->rtp, 1); 00926 tmp->fds[0] = ast_rtp_fd(i->rtp); 00927 tmp->fds[1] = ast_rtcp_fd(i->rtp); 00928 } 00929 if (i->vrtp) { 00930 ast_rtp_setstun(i->rtp, 1); 00931 tmp->fds[2] = ast_rtp_fd(i->vrtp); 00932 tmp->fds[3] = ast_rtcp_fd(i->vrtp); 00933 } 00934 if (state == AST_STATE_RING) 00935 tmp->rings = 1; 00936 tmp->adsicpe = AST_ADSI_UNAVAILABLE; 00937 tmp->writeformat = fmt; 00938 tmp->rawwriteformat = fmt; 00939 tmp->readformat = fmt; 00940 tmp->rawreadformat = fmt; 00941 tmp->tech_pvt = i; 00942 00943 tmp->callgroup = client->callgroup; 00944 tmp->pickupgroup = client->pickupgroup; 00945 tmp->cid.cid_pres = client->callingpres; 00946 if (!ast_strlen_zero(client->accountcode)) 00947 ast_string_field_set(tmp, accountcode, client->accountcode); 00948 if (client->amaflags) 00949 tmp->amaflags = client->amaflags; 00950 if (!ast_strlen_zero(client->language)) 00951 ast_string_field_set(tmp, language, client->language); 00952 if (!ast_strlen_zero(client->musicclass)) 00953 ast_string_field_set(tmp, musicclass, client->musicclass); 00954 i->owner = tmp; 00955 ast_module_ref(ast_module_info->self); 00956 ast_copy_string(tmp->context, client->context, sizeof(tmp->context)); 00957 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten)); 00958 /* Don't use ast_set_callerid() here because it will 00959 * generate a needless NewCallerID event */ 00960 tmp->cid.cid_num = ast_strdup(i->cid_num); 00961 tmp->cid.cid_ani = ast_strdup(i->cid_num); 00962 tmp->cid.cid_name = ast_strdup(i->cid_name); 00963 if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s")) 00964 tmp->cid.cid_dnid = ast_strdup(i->exten); 00965 tmp->priority = 1; 00966 if (i->rtp) 00967 ast_jb_configure(tmp, &global_jbconf); 00968 if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) { 00969 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name); 00970 tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION; 00971 ast_hangup(tmp); 00972 tmp = NULL; 00973 } 00974 00975 return tmp; 00976 } 00977 00978 static int gtalk_action(struct gtalk *client, struct gtalk_pvt *p, const char *action) 00979 { 00980 iks *request, *session = NULL; 00981 int res = -1; 00982 00983 request = iks_new("iq"); 00984 if (request) { 00985 iks_insert_attrib(request, "type", "set"); 00986 iks_insert_attrib(request, "from", p->us); 00987 iks_insert_attrib(request, "to", p->them); 00988 iks_insert_attrib(request, "id", client->connection->mid); 00989 ast_aji_increment_mid(client->connection->mid); 00990 session = iks_new("session"); 00991 if (session) { 00992 iks_insert_attrib(session, "type", action); 00993 iks_insert_attrib(session, "id", p->sid); 00994 iks_insert_attrib(session, "initiator", p->initiator ? p->us : p->them); 00995 iks_insert_attrib(session, "xmlns", "http://www.google.com/session"); 00996 iks_insert_node(request, session); 00997 iks_send(client->connection->p, request); 00998 iks_delete(session); 00999 res = 0; 01000 } 01001 iks_delete(request); 01002 } 01003 return res; 01004 } 01005 01006 static void gtalk_free_candidates(struct gtalk_candidate *candidate) 01007 { 01008 struct gtalk_candidate *last; 01009 while (candidate) { 01010 last = candidate; 01011 candidate = candidate->next; 01012 free(last); 01013 } 01014 } 01015 01016 static void gtalk_free_pvt(struct gtalk *client, struct gtalk_pvt *p) 01017 { 01018 struct gtalk_pvt *cur, *prev = NULL; 01019 cur = client->p; 01020 while (cur) { 01021 if (cur == p) { 01022 if (prev) 01023 prev->next = p->next; 01024 else 01025 client->p = p->next; 01026 break; 01027 } 01028 prev = cur; 01029 cur = cur->next; 01030 } 01031 if (p->ringrule) 01032 iks_filter_remove_rule(p->parent->connection->f, p->ringrule); 01033 if (p->owner) 01034 ast_log(LOG_WARNING, "Uh oh, there's an owner, this is going to be messy.\n"); 01035 if (p->rtp) 01036 ast_rtp_destroy(p->rtp); 01037 if (p->vrtp) 01038 ast_rtp_destroy(p->vrtp); 01039 gtalk_free_candidates(p->theircandidates); 01040 free(p); 01041 } 01042 01043 01044 static int gtalk_newcall(struct gtalk *client, ikspak *pak) 01045 { 01046 struct gtalk_pvt *p, *tmp = client->p; 01047 struct ast_channel *chan; 01048 int res; 01049 iks *codec; 01050 char *from = NULL; 01051 /* Make sure our new call doesn't exist yet */ 01052 from = iks_find_attrib(pak->x,"to"); 01053 if(!from) 01054 from = client->connection->jid->full; 01055 01056 while (tmp) { 01057 if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) { 01058 ast_log(LOG_NOTICE, "Ignoring duplicate call setup on SID %s\n", tmp->sid); 01059 gtalk_response(client, from, pak, "out-of-order", NULL); 01060 return -1; 01061 } 01062 tmp = tmp->next; 01063 } 01064 01065 p = gtalk_alloc(client, from, pak->from->full, iks_find_attrib(pak->query, "id")); 01066 if (!p) { 01067 ast_log(LOG_WARNING, "Unable to allocate gtalk structure!\n"); 01068 return -1; 01069 } 01070 chan = gtalk_new(client, p, AST_STATE_DOWN, pak->from->user); 01071 if (chan) { 01072 ast_mutex_lock(&p->lock); 01073 ast_copy_string(p->them, pak->from->full, sizeof(p->them)); 01074 if (iks_find_attrib(pak->query, "id")) { 01075 ast_copy_string(p->sid, iks_find_attrib(pak->query, "id"), 01076 sizeof(p->sid)); 01077 } 01078 01079 codec = iks_child(iks_child(iks_child(pak->x))); 01080 while (codec) { 01081 ast_rtp_set_m_type(p->rtp, atoi(iks_find_attrib(codec, "id"))); 01082 ast_rtp_set_rtpmap_type(p->rtp, atoi(iks_find_attrib(codec, "id")), "audio", 01083 iks_find_attrib(codec, "name"), 0); 01084 codec = iks_next(codec); 01085 } 01086 01087 ast_mutex_unlock(&p->lock); 01088 ast_setstate(chan, AST_STATE_RING); 01089 res = ast_pbx_start(chan); 01090 01091 switch (res) { 01092 case AST_PBX_FAILED: 01093 ast_log(LOG_WARNING, "Failed to start PBX :(\n"); 01094 gtalk_response(client, from, pak, "service-unavailable", NULL); 01095 break; 01096 case AST_PBX_CALL_LIMIT: 01097 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n"); 01098 gtalk_response(client, from, pak, "service-unavailable", NULL); 01099 break; 01100 case AST_PBX_SUCCESS: 01101 gtalk_response(client, from, pak, NULL, NULL); 01102 gtalk_invite_response(p, p->them, p->us,p->sid, 0); 01103 gtalk_create_candidates(client, p, p->sid, p->them, p->us); 01104 /* nothing to do */ 01105 break; 01106 } 01107 } else { 01108 gtalk_free_pvt(client, p); 01109 } 01110 return 1; 01111 } 01112 01113 static int gtalk_update_stun(struct gtalk *client, struct gtalk_pvt *p) 01114 { 01115 struct gtalk_candidate *tmp; 01116 struct hostent *hp; 01117 struct ast_hostent ahp; 01118 struct sockaddr_in sin; 01119 01120 if (time(NULL) == p->laststun) 01121 return 0; 01122 01123 tmp = p->theircandidates; 01124 p->laststun = time(NULL); 01125 while (tmp) { 01126 char username[256]; 01127 hp = ast_gethostbyname(tmp->ip, &ahp); 01128 sin.sin_family = AF_INET; 01129 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr)); 01130 sin.sin_port = htons(tmp->port); 01131 snprintf(username, sizeof(username), "%s%s", tmp->username, 01132 p->ourcandidates->username); 01133 01134 ast_rtp_stun_request(p->rtp, &sin, username); 01135 tmp = tmp->next; 01136 } 01137 return 1; 01138 } 01139 01140 static int gtalk_add_candidate(struct gtalk *client, ikspak *pak) 01141 { 01142 struct gtalk_pvt *p = NULL, *tmp = NULL; 01143 struct aji_client *c = client->connection; 01144 struct gtalk_candidate *newcandidate = NULL; 01145 iks *traversenodes = NULL, *receipt = NULL; 01146 char *from; 01147 01148 from = iks_find_attrib(pak->x,"to"); 01149 if(!from) 01150 from = c->jid->full; 01151 01152 newcandidate = ast_calloc(1, sizeof(*newcandidate)); 01153 if (!newcandidate) 01154 return 0; 01155 for (tmp = client->p; tmp; tmp = tmp->next) { 01156 if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) { 01157 p = tmp; 01158 break; 01159 } 01160 } 01161 01162 if (!p) 01163 return -1; 01164 01165 traversenodes = pak->query; 01166 while(traversenodes) { 01167 if(!strcasecmp(iks_name(traversenodes), "session")) { 01168 traversenodes = iks_child(traversenodes); 01169 continue; 01170 } 01171 if(!strcasecmp(iks_name(traversenodes), "transport")) { 01172 traversenodes = iks_child(traversenodes); 01173 continue; 01174 } 01175 if(!strcasecmp(iks_name(traversenodes), "candidate")) { 01176 newcandidate = ast_calloc(1, sizeof(*newcandidate)); 01177 if (!newcandidate) 01178 return 0; 01179 ast_copy_string(newcandidate->name, iks_find_attrib(traversenodes, "name"), 01180 sizeof(newcandidate->name)); 01181 ast_copy_string(newcandidate->ip, iks_find_attrib(traversenodes, "address"), 01182 sizeof(newcandidate->ip)); 01183 newcandidate->port = atoi(iks_find_attrib(traversenodes, "port")); 01184 ast_copy_string(newcandidate->username, iks_find_attrib(traversenodes, "username"), 01185 sizeof(newcandidate->username)); 01186 ast_copy_string(newcandidate->password, iks_find_attrib(traversenodes, "password"), 01187 sizeof(newcandidate->password)); 01188 newcandidate->preference = atof(iks_find_attrib(traversenodes, "preference")); 01189 if (!strcasecmp(iks_find_attrib(traversenodes, "protocol"), "udp")) 01190 newcandidate->protocol = AJI_PROTOCOL_UDP; 01191 if (!strcasecmp(iks_find_attrib(traversenodes, "protocol"), "ssltcp")) 01192 newcandidate->protocol = AJI_PROTOCOL_SSLTCP; 01193 01194 if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "stun")) 01195 newcandidate->type = AJI_CONNECT_STUN; 01196 if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "local")) 01197 newcandidate->type = AJI_CONNECT_LOCAL; 01198 if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "relay")) 01199 newcandidate->type = AJI_CONNECT_RELAY; 01200 ast_copy_string(newcandidate->network, iks_find_attrib(traversenodes, "network"), 01201 sizeof(newcandidate->network)); 01202 newcandidate->generation = atoi(iks_find_attrib(traversenodes, "generation")); 01203 newcandidate->next = NULL; 01204 01205 newcandidate->next = p->theircandidates; 01206 p->theircandidates = newcandidate; 01207 p->laststun = 0; 01208 gtalk_update_stun(p->parent, p); 01209 newcandidate = NULL; 01210 } 01211 traversenodes = iks_next(traversenodes); 01212 } 01213 01214 receipt = iks_new("iq"); 01215 iks_insert_attrib(receipt, "type", "result"); 01216 iks_insert_attrib(receipt, "from", from); 01217 iks_insert_attrib(receipt, "to", iks_find_attrib(pak->x, "from")); 01218 iks_insert_attrib(receipt, "id", iks_find_attrib(pak->x, "id")); 01219 iks_send(c->p, receipt); 01220 iks_delete(receipt); 01221 01222 return 1; 01223 } 01224 01225 static struct ast_frame *gtalk_rtp_read(struct ast_channel *ast, struct gtalk_pvt *p) 01226 { 01227 struct ast_frame *f; 01228 01229 if (!p->rtp) 01230 return &ast_null_frame; 01231 f = ast_rtp_read(p->rtp); 01232 gtalk_update_stun(p->parent, p); 01233 if (p->owner) { 01234 /* We already hold the channel lock */ 01235 if (f->frametype == AST_FRAME_VOICE) { 01236 if (f->subclass != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) { 01237 if (option_debug) 01238 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass); 01239 p->owner->nativeformats = 01240 (p->owner->nativeformats & AST_FORMAT_VIDEO_MASK) | f->subclass; 01241 ast_set_read_format(p->owner, p->owner->readformat); 01242 ast_set_write_format(p->owner, p->owner->writeformat); 01243 } 01244 /* if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) { 01245 f = ast_dsp_process(p->owner, p->vad, f); 01246 if (option_debug && f && (f->frametype == AST_FRAME_DTMF)) 01247 if (option_debug) 01248 ast_log(LOG_DEBUG, "* Detected inband DTMF '%c'\n", f->subclass); 01249 } */ 01250 } 01251 } 01252 return f; 01253 } 01254 01255 static struct ast_frame *gtalk_read(struct ast_channel *ast) 01256 { 01257 struct ast_frame *fr; 01258 struct gtalk_pvt *p = ast->tech_pvt; 01259 01260 ast_mutex_lock(&p->lock); 01261 fr = gtalk_rtp_read(ast, p); 01262 ast_mutex_unlock(&p->lock); 01263 return fr; 01264 } 01265 01266 /*! \brief Send frame to media channel (rtp) */ 01267 static int gtalk_write(struct ast_channel *ast, struct ast_frame *frame) 01268 { 01269 struct gtalk_pvt *p = ast->tech_pvt; 01270 int res = 0; 01271 01272 switch (frame->frametype) { 01273 case AST_FRAME_VOICE: 01274 if (!(frame->subclass & ast->nativeformats)) { 01275 ast_log(LOG_WARNING, 01276 "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n", 01277 frame->subclass, ast->nativeformats, ast->readformat, 01278 ast->writeformat); 01279 return 0; 01280 } 01281 if (p) { 01282 ast_mutex_lock(&p->lock); 01283 if (p->rtp) { 01284 res = ast_rtp_write(p->rtp, frame); 01285 } 01286 ast_mutex_unlock(&p->lock); 01287 } 01288 break; 01289 case AST_FRAME_VIDEO: 01290 if (p) { 01291 ast_mutex_lock(&p->lock); 01292 if (p->vrtp) { 01293 res = ast_rtp_write(p->vrtp, frame); 01294 } 01295 ast_mutex_unlock(&p->lock); 01296 } 01297 break; 01298 case AST_FRAME_IMAGE: 01299 return 0; 01300 break; 01301 default: 01302 ast_log(LOG_WARNING, "Can't send %d type frames with Gtalk write\n", 01303 frame->frametype); 01304 return 0; 01305 } 01306 01307 return res; 01308 } 01309 01310 static int gtalk_fixup(struct ast_channel *oldchan, struct ast_channel *newchan) 01311 { 01312 struct gtalk_pvt *p = newchan->tech_pvt; 01313 ast_mutex_lock(&p->lock); 01314 01315 if ((p->owner != oldchan)) { 01316 ast_mutex_unlock(&p->lock); 01317 return -1; 01318 } 01319 if (p->owner == oldchan) 01320 p->owner = newchan; 01321 ast_mutex_unlock(&p->lock); 01322 return 0; 01323 } 01324 01325 static int gtalk_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen) 01326 { 01327 int res = 0; 01328 01329 switch (condition) { 01330 case AST_CONTROL_HOLD: 01331 ast_moh_start(ast, data, NULL); 01332 break; 01333 case AST_CONTROL_UNHOLD: 01334 ast_moh_stop(ast); 01335 break; 01336 default: 01337 ast_log(LOG_NOTICE, "Don't know how to indicate condition '%d'\n", condition); 01338 res = -1; 01339 } 01340 01341 return res; 01342 } 01343 01344 static int gtalk_digit_begin(struct ast_channel *chan, char digit) 01345 { 01346 return gtalk_digit(chan, digit, 0); 01347 } 01348 01349 static int gtalk_digit_end(struct ast_channel *chan, char digit, unsigned int duration) 01350 { 01351 return gtalk_digit(chan, digit, duration); 01352 } 01353 01354 static int gtalk_digit(struct ast_channel *ast, char digit, unsigned int duration) 01355 { 01356 struct gtalk_pvt *p = ast->tech_pvt; 01357 struct gtalk *client = p->parent; 01358 iks *iq, *gtalk, *dtmf; 01359 char buffer[2] = {digit, '\0'}; 01360 iq = iks_new("iq"); 01361 gtalk = iks_new("gtalk"); 01362 dtmf = iks_new("dtmf"); 01363 if(!iq || !gtalk || !dtmf) { 01364 if(iq) 01365 iks_delete(iq); 01366 if(gtalk) 01367 iks_delete(gtalk); 01368 if(dtmf) 01369 iks_delete(dtmf); 01370 ast_log(LOG_ERROR, "Did not send dtmf do to memory issue\n"); 01371 return -1; 01372 } 01373 01374 iks_insert_attrib(iq, "type", "set"); 01375 iks_insert_attrib(iq, "to", p->them); 01376 iks_insert_attrib(iq, "from", p->us); 01377 iks_insert_attrib(iq, "id", client->connection->mid); 01378 ast_aji_increment_mid(client->connection->mid); 01379 iks_insert_attrib(gtalk, "xmlns", "http://jabber.org/protocol/gtalk"); 01380 iks_insert_attrib(gtalk, "action", "content-info"); 01381 iks_insert_attrib(gtalk, "initiator", p->initiator ? p->us: p->them); 01382 iks_insert_attrib(gtalk, "sid", p->sid); 01383 iks_insert_attrib(dtmf, "xmlns", "http://jabber.org/protocol/gtalk/info/dtmf"); 01384 iks_insert_attrib(dtmf, "code", buffer); 01385 iks_insert_node(iq, gtalk); 01386 iks_insert_node(gtalk, dtmf); 01387 01388 ast_mutex_lock(&p->lock); 01389 if (ast->dtmff.frametype == AST_FRAME_DTMF_BEGIN) { 01390 iks_insert_attrib(dtmf, "action", "button-down"); 01391 } else if (ast->dtmff.frametype == AST_FRAME_DTMF_END) { 01392 iks_insert_attrib(dtmf, "action", "button-up"); 01393 } 01394 iks_send(client->connection->p, iq); 01395 iks_delete(iq); 01396 iks_delete(gtalk); 01397 iks_delete(dtmf); 01398 ast_mutex_unlock(&p->lock); 01399 return 0; 01400 } 01401 01402 static int gtalk_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen) 01403 { 01404 ast_log(LOG_NOTICE, "XXX Implement gtalk sendhtml XXX\n"); 01405 01406 return -1; 01407 } 01408 01409 /* Not in use right now. 01410 static int gtalk_auto_congest(void *nothing) 01411 { 01412 struct gtalk_pvt *p = nothing; 01413 01414 ast_mutex_lock(&p->lock); 01415 if (p->owner) { 01416 if (!ast_channel_trylock(p->owner)) { 01417 ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name); 01418 ast_queue_control(p->owner, AST_CONTROL_CONGESTION); 01419 ast_channel_unlock(p->owner); 01420 } 01421 } 01422 ast_mutex_unlock(&p->lock); 01423 return 0; 01424 } 01425 */ 01426 01427 /*! \brief Initiate new call, part of PBX interface 01428 * dest is the dial string */ 01429 static int gtalk_call(struct ast_channel *ast, char *dest, int timeout) 01430 { 01431 struct gtalk_pvt *p = ast->tech_pvt; 01432 01433 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) { 01434 ast_log(LOG_WARNING, "gtalk_call called on %s, neither down nor reserved\n", ast->name); 01435 return -1; 01436 } 01437 01438 ast_setstate(ast, AST_STATE_RING); 01439 p->jointcapability = p->capability; 01440 if (!p->ringrule) { 01441 ast_copy_string(p->ring, p->parent->connection->mid, sizeof(p->ring)); 01442 p->ringrule = iks_filter_add_rule(p->parent->connection->f, gtalk_ringing_ack, p, 01443 IKS_RULE_ID, p->ring, IKS_RULE_DONE); 01444 } else 01445 ast_log(LOG_WARNING, "Whoa, already have a ring rule!\n"); 01446 01447 gtalk_invite(p, p->them, p->us, p->sid, 1); 01448 gtalk_create_candidates(p->parent, p, p->sid, p->them, p->us); 01449 01450 return 0; 01451 } 01452 01453 /*! \brief Hangup a call through the gtalk proxy channel */ 01454 static int gtalk_hangup(struct ast_channel *ast) 01455 { 01456 struct gtalk_pvt *p = ast->tech_pvt; 01457 struct gtalk *client; 01458 01459 ast_mutex_lock(&p->lock); 01460 client = p->parent; 01461 p->owner = NULL; 01462 ast->tech_pvt = NULL; 01463 if (!p->alreadygone) 01464 gtalk_action(client, p, "terminate"); 01465 ast_mutex_unlock(&p->lock); 01466 01467 gtalk_free_pvt(client, p); 01468 ast_module_unref(ast_module_info->self); 01469 01470 return 0; 01471 } 01472 01473 /*! \brief Part of PBX interface */ 01474 static struct ast_channel *gtalk_request(const char *type, int format, void *data, int *cause) 01475 { 01476 struct gtalk_pvt *p = NULL; 01477 struct gtalk *client = NULL; 01478 char *sender = NULL, *to = NULL, *s = NULL; 01479 struct ast_channel *chan = NULL; 01480 01481 if (data) { 01482 s = ast_strdupa((char *) data); 01483 if (s) { 01484 sender = strsep(&s, "/"); 01485 if (sender && (sender[0] != '\0')) 01486 to = strsep(&s, "/"); 01487 if (!to) { 01488 ast_log(LOG_ERROR, "Bad arguments in Gtalk Dialstring: %s\n", (char*) data); 01489 if (s) 01490 free(s); 01491 return NULL; 01492 } 01493 } 01494 } 01495 client = find_gtalk(to, sender); 01496 if (!client) { 01497 ast_log(LOG_WARNING, "Could not find recipient.\n"); 01498 if (s) 01499 free(s); 01500 return NULL; 01501 } 01502 p = gtalk_alloc(client, strchr(sender, '@') ? sender : client->connection->jid->full, strchr(to, '@') ? to : client->user, NULL); 01503 if (p) 01504 chan = gtalk_new(client, p, AST_STATE_DOWN, to); 01505 01506 return chan; 01507 } 01508 01509 /*! \brief CLI command "gtalk show channels" */ 01510 static int gtalk_show_channels(int fd, int argc, char **argv) 01511 { 01512 if (argc != 3) 01513 return RESULT_SHOWUSAGE; 01514 ast_mutex_lock(>alklock); 01515 // if (!gtalk_list->p) 01516 ast_cli(fd, "No gtalk channels in use\n"); 01517 ast_mutex_unlock(>alklock); 01518 return RESULT_SUCCESS; 01519 } 01520 01521 /*! \brief CLI command "gtalk show channels" */ 01522 static int gtalk_do_reload(int fd, int argc, char **argv) 01523 { 01524 ast_verbose("IT DOES WORK!\n"); 01525 return RESULT_SUCCESS; 01526 } 01527 01528 static int gtalk_parser(void *data, ikspak *pak) 01529 { 01530 struct gtalk *client = ASTOBJ_REF((struct gtalk *) data); 01531 01532 if (iks_find_with_attrib(pak->x, "session", "type", "initiate")) { 01533 /* New call */ 01534 gtalk_newcall(client, pak); 01535 } else if (iks_find_with_attrib(pak->x, "session", "type", "candidates") || iks_find_with_attrib(pak->x, "session", "type", "transport-info") ) { 01536 if (option_debug > 2) 01537 ast_log(LOG_DEBUG, "About to add candidate!\n"); 01538 gtalk_add_candidate(client, pak); 01539 if (option_debug > 2) 01540 ast_log(LOG_DEBUG, "Candidate Added!\n"); 01541 } else if (iks_find_with_attrib(pak->x, "session", "type", "accept")) { 01542 gtalk_is_answered(client, pak); 01543 } else if (iks_find_with_attrib(pak->x, "session", "type", "content-info")) { 01544 gtalk_handle_dtmf(client, pak); 01545 } else if (iks_find_with_attrib(pak->x, "session", "type", "terminate")) { 01546 gtalk_hangup_farend(client, pak); 01547 } else if (iks_find_with_attrib(pak->x, "session", "type", "reject")) { 01548 gtalk_hangup_farend(client, pak); 01549 } 01550 ASTOBJ_UNREF(client, gtalk_member_destroy); 01551 return IKS_FILTER_EAT; 01552 } 01553 01554 /* Not using this anymore probably take out soon 01555 static struct gtalk_candidate *gtalk_create_candidate(char *args) 01556 { 01557 char *name, *type, *preference, *protocol; 01558 struct gtalk_candidate *res; 01559 res = malloc(sizeof(struct gtalk_candidate)); 01560 memset(res, 0, sizeof(struct gtalk_candidate)); 01561 if (args) 01562 name = args; 01563 if ((args = strchr(args, ','))) { 01564 *args = '\0'; 01565 args++; 01566 preference = args; 01567 } 01568 if ((args = strchr(args, ','))) { 01569 *args = '\0'; 01570 args++; 01571 protocol = args; 01572 } 01573 if ((args = strchr(args, ','))) { 01574 *args = '\0'; 01575 args++; 01576 type = args; 01577 } 01578 if (name) 01579 ast_copy_string(res->name, name, sizeof(res->name)); 01580 if (preference) { 01581 res->preference = atof(preference); 01582 } 01583 if (protocol) { 01584 if (!strcasecmp("udp", protocol)) 01585 res->protocol = AJI_PROTOCOL_UDP; 01586 if (!strcasecmp("ssltcp", protocol)) 01587 res->protocol = AJI_PROTOCOL_SSLTCP; 01588 } 01589 if (type) { 01590 if (!strcasecmp("stun", type)) 01591 res->type = AJI_CONNECT_STUN; 01592 if (!strcasecmp("local", type)) 01593 res->type = AJI_CONNECT_LOCAL; 01594 if (!strcasecmp("relay", type)) 01595 res->type = AJI_CONNECT_RELAY; 01596 } 01597 01598 return res; 01599 } 01600 */ 01601 01602 static int gtalk_create_member(char *label, struct ast_variable *var, int allowguest, 01603 struct ast_codec_pref prefs, char *context, 01604 struct gtalk *member) 01605 { 01606 struct aji_client *client; 01607 01608 if (!member) 01609 ast_log(LOG_WARNING, "Out of memory.\n"); 01610 01611 ast_copy_string(member->name, label, sizeof(member->name)); 01612 ast_copy_string(member->user, label, sizeof(member->user)); 01613 ast_copy_string(member->context, context, sizeof(member->context)); 01614 member->allowguest = allowguest; 01615 member->prefs = prefs; 01616 while (var) { 01617 #if 0 01618 struct gtalk_candidate *candidate = NULL; 01619 #endif 01620 if (!strcasecmp(var->name, "username")) 01621 ast_copy_string(member->user, var->value, sizeof(member->user)); 01622 else if (!strcasecmp(var->name, "disallow")) 01623 ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 0); 01624 else if (!strcasecmp(var->name, "allow")) 01625 ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 1); 01626 else if (!strcasecmp(var->name, "context")) 01627 ast_copy_string(member->context, var->value, sizeof(member->context)); 01628 #if 0 01629 else if (!strcasecmp(var->name, "candidate")) { 01630 candidate = gtalk_create_candidate(var->value); 01631 if (candidate) { 01632 candidate->next = member->ourcandidates; 01633 member->ourcandidates = candidate; 01634 } 01635 } 01636 #endif 01637 else if (!strcasecmp(var->name, "connection")) { 01638 if ((client = ast_aji_get_client(var->value))) { 01639 member->connection = client; 01640 iks_filter_add_rule(client->f, gtalk_parser, member, IKS_RULE_TYPE, 01641 IKS_PAK_IQ, IKS_RULE_FROM_PARTIAL, member->user, 01642 IKS_RULE_NS, "http://www.google.com/session", 01643 IKS_RULE_DONE); 01644 01645 } else { 01646 ast_log(LOG_ERROR, "connection referenced not found!\n"); 01647 return 0; 01648 } 01649 } 01650 var = var->next; 01651 } 01652 if (member->connection && member->user) 01653 member->buddy = ASTOBJ_CONTAINER_FIND(&member->connection->buddies, member->user); 01654 else { 01655 ast_log(LOG_ERROR, "No Connection or Username!\n"); 01656 } 01657 return 1; 01658 } 01659 01660 static int gtalk_load_config(void) 01661 { 01662 char *cat = NULL; 01663 struct ast_config *cfg = NULL; 01664 char context[100]; 01665 int allowguest = 1; 01666 struct ast_variable *var; 01667 struct gtalk *member; 01668 struct ast_codec_pref prefs; 01669 struct aji_client_container *clients; 01670 struct gtalk_candidate *global_candidates = NULL; 01671 struct hostent *hp; 01672 struct ast_hostent ahp; 01673 01674 cfg = ast_config_load(GOOGLE_CONFIG); 01675 if (!cfg) 01676 return 0; 01677 01678 /* Copy the default jb config over global_jbconf */ 01679 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf)); 01680 01681 cat = ast_category_browse(cfg, NULL); 01682 for (var = ast_variable_browse(cfg, "general"); var; var = var->next) { 01683 /* handle jb conf */ 01684 if (!ast_jb_read_conf(&global_jbconf, var->name, var->value)) 01685 continue; 01686 01687 if (!strcasecmp(var->name, "allowguest")) 01688 allowguest = 01689 (ast_true(ast_variable_retrieve(cfg, "general", "allowguest"))) ? 1 : 0; 01690 else if (!strcasecmp(var->name, "disallow")) 01691 ast_parse_allow_disallow(&prefs, &global_capability, var->value, 0); 01692 else if (!strcasecmp(var->name, "allow")) 01693 ast_parse_allow_disallow(&prefs, &global_capability, var->value, 1); 01694 else if (!strcasecmp(var->name, "context")) 01695 ast_copy_string(context, var->value, sizeof(context)); 01696 else if (!strcasecmp(var->name, "bindaddr")) { 01697 if (!(hp = ast_gethostbyname(var->value, &ahp))) { 01698 ast_log(LOG_WARNING, "Invalid address: %s\n", var->value); 01699 } else { 01700 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr)); 01701 } 01702 } 01703 /* Idea to allow for custom candidates */ 01704 /* 01705 else if (!strcasecmp(var->name, "candidate")) { 01706 candidate = gtalk_create_candidate(var->value); 01707 if (candidate) { 01708 candidate->next = global_candidates; 01709 global_candidates = candidate; 01710 } 01711 } 01712 */ 01713 } 01714 while (cat) { 01715 if (strcasecmp(cat, "general")) { 01716 var = ast_variable_browse(cfg, cat); 01717 member = (struct gtalk *) malloc(sizeof(struct gtalk)); 01718 memset(member, 0, sizeof(struct gtalk)); 01719 ASTOBJ_INIT(member); 01720 ASTOBJ_WRLOCK(member); 01721 if (!strcasecmp(cat, "guest")) { 01722 ast_copy_string(member->name, "guest", sizeof(member->name)); 01723 ast_copy_string(member->user, "guest", sizeof(member->user)); 01724 ast_copy_string(member->context, context, sizeof(member->context)); 01725 member->allowguest = allowguest; 01726 member->prefs = prefs; 01727 while (var) { 01728 if (!strcasecmp(var->name, "disallow")) 01729 ast_parse_allow_disallow(&member->prefs, &member->capability, 01730 var->value, 0); 01731 else if (!strcasecmp(var->name, "allow")) 01732 ast_parse_allow_disallow(&member->prefs, &member->capability, 01733 var->value, 1); 01734 else if (!strcasecmp(var->name, "context")) 01735 ast_copy_string(member->context, var->value, 01736 sizeof(member->context)); 01737 /* Idea to allow for custom candidates */ 01738 /* 01739 else if (!strcasecmp(var->name, "candidate")) { 01740 candidate = gtalk_create_candidate(var->value); 01741 if (candidate) { 01742 candidate->next = member->ourcandidates; 01743 member->ourcandidates = candidate; 01744 } 01745 } 01746 */ 01747 var = var->next; 01748 } 01749 ASTOBJ_UNLOCK(member); 01750 clients = ast_aji_get_clients(); 01751 if (clients) { 01752 ASTOBJ_CONTAINER_TRAVERSE(clients, 1, { 01753 ASTOBJ_WRLOCK(iterator); 01754 ASTOBJ_WRLOCK(member); 01755 member->connection = iterator; 01756 iks_filter_add_rule(iterator->f, gtalk_parser, member, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_NS, 01757 "http://www.google.com/session", IKS_RULE_DONE); 01758 ASTOBJ_UNLOCK(member); 01759 ASTOBJ_CONTAINER_LINK(>alk_list, member); 01760 ASTOBJ_UNLOCK(iterator); 01761 }); 01762 } else { 01763 ASTOBJ_UNLOCK(member); 01764 ASTOBJ_UNREF(member, gtalk_member_destroy); 01765 } 01766 } else { 01767 ASTOBJ_UNLOCK(member); 01768 if (gtalk_create_member(cat, var, allowguest, prefs, context, member)) 01769 ASTOBJ_CONTAINER_LINK(>alk_list, member); 01770 ASTOBJ_UNREF(member, gtalk_member_destroy); 01771 } 01772 } 01773 cat = ast_category_browse(cfg, cat); 01774 } 01775 gtalk_free_candidates(global_candidates); 01776 return 1; 01777 } 01778 01779 /*! \brief Load module into PBX, register channel */ 01780 static int load_module(void) 01781 { 01782 ASTOBJ_CONTAINER_INIT(>alk_list); 01783 if (!gtalk_load_config()) { 01784 ast_log(LOG_ERROR, "Unable to read config file %s. Not loading module.\n", GOOGLE_CONFIG); 01785 return 0; 01786 } 01787 01788 sched = sched_context_create(); 01789 if (!sched) 01790 ast_log(LOG_WARNING, "Unable to create schedule context\n"); 01791 01792 io = io_context_create(); 01793 if (!io) 01794 ast_log(LOG_WARNING, "Unable to create I/O context\n"); 01795 01796 if (ast_find_ourip(&__ourip, bindaddr)) { 01797 ast_log(LOG_WARNING, "Unable to get own IP address, Gtalk disabled\n"); 01798 return 0; 01799 } 01800 01801 ast_rtp_proto_register(>alk_rtp); 01802 ast_cli_register_multiple(gtalk_cli, sizeof(gtalk_cli) / sizeof(gtalk_cli[0])); 01803 01804 /* Make sure we can register our channel type */ 01805 if (ast_channel_register(>alk_tech)) { 01806 ast_log(LOG_ERROR, "Unable to register channel class %s\n", gtalk_tech.type); 01807 return -1; 01808 } 01809 return 0; 01810 } 01811 01812 /*! \brief Reload module */ 01813 static int reload(void) 01814 { 01815 return 0; 01816 } 01817 01818 /*! \brief Unload the gtalk channel from Asterisk */ 01819 static int unload_module(void) 01820 { 01821 struct gtalk_pvt *privates = NULL; 01822 ast_cli_unregister_multiple(gtalk_cli, sizeof(gtalk_cli) / sizeof(gtalk_cli[0])); 01823 /* First, take us out of the channel loop */ 01824 ast_channel_unregister(>alk_tech); 01825 ast_rtp_proto_unregister(>alk_rtp); 01826 01827 if (!ast_mutex_lock(>alklock)) { 01828 /* Hangup all interfaces if they have an owner */ 01829 ASTOBJ_CONTAINER_TRAVERSE(>alk_list, 1, { 01830 ASTOBJ_WRLOCK(iterator); 01831 privates = iterator->p; 01832 while(privates) { 01833 if (privates->owner) 01834 ast_softhangup(privates->owner, AST_SOFTHANGUP_APPUNLOAD); 01835 privates = privates->next; 01836 } 01837 iterator->p = NULL; 01838 ASTOBJ_UNLOCK(iterator); 01839 }); 01840 ast_mutex_unlock(>alklock); 01841 } else { 01842 ast_log(LOG_WARNING, "Unable to lock the monitor\n"); 01843 return -1; 01844 } 01845 ASTOBJ_CONTAINER_DESTROYALL(>alk_list, gtalk_member_destroy); 01846 ASTOBJ_CONTAINER_DESTROY(>alk_list); 01847 return 0; 01848 } 01849 01850 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Gtalk Channel Driver", 01851 .load = load_module, 01852 .unload = unload_module, 01853 .reload = reload, 01854 );