congestion_control.c (4445B)
1 /* congestion_control.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 "congestion_control.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 congestioncontrol_deadcode_dummy__ = 0; 19 #define OR_DEADCODE_DUMMY || congestioncontrol_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 trn_extension_field_cc_t * 32 trn_extension_field_cc_new(void) 33 { 34 trn_extension_field_cc_t *val = trunnel_calloc(1, sizeof(trn_extension_field_cc_t)); 35 if (NULL == val) 36 return NULL; 37 return val; 38 } 39 40 /** Release all storage held inside 'obj', but do not free 'obj'. 41 */ 42 static void 43 trn_extension_field_cc_clear(trn_extension_field_cc_t *obj) 44 { 45 (void) obj; 46 } 47 48 void 49 trn_extension_field_cc_free(trn_extension_field_cc_t *obj) 50 { 51 if (obj == NULL) 52 return; 53 trn_extension_field_cc_clear(obj); 54 trunnel_memwipe(obj, sizeof(trn_extension_field_cc_t)); 55 trunnel_free_(obj); 56 } 57 58 uint8_t 59 trn_extension_field_cc_get_sendme_inc(const trn_extension_field_cc_t *inp) 60 { 61 return inp->sendme_inc; 62 } 63 int 64 trn_extension_field_cc_set_sendme_inc(trn_extension_field_cc_t *inp, uint8_t val) 65 { 66 inp->sendme_inc = val; 67 return 0; 68 } 69 const char * 70 trn_extension_field_cc_check(const trn_extension_field_cc_t *obj) 71 { 72 if (obj == NULL) 73 return "Object was NULL"; 74 if (obj->trunnel_error_code_) 75 return "A set function failed on this object"; 76 return NULL; 77 } 78 79 ssize_t 80 trn_extension_field_cc_encoded_len(const trn_extension_field_cc_t *obj) 81 { 82 ssize_t result = 0; 83 84 if (NULL != trn_extension_field_cc_check(obj)) 85 return -1; 86 87 88 /* Length of u8 sendme_inc */ 89 result += 1; 90 return result; 91 } 92 int 93 trn_extension_field_cc_clear_errors(trn_extension_field_cc_t *obj) 94 { 95 int r = obj->trunnel_error_code_; 96 obj->trunnel_error_code_ = 0; 97 return r; 98 } 99 ssize_t 100 trn_extension_field_cc_encode(uint8_t *output, const size_t avail, const trn_extension_field_cc_t *obj) 101 { 102 ssize_t result = 0; 103 size_t written = 0; 104 uint8_t *ptr = output; 105 const char *msg; 106 #ifdef TRUNNEL_CHECK_ENCODED_LEN 107 const ssize_t encoded_len = trn_extension_field_cc_encoded_len(obj); 108 #endif 109 110 if (NULL != (msg = trn_extension_field_cc_check(obj))) 111 goto check_failed; 112 113 #ifdef TRUNNEL_CHECK_ENCODED_LEN 114 trunnel_assert(encoded_len >= 0); 115 #endif 116 117 /* Encode u8 sendme_inc */ 118 trunnel_assert(written <= avail); 119 if (avail - written < 1) 120 goto truncated; 121 trunnel_set_uint8(ptr, (obj->sendme_inc)); 122 written += 1; ptr += 1; 123 124 125 trunnel_assert(ptr == output + written); 126 #ifdef TRUNNEL_CHECK_ENCODED_LEN 127 { 128 trunnel_assert(encoded_len >= 0); 129 trunnel_assert((size_t)encoded_len == written); 130 } 131 132 #endif 133 134 return written; 135 136 truncated: 137 result = -2; 138 goto fail; 139 check_failed: 140 (void)msg; 141 result = -1; 142 goto fail; 143 fail: 144 trunnel_assert(result < 0); 145 return result; 146 } 147 148 /** As trn_extension_field_cc_parse(), but do not allocate the output 149 * object. 150 */ 151 static ssize_t 152 trn_extension_field_cc_parse_into(trn_extension_field_cc_t *obj, const uint8_t *input, const size_t len_in) 153 { 154 const uint8_t *ptr = input; 155 size_t remaining = len_in; 156 ssize_t result = 0; 157 (void)result; 158 159 /* Parse u8 sendme_inc */ 160 CHECK_REMAINING(1, truncated); 161 obj->sendme_inc = (trunnel_get_uint8(ptr)); 162 remaining -= 1; ptr += 1; 163 trunnel_assert(ptr + remaining == input + len_in); 164 return len_in - remaining; 165 166 truncated: 167 return -2; 168 } 169 170 ssize_t 171 trn_extension_field_cc_parse(trn_extension_field_cc_t **output, const uint8_t *input, const size_t len_in) 172 { 173 ssize_t result; 174 *output = trn_extension_field_cc_new(); 175 if (NULL == *output) 176 return -1; 177 result = trn_extension_field_cc_parse_into(*output, input, len_in); 178 if (result < 0) { 179 trn_extension_field_cc_free(*output); 180 *output = NULL; 181 } 182 return result; 183 }