Skip to content
Snippets Groups Projects
chan_ooh323.c 148 KiB
Newer Older
/*
 * Copyright (C) 2004-2005 by Objective Systems, Inc.
 *
 * This software is furnished under an open source license and may be
 * used and copied only in accordance with the terms of this license.
 * The text of the license may generally be found in the root
 * directory of this installation in the COPYING file.  It
 * can also be viewed online at the following URL:
 *
 *   http://www.obj-sys.com/open/license.html
 *
 * Any redistributions of this file including modified versions must
 * maintain this copyright notice.
 *
 *****************************************************************************/

/* Reworked version I, Nov-2009, by Alexandr Anikin, may@telecom-service.ru */

/*** MODULEINFO
	<defaultenabled>no</defaultenabled>
	<support_level>extended</support_level>
/*** DOCUMENTATION
<info name="CHANNEL" language="en_US" tech="OOH323">
	<enumlist>
		<enum name="faxdetect">
			<para>R/W Fax Detect</para>
			<para>Returns 0 or 1</para>
			<para>Write yes or no</para>
		</enum>
		<enum name="t38support">
			<para>R/W t38support</para>
			<para>Returns 0 or 1</para>
			<para>Write yes or no</para>
		</enum>
		<enum name="h323id_url">
			<para>R/0 Returns caller URL</para>
			</enum>
		<enum name="caller_h323id">
			<para>R/0 Returns caller h323id</para>
		</enum>
		<enum name="caller_dialeddigits">
			<para>R/0 Returns caller dialed digits</para>
		</enum>
		<enum name="caller_email">
			<para>R/0 Returns caller email</para>
		</enum>
		<enum name="callee_email">
			<para>R/0 Returns callee email</para>
		</enum>
		<enum name="callee_dialeddigits">
			<para>R/0 Returns callee dialed digits</para>
		</enum>
		<enum name="caller_url">
			<para>R/0 Returns caller URL</para>
		</enum>
		<enum name="max_forwards">
			<para>R/W Get or set the maximum number of call forwards for this channel.

			This number describes the number of times a call may be forwarded by this channel
			before the call fails. "Forwards" in this case refers to redirects by phones as well
			as calls to local channels.

			Note that this has no relation to the SIP Max-Forwards header.
			</para>
		</enum>
	</enumlist>
</info>
 ***/

#include <math.h>
#ifndef IPTOS_MINCOST
#define IPTOS_MINCOST	0x02
#endif

#define FORMAT_STRING_SIZE	512

/* Defaults */
#define DEFAULT_CONTEXT "default"
#define DEFAULT_H323ID "Asterisk PBX"
#define DEFAULT_LOGFILE "h323_log"
#define DEFAULT_H323ACCNT "ast_h323"

/* Flags */
#define H323_SILENCESUPPRESSION (1<<0)
#define H323_GKROUTED           (1<<1)
#define H323_TUNNELING          (1<<2)
#define H323_FASTSTART          (1<<3)
#define H323_OUTGOING           (1<<4)
#define H323_ALREADYGONE        (1<<5)
#define H323_NEEDDESTROY        (1<<6)
#define H323_DISABLEGK          (1<<7)
#define H323_NEEDSTART		(1<<8)

#define MAXT30	240
#define T38TOAUDIOTIMEOUT 30
#define T38_DISABLED 0
#define T38_ENABLED 1
#define T38_FAXGW 1
#define FAXDETECT_CNG	1
#define FAXDETECT_T38	2

/* Channel description */
static const char type[] = "OOH323";
static const char tdesc[] = "Objective Systems H323 Channel Driver";
static const char config[] = "ooh323.conf";

struct ast_module *myself;
static struct ast_jb_conf default_jbconf =
{
	.flags = 0,
	.max_size = -1,
	.resync_threshold = -1,
	.impl = ""
};
static struct ast_jb_conf global_jbconf;
static struct ast_channel *ooh323_request(const char *type, struct ast_format_cap *cap,
			const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor,  const char *data, int *cause);
static int ooh323_digit_begin(struct ast_channel *ast, char digit);
static int ooh323_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
static int ooh323_call(struct ast_channel *ast, const char *dest, int timeout);
static int ooh323_hangup(struct ast_channel *ast);
static int ooh323_answer(struct ast_channel *ast);
static struct ast_frame *ooh323_read(struct ast_channel *ast);
static int ooh323_write(struct ast_channel *ast, struct ast_frame *f);
static int ooh323_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
static int ooh323_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
static int ooh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
static int function_ooh323_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len);
static int function_ooh323_write(struct ast_channel *chan, const char *cmd, char *data, const char *value);
static enum ast_rtp_glue_result ooh323_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **rtp);
static enum ast_rtp_glue_result ooh323_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **rtp);
static int ooh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp,
          struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, const struct ast_format_cap *codecs, int nat_active);
static void ooh323_get_codec(struct ast_channel *chan, struct ast_format_cap *result);
void setup_rtp_remote(ooCallData *call, const char *remoteIp, int remotePort);

struct ooh323_peer *find_friend(const char *name, int port);

static struct ast_channel_tech ooh323_tech = {
	.type = type,
	.description = tdesc,
	.properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
	.requester = ooh323_request,
	.send_digit_begin = ooh323_digit_begin,
	.send_digit_end = ooh323_digit_end,
	.call = ooh323_call,
	.hangup = ooh323_hangup,
	.answer = ooh323_answer,
	.read = ooh323_read,
	.write = ooh323_write,
	.exception = ooh323_read,
	.indicate = ooh323_indicate,
	.fixup = ooh323_fixup,
	.send_html = 0,
	.queryoption = ooh323_queryoption,
	.early_bridge = ast_rtp_instance_early_bridge,
	.func_channel_read = function_ooh323_read,
	.func_channel_write = function_ooh323_write,
static struct ast_rtp_glue ooh323_rtp = {
	.type = type,
	.get_rtp_info = ooh323_get_rtp_peer,
	.get_vrtp_info = ooh323_get_vrtp_peer,
	.update_peer = ooh323_set_rtp_peer,
	.get_codec = ooh323_get_codec,
/* H.323 channel private structure */
static struct ooh323_pvt {
	ast_mutex_t lock;		/* Channel private lock */
	ast_cond_t rtpcond;		/* RTP condition */
	struct ast_rtp_instance *rtp;
	struct ast_sockaddr redirip;	/* redir ip */
	struct ast_rtp_instance *vrtp; /* Placeholder for now */

	int t38support;			/* T.38 mode - disable, transparent, faxgw */
	int faxdetect;
	int faxdetected;
	int rtptimeout;
	struct ast_udptl *udptl;
	int faxmode;
	int t38_tx_enable;
	int t38_init;
	struct ast_sockaddr udptlredirip;
	time_t lastTxT38;
	struct ast_channel *owner;	/* Master Channel */
   	union {
    		char  *user;	/* cooperating user/peer */
    		char  *peer;
   	} neighbor;
	time_t lastrtptx;
	time_t lastrtprx;
	unsigned int flags;
	unsigned int call_reference;
	char *callToken;
	char *username;
	char *host;
	char *callerid_name;
	char *callerid_num;
	char caller_h323id[AST_MAX_EXTENSION];
	char caller_dialedDigits[AST_MAX_EXTENSION];
	char caller_email[AST_MAX_EXTENSION];
	char caller_url[256];
	char callee_h323id[AST_MAX_EXTENSION];
	char callee_dialedDigits[AST_MAX_EXTENSION];
	char callee_email[AST_MAX_EXTENSION];
	char callee_url[AST_MAX_EXTENSION];
	struct ast_format *readformat;   /* negotiated read format */
	struct ast_format *writeformat;  /* negotiated write format */
	int dtmfcodec;
	char exten[AST_MAX_EXTENSION];	/* Requested extension */
	char context[AST_MAX_EXTENSION];	/* Context where to start */
	char accountcode[256];	/* Account code */
	int nat;
	int amaflags;
	int progsent;			/* progress is sent */
	int alertsent;			/* alerting is sent */
	int directrtp;			/* direct rtp */
	int earlydirect;		/* early direct rtp */
	int g729onlyA;			/* G.729 only A */
	struct OOH323Regex *rtpmask;	/* rtp ip regexp */
	char rtpmaskstr[120];
	int rtdrcount, rtdrinterval;	/* roundtripdelayreq */
	int faststart, h245tunneling;	/* faststart & h245 tunneling */
	int aniasdni;			/* use dialed number as answering identification */
	struct ooh323_pvt *next;	/* Next entity */
} *iflist = NULL;

/* Protect the channel/interface list (ooh323_pvt) */
AST_MUTEX_DEFINE_STATIC(iflock);

/* Profile of H.323 user registered with PBX*/
struct ooh323_user{
	ast_mutex_t lock;
	char		name[256];
	char		context[AST_MAX_EXTENSION];
	int		incominglimit;
	unsigned	inUse;
	char		accountcode[20];
	int		amaflags;
	int		dtmfmode;
	int		dtmfcodec;
	int		faxdetect;
	int		t38support;
	int		rtptimeout;
	int		mUseIP;        /* Use IP address or H323-ID to search user */
	char		mIP[4*8+7+2];  /* Max for IPv6 - 2 brackets, 8 4hex, 7 - : */
	struct OOH323Regex *rtpmask;
	char		rtpmaskstr[120];
	int		rtdrcount, rtdrinterval;
	int		faststart, h245tunneling;
	int		directrtp;
	int		earlydirect;
	int		g729onlyA;
	struct ooh323_user *next;
};

/* Profile of valid asterisk peers */
struct ooh323_peer{
	ast_mutex_t lock;
	char        name[256];
	unsigned    outgoinglimit;
	unsigned    outUse;
	char        accountcode[20];
	int         amaflags;
	int         dtmfmode;
	int	    dtmfcodec;
	int	    faxdetect;
	int	    t38support;
	int         mFriend;    /* indicates defined as friend */
	char        ip[4*8+7+2]; /* Max for IPv6 - 2 brackets, 8 4hex, 7 - : */
	int         port;
	char        *h323id;    /* H323-ID alias, which asterisk will register with gk to reach this peer*/
	char        *email;     /* Email alias, which asterisk will register with gk to reach this peer*/
	char        *url;       /* url alias, which asterisk will register with gk to reach this peer*/
	char        *e164;      /* e164 alias, which asterisk will register with gk to reach this peer*/
	int         rtptimeout;
	struct OOH323Regex	    *rtpmask;
	char	    rtpmaskstr[120];
	int	    rtdrcount,rtdrinterval;
	int	    directrtp;
	int	    earlydirect;
	struct ooh323_peer *next;
};


/* List of H.323 users known to PBX */
static struct ast_user_list {
	struct ooh323_user *users;
	ast_mutex_t lock;
} userl;

static struct ast_peer_list {
	struct ooh323_peer *peers;
	ast_mutex_t lock;
} peerl;

/* Mutex to protect H.323 reload process */
static int h323_reloading = 0;
AST_MUTEX_DEFINE_STATIC(h323_reload_lock);

/* Mutex to protect usage counter */
static int usecnt = 0;
AST_MUTEX_DEFINE_STATIC(usecnt_lock);

static long callnumber = 0;
AST_MUTEX_DEFINE_STATIC(ooh323c_cn_lock);

/* stack callbacks */
int onAlerting(ooCallData *call);
int onProgress(ooCallData *call);
int onNewCallCreated(ooCallData *call);
int onOutgoingCall(ooCallData *call);
int onCallEstablished(ooCallData *call);
int onCallCleared(ooCallData *call);
void onModeChanged(ooCallData *call, int t38mode);
extern OOH323EndPoint gH323ep;

static char gLogFile[PATH_MAX] = DEFAULT_LOGFILE;
static char gInitError[256] = "";
static char gIP[2+8*4+7];	/* Max for IPv6 addr */
struct ast_sockaddr bindaddr;
int v6mode = 0;
static char gCallerID[AST_MAX_EXTENSION] = "";
static struct ooAliases *gAliasList;
static struct ast_format_cap *gCap;
static int  gDTMFMode = H323_DTMF_RFC2833;
static int  gDTMFCodec = 101;
static int  gFAXdetect = FAXDETECT_CNG;
static int  gT38Support = T38_FAXGW;
static char gRASIP[2+8*4+7];	/* Max for IPv6 addr */
static enum RasGatekeeperMode gRasGkMode = RasNoGatekeeper;

static int  gIsGateway = 0;
static int  gFastStart = 1;
static int  gTunneling = 1;
static int  gBeMaster = 0;
static int  gMediaWaitForConnect = 0;
static int  gDirectRTP = 0;
static int  gEarlyDirect = 0;
static int  gTOS = 0;
static int  gRTPTimeout = 60;
static int  g729onlyA = 0;
static char gAccountcode[80] = DEFAULT_H323ACCNT;
static int  gAMAFLAGS;
static char gContext[AST_MAX_EXTENSION] = DEFAULT_CONTEXT;
static int  gIncomingLimit = 1024;
static int  gOutgoingLimit = 1024;
static int gTRCLVL = OOTRCLVLERR;
static int gRTDRCount = 0, gRTDRInterval = 0;
static int gNat = FALSE;

static int t35countrycode = 0;
static int t35extensions = 0;
static int manufacturer = 0;
static char vendor[AST_MAX_EXTENSION] =  "";
static char version[AST_MAX_EXTENSION] = "";

static struct ooh323_config
{
   int  mTCPPortStart;
   int  mTCPPortEnd;
} ooconfig;

/** Asterisk RTP stuff*/
static struct ast_sched_context *sched;
/* Protect the monitoring thread, so only one process can kill or start it,
   and not when it's doing something critical. */
AST_MUTEX_DEFINE_STATIC(monlock);


/* This is the thread for the monitor which checks for input on the channels
   which are not currently in use.  */
static pthread_t monitor_thread = AST_PTHREADT_NULL;


static struct ast_channel *ooh323_new(struct ooh323_pvt *i, int state,
                                             const char *host, struct ast_format_cap *cap,
											 const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
	struct ast_channel *ch = NULL;
	if (gH323Debug) {
		ast_verb(0, "---   ooh323_new - %s\n", host);
	}
	caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);

	/* Don't hold a h323 pvt lock while we allocate a channel */
	ast_mutex_unlock(&i->lock);
   	ast_mutex_lock(&ooh323c_cn_lock);
   	ch = ast_channel_alloc(1, state, i->callerid_num, i->callerid_name,
				i->accountcode, i->exten, i->context, assignedids, requestor, i->amaflags,
				"OOH323/%s-%ld", host, callnumber);
   	callnumber++;
   	ast_mutex_unlock(&ooh323c_cn_lock);
		ast_channel_tech_set(ch, &ooh323_tech);
		if (cap) {
			tmpfmt = ast_format_cap_get_format(cap, 0);
		}
		if (!tmpfmt) {
			tmpfmt = ast_format_cap_get_format(i->cap, 0);
		}

		ast_format_cap_append(caps, tmpfmt, 0);
		ast_channel_nativeformats_set(ch, caps);
		ao2_ref(caps, -1);
		ast_channel_set_rawwriteformat(ch, tmpfmt);
		ast_channel_set_rawreadformat(ch, tmpfmt);
		ast_set_write_format(ch, tmpfmt);
		ast_set_read_format(ch, tmpfmt);
		ao2_ref(tmpfmt, -1);
		ast_jb_configure(ch, &global_jbconf);
			ast_channel_rings_set(ch, 1);
		ast_channel_adsicpe_set(ch, AST_ADSI_UNAVAILABLE);
		ast_channel_tech_pvt_set(ch, i);
		ast_module_ref(myself);

		/* Allocate dsp for in-band DTMF support */
		if ((i->dtmfmode & H323_DTMF_INBAND) || (i->faxdetect & FAXDETECT_CNG)) {
		/* inband DTMF*/
		if (i->dtmfmode & H323_DTMF_INBAND) {
			features |= DSP_FEATURE_DIGIT_DETECT;
			if (i->dtmfmode & H323_DTMF_INBANDRELAX) {
				ast_dsp_set_digitmode(i->vad, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
			}
		}

		/* fax detection*/
		if (i->faxdetect & FAXDETECT_CNG) {
			features |= DSP_FEATURE_FAX_DETECT;
			ast_dsp_set_faxmode(i->vad,
					DSP_FAXMODE_DETECT_CNG | DSP_FAXMODE_DETECT_CED);
		}

		if (features) {
			ast_dsp_set_features(i->vad, features);
		}

		ast_mutex_lock(&usecnt_lock);
		usecnt++;
		ast_mutex_unlock(&usecnt_lock);

		/* Notify the module monitors that use count for resource has changed*/
		ast_update_use_count();

		ast_channel_context_set(ch, i->context);
		ast_channel_exten_set(ch, i->exten);
		ast_channel_priority_set(ch, 1);
      		if(!ast_test_flag(i, H323_OUTGOING)) {
			if (!ast_strlen_zero(i->caller_h323id)) {
				pbx_builtin_setvar_helper(ch, "_CALLER_H323ID", i->caller_h323id);

			}
			if (!ast_strlen_zero(i->caller_dialedDigits)) {
				pbx_builtin_setvar_helper(ch, "_CALLER_H323DIALEDDIGITS",
				i->caller_dialedDigits);
			}
			if (!ast_strlen_zero(i->caller_email)) {
				pbx_builtin_setvar_helper(ch, "_CALLER_H323EMAIL",
				i->caller_email);
			}
			if (!ast_strlen_zero(i->caller_url)) {
				pbx_builtin_setvar_helper(ch, "_CALLER_H323URL", i->caller_url);
			}
		}

		if (!ast_strlen_zero(i->accountcode))
			ast_channel_accountcode_set(ch, i->accountcode);
			ast_channel_amaflags_set(ch, i->amaflags);

		ast_setstate(ch, state);
		if (state != AST_STATE_DOWN) {
         		if (ast_pbx_start(ch)) {
				ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(ch));
            			ast_channel_unlock(ch);
		ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
   	if(ch)   ast_channel_unlock(ch);
static struct ooh323_pvt *ooh323_alloc(int callref, char *callToken)

	if (!(pvt = ast_calloc(1, sizeof(*pvt)))) {
		ast_log(LOG_ERROR, "Couldn't allocate private ooh323 structure\n");
		return NULL;
	}
	if (!(pvt->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
		ast_free(pvt);
		ast_log(LOG_ERROR, "Couldn't allocate private ooh323 structure\n");
		return NULL;
	}

	ast_mutex_init(&pvt->lock);
	ast_mutex_lock(&pvt->lock);

	pvt->faxmode = 0;
	pvt->chmodepend = 0;
	pvt->faxdetected = 0;
	pvt->faxdetect = gFAXdetect;
	pvt->t38support = gT38Support;
	pvt->rtptimeout = gRTPTimeout;
	pvt->rtdrinterval = gRTDRInterval;
	pvt->rtdrcount = gRTDRCount;
	pvt->g729onlyA = g729onlyA;

	pvt->call_reference = callref;
	if (callToken)
		pvt->callToken = ast_strdup(callToken);

	/* whether to use gk for this call */
	if (gRasGkMode == RasNoGatekeeper)
		OO_SETFLAG(pvt->flags, H323_DISABLEGK);

	pvt->dtmfmode = gDTMFMode;
	pvt->dtmfcodec = gDTMFCodec;
	ast_copy_string(pvt->context, gContext, sizeof(pvt->context));
	ast_copy_string(pvt->accountcode, gAccountcode, sizeof(pvt->accountcode));
	ast_format_cap_append_from_cap(pvt->cap, gCap, AST_MEDIA_TYPE_UNKNOWN);
	ast_mutex_unlock(&pvt->lock);
	/* Add to interface list */
	ast_mutex_lock(&iflock);
	pvt->next = iflist;
	iflist = pvt;
	ast_mutex_unlock(&iflock);


	return pvt;
}


/*
	Possible data values - peername, exten/peername, exten@ip
 */
static struct ast_channel *ooh323_request(const char *type, struct ast_format_cap *cap,
		const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
	struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
	struct ast_channel *chan = NULL;
	struct ooh323_pvt *p = NULL;
	struct ooh323_peer *peer = NULL;
	char *dest = NULL;
	char *ext = NULL;
	char tmp[256];
	int port = 0;

	if (gH323Debug) {
		ast_verb(0, "---   ooh323_request - data %s format %s\n", data, ast_format_cap_get_names(cap, &codec_buf));
	}
	if (!(ast_format_cap_has_type(cap, AST_MEDIA_TYPE_AUDIO))) {
		ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n", ast_format_cap_get_names(cap, &codec_buf));
		return NULL;
	}

	p = ooh323_alloc(0,0); /* Initial callRef is zero */

	if (!p) {
		ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", data);
		return NULL;
	}
	ast_mutex_lock(&p->lock);

	/* This is an outgoing call, since ooh323_request is called */
	ast_set_flag(p, H323_OUTGOING);


   	ast_copy_string(tmp, data, sizeof(tmp));
		ext = dest;
		dest = tmp;
	} else if ((dest = strchr(tmp, '@'))) {
		*dest = '\0';
		dest++;
		ext = tmp;
	} else {
		dest = tmp;
		ext = NULL;
	}

#if 0
	if ((sport = strchr(dest, ':'))) {
		*sport = '\0';
		sport++;
		port = atoi(sport);
	}
#endif

	if (dest) {
		peer = find_peer(dest, port);
	} else{
		ast_mutex_lock(&iflock);
		ast_mutex_unlock(&p->lock);
		ooh323_destroy(p);
		ast_mutex_unlock(&iflock);
		ast_log(LOG_ERROR, "Destination format is not supported\n");
		*cause = AST_CAUSE_INVALID_NUMBER_FORMAT;
		p->username = ast_strdup(peer->name);
		p->host = ast_strdup(peer->ip);
		p->port = peer->port;
		/* Disable gk as we are going to call a known peer*/
		/* OO_SETFLAG(p->flags, H323_DISABLEGK); */

		if (ext)
			ast_copy_string(p->exten, ext, sizeof(p->exten));

		ast_format_cap_append_from_cap(p->cap, peer->cap, AST_MEDIA_TYPE_UNKNOWN);
		p->g729onlyA = peer->g729onlyA;
		p->dtmfmode |= peer->dtmfmode;
		p->dtmfcodec  = peer->dtmfcodec;
		p->faxdetect = peer->faxdetect;
		p->t38support = peer->t38support;
		p->rtptimeout = peer->rtptimeout;
		p->faststart = peer->faststart;
		p->h245tunneling = peer->h245tunneling;
		p->directrtp = peer->directrtp;
		p->earlydirect = peer->earlydirect;
		if (peer->rtpmask && peer->rtpmaskstr[0]) {
			p->rtpmask = peer->rtpmask;
			ast_copy_string(p->rtpmaskstr, peer->rtpmaskstr, sizeof(p->rtpmaskstr));
		}

		if (peer->rtdrinterval) {
			p->rtdrinterval = peer->rtdrinterval;
			p->rtdrcount = peer->rtdrcount;
		}

		ast_copy_string(p->accountcode, peer->accountcode, sizeof(p->accountcode));
		p->amaflags = peer->amaflags;
	} else {
		if (gRasGkMode ==  RasNoGatekeeper) {
			/* no gk and no peer */
			ast_log(LOG_ERROR, "Call to undefined peer %s", dest);
			ast_mutex_lock(&iflock);
			ast_mutex_unlock(&p->lock);
			ooh323_destroy(p);
			ast_mutex_unlock(&iflock);
			return NULL;
		} else if (!gH323ep.gkClient || (gH323ep.gkClient && gH323ep.gkClient->state != GkClientRegistered)) {
			ast_log(LOG_ERROR, "Gatekeeper client is configured but not registered\n");
			*cause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
			return NULL;
		p->g729onlyA = g729onlyA;
		p->dtmfcodec = gDTMFCodec;
		p->faxdetect = gFAXdetect;
		p->t38support = gT38Support;
		p->rtptimeout = gRTPTimeout;
		ast_format_cap_append_from_cap(p->cap, gCap, AST_MEDIA_TYPE_UNKNOWN);
		p->rtdrinterval = gRTDRInterval;
		p->rtdrcount = gRTDRCount;
		p->faststart = gFastStart;
		p->h245tunneling = gTunneling;
		p->directrtp = gDirectRTP;
		p->earlydirect = gEarlyDirect;
		p->username = ast_strdup(dest);
		p->host = ast_strdup(dest);
		if (port > 0) {
			p->port = port;
		}
		if (ext) {
			ast_copy_string(p->exten, ext, sizeof(p->exten));
		}
	}


	chan = ooh323_new(p, AST_STATE_DOWN, p->username, cap,
	ast_mutex_unlock(&p->lock);

	if (!chan) {
		ast_mutex_lock(&iflock);
		ooh323_destroy(p);
		ast_mutex_unlock(&iflock);
   	} else {
      		ast_mutex_lock(&p->lock);
      		p->callToken = (char*)ast_calloc(1, AST_MAX_EXTENSION);
      		if(!p->callToken) {
       			ast_mutex_unlock(&p->lock);
       			ast_mutex_lock(&iflock);
       			ooh323_destroy(p);
       			ast_mutex_unlock(&iflock);
       			ast_log(LOG_ERROR, "Failed to allocate memory for callToken\n");
       			return NULL;
      		}

		ast_cond_init(&p->rtpcond, NULL);
      		ooMakeCall(data, p->callToken, AST_MAX_EXTENSION, NULL);
		if (!p->rtp) {
			ast_cond_wait(&p->rtpcond, &p->lock);
		}
		ast_mutex_unlock(&p->lock);
		ast_cond_destroy(&p->rtpcond);

	return chan;

}


static struct ooh323_pvt* find_call(ooCallData *call)
{
	struct ooh323_pvt *p;

	if (gH323Debug)

	ast_mutex_lock(&iflock);

	for (p = iflist; p; p = p->next) {
		if (p->callToken && !strcmp(p->callToken, call->callToken)) {
			break;
		}
	}
	ast_mutex_unlock(&iflock);

	if (gH323Debug)

	return p;
}

struct ooh323_user *find_user(const char * name, const char* ip)
{
	struct ooh323_user *user;

	if (gH323Debug)
      ast_verb(0, "---   find_user: %s, %s\n",name,ip);
	for (user = userl.users; user; user = user->next) {
		if (ip && user->mUseIP && !strcmp(user->mIP, ip)) {
			break;
		}
		if (name && !strcmp(user->name, name)) {
			break;
		}
	}
	ast_mutex_unlock(&userl.lock);

	if (gH323Debug)

	return user;
}

struct ooh323_peer *find_friend(const char *name, int port)
{
	struct ooh323_peer *peer;


	ast_mutex_lock(&peerl.lock);
	for (peer = peerl.peers; peer; peer = peer->next) {
		if (gH323Debug) {
			ast_verb(0, "		comparing with \"%s\"\n", peer->ip);
		}
		if (!strcmp(peer->ip, name)) {
			if (port <= 0 || (port > 0 && peer->port == port)) {
				break;
			}
		}
	}
	ast_mutex_unlock(&peerl.lock);

	if (gH323Debug) {
		if (peer) {
}


struct ooh323_peer *find_peer(const char * name, int port)
{
	struct ooh323_peer *peer;

	if (gH323Debug)
	ast_mutex_lock(&peerl.lock);
	for (peer = peerl.peers; peer; peer = peer->next) {
		if (gH323Debug) {
			ast_verb(0, "		comparing with \"%s\"\n", peer->ip);
		}
		if (!strcasecmp(peer->name, name))
			break;
		if (peer->h323id && !strcasecmp(peer->h323id, name))
			break;
		if (peer->e164 && !strcasecmp(peer->e164, name))
			break;
		/*
		if (!strcmp(peer->ip, name)) {
			if (port > 0 && peer->port == port) { break; }
			else if (port <= 0) { break; }
		}
		*/
	}
	ast_mutex_unlock(&peerl.lock);

	if (gH323Debug) {
		if (peer) {
}

static int ooh323_digit_begin(struct ast_channel *chan, char digit)
{
	char dtmf[2];
	struct ooh323_pvt *p = (struct ooh323_pvt *) ast_channel_tech_pvt(chan);

	if (!p) {
		ast_log(LOG_ERROR, "No private structure for call\n");
		return -1;
	}
	ast_mutex_lock(&p->lock);
	if (p->rtp && ((p->dtmfmode & H323_DTMF_RFC2833) || (p->dtmfmode & H323_DTMF_CISCO))) {
		ast_rtp_instance_dtmf_begin(p->rtp, digit);
	} else if (((p->dtmfmode & H323_DTMF_Q931) ||
						 (p->dtmfmode & H323_DTMF_H245ALPHANUMERIC) ||
						 (p->dtmfmode & H323_DTMF_H245SIGNAL))) {
		dtmf[0] = digit;
		dtmf[1] = '\0';
		ooSendDTMFDigit(p->callToken, dtmf);
	} else if (p->dtmfmode & H323_DTMF_INBAND) {
		res = -1; // tell Asterisk to generate inband indications
		ast_verb(0, "+++   ooh323_digit_begin, res = %d\n", res);
}

static int ooh323_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
{
	struct ooh323_pvt *p = (struct ooh323_pvt *) ast_channel_tech_pvt(chan);

	if (!p) {
		ast_log(LOG_ERROR, "No private structure for call\n");
		return -1;
	}
	ast_mutex_lock(&p->lock);
	if (p->rtp && ((p->dtmfmode & H323_DTMF_RFC2833) || (p->dtmfmode & H323_DTMF_CISCO)) ) {
		ast_rtp_instance_dtmf_end(p->rtp, digit);
	} else if(p->dtmfmode & H323_DTMF_INBAND) {
		res = -1; // tell Asterisk to stop inband indications
	}
	if (gH323Debug) {
		ast_verb(0, "+++   ooh323_digit_end, res = %d\n", res);
	}
	return res;
static int ooh323_call(struct ast_channel *ast, const char *dest, int timeout)
	struct ooh323_pvt *p = ast_channel_tech_pvt(ast);
   	int res=0, i;
	const char *val = NULL;
	ooCallOptions opts = {
		.fastStart = TRUE,
		.tunneling = TRUE,
		.disableGk = TRUE,
      		.callMode = OO_CALLMODE_AUDIOCALL,
      		.transfercap = 0
   	if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
		ast_log(LOG_WARNING, "ooh323_call called on %s, neither down nor "
								"reserved\n", ast_channel_name(ast));
		return -1;
	}
	ast_mutex_lock(&p->lock);
	ast_set_flag(p, H323_OUTGOING);
	if (ast_channel_connected(ast)->id.number.valid && ast_channel_connected(ast)->id.number.str) {
		ast_free(p->callerid_num);
		p->callerid_num = ast_strdup(ast_channel_connected(ast)->id.number.str);
	if (ast_channel_connected(ast)->id.name.valid && ast_channel_connected(ast)->id.name.str) {
		ast_free(p->callerid_name);
		p->callerid_name = ast_strdup(ast_channel_connected(ast)->id.name.str);
	} else if (ast_channel_connected(ast)->id.number.valid && ast_channel_connected(ast)->id.number.str) {
		ast_free(p->callerid_name);
		p->callerid_name = ast_strdup(ast_channel_connected(ast)->id.number.str);
		ast_channel_connected(ast)->id.name.valid = 1;
		ast_free(ast_channel_connected(ast)->id.name.str);
		ast_channel_connected(ast)->id.name.str = ast_strdup(gCallerID);
		ast_free(p->callerid_name);
		p->callerid_name = ast_strdup(ast_channel_connected(ast)->id.name.str);
	}

	/* Retrieve vars */


	if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323ID"))) {
		ast_copy_string(p->caller_h323id, val, sizeof(p->caller_h323id));
	}
	if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323DIALEDDIGITS"))) {
		ast_copy_string(p->caller_dialedDigits, val, sizeof(p->caller_dialedDigits));
      		if(!p->callerid_num)
			p->callerid_num = ast_strdup(val);
	}

	if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323EMAIL"))) {
		ast_copy_string(p->caller_email, val, sizeof(p->caller_email));
	}

	if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323URL"))) {
		ast_copy_string(p->caller_url, val, sizeof(p->caller_url));
	}

	if (p->host && p->port != 0)
		snprintf(destination, sizeof(destination), "%s:%d", p->host, p->port);
	else if (p->host)
		snprintf(destination, sizeof(destination), "%s", p->host);
	else
		ast_copy_string(destination, dest, sizeof(destination));

	destination[sizeof(destination)-1]='\0';
	opts.transfercap = ast_channel_transfercapability(ast);
	opts.fastStart = p->faststart;
	opts.tunneling = p->h245tunneling;
	for (i=0;i<480 && !isRunning(p->callToken);i++) usleep(12000);
	if(OO_TESTFLAG(p->flags, H323_DISABLEGK)) {
		res = ooRunCall(destination, p->callToken, AST_MAX_EXTENSION, &opts);
	} else {
		res = ooRunCall(destination, p->callToken, AST_MAX_EXTENSION, NULL);
 	}

	ast_mutex_unlock(&p->lock);
	if (res != OO_OK) {
		ast_log(LOG_ERROR, "Failed to make call\n");
      		return -1; /* ToDO: cleanup */

  return 0;
}

static int ooh323_hangup(struct ast_channel *ast)
{
	struct ooh323_pvt *p = ast_channel_tech_pvt(ast);
   	int q931cause = AST_CAUSE_NORMAL_CLEARING;
        if (ast_channel_hangupcause(ast)) {
                q931cause = ast_channel_hangupcause(ast);
        } else {
                const char *cause = pbx_builtin_getvar_helper(ast, "DIALSTATUS");
                if (cause) {
                        if (!strcmp(cause, "CONGESTION")) {
                                q931cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
                        } else if (!strcmp(cause, "BUSY")) {
                                q931cause = AST_CAUSE_USER_BUSY;
                        } else if (!strcmp(cause, "CHANISUNVAIL")) {
                                q931cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
                        } else if (!strcmp(cause, "NOANSWER")) {
                                q931cause = AST_CAUSE_NO_ANSWER;
                        } else if (!strcmp(cause, "CANCEL")) {
                                q931cause = AST_CAUSE_CALL_REJECTED;
                        }
                }
        }



			ast_verb(0, "    hanging %s with cause: %d\n", p->username, q931cause);
		ast_channel_tech_pvt_set(ast, NULL);
		if (!ast_test_flag(p, H323_ALREADYGONE)) {
         		ooHangCall(p->callToken,
				ooh323_convert_hangupcause_asteriskToH323(q931cause), q931cause);
			ast_set_flag(p, H323_ALREADYGONE);
			/* ast_mutex_unlock(&p->lock); */
			ast_set_flag(p, H323_NEEDDESTROY);
		/* detach channel here */
		if (p->owner) {
			ast_channel_tech_pvt_set(p->owner, NULL);
			ast_module_unref(myself);
		}

		ast_mutex_unlock(&p->lock);
		ast_mutex_lock(&usecnt_lock);
		usecnt--;
		ast_mutex_unlock(&usecnt_lock);

		/* Notify the module monitors that use count for resource has changed */
		ast_update_use_count();
		ast_debug(1, "No call to hangup\n" );

  return 0;
}

static int ooh323_answer(struct ast_channel *ast)
{
	struct ooh323_pvt *p = ast_channel_tech_pvt(ast);
	char *callToken = (char *)NULL;
	if (p) {

		ast_mutex_lock(&p->lock);
		callToken = (p->callToken ? ast_strdup(p->callToken) : NULL);
		if (ast_channel_state(ast) != AST_STATE_UP) {
			ast_channel_lock(ast);
			if (!p->alertsent) {
	    			if (gH323Debug) {
					ast_debug(1, "Sending forced ringback for %s, res = %u\n",
						callToken, ooManualRingback(callToken));
				} else {
	    				ooManualRingback(callToken);
				}
				p->alertsent = 1;
			}
			ast_setstate(ast, AST_STATE_UP);
			ast_debug(1, "ooh323_answer(%s)\n", ast_channel_name(ast));
			ast_channel_unlock(ast);
			ooAnswerCall(p->callToken);
		}
		ast_mutex_unlock(&p->lock);

  return 0;
}

static struct ast_frame *ooh323_read(struct ast_channel *ast)
{
	struct ast_frame *fr;
	static struct ast_frame null_frame = { AST_FRAME_NULL, };
	struct ooh323_pvt *p = ast_channel_tech_pvt(ast);
	if (!p) return &null_frame;

	ast_mutex_lock(&p->lock);
	if (p->rtp)
		fr = ooh323_rtp_read(ast, p);
	else
		fr = &null_frame;
	/* time(&p->lastrtprx); */
	ast_mutex_unlock(&p->lock);
	return fr;
}

static int ooh323_write(struct ast_channel *ast, struct ast_frame *f)
{
	struct ooh323_pvt *p = ast_channel_tech_pvt(ast);
	if (p) {
		ast_mutex_lock(&p->lock);

		if (f->frametype == AST_FRAME_MODEM) {
			ast_debug(1, "Send UDPTL %u/%d len %d for %s\n",
				f->frametype, f->subclass.integer, f->datalen, ast_channel_name(ast));
			if (p->udptl)
				res = ast_udptl_write(p->udptl, f);
			ast_mutex_unlock(&p->lock);
			return res;
		}

		if (f->frametype == AST_FRAME_VOICE) {
/* sending progress for first */
			if (!ast_test_flag(p, H323_OUTGOING) && !p->progsent &&
			 		p->callToken) {
				ooManualProgress(p->callToken);
				p->progsent = 1;
			}


			if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
				if (ast_format_cap_count(ast_channel_nativeformats(ast))) {
					struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
					ast_log(LOG_WARNING,
							"Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
							ast_format_get_name(f->subclass.format),
							ast_format_cap_get_names(ast_channel_nativeformats(ast), &codec_buf),
							ast_format_get_name(ast_channel_readformat(ast)),
							ast_format_get_name(ast_channel_writeformat(ast)));
					ast_set_write_format(ast, f->subclass.format);
				} else {
					/* ast_set_write_format(ast, f->subclass);
					ast->nativeformats = f->subclass; */
				}
			ast_mutex_unlock(&p->lock);
			}

		if (p->rtp)
			res = ast_rtp_instance_write(p->rtp, f);

		ast_mutex_unlock(&p->lock);

		} else if (f->frametype == AST_FRAME_IMAGE) {
			return 0;
		} else {
			ast_log(LOG_WARNING, "Can't send %u type frames with OOH323 write\n",
			ast_mutex_unlock(&p->lock);
			return 0;
		}

	}

	return res;
}

static int ooh323_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
{

	struct ooh323_pvt *p = (struct ooh323_pvt *) ast_channel_tech_pvt(ast);
	char *callToken = (char *)NULL;
	if (!p) return -1;

	callToken = (p->callToken ? ast_strdup(p->callToken) : NULL);
	ast_mutex_unlock(&p->lock);

	if (!callToken) {
		if (gH323Debug)
			ast_verb(0, "	ooh323_indicate - No callToken\n");
	if (!ast_sockaddr_isnull(&p->redirip)) {
		res = 0;
	}

	if (gH323Debug) {
		ast_verb(0, "----- ooh323_indicate %d on call %s\n", condition, callToken);
   	ast_mutex_lock(&p->lock);
	case AST_CONTROL_INCOMPLETE:
		/* While h323 does support overlapped dialing, this channel driver does not
		 * at this time.  Treat a response of Incomplete as if it were congestion.
		 */
	case AST_CONTROL_CONGESTION:
		if (!ast_test_flag(p, H323_ALREADYGONE)) {
			ooHangCall(callToken, OO_REASON_LOCAL_CONGESTED, AST_CAUSE_SWITCH_CONGESTION);
		}
		break;
	case AST_CONTROL_BUSY:
		if (!ast_test_flag(p, H323_ALREADYGONE)) {
			ooHangCall(callToken, OO_REASON_LOCAL_BUSY, AST_CAUSE_USER_BUSY);
		ast_moh_start(ast, data, NULL);
		break;
	case AST_CONTROL_UNHOLD:
		ast_moh_stop(ast);
		break;
	case AST_CONTROL_PROGRESS:
		if (ast_channel_state(ast) != AST_STATE_UP) {
	    		if (!p->progsent) {
	     			if (gH323Debug) {
					ast_debug(1, "Sending manual progress for %s, res = %u\n", callToken, rres);
	     			p->progsent = 1;
	    		}
		}
	    break;
      case AST_CONTROL_RINGING:
		if (ast_channel_state(ast) == AST_STATE_RING || ast_channel_state(ast) == AST_STATE_RINGING) {
			if (!p->alertsent) {
				if (gH323Debug) {
					ast_debug(1, "Sending manual ringback for %s, res = %u\n", callToken, rres);
				}
				p->alertsent = 1;
	case AST_CONTROL_SRCUPDATE:
		if (p->rtp) {
			ast_rtp_instance_update_source(p->rtp);
		}
		break;
	case AST_CONTROL_SRCCHANGE:
		if (p->rtp) {
			ast_rtp_instance_change_source(p->rtp);
		}
	case AST_CONTROL_CONNECTED_LINE:
		if (!ast_channel_connected(ast)->id.name.valid
			|| ast_strlen_zero(ast_channel_connected(ast)->id.name.str)) {
			break;
		if (gH323Debug) {
			ast_debug(1, "Sending connected line info for %s (%s)\n",
				callToken, ast_channel_connected(ast)->id.name.str);
		ooSetANI(callToken, ast_channel_connected(ast)->id.name.str);
      case AST_CONTROL_T38_PARAMETERS:
		if (p->t38support != T38_ENABLED) {
			struct ast_control_t38_parameters parameters = { .request_response = 0 };
			parameters.request_response = AST_T38_REFUSED;
			ast_queue_control_data(ast, AST_CONTROL_T38_PARAMETERS,
						 &parameters, sizeof(parameters));
			break;
		}
		if (datalen != sizeof(struct ast_control_t38_parameters)) {
			ast_log(LOG_ERROR, "Invalid datalen for AST_CONTROL_T38. "
					   "Expected %d, got %d\n",
				(int)sizeof(enum ast_control_t38), (int)datalen);
		} else {
			const struct ast_control_t38_parameters *parameters = data;
			struct ast_control_t38_parameters our_parameters;
			enum ast_control_t38 message = parameters->request_response;
			switch (message) {

			case AST_T38_NEGOTIATED:
				if (p->faxmode) {
					res = 0;
					break;
				}
			case AST_T38_REQUEST_NEGOTIATE:

				if (p->faxmode) {
					/* T.38 already negotiated */
					our_parameters.request_response = AST_T38_NEGOTIATED;
					our_parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
					our_parameters.rate = AST_T38_RATE_14400;
					ast_queue_control_data(p->owner, AST_CONTROL_T38_PARAMETERS, &our_parameters, sizeof(our_parameters));
				} else if (!p->chmodepend) {
					p->chmodepend = 1;
					ooRequestChangeMode(p->callToken, 1);
				}
				break;

			case AST_T38_REQUEST_TERMINATE:

				if (!p->faxmode) {
					/* T.38 already terminated */
					our_parameters.request_response = AST_T38_TERMINATED;
					ast_queue_control_data(p->owner, AST_CONTROL_T38_PARAMETERS, &our_parameters, sizeof(our_parameters));
				} else if (!p->chmodepend) {
					p->chmodepend = 1;
					ooRequestChangeMode(p->callToken, 0);
			case AST_T38_REQUEST_PARMS:
				our_parameters.request_response = AST_T38_REQUEST_PARMS;
				our_parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
				our_parameters.rate = AST_T38_RATE_14400;
				ast_queue_control_data(p->owner, AST_CONTROL_T38_PARAMETERS, &our_parameters, sizeof(our_parameters));
				res = AST_T38_REQUEST_PARMS;
				break;
	case AST_CONTROL_PROCEEDING:
	case AST_CONTROL_PVT_CAUSE_CODE:
	case AST_CONTROL_MASQUERADE_NOTIFY:
	case -1:
		break;
	default:
		ast_log(LOG_WARNING, "Don't know how to indicate condition %d on %s\n",
									condition, callToken);
	}

   	ast_mutex_unlock(&p->lock);

	if (gH323Debug) {
		ast_verb(0, "++++  ooh323_indicate %d on %s is %d\n", condition, callToken, res);
	}
static int ooh323_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
{

	struct ooh323_pvt *p = (struct ooh323_pvt *) ast_channel_tech_pvt(ast);
	int res = -1;
	enum ast_t38_state state = T38_STATE_UNAVAILABLE;
	char* cp;

	if (!p) return -1;

	ast_mutex_lock(&p->lock);

	if (gH323Debug)
		ast_verb(0, "----- ooh323_queryoption %d on channel %s\n", option, ast_channel_name(ast));
	switch (option) {

		case AST_OPTION_T38_STATE:

			if (*datalen != sizeof(enum ast_t38_state)) {
				ast_log(LOG_ERROR, "Invalid datalen for AST_OPTION_T38_STATE option."
				" Expected %d, got %d\n", (int)sizeof(enum ast_t38_state), *datalen);
				break;
			}

			if (p->t38support != T38_DISABLED) {
				if (p->faxmode) {
					state = (p->chmodepend) ? T38_STATE_NEGOTIATING : T38_STATE_NEGOTIATED;
				} else {
					state = T38_STATE_UNKNOWN;
				}
			}

			*((enum ast_t38_state *) data) = state;
			res = 0;
			break;


		case AST_OPTION_DIGIT_DETECT:

			cp = (char *) data;
			*cp = p->vad ? 1 : 0;
			ast_debug(1, "Reporting digit detection %sabled on %s\n",
							 *cp ? "en" : "dis", ast_channel_name(ast));
		ast_verb(0, "+++++ ooh323_queryoption %d on channel %s\n", option, ast_channel_name(ast));
   	ast_mutex_unlock(&p->lock);
static int ooh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
{
	struct ooh323_pvt *p = ast_channel_tech_pvt(newchan);
	if (!p) return -1;


	ast_mutex_lock(&p->lock);
	if (p->owner != oldchan) {
		ast_log(LOG_WARNING, "Old channel wasn't %p but was %p\n", oldchan, p->owner);
		ast_mutex_unlock(&p->lock);
		return -1;
	}

	if (p->owner == oldchan) {
		p->owner = newchan;
	} else {
		p->owner = oldchan;
	}

	ast_mutex_unlock(&p->lock);

	if (gH323Debug)
void ooh323_set_write_format(ooCallData *call, struct ast_format *fmt, int txframes)
		ast_verb(0, "---   ooh323_update_writeformat %s/%d\n",
	p = find_call(call);
	if (!p) {
		ast_log(LOG_ERROR, "No matching call found for %s\n", call->callToken);
		return;
	}

	ast_mutex_lock(&p->lock);

		struct ast_format_cap *caps;

		caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
		if (!caps) {
			ast_log(LOG_ERROR, "Could not allocate capabilities structure\n");
			return;
		}

		while (p->owner && ast_channel_trylock(p->owner)) {
			ast_debug(1,"Failed to grab lock, trying again\n");
			DEADLOCK_AVOIDANCE(&p->lock);
		}
		if (!p->owner) {
			ast_mutex_unlock(&p->lock);
			ast_log(LOG_ERROR, "Channel has no owner\n");
			struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
			ast_verb(0, "Writeformat before update %s/%s\n",
			  ast_format_get_name(ast_channel_writeformat(p->owner)),
			  ast_format_cap_get_names(ast_channel_nativeformats(p->owner), &codec_buf));
		}

		if (p->dtmfmode & H323_DTMF_RFC2833 && p->dtmfcodec) {
			ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->rtp),
				 p->rtp, p->dtmfcodec, "audio", "telephone-event", 0);
		}
		if (p->dtmfmode & H323_DTMF_CISCO && p->dtmfcodec) {
			ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->rtp),
				 p->rtp, p->dtmfcodec, "audio", "cisco-telephone-event", 0);
		}

		if (txframes) {
			ast_format_cap_set_framing(caps, txframes);
		}
		ast_format_cap_append(caps, fmt, 0);
		ast_channel_nativeformats_set(p->owner, caps);
		ao2_ref(caps, -1);
	  	ast_set_write_format(p->owner, ast_channel_writeformat(p->owner));
	  	ast_set_read_format(p->owner, ast_channel_readformat(p->owner));
		ast_channel_unlock(p->owner);
   	} else
		ast_log(LOG_ERROR, "No owner found\n");
	ast_mutex_unlock(&p->lock);
	if (gH323Debug)
void ooh323_set_read_format(ooCallData *call, struct ast_format *fmt)
	if (gH323Debug)
		ast_verb(0, "---   ooh323_update_readformat %s\n",
	p = find_call(call);
	if (!p) {
		ast_log(LOG_ERROR, "No matching call found for %s\n", call->callToken);
		return;
	}

	ast_mutex_lock(&p->lock);
		struct ast_format_cap *caps;

		caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
		if (!caps) {
			ast_log(LOG_ERROR, "Could not allocate capabilities structure\n");
			return;
		}

		while (p->owner && ast_channel_trylock(p->owner)) {
			ast_debug(1,"Failed to grab lock, trying again\n");
			DEADLOCK_AVOIDANCE(&p->lock);
		}
		if (!p->owner) {
			ast_mutex_unlock(&p->lock);
			ast_log(LOG_ERROR, "Channel has no owner\n");
			ast_verb(0, "Readformat before update %s\n",
			  ast_format_get_name(ast_channel_readformat(p->owner)));
		}
		ast_format_cap_append(caps, fmt, 0);
		ast_channel_nativeformats_set(p->owner, caps);
		ao2_ref(caps, -1);
		ast_set_read_format(p->owner, ast_channel_readformat(p->owner));
		ast_channel_unlock(p->owner);
		ast_log(LOG_ERROR, "No owner found\n");

int onAlerting(ooCallData *call)
{
	struct ooh323_pvt *p = NULL;
	struct ast_channel *c = NULL;

	if (gH323Debug)
		ast_verb(0, "--- onAlerting %s\n", call->callToken);
   	p = find_call(call);
   	if(!p) {
		ast_log(LOG_ERROR, "No matching call found\n");
		return -1;
	if (!p->owner) {
		ast_debug(1, "Channel has no owner\n");
		return 0;
	}
	while (p->owner && ast_channel_trylock(p->owner)) {
		ast_debug(1, "Failed to grab lock, trying again\n");
		DEADLOCK_AVOIDANCE(&p->lock);
	}
	if (!p->owner) {
		ast_log(LOG_ERROR, "Channel has no owner\n");
		return 0;
	c = p->owner;

	if (call->remoteDisplayName) {
		struct ast_party_connected_line connected;
		struct ast_set_party_connected_line update_connected;

		memset(&update_connected, 0, sizeof(update_connected));
		update_connected.id.name = 1;
		ast_party_connected_line_init(&connected);
		connected.id.name.valid = 1;
		connected.id.name.str = (char *) call->remoteDisplayName;
		connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
		ast_channel_queue_connected_line_update(c, &connected, &update_connected);
	if (ast_channel_state(c) != AST_STATE_UP)
		ast_setstate(c, AST_STATE_RINGING);

	ast_queue_control(c, AST_CONTROL_RINGING);
      	ast_channel_unlock(c);
      	ast_mutex_unlock(&p->lock);
		ast_verb(0, "+++ onAlerting %s\n", call->callToken);
int onProgress(ooCallData *call)
{
	struct ooh323_pvt *p = NULL;
	struct ast_channel *c = NULL;

	if (gH323Debug)
		ast_verb(0, "--- onProgress %s\n", call->callToken);
   	p = find_call(call);
   	if(!p) {
		ast_log(LOG_ERROR, "No matching call found\n");
		return -1;
	ast_mutex_lock(&p->lock);
	if (!p->owner) {
		ast_mutex_unlock(&p->lock);
		ast_log(LOG_ERROR, "Channel has no owner\n");
		return 0;
	}
	while (p->owner && ast_channel_trylock(p->owner)) {
		ast_debug(1, "Failed to grab lock, trying again\n");
		DEADLOCK_AVOIDANCE(&p->lock);
	}
	if (!p->owner) {
		ast_mutex_unlock(&p->lock);
		ast_log(LOG_ERROR, "Channel has no owner\n");
		return 0;
	}
	c = p->owner;

	if (call->remoteDisplayName) {
		struct ast_party_connected_line connected;
		struct ast_set_party_connected_line update_connected;

		memset(&update_connected, 0, sizeof(update_connected));
		update_connected.id.name = 1;
		ast_party_connected_line_init(&connected);
		connected.id.name.valid = 1;
		connected.id.name.str = (char *) call->remoteDisplayName;
		connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
		ast_channel_queue_connected_line_update(c, &connected, &update_connected);
	if (ast_channel_state(c) != AST_STATE_UP)
		ast_setstate(c, AST_STATE_RINGING);

	ast_queue_control(c, AST_CONTROL_PROGRESS);
      	ast_channel_unlock(c);
      	ast_mutex_unlock(&p->lock);
		ast_verb(0, "+++ onProgress %s\n", call->callToken);
/**
  * Callback for sending digits from H.323 up to asterisk
  *
  */
int ooh323_onReceivedDigit(OOH323CallData *call, const char *digit)
{
	struct ooh323_pvt *p = NULL;
	struct ast_frame f;
	int res;

	ast_debug(1, "Received Digit: %c\n", digit[0]);
	p = find_call(call);
	if (!p) {
		ast_log(LOG_ERROR, "Failed to find a matching call.\n");
		return -1;
	}
	if (!p->owner) {
		ast_log(LOG_ERROR, "Channel has no owner\n");
		return -1;
	}
	ast_mutex_lock(&p->lock);
	memset(&f, 0, sizeof(f));
	f.frametype = AST_FRAME_DTMF;
	f.datalen = 0;
	f.samples = 800;
	f.offset = 0;
	f.data.ptr = NULL;
	f.mallocd = 0;
	f.src = "SEND_DIGIT";

	while (p->owner && ast_channel_trylock(p->owner)) {
		ast_debug(1, "Failed to grab lock, trying again\n");
		DEADLOCK_AVOIDANCE(&p->lock);
	}
	if (!p->owner) {
		ast_mutex_unlock(&p->lock);
		ast_log(LOG_ERROR, "Channel has no owner\n");
		return 0;
	}
	res = ast_queue_frame(p->owner, &f);
   	ast_channel_unlock(p->owner);
   	ast_mutex_unlock(&p->lock);
	return res;
}

int ooh323_onReceivedSetup(ooCallData *call, Q931Message *pmsg)
{
	struct ooh323_pvt *p = NULL;
	struct ooh323_user *user = NULL;
   	struct ast_channel *c = NULL;
	ooAliases *alias = NULL;
	char *at = NULL;
	char number [OO_MAX_NUMBER_LENGTH];

	if (gH323Debug)
		ast_verb(0, "---   ooh323_onReceivedSetup %s\n", call->callToken);


	if (!(p = ooh323_alloc(call->callReference, call->callToken))) {
		ast_log(LOG_ERROR, "Failed to create a new call.\n");
		return -1;
	}
	ast_mutex_lock(&p->lock);
	ast_clear_flag(p, H323_OUTGOING);
		p->callerid_name = ast_strdup(call->remoteDisplayName);
	}

	if (ooCallGetCallingPartyNumber(call, number, OO_MAX_NUMBER_LENGTH) == OO_OK) {
		p->callerid_num = ast_strdup(number);
	}

	if (call->remoteAliases) {
		for (alias = call->remoteAliases; alias; alias = alias->next) {
			if (alias->type == T_H225AliasAddress_h323_ID) {
				if (!p->callerid_name) {
					p->callerid_name = ast_strdup(alias->value);
				}
				ast_copy_string(p->caller_h323id, alias->value, sizeof(p->caller_h323id));
				}
         else if(alias->type == T_H225AliasAddress_dialedDigits)
         {
            if(!p->callerid_num)
               p->callerid_num = ast_strdup(alias->value);
				ast_copy_string(p->caller_dialedDigits, alias->value,
															sizeof(p->caller_dialedDigits));
         }
         else if(alias->type == T_H225AliasAddress_email_ID)
         {
				ast_copy_string(p->caller_email, alias->value, sizeof(p->caller_email));
         }
         else if(alias->type == T_H225AliasAddress_url_ID)
         {
				ast_copy_string(p->caller_url, alias->value, sizeof(p->caller_url));
			}
		}
	}

	number[0] = '\0';
   	if(ooCallGetCalledPartyNumber(call, number, OO_MAX_NUMBER_LENGTH)== OO_OK) {
      		ast_copy_string(p->exten, number, sizeof(p->exten));
		update_our_aliases(call, p);
		if (!ast_strlen_zero(p->callee_dialedDigits)) {
         		ast_copy_string(p->exten, p->callee_dialedDigits, sizeof(p->exten));
      		} else if(!ast_strlen_zero(p->callee_h323id)) {
			ast_copy_string(p->exten, p->callee_h323id, sizeof(p->exten));
      		} else if(!ast_strlen_zero(p->callee_email)) {
			ast_copy_string(p->exten, p->callee_email, sizeof(p->exten));
			if ((at = strchr(p->exten, '@'))) {
				*at = '\0';
			}
		}
	}

	/* if no extension found, set to default 's' */
	if (ast_strlen_zero(p->exten)) {
      		p->exten[0]='s';
      		p->exten[1]='\0';
      	user = find_user(p->callerid_name, call->remoteIP);
      	if(user && (user->incominglimit == 0 || user->inUse < user->incominglimit)) {
		ast_mutex_lock(&user->lock);
		p->username = ast_strdup(user->name);
 		p->neighbor.user = user->mUseIP ? ast_strdup(user->mIP) :
						  ast_strdup(user->name);
		ast_copy_string(p->context, user->context, sizeof(p->context));
		ast_copy_string(p->accountcode, user->accountcode, sizeof(p->accountcode));
		p->amaflags = user->amaflags;
		ast_format_cap_append_from_cap(p->cap, user->cap, AST_MEDIA_TYPE_UNKNOWN);
		p->g729onlyA = user->g729onlyA;
		p->dtmfmode |= user->dtmfmode;
		p->dtmfcodec = user->dtmfcodec;
		p->faxdetect = user->faxdetect;
		p->t38support = user->t38support;
		p->rtptimeout = user->rtptimeout;
		p->h245tunneling = user->h245tunneling;
		p->faststart = user->faststart;
		p->directrtp = user->directrtp;
		p->earlydirect = user->earlydirect;

		if (p->faststart)
         		OO_SETFLAG(call->flags, OO_M_FASTSTART);
		else
			OO_CLRFLAG(call->flags, OO_M_FASTSTART);
		/* if we disable h245tun for this user then we clear flag */
		/* in any other case we don't must touch this */
		/* ie if we receive setup without h245tun but enabled
		   				we can't enable it per call */
		if (!p->h245tunneling)
			OO_CLRFLAG(call->flags, OO_M_TUNNELING);

		if (user->rtpmask && user->rtpmaskstr[0]) {
			p->rtpmask = user->rtpmask;
			ast_copy_string(p->rtpmaskstr, user->rtpmaskstr,
							 sizeof(p->rtpmaskstr));
		if (user->rtdrcount > 0 && user->rtdrinterval > 0) {
			p->rtdrcount = user->rtdrcount;
			p->rtdrinterval = user->rtdrinterval;
		}
	 	if (user->incominglimit) user->inUse++;
		ast_mutex_unlock(&user->lock);
	} else {
	 if (!OO_TESTFLAG(p->flags,H323_DISABLEGK)) {
		p->username = ast_strdup(call->remoteIP);
		p->directrtp = gDirectRTP;
		p->earlydirect = gEarlyDirect;
	} else {
	  ast_mutex_unlock(&p->lock);
	  ast_log(LOG_ERROR, "Unacceptable ip %s\n", call->remoteIP);
	  if (!user) {
	   ooHangCall(call->callToken, ooh323_convert_hangupcause_asteriskToH323(AST_CAUSE_CALL_REJECTED), AST_CAUSE_CALL_REJECTED);
	   call->callEndReason = OO_REASON_REMOTE_REJECTED;
	  }
	  else {
	   ooHangCall(call->callToken, ooh323_convert_hangupcause_asteriskToH323(AST_CAUSE_NORMAL_CIRCUIT_CONGESTION), AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
	   call->callEndReason = OO_REASON_REMOTE_REJECTED;
	  }
	  ast_set_flag(p, H323_NEEDDESTROY);
	  return -1;
	 }
	ooh323c_set_capability_for_call(call, p->cap, p->dtmfmode, p->dtmfcodec,
					 p->t38support, p->g729onlyA);
/* Incoming call */
  	c = ooh323_new(p, AST_STATE_RING, p->username, 0, NULL, NULL);
  	if(!c) {
   	ast_mutex_unlock(&p->lock);
   	ast_log(LOG_ERROR, "Could not create ast_channel\n");
         return -1;
	if (!configure_local_rtp(p, call)) {
		ast_mutex_unlock(&p->lock);
		ast_log(LOG_ERROR, "Couldn't create rtp structure\n");
		return -1;
	}

	ast_mutex_unlock(&p->lock);

	if (gH323Debug)
		ast_verb(0, "+++   ooh323_onReceivedSetup - Determined context %s, "
int onOutgoingCall(ooCallData *call)
{
	struct ooh323_pvt *p = NULL;
	int i = 0;

	if (gH323Debug)
		ast_verb(0, "---   onOutgoingCall %lx: %s\n", (long unsigned int) call, call->callToken);

	if (!strcmp(call->callType, "outgoing")) {
		p = find_call(call);
		if (!p) {
      			ast_log(LOG_ERROR, "Failed to find a matching call.\n");
			return -1;
		}
		ast_mutex_lock(&p->lock);

		if (!ast_strlen_zero(p->callerid_name)) {
			ooCallSetCallerId(call, p->callerid_name);
		}
		if (!ast_strlen_zero(p->callerid_num)) {
			i = 0;
			while (*(p->callerid_num + i) != '\0') {
            			if(!isdigit(*(p->callerid_num+i))) { break; }
				i++;
			}
         		if(*(p->callerid_num+i) == '\0')
				ooCallSetCallingPartyNumber(call, p->callerid_num);
         		else {
            			if(!p->callerid_name)
					ooCallSetCallerId(call, p->callerid_num);
			}
		}
		if (!ast_strlen_zero(p->caller_h323id))
			ooCallAddAliasH323ID(call, p->caller_h323id);

		if (!ast_strlen_zero(p->caller_dialedDigits)) {
			if (gH323Debug) {
				ast_verb(0, "Setting dialed digits %s\n", p->caller_dialedDigits);
			}
			ooCallAddAliasDialedDigits(call, p->caller_dialedDigits);
		} else if (!ast_strlen_zero(p->callerid_num)) {
Josh Soref's avatar
Josh Soref committed
			if (ooIsDialedDigit(p->callerid_num)) {
				if (gH323Debug) {
					ast_verb(0, "setting callid number %s\n", p->callerid_num);
				}
				ooCallAddAliasDialedDigits(call, p->callerid_num);
			} else if (ast_strlen_zero(p->caller_h323id)) {
				ooCallAddAliasH323ID(call, p->callerid_num);
			}
		}
		if (p->rtpmask && p->rtpmaskstr[0]) {
			call->rtpMask = p->rtpmask;
			ast_mutex_lock(&call->rtpMask->lock);
			call->rtpMask->inuse++;
			ast_mutex_unlock(&call->rtpMask->lock);
			ast_copy_string(call->rtpMaskStr, p->rtpmaskstr, sizeof(call->rtpMaskStr));
		}

		if (!p->rtp && !configure_local_rtp(p, call)) {
			ast_mutex_unlock(&p->lock);
			return OO_FAILED;
		}

		ast_mutex_unlock(&p->lock);
	}

	if (gH323Debug)
		ast_verb(0, "+++   onOutgoingCall %s\n", call->callToken);
int onNewCallCreated(ooCallData *call)
{
	struct ooh323_pvt *p = NULL;
	int i = 0;

	if (gH323Debug)
		ast_verb(0, "---   onNewCallCreated %lx: %s\n", (long unsigned int) call, call->callToken);

   	ast_mutex_lock(&call->Lock);
   	if (ooh323c_start_call_thread(call)) {
    		ast_log(LOG_ERROR,"Failed to create call thread.\n");
    		ast_mutex_unlock(&call->Lock);
    		return -1;
   	}

	if (!strcmp(call->callType, "outgoing")) {
		p = find_call(call);
		if (!p) {
      			ast_log(LOG_ERROR, "Failed to find a matching call.\n");
			ast_mutex_unlock(&call->Lock);
		if (!ast_strlen_zero(p->callerid_name)) {
			ooCallSetCallerId(call, p->callerid_name);
		}
		if (!ast_strlen_zero(p->callerid_num)) {
			i = 0;
			while (*(p->callerid_num + i) != '\0') {
            			if(!isdigit(*(p->callerid_num+i))) { break; }
         		if(*(p->callerid_num+i) == '\0')
				ooCallSetCallingPartyNumber(call, p->callerid_num);
            			if(ast_strlen_zero(p->callerid_name))
					ooCallSetCallerId(call, p->callerid_num);
			}
		}
		if (!ast_strlen_zero(p->caller_h323id))
			ooCallAddAliasH323ID(call, p->caller_h323id);

		if (!ast_strlen_zero(p->caller_dialedDigits)) {
			if (gH323Debug) {
				ast_verb(0, "Setting dialed digits %s\n", p->caller_dialedDigits);
			}
			ooCallAddAliasDialedDigits(call, p->caller_dialedDigits);
		} else if (!ast_strlen_zero(p->callerid_num)) {
Josh Soref's avatar
Josh Soref committed
			if (ooIsDialedDigit(p->callerid_num)) {
					ast_verb(0, "setting callid number %s\n", p->callerid_num);
				}
				ooCallAddAliasDialedDigits(call, p->callerid_num);
			} else if (ast_strlen_zero(p->caller_h323id)) {
				ooCallAddAliasH323ID(call, p->callerid_num);
			}
		}
Josh Soref's avatar
Josh Soref committed
			if (ooIsDialedDigit(p->exten)) {
				ooCallSetCalledPartyNumber(call, p->exten);
				ooCallAddRemoteAliasDialedDigits(call, p->exten);
			} else {
			  ooCallAddRemoteAliasH323ID(call, p->exten);
			}
		}

		if (gH323Debug) {
			struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
			ast_verb(0, " Outgoing call %s(%s) - Codec prefs - %s\n",
				p->username?p->username:"NULL", call->callToken,
				ast_format_cap_get_names(p->cap, &codec_buf));
      		ooh323c_set_capability_for_call(call, p->cap,
                                     p->dtmfmode, p->dtmfcodec, p->t38support, p->g729onlyA);
		configure_local_rtp(p, call);
		ast_cond_signal(&p->rtpcond);

   	ast_mutex_unlock(&call->Lock);
		ast_verb(0, "+++   onNewCallCreated %s\n", call->callToken);
	return OO_OK;
}

int onCallEstablished(ooCallData *call)
{
	struct ooh323_pvt *p = NULL;

	if (gH323Debug)
		ast_verb(0, "---   onCallEstablished %s\n", call->callToken);
	if (!(p = find_call(call))) {
		ast_log(LOG_ERROR, "Failed to find a matching call.\n");
		return -1;
	}
   	if(ast_test_flag(p, H323_OUTGOING)) {
		if (!p->owner) {
			ast_mutex_unlock(&p->lock);
			ast_log(LOG_ERROR, "Channel has no owner\n");
			return -1;
		}
Loading
Loading full blame...