| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "sd-forward.h" |
| 5 |
|
| 6 |
#define TLV_TAG_PAD UINT32_C(0) |
| 7 |
#define TLV_TAG_END UINT32_C(0xFF) |
| 8 |
|
| 9 |
typedef enum TLVFlag { |
| 10 |
TLV_TAG_U8 = 1 << 0, |
| 11 |
TLV_TAG_U16 = 1 << 1, |
| 12 |
TLV_TAG_U32 = 1 << 2, |
| 13 |
_TLV_TAG_MASK = TLV_TAG_U8 | TLV_TAG_U16 | TLV_TAG_U32, |
| 14 |
TLV_LENGTH_U8 = 1 << 3, |
| 15 |
TLV_LENGTH_U16 = 1 << 4, |
| 16 |
TLV_LENGTH_U32 = 1 << 5, |
| 17 |
_TLV_LENGTH_MASK = TLV_LENGTH_U8 | TLV_LENGTH_U16 | TLV_LENGTH_U32, |
| 18 |
TLV_PAD = 1 << 6, /* If set, tag == 0 is a pad, and does not have the length field. */ |
| 19 |
TLV_END = 1 << 7, /* If set, tag == 0xFF is a sign of the end of the sequence. */ |
| 20 |
TLV_APPEND_END = 1 << 8, /* If set, append the END tag at the end of the sequence on build. */ |
| 21 |
TLV_MERGE = 1 << 9, /* If set, tlv_get_alloc() merges them, and tlv_append() split long data. */ |
| 22 |
TLV_TEMPORARY = 1 << 10, /* If set, tlv_append() and tlv_parse() do not copy the data. */ |
| 23 |
|
| 24 |
/* DHCPv4 options. */ |
| 25 |
TLV_DHCP4 = TLV_TAG_U8 | TLV_LENGTH_U8 | TLV_PAD | TLV_END | TLV_APPEND_END | TLV_MERGE, |
| 26 |
/* Used for DHCPv4 sub-options, e.g. |
| 27 |
* DHCPv4 Vendor Specific Information sub-option (43), |
| 28 |
* DHCPv4 Relay Agent Information sub-option (82), or |
| 29 |
* DHCPv4 Vendor-Identifying Vendor Specific Information sub-sub-option (125). |
| 30 |
* Note that the PAD is not mentioned in RFC, but some implementations use it, hence let's gracefully |
| 31 |
* handle it. Also note that the END tag is prohibited in most options, but we also gracefully handle |
| 32 |
* it on parse, but of course do not append it on build. */ |
| 33 |
TLV_DHCP4_SUBOPTION |
| 34 |
= TLV_TAG_U8 | TLV_LENGTH_U8 | TLV_PAD | TLV_END, |
| 35 |
/* DHCPv4 Vendor-Identifying Vendor Class sub-option (124) and |
| 36 |
* DHCPv4 Vendor-Identifying Vendor Specific Information sub-option (125). |
| 37 |
* The tag is called 'enterprise-number', and in uint32. */ |
| 38 |
TLV_DHCP4_VENDOR_IDENTIFYING_OPTION |
| 39 |
= TLV_TAG_U32 | TLV_LENGTH_U8 | TLV_MERGE, |
| 40 |
} TLVFlag; |
| 41 |
|
| 42 |
typedef struct TLV { |
| 43 |
unsigned n_ref; |
| 44 |
TLVFlag flags; |
| 45 |
unsigned n_entries; |
| 46 |
Hashmap *entries; |
| 47 |
} TLV; |
| 48 |
|
| 49 |
#define TLV_INIT(f) \ |
| 50 |
(TLV) { \ |
| 51 |
.n_ref = 1, \ |
| 52 |
.flags = tlv_flags_verify(f), \ |
| 53 |
} |
| 54 |
|
| 55 |
TLVFlag tlv_flags_verify(TLVFlag flags); |
| 56 |
|
| 57 |
void tlv_done(TLV *tlv); |
| 58 |
TLV* tlv_ref(TLV *p); |
| 59 |
TLV* tlv_unref(TLV *p); |
| 60 |
DEFINE_TRIVIAL_CLEANUP_FUNC(TLV*, tlv_unref); |
| 61 |
TLV* tlv_new(TLVFlag flags); |
| 62 |
|
| 63 |
bool tlv_isempty(const TLV *tlv); |
| 64 |
|
| 65 |
struct iovec_wrapper* tlv_get_all(const TLV *tlv, uint32_t tag); |
| 66 |
static inline bool tlv_contains(const TLV *tlv, uint32_t tag) { |
| 67 |
return tlv_get_all(tlv, tag); |
| 68 |
} |
| 69 |
int tlv_get_full(const TLV *tlv, uint32_t tag, size_t length, struct iovec *ret); |
| 70 |
static inline int tlv_get(const TLV *tlv, uint32_t tag, struct iovec *ret) { |
| 71 |
return tlv_get_full(tlv, tag, SIZE_MAX, ret); |
| 72 |
} |
| 73 |
int tlv_get_alloc(const TLV *tlv, uint32_t tag, struct iovec *ret); |
| 74 |
|
| 75 |
void tlv_remove(TLV *tlv, uint32_t tag); |
| 76 |
int tlv_append(TLV *tlv, uint32_t tag, size_t length, const void *data); |
| 77 |
int tlv_append_iov(TLV *tlv, uint32_t tag, const struct iovec *iov); |
| 78 |
int tlv_append_tlv(TLV *tlv, const TLV *source); |
| 79 |
|
| 80 |
int tlv_parse(TLV *tlv, const struct iovec *iov); |
| 81 |
size_t tlv_size(const TLV *tlv); |
| 82 |
int tlv_build(const TLV *tlv, struct iovec *ret); |
| 83 |
|
| 84 |
int tlv_build_json(const TLV *tlv, sd_json_variant **ret); |
| 85 |
int tlv_parse_json(TLV *tlv, sd_json_variant *v); |