chiark / gitweb /
Provide various wrappers for memcpy (COPY_OBJ, BUF_...)
[secnet.git] / secnet.h
index d5663ff980257ecfcb8b6b0736ee479e7f5eaf5f..5eeb2132554f8cb8e971eb8075283d6cc45b5244 100644 (file)
--- a/secnet.h
+++ b/secnet.h
@@ -9,6 +9,8 @@
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <fcntl.h>
+#include <unistd.h>
 #include <sys/poll.h>
 #include <sys/types.h>
 #include <sys/time.h>
 
 typedef char *string_t;
 typedef const char *cstring_t;
-typedef enum {False,True} bool_t;
+
+#define False (_Bool)0
+#define True  (_Bool)1
+typedef _Bool bool_t;
 
 #define ASSERT(x) do { if (!(x)) { fatal("assertion failed line %d file " \
                                         __FILE__,__LINE__); } } while(0)
@@ -133,6 +138,9 @@ extern char *safe_strdup(const char *string, const char *message);
 extern void *safe_malloc(size_t size, const char *message);
 extern void *safe_malloc_ary(size_t size, size_t count, const char *message);
 
+void setcloexec(int fd); /* cannot fail */
+void pipe_cloexec(int fd[2]); /* pipe(), setcloexec() twice; cannot fail */
+
 extern int sys_cmd(const char *file, const char *argc, ...);
 
 extern uint64_t now_global;
@@ -341,15 +349,22 @@ struct comm_if {
     comm_addr_to_string_fn *addr_to_string;
 };
 
+static inline const char *comm_addr_to_string(const struct comm_addr *ca)
+{
+    return ca->comm->addr_to_string(ca->comm->st, ca);
+}
+
 /* LOG interface */
 
+#define LOG_MESSAGE_BUFLEN 1023
+
 typedef void log_msg_fn(void *st, int class, const char *message, ...);
 typedef void log_vmsg_fn(void *st, int class, const char *message,
                         va_list args);
 struct log_if {
     void *st;
-    log_msg_fn *logfn;   /* Do not call these directly - you don't get */
     log_vmsg_fn *vlogfn; /* printf format checking.  Use [v]slilog instead */
+    char buff[LOG_MESSAGE_BUFLEN+1];
 };
 /* (convenience functions, defined in util.c) */
 extern void slilog(struct log_if *lf, int class, const char *message, ...)
@@ -357,6 +372,13 @@ FORMAT(printf,3,4);
 extern void vslilog(struct log_if *lf, int class, const char *message, va_list)
 FORMAT(printf,3,0);
 
+/* Versions which take (parts of) (multiple) messages, using \n to
+ * distinguish one message from another. */
+extern void slilog_part(struct log_if *lf, int class, const char *message, ...)
+FORMAT(printf,3,4);
+extern void vslilog_part(struct log_if *lf, int class, const char *message,
+                        va_list) FORMAT(printf,3,0);
+
 /* SITE interface */
 
 /* Pretty much a placeholder; allows starting and stopping of processing,
@@ -482,7 +504,7 @@ struct buffer_if {
     uint8_t *base;
     uint8_t *start;
     int32_t size; /* Size of buffer contents */
-    int32_t len; /* Total length allocated at base */
+    int32_t alloclen; /* Total length allocated at base */
 };
 
 /***** LOG functions *****/
@@ -529,4 +551,17 @@ extern void log_from_fd(int fd, cstring_t prefix, struct log_if *log);
 
 #define FILLZERO(obj) (memset(&(obj),0,sizeof((obj))))
 
+/*
+ * void COPY_OBJ(  OBJECT& dst, const OBJECT& src);
+ * void COPY_ARRAY(OBJECT *dst, const OBJECT *src, INTEGER count);
+ *   // Typesafe: we check that the type OBJECT is the same in both cases.
+ *   // It is OK to use COPY_OBJ on an array object, proviced it's
+ *   // _actually_ the whole array object and not decayed into a
+ *   // pointer (e.g. a formal parameter).
+ */
+#define COPY_OBJ(dst,src) \
+    (&(dst)==&(src), memcpy(&(dst),&(src),sizeof((dst))))
+#define COPY_ARRAY(dst,src,count) \
+    (&(dst)[0]==&(src)[0], memcpy((dst),(src),sizeof((dst)[0])*(count)))
+
 #endif /* secnet_h */