#ifndef _MAPIIPC_H #define _MAPIIPC_H 1 #include "mapi.h" #ifdef DIMAPI #include #include #include #include "flist.h" #ifdef DIMAPISSL #ifdef HAVE_OPENSSL #include #include #else #include #include #endif /* HAVE_OPENSSL */ #endif /* DIMAPISSL */ #endif /* DIMAPI */ #define MAX_SEND_SIZE 8192//1024 #define FUNCTARGS_BUF_SIZE 7168 #define DATA_SIZE 7168 #define DIMAPI_DATA_SIZE 2000000 #define FUNCT_NAME_LENGTH 256 #define ARG_LENGTH 32 #define MAPIDSOCKHOME "%s/.mapid.sock" #define MAPIDSOCKGLOBAL "/tmp/mapid.sock" #define MAPIDGSOCKHOME "%s/.mapid%d.sock" #define MAPIDGSOCKGLOBAL "/tmp/mapid%d.sock" #define ASYN_GNP_BUFFER_SIZE 500 // buffer size for mapi_asynchronous_get_next_pkt() function #define MAX_CAPLEN 1540U #define ASYN_GNP_THRESHOLD 0.1 //All IPC code needs to be rewritten and cleand up. //To support dynamic loading of new functions we should have an IPC //system that do not need to be changed for each new function type //that is added. #ifdef DIMAPISSL struct overload{ SSL * connection; int sock; }; #endif //Messages types that can be sent to/from mapi and mapid typedef enum { CREATE_FLOW, CREATE_FLOW_ACK, APPLY_FUNCTION, APPLY_FUNCTION_ACK, READ_RESULT, READ_RESULT_ACK, CONNECT, CONNECT_ACK, CLOSE_FLOW, CLOSE_FLOW_ACK, READ_ERROR, READ_ERROR_ACK, ERROR_ACK, CREATE_OFFLINE_DEVICE, CREATE_OFFLINE_DEVICE_ACK, START_OFFLINE_DEVICE, START_OFFLINE_DEVICE_ACK, DELETE_OFFLINE_DEVICE, DELETE_OFFLINE_DEVICE_ACK, CREATE_OFFLINE_FLOW, CREATE_OFFLINE_FLOW_ACK, SET_AUTHDATA, SET_AUTHDATA_ACK, AUTHENTICATE, AUTHENTICATE_ACK, LOAD_LIBRARY, LOAD_LIBRARY_ACK, GET_DEVICE_INFO, GET_DEVICE_INFO_ACK, GET_NEXT_DEVICE_INFO, GET_DEVICE_INFO_NACK, GET_LIBRARY_INFO, GET_LIBRARY_INFO_ACK, GET_NEXT_LIBRARY_INFO, GET_LIBRARY_INFO_NACK, GET_FLOW_INFO, GET_FLOW_INFO_ACK, GET_NEXT_FLOW_INFO, GET_FLOW_INFO_NACK, GET_FUNCTION_INFO, GET_FUNCTION_INFO_ACK, GET_NEXT_FUNCTION_INFO, GET_FUNCTION_INFO_NACK, GET_LIBPATH, GET_LIBPATH_ACK, GET_LIBPATH_NACK, GET_LIBS, GET_LIBS_ACK, GET_LIBS_NACK, SEND_FD, GET_NEXT_PKT, GET_NEXT_PKT_ACK, GET_NEXT_PKT_ASYN, GET_NEXT_PKT_ASYN_ACK, IGNORE_SLEEP, // start reconnection ... IGNORE_NOTIFY, READ_RESULT_ACK_RECONNECT, // end reconnection ... MAPI_STATS, MAPI_STATS_ACK, MAPI_STATS_ERR } mapiipcMsg; //Buffer that is sent to/from mapi and mapid struct mapiipcbuf { long mtype; mapiipcMsg cmd; int fd; #ifdef RECONNECT int user_fd; int user_fid; #endif char function[FUNCT_NAME_LENGTH]; int fid; int size; int pid; /* needed to identify target mtype, to send errors when invalid flow-id's are given, when no flow-id is made,... */ //int offset; /* Starting search position from the beginning of packet */ //int depth; /* Maximum search depth from the beginning of search position */ //unsigned long long maxpkts; /* maximum pkt position */ uid_t uid; //UID of the user running the application unsigned char data[DATA_SIZE]; unsigned char argdescr[ARG_LENGTH]; int remote_errorcode; }; //IPC calls //Initialize IPC variables int mapiipc_set_socket_names(char *socket, char *socketglobal); //Initialize IPC functions int mapiipc_client_init(void); void mapiipc_daemon_init(void); //Sends an IPC message int mapiipc_write(struct mapiipcbuf *qbuf); //Reads an IPC message. Blocking call. int mapiipc_read(struct mapiipcbuf *qbuf); //Send a file handle int mapiipc_send_fd(int sendfd); //receive a file handle int mapiipc_read_fd(); //Relase socket resources void mapiipc_client_close(void); void mapiipc_daemon_close(void); #define PKT_LENGTH 131072 // pkt info and actual pkt // large enough for cooking (stream re-assembled packets) #ifdef DIMAPI //holds info about a remote mapid struct host { char* hostname; int port; int sockfd; #ifdef DIMAPISSL SSL *con; SSL_CTX *ctx; #endif int num_flows; // to know when to close the socket flist_t *flows; flist_t* functions; pthread_t* comm_thread; // communication thread #ifdef RECONNECT sem_t connection; // use it in mapiipc.c source code file pthread_mutex_t rec_lock; pthread_cond_t rec_condition; int host_down; #endif flist_t *stats; //for mapi_stats }; //Buffer that is sent to/from mapi and agent struct dmapiipcbuf { unsigned int length; mapiipcMsg cmd; int fd; int fid; unsigned long long timestamp; char data[DIMAPI_DATA_SIZE]; }; #define BASIC_SIZE (sizeof(struct dmapiipcbuf) - DIMAPI_DATA_SIZE) typedef struct host_flow { struct host *rhost; char* dev; char *devtype; int scope_fd; int fd; //fd of flow in the mapid of host int id; int sockfd_asyn; #ifdef DIMAPISSL SSL *con_asyn; SSL_CTX *ctx_asyn; #endif pthread_t* asyn_comm_thread; struct dmapiipcbuf *dbuf; //buffer for writting results from this host -for this flow- struct mapipkt* pkt; struct asyn_mgnp_buffer *asyn_pkts; // used by mapi_asynchronous_get_next_pkt() pthread_spinlock_t asyn_get_next_pkt_lock; // used by mapi_asynchronous_get_next_pkt() flist_t *functions; //holds all fids for this host_flow } host_flow; typedef struct remote_flowdescr { int fd; // 'scope' fd int scope_size; flist_t* host_flowlist; flist_t* pkt_list; //FIFO list for get_next_pkt sem_t fd_sem; sem_t pkt_sem; flist_t* function_res; unsigned char is_connected; // This should be 1 if the flow is connected 0 otherwise #ifdef WITH_AUTHENTICATION unsigned char is_authenticated; char *username; char *vo; #ifdef RECONNECT char *password; #endif #endif #ifdef RECONNECT int to_buffer_fid; // fid returned to user, in response to to_buffer() function apply [used by mapi_get_next_pkt()] int to_buffer_fid_asyn; // fid returned to user, in response to to_buffer() function apply [used by mapi_asynchronous_get_next_pkt()] int daemons_down; #endif struct mapipkt* pkt; unsigned char is_asyn_gnp_called; // this should be 1 if the remote flow has called mapi_asynchronous_get_next_pkt(), 0 otherwise flist_node_t *asyn_mgnp_fnode; // the last host that returned a packet to a client } remote_flowdescr_t; struct asyn_mgnp{ // used by mapi_asynchronous_get_next_pkt() struct dmapiipcbuf *dbuf; int sock; int sent_packets; #ifdef DIMAPISSL SSL *con; #endif }; struct asyn_mgnp_buffer{ // used by mapi_asynchronous_get_next_pkt() function struct mapipkt **pkts; // buffer for saving packets int read; // index for read packet int write; // index for write packet int size; // the amount of packets that are stored in the buffer }; //dmapi functions and ipcbuffer int mapiipc_remote_init(struct host *h); int mapiipc_remote_init_asyn(struct host *h, struct host_flow *hflow); int mapiipc_remote_write(struct dmapiipcbuf *dbuf, struct host *h); int mapiipc_remote_write_asyn(struct dmapiipcbuf *dbuf, struct host_flow *hflow); int mapiipc_remote_write_to_all(remote_flowdescr_t* rflow); void mapiipc_remote_close(struct host *h); void mapiipc_remote_close_asyn(struct host_flow *hflow); void *mapiipc_comm_thread(void *host); void *mapiipc_asyn_comm_thread(void *host); /* Read "n" bytes from a socket. */ ssize_t readn(int fd, void *vptr, size_t n); #ifdef DIMAPISSL ssize_t SSL_readn(SSL *con, void *vptr, size_t n); #endif void check_for_read_results(struct host *h); #endif//DIMAPI #ifdef RECONNECT int check_network_mapid(void); // checks if network (mapid) is up. Returns 1 if network is up, 0 otherwise #endif #define INT 1 #define STRING 2 #define UNSIGNED_LONG_LONG 3 extern void addarg(mapiFunctArg **pos, void *arg, int type); extern int getargint(mapiFunctArg **pos); extern char getargchar(mapiFunctArg **pos); extern char * getargstr(mapiFunctArg **pos); extern unsigned long long getargulonglong(mapiFunctArg **pos); #endif//_MAPIIPC_H