`
coconut_zhang
  • 浏览: 541847 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

CMPP短信网关客户端发送程序

阅读更多

最近无聊,玩了玩ubuntu,想学点linux,找了几年前写的老代码,编译竟然不通过,就作了点修改。
原来的代码是2002年在红旗下编译通过的,不记得是哪个版本了。和现在不同的地方有
gettimeofday()定义变动
write(),read()等头文件包含不同
pthread_create()中,新的线程变成了void* (*)(void*)类型,感觉更合理了。
化了点时间修改了,在ubuntu5.1下通过编译。

因为是02年写的,当时刚出cmpp2.0,运行是没有问题,只是实现了收发消息的主要功能。而且当时系统主要是在2000下做的,在linux下我经验不多,所以也写不出太高水平的东西,但这个代码相信可以给你帮助。

1.文件cmppapi.c

/*

        腾思CMPP2.0接口程序

        作者:刘斌([email]liubinbj@sohu.com[/email])

        版本:1.01 (linux)

        最后修订:2002/9/30


        2006/05/09日在ubuntu5.1环境下修改了原代码,以适应新的编译环境gcc-4.0
        增加了makefile文件

       

*/
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
/*add on 2006/05/08 for gettimeofday() */
#include <sys/time.h>
/*add for read() write() close() 2006/05/09*/
#include <unistd.h>
/*add for kill()*/
#include <signal.h>


#include "cmpp_message_packet.h"

#define CMPP_SUBMIT_RESPONSE         (1)
#define CMPP_CONNECT_RESPONSE        (2)
#define CMPP_ACTIVE_RESPONSE        (3)
#define CMPP_DELIVER                        (4)
#define CMPP_LINK_ERROR                        (5)
#define CMPP_NONE_RESPONSE                (-1)

int        sockfd;

int         bIsConnected=0;
int                LogStatus=-1;
int                SequenceNum=0;
char        buf[]="abcdefghij";

int         ReturnPackType        =        -1;
int                SubmitResult        =        -1;
int                LoginResult                =        -1;

pthread_t                         ReadThreadID;
pthread_mutex_t         Mutex        =        PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t                 Cond        =        PTHREAD_COND_INITIALIZER;

pthread_mutex_t                Mutex2        =        PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t                Cond2        =        PTHREAD_COND_INITIALIZER;

struct _Deliver_Pack{
        char Phone[12];
        char Dest[15];
        char Message[161];
       
}DeliverPack;


/*Get new id*/
int GetNewSeq()
{
        return         (SequenceNum < 0x7fffffff )? (++SequenceNum):(SequenceNum=1);
}

int Send(int socketnum,unsigned char *buf,int len)
{
        int pos=0;
        int nRet=0;
        while(pos<len)
        {

                nRet=write(socketnum,buf+pos,len-pos);
               
                if(nRet==0)
                {
                        bIsConnected=0;
                        LogStatus=-1;
                        close(sockfd);
                        return -2;
                }
                else if(nRet==-1)
                {
                        bIsConnected=0;
                        LogStatus=-1;
                        close(sockfd);
                        return -1;
                }
                else
                {
                        pos+=nRet;
                        if(pos!=len)
                        {
                                continue;
                        }
                        else
                        {
                                return pos;
                        }
                }
        }
}
/*ok*/
int MoveAhead(unsigned char *buf,int oldbuflen,int movecount)
{
        int i;
        if(oldbuflen == movecount)
                return 0;
        for(i=0; i<movecount; i++)
        {
                *(buf+i)=*(buf+i+movecount);
        }
        return oldbuflen-movecount;
}

/*ok*/
/*2.6的线程模型改动了*/
/*void ReadThread(void)*/
void* ReadThread(void* data)
{
        unsigned char readbuf[7000];
        int head=0;
        int ret=0;
        int PackType;

        unsigned char *ptr;
        unsigned char  *pMsg_id,*pRegistered_delivery;
        unsigned char  *pService_id;
        unsigned char  *pTp_pid,*pTp_udhi,*pMsg_fmt;
        unsigned char  *pSrc_terminal_id;
        unsigned char  *pMsg_Length,*pMsg_Content;
        unsigned char  *pDestnation_id;
        memset(readbuf,0,7000);
        while(1)
        {
                ret=read(sockfd,readbuf+head,7000-head);
                if(ret==-1)
                {
                        if(errno!=EWOULDBLOCK)
                        {
                               
                                bIsConnected=0;
                                LogStatus=-1;
                                close(sockfd);
                                sockfd=0;
                                pthread_mutex_lock(&Mutex2);
                                ReturnPackType=CMPP_LINK_ERROR;
                                pthread_cond_signal(&Cond2);
                                pthread_mutex_unlock(&Mutex2);
                                /*return;*/
                                pthread_exit(NULL);//2.6
                        }
                        continue;
                }
                else if(ret==0)
                {
                        bIsConnected=0;
                        LogStatus=-1;
                        close(sockfd);
                        sockfd=0;
                        pthread_mutex_lock(&Mutex2);
                        ReturnPackType=CMPP_LINK_ERROR;
                        pthread_cond_signal(&Cond2);
                        pthread_mutex_unlock(&Mutex2);
                        /*return;*/
                        pthread_exit(NULL);
                }
                else
                {
                        head+=ret;
                        while(head>0)
                        {
                                if(head>=6999)
                                {
                                        head=0;
                                        continue;/*abandon data*/
                                }
                               
                                if(head>0 && head<12)
                                {
                                        head=0;
                                        continue;/*abandon data*/
                                }
                                else
                                {
                                        _CMPP_MsgHead_Pack PackHead;
                                        memset(&PackHead,0,sizeof(PackHead));
                                        memcpy(&PackHead,readbuf,12);
                                        if(ntohl(PackHead.Total_Length)>head)
                                        {
                                                head=0;
                                                continue;/*abandon data*/
                                        }
                                       
                                        if(ntohl(PackHead.Command_ID)==CMPP_Connect_REP)
                                                PackType= CMPP_CONNECT_RESPONSE;
                                        else if(ntohl(PackHead.Command_ID)==CMPP_Submit_REP)
                                                PackType= CMPP_SUBMIT_RESPONSE;
                                        else if(ntohl(PackHead.Command_ID)==CMPP_Active_Test_REP)
                                                PackType= CMPP_ACTIVE_RESPONSE;
                                        else if(ntohl(PackHead.Command_ID)==CMPP_Deliver)
                                                PackType= CMPP_DELIVER;
                                        else PackType=CMPP_NONE_RESPONSE;
                                       
                                        /*get PackType*/
                                        if(PackType==CMPP_SUBMIT_RESPONSE)
                                        {
                                                _CMPP_Submit_REP_Pack csrp;
                                                memcpy(&csrp,readbuf,sizeof(csrp));
                                                if(ntohl(csrp.Total_Length)<23||ntohl(csrp.Total_Length)>25)
                                                {
                                                        head=MoveAhead(readbuf,head,ntohl(csrp.Total_Length));/*not a legal packet*/
                                                        continue;
                                                }
                                                else        /*legal packet*/
                                                {
                                                        pthread_mutex_lock(&Mutex);/*lock right away*/
                                                        ReturnPackType=CMPP_SUBMIT_RESPONSE;/*erase mark*/
                                                        SubmitResult=csrp.Result;
                                                        head=MoveAhead(readbuf,head,ntohl(csrp.Total_Length));/*not a legal packet*/
                                                        pthread_cond_signal(&Cond);
                                                        pthread_mutex_unlock(&Mutex);
                                                        continue;
                                                }
                                        }
                                        else if(PackType==CMPP_CONNECT_RESPONSE)
                                        {
                                                _CMPP_Connect_REP_Pack ccrp;
                                                memcpy(&ccrp,readbuf,sizeof(ccrp));
                                                if(ntohl(ccrp.Total_Length)!=30)
                                                {
                                                        head=MoveAhead(readbuf,head,ntohl(ccrp.Total_Length));/*not a legal packet*/
                                                        continue;
                                                }
                                                else        /*legal packet*/
                                                {
                                                       
                                                        pthread_mutex_lock(&Mutex);/*lock right away*/
                                                        ReturnPackType=CMPP_CONNECT_RESPONSE;/*erase mark*/
                                                        LoginResult=ccrp.Status;
                                                        pthread_cond_signal(&Cond);
                                                        head=MoveAhead(readbuf,head,ntohl(ccrp.Total_Length));/*moved used packet*/
                                                        pthread_mutex_unlock(&Mutex);
                                                        continue;
                                                }
                                               
                                        }
                                        else if(PackType==CMPP_ACTIVE_RESPONSE)
                                        {
                                                 _CMPP_Active_Test_REP_Pack catrp;
                                                memcpy(&catrp,readbuf,sizeof(catrp));
                                                if(ntohl(catrp.Total_Length)!=13)
                                                {
                                                        head=MoveAhead(readbuf,head,ntohl(catrp.Total_Length));/*not a legal packet*/
                                                        continue;
                                                }
                                                else        /*legal packet*/
                                                {
                                                        pthread_mutex_lock(&Mutex);/*lock right away*/
                                                        ReturnPackType=CMPP_ACTIVE_RESPONSE;/*erase mark*/
                                                        head=MoveAhead(readbuf,head,ntohl(catrp.Total_Length));/*not a legal packet*/
                                                        pthread_cond_signal(&Cond);
                                                        pthread_mutex_unlock(&Mutex);
                                                        continue;
                                                }

                                        }
                                        else if(PackType==CMPP_DELIVER)
                                        {
                                                                                               
                                                _CMPP_Deliver_Pack cdp;
                                               
                                                memcpy(&cdp,readbuf,sizeof(cdp));
                                                if(ntohl(cdp.Total_Length)>head)
                                                {
                                                        head=0;
                                                        continue;
                                                }
                                                else if(ntohl(cdp.Total_Length)>159+160)
                                                {
                                                        head=MoveAhead(readbuf,head,ntohl(cdp.Total_Length));/*not a legal packet*/
                                                        continue;       
                                                }       
                                                else
                                                {
                                                       
                                                        pthread_mutex_lock(&Mutex2);
                                                        /*set argment*/
                                                        ptr=&cdp.Free[0];
                                                        pMsg_id=ptr+0;
                                                        pDestnation_id=ptr+8;
                                                        pService_id=ptr+29;
                                                        pTp_pid=ptr+39;
                                                        pTp_udhi=ptr+40;
                                                        pMsg_fmt=ptr+41;
                                                        pSrc_terminal_id=        ptr+42;
                                                        pRegistered_delivery=ptr+63;
                                                        pMsg_Length=ptr+64;
                                                        pMsg_Content=ptr+65;

                                                        if(*pRegistered_delivery)
                                                        {
                                                                head=MoveAhead(readbuf,head,ntohl(cdp.Total_Length));/*not a legal packet*/
                                                                pthread_mutex_unlock(&Mutex2);
                                                        }
                                                        else
                                                        {
                                                                memcpy(DeliverPack.Phone,pSrc_terminal_id,11);
                                                                DeliverPack.Phone[11]=0;
                                                                memcpy(DeliverPack.Dest,pDestnation_id,14);
                                                                DeliverPack.Dest[14]=0;
                                                                memcpy(DeliverPack.Message,pMsg_Content,(*pMsg_Length)>160?160:(*pMsg_Length));

                                                                DeliverPack.Message[(*pMsg_Length)>160?160:(*pMsg_Length)]=0;
                                                                head=MoveAhead(readbuf,head,ntohl(cdp.Total_Length));
                                                                ReturnPackType=CMPP_DELIVER;
                                                                pthread_cond_signal(&Cond2);
                                                                pthread_mutex_unlock(&Mutex2);       
                                                               
                                                        }
                                                }
                                                                                               
                                        }
                                        else
                                        {
                                               
                                                _CMPP_MsgHead_Pack cmp;
                                                memcpy(&cmp,readbuf,sizeof(cmp));
                                                head=MoveAhead(readbuf,head,ntohl(cmp.Total_Length));
                                                continue;
                                               
                                        }
                                       
                                }
                       
                                head=0;
                                ret=0;
                                continue;
                        }
                }
        }
}


/*initial an connect to host*/

int ConnectHost(char *Server,int Port)
{
        struct sockaddr_in servaddr;
        int ret;
       
        if(LogStatus==0)
                LogStatus=2;
        if(bIsConnected)
                bIsConnected=1;

        if(ReadThreadID)
        {
                kill(ReadThreadID,0);
                ReadThreadID=0;
                shutdown(sockfd,0);
        }

        sockfd=socket(AF_INET,SOCK_STREAM,0);
        if(sockfd<0)
                return -1;/*create socket error*/

        memset(&servaddr,0,sizeof(struct sockaddr_in));
        servaddr.sin_family=AF_INET;
        servaddr.sin_port=htons(Port);
        if(inet_pton(AF_INET,Server,&servaddr.sin_addr)<=0)
                return -2;/*Ip Error*/
        if(connect(sockfd,(struct sockaddr*)&servaddr,sizeof(struct sockaddr_in))<0)
                return -3;/*connect error*/
        else
                bIsConnected=1;

        ret=pthread_create(&ReadThreadID,NULL,ReadThread,NULL/*传递给线程的入口参数*/);
        if(ret!=0)
                return -4;/*create thread error*/

        return 0;/*ok*/
}

/*logto server*/

int Log(char *User,char *Pass)
{
        char packbuf[30];
        _CMPP_Connect_Pack        ccp;
       
        /*make login packet*/
        struct timeval now;
        struct timezone tz; /*add on 2006/05/09 for linux 2.6.x*/
        struct timespec timeout;
        int retcode;

        int LogReturn=-1;

        if(LogStatus==0)
                return 9;/*already logged ok*/
        if(!bIsConnected)
                return 8;/*not connected*/

        memset(&ccp,0,sizeof(ccp));
        ccp.Total_Length=htonl(39);
        ccp.Command_ID=htonl(CMPP_Connect);
        SequenceNum=0;
        ccp.Sequence_ID=htonl(GetNewSeq());
        ccp.Version=0x20;
        if(strlen(User)<=6)
                strcpy((char*)(&ccp.Source_Addr[0]),User);
        else
                memcpy(&ccp.Source_Addr[0],User,6);
        if(Send(sockfd,(unsigned char*)&ccp,39)==39)
        {
                /*send ok*/
                pthread_mutex_lock(&Mutex);/*lock right away*/
                gettimeofday(&now,&tz);/*changed for 2.6*/
                timeout.tv_sec = now.tv_sec + 1;
                timeout.tv_nsec = now.tv_usec*100;

                while(ReturnPackType!=CMPP_CONNECT_RESPONSE &&retcode!=ETIMEDOUT)
                {
                        retcode=pthread_cond_timedwait(&Cond,&Mutex,&timeout);
                }
                if(retcode==ETIMEDOUT)
                {
                        LogReturn=-3;
                }
                else
                {
                        /*do something*/
                        LogReturn = LoginResult;/*log response ok*/
                        LogStatus = LoginResult;
                        LoginResult = -1;
                }
                ReturnPackType=CMPP_NONE_RESPONSE;/*erase mark*/
               
                pthread_mutex_unlock(&Mutex);/*unlock*/

                return LogReturn;
        }
        else
        {
                return -1;/*network error*/
        }
}
/*send short msg to gateway*/

int SubmitMsg(
                          char *Phone,        /*Dest*/
                          char *Content,   /*message buffer*/
                          unsigned char Length,                /*message length*/
                          char *Source,                                /*from*/
                          char* Spid,                                /*SPID*/
                          unsigned char MsgFmt,                /*message format*/
                          unsigned int Feetype,                /*Feetype*/
                          unsigned int Feecode,                /*feecode*/
                          char *ServType                        /*Server_id*/
                          )
{
        struct        timeval now;
        struct         timezone tz;
        struct        timespec timeout;
        int                retcode;

        int                SubmitReturn=0;
        _CMPP_Submit_Pack csp;
        unsigned char  *ptr;
        unsigned char  * pMsg_id,*pPk_total,*pPk_number,*pRegistered_delivery,*pMsg_level,*pService_id,*pFee_usertype,*pFee_terminal_id;
        unsigned char  * pTp_pid,*pTp_udhi,*pMsg_fmt,*pMsg_src,*pFeetype,*pFeecode,*pValid_time,*pAt_time,*pSrc_terminal_id,*pDestUsr_tl;
        unsigned char   *pDest_terminal_id,*pMsg_Length,*pMsg_Content;/* *pReserve; */

        if(strlen(Phone) !=11 || (Length>160) || (Length>140 &&MsgFmt==4) || strlen(ServType)>10)
                return -4;

        memset(&csp,0,sizeof(csp));
        ptr= &csp.Free[0];
        pMsg_id=ptr+0;
        pPk_total=ptr+8;
        pPk_number=ptr+9;
        pRegistered_delivery=ptr+10;
        pMsg_level=ptr+11;
        pService_id=ptr+12;
        pFee_usertype=ptr+22;
        pFee_terminal_id=ptr+23;
        pTp_pid=ptr+44;
        pTp_udhi=ptr+45;
        pMsg_fmt=ptr+46;
        pMsg_src=ptr+47;
        pFeetype=ptr+53;
        pFeecode=ptr+55;
        pValid_time=ptr+61;
        pAt_time=ptr+78;
        pSrc_terminal_id=ptr+95;
        pDestUsr_tl=ptr+116;
        pDest_terminal_id=ptr+117;
        pMsg_Length=ptr+138;
        pMsg_Content=ptr+139;

        *pMsg_fmt=MsgFmt;

        *pDestUsr_tl=1;
        *pPk_total=1;
        *pPk_number=1;
        *pMsg_level=1;
        *pRegistered_delivery=0;

        strcpy((char*)pService_id,ServType);
        strcpy((char*)pDest_terminal_id,Phone);
        *pMsg_Length=Length;
        strcpy((char*)pMsg_src,Spid);

        strcpy((char*)pSrc_terminal_id,Source);

        switch(Feetype)
        {
        case 0:
                memcpy(pFeetype,"00",2);break;
        case 1:
                memcpy(pFeetype,"01",2);break;
        case 2:
                memcpy(pFeetype,"02",2);break;
        case 3:
                memcpy(pFeetype,"03",2);break;
        case 4:
                memcpy(pFeetype,"04",2);break;
        case 5:
                memcpy(pFeetype,"05",2);break;
        default :
                memcpy(pFeetype,"02",2);break;
        }
       
        memcpy(pMsg_Content,Content,Length);

        sprintf((char*)pFeecode,"%06d",Feecode);

        /*make the packet*/
        csp.Total_Length=htonl(159+Length);
        csp.Command_ID=htonl(CMPP_Submit);
        csp.Sequence_ID=htonl(GetNewSeq());

        if(Send(sockfd,(unsigned char*)&csp,159+Length)==159+Length)
        {
                /*send ok*/
                pthread_mutex_lock(&Mutex);/*lock right away*/
                gettimeofday(&now,&tz);
                timeout.tv_sec = now.tv_sec+1 ;
                timeout.tv_nsec = now.tv_usec*1000;

                while(ReturnPackType!=CMPP_SUBMIT_RESPONSE &&retcode!=ETIMEDOUT)
                {
                        retcode=pthread_cond_timedwait(&Cond,&Mutex,&timeout);
                }
                if(retcode==ETIMEDOUT)
                {
                        SubmitReturn=-3;/*time out*/
                       
                }
                else
                {
                        SubmitReturn=SubmitResult;
                        SubmitResult=-1;
                }
                ReturnPackType=CMPP_NONE_RESPONSE;/*erase mark*/
                pthread_mutex_unlock(&Mutex);/*unlock*/

                return SubmitReturn;
        }
        else
        {
                return -1;/*connection error.*/
        }

}

int ActiveTest()
{
        struct timeval now;
        struct timezone tz;
        struct timespec timeout;
        int retcode;
        int ActiveTestReturn=0;
        /*make the packet*/
        _CMPP_Active_Test_Pack catp;
        catp.Total_Length=htonl(12);
        catp.Command_ID=htonl(CMPP_Active_Test);
        catp.Sequence_ID=htonl(GetNewSeq());
       
        if(Send(sockfd,(unsigned char*)&catp,12)==12)
        {
                pthread_mutex_lock(&Mutex);/*lock right away*/
                gettimeofday(&now,&tz);
                timeout.tv_sec = now.tv_sec + 1;
                timeout.tv_nsec = now.tv_usec*1000;
       
                while(ReturnPackType!=CMPP_ACTIVE_RESPONSE && retcode!=ETIMEDOUT)
                {
                        retcode=pthread_cond_timedwait(&Cond,&Mutex,&timeout);
                }
               
                if(retcode==ETIMEDOUT)
                {
                        ActiveTestReturn=-3;/*time out*/
                }
                else
                {
                        ActiveTestReturn=0;/*activetest ok*/
                }
                ReturnPackType=CMPP_NONE_RESPONSE;/*erase mark*/
                pthread_mutex_unlock(&Mutex);/*unlock*/

                return ActiveTestReturn;
        }
        else
        {
                return -1;/*connection error.*/
        }
}

 

int ReceiveMsg(char *Phone,char *Dest, char *Content)
{
        int nRet;
       
        if(!bIsConnected)
                return 1;

        pthread_mutex_lock(&Mutex2);

        while(ReturnPackType!=CMPP_DELIVER && ReturnPackType!=CMPP_LINK_ERROR)
                pthread_cond_wait(&Cond2,&Mutex2);
        if(ReturnPackType==CMPP_LINK_ERROR)
        {
                nRet=-1;
                ReturnPackType=CMPP_NONE_RESPONSE;/*erase mark*/
        }
        else
        {

                strcpy(Phone,DeliverPack.Phone);
                strcpy(Dest,DeliverPack.Dest);
                strcpy(Content,DeliverPack.Message);
                nRet=0;
                ReturnPackType=CMPP_NONE_RESPONSE;/*erase mark*/
        }       
        pthread_mutex_unlock(&Mutex2);
        return nRet;
}

文件2:cmpp_command_id.h 这个没什么变化。

//CMPP_COMMAND_ID.H
/*
        this file contains CMPP protocol Command_ID
        Author: Rolland
        Create Date:        2001/07/24
        Last Revision:        2001/07/24
*/
#ifndef _CMPP_COMMAND_ID_H
#define _CMPP_COMMAND_ID_H
//Begin of CMPP COMMAND ID define
#define CMPP_Connect                        0x00000001        //请求连接
#define CMPP_Connect_REP                0x80000001        //请求连接应答
#define CMPP_Terminate                        0x00000002        //终止连接
#define CMPP_Terminate_REP                0x80000002        //终止连接应答
#define CMPP_Submit                                0x00000004        //提交短信
#define CMPP_Submit_REP                        0x80000004        //提交短信应答
#define CMPP_Deliver                        0x00000005        //短信下发
#define CMPP_Deliver_REP                0x80000005        //短信下发应答
#define CMPP_Query                                0x00000006        //发送短信状态查询
#define CMPP_Query_REP                        0x80000006        //发送短信状态查询应答
#define CMPP_Cancel                                0x00000007        //删除短信
#define CMPP_Cancel_REP                        0x80000007        //删除短信应答
#define CMPP_Active_Test                0x00000008        //激活测试
#define CMPP_Active_Test_REP        0x80000008        //激活测试应答
#define CMPP_Fwd                                0x00000009        //消息前转
#define CMPP_Fwd_REP                        0x80000009        //消息前转应答
#define CMPP_Route                                0x00000003        //路由请求
#define CMPP_Route_REP                        0x80000003        //路由请求应答

//End of CMPP COMMAND ID define
#endif

你运气还不错[2]

文件3:cmpp_message_packet.h 这个文件也没改过

//CMPP_MESSAGE_PACK_DEFINE.H
//
//CMPP 1.2版协议消息包定义
/*
        This file defines CMPP protocol Packets
        CMPP Version: 1.2

        Author: Rolland
        Create Date:        2001/07/26
        Last Revision:        2001/07/26
*/
#ifndef _CMPP_MESSAGE_PACK_DEFINE_H
#define _CMPP_MESSAGE_PACK_DEFINE_H

#include "cmpp_command_id.h"
//CMPP Messsage Head  define
typedef struct {
        unsigned int Total_Length;                //消息全长
        unsigned int Command_ID;                //命令类型
        unsigned int Sequence_ID;        //消息流水号
}_CMPP_MsgHead_Pack;
//End of CMPP Messsage Head define

//CMPP_Connect message head and body
typedef struct {
        unsigned int        Total_Length;        //消息全长
        unsigned int        Command_ID;                //命令类型
        unsigned int        Sequence_ID;        //消息流水号
        unsigned char        Source_Addr[6];        //SP_ID
        unsigned char        AuthenticatorSP[16];//SP认证码
        unsigned char        Version;        //协商的协议版本号
        unsigned int        Timestamp;        //时间戳的明文,由客户端产生,格式为MMDDHHMMSS,即月日时分秒,10位,右对齐
}_CMPP_Connect_Pack;
//End of CMPP_Connect message head and body

//CMPP_Connect_REP message head and body
typedef struct {
        unsigned int        Total_Length;        //消息全长
        unsigned int        Command_ID;                //命令类型
        unsigned int        Sequence_ID;        //消息流水号       
        unsigned char        Status;                //状态 0:正确;1:消息结构错 ;2:非法SP_ID ;3:SP认证错 ;4:版本太高  ;5~ :其他错误
        unsigned char        AuthenticatorISMG[16];        //ISMG认证码 ,SP认证出错时,此项为空
        unsigned char        Version;                //服务器支持的最高版本号
}_CMPP_Connect_REP_Pack;

//End of CMPP_Connect_REP message head and body

//CMPP_Terminate message head (no body)
typedef struct {
        unsigned int Total_Length;        //消息全长
        unsigned int Command_ID;        //命令类型
        unsigned int Sequence_ID;        //消息流水号
}_CMPP_Terminate_Pack;
//End of CMPP_Terminate message head (no body)

//CMPP_Terminate_REP message head (no body)
typedef struct {
        unsigned int Total_Length;        //消息全长
        unsigned int Command_ID;        //命令类型
        unsigned int Sequence_ID;        //消息流水号
}_CMPP_Terminate_REP_Pack;
//End of CMPP_Terminate_REP message head (no body)


//CMPP_Submit_REP message head and body
typedef struct {
        unsigned int Total_Length;        //消息全长
        unsigned int Command_ID;        //命令类型
        unsigned int Sequence_ID;        //消息流水号

        unsigned int Msg_Id[2];        //信息标识,由SP侧短信网关本身产生
                                        //生成MsgID的算法如下:
                                        //采用64位(8字节)的整数:
                                        //(1)时间(从月精确到分)       
                                        //63――44位(bit)
                                        //(2)SMC(短讯中心)号码 43――17位(bit)
                                        //(3)序列号  16――0位(bit)顺序累加,
                                        //步长为1,循环使用.各分段如不能填满,左补零

        unsigned char        Result;        //结果0:正确1:消息结构错 2:命令字错 3:消息序号重复4:消息长度错5:资费代码错6:超过最大信息长7:业务代码错8:流量控制错9~ :其他错误
}_CMPP_Submit_REP_Pack;
//End of CMPP_Submit_REP message head and body

//CMPP_Query message head and body
typedef struct {
        unsigned int Total_Length;        //消息全长
        unsigned int Command_ID;        //命令类型
        unsigned int Sequence_ID;        //消息流水号

        unsigned char Time[8];//时间(精确至日)
        unsigned char Query_Type;//查询类别  0:总数查询 1:按业务代码查询
        unsigned char Query_Code[10];//查询码 (业务类型)  当Query_Type为0时,此项无效;当Query_Type为1时,此项填写业务代码Service_id.
        unsigned char Reserve[8];
}_CMPP_Query_Pack;
//End of CMPP_Query message head and body

//CMPP_Query_REP message head and body
typedef struct {
        unsigned int Total_Length;        //消息全长
        unsigned int Command_ID;        //命令类型
        unsigned int Sequence_ID;        //消息流水号

        unsigned char Time[8];                        //时间(精确至日)
        unsigned char Query_Type;                //查询类别0:总数查询1:按业务代码查询
        unsigned char Query_Code[10];        //查询码
        int MT_TLMsg;                //从SP接收信息总数
        int MT_TLusr;                //从SP接收用户总数
        int MT_Scs;                        //成功转发数量
        int MT_WT;                        //待转发数量
        int MT_FL;                        //转发失败数量
        int MO_Scs;                        //向SP成功送达数量
        int MO_WT;                        //向SP待送达数量
        int MO_FL;                        //向SP送达失败数量
}_CMPP_Query_REP_Pack;
//End of CMPP_Query_REP  message head and body


//CMPP_Deliver_REP message head and body
typedef struct {
        unsigned int        Total_Length;        //消息全长
        unsigned int        Command_ID;        //命令类型
        unsigned int        Sequence_ID;        //消息流水号
       
        unsigned int        Msg_Id[2];        //消息标识
        unsigned char        Result;        //结果0:正确1:消息结构错 2:命令字错 3:消息序号重复4:消息长度错5:资费代码错6:超过最大信息长7:业务代码错8: 流量控制错9~ :其他错误
}_CMPP_Deliver_REP_Pack;
//End of CMPP_Deliver_REP message head and body

//CMPP_Cancel
typedef  struct {
        unsigned int        Total_Length;        //消息全长
        unsigned int        Command_ID;        //命令类型
        unsigned int        Sequence_ID;        //消息流水号

        unsigned int        Msg_Id[2];        //消息标识
}_CMPP_Cancel_Pack;
//End of CMPP_Cancel

//CMPP_Cancel_REP message head and body
typedef  struct {
        unsigned int        Total_Length;        //消息全长
        unsigned int        Command_ID;        //命令类型
        unsigned int        Sequence_ID;        //消息流水号

        unsigned char        Succeed_Id;                //成功标识(0:成功;  1:失败)
}_CMPP_Cancel_REP_Pack;
//End of CMPP_Cancel_REP message head and body

//CMPP_Active_Test message head (no body)
typedef  struct {
        unsigned int Total_Length;        //消息全长
        unsigned int Command_ID;        //命令类型
        unsigned int Sequence_ID;        //消息流水号
}_CMPP_Active_Test_Pack;

//End of _CMPP_Active_Test

//CMPP_Active_Test_REP message head and body
typedef struct {
        unsigned int Total_Length;        //消息全长
        unsigned int Command_ID;        //命令类型
        unsigned int Sequence_ID;        //消息流水号

        unsigned char        Succeed_Id;                //成功标识(0:成功;其它:不成功)
}_CMPP_Active_Test_REP_Pack;
//End of _CMPP_Active_Test

typedef struct {
        unsigned int Total_Length;        //消息全长
        unsigned int Command_ID;        //命令类型
        unsigned int Sequence_ID;        //消息流水号
        unsigned char Free[240];
}_CMPP_Deliver_Pack;

typedef struct {
        unsigned int Total_Length;        //消息全长
        unsigned int Command_ID;        //命令类型
        unsigned int Sequence_ID;        //消息流水号
        unsigned char Free[310];
}_CMPP_Submit_Pack;

#endif


文件4:makefile  这个是新加的,当时不会写
cmppapi.so:cmppapi.c
        gcc -o cmppapi.so -shared -fPIC -O2 cmppapi.c

# cc cmppapi1.c
/usr/lib/gcc-lib/i386-redhat-linux/3.2.3/../../../crt1.o(.text+0x18): In function `_start':
: undefined reference to `main'
collect2: ld returned 1 exit status

请问这是怎么回事?

gcc -o cmppapi1.so  -shared -fPIC -O2 cmppapi1.c
可以了,编译出来的 cmppapi1.so怎么用?

回复 5楼 xzh2002 的帖子

so是共享库阿。调用发送和接受函数就OK了,可以写一个cmppserver用来测试,我写过1.2和2.0两个server,不过不是linux下的,不适合你。你可以直接在移动接口上测,可能有些地方需要根据不同的平台作调整。不同提供商登录处理有些不同。给你发的这个版本Log函数中没有照规范作,因为是连接我们自己的2.0平台给别人提供服务的,我们的平台不验证password的md5(当时定义就比较含糊),我们是服务器上作ip检验来限制登录。你需要在Log函数中依照协议增加md5校验。
总体是这样来用的:
ConnectHost()建立连接
Log()登录
ReceiveMsg()接收消息
SubmitMsg()发送消息
ActiveTest()活动测试

ReceiveMsg()是同步的,有消息来就返回,没消息就锁定。
SubmitMsg()是异步的,发送成功就返回,是否成功发送要从ReceiveMsg()反馈来判断。

so库的用法看看书。

分享到:
评论

相关推荐

    CMPP短信网关范例

    CMPP短信网关是中国移动通信集团为SP(Service Provider)提供的一个重要的接口,用于实现SP与移动运营商之间的短信发送和接收。这个系统是基于CMPP(China Mobile Peer to Peer)协议,这是一种专为大规模、高效能...

    cmpp2.0模拟短信网关

    1. **连接管理**:模拟短信网关能够模拟真实的短信网关,接受和响应CMPP连接请求,建立与客户端的TCP连接。 2. **报文处理**:实现CMPP2.0协议规定的各种报文处理,如提交、接收、查询和取消等操作。 3. **消息模拟*...

    cmpp3.0移动短信发送程序

    1. **连接建立**:CMPP客户端(这里是SmsSend)首先需要通过TCP/IP连接到中国移动的短信网关,一般使用固定的端口号。 2. **认证与注册**:连接建立后,客户端会发送CMPP_CONNECT请求,包含SP的ID和密码进行身份...

    CMPP模拟网关的功能与实现.pdf

    1. **网关接收分析包模块**:该模块是模拟网关的核心部分,用于分析客户端发送的数据包。服务器端接收到客户端的数据包后,会将其放入消息队列中,并从队列中取出进行分析。 2. **消息头分析**:每个消息头包含了...

    移动短信CMPP3.0客户端调试程序

    1. **接口模拟**:程序可以模拟CMPP3.0协议的客户端,连接到指定的短信网关服务器,模拟提交、查询和接收短信的过程。 2. **报文解析与构造**:程序能解析和构造CMPP3.0协议的各类报文,包括报文头、业务参数、消息...

    CMPP短消息网关代码与模拟网关

    `37322220032171148010vc的cmpp代码.zip`可能是使用Visual C++编写的CMPP客户端或网关代码,这可能是用于SP端的短信发送程序,或者是一个简化版的网关实现。VC++代码可能包含了TCP连接、CMPP消息构造和解析的函数,...

    cmpp2_cmpp2客户端和服务端测试工具_

    CMPP2,全称为China Mobile Peer-to-Peer Protocol Version 2,是中国移动通信集团制定的一种基于TCP/IP协议的通信协议,主要用于短信中心(SMSC)与短信网关(SP Gateway)之间的数据传输,支持大批量、高并发的...

    CMPP3网关程序、中国移动网关程序、短信群发系统

    CMPP3网关程序是中国移动通信集团用于处理短信服务的核心组件,主要负责短信的收发工作,特别是大规模的群发操作。中国移动网关程序则是在这个框架下的一部分,它连接了移动运营商的网络与第三方应用,使得企业或者...

    CMPP短信网关3。0

    CMPP短信网关3.0是中国移动通信推出的一种高级短消息协议,用于高效、稳定地传输短信服务。这个系统的设计主要是为了满足大量的企业级短信发送需求,如验证码发送、营销推广、通知服务等。CMPP协议是基于TCP/IP的,...

    基于中国移动cmpp3协议开发的短信网关源码

    【描述】"基于中国移动cmpp3协议开发的短信网关源码",意味着这个项目直接利用了CMPP3.0协议的特性,实现了短信的发送、接收和处理功能。短信网关是连接移动运营商网络和企业应用的关键组件,它允许应用程序通过标准...

    cmpp客户端.7z.zip

    SGIP是中国电信的短信网关接口协议,它的设计目标是提高短信服务的处理能力和灵活性。SGIP同样基于TCP/IP,提供了一套完整的控制命令集,用于管理会话、提交和查询短信状态。 "cmpp客户端.7z.zip"是一个包含测试...

    CMPP2.0 短消息网关

    CMPP2.0(China Mobile Peer-to-Peer)短消息网关协议是中国移动通信集团设计的一种高效、可靠的短消息传输协议,主要用于SP(Service Provider,服务提供商)与运营商的短信中心(SMSC,Short Message Service ...

    基于中国移动cmpp2协议开发的短信网关源码

    在CMPP2协议下,短信网关的主要任务是解析并执行来自SP的命令,如发送短信、接收短信、查询状态等,并将结果返回给SP。同时,它还需要与运营商的短信中心(SMSC)进行数据交换,确保短信的正确路由和投递。 "cmpp2...

    cmpp 2.0 短信网关

    CMPP 2.0短信网关是电信运营商用于处理短信业务的一种通信协议,它基于TCP/IP,主要用于SP(Service Provider)和服务提供商之间的数据传输。这个协议标准由中国移动制定,旨在提高短信服务的效率和可靠性。下面我们...

    CMPP.rar_CSharp cmpp_cmpp_cmpp 2.0 C#_短信网关_网关

    本项目"CMPP.rar"是一个基于C#实现的CMPP协议版本2.0的短信网关程序。CMPP 2.0相较于早期版本,进行了优化和增强,提供了更稳定和高效的数据交换机制。开发者通过这个程序可以实现与移动运营商的短信平台进行交互,...

    中国移动短信接口CMPP3.0SP客户端源码

    中国移动短信接口CMPP3.0SP客户端源码是一款专门用于SP(Service Provider,服务提供商)与ISMG(Internet Short Message Gateway,互联网短信网关)之间交互的程序,它基于中国移动的CMPP3.0协议标准。这个源码是...

    cmpp3.0网关模拟器 sgip1.2网关模拟器

    当你需要测试短信收发程序是否正确封装这两种协议时,可以使用"cmpp3.0 网关模拟器"和"sgip1.2 网关模拟器"。这些模拟器能够模拟真实的网络环境,让你的程序在不实际连接到中国移动网关的情况下进行测试。通过模拟器...

    中国移动CMPP短信开发平台通讯包

    2. **消息提交**:将短信内容封装成CMPPSubmit报文,发送到短信网关,请求发送短信。 3. **消息查询**:通过CMPPQuery报文查询短信的发送状态,了解短信是否成功送达。 4. **消息接收**:监听来自短信网关的...

    华为cmpp协议短信网关Delphi源码.rar_Huawei CMPP_Huawei delphi_sms gateway d

    在华为CMPP短信网关项目中,开发者通常会使用 Indy 或其他网络通信库来处理TCP连接和数据包解析。Indy 提供了一套完整的网络组件,可以方便地建立TCP客户端和服务器,支持CMPP协议的报文封装和解封。 短信网关是...

    java实现cmpp协议开发代码

    Java实现CMPP协议开发代码主要涉及的是中国移动通信的CMPP(China Mobile Packet Protocol)协议,该协议主要用于短信中心(SMSC)与短信网关(SP)之间的数据传输,包括发送短消息、接收短消息以及相关的控制功能。...

Global site tag (gtag.js) - Google Analytics