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