Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Multi-buffer conversion to / from machine independent network
0003  *  format.
0004  *
0005  * @file exnetproto.h
0006  */
0007 /* -----------------------------------------------------------------------------
0008  * Enduro/X Middleware Platform for Distributed Transaction Processing
0009  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0010  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0011  * This software is released under one of the following licenses:
0012  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0013  * See LICENSE file for full text.
0014  * -----------------------------------------------------------------------------
0015  * AGPL license:
0016  *
0017  * This program is free software; you can redistribute it and/or modify it under
0018  * the terms of the GNU Affero General Public License, version 3 as published
0019  * by the Free Software Foundation;
0020  *
0021  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0022  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0023  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0024  * for more details.
0025  *
0026  * You should have received a copy of the GNU Affero General Public License along 
0027  * with this program; if not, write to the Free Software Foundation, Inc.,
0028  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0029  *
0030  * -----------------------------------------------------------------------------
0031  * A commercial use license is available from Mavimax, Ltd
0032  * contact@mavimax.com
0033  * -----------------------------------------------------------------------------
0034  */
0035 
0036 #ifndef EXNETPROTO_H
0037 #define EXNETPROTO_H
0038 
0039 /*---------------------------Includes-----------------------------------*/
0040 #include <ndrx_config.h>
0041 #include <ubfview.h>
0042 /*---------------------------Externs------------------------------------*/
0043 /*---------------------------Macros-------------------------------------*/
0044 
0045 #define PMSGMAX -2      /**< special case for max buffer size */
0046 
0047 #define TAG_BYTES   2   /**< Number of bytes used in tag */
0048 #define LEN_BYTES   4   /**< Number of bytes used in len */
0049 
0050 #define MKSIGN char sign = '0';\
0051             if (*tmp<0)\
0052             {\
0053                 sign = '1';\
0054             }
0055 
0056 #define OFSZ(s,e)   EXOFFSET(s,e), EXELEM_SIZE(s,e)
0057 #define OFSZ0       0,0
0058 
0059 /**
0060  * Field mode types for cproto_t.type
0061  * @defgroup modetype
0062  * @{
0063  */
0064 #define XFLD           0x01     /**< Normal field                           */
0065 #define XSUB           0x02     /**< Dummy field/subfield                   */
0066 #define XSBL           0x03     /**< Dummy field/subfield len               */
0067 #define XINC           0x04     /**< Include table                          */
0068 #define XLOOP          0x05     /**< Loop construction                      */
0069 #define XATMIBUF       0x06     /**< ATMI buffer type...                    */
0070 #define XMASTERBUF     0x07     /**< internal C tlv list of XATMIBUFS       */
0071 #define XFLDPTR        0x08     /**< Field is pointer to data block         */
0072 #define XATMIBUFPTR    0x09     /**< This is pointer to XATMI buf           */
0073 #define XSUBPTR        0x0B     /**< Ptr to buffer, for doing ex2proto      */
0074 #define XTYPE(X)       (X & 0xff) /**< Extract type infos                   */
0075 
0076 /** @} */ /* end of modetype */
0077 
0078 /**
0079  * Additional flags for mode types
0080  * @defgroup modetype_flags
0081  * @{
0082  */
0083 #define XFLAST         0x0100   /**< Terminat parse after this field        */
0084 
0085 /** @} */ /* end of modetype_flags */
0086 
0087 #define XTAB1(e1)           1, e1, NULL, NULL, NULL
0088 #define XTAB2(e1,e2)        2, e1, e2, NULL, NULL
0089 #define XTAB3(e1,e2,e3)     3, e1, e2, e3, NULL
0090 #define XTAB4(e1,e2,e3,e4)  4, e1, e2, e3, e4
0091 
0092 /**
0093  * Table bellow at index is UBF field type
0094  * the value is type data tag in protocol buffer.
0095  */
0096 /**
0097  * Table bellow at index is UBF field type
0098  * the value is type data tag in protocol buffer.
0099  */
0100 #define UBF_TAG_BFLD_SHORT          0x1113 
0101 #define UBF_TAG_BFLD_LONG           0x111D
0102 #define UBF_TAG_BFLD_CHAR           0x1127
0103 #define UBF_TAG_BFLD_FLOAT          0x1131
0104 #define UBF_TAG_BFLD_DOUBLE         0x113B
0105 #define UBF_TAG_BFLD_STRING         0x1145
0106 #define UBF_TAG_BFLD_CARRAY         0x114F
0107 #define UBF_TAG_BFLD_INT            0x1150 /**< RFU */
0108 #define UBF_TAG_BFLD_RFU0           0x1151 /**< RFU */
0109 #define UBF_TAG_BFLD_PTR            0x1152
0110 #define UBF_TAG_BFLD_UBF            0x1153
0111 #define UBF_TAG_BFLD_VIEW           0x1154
0112 
0113 #define UBF_TAG_BFLDID     0x10FF
0114 #define UBF_TAG_BFLDLEN    0x1109
0115 
0116 #define VIEW_TAG_CNAME     0x134D
0117 #define VIEW_TAG_BFLDLEN   0x1357
0118 
0119 
0120 #define VIEW_TAG_SHORT      0x1360
0121 #define VIEW_TAG_LONG       0x1361
0122 #define VIEW_TAG_CHAR       0x1362
0123 #define VIEW_TAG_FLOAT      0x1363
0124 #define VIEW_TAG_DOUBLE     0x1364
0125 #define VIEW_TAG_STRING     0x1365
0126 #define VIEW_TAG_CARRAY     0x1366
0127 #define VIEW_TAG_INT        0x1367
0128 
0129 /*
0130  * Standard check for output buffer space
0131  */
0132 #define CHECK_PROTO_BUFSZ(RET, CURSIZE, MAXSIZE, TO_WRITE) \
0133     if ((CURSIZE) + TO_WRITE > MAXSIZE) \
0134     {\
0135         NDRX_LOG(log_error, "ERROR ! EX2NET: Message size in bytes max: %ld, at "\
0136                     "current state: %ld, about "\
0137                     "to write: %ld (new total: %d) - EXCEEDS message size. "\
0138                     "Please increase NDRX_MSGSIZEMAX!", \
0139                     (long)MAXSIZE, (long)(CURSIZE), (long)TO_WRITE, (long)(CURSIZE) + (long)TO_WRITE);\
0140         userlog("ERROR ! EX2NET: Message size in bytes max: %ld, at current state: %ld, about "\
0141                     "to write: %ld (new total: %d) - EXCEEDS message size. "\
0142                     "Please increase NDRX_MSGSIZEMAX!", \
0143                     (long)MAXSIZE, (long)(CURSIZE), (long)TO_WRITE, (long)(CURSIZE) + (long)TO_WRITE);\
0144         EXFAIL_OUT(RET);\
0145     }
0146 /*
0147  *  Check the internal incoming buffers, for data overrun
0148  */
0149 #define CHECK_EX_BUFSZ(RET, CUROFFSZ, FLD_OFFSZ, MAXSIZE, TO_WRITE) \
0150 if ((CUROFFSZ) + (FLD_OFFSZ) + (TO_WRITE) > MAXSIZE) \
0151     {\
0152         NDRX_LOG(log_error, "ERROR ! NET2EX: Incomming buffer size in bytes: %ld, incoming "\
0153                             "message is larger %ld (offset: %ld, fld_offs: %ld, "\
0154                             "datasize: %ld) - dropping, please increase NDRX_MSGSIZEMAX!",\
0155                     (long)MAXSIZE, (long)((CUROFFSZ) + (FLD_OFFSZ) + (TO_WRITE)),\
0156                     (long)(CUROFFSZ), (long)(FLD_OFFSZ), (long)(TO_WRITE)\
0157                     );\
0158         userlog("ERROR ! NET2EX: Incomming buffer size in bytes: %ld, incoming "\
0159                             "message is larger %ld (offset: %ld, fld_offs: %ld, "\
0160                             "datasize: %ld) - dropping, please increase NDRX_MSGSIZEMAX!",\
0161                     (long)MAXSIZE, (long)((CUROFFSZ) + (FLD_OFFSZ) + (TO_WRITE)),\
0162                     (long)(CUROFFSZ), (long)(FLD_OFFSZ), (long)(TO_WRITE));\
0163         EXFAIL_OUT(RET);\
0164     }
0165 
0166 #define CHECK_EX_BUFSZ_SIMPLE(RET, MAXSIZE, TO_WRITE) \
0167 if ((TO_WRITE) > MAXSIZE) \
0168     {\
0169         NDRX_LOG(log_error, "ERROR ! NET2EX: Incomming buffer size in bytes: %ld "\
0170                                 "data size: %ld - dropping, please increase NDRX_MSGSIZEMAX!",\
0171                 MAXSIZE, (TO_WRITE));\
0172         userlog( "ERROR ! NET2EX: Incomming buffer size in bytes: %ld "\
0173                                 "data size: %ld - dropping, please increase NDRX_MSGSIZEMAX!",\
0174                 MAXSIZE, (TO_WRITE));\
0175         EXFAIL_OUT(RET);\
0176     }
0177 
0178 
0179 /*---------------------------Enums--------------------------------------*/
0180 /*---------------------------Typedefs-----------------------------------*/
0181 
0182 /* map between C structure and parm bnlock fields */
0183 typedef struct cproto cproto_t;
0184 typedef struct xmsg xmsg_t;
0185 
0186 struct cproto
0187 {
0188     int     tableid;            /**< table id.                      */
0189     long    tag;                /**< TLV tag                        */
0190     char*   cname;              /**< c field name                   */
0191     long    offset;             /**< offset in structure            */
0192     int     len;                /**< len in bytes                   */
0193     int     fld_type;           /**< field type                     */
0194     short   type;               /**< field type:  XFLD or XSUB      */
0195     int     min_len;            /**< min data len in chars          */
0196     int     max_len;            /**< max data len in chars          */
0197     cproto_t *include;          /**< include sub_structure          */
0198     long    counter_offset;     /**< counter offset...              */
0199     long    elem_size;          /**< size of array element.         */
0200     /* During parsing returns conv struct for XSUB */
0201     xmsg_t * (*p_classify_fn) (char *ex_buf, long ex_len); 
0202     
0203     size_t  buftype_offset;     /* buffer type offset */
0204 };
0205 
0206 /* Table used to define network messages. */
0207 struct xmsg
0208 {
0209     char    msg_type;
0210     int     command;            /**< Id of the command... */    
0211     char    *descr;             /**< Descr of the message */
0212     /** Extra tables to drive sub-elements. */
0213     int     tabcnt;
0214     cproto_t    *tab[4];        /**< Recursive/linear tables for sub buffers. */
0215 };
0216 
0217 /* protocol table info */
0218 struct ptinfo
0219 {
0220     int idx;
0221     int dim;
0222 };
0223 typedef struct ptinfo ptinfo_t;
0224 
0225 /* Internal structure for driving UBF fields. */
0226 typedef struct proto_ufb_fld proto_ufb_fld_t;
0227 struct proto_ufb_fld
0228 {
0229     int bfldid;     /**< used also as offset indicator in exbuf for mbuf     */
0230     int bfldlen;    /**< used also as indicator for total buffer len in mbuf */
0231     unsigned typetag;/**< type in mbuf bits                                  */
0232     long typelen;    /**< used for sub-buffers                               */
0233     char cname [NDRX_VIEW_CNAME_LEN+1]; /**< used by views                   */
0234     Bfld_loc_info_t next_fld;          /**< fast add pointers                */
0235     ndrx_typedview_t *v;               /**< keep the ptr to current view     */
0236     ndrx_viewocc_t *vocc;               /**< view occurrence handler for deserialize */
0237     
0238     char buf[0];
0239 };
0240 
0241 /*---------------------------Globals------------------------------------*/
0242 
0243 extern cproto_t ndrx_G_ndrx_mbuf_tlv_x[];
0244 extern short ndrx_G_ubf_proto_tag_map[];
0245 extern short ndrx_G_view_proto_tag_map[];
0246 extern cproto_t ndrx_G_view_field[];
0247 extern cproto_t ndrx_G_view[];
0248 
0249 /*---------------------------Statics------------------------------------*/
0250 /*---------------------------Prototypes---------------------------------*/
0251 
0252 extern int ndrx_write_tag(short tag, char *buf, long *proto_buf_offset, 
0253         long proto_bufsz);
0254 
0255 extern int ndrx_write_len(int len, char *buf, long *proto_buf_offset, 
0256         long proto_bufsz);
0257 
0258 extern int _exproto_proto2ex_mbuf(cproto_t *fld, char *proto_buf, long proto_len, 
0259         char *ex_buf, long ex_offset, long *max_struct, int level, 
0260         UBFH *p_x_fb, proto_ufb_fld_t *p_ub_data, long ex_bufsz);
0261 
0262 extern int exproto_build_ex2proto_mbuf(cproto_t *fld, int level, long offset,
0263         char *ex_buf, long ex_len, char *proto_buf, long *proto_buf_offset,
0264         short *accept_tags, proto_ufb_fld_t *p_ub_data, 
0265         long proto_bufsz);
0266 
0267 extern int exproto_build_ex2proto(xmsg_t *cv, int level, long offset,
0268         char *ex_buf, long ex_len, char *proto_buf, long *proto_buf_offset,
0269         short *accept_tags, proto_ufb_fld_t *p_ub_data, 
0270         long proto_bufsz);
0271 
0272 extern long _exproto_proto2ex(cproto_t *cur, char *proto_buf, long proto_len, 
0273         char *ex_buf, long ex_offset, long *max_struct, int level, 
0274         char *p_typedbuf, proto_ufb_fld_t *p_ub_data, long ex_bufsz);
0275 
0276 
0277 extern ndrx_typedview_t * ndrx_view_get_init(char *vname);
0278 
0279 extern int exproto_build_ex2proto_view(cproto_t *fld, int level, long offset,
0280         char *ex_buf, long ex_len, char *proto_buf, long *proto_buf_offset,
0281         long proto_bufsz);
0282 
0283 #endif  /* EXNETPROTO_H */
0284 
0285 /* vim: set ts=4 sw=4 et smartindent: */