James Kuszmaul | 4a42b18 | 2021-01-17 11:32:46 -0800 | [diff] [blame^] | 1 | #include "helper/handler.h" |
| 2 | #include "helper/parameters.h" |
| 3 | #include "helper/utils.h" |
| 4 | #include <rawrtc.h> |
| 5 | #include <rawrtcc.h> |
| 6 | #include <rawrtcdc.h> |
| 7 | #include <re.h> |
| 8 | #include <stdlib.h> // exit |
| 9 | #include <string.h> // memcpy |
| 10 | #include <unistd.h> // STDIN_FILENO |
| 11 | |
| 12 | #define DEBUG_MODULE "data-channel-sctp-throughput-app" |
| 13 | #define DEBUG_LEVEL 7 |
| 14 | #include <re_dbg.h> |
| 15 | |
| 16 | enum { |
| 17 | TRANSPORT_BUFFER_LENGTH = 1048576, // 1 MiB |
| 18 | }; |
| 19 | |
| 20 | struct parameters { |
| 21 | struct rawrtc_ice_parameters* ice_parameters; |
| 22 | struct rawrtc_ice_candidates* ice_candidates; |
| 23 | struct rawrtc_dtls_parameters* dtls_parameters; |
| 24 | struct sctp_parameters sctp_parameters; |
| 25 | }; |
| 26 | |
| 27 | // Note: Shadows struct client |
| 28 | struct data_channel_sctp_throughput_client { |
| 29 | char* name; |
| 30 | char** ice_candidate_types; |
| 31 | size_t n_ice_candidate_types; |
| 32 | uint64_t message_size; |
| 33 | uint16_t n_times_left; |
| 34 | uint32_t buffer_length; |
| 35 | enum rawrtc_sctp_transport_congestion_ctrl congestion_ctrl_algorithm; |
| 36 | uint32_t mtu; |
| 37 | struct rawrtc_ice_gather_options* gather_options; |
| 38 | enum rawrtc_ice_role role; |
| 39 | struct mbuf* start_buffer; |
| 40 | struct mbuf* throughput_buffer; |
| 41 | struct rawrtc_certificate* certificate; |
| 42 | struct rawrtc_ice_gatherer* gatherer; |
| 43 | struct rawrtc_ice_transport* ice_transport; |
| 44 | struct rawrtc_dtls_transport* dtls_transport; |
| 45 | struct rawrtc_sctp_transport* sctp_transport; |
| 46 | struct rawrtc_data_transport* data_transport; |
| 47 | struct data_channel_helper* data_channel; |
| 48 | struct parameters local_parameters; |
| 49 | struct parameters remote_parameters; |
| 50 | uint64_t start_time; |
| 51 | }; |
| 52 | |
| 53 | static void print_local_parameters(struct data_channel_sctp_throughput_client* client); |
| 54 | |
| 55 | static struct tmr timer = {0}; |
| 56 | |
| 57 | static void timer_handler(void* arg) { |
| 58 | struct data_channel_helper* const channel = arg; |
| 59 | struct data_channel_sctp_throughput_client* const client = |
| 60 | (struct data_channel_sctp_throughput_client*) channel->client; |
| 61 | enum rawrtc_code error; |
| 62 | enum rawrtc_dtls_role role; |
| 63 | |
| 64 | // Send start indicator |
| 65 | mbuf_set_pos(client->start_buffer, 0); |
| 66 | DEBUG_PRINTF("(%s) Sending start indicator\n", client->name); |
| 67 | error = rawrtc_data_channel_send(channel->channel, client->start_buffer, false); |
| 68 | if (error) { |
| 69 | DEBUG_WARNING("Could not send, reason: %s\n", rawrtc_code_to_str(error)); |
| 70 | goto out; |
| 71 | } |
| 72 | |
| 73 | // Send message |
| 74 | DEBUG_PRINTF( |
| 75 | "(%s) Sending %zu bytes\n", client->name, mbuf_get_left(client->throughput_buffer)); |
| 76 | error = rawrtc_data_channel_send(channel->channel, client->throughput_buffer, true); |
| 77 | if (error) { |
| 78 | DEBUG_WARNING("Could not send, reason: %s\n", rawrtc_code_to_str(error)); |
| 79 | goto out; |
| 80 | } |
| 81 | |
| 82 | out: |
| 83 | // Get DTLS role |
| 84 | EOE(rawrtc_dtls_parameters_get_role(&role, client->local_parameters.dtls_parameters)); |
| 85 | if (role == RAWRTC_DTLS_ROLE_CLIENT) { |
| 86 | // Close bear-noises |
| 87 | DEBUG_PRINTF("(%s) Closing channel\n", client->name, channel->label); |
| 88 | EOR(rawrtc_data_channel_close(client->data_channel->channel)); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static void data_channel_message_handler( |
| 93 | struct mbuf* const buffer, enum rawrtc_data_channel_message_flag const flags, void* const arg) { |
| 94 | struct data_channel_helper* const channel = arg; |
| 95 | struct data_channel_sctp_throughput_client* const client = |
| 96 | (struct data_channel_sctp_throughput_client*) channel->client; |
| 97 | size_t const length = mbuf_get_left(buffer); |
| 98 | |
| 99 | // Check role |
| 100 | if (client->role != RAWRTC_ICE_ROLE_CONTROLLED) { |
| 101 | DEBUG_WARNING( |
| 102 | "(%s) Unexpected message on data channel %s of size %zu\n", client->name, |
| 103 | channel->label, length); |
| 104 | } |
| 105 | |
| 106 | if (flags & RAWRTC_DATA_CHANNEL_MESSAGE_FLAG_IS_STRING) { |
| 107 | // Start indicator message |
| 108 | uint64_t expected_size; |
| 109 | |
| 110 | // Check size |
| 111 | if (mbuf_get_left(buffer) < 8) { |
| 112 | EOE(RAWRTC_CODE_INVALID_MESSAGE); |
| 113 | } |
| 114 | |
| 115 | // Parse message |
| 116 | expected_size = sys_ntohll(mbuf_read_u64(buffer)); |
| 117 | EOE(expected_size > 0 ? RAWRTC_CODE_SUCCESS : RAWRTC_CODE_INVALID_MESSAGE); |
| 118 | client->start_time = tmr_jiffies(); |
| 119 | DEBUG_INFO( |
| 120 | "(%s) Started throughput test of %.2f MiB\n", client->name, |
| 121 | ((double) expected_size) / 1048576); |
| 122 | return; |
| 123 | } else if (flags & RAWRTC_DATA_CHANNEL_MESSAGE_FLAG_IS_BINARY) { |
| 124 | // Check expected message size and print results |
| 125 | double const delta = ((double) (tmr_jiffies() - client->start_time)) / 1000; |
| 126 | DEBUG_INFO( |
| 127 | "(%s) Completed throughput test after %.2f seconds: %.2f Mbit/s\n", client->name, delta, |
| 128 | ((double) length) / 131072 / delta); |
| 129 | |
| 130 | // Check size |
| 131 | if (length != client->message_size) { |
| 132 | DEBUG_WARNING( |
| 133 | "(%s) Expected %zu bytes, received %zu bytes\n", client->name, client->message_size, |
| 134 | length); |
| 135 | return; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static void start_throughput_test(struct data_channel_helper* const channel) { |
| 141 | struct data_channel_sctp_throughput_client* const client = |
| 142 | (struct data_channel_sctp_throughput_client*) channel->client; |
| 143 | |
| 144 | // Start throughput test delayed (if controlling) |
| 145 | if (client->role == RAWRTC_ICE_ROLE_CONTROLLING && client->n_times_left > 0) { |
| 146 | size_t length; |
| 147 | mbuf_set_pos(client->throughput_buffer, 0); |
| 148 | length = mbuf_get_left(client->throughput_buffer); |
| 149 | DEBUG_INFO( |
| 150 | "Starting throughput test of %.2f MiB in 1 second\n", |
| 151 | (double) length / (double) 1048576); |
| 152 | tmr_start(&timer, 1000, timer_handler, channel); |
| 153 | --client->n_times_left; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static void data_channel_buffered_amount_low_handler(void* const arg) { |
| 158 | struct data_channel_helper* const channel = arg; |
| 159 | |
| 160 | // Print buffered amount low event |
| 161 | default_data_channel_buffered_amount_low_handler(arg); |
| 162 | |
| 163 | // Restart throughput test |
| 164 | start_throughput_test(channel); |
| 165 | } |
| 166 | |
| 167 | static void data_channel_open_handler(void* const arg) { |
| 168 | struct data_channel_helper* const channel = arg; |
| 169 | |
| 170 | // Print open event |
| 171 | default_data_channel_open_handler(arg); |
| 172 | |
| 173 | // Start throughput test |
| 174 | start_throughput_test(channel); |
| 175 | } |
| 176 | |
| 177 | static void ice_gatherer_local_candidate_handler( |
| 178 | struct rawrtc_ice_candidate* const candidate, |
| 179 | char const* const url, // read-only |
| 180 | void* const arg) { |
| 181 | struct data_channel_sctp_throughput_client* const client = arg; |
| 182 | |
| 183 | // Print local candidate |
| 184 | default_ice_gatherer_local_candidate_handler(candidate, url, arg); |
| 185 | |
| 186 | // Print local parameters (if last candidate) |
| 187 | if (!candidate) { |
| 188 | print_local_parameters(client); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static void client_init(struct data_channel_sctp_throughput_client* const client) { |
| 193 | struct rawrtc_certificate* certificates[1]; |
| 194 | struct rawrtc_data_channel_parameters* channel_parameters; |
| 195 | |
| 196 | // Generate certificates |
| 197 | EOE(rawrtc_certificate_generate(&client->certificate, NULL)); |
| 198 | certificates[0] = client->certificate; |
| 199 | |
| 200 | // Create ICE gatherer |
| 201 | EOE(rawrtc_ice_gatherer_create( |
| 202 | &client->gatherer, client->gather_options, default_ice_gatherer_state_change_handler, |
| 203 | default_ice_gatherer_error_handler, ice_gatherer_local_candidate_handler, client)); |
| 204 | |
| 205 | // Create ICE transport |
| 206 | EOE(rawrtc_ice_transport_create( |
| 207 | &client->ice_transport, client->gatherer, default_ice_transport_state_change_handler, |
| 208 | default_ice_transport_candidate_pair_change_handler, client)); |
| 209 | |
| 210 | // Create DTLS transport |
| 211 | EOE(rawrtc_dtls_transport_create( |
| 212 | &client->dtls_transport, client->ice_transport, certificates, ARRAY_SIZE(certificates), |
| 213 | default_dtls_transport_state_change_handler, default_dtls_transport_error_handler, client)); |
| 214 | |
| 215 | // Create SCTP transport |
| 216 | EOE(rawrtc_sctp_transport_create( |
| 217 | &client->sctp_transport, client->dtls_transport, |
| 218 | client->local_parameters.sctp_parameters.port, default_data_channel_handler, |
| 219 | default_sctp_transport_state_change_handler, client)); |
| 220 | EOE(rawrtc_sctp_transport_set_buffer_length( |
| 221 | client->sctp_transport, client->buffer_length, client->buffer_length)); |
| 222 | EOE(rawrtc_sctp_transport_set_congestion_ctrl_algorithm( |
| 223 | client->sctp_transport, client->congestion_ctrl_algorithm)); |
| 224 | |
| 225 | // Get data transport |
| 226 | EOE(rawrtc_sctp_transport_get_data_transport(&client->data_transport, client->sctp_transport)); |
| 227 | |
| 228 | // Create data channel helper |
| 229 | data_channel_helper_create(&client->data_channel, (struct client*) client, "throughput"); |
| 230 | |
| 231 | // Create data channel parameters |
| 232 | EOE(rawrtc_data_channel_parameters_create( |
| 233 | &channel_parameters, client->data_channel->label, RAWRTC_DATA_CHANNEL_TYPE_RELIABLE_ORDERED, |
| 234 | 0, NULL, true, 0)); |
| 235 | |
| 236 | // Create pre-negotiated data channel |
| 237 | EOE(rawrtc_data_channel_create( |
| 238 | &client->data_channel->channel, client->data_transport, channel_parameters, |
| 239 | data_channel_open_handler, data_channel_buffered_amount_low_handler, |
| 240 | default_data_channel_error_handler, default_data_channel_close_handler, |
| 241 | data_channel_message_handler, client->data_channel)); |
| 242 | |
| 243 | // Un-reference |
| 244 | mem_deref(channel_parameters); |
| 245 | } |
| 246 | |
| 247 | static void client_start_gathering(struct data_channel_sctp_throughput_client* const client) { |
| 248 | // Start gathering |
| 249 | EOE(rawrtc_ice_gatherer_gather(client->gatherer, NULL)); |
| 250 | } |
| 251 | |
| 252 | static void client_start_transports(struct data_channel_sctp_throughput_client* const client) { |
| 253 | struct parameters* const remote_parameters = &client->remote_parameters; |
| 254 | |
| 255 | // Start ICE transport |
| 256 | EOE(rawrtc_ice_transport_start( |
| 257 | client->ice_transport, client->gatherer, remote_parameters->ice_parameters, client->role)); |
| 258 | |
| 259 | // Start DTLS transport |
| 260 | EOE(rawrtc_dtls_transport_start(client->dtls_transport, remote_parameters->dtls_parameters)); |
| 261 | |
| 262 | // Start SCTP transport |
| 263 | EOE(rawrtc_sctp_transport_start( |
| 264 | client->sctp_transport, remote_parameters->sctp_parameters.capabilities, |
| 265 | remote_parameters->sctp_parameters.port)); |
| 266 | if (client->mtu != 0) { |
| 267 | EOE(rawrtc_sctp_transport_set_mtu(client->sctp_transport, client->mtu)); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | static void parameters_destroy(struct parameters* const parameters) { |
| 272 | // Un-reference |
| 273 | parameters->ice_parameters = mem_deref(parameters->ice_parameters); |
| 274 | parameters->ice_candidates = mem_deref(parameters->ice_candidates); |
| 275 | parameters->dtls_parameters = mem_deref(parameters->dtls_parameters); |
| 276 | if (parameters->sctp_parameters.capabilities) { |
| 277 | parameters->sctp_parameters.capabilities = |
| 278 | mem_deref(parameters->sctp_parameters.capabilities); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | static void client_stop(struct data_channel_sctp_throughput_client* const client) { |
| 283 | if (client->sctp_transport) { |
| 284 | EOE(rawrtc_sctp_transport_stop(client->sctp_transport)); |
| 285 | } |
| 286 | if (client->dtls_transport) { |
| 287 | EOE(rawrtc_dtls_transport_stop(client->dtls_transport)); |
| 288 | } |
| 289 | if (client->ice_transport) { |
| 290 | EOE(rawrtc_ice_transport_stop(client->ice_transport)); |
| 291 | } |
| 292 | if (client->gatherer) { |
| 293 | EOE(rawrtc_ice_gatherer_close(client->gatherer)); |
| 294 | } |
| 295 | |
| 296 | // Un-reference & close |
| 297 | parameters_destroy(&client->remote_parameters); |
| 298 | parameters_destroy(&client->local_parameters); |
| 299 | client->data_channel = mem_deref(client->data_channel); |
| 300 | client->data_transport = mem_deref(client->data_transport); |
| 301 | client->sctp_transport = mem_deref(client->sctp_transport); |
| 302 | client->dtls_transport = mem_deref(client->dtls_transport); |
| 303 | client->ice_transport = mem_deref(client->ice_transport); |
| 304 | client->gatherer = mem_deref(client->gatherer); |
| 305 | client->certificate = mem_deref(client->certificate); |
| 306 | client->throughput_buffer = mem_deref(client->throughput_buffer); |
| 307 | client->start_buffer = mem_deref(client->start_buffer); |
| 308 | client->gather_options = mem_deref(client->gather_options); |
| 309 | |
| 310 | // Stop listening on STDIN |
| 311 | fd_close(STDIN_FILENO); |
| 312 | } |
| 313 | |
| 314 | static void client_set_parameters(struct data_channel_sctp_throughput_client* const client) { |
| 315 | struct parameters* const remote_parameters = &client->remote_parameters; |
| 316 | |
| 317 | // Set remote ICE candidates |
| 318 | EOE(rawrtc_ice_transport_set_remote_candidates( |
| 319 | client->ice_transport, remote_parameters->ice_candidates->candidates, |
| 320 | remote_parameters->ice_candidates->n_candidates)); |
| 321 | } |
| 322 | |
| 323 | static void parse_remote_parameters(int flags, void* arg) { |
| 324 | struct data_channel_sctp_throughput_client* const client = arg; |
| 325 | enum rawrtc_code error; |
| 326 | struct odict* dict = NULL; |
| 327 | struct odict* node = NULL; |
| 328 | struct rawrtc_ice_parameters* ice_parameters = NULL; |
| 329 | struct rawrtc_ice_candidates* ice_candidates = NULL; |
| 330 | struct rawrtc_dtls_parameters* dtls_parameters = NULL; |
| 331 | struct sctp_parameters sctp_parameters = {0}; |
| 332 | (void) flags; |
| 333 | |
| 334 | // Get dict from JSON |
| 335 | error = get_json_stdin(&dict); |
| 336 | if (error) { |
| 337 | goto out; |
| 338 | } |
| 339 | |
| 340 | // Decode JSON |
| 341 | error |= dict_get_entry(&node, dict, "iceParameters", ODICT_OBJECT, true); |
| 342 | error |= get_ice_parameters(&ice_parameters, node); |
| 343 | error |= dict_get_entry(&node, dict, "iceCandidates", ODICT_ARRAY, true); |
| 344 | error |= get_ice_candidates(&ice_candidates, node, arg); |
| 345 | error |= dict_get_entry(&node, dict, "dtlsParameters", ODICT_OBJECT, true); |
| 346 | error |= get_dtls_parameters(&dtls_parameters, node); |
| 347 | error |= dict_get_entry(&node, dict, "sctpParameters", ODICT_OBJECT, true); |
| 348 | error |= get_sctp_parameters(&sctp_parameters, node); |
| 349 | |
| 350 | // Ok? |
| 351 | if (error) { |
| 352 | DEBUG_WARNING("Invalid remote parameters\n"); |
| 353 | if (sctp_parameters.capabilities) { |
| 354 | mem_deref(sctp_parameters.capabilities); |
| 355 | } |
| 356 | goto out; |
| 357 | } |
| 358 | |
| 359 | // Set parameters & start transports |
| 360 | client->remote_parameters.ice_parameters = mem_ref(ice_parameters); |
| 361 | client->remote_parameters.ice_candidates = mem_ref(ice_candidates); |
| 362 | client->remote_parameters.dtls_parameters = mem_ref(dtls_parameters); |
| 363 | memcpy(&client->remote_parameters.sctp_parameters, &sctp_parameters, sizeof(sctp_parameters)); |
| 364 | DEBUG_INFO("Applying remote parameters\n"); |
| 365 | client_set_parameters(client); |
| 366 | client_start_transports(client); |
| 367 | |
| 368 | out: |
| 369 | // Un-reference |
| 370 | mem_deref(dtls_parameters); |
| 371 | mem_deref(ice_candidates); |
| 372 | mem_deref(ice_parameters); |
| 373 | mem_deref(dict); |
| 374 | |
| 375 | // Exit? |
| 376 | if (error == RAWRTC_CODE_NO_VALUE) { |
| 377 | DEBUG_NOTICE("Exiting\n"); |
| 378 | |
| 379 | // Stop client & bye |
| 380 | client_stop(client); |
| 381 | tmr_cancel(&timer); |
| 382 | re_cancel(); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | static void client_get_parameters(struct data_channel_sctp_throughput_client* const client) { |
| 387 | struct parameters* const local_parameters = &client->local_parameters; |
| 388 | |
| 389 | // Get local ICE parameters |
| 390 | EOE(rawrtc_ice_gatherer_get_local_parameters( |
| 391 | &local_parameters->ice_parameters, client->gatherer)); |
| 392 | |
| 393 | // Get local ICE candidates |
| 394 | EOE(rawrtc_ice_gatherer_get_local_candidates( |
| 395 | &local_parameters->ice_candidates, client->gatherer)); |
| 396 | |
| 397 | // Get local DTLS parameters |
| 398 | EOE(rawrtc_dtls_transport_get_local_parameters( |
| 399 | &local_parameters->dtls_parameters, client->dtls_transport)); |
| 400 | |
| 401 | // Get local SCTP parameters |
| 402 | EOE(rawrtc_sctp_transport_get_capabilities(&local_parameters->sctp_parameters.capabilities)); |
| 403 | EOE(rawrtc_sctp_transport_get_port( |
| 404 | &local_parameters->sctp_parameters.port, client->sctp_transport)); |
| 405 | } |
| 406 | |
| 407 | static void print_local_parameters(struct data_channel_sctp_throughput_client* client) { |
| 408 | struct odict* dict; |
| 409 | struct odict* node; |
| 410 | |
| 411 | // Get local parameters |
| 412 | client_get_parameters(client); |
| 413 | |
| 414 | // Create dict |
| 415 | EOR(odict_alloc(&dict, 16)); |
| 416 | |
| 417 | // Create nodes |
| 418 | EOR(odict_alloc(&node, 16)); |
| 419 | set_ice_parameters(client->local_parameters.ice_parameters, node); |
| 420 | EOR(odict_entry_add(dict, "iceParameters", ODICT_OBJECT, node)); |
| 421 | mem_deref(node); |
| 422 | EOR(odict_alloc(&node, 16)); |
| 423 | set_ice_candidates(client->local_parameters.ice_candidates, node); |
| 424 | EOR(odict_entry_add(dict, "iceCandidates", ODICT_ARRAY, node)); |
| 425 | mem_deref(node); |
| 426 | EOR(odict_alloc(&node, 16)); |
| 427 | set_dtls_parameters(client->local_parameters.dtls_parameters, node); |
| 428 | EOR(odict_entry_add(dict, "dtlsParameters", ODICT_OBJECT, node)); |
| 429 | mem_deref(node); |
| 430 | EOR(odict_alloc(&node, 16)); |
| 431 | set_sctp_parameters(client->sctp_transport, &client->local_parameters.sctp_parameters, node); |
| 432 | EOR(odict_entry_add(dict, "sctpParameters", ODICT_OBJECT, node)); |
| 433 | mem_deref(node); |
| 434 | |
| 435 | // Print JSON |
| 436 | DEBUG_INFO("Local Parameters:\n%H\n", json_encode_odict, dict); |
| 437 | |
| 438 | // Un-reference |
| 439 | mem_deref(dict); |
| 440 | } |
| 441 | |
| 442 | static void exit_with_usage(char* program) { |
| 443 | DEBUG_WARNING( |
| 444 | "Usage: %s <0|1 (ice-role)> <message-size> [<n-times>] [<sctp-port>] " |
| 445 | "[<buffer-length>] [<cc-algorithm>] [<mtu>] [<ice-candidate-type> ...]\n", |
| 446 | program); |
| 447 | exit(1); |
| 448 | } |
| 449 | |
| 450 | int main(int argc, char* argv[argc + 1]) { |
| 451 | char** ice_candidate_types = NULL; |
| 452 | size_t n_ice_candidate_types = 0; |
| 453 | enum rawrtc_ice_role role; |
| 454 | struct rawrtc_ice_gather_options* gather_options; |
| 455 | struct data_channel_sctp_throughput_client client = {0}; |
| 456 | (void) client.ice_candidate_types; |
| 457 | (void) client.n_ice_candidate_types; |
| 458 | |
| 459 | // Debug |
| 460 | dbg_init(DBG_DEBUG, DBG_ALL); |
| 461 | DEBUG_PRINTF("Init\n"); |
| 462 | |
| 463 | // Initialise |
| 464 | EOE(rawrtc_init(true)); |
| 465 | |
| 466 | // Check arguments length |
| 467 | if (argc < 3) { |
| 468 | exit_with_usage(argv[0]); |
| 469 | } |
| 470 | |
| 471 | // Get ICE role |
| 472 | if (get_ice_role(&role, argv[1])) { |
| 473 | exit_with_usage(argv[0]); |
| 474 | } |
| 475 | |
| 476 | // Get message size |
| 477 | if (!str_to_uint64(&client.message_size, argv[2])) { |
| 478 | exit_with_usage(argv[0]); |
| 479 | } |
| 480 | |
| 481 | // Get number of times the test should run (optional) |
| 482 | client.n_times_left = 1; |
| 483 | if (argc >= 4 && !str_to_uint16(&client.n_times_left, argv[3])) { |
| 484 | exit_with_usage(argv[0]); |
| 485 | } |
| 486 | |
| 487 | // TODO: Add possibility to turn checksum generation/validation on or off |
| 488 | |
| 489 | // Get SCTP port (optional) |
| 490 | if (argc >= 5 && !str_to_uint16(&client.local_parameters.sctp_parameters.port, argv[4])) { |
| 491 | exit_with_usage(argv[0]); |
| 492 | } |
| 493 | |
| 494 | // Get send/receiver buffer length (optional) |
| 495 | client.buffer_length = TRANSPORT_BUFFER_LENGTH; |
| 496 | if (argc >= 6 && !str_to_uint32(&client.buffer_length, argv[5])) { |
| 497 | exit_with_usage(argv[0]); |
| 498 | } |
| 499 | |
| 500 | // Get congestion control algorithm (optional) |
| 501 | client.congestion_ctrl_algorithm = RAWRTC_SCTP_TRANSPORT_CONGESTION_CTRL_RFC2581; |
| 502 | if (argc >= 7 && get_congestion_control_algorithm(&client.congestion_ctrl_algorithm, argv[6])) { |
| 503 | exit_with_usage(argv[0]); |
| 504 | } |
| 505 | |
| 506 | // Get MTU (optional) |
| 507 | if (argc >= 8 && !str_to_uint32(&client.mtu, argv[7])) { |
| 508 | exit_with_usage(argv[0]); |
| 509 | } |
| 510 | |
| 511 | // Get enabled ICE candidate types to be added (optional) |
| 512 | if (argc >= 9) { |
| 513 | ice_candidate_types = &argv[8]; |
| 514 | n_ice_candidate_types = (size_t) argc - 8; |
| 515 | } |
| 516 | |
| 517 | // Create ICE gather options |
| 518 | EOE(rawrtc_ice_gather_options_create(&gather_options, RAWRTC_ICE_GATHER_POLICY_ALL)); |
| 519 | |
| 520 | // Set client fields |
| 521 | client.name = "A"; |
| 522 | client.ice_candidate_types = ice_candidate_types; |
| 523 | client.n_ice_candidate_types = n_ice_candidate_types; |
| 524 | client.gather_options = gather_options; |
| 525 | client.role = role; |
| 526 | |
| 527 | // Pre-generate messages (if 'controlling') |
| 528 | if (role == RAWRTC_ICE_ROLE_CONTROLLING) { |
| 529 | // Start indicator |
| 530 | client.start_buffer = mbuf_alloc(8); |
| 531 | EOE(client.start_buffer ? RAWRTC_CODE_SUCCESS : RAWRTC_CODE_NO_MEMORY); |
| 532 | EOR(mbuf_write_u64(client.start_buffer, sys_htonll(client.message_size))); |
| 533 | |
| 534 | // Throughput test buffer |
| 535 | client.throughput_buffer = mbuf_alloc(client.message_size); |
| 536 | EOE(client.throughput_buffer ? RAWRTC_CODE_SUCCESS : RAWRTC_CODE_NO_MEMORY); |
| 537 | EOR(mbuf_fill(client.throughput_buffer, 0x01, mbuf_get_space(client.throughput_buffer))); |
| 538 | } |
| 539 | |
| 540 | // Setup client |
| 541 | client_init(&client); |
| 542 | |
| 543 | // Start gathering |
| 544 | client_start_gathering(&client); |
| 545 | |
| 546 | // Listen on stdin |
| 547 | EOR(fd_listen(STDIN_FILENO, FD_READ, parse_remote_parameters, &client)); |
| 548 | |
| 549 | // Start main loop |
| 550 | EOR(re_main(default_signal_handler)); |
| 551 | |
| 552 | // Stop client & bye |
| 553 | client_stop(&client); |
| 554 | before_exit(); |
| 555 | return 0; |
| 556 | } |