summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuo Jinghua <sunmoon1997@gmail.com>2009-11-04 22:30:03 +0800
committerLuo Jinghua <sunmoon1997@gmail.com>2009-11-04 22:30:03 +0800
commit0946453cdf5809809952bbba08d45af299f3aaa1 (patch)
treea7292e1cb4ebdb17f14117121682d0264fba5bb3
parentfbe6410705659abc638e4f329c3b12aa5ab9bf4f (diff)
milkway: add network byte order from/to host byte order converters
-rw-r--r--milkway/mw-byteorder.h63
1 files changed, 63 insertions, 0 deletions
diff --git a/milkway/mw-byteorder.h b/milkway/mw-byteorder.h
index d934276..709f283 100644
--- a/milkway/mw-byteorder.h
+++ b/milkway/mw-byteorder.h
@@ -204,6 +204,69 @@ mw_letoh64 (mw_uint64_t u)
#endif
}
+/**
+ * @brief convert a value to network byte order from host byte order
+ *
+ * @param u the integer in host order
+ * @return the value of integer in network byte order
+ */
+static mw_inline mw_uint16_t
+mw_htons (mw_uint16_t u)
+{
+#ifdef MW_WORDS_BIGENDIAN
+ return u;
+#else
+ return mw_swap16 (u);
+#endif
+}
+
+/**
+ * @brief convert a value to host byte order from network byte order
+ *
+ * @param u the integer in host byte order(big endian)
+ * @return the value of integer in host byte order
+ */
+static mw_inline mw_uint16_t
+mw_ntohs (mw_uint16_t u)
+{
+#ifdef MW_WORDS_BIGENDIAN
+ return u;
+#else
+ return mw_swap16 (u);
+#endif
+}
+
+/**
+ * @brief convert values to network byte order from host byte order
+ *
+ * @param u the integer in host byte order
+ * @return the value of integer in network byte order
+ */
+static mw_inline mw_uint32_t
+mw_htonl (mw_uint32_t u)
+{
+#ifdef MW_WORDS_BIGENDIAN
+ return u;
+#else
+ return mw_swap32 (u);
+#endif
+}
+
+/**
+ * @brief convert values to host byte order from network byte order
+ *
+ * @param u the integer in network byte order
+ * @return the value of integer in host byte order
+ */
+static mw_inline mw_uint32_t
+mw_ntohl (mw_uint32_t u)
+{
+#ifdef MW_WORDS_BIGENDIAN
+ return u;
+#else
+ return mw_swap32 (u);
+#endif
+}
MW_END_DECLS
#endif