Line data Source code
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.
4 : */
5 : #include <stdlib.h>
6 : #include "trunnel-impl.h"
7 :
8 : #include "circpad_negotiation.h"
9 :
10 : #define TRUNNEL_SET_ERROR_CODE(obj) \
11 : do { \
12 : (obj)->trunnel_error_code_ = 1; \
13 : } while (0)
14 :
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__
20 : #else
21 : #define OR_DEADCODE_DUMMY
22 : #endif
23 :
24 : #define CHECK_REMAINING(nbytes, label) \
25 : do { \
26 : if (remaining < (nbytes) OR_DEADCODE_DUMMY) { \
27 : goto label; \
28 : } \
29 : } while (0)
30 :
31 : circpad_negotiate_t *
32 20 : circpad_negotiate_new(void)
33 : {
34 20 : circpad_negotiate_t *val = trunnel_calloc(1, sizeof(circpad_negotiate_t));
35 20 : if (NULL == val)
36 : return NULL;
37 20 : val->command = CIRCPAD_COMMAND_START;
38 20 : return val;
39 : }
40 :
41 : /** Release all storage held inside 'obj', but do not free 'obj'.
42 : */
43 : static void
44 : circpad_negotiate_clear(circpad_negotiate_t *obj)
45 : {
46 : (void) obj;
47 : }
48 :
49 : void
50 20 : circpad_negotiate_free(circpad_negotiate_t *obj)
51 : {
52 20 : if (obj == NULL)
53 : return;
54 20 : circpad_negotiate_clear(obj);
55 20 : trunnel_memwipe(obj, sizeof(circpad_negotiate_t));
56 20 : trunnel_free_(obj);
57 : }
58 :
59 : uint8_t
60 0 : circpad_negotiate_get_version(const circpad_negotiate_t *inp)
61 : {
62 0 : return inp->version;
63 : }
64 : int
65 19 : circpad_negotiate_set_version(circpad_negotiate_t *inp, uint8_t val)
66 : {
67 19 : if (! ((val == 0))) {
68 0 : TRUNNEL_SET_ERROR_CODE(inp);
69 0 : return -1;
70 : }
71 19 : inp->version = val;
72 19 : return 0;
73 : }
74 : uint8_t
75 0 : circpad_negotiate_get_command(const circpad_negotiate_t *inp)
76 : {
77 0 : return inp->command;
78 : }
79 : int
80 19 : circpad_negotiate_set_command(circpad_negotiate_t *inp, uint8_t val)
81 : {
82 19 : if (! ((val == CIRCPAD_COMMAND_START || val == CIRCPAD_COMMAND_STOP))) {
83 0 : TRUNNEL_SET_ERROR_CODE(inp);
84 0 : return -1;
85 : }
86 19 : inp->command = val;
87 19 : return 0;
88 : }
89 : uint8_t
90 0 : circpad_negotiate_get_machine_type(const circpad_negotiate_t *inp)
91 : {
92 0 : return inp->machine_type;
93 : }
94 : int
95 19 : circpad_negotiate_set_machine_type(circpad_negotiate_t *inp, uint8_t val)
96 : {
97 19 : inp->machine_type = val;
98 19 : return 0;
99 : }
100 : uint8_t
101 0 : circpad_negotiate_get_echo_request(const circpad_negotiate_t *inp)
102 : {
103 0 : return inp->echo_request;
104 : }
105 : int
106 0 : circpad_negotiate_set_echo_request(circpad_negotiate_t *inp, uint8_t val)
107 : {
108 0 : if (! ((val == 0 || val == 1))) {
109 0 : TRUNNEL_SET_ERROR_CODE(inp);
110 0 : return -1;
111 : }
112 0 : inp->echo_request = val;
113 0 : return 0;
114 : }
115 : uint32_t
116 0 : circpad_negotiate_get_machine_ctr(const circpad_negotiate_t *inp)
117 : {
118 0 : return inp->machine_ctr;
119 : }
120 : int
121 19 : circpad_negotiate_set_machine_ctr(circpad_negotiate_t *inp, uint32_t val)
122 : {
123 19 : inp->machine_ctr = val;
124 19 : return 0;
125 : }
126 : const char *
127 19 : circpad_negotiate_check(const circpad_negotiate_t *obj)
128 : {
129 19 : if (obj == NULL)
130 : return "Object was NULL";
131 19 : if (obj->trunnel_error_code_)
132 : return "A set function failed on this object";
133 19 : if (! (obj->version == 0))
134 : return "Integer out of bounds";
135 19 : if (! (obj->command == CIRCPAD_COMMAND_START || obj->command == CIRCPAD_COMMAND_STOP))
136 : return "Integer out of bounds";
137 19 : if (! (obj->echo_request == 0 || obj->echo_request == 1))
138 0 : return "Integer out of bounds";
139 : return NULL;
140 : }
141 :
142 : ssize_t
143 0 : circpad_negotiate_encoded_len(const circpad_negotiate_t *obj)
144 : {
145 0 : ssize_t result = 0;
146 :
147 0 : if (NULL != circpad_negotiate_check(obj))
148 0 : return -1;
149 :
150 :
151 : /* Length of u8 version IN [0] */
152 : result += 1;
153 :
154 : /* Length of u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
155 : result += 1;
156 :
157 : /* Length of u8 machine_type */
158 : result += 1;
159 :
160 : /* Length of u8 echo_request IN [0, 1] */
161 : result += 1;
162 :
163 : /* Length of u32 machine_ctr */
164 0 : result += 4;
165 : return result;
166 : }
167 : int
168 0 : circpad_negotiate_clear_errors(circpad_negotiate_t *obj)
169 : {
170 0 : int r = obj->trunnel_error_code_;
171 0 : obj->trunnel_error_code_ = 0;
172 0 : return r;
173 : }
174 : ssize_t
175 19 : circpad_negotiate_encode(uint8_t *output, const size_t avail, const circpad_negotiate_t *obj)
176 : {
177 19 : ssize_t result = 0;
178 19 : size_t written = 0;
179 19 : uint8_t *ptr = output;
180 19 : const char *msg;
181 : #ifdef TRUNNEL_CHECK_ENCODED_LEN
182 : const ssize_t encoded_len = circpad_negotiate_encoded_len(obj);
183 : #endif
184 :
185 19 : if (NULL != (msg = circpad_negotiate_check(obj)))
186 0 : goto check_failed;
187 :
188 : #ifdef TRUNNEL_CHECK_ENCODED_LEN
189 : trunnel_assert(encoded_len >= 0);
190 : #endif
191 :
192 : /* Encode u8 version IN [0] */
193 19 : trunnel_assert(written <= avail);
194 19 : if (avail - written < 1)
195 0 : goto truncated;
196 19 : trunnel_set_uint8(ptr, (obj->version));
197 19 : written += 1; ptr += 1;
198 :
199 : /* Encode u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
200 19 : trunnel_assert(written <= avail);
201 19 : if (avail - written < 1)
202 0 : goto truncated;
203 19 : trunnel_set_uint8(ptr, (obj->command));
204 19 : written += 1; ptr += 1;
205 :
206 : /* Encode u8 machine_type */
207 19 : trunnel_assert(written <= avail);
208 19 : if (avail - written < 1)
209 0 : goto truncated;
210 19 : trunnel_set_uint8(ptr, (obj->machine_type));
211 19 : written += 1; ptr += 1;
212 :
213 : /* Encode u8 echo_request IN [0, 1] */
214 19 : trunnel_assert(written <= avail);
215 19 : if (avail - written < 1)
216 0 : goto truncated;
217 19 : trunnel_set_uint8(ptr, (obj->echo_request));
218 19 : written += 1; ptr += 1;
219 :
220 : /* Encode u32 machine_ctr */
221 19 : trunnel_assert(written <= avail);
222 19 : if (avail - written < 4)
223 0 : goto truncated;
224 19 : trunnel_set_uint32(ptr, trunnel_htonl(obj->machine_ctr));
225 19 : written += 4; ptr += 4;
226 :
227 :
228 19 : trunnel_assert(ptr == output + written);
229 : #ifdef TRUNNEL_CHECK_ENCODED_LEN
230 : {
231 : trunnel_assert(encoded_len >= 0);
232 : trunnel_assert((size_t)encoded_len == written);
233 : }
234 :
235 : #endif
236 :
237 : return written;
238 :
239 0 : truncated:
240 0 : result = -2;
241 0 : goto fail;
242 0 : check_failed:
243 0 : (void)msg;
244 0 : result = -1;
245 0 : goto fail;
246 : fail:
247 : trunnel_assert(result < 0);
248 : return result;
249 : }
250 :
251 : /** As circpad_negotiate_parse(), but do not allocate the output
252 : * object.
253 : */
254 : static ssize_t
255 20 : circpad_negotiate_parse_into(circpad_negotiate_t *obj, const uint8_t *input, const size_t len_in)
256 : {
257 20 : const uint8_t *ptr = input;
258 20 : size_t remaining = len_in;
259 20 : ssize_t result = 0;
260 20 : (void)result;
261 :
262 : /* Parse u8 version IN [0] */
263 20 : CHECK_REMAINING(1, truncated);
264 20 : obj->version = (trunnel_get_uint8(ptr));
265 20 : remaining -= 1; ptr += 1;
266 20 : if (! (obj->version == 0))
267 0 : goto fail;
268 :
269 : /* Parse u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
270 20 : CHECK_REMAINING(1, truncated);
271 20 : obj->command = (trunnel_get_uint8(ptr));
272 20 : remaining -= 1; ptr += 1;
273 20 : if (! (obj->command == CIRCPAD_COMMAND_START || obj->command == CIRCPAD_COMMAND_STOP))
274 1 : goto fail;
275 :
276 : /* Parse u8 machine_type */
277 19 : CHECK_REMAINING(1, truncated);
278 19 : obj->machine_type = (trunnel_get_uint8(ptr));
279 19 : remaining -= 1; ptr += 1;
280 :
281 : /* Parse u8 echo_request IN [0, 1] */
282 19 : CHECK_REMAINING(1, truncated);
283 19 : obj->echo_request = (trunnel_get_uint8(ptr));
284 19 : remaining -= 1; ptr += 1;
285 19 : if (! (obj->echo_request == 0 || obj->echo_request == 1))
286 0 : goto fail;
287 :
288 : /* Parse u32 machine_ctr */
289 19 : CHECK_REMAINING(4, truncated);
290 19 : obj->machine_ctr = trunnel_ntohl(trunnel_get_uint32(ptr));
291 19 : remaining -= 4; ptr += 4;
292 19 : trunnel_assert(ptr + remaining == input + len_in);
293 : return len_in - remaining;
294 :
295 : truncated:
296 : return -2;
297 : fail:
298 20 : result = -1;
299 : return result;
300 : }
301 :
302 : ssize_t
303 20 : circpad_negotiate_parse(circpad_negotiate_t **output, const uint8_t *input, const size_t len_in)
304 : {
305 20 : ssize_t result;
306 20 : *output = circpad_negotiate_new();
307 20 : if (NULL == *output)
308 : return -1;
309 20 : result = circpad_negotiate_parse_into(*output, input, len_in);
310 20 : if (result < 0) {
311 1 : circpad_negotiate_free(*output);
312 1 : *output = NULL;
313 : }
314 : return result;
315 : }
316 : circpad_negotiated_t *
317 19 : circpad_negotiated_new(void)
318 : {
319 19 : circpad_negotiated_t *val = trunnel_calloc(1, sizeof(circpad_negotiated_t));
320 19 : if (NULL == val)
321 : return NULL;
322 19 : val->command = CIRCPAD_COMMAND_START;
323 19 : val->response = CIRCPAD_RESPONSE_ERR;
324 19 : return val;
325 : }
326 :
327 : /** Release all storage held inside 'obj', but do not free 'obj'.
328 : */
329 : static void
330 : circpad_negotiated_clear(circpad_negotiated_t *obj)
331 : {
332 : (void) obj;
333 : }
334 :
335 : void
336 19 : circpad_negotiated_free(circpad_negotiated_t *obj)
337 : {
338 19 : if (obj == NULL)
339 : return;
340 19 : circpad_negotiated_clear(obj);
341 19 : trunnel_memwipe(obj, sizeof(circpad_negotiated_t));
342 19 : trunnel_free_(obj);
343 : }
344 :
345 : uint8_t
346 0 : circpad_negotiated_get_version(const circpad_negotiated_t *inp)
347 : {
348 0 : return inp->version;
349 : }
350 : int
351 22 : circpad_negotiated_set_version(circpad_negotiated_t *inp, uint8_t val)
352 : {
353 22 : if (! ((val == 0))) {
354 0 : TRUNNEL_SET_ERROR_CODE(inp);
355 0 : return -1;
356 : }
357 22 : inp->version = val;
358 22 : return 0;
359 : }
360 : uint8_t
361 0 : circpad_negotiated_get_command(const circpad_negotiated_t *inp)
362 : {
363 0 : return inp->command;
364 : }
365 : int
366 22 : circpad_negotiated_set_command(circpad_negotiated_t *inp, uint8_t val)
367 : {
368 22 : if (! ((val == CIRCPAD_COMMAND_START || val == CIRCPAD_COMMAND_STOP))) {
369 0 : TRUNNEL_SET_ERROR_CODE(inp);
370 0 : return -1;
371 : }
372 22 : inp->command = val;
373 22 : return 0;
374 : }
375 : uint8_t
376 0 : circpad_negotiated_get_response(const circpad_negotiated_t *inp)
377 : {
378 0 : return inp->response;
379 : }
380 : int
381 22 : circpad_negotiated_set_response(circpad_negotiated_t *inp, uint8_t val)
382 : {
383 22 : if (! ((val == CIRCPAD_RESPONSE_ERR || val == CIRCPAD_RESPONSE_OK))) {
384 0 : TRUNNEL_SET_ERROR_CODE(inp);
385 0 : return -1;
386 : }
387 22 : inp->response = val;
388 22 : return 0;
389 : }
390 : uint8_t
391 0 : circpad_negotiated_get_machine_type(const circpad_negotiated_t *inp)
392 : {
393 0 : return inp->machine_type;
394 : }
395 : int
396 22 : circpad_negotiated_set_machine_type(circpad_negotiated_t *inp, uint8_t val)
397 : {
398 22 : inp->machine_type = val;
399 22 : return 0;
400 : }
401 : uint32_t
402 0 : circpad_negotiated_get_machine_ctr(const circpad_negotiated_t *inp)
403 : {
404 0 : return inp->machine_ctr;
405 : }
406 : int
407 22 : circpad_negotiated_set_machine_ctr(circpad_negotiated_t *inp, uint32_t val)
408 : {
409 22 : inp->machine_ctr = val;
410 22 : return 0;
411 : }
412 : const char *
413 22 : circpad_negotiated_check(const circpad_negotiated_t *obj)
414 : {
415 22 : if (obj == NULL)
416 : return "Object was NULL";
417 22 : if (obj->trunnel_error_code_)
418 : return "A set function failed on this object";
419 22 : if (! (obj->version == 0))
420 : return "Integer out of bounds";
421 22 : if (! (obj->command == CIRCPAD_COMMAND_START || obj->command == CIRCPAD_COMMAND_STOP))
422 : return "Integer out of bounds";
423 22 : if (! (obj->response == CIRCPAD_RESPONSE_ERR || obj->response == CIRCPAD_RESPONSE_OK))
424 0 : return "Integer out of bounds";
425 : return NULL;
426 : }
427 :
428 : ssize_t
429 0 : circpad_negotiated_encoded_len(const circpad_negotiated_t *obj)
430 : {
431 0 : ssize_t result = 0;
432 :
433 0 : if (NULL != circpad_negotiated_check(obj))
434 0 : return -1;
435 :
436 :
437 : /* Length of u8 version IN [0] */
438 : result += 1;
439 :
440 : /* Length of u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
441 : result += 1;
442 :
443 : /* Length of u8 response IN [CIRCPAD_RESPONSE_ERR, CIRCPAD_RESPONSE_OK] */
444 : result += 1;
445 :
446 : /* Length of u8 machine_type */
447 : result += 1;
448 :
449 : /* Length of u32 machine_ctr */
450 0 : result += 4;
451 : return result;
452 : }
453 : int
454 0 : circpad_negotiated_clear_errors(circpad_negotiated_t *obj)
455 : {
456 0 : int r = obj->trunnel_error_code_;
457 0 : obj->trunnel_error_code_ = 0;
458 0 : return r;
459 : }
460 : ssize_t
461 22 : circpad_negotiated_encode(uint8_t *output, const size_t avail, const circpad_negotiated_t *obj)
462 : {
463 22 : ssize_t result = 0;
464 22 : size_t written = 0;
465 22 : uint8_t *ptr = output;
466 22 : const char *msg;
467 : #ifdef TRUNNEL_CHECK_ENCODED_LEN
468 : const ssize_t encoded_len = circpad_negotiated_encoded_len(obj);
469 : #endif
470 :
471 22 : if (NULL != (msg = circpad_negotiated_check(obj)))
472 0 : goto check_failed;
473 :
474 : #ifdef TRUNNEL_CHECK_ENCODED_LEN
475 : trunnel_assert(encoded_len >= 0);
476 : #endif
477 :
478 : /* Encode u8 version IN [0] */
479 22 : trunnel_assert(written <= avail);
480 22 : if (avail - written < 1)
481 0 : goto truncated;
482 22 : trunnel_set_uint8(ptr, (obj->version));
483 22 : written += 1; ptr += 1;
484 :
485 : /* Encode u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
486 22 : trunnel_assert(written <= avail);
487 22 : if (avail - written < 1)
488 0 : goto truncated;
489 22 : trunnel_set_uint8(ptr, (obj->command));
490 22 : written += 1; ptr += 1;
491 :
492 : /* Encode u8 response IN [CIRCPAD_RESPONSE_ERR, CIRCPAD_RESPONSE_OK] */
493 22 : trunnel_assert(written <= avail);
494 22 : if (avail - written < 1)
495 0 : goto truncated;
496 22 : trunnel_set_uint8(ptr, (obj->response));
497 22 : written += 1; ptr += 1;
498 :
499 : /* Encode u8 machine_type */
500 22 : trunnel_assert(written <= avail);
501 22 : if (avail - written < 1)
502 0 : goto truncated;
503 22 : trunnel_set_uint8(ptr, (obj->machine_type));
504 22 : written += 1; ptr += 1;
505 :
506 : /* Encode u32 machine_ctr */
507 22 : trunnel_assert(written <= avail);
508 22 : if (avail - written < 4)
509 0 : goto truncated;
510 22 : trunnel_set_uint32(ptr, trunnel_htonl(obj->machine_ctr));
511 22 : written += 4; ptr += 4;
512 :
513 :
514 22 : trunnel_assert(ptr == output + written);
515 : #ifdef TRUNNEL_CHECK_ENCODED_LEN
516 : {
517 : trunnel_assert(encoded_len >= 0);
518 : trunnel_assert((size_t)encoded_len == written);
519 : }
520 :
521 : #endif
522 :
523 : return written;
524 :
525 0 : truncated:
526 0 : result = -2;
527 0 : goto fail;
528 0 : check_failed:
529 0 : (void)msg;
530 0 : result = -1;
531 0 : goto fail;
532 : fail:
533 : trunnel_assert(result < 0);
534 : return result;
535 : }
536 :
537 : /** As circpad_negotiated_parse(), but do not allocate the output
538 : * object.
539 : */
540 : static ssize_t
541 19 : circpad_negotiated_parse_into(circpad_negotiated_t *obj, const uint8_t *input, const size_t len_in)
542 : {
543 19 : const uint8_t *ptr = input;
544 19 : size_t remaining = len_in;
545 19 : ssize_t result = 0;
546 19 : (void)result;
547 :
548 : /* Parse u8 version IN [0] */
549 19 : CHECK_REMAINING(1, truncated);
550 19 : obj->version = (trunnel_get_uint8(ptr));
551 19 : remaining -= 1; ptr += 1;
552 19 : if (! (obj->version == 0))
553 1 : goto fail;
554 :
555 : /* Parse u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
556 18 : CHECK_REMAINING(1, truncated);
557 18 : obj->command = (trunnel_get_uint8(ptr));
558 18 : remaining -= 1; ptr += 1;
559 18 : if (! (obj->command == CIRCPAD_COMMAND_START || obj->command == CIRCPAD_COMMAND_STOP))
560 0 : goto fail;
561 :
562 : /* Parse u8 response IN [CIRCPAD_RESPONSE_ERR, CIRCPAD_RESPONSE_OK] */
563 18 : CHECK_REMAINING(1, truncated);
564 18 : obj->response = (trunnel_get_uint8(ptr));
565 18 : remaining -= 1; ptr += 1;
566 18 : if (! (obj->response == CIRCPAD_RESPONSE_ERR || obj->response == CIRCPAD_RESPONSE_OK))
567 0 : goto fail;
568 :
569 : /* Parse u8 machine_type */
570 18 : CHECK_REMAINING(1, truncated);
571 18 : obj->machine_type = (trunnel_get_uint8(ptr));
572 18 : remaining -= 1; ptr += 1;
573 :
574 : /* Parse u32 machine_ctr */
575 18 : CHECK_REMAINING(4, truncated);
576 18 : obj->machine_ctr = trunnel_ntohl(trunnel_get_uint32(ptr));
577 18 : remaining -= 4; ptr += 4;
578 18 : trunnel_assert(ptr + remaining == input + len_in);
579 : return len_in - remaining;
580 :
581 : truncated:
582 : return -2;
583 : fail:
584 19 : result = -1;
585 : return result;
586 : }
587 :
588 : ssize_t
589 19 : circpad_negotiated_parse(circpad_negotiated_t **output, const uint8_t *input, const size_t len_in)
590 : {
591 19 : ssize_t result;
592 19 : *output = circpad_negotiated_new();
593 19 : if (NULL == *output)
594 : return -1;
595 19 : result = circpad_negotiated_parse_into(*output, input, len_in);
596 19 : if (result < 0) {
597 1 : circpad_negotiated_free(*output);
598 1 : *output = NULL;
599 : }
600 : return result;
601 : }
|