libcollectdclient: Add defines for Windows compatibility.
[collectd.git] / src / libcollectdclient / network_buffer.c
index c795a10..b476794 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/libcollectdclient/network_buffer.c
- * Copyright (C) 2010-2012  Florian octo Forster
+ * Copyright (C) 2010-2014  Florian octo Forster
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  *   Florian octo Forster <octo at collectd.org>
  **/
 
-#include "config.h"
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#if WIN32
+
+#include <windows.h>
+#include <math.h>
+#include <assert.h>
+
+#else
 
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
 
 #if HAVE_LIBGCRYPT
-#include <gcrypt.h>
+# include <pthread.h>
+# if defined __APPLE__
+/* default xcode compiler throws warnings even when deprecated functionality
+ * is not used. -Werror breaks the build because of erroneous warnings.
+ * http://stackoverflow.com/questions/10556299/compiler-warnings-with-libgcrypt-v1-5-0/12830209#12830209
+ */
+#  pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+# endif
+/* FreeBSD's copy of libgcrypt extends the existing GCRYPT_NO_DEPRECATED
+ * to properly hide all deprecated functionality.
+ * http://svnweb.freebsd.org/ports/head/security/libgcrypt/files/patch-src__gcrypt.h.in
+ */
+# define GCRYPT_NO_DEPRECATED
+# include <gcrypt.h>
+# if defined __APPLE__
+/* Re enable deprecation warnings */
+#  pragma GCC diagnostic warning "-Wdeprecated-declarations"
+# endif
 GCRY_THREAD_OPTION_PTHREAD_IMPL;
 #endif
 
+#endif /* !WIN32 */
+
 #include "collectd/network_buffer.h"
 
 #define TYPE_HOST            0x0000
@@ -63,6 +92,10 @@ GCRY_THREAD_OPTION_PTHREAD_IMPL;
 #define PART_SIGNATURE_SHA256_SIZE 36
 #define PART_ENCRYPTION_AES256_SIZE 42
 
+#ifndef ENOTSUP
+# define ENOTSUP -1
+#endif
+
 #define ADD_GENERIC(nb,srcptr,size) do {         \
   assert ((size) <= (nb)->free);                 \
   memcpy ((nb)->ptr, (srcptr), (size));          \
@@ -89,9 +122,11 @@ struct lcc_network_buffer_s
   char *username;
   char *password;
 
+#if HAVE_LIBGCRYPT
   gcry_cipher_hd_t encr_cypher;
   size_t encr_header_len;
   char encr_iv[16];
+#endif
 };
 
 #define SSTRNCPY(dst,src,sz) do { \
@@ -111,6 +146,7 @@ static _Bool have_gcrypt (void) /* {{{ */
     return (result);
   need_init = 0;
 
+#if HAVE_LIBGCRYPT
   gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
 
   if (!gcry_check_version (GCRYPT_VERSION))
@@ -121,8 +157,12 @@ static _Bool have_gcrypt (void) /* {{{ */
 
   result = 1;
   return (1);
+#else
+  return(0);
+#endif
 } /* }}} _Bool have_gcrypt */
 
+#ifndef HAVE_HTONLL
 static uint64_t htonll (uint64_t val) /* {{{ */
 {
   static int config = 0;
@@ -152,6 +192,7 @@ static uint64_t htonll (uint64_t val) /* {{{ */
 
   return ((((uint64_t) lo) << 32) | ((uint64_t) hi));
 } /* }}} uint64_t htonll */
+#endif
 
 static double htond (double val) /* {{{ */
 {
@@ -477,6 +518,8 @@ static int nb_add_value_list (lcc_network_buffer_t *nb, /* {{{ */
   return (0);
 } /* }}} int nb_add_value_list */
 
+/* TODO: Add encryption for Windows */
+#if HAVE_LIBGCRYPT
 static int nb_add_signature (lcc_network_buffer_t *nb) /* {{{ */
 {
   char *buffer;
@@ -603,6 +646,7 @@ static int nb_add_encryption (lcc_network_buffer_t *nb) /* {{{ */
 
   return (0);
 } /* }}} int nb_add_encryption */
+#endif
 
 /*
  * Public functions
@@ -703,6 +747,7 @@ int lcc_network_buffer_initialize (lcc_network_buffer_t *nb) /* {{{ */
   nb->ptr = nb->buffer;
   nb->free = nb->size;
 
+#if HAVE_LIBGCRYPT
   if (nb->seclevel == SIGN)
   {
     size_t username_len;
@@ -748,6 +793,7 @@ int lcc_network_buffer_initialize (lcc_network_buffer_t *nb) /* {{{ */
     ADD_GENERIC (nb, hash, sizeof (hash));
     assert ((nb->encr_header_len + nb->free) == nb->size);
   }
+#endif
 
   return (0);
 } /* }}} int lcc_network_buffer_initialize */
@@ -757,10 +803,12 @@ int lcc_network_buffer_finalize (lcc_network_buffer_t *nb) /* {{{ */
   if (nb == NULL)
     return (EINVAL);
 
+#if HAVE_LIBGCRYPT
   if (nb->seclevel == SIGN)
     nb_add_signature (nb);
   else if (nb->seclevel == ENCRYPT)
     nb_add_encryption (nb);
+#endif
 
   return (0);
 } /* }}} int lcc_network_buffer_finalize */