diff options
author | Valery Ivanov <ivalery111@gmail.com> | 2021-05-08 13:42:38 +0300 |
---|---|---|
committer | Valery Ivanov <ivalery111@gmail.com> | 2021-05-08 13:42:38 +0300 |
commit | 2b052ff08a8d20bdc77462b3b7d383d376345090 (patch) | |
tree | 606151f91ef68691fe82006826b5dd8b2e83b8c4 /src | |
parent | 41e9ea7aaf0ff6cb40bb44ff831c49efc0b9991e (diff) | |
download | libnet-2b052ff08a8d20bdc77462b3b7d383d376345090.tar.gz |
feat(functions): add libnet_build_lldp_end function
Diffstat (limited to 'src')
-rw-r--r-- | src/libnet_build_lldp.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/libnet_build_lldp.c b/src/libnet_build_lldp.c index b0ee5fa..c49fb8f 100644 --- a/src/libnet_build_lldp.c +++ b/src/libnet_build_lldp.c @@ -211,6 +211,52 @@ bad: return (-1); } + +LIBNET_API +libnet_ptag_t libnet_build_lldp_end(libnet_t *l, libnet_ptag_t ptag) +{ + assert(LIBNET_LLDP_TLV_HDR_SIZE == 0x02 && "TLV header size must be a 2 bytes"); + assert(LIBNET_LLDP_SUBTYPE_SIZE == 0x01 && "Subtype field size must be 1 byte"); + + uint32_t n, h; + libnet_pblock_t *p; + struct libnet_lldp_hdr hdr; + memset(&hdr, 0, sizeof(struct libnet_lldp_hdr)); + + if (l == NULL) + { + return (-1); + } + + /* size of memory block */ + n = h = LIBNET_LLDP_TLV_HDR_SIZE; /* TLV Header size */ + + LIBNET_LLDP_TLV_SET_TYPE(hdr.tlv_info, LIBNET_LLDP_END_LLDPDU); + LIBNET_LLDP_TLV_SET_LEN(hdr.tlv_info, 0); + + /* + * Find the existing protocol block if a ptag is specified, or create + * a new one. + */ + p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_LLDP_TTL_H); + if (p == NULL) + { + return (-1); + } + + const uint16_t type_and_len = htons(hdr.tlv_info); + if (libnet_pblock_append(l, p, &type_and_len, sizeof(type_and_len)) == -1) + { + goto bad; + } + + return (ptag ? ptag + : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_LLDP_TTL_H)); +bad: + libnet_pblock_delete(l, p); + return (-1); +} + /** * Local Variables: * indent-tabs-mode: nil |