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