libmemif: fix insecure uses of strncpy
[vpp.git] / extras / libmemif / src / memif_private.h
index eceac67..59899fd 100644 (file)
@@ -66,6 +66,33 @@ _Static_assert (strlen (MEMIF_DEFAULT_APP_NAME) <= MEMIF_NAME_LEN,
 #define DBG(...)
 #endif /* MEMIF_DBG */
 
+#ifndef HAS_LIB_BSD
+static inline size_t
+strlcpy (char *dest, const char *src, size_t len)
+{
+  const char *s = src;
+  size_t n = len;
+
+  while (--n > 0)
+    {
+      if ((*dest++ = *s++) == '\0')
+       break;
+    }
+
+  if (n == 0)
+    {
+      if (len != 0)
+       *dest = '\0';
+      while (*s++)
+       ;
+    }
+
+  return (s - src - 1);
+}
+#else
+#include <bsd/string.h>
+#endif
+
 typedef enum
 {
   MEMIF_SOCKET_TYPE_NONE = 0,  /* unassigned, not used by any interface */
@@ -95,7 +122,7 @@ typedef struct
   int int_fd;
 
   uint64_t int_count;
-  uint32_t alloc_bufs;
+  uint32_t next_buf; /* points to next free buffer */
 } memif_queue_t;
 
 typedef struct memif_msg_queue_elt
@@ -120,6 +147,8 @@ typedef struct
   memif_log2_ring_size_t log2_ring_size;
 } memif_conn_run_args_t;
 
+struct libmemif_main;
+
 typedef struct memif_connection
 {
   uint16_t index;
@@ -165,19 +194,25 @@ typedef struct
   uint16_t use_count;
   memif_socket_type_t type;
   uint8_t *filename;
+  /* unique database */
+  struct libmemif_main *lm;
   uint16_t interface_list_len;
   void *private_ctx;
   memif_list_elt_t *interface_list;    /* memif master interfaces listening on this socket */
 } memif_socket_t;
 
-typedef struct
+typedef struct libmemif_main
 {
   memif_control_fd_update_t *control_fd_update;
   int timerfd;
+  int epfd;
+  int poll_cancel_fd;
   struct itimerspec arm, disarm;
   uint16_t disconn_slaves;
   uint8_t app_name[MEMIF_NAME_LEN];
 
+  void *private_ctx;
+
   memif_socket_handle_t default_socket;
 
   memif_add_external_region_t *add_external_region;
@@ -200,14 +235,13 @@ typedef struct
 } libmemif_main_t;
 
 extern libmemif_main_t libmemif_main;
-extern int memif_epfd;
 
 /* main.c */
 
 /* if region doesn't contain shared memory, mmap region, check ring cookie */
 int memif_connect1 (memif_connection_t * c);
 
-/* memory map region, initalize rings and queues */
+/* memory map region, initialize rings and queues */
 int memif_init_regions_and_queues (memif_connection_t * c);
 
 int memif_disconnect_internal (memif_connection_t * c);
@@ -215,7 +249,7 @@ int memif_disconnect_internal (memif_connection_t * c);
 /* map errno to memif error code */
 int memif_syscall_error_handler (int err_code);
 
-int add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list,
+int add_list_elt (libmemif_main_t *lm, memif_list_elt_t * e, memif_list_elt_t ** list,
                  uint16_t * len);
 
 int get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list,
@@ -223,6 +257,8 @@ int get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list,
 
 int free_list_elt (memif_list_elt_t * list, uint16_t len, int key);
 
+libmemif_main_t *get_libmemif_main (memif_socket_t * ms);
+
 #ifndef __NR_memfd_create
 #if defined __x86_64__
 #define __NR_memfd_create 319