⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rasutils.c

📁 基于h323协议的软phone
💻 C
📖 第 1 页 / 共 3 页
字号:
{
    cmHookSendToT   sendHook;
    int             status = 0;
    int             addrNodeId;
    int             encodedSize = 0;
    RvBool          process, printMsg;
    RvPstFieldId    fieldId;
    if (isMulticast);

    sendHook = cmiGetRasHooks(ras->app)->hookSendTo;
    printMsg = RvLogIsSelected(&ras->logChan, RV_LOGLEVEL_DEBUG);

    if (printMsg || (sendHook != NULL))
    {
        /* Create a transport address node id */
        /* todo: Don't use cmElem */
        addrNodeId = pvtAddRoot(ras->hVal, ((cmElem*)(ras->app))->hAddrSyn, 0, NULL);
        if (addrNodeId < 0) return addrNodeId;
        cmTAToVt(ras->hVal, addrNodeId, destAddr);
    }
    else
        addrNodeId = RV_ERROR_UNKNOWN;

    /* Make sure to notify the application of an outgoing RAS message */
    if (ras->evMessages.cmEvRasMessageSend != NULL)
    {
        /* An internal callback - no log for this one */
        ras->evMessages.cmEvRasMessageSend(nodeId, stage, rasTx, ras->evMessages.hAppRasMsg);
    }

    /* Call the hook before we encode this message */
    if (sendHook != NULL)
        process = !sendHook((HPROTCONN)&ras->unicastAppHandle, nodeId, addrNodeId, RV_FALSE);
    else
        process = RV_TRUE;

    /* Make sure we're still sending out this message */
    if (process)
    {
        RvUint8*           buffer;

        getEncodeDecodeBuffer((int)ras->bufferSize, &buffer);

        /* Encode the message in PER */
        if (ras->cmiEvRASSendRawMessage)
        {
            /* We've got an encoding callback - probably from security app. Call it */
            status = ras->cmiEvRASSendRawMessage(
                ras->app,
                (HPROTCONN)&ras->unicastAppHandle,
                rasTx,
                (HAPPRAS)emaGetApplicationHandle((EMAElement)rasTx),
                nodeId,
                (int)ras->bufferSize,
                buffer,
                &encodedSize);
        }
        else
        {
            /* We encode the message on our own */
            status = cmEmEncode(ras->hVal, nodeId, buffer, (int)ras->bufferSize, &encodedSize);
        }

        if (status >= 0)
        {
            if (storeInRPOOL)
            {
                /* Make sure we store this message somewhere */
                *rpoolHandle = rpoolAllocCopyExternal(ras->messages, buffer, encodedSize);
            }

            /* Print the message */
            pvtGet(ras->hVal, pvtChild(ras->hVal, nodeId), &fieldId, NULL, NULL, NULL);
            RvLogInfo(&ras->logChan,
                (&ras->logChan, "New message (channel %d)  sent --> %s:",
                     isMulticast, pstGetFieldNamePtr(ras->synMessage, fieldId)));

            if (printMsg)
            {
                RvLogTextDebug(&ras->logChan, "Address:");
                pvtPrintInternal(ras->hVal, addrNodeId, &ras->logChan, msGetDebugLevel());
                RvH323CmPrintMessage(&ras->logChan, "Message:",
                    ras->hVal, nodeId, buffer, encodedSize, RV_FALSE);
            }

            /* Send the message. We always send through the unicast port */
            status = ras->evSendMessage(ras->app, rasChanUnicast, destAddr, buffer, (RvSize_t)encodedSize);
            if (status < 0)
            {
                RvLogError(&ras->log,
                    (&ras->log, "rasEncodeAndSend: Couldn't send the message 0x%p: %d", rasTx, status));
            }
        }
        else
        {
            pvtGet(ras->hVal, pvtChild(ras->hVal, nodeId), &fieldId, NULL, NULL, NULL);
            RvLogInfo(&ras->logChan,
                (&ras->logChan, "New message (channel %d) Not sent (error) --> %s:",
                     isMulticast, pstGetFieldNamePtr(ras->synMessage, fieldId)));

            if (printMsg)
            {
                RvLogTextDebug(&ras->logChan, "Address:");
                pvtPrintInternal(ras->hVal, addrNodeId, &ras->logChan, msGetDebugLevel());
                RvH323CmPrintMessage(&ras->logChan, "Message:",
                    ras->hVal, nodeId, buffer, -1, RV_FALSE);
            }

            RvLogError(&ras->log,
                (&ras->log, "rasEncodeAndSend: Failed to encode the message 0x%p: %d", rasTx, status));
        }
    }
    else
    {
        RvLogDebug(&ras->log,
            (&ras->log, "rasEncodeAndSend: Application doesn't want this message processed (root=%d)", nodeId));
    }

    /* Make sure we clear any nodes needed for address printing */
    if (addrNodeId >= 0)
        pvtDelete(ras->hVal, addrNodeId);

    if (status > 0)
        status = 0; /* Make sure return value is not positive - gatekeeper depends on it */
    return status;
}


/************************************************************************
 * rasDecodeAndRecv
 * purpose: Decode and receive a message from the net
 *          This function is called after we already know if its an incoming
 *          or outgoing message.
 *          It returns as parameters the sequence number and message type,
 *          allowing the caller to know if these parameters were changed in
 *          the hook function to the application
 * input  : ras             - RAS instance handle
 *          messageBuf      - Message buffer
 *          messageLength   - Length of received message
 *          isMulticast     - Are we sending it to a multicast address
 *          srcAddr         - Source address
 * output : srcAddr         - Reply address if found inside the message
 *          nodeId          - Root where we placed the message
 *          messageType     - Message type after hook
 *          requestSeqNum   - Sequence number in decoded message after hook
 * return : RV_TRUE if message should be processed
 *          RV_FALSE if message souldn't be processed
 *          Negative value on failure
 ************************************************************************/
int rasDecodeAndRecv(
    IN     rasModule*          ras,
    IN     RvUint8*            messageBuf,
    IN     RvUint32            messageLength,
    IN     RvBool              isMulticast,
    INOUT  cmTransportAddress* srcAddr,
    OUT    int*                nodeId,
    OUT    rasMessages*        messageType,
    OUT    RvUint32*           requestSeqNum,
    OUT    void**              hMsgContext)
{
    cmHookRecvFromT recvHook;
    HPROTCONN       protConn;
    int             status = 0;
    int             addrNodeId, replyNodeId, msgNodeId;
    int             bytesDecoded = 0;
    RvBool          process, printMsg;
    RvPstFieldId    fieldId;

    *nodeId = pvtAddRoot(ras->hVal, ras->synMessage, 0, NULL);
    if ((*nodeId) < 0)
        return *nodeId;

    if (isMulticast)
        protConn = (HPROTCONN)&ras->multicastAppHandle;
    else
        protConn = (HPROTCONN)&ras->unicastAppHandle;

    /* Decode the message */
    if (ras->cmiEvRASNewRawMessage)
    {
        /* We've got a decoding callback - probably from security app. Call it */
        RvLogInfo(&ras->log,
            (&ras->log, "cmEvRASNewRawMessage(protConn = %p, pvt=%d)",
                protConn, *nodeId));

        status = ras->cmiEvRASNewRawMessage(
                                    (HAPP)ras->app,
                                    protConn,
                                    *nodeId, messageBuf, (int)messageLength, &bytesDecoded,
                                    hMsgContext);
    }
    else
    {
        /* Let's decode it on our own */
        status = cmEmDecode(ras->hVal, *nodeId, messageBuf, (int)messageLength, &bytesDecoded);
    }

    if ((status >= 0) && ((int)messageLength != bytesDecoded))
    {
        RvH323CmPrintMessage(&ras->logChan, "rasDecodeAndRecv: Bad message length:",
            ras->hVal, -1, messageBuf, bytesDecoded, RV_TRUE);
        pvtDelete(ras->hVal, *nodeId);
        return RV_ERROR_UNKNOWN;
    }
    if (status < 0)
    {
        pvtDelete(ras->hVal, *nodeId);
        RvH323CmPrintMessage(&ras->logChan, "Binary:",
            ras->hVal, -1, messageBuf, bytesDecoded, RV_TRUE);
        return status;
    }

    recvHook = cmiGetRasHooks(ras->app)->hookRecvFrom;
    printMsg = RvLogIsSelected(&ras->logChan, RV_LOGLEVEL_DEBUG);

    if (printMsg || (recvHook != NULL))
    {
        /* Create a transport address node id */
        addrNodeId = pvtAddRoot(ras->hVal, ((cmElem*)(ras->app))->hAddrSyn, 0, NULL);
        if (addrNodeId < 0)
        {
            pvtDelete(ras->hVal, *nodeId);
            return addrNodeId;
        }
        cmTAToVt(ras->hVal, addrNodeId, srcAddr);
    }
    else
        addrNodeId = RV_ERROR_UNKNOWN;

    /* Call the hook after decoding this message */
    if (recvHook != NULL)
    {
        process = !recvHook(protConn, *nodeId, addrNodeId, isMulticast, RV_FALSE);
    }
    else
        process = RV_TRUE;

    /* Make sure we still want to receive this message */
    if (process)
    {
        int tmpNodeId;

        /* Print the message */
        pvtGet(ras->hVal, pvtChild(ras->hVal, *nodeId), &fieldId, NULL, NULL, NULL);
        RvLogInfo(&ras->logChan,
            (&ras->logChan, "New message (channel %d)  recv <-- %s:",
                 isMulticast, pstGetFieldNamePtr(ras->synMessage, fieldId)));

        if (printMsg)
        {
            RvLogTextDebug(&ras->logChan, "Address:");
            pvtPrintInternal(ras->hVal, addrNodeId, &ras->logChan, msGetDebugLevel());
            RvH323CmPrintMessage(&ras->logChan, "Binary:",
                ras->hVal, *nodeId, messageBuf, bytesDecoded, RV_TRUE);
        }

        /* Get the index and sequence number again */
        msgNodeId = pvtGetByIndex(ras->hVal, *nodeId, 1, NULL);
        if (msgNodeId >= 0)
            *messageType = (rasMessages)(pvtGetSyntaxIndex(ras->hVal, msgNodeId) - 1);
        tmpNodeId = pvtGetChild(ras->hVal, msgNodeId, __q931(requestSeqNum), NULL);
        pvtGet(ras->hVal, tmpNodeId, NULL, NULL, (RvInt32*)requestSeqNum, NULL);

        /* See if we've got any specific reply address in the message */
        switch (*messageType)
        {
            case rasMsgRegistrationRequest:
                __pvtGetNodeIdByFieldIds(replyNodeId, ras->hVal, msgNodeId, {_q931(rasAddress) _nul(1) LAST_TOKEN});
                break;

            case rasMsgGatekeeperRequest:
            case rasMsgInfoRequestResponse:
                replyNodeId = pvtGetChild(ras->hVal, msgNodeId, __q931(rasAddress), NULL);
                break;

            case rasMsgLocationRequest:
            case rasMsgInfoRequest:
                replyNodeId = pvtGetChild(ras->hVal, msgNodeId, __q931(replyAddress), NULL);
                break;

            default:
                replyNodeId = -1;
        }
        if (replyNodeId >= 0)
            cmVtToTA(ras->hVal, replyNodeId, srcAddr);
    }
    else
    {
        RvLogDebug(&ras->log,
            (&ras->log, "rasDecodeAndRecv: Application doesn't want this message processed (root=%d)", *nodeId));
        pvtDelete(ras->hVal, *nodeId);
        *nodeId = -1;
    }

    if (addrNodeId >= 0)
        pvtDelete(ras->hVal, addrNodeId);

    return (process?0:-1);   /* Look! I made a smiley-thingy! */
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -