Some further small changes are made to allow this header to be used
from arbitrary ISO C compilers.
It can now be used from other projects.
---
common/protocol/Makefile.am | 2 ++
common/protocol/nbd-protocol.h | 38 ++++++++++++++++++++--------------
2 files changed, 24 insertions(+), 16 deletions(-)
diff --git a/common/protocol/Makefile.am b/common/protocol/Makefile.am
index 74c288a..b115c58 100644
--- a/common/protocol/Makefile.am
+++ b/common/protocol/Makefile.am
@@ -37,6 +37,8 @@ EXTRA_DIST = \
generate-protostrings.sh \
$(NULL)
+include_HEADERS = nbd-protocol.h
+
noinst_LTLIBRARIES = libprotocol.la
libprotocol_la_SOURCES = \
diff --git a/common/protocol/nbd-protocol.h b/common/protocol/nbd-protocol.h
index 724ffb6..7df411a 100644
--- a/common/protocol/nbd-protocol.h
+++ b/common/protocol/nbd-protocol.h
@@ -36,10 +36,16 @@
#include <stdint.h>
/* Note that all NBD fields are sent on the wire in network byte
- * order, so we must use beXXtoh or htobeXX when reading or writing
+ * order, so you must use beXXtoh or htobeXX when reading or writing
* these structures.
*/
+#if defined(__GNUC__) || defined(__clang__)
+#define NBD_ATTRIBUTE_PACKED __attribute__((__packed__))
+#else
+#define NBD_ATTRIBUTE_PACKED
+#endif
+
#define NBD_MAX_STRING 4096 /* Maximum length of a string field */
/* Old-style handshake. */
@@ -50,7 +56,7 @@ struct nbd_old_handshake {
uint16_t gflags; /* global flags */
uint16_t eflags; /* per-export flags */
char zeroes[124]; /* must be sent as zero bytes */
-} __attribute__((packed));
+} NBD_ATTRIBUTE_PACKED;
#define NBD_OLD_VERSION UINT64_C(0x420281861253)
@@ -59,7 +65,7 @@ struct nbd_new_handshake {
char nbdmagic[8]; /* "NBDMAGIC" */
uint64_t version; /* NBD_NEW_VERSION */
uint16_t gflags; /* global flags */
-} __attribute__((packed));
+} NBD_ATTRIBUTE_PACKED;
#define NBD_NEW_VERSION UINT64_C(0x49484156454F5054)
@@ -69,14 +75,14 @@ struct nbd_new_option {
uint32_t option; /* NBD_OPT_* */
uint32_t optlen; /* option data length */
/* option data follows */
-} __attribute__((packed));
+} NBD_ATTRIBUTE_PACKED;
/* Newstyle handshake OPT_EXPORT_NAME reply message. */
struct nbd_export_name_option_reply {
uint64_t exportsize; /* size of export */
uint16_t eflags; /* per-export flags */
char zeroes[124]; /* optional zeroes */
-} __attribute__((packed));;
+} NBD_ATTRIBUTE_PACKED;;
/* Fixed newstyle handshake reply message. */
struct nbd_fixed_new_option_reply {
@@ -84,7 +90,7 @@ struct nbd_fixed_new_option_reply {
uint32_t option; /* option we are replying to */
uint32_t reply; /* NBD_REP_* */
uint32_t replylen;
-} __attribute__((packed));
+} NBD_ATTRIBUTE_PACKED;
#define NBD_REP_MAGIC UINT64_C(0x3e889045565a9)
@@ -140,19 +146,19 @@ struct nbd_fixed_new_option_reply_info_export {
uint16_t info; /* NBD_INFO_EXPORT */
uint64_t exportsize; /* size of export */
uint16_t eflags; /* per-export flags */
-} __attribute__((packed));
+} NBD_ATTRIBUTE_PACKED;
/* NBD_REP_META_CONTEXT reply (follows fixed_new_option_reply). */
struct nbd_fixed_new_option_reply_meta_context {
uint32_t context_id; /* metadata context ID */
/* followed by a string */
-} __attribute__((packed));
+} NBD_ATTRIBUTE_PACKED;
/* NBD_REPLY_TYPE_BLOCK_STATUS block descriptor. */
struct nbd_block_descriptor {
uint32_t length; /* length of block */
uint32_t status_flags; /* block type (hole etc) */
-} __attribute__((packed));
+} NBD_ATTRIBUTE_PACKED;
/* New-style handshake server reply when using NBD_OPT_EXPORT_NAME.
* Modern clients use NBD_OPT_GO instead of this.
@@ -161,7 +167,7 @@ struct nbd_new_handshake_finish {
uint64_t exportsize;
uint16_t eflags; /* per-export flags */
char zeroes[124]; /* must be sent as zero bytes */
-} __attribute__((packed));
+} NBD_ATTRIBUTE_PACKED;
/* Request (client -> server). */
struct nbd_request {
@@ -171,14 +177,14 @@ struct nbd_request {
uint64_t handle; /* Opaque handle. */
uint64_t offset; /* Request offset. */
uint32_t count; /* Request length. */
-} __attribute__((packed));
+} NBD_ATTRIBUTE_PACKED;
/* Simple reply (server -> client). */
struct nbd_simple_reply {
uint32_t magic; /* NBD_SIMPLE_REPLY_MAGIC. */
uint32_t error; /* NBD_SUCCESS or one of NBD_E*. */
uint64_t handle; /* Opaque handle. */
-} __attribute__((packed));
+} NBD_ATTRIBUTE_PACKED;
/* Structured reply (server -> client). */
struct nbd_structured_reply {
@@ -187,23 +193,23 @@ struct nbd_structured_reply {
uint16_t type; /* NBD_REPLY_TYPE_* */
uint64_t handle; /* Opaque handle. */
uint32_t length; /* Length of payload which follows. */
-} __attribute__((packed));
+} NBD_ATTRIBUTE_PACKED;
struct nbd_structured_reply_offset_data {
uint64_t offset; /* offset */
/* Followed by data. */
-} __attribute__((packed));
+} NBD_ATTRIBUTE_PACKED;
struct nbd_structured_reply_offset_hole {
uint64_t offset;
uint32_t length; /* Length of hole. */
-} __attribute__((packed));
+} NBD_ATTRIBUTE_PACKED;
struct nbd_structured_reply_error {
uint32_t error; /* NBD_E* error number */
uint16_t len; /* Length of human readable error. */
/* Followed by human readable error string, and possibly more structure. */
-} __attribute__((packed));
+} NBD_ATTRIBUTE_PACKED;
#define NBD_REQUEST_MAGIC 0x25609513
#define NBD_SIMPLE_REPLY_MAGIC 0x67446698
--
2.23.0