James Kuszmaul | 4a42b18 | 2021-01-17 11:32:46 -0800 | [diff] [blame^] | 1 | #include "helper/handler.h" |
| 2 | #include "helper/utils.h" |
| 3 | #include <rawrtc.h> |
| 4 | #include <rawrtcc.h> |
| 5 | #include <rawrtcdc.h> |
| 6 | #include <re.h> |
| 7 | #include <stdlib.h> // exit |
| 8 | #include <unistd.h> // STDIN_FILENO |
| 9 | |
| 10 | #define DEBUG_MODULE "data-channel-sctp-loopback-app" |
| 11 | #define DEBUG_LEVEL 7 |
| 12 | #include <re_dbg.h> |
| 13 | |
| 14 | enum { |
| 15 | TRANSPORT_BUFFER_LENGTH = 1048576, // 1 MiB |
| 16 | }; |
| 17 | |
| 18 | // Note: Shadows struct client |
| 19 | struct data_channel_sctp_client { |
| 20 | char* name; |
| 21 | char** ice_candidate_types; |
| 22 | size_t n_ice_candidate_types; |
| 23 | struct rawrtc_ice_gather_options* gather_options; |
| 24 | struct rawrtc_ice_parameters* ice_parameters; |
| 25 | struct rawrtc_dtls_parameters* dtls_parameters; |
| 26 | struct rawrtc_sctp_capabilities* sctp_capabilities; |
| 27 | enum rawrtc_ice_role role; |
| 28 | struct rawrtc_certificate* certificate; |
| 29 | uint16_t sctp_port; |
| 30 | struct rawrtc_ice_gatherer* gatherer; |
| 31 | struct rawrtc_ice_transport* ice_transport; |
| 32 | struct rawrtc_dtls_transport* dtls_transport; |
| 33 | struct rawrtc_sctp_transport* sctp_transport; |
| 34 | struct rawrtc_data_transport* data_transport; |
| 35 | struct data_channel_helper* data_channel_negotiated; |
| 36 | struct data_channel_helper* data_channel; |
| 37 | struct data_channel_sctp_client* other_client; |
| 38 | }; |
| 39 | |
| 40 | static struct tmr timer = {0}; |
| 41 | |
| 42 | static void timer_handler(void* arg) { |
| 43 | struct data_channel_helper* const channel = arg; |
| 44 | struct data_channel_sctp_client* const client = |
| 45 | (struct data_channel_sctp_client*) channel->client; |
| 46 | struct mbuf* buffer; |
| 47 | enum rawrtc_code error; |
| 48 | enum rawrtc_dtls_role role; |
| 49 | |
| 50 | // Compose message (16 MiB) |
| 51 | buffer = mbuf_alloc(1 << 24); |
| 52 | EOE(buffer ? RAWRTC_CODE_SUCCESS : RAWRTC_CODE_NO_MEMORY); |
| 53 | EOR(mbuf_fill(buffer, 'M', mbuf_get_space(buffer))); |
| 54 | mbuf_set_pos(buffer, 0); |
| 55 | |
| 56 | // Send message |
| 57 | DEBUG_PRINTF("(%s) Sending %zu bytes\n", client->name, mbuf_get_left(buffer)); |
| 58 | error = rawrtc_data_channel_send(channel->channel, buffer, true); |
| 59 | if (error) { |
| 60 | DEBUG_WARNING("Could not send, reason: %s\n", rawrtc_code_to_str(error)); |
| 61 | } |
| 62 | mem_deref(buffer); |
| 63 | |
| 64 | // Get DTLS role |
| 65 | EOE(rawrtc_dtls_parameters_get_role(&role, client->dtls_parameters)); |
| 66 | if (role == RAWRTC_DTLS_ROLE_CLIENT) { |
| 67 | // Close bear-noises |
| 68 | DEBUG_PRINTF("(%s) Closing channel\n", client->name, channel->label); |
| 69 | EOR(rawrtc_data_channel_close(client->data_channel->channel)); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | static void data_channel_open_handler(void* const arg) { |
| 74 | struct data_channel_helper* const channel = arg; |
| 75 | struct data_channel_sctp_client* const client = |
| 76 | (struct data_channel_sctp_client*) channel->client; |
| 77 | struct mbuf* buffer; |
| 78 | enum rawrtc_code error; |
| 79 | |
| 80 | // Print open event |
| 81 | default_data_channel_open_handler(arg); |
| 82 | |
| 83 | // Send data delayed on bear-noises |
| 84 | if (str_cmp(channel->label, "bear-noises") == 0) { |
| 85 | tmr_start(&timer, 1000, timer_handler, channel); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | // Compose message (256 KiB) |
| 90 | buffer = mbuf_alloc(1 << 18); |
| 91 | EOE(buffer ? RAWRTC_CODE_SUCCESS : RAWRTC_CODE_NO_MEMORY); |
| 92 | EOR(mbuf_fill(buffer, 'M', mbuf_get_space(buffer))); |
| 93 | mbuf_set_pos(buffer, 0); |
| 94 | |
| 95 | // Send message |
| 96 | DEBUG_PRINTF("(%s) Sending %zu bytes\n", client->name, mbuf_get_left(buffer)); |
| 97 | error = rawrtc_data_channel_send(channel->channel, buffer, true); |
| 98 | if (error) { |
| 99 | DEBUG_WARNING("Could not send, reason: %s\n", rawrtc_code_to_str(error)); |
| 100 | } |
| 101 | mem_deref(buffer); |
| 102 | } |
| 103 | |
| 104 | static void ice_gatherer_local_candidate_handler( |
| 105 | struct rawrtc_ice_candidate* const candidate, |
| 106 | char const* const url, // read-only |
| 107 | void* const arg) { |
| 108 | struct data_channel_sctp_client* const client = arg; |
| 109 | |
| 110 | // Print local candidate |
| 111 | default_ice_gatherer_local_candidate_handler(candidate, url, arg); |
| 112 | |
| 113 | // Add to other client as remote candidate (if type enabled) |
| 114 | add_to_other_if_ice_candidate_type_enabled(arg, candidate, client->other_client->ice_transport); |
| 115 | } |
| 116 | |
| 117 | static void dtls_transport_state_change_handler( |
| 118 | enum rawrtc_dtls_transport_state const state, // read-only |
| 119 | void* const arg) { |
| 120 | struct data_channel_sctp_client* const client = arg; |
| 121 | |
| 122 | // Print state |
| 123 | default_dtls_transport_state_change_handler(state, arg); |
| 124 | |
| 125 | // Open? Create new data channel |
| 126 | // TODO: Move this once we can create data channels earlier |
| 127 | if (state == RAWRTC_DTLS_TRANSPORT_STATE_CONNECTED) { |
| 128 | enum rawrtc_dtls_role role; |
| 129 | |
| 130 | // Renew DTLS parameters |
| 131 | mem_deref(client->dtls_parameters); |
| 132 | EOE(rawrtc_dtls_transport_get_local_parameters( |
| 133 | &client->dtls_parameters, client->dtls_transport)); |
| 134 | |
| 135 | // Get DTLS role |
| 136 | EOE(rawrtc_dtls_parameters_get_role(&role, client->dtls_parameters)); |
| 137 | DEBUG_PRINTF("(%s) DTLS role: %s\n", client->name, rawrtc_dtls_role_to_str(role)); |
| 138 | |
| 139 | // Client? Create data channel |
| 140 | if (role == RAWRTC_DTLS_ROLE_CLIENT) { |
| 141 | struct rawrtc_data_channel_parameters* channel_parameters; |
| 142 | |
| 143 | // Create data channel helper |
| 144 | data_channel_helper_create( |
| 145 | &client->data_channel, (struct client*) client, "bear-noises"); |
| 146 | |
| 147 | // Create data channel parameters |
| 148 | EOE(rawrtc_data_channel_parameters_create( |
| 149 | &channel_parameters, client->data_channel->label, |
| 150 | RAWRTC_DATA_CHANNEL_TYPE_RELIABLE_UNORDERED, 0, NULL, false, 0)); |
| 151 | |
| 152 | // Create data channel |
| 153 | EOE(rawrtc_data_channel_create( |
| 154 | &client->data_channel->channel, client->data_transport, channel_parameters, |
| 155 | data_channel_open_handler, default_data_channel_buffered_amount_low_handler, |
| 156 | default_data_channel_error_handler, default_data_channel_close_handler, |
| 157 | default_data_channel_message_handler, client->data_channel)); |
| 158 | |
| 159 | // Un-reference |
| 160 | mem_deref(channel_parameters); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static void client_init(struct data_channel_sctp_client* const local) { |
| 166 | struct rawrtc_certificate* certificates[1]; |
| 167 | struct rawrtc_data_channel_parameters* channel_parameters; |
| 168 | |
| 169 | // Generate certificates |
| 170 | EOE(rawrtc_certificate_generate(&local->certificate, NULL)); |
| 171 | certificates[0] = local->certificate; |
| 172 | |
| 173 | // Create ICE gatherer |
| 174 | EOE(rawrtc_ice_gatherer_create( |
| 175 | &local->gatherer, local->gather_options, default_ice_gatherer_state_change_handler, |
| 176 | default_ice_gatherer_error_handler, ice_gatherer_local_candidate_handler, local)); |
| 177 | |
| 178 | // Create ICE transport |
| 179 | EOE(rawrtc_ice_transport_create( |
| 180 | &local->ice_transport, local->gatherer, default_ice_transport_state_change_handler, |
| 181 | default_ice_transport_candidate_pair_change_handler, local)); |
| 182 | |
| 183 | // Create DTLS transport |
| 184 | EOE(rawrtc_dtls_transport_create( |
| 185 | &local->dtls_transport, local->ice_transport, certificates, ARRAY_SIZE(certificates), |
| 186 | dtls_transport_state_change_handler, default_dtls_transport_error_handler, local)); |
| 187 | |
| 188 | // Create SCTP transport |
| 189 | EOE(rawrtc_sctp_transport_create( |
| 190 | &local->sctp_transport, local->dtls_transport, local->sctp_port, |
| 191 | default_data_channel_handler, default_sctp_transport_state_change_handler, local)); |
| 192 | EOE(rawrtc_sctp_transport_set_buffer_length( |
| 193 | local->sctp_transport, TRANSPORT_BUFFER_LENGTH, TRANSPORT_BUFFER_LENGTH)); |
| 194 | |
| 195 | // Get SCTP capabilities |
| 196 | EOE(rawrtc_sctp_transport_get_capabilities(&local->sctp_capabilities)); |
| 197 | |
| 198 | // Get data transport |
| 199 | EOE(rawrtc_sctp_transport_get_data_transport(&local->data_transport, local->sctp_transport)); |
| 200 | |
| 201 | // Create data channel helper |
| 202 | data_channel_helper_create( |
| 203 | &local->data_channel_negotiated, (struct client*) local, "cat-noises"); |
| 204 | |
| 205 | // Create data channel parameters |
| 206 | EOE(rawrtc_data_channel_parameters_create( |
| 207 | &channel_parameters, local->data_channel_negotiated->label, |
| 208 | RAWRTC_DATA_CHANNEL_TYPE_RELIABLE_ORDERED, 0, NULL, true, 0)); |
| 209 | |
| 210 | // Create pre-negotiated data channel |
| 211 | EOE(rawrtc_data_channel_create( |
| 212 | &local->data_channel_negotiated->channel, local->data_transport, channel_parameters, |
| 213 | data_channel_open_handler, default_data_channel_buffered_amount_low_handler, |
| 214 | default_data_channel_error_handler, default_data_channel_close_handler, |
| 215 | default_data_channel_message_handler, local->data_channel_negotiated)); |
| 216 | |
| 217 | // Un-reference |
| 218 | mem_deref(channel_parameters); |
| 219 | } |
| 220 | |
| 221 | static void client_start( |
| 222 | struct data_channel_sctp_client* const local, struct data_channel_sctp_client* const remote) { |
| 223 | // Get & set ICE parameters |
| 224 | EOE(rawrtc_ice_gatherer_get_local_parameters(&local->ice_parameters, remote->gatherer)); |
| 225 | |
| 226 | // Start gathering |
| 227 | EOE(rawrtc_ice_gatherer_gather(local->gatherer, NULL)); |
| 228 | |
| 229 | // Start ICE transport |
| 230 | EOE(rawrtc_ice_transport_start( |
| 231 | local->ice_transport, local->gatherer, local->ice_parameters, local->role)); |
| 232 | |
| 233 | // Get DTLS parameters |
| 234 | EOE(rawrtc_dtls_transport_get_local_parameters( |
| 235 | &remote->dtls_parameters, remote->dtls_transport)); |
| 236 | |
| 237 | // Start DTLS transport |
| 238 | EOE(rawrtc_dtls_transport_start(local->dtls_transport, remote->dtls_parameters)); |
| 239 | |
| 240 | // Start SCTP transport |
| 241 | EOE(rawrtc_sctp_transport_start( |
| 242 | local->sctp_transport, remote->sctp_capabilities, remote->sctp_port)); |
| 243 | } |
| 244 | |
| 245 | static void client_stop(struct data_channel_sctp_client* const client) { |
| 246 | // Stop transports & close gatherer |
| 247 | if (client->data_channel) { |
| 248 | EOE(rawrtc_data_channel_close(client->data_channel->channel)); |
| 249 | } |
| 250 | EOE(rawrtc_data_channel_close(client->data_channel_negotiated->channel)); |
| 251 | EOE(rawrtc_sctp_transport_stop(client->sctp_transport)); |
| 252 | EOE(rawrtc_dtls_transport_stop(client->dtls_transport)); |
| 253 | EOE(rawrtc_ice_transport_stop(client->ice_transport)); |
| 254 | EOE(rawrtc_ice_gatherer_close(client->gatherer)); |
| 255 | |
| 256 | // Un-reference & close |
| 257 | client->data_channel = mem_deref(client->data_channel); |
| 258 | client->data_channel_negotiated = mem_deref(client->data_channel_negotiated); |
| 259 | client->sctp_capabilities = mem_deref(client->sctp_capabilities); |
| 260 | client->dtls_parameters = mem_deref(client->dtls_parameters); |
| 261 | client->ice_parameters = mem_deref(client->ice_parameters); |
| 262 | client->data_transport = mem_deref(client->data_transport); |
| 263 | client->sctp_transport = mem_deref(client->sctp_transport); |
| 264 | client->dtls_transport = mem_deref(client->dtls_transport); |
| 265 | client->ice_transport = mem_deref(client->ice_transport); |
| 266 | client->gatherer = mem_deref(client->gatherer); |
| 267 | client->certificate = mem_deref(client->certificate); |
| 268 | } |
| 269 | |
| 270 | int main(int argc, char* argv[argc + 1]) { |
| 271 | char** ice_candidate_types = NULL; |
| 272 | size_t n_ice_candidate_types = 0; |
| 273 | struct rawrtc_ice_gather_options* gather_options; |
| 274 | char* const turn_zwuenf_org_urls[] = {"stun:turn.zwuenf.org"}; |
| 275 | struct data_channel_sctp_client a = {0}; |
| 276 | struct data_channel_sctp_client b = {0}; |
| 277 | (void) a.ice_candidate_types; |
| 278 | (void) a.n_ice_candidate_types; |
| 279 | (void) b.ice_candidate_types; |
| 280 | (void) b.n_ice_candidate_types; |
| 281 | |
| 282 | // Debug |
| 283 | dbg_init(DBG_DEBUG, DBG_ALL); |
| 284 | DEBUG_PRINTF("Init\n"); |
| 285 | |
| 286 | // Initialise |
| 287 | EOE(rawrtc_init(true)); |
| 288 | |
| 289 | // Get enabled ICE candidate types to be added (optional) |
| 290 | if (argc > 1) { |
| 291 | ice_candidate_types = &argv[1]; |
| 292 | n_ice_candidate_types = (size_t) argc - 1; |
| 293 | } |
| 294 | |
| 295 | // Create ICE gather options |
| 296 | EOE(rawrtc_ice_gather_options_create(&gather_options, RAWRTC_ICE_GATHER_POLICY_ALL)); |
| 297 | |
| 298 | // Add ICE servers to ICE gather options |
| 299 | EOE(rawrtc_ice_gather_options_add_server( |
| 300 | gather_options, turn_zwuenf_org_urls, ARRAY_SIZE(turn_zwuenf_org_urls), NULL, NULL, |
| 301 | RAWRTC_ICE_CREDENTIAL_TYPE_NONE)); |
| 302 | |
| 303 | // Setup client A |
| 304 | a.name = "A"; |
| 305 | a.ice_candidate_types = ice_candidate_types; |
| 306 | a.n_ice_candidate_types = n_ice_candidate_types; |
| 307 | a.gather_options = gather_options; |
| 308 | a.role = RAWRTC_ICE_ROLE_CONTROLLING; |
| 309 | a.sctp_port = 6000; |
| 310 | a.other_client = &b; |
| 311 | |
| 312 | // Setup client B |
| 313 | b.name = "B"; |
| 314 | b.ice_candidate_types = ice_candidate_types; |
| 315 | b.n_ice_candidate_types = n_ice_candidate_types; |
| 316 | b.gather_options = gather_options; |
| 317 | b.role = RAWRTC_ICE_ROLE_CONTROLLED; |
| 318 | b.sctp_port = 5000; |
| 319 | b.other_client = &a; |
| 320 | |
| 321 | // Initialise clients |
| 322 | client_init(&a); |
| 323 | client_init(&b); |
| 324 | |
| 325 | // Start clients |
| 326 | client_start(&a, &b); |
| 327 | client_start(&b, &a); |
| 328 | |
| 329 | // Listen on stdin |
| 330 | EOR(fd_listen(STDIN_FILENO, FD_READ, stop_on_return_handler, NULL)); |
| 331 | |
| 332 | // Start main loop |
| 333 | EOR(re_main(default_signal_handler)); |
| 334 | |
| 335 | // Stop clients |
| 336 | client_stop(&a); |
| 337 | client_stop(&b); |
| 338 | |
| 339 | // Stop listening on STDIN |
| 340 | fd_close(STDIN_FILENO); |
| 341 | |
| 342 | // Free |
| 343 | mem_deref(gather_options); |
| 344 | |
| 345 | // Bye |
| 346 | before_exit(); |
| 347 | return 0; |
| 348 | } |