mapiipc.h 7.71 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
25
#define DIMAPI_DATA_SIZE 2000000
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"
30
31
#define MAPIDGSOCKHOME "%s/.mapid%d.sock"
#define MAPIDGSOCKGLOBAL "/tmp/mapid%d.sock"
Arne Øslebø's avatar
Arne Øslebø committed
32

33
34
35
36
#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

Arne Øslebø's avatar
Arne Øslebø committed
37
38
39
40
41
//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
42
43
44
45
46
47
#ifdef DIMAPISSL
struct overload{
	SSL * connection;
	int sock;
};
#endif
48

Arne Øslebø's avatar
Arne Øslebø committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//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
64
65
66
67
68
69
  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
70
71
72
73
  CREATE_OFFLINE_FLOW,
  CREATE_OFFLINE_FLOW_ACK,
  SET_AUTHDATA,
  SET_AUTHDATA_ACK,
's avatar
   
committed
74
75
  AUTHENTICATE,
  AUTHENTICATE_ACK,
Arne Øslebø's avatar
Arne Øslebø committed
76
77
  LOAD_LIBRARY,
  LOAD_LIBRARY_ACK,
78
79
80
81
82
83
84
85
  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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  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
100
101
  SEND_FD,
  GET_NEXT_PKT,
's avatar
committed
102
  GET_NEXT_PKT_ACK,
103
104
  GET_NEXT_PKT_ASYN,
  GET_NEXT_PKT_ASYN_ACK,
105
  IGNORE_SLEEP,			// start reconnection ...
106
  IGNORE_NOTIFY,
107
  READ_RESULT_ACK_RECONNECT,	// end reconnection ...
108
109
110
  MAPI_STATS,
  MAPI_STATS_ACK,
  MAPI_STATS_ERR
Arne Øslebø's avatar
Arne Øslebø committed
111
112
113
114
115
116
117
118
} mapiipcMsg;


//Buffer that is sent to/from mapi and mapid
struct mapiipcbuf {
  long mtype;
  mapiipcMsg cmd;
  int fd;
119
120
121
122
#ifdef RECONNECT
  int user_fd;
  int user_fid;
#endif
Arne Øslebø's avatar
Arne Øslebø committed
123
124
125
126
127
128
129
130
131
132
  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];
133
  int remote_errorcode;
Arne Øslebø's avatar
Arne Øslebø committed
134
135
136
137
};

//IPC calls

138
//Initialize IPC variables
's avatar
committed
139
int mapiipc_set_socket_names(char *socket, char *socketglobal);
140

Arne Øslebø's avatar
Arne Øslebø committed
141
//Initialize IPC functions
's avatar
committed
142
int mapiipc_client_init(void);
Arne Øslebø's avatar
Arne Øslebø committed
143
144
145
void mapiipc_daemon_init(void);

//Sends an IPC message
146
int mapiipc_write(struct mapiipcbuf *qbuf);
Arne Øslebø's avatar
Arne Øslebø committed
147
//Reads an IPC message. Blocking call.
's avatar
committed
148
int mapiipc_read(struct mapiipcbuf *qbuf);
Arne Øslebø's avatar
Arne Øslebø committed
149
150
151
152
153
154
155
156
157
158

//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
159
160
161
#define PKT_LENGTH 131072               // pkt info and actual pkt
					// large enough for cooking (stream re-assembled packets)

's avatar
committed
162
163
164
165
166
167
#ifdef DIMAPI
//holds info about a remote mapid
struct host {
  char* hostname;
  int port;
  int sockfd;
's avatar
committed
168
169
#ifdef DIMAPISSL
  SSL *con;
170
  SSL_CTX *ctx;
's avatar
committed
171
#endif
172
  int num_flows;		// to know when to close the socket
's avatar
committed
173
  flist_t *flows;
's avatar
committed
174
  flist_t* functions;
175
176
177
  pthread_t* comm_thread;	// communication thread
#ifdef RECONNECT
  sem_t connection;		// use it in mapiipc.c source code file
178
179
180
  pthread_mutex_t rec_lock;
  pthread_cond_t rec_condition;
  int host_down;
181
#endif
182
  flist_t *stats;		//for mapi_stats
's avatar
committed
183
};
Arne Øslebø's avatar
Arne Øslebø committed
184

's avatar
committed
185
186
187
188
189
190
//Buffer that is sent to/from mapi and agent
struct dmapiipcbuf {
  unsigned int length;
  mapiipcMsg cmd;
  int fd;
  int fid;
's avatar
committed
191
  unsigned long long timestamp;
's avatar
committed
192
  char data[DIMAPI_DATA_SIZE];
's avatar
committed
193
194
};

's avatar
committed
195
#define BASIC_SIZE (sizeof(struct dmapiipcbuf) - DIMAPI_DATA_SIZE)
's avatar
committed
196
197
198
199

typedef struct host_flow {
	struct host *rhost;
	char* dev;
200
	char *devtype;
's avatar
committed
201
202
203
	int scope_fd;
	int fd;		//fd of flow in the mapid of host
	int id;
204
205
206
207
208
209
	int sockfd_asyn;
#ifdef DIMAPISSL
	SSL *con_asyn;
	SSL_CTX *ctx_asyn;
#endif
	pthread_t* asyn_comm_thread;
's avatar
committed
210
	struct dmapiipcbuf *dbuf;	//buffer for writting results from this host -for this flow-
's avatar
   
committed
211
	struct mapipkt* pkt;
212
213
	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()
's avatar
committed
214
215
216
217
218
	flist_t *functions;	//holds all fids for this host_flow
} host_flow;

typedef struct remote_flowdescr {
  int fd;			// 'scope' fd
's avatar
committed
219
  int scope_size;
's avatar
committed
220
221
222
  flist_t* host_flowlist;
  flist_t* pkt_list;		//FIFO list for get_next_pkt
  sem_t fd_sem;
's avatar
committed
223
  sem_t pkt_sem;
's avatar
committed
224
  flist_t* function_res;
225
  unsigned char is_connected; // This should be 1 if the flow is connected 0 otherwise
226
227
228
229
#ifdef WITH_AUTHENTICATION
  unsigned char is_authenticated;
  char *username;
  char *vo;
230
231
232
233
234
#ifdef RECONNECT
  char *password;
#endif
#endif
#ifdef RECONNECT
235
236
  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()]
237
  int daemons_down;  
238
#endif
's avatar
committed
239
  struct mapipkt* pkt;
240
241
  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 
's avatar
committed
242
243
} remote_flowdescr_t;

244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
};
's avatar
committed
261
262

//dmapi functions and ipcbuffer
's avatar
committed
263
int mapiipc_remote_init(struct host *h);
264
int mapiipc_remote_init_asyn(struct host *h, struct host_flow *hflow);
's avatar
committed
265
int mapiipc_remote_write(struct dmapiipcbuf *dbuf, struct host *h);
266
int mapiipc_remote_write_asyn(struct dmapiipcbuf *dbuf, struct host_flow *hflow);
's avatar
committed
267
int mapiipc_remote_write_to_all(remote_flowdescr_t* rflow);
's avatar
committed
268
void mapiipc_remote_close(struct host *h);
269
void mapiipc_remote_close_asyn(struct host_flow *hflow);
's avatar
committed
270
void *mapiipc_comm_thread(void *host);
271
void *mapiipc_asyn_comm_thread(void *host);
's avatar
committed
272

's avatar
committed
273
274
/* Read "n" bytes from a socket. */
ssize_t readn(int fd, void *vptr, size_t n);
's avatar
committed
275
276
277
#ifdef DIMAPISSL
ssize_t SSL_readn(SSL *con, void *vptr, size_t n);
#endif
's avatar
committed
278

279
void check_for_read_results(struct host *h);
's avatar
committed
280
281
#endif//DIMAPI

282
283
284
285
#ifdef RECONNECT
int check_network_mapid(void);			// checks if network (mapid) is up. Returns 1 if network is up, 0 otherwise
#endif

Arne Øslebø's avatar
Arne Øslebø committed
286
287
288
289
#define INT 1
#define STRING 2
#define UNSIGNED_LONG_LONG 3

's avatar
   
committed
290
291
292
293
294
295
296
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