00001 #ifndef IX_TOOLS_I_H
00002 #define IX_TOOLS_I_H
00003
00004 #include <pthread.h>
00005 #include <netinet/in.h>
00006 #include <netdb.h>
00007 #include <values.h>
00008 #include <limits.h>
00009 #include <sys/time.h>
00010 #include <string.h>
00011 #include <stdlib.h>
00012 #include <stdio.h>
00013 #include <unistd.h>
00014 #include "ix_tools.h"
00015
00016 #ifdef DEBUG
00017 #define VERBOSEDEBUG
00018 #endif
00019
00020 #define min(a,b) ((a)<(b)?(a):(b))
00021 #define max(a,b) ((a)>(b)?(a):(b))
00022
00023
00024
00025
00026 typedef enum {
00027 I_IX_V_LOG = 0x00,
00028 I_IX_V_ALERT = 0x01,
00029 I_IX_V_ERROR = 0x002,
00030 I_IX_V_DISP = 0x004,
00031 I_IX_V_TIMER = 0x008,
00032 I_IX_V_MSG = 0x010,
00033 I_IX_V_EV = 0x020,
00034 I_IX_V_THREAD = 0x040,
00035 I_IX_V_FD = 0x080,
00036 I_IX_V_SHM = 0x100,
00037 I_IX_V_SIG = 0x200,
00038 I_IX_V_ALL = 0xFFFFFFFF
00039 } i_ix_verbose_e;
00040
00041
00042 #define IX_EV_MAX 0xFFFF
00043
00044 #define IX_VMS_EVCLUSTER_SIZE 32
00045
00046
00047 #define IX_MAX_EVENTS 288
00048
00049
00050 #define IX_MAX_USER_EVENTS 128
00051
00052
00053
00054
00055 #define IX_MAX_ADM_EVENTS 256
00056
00057
00058 #define IX_SYS_EVENT0 IX_MAX_ADM_EVENTS
00059
00060
00061
00062
00063
00064
00065 #define IX_VMS_EVCLUSTER_MAX (IX_MAX_USER_EVENTS/IX_VMS_EVCLUSTER_SIZE)
00066
00067
00068
00069 #define IX_MAX_EVENT_LISTENER 256
00070
00071
00072 #define MAX_TIMER_POOL 512
00073
00074
00075 #define IX_MAX_SHM 16
00076
00077
00078 #define IX_PORT_START 50000
00079 #define IX_PORT_END 65535
00080
00081
00082 #define MAX_TCP_CLIENT_POOL 300
00083
00084
00085 #define MAX_TCP_SERVER_POOL 256
00086
00087
00088 #define IX_MAX_FD 256
00089
00090
00091 #define IX_CONN_ID 0x4958 // IX
00092
00093
00094 #define IX_NET_CONN_ID 0x434E // CN
00095
00096
00097 #define IX_DATA_CONN_ID 0x4344 // CD
00098
00099
00100 #define IX_SOCKET_LISTEN_QUEUE 8
00101
00102
00103
00104
00105 #define IX_SOCKET_CONNECT_RETRY 3
00106
00107
00108 #define IX_SOCKET_CONNECT_MICROSEC_WAIT 3000
00109
00110
00111 #define IX_SEND_TIO 10
00112
00113
00114 #define IX_MAX_NET_MSG_LEN (1024*32)
00115
00116
00117 #define IX_MAX_SIGNAL 32
00118
00119
00120
00121
00122 #define IX_NET_ID 0x5800
00123
00124
00125 #define IX_MAX_USER_HOOK 16
00126
00127
00128 #define IX_NET_V1 0x0001
00129
00130
00131 #define IX_LBE 0xFF00 // test for little or big endian
00132
00133
00134 #define IX_INTERN_ID 0x5849
00135 #define IX_TIMER_NEW 0x4E54
00136 #define IX_TIMER_DEL 0x4454
00137 #define IX_CONNECT 0x4343
00138 #define IX_FD 0x4643
00139 #define IX_SIGNAL 0x4743
00140 #define IX_CLIENT_RM 0x4843
00141 #define IX_SERVER_ACTION 0x4943
00142
00143
00144 #define IX_TIMER_INIT 0x00
00145 #define IX_TIMER_ACTIVE 0x01
00146 #define IX_TIMER_SYSTEM 0x02
00147
00148
00149 #define IX_SYSTIMER_SEC 1
00150
00151
00152
00153 #define IX_CLIENT_TIMEOUT 300
00154
00155
00156 #define IX_WRITE_TIMEOUT 4
00157
00158
00159 #define IX_REPLY_TIMEOUT 8
00160
00161
00162 #define IX_SERVER_ACTION_TIMEOUT 2
00163
00164
00165
00166
00167 #define MAX_THREAD_POOL 512
00168
00169
00170 #define IX_MSG_MAX 512
00171
00172
00173 #define IX_HOST_NAME_MAX 64
00174
00175 #define IX_SEM_FN "SEM_GROUP"
00176
00177
00178 struct ix_cs_s
00179 {
00180 pthread_mutex_t mt;
00181 };
00182 int ix_cs_init( ix_cs_t *pcs, ix_cs_e cs_type );
00183
00184 #define IX_SEM_UNLOCK 0x00
00185 #define IX_SEM_LOCK 0x01
00186 #define IX_SEM_POLLNO 3
00187
00188 struct ix_sem_s
00189 {
00190 int id;
00191 int fd;
00192 char fn[PATH_MAX];
00193 int state;
00194 int mode;
00195
00196 ix_event_t *pev;
00197 int group;
00198 ix_cs_t cs;
00199 };
00200
00201 struct ix_fd_s
00202 {
00203 int fd;
00204 ix_event_t *pev;
00205 int flag;
00206 ix_cs_t cs;
00207 IX_FD_FCT cb;
00208 void *userdata;
00209 };
00210
00211
00212 struct ix_evl_s
00213 {
00214 pthread_cond_t cond;
00215 pthread_mutex_t mut;
00216 int id;
00217 };
00218
00219
00220 struct ix_evs_s
00221 {
00222 int no;
00223 ix_event_t *ev[IX_MAX_EVENTS];
00224 int evl_init;
00225 ix_evl_t evl;
00226 };
00227
00228
00229 struct ix_event_s
00230 {
00231 unsigned long id_intern;
00232 int type;
00233 int set;
00234 int id;
00235 ix_cs_t cs;
00236 ix_list_t *listener;
00237 ix_list_t *appl_listener;
00238 void *userdata;
00239 };
00240
00241
00242 struct ix_thread_s
00243 {
00244 pthread_t pth;
00245 pthread_attr_t attr;
00246 char name[64];
00247 IX_START_FCT sfct;
00248 IX_EXIT_FCT efct;
00249 void *parg;
00250 pthread_key_t *pkey;
00251 int exit_flag;
00252 int init;
00253 struct ix_thread_s *pme;
00254 };
00255
00256
00257 struct ix_appl_s
00258 {
00259 int socketfd;
00260 int state;
00261 struct in_addr hostaddr;
00262 unsigned short applport;
00263 char hostname[PATH_MAX];
00264 char applname[PATH_MAX];
00265 };
00266
00267
00268 struct ix_timer_s
00269 {
00270 unsigned long id;
00271 unsigned long userid;
00272 ix_event_t *pev;
00273 ix_cs_t cs;
00274 int flag;
00275 struct timeval tv;
00276 struct timeval tv_per;
00277 void (*pcb) ();
00278 void *parg;
00279 };
00280
00281
00282 struct ix_msg_queue_s
00283 {
00284 ix_queue_t *pq;
00285 ix_event_t *pev;
00286 };
00287
00288 struct ix_qio_s
00289 {
00290 int fd;
00291 int ctrl_tag;
00292 ix_qio_ctrl_t ctrl;
00293 IX_QIO_SAVE_FCT save_fct;
00294 void *userarg;
00295 ix_msg_queue_t *pmq;
00296 ix_buffer_t *pbuf;
00297 int len;
00298 ix_cs_t cs;
00299 ix_fd_t *pfd;
00300 int status;
00301 };
00302
00303
00304 typedef struct
00305 {
00306 int key;
00307 IX_AST_PACKET_FCT user_hook;
00308 } hook_entry_t;
00309
00310
00311 typedef struct
00312 {
00313 IX_AST_PACKET_FCT hook_fct;
00314 ix_cs_t cs;
00315 int blocked;
00316 hook_entry_t hook[IX_MAX_USER_HOOK];
00317 int hook_count;
00318 } ix_receive_t;
00319
00320
00321
00322 typedef enum
00323 {
00324 IX_NET_MIN = 0x00,
00325 IX_NET_EVENT = 0x01,
00326 IX_NET_MSGUNCONF = 0x02,
00327 IX_NET_SEND = 0x05,
00328 IX_NET_REPLY = 0x06,
00329 IX_NET_NOTACK = 0x07,
00330 IX_NET_ERROR = 0xFF,
00331 IX_NET_MAX,
00332 IX_NET_VAL = 0xFFFF
00333 } ix_net_type_e;
00334
00335 typedef struct ix_hdr_s
00336 {
00337 unsigned short id;
00338 unsigned short lbe;
00339
00340
00341
00342
00343 unsigned short type;
00344 unsigned int len;
00345 unsigned short port;
00346
00347 } ix_hdr_t;
00348
00349 struct ix_net_s
00350 {
00351 ix_hdr_t hdr;
00352 ix_adr_t from;
00353 ix_adr_t to;
00354 ix_netinfo_t info;
00355 union
00356 {
00357 IX_SERVER_DISCONNECT_FCT disconn_cb;
00358 long long int align;
00359 };
00360 ix_buffer_t b;
00361 unsigned char msg[IX_MAX_NET_MSG_LEN];
00362 };
00363
00364 typedef unsigned int ix_cluster_t;
00365
00366 typedef struct
00367 {
00368 unsigned short ix_id;
00369 unsigned short conn_id;
00370 } ix_conn_t;
00371
00372
00373 #define IX_TCP_INIT 0x00
00374 #define IX_TCP_CONNECTED 0x01
00375 #define IX_NET_CONNECTED 0x02
00376 #define IX_DATA_CONNECTED 0x04
00377
00378
00379
00380 struct ix_tcp_client_s
00381 {
00382 int fd;
00383 ix_adr_t adr;
00384 int state;
00385 ix_net_t *pnet;
00386 time_t access_time;
00387 int timeout;
00388 ix_cs_t cs;
00389 void *userdata;
00390
00391 int header_f;
00392 int len;
00393 char *buffer;
00394 };
00395
00396
00397
00398 typedef int( * IX_SERVER_READ_FCT)( void *ps );
00399
00400 typedef enum
00401 {
00402 IX_SERVER_INIT = 0x00,
00403 IX_SERVER_ERROR = 0x01,
00404 IX_SERVER_SENT = 0x02
00405
00406 } ix_server_state_e;
00407
00408 typedef struct ix_tcp_server_s
00409 {
00410 int fd;
00411 ix_adr_t adr;
00412 struct ix_cs_s cs;
00413 struct ix_cs_s socket_cs;
00414 ix_net_t *pnet;
00415 int shared;
00416 int state;
00417 ix_cond_t sync;
00418 IX_SERVER_DISCONNECT_FCT disconn_cb;
00419
00420 int header_f;
00421 int len;
00422 char *buffer;
00423 } ix_tcp_server_t;
00424
00425 struct ix_signal_s
00426 {
00427 int signal;
00428 ix_event_t *pev;
00429 void *userdata;
00430 IX_SIGNAL_FCT cb;
00431 };
00432
00433 int rm_tcp_server( ix_tcp_server_t *ps );
00434
00435
00436 typedef struct ix_init_s
00437 {
00438 int done;
00439 sigset_t sigmask_block;
00440 char proc_name[PATH_MAX];
00441
00442 int node;
00443
00444 ix_list_t *ptp;
00445 ix_list_t *pti;
00446 ix_cs_t ev_cs;
00447 ix_cs_t server_cs;
00448 ix_cs_t fdcs;
00449 ix_cs_t initcs;
00450 ix_event_t *event[IX_MAX_EVENTS];
00451 ix_cluster_t ev_cluster[IX_VMS_EVCLUSTER_MAX];
00452 int pipe[2] ;
00453 int udp_out;
00454 char udp_host[IX_HOST_NAME_MAX];
00455 int udp_port;
00456 int udp_fd;
00457 char tcp_host[IX_HOST_NAME_MAX];
00458 int tcp_port;
00459 int tcp_fd;
00460 ix_list_t *ptcpcl;
00461 ix_list_t *ptcpsl;
00462 ix_list_t *pfdl;
00463 fd_set rfds;
00464 fd_set wfds;
00465 fd_set efds;
00466 fd_set active_rfds;
00467 fd_set active_wfds;
00468 fd_set active_efds;
00469 int nfds;
00470 int active_nfds;
00471 ix_cs_t fd_cs;
00472 ix_msg_queue_t msg;
00473 ix_receive_t recv;
00474 int client_timeout;
00475 ix_list_t *pshml;
00476 IX_CLIENT_CONNECT_FCT client_connect_cb;
00477 IX_CLIENT_REPLY_FCT client_reply_cb;
00478 IX_CLIENT_DATA_FCT client_data_cb;
00479 IX_CLIENT_DISCONNECT_FCT client_disconnect_cb;
00480 IX_SERVER_DISCONNECT_FCT server_disconnect_cb;
00481 struct ix_signal_s * signal[IX_MAX_SIGNAL];
00482 IX_EVENT_HOOK_FCT event_hook_fct;
00483 } ix_init_t;
00484
00485 void i_ix_log_level( unsigned long flag );
00486 void i_ix_log( i_ix_verbose_e flags, char *text, ... );
00487 void _ix_evl_signal( ix_evl_t *pevl );
00488 int _ix_evl_wait( ix_evl_t *pevl, ix_evs_t *mask, ix_evs_t *set,
00489 int msec );
00490 inline ix_event_t *_ix_evs_add( ix_evs_t *pevs, ix_event_t *pev );
00491 void ix_evl_init( ix_evl_t *pevl );
00492 int start_dispatcher(void);
00493 int snd_timer(void );
00494 int snd_fd( void );
00495 void *dispatch_fct( void *arg);
00496 int receive_init(void );
00497 int ix_udp_send( ix_net_t *pnet );
00498 int ix_tcp_send( ix_net_t *pnet, ix_net_t *preply );
00499 int snd_connect( void );
00500 ix_event_t *_ix_event_new( ix_cs_t *pcs, int id, int flags, void *userdata );
00501 int add_selrfd( int fd );
00502
00503
00504
00505
00506 int add_dyn_selrfd( int fd );
00507 int add_dyn_selwfd( int fd );
00508 int add_dyn_selefd( int fd );
00509 int rm_dyn_selrfd( int fd );
00510 int rm_dyn_selwfd( int fd );
00511 int rm_dyn_selefd( int fd );
00512 int init_selfd(void );
00513 int init_fds(void);
00514
00515 int ix_buffer_init( ix_buffer_t *pb, unsigned char *pbuf, size_t len );
00516
00517
00518 int ix_msg_queue_init( ix_msg_queue_t *pmq, ix_event_t *pev );
00519 int ix_msg_queue_size_init( ix_msg_queue_t *pmq, ix_event_t *pev, size_t size );
00520 int fd_comp_fct( const void *pth1, const void *pth2 );
00521 int shm_comp_fct( const void *pshm1, const void *pshm2 );
00522
00523
00524
00525 #endif
00526