2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005
6 * OpenH323 Channel Driver for ASTERISK PBX.
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.
26 * \brief This file is part of the chan_h323 driver for Asterisk
28 * \author Jeremy McNamara
33 * \ingroup channel_drivers
37 <depend>openh323</depend>
38 <defaultenabled>yes</defaultenabled>
47 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
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)
59 #define IPTOS_MINCOST 0x02
62 #include <arpa/inet.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
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"
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
;
119 setcapabilities_cb on_setcapabilities
;
120 setpeercapabilities_cb on_setpeercapabilities
;
122 /* global debug flag */
125 /*! Global jitterbuffer configuration - by default, jb is disabled */
126 static struct ast_jb_conf default_jbconf
=
130 .resync_threshold
= -1,
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;
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 */
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 */
200 static struct ast_user_list
{
201 ASTOBJ_CONTAINER_COMPONENTS(struct oh323_user
);
204 static struct ast_peer_list
{
205 ASTOBJ_CONTAINER_COMPONENTS(struct oh323_peer
);
208 static struct ast_alias_list
{
209 ASTOBJ_CONTAINER_COMPONENTS(struct oh323_alias
);
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
= {
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
,
256 .hangup
= oh323_hangup
,
257 .answer
= oh323_answer
,
259 .write
= oh323_write
,
260 .indicate
= oh323_indicate
,
261 .fixup
= oh323_fixup
,
262 /* disable, for now */
264 .bridge
= ast_rtp_bridge
,
268 static const char* redirectingreason2str(int redirectingreason
)
270 switch (redirectingreason
) {
278 return "UNCONDITIONAL";
284 static void oh323_destroy_alias(struct oh323_alias
*alias
)
287 ast_log(LOG_DEBUG
, "Destroying alias '%s'\n", alias
->name
);
291 static void oh323_destroy_user(struct oh323_user
*user
)
294 ast_log(LOG_DEBUG
, "Destroying user '%s'\n", user
->name
);
295 ast_free_ha(user
->ha
);
299 static void oh323_destroy_peer(struct oh323_peer
*peer
)
302 ast_log(LOG_DEBUG
, "Destroying peer '%s'\n", peer
->name
);
303 ast_free_ha(peer
->ha
);
307 static int oh323_simulate_dtmf_end(const void *data
)
309 struct oh323_pvt
*pvt
= (struct oh323_pvt
*)data
;
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
);
317 ast_mutex_lock(&pvt
->lock
);
321 struct ast_frame f
= {
322 .frametype
= AST_FRAME_DTMF_END
,
323 .subclass
= pvt
->curDTMF
,
325 .src
= "SIMULATE_DTMF_END",
327 ast_queue_frame(pvt
->owner
, &f
);
328 ast_channel_unlock(pvt
->owner
);
332 ast_mutex_unlock(&pvt
->lock
);
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
) {
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
) {
350 ast_log(LOG_DEBUG
, "Process pending hangup for %s\n", c
->name
);
351 c
->_softhangup
|= AST_SOFTHANGUP_DEV
;
352 c
->hangupcause
= pvt
->hangupcause
;
355 pvt
->newstate
= pvt
->newcontrol
= pvt
->newdigit
= pvt
->DTMFsched
= -1;
357 if (pvt
->newstate
>= 0) {
358 ast_setstate(c
, pvt
->newstate
);
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
);
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
);
391 if (pvt
->update_rtp_info
> 0) {
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
;
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
;
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
);
457 ast_rtp_destroy(pvt
->rtp
);
460 /* Free dsp used for in-band DTMF detection */
462 ast_dsp_free(pvt
->vad
);
464 cleanup_call_details(&pvt
->cd
);
466 /* Unlink us from the owner if we have one */
468 ast_channel_lock(pvt
->owner
);
470 ast_log(LOG_DEBUG
, "Detaching from %s\n", pvt
->owner
->name
);
471 pvt
->owner
->tech_pvt
= NULL
;
472 ast_channel_unlock(pvt
->owner
);
478 prev
->next
= cur
->next
;
487 ast_log(LOG_WARNING
, "%p is not in list?!?! \n", cur
);
489 ast_mutex_unlock(&pvt
->lock
);
490 ast_mutex_destroy(&pvt
->lock
);
495 static void oh323_destroy(struct oh323_pvt
*pvt
)
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
;
512 ast_log(LOG_ERROR
, "No private structure?! This is bad\n");
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 */
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
) {
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
);
536 ast_mutex_unlock(&pvt
->lock
);
537 oh323_update_info(c
);
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
;
551 ast_log(LOG_ERROR
, "No private structure?! This is bad\n");
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 */
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
);
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
, ' ');
575 oh323_update_info(c
);
580 * Make a call over the specified channel to the specified
582 * Returns -1 on error, 0 on success.
584 static int oh323_call(struct ast_channel
*c
, char *dest
, int timeout
)
587 struct oh323_pvt
*pvt
= (struct oh323_pvt
*)c
->tech_pvt
;
589 char called_addr
[1024];
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
);
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
));
603 snprintf(called_addr
, sizeof(called_addr
), "%s@%s", pvt
->exten
, dest
);
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
);
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';
618 ast_copy_string(pvt
->options
.cid_num
, c
->cid
.cid_num
, sizeof(pvt
->options
.cid_num
));
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;
640 pvt
->options
.redirect_reason
= -1;
642 pvt
->options
.redirect_reason
= -1;
644 pvt
->options
.transfer_capability
= c
->transfercapability
;
646 /* indicate that this is an outgoing call */
649 if (option_verbose
> 2)
650 ast_verbose(VERBOSE_PREFIX_3
"Requested transfer capability: 0x%.2x - %s\n", c
->transfercapability
, ast_transfercapability2str(c
->transfercapability
));
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
);
656 ast_log(LOG_NOTICE
, "h323_make_call failed(%s)\n", c
->name
);
659 oh323_update_info(c
);
663 static int oh323_answer(struct ast_channel
*c
)
666 struct oh323_pvt
*pvt
= (struct oh323_pvt
*) c
->tech_pvt
;
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);
679 oh323_update_info(c
);
680 if (c
->_state
!= AST_STATE_UP
) {
681 ast_setstate(c
, AST_STATE_UP
);
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
;
694 ast_log(LOG_DEBUG
, "Hanging up and scheduling destroy of call %s\n", c
->name
);
697 ast_log(LOG_WARNING
, "Asked to hangup channel not connected\n");
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
);
711 if (c
->hangupcause
) {
712 q931cause
= c
->hangupcause
;
714 const char *cause
= pbx_builtin_getvar_helper(c
, "DIALSTATUS");
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
;
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");
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
);
752 static struct ast_frame
*oh323_rtp_read(struct oh323_pvt
*pvt
)
754 /* Retrieve audio/etc from channel. Assumes pvt->lock is already held. */
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
;
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
;
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
);
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
)) {
800 ast_log(LOG_DTMF
, "Received in-band digit %c.\n", f
->subclass
);
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
);
816 fr
= oh323_rtp_read(pvt
);
820 fr
= ast_rtcp_read(pvt
->rtp
);
822 fr
= &ast_null_frame
;
825 ast_log(LOG_ERROR
, "Unable to handle fd %d on channel %s\n", c
->fdno
, c
->name
);
826 fr
= &ast_null_frame
;
829 ast_mutex_unlock(&pvt
->lock
);
833 static int oh323_write(struct ast_channel
*c
, struct ast_frame
*frame
)
835 struct oh323_pvt
*pvt
= (struct oh323_pvt
*) c
->tech_pvt
;
837 if (frame
->frametype
!= AST_FRAME_VOICE
) {
838 if (frame
->frametype
== AST_FRAME_IMAGE
) {
841 ast_log(LOG_WARNING
, "Can't send %d type frames with H323 write\n", frame
->frametype
);
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
);
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
);
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
;
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
);
879 ast_log(LOG_DEBUG
, "OH323: Indicating %d on %s\n", condition
, token
);
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 */
888 case AST_CONTROL_PROGRESS
:
889 if (c
->_state
!= AST_STATE_UP
) {
890 /* Do not send PROGRESS message more than once */
892 h323_send_progress(token
);
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
);
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
);
910 case AST_CONTROL_HOLD
:
911 ast_moh_start(c
, data
, NULL
);
914 case AST_CONTROL_UNHOLD
:
918 case AST_CONTROL_SRCUPDATE
:
919 ast_rtp_new_source(pvt
->rtp
);
922 case AST_CONTROL_PROCEEDING
:
926 ast_log(LOG_WARNING
, "OH323: Don't know how to indicate condition %d on %s\n", condition
, token
);
931 ast_log(LOG_DEBUG
, "OH323: Indicated %d on %s, res=%d\n", condition
, token
, res
);
934 oh323_update_info(c
);
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
);
948 pvt
->owner
= newchan
;
949 ast_mutex_unlock(&pvt
->lock
);
953 static int __oh323_rtp_create(struct oh323_pvt
*pvt
)
955 struct in_addr our_addr
;
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");
965 pvt
->rtp
= ast_rtp_new_with_bindaddr(sched
, io
, 1, 0, our_addr
);
967 ast_mutex_unlock(&pvt
->lock
);
968 ast_log(LOG_WARNING
, "Unable to create RTP session: %s\n", strerror(errno
));
972 ast_log(LOG_DEBUG
, "Created RTP channel\n");
974 ast_rtp_settos(pvt
->rtp
, tos
);
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
);
993 pvt
->update_rtp_info
= 1;
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
;
1005 if (!ast_strlen_zero(pvt
->options
.cid_num
))
1006 cid_num
= pvt
->options
.cid_num
;
1008 cid_num
= pvt
->cd
.call_source_e164
;
1010 if (!ast_strlen_zero(pvt
->options
.cid_name
))
1011 cid_name
= pvt
->options
.cid_name
;
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
);
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
;
1033 ch
->fds
[0] = ast_rtp_fd(pvt
->rtp
);
1034 ch
->fds
[1] = ast_rtcp_fd(pvt
->rtp
);
1036 #ifdef VIDEO_SUPPORT
1038 ch
->fds
[2] = ast_rtp_fd(pvt
->vrtp
);
1039 ch
->fds
[3] = ast_rtcp_fd(pvt
->vrtp
);
1044 ch
->fds
[4] = ast_udptl_fd(pvt
->udptl
);
1047 if (state
== AST_STATE_RING
) {
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. */
1057 /* Set the owner of this channel */
1060 ast_copy_string(ch
->context
, pvt
->context
, sizeof(ch
->context
));
1061 ast_copy_string(ch
->exten
, pvt
->exten
, sizeof(ch
->exten
));
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
);
1094 ast_log(LOG_WARNING
, "Unable to allocate channel structure\n");
1099 static struct oh323_pvt
*oh323_alloc(int callid
)
1101 struct oh323_pvt
*pvt
;
1103 pvt
= (struct oh323_pvt
*) malloc(sizeof(struct oh323_pvt
));
1105 ast_log(LOG_ERROR
, "Couldn't allocate private structure. This is bad\n");
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 */
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
);
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
;
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
);
1139 ast_mutex_unlock(&iflock
);
1143 static struct oh323_pvt
*find_call_locked(int call_reference
, const char *token
)
1145 struct oh323_pvt
*pvt
;
1147 ast_mutex_lock(&iflock
);
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
);
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
);
1165 ast_mutex_unlock(&iflock
);
1169 static int update_state(struct oh323_pvt
*pvt
, int state
, int signal
)
1173 if (pvt
->owner
&& !ast_channel_trylock(pvt
->owner
)) {
1175 ast_setstate(pvt
->owner
, state
);
1177 ast_queue_control(pvt
->owner
, signal
);
1178 ast_channel_unlock(pvt
->owner
);
1183 pvt
->newstate
= state
;
1185 pvt
->newcontrol
= signal
;
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
;
1195 alias
= ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&aliasl
, name
, name
, 0, 0, strcasecmp
);
1200 if (!(alias
= (struct oh323_alias
*)calloc(1, sizeof(*alias
))))
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
));
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
);
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
);
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
);
1243 a
= build_alias(alias
, var
, NULL
, 1);
1245 ast_variables_destroy(var
);
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
)
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
;
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
);
1273 ast_log(LOG_WARNING
, "Invalid %s value %s at line %d\n", v
->name
, v
->value
, v
->lineno
);
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
);
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
);
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
;
1325 ast_log(LOG_WARNING
, "Invalid value %s for %s at line %d\n", v
->value
, v
->name
, v
->lineno
);
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
;
1340 user
= ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&userl
, name
, name
, 0, 0, strcmp
);
1345 if (!(user
= (struct oh323_user
*)calloc(1, sizeof(*user
))))
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
));
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
;
1364 for (; v
|| ((v
= alt
) && !(alt
= NULL
)); v
= v
->next
) {
1365 if (!update_common_options(v
, &user
->options
))
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
);
1378 } else if (ast_get_ip(&user
->addr
, v
->value
)) {
1379 ASTOBJ_UNREF(user
, oh323_destroy_user
);
1382 /* Let us know we need to use ip authentication */
1384 } else if (!strcasecmp(v
->name
, "amaflags")) {
1385 format
= ast_cdr_amaflags2int(v
->value
);
1387 ast_log(LOG_WARNING
, "Invalid AMA Flags: %s at line %d\n", v
->value
, v
->lineno
);
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
);
1401 static struct oh323_user
*realtime_user(const call_details_t
*cd
)
1403 struct ast_variable
*var
, *tmp
;
1404 struct oh323_user
*user
;
1408 var
= ast_load_realtime("h323", "name", username
= cd
->call_source_aliases
, NULL
);
1410 username
= (char *)NULL
;
1411 var
= ast_load_realtime("h323", "host", cd
->sourceIp
, 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
);
1422 } else if (!username
&& !strcasecmp(tmp
->name
, "name"))
1423 username
= tmp
->value
;
1427 ast_log(LOG_WARNING
, "Cannot determine user name for IP address %s\n", cd
->sourceIp
);
1428 ast_variables_destroy(var
);
1432 user
= build_user(username
, var
, NULL
, 1);
1434 ast_variables_destroy(var
);
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
;
1445 peer
= ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&peerl
, name
, name
, 0, 0, strcmp
);
1450 if (!(peer
= (struct oh323_peer
*)calloc(1, sizeof(*peer
))))
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
;
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
;
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
))
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
);
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
);
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
);
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
;
1510 /* First check on peer name */
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
);
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
);
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
);
1538 /* Peer found in realtime, now build it in memory */
1539 peer
= build_peer(peername
, var
, NULL
, 1);
1541 ast_variables_destroy(var
);
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
;
1556 u
= ASTOBJ_CONTAINER_FIND(&userl
, cd
->call_source_aliases
);
1558 u
= ASTOBJ_CONTAINER_FIND_FULL(&userl
, cd
->sourceIp
, addr
.sin_addr
, 0, 0, oh323_addrcmp_str
);
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
);
1569 static int oh323_addrcmp(struct sockaddr_in addr
, struct sockaddr_in
*sin
)
1576 res
= inaddrcmp(&addr
, sin
);
1581 static struct oh323_peer
*find_peer(const char *peer
, struct sockaddr_in
*sin
, int realtime
)
1583 struct oh323_peer
*p
;
1586 p
= ASTOBJ_CONTAINER_FIND(&peerl
, peer
);
1588 p
= ASTOBJ_CONTAINER_FIND_FULL(&peerl
, sin
, addr
, 0, 0, oh323_addrcmp
);
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>"));
1599 static int create_addr(struct oh323_pvt
*pvt
, char *opeer
)
1602 struct ast_hostent ahp
;
1603 struct oh323_peer
*p
;
1608 char peer
[256] = "";
1610 ast_copy_string(peer
, opeer
, sizeof(peer
));
1611 port
= strchr(peer
, ':');
1616 pvt
->sa
.sin_family
= AF_INET
;
1617 p
= find_peer(peer
, NULL
, 1);
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
;
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
);
1638 portno
= atoi(port
);
1640 portno
= h323_signalling_port
;
1642 hp
= ast_gethostbyname(hostn
, &ahp
);
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
;
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
;
1657 pvt
->nonCodecCapability
&= ~AST_RTP_DTMF
;
1662 ast_log(LOG_WARNING
, "No such host: %s\n", peer
);
1665 } else if (!found
) {
1671 static struct ast_channel
*oh323_request(const char *type
, int format
, void *data
, int *cause
)
1674 struct oh323_pvt
*pvt
;
1675 struct ast_channel
*tmpc
= NULL
;
1676 char *dest
= (char *)data
;
1678 char *h323id
= NULL
;
1679 char tmp
[256], tmp1
[256];
1682 ast_log(LOG_DEBUG
, "type=%s, format=%d, data=%s.\n", type
, format
, (char *)data
);
1684 pvt
= oh323_alloc(0);
1686 ast_log(LOG_WARNING
, "Unable to build pvt data for '%s'\n", (char *)data
);
1690 format
&= ((AST_FORMAT_MAX_AUDIO
<< 1) - 1);
1692 ast_log(LOG_NOTICE
, "Asked to get a channel of unsupported format '%d'\n", format
);
1695 *cause
= AST_CAUSE_INCOMPATIBLE_DESTINATION
;
1698 ast_copy_string(tmp
, dest
, sizeof(tmp
));
1699 host
= strchr(tmp
, '@');
1705 ext
= strrchr(tmp
, '/');
1710 strtok_r(host
, "/", &(h323id
));
1711 if (!ast_strlen_zero(h323id
)) {
1712 h323_set_id(h323id
);
1715 ast_copy_string(pvt
->exten
, ext
, sizeof(pvt
->exten
));
1718 ast_log(LOG_DEBUG
, "Extension: %s Host: %s\n", pvt
->exten
, host
);
1720 if (gatekeeper_disable
) {
1721 if (create_addr(pvt
, host
)) {
1724 *cause
= AST_CAUSE_DESTINATION_OUT_OF_ORDER
;
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
;
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
);
1752 *cause
= AST_CAUSE_NORMAL_TEMPORARY_FAILURE
;
1754 ast_update_use_count();
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
);
1767 a
= realtime_alias(source_aliases
);
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
;
1781 pvt
= find_call_locked(call_reference
, token
);
1783 ast_log(LOG_ERROR
, "Received digit '%c' (%u ms) for call %s without private structure\n", digit
, duration
, token
);
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
)) {
1791 res
= ast_queue_control(pvt
->owner
, AST_CONTROL_FLASH
);
1793 struct ast_frame f
= {
1794 .frametype
= AST_FRAME_DTMF_END
,
1796 .samples
= duration
* 8,
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 */
1812 f
.samples
= duration
* 8;
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
);
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
);
1828 pvt
->newcontrol
= AST_CONTROL_FLASH
;
1830 pvt
->newduration
= duration
;
1831 pvt
->newdigit
= digit
;
1835 ast_mutex_unlock(&pvt
->lock
);
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
));
1852 ast_log(LOG_ERROR
, "Unable to allocated info structure, this is very bad\n");
1855 pvt
= find_call_locked(call_reference
, token
);
1858 ast_log(LOG_ERROR
, "Unable to find call %s(%d)\n", token
, call_reference
);
1862 __oh323_rtp_create(pvt
);
1864 ast_mutex_unlock(&pvt
->lock
);
1866 ast_log(LOG_ERROR
, "No RTP stream is available for call %s (%d)", token
, call_reference
);
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
);
1876 ast_log(LOG_DEBUG
, "Sending RTP 'US' %s:%d\n", info
->addr
, info
->port
);
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 */
1889 * Call-back function passing remote ip/port information from H.323 to asterisk
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
;
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
);
1907 ast_log(LOG_ERROR
, "Something is wrong: rtp\n");
1910 if (pvt
->alreadygone
) {
1911 ast_mutex_unlock(&pvt
->lock
);
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
) {
1927 rtp_change
= NEED_UNHOLD
;
1930 ast_rtp_stop(pvt
->rtp
);
1931 if (!pvt
->recvonly
) {
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
);
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
) {
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
) {
1965 ast_queue_control(pvt
->owner
, AST_CONTROL_HOLD
);
1968 ast_queue_control(pvt
->owner
, AST_CONTROL_UNHOLD
);
1973 ast_channel_unlock(pvt
->owner
);
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
;
1983 ast_log(LOG_DEBUG
, "RTP connection preparation for %s is pending...\n", token
);
1986 ast_mutex_unlock(&pvt
->lock
);
1989 ast_log(LOG_DEBUG
, "RTP connection prepared for %s\n", token
);
1995 * Call-back function to signal asterisk that the channel has been answered
1998 static void connection_made(unsigned call_reference
, const char *token
)
2000 struct oh323_pvt
*pvt
;
2003 ast_log(LOG_DEBUG
, "Call %s answered\n", token
);
2005 pvt
= find_call_locked(call_reference
, token
);
2007 ast_log(LOG_ERROR
, "Something is wrong: connection\n");
2011 /* Inform asterisk about remote party connected only on outgoing calls */
2012 if (!pvt
->outgoing
) {
2013 ast_mutex_unlock(&pvt
->lock
);
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
);
2025 static int progress(unsigned call_reference
, const char *token
, int inband
)
2027 struct oh323_pvt
*pvt
;
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
);
2034 ast_log(LOG_ERROR
, "Private structure not found in progress.\n");
2038 ast_mutex_unlock(&pvt
->lock
);
2039 ast_log(LOG_ERROR
, "No Asterisk channel associated with private structure.\n");
2042 update_state(pvt
, -1, (inband
? AST_CONTROL_PROGRESS
: AST_CONTROL_RINGING
));
2043 ast_mutex_unlock(&pvt
->lock
);
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
;
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
);
2066 ast_log(LOG_ERROR
, "Unable to allocate private structure, this is bad.\n");
2067 cleanup_call_details(cd
);
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
;
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
));
2094 alias
= find_alias(cd
->call_dest_alias
, 1);
2096 ast_log(LOG_ERROR
, "Call for %s rejected, alias not found\n", cd
->call_dest_alias
);
2100 ast_copy_string(pvt
->exten
, alias
->name
, sizeof(pvt
->exten
));
2101 ast_copy_string(pvt
->context
, alias
->context
, sizeof(pvt
->context
));
2104 /* Either this call is not from the Gatekeeper
2105 or we are not allowing gk routed calls */
2106 user
= find_user(cd
, 1);
2108 if (!acceptAnonymous
) {
2109 ast_log(LOG_NOTICE
, "Anonymous call from '%s@%s' rejected\n", pvt
->cd
.call_source_aliases
, pvt
->cd
.sourceIp
);
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
);
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
));
2122 ast_copy_string(pvt
->exten
, cd
->call_dest_alias
, sizeof(pvt
->exten
));
2125 ast_log(LOG_DEBUG
, "Sending %s@%s to context [%s] extension %s\n", cd
->call_source_aliases
, cd
->sourceIp
, pvt
->context
, pvt
->exten
);
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
);
2133 ASTOBJ_UNREF(user
, oh323_destroy_user
);
2136 ast_copy_string(pvt
->context
, default_context
, sizeof(pvt
->context
));
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
);
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
));
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
)];
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
);
2186 ast_log(LOG_ERROR
, "Something is wrong: answer_call\n");
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')
2196 else if (tmp_exten
[0] == 'i')
2199 try_exten
= ext_original
;
2201 try_exten
= ext_original
;
2203 if (ast_exists_extension(NULL
, pvt
->context
, tmp_exten
, 1, NULL
))
2205 switch (try_exten
) {
2208 tmp_exten
[1] = '\0';
2216 try_exten
= ext_notexists
;
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
);
2229 } else if ((try_exten
!= ext_original
) && (strcmp(pvt
->exten
, tmp_exten
) != 0)) {
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
);
2241 ast_log(LOG_ERROR
, "Couldn't create channel. This is bad\n");
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
);
2261 * Call-back function to signal asterisk that the channel is ringing
2264 static void chan_ringing(unsigned call_reference
, const char *token
)
2266 struct oh323_pvt
*pvt
;
2269 ast_log(LOG_DEBUG
, "Ringing on %s\n", token
);
2271 pvt
= find_call_locked(call_reference
, token
);
2273 ast_log(LOG_ERROR
, "Something is wrong: ringing\n");
2277 ast_mutex_unlock(&pvt
->lock
);
2278 ast_log(LOG_ERROR
, "Channel has no owner\n");
2281 update_state(pvt
, AST_STATE_RINGING
, AST_CONTROL_RINGING
);
2282 ast_mutex_unlock(&pvt
->lock
);
2287 * Call-back function to cleanup communication
2290 static void cleanup_connection(unsigned call_reference
, const char *call_token
)
2292 struct oh323_pvt
*pvt
;
2295 ast_log(LOG_DEBUG
, "Cleaning connection to %s\n", call_token
);
2298 pvt
= find_call_locked(call_reference
, call_token
);
2301 ast_log(LOG_DEBUG
, "No connection for %s\n", call_token
);
2304 if (!pvt
->owner
|| !ast_channel_trylock(pvt
->owner
))
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]);
2310 ast_log(LOG_NOTICE
, "Avoiding H.323 destory deadlock on %s\n", call_token
);
2313 ast_mutex_unlock(&pvt
->lock
);
2317 /* Immediately stop RTP */
2318 ast_rtp_destroy(pvt
->rtp
);
2321 /* Free dsp used for in-band DTMF detection */
2323 ast_dsp_free(pvt
->vad
);
2326 cleanup_call_details(&pvt
->cd
);
2327 pvt
->alreadygone
= 1;
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
);
2336 ast_log(LOG_DEBUG
, "Connection to %s cleaned\n", call_token
);
2340 static void hangup_connection(unsigned int call_reference
, const char *token
, int cause
)
2342 struct oh323_pvt
*pvt
;
2345 ast_log(LOG_DEBUG
, "Hanging up connection to %s with cause %d\n", token
, cause
);
2348 pvt
= find_call_locked(call_reference
, token
);
2351 ast_log(LOG_DEBUG
, "Connection to %s already cleared\n", token
);
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
);
2362 pvt
->needhangup
= 1;
2363 pvt
->hangupcause
= cause
;
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
;
2375 ast_log(LOG_DEBUG
, "Setting DTMF payload to %d on %s\n", payload
, token
);
2377 pvt
= find_call_locked(call_reference
, token
);
2382 ast_rtp_set_rtpmap_type(pvt
->rtp
, payload
, "audio", "telephone-event", 0);
2384 pvt
->dtmf_pt
= payload
;
2385 ast_mutex_unlock(&pvt
->lock
);
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
;
2395 ast_log(LOG_DEBUG
, "Got remote capabilities from connection %s\n", token
);
2397 pvt
= find_call_locked(call_reference
, token
);
2400 pvt
->peercapability
= capabilities
;
2401 pvt
->jointcapability
= pvt
->options
.capability
& capabilities
;
2403 memcpy(&pvt
->peer_prefs
, prefs
, sizeof(pvt
->peer_prefs
));
2406 for (i
= 0; i
< 32; ++i
) {
2407 if (!prefs
->order
[i
])
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
]);
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
;
2425 ast_log(LOG_DEBUG
, "Setting capabilities for connection %s\n", token
);
2427 pvt
= find_call_locked(call_reference
, token
);
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
);
2438 ast_log(LOG_DEBUG
, "Capabilities for connection %s is set\n", token
);
2441 static void *do_monitor(void *data
)
2445 struct oh323_pvt
*oh323
= NULL
;
2448 /* Check for a reload request */
2449 ast_mutex_lock(&h323_reload_lock
);
2450 reloading
= h323_reloading
;
2452 ast_mutex_unlock(&h323_reload_lock
);
2454 if (option_verbose
> 0) {
2455 ast_verbose(VERBOSE_PREFIX_1
"Reloading H.323\n");
2459 /* Check for interfaces needing to be killed */
2460 if (!ast_mutex_trylock(&iflock
)) {
2463 for (oh323
= iflist
; oh323
; oh323
= oh323
->next
) {
2464 if (!ast_mutex_trylock(&oh323
->lock
)) {
2465 if (oh323
->needdestroy
) {
2466 __oh323_destroy(oh323
);
2469 ast_mutex_unlock(&oh323
->lock
);
2472 } while (/*oh323*/ 0);
2477 if (!ast_mutex_trylock(&oh323
->lock
)) {
2478 if (oh323
->needdestroy
) {
2479 __oh323_destroy(oh323
);
2482 ast_mutex_unlock(&oh323
->lock
);
2483 oh323
= oh323
->next
;
2487 ast_mutex_unlock(&iflock
);
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)) {
2496 /* Do not wait if some channel(s) is destroyed, probably, more available too */
2499 res
= ast_io_wait(io
, res
);
2500 pthread_testcancel();
2501 ast_mutex_lock(&monlock
);
2503 ast_sched_runq(sched
);
2505 ast_mutex_unlock(&monlock
);
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");
2519 if (monitor_thread
== AST_PTHREADT_STOP
) {
2520 ast_mutex_unlock(&monlock
);
2523 if (monitor_thread
== pthread_self()) {
2524 ast_mutex_unlock(&monlock
);
2525 ast_log(LOG_WARNING
, "Cannot kill myself\n");
2528 if (monitor_thread
&& (monitor_thread
!= AST_PTHREADT_NULL
)) {
2529 /* Wake up the thread */
2530 pthread_kill(monitor_thread
, SIGURG
);
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
);
2542 pthread_attr_destroy(&attr
);
2544 ast_mutex_unlock(&monlock
);
2548 static int h323_do_trace(int fd
, int argc
, char *argv
[])
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
;
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
;
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
;
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
[])
2591 return RESULT_SHOWUSAGE
;
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
[])
2607 return RESULT_SHOWUSAGE
;
2609 if (h323_soft_hangup(argv
[2])) {
2610 ast_verbose(VERBOSE_PREFIX_3
"Hangup succeeded on %s\n", argv
[2]);
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
[])
2620 return RESULT_SHOWUSAGE
;
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",
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",
2668 static struct ast_cli_entry cli_h323_debug_deprecated
= {
2669 { "h.323", "debug", NULL
},
2670 h323_do_debug
, "Enable H.323 debug",
2673 static struct ast_cli_entry cli_h323_trace_deprecated
= {
2674 { "h.323", "trace", NULL
},
2675 h323_do_trace
, "Enable H.323 Stack Tracing",
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",
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)
2717 /* Delete all users */
2718 ASTOBJ_CONTAINER_WRLOCK(&userl
);
2719 ASTOBJ_CONTAINER_TRAVERSE(&userl
, 1, do {
2720 ASTOBJ_RDLOCK(iterator
);
2721 ASTOBJ_MARK(iterator
);
2723 ASTOBJ_UNLOCK(iterator
);
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
);
2736 ASTOBJ_CONTAINER_UNLOCK(&peerl
);
2739 static void delete_aliases(void)
2743 /* Delete all aliases */
2744 ASTOBJ_CONTAINER_WRLOCK(&aliasl
);
2745 ASTOBJ_CONTAINER_TRAVERSE(&aliasl
, 1, do {
2746 ASTOBJ_RDLOCK(iterator
);
2747 ASTOBJ_MARK(iterator
);
2749 ASTOBJ_UNLOCK(iterator
);
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
)
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
;
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 */
2782 ast_log(LOG_NOTICE
, "Unable to load config %s, H.323 disabled\n", config
);
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;
2813 acceptAnonymous
= 1;
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");
2822 struct ast_variable
*gen
;
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);
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);
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
))
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
);
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")) {
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;
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
)) {
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");
2907 is_user
= is_peer
= is_alias
= 0;
2908 if (!strcasecmp(utype
, "user"))
2910 else if (!strcasecmp(utype
, "peer"))
2912 else if (!strcasecmp(utype
, "friend"))
2913 is_user
= is_peer
= 1;
2914 else if (!strcasecmp(utype
, "h323") || !strcasecmp(utype
, "alias"))
2917 ast_log(LOG_WARNING
, "Unknown type '%s' for '%s' in %s\n", utype
, cat
, config
);
2921 user
= build_user(cat
, ast_variable_browse(cfg
, cat
), NULL
, 0);
2923 ASTOBJ_CONTAINER_LINK(&userl
, user
);
2924 ASTOBJ_UNREF(user
, oh323_destroy_user
);
2928 peer
= build_peer(cat
, ast_variable_browse(cfg
, cat
), NULL
, 0);
2930 ASTOBJ_CONTAINER_LINK(&peerl
, peer
);
2931 ASTOBJ_UNREF(peer
, oh323_destroy_peer
);
2935 alias
= build_alias(cat
, ast_variable_browse(cfg
, cat
), NULL
, 0);
2937 ASTOBJ_CONTAINER_LINK(&aliasl
, alias
);
2938 ASTOBJ_UNREF(alias
, oh323_destroy_alias
);
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
);
2957 ASTOBJ_UNLOCK(iterator
);
2959 ASTOBJ_CONTAINER_UNLOCK(&aliasl
);
2961 /* Don't touch GK if nothing changed because URQ will drop all existing calls */
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
;
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;
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");
2990 ast_mutex_unlock(&h323_reload_lock
);
2995 static int h323_do_reload(void)
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");
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",
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
))
3024 ast_mutex_lock(&pvt
->lock
);
3025 if (pvt
->rtp
&& pvt
->options
.bridge
) {
3027 res
= AST_RTP_TRY_NATIVE
;
3029 ast_mutex_unlock(&pvt
->lock
);
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
)
3042 case AST_FORMAT_G723_1
:
3044 case AST_FORMAT_GSM
:
3046 case AST_FORMAT_ULAW
:
3048 case AST_FORMAT_ALAW
:
3050 case AST_FORMAT_G722
:
3052 case AST_FORMAT_ADPCM
:
3054 case AST_FORMAT_G729A
:
3056 case AST_FORMAT_SPEEX
:
3058 case AST_FORMAT_ILBC
:
3061 ast_log(LOG_NOTICE
, "Don't know how to deal with mode %d\n", cap
);
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
;
3078 mode
= convertcap(chan
->writeformat
);
3079 pvt
= (struct oh323_pvt
*) chan
->tech_pvt
;
3081 ast_log(LOG_ERROR
, "No Private Structure, this is bad\n");
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
);
3092 static struct ast_rtp_protocol oh323_rtp
= {
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)
3104 sched
= sched_context_create();
3106 ast_log(LOG_WARNING
, "Unable to create schedule context\n");
3107 return AST_MODULE_LOAD_FAILURE
;
3109 io
= io_context_create();
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);
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
);
3125 sched_context_destroy(sched
);
3127 ASTOBJ_CONTAINER_DESTROY(&userl
);
3128 ASTOBJ_CONTAINER_DESTROY(&peerl
);
3129 ASTOBJ_CONTAINER_DESTROY(&aliasl
);
3130 return AST_MODULE_LOAD_DECLINE
;
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
);
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
,
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
);
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 */
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 */
3217 ast_softhangup(p
->owner
, AST_SOFTHANGUP_APPUNLOAD
);
3222 ast_mutex_unlock(&iflock
);
3224 ast_log(LOG_WARNING
, "Unable to lock the interface list\n");
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
);
3238 ast_log(LOG_WARNING
, "Unable to lock the monitor\n");
3241 if (!ast_mutex_lock(&iflock
)) {
3242 /* destroy all the interfaces and free their memory */
3247 /* free associated memory */
3248 ast_mutex_destroy(&pl
->lock
);
3252 ast_mutex_unlock(&iflock
);
3254 ast_log(LOG_WARNING
, "Unable to lock the interface list\n");
3257 if (!gatekeeper_disable
)
3261 io_context_destroy(io
);
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
);
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
,