2 * This file is part of the PCEPlib, a PCEP protocol library.
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * Author : Brady Johnson <brady@voltanet.io>
25 * Definitions for encoding and decoding PCEP messages, objects, and TLVs.
28 #ifndef PCEP_ENCODING_H
29 #define PCEP_ENCODING_H
33 #include "pcep_msg_messages.h"
34 #include "pcep_msg_objects.h"
35 #include "pcep_msg_tlvs.h"
41 struct pcep_versioning
{
42 bool draft_ietf_pce_segment_routing_07
; /* If false, use draft16 */
43 /* As more draft versions are incorporated, add appropriate attributes
47 #define MESSAGE_HEADER_LENGTH 4
48 #define PCEP_MESSAGE_LENGTH 65535
49 #define OBJECT_HEADER_LENGTH 4
50 #define OBJECT_RO_SUBOBJ_HEADER_LENGTH 2
51 #define TLV_HEADER_LENGTH 4
52 #define LENGTH_1WORD sizeof(uint32_t)
53 #define LENGTH_2WORDS sizeof(uint32_t) * 2
54 #define LENGTH_3WORDS sizeof(uint32_t) * 3
55 #define LENGTH_4WORDS sizeof(uint32_t) * 4
56 #define LENGTH_5WORDS sizeof(uint32_t) * 5
57 #define LENGTH_6WORDS sizeof(uint32_t) * 6
58 #define LENGTH_7WORDS sizeof(uint32_t) * 7
59 #define LENGTH_8WORDS sizeof(uint32_t) * 8
60 #define LENGTH_9WORDS sizeof(uint32_t) * 9
61 #define LENGTH_10WORDS sizeof(uint32_t) * 10
62 #define LENGTH_11WORDS sizeof(uint32_t) * 11
63 #define LENGTH_12WORDS sizeof(uint32_t) * 12
64 #define LENGTH_13WORDS sizeof(uint32_t) * 13
66 /* When iterating sub-objects or TLVs, limit to 10 in case corrupt data is
68 #define MAX_ITERATIONS 10
70 struct pcep_versioning
*create_default_pcep_versioning(void);
71 void destroy_pcep_versioning(struct pcep_versioning
*versioning
);
74 * Message encoding / decoding functions
77 /* Called before sending messages to encode the message to a byte buffer in
78 * Network byte order. This function will also encode all the objects and their
79 * TLVs in the message. The result will be stored in the encoded_message field
80 * in the pcep_message. Implemented in pcep-messages-encoding.c */
81 void pcep_encode_message(struct pcep_message
*message
,
82 struct pcep_versioning
*versioning
);
84 /* Decode the message header and return the message length.
85 * Returns < 0 for invalid message headers. */
86 int32_t pcep_decode_validate_msg_header(const uint8_t *msg_buf
);
88 /* Decode the entire message */
89 struct pcep_message
*pcep_decode_message(const uint8_t *message_buffer
);
93 * Object encoding / decoding functions
96 /* Implemented in pcep-objects-encoding.c
97 * Encode the object in struct pcep_object_header* into the uint8_t *buf,
98 * and return the encoded object_length. */
99 uint16_t pcep_encode_object(struct pcep_object_header
*object_hdr
,
100 struct pcep_versioning
*versioning
, uint8_t *buf
);
102 /* Implemented in pcep-objects-encoding.c
103 * Decode the object, including the TLVs (if any) and return the object.
104 * Returns object on success, NULL otherwise. */
105 struct pcep_object_header
*pcep_decode_object(const uint8_t *msg_buf
);
107 /* Internal util functions implemented in pcep-objects-encoding.c */
108 void encode_ipv6(struct in6_addr
*src_ipv6
, uint32_t *dst
);
109 void decode_ipv6(const uint32_t *src
, struct in6_addr
*dst_ipv6
);
110 uint16_t normalize_pcep_tlv_length(uint16_t length
);
111 bool pcep_object_has_tlvs(struct pcep_object_header
*object_hdr
);
112 uint16_t pcep_object_get_length_by_hdr(struct pcep_object_header
*object_hdr
);
113 uint16_t pcep_object_get_length(enum pcep_object_classes object_class
,
114 enum pcep_object_types object_type
);
118 * TLV encoding / decoding functions
121 /* Implemented in pcep-tlv-encoding.c
122 * Encode the tlv in struct pcep_tlv_header* into the uint8_t *buf,
123 * and return the encoded tlv_length. */
124 uint16_t pcep_encode_tlv(struct pcep_object_tlv_header
*tlv_hdr
,
125 struct pcep_versioning
*versioning
, uint8_t *buf
);
127 /* Decode the TLV in tlv_buf and return a pointer to the object */
128 struct pcep_object_tlv_header
*pcep_decode_tlv(const uint8_t *tlv_buf
);
132 * utils mainly for testing purposes
134 bool validate_message_objects(struct pcep_message
*msg
);