mapiipc.c 10.4 KB
Newer Older
Arne Øslebø's avatar
Arne Øslebø committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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

19
20
21
22
23
24
25
#ifdef DIMAPI
#include <netdb.h>
#include <netinet/in.h>
#include <semaphore.h>
#include "flist.h"
#endif

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

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

's avatar
committed
34
int mapiipc_write(struct mapiipcbuf *qbuf)
Arne Øslebø's avatar
Arne Øslebø committed
35
36
37
38
39
40
//Sends an IPC message to mapid
{
  qbuf->uid=getuid();
  if(send(sock, qbuf, sizeof(struct mapiipcbuf), 0) == -1) {
    WARNING_CMD(printf("send: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
    //    exit(1);
's avatar
committed
41
    return -1;
Arne Øslebø's avatar
Arne Øslebø committed
42
  }
's avatar
committed
43
  return 0;
Arne Øslebø's avatar
Arne Øslebø committed
44
45
}

's avatar
committed
46
int mapiipc_read(struct mapiipcbuf *qbuf)
Arne Øslebø's avatar
Arne Øslebø committed
47
48
49
50
//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
51
52
    //exit(1);
    return -1;
Arne Øslebø's avatar
Arne Øslebø committed
53
  }
's avatar
committed
54
  return 0;
Arne Øslebø's avatar
Arne Øslebø committed
55
56
}

's avatar
committed
57
int mapiipc_client_init()
Arne Øslebø's avatar
Arne Øslebø committed
58
59
60
61
//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
62
63
    //exit(1);
    return -1;
Arne Øslebø's avatar
Arne Øslebø committed
64
  }
's avatar
committed
65
66
67

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

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

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

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

99
100
101
102
#ifdef DIMAPI
struct sockaddr_in remoteaddr;
flist_t *remote_flowlist=NULL;

's avatar
committed
103
int mapiipc_remote_write(struct dmapiipcbuf *dbuf, struct host *h)
104
105
106
107
108
109
//Sends an IPC message to mapid
{
//  qbuf->uid=getuid(); 
  if(send(h->sockfd, dbuf, dbuf->length, 0) == -1) {
    WARNING_CMD(printf("send: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
    //    exit(1);
's avatar
committed
110
    return -1;
111
  }
's avatar
committed
112
  return 0;
113
114
}

's avatar
committed
115
int mapiipc_remote_write_to_all(remote_flowdescr_t* rflow)
116
117
118
{
  host_flow* hflow;
  flist_node_t* fnode;
's avatar
committed
119

120
  pthread_mutex_lock(&rflow->mutex);
's avatar
committed
121
  rflow->pending_msgs=0;
122
123
124
  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
125
    if (mapiipc_remote_write(hflow->dbuf, hflow->rhost)<0) return -1;
126
127
128
129
    rflow->pending_msgs++;
  }
  pthread_mutex_unlock(&rflow->mutex);
  sem_wait(&rflow->fd_sem);
's avatar
committed
130
  return 0;
131
132
133
134
}

void *mapiipc_comm_thread(void *host) {
//Reads an IPC message. Blocking call
's avatar
committed
135
  char buffer[DIMAPI_DATA_SIZE];
136
137
138
139
140
  struct dmapiipcbuf* dbuf;
  remote_flowdescr_t* rflow;
  host_flow* hflow;
  int recv_bytes;
  unsigned int dbuf_bytes=0;
's avatar
committed
141
  int pending;
's avatar
committed
142
  int sockfd=((struct host *)host)->sockfd;
's avatar
committed
143
144
145
146

  /* Guarantees that thread resources are deallocated upon return */
  pthread_detach(pthread_self());

147
  dbuf=(struct dmapiipcbuf*)&buffer[0];
148
149
150

  while (1) {

's avatar
fix    
committed
151
    if (dbuf_bytes==0 || (dbuf->length<DIMAPI_DATA_SIZE && dbuf_bytes<dbuf->length)) {
's avatar
committed
152
153
154
      if (host==NULL) return NULL;
      recv_bytes=recv(sockfd, (char*)&buffer+dbuf_bytes, DIMAPI_DATA_SIZE, 0);
      if (recv_bytes == -1) {
155
	//ERROR_CMD(printf("recv: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
's avatar
committed
156
	//exit(1);
's avatar
committed
157
	//printf("error in recv\n");
158
159
	continue ;
      }
's avatar
committed
160
161
162
163
164
      else if (recv_bytes == 0) {
	//printf("Socket closed\n");
	//exit(1);
	return NULL;
      }
165
166
167
168
169
170
171
172
173
174

      dbuf_bytes += recv_bytes; 
    }

    if (dbuf_bytes<dbuf->length) continue;


    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
175
176
      if (dbuf->cmd==GET_NEXT_PKT_ACK) {
		memcpy(hflow->pkt, dbuf->data, dbuf->length-BASIC_SIZE);
's avatar
committed
177
178
		flist_append(rflow->pkt_list, 0, hflow);
		sem_post(&rflow->pkt_sem);
's avatar
   
committed
179
180
181
182
183
      }
      else {
	memcpy( hflow->dbuf, dbuf, dbuf->length );	//place data
	pthread_mutex_lock(&rflow->mutex);
	//if (rflow->pending_msgs>0) {
184
	--rflow->pending_msgs;
's avatar
committed
185
	pending=rflow->pending_msgs;
186
187
	pthread_mutex_unlock(&rflow->mutex);

's avatar
committed
188
	if ( pending==0 ) {
189
190
	  sem_post( &rflow->fd_sem );
	}
's avatar
   
committed
191
192
      	//}
	//else pthread_mutex_unlock(&rflow->mutex);
193
194
195
      }
    }
    else {
's avatar
committed
196
197
      printf("Invalid IPC message, unknown fd %d\n",dbuf->fd);
      //exit(-1);
198
      //failure
's avatar
committed
199
      continue;
200
201
202
203
204
205
206
207
    }

    dbuf_bytes=dbuf_bytes-dbuf->length;
    memcpy(buffer,buffer+dbuf->length,dbuf_bytes);
  }
}


's avatar
committed
208
int mapiipc_remote_init(struct host *h)
209
210
//Initializes IPC for dmapi functions
{
's avatar
committed
211
212
  struct hostent* host=gethostbyname(h->hostname);
  struct timeval tv;
213
214
215

  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
216
217
    //exit(-1);
    return -1;
's avatar
committed
218
219
  }

's avatar
fix    
committed
220
  /*
221
  tv.tv_sec=20;		//timeout 20 sec for recv
's avatar
committed
222
223
224
225
226
  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
227
228
    //exit(-1);
    return -1;
's avatar
committed
229
  }
's avatar
fix    
committed
230
  */
231

's avatar
committed
232
233
234
235
236
237
  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
238
239
    //exit(-1);
    return -1;
's avatar
committed
240
241
242
243
  }

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

248
249
  // Construct name of dmapid's socket
  remoteaddr.sin_family = AF_INET;
's avatar
committed
250
  remoteaddr.sin_addr = *((struct in_addr *)host->h_addr);
251
252
253
  remoteaddr.sin_port = htons(h->port);

  if (connect(h->sockfd, (struct sockaddr *)&remoteaddr, sizeof(remoteaddr)) < 0) {     
's avatar
committed
254
    ERROR_CMD(printf("connect failed: %s [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
's avatar
committed
255
256
    //exit(EXIT_FAILURE);
    return -1;
257
  }
's avatar
committed
258
  return 0;
's avatar
committed
259

260
261
262
263
264
}

void mapiipc_remote_close(struct host *h)
//Releases socket resources
{
's avatar
committed
265
  shutdown(h->sockfd, SHUT_RDWR);
266
267
268
269
  close(h->sockfd);
}
#endif

Arne Øslebø's avatar
Arne Øslebø committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297

// 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;
298
	s = (char*)*pos;
Arne Øslebø's avatar
Arne Øslebø committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
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
388
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
	//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;
}