chunk: Provide a fallback chunk_map() if mmap is not available
[strongswan.git] / src / libstrongswan / utils / chunk.c
index ca277da..5961407 100644 (file)
  */
 
 #include <stdio.h>
+#include <sys/types.h>
 #include <sys/stat.h>
+#ifdef HAVE_MMAP
+# include <sys/mman.h>
+#endif
+#include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
+#include <pthread.h>
 #include <ctype.h>
 
 #include "chunk.h"
 #include "debug.h"
 
-/* required for chunk_hash */
-#undef get16bits
-#if (defined(__GNUC__) && defined(__i386__))
-#define get16bits(d) (*((const u_int16_t*)(d)))
-#endif
-#if !defined (get16bits)
-#define get16bits(d) ((((u_int32_t)(((const u_int8_t*)(d))[1])) << 8)\
-                      + (u_int32_t)(((const u_int8_t*)(d))[0]) )
-#endif
-
 /**
  * Empty chunk.
  */
@@ -250,6 +246,189 @@ bool chunk_write(chunk_t chunk, char *path, char *label, mode_t mask, bool force
        return good;
 }
 
+/**
+ * Described in header.
+ */
+bool chunk_from_fd(int fd, chunk_t *out)
+{
+       struct stat sb;
+       char *buf, *tmp;
+       ssize_t len, total = 0, bufsize;
+
+       if (fstat(fd, &sb) == 0 && S_ISREG(sb.st_mode))
+       {
+               bufsize = sb.st_size;
+       }
+       else
+       {
+               bufsize = 256;
+       }
+       buf = malloc(bufsize);
+       if (!buf)
+       {       /* for huge files */
+               return FALSE;
+       }
+
+       while (TRUE)
+       {
+               len = read(fd, buf + total, bufsize - total);
+               if (len < 0)
+               {
+                       free(buf);
+                       return FALSE;
+               }
+               if (len == 0)
+               {
+                       break;
+               }
+               total += len;
+               if (total == bufsize)
+               {
+                       bufsize *= 2;
+                       tmp = realloc(buf, bufsize);
+                       if (!tmp)
+                       {
+                               free(buf);
+                               return FALSE;
+                       }
+                       buf = tmp;
+               }
+       }
+       if (total == 0)
+       {
+               free(buf);
+               buf = NULL;
+       }
+       else if (total < bufsize)
+       {
+               buf = realloc(buf, total);
+       }
+       *out = chunk_create(buf, total);
+       return TRUE;
+}
+
+/**
+ * Implementation for mmap()ed chunks
+ */
+typedef struct {
+       /* public chunk interface */
+       chunk_t public;
+       /* FD of open file */
+       int fd;
+       /* mmap() address */
+       void *map;
+       /* size of map */
+       size_t len;
+       /* do we write? */
+       bool wr;
+} mmaped_chunk_t;
+
+/**
+ * See header.
+ */
+chunk_t *chunk_map(char *path, bool wr)
+{
+       mmaped_chunk_t *chunk;
+       struct stat sb;
+       int tmp;
+
+       INIT(chunk,
+               .fd = open(path, wr ? O_RDWR : O_RDONLY),
+               .wr = wr,
+       );
+
+       if (chunk->fd == -1)
+       {
+               free(chunk);
+               return NULL;
+       }
+       if (fstat(chunk->fd, &sb) == -1)
+       {
+               tmp = errno;
+               chunk_unmap(&chunk->public);
+               errno = tmp;
+               return NULL;
+       }
+#ifdef HAVE_MMAP
+       chunk->len = sb.st_size;
+       /* map non-empty files only, as mmap() complains otherwise */
+       if (chunk->len)
+       {
+               /* in read-only mode, we allow writes, but don't sync to disk */
+               chunk->map = mmap(NULL, chunk->len, PROT_READ | PROT_WRITE,
+                                                 wr ? MAP_SHARED : MAP_PRIVATE, chunk->fd, 0);
+               if (chunk->map == MAP_FAILED)
+               {
+                       tmp = errno;
+                       chunk_unmap(&chunk->public);
+                       errno = tmp;
+                       return NULL;
+               }
+       }
+       chunk->public = chunk_create(chunk->map, chunk->len);
+#else /* !HAVE_MMAP */
+       if (!chunk_from_fd(chunk->fd, &chunk->public))
+       {
+               tmp = errno;
+               chunk_unmap(&chunk->public);
+               errno = tmp;
+               return NULL;
+       }
+       chunk->map = chunk->public.ptr;
+       chunk->len = chunk->public.len;
+#endif /* !HAVE_MMAP */
+       return &chunk->public;
+}
+
+/**
+ * See header.
+ */
+bool chunk_unmap(chunk_t *public)
+{
+       mmaped_chunk_t *chunk;
+       bool ret = FALSE;
+       int tmp = 0;
+
+       chunk = (mmaped_chunk_t*)public;
+#ifdef HAVE_MMAP
+       if (chunk->map && chunk->map != MAP_FAILED)
+       {
+               ret = munmap(chunk->map, chunk->len) == 0;
+               tmp = errno;
+       }
+#else /* !HAVE_MMAP */
+       if (chunk->wr)
+       {
+               if (lseek(chunk->fd, 0, SEEK_SET) != -1)
+               {
+                       int len, total = 0;
+
+                       ret = TRUE;
+                       while (total < chunk->len)
+                       {
+                               len = write(chunk->fd, chunk->map + total, chunk->len - total);
+                               if (len <= 0)
+                               {
+                                       ret = FALSE;
+                                       break;
+                               }
+                               total += len;
+                       }
+               }
+               tmp = errno;
+       }
+       else
+       {
+               ret = TRUE;
+       }
+       free(chunk->map);
+#endif /* !HAVE_MMAP */
+       close(chunk->fd);
+       free(chunk);
+       errno = tmp;
+
+       return ret;
+}
 
 /** hex conversion digits */
 static char hexdig_upper[] = "0123456789ABCDEF";
@@ -659,11 +838,11 @@ static inline u_int64_t siplast(size_t len, u_char *pos)
 }
 
 /**
- * Described in header.
+ * Caculate SipHash-2-4 with an optional first block given as argument.
  */
-u_int64_t chunk_mac(chunk_t chunk, u_char *key)
+static u_int64_t chunk_mac_inc(chunk_t chunk, u_char *key, u_int64_t m)
 {
-       u_int64_t v0, v1, v2, v3, k0, k1, m;
+       u_int64_t v0, v1, v2, v3, k0, k1;
        size_t len = chunk.len;
        u_char *pos = chunk.ptr, *end;
 
@@ -677,6 +856,11 @@ u_int64_t chunk_mac(chunk_t chunk, u_char *key)
        v2 = k0 ^ 0x6c7967656e657261ULL;
        v3 = k1 ^ 0x7465646279746573ULL;
 
+       if (m)
+       {
+               sipcompress(&v0, &v1, &v2, &v3, m);
+       }
+
        /* compression with c = 2 */
        for (; pos != end; pos += 8)
        {
@@ -696,71 +880,71 @@ u_int64_t chunk_mac(chunk_t chunk, u_char *key)
 
 /**
  * Described in header.
- *
- * The implementation is based on Paul Hsieh's SuperFastHash:
- *      http://www.azillionmonkeys.com/qed/hash.html
  */
-u_int32_t chunk_hash_inc(chunk_t chunk, u_int32_t hash)
+u_int64_t chunk_mac(chunk_t chunk, u_char *key)
 {
-       u_char *data = chunk.ptr;
-       size_t len = chunk.len;
-       u_int32_t tmp;
-       int rem;
+       return chunk_mac_inc(chunk, key, 0);
+}
 
-       if (!len || data == NULL)
-       {
-               return 0;
-       }
+/**
+ * Secret key allocated randomly during first use.
+ */
+static u_char key[16];
 
-       rem = len & 3;
-       len >>= 2;
+/**
+ * Static key used in case predictable hash values are required.
+ */
+static u_char static_key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+                                                         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
 
-       /* Main loop */
-       for (; len > 0; --len)
-       {
-               hash += get16bits(data);
-               tmp   = (get16bits(data + 2) << 11) ^ hash;
-               hash  = (hash << 16) ^ tmp;
-               data += 2 * sizeof(u_int16_t);
-               hash += hash >> 11;
-       }
+/**
+ * Only allocate the key once
+ */
+static pthread_once_t key_allocated = PTHREAD_ONCE_INIT;
 
-       /* Handle end cases */
-       switch (rem)
+/**
+ * Allocate a key on first use, we do this manually to avoid dependencies on
+ * plugins.
+ */
+static void allocate_key()
+{
+       ssize_t len;
+       size_t done = 0;
+       int fd;
+
+       fd = open("/dev/urandom", O_RDONLY);
+       if (fd >= 0)
        {
-               case 3:
+               while (done < sizeof(key))
                {
-                       hash += get16bits(data);
-                       hash ^= hash << 16;
-                       hash ^= data[sizeof(u_int16_t)] << 18;
-                       hash += hash >> 11;
-                       break;
-               }
-               case 2:
-               {
-                       hash += get16bits(data);
-                       hash ^= hash << 11;
-                       hash += hash >> 17;
-                       break;
+                       len = read(fd, key + done, sizeof(key) - done);
+                       if (len < 0)
+                       {
+                               break;
+                       }
+                       done += len;
                }
-               case 1:
+               close(fd);
+       }
+       /* on error we use random() to generate the key (better than nothing) */
+       if (done < sizeof(key))
+       {
+               srandom(time(NULL) + getpid());
+               for (; done < sizeof(key); done++)
                {
-                       hash += *data;
-                       hash ^= hash << 10;
-                       hash += hash >> 1;
-                       break;
+                       key[done] = (u_char)random();
                }
        }
+}
 
-       /* Force "avalanching" of final 127 bits */
-       hash ^= hash << 3;
-       hash += hash >> 5;
-       hash ^= hash << 4;
-       hash += hash >> 17;
-       hash ^= hash << 25;
-       hash += hash >> 6;
-
-       return hash;
+/**
+ * Described in header.
+ */
+u_int32_t chunk_hash_inc(chunk_t chunk, u_int32_t hash)
+{
+       pthread_once(&key_allocated, allocate_key);
+       /* we could use a mac of the previous hash, but this is faster */
+       return chunk_mac_inc(chunk, key, ((u_int64_t)hash) << 32 | hash);
 }
 
 /**
@@ -768,7 +952,24 @@ u_int32_t chunk_hash_inc(chunk_t chunk, u_int32_t hash)
  */
 u_int32_t chunk_hash(chunk_t chunk)
 {
-       return chunk_hash_inc(chunk, chunk.len);
+       pthread_once(&key_allocated, allocate_key);
+       return chunk_mac(chunk, key);
+}
+
+/**
+ * Described in header.
+ */
+u_int32_t chunk_hash_static_inc(chunk_t chunk, u_int32_t hash)
+{      /* we could use a mac of the previous hash, but this is faster */
+       return chunk_mac_inc(chunk, static_key, ((u_int64_t)hash) << 32 | hash);
+}
+
+/**
+ * Described in header.
+ */
+u_int32_t chunk_hash_static(chunk_t chunk)
+{
+       return chunk_mac(chunk, static_key);
 }
 
 /**
@@ -782,7 +983,7 @@ int chunk_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
        chunk_t copy = *chunk;
        int written = 0;
 
-       if (!spec->hash)
+       if (!spec->hash && !spec->plus)
        {
                u_int chunk_len = chunk->len;
                const void *new_args[] = {&chunk->ptr, &chunk_len};
@@ -795,7 +996,7 @@ int chunk_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
                {
                        first = FALSE;
                }
-               else
+               else if (!spec->plus)
                {
                        written += print_in_hook(data, ":");
                }