Disable automatic build of chan_h323
[asterisk-bristuff.git] / channels / chan_h323.c
blobd5f8cae03a84d89f921eea414fedfaaaedc3a678
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>no</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));
1492 } else if (!strcasecmp(v->name, "hasvoicemail")) {
1493 if (ast_true(v->value) && ast_strlen_zero(peer->mailbox)) {
1494 ast_copy_string(peer->mailbox, name, sizeof(peer->mailbox));
1498 ASTOBJ_UNMARK(peer);
1499 ast_free_ha(oldha);
1500 return peer;
1503 static struct oh323_peer *realtime_peer(const char *peername, struct sockaddr_in *sin)
1505 struct oh323_peer *peer;
1506 struct ast_variable *var;
1507 struct ast_variable *tmp;
1508 const char *addr;
1510 /* First check on peer name */
1511 if (peername)
1512 var = ast_load_realtime("h323", "name", peername, addr = NULL);
1513 else if (sin) /* Then check on IP address for dynamic peers */
1514 var = ast_load_realtime("h323", "host", addr = ast_inet_ntoa(sin->sin_addr), NULL);
1515 else
1516 return NULL;
1518 if (!var)
1519 return NULL;
1521 for (tmp = var; tmp; tmp = tmp->next) {
1522 /* If this is type=user, then skip this object. */
1523 if (!strcasecmp(tmp->name, "type") &&
1524 !(!strcasecmp(tmp->value, "peer") || !strcasecmp(tmp->value, "friend"))) {
1525 ast_variables_destroy(var);
1526 return NULL;
1527 } else if (!peername && !strcasecmp(tmp->name, "name")) {
1528 peername = tmp->value;
1532 if (!peername) { /* Did not find peer in realtime */
1533 ast_log(LOG_WARNING, "Cannot determine peer name for IP address %s\n", addr);
1534 ast_variables_destroy(var);
1535 return NULL;
1538 /* Peer found in realtime, now build it in memory */
1539 peer = build_peer(peername, var, NULL, 1);
1541 ast_variables_destroy(var);
1543 return peer;
1546 static int oh323_addrcmp_str(struct in_addr inaddr, char *addr)
1548 return strcmp(ast_inet_ntoa(inaddr), addr);
1551 static struct oh323_user *find_user(const call_details_t *cd, int realtime)
1553 struct oh323_user *u;
1555 if (userbyalias)
1556 u = ASTOBJ_CONTAINER_FIND(&userl, cd->call_source_aliases);
1557 else
1558 u = ASTOBJ_CONTAINER_FIND_FULL(&userl, cd->sourceIp, addr.sin_addr, 0, 0, oh323_addrcmp_str);
1560 if (!u && realtime)
1561 u = realtime_user(cd);
1563 if (!u && h323debug)
1564 ast_log(LOG_DEBUG, "Could not find user by name %s or address %s\n", cd->call_source_aliases, cd->sourceIp);
1566 return u;
1569 static int oh323_addrcmp(struct sockaddr_in addr, struct sockaddr_in *sin)
1571 int res;
1573 if (!sin)
1574 res = -1;
1575 else
1576 res = inaddrcmp(&addr , sin);
1578 return res;
1581 static struct oh323_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime)
1583 struct oh323_peer *p;
1585 if (peer)
1586 p = ASTOBJ_CONTAINER_FIND(&peerl, peer);
1587 else
1588 p = ASTOBJ_CONTAINER_FIND_FULL(&peerl, sin, addr, 0, 0, oh323_addrcmp);
1590 if (!p && realtime)
1591 p = realtime_peer(peer, sin);
1593 if (!p && h323debug)
1594 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>"));
1596 return p;
1599 static int create_addr(struct oh323_pvt *pvt, char *opeer)
1601 struct hostent *hp;
1602 struct ast_hostent ahp;
1603 struct oh323_peer *p;
1604 int portno;
1605 int found = 0;
1606 char *port;
1607 char *hostn;
1608 char peer[256] = "";
1610 ast_copy_string(peer, opeer, sizeof(peer));
1611 port = strchr(peer, ':');
1612 if (port) {
1613 *port = '\0';
1614 port++;
1616 pvt->sa.sin_family = AF_INET;
1617 p = find_peer(peer, NULL, 1);
1618 if (p) {
1619 found++;
1620 memcpy(&pvt->options, &p->options, sizeof(pvt->options));
1621 pvt->jointcapability = pvt->options.capability;
1622 if (pvt->options.dtmfmode) {
1623 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
1624 pvt->nonCodecCapability |= AST_RTP_DTMF;
1625 } else {
1626 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1629 if (p->addr.sin_addr.s_addr) {
1630 pvt->sa.sin_addr = p->addr.sin_addr;
1631 pvt->sa.sin_port = p->addr.sin_port;
1633 ASTOBJ_UNREF(p, oh323_destroy_peer);
1635 if (!p && !found) {
1636 hostn = peer;
1637 if (port) {
1638 portno = atoi(port);
1639 } else {
1640 portno = h323_signalling_port;
1642 hp = ast_gethostbyname(hostn, &ahp);
1643 if (hp) {
1644 memcpy(&pvt->sa.sin_addr, hp->h_addr, sizeof(pvt->sa.sin_addr));
1645 pvt->sa.sin_port = htons(portno);
1646 /* Look peer by address */
1647 p = find_peer(NULL, &pvt->sa, 1);
1648 memcpy(&pvt->options, (p ? &p->options : &global_options), sizeof(pvt->options));
1649 pvt->jointcapability = pvt->options.capability;
1650 if (p) {
1651 ASTOBJ_UNREF(p, oh323_destroy_peer);
1653 if (pvt->options.dtmfmode) {
1654 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
1655 pvt->nonCodecCapability |= AST_RTP_DTMF;
1656 } else {
1657 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1660 return 0;
1661 } else {
1662 ast_log(LOG_WARNING, "No such host: %s\n", peer);
1663 return -1;
1665 } else if (!found) {
1666 return -1;
1667 } else {
1668 return 0;
1671 static struct ast_channel *oh323_request(const char *type, int format, void *data, int *cause)
1673 int oldformat;
1674 struct oh323_pvt *pvt;
1675 struct ast_channel *tmpc = NULL;
1676 char *dest = (char *)data;
1677 char *ext, *host;
1678 char *h323id = NULL;
1679 char tmp[256], tmp1[256];
1681 if (h323debug)
1682 ast_log(LOG_DEBUG, "type=%s, format=%d, data=%s.\n", type, format, (char *)data);
1684 pvt = oh323_alloc(0);
1685 if (!pvt) {
1686 ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
1687 return NULL;
1689 oldformat = format;
1690 format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1);
1691 if (!format) {
1692 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
1693 oh323_destroy(pvt);
1694 if (cause)
1695 *cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
1696 return NULL;
1698 ast_copy_string(tmp, dest, sizeof(tmp));
1699 host = strchr(tmp, '@');
1700 if (host) {
1701 *host = '\0';
1702 host++;
1703 ext = tmp;
1704 } else {
1705 ext = strrchr(tmp, '/');
1706 if (ext)
1707 *ext++ = '\0';
1708 host = tmp;
1710 strtok_r(host, "/", &(h323id));
1711 if (!ast_strlen_zero(h323id)) {
1712 h323_set_id(h323id);
1714 if (ext) {
1715 ast_copy_string(pvt->exten, ext, sizeof(pvt->exten));
1717 if (h323debug)
1718 ast_log(LOG_DEBUG, "Extension: %s Host: %s\n", pvt->exten, host);
1720 if (gatekeeper_disable) {
1721 if (create_addr(pvt, host)) {
1722 oh323_destroy(pvt);
1723 if (cause)
1724 *cause = AST_CAUSE_DESTINATION_OUT_OF_ORDER;
1725 return NULL;
1728 else {
1729 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
1730 pvt->jointcapability = pvt->options.capability;
1731 if (pvt->options.dtmfmode) {
1732 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
1733 pvt->nonCodecCapability |= AST_RTP_DTMF;
1734 } else {
1735 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1740 ast_mutex_lock(&caplock);
1741 /* Generate unique channel identifier */
1742 snprintf(tmp1, sizeof(tmp1)-1, "%s-%u", host, ++unique);
1743 tmp1[sizeof(tmp1)-1] = '\0';
1744 ast_mutex_unlock(&caplock);
1746 ast_mutex_lock(&pvt->lock);
1747 tmpc = __oh323_new(pvt, AST_STATE_DOWN, tmp1);
1748 ast_mutex_unlock(&pvt->lock);
1749 if (!tmpc) {
1750 oh323_destroy(pvt);
1751 if (cause)
1752 *cause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
1754 ast_update_use_count();
1755 restart_monitor();
1756 return tmpc;
1759 /** Find a call by alias */
1760 static struct oh323_alias *find_alias(const char *source_aliases, int realtime)
1762 struct oh323_alias *a;
1764 a = ASTOBJ_CONTAINER_FIND(&aliasl, source_aliases);
1766 if (!a && realtime)
1767 a = realtime_alias(source_aliases);
1769 return a;
1773 * Callback for sending digits from H.323 up to asterisk
1776 static int receive_digit(unsigned call_reference, char digit, const char *token, int duration)
1778 struct oh323_pvt *pvt;
1779 int res;
1781 pvt = find_call_locked(call_reference, token);
1782 if (!pvt) {
1783 ast_log(LOG_ERROR, "Received digit '%c' (%u ms) for call %s without private structure\n", digit, duration, token);
1784 return -1;
1786 if (h323debug)
1787 ast_log(LOG_DTMF, "Received %s digit '%c' (%u ms) for call %s\n", (digit == ' ' ? "update for" : "new"), (digit == ' ' ? pvt->curDTMF : digit), duration, token);
1789 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
1790 if (digit == '!')
1791 res = ast_queue_control(pvt->owner, AST_CONTROL_FLASH);
1792 else {
1793 struct ast_frame f = {
1794 .frametype = AST_FRAME_DTMF_END,
1795 .subclass = digit,
1796 .samples = duration * 8,
1797 .len = duration,
1798 .src = "SEND_DIGIT",
1800 if (digit == ' ') { /* signalUpdate message */
1801 f.subclass = pvt->curDTMF;
1802 AST_SCHED_DEL(sched, pvt->DTMFsched);
1803 } else { /* Regular input or signal message */
1804 if (pvt->DTMFsched >= 0) {
1805 /* We still don't send DTMF END from previous event, send it now */
1806 AST_SCHED_DEL(sched, pvt->DTMFsched);
1807 f.subclass = pvt->curDTMF;
1808 f.samples = f.len = 0;
1809 ast_queue_frame(pvt->owner, &f);
1810 /* Restore values */
1811 f.subclass = digit;
1812 f.samples = duration * 8;
1813 f.len = duration;
1815 if (duration) { /* This is a signal, signalUpdate follows */
1816 f.frametype = AST_FRAME_DTMF_BEGIN;
1817 pvt->DTMFsched = ast_sched_add(sched, duration, oh323_simulate_dtmf_end, pvt);
1818 if (h323debug)
1819 ast_log(LOG_DTMF, "Scheduled DTMF END simulation for %d ms, id=%d\n", duration, pvt->DTMFsched);
1821 pvt->curDTMF = digit;
1823 res = ast_queue_frame(pvt->owner, &f);
1825 ast_channel_unlock(pvt->owner);
1826 } else {
1827 if (digit == '!')
1828 pvt->newcontrol = AST_CONTROL_FLASH;
1829 else {
1830 pvt->newduration = duration;
1831 pvt->newdigit = digit;
1833 res = 0;
1835 ast_mutex_unlock(&pvt->lock);
1836 return res;
1840 * Callback function used to inform the H.323 stack of the local rtp ip/port details
1842 * Returns the local RTP information
1844 static struct rtp_info *external_rtp_create(unsigned call_reference, const char * token)
1846 struct oh323_pvt *pvt;
1847 struct sockaddr_in us;
1848 struct rtp_info *info;
1850 info = (struct rtp_info *)malloc(sizeof(struct rtp_info));
1851 if (!info) {
1852 ast_log(LOG_ERROR, "Unable to allocated info structure, this is very bad\n");
1853 return NULL;
1855 pvt = find_call_locked(call_reference, token);
1856 if (!pvt) {
1857 free(info);
1858 ast_log(LOG_ERROR, "Unable to find call %s(%d)\n", token, call_reference);
1859 return NULL;
1861 if (!pvt->rtp)
1862 __oh323_rtp_create(pvt);
1863 if (!pvt->rtp) {
1864 ast_mutex_unlock(&pvt->lock);
1865 free(info);
1866 ast_log(LOG_ERROR, "No RTP stream is available for call %s (%d)", token, call_reference);
1867 return NULL;
1869 /* figure out our local RTP port and tell the H.323 stack about it */
1870 ast_rtp_get_us(pvt->rtp, &us);
1871 ast_mutex_unlock(&pvt->lock);
1873 ast_copy_string(info->addr, ast_inet_ntoa(us.sin_addr), sizeof(info->addr));
1874 info->port = ntohs(us.sin_port);
1875 if (h323debug)
1876 ast_log(LOG_DEBUG, "Sending RTP 'US' %s:%d\n", info->addr, info->port);
1877 return info;
1881 * Definition taken from rtp.c for rtpPayloadType because we need it here.
1883 struct rtpPayloadType {
1884 int isAstFormat; /* whether the following code is an AST_FORMAT */
1885 int code;
1889 * Call-back function passing remote ip/port information from H.323 to asterisk
1891 * Returns nothing
1893 static void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort, const char *token, int pt)
1895 struct oh323_pvt *pvt;
1896 struct sockaddr_in them;
1897 struct rtpPayloadType rtptype;
1898 int nativeformats_changed;
1899 enum { NEED_NONE, NEED_HOLD, NEED_UNHOLD } rtp_change = NEED_NONE;
1901 if (h323debug)
1902 ast_log(LOG_DEBUG, "Setting up RTP connection for %s\n", token);
1904 /* Find the call or allocate a private structure if call not found */
1905 pvt = find_call_locked(call_reference, token);
1906 if (!pvt) {
1907 ast_log(LOG_ERROR, "Something is wrong: rtp\n");
1908 return;
1910 if (pvt->alreadygone) {
1911 ast_mutex_unlock(&pvt->lock);
1912 return;
1915 if (!pvt->rtp)
1916 __oh323_rtp_create(pvt);
1918 them.sin_family = AF_INET;
1919 /* only works for IPv4 */
1920 them.sin_addr.s_addr = inet_addr(remoteIp);
1921 them.sin_port = htons(remotePort);
1923 if (them.sin_addr.s_addr) {
1924 ast_rtp_set_peer(pvt->rtp, &them);
1925 if (pvt->recvonly) {
1926 pvt->recvonly = 0;
1927 rtp_change = NEED_UNHOLD;
1929 } else {
1930 ast_rtp_stop(pvt->rtp);
1931 if (!pvt->recvonly) {
1932 pvt->recvonly = 1;
1933 rtp_change = NEED_HOLD;
1937 /* Change native format to reflect information taken from OLC/OLCAck */
1938 nativeformats_changed = 0;
1939 if (pt != 128 && pvt->rtp) { /* Payload type is invalid, so try to use previously decided */
1940 rtptype = ast_rtp_lookup_pt(pvt->rtp, pt);
1941 if (h323debug)
1942 ast_log(LOG_DEBUG, "Native format is set to %d from %d by RTP payload type %d\n", rtptype.code, pvt->nativeformats, pt);
1943 if (pvt->nativeformats != rtptype.code) {
1944 pvt->nativeformats = rtptype.code;
1945 nativeformats_changed = 1;
1947 } else if (h323debug)
1948 ast_log(LOG_NOTICE, "Payload type is unknown, formats isn't changed\n");
1950 /* Don't try to lock the channel if nothing changed */
1951 if (nativeformats_changed || pvt->options.progress_audio || (rtp_change != NEED_NONE)) {
1952 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
1953 /* Re-build translation path only if native format(s) has been changed */
1954 if (pvt->owner->nativeformats != pvt->nativeformats) {
1955 if (h323debug)
1956 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);
1957 pvt->owner->nativeformats = pvt->nativeformats;
1958 ast_set_read_format(pvt->owner, pvt->owner->readformat);
1959 ast_set_write_format(pvt->owner, pvt->owner->writeformat);
1961 if (pvt->options.progress_audio)
1962 ast_queue_control(pvt->owner, AST_CONTROL_PROGRESS);
1963 switch (rtp_change) {
1964 case NEED_HOLD:
1965 ast_queue_control(pvt->owner, AST_CONTROL_HOLD);
1966 break;
1967 case NEED_UNHOLD:
1968 ast_queue_control(pvt->owner, AST_CONTROL_UNHOLD);
1969 break;
1970 default:
1971 break;
1973 ast_channel_unlock(pvt->owner);
1975 else {
1976 if (pvt->options.progress_audio)
1977 pvt->newcontrol = AST_CONTROL_PROGRESS;
1978 else if (rtp_change == NEED_HOLD)
1979 pvt->newcontrol = AST_CONTROL_HOLD;
1980 else if (rtp_change == NEED_UNHOLD)
1981 pvt->newcontrol = AST_CONTROL_UNHOLD;
1982 if (h323debug)
1983 ast_log(LOG_DEBUG, "RTP connection preparation for %s is pending...\n", token);
1986 ast_mutex_unlock(&pvt->lock);
1988 if (h323debug)
1989 ast_log(LOG_DEBUG, "RTP connection prepared for %s\n", token);
1991 return;
1995 * Call-back function to signal asterisk that the channel has been answered
1996 * Returns nothing
1998 static void connection_made(unsigned call_reference, const char *token)
2000 struct oh323_pvt *pvt;
2002 if (h323debug)
2003 ast_log(LOG_DEBUG, "Call %s answered\n", token);
2005 pvt = find_call_locked(call_reference, token);
2006 if (!pvt) {
2007 ast_log(LOG_ERROR, "Something is wrong: connection\n");
2008 return;
2011 /* Inform asterisk about remote party connected only on outgoing calls */
2012 if (!pvt->outgoing) {
2013 ast_mutex_unlock(&pvt->lock);
2014 return;
2016 /* Do not send ANSWER message more than once */
2017 if (!pvt->connection_established) {
2018 pvt->connection_established = 1;
2019 update_state(pvt, -1, AST_CONTROL_ANSWER);
2021 ast_mutex_unlock(&pvt->lock);
2022 return;
2025 static int progress(unsigned call_reference, const char *token, int inband)
2027 struct oh323_pvt *pvt;
2029 if (h323debug)
2030 ast_log(LOG_DEBUG, "Received ALERT/PROGRESS message for %s tones\n", (inband ? "inband" : "self-generated"));
2032 pvt = find_call_locked(call_reference, token);
2033 if (!pvt) {
2034 ast_log(LOG_ERROR, "Private structure not found in progress.\n");
2035 return -1;
2037 if (!pvt->owner) {
2038 ast_mutex_unlock(&pvt->lock);
2039 ast_log(LOG_ERROR, "No Asterisk channel associated with private structure.\n");
2040 return -1;
2042 update_state(pvt, -1, (inband ? AST_CONTROL_PROGRESS : AST_CONTROL_RINGING));
2043 ast_mutex_unlock(&pvt->lock);
2045 return 0;
2049 * Call-back function for incoming calls
2051 * Returns 1 on success
2053 static call_options_t *setup_incoming_call(call_details_t *cd)
2055 struct oh323_pvt *pvt;
2056 struct oh323_user *user = NULL;
2057 struct oh323_alias *alias = NULL;
2059 if (h323debug)
2060 ast_log(LOG_DEBUG, "Setting up incoming call for %s\n", cd->call_token);
2062 /* allocate the call*/
2063 pvt = oh323_alloc(cd->call_reference);
2065 if (!pvt) {
2066 ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
2067 cleanup_call_details(cd);
2068 return NULL;
2071 /* Populate the call details in the private structure */
2072 memcpy(&pvt->cd, cd, sizeof(pvt->cd));
2073 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
2074 pvt->jointcapability = pvt->options.capability;
2076 if (h323debug) {
2077 ast_verbose(VERBOSE_PREFIX_3 "Setting up Call\n");
2078 ast_verbose(VERBOSE_PREFIX_3 " \tCall token: [%s]\n", pvt->cd.call_token);
2079 ast_verbose(VERBOSE_PREFIX_3 " \tCalling party name: [%s]\n", pvt->cd.call_source_name);
2080 ast_verbose(VERBOSE_PREFIX_3 " \tCalling party number: [%s]\n", pvt->cd.call_source_e164);
2081 ast_verbose(VERBOSE_PREFIX_3 " \tCalled party name: [%s]\n", pvt->cd.call_dest_alias);
2082 ast_verbose(VERBOSE_PREFIX_3 " \tCalled party number: [%s]\n", pvt->cd.call_dest_e164);
2083 if (pvt->cd.redirect_reason >= 0)
2084 ast_verbose(VERBOSE_PREFIX_3 " \tRedirecting party number: [%s] (reason %d)\n", pvt->cd.redirect_number, pvt->cd.redirect_reason);
2085 ast_verbose(VERBOSE_PREFIX_3 " \tCalling party IP: [%s]\n", pvt->cd.sourceIp);
2088 /* Decide if we are allowing Gatekeeper routed calls*/
2089 if ((!strcasecmp(cd->sourceIp, gatekeeper)) && (gkroute == -1) && !gatekeeper_disable) {
2090 if (!ast_strlen_zero(cd->call_dest_e164)) {
2091 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2092 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2093 } else {
2094 alias = find_alias(cd->call_dest_alias, 1);
2095 if (!alias) {
2096 ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd->call_dest_alias);
2097 oh323_destroy(pvt);
2098 return NULL;
2100 ast_copy_string(pvt->exten, alias->name, sizeof(pvt->exten));
2101 ast_copy_string(pvt->context, alias->context, sizeof(pvt->context));
2103 } else {
2104 /* Either this call is not from the Gatekeeper
2105 or we are not allowing gk routed calls */
2106 user = find_user(cd, 1);
2107 if (!user) {
2108 if (!acceptAnonymous) {
2109 ast_log(LOG_NOTICE, "Anonymous call from '%s@%s' rejected\n", pvt->cd.call_source_aliases, pvt->cd.sourceIp);
2110 oh323_destroy(pvt);
2111 return NULL;
2113 if (ast_strlen_zero(default_context)) {
2114 ast_log(LOG_ERROR, "Call from '%s@%s' rejected due to no default context\n", pvt->cd.call_source_aliases, pvt->cd.sourceIp);
2115 oh323_destroy(pvt);
2116 return NULL;
2118 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2119 if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
2120 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2121 } else {
2122 ast_copy_string(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten));
2124 if (h323debug)
2125 ast_log(LOG_DEBUG, "Sending %s@%s to context [%s] extension %s\n", cd->call_source_aliases, cd->sourceIp, pvt->context, pvt->exten);
2126 } else {
2127 if (user->host) {
2128 if (strcasecmp(cd->sourceIp, ast_inet_ntoa(user->addr.sin_addr))) {
2129 if (ast_strlen_zero(user->context)) {
2130 if (ast_strlen_zero(default_context)) {
2131 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s) and no default context\n", user->name, cd->sourceIp);
2132 oh323_destroy(pvt);
2133 ASTOBJ_UNREF(user, oh323_destroy_user);
2134 return NULL;
2136 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2137 } else {
2138 ast_copy_string(pvt->context, user->context, sizeof(pvt->context));
2140 pvt->exten[0] = 'i';
2141 pvt->exten[1] = '\0';
2142 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s)s\n", user->name, cd->sourceIp);
2143 oh323_destroy(pvt);
2144 ASTOBJ_UNREF(user, oh323_destroy_user);
2145 return NULL; /* XXX: Hmmm... Why to setup context if we drop connection immediately??? */
2148 ast_copy_string(pvt->context, user->context, sizeof(pvt->context));
2149 memcpy(&pvt->options, &user->options, sizeof(pvt->options));
2150 pvt->jointcapability = pvt->options.capability;
2151 if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
2152 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2153 } else {
2154 ast_copy_string(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten));
2156 if (!ast_strlen_zero(user->accountcode)) {
2157 ast_copy_string(pvt->accountcode, user->accountcode, sizeof(pvt->accountcode));
2159 if (user->amaflags) {
2160 pvt->amaflags = user->amaflags;
2162 ASTOBJ_UNREF(user, oh323_destroy_user);
2165 return &pvt->options;
2169 * Call-back function to start PBX when OpenH323 ready to serve incoming call
2171 * Returns 1 on success
2173 static int answer_call(unsigned call_reference, const char *token)
2175 struct oh323_pvt *pvt;
2176 struct ast_channel *c = NULL;
2177 enum {ext_original, ext_s, ext_i, ext_notexists} try_exten;
2178 char tmp_exten[sizeof(pvt->exten)];
2180 if (h323debug)
2181 ast_log(LOG_DEBUG, "Preparing Asterisk to answer for %s\n", token);
2183 /* Find the call or allocate a private structure if call not found */
2184 pvt = find_call_locked(call_reference, token);
2185 if (!pvt) {
2186 ast_log(LOG_ERROR, "Something is wrong: answer_call\n");
2187 return 0;
2189 /* Check if requested extension@context pair exists in the dialplan */
2190 ast_copy_string(tmp_exten, pvt->exten, sizeof(tmp_exten));
2192 /* Try to find best extension in specified context */
2193 if ((tmp_exten[0] != '\0') && (tmp_exten[1] == '\0')) {
2194 if (tmp_exten[0] == 's')
2195 try_exten = ext_s;
2196 else if (tmp_exten[0] == 'i')
2197 try_exten = ext_i;
2198 else
2199 try_exten = ext_original;
2200 } else
2201 try_exten = ext_original;
2202 do {
2203 if (ast_exists_extension(NULL, pvt->context, tmp_exten, 1, NULL))
2204 break;
2205 switch (try_exten) {
2206 case ext_original:
2207 tmp_exten[0] = 's';
2208 tmp_exten[1] = '\0';
2209 try_exten = ext_s;
2210 break;
2211 case ext_s:
2212 tmp_exten[0] = 'i';
2213 try_exten = ext_i;
2214 break;
2215 case ext_i:
2216 try_exten = ext_notexists;
2217 break;
2218 default:
2219 break;
2221 } while (try_exten != ext_notexists);
2223 /* Drop the call if we don't have <exten>, s and i extensions */
2224 if (try_exten == ext_notexists) {
2225 ast_log(LOG_NOTICE, "Dropping call because extensions '%s', 's' and 'i' doesn't exists in context [%s]\n", pvt->exten, pvt->context);
2226 ast_mutex_unlock(&pvt->lock);
2227 h323_clear_call(token, AST_CAUSE_UNALLOCATED);
2228 return 0;
2229 } else if ((try_exten != ext_original) && (strcmp(pvt->exten, tmp_exten) != 0)) {
2230 if (h323debug)
2231 ast_log(LOG_DEBUG, "Going to extension %s@%s because %s@%s isn't exists\n", tmp_exten, pvt->context, pvt->exten, pvt->context);
2232 ast_copy_string(pvt->exten, tmp_exten, sizeof(pvt->exten));
2235 /* allocate a channel and tell asterisk about it */
2236 c = __oh323_new(pvt, AST_STATE_RINGING, pvt->cd.call_token);
2238 /* And release when done */
2239 ast_mutex_unlock(&pvt->lock);
2240 if (!c) {
2241 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
2242 return 0;
2244 return 1;
2248 * Call-back function to establish an outgoing H.323 call
2250 * Returns 1 on success
2252 static int setup_outgoing_call(call_details_t *cd)
2254 /* Use argument here or free it immediately */
2255 cleanup_call_details(cd);
2257 return 1;
2261 * Call-back function to signal asterisk that the channel is ringing
2262 * Returns nothing
2264 static void chan_ringing(unsigned call_reference, const char *token)
2266 struct oh323_pvt *pvt;
2268 if (h323debug)
2269 ast_log(LOG_DEBUG, "Ringing on %s\n", token);
2271 pvt = find_call_locked(call_reference, token);
2272 if (!pvt) {
2273 ast_log(LOG_ERROR, "Something is wrong: ringing\n");
2274 return;
2276 if (!pvt->owner) {
2277 ast_mutex_unlock(&pvt->lock);
2278 ast_log(LOG_ERROR, "Channel has no owner\n");
2279 return;
2281 update_state(pvt, AST_STATE_RINGING, AST_CONTROL_RINGING);
2282 ast_mutex_unlock(&pvt->lock);
2283 return;
2287 * Call-back function to cleanup communication
2288 * Returns nothing,
2290 static void cleanup_connection(unsigned call_reference, const char *call_token)
2292 struct oh323_pvt *pvt;
2294 if (h323debug)
2295 ast_log(LOG_DEBUG, "Cleaning connection to %s\n", call_token);
2297 while (1) {
2298 pvt = find_call_locked(call_reference, call_token);
2299 if (!pvt) {
2300 if (h323debug)
2301 ast_log(LOG_DEBUG, "No connection for %s\n", call_token);
2302 return;
2304 if (!pvt->owner || !ast_channel_trylock(pvt->owner))
2305 break;
2306 #if 1
2307 #ifdef DEBUG_THREADS
2308 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]);
2309 #else
2310 ast_log(LOG_NOTICE, "Avoiding H.323 destory deadlock on %s\n", call_token);
2311 #endif
2312 #endif
2313 ast_mutex_unlock(&pvt->lock);
2314 usleep(1);
2316 if (pvt->rtp) {
2317 /* Immediately stop RTP */
2318 ast_rtp_destroy(pvt->rtp);
2319 pvt->rtp = NULL;
2321 /* Free dsp used for in-band DTMF detection */
2322 if (pvt->vad) {
2323 ast_dsp_free(pvt->vad);
2324 pvt->vad = NULL;
2326 cleanup_call_details(&pvt->cd);
2327 pvt->alreadygone = 1;
2328 /* Send hangup */
2329 if (pvt->owner) {
2330 pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2331 ast_queue_hangup(pvt->owner);
2332 ast_channel_unlock(pvt->owner);
2334 ast_mutex_unlock(&pvt->lock);
2335 if (h323debug)
2336 ast_log(LOG_DEBUG, "Connection to %s cleaned\n", call_token);
2337 return;
2340 static void hangup_connection(unsigned int call_reference, const char *token, int cause)
2342 struct oh323_pvt *pvt;
2344 if (h323debug) {
2345 ast_log(LOG_DEBUG, "Hanging up connection to %s with cause %d\n", token, cause);
2348 pvt = find_call_locked(call_reference, token);
2349 if (!pvt) {
2350 if (h323debug) {
2351 ast_log(LOG_DEBUG, "Connection to %s already cleared\n", token);
2353 return;
2355 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
2356 pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2357 pvt->owner->hangupcause = pvt->hangupcause = cause;
2358 ast_queue_hangup(pvt->owner);
2359 ast_channel_unlock(pvt->owner);
2361 else {
2362 pvt->needhangup = 1;
2363 pvt->hangupcause = cause;
2364 if (h323debug)
2365 ast_log(LOG_DEBUG, "Hangup for %s is pending\n", token);
2367 ast_mutex_unlock(&pvt->lock);
2370 static void set_dtmf_payload(unsigned call_reference, const char *token, int payload)
2372 struct oh323_pvt *pvt;
2374 if (h323debug)
2375 ast_log(LOG_DEBUG, "Setting DTMF payload to %d on %s\n", payload, token);
2377 pvt = find_call_locked(call_reference, token);
2378 if (!pvt) {
2379 return;
2381 if (pvt->rtp) {
2382 ast_rtp_set_rtpmap_type(pvt->rtp, payload, "audio", "telephone-event", 0);
2384 pvt->dtmf_pt = payload;
2385 ast_mutex_unlock(&pvt->lock);
2386 if (h323debug)
2387 ast_log(LOG_DEBUG, "DTMF payload on %s set to %d\n", token, payload);
2390 static void set_peer_capabilities(unsigned call_reference, const char *token, int capabilities, struct ast_codec_pref *prefs)
2392 struct oh323_pvt *pvt;
2394 if (h323debug)
2395 ast_log(LOG_DEBUG, "Got remote capabilities from connection %s\n", token);
2397 pvt = find_call_locked(call_reference, token);
2398 if (!pvt)
2399 return;
2400 pvt->peercapability = capabilities;
2401 pvt->jointcapability = pvt->options.capability & capabilities;
2402 if (prefs) {
2403 memcpy(&pvt->peer_prefs, prefs, sizeof(pvt->peer_prefs));
2404 if (h323debug) {
2405 int i;
2406 for (i = 0; i < 32; ++i) {
2407 if (!prefs->order[i])
2408 break;
2409 ast_log(LOG_DEBUG, "prefs[%d]=%s:%d\n", i, (prefs->order[i] ? ast_getformatname(1 << (prefs->order[i]-1)) : "<none>"), prefs->framing[i]);
2412 if (pvt->rtp)
2413 ast_rtp_codec_setpref(pvt->rtp, &pvt->peer_prefs);
2415 ast_mutex_unlock(&pvt->lock);
2418 static void set_local_capabilities(unsigned call_reference, const char *token)
2420 struct oh323_pvt *pvt;
2421 int capability, dtmfmode, pref_codec;
2422 struct ast_codec_pref prefs;
2424 if (h323debug)
2425 ast_log(LOG_DEBUG, "Setting capabilities for connection %s\n", token);
2427 pvt = find_call_locked(call_reference, token);
2428 if (!pvt)
2429 return;
2430 capability = (pvt->jointcapability) ? pvt->jointcapability : pvt->options.capability;
2431 dtmfmode = pvt->options.dtmfmode;
2432 prefs = pvt->options.prefs;
2433 pref_codec = pvt->pref_codec;
2434 ast_mutex_unlock(&pvt->lock);
2435 h323_set_capabilities(token, capability, dtmfmode, &prefs, pref_codec);
2437 if (h323debug)
2438 ast_log(LOG_DEBUG, "Capabilities for connection %s is set\n", token);
2441 static void *do_monitor(void *data)
2443 int res;
2444 int reloading;
2445 struct oh323_pvt *oh323 = NULL;
2447 for(;;) {
2448 /* Check for a reload request */
2449 ast_mutex_lock(&h323_reload_lock);
2450 reloading = h323_reloading;
2451 h323_reloading = 0;
2452 ast_mutex_unlock(&h323_reload_lock);
2453 if (reloading) {
2454 if (option_verbose > 0) {
2455 ast_verbose(VERBOSE_PREFIX_1 "Reloading H.323\n");
2457 h323_do_reload();
2459 /* Check for interfaces needing to be killed */
2460 if (!ast_mutex_trylock(&iflock)) {
2461 #if 1
2462 do {
2463 for (oh323 = iflist; oh323; oh323 = oh323->next) {
2464 if (!ast_mutex_trylock(&oh323->lock)) {
2465 if (oh323->needdestroy) {
2466 __oh323_destroy(oh323);
2467 break;
2469 ast_mutex_unlock(&oh323->lock);
2472 } while (/*oh323*/ 0);
2473 #else
2474 restartsearch:
2475 oh323 = iflist;
2476 while(oh323) {
2477 if (!ast_mutex_trylock(&oh323->lock)) {
2478 if (oh323->needdestroy) {
2479 __oh323_destroy(oh323);
2480 goto restartsearch;
2482 ast_mutex_unlock(&oh323->lock);
2483 oh323 = oh323->next;
2486 #endif
2487 ast_mutex_unlock(&iflock);
2488 } else
2489 oh323 = (struct oh323_pvt *)1; /* Force fast loop */
2490 pthread_testcancel();
2491 /* Wait for sched or io */
2492 res = ast_sched_wait(sched);
2493 if ((res < 0) || (res > 1000)) {
2494 res = 1000;
2496 /* Do not wait if some channel(s) is destroyed, probably, more available too */
2497 if (oh323)
2498 res = 1;
2499 res = ast_io_wait(io, res);
2500 pthread_testcancel();
2501 ast_mutex_lock(&monlock);
2502 if (res >= 0) {
2503 ast_sched_runq(sched);
2505 ast_mutex_unlock(&monlock);
2507 /* Never reached */
2508 return NULL;
2511 static int restart_monitor(void)
2513 pthread_attr_t attr;
2514 /* If we're supposed to be stopped -- stay stopped */
2515 if (ast_mutex_lock(&monlock)) {
2516 ast_log(LOG_WARNING, "Unable to lock monitor\n");
2517 return -1;
2519 if (monitor_thread == AST_PTHREADT_STOP) {
2520 ast_mutex_unlock(&monlock);
2521 return 0;
2523 if (monitor_thread == pthread_self()) {
2524 ast_mutex_unlock(&monlock);
2525 ast_log(LOG_WARNING, "Cannot kill myself\n");
2526 return -1;
2528 if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
2529 /* Wake up the thread */
2530 pthread_kill(monitor_thread, SIGURG);
2531 } else {
2532 pthread_attr_init(&attr);
2533 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2534 /* Start a new monitor */
2535 if (ast_pthread_create_background(&monitor_thread, &attr, do_monitor, NULL) < 0) {
2536 monitor_thread = AST_PTHREADT_NULL;
2537 ast_mutex_unlock(&monlock);
2538 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
2539 pthread_attr_destroy(&attr);
2540 return -1;
2542 pthread_attr_destroy(&attr);
2544 ast_mutex_unlock(&monlock);
2545 return 0;
2548 static int h323_do_trace(int fd, int argc, char *argv[])
2550 if (argc != 4) {
2551 return RESULT_SHOWUSAGE;
2553 h323_debug(1, atoi(argv[3]));
2554 ast_cli(fd, "H.323 trace set to level %s\n", argv[2]);
2555 return RESULT_SUCCESS;
2558 static int h323_no_trace(int fd, int argc, char *argv[])
2560 if (argc < 3 || argc > 4) {
2561 return RESULT_SHOWUSAGE;
2563 h323_debug(0,0);
2564 ast_cli(fd, "H.323 trace disabled\n");
2565 return RESULT_SUCCESS;
2568 static int h323_do_debug(int fd, int argc, char *argv[])
2570 if (argc < 2 || argc > 3) {
2571 return RESULT_SHOWUSAGE;
2573 h323debug = 1;
2574 ast_cli(fd, "H.323 debug enabled\n");
2575 return RESULT_SUCCESS;
2578 static int h323_no_debug(int fd, int argc, char *argv[])
2580 if (argc < 3 || argc > 4) {
2581 return RESULT_SHOWUSAGE;
2583 h323debug = 0;
2584 ast_cli(fd, "H.323 debug disabled\n");
2585 return RESULT_SUCCESS;
2588 static int h323_gk_cycle(int fd, int argc, char *argv[])
2590 if (argc != 3) {
2591 return RESULT_SHOWUSAGE;
2593 h323_gk_urq();
2595 /* Possibly register with a GK */
2596 if (!gatekeeper_disable) {
2597 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
2598 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
2601 return RESULT_SUCCESS;
2604 static int h323_ep_hangup(int fd, int argc, char *argv[])
2606 if (argc != 3) {
2607 return RESULT_SHOWUSAGE;
2609 if (h323_soft_hangup(argv[2])) {
2610 ast_verbose(VERBOSE_PREFIX_3 "Hangup succeeded on %s\n", argv[2]);
2611 } else {
2612 ast_verbose(VERBOSE_PREFIX_3 "Hangup failed for %s\n", argv[2]);
2614 return RESULT_SUCCESS;
2617 static int h323_tokens_show(int fd, int argc, char *argv[])
2619 if (argc != 3) {
2620 return RESULT_SHOWUSAGE;
2622 h323_show_tokens();
2623 return RESULT_SUCCESS;
2626 static char trace_usage[] =
2627 "Usage: h.323 trace <level num>\n"
2628 " Enables H.323 stack tracing for debugging purposes\n";
2630 static char no_trace_usage[] =
2631 "Usage: h.323 trace off\n"
2632 " Disables H.323 stack tracing for debugging purposes\n";
2634 static char debug_usage[] =
2635 "Usage: h.323 debug\n"
2636 " Enables H.323 debug output\n";
2638 static char no_debug_usage[] =
2639 "Usage: h.323 debug off\n"
2640 " Disables H.323 debug output\n";
2642 static char show_cycle_usage[] =
2643 "Usage: h.323 gk cycle\n"
2644 " Manually re-register with the Gatekeper (Currently Disabled)\n";
2646 static char show_hangup_usage[] =
2647 "Usage: h.323 hangup <token>\n"
2648 " Manually try to hang up call identified by <token>\n";
2650 static char show_tokens_usage[] =
2651 "Usage: h.323 show tokens\n"
2652 " Print out all active call tokens\n";
2654 static char h323_reload_usage[] =
2655 "Usage: h323 reload\n"
2656 " Reloads H.323 configuration from h323.conf\n";
2658 static struct ast_cli_entry cli_h323_no_trace_deprecated = {
2659 { "h.323", "no", "trace", NULL },
2660 h323_no_trace, "Disable H.323 Stack Tracing",
2661 no_trace_usage };
2663 static struct ast_cli_entry cli_h323_no_debug_deprecated = {
2664 { "h.323", "no", "debug", NULL },
2665 h323_no_debug, "Disable H.323 debug",
2666 no_debug_usage };
2668 static struct ast_cli_entry cli_h323_debug_deprecated = {
2669 { "h.323", "debug", NULL },
2670 h323_do_debug, "Enable H.323 debug",
2671 debug_usage };
2673 static struct ast_cli_entry cli_h323_trace_deprecated = {
2674 { "h.323", "trace", NULL },
2675 h323_do_trace, "Enable H.323 Stack Tracing",
2676 trace_usage };
2678 static struct ast_cli_entry cli_h323_gk_cycle_deprecated = {
2679 { "h.323", "gk", "cycle", NULL },
2680 h323_gk_cycle, "Manually re-register with the Gatekeper",
2681 show_cycle_usage };
2683 static struct ast_cli_entry cli_h323[] = {
2684 { { "h323", "set", "trace", NULL },
2685 h323_do_trace, "Enable H.323 Stack Tracing",
2686 trace_usage, NULL, &cli_h323_trace_deprecated },
2688 { { "h323", "set", "trace", "off", NULL },
2689 h323_no_trace, "Disable H.323 Stack Tracing",
2690 no_trace_usage, NULL, &cli_h323_no_trace_deprecated },
2692 { { "h323", "set", "debug", NULL },
2693 h323_do_debug, "Enable H.323 debug",
2694 debug_usage, NULL, &cli_h323_debug_deprecated },
2696 { { "h323", "set", "debug", "off", NULL },
2697 h323_no_debug, "Disable H.323 debug",
2698 no_debug_usage, NULL, &cli_h323_no_debug_deprecated },
2700 { { "h323", "cycle", "gk", NULL },
2701 h323_gk_cycle, "Manually re-register with the Gatekeper",
2702 show_cycle_usage, NULL, &cli_h323_gk_cycle_deprecated },
2704 { { "h323", "hangup", NULL },
2705 h323_ep_hangup, "Manually try to hang up a call",
2706 show_hangup_usage },
2708 { { "h323", "show", "tokens", NULL },
2709 h323_tokens_show, "Show all active call tokens",
2710 show_tokens_usage },
2713 static void delete_users(void)
2715 int pruned = 0;
2717 /* Delete all users */
2718 ASTOBJ_CONTAINER_WRLOCK(&userl);
2719 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
2720 ASTOBJ_RDLOCK(iterator);
2721 ASTOBJ_MARK(iterator);
2722 ++pruned;
2723 ASTOBJ_UNLOCK(iterator);
2724 } while (0) );
2725 if (pruned) {
2726 ASTOBJ_CONTAINER_PRUNE_MARKED(&userl, oh323_destroy_user);
2728 ASTOBJ_CONTAINER_UNLOCK(&userl);
2730 ASTOBJ_CONTAINER_WRLOCK(&peerl);
2731 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
2732 ASTOBJ_RDLOCK(iterator);
2733 ASTOBJ_MARK(iterator);
2734 ASTOBJ_UNLOCK(iterator);
2735 } while (0) );
2736 ASTOBJ_CONTAINER_UNLOCK(&peerl);
2739 static void delete_aliases(void)
2741 int pruned = 0;
2743 /* Delete all aliases */
2744 ASTOBJ_CONTAINER_WRLOCK(&aliasl);
2745 ASTOBJ_CONTAINER_TRAVERSE(&aliasl, 1, do {
2746 ASTOBJ_RDLOCK(iterator);
2747 ASTOBJ_MARK(iterator);
2748 ++pruned;
2749 ASTOBJ_UNLOCK(iterator);
2750 } while (0) );
2751 if (pruned) {
2752 ASTOBJ_CONTAINER_PRUNE_MARKED(&aliasl, oh323_destroy_alias);
2754 ASTOBJ_CONTAINER_UNLOCK(&aliasl);
2757 static void prune_peers(void)
2759 /* Prune peers who still are supposed to be deleted */
2760 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, oh323_destroy_peer);
2763 static int reload_config(int is_reload)
2765 int format;
2766 struct ast_config *cfg, *ucfg;
2767 struct ast_variable *v;
2768 struct oh323_peer *peer = NULL;
2769 struct oh323_user *user = NULL;
2770 struct oh323_alias *alias = NULL;
2771 struct ast_hostent ahp; struct hostent *hp;
2772 char *cat;
2773 const char *utype;
2774 int is_user, is_peer, is_alias;
2775 char _gatekeeper[100];
2776 int gk_discover, gk_disable, gk_changed;
2778 cfg = ast_config_load(config);
2780 /* We *must* have a config file otherwise stop immediately */
2781 if (!cfg) {
2782 ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
2783 return 1;
2786 if (is_reload) {
2787 delete_users();
2788 delete_aliases();
2789 prune_peers();
2792 /* fire up the H.323 Endpoint */
2793 if (!h323_end_point_exist()) {
2794 h323_end_point_create();
2796 ast_copy_string(_gatekeeper, gatekeeper, sizeof(_gatekeeper));
2797 gk_discover = gatekeeper_discover;
2798 gk_disable = gatekeeper_disable;
2799 memset(&bindaddr, 0, sizeof(bindaddr));
2800 memset(&global_options, 0, sizeof(global_options));
2801 global_options.fastStart = 1;
2802 global_options.h245Tunneling = 1;
2803 global_options.dtmfcodec = 101;
2804 global_options.dtmfmode = H323_DTMF_RFC2833;
2805 global_options.capability = GLOBAL_CAPABILITY;
2806 global_options.bridge = 1; /* Do native bridging by default */
2807 strcpy(default_context, "default");
2808 h323_signalling_port = 1720;
2809 gatekeeper_disable = 1;
2810 gatekeeper_discover = 0;
2811 gkroute = 0;
2812 userbyalias = 1;
2813 acceptAnonymous = 1;
2814 tos = 0;
2816 /* Copy the default jb config over global_jbconf */
2817 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
2819 /* Load configuration from users.conf */
2820 ucfg = ast_config_load("users.conf");
2821 if (ucfg) {
2822 struct ast_variable *gen;
2823 int genhas_h323;
2824 const char *has_h323;
2826 genhas_h323 = ast_true(ast_variable_retrieve(ucfg, "general", "hash323"));
2827 gen = ast_variable_browse(ucfg, "general");
2828 for (cat = ast_category_browse(ucfg, NULL); cat; cat = ast_category_browse(ucfg, cat)) {
2829 if (strcasecmp(cat, "general")) {
2830 has_h323 = ast_variable_retrieve(ucfg, cat, "hash323");
2831 if (ast_true(has_h323) || (!has_h323 && genhas_h323)) {
2832 user = build_user(cat, gen, ast_variable_browse(ucfg, cat), 0);
2833 if (user) {
2834 ASTOBJ_CONTAINER_LINK(&userl, user);
2835 ASTOBJ_UNREF(user, oh323_destroy_user);
2837 peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0);
2838 if (peer) {
2839 ASTOBJ_CONTAINER_LINK(&peerl, peer);
2840 ASTOBJ_UNREF(peer, oh323_destroy_peer);
2845 ast_config_destroy(ucfg);
2848 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
2849 /* handle jb conf */
2850 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
2851 continue;
2852 /* Create the interface list */
2853 if (!strcasecmp(v->name, "port")) {
2854 h323_signalling_port = (int)strtol(v->value, NULL, 10);
2855 } else if (!strcasecmp(v->name, "bindaddr")) {
2856 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
2857 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
2858 } else {
2859 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
2861 } else if (!strcasecmp(v->name, "tos")) {
2862 if (sscanf(v->value, "%d", &format)) {
2863 tos = format & 0xff;
2864 } else if (!strcasecmp(v->value, "lowdelay")) {
2865 tos = IPTOS_LOWDELAY;
2866 } else if (!strcasecmp(v->value, "throughput")) {
2867 tos = IPTOS_THROUGHPUT;
2868 } else if (!strcasecmp(v->value, "reliability")) {
2869 tos = IPTOS_RELIABILITY;
2870 } else if (!strcasecmp(v->value, "mincost")) {
2871 tos = IPTOS_MINCOST;
2872 } else if (!strcasecmp(v->value, "none")) {
2873 tos = 0;
2874 } else {
2875 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
2877 } else if (!strcasecmp(v->name, "gatekeeper")) {
2878 if (!strcasecmp(v->value, "DISABLE")) {
2879 gatekeeper_disable = 1;
2880 } else if (!strcasecmp(v->value, "DISCOVER")) {
2881 gatekeeper_disable = 0;
2882 gatekeeper_discover = 1;
2883 } else {
2884 gatekeeper_disable = 0;
2885 ast_copy_string(gatekeeper, v->value, sizeof(gatekeeper));
2887 } else if (!strcasecmp(v->name, "secret")) {
2888 ast_copy_string(secret, v->value, sizeof(secret));
2889 } else if (!strcasecmp(v->name, "AllowGKRouted")) {
2890 gkroute = ast_true(v->value);
2891 } else if (!strcasecmp(v->name, "context")) {
2892 ast_copy_string(default_context, v->value, sizeof(default_context));
2893 ast_verbose(VERBOSE_PREFIX_2 "Setting default context to %s\n", default_context);
2894 } else if (!strcasecmp(v->name, "UserByAlias")) {
2895 userbyalias = ast_true(v->value);
2896 } else if (!strcasecmp(v->name, "AcceptAnonymous")) {
2897 acceptAnonymous = ast_true(v->value);
2898 } else if (!update_common_options(v, &global_options)) {
2899 /* dummy */
2903 for (cat = ast_category_browse(cfg, NULL); cat; cat = ast_category_browse(cfg, cat)) {
2904 if (strcasecmp(cat, "general")) {
2905 utype = ast_variable_retrieve(cfg, cat, "type");
2906 if (utype) {
2907 is_user = is_peer = is_alias = 0;
2908 if (!strcasecmp(utype, "user"))
2909 is_user = 1;
2910 else if (!strcasecmp(utype, "peer"))
2911 is_peer = 1;
2912 else if (!strcasecmp(utype, "friend"))
2913 is_user = is_peer = 1;
2914 else if (!strcasecmp(utype, "h323") || !strcasecmp(utype, "alias"))
2915 is_alias = 1;
2916 else {
2917 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
2918 continue;
2920 if (is_user) {
2921 user = build_user(cat, ast_variable_browse(cfg, cat), NULL, 0);
2922 if (user) {
2923 ASTOBJ_CONTAINER_LINK(&userl, user);
2924 ASTOBJ_UNREF(user, oh323_destroy_user);
2927 if (is_peer) {
2928 peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0);
2929 if (peer) {
2930 ASTOBJ_CONTAINER_LINK(&peerl, peer);
2931 ASTOBJ_UNREF(peer, oh323_destroy_peer);
2934 if (is_alias) {
2935 alias = build_alias(cat, ast_variable_browse(cfg, cat), NULL, 0);
2936 if (alias) {
2937 ASTOBJ_CONTAINER_LINK(&aliasl, alias);
2938 ASTOBJ_UNREF(alias, oh323_destroy_alias);
2941 } else {
2942 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
2946 ast_config_destroy(cfg);
2948 /* Register our H.323 aliases if any*/
2949 ASTOBJ_CONTAINER_WRLOCK(&aliasl);
2950 ASTOBJ_CONTAINER_TRAVERSE(&aliasl, 1, do {
2951 ASTOBJ_RDLOCK(iterator);
2952 if (h323_set_alias(iterator)) {
2953 ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
2954 ASTOBJ_UNLOCK(iterator);
2955 continue;
2957 ASTOBJ_UNLOCK(iterator);
2958 } while (0) );
2959 ASTOBJ_CONTAINER_UNLOCK(&aliasl);
2961 /* Don't touch GK if nothing changed because URQ will drop all existing calls */
2962 gk_changed = 0;
2963 if (gatekeeper_disable != gk_disable)
2964 gk_changed = is_reload;
2965 else if(!gatekeeper_disable && (gatekeeper_discover != gk_discover))
2966 gk_changed = is_reload;
2967 else if(!gatekeeper_disable && (strncmp(_gatekeeper, gatekeeper, sizeof(_gatekeeper)) != 0))
2968 gk_changed = is_reload;
2969 if (gk_changed) {
2970 if(!gk_disable)
2971 h323_gk_urq();
2972 if (!gatekeeper_disable) {
2973 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
2974 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
2975 gatekeeper_disable = 1;
2979 return 0;
2982 static int h323_reload(int fd, int argc, char *argv[])
2984 ast_mutex_lock(&h323_reload_lock);
2985 if (h323_reloading) {
2986 ast_verbose("Previous H.323 reload not yet done\n");
2987 } else {
2988 h323_reloading = 1;
2990 ast_mutex_unlock(&h323_reload_lock);
2991 restart_monitor();
2992 return 0;
2995 static int h323_do_reload(void)
2997 reload_config(1);
2998 return 0;
3001 static int reload(void)
3003 if (!sched || !io) {
3004 ast_log(LOG_NOTICE, "Unload and load chan_h323.so again in order to receive configuration changes.\n");
3005 return 0;
3007 return h323_reload(0, 0, NULL);
3010 static struct ast_cli_entry cli_h323_reload =
3011 { { "h.323", "reload", NULL },
3012 h323_reload, "Reload H.323 configuration",
3013 h323_reload_usage
3016 static enum ast_rtp_get_result oh323_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
3018 struct oh323_pvt *pvt;
3019 enum ast_rtp_get_result res = AST_RTP_GET_FAILED;
3021 if (!(pvt = (struct oh323_pvt *)chan->tech_pvt))
3022 return res;
3024 ast_mutex_lock(&pvt->lock);
3025 if (pvt->rtp && pvt->options.bridge) {
3026 *rtp = pvt->rtp;
3027 res = AST_RTP_TRY_NATIVE;
3029 ast_mutex_unlock(&pvt->lock);
3031 return res;
3034 static enum ast_rtp_get_result oh323_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
3036 return AST_RTP_GET_FAILED;
3039 static char *convertcap(int cap)
3041 switch (cap) {
3042 case AST_FORMAT_G723_1:
3043 return "G.723";
3044 case AST_FORMAT_GSM:
3045 return "GSM";
3046 case AST_FORMAT_ULAW:
3047 return "ULAW";
3048 case AST_FORMAT_ALAW:
3049 return "ALAW";
3050 case AST_FORMAT_G722:
3051 return "G.722";
3052 case AST_FORMAT_ADPCM:
3053 return "G.728";
3054 case AST_FORMAT_G729A:
3055 return "G.729";
3056 case AST_FORMAT_SPEEX:
3057 return "SPEEX";
3058 case AST_FORMAT_ILBC:
3059 return "ILBC";
3060 default:
3061 ast_log(LOG_NOTICE, "Don't know how to deal with mode %d\n", cap);
3062 return NULL;
3066 static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active)
3068 /* XXX Deal with Video */
3069 struct oh323_pvt *pvt;
3070 struct sockaddr_in them;
3071 struct sockaddr_in us;
3072 char *mode;
3074 if (!rtp) {
3075 return 0;
3078 mode = convertcap(chan->writeformat);
3079 pvt = (struct oh323_pvt *) chan->tech_pvt;
3080 if (!pvt) {
3081 ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
3082 return -1;
3084 ast_rtp_get_peer(rtp, &them);
3085 ast_rtp_get_us(rtp, &us);
3086 #if 0 /* Native bridge still isn't ready */
3087 h323_native_bridge(pvt->cd.call_token, ast_inet_ntoa(them.sin_addr), mode);
3088 #endif
3089 return 0;
3092 static struct ast_rtp_protocol oh323_rtp = {
3093 .type = "H323",
3094 .get_rtp_info = oh323_get_rtp_peer,
3095 .get_vrtp_info = oh323_get_vrtp_peer,
3096 .set_rtp_peer = oh323_set_rtp_peer,
3099 static enum ast_module_load_result load_module(void)
3101 int res;
3103 h323debug = 0;
3104 sched = sched_context_create();
3105 if (!sched) {
3106 ast_log(LOG_WARNING, "Unable to create schedule context\n");
3107 return AST_MODULE_LOAD_FAILURE;
3109 io = io_context_create();
3110 if (!io) {
3111 ast_log(LOG_WARNING, "Unable to create I/O context\n");
3112 return AST_MODULE_LOAD_FAILURE;
3114 ast_cli_register(&cli_h323_reload);
3115 ASTOBJ_CONTAINER_INIT(&userl);
3116 ASTOBJ_CONTAINER_INIT(&peerl);
3117 ASTOBJ_CONTAINER_INIT(&aliasl);
3118 res = reload_config(0);
3119 if (res) {
3120 /* No config entry */
3121 ast_log(LOG_NOTICE, "Unload and load chan_h323.so again in order to receive configuration changes.\n");
3122 ast_cli_unregister(&cli_h323_reload);
3123 io_context_destroy(io);
3124 io = NULL;
3125 sched_context_destroy(sched);
3126 sched = NULL;
3127 ASTOBJ_CONTAINER_DESTROY(&userl);
3128 ASTOBJ_CONTAINER_DESTROY(&peerl);
3129 ASTOBJ_CONTAINER_DESTROY(&aliasl);
3130 return AST_MODULE_LOAD_DECLINE;
3131 } else {
3132 /* Make sure we can register our channel type */
3133 if (ast_channel_register(&oh323_tech)) {
3134 ast_log(LOG_ERROR, "Unable to register channel class 'H323'\n");
3135 ast_cli_unregister(&cli_h323_reload);
3136 h323_end_process();
3137 io_context_destroy(io);
3138 sched_context_destroy(sched);
3140 ASTOBJ_CONTAINER_DESTROYALL(&userl, oh323_destroy_user);
3141 ASTOBJ_CONTAINER_DESTROY(&userl);
3142 ASTOBJ_CONTAINER_DESTROYALL(&peerl, oh323_destroy_peer);
3143 ASTOBJ_CONTAINER_DESTROY(&peerl);
3144 ASTOBJ_CONTAINER_DESTROYALL(&aliasl, oh323_destroy_alias);
3145 ASTOBJ_CONTAINER_DESTROY(&aliasl);
3147 return AST_MODULE_LOAD_FAILURE;
3149 ast_cli_register_multiple(cli_h323, sizeof(cli_h323) / sizeof(struct ast_cli_entry));
3151 ast_rtp_proto_register(&oh323_rtp);
3153 /* Register our callback functions */
3154 h323_callback_register(setup_incoming_call,
3155 setup_outgoing_call,
3156 external_rtp_create,
3157 setup_rtp_connection,
3158 cleanup_connection,
3159 chan_ringing,
3160 connection_made,
3161 receive_digit,
3162 answer_call,
3163 progress,
3164 set_dtmf_payload,
3165 hangup_connection,
3166 set_local_capabilities,
3167 set_peer_capabilities);
3168 /* start the h.323 listener */
3169 if (h323_start_listener(h323_signalling_port, bindaddr)) {
3170 ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
3171 ast_rtp_proto_unregister(&oh323_rtp);
3172 ast_cli_unregister_multiple(cli_h323, sizeof(cli_h323) / sizeof(struct ast_cli_entry));
3173 ast_cli_unregister(&cli_h323_reload);
3174 h323_end_process();
3175 io_context_destroy(io);
3176 sched_context_destroy(sched);
3178 ASTOBJ_CONTAINER_DESTROYALL(&userl, oh323_destroy_user);
3179 ASTOBJ_CONTAINER_DESTROY(&userl);
3180 ASTOBJ_CONTAINER_DESTROYALL(&peerl, oh323_destroy_peer);
3181 ASTOBJ_CONTAINER_DESTROY(&peerl);
3182 ASTOBJ_CONTAINER_DESTROYALL(&aliasl, oh323_destroy_alias);
3183 ASTOBJ_CONTAINER_DESTROY(&aliasl);
3185 return AST_MODULE_LOAD_FAILURE;
3187 /* Possibly register with a GK */
3188 if (!gatekeeper_disable) {
3189 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
3190 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
3191 gatekeeper_disable = 1;
3192 res = AST_MODULE_LOAD_SUCCESS;
3195 /* And start the monitor for the first time */
3196 restart_monitor();
3198 return res;
3201 static int unload_module(void)
3203 struct oh323_pvt *p, *pl;
3205 /* unregister commands */
3206 ast_cli_unregister_multiple(cli_h323, sizeof(cli_h323) / sizeof(struct ast_cli_entry));
3207 ast_cli_unregister(&cli_h323_reload);
3209 ast_channel_unregister(&oh323_tech);
3210 ast_rtp_proto_unregister(&oh323_rtp);
3212 if (!ast_mutex_lock(&iflock)) {
3213 /* hangup all interfaces if they have an owner */
3214 p = iflist;
3215 while(p) {
3216 if (p->owner) {
3217 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
3219 p = p->next;
3221 iflist = NULL;
3222 ast_mutex_unlock(&iflock);
3223 } else {
3224 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
3225 return -1;
3227 if (!ast_mutex_lock(&monlock)) {
3228 if ((monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
3229 /* this causes a seg, anyone know why? */
3230 if (monitor_thread != pthread_self())
3231 pthread_cancel(monitor_thread);
3232 pthread_kill(monitor_thread, SIGURG);
3233 pthread_join(monitor_thread, NULL);
3235 monitor_thread = AST_PTHREADT_STOP;
3236 ast_mutex_unlock(&monlock);
3237 } else {
3238 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
3239 return -1;
3241 if (!ast_mutex_lock(&iflock)) {
3242 /* destroy all the interfaces and free their memory */
3243 p = iflist;
3244 while(p) {
3245 pl = p;
3246 p = p->next;
3247 /* free associated memory */
3248 ast_mutex_destroy(&pl->lock);
3249 free(pl);
3251 iflist = NULL;
3252 ast_mutex_unlock(&iflock);
3253 } else {
3254 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
3255 return -1;
3257 if (!gatekeeper_disable)
3258 h323_gk_urq();
3259 h323_end_process();
3260 if (io)
3261 io_context_destroy(io);
3262 if (sched)
3263 sched_context_destroy(sched);
3265 ASTOBJ_CONTAINER_DESTROYALL(&userl, oh323_destroy_user);
3266 ASTOBJ_CONTAINER_DESTROY(&userl);
3267 ASTOBJ_CONTAINER_DESTROYALL(&peerl, oh323_destroy_peer);
3268 ASTOBJ_CONTAINER_DESTROY(&peerl);
3269 ASTOBJ_CONTAINER_DESTROYALL(&aliasl, oh323_destroy_alias);
3270 ASTOBJ_CONTAINER_DESTROY(&aliasl);
3272 return 0;
3275 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "The NuFone Network's OpenH323 Channel Driver",
3276 .load = load_module,
3277 .unload = unload_module,
3278 .reload = reload,