Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Enduro/X Network Access library (client/server)
0003  *
0004  * @file exnet.h
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #ifndef EXNET_H_
0035 #define EXNET_H_
0036 
0037 /*------------------------------Includes--------------------------------------*/
0038 #include <stdint.h>
0039 #include <sys/socket.h>
0040 #include <sys/time.h>
0041 #include <sys/types.h>
0042 #include <arpa/inet.h>
0043 #include <netinet/in.h>
0044 #include <netdb.h>
0045 #include <nstopwatch.h>
0046 
0047 #include "ndrstandard.h"
0048 /*------------------------------Externs---------------------------------------*/
0049 extern int G_recv_tout;             /* Was there timeout on receive? */
0050 /*------------------------------Macros----------------------------------------*/
0051 #define EXNET_ADDR_LEN      32      /**< Max len for address    */
0052 #define NET_LEN_PFX_LEN         4               /**< Len bytes              */
0053 
0054 #define NDRX_NET_MIN_SIZE       4096            /**< Min net read...        */
0055 
0056 /** Buffer size + netlen */
0057 #define DATA_BUF_MAX                    (NDRX_MSGSIZEMAX - NET_LEN_PFX_LEN)
0058 
0059 #define APPFLAGS_MASK           0x0001  /* Mask the content in prod mode */
0060 #define APPFLAGS_TOUT_OK        0x0002  /* Timeout is OK         */
0061 
0062 /**
0063  * Mark connection as open
0064  */
0065 #define EXNET_CONNECTED(X)  (X)->schedule_close = EXFALSE;\
0066     (X)->is_connected = EXTRUE;\
0067     ndrx_stopwatch_reset(&(X)->last_rcv);\
0068     ndrx_stopwatch_reset(&(X)->last_snd);
0069 
0070 /*------------------------------Enums-----------------------------------------*/
0071 /*------------------------------Typedefs--------------------------------------*/
0072 
0073 /**
0074  * Connection descriptor.
0075  * This structure is universal. It is suitable for Server and client connections.
0076  */
0077 typedef struct exnetcon exnetcon_t;
0078 struct exnetcon
0079 {
0080     /* General config: */
0081     
0082     char port[PATH_MAX];          /**< Service name or port                   */
0083     char addr[PATH_MAX];          /**< IP/DNS Host name                       */
0084     
0085     struct addrinfo *addr_cur;   /**< Current address from bellow list        */
0086     struct addrinfo *addrinfos;  /**< List of resolved IP addresses           */
0087     
0088     /* this is used by clients: */
0089     struct sockaddr_in address;   /**< the libc network address data structure*/
0090     int sock;                     /**< file descriptor for the network socket */    
0091     int is_connected;             /**< Connection state...                    */
0092     int is_server;                /**< Are we server or client?               */
0093     int is_incoming;              /**< Is connection incoming?                */
0094     int schedule_close;           /**< Schedule connection close...           */
0095     int is_ipv6;                  /**< Is socket configured for IPv6?         */
0096     int is_numeric;               /**< Is address numeric form?               */
0097     /* Client properties */
0098     exnetcon_t *my_server;  /**< Pointer to listener structure, used by server, 
0099                              * in case if this was incoming connection */
0100     int rcvtimeout;             /**< Receive timeout                        */
0101     char d[64];                 /**< Data buffer, prefix buffer             */
0102     int  dl;                    /**< Data left in databuffer                */
0103     char *dlsysbuf;             /**< Download sysbuf                        */
0104     int len_pfx;                /**< Length prefix                          */
0105     ndrx_stopwatch_t rcv_timer;     /**< Receive timer...  */
0106     ndrx_stopwatch_t connect_time;  /**< Time of connection in transit..... */
0107     int periodic_zero;              /**< send zero length message in seconds*/
0108     int recv_activity_timeout;      /**< max time into which we must rcv something */
0109     
0110     ndrx_stopwatch_t periodic_stopwatch; /**< Time interval for clocks             */
0111     int periodic_clock_time;              /**< Send clock sync periodically, sec    */
0112     
0113     ndrx_stopwatch_t last_rcv;      /**< Stop watch from last receive from soc */
0114     ndrx_stopwatch_t last_snd;      /**< Stop watch from last send to soc */
0115     
0116     pthread_rwlock_t rwlock;        /**< Needs lock for closing...         */
0117     
0118     MUTEX_VAR(rcvlock);             /**< Receive lock                       */
0119     MUTEX_VAR(sendlock);            /**< Send lock                          */
0120     MUTEX_VAR(flagslock);           /**< Some flags locking (send/rcv timers) */
0121     int lock_init;                  /**< had the lock init performed?         */
0122     /* Server settings */
0123     int backlog;                    /**< Incoming connection queue len (backlog) */
0124     int max_cons;                   /**< Max number of connections we will handle*/
0125     int incomming_cons;             /**< Current number of incoming connections  */
0126 
0127     /* Have some callbacks... */
0128     int (*p_process_msg)(exnetcon_t *net, char **buf, int len); /**< Callback when msg recived  */
0129     int (*p_connected)(exnetcon_t *net);    /**< Callback on even when we are connected */
0130     int (*p_disconnected)(exnetcon_t *net);     /**< Callback on even when we are disconnected */
0131     int (*p_snd_zero_len)(exnetcon_t *net);     /**< Callback for sending zero len msg */
0132     int (*p_snd_clock_sync)(exnetcon_t *net);   /**< Callback for sending clock sync msg */
0133     
0134     /* Stuff for linked list... */
0135     exnetcon_t *next, *prev;
0136 };
0137 
0138 /*------------------------------Globals---------------------------------------*/
0139 /*------------------------------Statics---------------------------------------*/
0140 /*------------------------------Prototypes------------------------------------*/
0141 extern int exnet_send_sync(exnetcon_t *net, char *hdr_buf, int hdr_len, 
0142         char *buf, int len, int flags, int appflags);
0143 extern int exnet_recv_sync(exnetcon_t *net, char **buf, int *len, int flags, int appflags);
0144 
0145 /* <Callback functions will be invoked by ndrxd extensions> */
0146 extern int exnet_b4_poll_cb(void);
0147 extern int exnet_poll_cb(int fd, uint32_t events, void *ptr1);
0148 extern int exnet_periodic(void);
0149 /* </Callback functions will be invoked by ndrxd extensions> */
0150 
0151 extern int exnet_install_cb(exnetcon_t *net, int (*p_process_msg)(exnetcon_t *net, char **buf, int len),
0152         int (*p_connected)(exnetcon_t *net), int (*p_disconnected)(exnetcon_t *net),
0153                 int (*p_snd_zero_len)(exnetcon_t *net), int (*p_snd_clock_sync)(exnetcon_t *net));
0154 extern int exnet_configure(exnetcon_t *net);
0155 extern void exnet_unconfigure(exnetcon_t *net);
0156 extern int exnet_addr_next(exnetcon_t *net);
0157 extern int exnet_addr_get(exnetcon_t *net);
0158 
0159 extern int exnet_is_connected(exnetcon_t *net);
0160 extern int exnet_close_shut(exnetcon_t *net);
0161 extern int exnet_set_timeout(exnetcon_t *net, int timeout);
0162 extern void exnet_reset_struct(exnetcon_t *net);
0163 extern int exnet_net_init(exnetcon_t *net);
0164 extern int exnet_configure_setopts(exnetcon_t *net);
0165 extern int exnet_bind(exnetcon_t *net);
0166 
0167 extern void exnet_rwlock_read(exnetcon_t *net);
0168 extern void exnet_rwlock_write(exnetcon_t *net);
0169 extern void exnet_rwlock_unlock(exnetcon_t *net);
0170 extern void exnet_rwlock_mainth_write(exnetcon_t *net);
0171 extern void exnet_rwlock_mainth_read(exnetcon_t *net);
0172 
0173 extern long exnet_stopwatch_get_delta_sec(exnetcon_t *net, ndrx_stopwatch_t *w);
0174 extern void exnet_stopwatch_reset(exnetcon_t *net, ndrx_stopwatch_t *w);
0175 extern in_port_t exnet_get_port(struct sockaddr *sa);
0176 
0177 /* Connection tracking: */
0178 extern void exnet_add_con(exnetcon_t *net);
0179 extern void exnet_del_con(exnetcon_t *net);
0180 extern exnetcon_t * extnet_get_con_head(void);
0181 extern exnetcon_t *exnet_find_free_conn(void);
0182 extern void exnet_remove_incoming(exnetcon_t *net);
0183 
0184 #endif /* EXNET_H_ */
0185 /* vim: set ts=4 sw=4 et smartindent: */