mapiipc.h 5.29 KB
Newer Older
Arne Øslebø's avatar
Arne Øslebø committed
1 2
#ifndef _MAPIIPC_H
#define _MAPIIPC_H 1
's avatar
committed
3

Arne Øslebø's avatar
Arne Øslebø committed
4 5
#include "mapi.h"

's avatar
committed
6 7 8 9 10
#ifdef DIMAPI
#include <netdb.h>
#include <pthread.h>
#include <semaphore.h>
#include "flist.h"
11 12 13 14 15 16 17 18 19 20
#ifdef DIMAPISSL
#ifdef HAVE_OPENSSL
#include <openssl/ssl.h>
#include <openssl/err.h>
#else
#include <ssl.h>
#include <err.h>
#endif /* HAVE_OPENSSL */
#endif /* DIMAPISSL */
#endif /* DIMAPI */
's avatar
committed
21

Arne Øslebø's avatar
Arne Øslebø committed
22 23 24
#define MAX_SEND_SIZE 8192//1024
#define FUNCTARGS_BUF_SIZE 7168
#define DATA_SIZE 7168
's avatar
fix  
committed
25
#define DIMAPI_DATA_SIZE 50000
Arne Øslebø's avatar
Arne Øslebø committed
26 27
#define FUNCT_NAME_LENGTH 256
#define ARG_LENGTH 32
's avatar
committed
28 29
#define MAPIDSOCKHOME "%s/.mapid.sock"
#define MAPIDSOCKGLOBAL "/tmp/mapid.sock"
Arne Øslebø's avatar
Arne Øslebø committed
30 31 32 33 34 35

//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.

's avatar
committed
36 37 38 39 40 41 42
#ifdef DIMAPISSL
static SSL_CTX *ctx;
struct overload{
	SSL * connection;
	int sock;
};
#endif
43

Arne Øslebø's avatar
Arne Øslebø committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
//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,
's avatar
committed
59 60 61 62 63 64
  CREATE_OFFLINE_DEVICE,
  CREATE_OFFLINE_DEVICE_ACK,
  START_OFFLINE_DEVICE,
  START_OFFLINE_DEVICE_ACK,
  DELETE_OFFLINE_DEVICE,
  DELETE_OFFLINE_DEVICE_ACK,
Arne Øslebø's avatar
Arne Øslebø committed
65 66 67 68
  CREATE_OFFLINE_FLOW,
  CREATE_OFFLINE_FLOW_ACK,
  SET_AUTHDATA,
  SET_AUTHDATA_ACK,
's avatar
 
committed
69 70
  AUTHENTICATE,
  AUTHENTICATE_ACK,
Arne Øslebø's avatar
Arne Øslebø committed
71 72
  LOAD_LIBRARY,
  LOAD_LIBRARY_ACK,
73 74 75 76 77 78 79 80
  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,
Arne Øslebø's avatar
Arne Øslebø committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94
  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,
's avatar
committed
95 96 97
  SEND_FD,
  GET_NEXT_PKT,
  GET_NEXT_PKT_ACK
Arne Øslebø's avatar
Arne Øslebø committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
} mapiipcMsg;


//Buffer that is sent to/from mapi and mapid
struct mapiipcbuf {
  long mtype;
  mapiipcMsg cmd;
  int fd;
  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];
116
  int remote_errorcode;
Arne Øslebø's avatar
Arne Øslebø committed
117 118 119 120 121
};

//IPC calls

//Initialize IPC functions
's avatar
committed
122
int mapiipc_client_init(void);
Arne Øslebø's avatar
Arne Øslebø committed
123 124 125
void mapiipc_daemon_init(void);

//Sends an IPC message
126
int mapiipc_write(struct mapiipcbuf *qbuf);
Arne Øslebø's avatar
Arne Øslebø committed
127
//Reads an IPC message. Blocking call.
's avatar
committed
128
int mapiipc_read(struct mapiipcbuf *qbuf);
Arne Øslebø's avatar
Arne Øslebø committed
129 130 131 132 133 134 135 136 137 138 139


//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);

's avatar
committed
140 141 142 143 144 145
#ifdef DIMAPI
//holds info about a remote mapid
struct host {
  char* hostname;
  int port;
  int sockfd;
's avatar
committed
146 147 148
#ifdef DIMAPISSL
  SSL *con;
#endif
's avatar
committed
149 150
  int num_flows;	//to know when to close the socket
  flist_t *flows;
's avatar
committed
151
  flist_t* functions;
's avatar
committed
152 153
  pthread_t* comm_thread;	//communication thread
};
Arne Øslebø's avatar
Arne Øslebø committed
154

's avatar
committed
155 156 157 158 159 160
//Buffer that is sent to/from mapi and agent
struct dmapiipcbuf {
  unsigned int length;
  mapiipcMsg cmd;
  int fd;
  int fid;
's avatar
committed
161
  unsigned long long timestamp;
's avatar
committed
162
  char data[DIMAPI_DATA_SIZE];
's avatar
committed
163 164
};

's avatar
committed
165
#define BASIC_SIZE (sizeof(struct dmapiipcbuf) - DIMAPI_DATA_SIZE)
166
#define PKT_LENGTH 131072	//pkt info and actual pkt
's avatar
committed
167 168 169 170 171 172 173 174

typedef struct host_flow {
	struct host *rhost;
	char* dev;
	int scope_fd;
	int fd;		//fd of flow in the mapid of host
	int id;
	struct dmapiipcbuf *dbuf;	//buffer for writting results from this host -for this flow-
's avatar
 
committed
175
	struct mapipkt* pkt;
's avatar
committed
176 177 178 179 180
	flist_t *functions;	//holds all fids for this host_flow
} host_flow;

typedef struct remote_flowdescr {
  int fd;			// 'scope' fd
's avatar
committed
181
  int scope_size;
's avatar
committed
182 183 184
  flist_t* host_flowlist;
  flist_t* pkt_list;		//FIFO list for get_next_pkt
  sem_t fd_sem;
's avatar
committed
185
  sem_t pkt_sem;
's avatar
committed
186 187
  //unsigned int pending_msgs;
  //pthread_mutex_t mutex;
's avatar
 
committed
188
  //struct mapipkt* pkt;	DELETE
's avatar
committed
189
  flist_t* function_res;
190
  unsigned char is_connected; // This should be 1 if the flow is connected 0 otherwise
191 192 193
/*  int error;
  char errstr[MAPI_ERRORSTR_LENGTH];
*/
194 195 196 197 198
#ifdef WITH_AUTHENTICATION
  unsigned char is_authenticated;
  char *username;
  char *vo;
#endif
's avatar
committed
199 200 201 202
} remote_flowdescr_t;


//dmapi functions and ipcbuffer
's avatar
committed
203 204 205
int mapiipc_remote_init(struct host *h);
int mapiipc_remote_write(struct dmapiipcbuf *dbuf, struct host *h);
int mapiipc_remote_write_to_all(remote_flowdescr_t* rflow);
's avatar
committed
206 207 208
void mapiipc_remote_close(struct host *h);
void *mapiipc_comm_thread(void *host);

's avatar
committed
209 210
/* Read "n" bytes from a socket. */
ssize_t readn(int fd, void *vptr, size_t n);
's avatar
committed
211 212 213
#ifdef DIMAPISSL
ssize_t SSL_readn(SSL *con, void *vptr, size_t n);
#endif
's avatar
committed
214 215 216

#endif//DIMAPI

Arne Øslebø's avatar
Arne Øslebø committed
217 218 219 220 221

#define INT 1
#define STRING 2
#define UNSIGNED_LONG_LONG 3

's avatar
 
committed
222 223 224 225 226 227 228 229
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