![]() |
Home page |
Mailing list |
Docs
Asterisk developer's documentation :: Codename Pineapple
chan_jingle.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 Jingle Channel Driver 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: 51385 $") 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 #include "asterisk/jingle.h" 00075 00076 #define JINGLE_CONFIG "jingle.conf" 00077 00078 /*! Global jitterbuffer configuration - by default, jb is disabled */ 00079 static struct ast_jb_conf default_jbconf = 00080 { 00081 .flags = 0, 00082 .max_size = -1, 00083 .resync_threshold = -1, 00084 .impl = "" 00085 }; 00086 static struct ast_jb_conf global_jbconf; 00087 00088 enum jingle_protocol { 00089 AJI_PROTOCOL_UDP = 1, 00090 AJI_PROTOCOL_SSLTCP = 2, 00091 }; 00092 00093 enum jingle_connect_type { 00094 AJI_CONNECT_STUN = 1, 00095 AJI_CONNECT_LOCAL = 2, 00096 AJI_CONNECT_RELAY = 3, 00097 }; 00098 00099 struct jingle_pvt { 00100 ast_mutex_t lock; /*!< Channel private lock */ 00101 time_t laststun; 00102 struct jingle *parent; /*!< Parent client */ 00103 char sid[100]; 00104 char from[100]; 00105 char ring[10]; /*!< Message ID of ring */ 00106 iksrule *ringrule; /*!< Rule for matching RING request */ 00107 int initiator; /*!< If we're the initiator */ 00108 int alreadygone; 00109 int capability; 00110 struct ast_codec_pref prefs; 00111 struct jingle_candidate *theircandidates; 00112 struct jingle_candidate *ourcandidates; 00113 char cid_num[80]; /*!< Caller ID num */ 00114 char cid_name[80]; /*!< Caller ID name */ 00115 char exten[80]; /*!< Called extension */ 00116 struct ast_channel *owner; /*!< Master Channel */ 00117 struct ast_rtp *rtp; /*!< RTP audio session */ 00118 struct ast_rtp *vrtp; /*!< RTP video session */ 00119 int jointcapability; /*!< Supported capability at both ends (codecs ) */ 00120 int peercapability; 00121 struct jingle_pvt *next; /* Next entity */ 00122 }; 00123 00124 struct jingle_candidate { 00125 char name[100]; 00126 enum jingle_protocol protocol; 00127 double preference; 00128 char username[100]; 00129 char password[100]; 00130 enum jingle_connect_type type; 00131 char network[6]; 00132 int generation; 00133 char ip[16]; 00134 int port; 00135 int receipt; 00136 struct jingle_candidate *next; 00137 }; 00138 00139 struct jingle { 00140 ASTOBJ_COMPONENTS(struct jingle); 00141 struct aji_client *connection; 00142 struct aji_buddy *buddy; 00143 struct jingle_pvt *p; 00144 struct ast_codec_pref prefs; 00145 int amaflags; /*!< AMA Flags */ 00146 char user[100]; 00147 char context[100]; 00148 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */ 00149 int capability; 00150 ast_group_t callgroup; /*!< Call group */ 00151 ast_group_t pickupgroup; /*!< Pickup group */ 00152 int callingpres; /*!< Calling presentation */ 00153 int allowguest; 00154 char language[MAX_LANGUAGE]; /*!< Default language for prompts */ 00155 char musicclass[MAX_MUSICCLASS]; /*!< Music on Hold class */ 00156 }; 00157 00158 struct jingle_container { 00159 ASTOBJ_CONTAINER_COMPONENTS(struct jingle); 00160 }; 00161 00162 static const char desc[] = "Jingle Channel"; 00163 static const char type[] = "Jingle"; 00164 00165 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263; 00166 00167 AST_MUTEX_DEFINE_STATIC(jinglelock); /*!< Protect the interface list (of jingle_pvt's) */ 00168 00169 /* Forward declarations */ 00170 static struct ast_channel *jingle_request(const char *type, int format, void *data, int *cause); 00171 static int jingle_digit_begin(struct ast_channel *ast, char digit); 00172 static int jingle_digit_end(struct ast_channel *ast, char digit, unsigned int duration); 00173 static int jingle_call(struct ast_channel *ast, char *dest, int timeout); 00174 static int jingle_hangup(struct ast_channel *ast); 00175 static int jingle_answer(struct ast_channel *ast); 00176 static int jingle_newcall(struct jingle *client, ikspak *pak); 00177 static struct ast_frame *jingle_read(struct ast_channel *ast); 00178 static int jingle_write(struct ast_channel *ast, struct ast_frame *f); 00179 static int jingle_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen); 00180 static int jingle_fixup(struct ast_channel *oldchan, struct ast_channel *newchan); 00181 static int jingle_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen); 00182 static struct jingle_pvt *jingle_alloc(struct jingle *client, const char *from, const char *sid); 00183 /*----- RTP interface functions */ 00184 static int jingle_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, 00185 struct ast_rtp *vrtp, int codecs, int nat_active); 00186 static enum ast_rtp_get_result jingle_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp); 00187 static int jingle_get_codec(struct ast_channel *chan); 00188 00189 /*! \brief PBX interface structure for channel registration */ 00190 static const struct ast_channel_tech jingle_tech = { 00191 .type = type, 00192 .description = "Jingle Channel Driver", 00193 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1), 00194 .requester = jingle_request, 00195 .send_digit_begin = jingle_digit_begin, 00196 .send_digit_end = jingle_digit_end, 00197 .bridge = ast_rtp_bridge, 00198 .call = jingle_call, 00199 .hangup = jingle_hangup, 00200 .answer = jingle_answer, 00201 .read = jingle_read, 00202 .write = jingle_write, 00203 .exception = jingle_read, 00204 .indicate = jingle_indicate, 00205 .fixup = jingle_fixup, 00206 .send_html = jingle_sendhtml, 00207 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER 00208 }; 00209 00210 static struct sockaddr_in bindaddr = { 0, }; /*!< The address we bind to */ 00211 00212 static struct sched_context *sched; /*!< The scheduling context */ 00213 static struct io_context *io; /*!< The IO context */ 00214 static struct in_addr __ourip; 00215 00216 00217 /*! \brief RTP driver interface */ 00218 static struct ast_rtp_protocol jingle_rtp = { 00219 type: "jingle", 00220 get_rtp_info: jingle_get_rtp_peer, 00221 set_rtp_peer: jingle_set_rtp_peer, 00222 get_codec: jingle_get_codec, 00223 }; 00224 00225 static char externip[16]; 00226 00227 static struct jingle_container jingles; 00228 00229 static void jingle_member_destroy(struct jingle *obj) 00230 { 00231 free(obj); 00232 } 00233 00234 static struct jingle *find_jingle(char *name, char *connection) 00235 { 00236 struct jingle *jingle = NULL; 00237 00238 jingle = ASTOBJ_CONTAINER_FIND(&jingles, name); 00239 if (!jingle && strchr(name, '@')) 00240 jingle = ASTOBJ_CONTAINER_FIND_FULL(&jingles, name, user,,, strcasecmp); 00241 00242 if (!jingle) { /* guest call */ 00243 ASTOBJ_CONTAINER_TRAVERSE(&jingles, 1, { 00244 ASTOBJ_WRLOCK(iterator); 00245 if (!strcasecmp(iterator->name, "guest")) { 00246 if (!strcasecmp(iterator->connection->jid->partial, connection)) { 00247 jingle = iterator; 00248 break; 00249 } else if (!strcasecmp(iterator->connection->name, connection)) { 00250 jingle = iterator; 00251 break; 00252 } 00253 } 00254 ASTOBJ_UNLOCK(iterator); 00255 }); 00256 00257 } 00258 return jingle; 00259 } 00260 00261 00262 static void add_codec_to_answer(const struct jingle_pvt *p, int codec, iks *dcodecs) 00263 { 00264 char *format = ast_getformatname(codec); 00265 00266 if (!strcasecmp("ulaw", format)) { 00267 iks *payload_eg711u, *payload_pcmu; 00268 payload_pcmu = iks_new("payload-type"); 00269 iks_insert_attrib(payload_pcmu, "id", "0"); 00270 iks_insert_attrib(payload_pcmu, "name", "PCMU"); 00271 iks_insert_attrib(payload_pcmu, "xmlns", "http://www.google.com/session/phone"); 00272 payload_eg711u = iks_new("payload-type"); 00273 iks_insert_attrib(payload_eg711u, "id", "100"); 00274 iks_insert_attrib(payload_eg711u, "name", "EG711U"); 00275 iks_insert_attrib(payload_eg711u, "xmlns", "http://www.google.com/session/phone"); 00276 iks_insert_node(dcodecs, payload_pcmu); 00277 iks_insert_node(dcodecs, payload_eg711u); 00278 } 00279 if (!strcasecmp("alaw", format)) { 00280 iks *payload_eg711a; 00281 iks *payload_pcma = iks_new("payload-type"); 00282 iks_insert_attrib(payload_pcma, "id", "8"); 00283 iks_insert_attrib(payload_pcma, "name", "PCMA"); 00284 iks_insert_attrib(payload_pcma, "xmlns", "http://www.google.com/session/phone"); 00285 payload_eg711a = iks_new("payload-type"); 00286 iks_insert_attrib(payload_eg711a, "id", "101"); 00287 iks_insert_attrib(payload_eg711a, "name", "EG711A"); 00288 iks_insert_attrib(payload_eg711a, "xmlns", "http://www.google.com/session/phone"); 00289 iks_insert_node(dcodecs, payload_pcma); 00290 iks_insert_node(dcodecs, payload_eg711a); 00291 } 00292 if (!strcasecmp("ilbc", format)) { 00293 iks *payload_ilbc = iks_new("payload-type"); 00294 iks_insert_attrib(payload_ilbc, "id", "102"); 00295 iks_insert_attrib(payload_ilbc, "name", "iLBC"); 00296 iks_insert_attrib(payload_ilbc, "xmlns", "http://www.google.com/session/phone"); 00297 iks_insert_node(dcodecs, payload_ilbc); 00298 } 00299 if (!strcasecmp("g723", format)) { 00300 iks *payload_g723 = iks_new("payload-type"); 00301 iks_insert_attrib(payload_g723, "id", "4"); 00302 iks_insert_attrib(payload_g723, "name", "G723"); 00303 iks_insert_attrib(payload_g723, "xmlns", "http://www.google.com/session/phone"); 00304 iks_insert_node(dcodecs, payload_g723); 00305 } 00306 ast_rtp_lookup_code(p->rtp, 1, codec); 00307 } 00308 00309 static int jingle_accept_call(struct jingle *client, struct jingle_pvt *p) 00310 { 00311 struct jingle_pvt *tmp = client->p; 00312 struct aji_client *c = client->connection; 00313 iks *iq, *jingle, *dcodecs, *payload_red, *payload_audio, *payload_cn; 00314 int x; 00315 int pref_codec = 0; 00316 int alreadysent = 0; 00317 00318 if (p->initiator) 00319 return 1; 00320 00321 iq = iks_new("iq"); 00322 jingle = iks_new(GOOGLE_NODE); 00323 dcodecs = iks_new("description"); 00324 if (iq && jingle && dcodecs) { 00325 iks_insert_attrib(dcodecs, "xmlns", "http://www.google.com/session/phone"); 00326 00327 for (x = 0; x < 32; x++) { 00328 if (!(pref_codec = ast_codec_pref_index(&client->prefs, x))) 00329 break; 00330 if (!(client->capability & pref_codec)) 00331 continue; 00332 if (alreadysent & pref_codec) 00333 continue; 00334 if (pref_codec <= AST_FORMAT_MAX_AUDIO) 00335 add_codec_to_answer(p, pref_codec, dcodecs); 00336 else 00337 add_codec_to_answer(p, pref_codec, dcodecs); 00338 alreadysent |= pref_codec; 00339 } 00340 payload_red = iks_new("payload-type"); 00341 iks_insert_attrib(payload_red, "id", "117"); 00342 iks_insert_attrib(payload_red, "name", "red"); 00343 iks_insert_attrib(payload_red, "xmlns", "http://www.google.com/session/phone"); 00344 payload_audio = iks_new("payload-type"); 00345 iks_insert_attrib(payload_audio, "id", "106"); 00346 iks_insert_attrib(payload_audio, "name", "audio/telephone-event"); 00347 iks_insert_attrib(payload_audio, "xmlns", "http://www.google.com/session/phone"); 00348 payload_cn = iks_new("payload-type"); 00349 iks_insert_attrib(payload_cn, "id", "13"); 00350 iks_insert_attrib(payload_cn, "name", "CN"); 00351 iks_insert_attrib(payload_cn, "xmlns", "http://www.google.com/session/phone"); 00352 00353 00354 iks_insert_attrib(iq, "type", "set"); 00355 iks_insert_attrib(iq, "to", (p->from) ? p->from : client->user); 00356 iks_insert_attrib(iq, "id", client->connection->mid); 00357 ast_aji_increment_mid(client->connection->mid); 00358 00359 iks_insert_attrib(jingle, "xmlns", "http://www.google.com/session"); 00360 iks_insert_attrib(jingle, "type", JINGLE_ACCEPT); 00361 iks_insert_attrib(jingle, "initiator", 00362 p->initiator ? client->connection->jid->full : p->from); 00363 iks_insert_attrib(jingle, GOOGLE_SID, tmp->sid); 00364 iks_insert_node(iq, jingle); 00365 iks_insert_node(jingle, dcodecs); 00366 iks_insert_node(dcodecs, payload_red); 00367 iks_insert_node(dcodecs, payload_audio); 00368 iks_insert_node(dcodecs, payload_cn); 00369 00370 iks_send(c->p, iq); 00371 iks_delete(payload_red); 00372 iks_delete(payload_audio); 00373 iks_delete(payload_cn); 00374 iks_delete(dcodecs); 00375 iks_delete(jingle); 00376 iks_delete(iq); 00377 } 00378 return 1; 00379 } 00380 00381 static int jingle_ringing_ack(void *data, ikspak *pak) 00382 { 00383 struct jingle_pvt *p = data; 00384 00385 if (p->ringrule) 00386 iks_filter_remove_rule(p->parent->connection->f, p->ringrule); 00387 p->ringrule = NULL; 00388 if (p->owner) 00389 ast_queue_control(p->owner, AST_CONTROL_RINGING); 00390 return IKS_FILTER_EAT; 00391 } 00392 00393 static int jingle_answer(struct ast_channel *ast) 00394 { 00395 struct jingle_pvt *p = ast->tech_pvt; 00396 struct jingle *client = p->parent; 00397 int res = 0; 00398 00399 if (option_debug) 00400 ast_log(LOG_DEBUG, "Answer!\n"); 00401 ast_mutex_lock(&p->lock); 00402 jingle_accept_call(client, p); 00403 ast_mutex_unlock(&p->lock); 00404 return res; 00405 } 00406 00407 static enum ast_rtp_get_result jingle_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp) 00408 { 00409 struct jingle_pvt *p = chan->tech_pvt; 00410 enum ast_rtp_get_result res = AST_RTP_GET_FAILED; 00411 00412 if (!p) 00413 return res; 00414 00415 ast_mutex_lock(&p->lock); 00416 if (p->rtp) { 00417 *rtp = p->rtp; 00418 res = AST_RTP_TRY_NATIVE; 00419 } 00420 ast_mutex_unlock(&p->lock); 00421 00422 return res; 00423 } 00424 00425 static int jingle_get_codec(struct ast_channel *chan) 00426 { 00427 struct jingle_pvt *p = chan->tech_pvt; 00428 return p->peercapability; 00429 } 00430 00431 static int jingle_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active) 00432 { 00433 struct jingle_pvt *p; 00434 00435 p = chan->tech_pvt; 00436 if (!p) 00437 return -1; 00438 ast_mutex_lock(&p->lock); 00439 00440 /* if (rtp) 00441 ast_rtp_get_peer(rtp, &p->redirip); 00442 else 00443 memset(&p->redirip, 0, sizeof(p->redirip)); 00444 p->redircodecs = codecs; */ 00445 00446 /* Reset lastrtprx timer */ 00447 ast_mutex_unlock(&p->lock); 00448 return 0; 00449 } 00450 00451 static int jingle_response(struct jingle *client, ikspak *pak, const char *reasonstr, const char *reasonstr2) 00452 { 00453 iks *response = NULL, *error = NULL, *reason = NULL; 00454 int res = -1; 00455 00456 response = iks_new("iq"); 00457 if (response) { 00458 iks_insert_attrib(response, "type", "result"); 00459 iks_insert_attrib(response, "from", client->connection->jid->full); 00460 iks_insert_attrib(response, "to", iks_find_attrib(pak->x, "from")); 00461 iks_insert_attrib(response, "id", iks_find_attrib(pak->x, "id")); 00462 if (reasonstr) { 00463 error = iks_new("error"); 00464 if (error) { 00465 iks_insert_attrib(error, "type", "cancel"); 00466 reason = iks_new(reasonstr); 00467 if (reason) 00468 iks_insert_node(error, reason); 00469 iks_insert_node(response, error); 00470 } 00471 } 00472 iks_send(client->connection->p, response); 00473 if (reason) 00474 iks_delete(reason); 00475 if (error) 00476 iks_delete(error); 00477 iks_delete(response); 00478 res = 0; 00479 } 00480 return res; 00481 } 00482 00483 static int jingle_is_answered(struct jingle *client, ikspak *pak) 00484 { 00485 struct jingle_pvt *tmp; 00486 00487 if (option_debug) 00488 ast_log(LOG_DEBUG, "The client is %s\n", client->name); 00489 /* Make sure our new call doesn't exist yet */ 00490 for (tmp = client->p; tmp; tmp = tmp->next) { 00491 if (iks_find_with_attrib(pak->x, GOOGLE_NODE, GOOGLE_SID, tmp->sid)) 00492 break; 00493 } 00494 00495 if (tmp) { 00496 if (tmp->owner) 00497 ast_queue_control(tmp->owner, AST_CONTROL_ANSWER); 00498 } else 00499 ast_log(LOG_NOTICE, "Whoa, didn't find call!\n"); 00500 jingle_response(client, pak, NULL, NULL); 00501 return 1; 00502 } 00503 00504 static int jingle_handle_dtmf(struct jingle *client, ikspak *pak) 00505 { 00506 struct jingle_pvt *tmp; 00507 iks *dtmfnode = NULL; 00508 char *dtmf; 00509 /* Make sure our new call doesn't exist yet */ 00510 for (tmp = client->p; tmp; tmp = tmp->next) { 00511 if (iks_find_with_attrib(pak->x, GOOGLE_NODE, GOOGLE_SID, tmp->sid)) 00512 break; 00513 } 00514 00515 if (tmp) { 00516 if(iks_find_with_attrib(pak->x, "dtmf-method", "method", "rtp")) { 00517 jingle_response(client,pak, 00518 "feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'", 00519 "unsupported-dtmf-method xmlns='http://jabber.org/protocol/jingle/info/dtmf#errors'"); 00520 return -1; 00521 } 00522 if ((dtmfnode = iks_find(pak->x, "dtmf"))) { 00523 if((dtmf = iks_find_attrib(dtmfnode, "code"))) { 00524 if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-up")) { 00525 struct ast_frame f = {AST_FRAME_DTMF_BEGIN, }; 00526 f.subclass = dtmf[0]; 00527 ast_queue_frame(tmp->owner, &f); 00528 ast_verbose("JINGLE! DTMF-relay event received: %c\n", f.subclass); 00529 } else if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-down")) { 00530 struct ast_frame f = {AST_FRAME_DTMF_END, }; 00531 f.subclass = dtmf[0]; 00532 ast_queue_frame(tmp->owner, &f); 00533 ast_verbose("JINGLE! DTMF-relay event received: %c\n", f.subclass); 00534 } else if(iks_find_attrib(pak->x, "dtmf")) { /* 250 millasecond default */ 00535 struct ast_frame f = {AST_FRAME_DTMF, }; 00536 f.subclass = dtmf[0]; 00537 ast_queue_frame(tmp->owner, &f); 00538 ast_verbose("JINGLE! DTMF-relay event received: %c\n", f.subclass); 00539 } 00540 } 00541 } 00542 jingle_response(client, pak, NULL, NULL); 00543 return 1; 00544 } else 00545 ast_log(LOG_NOTICE, "Whoa, didn't find call!\n"); 00546 00547 jingle_response(client, pak, NULL, NULL); 00548 return 1; 00549 } 00550 00551 00552 static int jingle_hangup_farend(struct jingle *client, ikspak *pak) 00553 { 00554 struct jingle_pvt *tmp; 00555 00556 if (option_debug) 00557 ast_log(LOG_DEBUG, "The client is %s\n", client->name); 00558 /* Make sure our new call doesn't exist yet */ 00559 for (tmp = client->p; tmp; tmp = tmp->next) { 00560 if (iks_find_with_attrib(pak->x, GOOGLE_NODE, GOOGLE_SID, tmp->sid)) 00561 break; 00562 } 00563 00564 if (tmp) { 00565 tmp->alreadygone = 1; 00566 ast_queue_hangup(tmp->owner); 00567 } else 00568 ast_log(LOG_NOTICE, "Whoa, didn't find call!\n"); 00569 jingle_response(client, pak, NULL, NULL); 00570 return 1; 00571 } 00572 00573 static int jingle_create_candidates(struct jingle *client, struct jingle_pvt *p, char *sid, char *from) 00574 { 00575 struct jingle_candidate *tmp; 00576 struct aji_client *c = client->connection; 00577 struct jingle_candidate *ours1 = NULL, *ours2 = NULL; 00578 struct sockaddr_in sin; 00579 struct sockaddr_in dest; 00580 struct in_addr us; 00581 iks *iq, *jingle, *candidate; 00582 char user[17], pass[17], preference[5], port[7]; 00583 00584 00585 iq = iks_new("iq"); 00586 jingle = iks_new(GOOGLE_NODE); 00587 candidate = iks_new("candidate"); 00588 if (!iq || !jingle || !candidate) { 00589 ast_log(LOG_ERROR, "Memory allocation error\n"); 00590 goto safeout; 00591 } 00592 ours1 = ast_calloc(1, sizeof(*ours1)); 00593 ours2 = ast_calloc(1, sizeof(*ours2)); 00594 if (!ours1 || !ours2) 00595 goto safeout; 00596 iks_insert_node(iq, jingle); 00597 iks_insert_node(jingle, candidate); 00598 00599 for (; p; p = p->next) { 00600 if (!strcasecmp(p->sid, sid)) 00601 break; 00602 } 00603 00604 if (!p) { 00605 ast_log(LOG_NOTICE, "No matching jingle session - SID %s!\n", sid); 00606 goto safeout; 00607 } 00608 00609 ast_rtp_get_us(p->rtp, &sin); 00610 ast_find_ourip(&us, bindaddr); 00611 00612 /* Setup our jingle candidates */ 00613 ast_copy_string(ours1->name, "rtp", sizeof(ours1->name)); 00614 ours1->port = ntohs(sin.sin_port); 00615 ours1->preference = 1; 00616 snprintf(user, sizeof(user), "%08lx%08lx", ast_random(), ast_random()); 00617 snprintf(pass, sizeof(pass), "%08lx%08lx", ast_random(), ast_random()); 00618 ast_copy_string(ours1->username, user, sizeof(ours1->username)); 00619 ast_copy_string(ours1->password, pass, sizeof(ours1->password)); 00620 ast_copy_string(ours1->ip, ast_inet_ntoa(us), sizeof(ours1->ip)); 00621 ours1->protocol = AJI_PROTOCOL_UDP; 00622 ours1->type = AJI_CONNECT_LOCAL; 00623 ours1->generation = 0; 00624 p->ourcandidates = ours1; 00625 00626 if (!ast_strlen_zero(externip)) { 00627 /* XXX We should really stun for this one not just go with externip XXX */ 00628 snprintf(user, sizeof(user), "%08lx%08lx", ast_random(), ast_random()); 00629 snprintf(pass, sizeof(pass), "%08lx%08lx", ast_random(), ast_random()); 00630 ast_copy_string(ours2->username, user, sizeof(ours2->username)); 00631 ast_copy_string(ours2->password, pass, sizeof(ours2->password)); 00632 ast_copy_string(ours2->ip, externip, sizeof(ours2->ip)); 00633 ast_copy_string(ours2->name, "rtp", sizeof(ours1->name)); 00634 ours2->port = ntohs(sin.sin_port); 00635 ours2->preference = 0.9; 00636 ours2->protocol = AJI_PROTOCOL_UDP; 00637 ours2->type = AJI_CONNECT_STUN; 00638 ours2->generation = 0; 00639 ours1->next = ours2; 00640 ours2 = NULL; 00641 } 00642 ours1 = NULL; 00643 dest.sin_addr = __ourip; 00644 dest.sin_port = sin.sin_port; 00645 00646 00647 for (tmp = p->ourcandidates; tmp; tmp = tmp->next) { 00648 snprintf(port, sizeof(port), "%d", tmp->port); 00649 snprintf(preference, sizeof(preference), "%.2f", tmp->preference); 00650 iks_insert_attrib(iq, "from", c->jid->full); 00651 iks_insert_attrib(iq, "to", from); 00652 iks_insert_attrib(iq, "type", "set"); 00653 iks_insert_attrib(iq, "id", c->mid); 00654 ast_aji_increment_mid(c->mid); 00655 iks_insert_attrib(jingle, "type", "candidates"); 00656 iks_insert_attrib(jingle, "id", sid); 00657 iks_insert_attrib(jingle, "initiator", (p->initiator) ? c->jid->full : from); 00658 iks_insert_attrib(jingle, "xmlns", GOOGLE_NS); 00659 iks_insert_attrib(candidate, "name", tmp->name); 00660 iks_insert_attrib(candidate, "address", tmp->ip); 00661 iks_insert_attrib(candidate, "port", port); 00662 iks_insert_attrib(candidate, "username", tmp->username); 00663 iks_insert_attrib(candidate, "password", tmp->password); 00664 iks_insert_attrib(candidate, "preference", preference); 00665 if (tmp->protocol == AJI_PROTOCOL_UDP) 00666 iks_insert_attrib(candidate, "protocol", "udp"); 00667 if (tmp->protocol == AJI_PROTOCOL_SSLTCP) 00668 iks_insert_attrib(candidate, "protocol", "ssltcp"); 00669 if (tmp->type == AJI_CONNECT_STUN) 00670 iks_insert_attrib(candidate, "type", "stun"); 00671 if (tmp->type == AJI_CONNECT_LOCAL) 00672 iks_insert_attrib(candidate, "type", "local"); 00673 if (tmp->type == AJI_CONNECT_RELAY) 00674 iks_insert_attrib(candidate, "type", "relay"); 00675 iks_insert_attrib(candidate, "network", "0"); 00676 iks_insert_attrib(candidate, "generation", "0"); 00677 iks_send(c->p, iq); 00678 } 00679 p->laststun = 0; 00680 00681 safeout: 00682 if (ours1) 00683 free(ours1); 00684 if (ours2) 00685 free(ours2); 00686 if (iq) 00687 iks_delete(iq); 00688 if (jingle) 00689 iks_delete(jingle); 00690 if (candidate) 00691 iks_delete(candidate); 00692 return 1; 00693 } 00694 00695 static struct jingle_pvt *jingle_alloc(struct jingle *client, const char *from, const char *sid) 00696 { 00697 struct jingle_pvt *tmp = NULL; 00698 struct aji_resource *resources = NULL; 00699 struct aji_buddy *buddy; 00700 char idroster[200]; 00701 00702 if (option_debug) 00703 ast_log(LOG_DEBUG, "The client is %s for alloc\n", client->name); 00704 if (!sid && !strchr(from, '/')) { /* I started call! */ 00705 if (!strcasecmp(client->name, "guest")) { 00706 buddy = ASTOBJ_CONTAINER_FIND(&client->connection->buddies, from); 00707 if (buddy) 00708 resources = buddy->resources; 00709 } else 00710 resources = client->buddy->resources; 00711 while (resources) { 00712 if (resources->cap->jingle) { 00713 break; 00714 } 00715 resources = resources->next; 00716 } 00717 if (resources) 00718 snprintf(idroster, sizeof(idroster), "%s/%s", from, resources->resource); 00719 else { 00720 ast_log(LOG_ERROR, "no jingle capable clients to talk to.\n"); 00721 return NULL; 00722 } 00723 } 00724 if (!(tmp = ast_calloc(1, sizeof(*tmp)))) { 00725 return NULL; 00726 } 00727 if (sid) { 00728 ast_copy_string(tmp->sid, sid, sizeof(tmp->sid)); 00729 ast_copy_string(tmp->from, from, sizeof(tmp->from)); 00730 } else { 00731 snprintf(tmp->sid, sizeof(tmp->sid), "%08lx%08lx", ast_random(), ast_random()); 00732 ast_copy_string(tmp->from, idroster, sizeof(tmp->from)); 00733 tmp->initiator = 1; 00734 } 00735 tmp->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr); 00736 tmp->parent = client; 00737 if (!tmp->rtp) { 00738 ast_log(LOG_WARNING, "Out of RTP sessions?\n"); 00739 free(tmp); 00740 return NULL; 00741 } 00742 ast_copy_string(tmp->exten, "s", sizeof(tmp->exten)); 00743 ast_mutex_init(&tmp->lock); 00744 ast_mutex_lock(&jinglelock); 00745 tmp->next = client->p; 00746 client->p = tmp; 00747 ast_mutex_unlock(&jinglelock); 00748 return tmp; 00749 } 00750 00751 /*! \brief Start new jingle channel */ 00752 static struct ast_channel *jingle_new(struct jingle *client, struct jingle_pvt *i, int state, const char *title) 00753 { 00754 struct ast_channel *tmp; 00755 int fmt; 00756 int what; 00757 const char *str; 00758 00759 if (title) 00760 str = title; 00761 else 00762 str = i->from; 00763 tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, "Jingle/%s-%04lx", str, ast_random() & 0xffff); 00764 if (!tmp) { 00765 ast_log(LOG_WARNING, "Unable to allocate Jingle channel structure!\n"); 00766 return NULL; 00767 } 00768 tmp->tech = &jingle_tech; 00769 00770 /* Select our native format based on codec preference until we receive 00771 something from another device to the contrary. */ 00772 if (i->jointcapability) 00773 what = i->jointcapability; 00774 else if (i->capability) 00775 what = i->capability; 00776 else 00777 what = global_capability; 00778 tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | (i->jointcapability & AST_FORMAT_VIDEO_MASK); 00779 fmt = ast_best_codec(tmp->nativeformats); 00780 00781 if (i->rtp) { 00782 tmp->fds[0] = ast_rtp_fd(i->rtp); 00783 tmp->fds[1] = ast_rtcp_fd(i->rtp); 00784 } 00785 if (i->vrtp) { 00786 tmp->fds[2] = ast_rtp_fd(i->vrtp); 00787 tmp->fds[3] = ast_rtcp_fd(i->vrtp); 00788 } 00789 if (state == AST_STATE_RING) 00790 tmp->rings = 1; 00791 tmp->adsicpe = AST_ADSI_UNAVAILABLE; 00792 tmp->writeformat = fmt; 00793 tmp->rawwriteformat = fmt; 00794 tmp->readformat = fmt; 00795 tmp->rawreadformat = fmt; 00796 tmp->tech_pvt = i; 00797 00798 tmp->callgroup = client->callgroup; 00799 tmp->pickupgroup = client->pickupgroup; 00800 tmp->cid.cid_pres = client->callingpres; 00801 if (!ast_strlen_zero(client->accountcode)) 00802 ast_string_field_set(tmp, accountcode, client->accountcode); 00803 if (client->amaflags) 00804 tmp->amaflags = client->amaflags; 00805 if (!ast_strlen_zero(client->language)) 00806 ast_string_field_set(tmp, language, client->language); 00807 if (!ast_strlen_zero(client->musicclass)) 00808 ast_string_field_set(tmp, musicclass, client->musicclass); 00809 i->owner = tmp; 00810 ast_copy_string(tmp->context, client->context, sizeof(tmp->context)); 00811 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten)); 00812 /* Don't use ast_set_callerid() here because it will 00813 * generate an unnecessary NewCallerID event */ 00814 tmp->cid.cid_num = ast_strdup(i->cid_num); 00815 tmp->cid.cid_ani = ast_strdup(i->cid_num); 00816 tmp->cid.cid_name = ast_strdup(i->cid_name); 00817 if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s")) 00818 tmp->cid.cid_dnid = ast_strdup(i->exten); 00819 tmp->priority = 1; 00820 if (i->rtp) 00821 ast_jb_configure(tmp, &global_jbconf); 00822 if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) { 00823 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name); 00824 tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION; 00825 ast_hangup(tmp); 00826 tmp = NULL; 00827 } 00828 00829 return tmp; 00830 } 00831 00832 static int jingle_action(struct jingle *client, struct jingle_pvt *p, const char *action) 00833 { 00834 iks *request, *session = NULL; 00835 int res = -1; 00836 00837 request = iks_new("iq"); 00838 if (request) { 00839 iks_insert_attrib(request, "type", "set"); 00840 iks_insert_attrib(request, "from", client->connection