Squashed 'third_party/rawrtc/rawrtc/' content from commit aa3ae4b24
Change-Id: I38a655a4259b62f591334e90a1315bd4e7e4d8ec
git-subtree-dir: third_party/rawrtc/rawrtc
git-subtree-split: aa3ae4b247275cc6e69c30613b3a4ba7fdc82d1b
diff --git a/tools/helper/common.c b/tools/helper/common.c
new file mode 100644
index 0000000..e4be7a2
--- /dev/null
+++ b/tools/helper/common.c
@@ -0,0 +1,225 @@
+#include "common.h"
+#include <rawrtc.h>
+#include <rawrtcc.h>
+#include <rawrtcdc.h>
+#include <re.h>
+#include <stdlib.h> // exit
+#include <string.h> // strerror
+
+#define DEBUG_MODULE "helper-common"
+#define DEBUG_LEVEL 7
+#include <re_dbg.h>
+
+/*
+ * Ignore success code list.
+ */
+enum rawrtc_code const ignore_success[] = {RAWRTC_CODE_SUCCESS};
+size_t const ignore_success_length = ARRAY_SIZE(ignore_success);
+
+/*
+ * Function to be called before exiting.
+ */
+void before_exit(void) {
+ // Close
+ rawrtc_close(true);
+
+ // Check memory leaks
+ tmr_debug();
+ mem_debug();
+}
+
+/*
+ * Exit on error code.
+ */
+void exit_on_error(
+ enum rawrtc_code const code,
+ enum rawrtc_code const ignore[],
+ size_t const n_ignore,
+ char const* const file,
+ uint32_t const line) {
+ size_t i;
+
+ // Ignore?
+ for (i = 0; i < n_ignore; ++i) {
+ if (code == ignore[i]) {
+ return;
+ }
+ }
+
+ // Handle
+ switch (code) {
+ case RAWRTC_CODE_SUCCESS:
+ return;
+ case RAWRTC_CODE_NOT_IMPLEMENTED:
+ DEBUG_WARNING("Not implemented in %s %" PRIu32 "\n", file, line);
+ return;
+ default:
+ DEBUG_WARNING(
+ "Error in %s %" PRIu32 " (%d): %s\n", file, line, code, rawrtc_code_to_str(code));
+ before_exit();
+ exit((int) code);
+ }
+}
+
+/*
+ * Exit on POSIX error code.
+ */
+void exit_on_posix_error(int code, char const* const file, uint32_t line) {
+ if (code != 0) {
+ DEBUG_WARNING("Error in %s %" PRIu32 " (%d): %s\n", file, line, code, strerror(code));
+ before_exit();
+ exit(code);
+ }
+}
+
+/*
+ * Exit with a custom error message.
+ */
+void exit_with_error(char const* const file, uint32_t line, char const* const formatter, ...) {
+ char* message;
+
+ // Format message
+ va_list ap;
+ va_start(ap, formatter);
+ re_vsdprintf(&message, formatter, ap);
+ va_end(ap);
+
+ // Print message
+ DEBUG_WARNING("%s %" PRIu32 ": %s\n", file, line, message);
+
+ // Un-reference & bye
+ mem_deref(message);
+ before_exit();
+ exit(1);
+}
+
+/*
+ * Check if the ICE candidate type is enabled.
+ */
+bool ice_candidate_type_enabled(
+ struct client* const client, enum rawrtc_ice_candidate_type const type) {
+ char const* const type_str = rawrtc_ice_candidate_type_to_str(type);
+ size_t i;
+
+ // All enabled?
+ if (client->n_ice_candidate_types == 0) {
+ return true;
+ }
+
+ // Specifically enabled?
+ for (i = 0; i < client->n_ice_candidate_types; ++i) {
+ if (str_cmp(client->ice_candidate_types[i], type_str) == 0) {
+ return true;
+ }
+ }
+
+ // Nope
+ return false;
+}
+
+/*
+ * Print ICE candidate information.
+ */
+void print_ice_candidate(
+ struct rawrtc_ice_candidate* const candidate,
+ char const* const url, // read-only
+ struct rawrtc_peer_connection_ice_candidate* const pc_candidate, // nullable
+ struct client* const client) {
+ if (candidate) {
+ enum rawrtc_code const ignore[] = {RAWRTC_CODE_NO_VALUE};
+ enum rawrtc_code error;
+ char* foundation;
+ enum rawrtc_ice_protocol protocol;
+ uint32_t priority;
+ char* ip;
+ uint16_t port;
+ enum rawrtc_ice_candidate_type type;
+ enum rawrtc_ice_tcp_candidate_type tcp_type;
+ char const* tcp_type_str = "n/a";
+ char* related_address = NULL;
+ uint16_t related_port = 0;
+ char* mid = NULL;
+ uint8_t media_line_index = UINT8_MAX;
+ char* media_line_index_str = NULL;
+ char* username_fragment = NULL;
+ bool is_enabled;
+ int level;
+
+ // Get candidate information
+ EOE(rawrtc_ice_candidate_get_foundation(&foundation, candidate));
+ EOE(rawrtc_ice_candidate_get_protocol(&protocol, candidate));
+ EOE(rawrtc_ice_candidate_get_priority(&priority, candidate));
+ EOE(rawrtc_ice_candidate_get_ip(&ip, candidate));
+ EOE(rawrtc_ice_candidate_get_port(&port, candidate));
+ EOE(rawrtc_ice_candidate_get_type(&type, candidate));
+ error = rawrtc_ice_candidate_get_tcp_type(&tcp_type, candidate);
+ switch (error) {
+ case RAWRTC_CODE_SUCCESS:
+ tcp_type_str = rawrtc_ice_tcp_candidate_type_to_str(tcp_type);
+ break;
+ case RAWRTC_CODE_NO_VALUE:
+ break;
+ default:
+ EOE(error);
+ break;
+ }
+ EOEIGN(rawrtc_ice_candidate_get_related_address(&related_address, candidate), ignore);
+ EOEIGN(rawrtc_ice_candidate_get_related_port(&related_port, candidate), ignore);
+ if (pc_candidate) {
+ EOEIGN(rawrtc_peer_connection_ice_candidate_get_sdp_mid(&mid, pc_candidate), ignore);
+ error = rawrtc_peer_connection_ice_candidate_get_sdp_media_line_index(
+ &media_line_index, pc_candidate);
+ switch (error) {
+ case RAWRTC_CODE_SUCCESS:
+ EOE(rawrtc_sdprintf(&media_line_index_str, "%" PRIu8, media_line_index));
+ break;
+ case RAWRTC_CODE_NO_VALUE:
+ break;
+ default:
+ EOE(error);
+ break;
+ }
+ EOEIGN(
+ rawrtc_peer_connection_ice_candidate_get_username_fragment(
+ &username_fragment, pc_candidate),
+ ignore);
+ }
+ is_enabled = ice_candidate_type_enabled(client, type);
+
+ // Print candidate (meh, lot's of repeated code... feel free to suggest an alternative)
+ level = is_enabled ? DBG_INFO : DBG_DEBUG;
+ if (!pc_candidate) {
+ dbg_printf(
+ level,
+ "(%s) ICE candidate: foundation=%s, protocol=%s"
+ ", priority=%" PRIu32 ", ip=%s, port=%" PRIu16 ", type=%s, tcp-type=%s"
+ ", related-address=%s, related-port=%" PRIu16 "; URL: %s; %s\n",
+ client->name, foundation, rawrtc_ice_protocol_to_str(protocol), priority, ip, port,
+ rawrtc_ice_candidate_type_to_str(type), tcp_type_str,
+ related_address ? related_address : "n/a", related_port, url ? url : "n/a",
+ is_enabled ? "enabled" : "disabled");
+ } else {
+ dbg_printf(
+ level,
+ "(%s) ICE candidate: foundation=%s, protocol=%s"
+ ", priority=%" PRIu32 ", ip=%s, port=%" PRIu16 ", type=%s, tcp-type=%s"
+ ", related-address=%s, related-port=%" PRIu16 "; URL: %s"
+ "; mid=%s, media_line_index=%s, username_fragment=%s; %s\n",
+ client->name, foundation, rawrtc_ice_protocol_to_str(protocol), priority, ip, port,
+ rawrtc_ice_candidate_type_to_str(type), tcp_type_str,
+ related_address ? related_address : "n/a", related_port, url ? url : "n/a",
+ mid ? mid : "n/a", media_line_index_str ? media_line_index_str : "n/a",
+ username_fragment ? username_fragment : "n/a", is_enabled ? "enabled" : "disabled");
+ }
+
+ // Unreference
+ mem_deref(username_fragment);
+ mem_deref(media_line_index_str);
+ mem_deref(mid);
+ mem_deref(related_address);
+ mem_deref(ip);
+ mem_deref(foundation);
+ } else {
+ DEBUG_INFO("(%s) ICE gatherer last local candidate\n", client->name);
+ }
+}
diff --git a/tools/helper/common.h b/tools/helper/common.h
new file mode 100644
index 0000000..e2a34ad
--- /dev/null
+++ b/tools/helper/common.h
@@ -0,0 +1,95 @@
+#pragma once
+#include <rawrtc.h>
+#include <rawrtcc.h>
+#include <rawrtcdc.h>
+#include <re.h>
+
+enum {
+ PARAMETERS_MAX_LENGTH = 8192,
+};
+
+/*
+ * SCTP parameters that need to be negotiated.
+ */
+struct sctp_parameters {
+ struct rawrtc_sctp_capabilities* capabilities;
+ uint16_t port;
+};
+
+/*
+ * Client structure. Can be extended.
+ */
+struct client {
+ char* name;
+ char** ice_candidate_types;
+ size_t n_ice_candidate_types;
+};
+
+/*
+ * Data channel helper structure. Can be extended.
+ */
+struct data_channel_helper {
+ struct le le;
+ struct rawrtc_data_channel* channel;
+ char* label;
+ struct client* client;
+ void* arg;
+};
+
+/*
+ * Ignore success code list.
+ */
+extern enum rawrtc_code const ignore_success[];
+extern size_t const ignore_success_length;
+
+/*
+ * Helper macros for exiting with error messages.
+ */
+#define EOE(code) exit_on_error(code, ignore_success, ignore_success_length, __FILE__, __LINE__)
+#define EOEIGN(code, ignore) exit_on_error(code, ignore, ARRAY_SIZE(ignore), __FILE__, __LINE__)
+#define EOR(code) exit_on_posix_error(code, __FILE__, __LINE__)
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (__GNUC__ >= 3)
+# define EWE(...) exit_with_error(__FILE__, __LINE__, __VA_ARGS__)
+#elif defined(__GNUC__)
+# define EWE(args...) exit_with_error(__FILE__, __LINE__, args)
+#endif
+
+/*
+ * Function to be called before exiting.
+ */
+void before_exit(void);
+
+/*
+ * Exit on error code.
+ */
+void exit_on_error(
+ enum rawrtc_code const code,
+ enum rawrtc_code const ignore[],
+ size_t const n_ignore,
+ char const* const file,
+ uint32_t const line);
+
+/*
+ * Exit on POSIX error code.
+ */
+void exit_on_posix_error(int code, char const* const file, uint32_t line);
+
+/*
+ * Exit with a custom error message.
+ */
+void exit_with_error(char const* const file, uint32_t line, char const* const formatter, ...);
+
+/*
+ * Check if the ICE candidate type is enabled.
+ */
+bool ice_candidate_type_enabled(
+ struct client* const client, enum rawrtc_ice_candidate_type const type);
+
+/*
+ * Print ICE candidate information.
+ */
+void print_ice_candidate(
+ struct rawrtc_ice_candidate* const candidate,
+ char const* const url, // read-only
+ struct rawrtc_peer_connection_ice_candidate* const pc_candidate, // nullable
+ struct client* const client);
diff --git a/tools/helper/handler.c b/tools/helper/handler.c
new file mode 100644
index 0000000..f6d8d1a
--- /dev/null
+++ b/tools/helper/handler.c
@@ -0,0 +1,337 @@
+#include "handler.h"
+#include "common.h"
+#include "utils.h"
+#include <rawrtc.h>
+#include <rawrtcc.h>
+#include <rawrtcdc.h>
+#include <re.h>
+#include <string.h> // strlen
+
+#define DEBUG_MODULE "helper-handler"
+#define DEBUG_LEVEL 7
+#include <re_dbg.h>
+
+/*
+ * Print the ICE gatherer's state.
+ */
+void default_ice_gatherer_state_change_handler(
+ enum rawrtc_ice_gatherer_state const state, // read-only
+ void* const arg // will be casted to `struct client*`
+) {
+ struct client* const client = arg;
+ char const* const state_name = rawrtc_ice_gatherer_state_to_name(state);
+ (void) arg;
+ DEBUG_PRINTF("(%s) ICE gatherer state: %s\n", client->name, state_name);
+}
+
+/*
+ * Print the ICE gatherer's error event.
+ */
+void default_ice_gatherer_error_handler(
+ struct rawrtc_ice_candidate* const candidate, // read-only, nullable
+ char const* const url, // read-only
+ uint16_t const error_code, // read-only
+ char const* const error_text, // read-only
+ void* const arg // will be casted to `struct client*`
+) {
+ struct client* const client = arg;
+ (void) candidate;
+ (void) error_code;
+ (void) arg;
+ DEBUG_NOTICE("(%s) ICE gatherer error, URL: %s, reason: %s\n", client->name, url, error_text);
+}
+
+/*
+ * Print the newly gathered local candidate.
+ */
+void default_ice_gatherer_local_candidate_handler(
+ struct rawrtc_ice_candidate* const candidate,
+ char const* const url, // read-only
+ void* const arg // will be casted to `struct client*`
+) {
+ struct client* const client = arg;
+ (void) arg;
+ print_ice_candidate(candidate, url, NULL, client);
+}
+
+/*
+ * Print the ICE transport's state.
+ */
+void default_ice_transport_state_change_handler(
+ enum rawrtc_ice_transport_state const state,
+ void* const arg // will be casted to `struct client*`
+) {
+ struct client* const client = arg;
+ char const* const state_name = rawrtc_ice_transport_state_to_name(state);
+ (void) arg;
+ DEBUG_PRINTF("(%s) ICE transport state: %s\n", client->name, state_name);
+}
+
+/*
+ * Print the ICE candidate pair change event.
+ */
+void default_ice_transport_candidate_pair_change_handler(
+ struct rawrtc_ice_candidate* const local, // read-only
+ struct rawrtc_ice_candidate* const remote, // read-only
+ void* const arg // will be casted to `struct client*`
+) {
+ struct client* const client = arg;
+ (void) local;
+ (void) remote;
+ DEBUG_PRINTF("(%s) ICE transport candidate pair change\n", client->name);
+}
+
+/*
+ * Print the DTLS transport's state.
+ */
+void default_dtls_transport_state_change_handler(
+ enum rawrtc_dtls_transport_state const state, // read-only
+ void* const arg // will be casted to `struct client*`
+) {
+ struct client* const client = arg;
+ char const* const state_name = rawrtc_dtls_transport_state_to_name(state);
+ DEBUG_PRINTF("(%s) DTLS transport state change: %s\n", client->name, state_name);
+}
+
+/*
+ * Print the DTLS transport's error event.
+ */
+void default_dtls_transport_error_handler(
+ // TODO: error.message (probably from OpenSSL)
+ void* const arg // will be casted to `struct client*`
+) {
+ struct client* const client = arg;
+ // TODO: Print error message
+ DEBUG_WARNING("(%s) DTLS transport error: %s\n", client->name, "???");
+}
+
+#if RAWRTC_HAVE_SCTP_REDIRECT_TRANSPORT
+/*
+ * Print the SCTP redirect transport's state.
+ */
+void default_sctp_redirect_transport_state_change_handler(
+ enum rawrtc_sctp_redirect_transport_state const state,
+ void* const arg // will be casted to `struct client*`
+) {
+ struct client* const client = arg;
+ char const* const state_name = rawrtc_sctp_redirect_transport_state_to_name(state);
+ DEBUG_PRINTF("(%s) SCTP redirect transport state change: %s\n", client->name, state_name);
+}
+#endif
+
+/*
+ * Print the SCTP transport's state.
+ */
+void default_sctp_transport_state_change_handler(
+ enum rawrtc_sctp_transport_state const state,
+ void* const arg // will be casted to `struct client*`
+) {
+ struct client* const client = arg;
+ char const* const state_name = rawrtc_sctp_transport_state_to_name(state);
+ DEBUG_PRINTF("(%s) SCTP transport state change: %s\n", client->name, state_name);
+}
+
+/*
+ * Print the newly created data channel's parameter.
+ */
+void default_data_channel_handler(
+ struct rawrtc_data_channel* const channel, // read-only, MUST be referenced when used
+ void* const arg // will be casted to `struct client*`
+) {
+ struct client* const client = arg;
+ struct rawrtc_data_channel_parameters* parameters;
+ enum rawrtc_code const ignore[] = {RAWRTC_CODE_NO_VALUE};
+ char* label = NULL;
+
+ // Get data channel label and protocol
+ EOE(rawrtc_data_channel_get_parameters(¶meters, channel));
+ EOEIGN(rawrtc_data_channel_parameters_get_label(&label, parameters), ignore);
+ DEBUG_INFO("(%s) New data channel instance: %s\n", client->name, label ? label : "n/a");
+ mem_deref(label);
+ mem_deref(parameters);
+}
+
+/*
+ * Print the data channel open event.
+ */
+void default_data_channel_open_handler(
+ void* const arg // will be casted to `struct data_channel_helper*`
+) {
+ struct data_channel_helper* const channel = arg;
+ struct client* const client = channel->client;
+ DEBUG_PRINTF("(%s) Data channel open: %s\n", client->name, channel->label);
+}
+
+/*
+ * Print the data channel buffered amount low event.
+ */
+void default_data_channel_buffered_amount_low_handler(
+ void* const arg // will be casted to `struct data_channel_helper*`
+) {
+ struct data_channel_helper* const channel = arg;
+ struct client* const client = channel->client;
+ DEBUG_PRINTF("(%s) Data channel buffered amount low: %s\n", client->name, channel->label);
+}
+
+/*
+ * Print the data channel error event.
+ */
+void default_data_channel_error_handler(
+ void* const arg // will be casted to `struct data_channel_helper*`
+) {
+ struct data_channel_helper* const channel = arg;
+ struct client* const client = channel->client;
+ DEBUG_WARNING("(%s) Data channel error: %s\n", client->name, channel->label);
+}
+
+/*
+ * Print the data channel close event.
+ */
+void default_data_channel_close_handler(
+ void* const arg // will be casted to `struct data_channel_helper*`
+) {
+ struct data_channel_helper* const channel = arg;
+ struct client* const client = channel->client;
+ DEBUG_PRINTF("(%s) Data channel closed: %s\n", client->name, channel->label);
+}
+
+char const* const separator = ", ";
+
+static int debug_data_channel_message_flags(
+ struct re_printf* const pf, enum rawrtc_data_channel_message_flag const flags) {
+ int err = 0;
+ char const* prefix = "";
+
+ if (flags & RAWRTC_DATA_CHANNEL_MESSAGE_FLAG_IS_ABORTED) {
+ err |= re_hprintf(pf, "%saborted", prefix);
+ prefix = separator;
+ }
+ if (flags & RAWRTC_DATA_CHANNEL_MESSAGE_FLAG_IS_COMPLETE) {
+ err |= re_hprintf(pf, "%scomplete", prefix);
+ prefix = separator;
+ }
+ if (flags & RAWRTC_DATA_CHANNEL_MESSAGE_FLAG_IS_STRING) {
+ err |= re_hprintf(pf, "%sstring", prefix);
+ prefix = separator;
+ }
+ if (flags & RAWRTC_DATA_CHANNEL_MESSAGE_FLAG_IS_BINARY) {
+ err |= re_hprintf(pf, "%sbinary", prefix);
+ }
+
+ return err;
+}
+
+/*
+ * Print the data channel's received message's size.
+ */
+void default_data_channel_message_handler(
+ struct mbuf* const buffer,
+ enum rawrtc_data_channel_message_flag const flags,
+ void* const arg // will be casted to `struct data_channel_helper*`
+) {
+ struct data_channel_helper* const channel = arg;
+ struct client* const client = channel->client;
+ DEBUG_PRINTF(
+ "(%s) Incoming message for data channel %s: %zu bytes; flags=(%H)\n", client->name,
+ channel->label, mbuf_get_left(buffer), debug_data_channel_message_flags, flags);
+}
+
+/*
+ * Print negotiation needed (duh!)
+ */
+void default_negotiation_needed_handler(void* const arg) {
+ struct client* const client = arg;
+ DEBUG_PRINTF("(%s) Negotiation needed\n", client->name);
+}
+
+/*
+ * Print the peer connection's state.
+ */
+void default_peer_connection_state_change_handler(
+ enum rawrtc_peer_connection_state const state, // read-only
+ void* const arg // will be casted to `struct client*`
+) {
+ struct client* const client = arg;
+ char const* const state_name = rawrtc_peer_connection_state_to_name(state);
+ DEBUG_PRINTF("(%s) Peer connection state change: %s\n", client->name, state_name);
+}
+
+/*
+ * Print the newly gathered local candidate (peer connection variant).
+ */
+void default_peer_connection_local_candidate_handler(
+ struct rawrtc_peer_connection_ice_candidate* const candidate,
+ char const* const url, // read-only
+ void* const arg) {
+ struct client* const client = arg;
+ struct rawrtc_ice_candidate* ortc_candidate = NULL;
+
+ // Get underlying ORTC ICE candidate (if any)
+ if (candidate) {
+ EOE(rawrtc_peer_connection_ice_candidate_get_ortc_candidate(&ortc_candidate, candidate));
+ }
+
+ // Print local candidate
+ print_ice_candidate(ortc_candidate, url, candidate, client);
+ mem_deref(ortc_candidate);
+}
+
+/*
+ * Print the peer connections local candidate error event.
+ */
+void default_peer_connection_local_candidate_error_handler(
+ struct rawrtc_peer_connection_ice_candidate* const candidate, // read-only, nullable
+ char const* const url, // read-only
+ uint16_t const error_code, // read-only
+ char const* const error_text, // read-only
+ void* const arg // will be casted to `struct client*`
+) {
+ struct client* const client = arg;
+ (void) candidate;
+ (void) error_code;
+ (void) arg;
+ DEBUG_NOTICE("(%s) ICE candidate error, URL: %s, reason: %s\n", client->name, url, error_text);
+}
+
+/*
+ * Print the signaling state.
+ */
+void default_signaling_state_change_handler(
+ enum rawrtc_signaling_state const state, // read-only
+ void* const arg) {
+ struct client* const client = arg;
+ char const* const state_name = rawrtc_signaling_state_to_name(state);
+ DEBUG_PRINTF("(%s) Signaling state change: %s\n", client->name, state_name);
+}
+
+/*
+ * Stop the main loop.
+ */
+void default_signal_handler(int sig) {
+ DEBUG_INFO("Got signal: %d, terminating...\n", sig);
+ re_cancel();
+}
+
+/*
+ * FD-listener that stops the main loop in case the input buffer
+ * contains a line feed or a carriage return.
+ */
+void stop_on_return_handler(int flags, void* arg) {
+ char buffer[128];
+ size_t length;
+ (void) flags;
+ (void) arg;
+
+ // Get message from stdin
+ if (!fgets((char*) buffer, 128, stdin)) {
+ EOR(errno);
+ }
+ length = strlen(buffer);
+
+ // Exit?
+ if (length > 0 && length < 3 && (buffer[0] == '\n' || buffer[0] == '\r')) {
+ // Stop main loop
+ DEBUG_INFO("Exiting\n");
+ re_cancel();
+ }
+}
diff --git a/tools/helper/handler.h b/tools/helper/handler.h
new file mode 100644
index 0000000..56d4211
--- /dev/null
+++ b/tools/helper/handler.h
@@ -0,0 +1,181 @@
+#pragma once
+#include "common.h"
+#include <rawrtc.h>
+#include <rawrtcc.h>
+#include <rawrtcdc.h>
+#include <re.h>
+
+/*
+ * Print the ICE gatherer's state.
+ */
+void default_ice_gatherer_state_change_handler(
+ enum rawrtc_ice_gatherer_state const state, // read-only
+ void* const arg);
+
+/*
+ * Print the ICE gatherer's error event.
+ */
+void default_ice_gatherer_error_handler(
+ struct rawrtc_ice_candidate* const host_candidate, // read-only, nullable
+ char const* const url, // read-only
+ uint16_t const error_code, // read-only
+ char const* const error_text, // read-only
+ void* const arg // will be casted to `struct client*`
+);
+
+/*
+ * Print the newly gatherered local candidate.
+ * Will print local parameters on stdout in case the client is not
+ * used in loopback mode.
+ */
+void default_ice_gatherer_local_candidate_handler(
+ struct rawrtc_ice_candidate* const candidate,
+ char const* const url, // read-only
+ void* const arg // will be casted to `struct client*`
+);
+
+/*
+ * Print the ICE transport's state.
+ */
+void default_ice_transport_state_change_handler(
+ enum rawrtc_ice_transport_state const state,
+ void* const arg // will be casted to `struct client*`
+);
+
+/*
+ * Print the ICE candidate pair change event.
+ */
+void default_ice_transport_candidate_pair_change_handler(
+ struct rawrtc_ice_candidate* const local, // read-only
+ struct rawrtc_ice_candidate* const remote, // read-only
+ void* const arg // will be casted to `struct client*`
+);
+
+/*
+ * Print the DTLS transport's state.
+ */
+void default_dtls_transport_state_change_handler(
+ enum rawrtc_dtls_transport_state const state, // read-only
+ void* const arg // will be casted to `struct client*`
+);
+
+/*
+ * Print the DTLS transport's error event.
+ */
+void default_dtls_transport_error_handler(
+ // TODO: error.message (probably from OpenSSL)
+ void* const arg // will be casted to `struct client*`
+);
+
+#if RAWRTC_HAVE_SCTP_REDIRECT_TRANSPORT
+/*
+ * Print the SCTP redirect transport's state.
+ */
+void default_sctp_redirect_transport_state_change_handler(
+ enum rawrtc_sctp_redirect_transport_state const state,
+ void* const arg // will be casted to `struct client*`
+);
+#endif
+
+/*
+ * Print the SCTP transport's state.
+ */
+void default_sctp_transport_state_change_handler(
+ enum rawrtc_sctp_transport_state const state,
+ void* const arg // will be casted to `struct client*`
+);
+
+/*
+ * Print the newly created data channel's parameter.
+ */
+void default_data_channel_handler(
+ struct rawrtc_data_channel* const data_channel, // read-only, MUST be referenced when used
+ void* const arg // will be casted to `struct data_channel_helper*`
+);
+
+/*
+ * Print the data channel open event.
+ */
+void default_data_channel_open_handler(
+ void* const arg // will be casted to `struct data_channel_helper*`
+);
+
+/*
+ * Print the data channel buffered amount low event.
+ */
+void default_data_channel_buffered_amount_low_handler(
+ void* const arg // will be casted to `struct data_channel_helper*`
+);
+
+/*
+ * Print the data channel error event.
+ */
+void default_data_channel_error_handler(
+ void* const arg // will be casted to `struct data_channel_helper*`
+);
+
+/*
+ * Print the data channel close event.
+ */
+void default_data_channel_close_handler(
+ void* const arg // will be casted to `struct data_channel_helper*`
+);
+
+/*
+ * Print the data channel's received message's size.
+ */
+void default_data_channel_message_handler(
+ struct mbuf* const buffer,
+ enum rawrtc_data_channel_message_flag const flags,
+ void* const arg // will be casted to `struct data_channel_helper*`
+);
+
+/*
+ * Print negotiation needed (duh!)
+ */
+void default_negotiation_needed_handler(void* const arg);
+
+/*
+ * Print the peer connection's state.
+ */
+void default_peer_connection_state_change_handler(
+ enum rawrtc_peer_connection_state const state, // read-only
+ void* const arg // will be casted to `struct client*`
+);
+
+/*
+ * Print the newly gathered local candidate (peer connection variant).
+ */
+void default_peer_connection_local_candidate_handler(
+ struct rawrtc_peer_connection_ice_candidate* const candidate,
+ char const* const url, // read-only
+ void* const arg);
+
+/*
+ * Print the peer connections local candidate error event.
+ */
+void default_peer_connection_local_candidate_error_handler(
+ struct rawrtc_peer_connection_ice_candidate* const candidate, // read-only, nullable
+ char const* const url, // read-only
+ uint16_t const error_code, // read-only
+ char const* const error_text, // read-only
+ void* const arg // will be casted to `struct client*`
+);
+
+/*
+ * Print the signaling state.
+ */
+void default_signaling_state_change_handler(
+ enum rawrtc_signaling_state const state, // read-only
+ void* const arg);
+
+/*
+ * Stop the main loop.
+ */
+void default_signal_handler(int sig);
+
+/*
+ * FD-listener that stops the main loop in case the input buffer is
+ * empty.
+ */
+void stop_on_return_handler(int flags, void* arg);
diff --git a/tools/helper/meson.build b/tools/helper/meson.build
new file mode 100644
index 0000000..b9ef950
--- /dev/null
+++ b/tools/helper/meson.build
@@ -0,0 +1,6 @@
+helper_sources = files([
+ 'common.c',
+ 'handler.c',
+ 'parameters.c',
+ 'utils.c',
+])
diff --git a/tools/helper/parameters.c b/tools/helper/parameters.c
new file mode 100644
index 0000000..46ae22b
--- /dev/null
+++ b/tools/helper/parameters.c
@@ -0,0 +1,448 @@
+#include "parameters.h"
+#include "common.h"
+#include "utils.h"
+#include <rawrtc.h>
+#include <rawrtcc.h>
+#include <rawrtcdc.h>
+#include <re.h>
+
+#define DEBUG_MODULE "helper-parameters"
+#define DEBUG_LEVEL 7
+#include <re_dbg.h>
+
+/*
+ * Set ICE parameters in dictionary.
+ */
+void set_ice_parameters(struct rawrtc_ice_parameters* const parameters, struct odict* const dict) {
+ char* username_fragment;
+ char* password;
+ bool ice_lite;
+
+ // Get values
+ EOE(rawrtc_ice_parameters_get_username_fragment(&username_fragment, parameters));
+ EOE(rawrtc_ice_parameters_get_password(&password, parameters));
+ EOE(rawrtc_ice_parameters_get_ice_lite(&ice_lite, parameters));
+
+ // Set ICE parameters
+ EOR(odict_entry_add(dict, "usernameFragment", ODICT_STRING, username_fragment));
+ EOR(odict_entry_add(dict, "password", ODICT_STRING, password));
+ EOR(odict_entry_add(dict, "iceLite", ODICT_BOOL, ice_lite));
+
+ // Un-reference values
+ mem_deref(password);
+ mem_deref(username_fragment);
+}
+
+/*
+ * Set ICE candidates in dictionary.
+ */
+void set_ice_candidates(struct rawrtc_ice_candidates* const parameters, struct odict* const array) {
+ size_t i;
+ struct odict* node;
+
+ // Set ICE candidates
+ for (i = 0; i < parameters->n_candidates; ++i) {
+ enum rawrtc_code error;
+ struct rawrtc_ice_candidate* const candidate = parameters->candidates[i];
+ char* foundation;
+ uint32_t priority;
+ char* ip;
+ enum rawrtc_ice_protocol protocol;
+ uint16_t port;
+ enum rawrtc_ice_candidate_type type;
+ enum rawrtc_ice_tcp_candidate_type tcp_type = RAWRTC_ICE_TCP_CANDIDATE_TYPE_ACTIVE;
+ char* related_address = NULL;
+ uint16_t related_port = 0;
+ char* key;
+
+ // Create object
+ EOR(odict_alloc(&node, 16));
+
+ // Get values
+ EOE(rawrtc_ice_candidate_get_foundation(&foundation, candidate));
+ EOE(rawrtc_ice_candidate_get_priority(&priority, candidate));
+ EOE(rawrtc_ice_candidate_get_ip(&ip, candidate));
+ EOE(rawrtc_ice_candidate_get_protocol(&protocol, candidate));
+ EOE(rawrtc_ice_candidate_get_port(&port, candidate));
+ EOE(rawrtc_ice_candidate_get_type(&type, candidate));
+ error = rawrtc_ice_candidate_get_tcp_type(&tcp_type, candidate);
+ EOE(error == RAWRTC_CODE_NO_VALUE ? RAWRTC_CODE_SUCCESS : error);
+ error = rawrtc_ice_candidate_get_related_address(&related_address, candidate);
+ EOE(error == RAWRTC_CODE_NO_VALUE ? RAWRTC_CODE_SUCCESS : error);
+ error = rawrtc_ice_candidate_get_related_port(&related_port, candidate);
+ EOE(error == RAWRTC_CODE_NO_VALUE ? RAWRTC_CODE_SUCCESS : error);
+
+ // Set ICE candidate values
+ EOR(odict_entry_add(node, "foundation", ODICT_STRING, foundation));
+ EOR(odict_entry_add(node, "priority", ODICT_INT, (int64_t) priority));
+ EOR(odict_entry_add(node, "ip", ODICT_STRING, ip));
+ EOR(odict_entry_add(node, "protocol", ODICT_STRING, rawrtc_ice_protocol_to_str(protocol)));
+ EOR(odict_entry_add(node, "port", ODICT_INT, (int64_t) port));
+ EOR(odict_entry_add(node, "type", ODICT_STRING, rawrtc_ice_candidate_type_to_str(type)));
+ if (protocol == RAWRTC_ICE_PROTOCOL_TCP) {
+ EOR(odict_entry_add(
+ node, "tcpType", ODICT_STRING, rawrtc_ice_tcp_candidate_type_to_str(tcp_type)));
+ }
+ if (related_address) {
+ EOR(odict_entry_add(node, "relatedAddress", ODICT_STRING, related_address));
+ }
+ if (related_port > 0) {
+ EOR(odict_entry_add(node, "relatedPort", (int64_t) ODICT_INT, related_port));
+ }
+
+ // Add to array
+ EOE(rawrtc_sdprintf(&key, "%zu", i));
+ EOR(odict_entry_add(array, key, ODICT_OBJECT, node));
+
+ // Un-reference values
+ mem_deref(key);
+ mem_deref(related_address);
+ mem_deref(ip);
+ mem_deref(foundation);
+ mem_deref(node);
+ }
+}
+
+/*
+ * Set DTLS parameters in dictionary.
+ */
+void set_dtls_parameters(
+ struct rawrtc_dtls_parameters* const parameters, struct odict* const dict) {
+ enum rawrtc_dtls_role role;
+ struct odict* array;
+ struct odict* node;
+ struct rawrtc_dtls_fingerprints* fingerprints;
+ size_t i;
+
+ // Get and set DTLS role
+ EOE(rawrtc_dtls_parameters_get_role(&role, parameters));
+ EOR(odict_entry_add(dict, "role", ODICT_STRING, rawrtc_dtls_role_to_str(role)));
+
+ // Create array
+ EOR(odict_alloc(&array, 16));
+
+ // Get and set fingerprints
+ EOE(rawrtc_dtls_parameters_get_fingerprints(&fingerprints, parameters));
+ for (i = 0; i < fingerprints->n_fingerprints; ++i) {
+ struct rawrtc_dtls_fingerprint* const fingerprint = fingerprints->fingerprints[i];
+ enum rawrtc_certificate_sign_algorithm sign_algorithm;
+ char* value;
+ char* key;
+
+ // Create object
+ EOR(odict_alloc(&node, 16));
+
+ // Get values
+ EOE(rawrtc_dtls_fingerprint_get_sign_algorithm(&sign_algorithm, fingerprint));
+ EOE(rawrtc_dtls_fingerprint_get_value(&value, fingerprint));
+
+ // Set fingerprint values
+ EOR(odict_entry_add(
+ node, "algorithm", ODICT_STRING,
+ rawrtc_certificate_sign_algorithm_to_str(sign_algorithm)));
+ EOR(odict_entry_add(node, "value", ODICT_STRING, value));
+
+ // Add to array
+ EOE(rawrtc_sdprintf(&key, "%zu", i));
+ EOR(odict_entry_add(array, key, ODICT_OBJECT, node));
+
+ // Un-reference values
+ mem_deref(key);
+ mem_deref(value);
+ mem_deref(node);
+ }
+
+ // Un-reference fingerprints
+ mem_deref(fingerprints);
+
+ // Add array to object
+ EOR(odict_entry_add(dict, "fingerprints", ODICT_ARRAY, array));
+ mem_deref(array);
+}
+
+/*
+ * Set SCTP parameters in dictionary.
+ */
+void set_sctp_parameters(
+ struct rawrtc_sctp_transport* const transport,
+ struct sctp_parameters* const parameters,
+ struct odict* const dict) {
+ uint64_t max_message_size;
+ uint16_t port;
+
+ // Get values
+ EOE(rawrtc_sctp_capabilities_get_max_message_size(&max_message_size, parameters->capabilities));
+ EOE(rawrtc_sctp_transport_get_port(&port, transport));
+
+ // Ensure maximum message size fits into int64
+ if (max_message_size > INT64_MAX) {
+ EOE(RAWRTC_CODE_INSUFFICIENT_SPACE);
+ }
+
+ // Set ICE parameters
+ EOR(odict_entry_add(dict, "maxMessageSize", ODICT_INT, (int64_t) max_message_size));
+ EOR(odict_entry_add(dict, "port", ODICT_INT, (int64_t) port));
+}
+
+#if RAWRTC_HAVE_SCTP_REDIRECT_TRANSPORT
+/*
+ * Set SCTP redirect parameters in dictionary.
+ */
+void set_sctp_redirect_parameters(
+ struct rawrtc_sctp_redirect_transport* const transport,
+ struct sctp_parameters* const parameters,
+ struct odict* const dict) {
+ uint64_t max_message_size;
+ uint16_t port;
+
+ // Get values
+ EOE(rawrtc_sctp_capabilities_get_max_message_size(&max_message_size, parameters->capabilities));
+ EOE(rawrtc_sctp_redirect_transport_get_port(&port, transport));
+
+ // Ensure maximum message size fits into int64
+ if (max_message_size > INT64_MAX) {
+ EOE(RAWRTC_CODE_INSUFFICIENT_SPACE);
+ }
+
+ // Set ICE parameters
+ EOR(odict_entry_add(dict, "maxMessageSize", ODICT_INT, (int64_t) max_message_size));
+ EOR(odict_entry_add(dict, "port", ODICT_INT, (int64_t) port));
+}
+#endif
+
+/*
+ * Get ICE parameters from dictionary.
+ */
+enum rawrtc_code get_ice_parameters(
+ struct rawrtc_ice_parameters** const parametersp, struct odict* const dict) {
+ enum rawrtc_code error = RAWRTC_CODE_SUCCESS;
+ char* username_fragment;
+ char* password;
+ bool ice_lite;
+
+ // Get ICE parameters
+ error |= dict_get_entry(&username_fragment, dict, "usernameFragment", ODICT_STRING, true);
+ error |= dict_get_entry(&password, dict, "password", ODICT_STRING, true);
+ error |= dict_get_entry(&ice_lite, dict, "iceLite", ODICT_BOOL, true);
+ if (error) {
+ return error;
+ }
+
+ // Create ICE parameters instance
+ return rawrtc_ice_parameters_create(parametersp, username_fragment, password, ice_lite);
+}
+
+static void ice_candidates_destroy(void* arg) {
+ struct rawrtc_ice_candidates* const candidates = arg;
+ size_t i;
+
+ // Un-reference each item
+ for (i = 0; i < candidates->n_candidates; ++i) {
+ mem_deref(candidates->candidates[i]);
+ }
+}
+
+/*
+ * Get ICE candidates from dictionary.
+ * Filter by enabled ICE candidate types if `client` argument is set to
+ * non-NULL.
+ */
+enum rawrtc_code get_ice_candidates(
+ struct rawrtc_ice_candidates** const candidatesp,
+ struct odict* const dict,
+ struct client* const client) {
+ size_t n;
+ struct rawrtc_ice_candidates* candidates;
+ enum rawrtc_code error = RAWRTC_CODE_SUCCESS;
+ struct le* le;
+
+ // Get length
+ n = list_count(&dict->lst) + 1;
+
+ // Allocate & set length immediately
+ // Note: We allocate more than we need in case ICE candidate types are being filtered but... meh
+ candidates = mem_zalloc(
+ sizeof(*candidates) + (sizeof(struct rawrtc_ice_candidate*) * n), ice_candidates_destroy);
+ if (!candidates) {
+ EWE("No memory to allocate ICE candidates array");
+ }
+ candidates->n_candidates = 0;
+
+ // Get ICE candidates
+ for (le = list_head(&dict->lst); le != NULL; le = le->next) {
+ struct odict* const node = ((struct odict_entry*) le->data)->u.odict;
+ char const* type_str = NULL;
+ enum rawrtc_ice_candidate_type type;
+ char* foundation;
+ uint32_t priority;
+ char* ip;
+ char const* protocol_str = NULL;
+ enum rawrtc_ice_protocol protocol;
+ uint16_t port;
+ char const* tcp_type_str = NULL;
+ enum rawrtc_ice_tcp_candidate_type tcp_type = RAWRTC_ICE_TCP_CANDIDATE_TYPE_ACTIVE;
+ char* related_address = NULL;
+ uint16_t related_port = 0;
+ struct rawrtc_ice_candidate* candidate;
+
+ // Get ICE candidate
+ error |= dict_get_entry(&type_str, node, "type", ODICT_STRING, true);
+ error |= rawrtc_str_to_ice_candidate_type(&type, type_str);
+ error |= dict_get_entry(&foundation, node, "foundation", ODICT_STRING, true);
+ error |= dict_get_uint32(&priority, node, "priority", true);
+ error |= dict_get_entry(&ip, node, "ip", ODICT_STRING, true);
+ error |= dict_get_entry(&protocol_str, node, "protocol", ODICT_STRING, true);
+ error |= rawrtc_str_to_ice_protocol(&protocol, protocol_str);
+ error |= dict_get_uint16(&port, node, "port", true);
+ if (protocol == RAWRTC_ICE_PROTOCOL_TCP) {
+ error |= dict_get_entry(&tcp_type_str, node, "tcpType", ODICT_STRING, true);
+ error |= rawrtc_str_to_ice_tcp_candidate_type(&tcp_type, tcp_type_str);
+ }
+ dict_get_entry(&related_address, node, "relatedAddress", ODICT_STRING, false);
+ dict_get_uint16(&related_port, node, "relatedPort", false);
+ if (error) {
+ goto out;
+ }
+
+ // Create and add ICE candidate
+ error = rawrtc_ice_candidate_create(
+ &candidate, foundation, priority, ip, protocol, port, type, tcp_type, related_address,
+ related_port);
+ if (error) {
+ goto out;
+ }
+
+ // Print ICE candidate
+ print_ice_candidate(candidate, NULL, NULL, client);
+
+ // Store if ICE candidate type enabled
+ if (ice_candidate_type_enabled(client, type)) {
+ candidates->candidates[candidates->n_candidates++] = candidate;
+ } else {
+ mem_deref(candidate);
+ }
+ }
+
+ // End-of-candidates
+ candidates->candidates[candidates->n_candidates++] = NULL;
+
+out:
+ if (error) {
+ mem_deref(candidates);
+ } else {
+ // Set pointer
+ *candidatesp = candidates;
+ }
+ return error;
+}
+
+static void dtls_fingerprints_destroy(void* arg) {
+ struct rawrtc_dtls_fingerprints* const fingerprints = arg;
+ size_t i;
+
+ // Un-reference each item
+ for (i = 0; i < fingerprints->n_fingerprints; ++i) {
+ mem_deref(fingerprints->fingerprints[i]);
+ }
+}
+
+/*
+ * Get DTLS parameters from dictionary.
+ */
+enum rawrtc_code get_dtls_parameters(
+ struct rawrtc_dtls_parameters** const parametersp, struct odict* const dict) {
+ size_t n;
+ struct rawrtc_dtls_parameters* parameters = NULL;
+ struct rawrtc_dtls_fingerprints* fingerprints;
+ enum rawrtc_code error;
+ char const* role_str = NULL;
+ enum rawrtc_dtls_role role;
+ struct odict* node;
+ struct le* le;
+ size_t i;
+
+ // Get fingerprints array and length
+ error = dict_get_entry(&node, dict, "fingerprints", ODICT_ARRAY, true);
+ if (error) {
+ return error;
+ }
+ n = list_count(&node->lst);
+
+ // Allocate & set length immediately
+ fingerprints = mem_zalloc(
+ sizeof(*fingerprints) + (sizeof(struct rawrtc_dtls_fingerprints*) * n),
+ dtls_fingerprints_destroy);
+ if (!fingerprints) {
+ EWE("No memory to allocate DTLS fingerprint array");
+ }
+ fingerprints->n_fingerprints = n;
+
+ // Get role
+ error |= dict_get_entry(&role_str, dict, "role", ODICT_STRING, true);
+ error |= rawrtc_str_to_dtls_role(&role, role_str);
+ if (error) {
+ role = RAWRTC_DTLS_ROLE_AUTO;
+ }
+
+ // Get fingerprints
+ for (le = list_head(&node->lst), i = 0; le != NULL; le = le->next, ++i) {
+ char* algorithm_str = NULL;
+ enum rawrtc_certificate_sign_algorithm algorithm;
+ char* value;
+ node = ((struct odict_entry*) le->data)->u.odict;
+
+ // Get fingerprint
+ error |= dict_get_entry(&algorithm_str, node, "algorithm", ODICT_STRING, true);
+ error |= rawrtc_str_to_certificate_sign_algorithm(&algorithm, algorithm_str);
+ error |= dict_get_entry(&value, node, "value", ODICT_STRING, true);
+ if (error) {
+ goto out;
+ }
+
+ // Create and add fingerprint
+ error = rawrtc_dtls_fingerprint_create(&fingerprints->fingerprints[i], algorithm, value);
+ if (error) {
+ goto out;
+ }
+ }
+
+ // Create DTLS parameters
+ error = rawrtc_dtls_parameters_create(
+ ¶meters, role, fingerprints->fingerprints, fingerprints->n_fingerprints);
+
+out:
+ mem_deref(fingerprints);
+
+ if (error) {
+ mem_deref(parameters);
+ } else {
+ // Set pointer
+ *parametersp = parameters;
+ }
+ return error;
+}
+
+/*
+ * Get SCTP parameters from dictionary.
+ */
+enum rawrtc_code get_sctp_parameters(
+ struct sctp_parameters* const parameters, struct odict* const dict) {
+ enum rawrtc_code error;
+ uint64_t max_message_size;
+
+ // Get maximum message size
+ error = dict_get_entry(&max_message_size, dict, "maxMessageSize", ODICT_INT, true);
+ if (error) {
+ return error;
+ }
+
+ // Get port
+ error = dict_get_uint16(¶meters->port, dict, "port", false);
+ if (error && error != RAWRTC_CODE_NO_VALUE) {
+ // Note: Nothing to do in NO VALUE case as port has been set to 0 by default
+ return error;
+ }
+
+ // Create SCTP capabilities instance
+ return rawrtc_sctp_capabilities_create(¶meters->capabilities, max_message_size);
+}
diff --git a/tools/helper/parameters.h b/tools/helper/parameters.h
new file mode 100644
index 0000000..bc6a5aa
--- /dev/null
+++ b/tools/helper/parameters.h
@@ -0,0 +1,67 @@
+#pragma once
+#include "common.h"
+#include <rawrtc.h>
+#include <rawrtcc.h>
+#include <rawrtcdc.h>
+#include <re.h>
+
+/*
+ * Set ICE parameters in dictionary.
+ */
+void set_ice_parameters(struct rawrtc_ice_parameters* const parameters, struct odict* const dict);
+
+/*
+ * Set ICE candidates in dictionary.
+ */
+void set_ice_candidates(struct rawrtc_ice_candidates* const parameters, struct odict* const array);
+
+/*
+ * Set DTLS parameters in dictionary.
+ */
+void set_dtls_parameters(struct rawrtc_dtls_parameters* const parameters, struct odict* const dict);
+
+/*
+ * Set SCTP parameters in dictionary.
+ */
+void set_sctp_parameters(
+ struct rawrtc_sctp_transport* const transport,
+ struct sctp_parameters* const parameters,
+ struct odict* const dict);
+
+#if RAWRTC_HAVE_SCTP_REDIRECT_TRANSPORT
+/*
+ * Set SCTP redirect parameters in dictionary.
+ */
+void set_sctp_redirect_parameters(
+ struct rawrtc_sctp_redirect_transport* const transport,
+ struct sctp_parameters* const parameters,
+ struct odict* const dict);
+#endif
+
+/*
+ * Get ICE parameters from dictionary.
+ */
+enum rawrtc_code get_ice_parameters(
+ struct rawrtc_ice_parameters** const parametersp, struct odict* const dict);
+
+/*
+ * Get ICE candidates from dictionary.
+ * Filter by enabled ICE candidate types if `client` argument is set to
+ * non-NULL.
+ */
+enum rawrtc_code get_ice_candidates(
+ struct rawrtc_ice_candidates** const candidatesp,
+ struct odict* const dict,
+ struct client* const client);
+
+/*
+ * Get DTLS parameters from dictionary.
+ */
+enum rawrtc_code get_dtls_parameters(
+ struct rawrtc_dtls_parameters** const parametersp, struct odict* const dict);
+
+/*
+ * Get SCTP parameters from dictionary.
+ */
+enum rawrtc_code get_sctp_parameters(
+ struct sctp_parameters* const parameters, struct odict* const dict);
diff --git a/tools/helper/utils.c b/tools/helper/utils.c
new file mode 100644
index 0000000..2c15b00
--- /dev/null
+++ b/tools/helper/utils.c
@@ -0,0 +1,380 @@
+#include "utils.h"
+#include "common.h"
+#include <rawrtc.h>
+#include <rawrtcc.h>
+#include <rawrtcdc.h>
+#include <re.h>
+#include <limits.h> // ULONG_MAX
+#include <stdlib.h> // strto*
+#include <string.h> // strlen
+
+#define DEBUG_MODULE "helper-utils"
+#define DEBUG_LEVEL 7
+#include <re_dbg.h>
+
+/*
+ * Convert string to uint16.
+ */
+bool str_to_uint16(uint16_t* const numberp, char* const str) {
+ char* end;
+ unsigned long number = strtoul(str, &end, 10);
+
+ // Check result (this function is insane, srsly...)
+ if (*end != '\0' || (number == ULONG_MAX && errno == ERANGE)) {
+ return false;
+ }
+
+ // Check bounds
+#if (ULONG_MAX > UINT16_MAX)
+ if (number > UINT16_MAX) {
+ return false;
+ }
+#endif
+
+ // Done
+ *numberp = (uint16_t) number;
+ return true;
+}
+
+/*
+ * Convert string to uint64.
+ */
+bool str_to_uint64(uint64_t* const numberp, char* const str) {
+ char* end;
+ unsigned long long number = strtoull(str, &end, 10);
+
+ // Check result (this function is insane, srsly...)
+ if (*end != '\0' || (number == ULONG_MAX && errno == ERANGE)) {
+ return false;
+ }
+
+ // Check bounds
+#if (ULONG_MAX > UINT64_MAX)
+ if (number > UINT64_MAX) {
+ return false;
+ }
+#endif
+
+ // Done
+ *numberp = (uint64_t) number;
+ return true;
+}
+
+/*
+ * Convert string to uint32.
+ */
+bool str_to_uint32(uint32_t* const numberp, char* const str) {
+ uint64_t number;
+ bool success = str_to_uint64(&number, str);
+
+ // Validate
+ if (!success || number > UINT32_MAX) {
+ return false;
+ }
+
+ // Done
+ *numberp = (uint32_t) number;
+ return true;
+}
+
+/*
+ * Get a dictionary entry and store it in `*valuep`.
+ */
+enum rawrtc_code dict_get_entry(
+ void* const valuep,
+ struct odict* const parent,
+ char* const key,
+ enum odict_type const type,
+ bool required) {
+ struct odict_entry const* entry;
+
+ // Check arguments
+ if (!valuep || !parent || !key) {
+ return RAWRTC_CODE_INVALID_ARGUMENT;
+ }
+
+ // Do lookup
+ entry = odict_lookup(parent, key);
+
+ // Check for entry
+ if (!entry) {
+ if (required) {
+ DEBUG_WARNING("'%s' missing\n", key);
+ return RAWRTC_CODE_INVALID_ARGUMENT;
+ } else {
+ return RAWRTC_CODE_NO_VALUE;
+ }
+ }
+
+ // Check for type
+ if (entry->type != type) {
+ DEBUG_WARNING("'%s' is of different type than expected\n", key);
+ return RAWRTC_CODE_INVALID_ARGUMENT;
+ }
+
+ // Set value according to type
+ switch (type) {
+ case ODICT_OBJECT:
+ case ODICT_ARRAY:
+ *((struct odict * * const) valuep) = entry->u.odict;
+ break;
+ case ODICT_STRING:
+ *((char** const) valuep) = entry->u.str;
+ break;
+ case ODICT_INT:
+ *((int64_t* const) valuep) = entry->u.integer;
+ break;
+ case ODICT_DOUBLE:
+ *((double* const) valuep) = entry->u.dbl;
+ break;
+ case ODICT_BOOL:
+ *((bool* const) valuep) = entry->u.boolean;
+ break;
+ case ODICT_NULL:
+ *((char** const) valuep) = NULL; // meh!
+ break;
+ default:
+ return RAWRTC_CODE_INVALID_ARGUMENT;
+ }
+
+ // Done
+ return RAWRTC_CODE_SUCCESS;
+}
+
+/*
+ * Get a uint32 entry and store it in `*valuep`.
+ */
+enum rawrtc_code dict_get_uint32(
+ uint32_t* const valuep, struct odict* const parent, char* const key, bool required) {
+ enum rawrtc_code error;
+ int64_t value;
+
+ // Check arguments
+ if (!valuep || !parent || !key) {
+ return RAWRTC_CODE_INVALID_ARGUMENT;
+ }
+
+ // Get int64_t
+ error = dict_get_entry(&value, parent, key, ODICT_INT, required);
+ if (error) {
+ return error;
+ }
+
+ // Check bounds
+ if (value < 0 || value > UINT32_MAX) {
+ return RAWRTC_CODE_INVALID_ARGUMENT;
+ }
+
+ // Set value & done
+ *valuep = (uint32_t) value;
+ return RAWRTC_CODE_SUCCESS;
+}
+
+/*
+ * Get a uint16 entry and store it in `*valuep`.
+ */
+enum rawrtc_code dict_get_uint16(
+ uint16_t* const valuep, struct odict* const parent, char* const key, bool required) {
+ enum rawrtc_code error;
+ int64_t value;
+
+ // Check arguments
+ if (!valuep || !parent || !key) {
+ return RAWRTC_CODE_INVALID_ARGUMENT;
+ }
+
+ // Get int64_t
+ error = dict_get_entry(&value, parent, key, ODICT_INT, required);
+ if (error) {
+ return error;
+ }
+
+ // Check bounds
+ if (value < 0 || value > UINT16_MAX) {
+ return RAWRTC_CODE_INVALID_ARGUMENT;
+ }
+
+ // Set value & done
+ *valuep = (uint16_t) value;
+ return RAWRTC_CODE_SUCCESS;
+}
+
+/*
+ * Get JSON from stdin and parse it to a dictionary.
+ */
+enum rawrtc_code get_json_stdin(struct odict** const dictp // de-referenced
+) {
+ char buffer[PARAMETERS_MAX_LENGTH];
+ size_t length;
+
+ // Get message from stdin
+ if (!fgets((char*) buffer, PARAMETERS_MAX_LENGTH, stdin)) {
+ EWE("Error polling stdin");
+ }
+ length = strlen(buffer);
+
+ // Exit?
+ if (length == 1 && buffer[0] == '\n') {
+ return RAWRTC_CODE_NO_VALUE;
+ }
+
+ // Decode JSON
+ EOR(json_decode_odict(dictp, 16, buffer, length, 3));
+ return RAWRTC_CODE_SUCCESS;
+}
+
+/*
+ * Get the ICE role from a string.
+ */
+enum rawrtc_code get_ice_role(
+ enum rawrtc_ice_role* const rolep, // de-referenced
+ char const* const str) {
+ // Get ICE role
+ switch (str[0]) {
+ case '0':
+ *rolep = RAWRTC_ICE_ROLE_CONTROLLED;
+ return RAWRTC_CODE_SUCCESS;
+ case '1':
+ *rolep = RAWRTC_ICE_ROLE_CONTROLLING;
+ return RAWRTC_CODE_SUCCESS;
+ default:
+ return RAWRTC_CODE_INVALID_ARGUMENT;
+ }
+}
+
+/*
+ * Get the congestion control algorithm from a string.
+ */
+enum rawrtc_code get_congestion_control_algorithm(
+ enum rawrtc_sctp_transport_congestion_ctrl* const algorithmp, // de-referenced
+ char const* const str) {
+ if (str_casecmp(str, "RFC2581") == 0) {
+ *algorithmp = RAWRTC_SCTP_TRANSPORT_CONGESTION_CTRL_RFC2581;
+ return RAWRTC_CODE_SUCCESS;
+ } else if (str_casecmp(str, "HSTCP") == 0) {
+ *algorithmp = RAWRTC_SCTP_TRANSPORT_CONGESTION_CTRL_HSTCP;
+ return RAWRTC_CODE_SUCCESS;
+ } else if (str_casecmp(str, "HTCP") == 0) {
+ *algorithmp = RAWRTC_SCTP_TRANSPORT_CONGESTION_CTRL_HTCP;
+ return RAWRTC_CODE_SUCCESS;
+ } else if (str_casecmp(str, "RTCC") == 0) {
+ *algorithmp = RAWRTC_SCTP_TRANSPORT_CONGESTION_CTRL_RTCC;
+ return RAWRTC_CODE_SUCCESS;
+ } else {
+ return RAWRTC_CODE_INVALID_ARGUMENT;
+ }
+}
+
+static void data_channel_helper_destroy(void* arg) {
+ struct data_channel_helper* const channel = arg;
+
+ // Unset handler argument & handlers of the channel
+ if (channel->channel) {
+ EOE(rawrtc_data_channel_unset_handlers(channel->channel));
+ }
+
+ // Remove from list
+ list_unlink(&channel->le);
+
+ // Un-reference
+ mem_deref(channel->arg);
+ mem_deref(channel->label);
+ mem_deref(channel->channel);
+}
+
+/*
+ * Create a data channel helper instance.
+ */
+void data_channel_helper_create(
+ struct data_channel_helper** const channel_helperp, // de-referenced
+ struct client* const client,
+ char* const label) {
+ // Allocate
+ struct data_channel_helper* const channel =
+ mem_zalloc(sizeof(*channel), data_channel_helper_destroy);
+ if (!channel) {
+ EOE(RAWRTC_CODE_NO_MEMORY);
+ return;
+ }
+
+ // Set fields
+ channel->client = client;
+ EOE(rawrtc_strdup(&channel->label, label));
+
+ // Set pointer & done
+ *channel_helperp = channel;
+}
+
+/*
+ * Create a data channel helper instance from parameters.
+ */
+void data_channel_helper_create_from_channel(
+ struct data_channel_helper** const channel_helperp, // de-referenced
+ struct rawrtc_data_channel* channel,
+ struct client* const client,
+ void* const arg // nullable
+) {
+ enum rawrtc_code error;
+ struct rawrtc_data_channel_parameters* parameters;
+ char* label;
+
+ // Allocate
+ struct data_channel_helper* const channel_helper =
+ mem_zalloc(sizeof(*channel_helper), data_channel_helper_destroy);
+ if (!channel_helper) {
+ EOE(RAWRTC_CODE_NO_MEMORY);
+ return;
+ }
+
+ // Get parameters
+ EOE(rawrtc_data_channel_get_parameters(¶meters, channel));
+
+ // Get & set label
+ error = rawrtc_data_channel_parameters_get_label(&label, parameters);
+ switch (error) {
+ case RAWRTC_CODE_SUCCESS:
+ EOE(rawrtc_strdup(&channel_helper->label, label));
+ mem_deref(label);
+ break;
+ case RAWRTC_CODE_NO_VALUE:
+ EOE(rawrtc_strdup(&channel_helper->label, "n/a"));
+ break;
+ default:
+ EOE(error);
+ }
+
+ // Set fields
+ channel_helper->client = client;
+ channel_helper->channel = channel;
+ channel_helper->arg = mem_ref(arg);
+
+ // Set pointer
+ *channel_helperp = channel_helper;
+
+ // Un-reference & done
+ mem_deref(parameters);
+}
+
+/*
+ * Add the ICE candidate to the remote ICE transport if the ICE
+ * candidate type is enabled.
+ */
+void add_to_other_if_ice_candidate_type_enabled(
+ struct client* const client,
+ struct rawrtc_ice_candidate* const candidate,
+ struct rawrtc_ice_transport* const transport) {
+ if (candidate) {
+ enum rawrtc_ice_candidate_type type;
+
+ // Get ICE candidate type
+ EOE(rawrtc_ice_candidate_get_type(&type, candidate));
+
+ // Add to other client as remote candidate (if type enabled)
+ if (ice_candidate_type_enabled(client, type)) {
+ EOE(rawrtc_ice_transport_add_remote_candidate(transport, candidate));
+ }
+ } else {
+ // Last candidate is always being added
+ EOE(rawrtc_ice_transport_add_remote_candidate(transport, candidate));
+ }
+}
diff --git a/tools/helper/utils.h b/tools/helper/utils.h
new file mode 100644
index 0000000..d521b80
--- /dev/null
+++ b/tools/helper/utils.h
@@ -0,0 +1,90 @@
+#pragma once
+#include "common.h"
+#include <rawrtc.h>
+#include <rawrtcc.h>
+#include <rawrtcdc.h>
+#include <re.h>
+
+/*
+ * Convert string to uint16.
+ */
+bool str_to_uint16(uint16_t* const numberp, char* const str);
+
+/*
+ * Convert string to uint64.
+ */
+bool str_to_uint64(uint64_t* const numberp, char* const str);
+
+/*
+ * Convert string to uint32.
+ */
+bool str_to_uint32(uint32_t* const numberp, char* const str);
+
+/*
+ * Get a dictionary entry and store it in `*valuep`.
+ */
+enum rawrtc_code dict_get_entry(
+ void* const valuep,
+ struct odict* const parent,
+ char* const key,
+ enum odict_type const type,
+ bool required);
+
+/*
+ * Get a uint32 entry and store it in `*valuep`.
+ */
+enum rawrtc_code dict_get_uint32(
+ uint32_t* const valuep, struct odict* const parent, char* const key, bool required);
+
+/*
+ * Get a uint16 entry and store it in `*valuep`.
+ */
+enum rawrtc_code dict_get_uint16(
+ uint16_t* const valuep, struct odict* const parent, char* const key, bool required);
+
+/*
+ * Get JSON from stdin and parse it to a dictionary.
+ */
+enum rawrtc_code get_json_stdin(struct odict** const dictp // de-referenced
+);
+
+/*
+ * Get the ICE role from a string.
+ */
+enum rawrtc_code get_ice_role(
+ enum rawrtc_ice_role* const rolep, // de-referenced
+ char const* const str);
+
+/*
+ * Get the congestion control algorithm from a string.
+ */
+enum rawrtc_code get_congestion_control_algorithm(
+ enum rawrtc_sctp_transport_congestion_ctrl* const algorithmp, // de-referenced
+ char const* const str);
+
+/*
+ * Create a data channel helper instance.
+ */
+void data_channel_helper_create(
+ struct data_channel_helper** const channel_helperp, // de-referenced
+ struct client* const client,
+ char* const label);
+
+/*
+ * Create a data channel helper instance from parameters.
+ */
+void data_channel_helper_create_from_channel(
+ struct data_channel_helper** const channel_helperp, // de-referenced
+ struct rawrtc_data_channel* channel,
+ struct client* const client,
+ void* const arg // nullable
+);
+
+/*
+ * Add the ICE candidate to the remote ICE transport if the ICE
+ * candidate type is enabled.
+ */
+void add_to_other_if_ice_candidate_type_enabled(
+ struct client* const client,
+ struct rawrtc_ice_candidate* const candidate,
+ struct rawrtc_ice_transport* const transport);