Fix DTMF following what has been done in issue #9401. Thanks irroot.
[asterisk/asterisk.git] / channels / chan_jingle.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Matt O'Gorman <mogorman@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \author Matt O'Gorman <mogorman@digium.com>
22  *
23  * \brief Jingle Channel Driver
24  *
25  * \extref Iksemel http://iksemel.jabberstudio.org/
26  * 
27  * \ingroup channel_drivers
28  */
29
30 /*** MODULEINFO
31         <depend>iksemel</depend>
32         <depend>res_jabber</depend>
33         <use>gnutls</use>
34  ***/
35
36 #include "asterisk.h"
37
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <sys/socket.h>
45 #include <errno.h>
46 #include <stdlib.h>
47 #include <fcntl.h>
48 #include <netdb.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
51 #include <sys/signal.h>
52 #include <iksemel.h>
53 #include <pthread.h>
54
55 #ifdef HAVE_GNUTLS
56 #include <gcrypt.h>
57 GCRY_THREAD_OPTION_PTHREAD_IMPL;
58 #endif /* HAVE_GNUTLS */
59
60 #include "asterisk/lock.h"
61 #include "asterisk/channel.h"
62 #include "asterisk/config.h"
63 #include "asterisk/logger.h"
64 #include "asterisk/module.h"
65 #include "asterisk/pbx.h"
66 #include "asterisk/options.h"
67 #include "asterisk/lock.h"
68 #include "asterisk/sched.h"
69 #include "asterisk/io.h"
70 #include "asterisk/rtp.h"
71 #include "asterisk/acl.h"
72 #include "asterisk/callerid.h"
73 #include "asterisk/file.h"
74 #include "asterisk/cli.h"
75 #include "asterisk/app.h"
76 #include "asterisk/musiconhold.h"
77 #include "asterisk/manager.h"
78 #include "asterisk/stringfields.h"
79 #include "asterisk/utils.h"
80 #include "asterisk/causes.h"
81 #include "asterisk/astobj.h"
82 #include "asterisk/abstract_jb.h"
83 #include "asterisk/jabber.h"
84 #include "asterisk/jingle.h"
85
86 #define JINGLE_CONFIG "jingle.conf"
87
88 /*! Global jitterbuffer configuration - by default, jb is disabled */
89 static struct ast_jb_conf default_jbconf =
90 {
91         .flags = 0,
92         .max_size = -1,
93         .resync_threshold = -1,
94         .impl = ""
95 };
96 static struct ast_jb_conf global_jbconf;
97
98 enum jingle_protocol {
99         AJI_PROTOCOL_UDP = 1,
100         AJI_PROTOCOL_SSLTCP = 2,
101 };
102
103 enum jingle_connect_type {
104         AJI_CONNECT_STUN = 1,
105         AJI_CONNECT_LOCAL = 2,
106         AJI_CONNECT_RELAY = 3,
107 };
108
109 struct jingle_pvt {
110         ast_mutex_t lock;                /*!< Channel private lock */
111         time_t laststun;
112         struct jingle *parent;           /*!< Parent client */
113         char sid[100];
114         char from[100];
115         char ring[10];                   /*!< Message ID of ring */
116         iksrule *ringrule;               /*!< Rule for matching RING request */
117         int initiator;                   /*!< If we're the initiator */
118         int alreadygone;
119         int capability;
120         struct ast_codec_pref prefs;
121         struct jingle_candidate *theircandidates;
122         struct jingle_candidate *ourcandidates;
123         char cid_num[80];                /*!< Caller ID num */
124         char cid_name[80];               /*!< Caller ID name */
125         char exten[80];                  /*!< Called extension */
126         struct ast_channel *owner;       /*!< Master Channel */
127         struct ast_rtp *rtp;             /*!< RTP audio session */
128         struct ast_rtp *vrtp;            /*!< RTP video session */
129         int jointcapability;             /*!< Supported capability at both ends (codecs ) */
130         int peercapability;
131         struct jingle_pvt *next;        /* Next entity */
132 };
133
134 struct jingle_candidate {
135         char name[100];
136         enum jingle_protocol protocol;
137         double preference;
138         char username[100];
139         char password[100];
140         enum jingle_connect_type type;
141         char network[6];
142         int generation;
143         char ip[16];
144         int port;
145         int receipt;
146         struct jingle_candidate *next;
147 };
148
149 struct jingle {
150         ASTOBJ_COMPONENTS(struct jingle);
151         struct aji_client *connection;
152         struct aji_buddy *buddy;
153         struct jingle_pvt *p;
154         struct ast_codec_pref prefs;
155         int amaflags;                   /*!< AMA Flags */
156         char user[100];
157         char context[100];
158         char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
159         int capability;
160         ast_group_t callgroup;  /*!< Call group */
161         ast_group_t pickupgroup;        /*!< Pickup group */
162         int callingpres;                /*!< Calling presentation */
163         int allowguest;
164         char language[MAX_LANGUAGE];    /*!<  Default language for prompts */
165         char musicclass[MAX_MUSICCLASS];        /*!<  Music on Hold class */
166 };
167
168 struct jingle_container {
169         ASTOBJ_CONTAINER_COMPONENTS(struct jingle);
170 };
171
172 static const char desc[] = "Jingle Channel";
173 static const char type[] = "Jingle";
174
175 static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
176
177 AST_MUTEX_DEFINE_STATIC(jinglelock); /*!< Protect the interface list (of jingle_pvt's) */
178
179 /* Forward declarations */
180 static struct ast_channel *jingle_request(const char *type, int format, void *data, int *cause);
181 static int jingle_digit_begin(struct ast_channel *ast, char digit);
182 static int jingle_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
183 static int jingle_call(struct ast_channel *ast, char *dest, int timeout);
184 static int jingle_hangup(struct ast_channel *ast);
185 static int jingle_answer(struct ast_channel *ast);
186 static int jingle_newcall(struct jingle *client, ikspak *pak);
187 static struct ast_frame *jingle_read(struct ast_channel *ast);
188 static int jingle_write(struct ast_channel *ast, struct ast_frame *f);
189 static int jingle_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
190 static int jingle_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
191 static int jingle_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
192 static struct jingle_pvt *jingle_alloc(struct jingle *client, const char *from, const char *sid);
193 /*----- RTP interface functions */
194 static int jingle_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp,
195                                                            struct ast_rtp *vrtp, struct ast_rtp *tpeer, int codecs, int nat_active);
196 static enum ast_rtp_get_result jingle_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
197 static int jingle_get_codec(struct ast_channel *chan);
198
199 /*! \brief PBX interface structure for channel registration */
200 static const struct ast_channel_tech jingle_tech = {
201         .type = type,
202         .description = "Jingle Channel Driver",
203         .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
204         .requester = jingle_request,
205         .send_digit_begin = jingle_digit_begin,
206         .send_digit_end = jingle_digit_end,
207         .bridge = ast_rtp_bridge,
208         .call = jingle_call,
209         .hangup = jingle_hangup,
210         .answer = jingle_answer,
211         .read = jingle_read,
212         .write = jingle_write,
213         .exception = jingle_read,
214         .indicate = jingle_indicate,
215         .fixup = jingle_fixup,
216         .send_html = jingle_sendhtml,
217         .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER
218 };
219
220 static struct sockaddr_in bindaddr = { 0, };    /*!< The address we bind to */
221
222 static struct sched_context *sched;     /*!< The scheduling context */
223 static struct io_context *io;   /*!< The IO context */
224 static struct in_addr __ourip;
225
226
227 /*! \brief RTP driver interface */
228 static struct ast_rtp_protocol jingle_rtp = {
229         type: "jingle",
230         get_rtp_info: jingle_get_rtp_peer,
231         set_rtp_peer: jingle_set_rtp_peer,
232         get_codec: jingle_get_codec,
233 };
234
235 static char externip[16];
236
237 static struct jingle_container jingles;
238
239 static void jingle_member_destroy(struct jingle *obj)
240 {
241         ast_free(obj);
242 }
243
244 static struct jingle *find_jingle(char *name, char *connection)
245 {
246         struct jingle *jingle = NULL;
247
248         jingle = ASTOBJ_CONTAINER_FIND(&jingles, name);
249         if (!jingle && strchr(name, '@'))
250                 jingle = ASTOBJ_CONTAINER_FIND_FULL(&jingles, name, user,,, strcasecmp);
251
252         if (!jingle) {                          /* guest call */
253                 ASTOBJ_CONTAINER_TRAVERSE(&jingles, 1, {
254                         ASTOBJ_RDLOCK(iterator);
255                         if (!strcasecmp(iterator->name, "guest")) {
256                                 if (!strcasecmp(iterator->connection->jid->partial, connection)) {
257                                         jingle = iterator;
258                                 } else if (!strcasecmp(iterator->connection->name, connection)) {
259                                         jingle = iterator;
260                                 }
261                         }
262                         ASTOBJ_UNLOCK(iterator);
263
264                         if (jingle)
265                                 break;
266                 });
267
268         }
269         return jingle;
270 }
271
272
273 static void add_codec_to_answer(const struct jingle_pvt *p, int codec, iks *dcodecs)
274 {
275         char *format = ast_getformatname(codec);
276
277         if (!strcasecmp("ulaw", format)) {
278                 iks *payload_eg711u, *payload_pcmu;
279                 payload_pcmu = iks_new("payload-type");
280                 iks_insert_attrib(payload_pcmu, "id", "0");
281                 iks_insert_attrib(payload_pcmu, "name", "PCMU");
282                 iks_insert_attrib(payload_pcmu, "xmlns", "http://www.google.com/session/phone");
283                 payload_eg711u = iks_new("payload-type");
284                 iks_insert_attrib(payload_eg711u, "id", "100");
285                 iks_insert_attrib(payload_eg711u, "name", "EG711U");
286                 iks_insert_attrib(payload_eg711u, "xmlns", "http://www.google.com/session/phone");
287                 iks_insert_node(dcodecs, payload_pcmu);
288                 iks_insert_node(dcodecs, payload_eg711u);
289         }
290         if (!strcasecmp("alaw", format)) {
291                 iks *payload_eg711a;
292                 iks *payload_pcma = iks_new("payload-type");
293                 iks_insert_attrib(payload_pcma, "id", "8");
294                 iks_insert_attrib(payload_pcma, "name", "PCMA");
295                 iks_insert_attrib(payload_pcma, "xmlns", "http://www.google.com/session/phone");
296                 payload_eg711a = iks_new("payload-type");
297                 iks_insert_attrib(payload_eg711a, "id", "101");
298                 iks_insert_attrib(payload_eg711a, "name", "EG711A");
299                 iks_insert_attrib(payload_eg711a, "xmlns", "http://www.google.com/session/phone");
300                 iks_insert_node(dcodecs, payload_pcma);
301                 iks_insert_node(dcodecs, payload_eg711a);
302         }
303         if (!strcasecmp("ilbc", format)) {
304                 iks *payload_ilbc = iks_new("payload-type");
305                 iks_insert_attrib(payload_ilbc, "id", "97");
306                 iks_insert_attrib(payload_ilbc, "name", "iLBC");
307                 iks_insert_attrib(payload_ilbc, "xmlns", "http://www.google.com/session/phone");
308                 iks_insert_node(dcodecs, payload_ilbc);
309         }
310         if (!strcasecmp("g723", format)) {
311                 iks *payload_g723 = iks_new("payload-type");
312                 iks_insert_attrib(payload_g723, "id", "4");
313                 iks_insert_attrib(payload_g723, "name", "G723");
314                 iks_insert_attrib(payload_g723, "xmlns", "http://www.google.com/session/phone");
315                 iks_insert_node(dcodecs, payload_g723);
316         }
317         ast_rtp_lookup_code(p->rtp, 1, codec);
318 }
319
320 static int jingle_accept_call(struct jingle *client, struct jingle_pvt *p)
321 {
322         struct jingle_pvt *tmp = client->p;
323         struct aji_client *c = client->connection;
324         iks *iq, *jingle, *dcodecs, *payload_red, *payload_audio, *payload_cn;
325         int x;
326         int pref_codec = 0;
327         int alreadysent = 0;
328
329         if (p->initiator)
330                 return 1;
331
332         iq = iks_new("iq");
333         jingle = iks_new(GOOGLE_NODE);
334         dcodecs = iks_new("description");
335         if (iq && jingle && dcodecs) {
336                 iks_insert_attrib(dcodecs, "xmlns", "http://www.google.com/session/phone");
337
338                 for (x = 0; x < 32; x++) {
339                         if (!(pref_codec = ast_codec_pref_index(&client->prefs, x)))
340                                 break;
341                         if (!(client->capability & pref_codec))
342                                 continue;
343                         if (alreadysent & pref_codec)
344                                 continue;
345                         if (pref_codec <= AST_FORMAT_MAX_AUDIO)
346                                 add_codec_to_answer(p, pref_codec, dcodecs);
347                         else
348                                 add_codec_to_answer(p, pref_codec, dcodecs);
349                         alreadysent |= pref_codec;
350                 }
351                 payload_red = iks_new("payload-type");
352                 iks_insert_attrib(payload_red, "id", "117");
353                 iks_insert_attrib(payload_red, "name", "red");
354                 iks_insert_attrib(payload_red, "xmlns", "http://www.google.com/session/phone");
355                 payload_audio = iks_new("payload-type");
356                 iks_insert_attrib(payload_audio, "id", "106");
357                 iks_insert_attrib(payload_audio, "name", "audio/telephone-event");
358                 iks_insert_attrib(payload_audio, "xmlns", "http://www.google.com/session/phone");
359                 payload_cn = iks_new("payload-type");
360                 iks_insert_attrib(payload_cn, "id", "13");
361                 iks_insert_attrib(payload_cn, "name", "CN");
362                 iks_insert_attrib(payload_cn, "xmlns", "http://www.google.com/session/phone");
363
364
365                 iks_insert_attrib(iq, "type", "set");
366                 iks_insert_attrib(iq, "to", (p->from) ? p->from : client->user);
367                 iks_insert_attrib(iq, "id", client->connection->mid);
368                 ast_aji_increment_mid(client->connection->mid);
369
370                 iks_insert_attrib(jingle, "xmlns", GOOGLE_NS);
371                 iks_insert_attrib(jingle, "type", JINGLE_ACCEPT);
372                 iks_insert_attrib(jingle, "initiator",
373                                                   p->initiator ? client->connection->jid->full : p->from);
374                 iks_insert_attrib(jingle, GOOGLE_SID, tmp->sid);
375                 iks_insert_node(iq, jingle);
376                 iks_insert_node(jingle, dcodecs);
377                 iks_insert_node(dcodecs, payload_red);
378                 iks_insert_node(dcodecs, payload_audio);
379                 iks_insert_node(dcodecs, payload_cn);
380
381                 iks_send(c->p, iq);
382                 iks_delete(payload_red);
383                 iks_delete(payload_audio);
384                 iks_delete(payload_cn);
385                 iks_delete(dcodecs);
386                 iks_delete(jingle);
387                 iks_delete(iq);
388         }
389         return 1;
390 }
391
392 static int jingle_ringing_ack(void *data, ikspak *pak)
393 {
394         struct jingle_pvt *p = data;
395
396         if (p->ringrule)
397                 iks_filter_remove_rule(p->parent->connection->f, p->ringrule);
398         p->ringrule = NULL;
399         if (p->owner)
400                 ast_queue_control(p->owner, AST_CONTROL_RINGING);
401         return IKS_FILTER_EAT;
402 }
403
404 static int jingle_answer(struct ast_channel *ast)
405 {
406         struct jingle_pvt *p = ast->tech_pvt;
407         struct jingle *client = p->parent;
408         int res = 0;
409
410         ast_debug(1, "Answer!\n");
411         ast_mutex_lock(&p->lock);
412         jingle_accept_call(client, p);
413         ast_mutex_unlock(&p->lock);
414         return res;
415 }
416
417 static enum ast_rtp_get_result jingle_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
418 {
419         struct jingle_pvt *p = chan->tech_pvt;
420         enum ast_rtp_get_result res = AST_RTP_GET_FAILED;
421
422         if (!p)
423                 return res;
424
425         ast_mutex_lock(&p->lock);
426         if (p->rtp) {
427                 *rtp = p->rtp;
428                 res = AST_RTP_TRY_NATIVE;
429         }
430         ast_mutex_unlock(&p->lock);
431
432         return res;
433 }
434
435 static int jingle_get_codec(struct ast_channel *chan)
436 {
437         struct jingle_pvt *p = chan->tech_pvt;
438         return p->peercapability;
439 }
440
441 static int jingle_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, struct ast_rtp *tpeer, int codecs, int nat_active)
442 {
443         struct jingle_pvt *p;
444
445         p = chan->tech_pvt;
446         if (!p)
447                 return -1;
448         ast_mutex_lock(&p->lock);
449
450 /*      if (rtp)
451                 ast_rtp_get_peer(rtp, &p->redirip);
452         else
453                 memset(&p->redirip, 0, sizeof(p->redirip));
454         p->redircodecs = codecs; */
455
456         /* Reset lastrtprx timer */
457         ast_mutex_unlock(&p->lock);
458         return 0;
459 }
460
461 static int jingle_response(struct jingle *client, ikspak *pak, const char *reasonstr, const char *reasonstr2)
462 {
463         iks *response = NULL, *error = NULL, *reason = NULL;
464         int res = -1;
465
466         response = iks_new("iq");
467         if (response) {
468                 iks_insert_attrib(response, "type", "result");
469                 iks_insert_attrib(response, "from", client->connection->jid->full);
470                 iks_insert_attrib(response, "to", iks_find_attrib(pak->x, "from"));
471                 iks_insert_attrib(response, "id", iks_find_attrib(pak->x, "id"));
472                 if (reasonstr) {
473                         error = iks_new("error");
474                         if (error) {
475                                 iks_insert_attrib(error, "type", "cancel");
476                                 reason = iks_new(reasonstr);
477                                 if (reason)
478                                         iks_insert_node(error, reason);
479                                 iks_insert_node(response, error);
480                         }
481                 }
482                 iks_send(client->connection->p, response);
483                 if (reason)
484                         iks_delete(reason);
485                 if (error)
486                         iks_delete(error);
487                 iks_delete(response);
488                 res = 0;
489         }
490         return res;
491 }
492
493 static int jingle_is_answered(struct jingle *client, ikspak *pak)
494 {
495         struct jingle_pvt *tmp;
496
497         ast_debug(1, "The client is %s\n", client->name);
498         /* Make sure our new call doesn't exist yet */
499         for (tmp = client->p; tmp; tmp = tmp->next) {
500                 if (iks_find_with_attrib(pak->x, GOOGLE_NODE, GOOGLE_SID, tmp->sid))
501                         break;
502         }
503
504         if (tmp) {
505                 if (tmp->owner)
506                         ast_queue_control(tmp->owner, AST_CONTROL_ANSWER);
507         } else
508                 ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
509         jingle_response(client, pak, NULL, NULL);
510         return 1;
511 }
512
513 static int jingle_handle_dtmf(struct jingle *client, ikspak *pak)
514 {
515         struct jingle_pvt *tmp;
516         iks *dtmfnode = NULL, *dtmfchild = NULL;
517         char *dtmf;
518         /* Make sure our new call doesn't exist yet */
519         for (tmp = client->p; tmp; tmp = tmp->next) {
520                 if (iks_find_with_attrib(pak->x, GOOGLE_NODE, GOOGLE_SID, tmp->sid) || iks_find_with_attrib(pak->x, JINGLE_NODE, JINGLE_SID, tmp->sid))
521                         break;
522         }
523
524         if (tmp) {
525                 if(iks_find_with_attrib(pak->x, "dtmf-method", "method", "rtp")) {
526                         jingle_response(client,pak,
527                                         "feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
528                                         "unsupported-dtmf-method xmlns='http://www.xmpp.org/extensions/xep-0181.html#ns-errors'");
529                         return -1;
530                 }
531                 if ((dtmfnode = iks_find(pak->x, "dtmf"))) {
532                         if((dtmf = iks_find_attrib(dtmfnode, "code"))) {
533                                 if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-up")) {
534                                         struct ast_frame f = {AST_FRAME_DTMF_BEGIN, };
535                                         f.subclass = dtmf[0];
536                                         ast_queue_frame(tmp->owner, &f);
537                                         ast_verbose("JINGLE! DTMF-relay event received: %c\n", f.subclass);
538                                 } else if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-down")) {
539                                         struct ast_frame f = {AST_FRAME_DTMF_END, };
540                                         f.subclass = dtmf[0];
541                                         ast_queue_frame(tmp->owner, &f);
542                                         ast_verbose("JINGLE! DTMF-relay event received: %c\n", f.subclass);
543                                 } else if(iks_find_attrib(pak->x, "dtmf")) { /* 250 millasecond default */
544                                         struct ast_frame f = {AST_FRAME_DTMF, };
545                                         f.subclass = dtmf[0];
546                                         ast_queue_frame(tmp->owner, &f);
547                                         ast_verbose("JINGLE! DTMF-relay event received: %c\n", f.subclass);
548                                 }
549                         }
550                 } else if ((dtmfnode = iks_find_with_attrib(pak->x, JINGLE_NODE, "action", "session-info"))) {
551                         if((dtmfchild = iks_find(dtmfnode, "dtmf"))) {
552                                 if((dtmf = iks_find_attrib(dtmfchild, "code"))) {
553                                         if(iks_find_with_attrib(dtmfnode, "dtmf", "action", "button-up")) {
554                                                 struct ast_frame f = {AST_FRAME_DTMF_END, };
555                                                 f.subclass = dtmf[0];
556                                                 ast_queue_frame(tmp->owner, &f);
557                                                 ast_verbose("JINGLE! DTMF-relay event received: %c\n", f.subclass);
558                                         } else if(iks_find_with_attrib(dtmfnode, "dtmf", "action", "button-down")) {
559                                                 struct ast_frame f = {AST_FRAME_DTMF_BEGIN, };
560                                                 f.subclass = dtmf[0];
561                                                 ast_queue_frame(tmp->owner, &f);
562                                                 ast_verbose("JINGLE! DTMF-relay event received: %c\n", f.subclass);
563                                         }
564                                 }
565                         }
566                 }
567                 jingle_response(client, pak, NULL, NULL);
568                 return 1;
569         } else
570                 ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
571
572         jingle_response(client, pak, NULL, NULL);
573         return 1;
574 }
575
576
577 static int jingle_hangup_farend(struct jingle *client, ikspak *pak)
578 {
579         struct jingle_pvt *tmp;
580
581         ast_debug(1, "The client is %s\n", client->name);
582         /* Make sure our new call doesn't exist yet */
583         for (tmp = client->p; tmp; tmp = tmp->next) {
584                 if (iks_find_with_attrib(pak->x, GOOGLE_NODE, GOOGLE_SID, tmp->sid))
585                         break;
586         }
587
588         if (tmp) {
589                 tmp->alreadygone = 1;
590                 if (tmp->owner)
591                         ast_queue_hangup(tmp->owner);
592         } else
593                 ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
594         jingle_response(client, pak, NULL, NULL);
595         return 1;
596 }
597
598 static int jingle_create_candidates(struct jingle *client, struct jingle_pvt *p, char *sid, char *from)
599 {
600         struct jingle_candidate *tmp;
601         struct aji_client *c = client->connection;
602         struct jingle_candidate *ours1 = NULL, *ours2 = NULL;
603         struct sockaddr_in sin;
604         struct sockaddr_in dest;
605         struct in_addr us;
606         iks *iq, *jingle, *candidate;
607         char user[17], pass[17], preference[5], port[7];
608
609
610         iq = iks_new("iq");
611         jingle = iks_new(GOOGLE_NODE);
612         candidate = iks_new("candidate");
613         if (!iq || !jingle || !candidate) {
614                 ast_log(LOG_ERROR, "Memory allocation error\n");
615                 goto safeout;
616         }
617         ours1 = ast_calloc(1, sizeof(*ours1));
618         ours2 = ast_calloc(1, sizeof(*ours2));
619         if (!ours1 || !ours2)
620                 goto safeout;
621         iks_insert_node(iq, jingle);
622         iks_insert_node(jingle, candidate);
623
624         for (; p; p = p->next) {
625                 if (!strcasecmp(p->sid, sid))
626                         break;
627         }
628
629         if (!p) {
630                 ast_log(LOG_NOTICE, "No matching jingle session - SID %s!\n", sid);
631                 goto safeout;
632         }
633
634         ast_rtp_get_us(p->rtp, &sin);
635         ast_find_ourip(&us, bindaddr);
636
637         /* Setup our jingle candidates */
638         ast_copy_string(ours1->name, "rtp", sizeof(ours1->name));
639         ours1->port = ntohs(sin.sin_port);
640         ours1->preference = 1;
641         snprintf(user, sizeof(user), "%08lx%08lx", ast_random(), ast_random());
642         snprintf(pass, sizeof(pass), "%08lx%08lx", ast_random(), ast_random());
643         ast_copy_string(ours1->username, user, sizeof(ours1->username));
644         ast_copy_string(ours1->password, pass, sizeof(ours1->password));
645         ast_copy_string(ours1->ip, ast_inet_ntoa(us), sizeof(ours1->ip));
646         ours1->protocol = AJI_PROTOCOL_UDP;
647         ours1->type = AJI_CONNECT_LOCAL;
648         ours1->generation = 0;
649         p->ourcandidates = ours1;
650
651         if (!ast_strlen_zero(externip)) {
652                 /* XXX We should really stun for this one not just go with externip XXX */
653                 snprintf(user, sizeof(user), "%08lx%08lx", ast_random(), ast_random());
654                 snprintf(pass, sizeof(pass), "%08lx%08lx", ast_random(), ast_random());
655                 ast_copy_string(ours2->username, user, sizeof(ours2->username));
656                 ast_copy_string(ours2->password, pass, sizeof(ours2->password));
657                 ast_copy_string(ours2->ip, externip, sizeof(ours2->ip));
658                 ast_copy_string(ours2->name, "rtp", sizeof(ours1->name));
659                 ours2->port = ntohs(sin.sin_port);
660                 ours2->preference = 0.9;
661                 ours2->protocol = AJI_PROTOCOL_UDP;
662                 ours2->type = AJI_CONNECT_STUN;
663                 ours2->generation = 0;
664                 ours1->next = ours2;
665                 ours2 = NULL;
666         }
667         ours1 = NULL;
668         dest.sin_addr = __ourip;
669         dest.sin_port = sin.sin_port;
670
671
672         for (tmp = p->ourcandidates; tmp; tmp = tmp->next) {
673                 snprintf(port, sizeof(port), "%d", tmp->port);
674                 snprintf(preference, sizeof(preference), "%.2f", tmp->preference);
675                 iks_insert_attrib(iq, "from", c->jid->full);
676                 iks_insert_attrib(iq, "to", from);
677                 iks_insert_attrib(iq, "type", "set");
678                 iks_insert_attrib(iq, "id", c->mid);
679                 ast_aji_increment_mid(c->mid);
680                 iks_insert_attrib(jingle, "type", "candidates");
681                 iks_insert_attrib(jingle, "id", sid);
682                 iks_insert_attrib(jingle, "initiator", (p->initiator) ? c->jid->full : from);
683                 iks_insert_attrib(jingle, "xmlns", GOOGLE_NS);
684                 iks_insert_attrib(candidate, "name", tmp->name);
685                 iks_insert_attrib(candidate, "address", tmp->ip);
686                 iks_insert_attrib(candidate, "port", port);
687                 iks_insert_attrib(candidate, "username", tmp->username);
688                 iks_insert_attrib(candidate, "password", tmp->password);
689                 iks_insert_attrib(candidate, "preference", preference);
690                 if (tmp->protocol == AJI_PROTOCOL_UDP)
691                         iks_insert_attrib(candidate, "protocol", "udp");
692                 if (tmp->protocol == AJI_PROTOCOL_SSLTCP)
693                         iks_insert_attrib(candidate, "protocol", "ssltcp");
694                 if (tmp->type == AJI_CONNECT_STUN)
695                         iks_insert_attrib(candidate, "type", "stun");
696                 if (tmp->type == AJI_CONNECT_LOCAL)
697                         iks_insert_attrib(candidate, "type", "local");
698                 if (tmp->type == AJI_CONNECT_RELAY)
699                         iks_insert_attrib(candidate, "type", "relay");
700                 iks_insert_attrib(candidate, "network", "0");
701                 iks_insert_attrib(candidate, "generation", "0");
702                 iks_send(c->p, iq);
703         }
704         p->laststun = 0;
705
706 safeout:
707         if (ours1)
708                 ast_free(ours1);
709         if (ours2)
710                 ast_free(ours2);
711         if (iq)
712                 iks_delete(iq);
713         if (jingle)
714                 iks_delete(jingle);
715         if (candidate)
716                 iks_delete(candidate);
717         return 1;
718 }
719
720 static struct jingle_pvt *jingle_alloc(struct jingle *client, const char *from, const char *sid)
721 {
722         struct jingle_pvt *tmp = NULL;
723         struct aji_resource *resources = NULL;
724         struct aji_buddy *buddy;
725         char idroster[200];
726
727         ast_debug(1, "The client is %s for alloc\n", client->name);
728         if (!sid && !strchr(from, '/')) {       /* I started call! */
729                 if (!strcasecmp(client->name, "guest")) {
730                         buddy = ASTOBJ_CONTAINER_FIND(&client->connection->buddies, from);
731                         if (buddy)
732                                 resources = buddy->resources;
733                 } else if (client->buddy)
734                         resources = client->buddy->resources;
735                 while (resources) {
736                         if (resources->cap->jingle) {
737                                 break;
738                         }
739                         resources = resources->next;
740                 }
741                 if (resources)
742                         snprintf(idroster, sizeof(idroster), "%s/%s", from, resources->resource);
743                 else {
744                         ast_log(LOG_ERROR, "no jingle capable clients to talk to.\n");
745                         return NULL;
746                 }
747         }
748         if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
749                 return NULL;
750         }
751
752         memcpy(&tmp->prefs, &client->prefs, sizeof(tmp->prefs));
753
754         if (sid) {
755                 ast_copy_string(tmp->sid, sid, sizeof(tmp->sid));
756                 ast_copy_string(tmp->from, from, sizeof(tmp->from));
757         } else {
758                 snprintf(tmp->sid, sizeof(tmp->sid), "%08lx%08lx", ast_random(), ast_random());
759                 ast_copy_string(tmp->from, idroster, sizeof(tmp->from));
760                 tmp->initiator = 1;
761         }
762         tmp->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
763         tmp->parent = client;
764         if (!tmp->rtp) {
765                 ast_log(LOG_WARNING, "Out of RTP sessions?\n");
766                 ast_free(tmp);
767                 return NULL;
768         }
769         ast_copy_string(tmp->exten, "s", sizeof(tmp->exten));
770         ast_mutex_init(&tmp->lock);
771         ast_mutex_lock(&jinglelock);
772         tmp->next = client->p;
773         client->p = tmp;
774         ast_mutex_unlock(&jinglelock);
775         return tmp;
776 }
777
778 /*! \brief Start new jingle channel */
779 static struct ast_channel *jingle_new(struct jingle *client, struct jingle_pvt *i, int state, const char *title)
780 {
781         struct ast_channel *tmp;
782         int fmt;
783         int what;
784         const char *str;
785
786         if (title)
787                 str = title;
788         else
789                 str = i->from;
790         tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, "", "", "", 0, "Jingle/%s-%04lx", str, ast_random() & 0xffff);
791         if (!tmp) {
792                 ast_log(LOG_WARNING, "Unable to allocate Jingle channel structure!\n");
793                 return NULL;
794         }
795         tmp->tech = &jingle_tech;
796
797         /* Select our native format based on codec preference until we receive
798            something from another device to the contrary. */
799         if (i->jointcapability)
800                 what = i->jointcapability;
801         else if (i->capability)
802                 what = i->capability;
803         else
804                 what = global_capability;
805
806         /* Set Frame packetization */
807         if (i->rtp)
808                 ast_rtp_codec_setpref(i->rtp, &i->prefs);
809
810         tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | (i->jointcapability & AST_FORMAT_VIDEO_MASK);
811         fmt = ast_best_codec(tmp->nativeformats);
812
813         if (i->rtp) {
814                 ast_channel_set_fd(tmp, 0, ast_rtp_fd(i->rtp));
815                 ast_channel_set_fd(tmp, 1, ast_rtcp_fd(i->rtp));
816         }
817         if (i->vrtp) {
818                 ast_channel_set_fd(tmp, 2, ast_rtp_fd(i->vrtp));
819                 ast_channel_set_fd(tmp, 3, ast_rtcp_fd(i->vrtp));
820         }
821         if (state == AST_STATE_RING)
822                 tmp->rings = 1;
823         tmp->adsicpe = AST_ADSI_UNAVAILABLE;
824         tmp->writeformat = fmt;
825         tmp->rawwriteformat = fmt;
826         tmp->readformat = fmt;
827         tmp->rawreadformat = fmt;
828         tmp->tech_pvt = i;
829
830         tmp->callgroup = client->callgroup;
831         tmp->pickupgroup = client->pickupgroup;
832         tmp->cid.cid_pres = client->callingpres;
833         if (!ast_strlen_zero(client->accountcode))
834                 ast_string_field_set(tmp, accountcode, client->accountcode);
835         if (client->amaflags)
836                 tmp->amaflags = client->amaflags;
837         if (!ast_strlen_zero(client->language))
838                 ast_string_field_set(tmp, language, client->language);
839         if (!ast_strlen_zero(client->musicclass))
840                 ast_string_field_set(tmp, musicclass, client->musicclass);
841         i->owner = tmp;
842         ast_copy_string(tmp->context, client->context, sizeof(tmp->context));
843         ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
844         /* Don't use ast_set_callerid() here because it will
845          * generate an unnecessary NewCallerID event  */
846         tmp->cid.cid_ani = ast_strdup(i->cid_num);
847         if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
848                 tmp->cid.cid_dnid = ast_strdup(i->exten);
849         tmp->priority = 1;
850         if (i->rtp)
851                 ast_jb_configure(tmp, &global_jbconf);
852         if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
853                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
854                 tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
855                 ast_hangup(tmp);
856                 tmp = NULL;
857         }
858
859         return tmp;
860 }
861
862 static int jingle_action(struct jingle *client, struct jingle_pvt *p, const char *action)
863 {
864         iks *request, *session = NULL;
865         int res = -1;
866
867         request = iks_new("iq");
868         if (request) {
869                 iks_insert_attrib(request, "type", "set");
870                 iks_insert_attrib(request, "from", client->connection->jid->full);
871                 iks_insert_attrib(request, "to", p->from);
872                 iks_insert_attrib(request, "id", client->connection->mid);
873                 ast_aji_increment_mid(client->connection->mid);
874                 session = iks_new("session");
875                 if (session) {
876                         iks_insert_attrib(session, "type", action);
877                         iks_insert_attrib(session, "id", p->sid);
878                         iks_insert_attrib(session, "initiator",
879                                                           p->initiator ? client->connection->jid->full : p->from);
880                         iks_insert_attrib(session, "xmlns", GOOGLE_NS);
881                         iks_insert_node(request, session);
882                         iks_send(client->connection->p, request);
883                         iks_delete(session);
884                         res = 0;
885                 }
886                 iks_delete(request);
887         }
888         return res;
889 }
890
891 static void jingle_free_candidates(struct jingle_candidate *candidate)
892 {
893         struct jingle_candidate *last;
894         while (candidate) {
895                 last = candidate;
896                 candidate = candidate->next;
897                 ast_free(last);
898         }
899 }
900
901 static void jingle_free_pvt(struct jingle *client, struct jingle_pvt *p)
902 {
903         struct jingle_pvt *cur, *prev = NULL;
904         cur = client->p;
905         while (cur) {
906                 if (cur == p) {
907                         if (prev)
908                                 prev->next = p->next;
909                         else
910                                 client->p = p->next;
911                         break;
912                 }
913                 prev = cur;
914                 cur = cur->next;
915         }
916         if (p->ringrule)
917                 iks_filter_remove_rule(p->parent->connection->f, p->ringrule);
918         if (p->owner)
919                 ast_log(LOG_WARNING, "Uh oh, there's an owner, this is going to be messy.\n");
920         if (p->rtp)
921                 ast_rtp_destroy(p->rtp);
922         if (p->vrtp)
923                 ast_rtp_destroy(p->vrtp);
924         jingle_free_candidates(p->theircandidates);
925         ast_free(p);
926 }
927
928
929 static int jingle_newcall(struct jingle *client, ikspak *pak)
930 {
931         struct jingle_pvt *p, *tmp = client->p;
932         struct ast_channel *chan;
933         int res;
934         iks *codec;
935
936         /* Make sure our new call doesn't exist yet */
937         while (tmp) {
938                 if (iks_find_with_attrib(pak->x, GOOGLE_NODE, GOOGLE_SID, tmp->sid)) {
939                         ast_log(LOG_NOTICE, "Ignoring duplicate call setup on SID %s\n", tmp->sid);
940                         jingle_response(client, pak, "out-of-order", NULL);
941                         return -1;
942                 }
943                 tmp = tmp->next;
944         }
945
946         p = jingle_alloc(client, pak->from->partial, iks_find_attrib(pak->query, GOOGLE_SID));
947         if (!p) {
948                 ast_log(LOG_WARNING, "Unable to allocate jingle structure!\n");
949                 return -1;
950         }
951         chan = jingle_new(client, p, AST_STATE_DOWN, pak->from->user);
952         if (chan) {
953                 ast_mutex_lock(&p->lock);
954                 ast_copy_string(p->from, pak->from->full, sizeof(p->from));
955                 if (iks_find_attrib(pak->query, GOOGLE_SID)) {
956                         ast_copy_string(p->sid, iks_find_attrib(pak->query, GOOGLE_SID),
957                                                         sizeof(p->sid));
958                 }
959
960                 codec = iks_child(iks_child(iks_child(pak->x)));
961                 while (codec) {
962                         ast_rtp_set_m_type(p->rtp, atoi(iks_find_attrib(codec, "id")));
963                         ast_rtp_set_rtpmap_type(p->rtp, atoi(iks_find_attrib(codec, "id")), "audio",
964                                                 iks_find_attrib(codec, "name"), 0);
965                         codec = iks_next(codec);
966                 }
967                 
968                 ast_mutex_unlock(&p->lock);
969                 ast_setstate(chan, AST_STATE_RING);
970                 res = ast_pbx_start(chan);
971
972                 switch (res) {
973                 case AST_PBX_FAILED:
974                         ast_log(LOG_WARNING, "Failed to start PBX :(\n");
975                         jingle_response(client, pak, "service-unavailable", NULL);
976                         break;
977                 case AST_PBX_CALL_LIMIT:
978                         ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
979                         jingle_response(client, pak, "service-unavailable", NULL);
980                         break;
981                 case AST_PBX_SUCCESS:
982                         jingle_response(client, pak, NULL, NULL);
983                         jingle_create_candidates(client, p,
984                                         iks_find_attrib(pak->query, GOOGLE_SID),
985                                         iks_find_attrib(pak->x, "from"));
986                         /* nothing to do */
987                         break;
988                 }
989         } else {
990                 jingle_free_pvt(client, p);
991         }
992         return 1;
993 }
994
995 static int jingle_update_stun(struct jingle *client, struct jingle_pvt *p)
996 {
997         struct jingle_candidate *tmp;
998         struct hostent *hp;
999         struct ast_hostent ahp;
1000         struct sockaddr_in sin;
1001
1002         if (time(NULL) == p->laststun)
1003                 return 0;
1004
1005         tmp = p->theircandidates;
1006         p->laststun = time(NULL);
1007         while (tmp) {
1008                 char username[256];
1009                 hp = ast_gethostbyname(tmp->ip, &ahp);
1010                 sin.sin_family = AF_INET;
1011                 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
1012                 sin.sin_port = htons(tmp->port);
1013                 snprintf(username, sizeof(username), "%s%s", tmp->username,
1014                                  p->ourcandidates->username);
1015
1016                 ast_rtp_stun_request(p->rtp, &sin, username);
1017                 tmp = tmp->next;
1018         }
1019         return 1;
1020 }
1021
1022 static int jingle_add_candidate(struct jingle *client, ikspak *pak)
1023 {
1024         struct jingle_pvt *p = NULL, *tmp = NULL;
1025         struct aji_client *c = client->connection;
1026         struct jingle_candidate *newcandidate = NULL;
1027         iks *traversenodes = NULL, *receipt = NULL;
1028         newcandidate = ast_calloc(1, sizeof(*newcandidate));
1029         if (!newcandidate)
1030                 return 0;
1031         for (tmp = client->p; tmp; tmp = tmp->next) {
1032                 if (iks_find_with_attrib(pak->x, GOOGLE_NODE, GOOGLE_SID, tmp->sid)) {
1033                         p = tmp;
1034                         break;
1035                 }
1036         }
1037
1038         if (!p)
1039                 return -1;
1040
1041         traversenodes = pak->query;
1042         while(traversenodes) {
1043                 if(!strcasecmp(iks_name(traversenodes), "session")) {
1044                         traversenodes = iks_child(traversenodes);
1045                         continue;
1046                 }
1047                 if(!strcasecmp(iks_name(traversenodes), "candidate")) {
1048                         newcandidate = ast_calloc(1, sizeof(*newcandidate));
1049                         if (!newcandidate)
1050                                 return 0;
1051                         ast_copy_string(newcandidate->name, iks_find_attrib(traversenodes, "name"),
1052                                                         sizeof(newcandidate->name));
1053                         ast_copy_string(newcandidate->ip, iks_find_attrib(traversenodes, "address"),
1054                                                         sizeof(newcandidate->ip));
1055                         newcandidate->port = atoi(iks_find_attrib(traversenodes, "port"));
1056                         ast_copy_string(newcandidate->username, iks_find_attrib(traversenodes, "username"),
1057                                                         sizeof(newcandidate->username));
1058                         ast_copy_string(newcandidate->password, iks_find_attrib(traversenodes, "password"),
1059                                                         sizeof(newcandidate->password));
1060                         newcandidate->preference = atof(iks_find_attrib(traversenodes, "preference"));
1061                         if (!strcasecmp(iks_find_attrib(traversenodes, "protocol"), "udp"))
1062                                 newcandidate->protocol = AJI_PROTOCOL_UDP;
1063                         if (!strcasecmp(iks_find_attrib(traversenodes, "protocol"), "ssltcp"))
1064                                 newcandidate->protocol = AJI_PROTOCOL_SSLTCP;
1065                 
1066                         if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "stun"))
1067                                 newcandidate->type = AJI_CONNECT_STUN;
1068                         if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "local"))
1069                                 newcandidate->type = AJI_CONNECT_LOCAL;
1070                         if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "relay"))
1071                                 newcandidate->type = AJI_CONNECT_RELAY;
1072                         ast_copy_string(newcandidate->network, iks_find_attrib(traversenodes, "network"),
1073                                                         sizeof(newcandidate->network));
1074                         newcandidate->generation = atoi(iks_find_attrib(traversenodes, "generation"));
1075                         newcandidate->next = NULL;
1076                 
1077                         newcandidate->next = p->theircandidates;
1078                         p->theircandidates = newcandidate;
1079                         p->laststun = 0;
1080                         jingle_update_stun(p->parent, p);
1081                         newcandidate = NULL;
1082                 }
1083                 traversenodes = iks_next(traversenodes);
1084         }
1085         
1086         receipt = iks_new("iq");
1087         iks_insert_attrib(receipt, "type", "result");
1088         iks_insert_attrib(receipt, "from", c->jid->full);
1089         iks_insert_attrib(receipt, "to", iks_find_attrib(pak->x, "from"));
1090         iks_insert_attrib(receipt, "id", iks_find_attrib(pak->x, "id"));
1091         iks_send(c->p, receipt);
1092         iks_delete(receipt);
1093
1094         return 1;
1095 }
1096
1097 static struct ast_frame *jingle_rtp_read(struct ast_channel *ast, struct jingle_pvt *p)
1098 {
1099         struct ast_frame *f;
1100
1101         if (!p->rtp)
1102                 return &ast_null_frame;
1103         f = ast_rtp_read(p->rtp);
1104         jingle_update_stun(p->parent, p);
1105         if (p->owner) {
1106                 /* We already hold the channel lock */
1107                 if (f->frametype == AST_FRAME_VOICE) {
1108                         if (f->subclass != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
1109                                 ast_debug(1, "Oooh, format changed to %d\n", f->subclass);
1110                                 p->owner->nativeformats =
1111                                         (p->owner->nativeformats & AST_FORMAT_VIDEO_MASK) | f->subclass;
1112                                 ast_set_read_format(p->owner, p->owner->readformat);
1113                                 ast_set_write_format(p->owner, p->owner->writeformat);
1114                         }
1115 /*                      if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
1116                                 f = ast_dsp_process(p->owner, p->vad, f);
1117                                 if (f && (f->frametype == AST_FRAME_DTMF))
1118                                         ast_debug(1, "* Detected inband DTMF '%c'\n", f->subclass);
1119                         } */
1120                 }
1121         }
1122         return f;
1123 }
1124
1125 static struct ast_frame *jingle_read(struct ast_channel *ast)
1126 {
1127         struct ast_frame *fr;
1128         struct jingle_pvt *p = ast->tech_pvt;
1129
1130         ast_mutex_lock(&p->lock);
1131         fr = jingle_rtp_read(ast, p);
1132         ast_mutex_unlock(&p->lock);
1133         return fr;
1134 }
1135
1136 /*! \brief Send frame to media channel (rtp) */
1137 static int jingle_write(struct ast_channel *ast, struct ast_frame *frame)
1138 {
1139         struct jingle_pvt *p = ast->tech_pvt;
1140         int res = 0;
1141
1142         switch (frame->frametype) {
1143         case AST_FRAME_VOICE:
1144                 if (!(frame->subclass & ast->nativeformats)) {
1145                         ast_log(LOG_WARNING,
1146                                         "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
1147                                         frame->subclass, ast->nativeformats, ast->readformat,
1148                                         ast->writeformat);
1149                         return 0;
1150                 }
1151                 if (p) {
1152                         ast_mutex_lock(&p->lock);
1153                         if (p->rtp) {
1154                                 res = ast_rtp_write(p->rtp, frame);
1155                         }
1156                         ast_mutex_unlock(&p->lock);
1157                 }
1158                 break;
1159         case AST_FRAME_VIDEO:
1160                 if (p) {
1161                         ast_mutex_lock(&p->lock);
1162                         if (p->vrtp) {
1163                                 res = ast_rtp_write(p->vrtp, frame);
1164                         }
1165                         ast_mutex_unlock(&p->lock);
1166                 }
1167                 break;
1168         case AST_FRAME_IMAGE:
1169                 return 0;
1170                 break;
1171         default:
1172                 ast_log(LOG_WARNING, "Can't send %d type frames with Jingle write\n",
1173                                 frame->frametype);
1174                 return 0;
1175         }
1176
1177         return res;
1178 }
1179
1180 static int jingle_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1181 {
1182         struct jingle_pvt *p = newchan->tech_pvt;
1183         ast_mutex_lock(&p->lock);
1184
1185         if ((p->owner != oldchan)) {
1186                 ast_mutex_unlock(&p->lock);
1187                 return -1;
1188         }
1189         if (p->owner == oldchan)
1190                 p->owner = newchan;
1191         ast_mutex_unlock(&p->lock);
1192         return 0;
1193 }
1194
1195 static int jingle_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
1196 {
1197         int res = 0;
1198
1199         switch (condition) {
1200         case AST_CONTROL_HOLD:
1201                 ast_moh_start(ast, data, NULL);
1202                 break;
1203         case AST_CONTROL_UNHOLD:
1204                 ast_moh_stop(ast);
1205                 break;
1206         default:
1207                 ast_log(LOG_NOTICE, "Don't know how to indicate condition '%d'\n", condition);
1208                 res = -1;
1209         }
1210
1211         return res;
1212 }
1213
1214 static int jingle_digit(struct ast_channel *ast, char digit, unsigned int duration)
1215 {
1216         struct jingle_pvt *p = ast->tech_pvt;
1217         struct jingle *client = p->parent;
1218         iks *iq, *jingle, *dtmf;
1219         char buffer[2] = {digit, '\0'};
1220         iq = iks_new("iq");
1221         jingle = iks_new("jingle");
1222         dtmf = iks_new("dtmf");
1223         if(!iq || !jingle || !dtmf) {
1224                 if(iq)
1225                         iks_delete(iq);
1226                 if(jingle)
1227                         iks_delete(jingle);
1228                 if(dtmf)
1229                         iks_delete(dtmf);
1230                 ast_log(LOG_ERROR, "Did not send dtmf do to memory issue\n");
1231                 return -1;
1232         }
1233
1234         iks_insert_attrib(iq, "type", "set");
1235         iks_insert_attrib(iq, "to", p->from);
1236         iks_insert_attrib(iq, "from", client->connection->jid->full);
1237         iks_insert_attrib(iq, "id", client->connection->mid);
1238         ast_aji_increment_mid(client->connection->mid);
1239         iks_insert_attrib(jingle, "xmlns", JINGLE_NS);
1240         iks_insert_attrib(jingle, "action", "session-info");
1241         iks_insert_attrib(jingle, "initiator", p->initiator ? client->connection->jid->full : p->from);
1242         iks_insert_attrib(jingle, "sid", p->sid);
1243         iks_insert_attrib(dtmf, "xmlns", JINGLE_DTMF_NS);
1244         iks_insert_attrib(dtmf, "code", buffer);
1245         iks_insert_node(iq, jingle);
1246         iks_insert_node(jingle, dtmf);
1247
1248         ast_mutex_lock(&p->lock);
1249         if (ast->dtmff.frametype == AST_FRAME_DTMF_BEGIN || duration == 0) {
1250                 iks_insert_attrib(dtmf, "action", "button-down");
1251         } else if (ast->dtmff.frametype == AST_FRAME_DTMF_END || duration != 0) {
1252                 iks_insert_attrib(dtmf, "action", "button-up");
1253         }
1254         iks_send(client->connection->p, iq);
1255         iks_delete(iq);
1256         iks_delete(jingle);
1257         iks_delete(dtmf);
1258         ast_mutex_unlock(&p->lock);
1259         return 0;
1260 }
1261
1262 static int jingle_digit_begin(struct ast_channel *chan, char digit)
1263 {
1264         return jingle_digit(chan, digit, 0);
1265 }
1266
1267 static int jingle_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
1268 {
1269         return jingle_digit(ast, digit, duration);
1270 }
1271
1272 static int jingle_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
1273 {
1274         ast_log(LOG_NOTICE, "XXX Implement jingle sendhtml XXX\n");
1275
1276         return -1;
1277 }
1278 static int jingle_transmit_invite(struct jingle_pvt *p)
1279 {
1280         struct jingle *jingle = NULL;
1281         struct aji_client *client = NULL;
1282         iks *iq, *desc, *session;
1283         iks *payload_eg711u, *payload_pcmu;
1284
1285         jingle = p->parent;
1286         client = jingle->connection;
1287         iq = iks_new("iq");
1288         desc = iks_new("description");
1289         session = iks_new("session");
1290         iks_insert_attrib(iq, "type", "set");
1291         iks_insert_attrib(iq, "to", p->from);
1292         iks_insert_attrib(iq, "from", client->jid->full);
1293         iks_insert_attrib(iq, "id", client->mid);
1294         ast_aji_increment_mid(client->mid);
1295         iks_insert_attrib(session, "type", "initiate");
1296         iks_insert_attrib(session, "id", p->sid);
1297         iks_insert_attrib(session, "initiator", client->jid->full);
1298         iks_insert_attrib(session, "xmlns", GOOGLE_NS);
1299         iks_insert_attrib(desc, "xmlns", "http://www.google.com/session/phone");
1300         payload_pcmu = iks_new("payload-type");
1301         iks_insert_attrib(payload_pcmu, "id", "0");
1302         iks_insert_attrib(payload_pcmu, "name", "PCMU");
1303         payload_eg711u = iks_new("payload-type");
1304         iks_insert_attrib(payload_eg711u, "id", "100");
1305         iks_insert_attrib(payload_eg711u, "name", "EG711U");
1306         iks_insert_node(desc, payload_pcmu);
1307         iks_insert_node(desc, payload_eg711u);
1308         iks_insert_node(iq, session);
1309         iks_insert_node(session, desc);
1310         iks_send(client->p, iq);
1311         iks_delete(iq);
1312         iks_delete(desc);
1313         iks_delete(session);
1314         iks_delete(payload_eg711u);
1315         iks_delete(payload_pcmu);
1316         return 0;
1317 }
1318
1319 /* Not in use right now.
1320 static int jingle_auto_congest(void *nothing)
1321 {
1322         struct jingle_pvt *p = nothing;
1323
1324         ast_mutex_lock(&p->lock);
1325         if (p->owner) {
1326                 if (!ast_channel_trylock(p->owner)) {
1327                         ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
1328                         ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
1329                         ast_channel_unlock(p->owner);
1330                 }
1331         }
1332         ast_mutex_unlock(&p->lock);
1333         return 0;
1334 }
1335 */
1336
1337 /*! \brief Initiate new call, part of PBX interface 
1338  *      dest is the dial string */
1339 static int jingle_call(struct ast_channel *ast, char *dest, int timeout)
1340 {
1341         struct jingle_pvt *p = ast->tech_pvt;
1342
1343         if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1344                 ast_log(LOG_WARNING, "jingle_call called on %s, neither down nor reserved\n", ast->name);
1345                 return -1;
1346         }
1347
1348         ast_setstate(ast, AST_STATE_RING);
1349         p->jointcapability = p->capability;
1350         if (!p->ringrule) {
1351                 ast_copy_string(p->ring, p->parent->connection->mid, sizeof(p->ring));
1352                 p->ringrule = iks_filter_add_rule(p->parent->connection->f, jingle_ringing_ack, p,
1353                                                         IKS_RULE_ID, p->ring, IKS_RULE_DONE);
1354         } else
1355                 ast_log(LOG_WARNING, "Whoa, already have a ring rule!\n");
1356
1357         jingle_transmit_invite(p);
1358         jingle_create_candidates(p->parent, p, p->sid, p->from);
1359
1360         return 0;
1361 }
1362
1363 /*! \brief Hangup a call through the jingle proxy channel */
1364 static int jingle_hangup(struct ast_channel *ast)
1365 {
1366         struct jingle_pvt *p = ast->tech_pvt;
1367         struct jingle *client;
1368
1369         ast_mutex_lock(&p->lock);
1370         client = p->parent;
1371         p->owner = NULL;
1372         ast->tech_pvt = NULL;
1373         if (!p->alreadygone)
1374                 jingle_action(client, p, "terminate");
1375         ast_mutex_unlock(&p->lock);
1376
1377         jingle_free_pvt(client, p);
1378
1379         return 0;
1380 }
1381
1382 /*! \brief Part of PBX interface */
1383 static struct ast_channel *jingle_request(const char *type, int format, void *data, int *cause)
1384 {
1385         struct jingle_pvt *p = NULL;
1386         struct jingle *client = NULL;
1387         char *sender = NULL, *to = NULL, *s = NULL;
1388         struct ast_channel *chan = NULL;
1389
1390         if (data) {
1391                 s = ast_strdupa(data);
1392                 if (s) {
1393                         sender = strsep(&s, "/");
1394                         if (sender && (sender[0] != '\0'))
1395                                 to = strsep(&s, "/");
1396                         if (!to) {
1397                                 ast_log(LOG_ERROR, "Bad arguments in Jingle Dialstring: %s\n", (char*) data);
1398                                 return NULL;
1399                         }
1400                 }
1401         }
1402         client = find_jingle(to, sender);
1403         if (!client) {
1404                 ast_log(LOG_WARNING, "Could not find recipient.\n");
1405                 return NULL;
1406         }
1407         ASTOBJ_WRLOCK(client);
1408         p = jingle_alloc(client, to, NULL);
1409         if (p)
1410                 chan = jingle_new(client, p, AST_STATE_DOWN, to);
1411         ASTOBJ_UNLOCK(client);
1412
1413         return chan;
1414 }
1415
1416 #if 0
1417 /*! \brief CLI command "jingle show channels" */
1418 static int jingle_show(int fd, int argc, char **argv)
1419 {
1420         struct jingle_pvt *p = NULL;
1421         struct jingle *peer = NULL;
1422         struct jingle_candidate *tmp;
1423         struct jingle *client = NULL;
1424         client = ast_aji_get_client("asterisk");
1425         if (argc != 3)
1426                 return RESULT_SHOWUSAGE;
1427         ast_mutex_lock(&jinglelock);
1428         if (client)
1429                 p = jingles->p;
1430         while (p) {
1431                 ast_mutex_lock(&p->lock);
1432                 ast_cli(fd, "SID = %s\n", p->sid);
1433                 tmp = p->candidates;
1434                 while (tmp) {
1435                         ast_verbose("port %d\n", tmp->port);
1436                         tmp = tmp->next;
1437                 }
1438                 ast_mutex_unlock(&p->lock);
1439                 p = p->next;
1440         }
1441         if (!jingles->p)
1442                 ast_cli(fd, "No jingle channels in use\n");
1443         ast_mutex_unlock(&jinglelock);
1444         return RESULT_SUCCESS;
1445 }
1446 #endif
1447
1448 static int jingle_parser(void *data, ikspak *pak)
1449 {
1450         struct jingle *client = ASTOBJ_REF((struct jingle *) data);
1451
1452         if (iks_find_with_attrib(pak->x, GOOGLE_NODE, "type", JINGLE_INITIATE)) {
1453                 /* New call */
1454                 jingle_newcall(client, pak);
1455         } else if (iks_find_with_attrib(pak->x, GOOGLE_NODE, "type", GOOGLE_NEGOTIATE)) {
1456                 ast_debug(3, "About to add candidate!\n");
1457                 jingle_add_candidate(client, pak);
1458                 ast_debug(3, "Candidate Added!\n");
1459         } else if (iks_find_with_attrib(pak->x, GOOGLE_NODE, "type", GOOGLE_ACCEPT)) {
1460                 jingle_is_answered(client, pak);
1461         } else if (iks_find_with_attrib(pak->x, GOOGLE_NODE, "type", "session-info") || iks_find_with_attrib(pak->x, JINGLE_NODE, "action", "session-info")) {
1462                 jingle_handle_dtmf(client, pak);
1463         } else if (iks_find_with_attrib(pak->x, GOOGLE_NODE, "type", "terminate")) {
1464                 jingle_hangup_farend(client, pak);
1465         } else if (iks_find_with_attrib(pak->x, GOOGLE_NODE, "type", "reject")) {
1466                 jingle_hangup_farend(client, pak);
1467         }
1468         ASTOBJ_UNREF(client, jingle_member_destroy);
1469         return IKS_FILTER_EAT;
1470 }
1471 /* Not using this anymore probably take out soon 
1472 static struct jingle_candidate *jingle_create_candidate(char *args)
1473 {
1474         char *name, *type, *preference, *protocol;
1475         struct jingle_candidate *res;
1476         res = ast_calloc(1, sizeof(*res));
1477         if (args)
1478                 name = args;
1479         if ((args = strchr(args, ','))) {
1480                 *args = '\0';
1481                 args++;
1482                 preference = args;
1483         }
1484         if ((args = strchr(args, ','))) {
1485                 *args = '\0';
1486                 args++;
1487                 protocol = args;
1488         }
1489         if ((args = strchr(args, ','))) {
1490                 *args = '\0';
1491                 args++;
1492                 type = args;
1493         }
1494         if (name)
1495                 ast_copy_string(res->name, name, sizeof(res->name));
1496         if (preference) {
1497                 res->preference = atof(preference);
1498         }
1499         if (protocol) {
1500                 if (!strcasecmp("udp", protocol))
1501                         res->protocol = AJI_PROTOCOL_UDP;
1502                 if (!strcasecmp("ssltcp", protocol))
1503                         res->protocol = AJI_PROTOCOL_SSLTCP;
1504         }
1505         if (type) {
1506                 if (!strcasecmp("stun", type))
1507                         res->type = AJI_CONNECT_STUN;
1508                 if (!strcasecmp("local", type))
1509                         res->type = AJI_CONNECT_LOCAL;
1510                 if (!strcasecmp("relay", type))
1511                         res->type = AJI_CONNECT_RELAY;
1512         }
1513
1514         return res;
1515 }
1516 */
1517
1518 static int jingle_create_member(char *label, struct ast_variable *var, int allowguest,
1519                                                                 struct ast_codec_pref prefs, char *context,
1520                                                                 struct jingle *member)
1521 {
1522         struct aji_client *client;
1523
1524         if (!member)
1525                 ast_log(LOG_WARNING, "Out of memory.\n");
1526
1527         ast_copy_string(member->name, label, sizeof(member->name));
1528         ast_copy_string(member->user, label, sizeof(member->user));
1529         ast_copy_string(member->context, context, sizeof(member->context));
1530         member->allowguest = allowguest;
1531         member->prefs = prefs;
1532         while (var) {
1533 #if 0
1534                 struct jingle_candidate *candidate = NULL;
1535 #endif
1536                 if (!strcasecmp(var->name, "username"))
1537                         ast_copy_string(member->user, var->value, sizeof(member->user));
1538                 else if (!strcasecmp(var->name, "disallow"))
1539                         ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 0);
1540                 else if (!strcasecmp(var->name, "allow"))
1541                         ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 1);
1542                 else if (!strcasecmp(var->name, "context"))
1543                         ast_copy_string(member->context, var->value, sizeof(member->context));
1544 #if 0
1545                 else if (!strcasecmp(var->name, "candidate")) {
1546                         candidate = jingle_create_candidate(var->value);
1547                         if (candidate) {
1548                                 candidate->next = member->ourcandidates;
1549                                 member->ourcandidates = candidate;
1550                         }
1551                 }
1552 #endif
1553                 else if (!strcasecmp(var->name, "connection")) {
1554                         if ((client = ast_aji_get_client(var->value))) {
1555                                 member->connection = client;
1556                                 iks_filter_add_rule(client->f, jingle_parser, member,
1557                                                     IKS_RULE_TYPE, IKS_PAK_IQ,
1558                                                     IKS_RULE_FROM_PARTIAL, member->user,
1559                                                     IKS_RULE_NS, JINGLE_NS,
1560                                                     IKS_RULE_DONE);
1561                         } else {
1562                                 ast_log(LOG_ERROR, "connection referenced not found!\n");
1563                                 return 0;
1564                         }
1565                 }
1566                 var = var->next;
1567         }
1568         if (member->connection && member->user)
1569                 member->buddy = ASTOBJ_CONTAINER_FIND(&member->connection->buddies, member->user);
1570         else {
1571                 ast_log(LOG_ERROR, "No Connection or Username!\n");
1572         }
1573         return 1;
1574 }
1575
1576 static int jingle_load_config(void)
1577 {
1578         char *cat = NULL;
1579         struct ast_config *cfg = NULL;
1580         char context[100];
1581         int allowguest = 1;
1582         struct ast_variable *var;
1583         struct jingle *member;
1584         struct ast_codec_pref prefs;
1585         struct aji_client_container *clients;
1586         struct jingle_candidate *global_candidates = NULL;
1587         struct ast_flags config_flags = { 0 };
1588
1589         cfg = ast_config_load(JINGLE_CONFIG, config_flags);
1590         if (!cfg)
1591                 return 0;
1592
1593         /* Copy the default jb config over global_jbconf */
1594         memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
1595
1596         cat = ast_category_browse(cfg, NULL);
1597         for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
1598                 /* handle jb conf */
1599                 if (!ast_jb_read_conf(&global_jbconf, var->name, var->value))
1600                         continue;
1601
1602                 if (!strcasecmp(var->name, "allowguest"))
1603                         allowguest =
1604                                 (ast_true(ast_variable_retrieve(cfg, "general", "allowguest"))) ? 1 : 0;
1605                 else if (!strcasecmp(var->name, "disallow"))
1606                         ast_parse_allow_disallow(&prefs, &global_capability, var->value, 0);
1607                 else if (!strcasecmp(var->name, "allow"))
1608                         ast_parse_allow_disallow(&prefs, &global_capability, var->value, 1);
1609                 else if (!strcasecmp(var->name, "context"))
1610                         ast_copy_string(context, var->value, sizeof(context));
1611                 else if (!strcasecmp(var->name, "externip"))
1612                         ast_copy_string(externip, var->value, sizeof(externip));
1613 /*  Idea to allow for custom candidates  */
1614 /*
1615                 else if (!strcasecmp(var->name, "candidate")) {
1616                         candidate = jingle_create_candidate(var->value);
1617                         if (candidate) {
1618                                 candidate->next = global_candidates;
1619                                 global_candidates = candidate;
1620                         }
1621                 }
1622 */
1623         }
1624         while (cat) {
1625                 if (strcasecmp(cat, "general")) {
1626                         var = ast_variable_browse(cfg, cat);
1627                         member = ast_calloc(1, sizeof(*member));
1628                         ASTOBJ_INIT(member);
1629                         ASTOBJ_WRLOCK(member);
1630                         if (!strcasecmp(cat, "guest")) {
1631                                 ast_copy_string(member->name, "guest", sizeof(member->name));
1632                                 ast_copy_string(member->user, "guest", sizeof(member->user));
1633                                 ast_copy_string(member->context, context, sizeof(member->context));
1634                                 member->allowguest = allowguest;
1635                                 member->prefs = prefs;
1636                                 while (var) {
1637                                         if (!strcasecmp(var->name, "disallow"))
1638                                                 ast_parse_allow_disallow(&member->prefs, &member->capability,
1639                                                                                                  var->value, 0);
1640                                         else if (!strcasecmp(var->name, "allow"))
1641                                                 ast_parse_allow_disallow(&member->prefs, &member->capability,
1642                                                                                                  var->value, 1);
1643                                         else if (!strcasecmp(var->name, "context"))
1644                                                 ast_copy_string(member->context, var->value,
1645                                                                                 sizeof(member->context));
1646 /*  Idea to allow for custom candidates  */
1647 /*
1648                                         else if (!strcasecmp(var->name, "candidate")) {
1649                                                 candidate = jingle_create_candidate(var->value);
1650                                                 if (candidate) {
1651                                                         candidate->next = member->ourcandidates;
1652                                                         member->ourcandidates = candidate;
1653                                                 }
1654                                         }
1655 */
1656                                         var = var->next;
1657                                 }
1658                                 ASTOBJ_UNLOCK(member);
1659                                 clients = ast_aji_get_clients();
1660                                 if (clients) {
1661                                         ASTOBJ_CONTAINER_TRAVERSE(clients, 1, {
1662                                                 ASTOBJ_WRLOCK(iterator);
1663                                                 ASTOBJ_WRLOCK(member);
1664                                                 member->connection = iterator;
1665                                                 iks_filter_add_rule(iterator->f, jingle_parser, member, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_NS, JINGLE_NS, IKS_RULE_DONE);
1666                                                 iks_filter_add_rule(iterator->f, jingle_parser, member, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_NS, JINGLE_DTMF_NS, IKS_RULE_DONE);
1667                                                 ASTOBJ_UNLOCK(member);
1668                                                 ASTOBJ_CONTAINER_LINK(&jingles, member);
1669                                                 ASTOBJ_UNLOCK(iterator);
1670                                         });
1671                                 } else {
1672                                         ASTOBJ_UNLOCK(member);
1673                                         ASTOBJ_UNREF(member, jingle_member_destroy);
1674                                 }
1675                         } else {
1676                                 ASTOBJ_UNLOCK(member);
1677                                 if (jingle_create_member(cat, var, allowguest, prefs, context, member))
1678                                         ASTOBJ_CONTAINER_LINK(&jingles, member);
1679                                 ASTOBJ_UNREF(member, jingle_member_destroy);
1680                         }
1681                 }
1682                 cat = ast_category_browse(cfg, cat);
1683         }
1684         jingle_free_candidates(global_candidates);
1685         return 1;
1686 }
1687
1688 /*! \brief Load module into PBX, register channel */
1689 static int load_module(void)
1690 {
1691 #ifdef HAVE_GNUTLS      
1692         gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
1693 #endif /* HAVE_GNUTLS */
1694
1695         ASTOBJ_CONTAINER_INIT(&jingles);
1696         if (!jingle_load_config()) {
1697                 ast_log(LOG_ERROR, "Unable to read config file %s. Not loading module.\n", JINGLE_CONFIG);
1698                 return AST_MODULE_LOAD_DECLINE;
1699         }
1700
1701         sched = sched_context_create();
1702         if (!sched) 
1703                 ast_log(LOG_WARNING, "Unable to create schedule context\n");
1704
1705         io = io_context_create();
1706         if (!io) 
1707                 ast_log(LOG_WARNING, "Unable to create I/O context\n");
1708
1709         if (ast_find_ourip(&__ourip, bindaddr)) {
1710                 ast_log(LOG_WARNING, "Unable to get own IP address, Jingle disabled\n");
1711                 return 0;
1712         }
1713
1714         ast_rtp_proto_register(&jingle_rtp);
1715
1716         /* Make sure we can register our channel type */
1717         if (ast_channel_register(&jingle_tech)) {
1718                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1719                 return -1;
1720         }
1721         return 0;
1722 }
1723
1724 /*! \brief Reload module */
1725 static int reload(void)
1726 {
1727         return 0;
1728 }
1729
1730 /*! \brief Unload the jingle channel from Asterisk */
1731 static int unload_module(void)
1732 {
1733         struct jingle_pvt *privates = NULL;
1734
1735         /* First, take us out of the channel loop */
1736         ast_channel_unregister(&jingle_tech);
1737         ast_rtp_proto_unregister(&jingle_rtp);
1738
1739         if (!ast_mutex_lock(&jinglelock)) {
1740                 /* Hangup all interfaces if they have an owner */
1741                 ASTOBJ_CONTAINER_TRAVERSE(&jingles, 1, {
1742                         ASTOBJ_WRLOCK(iterator);
1743                         privates = iterator->p;
1744                         while(privates) {
1745                                 if (privates->owner)
1746                                         ast_softhangup(privates->owner, AST_SOFTHANGUP_APPUNLOAD);
1747                                 privates = privates->next;
1748                         }
1749                         iterator->p = NULL;
1750                         ASTOBJ_UNLOCK(iterator);
1751                 });
1752                 ast_mutex_unlock(&jinglelock);
1753         } else {
1754                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1755                 return -1;
1756         }
1757         ASTOBJ_CONTAINER_DESTROYALL(&jingles, jingle_member_destroy);
1758         ASTOBJ_CONTAINER_DESTROY(&jingles);
1759         return 0;
1760 }
1761
1762 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Jingle Channel Driver",
1763                 .load = load_module,
1764                 .unload = unload_module,
1765                 .reload = reload,
1766                );