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

📄 cmpp.h

📁 Unix/Linux下的cmpp实现源程序
💻 H
字号:

/*******************************************************
    NAME:      cmpp.h
    PURPOSE:   Header definition of China Mobile Peer to 
               Peer Protocol 2.0
               Permanent connection implementation to 
               submit data to Internet Short Message Gateway
               or deliver data from ISMG to upper layer.
               routine implementation.
    VERSION:   0.0.1
    AUTHOR:    Ke Heng Zhong
    DATE:      2002/06/17 12:52
    MODIFIED:  2000/06/17 21:12
 ********************************************************/

#ifndef _CMPP_H_
#define _CMPP_H_

#include "btype.h"



uint32 cmpp_alloc_sequence_id ();



/* define the CMPP protocol primitive type */

#define CMPP_PRIMITIVE       5000

#define cmpp_start_req       (CMPP_PRIMITIVE + 0)
#define cmpp_terminate_req   (CMPP_PRIMITIVE + 1)
#define cmpp_submit_req      (CMPP_PRIMITIVE + 2)
#define cmpp_deliver_ind     (CMPP_PRIMITIVE + 3)


typedef struct cmpp_block {

    uint8           remote_host[128];
    struct in_addr  remote_ip;
    uint16          remote_port;

    int             conid;

    FRAME_PTR       data;

} CmppBlock;


/* define the CMPP protocol timer primitive type */

#define CMPP_TIMER_MACRO     6000

#define TimerTO_ActTest      (CMPP_TIMER_MACRO + 0)
#define TimerTO_Retry        (CMPP_TIMER_MACRO + 1)







/* definition of number of permanent TCP connection */

#define MAX_PCON    4


/* definition of ISMG TCP connection listening port from SP */

#define ISMG_PORT    7890



/* definition of maximum Retransmission count */

#define MAXRETRY    3


/* definition of Timer ID and Timeout value */

#define t_acttest     6200
#define t_retry       6201
#define t_build       6202
#define t_resubmit    6203
#define t_reactive    6204

#define TACTTEST      3  * 60 * 1000
#define TRETRY        60 * 1000
#define TBUILD        3  * 1000
#define TRESUBMIT     60 * 1000
#define TREACTIVE     60 * 1000


/* definition of sliding window size for flow control */

#define MAX_SLIDING_WIN    16
#define MAX_CMPP_CON       30



/* finite automation machine state */
#define cmpp_null           0
#define cmpp_connecting     1
#define cmpp_handshaking    2
#define cmpp_ready          3



/* definition of command id or CMPP PDU type */

#define CMPP_CONNECT                    0x00000001
#define CMPP_CONNECT_RESP               0x80000001
#define CMPP_TERMINATE                  0x00000002
#define CMPP_TERMINATE_RESP             0x80000002
#define CMPP_SUBMIT                     0x00000004
#define CMPP_SUBMIT_RESP                0x80000004
#define CMPP_DELIVER                    0x00000005
#define CMPP_DELIVER_RESP               0x80000005
#define CMPP_QUERY                      0x00000006
#define CMPP_QUERY_RESP                 0x80000006
#define CMPP_CANCEL                     0x00000007
#define CMPP_CANCEL_RESP                0x80000007
#define CMPP_ACTIVE_TEST                0x00000008
#define CMPP_ACTIVE_TEST_RESP           0x80000008
#define CMPP_FWD                        0x00000009
#define CMPP_FWD_RESP                   0x80000009
#define CMPP_MT_ROUTE                   0x00000010
#define CMPP_MT_ROUTE_RESP              0x80000010
#define CMPP_MO_ROUTE                   0x00000011
#define CMPP_MO_ROUTE_RESP              0x80000011
#define CMPP_GET_ROUTE                  0x00000012
#define CMPP_GET_ROUTE_RESP             0x80000012
#define CMPP_MT_ROUTE_UPDATE            0x00000013
#define CMPP_MT_ROUTE_UPDATE_RESP       0x80000013
#define CMPP_MO_ROUTE_UPDATE            0x00000014
#define CMPP_MO_ROUTE_UPDATE_RESP       0x80000014
#define CMPP_PUSH_MT_ROUTE_UPDATE       0x00000015
#define CMPP_PUSH_MT_ROUTE_UPDATE_RESP  0x80000015
#define CMPP_PUSH_MO_ROUTE_UPDATE       0x00000016
#define CMPP_PUSH_MO_ROUTE_UPDATE_RESP  0x80000016



/* definition of PDU structure */
typedef struct cmpp_connect_pdu {
    uint8    res[8];
    uint32   command_id;
    uint32   sequence_id;

    uint8    source_addr [6];
    uint8    auth_src [16];
    uint8    version;
    uint32   time_stamp;
} CmppConnect;

typedef struct cmpp_connect_resp_pdu {
    uint8    res[8];
    uint32   command_id;
    uint32   sequence_id;

    uint8    status;
    uint8    auth_ismg [16];
    uint8    version;
} CmppConnectResp;

typedef struct cmpp_submit_resp_pdu {
    uint8    res[8];
    uint32   command_id;
    uint32   sequence_id;

    uint8    msg_id [8];
    uint8    result;
} CmppSubmitResp;


typedef struct cmpp_query_pdu {
    uint8    res[8];
    uint32   command_id;
    uint32   sequence_id;

    uint8    time [8];
    uint8    query_type;
    uint8    query_code [10];
    uint8    reserve [8];
} CmppQuery;


typedef struct cmpp_query_pdu {
    uint8    res[8];
    uint32   command_id;
    uint32   sequence_id;


typedef struct cmpp_pdu {
    struct   cmpp_pdu * prev, * next;

    uint32   total_length;
    uint32   command_id;
    uint32   sequence_id;

    union {

        struct cmpp_connect_ {
            uint8    source_addr [6];
            uint8    auth_src [16];
            uint8    version;
            uint32   time_stamp;
        } CmppConnect;

        struct cmpp_connect_resp_ {
            uint8    status;
            uint8    auth_ismg [16];
            uint8    version;
        } CmppConnectResp;

        struct cmpp_submit_ {
            uint8      msg_id [8];
            uint8      pk_total;
            uint8      pk_number;
            uint8      reg_delivery;
            uint8      msg_level;
            uint8      service_id [10];
            uint8      fee_usertype;
            uint8      fee_terminal_id [21];
            uint8      tp_pid;
            uint8      tp_udhi;
            uint8      msg_fmt;
            uint8      msg_src [6];
            uint8      feetype [2];
            uint8      feecode [6];
            uint8      valid_time [17];
            uint8      at_time [17];
            uint8      src_id [21];
            uint8      destusr_tl;
            FRAME_PTR  dest_terminal_id;
            uint8      msg_length;
            FRAME_PTR  msg_content;
        } CmppSubmit;

        struct cmpp_submit_resp_ {
            uint8     msg_id [8];
            uint8     result;
        } CmppSubmitResp;

        struct cmpp_query_ {
            uint8      time [8];
            uint8      query_type;
            uint8      query_code [10];
            uint8      reserve [8];
        } CmppQuery;

        struct cmpp_query_resp_ {
            uint8      time [8];
            uint8      query_type;
            uint8      query_code [10];
            uint32     mt_tlmsg;
            uint32     mt_tlusr;
            uint32     mt_scs;
            uint32     mt_wt;
            uint32     mt_fl;
            uint32     mo_scs;
            uint32     mo_wt;
            uint32     mo_fl;
        } CmppQueryResp;

        struct cmpp_deliver_ {
            uint8      msg_id [8];
            uint8      dest_id [21];
            uint8      service_id [10];
            uint8      tp_pid;
            uint8      tp_udhi;
            uint8      msg_fmt;
            uint8      src_terminal_id [21];
            uint8      reg_delivery;
            uint8      msg_length;
            FRAME_PTR  msg_content;
        } CmppDeliver;

        struct cmpp_deliver_resp_ {
            uint8      msg_id [8];
            uint8      result;
        } CmppDeliverResp;

        struct cmpp_cancel_ {
            uint8      msg_id [8];
        } CmppCancel;

        struct cmpp_cancel_resp_ {
            uint8      success_id;
        } CmppCancelResp;

    } u;

#define Connect      u.CmppConnect
#define ConnectResp  u.CmppConnectResp
#define Submit       u.CmppSubmit
#define SubmitResp   u.CmppSubmitResp
#define Query        u.CmppQuery
#define QueryResp    u.CmppQueryResp
#define Deliver      u.CmppDeliver
#define DeliverResp  u.CmppDeliverResp
#define Cancel       u.CmppCancel
#define CancelResp   u.CmppCancelResp

} CmppPdu;

int       cmpdu_size   (uint32 type);
CmppPdu * cmpdu_alloc  (uint32 type);
void      cmpdu_free   (CmppPdu * cmpdu);
int       cmpdu_init   (CmppPdu * cmpdu);
int       cmpdu_encode (CmppPdu * cmpdu, FRAME_PTR * data);
CmppPdu * cmpdu_decode (FRAME_PTR * data);



#define SUBMIT_AVAIL       0
#define SUBMIT_PRESEND     1
#define SUBMIT_WAITRESP    2
#define SUBMIT_REWAITRESP  3

typedef struct submit_pending {
    uint8       status;

    uint8       retrynum;

    CmppPdu   * submit;
    FRAME_PTR   submitframe;

    TIMER     * submtimer;
} SubmitPending;
    


/* definition of internal variables of one permanent 
 * TCP connection instance */

typedef struct cmpp_con {
    int             conid;

    uint8           remote_host[128];
    struct in_addr  remote_ip;
    uint16          remote_port;

    struct in_addr  local_ip;
    uint16          local_port;

    int             fd;
    int             devid;

    /* MT or MO connection type */
    uint8           is_mo;


    /* used for connection */
    uint8           sp_id [6];
    uint8           sharedsecret [32];
    uint8           version;
    uint32          timestamp;
    uint8           authsrc [16];
    FRAME_PTR       conpdu;
    uint8           concreated;
    uint8           retrynum;

    /* following para used for receiving data from socket */
    FRAME_PTR       rcvbuf;


    /* following para used for submit pdu sending */
    int             sliding_len;
    int             availwin;
    SubmitPending   subpdu [MAX_SLIDING_WIN];

    /* handling the deliver response */
    CmppPdu       * deliverresp;
    FRAME_PTR       drespdu;

    /* handling the active test */
    FRAME_PTR       actpdu;
    FRAME_PTR       actrespdu;
    CmppPdu       * acttest;
    uint8           actretry;
} CmppCon;

void cmppcon_init     (VTASK * vtask);
void cmppcon_destruct (VTASK * vtask);


/* definition of cmpp entity */

typedef struct cmpp_entity {

    uint8              remote_host[128];   /*get from conf*/
    uint16             remote_port;        /*get from conf*/
    struct in_addr     remote_ip;

    uint8              sp_id [6];        /*get from conf*/
    uint8              sp_passwd [32];   /*get from conf*/

    uint8              mt_num;      /*get from conf*/
    uint8              mo_num;      /*get from conf*/
    uint8              mt_ver;      /*get from conf*/
    uint8              mo_ver;      /*get from conf*/
    uint8              cmpp_con_num;

    int                sliding_len;  /*get from conf*/

    EVENT            * hAvailEvent;
    EVENT            * hRespEvent;
    EVENT            * hDeliverEvent;

    HOOK             * hBuildConn;
    HOOK             * hDistribute;

    STACK            * init_list;
    STACK            * mo_list;

   

    int                total_avail;
    int                consumedwin;

    CRITICAL_SECTION   submitCS;
    LIST             * submits;

    CRITICAL_SECTION   respCS;
    LIST             * submitresps;

    CRITICAL_SECTION   deliverCS;
    LIST             * delivers;

    LIST             * unsucc_submits;

    BmemPool         * mt_pool;
    BmemPool         * mtr_pool;
    BmemPool         * mo_pool;

} CmppEntity;

void cmppent_init      (VTASK * vtask);
void cmppent_destruct  (VTASK * vtask);

void cmpp_type_init (VTASK * type);


void cmpp_schedule (MESSAGE * msg);

int create_cmpp_connect (VTASK * vtask);
int send_cmpp_connect   (VTASK * vtask);

void cmpp_data_recv (VTASK * vtask, int fd, unsigned event);



/* automatically restart CMPP connection mechanism */

int  cmcon_get_unique_id ();
int  cmpp_build_conn     (VTASK * entity, void * userInfo);
void cmpp_conn_succ      (VTASK * vtask,  int fd, unsigned event);



/* data management implemantation */

int if_get_submit_num (VTASK * vtask);
int if_get_submitresp_num (VTASK * vtask);
int if_get_deliver_num (VTASK * vtask);

int get_submit_num (VTASK * vtask);


/* invoked by CMPP IF CMPPSubmit() to save upper
 * submit data to inner list */
int if_store_submit (VTASK * vtask, CmppPdu * submit);

/* fetch one Submit PDU from inner list to distibute 
 * to corresponding CMPP connection for sending.*/
CmppPdu * fetch_submit (VTASK * vtask);

/* according to available sending queue, distribute the
 * Submit data of available number to each CMPP connection.
 * This is one part of flowing control mechanism: sliding window.
 * it is started automatically by events from CMPP IF */
int distribute_submit (VTASK * vtask, void * userinfo);

/* if the CMPP connection crashed, the not-acknowledged Submit
 * will be returned for next delivery via other CMPP channel.
 * this function stores the unsuccessful Submits pending in the
 * queue to System unsucc list. */
int store_unsucc_submit (VTASK * vtask, CmppPdu * submit);

/* recycle submit pdu to unsuccessful-sending queue for 
 * following distribution */
int recycle_submit_pdu (VTASK * vtask, CmppPdu * pdu);

/* get one Submit PDU from unsuccessful-sending queue */
CmppPdu * get_recycle_submit_pdu (VTASK * vtask);



/* manipulation of submit response */
int       store_submit_resp          (VTASK * vtask, CmppPdu * pdu);
int       handle_submit_resp         (VTASK * vtask, CmppPdu * pdu);

int       recycle_submitresp_pdu     (VTASK * vtask, CmppPdu * pdu);
CmppPdu * get_recycle_submitresp_pdu (VTASK * vtask);

int store_deliver (VTASK * vtask, CmppPdu * pdu);
int handle_deliver (VTASK * vtask, CmppPdu * pdu);

int       recycle_deliver_pdu     (VTASK * vtask, CmppPdu * pdu);
CmppPdu * get_recycle_deliver_pdu (VTASK * vtask);

int issue_active_test_resp (VTASK * vtask, CmppPdu * pdu);
int issue_active_test (VTASK * vtask);
int handle_active_test_resp (VTASK * vtask, CmppPdu * pdu);

int issue_terminate_resp (VTASK * vtask, CmppPdu * pdu);



typedef struct cmpp_arg {

    CRITICAL_SECTION   seqidCS;
    uint32             gseqid;

    uint8              initialized;

    FILE             * fpSeq;

    CRITICAL_SECTION   spcmppCS;
    LIST             * spcmpp_list;

} CmppArg;



typedef struct sp_cmpp {

    struct sp_cmpp * prev, * next;

    sem_t          init_end;

    CVOPS_STATE  * cvstate;
    VTASK        * entity;

    pthread_t      thread_id;

    uint8          ismgaddr [128];
    uint16         ismgport;
    uint8          spid [32];
    uint8          secret [32];
    int            mtnum;
    int            monum;
    uint8          mtmajorver;
    uint8          mtminorver;
    uint8          momajorver;
    uint8          mominorver;
    int            slidingwin;

    uint16         routeid;

} SPCMPP;




void * cmppmain (void * arg);



#endif

⌨️ 快捷键说明

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