summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-10-29 01:22:12 -0600
committerLuke T. Shumaker <lukeshu@lukeshu.com>2024-10-29 01:23:49 -0600
commitaffe77a2b068a9de236f0d7703092dbfafacff5e (patch)
tree996ad7216b10c0237657e947bbb7aafc24aef170
parent4d185da010aea1d307c8ff7807ef3d4359083ed0 (diff)
lib9p/9p.h: Tidy up
-rw-r--r--lib9p/9p.c20
-rw-r--r--lib9p/include/lib9p/9p.h47
2 files changed, 41 insertions, 26 deletions
diff --git a/lib9p/9p.c b/lib9p/9p.c
index 9f0bd4c..9ea0c46 100644
--- a/lib9p/9p.c
+++ b/lib9p/9p.c
@@ -13,6 +13,21 @@
#include "internal.h"
+/* ctx ************************************************************************/
+
+void lib9p_ctx_clear_error(struct lib9p_ctx *ctx) {
+ assert(ctx);
+#ifdef CONFIG_9P_ENABLE_9P2000_u
+ ctx->err_num = 0;
+#endif
+ ctx->err_msg[0] = '\0';
+}
+
+bool lib9p_ctx_has_error(struct lib9p_ctx *ctx) {
+ assert(ctx);
+ return ctx->err_msg[0];
+}
+
int lib9p_error(struct lib9p_ctx *ctx, uint32_t linux_errno, char const *msg) {
if (lib9p_ctx_has_error(ctx))
return -1;
@@ -49,12 +64,13 @@ int lib9p_errorf(struct lib9p_ctx *ctx, uint32_t linux_errno, char const *fmt, .
return -1;
}
-
const char *lib9p_msg_type_str(struct lib9p_ctx *ctx, enum lib9p_msg_type typ) {
assert(0 <= typ && typ <= 0xFF);
return _lib9p_versions[ctx->version].msgs[typ].name;
}
+/* main message functions *****************************************************/
+
ssize_t lib9p_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes) {
/* Inspect the first 5 bytes ourselves. */
struct _validate_ctx subctx = {
@@ -115,6 +131,8 @@ bool lib9p_marshal(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body,
return table.marshal(&subctx, body);
}
+/* `struct lib9p_stat` helpers ************************************************/
+
bool lib9p_validate_stat(struct lib9p_ctx *ctx, uint32_t net_size, uint8_t *net_bytes,
uint32_t *ret_net_size, ssize_t *ret_host_size) {
struct _validate_ctx subctx = {
diff --git a/lib9p/include/lib9p/9p.h b/lib9p/include/lib9p/9p.h
index bf304ac..171ad3b 100644
--- a/lib9p/include/lib9p/9p.h
+++ b/lib9p/include/lib9p/9p.h
@@ -24,13 +24,15 @@
#define CONFIG_9P_ENABLE_9P2000
#endif
-/******************************************************************************/
+/* protocol definitions *******************************************************/
#include <lib9p/9p.generated.h> /* *after* config.h */
#define LIB9P_NOTAG ((uint16_t)~0U)
#define LIB9P_NOFID ((uint32_t)~0U)
+/* ctx ************************************************************************/
+
struct lib9p_ctx {
/* negotiated */
enum lib9p_version version;
@@ -43,37 +45,19 @@ struct lib9p_ctx {
char err_msg[CONFIG_9P_MAX_ERR_SIZE];
};
-static void lib9p_ctx_clear_error(struct lib9p_ctx *ctx) {
- assert(ctx);
-#ifdef CONFIG_9P_ENABLE_9P2000_u
- ctx->err_num = 0;
-#endif
- ctx->err_msg[0] = '\0';
-}
+void lib9p_ctx_clear_error(struct lib9p_ctx *ctx);
-static bool lib9p_ctx_has_error(struct lib9p_ctx *ctx) {
- assert(ctx);
- return ctx->err_msg[0];
-}
-
-const char *lib9p_msg_type_str(struct lib9p_ctx *, enum lib9p_msg_type);
-
-/** Assert that a `struct lib9p_stat` object looks valid. */
-static inline void lib9p_assert_stat(struct lib9p_stat stat) {
- assert( ((bool)(stat.file_mode & LIB9P_DM_DIR )) == ((bool)(stat.file_qid.type & LIB9P_QT_DIR )) );
- assert( ((bool)(stat.file_mode & LIB9P_DM_APPEND )) == ((bool)(stat.file_qid.type & LIB9P_QT_APPEND )) );
- assert( ((bool)(stat.file_mode & LIB9P_DM_EXCL )) == ((bool)(stat.file_qid.type & LIB9P_QT_EXCL )) );
- assert( ((bool)(stat.file_mode & _LIB9P_DM_PLAN9_MOUNT)) == ((bool)(stat.file_qid.type & _LIB9P_QT_PLAN9_MOUNT)) );
- assert( ((bool)(stat.file_mode & LIB9P_DM_AUTH )) == ((bool)(stat.file_qid.type & LIB9P_QT_AUTH )) );
- assert( ((bool)(stat.file_mode & LIB9P_DM_TMP )) == ((bool)(stat.file_qid.type & LIB9P_QT_TMP )) );
- assert( (stat.file_size == 0) || !(stat.file_mode & LIB9P_DM_DIR) );
-}
+bool lib9p_ctx_has_error(struct lib9p_ctx *ctx);
/** Write an static error into ctx, return -1. */
int lib9p_error(struct lib9p_ctx *ctx, uint32_t linux_errno, char const *msg);
/** Write a printf-style error into ctx, return -1. */
int lib9p_errorf(struct lib9p_ctx *ctx, uint32_t linux_errno, char const *fmt, ...);
+const char *lib9p_msg_type_str(struct lib9p_ctx *, enum lib9p_msg_type);
+
+/* main message functions *****************************************************/
+
/**
* Validate a message's structure; it's size, string encodings, enums,
* and bitfields.
@@ -138,6 +122,19 @@ void lib9p_unmarshal(struct lib9p_ctx *ctx, uint8_t *net_bytes,
bool lib9p_marshal(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body,
uint8_t *ret_bytes);
+/* `struct lib9p_stat` helpers ************************************************/
+
+/** Assert that a `struct lib9p_stat` object looks valid. */
+static inline void lib9p_assert_stat(struct lib9p_stat stat) {
+ assert( ((bool)(stat.file_mode & LIB9P_DM_DIR )) == ((bool)(stat.file_qid.type & LIB9P_QT_DIR )) );
+ assert( ((bool)(stat.file_mode & LIB9P_DM_APPEND )) == ((bool)(stat.file_qid.type & LIB9P_QT_APPEND )) );
+ assert( ((bool)(stat.file_mode & LIB9P_DM_EXCL )) == ((bool)(stat.file_qid.type & LIB9P_QT_EXCL )) );
+ assert( ((bool)(stat.file_mode & _LIB9P_DM_PLAN9_MOUNT)) == ((bool)(stat.file_qid.type & _LIB9P_QT_PLAN9_MOUNT)) );
+ assert( ((bool)(stat.file_mode & LIB9P_DM_AUTH )) == ((bool)(stat.file_qid.type & LIB9P_QT_AUTH )) );
+ assert( ((bool)(stat.file_mode & LIB9P_DM_TMP )) == ((bool)(stat.file_qid.type & LIB9P_QT_TMP )) );
+ assert( (stat.file_size == 0) || !(stat.file_mode & LIB9P_DM_DIR) );
+}
+
/**
* TODO
*