Code

src/common.[ch]: Implemented `htond' and `ntohd' and applied it to the network plugin.
authorFlorian Forster <octo@noris.net>
Thu, 28 Feb 2008 14:15:53 +0000 (15:15 +0100)
committerFlorian Forster <octo@noris.net>
Thu, 28 Feb 2008 14:15:53 +0000 (15:15 +0100)
The functions convert doubles to the x86 representation or from the x86
representation to the representation used on the host. On x86 systems, this is
a NOP.

src/common.c
src/common.h
src/network.c

index 1138f96ff92e688063990c1bdd084eca816fbb47..1c8f20a42b592744dae45d729ed6679845ab80ba 100644 (file)
@@ -570,6 +570,83 @@ unsigned long long htonll (unsigned long long n)
 #endif
 } /* unsigned long long htonll */
 
+#if FP_LAYOUT_NEED_NOTHING
+/* Well, we need nothing.. */
+/* #endif FP_LAYOUT_NEED_NOTHING */
+
+#elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
+# if FP_LAYOUT_NEED_ENDIANFLIP
+#  define FP_CONVERT(A) ((((uint64_t)(A) & 0xff00000000000000LL) >> 56) | \
+                         (((uint64_t)(A) & 0x00ff000000000000LL) >> 40) | \
+                         (((uint64_t)(A) & 0x0000ff0000000000LL) >> 24) | \
+                         (((uint64_t)(A) & 0x000000ff00000000LL) >> 8)  | \
+                         (((uint64_t)(A) & 0x00000000ff000000LL) << 8)  | \
+                         (((uint64_t)(A) & 0x0000000000ff0000LL) << 24) | \
+                         (((uint64_t)(A) & 0x000000000000ff00LL) << 40) | \
+                         (((uint64_t)(A) & 0x00000000000000ffLL) << 56))
+# else
+#  define FP_CONVERT(A) ((((uint64_t)(A) & 0xffffffff00000000LL) >> 32) | \
+                         (((uint64_t)(A) & 0x00000000ffffffffLL) << 32))
+# endif
+
+double ntohd (double d)
+{
+       union
+       {
+               char     byte[8];
+               uint64_t integer;
+               double   floating;
+       } ret;
+
+       ret.floating = d;
+
+       /* NAN in x86 byte order */
+       if ((ret.byte[0] == 0x00) && (ret.byte[1] == 0x00)
+                       && (ret.byte[2] == 0x00) && (ret.byte[3] == 0x00)
+                       && (ret.byte[4] == 0x00) && (ret.byte[5] == 0x00)
+                       && (ret.byte[6] == 0xf8) && (ret.byte[7] == 0x7f))
+       {
+               return (NAN);
+       }
+       else
+       {
+               uint64_t tmp;
+
+               tmp = ret.integer;
+               ret.integer = FP_CONVERT (tmp);
+               return (ret.floating);
+       }
+} /* double ntohd */
+
+double htond (double d)
+{
+       union
+       {
+               char     byte[8];
+               uint64_t integer;
+               double   floating;
+       } ret;
+
+       if (isnan (d))
+       {
+               ret.byte[0] = ret.byte[1] = ret.byte[2] = ret.byte[3] = 0x00;
+               ret.byte[4] = ret.byte[5] = 0x00;
+               ret.byte[6] = 0xf8;
+               ret.byte[7] = 0x7f;
+               return (ret.floating);
+       }
+       else
+       {
+               uint64_t tmp;
+
+               ret.floating = d;
+               tmp = FP_CONVERT (ret.integer);
+               ret.integer = tmp;
+               return (ret.floating);
+       }
+} /* double htond */
+#endif /* FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP */
+
 int format_name (char *ret, int ret_len,
                const char *hostname,
                const char *plugin, const char *plugin_instance,
index f759bc5ec0019d42d4b0461589c70e24db50b99f..9cd2476fe15bef98ea898f7a3a27c9eee6f9d11d 100644 (file)
@@ -163,6 +163,16 @@ long long get_kstat_value (kstat_t *ksp, char *name);
 unsigned long long ntohll (unsigned long long n);
 unsigned long long htonll (unsigned long long n);
 
+#if FP_LAYOUT_NEED_NOTHING
+# define ntohd(d) (d)
+# define htond(d) (d)
+#elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
+double ntohd (double d);
+double htond (double d);
+#else
+# error "Don't know how to convert between host and network representation of doubles."
+#endif
+
 int format_name (char *ret, int ret_len,
                const char *hostname,
                const char *plugin, const char *plugin_instance,
index 469baf7b8bbd7436ab591052a4206a55d3cbe7c4..e7bf7d42cdfceea68431002a4aa1069746ce7c1a 100644 (file)
@@ -356,7 +356,7 @@ static int write_part_values (char **ret_buffer, int *ret_buffer_len,
                else
                {
                        pkg_values_types[i] = DS_TYPE_GAUGE;
-                       pkg_values[i].gauge = vl->values[i].gauge;
+                       pkg_values[i].gauge = htond (vl->values[i].gauge);
                }
        }
 
@@ -496,6 +496,8 @@ static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
        for (i = 0; i < h_num; i++)
                if (pv.values_types[i] == DS_TYPE_COUNTER)
                        pv.values[i].counter = ntohll (pv.values[i].counter);
+               else
+                       pv.values[i].gauge = ntohd (pv.values[i].gauge);
 
        *ret_buffer     = (void *) (pv.values + h_num);
        *ret_buffer_len = buffer_len - h_length;