![]() |
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