api: verify message size on receipt
[vpp.git] / src / vlibmemory / memclnt_api.c
index 23d0088..87bb36a 100644 (file)
@@ -29,6 +29,7 @@
 #include <vlib/unix/unix.h>
 #include <vlibapi/api.h>
 #include <vlibmemory/api.h>
+#include <vlibapi/api_helper_macros.h>
 
 /**
  * @file
 #include <vlibmemory/vl_memory_api_h.h>
 #undef vl_endianfun
 
+#define vl_calcsizefun
+#include <vlibmemory/vl_memory_api_h.h>
+#undef vl_calcsizefun
+
 static void
 vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t *mp)
 {
@@ -131,9 +136,20 @@ vl_api_api_versions_t_handler (vl_api_api_versions_t *mp)
   vl_api_send_msg (reg, (u8 *) rmp);
 }
 
+static void
+vl_api_control_ping_t_handler (vl_api_control_ping_t *mp)
+{
+  vl_api_control_ping_reply_t *rmp;
+  int rv = 0;
+
+  REPLY_MACRO2 (VL_API_CONTROL_PING_REPLY,
+               ({ rmp->vpe_pid = ntohl (getpid ()); }));
+}
+
 #define foreach_vlib_api_msg                                                  \
   _ (GET_FIRST_MSG_ID, get_first_msg_id)                                      \
-  _ (API_VERSIONS, api_versions)
+  _ (API_VERSIONS, api_versions)                                              \
+  _ (CONTROL_PING, control_ping)
 
 /*
  * vl_api_init
@@ -141,6 +157,7 @@ vl_api_api_versions_t_handler (vl_api_api_versions_t *mp)
 static int
 vlib_api_init (void)
 {
+  api_main_t *am = vlibapi_get_main ();
   vl_msg_api_msg_config_t cfg;
   vl_msg_api_msg_config_t *c = &cfg;
 
@@ -161,6 +178,10 @@ vlib_api_init (void)
       c->cleanup = vl_noop_handler;                                           \
       c->endian = vl_api_##n##_t_endian;                                      \
       c->print = vl_api_##n##_t_print;                                        \
+      c->print_json = vl_api_##n##_t_print_json;                              \
+      c->tojson = vl_api_##n##_t_tojson;                                      \
+      c->fromjson = vl_api_##n##_t_fromjson;                                  \
+      c->calc_size = vl_api_##n##_t_calc_size;                                \
       c->size = sizeof (vl_api_##n##_t);                                      \
       c->traced = 1;        /* trace, so these msgs print */                 \
       c->replay = 0;        /* don't replay client create/delete msgs */     \
@@ -172,6 +193,9 @@ vlib_api_init (void)
   foreach_vlib_api_msg;
 #undef _
 
+  am->is_mp_safe[VL_API_CONTROL_PING] = 1;
+  am->is_mp_safe[VL_API_CONTROL_PING_REPLY] = 1;
+
   return 0;
 }
 
@@ -486,8 +510,9 @@ api_rx_from_node (vlib_main_t *vm, vlib_node_runtime_t *node,
              vec_add (long_msg, msg, msg_len);
            }
          msg = long_msg;
+         msg_len = vec_len (long_msg);
        }
-      vl_msg_api_handler_no_trace_no_free (msg);
+      vl_msg_api_handler_no_trace_no_free (msg, msg_len);
     }
 
   /* Free what we've been given. */
@@ -685,20 +710,20 @@ rpc_api_hookup (vlib_main_t *vm)
 {
   api_main_t *am = vlibapi_get_main ();
 #define _(N, n)                                                               \
-  vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler,            \
-                          vl_noop_handler, vl_noop_handler,                  \
-                          vl_api_##n##_t_print, sizeof (vl_api_##n##_t),     \
-                          0 /* do not trace */, vl_api_##n##_t_print_json,   \
-                          vl_api_##n##_t_tojson, vl_api_##n##_t_fromjson);
+  vl_msg_api_set_handlers (                                                   \
+    VL_API_##N, #n, vl_api_##n##_t_handler, vl_noop_handler, vl_noop_handler, \
+    vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 0 /* do not trace */,      \
+    vl_api_##n##_t_print_json, vl_api_##n##_t_tojson,                         \
+    vl_api_##n##_t_fromjson, vl_api_##n##_t_calc_size);
   foreach_rpc_api_msg;
 #undef _
 
 #define _(N, n)                                                               \
-  vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler,            \
-                          vl_noop_handler, vl_noop_handler,                  \
-                          vl_api_##n##_t_print, sizeof (vl_api_##n##_t),     \
-                          1 /* do trace */, vl_api_##n##_t_print_json,       \
-                          vl_api_##n##_t_tojson, vl_api_##n##_t_fromjson);
+  vl_msg_api_set_handlers (                                                   \
+    VL_API_##N, #n, vl_api_##n##_t_handler, vl_noop_handler, vl_noop_handler, \
+    vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 1 /* do trace */,          \
+    vl_api_##n##_t_print_json, vl_api_##n##_t_tojson,                         \
+    vl_api_##n##_t_fromjson, vl_api_##n##_t_calc_size);
   foreach_plugin_trace_msg;
 #undef _