api: verify message size on receipt
[vpp.git] / src / vlibmemory / socket_client.c
index 5483110..7082d8b 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <stdio.h>
 #define __USE_GNU
+#define _GNU_SOURCE
 #include <sys/socket.h>
 
 #include <svm/ssvm.h>
 #include <vlibmemory/vl_memory_api_h.h>
 #undef vl_endianfun
 
+#define vl_calcsizefun
+#include <vlibmemory/vl_memory_api_h.h>
+#undef vl_calcsizefun
+
 /* instantiate all the print functions we know about */
 #define vl_print(handle, ...) clib_warning (__VA_ARGS__)
 #define vl_printfun
@@ -133,7 +138,7 @@ vl_socket_client_read_internal (socket_client_main_t * scm, int wait)
 
       if (vec_len (scm->socket_rx_buffer) >= data_len + sizeof (*mbp))
        {
-         vl_msg_api_socket_handler ((void *) (mbp->data));
+         vl_msg_api_socket_handler ((void *) (mbp->data), data_len);
 
          if (vec_len (scm->socket_rx_buffer) == data_len + sizeof (*mbp))
            _vec_len (scm->socket_rx_buffer) = 0;
@@ -260,10 +265,10 @@ vl_socket_client_enable_disable (int enable)
   vl_socket_client_enable_disable2 (socket_client_ctx, enable);
 }
 
-clib_error_t *
-vl_sock_api_recv_fd_msg (int socket_fd, int fds[], int n_fds, u32 wait)
+static clib_error_t *
+vl_sock_api_recv_fd_msg_internal (socket_client_main_t * scm, int fds[],
+                                 int n_fds, u32 wait)
 {
-  socket_client_main_t *scm = socket_client_ctx;
   char msgbuf[16];
   char ctl[CMSG_SPACE (sizeof (int) * n_fds)
           + CMSG_SPACE (sizeof (struct ucred))];
@@ -275,8 +280,11 @@ vl_sock_api_recv_fd_msg (int socket_fd, int fds[], int n_fds, u32 wait)
   pid_t pid __attribute__ ((unused));
   uid_t uid __attribute__ ((unused));
   gid_t gid __attribute__ ((unused));
+  int socket_fd;
   f64 timeout;
 
+  socket_fd = scm->client_socket.fd;
+
   iov[0].iov_base = msgbuf;
   iov[0].iov_len = 5;
   mh.msg_iov = iov;
@@ -324,6 +332,26 @@ vl_sock_api_recv_fd_msg (int socket_fd, int fds[], int n_fds, u32 wait)
   return 0;
 }
 
+clib_error_t *
+vl_sock_api_recv_fd_msg (int socket_fd, int fds[], int n_fds, u32 wait)
+{
+  return vl_sock_api_recv_fd_msg_internal (socket_client_ctx, fds, n_fds,
+                                          wait);
+}
+
+clib_error_t *
+vl_sock_api_recv_fd_msg2 (socket_client_main_t * scm, int socket_fd,
+                         int fds[], int n_fds, u32 wait)
+{
+  socket_client_main_t *old_ctx;
+  clib_error_t *error;
+
+  old_ctx = vl_socket_client_ctx_push (scm);
+  error = vl_sock_api_recv_fd_msg_internal (scm, fds, n_fds, wait);
+  vl_socket_client_ctx_pop (old_ctx);
+  return error;
+}
+
 static void vl_api_sock_init_shm_reply_t_handler
   (vl_api_sock_init_shm_reply_t * mp)
 {
@@ -356,7 +384,7 @@ static void vl_api_sock_init_shm_reply_t_handler
   memfd->fd = my_fd;
 
   /* Note: this closes memfd.fd */
-  retval = ssvm_slave_init_memfd (memfd);
+  retval = ssvm_client_init_memfd (memfd);
   if (retval)
     clib_warning ("WARNING: segment map returned %d", retval);
 
@@ -408,13 +436,12 @@ void
 vl_sock_client_install_message_handlers (void)
 {
 
-#define _(N,n)                                                  \
-    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
-                            vl_api_##n##_t_handler,             \
-                            noop_handler,                       \
-                            vl_api_##n##_t_endian,              \
-                            vl_api_##n##_t_print,               \
-                            sizeof(vl_api_##n##_t), 1);
+#define _(N, n)                                                               \
+  vl_msg_api_set_handlers (                                                   \
+    VL_API_##N, #n, vl_api_##n##_t_handler, noop_handler,                     \
+    vl_api_##n##_t_endian, vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 0,  \
+    vl_api_##n##_t_print_json, vl_api_##n##_t_tojson,                         \
+    vl_api_##n##_t_fromjson, vl_api_##n##_t_calc_size);
   foreach_sock_client_api_msg;
 #undef _
 }
@@ -438,7 +465,7 @@ vl_socket_client_connect_internal (socket_client_main_t * scm,
 
   sock = &scm->client_socket;
   sock->config = socket_path;
-  sock->flags = CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_NON_BLOCKING_CONNECT;
+  sock->flags = CLIB_SOCKET_F_IS_CLIENT;
 
   if ((error = clib_socket_init (sock)))
     {
@@ -562,7 +589,7 @@ vl_socket_client_recv_fd_msg2 (socket_client_main_t * scm, int fds[],
 {
   if (!scm->socket_fd)
     return clib_error_return (0, "no socket");
-  return vl_sock_api_recv_fd_msg (scm->client_socket.fd, fds, n_fds, wait);
+  return vl_sock_api_recv_fd_msg_internal (scm, fds, n_fds, wait);
 }
 
 clib_error_t *