From: Florian Forster Date: Thu, 28 Feb 2008 14:15:53 +0000 (+0100) Subject: src/common.[ch]: Implemented `htond' and `ntohd' and applied it to the network plugin. X-Git-Tag: collectd-4.2.5~6 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=4438c61e3f0656b50867753312a011ba737cc649;p=collectd.git src/common.[ch]: Implemented `htond' and `ntohd' and applied it to the network plugin. 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. --- diff --git a/src/common.c b/src/common.c index 1138f96f..1c8f20a4 100644 --- a/src/common.c +++ b/src/common.c @@ -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, diff --git a/src/common.h b/src/common.h index f759bc5e..9cd2476f 100644 --- a/src/common.h +++ b/src/common.h @@ -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, diff --git a/src/network.c b/src/network.c index 469baf7b..e7bf7d42 100644 --- a/src/network.c +++ b/src/network.c @@ -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;