cmpppack.c

来自「用c/c++实现的一个CMPP API」· C语言 代码 · 共 2,267 行 · 第 1/5 页

C
2,267
字号
/***************************************************************************  Copyright    : 2002, ASPIRE TECHNOLOGIES (SHENZHEN) LTD.  Program ID   : CMPPAPI.C  Description  : CMPPAPI定义的成员函数  Version      : CMPPAPI 1.5  Functions    : 内部函数  Modification Log:       DATE         AUTHOR          DESCRIPTION --------------------------------------------------------------------------       11/13/2002   chenggang       Create***************************************************************************/#include "cmppapi.h"#include "datatype.h"#include "cmpppack.h"#include "seterrno.h"#include "log.h"/************************************************************************  Function ID:  nIsCorrectLen  Description:  判断消息包中的长度是否符合协议中规定的长度  Input Param:  unsigned int unCmdId  				unsigned int unMsgLen  Output Param:   Return:       int             0     success                                -1    failed                                *************************************************************************/int nIsCorrectLen(unsigned int unCmdId,unsigned int unMsgLen){	int nRet = -1;		switch(unCmdId)	{		case MacC_ConnectRqst:        {            if(unMsgLen == MacHeadLen + MacConnectLen)            {            	nRet = 0;            }            break;        }        case MacC_ConnectRep:        {            if(unMsgLen == MacHeadLen + MacConnectResLen)            {            	nRet = 0;            }            break;        }        case MacC_SubmitRqst:        {            if((unMsgLen >= MacHeadLen + MacMinSubmitLen)&&               (unMsgLen < MacHeadLen + MacMaxSubmitLen))            {            	nRet = 0;            }            break;        }        case MacC_SubmitRep:        {           if(unMsgLen == MacHeadLen + MacSubmitResLen)            {            	nRet = 0;            }            break;        }        case MacC_DeliverRqst:        {            if((unMsgLen >= MacHeadLen + MacMinDeliverLen)&&               (unMsgLen < MacHeadLen + MacMaxDeliverLen))            {            	nRet = 0;            }            break;        }        case MacC_DeliverRep:        {            if(unMsgLen == MacHeadLen + MacDeliverResLen)            {            	nRet = 0;            }            break;        }        case MacC_QueryRqst:        {            if(unMsgLen == MacHeadLen + MacQueryLen)            {            	nRet = 0;            }            break;        }        case MacC_QueryRep:        {            if(unMsgLen == MacHeadLen + MacQueryResLen)            {            	nRet = 0;            }            break;        }        case MacC_CancelRqst:        {            if(unMsgLen == MacHeadLen + MacCancelLen)            {            	nRet = 0;            }            break;        }        case MacC_CancelRep:        {            if(unMsgLen == MacHeadLen + MacCancelResLen)            {            	nRet = 0;            }            break;        }        case MacC_ActiveRqst:        {            if(unMsgLen == MacHeadLen + MacActiveTestLen)            {            	nRet = 0;            }            break;        }        case MacC_ActiveRep:        {            if(unMsgLen == MacHeadLen + MacActiveTestResLen)            {            	nRet = 0;            }        }        default:        {            break;        } 	}	return nRet;}/************************************************************************  Function ID:  nVerifyConnect  Description:  CMPP协议中Connect pack 合法性校验  Input Param:  recCmppConnect* prCmppConnect  Output Param:   Return:       int             0     success                                -1    failed                                *************************************************************************/int nVerifyConnect(recCmppConnect* prCmppConnect){	if(NULL == prCmppConnect)	{		return -1;	}	return 0;}/************************************************************************  Function ID:  nConnectReqDecode  Description:  CMPP协议的Connect pack 解析函数  Input Param:  unsigned char*  psDecodeStr  Output Param: recCmppConnect* prCmppConnect  Return:       int             0     success                                <0    failed                                *************************************************************************/int nConnectReqDecode(unsigned char* psDecodeStr, recCmppConnect* prCmppConnect){    int nRet;    unsigned char* psHead;    int nTmp;        if((NULL == psDecodeStr) ||(NULL == prCmppConnect))    {        return -1;    }        psHead = psDecodeStr;    /* 1 Name: Source_Addr(Sp_Id) length: 6 */    memcpy(prCmppConnect->chSourceAddr, psHead, MacSourceAddress);    psHead = psHead + MacSourceAddress;    /* 2 Name: Authenticator Source length: 16 */    memcpy(prCmppConnect->chAuthSource, psHead, MacAuthSource);    psHead = psHead + MacAuthSource;    /* 3 Name: Version length : 1 */    prCmppConnect->ucVersion = *psHead;    psHead ++;    /* 4 Name: Timestamp length: sizeof(int) */    memcpy(&nTmp, psHead, sizeof(int));    prCmppConnect->unTimestamp = nTmp;       nRet = nVerifyConnect(prCmppConnect);    if(nRet != 0)    {    	return -2;    }        return 0;}/************************************************************************  Function ID:  nVerifyConnectResp  Description:  CMPP协议中ConnectResp pack 合法性校验  Input Param:  recCmppConnectResp* prCmppConnectResp  Output Param:   Return:       int             0     success                                -1    failed                                *************************************************************************/int nVerifyConnectResp(recCmppConnectResp* prCmppConnectResp){	int nStatus;	if(NULL == prCmppConnectResp)	{		return -1;	}	/* Check status */	nStatus = prCmppConnectResp->unStatus;		if(nStatus < 0)	{		return -2;	}	/* Check AuthenticatorIsmg == NULL when status == 3 *//*	if( 3 == nStatus)	{		if(prCmppConnectResp->chAuthIsmg[0] != 0)		{			return -1;		}	}*/		return 0;}/************************************************************************  Function ID:  nConnectRespDecode  Description:  CMPP协议的ConnectResp pack 解析函数  Input Param:  unsigned char*      psDecodeStr  Output Param: recCmppConnectResp* prCmppConnectResp  Return:       int             0   success                                -1  failed                                *************************************************************************/int nConnectRespDecode(unsigned char* psDecodeStr, recCmppConnectResp* prCmppConnectResp){    int nRet;    unsigned char* psHead;    unsigned int nTmp;    if((NULL == psDecodeStr) ||(NULL == prCmppConnectResp))    {        return -1;    }        psHead = psDecodeStr;    /* 1 Name: Resp state  lenght: 4 */    memcpy(&nTmp, psHead, MacIntLen);    prCmppConnectResp->unStatus = ntohl(nTmp);    psHead = psHead + MacIntLen;        /* 2 Name: Auth Ismg length :16*/    memcpy(prCmppConnectResp->chAuthIsmg, psHead, MacAuthIsmg);    psHead = psHead + MacAuthIsmg;    /* 3 Name: Version length: 1 */    prCmppConnectResp->ucVersion = *psHead;	nRet = nVerifyConnectResp(prCmppConnectResp);	if(nRet != 0)	{		return -2;	}	    return 0;}/************************************************************************  Function ID:  nVerifySubmit  Description:  CMPP协议中Submit pack 合法性校验  Input Param:  recCmppConnectResp* prCmppConnectResp  Output Param:   Return:       int             0     success                                -1    failed                                *************************************************************************/int nVerifySubmit(recCmppSubmit* prCmppSubmit){	int nRegiterDeliver;	int nFeeUserType;	int nTerminalIdType;	char sTmp[256];	int nFeeType;	/*int nTp_pId;	int nTp_udhi;	int nMsgFmt;	*/	//int nMsgFmt;	if(NULL == prCmppSubmit)	{		return -1;	}	/* Check Registered_delivery */	nRegiterDeliver = (int)prCmppSubmit->ucRegisterDelivery;	if((nRegiterDeliver < 0) || (nRegiterDeliver > 2))	{		vSetErrorDetail("RegiterDeliver is error value  when  Submit pack  Encode");			return -2;	}	/* Check mutex between Fee_UserType and Fee_terminal_ID */	nFeeUserType = (int)prCmppSubmit->ucFeeUserType;	//modify by lijian 2003-12-11 修改伪码问题	if( prCmppSubmit->ucFeeTerminalType != 1)	{		if(strlen(prCmppSubmit->chFeeTerminateId) > 0)		{			if(nFeeUserType != 3)			{				vSetErrorDetail("FeeUserType is error value  when  Submit pack  Encode,feeUserType is [%d]",nFeeUserType);				return -2;			}		}		else		{			if(nFeeUserType != 0)			{				vSetErrorDetail("FeeUserType is error value  when  Submit pack  Encode,feeUserType is [%d]",nFeeUserType);				return -2;			}		}	}	else	{		if(nFeeUserType != 3)		{			vSetErrorDetail("FeeUserType is error value  when  Submit pack  Encode,ucFeeTerminalType = 1 feeUserType is [%d]",nFeeUserType);			return -2;		}	}       nTerminalIdType = (int)prCmppSubmit->ucFeeTerminalType;       if(nTerminalIdType < 0 || nTerminalIdType >1)       {       	vSetErrorDetail("FeeTerminalType is error value  when  Submit pack  Encode");		return -2;       }	/* Check mutex between Msg_Fmt and Tp_pId and Tp_udhi */	/*nTp_pId  = (int)prCmppSubmit->ucTpPid;	nTp_udhi = (int)prCmppSubmit->ucTpUdhi;	nMsgFmt = (int)prCmppSubmit->ucMsgFmt;	if(nMsgFmt == MSG_FORMAT_BINARY)	{		if( (nTp_pId !=1)  || (nTp_udhi != 1) )		{			return -2;		}	}	else	{		if( (nTp_pId !=0) || (nTp_udhi!= 0) )		{			return -2;		}	}	*/	/* Check FeeType */	memset(sTmp, 0, sizeof(sTmp));	memcpy(sTmp, prCmppSubmit->chFeeType, MacFeeType);	nFeeType = atoi(sTmp);	if((nFeeType < 1) || (nFeeType  > 5))	{		if((nFeeType != 8)&&(nFeeType != 9))		{			vSetErrorDetail("FeeType  is error value  when  Submit pack  Encode");			return -2;		}	}	/* Check Dest_terminalType */	nTerminalIdType = (int)prCmppSubmit->ucDstTerminalType;       if(nTerminalIdType < 0 || nTerminalIdType >1)       {       	vSetErrorDetail("DstTerminalType is error value  when  Submit pack  Encode");		return -2;       }       	return 0;}/************************************************************************  Function ID:  nSubmitReqDecode  Description:  CMPP协议的Submit pack 解析函数  Input Param:  unsigned char*      psDecodeStr  		        int                 nLen  Output Param: recCmppSubmit*      prCmppSubmit  Return:       int                 0   success                                    -1  failed                                *************************************************************************/int nSubmitReqDecode(unsigned char* psDecodeStr, recCmppSubmit* prCmppSubmit, int nLen){    int nRet;    int ActualLen;    int nDestUsrNum;    int nContentLen;    int nMsgFmt;    unsigned char* psHead;    if((NULL == psDecodeStr) ||(NULL == prCmppSubmit))    {        return -1;    }        psHead = psDecodeStr;    ActualLen = nLen - MacHeadLen;	    /* 1 Name: MsgId length: 8 */    memcpy(prCmppSubmit->uchMsgId, psHead, MacMsgId);    psHead = psHead + MacMsgId;    /* 2 Name: PK_Total length: 1 */    prCmppSubmit->ucPkTotal = *psHead;    psHead ++;    /* 3 Name: PK_Nmber length: 1 */    prCmppSubmit->ucPkNumber = *psHead;    psHead ++;    /* 4 Name: Registered_Deliever length: 1 */    prCmppSubmit->ucRegisterDelivery = *psHead;    psHead ++;    /* 5 Name: Msg_Level length: 1 */    prCmppSubmit->ucMsgLevel = *psHead;    psHead ++;    /* 6 Name: ServiceId length: 10 */    memcpy(prCmppSubmit->chServiceId, psHead, MacServiceId);    psHead = psHead + MacServiceId;    /* 7 Name: Fee_USerType length: 1 */    prCmppSubmit->ucFeeUserType = *psHead;    psHead ++;    /* 8 Name: Fee_TerminalId length: 32 */    memcpy(prCmppSubmit->chFeeTerminateId, psHead, MacFeeTermId);    psHead = psHead + MacFeeTermId;

⌨️ 快捷键说明

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