mapiipc.c 14.6 KB
Newer Older
1
2
3
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
Arne Øslebø's avatar
Arne Øslebø committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <string.h>
#include <errno.h>

#include "mapiipc.h"
#include "debug.h"

#define HAVE_MSGHDR_MSG_CONTROL 1

22
23
24
25
26
27
28
#ifdef DIMAPI
#include <netdb.h>
#include <netinet/in.h>
#include <semaphore.h>
#include "flist.h"
#endif

Arne Øslebø's avatar
Arne Øslebø committed
29
30
// this file contains all the client-side IPC related functions

's avatar
   
committed
31
32
33
static int sock;
static int mapidaddr_len;
static struct sockaddr_un mapidaddr;
's avatar
committed
34
//static struct sockaddr_un fromaddr;
's avatar
committed
35
static char* mapidsocket;
Arne Øslebø's avatar
Arne Øslebø committed
36

's avatar
committed
37
38
39
40
#ifdef DIMAPISSL
static SSL_CTX *ctx;
#endif

's avatar
committed
41
int mapiipc_write(struct mapiipcbuf *qbuf)
Arne Øslebø's avatar
Arne Øslebø committed
42
43
44
//Sends an IPC message to mapid
{
  qbuf->uid=getuid();
's avatar
committed
45
  if(send(sock, qbuf, sizeof(struct mapiipcbuf), MSG_NOSIGNAL) == -1) {
Arne Øslebø's avatar
Arne Øslebø committed
46
47
    WARNING_CMD(printf("send: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
    //    exit(1);
's avatar
committed
48
    return -1;
Arne Øslebø's avatar
Arne Øslebø committed
49
  }
's avatar
committed
50
  return 0;
Arne Øslebø's avatar
Arne Øslebø committed
51
52
}

's avatar
committed
53
int mapiipc_read(struct mapiipcbuf *qbuf)
Arne Øslebø's avatar
Arne Øslebø committed
54
55
56
57
//Reads an IPC message. Blocking call
{
  if(recv(sock, qbuf, MAX_SEND_SIZE, 0) == -1){
    ERROR_CMD(printf("recv: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
's avatar
committed
58
59
    //exit(1);
    return -1;
Arne Øslebø's avatar
Arne Øslebø committed
60
  }
's avatar
committed
61
  return 0;
Arne Øslebø's avatar
Arne Øslebø committed
62
63
}

's avatar
committed
64
int mapiipc_client_init()
Arne Øslebø's avatar
Arne Øslebø committed
65
66
67
68
//Initializes IPC for mapi functions
{
  if ((sock = socket(AF_LOCAL, SOCK_STREAM, 0)) == -1) {
    ERROR_CMD(printf("socket: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
's avatar
committed
69
70
    //exit(1);
    return -1;
Arne Øslebø's avatar
Arne Øslebø committed
71
  }
's avatar
committed
72
73
74

  mapidsocket=malloc(sizeof(MAPIDSOCKHOME)-2+strlen(getenv("HOME")));
  sprintf(mapidsocket,MAPIDSOCKHOME,getenv("HOME")); 
Arne Øslebø's avatar
Arne Øslebø committed
75
76
77

  // Construct name of mapid's socket
  mapidaddr.sun_family = AF_LOCAL;
's avatar
committed
78
  strcpy(mapidaddr.sun_path, mapidsocket);
Arne Øslebø's avatar
Arne Øslebø committed
79
80
  mapidaddr_len = sizeof mapidaddr.sun_family + strlen(mapidaddr.sun_path);

's avatar
committed
81
  if (connect(sock, (struct sockaddr *)&mapidaddr, mapidaddr_len) < 0) 
's avatar
   
committed
82
  {
's avatar
committed
83
	//	ERROR_CMD(printf("connect: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
's avatar
committed
84
85
86
87
      free(mapidsocket);
      mapidsocket=strdup(MAPIDSOCKGLOBAL);
      strcpy(mapidaddr.sun_path, mapidsocket);
      mapidaddr_len = sizeof mapidaddr.sun_family + strlen(mapidaddr.sun_path);
88
89
90
      if (connect(sock, (struct sockaddr *)&mapidaddr, mapidaddr_len) < 0) {
    		ERROR_CMD(printf("connect: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
	}
's avatar
committed
91
      else {
92
		free(mapidsocket);
's avatar
committed
93
      	//exit(EXIT_FAILURE);
94
      	return 0;
's avatar
committed
95
      }
's avatar
   
committed
96
  }
's avatar
committed
97
  free(mapidsocket);
98
  return -1;
Arne Øslebø's avatar
Arne Øslebø committed
99
100
101
102
103
104
105
106
}

void mapiipc_client_close()
//Releases socket resources
{
  close(sock);
}

107
108
109
110
#ifdef DIMAPI
struct sockaddr_in remoteaddr;
flist_t *remote_flowlist=NULL;

's avatar
committed
111
int mapiipc_remote_write(struct dmapiipcbuf *dbuf, struct host *h)
112
113
//Sends an IPC message to mapid
{
114
//  qbuf->uid=getuid();
's avatar
committed
115
116
117
118
119
120

#ifdef DIMAPISSL
	if(SSL_write(h->con,dbuf,dbuf->length) == -1){
		WARNING_CMD(printf("SSL_write: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
		return -1;
	}
121
122
123
124
125
#else
	if(send(h->sockfd, dbuf, dbuf->length, 0) == -1) {
		WARNING_CMD(printf("send: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
		return -1;
	}
's avatar
committed
126
#endif
127
	return 0;
128
129
}

's avatar
committed
130
131


's avatar
committed
132
int mapiipc_remote_write_to_all(remote_flowdescr_t* rflow)
133
134
135
{
  host_flow* hflow;
  flist_node_t* fnode;
's avatar
committed
136

's avatar
committed
137
138
  //pthread_mutex_lock(&rflow->mutex);
  //rflow->pending_msgs=0;
139
140
141
  for (fnode=flist_head(rflow->host_flowlist); fnode!=NULL; fnode=flist_next(fnode)) {
    hflow=(host_flow*)fnode->data;
    hflow->dbuf->fd=hflow->fd;
's avatar
fix    
committed
142
    if (mapiipc_remote_write(hflow->dbuf, hflow->rhost)<0) return -1;
's avatar
committed
143
144
145
146
    //rflow->pending_msgs++;
  }
  //pthread_mutex_unlock(&rflow->mutex);
  for (fnode=flist_head(rflow->host_flowlist); fnode!=NULL; fnode=flist_next(fnode)) {
147
     sem_wait(&rflow->fd_sem);
148
  }
's avatar
committed
149
  return 0;
150
}
151
152
153
154
155
156
 
void cleanup_handler(void *arg){	//the cleanup handler

        free(arg);
	return;
}
157
158
159
160
161
162
163

void *mapiipc_comm_thread(void *host) {
//Reads an IPC message. Blocking call
  struct dmapiipcbuf* dbuf;
  remote_flowdescr_t* rflow;
  host_flow* hflow;
  int recv_bytes;
's avatar
committed
164
165
  
#ifndef DIMAPISSL
's avatar
committed
166
  int sockfd=((struct host *)host)->sockfd;
's avatar
committed
167
#endif
's avatar
committed
168

's avatar
committed
169
170
171
172
#ifdef DIMAPISSL
  SSL *hostconn = ((struct host*)host)->con;
#endif

's avatar
committed
173
174
  /* Guarantees that thread resources are deallocated upon return */

175
176
177
178
  pthread_detach(pthread_self());
  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);		// enable cancellation
  pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);	// changes the type of responses to cancellation requests for the calling thread
 								// asynchronous (cancel the calling thread as soon as the cancellation request is received)
179
  dbuf = (struct dmapiipcbuf *)malloc(sizeof(struct dmapiipcbuf));
180

181
182
183
184
185
186
  // pthread_cleanup_push() function pushes the specified cancellation cleanup handler onto
  // the cancellation cleanup stack of the calling thread. When a thread exits or is cancelled,
  // and its cancellation cleanup stack is not empty, the cleanup handlers are invoked with
  // the specified argument in last-in-first-out order from the cancellation cleanup stack
  pthread_cleanup_push(cleanup_handler, dbuf);

187
188
  while (1) {

189
    if (host==NULL) break;
's avatar
committed
190

's avatar
committed
191
#ifdef DIMAPISSL
192
193
194
    recv_bytes = SSL_readn(hostconn,dbuf,BASIC_SIZE);
#else
    recv_bytes = readn(sockfd, dbuf, BASIC_SIZE);
's avatar
committed
195
196
#endif

's avatar
committed
197
198
199
200
201
202
203
204
    if (recv_bytes == 0) { // the peer has gone
	//printf("Socket closed\n");
	break;
    }
    else if (recv_bytes == -1) {
	//ERROR_CMD(printf("recv: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
	continue;
    }
205

's avatar
committed
206
207
208
209
    if (dbuf->length > DIMAPI_DATA_SIZE) {
	fprintf(stderr,"Bad IPC message from agent\n");
	continue;
    }
210

's avatar
committed
211
    if (dbuf->length-BASIC_SIZE>0) {
's avatar
committed
212
213

#ifdef DIMAPISSL
214
215
    recv_bytes = SSL_readn(hostconn,(char *)dbuf + BASIC_SIZE, dbuf->length - BASIC_SIZE );
#else
216
    recv_bytes=readn(sockfd, (char*)dbuf+BASIC_SIZE, dbuf->length-BASIC_SIZE);
's avatar
committed
217
#endif
's avatar
committed
218
219
220
221
222
223
224
225
226
	if (recv_bytes == 0) { // the peer has gone
		//printf("Socket closed\n");
		break;
	}
	else if (recv_bytes == -1) {
		//ERROR_CMD(printf("recv: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
		continue;
	}
    }
227

228
229
230
    hflow=(host_flow*)flist_get( ((struct host*)host)->flows, dbuf->fd );
    if (hflow!=NULL) {
      rflow=flist_get(remote_flowlist, hflow->scope_fd);
's avatar
   
committed
231
232
      if (dbuf->cmd==GET_NEXT_PKT_ACK) {
		memcpy(hflow->pkt, dbuf->data, dbuf->length-BASIC_SIZE);
's avatar
committed
233
234
		flist_append(rflow->pkt_list, 0, hflow);
		sem_post(&rflow->pkt_sem);
's avatar
   
committed
235
236
237
      }
      else {
	memcpy( hflow->dbuf, dbuf, dbuf->length );	//place data
's avatar
committed
238
239

	sem_post( &rflow->fd_sem );
240
241
242
      }
    }
    else {
's avatar
committed
243
      fprintf(stderr,"Invalid IPC message, unknown fd %d\n",dbuf->fd);
's avatar
committed
244
      //exit(-1);
245
      //failure
's avatar
committed
246
      continue;
247
248
249
    }

  }
250
251
252
  
  pthread_cleanup_pop(1);	// pthread_cleanup_pop() function shall remove the routine at the top of the
 				// calling thread's cancellation cleanup stack and invoke it
's avatar
committed
253
  return NULL;
254
255
256
}


's avatar
committed
257
int mapiipc_remote_init(struct host *h)
258
259
//Initializes IPC for dmapi functions
{
's avatar
committed
260
261
  struct hostent* host=gethostbyname(h->hostname);
  struct timeval tv;
262

263
#ifdef DIMAPISSL
264
265
	SSL_library_init();		// registers the available ciphers and digests
	SSL_load_error_strings();	// registers the error strings for all libcrypto functions and libssl
's avatar
committed
266
267
268

	if ((ctx=SSL_CTX_new(SSLv3_client_method())) == NULL) {
		ERR_print_errors_fp(stderr);
's avatar
committed
269
		return -1;
's avatar
committed
270
271
272
	}
	if ((h->con = SSL_new(ctx)) == NULL) {
		ERR_print_errors_fp(stderr);
's avatar
committed
273
		return -1;
's avatar
committed
274
275
276
	}
#endif

277
278
  if ((h->sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    ERROR_CMD(printf("socket: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
's avatar
committed
279
280
    //exit(-1);
    return -1;
's avatar
committed
281
282
  }

's avatar
fix    
committed
283
  /*
284
  tv.tv_sec=20;		//timeout 20 sec for recv
's avatar
committed
285
286
287
288
289
  tv.tv_usec=0;

  if (setsockopt(h->sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)) == -1) {
    close(h->sockfd);
    printf("Unexpected error on setsockopt()");
's avatar
committed
290
291
    //exit(-1);
    return -1;
's avatar
committed
292
  }
's avatar
fix    
committed
293
  */
294

's avatar
committed
295
296
297
298
299
300
  tv.tv_sec=10;		//timeout 10 sec for send
  tv.tv_usec=0;

  if (setsockopt(h->sockfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval)) == -1) {
    close(h->sockfd);
    printf("Unexpected error on setsockopt()");
's avatar
committed
301
302
    //exit(-1);
    return -1;
's avatar
committed
303
304
305
306
  }

  if (host==NULL) {
    printf("Could not determine address for %s\n",h->hostname);
's avatar
committed
307
308
    //exit(1);
    return -1;
309
  }
's avatar
committed
310

311
312
  // Construct name of dmapid's socket
  remoteaddr.sin_family = AF_INET;
's avatar
committed
313
  remoteaddr.sin_addr = *((struct in_addr *)host->h_addr);
314
315
  remoteaddr.sin_port = htons(h->port);

316
  if (connect(h->sockfd, (struct sockaddr *)&remoteaddr, sizeof(remoteaddr)) < 0) {
's avatar
committed
317
    ERROR_CMD(printf("connect failed: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
's avatar
committed
318
319
    //exit(EXIT_FAILURE);
    return -1;
320
  }
's avatar
committed
321
322

#ifdef DIMAPISSL
323
	if (SSL_set_fd(h->con, h->sockfd) == 0) {
's avatar
committed
324
		ERR_print_errors_fp(stderr);
's avatar
committed
325
		return -1;
's avatar
committed
326
327
328
	}
	if (SSL_connect(h->con) <= 0) {
		ERR_print_errors_fp(stderr);
's avatar
committed
329
		return -1;
's avatar
committed
330
331
	}
#endif
's avatar
committed
332
  return 0;
's avatar
committed
333

334
335
336
337
338
}

void mapiipc_remote_close(struct host *h)
//Releases socket resources
{
339
340
341
	shutdown(h->sockfd, SHUT_RDWR);
	close(h->sockfd);

's avatar
committed
342
#ifdef DIMAPISSL
343
	if (SSL_shutdown(h->con) == -1)	// shut down a TLS/SSL connection
's avatar
committed
344
		ERR_print_errors_fp(stderr);
345
346
347
348
349
350
351
352
353
354

	SSL_free(h->con);		// decrements the reference count of ssl, and removes the SSL structure pointed to by ssl
       					// frees up the allocated memory if the the reference count has reached 0
	if(ctx != NULL)
		SSL_CTX_free(ctx);	// decrements the reference count of ctx, and removes the SSL_CTX object pointed to by ctx
					// frees up the allocated memory if the the reference count has reached 0
	ERR_remove_state(0);		// the current thread will have its error queue removed	
	ERR_free_strings();		// frees all previously loaded error strings
	EVP_cleanup();			// removes all ciphers and digests from the table	
	CRYPTO_cleanup_all_ex_data();	// clean up all allocated state
's avatar
committed
355
#endif
356
}
357
358

#endif /* DIMAPI */
359

Arne Øslebø's avatar
Arne Øslebø committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387

// Helper functions for function arguments retrieval
int getargint(mapiFunctArg **pos){
	int i;
	i = *((int *)(*pos));
	//	printf("getint: %d\n", i);
	(*pos) += sizeof(int);
	return i;
}

char getargchar(mapiFunctArg **pos){
	char c;
	c = *((char *)(*pos));
	//printf("getchar: %c\n", c);
	(*pos) += sizeof(char);
	return c;
}

unsigned long long getargulonglong(mapiFunctArg **pos){
	unsigned long long l;
	l = *((unsigned long long *)(*pos));
	//printf("getulonglong: %lld\n", l);
	(*pos) += sizeof(unsigned long long);
	return l;
}

char * getargstr(mapiFunctArg **pos){
	char *s;
388
	s = (char*)*pos;
Arne Øslebø's avatar
Arne Øslebø committed
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
	//printf("getstr: %s\n", s);
	(*pos) += strlen(s)+1;
	return s;
}

void addarg(mapiFunctArg **pos, void *arg, int type)
// Helper function for mapi_apply_function()
// pos: current position in message's argument buffer.
// arg: argument to copy into buffer
// type: argument type
{
  switch(type){
    case INT:
      memcpy(*pos, arg, sizeof(int));
      //printf("add_arg: %d\n", *((int *)(*pos)));
      (*pos) += sizeof(int);
      break;
    case CHAR:
      memcpy(*pos, arg, sizeof(char));
      //printf("add_arg: %d\n", *((char *)(*pos)));
      (*pos) += sizeof(char);
      break;
    case UNSIGNED_LONG_LONG:
      memcpy(*pos, arg, sizeof(unsigned long long));
      //printf("add_arg: %llu\n", *((unsigned long long *)(*pos)));
      (*pos) += sizeof(unsigned long long);
      break;
    case STRING:
      memcpy(*pos, arg, strlen((char *)arg)+1);
      //printf("add_arg: %s\n", (char *)(*pos));
      (*pos) += strlen((char *)arg)+1;
      break;
    default:
      break;
  }
}

int mapiipc_send_fd(int sendfd)
{
  struct msghdr	msg;
  struct iovec	iov[1];
  char ptr[2];
  int ret;

#ifdef	HAVE_MSGHDR_MSG_CONTROL
  union {
    struct cmsghdr	cm;
    char control[CMSG_SPACE(sizeof(int))];
  } control_un;
  struct cmsghdr	*cmptr;
  
  msg.msg_control = control_un.control;
  msg.msg_controllen = sizeof(control_un.control);
  
  cmptr = CMSG_FIRSTHDR(&msg);
  cmptr->cmsg_len = CMSG_LEN(sizeof(int));
  cmptr->cmsg_level = SOL_SOCKET;
  cmptr->cmsg_type = SCM_RIGHTS;
  *((int *) CMSG_DATA(cmptr)) = sendfd;
#else
  msg.msg_accrights = (caddr_t) &sendfd;
  msg.msg_accrightslen = sizeof(int);
#endif
    
  iov[0].iov_base = ptr;
  iov[0].iov_len = 2;
  msg.msg_iov = iov;
  msg.msg_iovlen = 1;
  msg.msg_name = NULL;
  msg.msg_namelen = 0;

  ret=sendmsg(sock,&msg,0);
  return(ret);
}


int mapiipc_read_fd(int sock)
{
  struct msghdr	msg;
  struct iovec	iov[1];
  ssize_t n;
  int recvfd;
  char c[2];
  
#ifdef	HAVE_MSGHDR_MSG_CONTROL
  union {
    struct cmsghdr	cm;
    char				control[CMSG_SPACE(sizeof(int))];
  } control_un;
  struct cmsghdr	*cmptr;
  
  msg.msg_control = control_un.control;
  msg.msg_controllen = sizeof(control_un.control);
#else
  msg.msg_accrights = (caddr_t) &newfd;
  msg.msg_accrightslen = sizeof(int);
#endif
  
  iov[0].iov_base = &c;
  iov[0].iov_len = 2;
  msg.msg_iov = iov;
  msg.msg_iovlen = 1;
  msg.msg_name = NULL;
  msg.msg_namelen = 0;
  
  
  if ( (n = recvmsg(sock, &msg, 0)) <= 0)
    return(n);
  
#ifdef	HAVE_MSGHDR_MSG_CONTROL
  if ( (cmptr = CMSG_FIRSTHDR(&msg)) != NULL &&
       cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
    if (cmptr->cmsg_level != SOL_SOCKET) {
      // err_quit("control level != SOL_SOCKET");
      fprintf(stderr, "control level != SOL_SOCKET");
      return -1;
    }
    if (cmptr->cmsg_type != SCM_RIGHTS) {
      // err_quit("control type != SCM_RIGHTS");
      fprintf(stderr, "control type != SCM_RIGHTS");
      return -1;
    }
    recvfd = *((int *) CMSG_DATA(cmptr));
  } else
    recvfd = -1;		/* descriptor was not passed */
#else
  /* *INDENT-OFF* */
  if (msg.msg_accrightslen == sizeof(int))
    recvfd = newfd;
  else
    recvfd = -1;		/* descriptor was not passed */
  /* *INDENT-ON* */
#endif

  return recvfd;
}
's avatar
committed
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549

/* Read "n" bytes from a socket. */
ssize_t readn(int fd, void *vptr, size_t n) {
        size_t nleft;
        ssize_t nread;
        char *ptr;

        ptr = vptr;
        nleft = n;
        while (nleft > 0) {
		errno=0;
                if ( (nread = read(fd, ptr, nleft)) < 0) {
                        if (errno == EINTR)
                                nread = 0;              /* and call read() again */
                        else
                                return(-1);
                } else if (nread == 0)
                        return 0;                          /* EOF */

                nleft -= nread;
                ptr   += nread;
        }
        return(n - nleft);              /* return >= 0 */
}

's avatar
committed
550
551
#ifdef DIMAPISSL
ssize_t SSL_readn(SSL *con, void *vptr, size_t n) {
552

's avatar
committed
553
554
555
        size_t nleft;
        ssize_t nread;
        char *ptr;
's avatar
committed
556

's avatar
committed
557
558
        ptr = vptr;
        nleft = n;
559

's avatar
committed
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
        while (nleft > 0) {
		errno=0;
                if ( (nread = SSL_read(con, ptr, nleft)) < 0) {
                        if (errno == EINTR)
                                nread = 0;              /* and call read() again */
                        else
                                return(-1);
                } else if (nread == 0)
                        return 0;                          /* EOF */

                nleft -= nread;
                ptr   += nread;
        }
        return(n - nleft);              /* return >= 0 */
}
#endif