Shared memory packet interface (memif) library
[vpp.git] / extras / libmemif / src / socket.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #define _GNU_SOURCE
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <sys/un.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <net/if.h>
26 #include <sys/ioctl.h>
27 #include <sys/uio.h>
28 #include <sys/mman.h>
29 #include <sys/prctl.h>
30 #include <fcntl.h>
31 #include <errno.h>
32
33 #include <socket.h>
34 #include <memif.h>
35
36 #define memif_min(a,b) ((a < b) ? (a) : (b))
37
38 /* sends msg to socket */
39 static_fn int
40 memif_msg_send (int fd, memif_msg_t * msg, int afd)
41 {
42   struct msghdr mh = { 0 };
43   struct iovec iov[1];
44   char ctl[CMSG_SPACE (sizeof (int))];
45   int rv, err = MEMIF_ERR_SUCCESS;      /* 0 */
46
47   iov[0].iov_base = (void *) msg;
48   iov[0].iov_len = sizeof (memif_msg_t);
49   mh.msg_iov = iov;
50   mh.msg_iovlen = 1;
51
52   if (afd > 0)
53     {
54       struct cmsghdr *cmsg;
55       memset (&ctl, 0, sizeof (ctl));
56       mh.msg_control = ctl;
57       mh.msg_controllen = sizeof (ctl);
58       cmsg = CMSG_FIRSTHDR (&mh);
59       cmsg->cmsg_len = CMSG_LEN (sizeof (int));
60       cmsg->cmsg_level = SOL_SOCKET;
61       cmsg->cmsg_type = SCM_RIGHTS;
62       memcpy (CMSG_DATA (cmsg), &afd, sizeof (int));
63     }
64   rv = sendmsg (fd, &mh, 0);
65   if (rv < 0)
66     err = memif_syscall_error_handler (errno);
67   DBG ("Message type %u sent", msg->type);
68   return err;
69 }
70
71 /* response from memif master - master is ready to handle next message */
72 static_fn int
73 memif_msg_enq_ack (memif_connection_t * c)
74 {
75   memif_msg_queue_elt_t *e =
76     (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
77   if (e == NULL)
78     return memif_syscall_error_handler (errno);
79
80   memset (&e->msg, 0, sizeof (e->msg));
81   e->msg.type = MEMIF_MSG_TYPE_ACK;
82   e->fd = -1;
83
84   e->next = NULL;
85   if (c->msg_queue == NULL)
86     {
87       c->msg_queue = e;
88       return MEMIF_ERR_SUCCESS; /* 0 */
89     }
90
91   memif_msg_queue_elt_t *cur = c->msg_queue;
92   while (cur->next != NULL)
93     {
94       cur = cur->next;
95     }
96   cur->next = e;
97
98   return MEMIF_ERR_SUCCESS;     /* 0 */
99 }
100
101 static_fn int
102 memif_msg_send_hello (int fd)
103 {
104   libmemif_main_t *lm = &libmemif_main;
105   memif_msg_t msg = { 0 };
106   memif_msg_hello_t *h = &msg.hello;
107   msg.type = MEMIF_MSG_TYPE_HELLO;
108   h->min_version = MEMIF_VERSION;
109   h->max_version = MEMIF_VERSION;
110   h->max_s2m_ring = MEMIF_MAX_M2S_RING;
111   h->max_m2s_ring = MEMIF_MAX_M2S_RING;
112   h->max_region = MEMIF_MAX_REGION;
113   h->max_log2_ring_size = MEMIF_MAX_LOG2_RING_SIZE;
114
115   strncpy ((char *) h->name, lm->app_name, strlen (lm->app_name));
116
117   /* msg hello is not enqueued but sent directly,
118      because it is the first msg to be sent */
119   return memif_msg_send (fd, &msg, -1);
120 }
121
122 /* send id and secret (optional) for interface identification */
123 static_fn int
124 memif_msg_enq_init (memif_connection_t * c)
125 {
126   memif_msg_queue_elt_t *e =
127     (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
128   if (e == NULL)
129     return memif_syscall_error_handler (errno);
130   memset (e, 0, sizeof (memif_msg_queue_elt_t));
131
132   memset (&e->msg, 0, sizeof (e->msg));
133   memif_msg_init_t *i = &e->msg.init;
134
135   e->msg.type = MEMIF_MSG_TYPE_INIT;
136   e->fd = -1;
137   i->version = MEMIF_VERSION;
138   i->id = c->args.interface_id;
139   i->mode = c->args.mode;
140
141   strncpy ((char *) i->name, (char *) c->args.instance_name,
142            strlen ((char *) c->args.instance_name));
143   if (c->args.secret)
144     strncpy ((char *) i->secret, (char *) c->args.secret, sizeof (i->secret));
145
146   e->next = NULL;
147   if (c->msg_queue == NULL)
148     {
149       c->msg_queue = e;
150       return MEMIF_ERR_SUCCESS; /* 0 */
151     }
152
153   memif_msg_queue_elt_t *cur = c->msg_queue;
154   while (cur->next != NULL)
155     {
156       cur = cur->next;
157     }
158   cur->next = e;
159
160   return MEMIF_ERR_SUCCESS;     /* 0 */
161 }
162
163 /* send information about region specified by region_index */
164 static_fn int
165 memif_msg_enq_add_region (memif_connection_t * c, uint8_t region_index)
166 {
167   /* maybe check if region is valid? */
168   memif_region_t *mr = &c->regions[region_index];
169
170   memif_msg_queue_elt_t *e =
171     (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
172   if (e == NULL)
173     return memif_syscall_error_handler (errno);
174
175   memset (&e->msg, 0, sizeof (e->msg));
176   memif_msg_add_region_t *ar = &e->msg.add_region;
177
178   e->msg.type = MEMIF_MSG_TYPE_ADD_REGION;
179   e->fd = mr->fd;
180   ar->index = region_index;
181   ar->size = mr->region_size;
182
183   e->next = NULL;
184   if (c->msg_queue == NULL)
185     {
186       c->msg_queue = e;
187       return MEMIF_ERR_SUCCESS; /* 0 */
188     }
189
190   memif_msg_queue_elt_t *cur = c->msg_queue;
191   while (cur->next != NULL)
192     {
193       cur = cur->next;
194     }
195   cur->next = e;
196
197   return MEMIF_ERR_SUCCESS;     /* 0 */
198 }
199
200 /* send information about ring specified by direction (S2M | M2S) and index */
201 static_fn int
202 memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
203 {
204   memif_msg_queue_elt_t *e =
205     (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
206   if (e == NULL)
207     return memif_syscall_error_handler (errno);
208
209   memset (&e->msg, 0, sizeof (e->msg));
210   memif_msg_add_ring_t *ar = &e->msg.add_ring;
211
212   e->msg.type = MEMIF_MSG_TYPE_ADD_RING;
213
214   /* TODO: support multiple rings */
215   memif_queue_t *mq;
216   if (dir == MEMIF_RING_M2S)
217     mq = &c->rx_queues[index];
218   else
219     mq = &c->tx_queues[index];
220
221   e->fd = mq->int_fd;
222   ar->index = index;
223   ar->offset = mq->offset;
224   ar->region = mq->region;
225   ar->log2_ring_size = mq->log2_ring_size;
226   ar->flags = (dir == MEMIF_RING_S2M) ? MEMIF_MSG_ADD_RING_FLAG_S2M : 0;
227
228   e->next = NULL;
229   if (c->msg_queue == NULL)
230     {
231       c->msg_queue = e;
232       return MEMIF_ERR_SUCCESS; /* 0 */
233     }
234
235   memif_msg_queue_elt_t *cur = c->msg_queue;
236   while (cur->next != NULL)
237     {
238       cur = cur->next;
239     }
240   cur->next = e;
241
242   return MEMIF_ERR_SUCCESS;     /* 0 */
243 }
244
245 /* used as connection request from slave */
246 static_fn int
247 memif_msg_enq_connect (memif_connection_t * c)
248 {
249   memif_msg_queue_elt_t *e =
250     (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
251   if (e == NULL)
252     return memif_syscall_error_handler (errno);
253
254   memset (&e->msg, 0, sizeof (e->msg));
255   memif_msg_connect_t *cm = &e->msg.connect;
256
257   e->msg.type = MEMIF_MSG_TYPE_CONNECT;
258   e->fd = -1;
259   strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
260            strlen ((char *) c->args.interface_name));
261
262   e->next = NULL;
263   if (c->msg_queue == NULL)
264     {
265       c->msg_queue = e;
266       return MEMIF_ERR_SUCCESS; /* 0 */
267     }
268
269   memif_msg_queue_elt_t *cur = c->msg_queue;
270   while (cur->next != NULL)
271     {
272       cur = cur->next;
273     }
274   cur->next = e;
275
276   return MEMIF_ERR_SUCCESS;     /* 0 */
277 }
278
279 /* used as confirmation of connection by master */
280 static_fn int
281 memif_msg_enq_connected (memif_connection_t * c)
282 {
283   memif_msg_queue_elt_t *e =
284     (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
285   if (e == NULL)
286     return memif_syscall_error_handler (errno);
287
288   memset (&e->msg, 0, sizeof (e->msg));
289   memif_msg_connected_t *cm = &e->msg.connected;
290
291   e->msg.type = MEMIF_MSG_TYPE_CONNECTED;
292   e->fd = -1;
293   strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
294            strlen ((char *) c->args.interface_name));
295
296   e->next = NULL;
297   if (c->msg_queue == NULL)
298     {
299       c->msg_queue = e;
300       return MEMIF_ERR_SUCCESS; /* 0 */
301     }
302
303   memif_msg_queue_elt_t *cur = c->msg_queue;
304   while (cur->next != NULL)
305     {
306       cur = cur->next;
307     }
308   cur->next = e;
309
310   return MEMIF_ERR_SUCCESS;     /* 0 */
311 }
312
313 /* immediately send disconnect msg */
314     /* specifie protocol for disconnect msg err_code
315        so that it will be compatible with VPP? (header/doc) */
316 int
317 memif_msg_send_disconnect (int fd, uint8_t * err_string, uint32_t err_code)
318 {
319   memif_msg_t msg = { 0 };
320   memif_msg_disconnect_t *d = &msg.disconnect;
321
322   msg.type = MEMIF_MSG_TYPE_DISCONNECT;
323   d->code = err_code;
324   uint16_t l = strlen ((char *) err_string);
325   if (l > 96)
326     {
327       DBG ("Disconnect string too long. Sending first 96 characters.");
328       l = 96;
329     }
330   strncpy ((char *) d->string, (char *) err_string, l);
331
332   return memif_msg_send (fd, &msg, -1);
333 }
334
335 static_fn int
336 memif_msg_receive_hello (memif_connection_t * c, memif_msg_t * msg)
337 {
338   memif_msg_hello_t *h = &msg->hello;
339
340   if (msg->hello.min_version > MEMIF_VERSION ||
341       msg->hello.max_version < MEMIF_VERSION)
342     {
343       DBG ("incompatible protocol version");
344       return MEMIF_ERR_PROTO;
345     }
346
347   c->run_args.num_s2m_rings = memif_min (h->max_s2m_ring + 1,
348                                          c->args.num_s2m_rings);
349   c->run_args.num_m2s_rings = memif_min (h->max_m2s_ring + 1,
350                                          c->args.num_m2s_rings);
351   c->run_args.log2_ring_size = memif_min (h->max_log2_ring_size,
352                                           c->args.log2_ring_size);
353   c->run_args.buffer_size = c->args.buffer_size;
354   strncpy ((char *) c->remote_name, (char *) h->name,
355            strlen ((char *) h->name));
356
357   return MEMIF_ERR_SUCCESS;     /* 0 */
358 }
359
360 /* handle interface identification (id, secret (optional)) */
361 static_fn int
362 memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
363 {
364   memif_msg_init_t *i = &msg->init;
365   memif_list_elt_t *elt = NULL;
366   memif_list_elt_t elt2;
367   memif_connection_t *c = NULL;
368   libmemif_main_t *lm = &libmemif_main;
369   uint8_t err_string[96];
370   memset (err_string, 0, sizeof (char) * 96);
371   int err = MEMIF_ERR_SUCCESS;  /* 0 */
372   int err_disc;
373   if (i->version != MEMIF_VERSION)
374     {
375       DBG ("MEMIF_VER_ERR");
376       strncpy ((char *) err_string, MEMIF_VER_ERR, strlen (MEMIF_VER_ERR));
377       err = MEMIF_ERR_PROTO;
378       goto error;
379     }
380
381   get_list_elt (&elt, ms->interface_list, ms->interface_list_len, i->id);
382   if (elt == NULL)
383     {
384       DBG ("MEMIF_ID_ERR");
385       strncpy ((char *) err_string, MEMIF_ID_ERR, strlen (MEMIF_ID_ERR));
386       err = MEMIF_ERR_ID;
387       goto error;
388     }
389
390   c = (memif_connection_t *) elt->data_struct;
391
392   if (!(c->args.is_master))
393     {
394       DBG ("MEMIF_SLAVE_ERR");
395       strncpy ((char *) err_string, MEMIF_SLAVE_ERR,
396                strlen (MEMIF_SLAVE_ERR));
397       err = MEMIF_ERR_ACCSLAVE;
398       goto error;
399     }
400   if (c->fd != -1)
401     {
402       DBG ("MEMIF_CONN_ERR");
403       strncpy ((char *) err_string, MEMIF_CONN_ERR, strlen (MEMIF_CONN_ERR));
404       err = MEMIF_ERR_ALRCONN;
405       goto error;
406     }
407
408   c->fd = fd;
409
410   if (i->mode != c->args.mode)
411     {
412       DBG ("MEMIF_MODE_ERR");
413       strncpy ((char *) err_string, MEMIF_MODE_ERR, strlen (MEMIF_MODE_ERR));
414       err = MEMIF_ERR_MODE;
415       goto error;
416     }
417
418   strncpy ((char *) c->remote_name, (char *) i->name,
419            strlen ((char *) i->name));
420
421   if (c->args.secret)
422     {
423       int r;
424       if (i->secret)
425         {
426           if (strlen ((char *) c->args.secret) != strlen ((char *) i->secret))
427             {
428               DBG ("MEMIF_SECRET_ERR");
429               strncpy ((char *) err_string,
430                        MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
431               err = MEMIF_ERR_SECRET;
432               goto error;
433             }
434           r = strncmp ((char *) i->secret, (char *) c->args.secret,
435                        strlen ((char *) c->args.secret));
436           if (r != 0)
437             {
438               DBG ("MEMIF_SECRET_ERR");
439               strncpy ((char *) err_string,
440                        MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
441               err = MEMIF_ERR_SECRET;
442               goto error;
443             }
444         }
445       else
446         {
447           DBG ("MEMIF_NOSECRET_ERR");
448           strncpy ((char *) err_string,
449                    MEMIF_NOSECRET_ERR, strlen (MEMIF_NOSECRET_ERR));
450           err = MEMIF_ERR_NOSECRET;
451           goto error;
452         }
453     }
454
455   c->read_fn = memif_conn_fd_read_ready;
456   c->write_fn = memif_conn_fd_write_ready;
457   c->error_fn = memif_conn_fd_error;
458
459   elt2.key = c->fd;
460   elt2.data_struct = c;
461
462   add_list_elt (&elt2, &lm->control_list, &lm->control_list_len);
463   free_list_elt (lm->pending_list, lm->pending_list_len, fd);
464
465   return err;
466
467 error:
468   memif_msg_send_disconnect (fd, err_string, 0);
469   lm->control_fd_update (fd, MEMIF_FD_EVENT_DEL);
470   free_list_elt (lm->pending_list, lm->pending_list_len, fd);
471   close (fd);
472   fd = -1;
473   return err;
474 }
475
476 /* receive region information and add new region to connection (if possible) */
477 static_fn int
478 memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
479                               int fd)
480 {
481   memif_msg_add_region_t *ar = &msg->add_region;
482   memif_region_t *mr;
483   if (fd < 0)
484     return MEMIF_ERR_NO_SHMFD;
485
486   if (ar->index > MEMIF_MAX_REGION)
487     return MEMIF_ERR_MAXREG;
488
489   mr =
490     (memif_region_t *) realloc (c->regions,
491                                 sizeof (memif_region_t) * (ar->index + 1));
492   if (mr == NULL)
493     return memif_syscall_error_handler (errno);
494   c->regions = mr;
495   c->regions[ar->index].fd = fd;
496   c->regions[ar->index].region_size = ar->size;
497   c->regions[ar->index].shm = NULL;
498
499   return MEMIF_ERR_SUCCESS;     /* 0 */
500 }
501
502 /* receive ring information and add new ring to connection queue
503    (based on direction S2M | M2S) */
504 static_fn int
505 memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
506 {
507   memif_msg_add_ring_t *ar = &msg->add_ring;
508
509   memif_queue_t *mq;
510
511   if (fd < 0)
512     return MEMIF_ERR_NO_INTFD;
513
514   if (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M)
515     {
516       if (ar->index > MEMIF_MAX_S2M_RING)
517         return MEMIF_ERR_MAXRING;
518       if (ar->index >= c->args.num_s2m_rings)
519         return MEMIF_ERR_MAXRING;
520
521       mq =
522         (memif_queue_t *) realloc (c->rx_queues,
523                                    sizeof (memif_queue_t) * (ar->index + 1));
524       if (mq == NULL)
525         return memif_syscall_error_handler (errno);
526       c->rx_queues = mq;
527       c->rx_queues[ar->index].int_fd = fd;
528       c->rx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
529       c->rx_queues[ar->index].region = ar->region;
530       c->rx_queues[ar->index].offset = ar->offset;
531       c->run_args.num_s2m_rings++;
532     }
533   else
534     {
535       if (ar->index > MEMIF_MAX_M2S_RING)
536         return MEMIF_ERR_MAXRING;
537       if (ar->index >= c->args.num_m2s_rings)
538         return MEMIF_ERR_MAXRING;
539
540       mq =
541         (memif_queue_t *) realloc (c->tx_queues,
542                                    sizeof (memif_queue_t) * (ar->index + 1));
543       if (mq == NULL)
544         return memif_syscall_error_handler (errno);
545       c->tx_queues = mq;
546       c->tx_queues[ar->index].int_fd = fd;
547       c->tx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
548       c->tx_queues[ar->index].region = ar->region;
549       c->tx_queues[ar->index].offset = ar->offset;
550       c->run_args.num_m2s_rings++;
551     }
552
553   return MEMIF_ERR_SUCCESS;     /* 0 */
554 }
555
556 /* slave -> master */
557 static_fn int
558 memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
559 {
560   memif_msg_connect_t *cm = &msg->connect;
561   libmemif_main_t *lm = &libmemif_main;
562   memif_list_elt_t elt;
563
564   int err;
565   err = memif_connect1 (c);
566   if (err != MEMIF_ERR_SUCCESS)
567     return err;
568
569   strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
570            strlen ((char *) cm->if_name));
571
572   int i;
573   if (c->on_interrupt != NULL)
574     {
575       for (i = 0; i < c->run_args.num_m2s_rings; i++)
576         {
577           elt.key = c->rx_queues[i].int_fd;
578           elt.data_struct = c;
579           add_list_elt (&elt, &lm->interrupt_list, &lm->interrupt_list_len);
580
581           lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
582         }
583
584     }
585
586   c->on_connect ((void *) c, c->private_ctx);
587
588   return err;
589 }
590
591 /* master -> slave */
592 static_fn int
593 memif_msg_receive_connected (memif_connection_t * c, memif_msg_t * msg)
594 {
595   memif_msg_connect_t *cm = &msg->connect;
596   libmemif_main_t *lm = &libmemif_main;
597
598   int err;
599   err = memif_connect1 (c);
600   if (err != MEMIF_ERR_SUCCESS)
601     return err;
602
603   strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
604            strlen ((char *) cm->if_name));
605
606   int i;
607   if (c->on_interrupt != NULL)
608     {
609       for (i = 0; i < c->run_args.num_s2m_rings; i++)
610         lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
611     }
612
613   c->on_connect ((void *) c, c->private_ctx);
614
615   return err;
616 }
617
618 static_fn int
619 memif_msg_receive_disconnect (memif_connection_t * c, memif_msg_t * msg)
620 {
621   memif_msg_disconnect_t *d = &msg->disconnect;
622
623   memset (c->remote_disconnect_string, 0,
624           sizeof (c->remote_disconnect_string));
625   strncpy ((char *) c->remote_disconnect_string, (char *) d->string,
626            strlen ((char *) d->string));
627
628   /* on returning error, handle function will call memif_disconnect () */
629   DBG ("disconnect received: %s, mode: %d",
630        c->remote_disconnect_string, c->args.mode);
631   return MEMIF_ERR_DISCONNECT;
632 }
633
634 static_fn int
635 memif_msg_receive (int ifd)
636 {
637   char ctl[CMSG_SPACE (sizeof (int)) +
638            CMSG_SPACE (sizeof (struct ucred))] = { 0 };
639   struct msghdr mh = { 0 };
640   struct iovec iov[1];
641   memif_msg_t msg = { 0 };
642   ssize_t size;
643   int err = MEMIF_ERR_SUCCESS;  /* 0 */
644   int fd = -1;
645   int i;
646   libmemif_main_t *lm = &libmemif_main;
647   memif_connection_t *c = NULL;
648   memif_socket_t *ms = NULL;
649   memif_list_elt_t *elt = NULL;
650
651   iov[0].iov_base = (void *) &msg;
652   iov[0].iov_len = sizeof (memif_msg_t);
653   mh.msg_iov = iov;
654   mh.msg_iovlen = 1;
655   mh.msg_control = ctl;
656   mh.msg_controllen = sizeof (ctl);
657
658   DBG ("recvmsg fd %d", ifd);
659   size = recvmsg (ifd, &mh, 0);
660   DBG ("done");
661   if (size != sizeof (memif_msg_t))
662     {
663       if (size == 0)
664         return MEMIF_ERR_DISCONNECTED;
665       else
666         return MEMIF_ERR_MFMSG;
667     }
668
669   struct ucred *cr = 0;
670   struct cmsghdr *cmsg;
671
672   cmsg = CMSG_FIRSTHDR (&mh);
673   while (cmsg)
674     {
675       if (cmsg->cmsg_level == SOL_SOCKET)
676         {
677           if (cmsg->cmsg_type == SCM_CREDENTIALS)
678             {
679               cr = (struct ucred *) CMSG_DATA (cmsg);
680             }
681           else if (cmsg->cmsg_type == SCM_RIGHTS)
682             {
683               int *fdp = (int *) CMSG_DATA (cmsg);
684               fd = *fdp;
685             }
686         }
687       cmsg = CMSG_NXTHDR (&mh, cmsg);
688     }
689
690   DBG ("Message type %u received", msg.type);
691
692   get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
693   if (elt != NULL)
694     c = (memif_connection_t *) elt->data_struct;
695
696   switch (msg.type)
697     {
698     case MEMIF_MSG_TYPE_ACK:
699       break;
700
701     case MEMIF_MSG_TYPE_HELLO:
702       if ((err = memif_msg_receive_hello (c, &msg)) != MEMIF_ERR_SUCCESS)
703         return err;
704       if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
705         return err;
706       if ((err = memif_msg_enq_init (c)) != MEMIF_ERR_SUCCESS)
707         return err;
708       if ((err = memif_msg_enq_add_region (c, 0)) != MEMIF_ERR_SUCCESS)
709         return err;
710       for (i = 0; i < c->run_args.num_s2m_rings; i++)
711         {
712           if ((err =
713                memif_msg_enq_add_ring (c, i,
714                                        MEMIF_RING_S2M)) != MEMIF_ERR_SUCCESS)
715             return err;
716         }
717       for (i = 0; i < c->run_args.num_m2s_rings; i++)
718         {
719           if ((err =
720                memif_msg_enq_add_ring (c, i,
721                                        MEMIF_RING_M2S)) != MEMIF_ERR_SUCCESS)
722             return err;
723         }
724       if ((err = memif_msg_enq_connect (c)) != MEMIF_ERR_SUCCESS)
725         return err;
726       break;
727
728     case MEMIF_MSG_TYPE_INIT:
729       get_list_elt (&elt, lm->pending_list, lm->pending_list_len, ifd);
730       if (elt == NULL)
731         return -1;
732       ms = (memif_socket_t *) elt->data_struct;
733       if ((err = memif_msg_receive_init (ms, ifd, &msg)) != MEMIF_ERR_SUCCESS)
734         return err;
735       /* c->remote_pid = cr->pid */
736       /* c->remote_uid = cr->uid */
737       /* c->remote_gid = cr->gid */
738       get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
739       if (elt == NULL)
740         return -1;
741       c = (memif_connection_t *) elt->data_struct;
742       if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
743         return err;
744       break;
745
746     case MEMIF_MSG_TYPE_ADD_REGION:
747       if ((err =
748            memif_msg_receive_add_region (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
749         return err;
750       if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
751         return err;
752       break;
753
754     case MEMIF_MSG_TYPE_ADD_RING:
755       if ((err =
756            memif_msg_receive_add_ring (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
757         return err;
758       if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
759         return err;
760       break;
761
762     case MEMIF_MSG_TYPE_CONNECT:
763       if ((err = memif_msg_receive_connect (c, &msg)) != MEMIF_ERR_SUCCESS)
764         return err;
765       if ((err = memif_msg_enq_connected (c)) != MEMIF_ERR_SUCCESS)
766         return err;
767       break;
768
769     case MEMIF_MSG_TYPE_CONNECTED:
770       if ((err = memif_msg_receive_connected (c, &msg)) != MEMIF_ERR_SUCCESS)
771         return err;
772       break;
773
774     case MEMIF_MSG_TYPE_DISCONNECT:
775       if ((err = memif_msg_receive_disconnect (c, &msg)) != MEMIF_ERR_SUCCESS)
776         return err;
777       break;
778
779     default:
780       return MEMIF_ERR_UNKNOWN_MSG;;
781       break;
782     }
783
784   if (c != NULL)
785     c->flags |= MEMIF_CONNECTION_FLAG_WRITE;
786 /*    libmemif_main_t *lm = &libmemif_main;
787     lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); */
788   return MEMIF_ERR_SUCCESS;     /* 0 */
789 }
790
791 int
792 memif_conn_fd_error (memif_connection_t * c)
793 {
794   DBG ("connection fd error");
795   strncpy ((char *) c->remote_disconnect_string, "connection fd error", 19);
796   int err = memif_disconnect_internal (c);
797   return err;
798 }
799
800 /* calls memif_msg_receive to handle pending messages on socket */
801 int
802 memif_conn_fd_read_ready (memif_connection_t * c)
803 {
804   int err;
805   err = memif_msg_receive (c->fd);
806   if (err != 0)
807     {
808       err = memif_disconnect_internal (c);
809     }
810   return err;
811 }
812
813 /* get msg from msg queue buffer and send it to socket */
814 int
815 memif_conn_fd_write_ready (memif_connection_t * c)
816 {
817   int err = MEMIF_ERR_SUCCESS;  /* 0 */
818
819
820   if ((c->flags & MEMIF_CONNECTION_FLAG_WRITE) == 0)
821     goto done;
822
823   memif_msg_queue_elt_t *e = c->msg_queue;
824   if (e == NULL)
825     goto done;
826
827   c->msg_queue = c->msg_queue->next;
828
829   c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE;
830 /*
831     libmemif_main_t *lm = &libmemif_main;
832
833     lm->control_fd_update (c->fd,
834         MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE | MEMIF_FD_EVENT_MOD);
835 */
836   err = memif_msg_send (c->fd, &e->msg, e->fd);
837   free (e);
838   goto done;
839
840 done:
841   return err;
842 }
843
844 int
845 memif_conn_fd_accept_ready (memif_socket_t * ms)
846 {
847   int addr_len;
848   struct sockaddr_un client;
849   int conn_fd;
850   libmemif_main_t *lm = &libmemif_main;
851
852   DBG ("accept called");
853
854   addr_len = sizeof (client);
855   conn_fd =
856     accept (ms->fd, (struct sockaddr *) &client, (socklen_t *) & addr_len);
857
858   if (conn_fd < 0)
859     {
860       return memif_syscall_error_handler (errno);
861     }
862   DBG ("accept fd %d", ms->fd);
863   DBG ("conn fd %d", conn_fd);
864
865   memif_list_elt_t elt;
866   elt.key = conn_fd;
867   elt.data_struct = ms;
868
869   add_list_elt (&elt, &lm->pending_list, &lm->pending_list_len);
870   lm->control_fd_update (conn_fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE);
871
872   return memif_msg_send_hello (conn_fd);
873 }
874
875 int
876 memif_read_ready (int fd)
877 {
878   int err;
879   DBG ("call recv");
880   err = memif_msg_receive (fd);
881   DBG ("recv finished");
882   return err;
883 }