You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.3 KiB
C

#pragma once
#include <stdint.h>
#include <arpa/inet.h>
#define OWD_MAGIC 0x4f574454
#define OWD_VERSION 1
#define OWD_DECL_DATA_END char _data_end[0]
#define OWD_DATA_LEN(HDR) (offsetof(__typeof__(HDR), _data_end))
enum owd_msg_type {
OWD_MSG_ERR = 0,
OWD_MSG_PING = 1,
OWD_MSG_PONG = 2,
OWD_MSG_SYNC_REQUEST = 3,
OWD_MSG_SYNC_RESPONSE = 4,
};
enum owd_error_code {
OWD_ERR_BAD_HEADER,
OWD_ERR_BAD_TYPE,
};
struct owd_timestamp {
uint32_t ts_upper;
uint32_t ts_lower;
};
struct owdhdr {
uint32_t magic;
uint8_t version;
uint8_t msg_type;
uint16_t length;
uint32_t tid;
};
struct owd_msg_err {
struct owdhdr hdr;
uint8_t err;
OWD_DECL_DATA_END;
};
struct owd_msg_sync_response {
struct owdhdr hdr;
struct owd_timestamp ts;
OWD_DECL_DATA_END;
};
static inline void owd_hdr_populate(
struct owdhdr *hdr,
uint8_t msg_type,
uint16_t length,
uint32_t tid)
{
hdr->magic = htonl(OWD_MAGIC);
hdr->version = OWD_VERSION;
hdr->msg_type = msg_type;
hdr->length = htons(length);
hdr->tid = htonl(tid);
}
static inline int owd_hdr_validate(struct owdhdr *hdr, ssize_t len) {
if (len < sizeof(struct owdhdr))
return 1;
if (hdr->magic != ntohl(OWD_MAGIC))
return 1;
if (hdr->version != OWD_VERSION)
return 1;
return 0;
}