| 1 |
|
| 2 |
C |
| 3 |
C |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
r. |
| 9 |
|
| 10 |
|
| 11 |
#include "proto/adv.h" |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
size_t rist_adv_header_size(const struct rist_adv_params *params) |
| 15 |
{ |
| 16 |
size_t sz = RIST_ADV_HEADER_MIN; |
| 17 |
|
| 18 |
if (params->flow_id) |
| 19 |
sz += RIST_ADV_FLOW_ID_SIZE; |
| 20 |
|
| 21 |
sz += rist_adv_psk_hdr_size(params->psk_mode); |
| 22 |
|
| 23 |
if (params->lpc_mode == RIST_ADV_LPC_FIELD_PRESENT && params->compression) |
| 24 |
sz += RIST_ADV_COMPRESSION_SIZE; |
| 25 |
|
| 26 |
if (params->pfd) |
| 27 |
sz += RIST_ADV_PFD_SIZE; |
| 28 |
|
| 29 |
if (params->hdr_ext && params->hdr_ext_len > 0) |
| 30 |
sz += params->hdr_ext_len; |
| 31 |
|
| 32 |
return sz; |
| 33 |
} |
| 34 |
|
| 35 |
int rist_adv_parse(const uint8_t *buf, size_t len, |
| 36 |
struct rist_adv_parsed *out) |
| 37 |
{ |
| 38 |
if (!buf || !out || len < RIST_ADV_HEADER_MIN) |
| 39 |
return -1; |
| 40 |
|
| 41 |
memset(out, 0, sizeof(*out)); |
| 42 |
|
| 43 |
const struct rist_rtp_hdr *rtp = (const struct rist_rtp_hdr *)buf; |
| 44 |
|
| 45 |
|
| 46 |
if ((rtp->flags & 0xC0) != 0x80) |
| 47 |
return -1; |
| 48 |
|
| 49 |
|
| 50 |
uint8_t pt = rtp->payload_type & 0x7F; |
| 51 |
if (pt != RIST_ADV_PT && pt < 96) |
| 52 |
return -1; |
| 53 |
|
| 54 |
out->rtp_padding = !!(rtp->flags & 0x20); |
| 55 |
out->rtp_ext_present = !!(rtp->flags & 0x10); |
| 56 |
|
| 57 |
|
| 58 |
uint8_t cc = rtp->flags & 0x0F; |
| 59 |
|
| 60 |
out->timestamp = ntohl(rtp->ts); |
| 61 |
out->ssrc = ntohl(rtp->ssrc); |
| 62 |
|
| 63 |
size_t offset = sizeof(struct rist_rtp_hdr); |
| 64 |
|
| 65 |
|
| 66 |
offset += (size_t)cc * 4; |
| 67 |
if (offset > len) |
| 68 |
return -1; |
| 69 |
|
| 70 |
|
| 71 |
if (out->rtp_ext_present) { |
| 72 |
if (offset + 4 > len) |
| 73 |
return -1; |
| 74 |
uint16_t ext_len_words = ntohs(*(const uint16_t *)(buf + offset + 2)); |
| 75 |
offset += 4 + (size_t)ext_len_words * 4; |
| 76 |
if (offset > len) |
| 77 |
return -1; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
if (offset + RIST_ADV_EXT_SIZE > len) |
| 82 |
return -1; |
| 83 |
|
| 84 |
const struct rist_adv_ext *ext = |
| 85 |
(const struct rist_adv_ext *)(buf + offset); |
| 86 |
offset += RIST_ADV_EXT_SIZE; |
| 87 |
|
| 88 |
out->seq = rist_adv_seq32(rtp, ext); |
| 89 |
out->first_frag = !!(ext->flags & RIST_ADV_FLAG_F); |
| 90 |
out->last_frag = !!(ext->flags & RIST_ADV_FLAG_L); |
| 91 |
out->expedite = !!(ext->flags & RIST_ADV_FLAG_E); |
| 92 |
out->retransmit = !!(ext->flags & RIST_ADV_FLAG_R); |
| 93 |
out->has_flow_id = !!(ext->flags & RIST_ADV_FLAG_I); |
| 94 |
out->has_pfd = !!(ext->flags & RIST_ADV_FLAG_P); |
| 95 |
out->has_hdr_ext = !!(ext->flags & RIST_ADV_FLAG_H); |
| 96 |
|
| 97 |
out->psk_mode = rist_adv_get_psk(ext); |
| 98 |
out->lpc_mode = rist_adv_get_lpc(ext); |
| 99 |
out->enc_type = rist_adv_get_type(ext); |
| 100 |
|
| 101 |
out->has_psk = (out->psk_mode > 0); |
| 102 |
out->has_compression = (out->lpc_mode == RIST_ADV_LPC_FIELD_PRESENT); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
if (out->has_flow_id) { |
| 110 |
if (offset + RIST_ADV_FLOW_ID_SIZE > len) |
| 111 |
return -1; |
| 112 |
out->flow_id = (const struct rist_adv_flow_id *)(buf + offset); |
| 113 |
offset += RIST_ADV_FLOW_ID_SIZE; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
if (rist_adv_psk_has_hash(out->psk_mode)) { |
| 118 |
if (offset + RIST_ADV_PSK_HASH_SIZE > len) |
| 119 |
return -1; |
| 120 |
out->psk_hash = buf + offset; |
| 121 |
offset += RIST_ADV_PSK_HASH_SIZE; |
| 122 |
} |
| 123 |
if (rist_adv_psk_has_nonce(out->psk_mode)) { |
| 124 |
if (offset + RIST_ADV_PSK_NONCE_SIZE > len) |
| 125 |
return -1; |
| 126 |
out->psk_nonce = buf + offset; |
| 127 |
offset += RIST_ADV_PSK_NONCE_SIZE; |
| 128 |
} |
| 129 |
if (rist_adv_psk_has_iv(out->psk_mode)) { |
| 130 |
if (offset + RIST_ADV_PSK_IV_SIZE > len) |
| 131 |
return -1; |
| 132 |
out->psk_iv = buf + offset; |
| 133 |
offset += RIST_ADV_PSK_IV_SIZE; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
if (out->has_compression) { |
| 138 |
if (offset + RIST_ADV_COMPRESSION_SIZE > len) |
| 139 |
return -1; |
| 140 |
out->compression = buf + offset; |
| 141 |
offset += RIST_ADV_COMPRESSION_SIZE; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
if (out->has_pfd) { |
| 146 |
if (offset + RIST_ADV_PFD_SIZE > len) |
| 147 |
return -1; |
| 148 |
out->pfd = (const struct rist_adv_pfd *)(buf + offset); |
| 149 |
offset += RIST_ADV_PFD_SIZE; |
| 150 |
} |
| 151 |
|
| 152 |
*/ |
| 153 |
if (out->has_hdr_ext) { |
| 154 |
if (offset + 4 > len) |
| 155 |
return -1; |
| 156 |
uint16_t hdr_ext_words = ntohs(*(const uint16_t *)(buf + offset + 2)); |
| 157 |
size_t hdr_ext_total = 4 + (size_t)hdr_ext_words * 4; |
| 158 |
if (offset + hdr_ext_total > len) |
| 159 |
return -1; |
| 160 |
out->hdr_ext = buf + offset; |
| 161 |
out->hdr_ext_len = hdr_ext_total; |
| 162 |
offset += hdr_ext_total; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
out->payload = buf + offset; |
| 167 |
out->payload_len = len - offset; |
| 168 |
|
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
int rist_adv_build(uint8_t *buf, const struct rist_adv_params *params, |
| 173 |
const uint8_t *payload, size_t payload_len) |
| 174 |
{ |
| 175 |
if (!buf || !params) |
| 176 |
return -1; |
| 177 |
|
| 178 |
size_t offset = 0; |
| 179 |
|
| 180 |
|
| 181 |
struct rist_rtp_hdr *rtp = (struct rist_rtp_hdr *)buf; |
| 182 |
rtp->flags = RIST_ADV_RTP_FLAGS; |
| 183 |
rtp->payload_type = RIST_ADV_PT; |
| 184 |
rtp->ts = htonl(params->timestamp); |
| 185 |
rtp->ssrc = htonl(params->ssrc); |
| 186 |
offset += sizeof(struct rist_rtp_hdr); |
| 187 |
|
| 188 |
|
| 189 |
struct rist_adv_ext *ext = (struct rist_adv_ext *)(buf + offset); |
| 190 |
memset(ext, 0, sizeof(*ext)); |
| 191 |
offset += RIST_ADV_EXT_SIZE; |
| 192 |
|
| 193 |
rist_adv_set_seq32(rtp, ext, params->seq); |
| 194 |
|
| 195 |
|
| 196 |
ext->flags = 0; |
| 197 |
if (params->first_frag) ext->flags |= RIST_ADV_FLAG_F; |
| 198 |
if (params->last_frag) ext->flags |= RIST_ADV_FLAG_L; |
| 199 |
if (params->expedite) ext->flags |= RIST_ADV_FLAG_E; |
| 200 |
if (params->retransmit) ext->flags |= RIST_ADV_FLAG_R; |
| 201 |
if (params->flow_id) ext->flags |= RIST_ADV_FLAG_I; |
| 202 |
if (params->pfd) ext->flags |= RIST_ADV_FLAG_P; |
| 203 |
if (params->hdr_ext && params->hdr_ext_len > 0) |
| 204 |
ext->flags |= RIST_ADV_FLAG_H; |
| 205 |
|
| 206 |
|
| 207 |
rist_adv_set_psk(ext, params->psk_mode); |
| 208 |
rist_adv_set_lpc(ext, params->lpc_mode); |
| 209 |
rist_adv_set_type(ext, params->enc_type); |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
if (params->flow_id) { |
| 215 |
memcpy(buf + offset, params->flow_id, RIST_ADV_FLOW_ID_SIZE); |
| 216 |
offset += RIST_ADV_FLOW_ID_SIZE; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
if (rist_adv_psk_has_hash(params->psk_mode) && params->psk_hash) { |
| 221 |
memcpy(buf + offset, params->psk_hash, RIST_ADV_PSK_HASH_SIZE); |
| 222 |
offset += RIST_ADV_PSK_HASH_SIZE; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
if (rist_adv_psk_has_nonce(params->psk_mode) && params->psk_nonce) { |
| 227 |
memcpy(buf + offset, params->psk_nonce, RIST_ADV_PSK_NONCE_SIZE); |
| 228 |
offset += RIST_ADV_PSK_NONCE_SIZE; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
if (rist_adv_psk_has_iv(params->psk_mode) && params->psk_iv) { |
| 233 |
memcpy(buf + offset, params->psk_iv, RIST_ADV_PSK_IV_SIZE); |
| 234 |
offset += RIST_ADV_PSK_IV_SIZE; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
if (params->lpc_mode == RIST_ADV_LPC_FIELD_PRESENT && params->compression) { |
| 239 |
memcpy(buf + offset, params->compression, RIST_ADV_COMPRESSION_SIZE); |
| 240 |
offset += RIST_ADV_COMPRESSION_SIZE; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
if (params->pfd) { |
| 245 |
memcpy(buf + offset, params->pfd, RIST_ADV_PFD_SIZE); |
| 246 |
offset += RIST_ADV_PFD_SIZE; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
if (params->hdr_ext && params->hdr_ext_len > 0) { |
| 251 |
memcpy(buf + offset, params->hdr_ext, params->hdr_ext_len); |
| 252 |
offset += params->hdr_ext_len; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
if (payload && payload_len > 0) |
| 257 |
memcpy(buf + offset, payload, payload_len); |
| 258 |
|
| 259 |
return (int)(offset + payload_len); |
| 260 |
} |