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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
19 #include <sys/socket.h>
20 #include <sys/types.h>
26 #include <sys/ioctl.h>
29 #include <sys/prctl.h>
35 #include <memif_private.h>
37 /* sends msg to socket */
39 memif_msg_send (int fd, memif_msg_t * msg, int afd)
41 struct msghdr mh = { 0 };
43 char ctl[CMSG_SPACE (sizeof (int))];
44 int rv, err = MEMIF_ERR_SUCCESS; /* 0 */
46 iov[0].iov_base = (void *) msg;
47 iov[0].iov_len = sizeof (memif_msg_t);
54 memset (&ctl, 0, sizeof (ctl));
56 mh.msg_controllen = sizeof (ctl);
57 cmsg = CMSG_FIRSTHDR (&mh);
58 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
59 cmsg->cmsg_level = SOL_SOCKET;
60 cmsg->cmsg_type = SCM_RIGHTS;
61 memcpy (CMSG_DATA (cmsg), &afd, sizeof (int));
63 rv = sendmsg (fd, &mh, 0);
65 err = memif_syscall_error_handler (errno);
66 DBG ("Message type %u sent", msg->type);
70 /* response from memif master - master is ready to handle next message */
72 memif_msg_enq_ack (memif_connection_t * c)
74 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
75 memif_msg_queue_elt_t *e =
76 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
78 return memif_syscall_error_handler (errno);
80 memset (&e->msg, 0, sizeof (e->msg));
81 e->msg.type = MEMIF_MSG_TYPE_ACK;
85 if (c->msg_queue == NULL)
88 return MEMIF_ERR_SUCCESS; /* 0 */
91 memif_msg_queue_elt_t *cur = c->msg_queue;
92 while (cur->next != NULL)
98 return MEMIF_ERR_SUCCESS; /* 0 */
102 memif_msg_send_hello (libmemif_main_t * lm, int fd)
104 memif_msg_t msg = { 0 };
105 memif_msg_hello_t *h = &msg.hello;
106 msg.type = MEMIF_MSG_TYPE_HELLO;
107 h->min_version = MEMIF_VERSION;
108 h->max_version = MEMIF_VERSION;
109 h->max_s2m_ring = MEMIF_MAX_S2M_RING;
110 h->max_m2s_ring = MEMIF_MAX_M2S_RING;
111 h->max_region = MEMIF_MAX_REGION;
112 h->max_log2_ring_size = MEMIF_MAX_LOG2_RING_SIZE;
114 strlcpy ((char *) h->name, (char *) lm->app_name, sizeof (h->name));
116 /* msg hello is not enqueued but sent directly,
117 because it is the first msg to be sent */
118 return memif_msg_send (fd, &msg, -1);
121 /* send id and secret (optional) for interface identification */
123 memif_msg_enq_init (memif_connection_t * c)
125 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
126 memif_msg_queue_elt_t *e =
127 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
129 return memif_syscall_error_handler (errno);
130 memset (e, 0, sizeof (memif_msg_queue_elt_t));
132 memset (&e->msg, 0, sizeof (e->msg));
133 memif_msg_init_t *i = &e->msg.init;
135 e->msg.type = MEMIF_MSG_TYPE_INIT;
137 i->version = MEMIF_VERSION;
138 i->id = c->args.interface_id;
139 i->mode = c->args.mode;
141 strlcpy ((char *) i->name, (char *) lm->app_name, sizeof (i->name));
142 if (strlen ((char *) c->args.secret) > 0)
143 strncpy ((char *) i->secret, (char *) c->args.secret, sizeof (i->secret));
146 if (c->msg_queue == NULL)
149 return MEMIF_ERR_SUCCESS; /* 0 */
152 memif_msg_queue_elt_t *cur = c->msg_queue;
153 while (cur->next != NULL)
159 return MEMIF_ERR_SUCCESS; /* 0 */
162 /* send information about region specified by region_index */
164 memif_msg_enq_add_region (memif_connection_t * c, uint8_t region_index)
166 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
167 memif_region_t *mr = &c->regions[region_index];
169 memif_msg_queue_elt_t *e =
170 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
172 return memif_syscall_error_handler (errno);
174 memset (&e->msg, 0, sizeof (e->msg));
175 memif_msg_add_region_t *ar = &e->msg.add_region;
177 e->msg.type = MEMIF_MSG_TYPE_ADD_REGION;
179 ar->index = region_index;
180 ar->size = mr->region_size;
183 if (c->msg_queue == NULL)
186 return MEMIF_ERR_SUCCESS; /* 0 */
189 memif_msg_queue_elt_t *cur = c->msg_queue;
190 while (cur->next != NULL)
196 return MEMIF_ERR_SUCCESS; /* 0 */
199 /* send information about ring specified by direction (S2M | M2S) and index */
201 memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
203 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
204 memif_msg_queue_elt_t *e =
205 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
207 return memif_syscall_error_handler (errno);
209 memset (&e->msg, 0, sizeof (e->msg));
210 memif_msg_add_ring_t *ar = &e->msg.add_ring;
212 e->msg.type = MEMIF_MSG_TYPE_ADD_RING;
214 /* TODO: support multiple rings */
216 if (dir == MEMIF_RING_M2S)
217 mq = &c->rx_queues[index];
219 mq = &c->tx_queues[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 ar->private_hdr_size = 0;
230 if (c->msg_queue == NULL)
233 return MEMIF_ERR_SUCCESS; /* 0 */
236 memif_msg_queue_elt_t *cur = c->msg_queue;
237 while (cur->next != NULL)
243 return MEMIF_ERR_SUCCESS; /* 0 */
246 /* used as connection request from slave */
248 memif_msg_enq_connect (memif_connection_t * c)
250 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
251 memif_msg_queue_elt_t *e =
252 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
254 return memif_syscall_error_handler (errno);
256 memset (&e->msg, 0, sizeof (e->msg));
257 memif_msg_connect_t *cm = &e->msg.connect;
259 e->msg.type = MEMIF_MSG_TYPE_CONNECT;
261 strlcpy ((char *) cm->if_name, (char *) c->args.interface_name,
262 sizeof (cm->if_name));
265 if (c->msg_queue == NULL)
268 return MEMIF_ERR_SUCCESS; /* 0 */
271 memif_msg_queue_elt_t *cur = c->msg_queue;
272 while (cur->next != NULL)
278 return MEMIF_ERR_SUCCESS; /* 0 */
281 /* used as confirmation of connection by master */
283 memif_msg_enq_connected (memif_connection_t * c)
285 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
286 memif_msg_queue_elt_t *e =
287 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
289 return memif_syscall_error_handler (errno);
291 memset (&e->msg, 0, sizeof (e->msg));
292 memif_msg_connected_t *cm = &e->msg.connected;
294 e->msg.type = MEMIF_MSG_TYPE_CONNECTED;
296 strlcpy ((char *) cm->if_name, (char *) c->args.interface_name,
297 sizeof (cm->if_name));
300 if (c->msg_queue == NULL)
303 return MEMIF_ERR_SUCCESS; /* 0 */
306 memif_msg_queue_elt_t *cur = c->msg_queue;
307 while (cur->next != NULL)
313 return MEMIF_ERR_SUCCESS; /* 0 */
316 /* immediately send disconnect msg */
317 /* specify protocol for disconnect msg err_code
318 so that it will be compatible with VPP? (header/doc) */
320 memif_msg_send_disconnect (int fd, uint8_t * err_string, uint32_t err_code)
322 memif_msg_t msg = { 0 };
323 memif_msg_disconnect_t *d = &msg.disconnect;
325 msg.type = MEMIF_MSG_TYPE_DISCONNECT;
327 uint16_t l = strlen ((char *) err_string);
328 if (l > sizeof (d->string) - 1)
330 DBG ("Disconnect string too long. Sending the first %d characters.",
331 sizeof (d->string) - 1);
333 strlcpy ((char *) d->string, (char *) err_string, sizeof (d->string));
335 return memif_msg_send (fd, &msg, -1);
339 memif_msg_receive_hello (memif_connection_t * c, memif_msg_t * msg)
341 memif_msg_hello_t *h = &msg->hello;
343 if (msg->hello.min_version > MEMIF_VERSION ||
344 msg->hello.max_version < MEMIF_VERSION)
346 DBG ("incompatible protocol version");
347 return MEMIF_ERR_PROTO;
350 c->run_args.num_s2m_rings = memif_min (h->max_s2m_ring + 1,
351 c->args.num_s2m_rings);
352 c->run_args.num_m2s_rings = memif_min (h->max_m2s_ring + 1,
353 c->args.num_m2s_rings);
354 c->run_args.log2_ring_size = memif_min (h->max_log2_ring_size,
355 c->args.log2_ring_size);
356 c->run_args.buffer_size = c->args.buffer_size;
357 strlcpy ((char *) c->remote_name, (char *) h->name, sizeof (c->remote_name));
359 return MEMIF_ERR_SUCCESS; /* 0 */
362 /* handle interface identification (id, secret (optional)) */
364 memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
366 memif_msg_init_t *i = &msg->init;
367 memif_list_elt_t *elt = NULL;
368 memif_list_elt_t elt2;
369 memif_connection_t *c = NULL;
370 libmemif_main_t *lm = get_libmemif_main (ms);
371 uint8_t err_string[96];
372 memset (err_string, 0, sizeof (char) * 96);
373 int err = MEMIF_ERR_SUCCESS; /* 0 */
375 if (i->version != MEMIF_VERSION)
377 DBG ("MEMIF_VER_ERR");
378 strncpy ((char *) err_string, MEMIF_VER_ERR, strlen (MEMIF_VER_ERR));
379 err = MEMIF_ERR_PROTO;
383 get_list_elt (&elt, ms->interface_list, ms->interface_list_len, i->id);
386 DBG ("MEMIF_ID_ERR");
387 strncpy ((char *) err_string, MEMIF_ID_ERR, strlen (MEMIF_ID_ERR));
392 c = (memif_connection_t *) elt->data_struct;
394 if (!(c->args.is_master))
396 DBG ("MEMIF_SLAVE_ERR");
397 strncpy ((char *) err_string, MEMIF_SLAVE_ERR,
398 strlen (MEMIF_SLAVE_ERR));
399 err = MEMIF_ERR_ACCSLAVE;
404 DBG ("MEMIF_CONN_ERR");
405 strncpy ((char *) err_string, MEMIF_CONN_ERR, strlen (MEMIF_CONN_ERR));
406 err = MEMIF_ERR_ALRCONN;
412 if (i->mode != c->args.mode)
414 DBG ("MEMIF_MODE_ERR");
415 strncpy ((char *) err_string, MEMIF_MODE_ERR, strlen (MEMIF_MODE_ERR));
416 err = MEMIF_ERR_MODE;
420 strlcpy ((char *) c->remote_name, (char *) i->name, sizeof (c->remote_name));
422 if (strlen ((char *) c->args.secret) > 0)
425 if (strlen ((char *) i->secret) > 0)
427 if (strlen ((char *) c->args.secret) != strlen ((char *) i->secret))
429 DBG ("MEMIF_SECRET_ERR");
430 strncpy ((char *) err_string,
431 MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
432 err = MEMIF_ERR_SECRET;
435 r = strncmp ((char *) i->secret, (char *) c->args.secret,
436 strlen ((char *) c->args.secret));
439 DBG ("MEMIF_SECRET_ERR");
440 strncpy ((char *) err_string,
441 MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
442 err = MEMIF_ERR_SECRET;
448 DBG ("MEMIF_NOSECRET_ERR");
449 strncpy ((char *) err_string,
450 MEMIF_NOSECRET_ERR, strlen (MEMIF_NOSECRET_ERR));
451 err = MEMIF_ERR_NOSECRET;
456 c->read_fn = memif_conn_fd_read_ready;
457 c->write_fn = memif_conn_fd_write_ready;
458 c->error_fn = memif_conn_fd_error;
461 elt2.data_struct = c;
463 add_list_elt (lm, &elt2, &lm->control_list, &lm->control_list_len);
464 free_list_elt (lm->pending_list, lm->pending_list_len, fd);
469 memif_msg_send_disconnect (fd, err_string, 0);
470 lm->control_fd_update (fd, MEMIF_FD_EVENT_DEL, lm->private_ctx);
471 free_list_elt (lm->pending_list, lm->pending_list_len, fd);
477 /* receive region information and add new region to connection (if possible) */
479 memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
482 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
484 memif_msg_add_region_t *ar = &msg->add_region;
487 return MEMIF_ERR_NO_SHMFD;
489 if (ar->index > MEMIF_MAX_REGION)
490 return MEMIF_ERR_MAXREG;
493 (memif_region_t *) lm->realloc (c->regions,
494 sizeof (memif_region_t) *
497 return memif_syscall_error_handler (errno);
498 memset (mr + ar->index, 0, sizeof (memif_region_t));
500 c->regions[ar->index].fd = fd;
501 c->regions[ar->index].region_size = ar->size;
502 c->regions[ar->index].addr = NULL;
504 /* region 0 is never external */
505 if (lm->get_external_region_addr && (ar->index != 0))
506 c->regions[ar->index].is_external = 1;
508 return MEMIF_ERR_SUCCESS; /* 0 */
511 /* receive ring information and add new ring to connection queue
512 (based on direction S2M | M2S) */
514 memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
516 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
518 memif_msg_add_ring_t *ar = &msg->add_ring;
523 return MEMIF_ERR_NO_INTFD;
525 if (ar->private_hdr_size != 0)
526 return MEMIF_ERR_PRIVHDR;
528 if (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M)
530 if (ar->index > MEMIF_MAX_S2M_RING)
531 return MEMIF_ERR_MAXRING;
532 if (ar->index >= c->args.num_s2m_rings)
533 return MEMIF_ERR_MAXRING;
536 (memif_queue_t *) lm->realloc (c->rx_queues,
537 sizeof (memif_queue_t) *
538 (++c->rx_queues_num));
539 memset (mq + ar->index, 0, sizeof (memif_queue_t));
541 return memif_syscall_error_handler (errno);
543 c->rx_queues[ar->index].int_fd = fd;
544 c->rx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
545 c->rx_queues[ar->index].region = ar->region;
546 c->rx_queues[ar->index].offset = ar->offset;
547 c->run_args.num_s2m_rings++;
551 if (ar->index > MEMIF_MAX_M2S_RING)
552 return MEMIF_ERR_MAXRING;
553 if (ar->index >= c->args.num_m2s_rings)
554 return MEMIF_ERR_MAXRING;
557 (memif_queue_t *) lm->realloc (c->tx_queues,
558 sizeof (memif_queue_t) *
559 (++c->tx_queues_num));
560 memset (mq + ar->index, 0, sizeof (memif_queue_t));
562 return memif_syscall_error_handler (errno);
564 c->tx_queues[ar->index].int_fd = fd;
565 c->tx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
566 c->tx_queues[ar->index].region = ar->region;
567 c->tx_queues[ar->index].offset = ar->offset;
568 c->run_args.num_m2s_rings++;
571 return MEMIF_ERR_SUCCESS; /* 0 */
574 /* slave -> master */
576 memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
578 memif_msg_connect_t *cm = &msg->connect;
579 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
580 memif_list_elt_t elt;
583 err = memif_connect1 (c);
584 if (err != MEMIF_ERR_SUCCESS)
587 strlcpy ((char *) c->remote_if_name, (char *) cm->if_name,
588 sizeof (c->remote_if_name));
591 if (c->on_interrupt != NULL)
593 for (i = 0; i < c->run_args.num_m2s_rings; i++)
595 elt.key = c->rx_queues[i].int_fd;
597 add_list_elt (lm, &elt, &lm->interrupt_list,
598 &lm->interrupt_list_len);
600 lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ,
606 c->on_connect ((void *) c, c->private_ctx);
611 /* master -> slave */
613 memif_msg_receive_connected (memif_connection_t * c, memif_msg_t * msg)
615 memif_msg_connect_t *cm = &msg->connect;
616 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
619 err = memif_connect1 (c);
620 if (err != MEMIF_ERR_SUCCESS)
623 strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
624 sizeof (c->remote_if_name));
627 if (c->on_interrupt != NULL)
629 for (i = 0; i < c->run_args.num_s2m_rings; i++)
631 lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ,
636 c->on_connect ((void *) c, c->private_ctx);
642 memif_msg_receive_disconnect (memif_connection_t * c, memif_msg_t * msg)
644 memif_msg_disconnect_t *d = &msg->disconnect;
646 memset (c->remote_disconnect_string, 0,
647 sizeof (c->remote_disconnect_string));
648 strncpy ((char *) c->remote_disconnect_string, (char *) d->string,
649 sizeof (c->remote_disconnect_string));
651 /* on returning error, handle function will call memif_disconnect () */
652 DBG ("disconnect received: %s, mode: %d",
653 c->remote_disconnect_string, c->args.mode);
654 return MEMIF_ERR_DISCONNECT;
658 memif_msg_receive (libmemif_main_t * lm, int ifd)
660 char ctl[CMSG_SPACE (sizeof (int)) +
661 CMSG_SPACE (sizeof (struct ucred))] = { 0 };
662 struct msghdr mh = { 0 };
664 memif_msg_t msg = { 0 };
666 int err = MEMIF_ERR_SUCCESS; /* 0 */
669 memif_connection_t *c = NULL;
670 memif_socket_t *ms = NULL;
671 memif_list_elt_t *elt = NULL;
673 iov[0].iov_base = (void *) &msg;
674 iov[0].iov_len = sizeof (memif_msg_t);
677 mh.msg_control = ctl;
678 mh.msg_controllen = sizeof (ctl);
680 DBG ("recvmsg fd %d", ifd);
681 size = recvmsg (ifd, &mh, 0);
682 if (size != sizeof (memif_msg_t))
685 return MEMIF_ERR_DISCONNECTED;
687 return MEMIF_ERR_MFMSG;
690 struct cmsghdr *cmsg;
692 cmsg = CMSG_FIRSTHDR (&mh);
695 if (cmsg->cmsg_level == SOL_SOCKET)
697 if (cmsg->cmsg_type == SCM_CREDENTIALS)
701 else if (cmsg->cmsg_type == SCM_RIGHTS)
703 int *fdp = (int *) CMSG_DATA (cmsg);
707 cmsg = CMSG_NXTHDR (&mh, cmsg);
710 DBG ("Message type %u received", msg.type);
712 get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
714 c = (memif_connection_t *) elt->data_struct;
718 case MEMIF_MSG_TYPE_ACK:
721 case MEMIF_MSG_TYPE_HELLO:
722 if ((err = memif_msg_receive_hello (c, &msg)) != MEMIF_ERR_SUCCESS)
724 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
726 if ((err = memif_msg_enq_init (c)) != MEMIF_ERR_SUCCESS)
728 for (i = 0; i < c->regions_num; i++)
730 if ((err = memif_msg_enq_add_region (c, i)) != MEMIF_ERR_SUCCESS)
733 for (i = 0; i < c->run_args.num_s2m_rings; i++)
736 memif_msg_enq_add_ring (c, i,
737 MEMIF_RING_S2M)) != MEMIF_ERR_SUCCESS)
740 for (i = 0; i < c->run_args.num_m2s_rings; i++)
743 memif_msg_enq_add_ring (c, i,
744 MEMIF_RING_M2S)) != MEMIF_ERR_SUCCESS)
747 if ((err = memif_msg_enq_connect (c)) != MEMIF_ERR_SUCCESS)
751 case MEMIF_MSG_TYPE_INIT:
752 get_list_elt (&elt, lm->pending_list, lm->pending_list_len, ifd);
755 ms = (memif_socket_t *) elt->data_struct;
756 if ((err = memif_msg_receive_init (ms, ifd, &msg)) != MEMIF_ERR_SUCCESS)
758 /* c->remote_pid = cr->pid */
759 /* c->remote_uid = cr->uid */
760 /* c->remote_gid = cr->gid */
761 get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
764 c = (memif_connection_t *) elt->data_struct;
765 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
769 case MEMIF_MSG_TYPE_ADD_REGION:
771 memif_msg_receive_add_region (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
773 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
777 case MEMIF_MSG_TYPE_ADD_RING:
779 memif_msg_receive_add_ring (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
781 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
785 case MEMIF_MSG_TYPE_CONNECT:
786 if ((err = memif_msg_receive_connect (c, &msg)) != MEMIF_ERR_SUCCESS)
788 if ((err = memif_msg_enq_connected (c)) != MEMIF_ERR_SUCCESS)
792 case MEMIF_MSG_TYPE_CONNECTED:
793 if ((err = memif_msg_receive_connected (c, &msg)) != MEMIF_ERR_SUCCESS)
797 case MEMIF_MSG_TYPE_DISCONNECT:
798 if ((err = memif_msg_receive_disconnect (c, &msg)) != MEMIF_ERR_SUCCESS)
803 return MEMIF_ERR_UNKNOWN_MSG;;
808 c->flags |= MEMIF_CONNECTION_FLAG_WRITE;
810 return MEMIF_ERR_SUCCESS; /* 0 */
814 memif_conn_fd_error (memif_connection_t * c)
816 DBG ("connection fd error");
817 strncpy ((char *) c->remote_disconnect_string, "connection fd error", 19);
818 int err = memif_disconnect_internal (c);
822 /* calls memif_msg_receive to handle pending messages on socket */
824 memif_conn_fd_read_ready (memif_connection_t * c)
826 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
829 err = memif_msg_receive (lm, c->fd);
832 err = memif_disconnect_internal (c);
837 /* get msg from msg queue buffer and send it to socket */
839 memif_conn_fd_write_ready (memif_connection_t * c)
841 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
842 int err = MEMIF_ERR_SUCCESS; /* 0 */
845 if ((c->flags & MEMIF_CONNECTION_FLAG_WRITE) == 0)
848 memif_msg_queue_elt_t *e = c->msg_queue;
852 c->msg_queue = c->msg_queue->next;
854 c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE;
856 err = memif_msg_send (c->fd, &e->msg, e->fd);
865 memif_conn_fd_accept_ready (memif_socket_t * ms)
868 struct sockaddr_un client;
870 libmemif_main_t *lm = get_libmemif_main (ms);
872 DBG ("accept called");
874 addr_len = sizeof (client);
876 accept (ms->fd, (struct sockaddr *) &client, (socklen_t *) & addr_len);
880 return memif_syscall_error_handler (errno);
882 DBG ("accept fd %d", ms->fd);
883 DBG ("conn fd %d", conn_fd);
885 memif_list_elt_t elt;
887 elt.data_struct = ms;
889 add_list_elt (lm, &elt, &lm->pending_list, &lm->pending_list_len);
890 lm->control_fd_update (conn_fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE,
893 return memif_msg_send_hello (lm, conn_fd);
897 memif_read_ready (libmemif_main_t * lm, int fd)
901 err = memif_msg_receive (lm, fd);