1 /* circpad_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 "circpad_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 circpadnegotiation_deadcode_dummy__
= 0;
19 #define OR_DEADCODE_DUMMY || circpadnegotiation_deadcode_dummy__
21 #define OR_DEADCODE_DUMMY
24 #define CHECK_REMAINING(nbytes, label) \
26 if (remaining < (nbytes) OR_DEADCODE_DUMMY) { \
32 circpad_negotiate_new(void)
34 circpad_negotiate_t
*val
= trunnel_calloc(1, sizeof(circpad_negotiate_t
));
37 val
->command
= CIRCPAD_COMMAND_START
;
41 /** Release all storage held inside 'obj', but do not free 'obj'.
44 circpad_negotiate_clear(circpad_negotiate_t
*obj
)
50 circpad_negotiate_free(circpad_negotiate_t
*obj
)
54 circpad_negotiate_clear(obj
);
55 trunnel_memwipe(obj
, sizeof(circpad_negotiate_t
));
60 circpad_negotiate_get_version(const circpad_negotiate_t
*inp
)
65 circpad_negotiate_set_version(circpad_negotiate_t
*inp
, uint8_t val
)
68 TRUNNEL_SET_ERROR_CODE(inp
);
75 circpad_negotiate_get_command(const circpad_negotiate_t
*inp
)
80 circpad_negotiate_set_command(circpad_negotiate_t
*inp
, uint8_t val
)
82 if (! ((val
== CIRCPAD_COMMAND_START
|| val
== CIRCPAD_COMMAND_STOP
))) {
83 TRUNNEL_SET_ERROR_CODE(inp
);
90 circpad_negotiate_get_machine_type(const circpad_negotiate_t
*inp
)
92 return inp
->machine_type
;
95 circpad_negotiate_set_machine_type(circpad_negotiate_t
*inp
, uint8_t val
)
97 inp
->machine_type
= val
;
101 circpad_negotiate_get_echo_request(const circpad_negotiate_t
*inp
)
103 return inp
->echo_request
;
106 circpad_negotiate_set_echo_request(circpad_negotiate_t
*inp
, uint8_t val
)
108 if (! ((val
== 0 || val
== 1))) {
109 TRUNNEL_SET_ERROR_CODE(inp
);
112 inp
->echo_request
= val
;
116 circpad_negotiate_get_machine_ctr(const circpad_negotiate_t
*inp
)
118 return inp
->machine_ctr
;
121 circpad_negotiate_set_machine_ctr(circpad_negotiate_t
*inp
, uint32_t val
)
123 inp
->machine_ctr
= val
;
127 circpad_negotiate_check(const circpad_negotiate_t
*obj
)
130 return "Object was NULL";
131 if (obj
->trunnel_error_code_
)
132 return "A set function failed on this object";
133 if (! (obj
->version
== 0))
134 return "Integer out of bounds";
135 if (! (obj
->command
== CIRCPAD_COMMAND_START
|| obj
->command
== CIRCPAD_COMMAND_STOP
))
136 return "Integer out of bounds";
137 if (! (obj
->echo_request
== 0 || obj
->echo_request
== 1))
138 return "Integer out of bounds";
143 circpad_negotiate_encoded_len(const circpad_negotiate_t
*obj
)
147 if (NULL
!= circpad_negotiate_check(obj
))
151 /* Length of u8 version IN [0] */
154 /* Length of u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
157 /* Length of u8 machine_type */
160 /* Length of u8 echo_request IN [0, 1] */
163 /* Length of u32 machine_ctr */
168 circpad_negotiate_clear_errors(circpad_negotiate_t
*obj
)
170 int r
= obj
->trunnel_error_code_
;
171 obj
->trunnel_error_code_
= 0;
175 circpad_negotiate_encode(uint8_t *output
, const size_t avail
, const circpad_negotiate_t
*obj
)
179 uint8_t *ptr
= output
;
181 #ifdef TRUNNEL_CHECK_ENCODED_LEN
182 const ssize_t encoded_len
= circpad_negotiate_encoded_len(obj
);
185 if (NULL
!= (msg
= circpad_negotiate_check(obj
)))
188 #ifdef TRUNNEL_CHECK_ENCODED_LEN
189 trunnel_assert(encoded_len
>= 0);
192 /* Encode u8 version IN [0] */
193 trunnel_assert(written
<= avail
);
194 if (avail
- written
< 1)
196 trunnel_set_uint8(ptr
, (obj
->version
));
197 written
+= 1; ptr
+= 1;
199 /* Encode u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
200 trunnel_assert(written
<= avail
);
201 if (avail
- written
< 1)
203 trunnel_set_uint8(ptr
, (obj
->command
));
204 written
+= 1; ptr
+= 1;
206 /* Encode u8 machine_type */
207 trunnel_assert(written
<= avail
);
208 if (avail
- written
< 1)
210 trunnel_set_uint8(ptr
, (obj
->machine_type
));
211 written
+= 1; ptr
+= 1;
213 /* Encode u8 echo_request IN [0, 1] */
214 trunnel_assert(written
<= avail
);
215 if (avail
- written
< 1)
217 trunnel_set_uint8(ptr
, (obj
->echo_request
));
218 written
+= 1; ptr
+= 1;
220 /* Encode u32 machine_ctr */
221 trunnel_assert(written
<= avail
);
222 if (avail
- written
< 4)
224 trunnel_set_uint32(ptr
, trunnel_htonl(obj
->machine_ctr
));
225 written
+= 4; ptr
+= 4;
228 trunnel_assert(ptr
== output
+ written
);
229 #ifdef TRUNNEL_CHECK_ENCODED_LEN
231 trunnel_assert(encoded_len
>= 0);
232 trunnel_assert((size_t)encoded_len
== written
);
247 trunnel_assert(result
< 0);
251 /** As circpad_negotiate_parse(), but do not allocate the output
255 circpad_negotiate_parse_into(circpad_negotiate_t
*obj
, const uint8_t *input
, const size_t len_in
)
257 const uint8_t *ptr
= input
;
258 size_t remaining
= len_in
;
262 /* Parse u8 version IN [0] */
263 CHECK_REMAINING(1, truncated
);
264 obj
->version
= (trunnel_get_uint8(ptr
));
265 remaining
-= 1; ptr
+= 1;
266 if (! (obj
->version
== 0))
269 /* Parse u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
270 CHECK_REMAINING(1, truncated
);
271 obj
->command
= (trunnel_get_uint8(ptr
));
272 remaining
-= 1; ptr
+= 1;
273 if (! (obj
->command
== CIRCPAD_COMMAND_START
|| obj
->command
== CIRCPAD_COMMAND_STOP
))
276 /* Parse u8 machine_type */
277 CHECK_REMAINING(1, truncated
);
278 obj
->machine_type
= (trunnel_get_uint8(ptr
));
279 remaining
-= 1; ptr
+= 1;
281 /* Parse u8 echo_request IN [0, 1] */
282 CHECK_REMAINING(1, truncated
);
283 obj
->echo_request
= (trunnel_get_uint8(ptr
));
284 remaining
-= 1; ptr
+= 1;
285 if (! (obj
->echo_request
== 0 || obj
->echo_request
== 1))
288 /* Parse u32 machine_ctr */
289 CHECK_REMAINING(4, truncated
);
290 obj
->machine_ctr
= trunnel_ntohl(trunnel_get_uint32(ptr
));
291 remaining
-= 4; ptr
+= 4;
292 trunnel_assert(ptr
+ remaining
== input
+ len_in
);
293 return len_in
- remaining
;
303 circpad_negotiate_parse(circpad_negotiate_t
**output
, const uint8_t *input
, const size_t len_in
)
306 *output
= circpad_negotiate_new();
309 result
= circpad_negotiate_parse_into(*output
, input
, len_in
);
311 circpad_negotiate_free(*output
);
316 circpad_negotiated_t
*
317 circpad_negotiated_new(void)
319 circpad_negotiated_t
*val
= trunnel_calloc(1, sizeof(circpad_negotiated_t
));
322 val
->command
= CIRCPAD_COMMAND_START
;
323 val
->response
= CIRCPAD_RESPONSE_ERR
;
327 /** Release all storage held inside 'obj', but do not free 'obj'.
330 circpad_negotiated_clear(circpad_negotiated_t
*obj
)
336 circpad_negotiated_free(circpad_negotiated_t
*obj
)
340 circpad_negotiated_clear(obj
);
341 trunnel_memwipe(obj
, sizeof(circpad_negotiated_t
));
346 circpad_negotiated_get_version(const circpad_negotiated_t
*inp
)
351 circpad_negotiated_set_version(circpad_negotiated_t
*inp
, uint8_t val
)
353 if (! ((val
== 0))) {
354 TRUNNEL_SET_ERROR_CODE(inp
);
361 circpad_negotiated_get_command(const circpad_negotiated_t
*inp
)
366 circpad_negotiated_set_command(circpad_negotiated_t
*inp
, uint8_t val
)
368 if (! ((val
== CIRCPAD_COMMAND_START
|| val
== CIRCPAD_COMMAND_STOP
))) {
369 TRUNNEL_SET_ERROR_CODE(inp
);
376 circpad_negotiated_get_response(const circpad_negotiated_t
*inp
)
378 return inp
->response
;
381 circpad_negotiated_set_response(circpad_negotiated_t
*inp
, uint8_t val
)
383 if (! ((val
== CIRCPAD_RESPONSE_ERR
|| val
== CIRCPAD_RESPONSE_OK
))) {
384 TRUNNEL_SET_ERROR_CODE(inp
);
391 circpad_negotiated_get_machine_type(const circpad_negotiated_t
*inp
)
393 return inp
->machine_type
;
396 circpad_negotiated_set_machine_type(circpad_negotiated_t
*inp
, uint8_t val
)
398 inp
->machine_type
= val
;
402 circpad_negotiated_get_machine_ctr(const circpad_negotiated_t
*inp
)
404 return inp
->machine_ctr
;
407 circpad_negotiated_set_machine_ctr(circpad_negotiated_t
*inp
, uint32_t val
)
409 inp
->machine_ctr
= val
;
413 circpad_negotiated_check(const circpad_negotiated_t
*obj
)
416 return "Object was NULL";
417 if (obj
->trunnel_error_code_
)
418 return "A set function failed on this object";
419 if (! (obj
->version
== 0))
420 return "Integer out of bounds";
421 if (! (obj
->command
== CIRCPAD_COMMAND_START
|| obj
->command
== CIRCPAD_COMMAND_STOP
))
422 return "Integer out of bounds";
423 if (! (obj
->response
== CIRCPAD_RESPONSE_ERR
|| obj
->response
== CIRCPAD_RESPONSE_OK
))
424 return "Integer out of bounds";
429 circpad_negotiated_encoded_len(const circpad_negotiated_t
*obj
)
433 if (NULL
!= circpad_negotiated_check(obj
))
437 /* Length of u8 version IN [0] */
440 /* Length of u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
443 /* Length of u8 response IN [CIRCPAD_RESPONSE_ERR, CIRCPAD_RESPONSE_OK] */
446 /* Length of u8 machine_type */
449 /* Length of u32 machine_ctr */
454 circpad_negotiated_clear_errors(circpad_negotiated_t
*obj
)
456 int r
= obj
->trunnel_error_code_
;
457 obj
->trunnel_error_code_
= 0;
461 circpad_negotiated_encode(uint8_t *output
, const size_t avail
, const circpad_negotiated_t
*obj
)
465 uint8_t *ptr
= output
;
467 #ifdef TRUNNEL_CHECK_ENCODED_LEN
468 const ssize_t encoded_len
= circpad_negotiated_encoded_len(obj
);
471 if (NULL
!= (msg
= circpad_negotiated_check(obj
)))
474 #ifdef TRUNNEL_CHECK_ENCODED_LEN
475 trunnel_assert(encoded_len
>= 0);
478 /* Encode u8 version IN [0] */
479 trunnel_assert(written
<= avail
);
480 if (avail
- written
< 1)
482 trunnel_set_uint8(ptr
, (obj
->version
));
483 written
+= 1; ptr
+= 1;
485 /* Encode u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
486 trunnel_assert(written
<= avail
);
487 if (avail
- written
< 1)
489 trunnel_set_uint8(ptr
, (obj
->command
));
490 written
+= 1; ptr
+= 1;
492 /* Encode u8 response IN [CIRCPAD_RESPONSE_ERR, CIRCPAD_RESPONSE_OK] */
493 trunnel_assert(written
<= avail
);
494 if (avail
- written
< 1)
496 trunnel_set_uint8(ptr
, (obj
->response
));
497 written
+= 1; ptr
+= 1;
499 /* Encode u8 machine_type */
500 trunnel_assert(written
<= avail
);
501 if (avail
- written
< 1)
503 trunnel_set_uint8(ptr
, (obj
->machine_type
));
504 written
+= 1; ptr
+= 1;
506 /* Encode u32 machine_ctr */
507 trunnel_assert(written
<= avail
);
508 if (avail
- written
< 4)
510 trunnel_set_uint32(ptr
, trunnel_htonl(obj
->machine_ctr
));
511 written
+= 4; ptr
+= 4;
514 trunnel_assert(ptr
== output
+ written
);
515 #ifdef TRUNNEL_CHECK_ENCODED_LEN
517 trunnel_assert(encoded_len
>= 0);
518 trunnel_assert((size_t)encoded_len
== written
);
533 trunnel_assert(result
< 0);
537 /** As circpad_negotiated_parse(), but do not allocate the output
541 circpad_negotiated_parse_into(circpad_negotiated_t
*obj
, const uint8_t *input
, const size_t len_in
)
543 const uint8_t *ptr
= input
;
544 size_t remaining
= len_in
;
548 /* Parse u8 version IN [0] */
549 CHECK_REMAINING(1, truncated
);
550 obj
->version
= (trunnel_get_uint8(ptr
));
551 remaining
-= 1; ptr
+= 1;
552 if (! (obj
->version
== 0))
555 /* Parse u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
556 CHECK_REMAINING(1, truncated
);
557 obj
->command
= (trunnel_get_uint8(ptr
));
558 remaining
-= 1; ptr
+= 1;
559 if (! (obj
->command
== CIRCPAD_COMMAND_START
|| obj
->command
== CIRCPAD_COMMAND_STOP
))
562 /* Parse u8 response IN [CIRCPAD_RESPONSE_ERR, CIRCPAD_RESPONSE_OK] */
563 CHECK_REMAINING(1, truncated
);
564 obj
->response
= (trunnel_get_uint8(ptr
));
565 remaining
-= 1; ptr
+= 1;
566 if (! (obj
->response
== CIRCPAD_RESPONSE_ERR
|| obj
->response
== CIRCPAD_RESPONSE_OK
))
569 /* Parse u8 machine_type */
570 CHECK_REMAINING(1, truncated
);
571 obj
->machine_type
= (trunnel_get_uint8(ptr
));
572 remaining
-= 1; ptr
+= 1;
574 /* Parse u32 machine_ctr */
575 CHECK_REMAINING(4, truncated
);
576 obj
->machine_ctr
= trunnel_ntohl(trunnel_get_uint32(ptr
));
577 remaining
-= 4; ptr
+= 4;
578 trunnel_assert(ptr
+ remaining
== input
+ len_in
);
579 return len_in
- remaining
;
589 circpad_negotiated_parse(circpad_negotiated_t
**output
, const uint8_t *input
, const size_t len_in
)
592 *output
= circpad_negotiated_new();
595 result
= circpad_negotiated_parse_into(*output
, input
, len_in
);
597 circpad_negotiated_free(*output
);