1 /* channelpadding_negotiation.c -- generated by Trunnel v1.5.3.
2 * https://gitweb.torproject.org/trunnel.git
3 * You probably shouldn't edit this file.
6 #include "trunnel-impl.h"
8 #include "channelpadding_negotiation.h"
10 #define TRUNNEL_SET_ERROR_CODE(obj) \
12 (obj)->trunnel_error_code_ = 1; \
15 #if defined(__COVERITY__) || defined(__clang_analyzer__)
16 /* If we're running a static analysis tool, we don't want it to complain
17 * that some of our remaining-bytes checks are dead-code. */
18 int channelpaddingnegotiation_deadcode_dummy__
= 0;
19 #define OR_DEADCODE_DUMMY || channelpaddingnegotiation_deadcode_dummy__
21 #define OR_DEADCODE_DUMMY
24 #define CHECK_REMAINING(nbytes, label) \
26 if (remaining < (nbytes) OR_DEADCODE_DUMMY) { \
31 channelpadding_negotiate_t
*
32 channelpadding_negotiate_new(void)
34 channelpadding_negotiate_t
*val
= trunnel_calloc(1, sizeof(channelpadding_negotiate_t
));
37 val
->command
= CHANNELPADDING_COMMAND_START
;
41 /** Release all storage held inside 'obj', but do not free 'obj'.
44 channelpadding_negotiate_clear(channelpadding_negotiate_t
*obj
)
50 channelpadding_negotiate_free(channelpadding_negotiate_t
*obj
)
54 channelpadding_negotiate_clear(obj
);
55 trunnel_memwipe(obj
, sizeof(channelpadding_negotiate_t
));
60 channelpadding_negotiate_get_version(const channelpadding_negotiate_t
*inp
)
65 channelpadding_negotiate_set_version(channelpadding_negotiate_t
*inp
, uint8_t val
)
68 TRUNNEL_SET_ERROR_CODE(inp
);
75 channelpadding_negotiate_get_command(const channelpadding_negotiate_t
*inp
)
80 channelpadding_negotiate_set_command(channelpadding_negotiate_t
*inp
, uint8_t val
)
82 if (! ((val
== CHANNELPADDING_COMMAND_START
|| val
== CHANNELPADDING_COMMAND_STOP
))) {
83 TRUNNEL_SET_ERROR_CODE(inp
);
90 channelpadding_negotiate_get_ito_low_ms(const channelpadding_negotiate_t
*inp
)
92 return inp
->ito_low_ms
;
95 channelpadding_negotiate_set_ito_low_ms(channelpadding_negotiate_t
*inp
, uint16_t val
)
97 inp
->ito_low_ms
= val
;
101 channelpadding_negotiate_get_ito_high_ms(const channelpadding_negotiate_t
*inp
)
103 return inp
->ito_high_ms
;
106 channelpadding_negotiate_set_ito_high_ms(channelpadding_negotiate_t
*inp
, uint16_t val
)
108 inp
->ito_high_ms
= val
;
112 channelpadding_negotiate_check(const channelpadding_negotiate_t
*obj
)
115 return "Object was NULL";
116 if (obj
->trunnel_error_code_
)
117 return "A set function failed on this object";
118 if (! (obj
->version
== 0))
119 return "Integer out of bounds";
120 if (! (obj
->command
== CHANNELPADDING_COMMAND_START
|| obj
->command
== CHANNELPADDING_COMMAND_STOP
))
121 return "Integer out of bounds";
126 channelpadding_negotiate_encoded_len(const channelpadding_negotiate_t
*obj
)
130 if (NULL
!= channelpadding_negotiate_check(obj
))
134 /* Length of u8 version IN [0] */
137 /* Length of u8 command IN [CHANNELPADDING_COMMAND_START, CHANNELPADDING_COMMAND_STOP] */
140 /* Length of u16 ito_low_ms */
143 /* Length of u16 ito_high_ms */
148 channelpadding_negotiate_clear_errors(channelpadding_negotiate_t
*obj
)
150 int r
= obj
->trunnel_error_code_
;
151 obj
->trunnel_error_code_
= 0;
155 channelpadding_negotiate_encode(uint8_t *output
, const size_t avail
, const channelpadding_negotiate_t
*obj
)
159 uint8_t *ptr
= output
;
161 #ifdef TRUNNEL_CHECK_ENCODED_LEN
162 const ssize_t encoded_len
= channelpadding_negotiate_encoded_len(obj
);
165 if (NULL
!= (msg
= channelpadding_negotiate_check(obj
)))
168 #ifdef TRUNNEL_CHECK_ENCODED_LEN
169 trunnel_assert(encoded_len
>= 0);
172 /* Encode u8 version IN [0] */
173 trunnel_assert(written
<= avail
);
174 if (avail
- written
< 1)
176 trunnel_set_uint8(ptr
, (obj
->version
));
177 written
+= 1; ptr
+= 1;
179 /* Encode u8 command IN [CHANNELPADDING_COMMAND_START, CHANNELPADDING_COMMAND_STOP] */
180 trunnel_assert(written
<= avail
);
181 if (avail
- written
< 1)
183 trunnel_set_uint8(ptr
, (obj
->command
));
184 written
+= 1; ptr
+= 1;
186 /* Encode u16 ito_low_ms */
187 trunnel_assert(written
<= avail
);
188 if (avail
- written
< 2)
190 trunnel_set_uint16(ptr
, trunnel_htons(obj
->ito_low_ms
));
191 written
+= 2; ptr
+= 2;
193 /* Encode u16 ito_high_ms */
194 trunnel_assert(written
<= avail
);
195 if (avail
- written
< 2)
197 trunnel_set_uint16(ptr
, trunnel_htons(obj
->ito_high_ms
));
198 written
+= 2; ptr
+= 2;
201 trunnel_assert(ptr
== output
+ written
);
202 #ifdef TRUNNEL_CHECK_ENCODED_LEN
204 trunnel_assert(encoded_len
>= 0);
205 trunnel_assert((size_t)encoded_len
== written
);
220 trunnel_assert(result
< 0);
224 /** As channelpadding_negotiate_parse(), but do not allocate the
228 channelpadding_negotiate_parse_into(channelpadding_negotiate_t
*obj
, const uint8_t *input
, const size_t len_in
)
230 const uint8_t *ptr
= input
;
231 size_t remaining
= len_in
;
235 /* Parse u8 version IN [0] */
236 CHECK_REMAINING(1, truncated
);
237 obj
->version
= (trunnel_get_uint8(ptr
));
238 remaining
-= 1; ptr
+= 1;
239 if (! (obj
->version
== 0))
242 /* Parse u8 command IN [CHANNELPADDING_COMMAND_START, CHANNELPADDING_COMMAND_STOP] */
243 CHECK_REMAINING(1, truncated
);
244 obj
->command
= (trunnel_get_uint8(ptr
));
245 remaining
-= 1; ptr
+= 1;
246 if (! (obj
->command
== CHANNELPADDING_COMMAND_START
|| obj
->command
== CHANNELPADDING_COMMAND_STOP
))
249 /* Parse u16 ito_low_ms */
250 CHECK_REMAINING(2, truncated
);
251 obj
->ito_low_ms
= trunnel_ntohs(trunnel_get_uint16(ptr
));
252 remaining
-= 2; ptr
+= 2;
254 /* Parse u16 ito_high_ms */
255 CHECK_REMAINING(2, truncated
);
256 obj
->ito_high_ms
= trunnel_ntohs(trunnel_get_uint16(ptr
));
257 remaining
-= 2; ptr
+= 2;
258 trunnel_assert(ptr
+ remaining
== input
+ len_in
);
259 return len_in
- remaining
;
269 channelpadding_negotiate_parse(channelpadding_negotiate_t
**output
, const uint8_t *input
, const size_t len_in
)
272 *output
= channelpadding_negotiate_new();
275 result
= channelpadding_negotiate_parse_into(*output
, input
, len_in
);
277 channelpadding_negotiate_free(*output
);