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
));
1494 ASTOBJ_UNMARK(peer
);
1499 static struct oh323_peer
*realtime_peer(const char *peername
, struct sockaddr_in
*sin
)
1501 struct oh323_peer
*peer
;
1502 struct ast_variable
*var
;
1503 struct ast_variable
*tmp
;
1506 /* First check on peer name */
1508 var
= ast_load_realtime("h323", "name", peername
, addr
= NULL
);
1509 else if (sin
) /* Then check on IP address for dynamic peers */
1510 var
= ast_load_realtime("h323", "host", addr
= ast_inet_ntoa(sin
->sin_addr
), NULL
);
1517 for (tmp
= var
; tmp
; tmp
= tmp
->next
) {
1518 /* If this is type=user, then skip this object. */
1519 if (!strcasecmp(tmp
->name
, "type") &&
1520 !(!strcasecmp(tmp
->value
, "peer") || !strcasecmp(tmp
->value
, "friend"))) {
1521 ast_variables_destroy(var
);
1523 } else if (!peername
&& !strcasecmp(tmp
->name
, "name")) {
1524 peername
= tmp
->value
;
1528 if (!peername
) { /* Did not find peer in realtime */
1529 ast_log(LOG_WARNING
, "Cannot determine peer name for IP address %s\n", addr
);
1530 ast_variables_destroy(var
);
1534 /* Peer found in realtime, now build it in memory */
1535 peer
= build_peer(peername
, var
, NULL
, 1);
1537 ast_variables_destroy(var
);
1542 static int oh323_addrcmp_str(struct in_addr inaddr
, char *addr
)
1544 return strcmp(ast_inet_ntoa(inaddr
), addr
);
1547 static struct oh323_user
*find_user(const call_details_t
*cd
, int realtime
)
1549 struct oh323_user
*u
;
1552 u
= ASTOBJ_CONTAINER_FIND(&userl
, cd
->call_source_aliases
);
1554 u
= ASTOBJ_CONTAINER_FIND_FULL(&userl
, cd
->sourceIp
, addr
.sin_addr
, 0, 0, oh323_addrcmp_str
);
1557 u
= realtime_user(cd
);
1559 if (!u
&& h323debug
)
1560 ast_log(LOG_DEBUG
, "Could not find user by name %s or address %s\n", cd
->call_source_aliases
, cd
->sourceIp
);
1565 static int oh323_addrcmp(struct sockaddr_in addr
, struct sockaddr_in
*sin
)
1572 res
= inaddrcmp(&addr
, sin
);
1577 static struct oh323_peer
*find_peer(const char *peer
, struct sockaddr_in
*sin
, int realtime
)
1579 struct oh323_peer
*p
;
1582 p
= ASTOBJ_CONTAINER_FIND(&peerl
, peer
);
1584 p
= ASTOBJ_CONTAINER_FIND_FULL(&peerl
, sin
, addr
, 0, 0, oh323_addrcmp
);
1587 p
= realtime_peer(peer
, sin
);
1589 if (!p
&& h323debug
)
1590 ast_log(LOG_DEBUG
, "Could not find peer by name %s or address %s\n", (peer
? peer
: "<NONE>"), (sin
? ast_inet_ntoa(sin
->sin_addr
) : "<NONE>"));
1595 static int create_addr(struct oh323_pvt
*pvt
, char *opeer
)
1598 struct ast_hostent ahp
;
1599 struct oh323_peer
*p
;
1604 char peer
[256] = "";
1606 ast_copy_string(peer
, opeer
, sizeof(peer
));
1607 port
= strchr(peer
, ':');
1612 pvt
->sa
.sin_family
= AF_INET
;
1613 p
= find_peer(peer
, NULL
, 1);
1616 memcpy(&pvt
->options
, &p
->options
, sizeof(pvt
->options
));
1617 pvt
->jointcapability
= pvt
->options
.capability
;
1618 if (pvt
->options
.dtmfmode
) {
1619 if (pvt
->options
.dtmfmode
& H323_DTMF_RFC2833
) {
1620 pvt
->nonCodecCapability
|= AST_RTP_DTMF
;
1622 pvt
->nonCodecCapability
&= ~AST_RTP_DTMF
;
1625 if (p
->addr
.sin_addr
.s_addr
) {
1626 pvt
->sa
.sin_addr
= p
->addr
.sin_addr
;
1627 pvt
->sa
.sin_port
= p
->addr
.sin_port
;
1629 ASTOBJ_UNREF(p
, oh323_destroy_peer
);
1634 portno
= atoi(port
);
1636 portno
= h323_signalling_port
;
1638 hp
= ast_gethostbyname(hostn
, &ahp
);
1640 memcpy(&pvt
->sa
.sin_addr
, hp
->h_addr
, sizeof(pvt
->sa
.sin_addr
));
1641 pvt
->sa
.sin_port
= htons(portno
);
1642 /* Look peer by address */
1643 p
= find_peer(NULL
, &pvt
->sa
, 1);
1644 memcpy(&pvt
->options
, (p
? &p
->options
: &global_options
), sizeof(pvt
->options
));
1645 pvt
->jointcapability
= pvt
->options
.capability
;
1647 ASTOBJ_UNREF(p
, oh323_destroy_peer
);
1649 if (pvt
->options
.dtmfmode
) {
1650 if (pvt
->options
.dtmfmode
& H323_DTMF_RFC2833
) {
1651 pvt
->nonCodecCapability
|= AST_RTP_DTMF
;
1653 pvt
->nonCodecCapability
&= ~AST_RTP_DTMF
;
1658 ast_log(LOG_WARNING
, "No such host: %s\n", peer
);
1661 } else if (!found
) {
1667 static struct ast_channel
*oh323_request(const char *type
, int format
, void *data
, int *cause
)
1670 struct oh323_pvt
*pvt
;
1671 struct ast_channel
*tmpc
= NULL
;
1672 char *dest
= (char *)data
;
1674 char *h323id
= NULL
;
1675 char tmp
[256], tmp1
[256];
1678 ast_log(LOG_DEBUG
, "type=%s, format=%d, data=%s.\n", type
, format
, (char *)data
);
1680 pvt
= oh323_alloc(0);
1682 ast_log(LOG_WARNING
, "Unable to build pvt data for '%s'\n", (char *)data
);
1686 format
&= ((AST_FORMAT_MAX_AUDIO
<< 1) - 1);
1688 ast_log(LOG_NOTICE
, "Asked to get a channel of unsupported format '%d'\n", format
);
1691 *cause
= AST_CAUSE_INCOMPATIBLE_DESTINATION
;
1694 ast_copy_string(tmp
, dest
, sizeof(tmp
));
1695 host
= strchr(tmp
, '@');
1701 ext
= strrchr(tmp
, '/');
1706 strtok_r(host
, "/", &(h323id
));
1707 if (!ast_strlen_zero(h323id
)) {
1708 h323_set_id(h323id
);
1711 ast_copy_string(pvt
->exten
, ext
, sizeof(pvt
->exten
));
1714 ast_log(LOG_DEBUG
, "Extension: %s Host: %s\n", pvt
->exten
, host
);
1716 if (gatekeeper_disable
) {
1717 if (create_addr(pvt
, host
)) {
1720 *cause
= AST_CAUSE_DESTINATION_OUT_OF_ORDER
;
1725 memcpy(&pvt
->options
, &global_options
, sizeof(pvt
->options
));
1726 pvt
->jointcapability
= pvt
->options
.capability
;
1727 if (pvt
->options
.dtmfmode
) {
1728 if (pvt
->options
.dtmfmode
& H323_DTMF_RFC2833
) {
1729 pvt
->nonCodecCapability
|= AST_RTP_DTMF
;
1731 pvt
->nonCodecCapability
&= ~AST_RTP_DTMF
;
1736 ast_mutex_lock(&caplock
);
1737 /* Generate unique channel identifier */
1738 snprintf(tmp1
, sizeof(tmp1
)-1, "%s-%u", host
, ++unique
);
1739 tmp1
[sizeof(tmp1
)-1] = '\0';
1740 ast_mutex_unlock(&caplock
);
1742 ast_mutex_lock(&pvt
->lock
);
1743 tmpc
= __oh323_new(pvt
, AST_STATE_DOWN
, tmp1
);
1744 ast_mutex_unlock(&pvt
->lock
);
1748 *cause
= AST_CAUSE_NORMAL_TEMPORARY_FAILURE
;
1750 ast_update_use_count();
1755 /** Find a call by alias */
1756 static struct oh323_alias
*find_alias(const char *source_aliases
, int realtime
)
1758 struct oh323_alias
*a
;
1760 a
= ASTOBJ_CONTAINER_FIND(&aliasl
, source_aliases
);
1763 a
= realtime_alias(source_aliases
);
1769 * Callback for sending digits from H.323 up to asterisk
1772 static int receive_digit(unsigned call_reference
, char digit
, const char *token
, int duration
)
1774 struct oh323_pvt
*pvt
;
1777 pvt
= find_call_locked(call_reference
, token
);
1779 ast_log(LOG_ERROR
, "Received digit '%c' (%u ms) for call %s without private structure\n", digit
, duration
, token
);
1783 ast_log(LOG_DTMF
, "Received %s digit '%c' (%u ms) for call %s\n", (digit
== ' ' ? "update for" : "new"), (digit
== ' ' ? pvt
->curDTMF
: digit
), duration
, token
);
1785 if (pvt
->owner
&& !ast_channel_trylock(pvt
->owner
)) {
1787 res
= ast_queue_control(pvt
->owner
, AST_CONTROL_FLASH
);
1789 struct ast_frame f
= {
1790 .frametype
= AST_FRAME_DTMF_END
,
1792 .samples
= duration
* 8,
1794 .src
= "SEND_DIGIT",
1796 if (digit
== ' ') { /* signalUpdate message */
1797 f
.subclass
= pvt
->curDTMF
;
1798 AST_SCHED_DEL(sched
, pvt
->DTMFsched
);
1799 } else { /* Regular input or signal message */
1800 if (pvt
->DTMFsched
>= 0) {
1801 /* We still don't send DTMF END from previous event, send it now */
1802 AST_SCHED_DEL(sched
, pvt
->DTMFsched
);
1803 f
.subclass
= pvt
->curDTMF
;
1804 f
.samples
= f
.len
= 0;
1805 ast_queue_frame(pvt
->owner
, &f
);
1806 /* Restore values */
1808 f
.samples
= duration
* 8;
1811 if (duration
) { /* This is a signal, signalUpdate follows */
1812 f
.frametype
= AST_FRAME_DTMF_BEGIN
;
1813 pvt
->DTMFsched
= ast_sched_add(sched
, duration
, oh323_simulate_dtmf_end
, pvt
);
1815 ast_log(LOG_DTMF
, "Scheduled DTMF END simulation for %d ms, id=%d\n", duration
, pvt
->DTMFsched
);
1817 pvt
->curDTMF
= digit
;
1819 res
= ast_queue_frame(pvt
->owner
, &f
);
1821 ast_channel_unlock(pvt
->owner
);
1824 pvt
->newcontrol
= AST_CONTROL_FLASH
;
1826 pvt
->newduration
= duration
;
1827 pvt
->newdigit
= digit
;
1831 ast_mutex_unlock(&pvt
->lock
);
1836 * Callback function used to inform the H.323 stack of the local rtp ip/port details
1838 * Returns the local RTP information
1840 static struct rtp_info
*external_rtp_create(unsigned call_reference
, const char * token
)
1842 struct oh323_pvt
*pvt
;
1843 struct sockaddr_in us
;
1844 struct rtp_info
*info
;
1846 info
= (struct rtp_info
*)malloc(sizeof(struct rtp_info
));
1848 ast_log(LOG_ERROR
, "Unable to allocated info structure, this is very bad\n");
1851 pvt
= find_call_locked(call_reference
, token
);
1854 ast_log(LOG_ERROR
, "Unable to find call %s(%d)\n", token
, call_reference
);
1858 __oh323_rtp_create(pvt
);
1860 ast_mutex_unlock(&pvt
->lock
);
1862 ast_log(LOG_ERROR
, "No RTP stream is available for call %s (%d)", token
, call_reference
);
1865 /* figure out our local RTP port and tell the H.323 stack about it */
1866 ast_rtp_get_us(pvt
->rtp
, &us
);
1867 ast_mutex_unlock(&pvt
->lock
);
1869 ast_copy_string(info
->addr
, ast_inet_ntoa(us
.sin_addr
), sizeof(info
->addr
));
1870 info
->port
= ntohs(us
.sin_port
);
1872 ast_log(LOG_DEBUG
, "Sending RTP 'US' %s:%d\n", info
->addr
, info
->port
);
1877 * Definition taken from rtp.c for rtpPayloadType because we need it here.
1879 struct rtpPayloadType
{
1880 int isAstFormat
; /* whether the following code is an AST_FORMAT */
1885 * Call-back function passing remote ip/port information from H.323 to asterisk
1889 static void setup_rtp_connection(unsigned call_reference
, const char *remoteIp
, int remotePort
, const char *token
, int pt
)
1891 struct oh323_pvt
*pvt
;
1892 struct sockaddr_in them
;
1893 struct rtpPayloadType rtptype
;
1894 int nativeformats_changed
;
1895 enum { NEED_NONE
, NEED_HOLD
, NEED_UNHOLD
} rtp_change
= NEED_NONE
;
1898 ast_log(LOG_DEBUG
, "Setting up RTP connection for %s\n", token
);
1900 /* Find the call or allocate a private structure if call not found */
1901 pvt
= find_call_locked(call_reference
, token
);
1903 ast_log(LOG_ERROR
, "Something is wrong: rtp\n");
1906 if (pvt
->alreadygone
) {
1907 ast_mutex_unlock(&pvt
->lock
);
1912 __oh323_rtp_create(pvt
);
1914 them
.sin_family
= AF_INET
;
1915 /* only works for IPv4 */
1916 them
.sin_addr
.s_addr
= inet_addr(remoteIp
);
1917 them
.sin_port
= htons(remotePort
);
1919 if (them
.sin_addr
.s_addr
) {
1920 ast_rtp_set_peer(pvt
->rtp
, &them
);
1921 if (pvt
->recvonly
) {
1923 rtp_change
= NEED_UNHOLD
;
1926 ast_rtp_stop(pvt
->rtp
);
1927 if (!pvt
->recvonly
) {
1929 rtp_change
= NEED_HOLD
;
1933 /* Change native format to reflect information taken from OLC/OLCAck */
1934 nativeformats_changed
= 0;
1935 if (pt
!= 128 && pvt
->rtp
) { /* Payload type is invalid, so try to use previously decided */
1936 rtptype
= ast_rtp_lookup_pt(pvt
->rtp
, pt
);
1938 ast_log(LOG_DEBUG
, "Native format is set to %d from %d by RTP payload type %d\n", rtptype
.code
, pvt
->nativeformats
, pt
);
1939 if (pvt
->nativeformats
!= rtptype
.code
) {
1940 pvt
->nativeformats
= rtptype
.code
;
1941 nativeformats_changed
= 1;
1943 } else if (h323debug
)
1944 ast_log(LOG_NOTICE
, "Payload type is unknown, formats isn't changed\n");
1946 /* Don't try to lock the channel if nothing changed */
1947 if (nativeformats_changed
|| pvt
->options
.progress_audio
|| (rtp_change
!= NEED_NONE
)) {
1948 if (pvt
->owner
&& !ast_channel_trylock(pvt
->owner
)) {
1949 /* Re-build translation path only if native format(s) has been changed */
1950 if (pvt
->owner
->nativeformats
!= pvt
->nativeformats
) {
1952 ast_log(LOG_DEBUG
, "Native format changed to %d from %d, read format is %d, write format is %d\n", pvt
->nativeformats
, pvt
->owner
->nativeformats
, pvt
->owner
->readformat
, pvt
->owner
->writeformat
);
1953 pvt
->owner
->nativeformats
= pvt
->nativeformats
;
1954 ast_set_read_format(pvt
->owner
, pvt
->owner
->readformat
);
1955 ast_set_write_format(pvt
->owner
, pvt
->owner
->writeformat
);
1957 if (pvt
->options
.progress_audio
)
1958 ast_queue_control(pvt
->owner
, AST_CONTROL_PROGRESS
);
1959 switch (rtp_change
) {
1961 ast_queue_control(pvt
->owner
, AST_CONTROL_HOLD
);
1964 ast_queue_control(pvt
->owner
, AST_CONTROL_UNHOLD
);
1969 ast_channel_unlock(pvt
->owner
);
1972 if (pvt
->options
.progress_audio
)
1973 pvt
->newcontrol
= AST_CONTROL_PROGRESS
;
1974 else if (rtp_change
== NEED_HOLD
)
1975 pvt
->newcontrol
= AST_CONTROL_HOLD
;
1976 else if (rtp_change
== NEED_UNHOLD
)
1977 pvt
->newcontrol
= AST_CONTROL_UNHOLD
;
1979 ast_log(LOG_DEBUG
, "RTP connection preparation for %s is pending...\n", token
);
1982 ast_mutex_unlock(&pvt
->lock
);
1985 ast_log(LOG_DEBUG
, "RTP connection prepared for %s\n", token
);
1991 * Call-back function to signal asterisk that the channel has been answered
1994 static void connection_made(unsigned call_reference
, const char *token
)
1996 struct oh323_pvt
*pvt
;
1999 ast_log(LOG_DEBUG
, "Call %s answered\n", token
);
2001 pvt
= find_call_locked(call_reference
, token
);
2003 ast_log(LOG_ERROR
, "Something is wrong: connection\n");
2007 /* Inform asterisk about remote party connected only on outgoing calls */
2008 if (!pvt
->outgoing
) {
2009 ast_mutex_unlock(&pvt
->lock
);
2012 /* Do not send ANSWER message more than once */
2013 if (!pvt
->connection_established
) {
2014 pvt
->connection_established
= 1;
2015 update_state(pvt
, -1, AST_CONTROL_ANSWER
);
2017 ast_mutex_unlock(&pvt
->lock
);
2021 static int progress(unsigned call_reference
, const char *token
, int inband
)
2023 struct oh323_pvt
*pvt
;
2026 ast_log(LOG_DEBUG
, "Received ALERT/PROGRESS message for %s tones\n", (inband
? "inband" : "self-generated"));
2028 pvt
= find_call_locked(call_reference
, token
);
2030 ast_log(LOG_ERROR
, "Private structure not found in progress.\n");
2034 ast_mutex_unlock(&pvt
->lock
);
2035 ast_log(LOG_ERROR
, "No Asterisk channel associated with private structure.\n");
2038 update_state(pvt
, -1, (inband
? AST_CONTROL_PROGRESS
: AST_CONTROL_RINGING
));
2039 ast_mutex_unlock(&pvt
->lock
);
2045 * Call-back function for incoming calls
2047 * Returns 1 on success
2049 static call_options_t
*setup_incoming_call(call_details_t
*cd
)
2051 struct oh323_pvt
*pvt
;
2052 struct oh323_user
*user
= NULL
;
2053 struct oh323_alias
*alias
= NULL
;
2056 ast_log(LOG_DEBUG
, "Setting up incoming call for %s\n", cd
->call_token
);
2058 /* allocate the call*/
2059 pvt
= oh323_alloc(cd
->call_reference
);
2062 ast_log(LOG_ERROR
, "Unable to allocate private structure, this is bad.\n");
2063 cleanup_call_details(cd
);
2067 /* Populate the call details in the private structure */
2068 memcpy(&pvt
->cd
, cd
, sizeof(pvt
->cd
));
2069 memcpy(&pvt
->options
, &global_options
, sizeof(pvt
->options
));
2070 pvt
->jointcapability
= pvt
->options
.capability
;
2073 ast_verbose(VERBOSE_PREFIX_3
"Setting up Call\n");
2074 ast_verbose(VERBOSE_PREFIX_3
" \tCall token: [%s]\n", pvt
->cd
.call_token
);
2075 ast_verbose(VERBOSE_PREFIX_3
" \tCalling party name: [%s]\n", pvt
->cd
.call_source_name
);
2076 ast_verbose(VERBOSE_PREFIX_3
" \tCalling party number: [%s]\n", pvt
->cd
.call_source_e164
);
2077 ast_verbose(VERBOSE_PREFIX_3
" \tCalled party name: [%s]\n", pvt
->cd
.call_dest_alias
);
2078 ast_verbose(VERBOSE_PREFIX_3
" \tCalled party number: [%s]\n", pvt
->cd
.call_dest_e164
);
2079 if (pvt
->cd
.redirect_reason
>= 0)
2080 ast_verbose(VERBOSE_PREFIX_3
" \tRedirecting party number: [%s] (reason %d)\n", pvt
->cd
.redirect_number
, pvt
->cd
.redirect_reason
);
2081 ast_verbose(VERBOSE_PREFIX_3
" \tCalling party IP: [%s]\n", pvt
->cd
.sourceIp
);
2084 /* Decide if we are allowing Gatekeeper routed calls*/
2085 if ((!strcasecmp(cd
->sourceIp
, gatekeeper
)) && (gkroute
== -1) && !gatekeeper_disable
) {
2086 if (!ast_strlen_zero(cd
->call_dest_e164
)) {
2087 ast_copy_string(pvt
->exten
, cd
->call_dest_e164
, sizeof(pvt
->exten
));
2088 ast_copy_string(pvt
->context
, default_context
, sizeof(pvt
->context
));
2090 alias
= find_alias(cd
->call_dest_alias
, 1);
2092 ast_log(LOG_ERROR
, "Call for %s rejected, alias not found\n", cd
->call_dest_alias
);
2096 ast_copy_string(pvt
->exten
, alias
->name
, sizeof(pvt
->exten
));
2097 ast_copy_string(pvt
->context
, alias
->context
, sizeof(pvt
->context
));
2100 /* Either this call is not from the Gatekeeper
2101 or we are not allowing gk routed calls */
2102 user
= find_user(cd
, 1);
2104 if (!acceptAnonymous
) {
2105 ast_log(LOG_NOTICE
, "Anonymous call from '%s@%s' rejected\n", pvt
->cd
.call_source_aliases
, pvt
->cd
.sourceIp
);
2109 if (ast_strlen_zero(default_context
)) {
2110 ast_log(LOG_ERROR
, "Call from '%s@%s' rejected due to no default context\n", pvt
->cd
.call_source_aliases
, pvt
->cd
.sourceIp
);
2114 ast_copy_string(pvt
->context
, default_context
, sizeof(pvt
->context
));
2115 if (!ast_strlen_zero(pvt
->cd
.call_dest_e164
)) {
2116 ast_copy_string(pvt
->exten
, cd
->call_dest_e164
, sizeof(pvt
->exten
));
2118 ast_copy_string(pvt
->exten
, cd
->call_dest_alias
, sizeof(pvt
->exten
));
2121 ast_log(LOG_DEBUG
, "Sending %s@%s to context [%s] extension %s\n", cd
->call_source_aliases
, cd
->sourceIp
, pvt
->context
, pvt
->exten
);
2124 if (strcasecmp(cd
->sourceIp
, ast_inet_ntoa(user
->addr
.sin_addr
))) {
2125 if (ast_strlen_zero(user
->context
)) {
2126 if (ast_strlen_zero(default_context
)) {
2127 ast_log(LOG_ERROR
, "Call from '%s' rejected due to non-matching IP address (%s) and no default context\n", user
->name
, cd
->sourceIp
);
2129 ASTOBJ_UNREF(user
, oh323_destroy_user
);
2132 ast_copy_string(pvt
->context
, default_context
, sizeof(pvt
->context
));
2134 ast_copy_string(pvt
->context
, user
->context
, sizeof(pvt
->context
));
2136 pvt
->exten
[0] = 'i';
2137 pvt
->exten
[1] = '\0';
2138 ast_log(LOG_ERROR
, "Call from '%s' rejected due to non-matching IP address (%s)s\n", user
->name
, cd
->sourceIp
);
2140 ASTOBJ_UNREF(user
, oh323_destroy_user
);
2141 return NULL
; /* XXX: Hmmm... Why to setup context if we drop connection immediately??? */
2144 ast_copy_string(pvt
->context
, user
->context
, sizeof(pvt
->context
));
2145 memcpy(&pvt
->options
, &user
->options
, sizeof(pvt
->options
));
2146 pvt
->jointcapability
= pvt
->options
.capability
;
2147 if (!ast_strlen_zero(pvt
->cd
.call_dest_e164
)) {
2148 ast_copy_string(pvt
->exten
, cd
->call_dest_e164
, sizeof(pvt
->exten
));
2150 ast_copy_string(pvt
->exten
, cd
->call_dest_alias
, sizeof(pvt
->exten
));
2152 if (!ast_strlen_zero(user
->accountcode
)) {
2153 ast_copy_string(pvt
->accountcode
, user
->accountcode
, sizeof(pvt
->accountcode
));
2155 if (user
->amaflags
) {
2156 pvt
->amaflags
= user
->amaflags
;
2158 ASTOBJ_UNREF(user
, oh323_destroy_user
);
2161 return &pvt
->options
;
2165 * Call-back function to start PBX when OpenH323 ready to serve incoming call
2167 * Returns 1 on success
2169 static int answer_call(unsigned call_reference
, const char *token
)
2171 struct oh323_pvt
*pvt
;
2172 struct ast_channel
*c
= NULL
;
2173 enum {ext_original
, ext_s
, ext_i
, ext_notexists
} try_exten
;
2174 char tmp_exten
[sizeof(pvt
->exten
)];
2177 ast_log(LOG_DEBUG
, "Preparing Asterisk to answer for %s\n", token
);
2179 /* Find the call or allocate a private structure if call not found */
2180 pvt
= find_call_locked(call_reference
, token
);
2182 ast_log(LOG_ERROR
, "Something is wrong: answer_call\n");
2185 /* Check if requested extension@context pair exists in the dialplan */
2186 ast_copy_string(tmp_exten
, pvt
->exten
, sizeof(tmp_exten
));
2188 /* Try to find best extension in specified context */
2189 if ((tmp_exten
[0] != '\0') && (tmp_exten
[1] == '\0')) {
2190 if (tmp_exten
[0] == 's')
2192 else if (tmp_exten
[0] == 'i')
2195 try_exten
= ext_original
;
2197 try_exten
= ext_original
;
2199 if (ast_exists_extension(NULL
, pvt
->context
, tmp_exten
, 1, NULL
))
2201 switch (try_exten
) {
2204 tmp_exten
[1] = '\0';
2212 try_exten
= ext_notexists
;
2217 } while (try_exten
!= ext_notexists
);
2219 /* Drop the call if we don't have <exten>, s and i extensions */
2220 if (try_exten
== ext_notexists
) {
2221 ast_log(LOG_NOTICE
, "Dropping call because extensions '%s', 's' and 'i' doesn't exists in context [%s]\n", pvt
->exten
, pvt
->context
);
2222 ast_mutex_unlock(&pvt
->lock
);
2223 h323_clear_call(token
, AST_CAUSE_UNALLOCATED
);
2225 } else if ((try_exten
!= ext_original
) && (strcmp(pvt
->exten
, tmp_exten
) != 0)) {
2227 ast_log(LOG_DEBUG
, "Going to extension %s@%s because %s@%s isn't exists\n", tmp_exten
, pvt
->context
, pvt
->exten
, pvt
->context
);
2228 ast_copy_string(pvt
->exten
, tmp_exten
, sizeof(pvt
->exten
));
2231 /* allocate a channel and tell asterisk about it */
2232 c
= __oh323_new(pvt
, AST_STATE_RINGING
, pvt
->cd
.call_token
);
2234 /* And release when done */
2235 ast_mutex_unlock(&pvt
->lock
);
2237 ast_log(LOG_ERROR
, "Couldn't create channel. This is bad\n");
2244 * Call-back function to establish an outgoing H.323 call
2246 * Returns 1 on success
2248 static int setup_outgoing_call(call_details_t
*cd
)
2250 /* Use argument here or free it immediately */
2251 cleanup_call_details(cd
);
2257 * Call-back function to signal asterisk that the channel is ringing
2260 static void chan_ringing(unsigned call_reference
, const char *token
)
2262 struct oh323_pvt
*pvt
;
2265 ast_log(LOG_DEBUG
, "Ringing on %s\n", token
);
2267 pvt
= find_call_locked(call_reference
, token
);
2269 ast_log(LOG_ERROR
, "Something is wrong: ringing\n");
2273 ast_mutex_unlock(&pvt
->lock
);
2274 ast_log(LOG_ERROR
, "Channel has no owner\n");
2277 update_state(pvt
, AST_STATE_RINGING
, AST_CONTROL_RINGING
);
2278 ast_mutex_unlock(&pvt
->lock
);
2283 * Call-back function to cleanup communication
2286 static void cleanup_connection(unsigned call_reference
, const char *call_token
)
2288 struct oh323_pvt
*pvt
;
2291 ast_log(LOG_DEBUG
, "Cleaning connection to %s\n", call_token
);
2294 pvt
= find_call_locked(call_reference
, call_token
);
2297 ast_log(LOG_DEBUG
, "No connection for %s\n", call_token
);
2300 if (!pvt
->owner
|| !ast_channel_trylock(pvt
->owner
))
2303 #ifdef DEBUG_THREADS
2304 ast_log(LOG_NOTICE
, "Avoiding H.323 destory deadlock on %s, locked at %ld/%d by %s (%s:%d)\n", call_token
, pvt
->owner
->lock
.thread
[0], pvt
->owner
->lock
.reentrancy
, pvt
->owner
->lock
.func
[0], pvt
->owner
->lock
.file
[0], pvt
->owner
->lock
.lineno
[0]);
2306 ast_log(LOG_NOTICE
, "Avoiding H.323 destory deadlock on %s\n", call_token
);
2309 ast_mutex_unlock(&pvt
->lock
);
2313 /* Immediately stop RTP */
2314 ast_rtp_destroy(pvt
->rtp
);
2317 /* Free dsp used for in-band DTMF detection */
2319 ast_dsp_free(pvt
->vad
);
2322 cleanup_call_details(&pvt
->cd
);
2323 pvt
->alreadygone
= 1;
2326 pvt
->owner
->_softhangup
|= AST_SOFTHANGUP_DEV
;
2327 ast_queue_hangup(pvt
->owner
);
2328 ast_channel_unlock(pvt
->owner
);
2330 ast_mutex_unlock(&pvt
->lock
);
2332 ast_log(LOG_DEBUG
, "Connection to %s cleaned\n", call_token
);
2336 static void hangup_connection(unsigned int call_reference
, const char *token
, int cause
)
2338 struct oh323_pvt
*pvt
;
2341 ast_log(LOG_DEBUG
, "Hanging up connection to %s with cause %d\n", token
, cause
);
2344 pvt
= find_call_locked(call_reference
, token
);
2347 ast_log(LOG_DEBUG
, "Connection to %s already cleared\n", token
);
2351 if (pvt
->owner
&& !ast_channel_trylock(pvt
->owner
)) {
2352 pvt
->owner
->_softhangup
|= AST_SOFTHANGUP_DEV
;
2353 pvt
->owner
->hangupcause
= pvt
->hangupcause
= cause
;
2354 ast_queue_hangup(pvt
->owner
);
2355 ast_channel_unlock(pvt
->owner
);
2358 pvt
->needhangup
= 1;
2359 pvt
->hangupcause
= cause
;
2361 ast_log(LOG_DEBUG
, "Hangup for %s is pending\n", token
);
2363 ast_mutex_unlock(&pvt
->lock
);
2366 static void set_dtmf_payload(unsigned call_reference
, const char *token
, int payload
)
2368 struct oh323_pvt
*pvt
;
2371 ast_log(LOG_DEBUG
, "Setting DTMF payload to %d on %s\n", payload
, token
);
2373 pvt
= find_call_locked(call_reference
, token
);
2378 ast_rtp_set_rtpmap_type(pvt
->rtp
, payload
, "audio", "telephone-event", 0);
2380 pvt
->dtmf_pt
= payload
;
2381 ast_mutex_unlock(&pvt
->lock
);
2383 ast_log(LOG_DEBUG
, "DTMF payload on %s set to %d\n", token
, payload
);
2386 static void set_peer_capabilities(unsigned call_reference
, const char *token
, int capabilities
, struct ast_codec_pref
*prefs
)
2388 struct oh323_pvt
*pvt
;
2391 ast_log(LOG_DEBUG
, "Got remote capabilities from connection %s\n", token
);
2393 pvt
= find_call_locked(call_reference
, token
);
2396 pvt
->peercapability
= capabilities
;
2397 pvt
->jointcapability
= pvt
->options
.capability
& capabilities
;
2399 memcpy(&pvt
->peer_prefs
, prefs
, sizeof(pvt
->peer_prefs
));
2402 for (i
= 0; i
< 32; ++i
) {
2403 if (!prefs
->order
[i
])
2405 ast_log(LOG_DEBUG
, "prefs[%d]=%s:%d\n", i
, (prefs
->order
[i
] ? ast_getformatname(1 << (prefs
->order
[i
]-1)) : "<none>"), prefs
->framing
[i
]);
2409 ast_rtp_codec_setpref(pvt
->rtp
, &pvt
->peer_prefs
);
2411 ast_mutex_unlock(&pvt
->lock
);
2414 static void set_local_capabilities(unsigned call_reference
, const char *token
)
2416 struct oh323_pvt
*pvt
;
2417 int capability
, dtmfmode
, pref_codec
;
2418 struct ast_codec_pref prefs
;
2421 ast_log(LOG_DEBUG
, "Setting capabilities for connection %s\n", token
);
2423 pvt
= find_call_locked(call_reference
, token
);
2426 capability
= (pvt
->jointcapability
) ? pvt
->jointcapability
: pvt
->options
.capability
;
2427 dtmfmode
= pvt
->options
.dtmfmode
;
2428 prefs
= pvt
->options
.prefs
;
2429 pref_codec
= pvt
->pref_codec
;
2430 ast_mutex_unlock(&pvt
->lock
);
2431 h323_set_capabilities(token
, capability
, dtmfmode
, &prefs
, pref_codec
);
2434 ast_log(LOG_DEBUG
, "Capabilities for connection %s is set\n", token
);
2437 static void *do_monitor(void *data
)
2441 struct oh323_pvt
*oh323
= NULL
;
2444 /* Check for a reload request */
2445 ast_mutex_lock(&h323_reload_lock
);
2446 reloading
= h323_reloading
;
2448 ast_mutex_unlock(&h323_reload_lock
);
2450 if (option_verbose
> 0) {
2451 ast_verbose(VERBOSE_PREFIX_1
"Reloading H.323\n");
2455 /* Check for interfaces needing to be killed */
2456 if (!ast_mutex_trylock(&iflock
)) {
2459 for (oh323
= iflist
; oh323
; oh323
= oh323
->next
) {
2460 if (!ast_mutex_trylock(&oh323
->lock
)) {
2461 if (oh323
->needdestroy
) {
2462 __oh323_destroy(oh323
);
2465 ast_mutex_unlock(&oh323
->lock
);
2468 } while (/*oh323*/ 0);
2473 if (!ast_mutex_trylock(&oh323
->lock
)) {
2474 if (oh323
->needdestroy
) {
2475 __oh323_destroy(oh323
);
2478 ast_mutex_unlock(&oh323
->lock
);
2479 oh323
= oh323
->next
;
2483 ast_mutex_unlock(&iflock
);
2485 oh323
= (struct oh323_pvt
*)1; /* Force fast loop */
2486 pthread_testcancel();
2487 /* Wait for sched or io */
2488 res
= ast_sched_wait(sched
);
2489 if ((res
< 0) || (res
> 1000)) {
2492 /* Do not wait if some channel(s) is destroyed, probably, more available too */
2495 res
= ast_io_wait(io
, res
);
2496 pthread_testcancel();
2497 ast_mutex_lock(&monlock
);
2499 ast_sched_runq(sched
);
2501 ast_mutex_unlock(&monlock
);
2507 static int restart_monitor(void)
2509 pthread_attr_t attr
;
2510 /* If we're supposed to be stopped -- stay stopped */
2511 if (ast_mutex_lock(&monlock
)) {
2512 ast_log(LOG_WARNING
, "Unable to lock monitor\n");
2515 if (monitor_thread
== AST_PTHREADT_STOP
) {
2516 ast_mutex_unlock(&monlock
);
2519 if (monitor_thread
== pthread_self()) {
2520 ast_mutex_unlock(&monlock
);
2521 ast_log(LOG_WARNING
, "Cannot kill myself\n");
2524 if (monitor_thread
&& (monitor_thread
!= AST_PTHREADT_NULL
)) {
2525 /* Wake up the thread */
2526 pthread_kill(monitor_thread
, SIGURG
);
2528 pthread_attr_init(&attr
);
2529 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
2530 /* Start a new monitor */
2531 if (ast_pthread_create_background(&monitor_thread
, &attr
, do_monitor
, NULL
) < 0) {
2532 monitor_thread
= AST_PTHREADT_NULL
;
2533 ast_mutex_unlock(&monlock
);
2534 ast_log(LOG_ERROR
, "Unable to start monitor thread.\n");
2535 pthread_attr_destroy(&attr
);
2538 pthread_attr_destroy(&attr
);
2540 ast_mutex_unlock(&monlock
);
2544 static int h323_do_trace(int fd
, int argc
, char *argv
[])
2547 return RESULT_SHOWUSAGE
;
2549 h323_debug(1, atoi(argv
[3]));
2550 ast_cli(fd
, "H.323 trace set to level %s\n", argv
[2]);
2551 return RESULT_SUCCESS
;
2554 static int h323_no_trace(int fd
, int argc
, char *argv
[])
2556 if (argc
< 3 || argc
> 4) {
2557 return RESULT_SHOWUSAGE
;
2560 ast_cli(fd
, "H.323 trace disabled\n");
2561 return RESULT_SUCCESS
;
2564 static int h323_do_debug(int fd
, int argc
, char *argv
[])
2566 if (argc
< 2 || argc
> 3) {
2567 return RESULT_SHOWUSAGE
;
2570 ast_cli(fd
, "H.323 debug enabled\n");
2571 return RESULT_SUCCESS
;
2574 static int h323_no_debug(int fd
, int argc
, char *argv
[])
2576 if (argc
< 3 || argc
> 4) {
2577 return RESULT_SHOWUSAGE
;
2580 ast_cli(fd
, "H.323 debug disabled\n");
2581 return RESULT_SUCCESS
;
2584 static int h323_gk_cycle(int fd
, int argc
, char *argv
[])
2587 return RESULT_SHOWUSAGE
;
2591 /* Possibly register with a GK */
2592 if (!gatekeeper_disable
) {
2593 if (h323_set_gk(gatekeeper_discover
, gatekeeper
, secret
)) {
2594 ast_log(LOG_ERROR
, "Gatekeeper registration failed.\n");
2597 return RESULT_SUCCESS
;
2600 static int h323_ep_hangup(int fd
, int argc
, char *argv
[])
2603 return RESULT_SHOWUSAGE
;
2605 if (h323_soft_hangup(argv
[2])) {
2606 ast_verbose(VERBOSE_PREFIX_3
"Hangup succeeded on %s\n", argv
[2]);
2608 ast_verbose(VERBOSE_PREFIX_3
"Hangup failed for %s\n", argv
[2]);
2610 return RESULT_SUCCESS
;
2613 static int h323_tokens_show(int fd
, int argc
, char *argv
[])
2616 return RESULT_SHOWUSAGE
;
2619 return RESULT_SUCCESS
;
2622 static char trace_usage
[] =
2623 "Usage: h.323 trace <level num>\n"
2624 " Enables H.323 stack tracing for debugging purposes\n";
2626 static char no_trace_usage
[] =
2627 "Usage: h.323 trace off\n"
2628 " Disables H.323 stack tracing for debugging purposes\n";
2630 static char debug_usage
[] =
2631 "Usage: h.323 debug\n"
2632 " Enables H.323 debug output\n";
2634 static char no_debug_usage
[] =
2635 "Usage: h.323 debug off\n"
2636 " Disables H.323 debug output\n";
2638 static char show_cycle_usage
[] =
2639 "Usage: h.323 gk cycle\n"
2640 " Manually re-register with the Gatekeper (Currently Disabled)\n";
2642 static char show_hangup_usage
[] =
2643 "Usage: h.323 hangup <token>\n"
2644 " Manually try to hang up call identified by <token>\n";
2646 static char show_tokens_usage
[] =
2647 "Usage: h.323 show tokens\n"
2648 " Print out all active call tokens\n";
2650 static char h323_reload_usage
[] =
2651 "Usage: h323 reload\n"
2652 " Reloads H.323 configuration from h323.conf\n";
2654 static struct ast_cli_entry cli_h323_no_trace_deprecated
= {
2655 { "h.323", "no", "trace", NULL
},
2656 h323_no_trace
, "Disable H.323 Stack Tracing",
2659 static struct ast_cli_entry cli_h323_no_debug_deprecated
= {
2660 { "h.323", "no", "debug", NULL
},
2661 h323_no_debug
, "Disable H.323 debug",
2664 static struct ast_cli_entry cli_h323_debug_deprecated
= {
2665 { "h.323", "debug", NULL
},
2666 h323_do_debug
, "Enable H.323 debug",
2669 static struct ast_cli_entry cli_h323_trace_deprecated
= {
2670 { "h.323", "trace", NULL
},
2671 h323_do_trace
, "Enable H.323 Stack Tracing",
2674 static struct ast_cli_entry cli_h323_gk_cycle_deprecated
= {
2675 { "h.323", "gk", "cycle", NULL
},
2676 h323_gk_cycle
, "Manually re-register with the Gatekeper",
2679 static struct ast_cli_entry cli_h323
[] = {
2680 { { "h323", "set", "trace", NULL
},
2681 h323_do_trace
, "Enable H.323 Stack Tracing",
2682 trace_usage
, NULL
, &cli_h323_trace_deprecated
},
2684 { { "h323", "set", "trace", "off", NULL
},
2685 h323_no_trace
, "Disable H.323 Stack Tracing",
2686 no_trace_usage
, NULL
, &cli_h323_no_trace_deprecated
},
2688 { { "h323", "set", "debug", NULL
},
2689 h323_do_debug
, "Enable H.323 debug",
2690 debug_usage
, NULL
, &cli_h323_debug_deprecated
},
2692 { { "h323", "set", "debug", "off", NULL
},
2693 h323_no_debug
, "Disable H.323 debug",
2694 no_debug_usage
, NULL
, &cli_h323_no_debug_deprecated
},
2696 { { "h323", "cycle", "gk", NULL
},
2697 h323_gk_cycle
, "Manually re-register with the Gatekeper",
2698 show_cycle_usage
, NULL
, &cli_h323_gk_cycle_deprecated
},
2700 { { "h323", "hangup", NULL
},
2701 h323_ep_hangup
, "Manually try to hang up a call",
2702 show_hangup_usage
},
2704 { { "h323", "show", "tokens", NULL
},
2705 h323_tokens_show
, "Show all active call tokens",
2706 show_tokens_usage
},
2709 static void delete_users(void)
2713 /* Delete all users */
2714 ASTOBJ_CONTAINER_WRLOCK(&userl
);
2715 ASTOBJ_CONTAINER_TRAVERSE(&userl
, 1, do {
2716 ASTOBJ_RDLOCK(iterator
);
2717 ASTOBJ_MARK(iterator
);
2719 ASTOBJ_UNLOCK(iterator
);
2722 ASTOBJ_CONTAINER_PRUNE_MARKED(&userl
, oh323_destroy_user
);
2724 ASTOBJ_CONTAINER_UNLOCK(&userl
);
2726 ASTOBJ_CONTAINER_WRLOCK(&peerl
);
2727 ASTOBJ_CONTAINER_TRAVERSE(&peerl
, 1, do {
2728 ASTOBJ_RDLOCK(iterator
);
2729 ASTOBJ_MARK(iterator
);
2730 ASTOBJ_UNLOCK(iterator
);
2732 ASTOBJ_CONTAINER_UNLOCK(&peerl
);
2735 static void delete_aliases(void)
2739 /* Delete all aliases */
2740 ASTOBJ_CONTAINER_WRLOCK(&aliasl
);
2741 ASTOBJ_CONTAINER_TRAVERSE(&aliasl
, 1, do {
2742 ASTOBJ_RDLOCK(iterator
);
2743 ASTOBJ_MARK(iterator
);
2745 ASTOBJ_UNLOCK(iterator
);
2748 ASTOBJ_CONTAINER_PRUNE_MARKED(&aliasl
, oh323_destroy_alias
);
2750 ASTOBJ_CONTAINER_UNLOCK(&aliasl
);
2753 static void prune_peers(void)
2755 /* Prune peers who still are supposed to be deleted */
2756 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl
, oh323_destroy_peer
);
2759 static int reload_config(int is_reload
)
2762 struct ast_config
*cfg
, *ucfg
;
2763 struct ast_variable
*v
;
2764 struct oh323_peer
*peer
= NULL
;
2765 struct oh323_user
*user
= NULL
;
2766 struct oh323_alias
*alias
= NULL
;
2767 struct ast_hostent ahp
; struct hostent
*hp
;
2770 int is_user
, is_peer
, is_alias
;
2771 char _gatekeeper
[100];
2772 int gk_discover
, gk_disable
, gk_changed
;
2774 cfg
= ast_config_load(config
);
2776 /* We *must* have a config file otherwise stop immediately */
2778 ast_log(LOG_NOTICE
, "Unable to load config %s, H.323 disabled\n", config
);
2788 /* fire up the H.323 Endpoint */
2789 if (!h323_end_point_exist()) {
2790 h323_end_point_create();
2792 ast_copy_string(_gatekeeper
, gatekeeper
, sizeof(_gatekeeper
));
2793 gk_discover
= gatekeeper_discover
;
2794 gk_disable
= gatekeeper_disable
;
2795 memset(&bindaddr
, 0, sizeof(bindaddr
));
2796 memset(&global_options
, 0, sizeof(global_options
));
2797 global_options
.fastStart
= 1;
2798 global_options
.h245Tunneling
= 1;
2799 global_options
.dtmfcodec
= 101;
2800 global_options
.dtmfmode
= H323_DTMF_RFC2833
;
2801 global_options
.capability
= GLOBAL_CAPABILITY
;
2802 global_options
.bridge
= 1; /* Do native bridging by default */
2803 strcpy(default_context
, "default");
2804 h323_signalling_port
= 1720;
2805 gatekeeper_disable
= 1;
2806 gatekeeper_discover
= 0;
2809 acceptAnonymous
= 1;
2812 /* Copy the default jb config over global_jbconf */
2813 memcpy(&global_jbconf
, &default_jbconf
, sizeof(struct ast_jb_conf
));
2815 /* Load configuration from users.conf */
2816 ucfg
= ast_config_load("users.conf");
2818 struct ast_variable
*gen
;
2820 const char *has_h323
;
2822 genhas_h323
= ast_true(ast_variable_retrieve(ucfg
, "general", "hash323"));
2823 gen
= ast_variable_browse(ucfg
, "general");
2824 for (cat
= ast_category_browse(ucfg
, NULL
); cat
; cat
= ast_category_browse(ucfg
, cat
)) {
2825 if (strcasecmp(cat
, "general")) {
2826 has_h323
= ast_variable_retrieve(ucfg
, cat
, "hash323");
2827 if (ast_true(has_h323
) || (!has_h323
&& genhas_h323
)) {
2828 user
= build_user(cat
, gen
, ast_variable_browse(ucfg
, cat
), 0);
2830 ASTOBJ_CONTAINER_LINK(&userl
, user
);
2831 ASTOBJ_UNREF(user
, oh323_destroy_user
);
2833 peer
= build_peer(cat
, gen
, ast_variable_browse(ucfg
, cat
), 0);
2835 ASTOBJ_CONTAINER_LINK(&peerl
, peer
);
2836 ASTOBJ_UNREF(peer
, oh323_destroy_peer
);
2841 ast_config_destroy(ucfg
);
2844 for (v
= ast_variable_browse(cfg
, "general"); v
; v
= v
->next
) {
2845 /* handle jb conf */
2846 if (!ast_jb_read_conf(&global_jbconf
, v
->name
, v
->value
))
2848 /* Create the interface list */
2849 if (!strcasecmp(v
->name
, "port")) {
2850 h323_signalling_port
= (int)strtol(v
->value
, NULL
, 10);
2851 } else if (!strcasecmp(v
->name
, "bindaddr")) {
2852 if (!(hp
= ast_gethostbyname(v
->value
, &ahp
))) {
2853 ast_log(LOG_WARNING
, "Invalid address: %s\n", v
->value
);
2855 memcpy(&bindaddr
.sin_addr
, hp
->h_addr
, sizeof(bindaddr
.sin_addr
));
2857 } else if (!strcasecmp(v
->name
, "tos")) {
2858 if (sscanf(v
->value
, "%d", &format
)) {
2859 tos
= format
& 0xff;
2860 } else if (!strcasecmp(v
->value
, "lowdelay")) {
2861 tos
= IPTOS_LOWDELAY
;
2862 } else if (!strcasecmp(v
->value
, "throughput")) {
2863 tos
= IPTOS_THROUGHPUT
;
2864 } else if (!strcasecmp(v
->value
, "reliability")) {
2865 tos
= IPTOS_RELIABILITY
;
2866 } else if (!strcasecmp(v
->value
, "mincost")) {
2867 tos
= IPTOS_MINCOST
;
2868 } else if (!strcasecmp(v
->value
, "none")) {
2871 ast_log(LOG_WARNING
, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v
->lineno
);
2873 } else if (!strcasecmp(v
->name
, "gatekeeper")) {
2874 if (!strcasecmp(v
->value
, "DISABLE")) {
2875 gatekeeper_disable
= 1;
2876 } else if (!strcasecmp(v
->value
, "DISCOVER")) {
2877 gatekeeper_disable
= 0;
2878 gatekeeper_discover
= 1;
2880 gatekeeper_disable
= 0;
2881 ast_copy_string(gatekeeper
, v
->value
, sizeof(gatekeeper
));
2883 } else if (!strcasecmp(v
->name
, "secret")) {
2884 ast_copy_string(secret
, v
->value
, sizeof(secret
));
2885 } else if (!strcasecmp(v
->name
, "AllowGKRouted")) {
2886 gkroute
= ast_true(v
->value
);
2887 } else if (!strcasecmp(v
->name
, "context")) {
2888 ast_copy_string(default_context
, v
->value
, sizeof(default_context
));
2889 ast_verbose(VERBOSE_PREFIX_2
"Setting default context to %s\n", default_context
);
2890 } else if (!strcasecmp(v
->name
, "UserByAlias")) {
2891 userbyalias
= ast_true(v
->value
);
2892 } else if (!strcasecmp(v
->name
, "AcceptAnonymous")) {
2893 acceptAnonymous
= ast_true(v
->value
);
2894 } else if (!update_common_options(v
, &global_options
)) {
2899 for (cat
= ast_category_browse(cfg
, NULL
); cat
; cat
= ast_category_browse(cfg
, cat
)) {
2900 if (strcasecmp(cat
, "general")) {
2901 utype
= ast_variable_retrieve(cfg
, cat
, "type");
2903 is_user
= is_peer
= is_alias
= 0;
2904 if (!strcasecmp(utype
, "user"))
2906 else if (!strcasecmp(utype
, "peer"))
2908 else if (!strcasecmp(utype
, "friend"))
2909 is_user
= is_peer
= 1;
2910 else if (!strcasecmp(utype
, "h323") || !strcasecmp(utype
, "alias"))
2913 ast_log(LOG_WARNING
, "Unknown type '%s' for '%s' in %s\n", utype
, cat
, config
);
2917 user
= build_user(cat
, ast_variable_browse(cfg
, cat
), NULL
, 0);
2919 ASTOBJ_CONTAINER_LINK(&userl
, user
);
2920 ASTOBJ_UNREF(user
, oh323_destroy_user
);
2924 peer
= build_peer(cat
, ast_variable_browse(cfg
, cat
), NULL
, 0);
2926 ASTOBJ_CONTAINER_LINK(&peerl
, peer
);
2927 ASTOBJ_UNREF(peer
, oh323_destroy_peer
);
2931 alias
= build_alias(cat
, ast_variable_browse(cfg
, cat
), NULL
, 0);
2933 ASTOBJ_CONTAINER_LINK(&aliasl
, alias
);
2934 ASTOBJ_UNREF(alias
, oh323_destroy_alias
);
2938 ast_log(LOG_WARNING
, "Section '%s' lacks type\n", cat
);
2942 ast_config_destroy(cfg
);
2944 /* Register our H.323 aliases if any*/
2945 ASTOBJ_CONTAINER_WRLOCK(&aliasl
);
2946 ASTOBJ_CONTAINER_TRAVERSE(&aliasl
, 1, do {
2947 ASTOBJ_RDLOCK(iterator
);
2948 if (h323_set_alias(iterator
)) {
2949 ast_log(LOG_ERROR
, "Alias %s rejected by endpoint\n", alias
->name
);
2950 ASTOBJ_UNLOCK(iterator
);
2953 ASTOBJ_UNLOCK(iterator
);
2955 ASTOBJ_CONTAINER_UNLOCK(&aliasl
);
2957 /* Don't touch GK if nothing changed because URQ will drop all existing calls */
2959 if (gatekeeper_disable
!= gk_disable
)
2960 gk_changed
= is_reload
;
2961 else if(!gatekeeper_disable
&& (gatekeeper_discover
!= gk_discover
))
2962 gk_changed
= is_reload
;
2963 else if(!gatekeeper_disable
&& (strncmp(_gatekeeper
, gatekeeper
, sizeof(_gatekeeper
)) != 0))
2964 gk_changed
= is_reload
;
2968 if (!gatekeeper_disable
) {
2969 if (h323_set_gk(gatekeeper_discover
, gatekeeper
, secret
)) {
2970 ast_log(LOG_ERROR
, "Gatekeeper registration failed.\n");
2971 gatekeeper_disable
= 1;
2978 static int h323_reload(int fd
, int argc
, char *argv
[])
2980 ast_mutex_lock(&h323_reload_lock
);
2981 if (h323_reloading
) {
2982 ast_verbose("Previous H.323 reload not yet done\n");
2986 ast_mutex_unlock(&h323_reload_lock
);
2991 static int h323_do_reload(void)
2997 static int reload(void)
2999 if (!sched
|| !io
) {
3000 ast_log(LOG_NOTICE
, "Unload and load chan_h323.so again in order to receive configuration changes.\n");
3003 return h323_reload(0, 0, NULL
);
3006 static struct ast_cli_entry cli_h323_reload
=
3007 { { "h.323", "reload", NULL
},
3008 h323_reload
, "Reload H.323 configuration",
3012 static enum ast_rtp_get_result
oh323_get_rtp_peer(struct ast_channel
*chan
, struct ast_rtp
**rtp
)
3014 struct oh323_pvt
*pvt
;
3015 enum ast_rtp_get_result res
= AST_RTP_GET_FAILED
;
3017 if (!(pvt
= (struct oh323_pvt
*)chan
->tech_pvt
))
3020 ast_mutex_lock(&pvt
->lock
);
3021 if (pvt
->rtp
&& pvt
->options
.bridge
) {
3023 res
= AST_RTP_TRY_NATIVE
;
3025 ast_mutex_unlock(&pvt
->lock
);
3030 static enum ast_rtp_get_result
oh323_get_vrtp_peer(struct ast_channel
*chan
, struct ast_rtp
**rtp
)
3032 return AST_RTP_GET_FAILED
;
3035 static char *convertcap(int cap
)
3038 case AST_FORMAT_G723_1
:
3040 case AST_FORMAT_GSM
:
3042 case AST_FORMAT_ULAW
:
3044 case AST_FORMAT_ALAW
:
3046 case AST_FORMAT_G722
:
3048 case AST_FORMAT_ADPCM
:
3050 case AST_FORMAT_G729A
:
3052 case AST_FORMAT_SPEEX
:
3054 case AST_FORMAT_ILBC
:
3057 ast_log(LOG_NOTICE
, "Don't know how to deal with mode %d\n", cap
);
3062 static int oh323_set_rtp_peer(struct ast_channel
*chan
, struct ast_rtp
*rtp
, struct ast_rtp
*vrtp
, int codecs
, int nat_active
)
3064 /* XXX Deal with Video */
3065 struct oh323_pvt
*pvt
;
3066 struct sockaddr_in them
;
3067 struct sockaddr_in us
;
3074 mode
= convertcap(chan
->writeformat
);
3075 pvt
= (struct oh323_pvt
*) chan
->tech_pvt
;
3077 ast_log(LOG_ERROR
, "No Private Structure, this is bad\n");
3080 ast_rtp_get_peer(rtp
, &them
);
3081 ast_rtp_get_us(rtp
, &us
);
3082 #if 0 /* Native bridge still isn't ready */
3083 h323_native_bridge(pvt
->cd
.call_token
, ast_inet_ntoa(them
.sin_addr
), mode
);
3088 static struct ast_rtp_protocol oh323_rtp
= {
3090 .get_rtp_info
= oh323_get_rtp_peer
,
3091 .get_vrtp_info
= oh323_get_vrtp_peer
,
3092 .set_rtp_peer
= oh323_set_rtp_peer
,
3095 static enum ast_module_load_result
load_module(void)
3100 sched
= sched_context_create();
3102 ast_log(LOG_WARNING
, "Unable to create schedule context\n");
3103 return AST_MODULE_LOAD_FAILURE
;
3105 io
= io_context_create();
3107 ast_log(LOG_WARNING
, "Unable to create I/O context\n");
3108 return AST_MODULE_LOAD_FAILURE
;
3110 ast_cli_register(&cli_h323_reload
);
3111 ASTOBJ_CONTAINER_INIT(&userl
);
3112 ASTOBJ_CONTAINER_INIT(&peerl
);
3113 ASTOBJ_CONTAINER_INIT(&aliasl
);
3114 res
= reload_config(0);
3116 /* No config entry */
3117 ast_log(LOG_NOTICE
, "Unload and load chan_h323.so again in order to receive configuration changes.\n");
3118 ast_cli_unregister(&cli_h323_reload
);
3119 io_context_destroy(io
);
3121 sched_context_destroy(sched
);
3123 ASTOBJ_CONTAINER_DESTROY(&userl
);
3124 ASTOBJ_CONTAINER_DESTROY(&peerl
);
3125 ASTOBJ_CONTAINER_DESTROY(&aliasl
);
3126 return AST_MODULE_LOAD_DECLINE
;
3128 /* Make sure we can register our channel type */
3129 if (ast_channel_register(&oh323_tech
)) {
3130 ast_log(LOG_ERROR
, "Unable to register channel class 'H323'\n");
3131 ast_cli_unregister(&cli_h323_reload
);
3133 io_context_destroy(io
);
3134 sched_context_destroy(sched
);
3136 ASTOBJ_CONTAINER_DESTROYALL(&userl
, oh323_destroy_user
);
3137 ASTOBJ_CONTAINER_DESTROY(&userl
);
3138 ASTOBJ_CONTAINER_DESTROYALL(&peerl
, oh323_destroy_peer
);
3139 ASTOBJ_CONTAINER_DESTROY(&peerl
);
3140 ASTOBJ_CONTAINER_DESTROYALL(&aliasl
, oh323_destroy_alias
);
3141 ASTOBJ_CONTAINER_DESTROY(&aliasl
);
3143 return AST_MODULE_LOAD_FAILURE
;
3145 ast_cli_register_multiple(cli_h323
, sizeof(cli_h323
) / sizeof(struct ast_cli_entry
));
3147 ast_rtp_proto_register(&oh323_rtp
);
3149 /* Register our callback functions */
3150 h323_callback_register(setup_incoming_call
,
3151 setup_outgoing_call
,
3152 external_rtp_create
,
3153 setup_rtp_connection
,
3162 set_local_capabilities
,
3163 set_peer_capabilities
);
3164 /* start the h.323 listener */
3165 if (h323_start_listener(h323_signalling_port
, bindaddr
)) {
3166 ast_log(LOG_ERROR
, "Unable to create H323 listener.\n");
3167 ast_rtp_proto_unregister(&oh323_rtp
);
3168 ast_cli_unregister_multiple(cli_h323
, sizeof(cli_h323
) / sizeof(struct ast_cli_entry
));
3169 ast_cli_unregister(&cli_h323_reload
);
3171 io_context_destroy(io
);
3172 sched_context_destroy(sched
);
3174 ASTOBJ_CONTAINER_DESTROYALL(&userl
, oh323_destroy_user
);
3175 ASTOBJ_CONTAINER_DESTROY(&userl
);
3176 ASTOBJ_CONTAINER_DESTROYALL(&peerl
, oh323_destroy_peer
);
3177 ASTOBJ_CONTAINER_DESTROY(&peerl
);
3178 ASTOBJ_CONTAINER_DESTROYALL(&aliasl
, oh323_destroy_alias
);
3179 ASTOBJ_CONTAINER_DESTROY(&aliasl
);
3181 return AST_MODULE_LOAD_FAILURE
;
3183 /* Possibly register with a GK */
3184 if (!gatekeeper_disable
) {
3185 if (h323_set_gk(gatekeeper_discover
, gatekeeper
, secret
)) {
3186 ast_log(LOG_ERROR
, "Gatekeeper registration failed.\n");
3187 gatekeeper_disable
= 1;
3188 res
= AST_MODULE_LOAD_SUCCESS
;
3191 /* And start the monitor for the first time */
3197 static int unload_module(void)
3199 struct oh323_pvt
*p
, *pl
;
3201 /* unregister commands */
3202 ast_cli_unregister_multiple(cli_h323
, sizeof(cli_h323
) / sizeof(struct ast_cli_entry
));
3203 ast_cli_unregister(&cli_h323_reload
);
3205 ast_channel_unregister(&oh323_tech
);
3206 ast_rtp_proto_unregister(&oh323_rtp
);
3208 if (!ast_mutex_lock(&iflock
)) {
3209 /* hangup all interfaces if they have an owner */
3213 ast_softhangup(p
->owner
, AST_SOFTHANGUP_APPUNLOAD
);
3218 ast_mutex_unlock(&iflock
);
3220 ast_log(LOG_WARNING
, "Unable to lock the interface list\n");
3223 if (!ast_mutex_lock(&monlock
)) {
3224 if ((monitor_thread
!= AST_PTHREADT_STOP
) && (monitor_thread
!= AST_PTHREADT_NULL
)) {
3225 /* this causes a seg, anyone know why? */
3226 if (monitor_thread
!= pthread_self())
3227 pthread_cancel(monitor_thread
);
3228 pthread_kill(monitor_thread
, SIGURG
);
3229 pthread_join(monitor_thread
, NULL
);
3231 monitor_thread
= AST_PTHREADT_STOP
;
3232 ast_mutex_unlock(&monlock
);
3234 ast_log(LOG_WARNING
, "Unable to lock the monitor\n");
3237 if (!ast_mutex_lock(&iflock
)) {
3238 /* destroy all the interfaces and free their memory */
3243 /* free associated memory */
3244 ast_mutex_destroy(&pl
->lock
);
3248 ast_mutex_unlock(&iflock
);
3250 ast_log(LOG_WARNING
, "Unable to lock the interface list\n");
3253 if (!gatekeeper_disable
)
3257 io_context_destroy(io
);
3259 sched_context_destroy(sched
);
3261 ASTOBJ_CONTAINER_DESTROYALL(&userl
, oh323_destroy_user
);
3262 ASTOBJ_CONTAINER_DESTROY(&userl
);
3263 ASTOBJ_CONTAINER_DESTROYALL(&peerl
, oh323_destroy_peer
);
3264 ASTOBJ_CONTAINER_DESTROY(&peerl
);
3265 ASTOBJ_CONTAINER_DESTROYALL(&aliasl
, oh323_destroy_alias
);
3266 ASTOBJ_CONTAINER_DESTROY(&aliasl
);
3271 AST_MODULE_INFO(ASTERISK_GPL_KEY
, AST_MODFLAG_DEFAULT
, "The NuFone Network's OpenH323 Channel Driver",
3272 .load
= load_module
,
3273 .unload
= unload_module
,