Merge changes from team/russell/issue_9520
[asterisk-bristuff.git] / channels / chan_h323.c
blob3ae30087635e90b3100d6d44321601159da9d3ab
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005
6 * OpenH323 Channel Driver for ASTERISK PBX.
7 * By Jeremy McNamara
8 * For The NuFone Network
10 * chan_h323 has been derived from code created by
11 * Michael Manousos and Mark Spencer
13 * See http://www.asterisk.org for more information about
14 * the Asterisk project. Please do not directly contact
15 * any of the maintainers of this project for assistance;
16 * the project provides a web site, mailing lists and IRC
17 * channels for your use.
19 * This program is free software, distributed under the terms of
20 * the GNU General Public License Version 2. See the LICENSE file
21 * at the top of the source tree.
24 /*! \file
26 * \brief This file is part of the chan_h323 driver for Asterisk
28 * \author Jeremy McNamara
30 * \par See also
31 * \arg Config_h323
33 * \ingroup channel_drivers
36 /*** MODULEINFO
37 <depend>openh323</depend>
38 <defaultenabled>yes</defaultenabled>
39 ***/
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
45 #include "asterisk.h"
47 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
49 #ifdef __cplusplus
51 #endif
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <sys/signal.h>
56 #include <sys/param.h>
57 #if defined(BSD) || defined(SOLARIS)
58 #ifndef IPTOS_MINCOST
59 #define IPTOS_MINCOST 0x02
60 #endif
61 #endif
62 #include <arpa/inet.h>
63 #include <net/if.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <unistd.h>
68 #include <stdlib.h>
69 #include <netdb.h>
70 #include <stdio.h>
71 #include <string.h>
72 #include <errno.h>
73 #include <fcntl.h>
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
79 #include "asterisk/lock.h"
80 #include "asterisk/logger.h"
81 #include "asterisk/channel.h"
82 #include "asterisk/config.h"
83 #include "asterisk/module.h"
84 #include "asterisk/musiconhold.h"
85 #include "asterisk/pbx.h"
86 #include "asterisk/options.h"
87 #include "asterisk/utils.h"
88 #include "asterisk/lock.h"
89 #include "asterisk/sched.h"
90 #include "asterisk/io.h"
91 #include "asterisk/rtp.h"
92 #include "asterisk/acl.h"
93 #include "asterisk/callerid.h"
94 #include "asterisk/cli.h"
95 #include "asterisk/dsp.h"
96 #include "asterisk/causes.h"
97 #include "asterisk/stringfields.h"
98 #include "asterisk/abstract_jb.h"
99 #include "asterisk/astobj.h"
101 #ifdef __cplusplus
103 #endif
105 #include "h323/chan_h323.h"
107 receive_digit_cb on_receive_digit;
108 on_rtp_cb on_external_rtp_create;
109 start_rtp_cb on_start_rtp_channel;
110 setup_incoming_cb on_incoming_call;
111 setup_outbound_cb on_outgoing_call;
112 chan_ringing_cb on_chan_ringing;
113 con_established_cb on_connection_established;
114 clear_con_cb on_connection_cleared;
115 answer_call_cb on_answer_call;
116 progress_cb on_progress;
117 rfc2833_cb on_set_rfc2833_payload;
118 hangup_cb on_hangup;
119 setcapabilities_cb on_setcapabilities;
120 setpeercapabilities_cb on_setpeercapabilities;
122 /* global debug flag */
123 int h323debug;
125 /*! Global jitterbuffer configuration - by default, jb is disabled */
126 static struct ast_jb_conf default_jbconf =
128 .flags = 0,
129 .max_size = -1,
130 .resync_threshold = -1,
131 .impl = ""
133 static struct ast_jb_conf global_jbconf;
135 /** Variables required by Asterisk */
136 static const char tdesc[] = "The NuFone Network's Open H.323 Channel Driver";
137 static const char config[] = "h323.conf";
138 static char default_context[AST_MAX_CONTEXT] = "default";
139 static struct sockaddr_in bindaddr;
141 #define GLOBAL_CAPABILITY (AST_FORMAT_G723_1 | AST_FORMAT_GSM | AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_G729A | AST_FORMAT_H261)
143 /** H.323 configuration values */
144 static int h323_signalling_port = 1720;
145 static char gatekeeper[100];
146 static int gatekeeper_disable = 1;
147 static int gatekeeper_discover = 0;
148 static int gkroute = 0;
149 /* Find user by alias (h.323 id) is default, alternative is the incomming call's source IP address*/
150 static int userbyalias = 1;
151 static int acceptAnonymous = 1;
152 static int tos = 0;
153 static char secret[50];
154 static unsigned int unique = 0;
156 static call_options_t global_options;
158 /** Private structure of a OpenH323 channel */
159 struct oh323_pvt {
160 ast_mutex_t lock; /* Channel private lock */
161 call_options_t options; /* Options to be used during call setup */
162 int alreadygone; /* Whether or not we've already been destroyed by our peer */
163 int needdestroy; /* if we need to be destroyed */
164 call_details_t cd; /* Call details */
165 struct ast_channel *owner; /* Who owns us */
166 struct sockaddr_in sa; /* Our peer */
167 struct sockaddr_in redirip; /* Where our RTP should be going if not to us */
168 int nonCodecCapability; /* non-audio capability */
169 int outgoing; /* Outgoing or incoming call? */
170 char exten[AST_MAX_EXTENSION]; /* Requested extension */
171 char context[AST_MAX_CONTEXT]; /* Context where to start */
172 char accountcode[256]; /* Account code */
173 char rdnis[80]; /* Referring DNIS, if available */
174 int amaflags; /* AMA Flags */
175 struct ast_rtp *rtp; /* RTP Session */
176 struct ast_dsp *vad; /* Used for in-band DTMF detection */
177 int nativeformats; /* Codec formats supported by a channel */
178 int needhangup; /* Send hangup when Asterisk is ready */
179 int hangupcause; /* Hangup cause from OpenH323 layer */
180 int newstate; /* Pending state change */
181 int newcontrol; /* Pending control to send */
182 int newdigit; /* Pending DTMF digit to send */
183 int newduration; /* Pending DTMF digit duration to send */
184 int pref_codec; /* Preferred codec */
185 int peercapability; /* Capabilities learned from peer */
186 int jointcapability; /* Common capabilities for local and remote side */
187 struct ast_codec_pref peer_prefs; /* Preferenced list of codecs which remote side supports */
188 int dtmf_pt; /* Payload code used for RFC2833 messages */
189 int curDTMF; /* DTMF tone being generated to Asterisk side */
190 int DTMFsched; /* Scheduler descriptor for DTMF */
191 int update_rtp_info; /* Configuration of fd's array is pending */
192 int recvonly; /* Peer isn't wish to receive our voice stream */
193 int txDtmfDigit; /* DTMF digit being to send to H.323 side */
194 int noInbandDtmf; /* Inband DTMF processing by DSP isn't available */
195 int connection_established; /* Call got CONNECT message */
196 int got_progress; /* Call got PROGRESS message, pass inband audio */
197 struct oh323_pvt *next; /* Next channel in list */
198 } *iflist = NULL;
200 static struct ast_user_list {
201 ASTOBJ_CONTAINER_COMPONENTS(struct oh323_user);
202 } userl;
204 static struct ast_peer_list {
205 ASTOBJ_CONTAINER_COMPONENTS(struct oh323_peer);
206 } peerl;
208 static struct ast_alias_list {
209 ASTOBJ_CONTAINER_COMPONENTS(struct oh323_alias);
210 } aliasl;
212 /** Asterisk RTP stuff */
213 static struct sched_context *sched;
214 static struct io_context *io;
216 /** Protect the interface list (oh323_pvt) */
217 AST_MUTEX_DEFINE_STATIC(iflock);
219 /* Protect the monitoring thread, so only one process can kill or start it, and not
220 when it's doing something critical. */
221 AST_MUTEX_DEFINE_STATIC(monlock);
223 /* Protect the H.323 capabilities list, to avoid more than one channel to set the capabilities simultaneaously in the h323 stack. */
224 AST_MUTEX_DEFINE_STATIC(caplock);
226 /* Protect the reload process */
227 AST_MUTEX_DEFINE_STATIC(h323_reload_lock);
228 static int h323_reloading = 0;
230 /* This is the thread for the monitor which checks for input on the channels
231 which are not currently in use. */
232 static pthread_t monitor_thread = AST_PTHREADT_NULL;
233 static int restart_monitor(void);
234 static int h323_do_reload(void);
236 static struct ast_channel *oh323_request(const char *type, int format, void *data, int *cause);
237 static int oh323_digit_begin(struct ast_channel *c, char digit);
238 static int oh323_digit_end(struct ast_channel *c, char digit, unsigned int duration);
239 static int oh323_call(struct ast_channel *c, char *dest, int timeout);
240 static int oh323_hangup(struct ast_channel *c);
241 static int oh323_answer(struct ast_channel *c);
242 static struct ast_frame *oh323_read(struct ast_channel *c);
243 static int oh323_write(struct ast_channel *c, struct ast_frame *frame);
244 static int oh323_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen);
245 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
247 static const struct ast_channel_tech oh323_tech = {
248 .type = "H323",
249 .description = tdesc,
250 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
251 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
252 .requester = oh323_request,
253 .send_digit_begin = oh323_digit_begin,
254 .send_digit_end = oh323_digit_end,
255 .call = oh323_call,
256 .hangup = oh323_hangup,
257 .answer = oh323_answer,
258 .read = oh323_read,
259 .write = oh323_write,
260 .indicate = oh323_indicate,
261 .fixup = oh323_fixup,
262 /* disable, for now */
263 #if 0
264 .bridge = ast_rtp_bridge,
265 #endif
268 static const char* redirectingreason2str(int redirectingreason)
270 switch (redirectingreason) {
271 case 0:
272 return "UNKNOWN";
273 case 1:
274 return "BUSY";
275 case 2:
276 return "NO_REPLY";
277 case 0xF:
278 return "UNCONDITIONAL";
279 default:
280 return "NOREDIRECT";
284 static void oh323_destroy_alias(struct oh323_alias *alias)
286 if (h323debug)
287 ast_log(LOG_DEBUG, "Destroying alias '%s'\n", alias->name);
288 free(alias);
291 static void oh323_destroy_user(struct oh323_user *user)
293 if (h323debug)
294 ast_log(LOG_DEBUG, "Destroying user '%s'\n", user->name);
295 ast_free_ha(user->ha);
296 free(user);
299 static void oh323_destroy_peer(struct oh323_peer *peer)
301 if (h323debug)
302 ast_log(LOG_DEBUG, "Destroying peer '%s'\n", peer->name);
303 ast_free_ha(peer->ha);
304 free(peer);
307 static int oh323_simulate_dtmf_end(const void *data)
309 struct oh323_pvt *pvt = (struct oh323_pvt *)data;
311 if (pvt) {
312 ast_mutex_lock(&pvt->lock);
313 /* Don't hold pvt lock while trying to lock the channel */
314 while(pvt->owner && ast_channel_trylock(pvt->owner)) {
315 ast_mutex_unlock(&pvt->lock);
316 usleep(1);
317 ast_mutex_lock(&pvt->lock);
320 if (pvt->owner) {
321 struct ast_frame f = {
322 .frametype = AST_FRAME_DTMF_END,
323 .subclass = pvt->curDTMF,
324 .samples = 0,
325 .src = "SIMULATE_DTMF_END",
327 ast_queue_frame(pvt->owner, &f);
328 ast_channel_unlock(pvt->owner);
331 pvt->DTMFsched = -1;
332 ast_mutex_unlock(&pvt->lock);
335 return 0;
338 /* Channel and private structures should be already locked */
339 static void __oh323_update_info(struct ast_channel *c, struct oh323_pvt *pvt)
341 if (c->nativeformats != pvt->nativeformats) {
342 if (h323debug)
343 ast_log(LOG_DEBUG, "Preparing %s for new native format\n", c->name);
344 c->nativeformats = pvt->nativeformats;
345 ast_set_read_format(c, c->readformat);
346 ast_set_write_format(c, c->writeformat);
348 if (pvt->needhangup) {
349 if (h323debug)
350 ast_log(LOG_DEBUG, "Process pending hangup for %s\n", c->name);
351 c->_softhangup |= AST_SOFTHANGUP_DEV;
352 c->hangupcause = pvt->hangupcause;
353 ast_queue_hangup(c);
354 pvt->needhangup = 0;
355 pvt->newstate = pvt->newcontrol = pvt->newdigit = pvt->DTMFsched = -1;
357 if (pvt->newstate >= 0) {
358 ast_setstate(c, pvt->newstate);
359 pvt->newstate = -1;
361 if (pvt->newcontrol >= 0) {
362 ast_queue_control(c, pvt->newcontrol);
363 pvt->newcontrol = -1;
365 if (pvt->newdigit >= 0) {
366 struct ast_frame f = {
367 .frametype = AST_FRAME_DTMF_END,
368 .subclass = pvt->newdigit,
369 .samples = pvt->newduration * 8,
370 .len = pvt->newduration,
371 .src = "UPDATE_INFO",
373 if (pvt->newdigit == ' ') { /* signalUpdate message */
374 f.subclass = pvt->curDTMF;
375 if (pvt->DTMFsched >= 0) {
376 AST_SCHED_DEL(sched, pvt->DTMFsched);
378 } else { /* Regular input or signal message */
379 if (pvt->newduration) { /* This is a signal, signalUpdate follows */
380 f.frametype = AST_FRAME_DTMF_BEGIN;
381 AST_SCHED_DEL(sched, pvt->DTMFsched);
382 pvt->DTMFsched = ast_sched_add(sched, pvt->newduration, oh323_simulate_dtmf_end, pvt);
383 if (h323debug)
384 ast_log(LOG_DTMF, "Scheduled DTMF END simulation for %d ms, id=%d\n", pvt->newduration, pvt->DTMFsched);
386 pvt->curDTMF = pvt->newdigit;
388 ast_queue_frame(c, &f);
389 pvt->newdigit = -1;
391 if (pvt->update_rtp_info > 0) {
392 if (pvt->rtp) {
393 ast_jb_configure(c, &global_jbconf);
394 c->fds[0] = ast_rtp_fd(pvt->rtp);
395 c->fds[1] = ast_rtcp_fd(pvt->rtp);
396 ast_queue_frame(pvt->owner, &ast_null_frame); /* Tell Asterisk to apply changes */
398 pvt->update_rtp_info = -1;
402 /* Only channel structure should be locked */
403 static void oh323_update_info(struct ast_channel *c)
405 struct oh323_pvt *pvt = c->tech_pvt;
407 if (pvt) {
408 ast_mutex_lock(&pvt->lock);
409 __oh323_update_info(c, pvt);
410 ast_mutex_unlock(&pvt->lock);
414 static void cleanup_call_details(call_details_t *cd)
416 if (cd->call_token) {
417 free(cd->call_token);
418 cd->call_token = NULL;
420 if (cd->call_source_aliases) {
421 free(cd->call_source_aliases);
422 cd->call_source_aliases = NULL;
424 if (cd->call_dest_alias) {
425 free(cd->call_dest_alias);
426 cd->call_dest_alias = NULL;
428 if (cd->call_source_name) {
429 free(cd->call_source_name);
430 cd->call_source_name = NULL;
432 if (cd->call_source_e164) {
433 free(cd->call_source_e164);
434 cd->call_source_e164 = NULL;
436 if (cd->call_dest_e164) {
437 free(cd->call_dest_e164);
438 cd->call_dest_e164 = NULL;
440 if (cd->sourceIp) {
441 free(cd->sourceIp);
442 cd->sourceIp = NULL;
444 if (cd->redirect_number) {
445 free(cd->redirect_number);
446 cd->redirect_number = NULL;
450 static void __oh323_destroy(struct oh323_pvt *pvt)
452 struct oh323_pvt *cur, *prev = NULL;
454 AST_SCHED_DEL(sched, pvt->DTMFsched);
456 if (pvt->rtp) {
457 ast_rtp_destroy(pvt->rtp);
460 /* Free dsp used for in-band DTMF detection */
461 if (pvt->vad) {
462 ast_dsp_free(pvt->vad);
464 cleanup_call_details(&pvt->cd);
466 /* Unlink us from the owner if we have one */
467 if (pvt->owner) {
468 ast_channel_lock(pvt->owner);
469 if (h323debug)
470 ast_log(LOG_DEBUG, "Detaching from %s\n", pvt->owner->name);
471 pvt->owner->tech_pvt = NULL;
472 ast_channel_unlock(pvt->owner);
474 cur = iflist;
475 while(cur) {
476 if (cur == pvt) {
477 if (prev)
478 prev->next = cur->next;
479 else
480 iflist = cur->next;
481 break;
483 prev = cur;
484 cur = cur->next;
486 if (!cur) {
487 ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
488 } else {
489 ast_mutex_unlock(&pvt->lock);
490 ast_mutex_destroy(&pvt->lock);
491 free(pvt);
495 static void oh323_destroy(struct oh323_pvt *pvt)
497 if (h323debug) {
498 ast_log(LOG_DEBUG, "Destroying channel %s\n", (pvt->owner ? pvt->owner->name : "<unknown>"));
500 ast_mutex_lock(&iflock);
501 ast_mutex_lock(&pvt->lock);
502 __oh323_destroy(pvt);
503 ast_mutex_unlock(&iflock);
506 static int oh323_digit_begin(struct ast_channel *c, char digit)
508 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
509 char *token;
511 if (!pvt) {
512 ast_log(LOG_ERROR, "No private structure?! This is bad\n");
513 return -1;
515 ast_mutex_lock(&pvt->lock);
516 if (pvt->rtp && (pvt->options.dtmfmode & H323_DTMF_RFC2833) && (pvt->dtmf_pt > 0)) {
517 /* out-of-band DTMF */
518 if (h323debug) {
519 ast_log(LOG_DTMF, "Begin sending out-of-band digit %c on %s\n", digit, c->name);
521 ast_rtp_senddigit_begin(pvt->rtp, digit);
522 ast_mutex_unlock(&pvt->lock);
523 } else if (pvt->txDtmfDigit != digit) {
524 /* in-band DTMF */
525 if (h323debug) {
526 ast_log(LOG_DTMF, "Begin sending inband digit %c on %s\n", digit, c->name);
528 pvt->txDtmfDigit = digit;
529 token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
530 ast_mutex_unlock(&pvt->lock);
531 h323_send_tone(token, digit);
532 if (token) {
533 free(token);
535 } else
536 ast_mutex_unlock(&pvt->lock);
537 oh323_update_info(c);
538 return 0;
542 * Send (play) the specified digit to the channel.
545 static int oh323_digit_end(struct ast_channel *c, char digit, unsigned int duration)
547 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
548 char *token;
550 if (!pvt) {
551 ast_log(LOG_ERROR, "No private structure?! This is bad\n");
552 return -1;
554 ast_mutex_lock(&pvt->lock);
555 if (pvt->rtp && (pvt->options.dtmfmode & H323_DTMF_RFC2833) && (pvt->dtmf_pt > 0)) {
556 /* out-of-band DTMF */
557 if (h323debug) {
558 ast_log(LOG_DTMF, "End sending out-of-band digit %c on %s, duration %d\n", digit, c->name, duration);
560 ast_rtp_senddigit_end(pvt->rtp, digit);
561 ast_mutex_unlock(&pvt->lock);
562 } else {
563 /* in-band DTMF */
564 if (h323debug) {
565 ast_log(LOG_DTMF, "End sending inband digit %c on %s, duration %d\n", digit, c->name, duration);
567 pvt->txDtmfDigit = ' ';
568 token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
569 ast_mutex_unlock(&pvt->lock);
570 h323_send_tone(token, ' ');
571 if (token) {
572 free(token);
575 oh323_update_info(c);
576 return 0;
580 * Make a call over the specified channel to the specified
581 * destination.
582 * Returns -1 on error, 0 on success.
584 static int oh323_call(struct ast_channel *c, char *dest, int timeout)
586 int res = 0;
587 struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
588 const char *addr;
589 char called_addr[1024];
591 if (h323debug) {
592 ast_log(LOG_DEBUG, "Calling to %s on %s\n", dest, c->name);
594 if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
595 ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
596 return -1;
598 ast_mutex_lock(&pvt->lock);
599 if (!gatekeeper_disable) {
600 if (ast_strlen_zero(pvt->exten)) {
601 ast_copy_string(called_addr, dest, sizeof(called_addr));
602 } else {
603 snprintf(called_addr, sizeof(called_addr), "%s@%s", pvt->exten, dest);
605 } else {
606 res = htons(pvt->sa.sin_port);
607 addr = ast_inet_ntoa(pvt->sa.sin_addr);
608 if (ast_strlen_zero(pvt->exten)) {
609 snprintf(called_addr, sizeof(called_addr), "%s:%d", addr, res);
610 } else {
611 snprintf(called_addr, sizeof(called_addr), "%s@%s:%d", pvt->exten, addr, res);
614 /* make sure null terminated */
615 called_addr[sizeof(called_addr) - 1] = '\0';
617 if (c->cid.cid_num)
618 ast_copy_string(pvt->options.cid_num, c->cid.cid_num, sizeof(pvt->options.cid_num));
620 if (c->cid.cid_name)
621 ast_copy_string(pvt->options.cid_name, c->cid.cid_name, sizeof(pvt->options.cid_name));
623 if (c->cid.cid_rdnis) {
624 ast_copy_string(pvt->options.cid_rdnis, c->cid.cid_rdnis, sizeof(pvt->options.cid_rdnis));
627 pvt->options.presentation = c->cid.cid_pres;
628 pvt->options.type_of_number = c->cid.cid_ton;
630 if ((addr = pbx_builtin_getvar_helper(c, "PRIREDIRECTREASON"))) {
631 if (!strcasecmp(addr, "UNKNOWN"))
632 pvt->options.redirect_reason = 0;
633 else if (!strcasecmp(addr, "BUSY"))
634 pvt->options.redirect_reason = 1;
635 else if (!strcasecmp(addr, "NO_REPLY"))
636 pvt->options.redirect_reason = 2;
637 else if (!strcasecmp(addr, "UNCONDITIONAL"))
638 pvt->options.redirect_reason = 15;
639 else
640 pvt->options.redirect_reason = -1;
641 } else
642 pvt->options.redirect_reason = -1;
644 pvt->options.transfer_capability = c->transfercapability;
646 /* indicate that this is an outgoing call */
647 pvt->outgoing = 1;
649 if (option_verbose > 2)
650 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", c->transfercapability, ast_transfercapability2str(c->transfercapability));
651 if (h323debug)
652 ast_log(LOG_DEBUG, "Placing outgoing call to %s, %d\n", called_addr, pvt->options.dtmfcodec);
653 ast_mutex_unlock(&pvt->lock);
654 res = h323_make_call(called_addr, &(pvt->cd), &pvt->options);
655 if (res) {
656 ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
657 return -1;
659 oh323_update_info(c);
660 return 0;
663 static int oh323_answer(struct ast_channel *c)
665 int res;
666 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
667 char *token;
669 if (h323debug)
670 ast_log(LOG_DEBUG, "Answering on %s\n", c->name);
672 ast_mutex_lock(&pvt->lock);
673 token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
674 ast_mutex_unlock(&pvt->lock);
675 res = h323_answering_call(token, 0);
676 if (token)
677 free(token);
679 oh323_update_info(c);
680 if (c->_state != AST_STATE_UP) {
681 ast_setstate(c, AST_STATE_UP);
683 return res;
686 static int oh323_hangup(struct ast_channel *c)
688 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
689 int q931cause = AST_CAUSE_NORMAL_CLEARING;
690 char *call_token;
693 if (h323debug)
694 ast_log(LOG_DEBUG, "Hanging up and scheduling destroy of call %s\n", c->name);
696 if (!c->tech_pvt) {
697 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
698 return 0;
700 ast_mutex_lock(&pvt->lock);
701 /* Determine how to disconnect */
702 if (pvt->owner != c) {
703 ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
704 ast_mutex_unlock(&pvt->lock);
705 return 0;
708 pvt->owner = NULL;
709 c->tech_pvt = NULL;
711 if (c->hangupcause) {
712 q931cause = c->hangupcause;
713 } else {
714 const char *cause = pbx_builtin_getvar_helper(c, "DIALSTATUS");
715 if (cause) {
716 if (!strcmp(cause, "CONGESTION")) {
717 q931cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
718 } else if (!strcmp(cause, "BUSY")) {
719 q931cause = AST_CAUSE_USER_BUSY;
720 } else if (!strcmp(cause, "CHANISUNVAIL")) {
721 q931cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
722 } else if (!strcmp(cause, "NOANSWER")) {
723 q931cause = AST_CAUSE_NO_ANSWER;
724 } else if (!strcmp(cause, "CANCEL")) {
725 q931cause = AST_CAUSE_CALL_REJECTED;
730 /* Start the process if it's not already started */
731 if (!pvt->alreadygone && !pvt->hangupcause) {
732 call_token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
733 if (call_token) {
734 /* Release lock to eliminate deadlock */
735 ast_mutex_unlock(&pvt->lock);
736 if (h323_clear_call(call_token, q931cause)) {
737 ast_log(LOG_WARNING, "ClearCall failed.\n");
739 free(call_token);
740 ast_mutex_lock(&pvt->lock);
743 pvt->needdestroy = 1;
744 ast_mutex_unlock(&pvt->lock);
746 /* Update usage counter */
747 ast_module_unref(ast_module_info->self);
749 return 0;
752 static struct ast_frame *oh323_rtp_read(struct oh323_pvt *pvt)
754 /* Retrieve audio/etc from channel. Assumes pvt->lock is already held. */
755 struct ast_frame *f;
757 /* Only apply it for the first packet, we just need the correct ip/port */
758 if (pvt->options.nat) {
759 ast_rtp_setnat(pvt->rtp, pvt->options.nat);
760 pvt->options.nat = 0;
763 f = ast_rtp_read(pvt->rtp);
764 /* Don't send RFC2833 if we're not supposed to */
765 if (f && (f->frametype == AST_FRAME_DTMF) && !(pvt->options.dtmfmode & H323_DTMF_RFC2833)) {
766 return &ast_null_frame;
768 if (pvt->owner) {
769 /* We already hold the channel lock */
770 if (f->frametype == AST_FRAME_VOICE) {
771 if (f->subclass != pvt->owner->nativeformats) {
772 /* Try to avoid deadlock */
773 if (ast_channel_trylock(pvt->owner)) {
774 ast_log(LOG_NOTICE, "Format changed but channel is locked. Ignoring frame...\n");
775 return &ast_null_frame;
777 if (h323debug)
778 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
779 pvt->owner->nativeformats = f->subclass;
780 pvt->nativeformats = f->subclass;
781 ast_set_read_format(pvt->owner, pvt->owner->readformat);
782 ast_set_write_format(pvt->owner, pvt->owner->writeformat);
783 ast_channel_unlock(pvt->owner);
785 /* Do in-band DTMF detection */
786 if ((pvt->options.dtmfmode & H323_DTMF_INBAND) && pvt->vad) {
787 if ((pvt->nativeformats & (AST_FORMAT_SLINEAR | AST_FORMAT_ALAW | AST_FORMAT_ULAW))) {
788 if (!ast_channel_trylock(pvt->owner)) {
789 f = ast_dsp_process(pvt->owner, pvt->vad, f);
790 ast_channel_unlock(pvt->owner);
792 else
793 ast_log(LOG_NOTICE, "Unable to process inband DTMF while channel is locked\n");
794 } else if (pvt->nativeformats && !pvt->noInbandDtmf) {
795 ast_log(LOG_NOTICE, "Inband DTMF is not supported on codec %s. Use RFC2833\n", ast_getformatname(f->subclass));
796 pvt->noInbandDtmf = 1;
798 if (f &&(f->frametype == AST_FRAME_DTMF)) {
799 if (h323debug)
800 ast_log(LOG_DTMF, "Received in-band digit %c.\n", f->subclass);
805 return f;
808 static struct ast_frame *oh323_read(struct ast_channel *c)
810 struct ast_frame *fr;
811 struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
812 ast_mutex_lock(&pvt->lock);
813 __oh323_update_info(c, pvt);
814 switch(c->fdno) {
815 case 0:
816 fr = oh323_rtp_read(pvt);
817 break;
818 case 1:
819 if (pvt->rtp)
820 fr = ast_rtcp_read(pvt->rtp);
821 else
822 fr = &ast_null_frame;
823 break;
824 default:
825 ast_log(LOG_ERROR, "Unable to handle fd %d on channel %s\n", c->fdno, c->name);
826 fr = &ast_null_frame;
827 break;
829 ast_mutex_unlock(&pvt->lock);
830 return fr;
833 static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
835 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
836 int res = 0;
837 if (frame->frametype != AST_FRAME_VOICE) {
838 if (frame->frametype == AST_FRAME_IMAGE) {
839 return 0;
840 } else {
841 ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
842 return 0;
844 } else {
845 if (!(frame->subclass & c->nativeformats)) {
846 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
847 frame->subclass, c->nativeformats, c->readformat, c->writeformat);
848 return 0;
851 if (pvt) {
852 ast_mutex_lock(&pvt->lock);
853 if (pvt->rtp && !pvt->recvonly)
854 res = ast_rtp_write(pvt->rtp, frame);
855 __oh323_update_info(c, pvt);
856 ast_mutex_unlock(&pvt->lock);
858 return res;
861 static int oh323_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen)
864 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
865 char *token = (char *)NULL;
866 int res = -1;
867 int got_progress;
869 ast_mutex_lock(&pvt->lock);
870 token = (pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL);
871 got_progress = pvt->got_progress;
872 if (condition == AST_CONTROL_PROGRESS)
873 pvt->got_progress = 1;
874 else if ((condition == AST_CONTROL_BUSY) || (condition == AST_CONTROL_CONGESTION))
875 pvt->alreadygone = 1;
876 ast_mutex_unlock(&pvt->lock);
878 if (h323debug)
879 ast_log(LOG_DEBUG, "OH323: Indicating %d on %s\n", condition, token);
881 switch(condition) {
882 case AST_CONTROL_RINGING:
883 if (c->_state == AST_STATE_RING || c->_state == AST_STATE_RINGING) {
884 h323_send_alerting(token);
885 res = (got_progress ? 0 : -1); /* Do not simulate any audio tones if we got PROGRESS message */
887 break;
888 case AST_CONTROL_PROGRESS:
889 if (c->_state != AST_STATE_UP) {
890 /* Do not send PROGRESS message more than once */
891 if (!got_progress)
892 h323_send_progress(token);
893 res = 0;
895 break;
896 case AST_CONTROL_BUSY:
897 if (c->_state != AST_STATE_UP) {
898 h323_answering_call(token, 1);
899 ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
900 res = 0;
902 break;
903 case AST_CONTROL_CONGESTION:
904 if (c->_state != AST_STATE_UP) {
905 h323_answering_call(token, 1);
906 ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
907 res = 0;
909 break;
910 case AST_CONTROL_HOLD:
911 ast_moh_start(c, data, NULL);
912 res = 0;
913 break;
914 case AST_CONTROL_UNHOLD:
915 ast_moh_stop(c);
916 res = 0;
917 break;
918 case AST_CONTROL_SRCUPDATE:
919 ast_rtp_new_source(pvt->rtp);
920 res = 0;
921 break;
922 case AST_CONTROL_PROCEEDING:
923 case -1:
924 break;
925 default:
926 ast_log(LOG_WARNING, "OH323: Don't know how to indicate condition %d on %s\n", condition, token);
927 break;
930 if (h323debug)
931 ast_log(LOG_DEBUG, "OH323: Indicated %d on %s, res=%d\n", condition, token, res);
932 if (token)
933 free(token);
934 oh323_update_info(c);
936 return res;
939 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
941 struct oh323_pvt *pvt = (struct oh323_pvt *) newchan->tech_pvt;
943 ast_mutex_lock(&pvt->lock);
944 if (pvt->owner != oldchan) {
945 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, pvt->owner);
946 return -1;
948 pvt->owner = newchan;
949 ast_mutex_unlock(&pvt->lock);
950 return 0;
953 static int __oh323_rtp_create(struct oh323_pvt *pvt)
955 struct in_addr our_addr;
957 if (pvt->rtp)
958 return 0;
960 if (ast_find_ourip(&our_addr, bindaddr)) {
961 ast_mutex_unlock(&pvt->lock);
962 ast_log(LOG_ERROR, "Unable to locate local IP address for RTP stream\n");
963 return -1;
965 pvt->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, our_addr);
966 if (!pvt->rtp) {
967 ast_mutex_unlock(&pvt->lock);
968 ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
969 return -1;
971 if (h323debug)
972 ast_log(LOG_DEBUG, "Created RTP channel\n");
974 ast_rtp_settos(pvt->rtp, tos);
976 if (h323debug)
977 ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", pvt->options.nat);
978 ast_rtp_setnat(pvt->rtp, pvt->options.nat);
980 if (pvt->dtmf_pt > 0)
981 ast_rtp_set_rtpmap_type(pvt->rtp, pvt->dtmf_pt, "audio", "telephone-event", 0);
983 if (pvt->peercapability)
984 ast_rtp_codec_setpref(pvt->rtp, &pvt->peer_prefs);
986 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
987 ast_jb_configure(pvt->owner, &global_jbconf);
988 pvt->owner->fds[0] = ast_rtp_fd(pvt->rtp);
989 pvt->owner->fds[1] = ast_rtcp_fd(pvt->rtp);
990 ast_queue_frame(pvt->owner, &ast_null_frame); /* Tell Asterisk to apply changes */
991 ast_channel_unlock(pvt->owner);
992 } else
993 pvt->update_rtp_info = 1;
995 return 0;
998 /* Private structure should be locked on a call */
999 static struct ast_channel *__oh323_new(struct oh323_pvt *pvt, int state, const char *host)
1001 struct ast_channel *ch;
1002 char *cid_num, *cid_name;
1003 int fmt;
1005 if (!ast_strlen_zero(pvt->options.cid_num))
1006 cid_num = pvt->options.cid_num;
1007 else
1008 cid_num = pvt->cd.call_source_e164;
1010 if (!ast_strlen_zero(pvt->options.cid_name))
1011 cid_name = pvt->options.cid_name;
1012 else
1013 cid_name = pvt->cd.call_source_name;
1015 /* Don't hold a oh323_pvt lock while we allocate a chanel */
1016 ast_mutex_unlock(&pvt->lock);
1017 ch = ast_channel_alloc(1, state, cid_num, cid_name, pvt->accountcode, pvt->exten, pvt->context, pvt->amaflags, "H323/%s", host);
1018 /* Update usage counter */
1019 ast_module_ref(ast_module_info->self);
1020 ast_mutex_lock(&pvt->lock);
1021 if (ch) {
1022 ch->tech = &oh323_tech;
1023 if (!(fmt = pvt->jointcapability) && !(fmt = pvt->options.capability))
1024 fmt = global_options.capability;
1025 ch->nativeformats = ast_codec_choose(&pvt->options.prefs, fmt, 1)/* | (pvt->jointcapability & AST_FORMAT_VIDEO_MASK)*/;
1026 pvt->nativeformats = ch->nativeformats;
1027 fmt = ast_best_codec(ch->nativeformats);
1028 ch->writeformat = fmt;
1029 ch->rawwriteformat = fmt;
1030 ch->readformat = fmt;
1031 ch->rawreadformat = fmt;
1032 #if 0
1033 ch->fds[0] = ast_rtp_fd(pvt->rtp);
1034 ch->fds[1] = ast_rtcp_fd(pvt->rtp);
1035 #endif
1036 #ifdef VIDEO_SUPPORT
1037 if (pvt->vrtp) {
1038 ch->fds[2] = ast_rtp_fd(pvt->vrtp);
1039 ch->fds[3] = ast_rtcp_fd(pvt->vrtp);
1041 #endif
1042 #ifdef T38_SUPPORT
1043 if (pvt->udptl) {
1044 ch->fds[4] = ast_udptl_fd(pvt->udptl);
1046 #endif
1047 if (state == AST_STATE_RING) {
1048 ch->rings = 1;
1050 /* Allocate dsp for in-band DTMF support */
1051 if (pvt->options.dtmfmode & H323_DTMF_INBAND) {
1052 pvt->vad = ast_dsp_new();
1053 ast_dsp_set_features(pvt->vad, DSP_FEATURE_DTMF_DETECT);
1055 /* Register channel functions. */
1056 ch->tech_pvt = pvt;
1057 /* Set the owner of this channel */
1058 pvt->owner = ch;
1060 ast_copy_string(ch->context, pvt->context, sizeof(ch->context));
1061 ast_copy_string(ch->exten, pvt->exten, sizeof(ch->exten));
1062 ch->priority = 1;
1063 if (!ast_strlen_zero(pvt->accountcode)) {
1064 ast_string_field_set(ch, accountcode, pvt->accountcode);
1066 if (pvt->amaflags) {
1067 ch->amaflags = pvt->amaflags;
1070 /* Don't use ast_set_callerid() here because it will
1071 * generate a needless NewCallerID event */
1072 ch->cid.cid_ani = ast_strdup(cid_num);
1074 if (pvt->cd.redirect_reason >= 0) {
1075 ch->cid.cid_rdnis = ast_strdup(pvt->cd.redirect_number);
1076 pbx_builtin_setvar_helper(ch, "PRIREDIRECTREASON", redirectingreason2str(pvt->cd.redirect_reason));
1078 ch->cid.cid_pres = pvt->cd.presentation;
1079 ch->cid.cid_ton = pvt->cd.type_of_number;
1081 if (!ast_strlen_zero(pvt->exten) && strcmp(pvt->exten, "s")) {
1082 ch->cid.cid_dnid = strdup(pvt->exten);
1084 if (pvt->cd.transfer_capability >= 0)
1085 ch->transfercapability = pvt->cd.transfer_capability;
1086 if (state != AST_STATE_DOWN) {
1087 if (ast_pbx_start(ch)) {
1088 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
1089 ast_hangup(ch);
1090 ch = NULL;
1093 } else {
1094 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
1096 return ch;
1099 static struct oh323_pvt *oh323_alloc(int callid)
1101 struct oh323_pvt *pvt;
1103 pvt = (struct oh323_pvt *) malloc(sizeof(struct oh323_pvt));
1104 if (!pvt) {
1105 ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
1106 return NULL;
1108 memset(pvt, 0, sizeof(struct oh323_pvt));
1109 pvt->cd.redirect_reason = -1;
1110 pvt->cd.transfer_capability = -1;
1111 /* Ensure the call token is allocated for outgoing call */
1112 if (!callid) {
1113 if ((pvt->cd).call_token == NULL) {
1114 (pvt->cd).call_token = (char *)malloc(128);
1116 if (!pvt->cd.call_token) {
1117 ast_log(LOG_ERROR, "Not enough memory to alocate call token\n");
1118 ast_rtp_destroy(pvt->rtp);
1119 free(pvt);
1120 return NULL;
1122 memset((char *)(pvt->cd).call_token, 0, 128);
1123 pvt->cd.call_reference = callid;
1125 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
1126 pvt->jointcapability = pvt->options.capability;
1127 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
1128 pvt->nonCodecCapability |= AST_RTP_DTMF;
1129 } else {
1130 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1132 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
1133 pvt->newstate = pvt->newcontrol = pvt->newdigit = pvt->update_rtp_info = pvt->DTMFsched = -1;
1134 ast_mutex_init(&pvt->lock);
1135 /* Add to interface list */
1136 ast_mutex_lock(&iflock);
1137 pvt->next = iflist;
1138 iflist = pvt;
1139 ast_mutex_unlock(&iflock);
1140 return pvt;
1143 static struct oh323_pvt *find_call_locked(int call_reference, const char *token)
1145 struct oh323_pvt *pvt;
1147 ast_mutex_lock(&iflock);
1148 pvt = iflist;
1149 while(pvt) {
1150 if (!pvt->needdestroy && ((signed int)pvt->cd.call_reference == call_reference)) {
1151 /* Found the call */
1152 if ((token != NULL) && (pvt->cd.call_token != NULL) && (!strcmp(pvt->cd.call_token, token))) {
1153 ast_mutex_lock(&pvt->lock);
1154 ast_mutex_unlock(&iflock);
1155 return pvt;
1156 } else if (token == NULL) {
1157 ast_log(LOG_WARNING, "Call Token is NULL\n");
1158 ast_mutex_lock(&pvt->lock);
1159 ast_mutex_unlock(&iflock);
1160 return pvt;
1163 pvt = pvt->next;
1165 ast_mutex_unlock(&iflock);
1166 return NULL;
1169 static int update_state(struct oh323_pvt *pvt, int state, int signal)
1171 if (!pvt)
1172 return 0;
1173 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
1174 if (state >= 0)
1175 ast_setstate(pvt->owner, state);
1176 if (signal >= 0)
1177 ast_queue_control(pvt->owner, signal);
1178 ast_channel_unlock(pvt->owner);
1179 return 1;
1181 else {
1182 if (state >= 0)
1183 pvt->newstate = state;
1184 if (signal >= 0)
1185 pvt->newcontrol = signal;
1186 return 0;
1190 static struct oh323_alias *build_alias(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
1192 struct oh323_alias *alias;
1193 int found = 0;
1195 alias = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&aliasl, name, name, 0, 0, strcasecmp);
1197 if (alias)
1198 found++;
1199 else {
1200 if (!(alias = (struct oh323_alias *)calloc(1, sizeof(*alias))))
1201 return NULL;
1202 ASTOBJ_INIT(alias);
1204 if (!found && name)
1205 ast_copy_string(alias->name, name, sizeof(alias->name));
1206 for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
1207 if (!strcasecmp(v->name, "e164")) {
1208 ast_copy_string(alias->e164, v->value, sizeof(alias->e164));
1209 } else if (!strcasecmp(v->name, "prefix")) {
1210 ast_copy_string(alias->prefix, v->value, sizeof(alias->prefix));
1211 } else if (!strcasecmp(v->name, "context")) {
1212 ast_copy_string(alias->context, v->value, sizeof(alias->context));
1213 } else if (!strcasecmp(v->name, "secret")) {
1214 ast_copy_string(alias->secret, v->value, sizeof(alias->secret));
1215 } else {
1216 if (strcasecmp(v->value, "h323")) {
1217 ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->name);
1221 ASTOBJ_UNMARK(alias);
1222 return alias;
1225 static struct oh323_alias *realtime_alias(const char *alias)
1227 struct ast_variable *var, *tmp;
1228 struct oh323_alias *a;
1230 var = ast_load_realtime("h323", "name", alias, NULL);
1232 if (!var)
1233 return NULL;
1235 for (tmp = var; tmp; tmp = tmp->next) {
1236 if (!strcasecmp(tmp->name, "type") &&
1237 !(!strcasecmp(tmp->value, "alias") || !strcasecmp(tmp->value, "h323"))) {
1238 ast_variables_destroy(var);
1239 return NULL;
1243 a = build_alias(alias, var, NULL, 1);
1245 ast_variables_destroy(var);
1247 return a;
1250 #define DEPRECATED(_v, _new_opt) \
1251 ast_log(LOG_WARNING, "Option %s found at line %d has beed deprecated. Use %s instead.\n", (_v)->name, (_v)->lineno, (_new_opt))
1253 static int update_common_options(struct ast_variable *v, struct call_options *options)
1255 int tmp;
1257 if (!strcasecmp(v->name, "allow")) {
1258 ast_parse_allow_disallow(&options->prefs, &options->capability, v->value, 1);
1259 } else if (!strcasecmp(v->name, "disallow")) {
1260 ast_parse_allow_disallow(&options->prefs, &options->capability, v->value, 0);
1261 } else if (!strcasecmp(v->name, "dtmfmode")) {
1262 if (!strcasecmp(v->value, "inband")) {
1263 options->dtmfmode = H323_DTMF_INBAND;
1264 } else if (!strcasecmp(v->value, "rfc2833")) {
1265 options->dtmfmode = H323_DTMF_RFC2833;
1266 } else {
1267 ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", v->value);
1268 options->dtmfmode = H323_DTMF_RFC2833;
1270 } else if (!strcasecmp(v->name, "dtmfcodec")) {
1271 tmp = atoi(v->value);
1272 if (tmp < 96)
1273 ast_log(LOG_WARNING, "Invalid %s value %s at line %d\n", v->name, v->value, v->lineno);
1274 else
1275 options->dtmfcodec = tmp;
1276 } else if (!strcasecmp(v->name, "bridge")) {
1277 options->bridge = ast_true(v->value);
1278 } else if (!strcasecmp(v->name, "nat")) {
1279 options->nat = ast_true(v->value);
1280 } else if (!strcasecmp(v->name, "noFastStart")) {
1281 DEPRECATED(v, "fastStart");
1282 options->fastStart = !ast_true(v->value);
1283 } else if (!strcasecmp(v->name, "fastStart")) {
1284 options->fastStart = ast_true(v->value);
1285 } else if (!strcasecmp(v->name, "noH245Tunneling")) {
1286 DEPRECATED(v, "h245Tunneling");
1287 options->h245Tunneling = !ast_true(v->value);
1288 } else if (!strcasecmp(v->name, "h245Tunneling")) {
1289 options->h245Tunneling = ast_true(v->value);
1290 } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
1291 DEPRECATED(v, "silenceSuppression");
1292 options->silenceSuppression = !ast_true(v->value);
1293 } else if (!strcasecmp(v->name, "silenceSuppression")) {
1294 options->silenceSuppression = ast_true(v->value);
1295 } else if (!strcasecmp(v->name, "progress_setup")) {
1296 tmp = atoi(v->value);
1297 if ((tmp != 0) && (tmp != 1) && (tmp != 3) && (tmp != 8)) {
1298 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d, assuming 0\n", v->value, v->name, v->lineno);
1299 tmp = 0;
1301 options->progress_setup = tmp;
1302 } else if (!strcasecmp(v->name, "progress_alert")) {
1303 tmp = atoi(v->value);
1304 if ((tmp != 0) && (tmp != 1) && (tmp != 8)) {
1305 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d, assuming 0\n", v->value, v->name, v->lineno);
1306 tmp = 0;
1308 options->progress_alert = tmp;
1309 } else if (!strcasecmp(v->name, "progress_audio")) {
1310 options->progress_audio = ast_true(v->value);
1311 } else if (!strcasecmp(v->name, "callerid")) {
1312 ast_callerid_split(v->value, options->cid_name, sizeof(options->cid_name), options->cid_num, sizeof(options->cid_num));
1313 } else if (!strcasecmp(v->name, "fullname")) {
1314 ast_copy_string(options->cid_name, v->value, sizeof(options->cid_name));
1315 } else if (!strcasecmp(v->name, "cid_number")) {
1316 ast_copy_string(options->cid_num, v->value, sizeof(options->cid_num));
1317 } else if (!strcasecmp(v->name, "tunneling")) {
1318 if (!strcasecmp(v->value, "none"))
1319 options->tunnelOptions = 0;
1320 else if (!strcasecmp(v->value, "cisco"))
1321 options->tunnelOptions |= H323_TUNNEL_CISCO;
1322 else if (!strcasecmp(v->value, "qsig"))
1323 options->tunnelOptions |= H323_TUNNEL_QSIG;
1324 else
1325 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d\n", v->value, v->name, v->lineno);
1326 } else
1327 return 1;
1329 return 0;
1331 #undef DEPRECATED
1333 static struct oh323_user *build_user(char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
1335 struct oh323_user *user;
1336 struct ast_ha *oldha;
1337 int found = 0;
1338 int format;
1340 user = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&userl, name, name, 0, 0, strcmp);
1342 if (user)
1343 found++;
1344 else {
1345 if (!(user = (struct oh323_user *)calloc(1, sizeof(*user))))
1346 return NULL;
1347 ASTOBJ_INIT(user);
1349 oldha = user->ha;
1350 user->ha = (struct ast_ha *)NULL;
1351 memcpy(&user->options, &global_options, sizeof(user->options));
1352 /* Set default context */
1353 ast_copy_string(user->context, default_context, sizeof(user->context));
1354 if (user && !found)
1355 ast_copy_string(user->name, name, sizeof(user->name));
1357 #if 0 /* XXX Port channel variables functionality from chan_sip XXX */
1358 if (user->chanvars) {
1359 ast_variables_destroy(user->chanvars);
1360 user->chanvars = NULL;
1362 #endif
1364 for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
1365 if (!update_common_options(v, &user->options))
1366 continue;
1367 if (!strcasecmp(v->name, "context")) {
1368 ast_copy_string(user->context, v->value, sizeof(user->context));
1369 } else if (!strcasecmp(v->name, "secret")) {
1370 ast_copy_string(user->secret, v->value, sizeof(user->secret));
1371 } else if (!strcasecmp(v->name, "accountcode")) {
1372 ast_copy_string(user->accountcode, v->value, sizeof(user->accountcode));
1373 } else if (!strcasecmp(v->name, "host")) {
1374 if (!strcasecmp(v->value, "dynamic")) {
1375 ast_log(LOG_ERROR, "A dynamic host on a type=user does not make any sense\n");
1376 ASTOBJ_UNREF(user, oh323_destroy_user);
1377 return NULL;
1378 } else if (ast_get_ip(&user->addr, v->value)) {
1379 ASTOBJ_UNREF(user, oh323_destroy_user);
1380 return NULL;
1382 /* Let us know we need to use ip authentication */
1383 user->host = 1;
1384 } else if (!strcasecmp(v->name, "amaflags")) {
1385 format = ast_cdr_amaflags2int(v->value);
1386 if (format < 0) {
1387 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
1388 } else {
1389 user->amaflags = format;
1391 } else if (!strcasecmp(v->name, "permit") ||
1392 !strcasecmp(v->name, "deny")) {
1393 user->ha = ast_append_ha(v->name, v->value, user->ha);
1396 ASTOBJ_UNMARK(user);
1397 ast_free_ha(oldha);
1398 return user;
1401 static struct oh323_user *realtime_user(const call_details_t *cd)
1403 struct ast_variable *var, *tmp;
1404 struct oh323_user *user;
1405 char *username;
1407 if (userbyalias)
1408 var = ast_load_realtime("h323", "name", username = cd->call_source_aliases, NULL);
1409 else {
1410 username = (char *)NULL;
1411 var = ast_load_realtime("h323", "host", cd->sourceIp, NULL);
1414 if (!var)
1415 return NULL;
1417 for (tmp = var; tmp; tmp = tmp->next) {
1418 if (!strcasecmp(tmp->name, "type") &&
1419 !(!strcasecmp(tmp->value, "user") || !strcasecmp(tmp->value, "friend"))) {
1420 ast_variables_destroy(var);
1421 return NULL;
1422 } else if (!username && !strcasecmp(tmp->name, "name"))
1423 username = tmp->value;
1426 if (!username) {
1427 ast_log(LOG_WARNING, "Cannot determine user name for IP address %s\n", cd->sourceIp);
1428 ast_variables_destroy(var);
1429 return NULL;
1432 user = build_user(username, var, NULL, 1);
1434 ast_variables_destroy(var);
1436 return user;
1439 static struct oh323_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
1441 struct oh323_peer *peer;
1442 struct ast_ha *oldha;
1443 int found = 0;
1445 peer = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&peerl, name, name, 0, 0, strcmp);
1447 if (peer)
1448 found++;
1449 else {
1450 if (!(peer = (struct oh323_peer*)calloc(1, sizeof(*peer))))
1451 return NULL;
1452 ASTOBJ_INIT(peer);
1454 oldha = peer->ha;
1455 peer->ha = NULL;
1456 memcpy(&peer->options, &global_options, sizeof(peer->options));
1457 peer->addr.sin_port = htons(h323_signalling_port);
1458 peer->addr.sin_family = AF_INET;
1459 if (!found && name)
1460 ast_copy_string(peer->name, name, sizeof(peer->name));
1462 #if 0 /* XXX Port channel variables functionality from chan_sip XXX */
1463 if (peer->chanvars) {
1464 ast_variables_destroy(peer->chanvars);
1465 peer->chanvars = NULL;
1467 #endif
1468 /* Default settings for mailbox */
1469 peer->mailbox[0] = '\0';
1471 for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
1472 if (!update_common_options(v, &peer->options))
1473 continue;
1474 if (!strcasecmp(v->name, "host")) {
1475 if (!strcasecmp(v->value, "dynamic")) {
1476 ast_log(LOG_ERROR, "Dynamic host configuration not implemented.\n");
1477 ASTOBJ_UNREF(peer, oh323_destroy_peer);
1478 return NULL;
1480 if (ast_get_ip(&peer->addr, v->value)) {
1481 ast_log(LOG_ERROR, "Could not determine IP for %s\n", v->value);
1482 ASTOBJ_UNREF(peer, oh323_destroy_peer);
1483 return NULL;
1485 } else if (!strcasecmp(v->name, "port")) {
1486 peer->addr.sin_port = htons(atoi(v->value));
1487 } else if (!strcasecmp(v->name, "permit") ||
1488 !strcasecmp(v->name, "deny")) {
1489 peer->ha = ast_append_ha(v->name, v->value, peer->ha);
1490 } else if (!strcasecmp(v->name, "mailbox")) {
1491 ast_copy_string(peer->mailbox, v->value, sizeof(peer->mailbox));
1494 ASTOBJ_UNMARK(peer);
1495 ast_free_ha(oldha);
1496 return peer;
1499 static struct oh323_peer *realtime_peer(const char *peername, struct sockaddr_in *sin)
1501 struct oh323_peer *peer;
1502 struct ast_variable *var;
1503 struct ast_variable *tmp;
1504 const char *addr;
1506 /* First check on peer name */
1507 if (peername)
1508 var = ast_load_realtime("h323", "name", peername, addr = NULL);
1509 else if (sin) /* Then check on IP address for dynamic peers */
1510 var = ast_load_realtime("h323", "host", addr = ast_inet_ntoa(sin->sin_addr), NULL);
1511 else
1512 return NULL;
1514 if (!var)
1515 return NULL;
1517 for (tmp = var; tmp; tmp = tmp->next) {
1518 /* If this is type=user, then skip this object. */
1519 if (!strcasecmp(tmp->name, "type") &&
1520 !(!strcasecmp(tmp->value, "peer") || !strcasecmp(tmp->value, "friend"))) {
1521 ast_variables_destroy(var);
1522 return NULL;
1523 } else if (!peername && !strcasecmp(tmp->name, "name")) {
1524 peername = tmp->value;
1528 if (!peername) { /* Did not find peer in realtime */
1529 ast_log(LOG_WARNING, "Cannot determine peer name for IP address %s\n", addr);
1530 ast_variables_destroy(var);
1531 return NULL;
1534 /* Peer found in realtime, now build it in memory */
1535 peer = build_peer(peername, var, NULL, 1);
1537 ast_variables_destroy(var);
1539 return peer;
1542 static int oh323_addrcmp_str(struct in_addr inaddr, char *addr)
1544 return strcmp(ast_inet_ntoa(inaddr), addr);
1547 static struct oh323_user *find_user(const call_details_t *cd, int realtime)
1549 struct oh323_user *u;
1551 if (userbyalias)
1552 u = ASTOBJ_CONTAINER_FIND(&userl, cd->call_source_aliases);
1553 else
1554 u = ASTOBJ_CONTAINER_FIND_FULL(&userl, cd->sourceIp, addr.sin_addr, 0, 0, oh323_addrcmp_str);
1556 if (!u && realtime)
1557 u = realtime_user(cd);
1559 if (!u && h323debug)
1560 ast_log(LOG_DEBUG, "Could not find user by name %s or address %s\n", cd->call_source_aliases, cd->sourceIp);
1562 return u;
1565 static int oh323_addrcmp(struct sockaddr_in addr, struct sockaddr_in *sin)
1567 int res;
1569 if (!sin)
1570 res = -1;
1571 else
1572 res = inaddrcmp(&addr , sin);
1574 return res;
1577 static struct oh323_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime)
1579 struct oh323_peer *p;
1581 if (peer)
1582 p = ASTOBJ_CONTAINER_FIND(&peerl, peer);
1583 else
1584 p = ASTOBJ_CONTAINER_FIND_FULL(&peerl, sin, addr, 0, 0, oh323_addrcmp);
1586 if (!p && realtime)
1587 p = realtime_peer(peer, sin);
1589 if (!p && h323debug)
1590 ast_log(LOG_DEBUG, "Could not find peer by name %s or address %s\n", (peer ? peer : "<NONE>"), (sin ? ast_inet_ntoa(sin->sin_addr) : "<NONE>"));
1592 return p;
1595 static int create_addr(struct oh323_pvt *pvt, char *opeer)
1597 struct hostent *hp;
1598 struct ast_hostent ahp;
1599 struct oh323_peer *p;
1600 int portno;
1601 int found = 0;
1602 char *port;
1603 char *hostn;
1604 char peer[256] = "";
1606 ast_copy_string(peer, opeer, sizeof(peer));
1607 port = strchr(peer, ':');
1608 if (port) {
1609 *port = '\0';
1610 port++;
1612 pvt->sa.sin_family = AF_INET;
1613 p = find_peer(peer, NULL, 1);
1614 if (p) {
1615 found++;
1616 memcpy(&pvt->options, &p->options, sizeof(pvt->options));
1617 pvt->jointcapability = pvt->options.capability;
1618 if (pvt->options.dtmfmode) {
1619 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
1620 pvt->nonCodecCapability |= AST_RTP_DTMF;
1621 } else {
1622 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1625 if (p->addr.sin_addr.s_addr) {
1626 pvt->sa.sin_addr = p->addr.sin_addr;
1627 pvt->sa.sin_port = p->addr.sin_port;
1629 ASTOBJ_UNREF(p, oh323_destroy_peer);
1631 if (!p && !found) {
1632 hostn = peer;
1633 if (port) {
1634 portno = atoi(port);
1635 } else {
1636 portno = h323_signalling_port;
1638 hp = ast_gethostbyname(hostn, &ahp);
1639 if (hp) {
1640 memcpy(&pvt->sa.sin_addr, hp->h_addr, sizeof(pvt->sa.sin_addr));
1641 pvt->sa.sin_port = htons(portno);
1642 /* Look peer by address */
1643 p = find_peer(NULL, &pvt->sa, 1);
1644 memcpy(&pvt->options, (p ? &p->options : &global_options), sizeof(pvt->options));
1645 pvt->jointcapability = pvt->options.capability;
1646 if (p) {
1647 ASTOBJ_UNREF(p, oh323_destroy_peer);
1649 if (pvt->options.dtmfmode) {
1650 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
1651 pvt->nonCodecCapability |= AST_RTP_DTMF;
1652 } else {
1653 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1656 return 0;
1657 } else {
1658 ast_log(LOG_WARNING, "No such host: %s\n", peer);
1659 return -1;
1661 } else if (!found) {
1662 return -1;
1663 } else {
1664 return 0;
1667 static struct ast_channel *oh323_request(const char *type, int format, void *data, int *cause)
1669 int oldformat;
1670 struct oh323_pvt *pvt;
1671 struct ast_channel *tmpc = NULL;
1672 char *dest = (char *)data;
1673 char *ext, *host;
1674 char *h323id = NULL;
1675 char tmp[256], tmp1[256];
1677 if (h323debug)
1678 ast_log(LOG_DEBUG, "type=%s, format=%d, data=%s.\n", type, format, (char *)data);
1680 pvt = oh323_alloc(0);
1681 if (!pvt) {
1682 ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
1683 return NULL;
1685 oldformat = format;
1686 format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1);
1687 if (!format) {
1688 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
1689 oh323_destroy(pvt);
1690 if (cause)
1691 *cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
1692 return NULL;
1694 ast_copy_string(tmp, dest, sizeof(tmp));
1695 host = strchr(tmp, '@');
1696 if (host) {
1697 *host = '\0';
1698 host++;
1699 ext = tmp;
1700 } else {
1701 ext = strrchr(tmp, '/');
1702 if (ext)
1703 *ext++ = '\0';
1704 host = tmp;
1706 strtok_r(host, "/", &(h323id));
1707 if (!ast_strlen_zero(h323id)) {
1708 h323_set_id(h323id);
1710 if (ext) {
1711 ast_copy_string(pvt->exten, ext, sizeof(pvt->exten));
1713 if (h323debug)
1714 ast_log(LOG_DEBUG, "Extension: %s Host: %s\n", pvt->exten, host);
1716 if (gatekeeper_disable) {
1717 if (create_addr(pvt, host)) {
1718 oh323_destroy(pvt);
1719 if (cause)
1720 *cause = AST_CAUSE_DESTINATION_OUT_OF_ORDER;
1721 return NULL;
1724 else {
1725 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
1726 pvt->jointcapability = pvt->options.capability;
1727 if (pvt->options.dtmfmode) {
1728 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
1729 pvt->nonCodecCapability |= AST_RTP_DTMF;
1730 } else {
1731 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1736 ast_mutex_lock(&caplock);
1737 /* Generate unique channel identifier */
1738 snprintf(tmp1, sizeof(tmp1)-1, "%s-%u", host, ++unique);
1739 tmp1[sizeof(tmp1)-1] = '\0';
1740 ast_mutex_unlock(&caplock);
1742 ast_mutex_lock(&pvt->lock);
1743 tmpc = __oh323_new(pvt, AST_STATE_DOWN, tmp1);
1744 ast_mutex_unlock(&pvt->lock);
1745 if (!tmpc) {
1746 oh323_destroy(pvt);
1747 if (cause)
1748 *cause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
1750 ast_update_use_count();
1751 restart_monitor();
1752 return tmpc;
1755 /** Find a call by alias */
1756 static struct oh323_alias *find_alias(const char *source_aliases, int realtime)
1758 struct oh323_alias *a;
1760 a = ASTOBJ_CONTAINER_FIND(&aliasl, source_aliases);
1762 if (!a && realtime)
1763 a = realtime_alias(source_aliases);
1765 return a;
1769 * Callback for sending digits from H.323 up to asterisk
1772 static int receive_digit(unsigned call_reference, char digit, const char *token, int duration)
1774 struct oh323_pvt *pvt;
1775 int res;
1777 pvt = find_call_locked(call_reference, token);
1778 if (!pvt) {
1779 ast_log(LOG_ERROR, "Received digit '%c' (%u ms) for call %s without private structure\n", digit, duration, token);
1780 return -1;
1782 if (h323debug)
1783 ast_log(LOG_DTMF, "Received %s digit '%c' (%u ms) for call %s\n", (digit == ' ' ? "update for" : "new"), (digit == ' ' ? pvt->curDTMF : digit), duration, token);
1785 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
1786 if (digit == '!')
1787 res = ast_queue_control(pvt->owner, AST_CONTROL_FLASH);
1788 else {
1789 struct ast_frame f = {
1790 .frametype = AST_FRAME_DTMF_END,
1791 .subclass = digit,
1792 .samples = duration * 8,
1793 .len = duration,
1794 .src = "SEND_DIGIT",
1796 if (digit == ' ') { /* signalUpdate message */
1797 f.subclass = pvt->curDTMF;
1798 AST_SCHED_DEL(sched, pvt->DTMFsched);
1799 } else { /* Regular input or signal message */
1800 if (pvt->DTMFsched >= 0) {
1801 /* We still don't send DTMF END from previous event, send it now */
1802 AST_SCHED_DEL(sched, pvt->DTMFsched);
1803 f.subclass = pvt->curDTMF;
1804 f.samples = f.len = 0;
1805 ast_queue_frame(pvt->owner, &f);
1806 /* Restore values */
1807 f.subclass = digit;
1808 f.samples = duration * 8;
1809 f.len = duration;
1811 if (duration) { /* This is a signal, signalUpdate follows */
1812 f.frametype = AST_FRAME_DTMF_BEGIN;
1813 pvt->DTMFsched = ast_sched_add(sched, duration, oh323_simulate_dtmf_end, pvt);
1814 if (h323debug)
1815 ast_log(LOG_DTMF, "Scheduled DTMF END simulation for %d ms, id=%d\n", duration, pvt->DTMFsched);
1817 pvt->curDTMF = digit;
1819 res = ast_queue_frame(pvt->owner, &f);
1821 ast_channel_unlock(pvt->owner);
1822 } else {
1823 if (digit == '!')
1824 pvt->newcontrol = AST_CONTROL_FLASH;
1825 else {
1826 pvt->newduration = duration;
1827 pvt->newdigit = digit;
1829 res = 0;
1831 ast_mutex_unlock(&pvt->lock);
1832 return res;
1836 * Callback function used to inform the H.323 stack of the local rtp ip/port details
1838 * Returns the local RTP information
1840 static struct rtp_info *external_rtp_create(unsigned call_reference, const char * token)
1842 struct oh323_pvt *pvt;
1843 struct sockaddr_in us;
1844 struct rtp_info *info;
1846 info = (struct rtp_info *)malloc(sizeof(struct rtp_info));
1847 if (!info) {
1848 ast_log(LOG_ERROR, "Unable to allocated info structure, this is very bad\n");
1849 return NULL;
1851 pvt = find_call_locked(call_reference, token);
1852 if (!pvt) {
1853 free(info);
1854 ast_log(LOG_ERROR, "Unable to find call %s(%d)\n", token, call_reference);
1855 return NULL;
1857 if (!pvt->rtp)
1858 __oh323_rtp_create(pvt);
1859 if (!pvt->rtp) {
1860 ast_mutex_unlock(&pvt->lock);
1861 free(info);
1862 ast_log(LOG_ERROR, "No RTP stream is available for call %s (%d)", token, call_reference);
1863 return NULL;
1865 /* figure out our local RTP port and tell the H.323 stack about it */
1866 ast_rtp_get_us(pvt->rtp, &us);
1867 ast_mutex_unlock(&pvt->lock);
1869 ast_copy_string(info->addr, ast_inet_ntoa(us.sin_addr), sizeof(info->addr));
1870 info->port = ntohs(us.sin_port);
1871 if (h323debug)
1872 ast_log(LOG_DEBUG, "Sending RTP 'US' %s:%d\n", info->addr, info->port);
1873 return info;
1877 * Definition taken from rtp.c for rtpPayloadType because we need it here.
1879 struct rtpPayloadType {
1880 int isAstFormat; /* whether the following code is an AST_FORMAT */
1881 int code;
1885 * Call-back function passing remote ip/port information from H.323 to asterisk
1887 * Returns nothing
1889 static void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort, const char *token, int pt)
1891 struct oh323_pvt *pvt;
1892 struct sockaddr_in them;
1893 struct rtpPayloadType rtptype;
1894 int nativeformats_changed;
1895 enum { NEED_NONE, NEED_HOLD, NEED_UNHOLD } rtp_change = NEED_NONE;
1897 if (h323debug)
1898 ast_log(LOG_DEBUG, "Setting up RTP connection for %s\n", token);
1900 /* Find the call or allocate a private structure if call not found */
1901 pvt = find_call_locked(call_reference, token);
1902 if (!pvt) {
1903 ast_log(LOG_ERROR, "Something is wrong: rtp\n");
1904 return;
1906 if (pvt->alreadygone) {
1907 ast_mutex_unlock(&pvt->lock);
1908 return;
1911 if (!pvt->rtp)
1912 __oh323_rtp_create(pvt);
1914 them.sin_family = AF_INET;
1915 /* only works for IPv4 */
1916 them.sin_addr.s_addr = inet_addr(remoteIp);
1917 them.sin_port = htons(remotePort);
1919 if (them.sin_addr.s_addr) {
1920 ast_rtp_set_peer(pvt->rtp, &them);
1921 if (pvt->recvonly) {
1922 pvt->recvonly = 0;
1923 rtp_change = NEED_UNHOLD;
1925 } else {
1926 ast_rtp_stop(pvt->rtp);
1927 if (!pvt->recvonly) {
1928 pvt->recvonly = 1;
1929 rtp_change = NEED_HOLD;
1933 /* Change native format to reflect information taken from OLC/OLCAck */
1934 nativeformats_changed = 0;
1935 if (pt != 128 && pvt->rtp) { /* Payload type is invalid, so try to use previously decided */
1936 rtptype = ast_rtp_lookup_pt(pvt->rtp, pt);
1937 if (h323debug)
1938 ast_log(LOG_DEBUG, "Native format is set to %d from %d by RTP payload type %d\n", rtptype.code, pvt->nativeformats, pt);
1939 if (pvt->nativeformats != rtptype.code) {
1940 pvt->nativeformats = rtptype.code;
1941 nativeformats_changed = 1;
1943 } else if (h323debug)
1944 ast_log(LOG_NOTICE, "Payload type is unknown, formats isn't changed\n");
1946 /* Don't try to lock the channel if nothing changed */
1947 if (nativeformats_changed || pvt->options.progress_audio || (rtp_change != NEED_NONE)) {
1948 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
1949 /* Re-build translation path only if native format(s) has been changed */
1950 if (pvt->owner->nativeformats != pvt->nativeformats) {
1951 if (h323debug)
1952 ast_log(LOG_DEBUG, "Native format changed to %d from %d, read format is %d, write format is %d\n", pvt->nativeformats, pvt->owner->nativeformats, pvt->owner->readformat, pvt->owner->writeformat);
1953 pvt->owner->nativeformats = pvt->nativeformats;
1954 ast_set_read_format(pvt->owner, pvt->owner->readformat);
1955 ast_set_write_format(pvt->owner, pvt->owner->writeformat);
1957 if (pvt->options.progress_audio)
1958 ast_queue_control(pvt->owner, AST_CONTROL_PROGRESS);
1959 switch (rtp_change) {
1960 case NEED_HOLD:
1961 ast_queue_control(pvt->owner, AST_CONTROL_HOLD);
1962 break;
1963 case NEED_UNHOLD:
1964 ast_queue_control(pvt->owner, AST_CONTROL_UNHOLD);
1965 break;
1966 default:
1967 break;
1969 ast_channel_unlock(pvt->owner);
1971 else {
1972 if (pvt->options.progress_audio)
1973 pvt->newcontrol = AST_CONTROL_PROGRESS;
1974 else if (rtp_change == NEED_HOLD)
1975 pvt->newcontrol = AST_CONTROL_HOLD;
1976 else if (rtp_change == NEED_UNHOLD)
1977 pvt->newcontrol = AST_CONTROL_UNHOLD;
1978 if (h323debug)
1979 ast_log(LOG_DEBUG, "RTP connection preparation for %s is pending...\n", token);
1982 ast_mutex_unlock(&pvt->lock);
1984 if (h323debug)
1985 ast_log(LOG_DEBUG, "RTP connection prepared for %s\n", token);
1987 return;
1991 * Call-back function to signal asterisk that the channel has been answered
1992 * Returns nothing
1994 static void connection_made(unsigned call_reference, const char *token)
1996 struct oh323_pvt *pvt;
1998 if (h323debug)
1999 ast_log(LOG_DEBUG, "Call %s answered\n", token);
2001 pvt = find_call_locked(call_reference, token);
2002 if (!pvt) {
2003 ast_log(LOG_ERROR, "Something is wrong: connection\n");
2004 return;
2007 /* Inform asterisk about remote party connected only on outgoing calls */
2008 if (!pvt->outgoing) {
2009 ast_mutex_unlock(&pvt->lock);
2010 return;
2012 /* Do not send ANSWER message more than once */
2013 if (!pvt->connection_established) {
2014 pvt->connection_established = 1;
2015 update_state(pvt, -1, AST_CONTROL_ANSWER);
2017 ast_mutex_unlock(&pvt->lock);
2018 return;
2021 static int progress(unsigned call_reference, const char *token, int inband)
2023 struct oh323_pvt *pvt;
2025 if (h323debug)
2026 ast_log(LOG_DEBUG, "Received ALERT/PROGRESS message for %s tones\n", (inband ? "inband" : "self-generated"));
2028 pvt = find_call_locked(call_reference, token);
2029 if (!pvt) {
2030 ast_log(LOG_ERROR, "Private structure not found in progress.\n");
2031 return -1;
2033 if (!pvt->owner) {
2034 ast_mutex_unlock(&pvt->lock);
2035 ast_log(LOG_ERROR, "No Asterisk channel associated with private structure.\n");
2036 return -1;
2038 update_state(pvt, -1, (inband ? AST_CONTROL_PROGRESS : AST_CONTROL_RINGING));
2039 ast_mutex_unlock(&pvt->lock);
2041 return 0;
2045 * Call-back function for incoming calls
2047 * Returns 1 on success
2049 static call_options_t *setup_incoming_call(call_details_t *cd)
2051 struct oh323_pvt *pvt;
2052 struct oh323_user *user = NULL;
2053 struct oh323_alias *alias = NULL;
2055 if (h323debug)
2056 ast_log(LOG_DEBUG, "Setting up incoming call for %s\n", cd->call_token);
2058 /* allocate the call*/
2059 pvt = oh323_alloc(cd->call_reference);
2061 if (!pvt) {
2062 ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
2063 cleanup_call_details(cd);
2064 return NULL;
2067 /* Populate the call details in the private structure */
2068 memcpy(&pvt->cd, cd, sizeof(pvt->cd));
2069 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
2070 pvt->jointcapability = pvt->options.capability;
2072 if (h323debug) {
2073 ast_verbose(VERBOSE_PREFIX_3 "Setting up Call\n");
2074 ast_verbose(VERBOSE_PREFIX_3 " \tCall token: [%s]\n", pvt->cd.call_token);
2075 ast_verbose(VERBOSE_PREFIX_3 " \tCalling party name: [%s]\n", pvt->cd.call_source_name);
2076 ast_verbose(VERBOSE_PREFIX_3 " \tCalling party number: [%s]\n", pvt->cd.call_source_e164);
2077 ast_verbose(VERBOSE_PREFIX_3 " \tCalled party name: [%s]\n", pvt->cd.call_dest_alias);
2078 ast_verbose(VERBOSE_PREFIX_3 " \tCalled party number: [%s]\n", pvt->cd.call_dest_e164);
2079 if (pvt->cd.redirect_reason >= 0)
2080 ast_verbose(VERBOSE_PREFIX_3 " \tRedirecting party number: [%s] (reason %d)\n", pvt->cd.redirect_number, pvt->cd.redirect_reason);
2081 ast_verbose(VERBOSE_PREFIX_3 " \tCalling party IP: [%s]\n", pvt->cd.sourceIp);
2084 /* Decide if we are allowing Gatekeeper routed calls*/
2085 if ((!strcasecmp(cd->sourceIp, gatekeeper)) && (gkroute == -1) && !gatekeeper_disable) {
2086 if (!ast_strlen_zero(cd->call_dest_e164)) {
2087 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2088 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2089 } else {
2090 alias = find_alias(cd->call_dest_alias, 1);
2091 if (!alias) {
2092 ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd->call_dest_alias);
2093 oh323_destroy(pvt);
2094 return NULL;
2096 ast_copy_string(pvt->exten, alias->name, sizeof(pvt->exten));
2097 ast_copy_string(pvt->context, alias->context, sizeof(pvt->context));
2099 } else {
2100 /* Either this call is not from the Gatekeeper
2101 or we are not allowing gk routed calls */
2102 user = find_user(cd, 1);
2103 if (!user) {
2104 if (!acceptAnonymous) {
2105 ast_log(LOG_NOTICE, "Anonymous call from '%s@%s' rejected\n", pvt->cd.call_source_aliases, pvt->cd.sourceIp);
2106 oh323_destroy(pvt);
2107 return NULL;
2109 if (ast_strlen_zero(default_context)) {
2110 ast_log(LOG_ERROR, "Call from '%s@%s' rejected due to no default context\n", pvt->cd.call_source_aliases, pvt->cd.sourceIp);
2111 oh323_destroy(pvt);
2112 return NULL;
2114 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2115 if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
2116 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2117 } else {
2118 ast_copy_string(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten));
2120 if (h323debug)
2121 ast_log(LOG_DEBUG, "Sending %s@%s to context [%s] extension %s\n", cd->call_source_aliases, cd->sourceIp, pvt->context, pvt->exten);
2122 } else {
2123 if (user->host) {
2124 if (strcasecmp(cd->sourceIp, ast_inet_ntoa(user->addr.sin_addr))) {
2125 if (ast_strlen_zero(user->context)) {
2126 if (ast_strlen_zero(default_context)) {
2127 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s) and no default context\n", user->name, cd->sourceIp);
2128 oh323_destroy(pvt);
2129 ASTOBJ_UNREF(user, oh323_destroy_user);
2130 return NULL;
2132 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2133 } else {
2134 ast_copy_string(pvt->context, user->context, sizeof(pvt->context));
2136 pvt->exten[0] = 'i';
2137 pvt->exten[1] = '\0';
2138 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s)s\n", user->name, cd->sourceIp);
2139 oh323_destroy(pvt);
2140 ASTOBJ_UNREF(user, oh323_destroy_user);
2141 return NULL; /* XXX: Hmmm... Why to setup context if we drop connection immediately??? */
2144 ast_copy_string(pvt->context, user->context, sizeof(pvt->context));
2145 memcpy(&pvt->options, &user->options, sizeof(pvt->options));
2146 pvt->jointcapability = pvt->options.capability;
2147 if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
2148 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2149 } else {
2150 ast_copy_string(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten));
2152 if (!ast_strlen_zero(user->accountcode)) {
2153 ast_copy_string(pvt->accountcode, user->accountcode, sizeof(pvt->accountcode));
2155 if (user->amaflags) {
2156 pvt->amaflags = user->amaflags;
2158 ASTOBJ_UNREF(user, oh323_destroy_user);
2161 return &pvt->options;
2165 * Call-back function to start PBX when OpenH323 ready to serve incoming call
2167 * Returns 1 on success
2169 static int answer_call(unsigned call_reference, const char *token)
2171 struct oh323_pvt *pvt;
2172 struct ast_channel *c = NULL;
2173 enum {ext_original, ext_s, ext_i, ext_notexists} try_exten;
2174 char tmp_exten[sizeof(pvt->exten)];
2176 if (h323debug)
2177 ast_log(LOG_DEBUG, "Preparing Asterisk to answer for %s\n", token);
2179 /* Find the call or allocate a private structure if call not found */
2180 pvt = find_call_locked(call_reference, token);
2181 if (!pvt) {
2182 ast_log(LOG_ERROR, "Something is wrong: answer_call\n");
2183 return 0;
2185 /* Check if requested extension@context pair exists in the dialplan */
2186 ast_copy_string(tmp_exten, pvt->exten, sizeof(tmp_exten));
2188 /* Try to find best extension in specified context */
2189 if ((tmp_exten[0] != '\0') && (tmp_exten[1] == '\0')) {
2190 if (tmp_exten[0] == 's')
2191 try_exten = ext_s;
2192 else if (tmp_exten[0] == 'i')
2193 try_exten = ext_i;
2194 else
2195 try_exten = ext_original;
2196 } else
2197 try_exten = ext_original;
2198 do {
2199 if (ast_exists_extension(NULL, pvt->context, tmp_exten, 1, NULL))
2200 break;
2201 switch (try_exten) {
2202 case ext_original:
2203 tmp_exten[0] = 's';
2204 tmp_exten[1] = '\0';
2205 try_exten = ext_s;
2206 break;
2207 case ext_s:
2208 tmp_exten[0] = 'i';
2209 try_exten = ext_i;
2210 break;
2211 case ext_i:
2212 try_exten = ext_notexists;
2213 break;
2214 default:
2215 break;
2217 } while (try_exten != ext_notexists);
2219 /* Drop the call if we don't have <exten>, s and i extensions */
2220 if (try_exten == ext_notexists) {
2221 ast_log(LOG_NOTICE, "Dropping call because extensions '%s', 's' and 'i' doesn't exists in context [%s]\n", pvt->exten, pvt->context);
2222 ast_mutex_unlock(&pvt->lock);
2223 h323_clear_call(token, AST_CAUSE_UNALLOCATED);
2224 return 0;
2225 } else if ((try_exten != ext_original) && (strcmp(pvt->exten, tmp_exten) != 0)) {
2226 if (h323debug)
2227 ast_log(LOG_DEBUG, "Going to extension %s@%s because %s@%s isn't exists\n", tmp_exten, pvt->context, pvt->exten, pvt->context);
2228 ast_copy_string(pvt->exten, tmp_exten, sizeof(pvt->exten));
2231 /* allocate a channel and tell asterisk about it */
2232 c = __oh323_new(pvt, AST_STATE_RINGING, pvt->cd.call_token);
2234 /* And release when done */
2235 ast_mutex_unlock(&pvt->lock);
2236 if (!c) {
2237 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
2238 return 0;
2240 return 1;
2244 * Call-back function to establish an outgoing H.323 call
2246 * Returns 1 on success
2248 static int setup_outgoing_call(call_details_t *cd)
2250 /* Use argument here or free it immediately */
2251 cleanup_call_details(cd);
2253 return 1;
2257 * Call-back function to signal asterisk that the channel is ringing
2258 * Returns nothing
2260 static void chan_ringing(unsigned call_reference, const char *token)
2262 struct oh323_pvt *pvt;
2264 if (h323debug)
2265 ast_log(LOG_DEBUG, "Ringing on %s\n", token);
2267 pvt = find_call_locked(call_reference, token);
2268 if (!pvt) {
2269 ast_log(LOG_ERROR, "Something is wrong: ringing\n");
2270 return;
2272 if (!pvt->owner) {
2273 ast_mutex_unlock(&pvt->lock);
2274 ast_log(LOG_ERROR, "Channel has no owner\n");
2275 return;
2277 update_state(pvt, AST_STATE_RINGING, AST_CONTROL_RINGING);
2278 ast_mutex_unlock(&pvt->lock);
2279 return;
2283 * Call-back function to cleanup communication
2284 * Returns nothing,
2286 static void cleanup_connection(unsigned call_reference, const char *call_token)
2288 struct oh323_pvt *pvt;
2290 if (h323debug)
2291 ast_log(LOG_DEBUG, "Cleaning connection to %s\n", call_token);
2293 while (1) {
2294 pvt = find_call_locked(call_reference, call_token);
2295 if (!pvt) {
2296 if (h323debug)
2297 ast_log(LOG_DEBUG, "No connection for %s\n", call_token);
2298 return;
2300 if (!pvt->owner || !ast_channel_trylock(pvt->owner))
2301 break;
2302 #if 1
2303 #ifdef DEBUG_THREADS
2304 ast_log(LOG_NOTICE, "Avoiding H.323 destory deadlock on %s, locked at %ld/%d by %s (%s:%d)\n", call_token, pvt->owner->lock.thread[0], pvt->owner->lock.reentrancy, pvt->owner->lock.func[0], pvt->owner->lock.file[0], pvt->owner->lock.lineno[0]);
2305 #else
2306 ast_log(LOG_NOTICE, "Avoiding H.323 destory deadlock on %s\n", call_token);
2307 #endif
2308 #endif
2309 ast_mutex_unlock(&pvt->lock);
2310 usleep(1);
2312 if (pvt->rtp) {
2313 /* Immediately stop RTP */
2314 ast_rtp_destroy(pvt->rtp);
2315 pvt->rtp = NULL;
2317 /* Free dsp used for in-band DTMF detection */
2318 if (pvt->vad) {
2319 ast_dsp_free(pvt->vad);
2320 pvt->vad = NULL;
2322 cleanup_call_details(&pvt->cd);
2323 pvt->alreadygone = 1;
2324 /* Send hangup */
2325 if (pvt->owner) {
2326 pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2327 ast_queue_hangup(pvt->owner);
2328 ast_channel_unlock(pvt->owner);
2330 ast_mutex_unlock(&pvt->lock);
2331 if (h323debug)
2332 ast_log(LOG_DEBUG, "Connection to %s cleaned\n", call_token);
2333 return;
2336 static void hangup_connection(unsigned int call_reference, const char *token, int cause)
2338 struct oh323_pvt *pvt;
2340 if (h323debug) {
2341 ast_log(LOG_DEBUG, "Hanging up connection to %s with cause %d\n", token, cause);
2344 pvt = find_call_locked(call_reference, token);
2345 if (!pvt) {
2346 if (h323debug) {
2347 ast_log(LOG_DEBUG, "Connection to %s already cleared\n", token);
2349 return;
2351 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
2352 pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2353 pvt->owner->hangupcause = pvt->hangupcause = cause;
2354 ast_queue_hangup(pvt->owner);
2355 ast_channel_unlock(pvt->owner);
2357 else {
2358 pvt->needhangup = 1;
2359 pvt->hangupcause = cause;
2360 if (h323debug)
2361 ast_log(LOG_DEBUG, "Hangup for %s is pending\n", token);
2363 ast_mutex_unlock(&pvt->lock);
2366 static void set_dtmf_payload(unsigned call_reference, const char *token, int payload)
2368 struct oh323_pvt *pvt;
2370 if (h323debug)
2371 ast_log(LOG_DEBUG, "Setting DTMF payload to %d on %s\n", payload, token);
2373 pvt = find_call_locked(call_reference, token);
2374 if (!pvt) {
2375 return;
2377 if (pvt->rtp) {
2378 ast_rtp_set_rtpmap_type(pvt->rtp, payload, "audio", "telephone-event", 0);
2380 pvt->dtmf_pt = payload;
2381 ast_mutex_unlock(&pvt->lock);
2382 if (h323debug)
2383 ast_log(LOG_DEBUG, "DTMF payload on %s set to %d\n", token, payload);
2386 static void set_peer_capabilities(unsigned call_reference, const char *token, int capabilities, struct ast_codec_pref *prefs)
2388 struct oh323_pvt *pvt;
2390 if (h323debug)
2391 ast_log(LOG_DEBUG, "Got remote capabilities from connection %s\n", token);
2393 pvt = find_call_locked(call_reference, token);
2394 if (!pvt)
2395 return;
2396 pvt->peercapability = capabilities;
2397 pvt->jointcapability = pvt->options.capability & capabilities;
2398 if (prefs) {
2399 memcpy(&pvt->peer_prefs, prefs, sizeof(pvt->peer_prefs));
2400 if (h323debug) {
2401 int i;
2402 for (i = 0; i < 32; ++i) {
2403 if (!prefs->order[i])
2404 break;
2405 ast_log(LOG_DEBUG, "prefs[%d]=%s:%d\n", i, (prefs->order[i] ? ast_getformatname(1 << (prefs->order[i]-1)) : "<none>"), prefs->framing[i]);
2408 if (pvt->rtp)
2409 ast_rtp_codec_setpref(pvt->rtp, &pvt->peer_prefs);
2411 ast_mutex_unlock(&pvt->lock);
2414 static void set_local_capabilities(unsigned call_reference, const char *token)
2416 struct oh323_pvt *pvt;
2417 int capability, dtmfmode, pref_codec;
2418 struct ast_codec_pref prefs;
2420 if (h323debug)
2421 ast_log(LOG_DEBUG, "Setting capabilities for connection %s\n", token);
2423 pvt = find_call_locked(call_reference, token);
2424 if (!pvt)
2425 return;
2426 capability = (pvt->jointcapability) ? pvt->jointcapability : pvt->options.capability;
2427 dtmfmode = pvt->options.dtmfmode;
2428 prefs = pvt->options.prefs;
2429 pref_codec = pvt->pref_codec;
2430 ast_mutex_unlock(&pvt->lock);
2431 h323_set_capabilities(token, capability, dtmfmode, &prefs, pref_codec);
2433 if (h323debug)
2434 ast_log(LOG_DEBUG, "Capabilities for connection %s is set\n", token);
2437 static void *do_monitor(void *data)
2439 int res;
2440 int reloading;
2441 struct oh323_pvt *oh323 = NULL;
2443 for(;;) {
2444 /* Check for a reload request */
2445 ast_mutex_lock(&h323_reload_lock);
2446 reloading = h323_reloading;
2447 h323_reloading = 0;
2448 ast_mutex_unlock(&h323_reload_lock);
2449 if (reloading) {
2450 if (option_verbose > 0) {
2451 ast_verbose(VERBOSE_PREFIX_1 "Reloading H.323\n");
2453 h323_do_reload();
2455 /* Check for interfaces needing to be killed */
2456 if (!ast_mutex_trylock(&iflock)) {
2457 #if 1
2458 do {
2459 for (oh323 = iflist; oh323; oh323 = oh323->next) {
2460 if (!ast_mutex_trylock(&oh323->lock)) {
2461 if (oh323->needdestroy) {
2462 __oh323_destroy(oh323);
2463 break;
2465 ast_mutex_unlock(&oh323->lock);
2468 } while (/*oh323*/ 0);
2469 #else
2470 restartsearch:
2471 oh323 = iflist;
2472 while(oh323) {
2473 if (!ast_mutex_trylock(&oh323->lock)) {
2474 if (oh323->needdestroy) {
2475 __oh323_destroy(oh323);
2476 goto restartsearch;
2478 ast_mutex_unlock(&oh323->lock);
2479 oh323 = oh323->next;
2482 #endif
2483 ast_mutex_unlock(&iflock);
2484 } else
2485 oh323 = (struct oh323_pvt *)1; /* Force fast loop */
2486 pthread_testcancel();
2487 /* Wait for sched or io */
2488 res = ast_sched_wait(sched);
2489 if ((res < 0) || (res > 1000)) {
2490 res = 1000;
2492 /* Do not wait if some channel(s) is destroyed, probably, more available too */
2493 if (oh323)
2494 res = 1;
2495 res = ast_io_wait(io, res);
2496 pthread_testcancel();
2497 ast_mutex_lock(&monlock);
2498 if (res >= 0) {
2499 ast_sched_runq(sched);
2501 ast_mutex_unlock(&monlock);
2503 /* Never reached */
2504 return NULL;
2507 static int restart_monitor(void)
2509 pthread_attr_t attr;
2510 /* If we're supposed to be stopped -- stay stopped */
2511 if (ast_mutex_lock(&monlock)) {
2512 ast_log(LOG_WARNING, "Unable to lock monitor\n");
2513 return -1;
2515 if (monitor_thread == AST_PTHREADT_STOP) {
2516 ast_mutex_unlock(&monlock);
2517 return 0;
2519 if (monitor_thread == pthread_self()) {
2520 ast_mutex_unlock(&monlock);
2521 ast_log(LOG_WARNING, "Cannot kill myself\n");
2522 return -1;
2524 if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
2525 /* Wake up the thread */
2526 pthread_kill(monitor_thread, SIGURG);
2527 } else {
2528 pthread_attr_init(&attr);
2529 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2530 /* Start a new monitor */
2531 if (ast_pthread_create_background(&monitor_thread, &attr, do_monitor, NULL) < 0) {
2532 monitor_thread = AST_PTHREADT_NULL;
2533 ast_mutex_unlock(&monlock);
2534 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
2535 pthread_attr_destroy(&attr);
2536 return -1;
2538 pthread_attr_destroy(&attr);
2540 ast_mutex_unlock(&monlock);
2541 return 0;
2544 static int h323_do_trace(int fd, int argc, char *argv[])
2546 if (argc != 4) {
2547 return RESULT_SHOWUSAGE;
2549 h323_debug(1, atoi(argv[3]));
2550 ast_cli(fd, "H.323 trace set to level %s\n", argv[2]);
2551 return RESULT_SUCCESS;
2554 static int h323_no_trace(int fd, int argc, char *argv[])
2556 if (argc < 3 || argc > 4) {
2557 return RESULT_SHOWUSAGE;
2559 h323_debug(0,0);
2560 ast_cli(fd, "H.323 trace disabled\n");
2561 return RESULT_SUCCESS;
2564 static int h323_do_debug(int fd, int argc, char *argv[])
2566 if (argc < 2 || argc > 3) {
2567 return RESULT_SHOWUSAGE;
2569 h323debug = 1;
2570 ast_cli(fd, "H.323 debug enabled\n");
2571 return RESULT_SUCCESS;
2574 static int h323_no_debug(int fd, int argc, char *argv[])
2576 if (argc < 3 || argc > 4) {
2577 return RESULT_SHOWUSAGE;
2579 h323debug = 0;
2580 ast_cli(fd, "H.323 debug disabled\n");
2581 return RESULT_SUCCESS;
2584 static int h323_gk_cycle(int fd, int argc, char *argv[])
2586 if (argc != 3) {
2587 return RESULT_SHOWUSAGE;
2589 h323_gk_urq();
2591 /* Possibly register with a GK */
2592 if (!gatekeeper_disable) {
2593 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
2594 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
2597 return RESULT_SUCCESS;
2600 static int h323_ep_hangup(int fd, int argc, char *argv[])
2602 if (argc != 3) {
2603 return RESULT_SHOWUSAGE;
2605 if (h323_soft_hangup(argv[2])) {
2606 ast_verbose(VERBOSE_PREFIX_3 "Hangup succeeded on %s\n", argv[2]);
2607 } else {
2608 ast_verbose(VERBOSE_PREFIX_3 "Hangup failed for %s\n", argv[2]);
2610 return RESULT_SUCCESS;
2613 static int h323_tokens_show(int fd, int argc, char *argv[])
2615 if (argc != 3) {
2616 return RESULT_SHOWUSAGE;
2618 h323_show_tokens();
2619 return RESULT_SUCCESS;
2622 static char trace_usage[] =
2623 "Usage: h.323 trace <level num>\n"
2624 " Enables H.323 stack tracing for debugging purposes\n";
2626 static char no_trace_usage[] =
2627 "Usage: h.323 trace off\n"
2628 " Disables H.323 stack tracing for debugging purposes\n";
2630 static char debug_usage[] =
2631 "Usage: h.323 debug\n"
2632 " Enables H.323 debug output\n";
2634 static char no_debug_usage[] =
2635 "Usage: h.323 debug off\n"
2636 " Disables H.323 debug output\n";
2638 static char show_cycle_usage[] =
2639 "Usage: h.323 gk cycle\n"
2640 " Manually re-register with the Gatekeper (Currently Disabled)\n";
2642 static char show_hangup_usage[] =
2643 "Usage: h.323 hangup <token>\n"
2644 " Manually try to hang up call identified by <token>\n";
2646 static char show_tokens_usage[] =
2647 "Usage: h.323 show tokens\n"
2648 " Print out all active call tokens\n";
2650 static char h323_reload_usage[] =
2651 "Usage: h323 reload\n"
2652 " Reloads H.323 configuration from h323.conf\n";
2654 static struct ast_cli_entry cli_h323_no_trace_deprecated = {
2655 { "h.323", "no", "trace", NULL },
2656 h323_no_trace, "Disable H.323 Stack Tracing",
2657 no_trace_usage };
2659 static struct ast_cli_entry cli_h323_no_debug_deprecated = {
2660 { "h.323", "no", "debug", NULL },
2661 h323_no_debug, "Disable H.323 debug",
2662 no_debug_usage };
2664 static struct ast_cli_entry cli_h323_debug_deprecated = {
2665 { "h.323", "debug", NULL },
2666 h323_do_debug, "Enable H.323 debug",
2667 debug_usage };
2669 static struct ast_cli_entry cli_h323_trace_deprecated = {
2670 { "h.323", "trace", NULL },
2671 h323_do_trace, "Enable H.323 Stack Tracing",
2672 trace_usage };
2674 static struct ast_cli_entry cli_h323_gk_cycle_deprecated = {
2675 { "h.323", "gk", "cycle", NULL },
2676 h323_gk_cycle, "Manually re-register with the Gatekeper",
2677 show_cycle_usage };
2679 static struct ast_cli_entry cli_h323[] = {
2680 { { "h323", "set", "trace", NULL },
2681 h323_do_trace, "Enable H.323 Stack Tracing",
2682 trace_usage, NULL, &cli_h323_trace_deprecated },
2684 { { "h323", "set", "trace", "off", NULL },
2685 h323_no_trace, "Disable H.323 Stack Tracing",
2686 no_trace_usage, NULL, &cli_h323_no_trace_deprecated },
2688 { { "h323", "set", "debug", NULL },
2689 h323_do_debug, "Enable H.323 debug",
2690 debug_usage, NULL, &cli_h323_debug_deprecated },
2692 { { "h323", "set", "debug", "off", NULL },
2693 h323_no_debug, "Disable H.323 debug",
2694 no_debug_usage, NULL, &cli_h323_no_debug_deprecated },
2696 { { "h323", "cycle", "gk", NULL },
2697 h323_gk_cycle, "Manually re-register with the Gatekeper",
2698 show_cycle_usage, NULL, &cli_h323_gk_cycle_deprecated },
2700 { { "h323", "hangup", NULL },
2701 h323_ep_hangup, "Manually try to hang up a call",
2702 show_hangup_usage },
2704 { { "h323", "show", "tokens", NULL },
2705 h323_tokens_show, "Show all active call tokens",
2706 show_tokens_usage },
2709 static void delete_users(void)
2711 int pruned = 0;
2713 /* Delete all users */
2714 ASTOBJ_CONTAINER_WRLOCK(&userl);
2715 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
2716 ASTOBJ_RDLOCK(iterator);
2717 ASTOBJ_MARK(iterator);
2718 ++pruned;
2719 ASTOBJ_UNLOCK(iterator);
2720 } while (0) );
2721 if (pruned) {
2722 ASTOBJ_CONTAINER_PRUNE_MARKED(&userl, oh323_destroy_user);
2724 ASTOBJ_CONTAINER_UNLOCK(&userl);
2726 ASTOBJ_CONTAINER_WRLOCK(&peerl);
2727 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
2728 ASTOBJ_RDLOCK(iterator);
2729 ASTOBJ_MARK(iterator);
2730 ASTOBJ_UNLOCK(iterator);
2731 } while (0) );
2732 ASTOBJ_CONTAINER_UNLOCK(&peerl);
2735 static void delete_aliases(void)
2737 int pruned = 0;
2739 /* Delete all aliases */
2740 ASTOBJ_CONTAINER_WRLOCK(&aliasl);
2741 ASTOBJ_CONTAINER_TRAVERSE(&aliasl, 1, do {
2742 ASTOBJ_RDLOCK(iterator);
2743 ASTOBJ_MARK(iterator);
2744 ++pruned;
2745 ASTOBJ_UNLOCK(iterator);
2746 } while (0) );
2747 if (pruned) {
2748 ASTOBJ_CONTAINER_PRUNE_MARKED(&aliasl, oh323_destroy_alias);
2750 ASTOBJ_CONTAINER_UNLOCK(&aliasl);
2753 static void prune_peers(void)
2755 /* Prune peers who still are supposed to be deleted */
2756 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, oh323_destroy_peer);
2759 static int reload_config(int is_reload)
2761 int format;
2762 struct ast_config *cfg, *ucfg;
2763 struct ast_variable *v;
2764 struct oh323_peer *peer = NULL;
2765 struct oh323_user *user = NULL;
2766 struct oh323_alias *alias = NULL;
2767 struct ast_hostent ahp; struct hostent *hp;
2768 char *cat;
2769 const char *utype;
2770 int is_user, is_peer, is_alias;
2771 char _gatekeeper[100];
2772 int gk_discover, gk_disable, gk_changed;
2774 cfg = ast_config_load(config);
2776 /* We *must* have a config file otherwise stop immediately */
2777 if (!cfg) {
2778 ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
2779 return 1;
2782 if (is_reload) {
2783 delete_users();
2784 delete_aliases();
2785 prune_peers();
2788 /* fire up the H.323 Endpoint */
2789 if (!h323_end_point_exist()) {
2790 h323_end_point_create();
2792 ast_copy_string(_gatekeeper, gatekeeper, sizeof(_gatekeeper));
2793 gk_discover = gatekeeper_discover;
2794 gk_disable = gatekeeper_disable;
2795 memset(&bindaddr, 0, sizeof(bindaddr));
2796 memset(&global_options, 0, sizeof(global_options));
2797 global_options.fastStart = 1;
2798 global_options.h245Tunneling = 1;
2799 global_options.dtmfcodec = 101;
2800 global_options.dtmfmode = H323_DTMF_RFC2833;
2801 global_options.capability = GLOBAL_CAPABILITY;
2802 global_options.bridge = 1; /* Do native bridging by default */
2803 strcpy(default_context, "default");
2804 h323_signalling_port = 1720;
2805 gatekeeper_disable = 1;
2806 gatekeeper_discover = 0;
2807 gkroute = 0;
2808 userbyalias = 1;
2809 acceptAnonymous = 1;
2810 tos = 0;
2812 /* Copy the default jb config over global_jbconf */
2813 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
2815 /* Load configuration from users.conf */
2816 ucfg = ast_config_load("users.conf");
2817 if (ucfg) {
2818 struct ast_variable *gen;
2819 int genhas_h323;
2820 const char *has_h323;
2822 genhas_h323 = ast_true(ast_variable_retrieve(ucfg, "general", "hash323"));
2823 gen = ast_variable_browse(ucfg, "general");
2824 for (cat = ast_category_browse(ucfg, NULL); cat; cat = ast_category_browse(ucfg, cat)) {
2825 if (strcasecmp(cat, "general")) {
2826 has_h323 = ast_variable_retrieve(ucfg, cat, "hash323");
2827 if (ast_true(has_h323) || (!has_h323 && genhas_h323)) {
2828 user = build_user(cat, gen, ast_variable_browse(ucfg, cat), 0);
2829 if (user) {
2830 ASTOBJ_CONTAINER_LINK(&userl, user);
2831 ASTOBJ_UNREF(user, oh323_destroy_user);
2833 peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0);
2834 if (peer) {
2835 ASTOBJ_CONTAINER_LINK(&peerl, peer);
2836 ASTOBJ_UNREF(peer, oh323_destroy_peer);
2841 ast_config_destroy(ucfg);
2844 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
2845 /* handle jb conf */
2846 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
2847 continue;
2848 /* Create the interface list */
2849 if (!strcasecmp(v->name, "port")) {
2850 h323_signalling_port = (int)strtol(v->value, NULL, 10);
2851 } else if (!strcasecmp(v->name, "bindaddr")) {
2852 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
2853 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
2854 } else {
2855 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
2857 } else if (!strcasecmp(v->name, "tos")) {
2858 if (sscanf(v->value, "%d", &format)) {
2859 tos = format & 0xff;
2860 } else if (!strcasecmp(v->value, "lowdelay")) {
2861 tos = IPTOS_LOWDELAY;
2862 } else if (!strcasecmp(v->value, "throughput")) {
2863 tos = IPTOS_THROUGHPUT;
2864 } else if (!strcasecmp(v->value, "reliability")) {
2865 tos = IPTOS_RELIABILITY;
2866 } else if (!strcasecmp(v->value, "mincost")) {
2867 tos = IPTOS_MINCOST;
2868 } else if (!strcasecmp(v->value, "none")) {
2869 tos = 0;
2870 } else {
2871 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
2873 } else if (!strcasecmp(v->name, "gatekeeper")) {
2874 if (!strcasecmp(v->value, "DISABLE")) {
2875 gatekeeper_disable = 1;
2876 } else if (!strcasecmp(v->value, "DISCOVER")) {
2877 gatekeeper_disable = 0;
2878 gatekeeper_discover = 1;
2879 } else {
2880 gatekeeper_disable = 0;
2881 ast_copy_string(gatekeeper, v->value, sizeof(gatekeeper));
2883 } else if (!strcasecmp(v->name, "secret")) {
2884 ast_copy_string(secret, v->value, sizeof(secret));
2885 } else if (!strcasecmp(v->name, "AllowGKRouted")) {
2886 gkroute = ast_true(v->value);
2887 } else if (!strcasecmp(v->name, "context")) {
2888 ast_copy_string(default_context, v->value, sizeof(default_context));
2889 ast_verbose(VERBOSE_PREFIX_2 "Setting default context to %s\n", default_context);
2890 } else if (!strcasecmp(v->name, "UserByAlias")) {
2891 userbyalias = ast_true(v->value);
2892 } else if (!strcasecmp(v->name, "AcceptAnonymous")) {
2893 acceptAnonymous = ast_true(v->value);
2894 } else if (!update_common_options(v, &global_options)) {
2895 /* dummy */
2899 for (cat = ast_category_browse(cfg, NULL); cat; cat = ast_category_browse(cfg, cat)) {
2900 if (strcasecmp(cat, "general")) {
2901 utype = ast_variable_retrieve(cfg, cat, "type");
2902 if (utype) {
2903 is_user = is_peer = is_alias = 0;
2904 if (!strcasecmp(utype, "user"))
2905 is_user = 1;
2906 else if (!strcasecmp(utype, "peer"))
2907 is_peer = 1;
2908 else if (!strcasecmp(utype, "friend"))
2909 is_user = is_peer = 1;
2910 else if (!strcasecmp(utype, "h323") || !strcasecmp(utype, "alias"))
2911 is_alias = 1;
2912 else {
2913 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
2914 continue;
2916 if (is_user) {
2917 user = build_user(cat, ast_variable_browse(cfg, cat), NULL, 0);
2918 if (user) {
2919 ASTOBJ_CONTAINER_LINK(&userl, user);
2920 ASTOBJ_UNREF(user, oh323_destroy_user);
2923 if (is_peer) {
2924 peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0);
2925 if (peer) {
2926 ASTOBJ_CONTAINER_LINK(&peerl, peer);
2927 ASTOBJ_UNREF(peer, oh323_destroy_peer);
2930 if (is_alias) {
2931 alias = build_alias(cat, ast_variable_browse(cfg, cat), NULL, 0);
2932 if (alias) {
2933 ASTOBJ_CONTAINER_LINK(&aliasl, alias);
2934 ASTOBJ_UNREF(alias, oh323_destroy_alias);
2937 } else {
2938 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
2942 ast_config_destroy(cfg);
2944 /* Register our H.323 aliases if any*/
2945 ASTOBJ_CONTAINER_WRLOCK(&aliasl);
2946 ASTOBJ_CONTAINER_TRAVERSE(&aliasl, 1, do {
2947 ASTOBJ_RDLOCK(iterator);
2948 if (h323_set_alias(iterator)) {
2949 ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
2950 ASTOBJ_UNLOCK(iterator);
2951 continue;
2953 ASTOBJ_UNLOCK(iterator);
2954 } while (0) );
2955 ASTOBJ_CONTAINER_UNLOCK(&aliasl);
2957 /* Don't touch GK if nothing changed because URQ will drop all existing calls */
2958 gk_changed = 0;
2959 if (gatekeeper_disable != gk_disable)
2960 gk_changed = is_reload;
2961 else if(!gatekeeper_disable && (gatekeeper_discover != gk_discover))
2962 gk_changed = is_reload;
2963 else if(!gatekeeper_disable && (strncmp(_gatekeeper, gatekeeper, sizeof(_gatekeeper)) != 0))
2964 gk_changed = is_reload;
2965 if (gk_changed) {
2966 if(!gk_disable)
2967 h323_gk_urq();
2968 if (!gatekeeper_disable) {
2969 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
2970 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
2971 gatekeeper_disable = 1;
2975 return 0;
2978 static int h323_reload(int fd, int argc, char *argv[])
2980 ast_mutex_lock(&h323_reload_lock);
2981 if (h323_reloading) {
2982 ast_verbose("Previous H.323 reload not yet done\n");
2983 } else {
2984 h323_reloading = 1;
2986 ast_mutex_unlock(&h323_reload_lock);
2987 restart_monitor();
2988 return 0;
2991 static int h323_do_reload(void)
2993 reload_config(1);
2994 return 0;
2997 static int reload(void)
2999 if (!sched || !io) {
3000 ast_log(LOG_NOTICE, "Unload and load chan_h323.so again in order to receive configuration changes.\n");
3001 return 0;
3003 return h323_reload(0, 0, NULL);
3006 static struct ast_cli_entry cli_h323_reload =
3007 { { "h.323", "reload", NULL },
3008 h323_reload, "Reload H.323 configuration",
3009 h323_reload_usage
3012 static enum ast_rtp_get_result oh323_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
3014 struct oh323_pvt *pvt;
3015 enum ast_rtp_get_result res = AST_RTP_GET_FAILED;
3017 if (!(pvt = (struct oh323_pvt *)chan->tech_pvt))
3018 return res;
3020 ast_mutex_lock(&pvt->lock);
3021 if (pvt->rtp && pvt->options.bridge) {
3022 *rtp = pvt->rtp;
3023 res = AST_RTP_TRY_NATIVE;
3025 ast_mutex_unlock(&pvt->lock);
3027 return res;
3030 static enum ast_rtp_get_result oh323_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
3032 return AST_RTP_GET_FAILED;
3035 static char *convertcap(int cap)
3037 switch (cap) {
3038 case AST_FORMAT_G723_1:
3039 return "G.723";
3040 case AST_FORMAT_GSM:
3041 return "GSM";
3042 case AST_FORMAT_ULAW:
3043 return "ULAW";
3044 case AST_FORMAT_ALAW:
3045 return "ALAW";
3046 case AST_FORMAT_G722:
3047 return "G.722";
3048 case AST_FORMAT_ADPCM:
3049 return "G.728";
3050 case AST_FORMAT_G729A:
3051 return "G.729";
3052 case AST_FORMAT_SPEEX:
3053 return "SPEEX";
3054 case AST_FORMAT_ILBC:
3055 return "ILBC";
3056 default:
3057 ast_log(LOG_NOTICE, "Don't know how to deal with mode %d\n", cap);
3058 return NULL;
3062 static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active)
3064 /* XXX Deal with Video */
3065 struct oh323_pvt *pvt;
3066 struct sockaddr_in them;
3067 struct sockaddr_in us;
3068 char *mode;
3070 if (!rtp) {
3071 return 0;
3074 mode = convertcap(chan->writeformat);
3075 pvt = (struct oh323_pvt *) chan->tech_pvt;
3076 if (!pvt) {
3077 ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
3078 return -1;
3080 ast_rtp_get_peer(rtp, &them);
3081 ast_rtp_get_us(rtp, &us);
3082 #if 0 /* Native bridge still isn't ready */
3083 h323_native_bridge(pvt->cd.call_token, ast_inet_ntoa(them.sin_addr), mode);
3084 #endif
3085 return 0;
3088 static struct ast_rtp_protocol oh323_rtp = {
3089 .type = "H323",
3090 .get_rtp_info = oh323_get_rtp_peer,
3091 .get_vrtp_info = oh323_get_vrtp_peer,
3092 .set_rtp_peer = oh323_set_rtp_peer,
3095 static enum ast_module_load_result load_module(void)
3097 int res;
3099 h323debug = 0;
3100 sched = sched_context_create();
3101 if (!sched) {
3102 ast_log(LOG_WARNING, "Unable to create schedule context\n");
3103 return AST_MODULE_LOAD_FAILURE;
3105 io = io_context_create();
3106 if (!io) {
3107 ast_log(LOG_WARNING, "Unable to create I/O context\n");
3108 return AST_MODULE_LOAD_FAILURE;
3110 ast_cli_register(&cli_h323_reload);
3111 ASTOBJ_CONTAINER_INIT(&userl);
3112 ASTOBJ_CONTAINER_INIT(&peerl);
3113 ASTOBJ_CONTAINER_INIT(&aliasl);
3114 res = reload_config(0);
3115 if (res) {
3116 /* No config entry */
3117 ast_log(LOG_NOTICE, "Unload and load chan_h323.so again in order to receive configuration changes.\n");
3118 ast_cli_unregister(&cli_h323_reload);
3119 io_context_destroy(io);
3120 io = NULL;
3121 sched_context_destroy(sched);
3122 sched = NULL;
3123 ASTOBJ_CONTAINER_DESTROY(&userl);
3124 ASTOBJ_CONTAINER_DESTROY(&peerl);
3125 ASTOBJ_CONTAINER_DESTROY(&aliasl);
3126 return AST_MODULE_LOAD_DECLINE;
3127 } else {
3128 /* Make sure we can register our channel type */
3129 if (ast_channel_register(&oh323_tech)) {
3130 ast_log(LOG_ERROR, "Unable to register channel class 'H323'\n");
3131 ast_cli_unregister(&cli_h323_reload);
3132 h323_end_process();
3133 io_context_destroy(io);
3134 sched_context_destroy(sched);
3136 ASTOBJ_CONTAINER_DESTROYALL(&userl, oh323_destroy_user);
3137 ASTOBJ_CONTAINER_DESTROY(&userl);
3138 ASTOBJ_CONTAINER_DESTROYALL(&peerl, oh323_destroy_peer);
3139 ASTOBJ_CONTAINER_DESTROY(&peerl);
3140 ASTOBJ_CONTAINER_DESTROYALL(&aliasl, oh323_destroy_alias);
3141 ASTOBJ_CONTAINER_DESTROY(&aliasl);
3143 return AST_MODULE_LOAD_FAILURE;
3145 ast_cli_register_multiple(cli_h323, sizeof(cli_h323) / sizeof(struct ast_cli_entry));
3147 ast_rtp_proto_register(&oh323_rtp);
3149 /* Register our callback functions */
3150 h323_callback_register(setup_incoming_call,
3151 setup_outgoing_call,
3152 external_rtp_create,
3153 setup_rtp_connection,
3154 cleanup_connection,
3155 chan_ringing,
3156 connection_made,
3157 receive_digit,
3158 answer_call,
3159 progress,
3160 set_dtmf_payload,
3161 hangup_connection,
3162 set_local_capabilities,
3163 set_peer_capabilities);
3164 /* start the h.323 listener */
3165 if (h323_start_listener(h323_signalling_port, bindaddr)) {
3166 ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
3167 ast_rtp_proto_unregister(&oh323_rtp);
3168 ast_cli_unregister_multiple(cli_h323, sizeof(cli_h323) / sizeof(struct ast_cli_entry));
3169 ast_cli_unregister(&cli_h323_reload);
3170 h323_end_process();
3171 io_context_destroy(io);
3172 sched_context_destroy(sched);
3174 ASTOBJ_CONTAINER_DESTROYALL(&userl, oh323_destroy_user);
3175 ASTOBJ_CONTAINER_DESTROY(&userl);
3176 ASTOBJ_CONTAINER_DESTROYALL(&peerl, oh323_destroy_peer);
3177 ASTOBJ_CONTAINER_DESTROY(&peerl);
3178 ASTOBJ_CONTAINER_DESTROYALL(&aliasl, oh323_destroy_alias);
3179 ASTOBJ_CONTAINER_DESTROY(&aliasl);
3181 return AST_MODULE_LOAD_FAILURE;
3183 /* Possibly register with a GK */
3184 if (!gatekeeper_disable) {
3185 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
3186 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
3187 gatekeeper_disable = 1;
3188 res = AST_MODULE_LOAD_SUCCESS;
3191 /* And start the monitor for the first time */
3192 restart_monitor();
3194 return res;
3197 static int unload_module(void)
3199 struct oh323_pvt *p, *pl;
3201 /* unregister commands */
3202 ast_cli_unregister_multiple(cli_h323, sizeof(cli_h323) / sizeof(struct ast_cli_entry));
3203 ast_cli_unregister(&cli_h323_reload);
3205 ast_channel_unregister(&oh323_tech);
3206 ast_rtp_proto_unregister(&oh323_rtp);
3208 if (!ast_mutex_lock(&iflock)) {
3209 /* hangup all interfaces if they have an owner */
3210 p = iflist;
3211 while(p) {
3212 if (p->owner) {
3213 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
3215 p = p->next;
3217 iflist = NULL;
3218 ast_mutex_unlock(&iflock);
3219 } else {
3220 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
3221 return -1;
3223 if (!ast_mutex_lock(&monlock)) {
3224 if ((monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
3225 /* this causes a seg, anyone know why? */
3226 if (monitor_thread != pthread_self())
3227 pthread_cancel(monitor_thread);
3228 pthread_kill(monitor_thread, SIGURG);
3229 pthread_join(monitor_thread, NULL);
3231 monitor_thread = AST_PTHREADT_STOP;
3232 ast_mutex_unlock(&monlock);
3233 } else {
3234 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
3235 return -1;
3237 if (!ast_mutex_lock(&iflock)) {
3238 /* destroy all the interfaces and free their memory */
3239 p = iflist;
3240 while(p) {
3241 pl = p;
3242 p = p->next;
3243 /* free associated memory */
3244 ast_mutex_destroy(&pl->lock);
3245 free(pl);
3247 iflist = NULL;
3248 ast_mutex_unlock(&iflock);
3249 } else {
3250 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
3251 return -1;
3253 if (!gatekeeper_disable)
3254 h323_gk_urq();
3255 h323_end_process();
3256 if (io)
3257 io_context_destroy(io);
3258 if (sched)
3259 sched_context_destroy(sched);
3261 ASTOBJ_CONTAINER_DESTROYALL(&userl, oh323_destroy_user);
3262 ASTOBJ_CONTAINER_DESTROY(&userl);
3263 ASTOBJ_CONTAINER_DESTROYALL(&peerl, oh323_destroy_peer);
3264 ASTOBJ_CONTAINER_DESTROY(&peerl);
3265 ASTOBJ_CONTAINER_DESTROYALL(&aliasl, oh323_destroy_alias);
3266 ASTOBJ_CONTAINER_DESTROY(&aliasl);
3268 return 0;
3271 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "The NuFone Network's OpenH323 Channel Driver",
3272 .load = load_module,
3273 .unload = unload_module,
3274 .reload = reload,