| 1 |
|
| 2 |
C |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
ge |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include "proto/adv.h" |
| 12 |
#include "rist-private.h" |
| 13 |
#include "udp-private.h" |
| 14 |
#include "transport-private.h" |
| 15 |
#include "log-private.h" |
| 16 |
#include "proto/rist_time.h" |
| 17 |
#include "crypto/psk.h" |
| 18 |
#include "cjson/cJSON.h" |
| 19 |
#include <string.h> |
| 20 |
#include <errno.h> |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
static int rist_adv_build_control(uint8_t *buf, uint32_t seq, uint32_t timestamp, |
| 37 |
uint32_t ssrc, const uint8_t *ctrl_payload, |
| 38 |
size_t ctrl_len) |
| 39 |
{ |
| 40 |
struct rist_adv_params params; |
| 41 |
memset(¶ms, 0, sizeof(params)); |
| 42 |
params.seq = seq; |
| 43 |
params.timestamp = timestamp; |
| 44 |
params.ssrc = ssrc; |
| 45 |
params.enc_type = RIST_ADV_TYPE_CONTROL; |
| 46 |
params.psk_mode = RIST_ADV_PSK_NONE; |
| 47 |
params.lpc_mode = RIST_ADV_LPC_NONE; |
| 48 |
params.first_frag = true; |
| 49 |
params.last_frag = true; |
| 50 |
params.expedite = true; |
| 51 |
params.retransmit = false; |
| 52 |
|
| 53 |
return rist_adv_build(buf, ¶ms, ctrl_payload, ctrl_len); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
| |
| 62 |
| |
| 63 |
|
| 64 |
|
| 65 |
int rist_adv_send_nack_bitmask(struct rist_peer *peer, |
| 66 |
uint32_t media_ssrc, |
| 67 |
uint32_t pss, uint32_t blp) |
| 68 |
{ |
| 69 |
struct rist_common_ctx *ctx = get_cctx(peer); |
| 70 |
uint8_t pkt[RIST_MAX_PACKET_SIZE]; |
| 71 |
uint8_t ctrl[4 + 12]; |
| 72 |
size_t off = 0; |
| 73 |
|
| 74 |
|
| 75 |
ctrl[off++] = (RIST_ADV_CI_NACK_BITMASK >> 8) & 0xFF; |
| 76 |
ctrl[off++] = RIST_ADV_CI_NACK_BITMASK & 0xFF; |
| 77 |
uint16_t body_len = 12; |
| 78 |
ctrl[off++] = (body_len >> 8) & 0xFF; |
| 79 |
ctrl[off++] = body_len & 0xFF; |
| 80 |
|
| 81 |
|
| 82 |
ctrl[off++] = (media_ssrc >> 24) & 0xFF; |
| 83 |
ctrl[off++] = (media_ssrc >> 16) & 0xFF; |
| 84 |
ctrl[off++] = (media_ssrc >> 8) & 0xFF; |
| 85 |
ctrl[off++] = media_ssrc & 0xFF; |
| 86 |
|
| 87 |
|
| 88 |
ctrl[off++] = (pss >> 24) & 0xFF; |
| 89 |
ctrl[off++] = (pss >> 16) & 0xFF; |
| 90 |
ctrl[off++] = (pss >> 8) & 0xFF; |
| 91 |
ctrl[off++] = pss & 0xFF; |
| 92 |
|
| 93 |
|
| 94 |
ctrl[off++] = (blp >> 24) & 0xFF; |
| 95 |
ctrl[off++] = (blp >> 16) & 0xFF; |
| 96 |
ctrl[off++] = (blp >> 8) & 0xFF; |
| 97 |
ctrl[off++] = blp & 0xFF; |
| 98 |
|
| 99 |
uint32_t seq = ctx->adv_seq_unprotected++; |
| 100 |
uint32_t ts = timestampRTP_u32(1, timestampNTP_u64()); |
| 101 |
uint32_t ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base); |
| 102 |
|
| 103 |
int total = rist_adv_build_control(pkt, seq, ts, ssrc, ctrl, off); |
| 104 |
if (total < 0) |
| 105 |
return -1; |
| 106 |
|
| 107 |
ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0); |
| 108 |
if (ret < 0) |
| 109 |
_librist_log_send_error(peer, errno, (size_t)total, "adv-nack-bitmask sendto"); |
| 110 |
|
| 111 |
return (ret > 0) ? 0 : -1; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
| |
| 120 |
| |
| 121 |
|
| 122 |
|
| 123 |
int rist_adv_send_nack_range(struct rist_peer *peer, |
| 124 |
uint32_t media_ssrc, |
| 125 |
uint32_t pss, uint32_t nalp) |
| 126 |
{ |
| 127 |
struct rist_common_ctx *ctx = get_cctx(peer); |
| 128 |
uint8_t pkt[RIST_MAX_PACKET_SIZE]; |
| 129 |
uint8_t ctrl[4 + 12]; |
| 130 |
size_t off = 0; |
| 131 |
|
| 132 |
ctrl[off++] = (RIST_ADV_CI_NACK_RANGE >> 8) & 0xFF; |
| 133 |
ctrl[off++] = RIST_ADV_CI_NACK_RANGE & 0xFF; |
| 134 |
uint16_t body_len = 12; |
| 135 |
ctrl[off++] = (body_len >> 8) & 0xFF; |
| 136 |
ctrl[off++] = body_len & 0xFF; |
| 137 |
|
| 138 |
ctrl[off++] = (media_ssrc >> 24) & 0xFF; |
| 139 |
ctrl[off++] = (media_ssrc >> 16) & 0xFF; |
| 140 |
ctrl[off++] = (media_ssrc >> 8) & 0xFF; |
| 141 |
ctrl[off++] = media_ssrc & 0xFF; |
| 142 |
|
| 143 |
ctrl[off++] = (pss >> 24) & 0xFF; |
| 144 |
ctrl[off++] = (pss >> 16) & 0xFF; |
| 145 |
ctrl[off++] = (pss >> 8) & 0xFF; |
| 146 |
ctrl[off++] = pss & 0xFF; |
| 147 |
|
| 148 |
ctrl[off++] = (nalp >> 24) & 0xFF; |
| 149 |
ctrl[off++] = (nalp >> 16) & 0xFF; |
| 150 |
ctrl[off++] = (nalp >> 8) & 0xFF; |
| 151 |
ctrl[off++] = nalp & 0xFF; |
| 152 |
|
| 153 |
uint32_t seq = ctx->adv_seq_unprotected++; |
| 154 |
uint32_t ts = timestampRTP_u32(1, timestampNTP_u64()); |
| 155 |
uint32_t ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base); |
| 156 |
|
| 157 |
int total = rist_adv_build_control(pkt, seq, ts, ssrc, ctrl, off); |
| 158 |
if (total < 0) |
| 159 |
return -1; |
| 160 |
|
| 161 |
ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0); |
| 162 |
if (ret < 0) |
| 163 |
_librist_log_send_error(peer, errno, (size_t)total, "adv-nack-range sendto"); |
| 164 |
|
| 165 |
return (ret > 0) ? 0 : -1; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
int rist_adv_send_rtt_echo_request(struct rist_peer *peer) |
| 180 |
{ |
| 181 |
struct rist_common_ctx *ctx = get_cctx(peer); |
| 182 |
uint8_t pkt[RIST_MAX_PACKET_SIZE]; |
| 183 |
uint8_t ctrl[4 + 16]; |
| 184 |
size_t off = 0; |
| 185 |
|
| 186 |
ctrl[off++] = (RIST_ADV_CI_RTT_ECHO_REQ >> 8) & 0xFF; |
| 187 |
ctrl[off++] = RIST_ADV_CI_RTT_ECHO_REQ & 0xFF; |
| 188 |
uint16_t body_len = 16; |
| 189 |
ctrl[off++] = (body_len >> 8) & 0xFF; |
| 190 |
ctrl[off++] = body_len & 0xFF; |
| 191 |
|
| 192 |
uint32_t req_ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base); |
| 193 |
ctrl[off++] = (req_ssrc >> 24) & 0xFF; |
| 194 |
ctrl[off++] = (req_ssrc >> 16) & 0xFF; |
| 195 |
ctrl[off++] = (req_ssrc >> 8) & 0xFF; |
| 196 |
ctrl[off++] = req_ssrc & 0xFF; |
| 197 |
|
| 198 |
uint64_t ntp = timestampNTP_u64(); |
| 199 |
uint32_t msw = (uint32_t)(ntp >> 32); |
| 200 |
uint32_t lsw = (uint32_t)(ntp & 0xFFFFFFFF); |
| 201 |
|
| 202 |
ctrl[off++] = (msw >> 24) & 0xFF; |
| 203 |
ctrl[off++] = (msw >> 16) & 0xFF; |
| 204 |
ctrl[off++] = (msw >> 8) & 0xFF; |
| 205 |
ctrl[off++] = msw & 0xFF; |
| 206 |
|
| 207 |
ctrl[off++] = (lsw >> 24) & 0xFF; |
| 208 |
ctrl[off++] = (lsw >> 16) & 0xFF; |
| 209 |
ctrl[off++] = (lsw >> 8) & 0xFF; |
| 210 |
ctrl[off++] = lsw & 0xFF; |
| 211 |
|
| 212 |
|
| 213 |
ctrl[off++] = 0; ctrl[off++] = 0; ctrl[off++] = 0; ctrl[off++] = 0; |
| 214 |
|
| 215 |
uint32_t seq = ctx->adv_seq_unprotected++; |
| 216 |
uint32_t ts = timestampRTP_u32(1, ntp); |
| 217 |
|
| 218 |
int total = rist_adv_build_control(pkt, seq, ts, req_ssrc, ctrl, off); |
| 219 |
if (total < 0) |
| 220 |
return -1; |
| 221 |
|
| 222 |
ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0); |
| 223 |
if (ret < 0) |
| 224 |
_librist_log_send_error(peer, errno, (size_t)total, "adv-rtt-echo-req sendto"); |
| 225 |
|
| 226 |
return (ret > 0) ? 0 : -1; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
int rist_adv_send_rtt_echo_response(struct rist_peer *peer, |
| 236 |
uint32_t requester_ssrc, |
| 237 |
uint32_t orig_msw, uint32_t orig_lsw, |
| 238 |
uint32_t processing_delay_us) |
| 239 |
{ |
| 240 |
struct rist_common_ctx *ctx = get_cctx(peer); |
| 241 |
uint8_t pkt[RIST_MAX_PACKET_SIZE]; |
| 242 |
uint8_t ctrl[4 + 16]; |
| 243 |
size_t off = 0; |
| 244 |
|
| 245 |
ctrl[off++] = (RIST_ADV_CI_RTT_ECHO_RESP >> 8) & 0xFF; |
| 246 |
ctrl[off++] = RIST_ADV_CI_RTT_ECHO_RESP & 0xFF; |
| 247 |
uint16_t body_len = 16; |
| 248 |
ctrl[off++] = (body_len >> 8) & 0xFF; |
| 249 |
ctrl[off++] = body_len & 0xFF; |
| 250 |
|
| 251 |
ctrl[off++] = (requester_ssrc >> 24) & 0xFF; |
| 252 |
ctrl[off++] = (requester_ssrc >> 16) & 0xFF; |
| 253 |
ctrl[off++] = (requester_ssrc >> 8) & 0xFF; |
| 254 |
ctrl[off++] = requester_ssrc & 0xFF; |
| 255 |
|
| 256 |
ctrl[off++] = (orig_msw >> 24) & 0xFF; |
| 257 |
ctrl[off++] = (orig_msw >> 16) & 0xFF; |
| 258 |
ctrl[off++] = (orig_msw >> 8) & 0xFF; |
| 259 |
ctrl[off++] = orig_msw & 0xFF; |
| 260 |
|
| 261 |
ctrl[off++] = (orig_lsw >> 24) & 0xFF; |
| 262 |
ctrl[off++] = (orig_lsw >> 16) & 0xFF; |
| 263 |
ctrl[off++] = (orig_lsw >> 8) & 0xFF; |
| 264 |
ctrl[off++] = orig_lsw & 0xFF; |
| 265 |
|
| 266 |
ctrl[off++] = (processing_delay_us >> 24) & 0xFF; |
| 267 |
ctrl[off++] = (processing_delay_us >> 16) & 0xFF; |
| 268 |
ctrl[off++] = (processing_delay_us >> 8) & 0xFF; |
| 269 |
ctrl[off++] = processing_delay_us & 0xFF; |
| 270 |
|
| 271 |
uint32_t seq = ctx->adv_seq_unprotected++; |
| 272 |
uint64_t ntp = timestampNTP_u64(); |
| 273 |
uint32_t ts = timestampRTP_u32(1, ntp); |
| 274 |
uint32_t ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base); |
| 275 |
|
| 276 |
int total = rist_adv_build_control(pkt, seq, ts, ssrc, ctrl, off); |
| 277 |
if (total < 0) |
| 278 |
return -1; |
| 279 |
|
| 280 |
ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0); |
| 281 |
if (ret < 0) |
| 282 |
_librist_log_send_error(peer, errno, (size_t)total, "adv-rtt-echo-resp sendto"); |
| 283 |
|
| 284 |
return (ret > 0) ? 0 : -1; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
#define RIST_ADV_KEEPALIVE_CAP_I (1u << 31) |
| 299 |
#define RIST_ADV_KEEPALIVE_CAP_G (1u << 30) |
| 300 |
#define RIST_ADV_KEEPALIVE_CAP_C (1u << 29) |
| 301 |
|
| 302 |
int rist_adv_send_keepalive(struct rist_peer *peer) |
| 303 |
{ |
| 304 |
struct rist_common_ctx *ctx = get_cctx(peer); |
| 305 |
uint8_t pkt[RIST_MAX_PACKET_SIZE]; |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
uint8_t ctrl[4 + 6 + 4]; |
| 316 |
size_t off = 0; |
| 317 |
|
| 318 |
ctrl[off++] = (RIST_ADV_CI_KEEPALIVE >> 8) & 0xFF; |
| 319 |
ctrl[off++] = RIST_ADV_CI_KEEPALIVE & 0xFF; |
| 320 |
uint16_t body_len = 10; |
| 321 |
ctrl[off++] = (body_len >> 8) & 0xFF; |
| 322 |
ctrl[off++] = body_len & 0xFF; |
| 323 |
|
| 324 |
|
| 325 |
memcpy(&ctrl[off], peer->mac_addr, 6); |
| 326 |
off += 6; |
| 327 |
|
| 328 |
|
| 329 |
uint32_t caps = RIST_ADV_KEEPALIVE_CAP_I; |
| 330 |
ctrl[off++] = (caps >> 24) & 0xFF; |
| 331 |
ctrl[off++] = (caps >> 16) & 0xFF; |
| 332 |
ctrl[off++] = (caps >> 8) & 0xFF; |
| 333 |
ctrl[off++] = caps & 0xFF; |
| 334 |
|
| 335 |
uint32_t seq = ctx->adv_seq_unprotected++; |
| 336 |
uint64_t ntp = timestampNTP_u64(); |
| 337 |
uint32_t ts = timestampRTP_u32(1, ntp); |
| 338 |
uint32_t ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base); |
| 339 |
|
| 340 |
int total = rist_adv_build_control(pkt, seq, ts, ssrc, ctrl, off); |
| 341 |
if (total < 0) |
| 342 |
return -1; |
| 343 |
|
| 344 |
ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0); |
| 345 |
if (ret < 0) |
| 346 |
_librist_log_send_error(peer, errno, (size_t)total, "adv-keepalive sendto"); |
| 347 |
|
| 348 |
return (ret > 0) ? 0 : -1; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
int rist_adv_send_unsupported(struct rist_peer *peer, |
| 361 |
uint16_t incoming_ci, |
| 362 |
const uint8_t *incoming_payload, |
| 363 |
size_t incoming_len) |
| 364 |
{ |
| 365 |
struct rist_common_ctx *ctx = get_cctx(peer); |
| 366 |
uint8_t pkt[RIST_MAX_PACKET_SIZE]; |
| 367 |
uint8_t ctrl[4 + 16]; |
| 368 |
size_t off = 0; |
| 369 |
|
| 370 |
ctrl[off++] = (RIST_ADV_CI_UNSUPPORTED >> 8) & 0xFF; |
| 371 |
ctrl[off++] = RIST_ADV_CI_UNSUPPORTED & 0xFF; |
| 372 |
uint16_t body_len = 12; |
| 373 |
ctrl[off++] = (body_len >> 8) & 0xFF; |
| 374 |
ctrl[off++] = body_len & 0xFF; |
| 375 |
|
| 376 |
uint32_t resp_ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base); |
| 377 |
ctrl[off++] = (resp_ssrc >> 24) & 0xFF; |
| 378 |
ctrl[off++] = (resp_ssrc >> 16) & 0xFF; |
| 379 |
ctrl[off++] = (resp_ssrc >> 8) & 0xFF; |
| 380 |
ctrl[off++] = resp_ssrc & 0xFF; |
| 381 |
|
| 382 |
ctrl[off++] = (incoming_ci >> 8) & 0xFF; |
| 383 |
ctrl[off++] = incoming_ci & 0xFF; |
| 384 |
ctrl[off++] = 0; |
| 385 |
ctrl[off++] = 0; |
| 386 |
|
| 387 |
|
| 388 |
size_t copy_len = incoming_len < 6 ? incoming_len : 6; |
| 389 |
if (incoming_payload && copy_len > 0) |
| 390 |
memcpy(&ctrl[off], incoming_payload, copy_len); |
| 391 |
if (copy_len < 6) |
| 392 |
memset(&ctrl[off + copy_len], 0, 6 - copy_len); |
| 393 |
off += 6; |
| 394 |
|
| 395 |
ctrl[off++] = 0; |
| 396 |
ctrl[off++] = 0; |
| 397 |
|
| 398 |
uint32_t seq = ctx->adv_seq_unprotected++; |
| 399 |
uint64_t ntp = timestampNTP_u64(); |
| 400 |
uint32_t ts = timestampRTP_u32(1, ntp); |
| 401 |
|
| 402 |
int total = rist_adv_build_control(pkt, seq, ts, resp_ssrc, ctrl, off); |
| 403 |
if (total < 0) |
| 404 |
return -1; |
| 405 |
|
| 406 |
ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0); |
| 407 |
if (ret < 0) |
| 408 |
_librist_log_send_error(peer, errno, (size_t)total, "adv-unsupported sendto"); |
| 409 |
|
| 410 |
return (ret > 0) ? 0 : -1; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
8) |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
int rist_adv_send_type8(struct rist_peer *peer, |
| 423 |
const uint8_t *gre_packet, size_t gre_len) |
| 424 |
{ |
| 425 |
struct rist_common_ctx *ctx = get_cctx(peer); |
| 426 |
uint8_t pkt[RIST_MAX_PACKET_SIZE + RIST_ADV_MAX_FIXED_HEADER]; |
| 427 |
|
| 428 |
if (gre_len > RIST_MAX_PACKET_SIZE) |
| 429 |
return -1; |
| 430 |
|
| 431 |
struct rist_adv_params params; |
| 432 |
memset(¶ms, 0, sizeof(params)); |
| 433 |
params.seq = ctx->adv_seq_unprotected++; |
| 434 |
params.timestamp = timestampRTP_u32(1, timestampNTP_u64()); |
| 435 |
params.ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base); |
| 436 |
params.enc_type = RIST_ADV_TYPE_GRE_MAIN; |
| 437 |
params.psk_mode = RIST_ADV_PSK_NONE; |
| 438 |
params.lpc_mode = RIST_ADV_LPC_NONE; |
| 439 |
params.first_frag = true; |
| 440 |
params.last_frag = true; |
| 441 |
params.expedite = true; |
| 442 |
params.retransmit = false; |
| 443 |
|
| 444 |
int total = rist_adv_build(pkt, ¶ms, gre_packet, gre_len); |
| 445 |
if (total < 0) |
| 446 |
return -1; |
| 447 |
|
| 448 |
ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0); |
| 449 |
if (ret < 0) |
| 450 |
_librist_log_send_error(peer, errno, (size_t)total, "adv-type8 sendto"); |
| 451 |
|
| 452 |
return (ret > 0) ? 0 : -1; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
int rist_adv_send_psk_nonce(struct rist_peer *peer, |
| 468 |
const uint8_t nonce[4], uint16_t key_size_bits) |
| 469 |
{ |
| 470 |
struct rist_common_ctx *ctx = get_cctx(peer); |
| 471 |
uint8_t pkt[RIST_MAX_PACKET_SIZE]; |
| 472 |
uint8_t ctrl[4 + 8]; |
| 473 |
size_t off = 0; |
| 474 |
|
| 475 |
ctrl[off++] = (RIST_ADV_CI_PSK_NONCE >> 8) & 0xFF; |
| 476 |
ctrl[off++] = RIST_ADV_CI_PSK_NONCE & 0xFF; |
| 477 |
uint16_t body_len = 8; |
| 478 |
ctrl[off++] = (body_len >> 8) & 0xFF; |
| 479 |
ctrl[off++] = body_len & 0xFF; |
| 480 |
|
| 481 |
memcpy(&ctrl[off], nonce, 4); |
| 482 |
off += 4; |
| 483 |
|
| 484 |
ctrl[off++] = (key_size_bits >> 8) & 0xFF; |
| 485 |
ctrl[off++] = key_size_bits & 0xFF; |
| 486 |
ctrl[off++] = 0; |
| 487 |
ctrl[off++] = 0; |
| 488 |
|
| 489 |
uint32_t seq = ctx->adv_seq_unprotected++; |
| 490 |
uint64_t ntp = timestampNTP_u64(); |
| 491 |
uint32_t ts = timestampRTP_u32(1, ntp); |
| 492 |
uint32_t ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base); |
| 493 |
|
| 494 |
int total = rist_adv_build_control(pkt, seq, ts, ssrc, ctrl, off); |
| 495 |
if (total < 0) |
| 496 |
return -1; |
| 497 |
|
| 498 |
ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0); |
| 499 |
if (ret < 0) |
| 500 |
_librist_log_send_error(peer, errno, (size_t)total, "adv-psk-nonce sendto"); |
| 501 |
|
| 502 |
return (ret > 0) ? 0 : -1; |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
int rist_adv_send_flow_attr(struct rist_peer *peer) |
| 518 |
{ |
| 519 |
struct rist_common_ctx *ctx = get_cctx(peer); |
| 520 |
uint8_t pkt[RIST_MAX_PACKET_SIZE]; |
| 521 |
|
| 522 |
cJSON *root = cJSON_CreateObject(); |
| 523 |
if (!root) |
| 524 |
return -1; |
| 525 |
|
| 526 |
cJSON_AddStringToObject(root, "session", peer->cname); |
| 527 |
cJSON_AddNumberToObject(root, "virt_dst_port", peer->config.virt_dst_port); |
| 528 |
cJSON_AddStringToObject(root, "profile", "advanced"); |
| 529 |
|
| 530 |
uint16_t outer = peer->config.virt_dst_port; |
| 531 |
uint16_t inner = 0; |
| 532 |
cJSON_AddNumberToObject(root, "flow_id", outer); |
| 533 |
cJSON_AddNumberToObject(root, "flow_inner", inner); |
| 534 |
|
| 535 |
char *json = cJSON_PrintUnformatted(root); |
| 536 |
cJSON_Delete(root); |
| 537 |
if (!json) |
| 538 |
return -1; |
| 539 |
|
| 540 |
size_t json_len = strlen(json); |
| 541 |
if (json_len > RIST_MAX_PACKET_SIZE - RIST_ADV_MAX_FIXED_HEADER - 4) { |
| 542 |
free(json); |
| 543 |
return -1; |
| 544 |
} |
| 545 |
|
| 546 |
uint8_t ctrl[4 + RIST_MAX_PACKET_SIZE]; |
| 547 |
size_t off = 0; |
| 548 |
|
| 549 |
ctrl[off++] = (RIST_ADV_CI_FLOW_ATTR >> 8) & 0xFF; |
| 550 |
ctrl[off++] = RIST_ADV_CI_FLOW_ATTR & 0xFF; |
| 551 |
uint16_t body_len = (uint16_t)json_len; |
| 552 |
ctrl[off++] = (body_len >> 8) & 0xFF; |
| 553 |
ctrl[off++] = body_len & 0xFF; |
| 554 |
|
| 555 |
memcpy(&ctrl[off], json, json_len); |
| 556 |
off += json_len; |
| 557 |
free(json); |
| 558 |
|
| 559 |
uint32_t seq = ctx->adv_seq_unprotected++; |
| 560 |
uint64_t ntp = timestampNTP_u64(); |
| 561 |
uint32_t ts = timestampRTP_u32(1, ntp); |
| 562 |
uint32_t ssrc = rist_adv_ssrc_unprotected(ctx->adv_ssrc_base); |
| 563 |
|
| 564 |
int total = rist_adv_build_control(pkt, seq, ts, ssrc, ctrl, off); |
| 565 |
if (total < 0) |
| 566 |
return -1; |
| 567 |
|
| 568 |
ssize_t ret = rist_transport_sendto(peer, pkt, (size_t)total, 0); |
| 569 |
if (ret < 0) |
| 570 |
_librist_log_send_error(peer, errno, (size_t)total, "adv-flow-attr sendto"); |
| 571 |
|
| 572 |
return (ret > 0) ? 0 : -1; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
int rist_adv_recv_control(struct rist_peer *peer, |
| 583 |
const uint8_t *ctrl_payload, |
| 584 |
size_t ctrl_len) |
| 585 |
{ |
| 586 |
struct rist_common_ctx *ctx = get_cctx(peer); |
| 587 |
|
| 588 |
if (ctrl_len < RIST_ADV_CTRL_HDR_SIZE) |
| 589 |
return -1; |
| 590 |
|
| 591 |
uint16_t ci = (uint16_t)((ctrl_payload[0] << 8) | ctrl_payload[1]); |
| 592 |
uint16_t body_len = (uint16_t)((ctrl_payload[2] << 8) | ctrl_payload[3]); |
| 593 |
|
| 594 |
if ((size_t)4 + body_len > ctrl_len) |
| 595 |
return -1; |
| 596 |
|
| 597 |
const uint8_t *body = ctrl_payload + 4; |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
switch (ci) { |
| 605 |
case RIST_ADV_CI_NACK_BITMASK: |
| 606 |
case RIST_ADV_CI_NACK_RANGE: |
| 607 |
case RIST_ADV_CI_RTT_ECHO_RESP: |
| 608 |
case RIST_ADV_CI_FLOW_ATTR: |
| 609 |
case RIST_ADV_CI_PSK_NONCE: |
| 610 |
if (!peer->authenticated) |
| 611 |
return 0; |
| 612 |
break; |
| 613 |
default: |
| 614 |
break; |
| 615 |
} |
| 616 |
|
| 617 |
switch (ci) { |
| 618 |
case RIST_ADV_CI_NACK_BITMASK: { |
| 619 |
if (body_len < 12) |
| 620 |
return -1; |
| 621 |
uint32_t media_ssrc = (uint32_t)body[0] << 24 | body[1] << 16 | body[2] << 8 | body[3]; |
| 622 |
uint32_t pss = (uint32_t)body[4] << 24 | body[5] << 16 | body[6] << 8 | body[7]; |
| 623 |
uint32_t blp = (uint32_t)body[8] << 24 | body[9] << 16 | body[10] << 8 | body[11]; |
| 624 |
(void)media_ssrc; |
| 625 |
rist_log_priv(ctx, RIST_LOG_DEBUG, |
| 626 |
"Advanced NACK Bitmask received: PSS=%u BLP=0x%08x\n", pss, blp); |
| 627 |
|
| 628 |
|
| 629 |
if (peer->sender_ctx) { |
| 630 |
struct rist_sender *sender = peer->sender_ctx; |
| 631 |
rist_retry_enqueue(sender, pss, peer); |
| 632 |
for (int i = 0; i < 32; i++) { |
| 633 |
if (blp & (1u << i)) |
| 634 |
rist_retry_enqueue(sender, pss + 1 + (uint32_t)i, peer); |
| 635 |
} |
| 636 |
} |
| 637 |
return 0; |
| 638 |
} |
| 639 |
|
| 640 |
case RIST_ADV_CI_NACK_RANGE: { |
| 641 |
if (body_len < 12) |
| 642 |
return -1; |
| 643 |
uint32_t media_ssrc = (uint32_t)body[0] << 24 | body[1] << 16 | body[2] << 8 | body[3]; |
| 644 |
uint32_t pss = (uint32_t)body[4] << 24 | body[5] << 16 | body[6] << 8 | body[7]; |
| 645 |
uint32_t nalp = (uint32_t)body[8] << 24 | body[9] << 16 | body[10] << 8 | body[11]; |
| 646 |
(void)media_ssrc; |
| 647 |
rist_log_priv(ctx, RIST_LOG_DEBUG, |
| 648 |
"Advanced NACK Range received: PSS=%u NALP=%u\n", pss, nalp); |
| 649 |
|
| 650 |
if (peer->sender_ctx) { |
| 651 |
struct rist_sender *sender = peer->sender_ctx; |
| 652 |
for (uint32_t i = 0; i <= nalp && i < 10000; i++) |
| 653 |
rist_retry_enqueue(sender, pss + i, peer); |
| 654 |
} |
| 655 |
return 0; |
| 656 |
} |
| 657 |
|
| 658 |
case RIST_ADV_CI_RTT_ECHO_REQ: { |
| 659 |
if (body_len < 16) |
| 660 |
return -1; |
| 661 |
uint32_t req_ssrc = (uint32_t)body[0] << 24 | body[1] << 16 | body[2] << 8 | body[3]; |
| 662 |
uint32_t orig_msw = (uint32_t)body[4] << 24 | body[5] << 16 | body[6] << 8 | body[7]; |
| 663 |
uint32_t orig_lsw = (uint32_t)body[8] << 24 | body[9] << 16 | body[10] << 8 | body[11]; |
| 664 |
|
| 665 |
rist_log_priv(ctx, RIST_LOG_DEBUG, "Advanced RTT Echo Request from SSRC %u\n", req_ssrc); |
| 666 |
return rist_adv_send_rtt_echo_response(peer, req_ssrc, orig_msw, orig_lsw, 0); |
| 667 |
} |
| 668 |
|
| 669 |
case RIST_ADV_CI_RTT_ECHO_RESP: { |
| 670 |
if (body_len < 16) |
| 671 |
return -1; |
| 672 |
uint32_t req_ssrc = (uint32_t)body[0] << 24 | body[1] << 16 | body[2] << 8 | body[3]; |
| 673 |
uint32_t orig_msw = (uint32_t)body[4] << 24 | body[5] << 16 | body[6] << 8 | body[7]; |
| 674 |
uint32_t orig_lsw = (uint32_t)body[8] << 24 | body[9] << 16 | body[10] << 8 | body[11]; |
| 675 |
uint32_t proc_delay = (uint32_t)body[12] << 24 | body[13] << 16 | body[14] << 8 | body[15]; |
| 676 |
(void)req_ssrc; |
| 677 |
|
| 678 |
uint64_t orig_ntp = ((uint64_t)orig_msw << 32) | orig_lsw; |
| 679 |
rist_peer_rtt_update(peer, |
| 680 |
calculate_rtt_delay(orig_ntp, timestampNTP_u64(), proc_delay)); |
| 681 |
rist_log_priv(ctx, RIST_LOG_DEBUG, |
| 682 |
"Advanced RTT Echo Response: RTT=%"PRIu64" us\n", |
| 683 |
(peer->last_rtt * 1000000ULL) >> 32); |
| 684 |
return 0; |
| 685 |
} |
| 686 |
|
| 687 |
case RIST_ADV_CI_KEEPALIVE: { |
| 688 |
if (body_len < 10) |
| 689 |
return -1; |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
uint32_t caps = (uint32_t)body[6] << 24 | body[7] << 16 | body[8] << 8 | body[9]; |
| 694 |
if (caps & RIST_ADV_KEEPALIVE_CAP_I) |
| 695 |
peer->remote_supports_advanced = true; |
| 696 |
peer->last_pkt_received = timestampNTP_u64(); |
| 697 |
rist_log_priv(ctx, RIST_LOG_DEBUG, |
| 698 |
"Advanced Keep-Alive: caps=0x%08x (I=%d)\n", |
| 699 |
caps, peer->remote_supports_advanced); |
| 700 |
return 0; |
| 701 |
} |
| 702 |
|
| 703 |
case RIST_ADV_CI_FLOW_ATTR: { |
| 704 |
if (body_len == 0) |
| 705 |
return 0; |
| 706 |
rist_log_priv(ctx, RIST_LOG_DEBUG, |
| 707 |
"Advanced Flow Attribute received (%u bytes)\n", body_len); |
| 708 |
if (peer->receiver_ctx) { |
| 709 |
struct rist_receiver *rcv = peer->receiver_ctx; |
| 710 |
if (rcv->receiver_flow_attr_callback) { |
| 711 |
char json_buf[RIST_MAX_PACKET_SIZE]; |
| 712 |
size_t copy_len = body_len < sizeof(json_buf) - 1 ? body_len : sizeof(json_buf) - 1; |
| 713 |
memcpy(json_buf, body, copy_len); |
| 714 |
json_buf[copy_len] = '\0'; |
| 715 |
rcv->receiver_flow_attr_callback( |
| 716 |
rcv->receiver_flow_attr_callback_argument, |
| 717 |
peer, json_buf, copy_len); |
| 718 |
} |
| 719 |
} |
| 720 |
return 0; |
| 721 |
} |
| 722 |
|
| 723 |
case RIST_ADV_CI_PSK_NONCE: { |
| 724 |
if (body_len < 8) |
| 725 |
return -1; |
| 726 |
uint8_t future_nonce[4]; |
| 727 |
memcpy(future_nonce, body, 4); |
| 728 |
uint16_t key_bits = (uint16_t)((body[4] << 8) | body[5]); |
| 729 |
if (key_bits != 0 && key_bits != 128 && key_bits != 192 && key_bits != 256) |
| 730 |
return -1; |
| 731 |
|
| 732 |
bool odd = CHECK_BIT(future_nonce[0], 7); |
| 733 |
struct rist_key *ak = odd ? &peer->key_rx_odd : &peer->key_rx; |
| 734 |
if (ak->password_len > 0) { |
| 735 |
pthread_mutex_lock(&peer->peer_lock); |
| 736 |
_librist_crypto_psk_preannounce_nonce(ak, future_nonce, key_bits); |
| 737 |
pthread_mutex_unlock(&peer->peer_lock); |
| 738 |
} |
| 739 |
|
| 740 |
rist_log_priv(ctx, RIST_LOG_DEBUG, |
| 741 |
"Advanced PSK Future Nonce: nonce=0x%02x%02x%02x%02x key_bits=%u\n", |
| 742 |
future_nonce[0], future_nonce[1], future_nonce[2], future_nonce[3], |
| 743 |
key_bits); |
| 744 |
return 0; |
| 745 |
} |
| 746 |
|
| 747 |
case RIST_ADV_CI_UNSUPPORTED: { |
| 748 |
if (body_len < 8) |
| 749 |
return -1; |
| 750 |
uint16_t unsup_ci = (uint16_t)((body[4] << 8) | body[5]); |
| 751 |
rist_log_priv(ctx, RIST_LOG_WARN, |
| 752 |
"Remote does not support control message CI=0x%04x\n", unsup_ci); |
| 753 |
return 0; |
| 754 |
} |
| 755 |
|
| 756 |
default: |
| 757 |
rist_log_priv(ctx, RIST_LOG_DEBUG, |
| 758 |
"Unknown Advanced Profile control CI=0x%04x, sending unsupported response\n", ci); |
| 759 |
rist_adv_send_unsupported(peer, ci, body, body_len); |
| 760 |
return -1; |
| 761 |
} |
| 762 |
} |