/* * Usurpation – clinet-server protocol implementation. * * Copyright (C) 2019 Ramūnas Mažeikis * Copyright (C) 2019 Gediminas Jakutis * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; version 2.1 * of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ #ifndef USURPATION_PROTOCOL_H_INCLUDED #define USURPATION_PROTOCOL_H_INCLUDED #include #include #include #include #define TLV_SZ_MAX (MTU - 64) #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ /** * Message sequence number since beggining of sesssion. * * Mainly used for identifying lost messages. */ typedef unsigned int msg_idx_t; enum tlv_type { /** * Explicitly states that this tlv is no longer valid for reading. */ INVALID = 0, /** * NULL-terminated string. To be put in a queue to display on the * screen. */ TEXT, /** Fixed point. 1 decimal digit of precision. */ FPI1, /** Literally time_t */ TIMESTAMP, /** Represents a request for lost message. Data is unsigned integer * that uniquely identifies the message. */ REQUEST, /** * Response to request. Begins with unsigned integer that represents * which message is being repeated and the actual null-terminated * message after that. */ REPLY, /** * UUID that represents a particular device. */ UUID, /** * Says "I'm not dead yet.". */ HEARTBEAT, }; struct tlv_header { int32_t type; uint32_t size; }; struct tlv { struct tlv_header head; char *data; }; /* for tlv to "raw" conversion */ struct tlv_packet { size_t size; ptrdiff_t cursor; char *data; }; /* used as a termninator */ #define tlv_none ((struct tlv) {{INVALID, 0}, NULL}) /** * Fills tlv structure to represent the next tlv in the packet. * If saveptr is non-NULL, saves the offset to the next field in saveptr. * Returns NONEWDATA if once all tlvs were read. * * tlv data fields obtained through this function MUST NOT be given * to free(), as they point to the offsets in the "in" buffer. */ int tlv_get(char *in, struct tlv *tlv, char **saveptr); /* host<->network endianess conversion for the header */ struct tlv_header ntoh_tlvh(const struct tlv_header * const in); struct tlv_header hton_tlvh(const struct tlv_header * const in); /** * Forms (packs up) a raw data packet from the given tlv by appending it to the * packet. Returns E_TLV_OVERFLOW if pushing data would cause the final size * (including the terminator tlv) to be greater than TLV_SZ_MAX_RAW. In case of * such error the data is left untouched. */ int tlv_pack(struct tlv_packet *pack, struct tlv *tlv); /* finalized the packet by placing tlv_none at the end */ int tlv_packet_finalize(struct tlv_packet *pack); struct tlv *tlv_init(struct tlv *in, enum tlv_type type); void tlv_destroy(struct tlv *in); struct tlv_packet *tlv_packet_init(struct tlv_packet *in); void tlv_packet_destroy(struct tlv_packet *in); /* resets the packet to form a new one, without deallocating buffers */ struct tlv_packet *tlv_packet_reset(struct tlv_packet *in); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* USURPATION_PROTOCOL_H_INCLUDED */