📄 transnet.c
字号:
/***********************************************************************
Copyright (c) 2002 RADVISION Ltd.
************************************************************************
NOTICE:
This document contains information that is confidential and proprietary
to RADVISION Ltd.. No part of this document may be reproduced in any
form whatsoever without written prior approval by RADVISION Ltd..
RADVISION Ltd. reserve the right to revise this publication and make
changes without obligation to notify any person of such revisions or
changes.
***********************************************************************/
#include "rvinternal.h"
#include "transport.h"
#include "transportint.h"
#include "transutil.h"
#include "transStates.h"
#include "q931asn1.h"
#include "transnet.h"
#include "emanag.h"
#include "cmutils.h"
#include "prnutils.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus*/
/**************************************************************************************
* createHostByType
*
* Purpose: To allocate a new host element and fill it with data according to its
* type, i.e. Q.931-TPKT, Q.931-Annex E, H.245
*
* Input: transGlobals - The global variables of the module
* hsTranSession - The session for which the host is created (may be NULL)
* type - The type of the connection (TPKT, H.245, Annex E)
* hTpkt - The connection handle in TPKT for a new incoming connection
* annexEUsageMode - is this an annex E host or other, i.e. TPKT for Q.931 or H.245
*
* Output: hsTransHost - A handle for the newly created host element.
*
**************************************************************************************/
TRANSERR createHostByType( cmTransGlobals *transGlobals,
HSTRANSSESSION hsTransSession,
HSTRANSHOST *hsTransHost,
TRANSCONNTYPE type,
HTPKT hTpkt,
cmAnnexEUsageMode annexEUsageMode)
{
cmTransSession *session = (cmTransSession *)hsTransSession;
cmTransHost *host;
cmTransportAddress local, remote;
TRANSERR res;
/* allocate a new host element */
*hsTransHost = (HSTRANSHOST)emaAdd(transGlobals->hEmaHosts, NULL);
if (!*hsTransHost)
{
RvLogError(&transGlobals->hLog,
(&transGlobals->hLog, "createHostByType failed on allocating host element"));
return cmTransErr;
}
else
{
host = (cmTransHost *)*hsTransHost;
memset(host, 0, sizeof(cmTransHost));
RvLogInfo(&transGlobals->hLog,
(&transGlobals->hLog, "createHostByType created host=%d(%p)",emaGetIndex((EMAElement)host),host));
}
local.type = (cmTransportType)-1;
local.ip = 0;
remote.type = (cmTransportType)-1;
remote.ip = 0;
/* set the params of the new host */
switch (type)
{
case cmTransQ931Conn:
{
/* The host doesn't have a session attached to it yet,
so fill it with default params and data from the new
incoming connection */
if (annexEUsageMode != cmTransNoAnnexE)
{
annexEStatus eStat;
/* set parameters for the newly created host */
host->closeOnNoSessions = RV_FALSE;
host->isMultiplexed = RV_TRUE;
host->remoteCloseOnNoSessions = RV_FALSE;
host->remoteIsMultiplexed = RV_TRUE; /* Annex E is always multiplexed */
host->annexEUsageMode = cmTransUseAnnexE;
host->firstSession = NULL;
host->state = hostConnected;
host->type = type;
host->incomingMessage = NULL;
host->hTpkt = NULL;
/* get the local address of the annex E */
eStat = annexEGetLocalAddress(transGlobals->annexECntrl, &local);
if (eStat != annexEStatusNormal)
{
RvLogError(&transGlobals->hLog,
(&transGlobals->hLog, "createHostByType failed on getting annex E local address"));
}
}
else
{
host->closeOnNoSessions = (session? session->closeOnNoSessions : RV_FALSE);
host->isMultiplexed = (session? session->isMultiplexed : RV_TRUE);
host->remoteCloseOnNoSessions = host->closeOnNoSessions;
host->remoteIsMultiplexed = (session ? !session->outgoing : RV_TRUE); /* Assume non-multiplexed for outgoing connections until we know better */
host->annexEUsageMode = cmTransNoAnnexE;
host->firstSession = NULL;
host->state = hostIdle;
host->type = type;
host->incomingMessage = NULL;
host->hTpkt = hTpkt;
if (hTpkt)
{
tpktGetAddresses(hTpkt, &local, &remote);
}
}
break;
}
case cmTransH245Conn:
{
/* must always have a session on whose behalf either the connect or the
listen was done */
host->closeOnNoSessions = RV_TRUE;
host->isMultiplexed = RV_FALSE;
host->remoteCloseOnNoSessions = RV_TRUE;
host->remoteIsMultiplexed = RV_FALSE;
host->annexEUsageMode = cmTransNoAnnexE;
host->firstSession = session;
host->state = hostIdle;
host->type = type;
host->hTpkt = hTpkt;
if (hTpkt)
{
tpktGetAddresses(hTpkt, &local, &remote);
}
break;
}
}
/* update the local and remote addresses of the host */
host->localAddress = local;
res = setRemoteAddress(host, &remote);
if (res != cmTransOK)
{
RvLogError(&transGlobals->hLog,
(&transGlobals->hLog, "createHostByType failed on setting host remote address"));
closeHostInternal(*hsTransHost, RV_TRUE);
return cmTransErr;
}
/* return the newly created host element */
if (hsTransHost)
*hsTransHost = (HSTRANSHOST)host;
return cmTransOK;
}
/**************************************************************************************
* determineWinningHost
*
* Purpose: check if there was a competition between TPKT and annex E, and if so
* close the Annex E.
*
* Input: transGlobals - The global variables of the module
* session - The session for which the conection was made
* annexEWon - which of the two hosts won..
*
**************************************************************************************/
RvBool determineWinningHost(cmTransGlobals *transGlobals, cmTransSession *session, RvBool annexEWon)
{
cmTransHost *loosingHost = NULL;
cmTransHost *winningHost = NULL;
RvBool answer = RV_TRUE;
HATRANSHOST winHaTransHost = NULL;
HATRANSHOST looseHaTransHost = NULL;
if (!transGlobals)
return answer;
if (annexEWon)
{
winningHost = session->annexEConnection;
loosingHost = session->Q931Connection;
}
else
{
winningHost = session->Q931Connection;
loosingHost = session->annexEConnection;
}
/* check if we're not too late */
if ( (loosingHost) && (loosingHost->hostIsApproved) && (!winningHost->hostIsApproved) )
{
cmTransHost *tempHost;
/* reversal of fortunes */
tempHost = loosingHost;
loosingHost = winningHost;
winningHost = tempHost;
annexEWon = !annexEWon;
answer = RV_FALSE;
}
/* print the result */
if (annexEWon)
{
RvLogInfo(&transGlobals->hLog,
(&transGlobals->hLog, "determineWinningHost: annex E has won (host=%d-%p)",
emaGetIndex((EMAElement)session->annexEConnection), session->annexEConnection));
}
else
{
RvLogInfo(&transGlobals->hLog,
(&transGlobals->hLog, "determineWinningHost: TPKT has won (host=%d-%p)",
emaGetIndex((EMAElement)session->Q931Connection), session->Q931Connection));
}
/* disconnect the loosing host and mark the winning one */
{
winningHost->hostIsApproved = RV_TRUE;
if (emaLock((EMAElement)loosingHost))
{
looseHaTransHost = (HATRANSHOST)emaGetApplicationHandle((EMAElement)loosingHost);
if ( annexEWon )
{
RvMutexLock(&transGlobals->lockSessionsList);
/* disconnect it from the loosing host */
loosingHost->firstSession = session->nextSession;
if (session->nextSession)
session->nextSession->prevSession = NULL;
RvMutexUnlock(&transGlobals->lockSessionsList);
}
if ( (!loosingHost->firstSession) &&
(loosingHost->closeOnNoSessions) )
{
/* totally kill the host */
closeHostInternal((HSTRANSHOST)loosingHost, RV_FALSE);
}
emaUnlock((EMAElement)loosingHost);
}
RvMutexLock(&transGlobals->lockSessionsList);
if ( annexEWon )
{
if (!session->connectedToHost)
{
/* connect the session to the winning host */
if (session != winningHost->firstSession)
{
session->nextSession = winningHost->firstSession;
if (session->nextSession)
session->nextSession->prevSession = session;
winningHost->firstSession = session;
}
else
printf("transnet(1):insert same seesion as firstSession:%x\n", session);
session->annexEUsageMode = cmTransUseAnnexE;
session->connectedToHost = RV_TRUE;
}
}
else
session->annexEUsageMode = cmTransNoAnnexE;
/* save the application handle of the loosing host */
winHaTransHost = (HATRANSHOST)emaGetApplicationHandle((EMAElement)winningHost);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -