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

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

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

1.文件cmppapi.c

/*

        腾思CMPP2.0接口程序

        作者:刘斌([email][email protected][/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库的用法看看书。

你可能感兴趣的:(linux,socket,redhat,gcc,ubuntu)