| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include <librist/librist.h> |
| 8 |
#include <librist/udpsocket.h> |
| 9 |
#include <stdint.h> |
| 10 |
#include "headers.h" |
| 11 |
#include "librist/version.h" |
| 12 |
#include "config.h" |
| 13 |
#if HAVE_SRP_SUPPORT |
| 14 |
#include "librist/librist_srp.h" |
| 15 |
#include "srp_shared.h" |
| 16 |
#endif |
| 17 |
#include "vcs_version.h" |
| 18 |
#include <errno.h> |
| 19 |
#include <fcntl.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <string.h> |
| 22 |
#include <sys/types.h> |
| 23 |
#include "getopt-shim.h" |
| 24 |
#include "pthread-shim.h" |
| 25 |
#include <stdbool.h> |
| 26 |
#include <signal.h> |
| 27 |
#include "risturlhelp.h" |
| 28 |
#define __STDC_FORMAT_MACROS |
| 29 |
#include <inttypes.h> |
| 30 |
#include "oob_shared.h" |
| 31 |
#include "prometheus-exporter.h" |
| 32 |
#include "rist-private.h" |
| 33 |
#include "endian-shim.h" |
| 34 |
#ifndef _WIN32 |
| 35 |
#include <sys/ioctl.h> |
| 36 |
#endif |
| 37 |
#include "yamlparse.h" |
| 38 |
#include "string-shim.h" |
| 39 |
|
| 40 |
#define RISTRECEIVER_VERSION "30" |
| 41 |
|
| 42 |
#define MAX_INPUT_COUNT 20 |
| 43 |
#define MAX_OUTPUT_COUNT 20 |
| 44 |
#define ReadEnd 0 |
| 45 |
#define WriteEnd 1 |
| 46 |
#define DATA_READ_MODE_CALLBACK 0 |
| 47 |
#define DATA_READ_MODE_POLL 1 |
| 48 |
#define DATA_READ_MODE_API 2 |
| 49 |
|
| 50 |
pthread_mutex_t signal_lock; |
| 51 |
static int signalReceived = 0; |
| 52 |
static struct rist_logging_settings logging_settings = LOGGING_SETTINGS_INITIALIZER; |
| 53 |
enum rist_profile profile = RIST_DEFAULT_PROFILE; |
| 54 |
static int peer_connected_count = 0; |
| 55 |
static int noflow_counter = 0; |
| 56 |
|
| 57 |
#if HAVE_PROMETHEUS_SUPPORT |
| 58 |
struct rist_prometheus_stats *prom_stats_ctx; |
| 59 |
bool prometheus_multipoint = false; |
| 60 |
bool prometheus_nocreated = false; |
| 61 |
bool prometheus_httpd = false; |
| 62 |
bool enable_prometheus = false; |
| 63 |
char *prometheus_tags = NULL; |
| 64 |
uint16_t prometheus_port = 9100; |
| 65 |
char *prometheus_ip = NULL; |
| 66 |
char *prometheus_unix_sock = NULL; |
| 67 |
#endif |
| 68 |
|
| 69 |
static struct option long_options[] = { |
| 70 |
{ "inputurl", required_argument, NULL, 'i' }, |
| 71 |
{ "outputurl", required_argument, NULL, 'o' }, |
| 72 |
{ "buffer", required_argument, NULL, 'b' }, |
| 73 |
{ "secret", required_argument, NULL, 's' }, |
| 74 |
{ "encryption-type", required_argument, NULL, 'e' }, |
| 75 |
{ "profile", required_argument, NULL, 'p' }, |
| 76 |
{ "tun", required_argument, NULL, 't' }, |
| 77 |
{ "tun-mode", required_argument, NULL, 'm' }, |
| 78 |
{ "stats", required_argument, NULL, 'S' }, |
| 79 |
{ "verbose-level", required_argument, NULL, 'v' }, |
| 80 |
{ "remote-logging", required_argument, NULL, 'r' }, |
| 81 |
#if HAVE_SRP_SUPPORT |
| 82 |
{ "srpfile", required_argument, NULL, 'F' }, |
| 83 |
#endif |
| 84 |
{ "config", required_argument, NULL, 'c' }, |
| 85 |
{ "session-timeout-exit", no_argument, NULL, 'x' }, |
| 86 |
{ "help", no_argument, NULL, 'h' }, |
| 87 |
{ "help-url", no_argument, NULL, 'u' }, |
| 88 |
#if HAVE_PROMETHEUS_SUPPORT |
| 89 |
{ "enable-metrics", no_argument, NULL, 'M' }, |
| 90 |
{ "metrics-tags", required_argument, NULL, 1 }, |
| 91 |
{ "metrics-multipoint",no_argument, (int*)&prometheus_multipoint, true }, |
| 92 |
{ "metrics-nocreated",no_argument, (int*)&prometheus_nocreated, true }, |
| 93 |
#if HAVE_LIBMICROHTTPD |
| 94 |
{ "metrics-http", no_argument, (int*)&prometheus_httpd, true }, |
| 95 |
{ "metrics-port", required_argument, NULL, 2 }, |
| 96 |
{ "metrics-ip", required_argument, NULL, 3 }, |
| 97 |
#endif |
| 98 |
#if HAVE_SOCK_UN_H |
| 99 |
{ "metrics-unix", required_argument, NULL, 4 }, |
| 100 |
#endif |
| 101 |
#endif |
| 102 |
{ 0, 0, 0, 0 }, |
| 103 |
}; |
| 104 |
|
| 105 |
const char help_str[] = "Usage: %s [OPTIONS] \nWhere OPTIONS are:\n" |
| 106 |
" -i | --inputurl rist://... * | Comma separated list of input rist URLs |\n" |
| 107 |
" -o | --outputurl udp://... or rtp://... * | Comma separated list of output udp or rtp URLs |\n" |
| 108 |
" | Use tun://@ to write udp data to a tun device defined |\n" |
| 109 |
" | using the -t option |\n" |
| 110 |
" -b | --buffer value | Default buffer size for packet retransmissions |\n" |
| 111 |
" -s | --secret PWD | Default pre-shared encryption secret |\n" |
| 112 |
" -e | --encryption-type TYPE | Default Encryption type (0, 128 = AES-128, 256 = AES-256)|\n" |
| 113 |
" -p | --profile number | Rist profile (0 = simple, 1 = main, 2 = advanced) |\n" |
| 114 |
" -S | --statsinterval value (ms) | Interval at which stats get printed, 0 to disable |\n" |
| 115 |
" -c | --config name.yaml | YAML config file |\n" |
| 116 |
" -v | --verbose-level value | To disable logging: -1, log levels match syslog levels |\n" |
| 117 |
" -r | --remote-logging IP:PORT | Send logs and stats to this IP:PORT using udp messages |\n" |
| 118 |
#if HAVE_SRP_SUPPORT |
| 119 |
" -F | --srpfile filepath | When in listening mode, use this file to hold the list |\n" |
| 120 |
" | of usernames and passwords to validate against. Use the |\n" |
| 121 |
" | ristsrppasswd tool to create the line entries. |\n" |
| 122 |
#endif |
| 123 |
" -t | --tun name | Create a tun device and use it for data communications |\n" |
| 124 |
" -m | --tun-mode number | Data management on the tun interface: |\n" |
| 125 |
" | 0 = all tun data is accepted into or out of oob channel |\n" |
| 126 |
" | 1 = only non udp data is accepted (default) |\n" |
| 127 |
" | 2 = no data goes into or out of oob channel |\n" |
| 128 |
#if HAVE_PROMETHEUS_SUPPORT |
| 129 |
" -M | --enable-metrics | Enable OpenMetrics/Prometheus compatible metrics |\n" |
| 130 |
" | --metrics-tags | Additional tags to add to the metrics |\n" |
| 131 |
" | --metrics-multipoint | Metrics return multiple timestamped data points |\n" |
| 132 |
" | --metrics-nocreated | Metrics skip the created metric for Prometheus compat |\n" |
| 133 |
#if HAVE_LIBMICROHTTPD |
| 134 |
" | --metrics-http | Start HTTP Server to expose metrics on |\n" |
| 135 |
" | defaults to http://0.0.0.0:9100/metrics |\n" |
| 136 |
" | --metrics-port | Port for metrics HTTP server to listen on |\n" |
| 137 |
" | --metrics-ip | IP for metrics HTTP server to listen on |\n" |
| 138 |
#endif |
| 139 |
#if HAVE_SOCK_UN_H |
| 140 |
" | --metrics-unix | Unix socket to expose metrics on |\n" |
| 141 |
#endif |
| 142 |
#endif |
| 143 |
" -f | --noflow-timeout-exit value (ms) | Exit on no flow after Timeout |\n" |
| 144 |
" -h | --help | Show this help |\n" |
| 145 |
" -u | --help-url | Show all the possible url options |\n" |
| 146 |
" * == mandatory value \n" |
| 147 |
"Default values: %s \n" |
| 148 |
" --profile 1 \\\n" |
| 149 |
" --statsinterval 1000 \\\n" |
| 150 |
" --verbose-level 6 \n"; |
| 151 |
|
| 152 |
static void usage(char *cmd) |
| 153 |
{ |
| 154 |
rist_log(&logging_settings, RIST_LOG_INFO, "%s\n%s version %s libRIST library: %s API version: %s\n", cmd, help_str, RISTRECEIVER_VERSION, librist_version(), librist_api_version()); |
| 155 |
exit(1); |
| 156 |
} |
| 157 |
|
| 158 |
struct rist_callback_object { |
| 159 |
int mpeg[MAX_OUTPUT_COUNT]; |
| 160 |
struct rist_udp_config *udp_config[MAX_OUTPUT_COUNT]; |
| 161 |
uint16_t i_seqnum[MAX_OUTPUT_COUNT]; |
| 162 |
struct rist_ctx *receiver_ctx; |
| 163 |
int tun; |
| 164 |
int tun_mode; |
| 165 |
int noflow_timeout_exit; |
| 166 |
}; |
| 167 |
|
| 168 |
static inline void risttools_rtp_set_hdr(uint8_t *p_rtp, uint8_t i_type, uint16_t i_seqnum, uint32_t i_timestamp, uint32_t i_ssrc) |
| 169 |
{ |
| 170 |
p_rtp[0] = 0x80; |
| 171 |
p_rtp[1] = i_type & 0x7f; |
| 172 |
p_rtp[2] = i_seqnum >> 8; |
| 173 |
p_rtp[3] = i_seqnum & 0xff; |
| 174 |
p_rtp[4] = (i_timestamp >> 24) & 0xff; |
| 175 |
p_rtp[5] = (i_timestamp >> 16) & 0xff; |
| 176 |
p_rtp[6] = (i_timestamp >> 8) & 0xff; |
| 177 |
p_rtp[7] = i_timestamp & 0xff; |
| 178 |
p_rtp[8] = (i_ssrc >> 24) & 0xff; |
| 179 |
p_rtp[9] = (i_ssrc >> 16) & 0xff; |
| 180 |
p_rtp[10] = (i_ssrc >> 8) & 0xff; |
| 181 |
p_rtp[11] = i_ssrc & 0xff; |
| 182 |
} |
| 183 |
|
| 184 |
static uint32_t risttools_convertNTPtoRTP(uint64_t i_ntp) |
| 185 |
{ |
| 186 |
i_ntp *= 90000; |
| 187 |
i_ntp = i_ntp >> 32; |
| 188 |
return (uint32_t)i_ntp; |
| 189 |
} |
| 190 |
|
| 191 |
static void connection_status_callback(void *arg, struct rist_peer *peer, enum rist_connection_status peer_connection_status) |
| 192 |
{ |
| 193 |
(void)arg; |
| 194 |
if (peer_connection_status == RIST_CONNECTION_ESTABLISHED || peer_connection_status == RIST_CLIENT_CONNECTED) { |
| 195 |
peer_connected_count++; |
| 196 |
} |
| 197 |
else { |
| 198 |
peer_connected_count--; |
| 199 |
} |
| 200 |
rist_log(&logging_settings, RIST_LOG_INFO,"Connection Status changed for Peer %"PRIu64", new status is %d, peer connected count is %d\n", |
| 201 |
peer, peer_connection_status, peer_connected_count); |
| 202 |
} |
| 203 |
|
| 204 |
static int cb_recv(void *arg, struct rist_data_block *b) |
| 205 |
{ |
| 206 |
struct rist_callback_object *callback_object = (void *) arg; |
| 207 |
int found = 0; |
| 208 |
int i = 0; |
| 209 |
for (i = 0; i < MAX_OUTPUT_COUNT; i++) { |
| 210 |
if (!callback_object->udp_config[i]) |
| 211 |
continue; |
| 212 |
struct rist_udp_config *udp_config = callback_object->udp_config[i]; |
| 213 |
bool found_it = false; |
| 214 |
int mux_mode = 0; |
| 215 |
if (udp_config->version == 1) |
| 216 |
mux_mode = udp_config->multiplex_mode; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
if (profile == RIST_PROFILE_SIMPLE || udp_config->stream_id == 0 || |
| 221 |
(mux_mode == LIBRIST_MULTIPLEX_MODE_VIRT_DESTINATION_PORT && udp_config->stream_id == b->virt_dst_port) || |
| 222 |
(mux_mode == LIBRIST_MULTIPLEX_MODE_VIRT_SOURCE_PORT && udp_config->stream_id == b->virt_src_port) || |
| 223 |
(mux_mode == LIBRIST_MULTIPLEX_MODE_IPV4)) |
| 224 |
{ |
| 225 |
|
| 226 |
found_it = true; |
| 227 |
} |
| 228 |
else if (mux_mode == LIBRIST_MULTIPLEX_MODE_AUTO) |
| 229 |
{ |
| 230 |
|
| 231 |
if (b->virt_src_port == UINT16_MAX) { |
| 232 |
mux_mode = LIBRIST_MULTIPLEX_MODE_IPV4; |
| 233 |
found_it = true; |
| 234 |
} |
| 235 |
if (b->virt_src_port < 32768 && udp_config->stream_id == b->virt_src_port) { |
| 236 |
mux_mode = LIBRIST_MULTIPLEX_MODE_VIRT_SOURCE_PORT; |
| 237 |
found_it = true; |
| 238 |
} |
| 239 |
else if (b->virt_src_port >= 32768 && udp_config->stream_id == b->virt_dst_port) { |
| 240 |
mux_mode = LIBRIST_MULTIPLEX_MODE_VIRT_DESTINATION_PORT; |
| 241 |
found_it = true; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
if (found_it) { |
| 246 |
if (callback_object->mpeg[i] > 0) { |
| 247 |
uint8_t *payload = NULL; |
| 248 |
size_t payload_len = 0; |
| 249 |
if (udp_config->rtp) { |
| 250 |
payload = malloc(12 + b->payload_len); |
| 251 |
payload_len = 12 + b->payload_len; |
| 252 |
|
| 253 |
memcpy(payload + 12, b->payload, b->payload_len); |
| 254 |
|
| 255 |
uint16_t i_seqnum = udp_config->rtp_sequence ? (uint16_t)b->seq : callback_object->i_seqnum[i]++; |
| 256 |
uint32_t i_timestamp = risttools_convertNTPtoRTP(b->ts_ntp); |
| 257 |
uint8_t ptype = 0x21; |
| 258 |
if (udp_config->rtp_ptype != 0) |
| 259 |
ptype = udp_config->rtp_ptype; |
| 260 |
risttools_rtp_set_hdr(payload, ptype, i_seqnum, i_timestamp, b->flow_id); |
| 261 |
} |
| 262 |
else if (mux_mode == LIBRIST_MULTIPLEX_MODE_IPV4) { |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
size_t ipheader_bytes = sizeof(struct ipheader) + sizeof(struct udpheader); |
| 268 |
payload = (uint8_t *)b->payload; |
| 269 |
payload += ipheader_bytes; |
| 270 |
payload_len = b->payload_len - ipheader_bytes; |
| 271 |
} |
| 272 |
else { |
| 273 |
payload = (uint8_t *)b->payload; |
| 274 |
payload_len = b->payload_len; |
| 275 |
} |
| 276 |
int ret = udpsocket_send(callback_object->mpeg[i], payload, payload_len); |
| 277 |
if (udp_config->rtp) |
| 278 |
free(payload); |
| 279 |
if (ret <= 0 && errno != ECONNREFUSED) |
| 280 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Error %d sending udp packet to socket %d\n", errno, callback_object->mpeg[i]); |
| 281 |
found = 1; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
if (b->virt_src_port == 1 && found == 0) |
| 287 |
{ |
| 288 |
|
| 289 |
if (callback_object->tun) { |
| 290 |
if (rist_tun_write(callback_object->tun, b->payload, b->payload_len) < 0) { |
| 291 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Error %d writing %d rist bytes to output tun\n", errno, b->payload_len); |
| 292 |
} |
| 293 |
} |
| 294 |
rist_receiver_data_block_free2(&b); |
| 295 |
return 0; |
| 296 |
} |
| 297 |
|
| 298 |
if (found == 0) |
| 299 |
{ |
| 300 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Destination port mismatch, no output found for %d\n", b->virt_dst_port); |
| 301 |
rist_receiver_data_block_free2(&b); |
| 302 |
return -1; |
| 303 |
} |
| 304 |
rist_receiver_data_block_free2(&b); |
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
static void intHandler(int signal) { |
| 309 |
pthread_mutex_lock(&signal_lock); |
| 310 |
signalReceived = signal; |
| 311 |
pthread_mutex_unlock(&signal_lock); |
| 312 |
} |
| 313 |
|
| 314 |
static int cb_auth_connect(void *arg, const char* connecting_ip, uint16_t connecting_port, const char* local_ip, uint16_t local_port, struct rist_peer *peer) |
| 315 |
{ |
| 316 |
struct rist_callback_object *callback_object = (void *) arg; |
| 317 |
uint16_t buffer[250]; |
| 318 |
char message[200]; |
| 319 |
int message_len = snprintf(message, 200, "auth,%s:%d,%s:%d", connecting_ip, connecting_port, local_ip, local_port); |
| 320 |
|
| 321 |
int ret = oob_build_api_payload(buffer, (char *)connecting_ip, (char *)local_ip, message, message_len); |
| 322 |
rist_log(&logging_settings, RIST_LOG_INFO,"Peer has been authenticated, sending oob/api message: %s\n", message); |
| 323 |
struct rist_oob_block oob_block; |
| 324 |
oob_block.peer = peer; |
| 325 |
oob_block.payload = buffer; |
| 326 |
oob_block.payload_len = ret; |
| 327 |
rist_oob_write(callback_object->receiver_ctx, &oob_block); |
| 328 |
return 0; |
| 329 |
} |
| 330 |
|
| 331 |
static int cb_auth_disconnect(void *arg, struct rist_peer *peer) |
| 332 |
{ |
| 333 |
(void)peer; |
| 334 |
struct rist_callback_object *callback_object = (void *) arg; |
| 335 |
(void)callback_object; |
| 336 |
return 0; |
| 337 |
} |
| 338 |
|
| 339 |
static int rist_validate_tun_data(uint8_t *buffer, ssize_t buffer_len) |
| 340 |
{ |
| 341 |
struct ipheader *ip = (struct ipheader *) buffer; |
| 342 |
int protocol = 0; |
| 343 |
ssize_t payload_len = 0; |
| 344 |
|
| 345 |
if (RIST_IPH_GET_VER(ip->iph_verlen) == 4) { |
| 346 |
protocol = (int) ip->iph_protocol; |
| 347 |
payload_len = (ssize_t)be16toh(ip->iph_len); |
| 348 |
if (payload_len != buffer_len) { |
| 349 |
rist_log(&logging_settings, RIST_LOG_INFO, "Malformed ipv4 packet %d != %d\n", |
| 350 |
payload_len != buffer_len); |
| 351 |
return -1; |
| 352 |
} |
| 353 |
} |
| 354 |
else if (RIST_IPH_GET_VER(ip->iph_verlen) == 6) { |
| 355 |
|
| 356 |
|
| 357 |
} |
| 358 |
else { |
| 359 |
rist_log(&logging_settings, RIST_LOG_INFO, "Unknown ipv? payload %d\n", |
| 360 |
RIST_IPH_GET_VER(ip->iph_verlen)); |
| 361 |
return -1; |
| 362 |
} |
| 363 |
return protocol; |
| 364 |
} |
| 365 |
|
| 366 |
static int cb_recv_oob(void *arg, const struct rist_oob_block *oob_block) |
| 367 |
{ |
| 368 |
struct rist_callback_object *callback_object = (void *) arg; |
| 369 |
int message_len = 0; |
| 370 |
char *message = oob_process_api_message((int)oob_block->payload_len, (char *)oob_block->payload, &message_len); |
| 371 |
if (message) { |
| 372 |
rist_log(&logging_settings, RIST_LOG_INFO,"Out-of-band api data received: %.*s\n", message_len, message); |
| 373 |
} |
| 374 |
else if (callback_object->tun) |
| 375 |
{ |
| 376 |
|
| 377 |
int protocol = rist_validate_tun_data((uint8_t *)oob_block->payload, oob_block->payload_len); |
| 378 |
if (protocol >= 0) |
| 379 |
{ |
| 380 |
if (callback_object->tun_mode == 0 || |
| 381 |
(callback_object->tun_mode == 1 && protocol != 17)) { |
| 382 |
if (rist_tun_write(callback_object->tun, oob_block->payload, oob_block->payload_len) < 0) { |
| 383 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Error %d writing %d bytes to output tun\n", errno, oob_block->payload_len); |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
return 0; |
| 389 |
} |
| 390 |
|
| 391 |
struct ristreceiver_flow_cumulative_stats { |
| 392 |
uint32_t flow_id; |
| 393 |
uint64_t received; |
| 394 |
uint64_t recovered; |
| 395 |
uint64_t lost; |
| 396 |
struct ristreceiver_flow_cumulative_stats *next; |
| 397 |
}; |
| 398 |
|
| 399 |
struct ristreceiver_flow_cumulative_stats *stats_list; |
| 400 |
|
| 401 |
static int session_timeout_callback(void *arg, uint32_t flow_id) { |
| 402 |
struct rist_callback_object *callback_object = (void *) arg; |
| 403 |
if (flow_id == 0 && callback_object->noflow_timeout_exit > 0) |
| 404 |
{ |
| 405 |
|
| 406 |
noflow_counter++; |
| 407 |
rist_log(&logging_settings, RIST_LOG_INFO, "No flow detected for %d ms, deadline is %d ms\n", noflow_counter * 1000, callback_object->noflow_timeout_exit); |
| 408 |
if (1000*noflow_counter >= callback_object->noflow_timeout_exit) |
| 409 |
{ |
| 410 |
rist_log(&logging_settings, RIST_LOG_INFO, "Timeout limit reached, exiting\n", noflow_counter); |
| 411 |
exit(1); |
| 412 |
} |
| 413 |
} |
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
static int cb_stats(void *arg, const struct rist_stats *stats_container) { |
| 418 |
rist_log(&logging_settings, RIST_LOG_INFO, "%s\n", stats_container->stats_json); |
| 419 |
if (stats_container->stats_type == RIST_STATS_RECEIVER_FLOW) |
| 420 |
{ |
| 421 |
struct ristreceiver_flow_cumulative_stats *stats = stats_list; |
| 422 |
struct ristreceiver_flow_cumulative_stats **prev = &stats_list; |
| 423 |
while (stats && stats->flow_id != stats_container->stats.receiver_flow.flow_id) |
| 424 |
{ |
| 425 |
prev = &stats->next; |
| 426 |
stats = stats->next; |
| 427 |
} |
| 428 |
if (!stats) { |
| 429 |
stats = calloc(1, sizeof(*stats)); |
| 430 |
stats->flow_id = stats_container->stats.receiver_flow.flow_id; |
| 431 |
*prev = stats; |
| 432 |
} |
| 433 |
stats->received += stats_container->stats.receiver_flow.received; |
| 434 |
stats->lost += stats_container->stats.receiver_flow.lost; |
| 435 |
stats->recovered += stats_container->stats.receiver_flow.recovered; |
| 436 |
|
| 437 |
rist_log(&logging_settings, RIST_LOG_INFO, |
| 438 |
"{\"flow_cumulative_stats\":{\"flow_id\":%"PRIu32",\"received\":%"PRIu64",\"recovered\":%"PRIu64",\"lost\":%"PRIu64"}}\n", |
| 439 |
stats->flow_id, stats->received, stats->recovered, stats->lost); |
| 440 |
#if HAVE_PROMETHEUS_SUPPORT |
| 441 |
if (prom_stats_ctx != NULL) |
| 442 |
rist_prometheus_parse_stats(prom_stats_ctx, stats_container, (uintptr_t)arg); |
| 443 |
#else |
| 444 |
(void)arg; |
| 445 |
#endif |
| 446 |
} |
| 447 |
rist_stats_free(stats_container); |
| 448 |
return 0; |
| 449 |
} |
| 450 |
|
| 451 |
static void rist_process_tun_data(struct rist_callback_object *callback_object, uint8_t *buffer, ssize_t buffer_len) |
| 452 |
{ |
| 453 |
int protocol = rist_validate_tun_data(buffer, buffer_len); |
| 454 |
if (protocol >=0) { |
| 455 |
|
| 456 |
if (callback_object->tun_mode == 0 || |
| 457 |
(callback_object->tun_mode == 1 && protocol != 17)) { |
| 458 |
struct rist_oob_block oob_block = { 0 }; |
| 459 |
oob_block.peer = NULL; |
| 460 |
oob_block.payload = &buffer[0]; |
| 461 |
oob_block.payload_len = buffer_len; |
| 462 |
if (rist_oob_write(callback_object->receiver_ctx, &oob_block) < 0) |
| 463 |
rist_log(&logging_settings, RIST_LOG_INFO, "Error writing %d bytes to rist_oob_write\n", buffer_len); |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
static PTHREAD_START_FUNC(tun_loop, arg) |
| 469 |
{ |
| 470 |
struct rist_callback_object *callback_object = (void *) arg; |
| 471 |
while (!signalReceived) { |
| 472 |
uint8_t buffer[RIST_MAX_PACKET_SIZE]; |
| 473 |
fd_set read_fds; |
| 474 |
FD_ZERO(&read_fds); |
| 475 |
FD_SET(callback_object->tun, &read_fds); |
| 476 |
|
| 477 |
struct timeval timeout; |
| 478 |
timeout.tv_sec = 0; |
| 479 |
timeout.tv_usec = 100000; |
| 480 |
|
| 481 |
if (select(FD_SETSIZE, &read_fds, NULL, NULL, &timeout) == 1) |
| 482 |
{ |
| 483 |
ssize_t r = rist_tun_read(callback_object->tun, &buffer[0], RIST_MAX_PACKET_SIZE); |
| 484 |
if (r > 0) { |
| 485 |
rist_process_tun_data(callback_object, &buffer[0], r); |
| 486 |
} |
| 487 |
else |
| 488 |
break; |
| 489 |
} |
| 490 |
} |
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
int main(int argc, char *argv[]) |
| 495 |
{ |
| 496 |
int option_index; |
| 497 |
int c; |
| 498 |
int data_read_mode = DATA_READ_MODE_CALLBACK; |
| 499 |
const struct rist_peer_config *peer_input_config[MAX_INPUT_COUNT]; |
| 500 |
char *inputurl = NULL; |
| 501 |
char *outputurl = NULL; |
| 502 |
char *oobtun = NULL; |
| 503 |
char *shared_secret = NULL; |
| 504 |
int buffer = 0; |
| 505 |
int encryption_type = 0; |
| 506 |
struct rist_callback_object callback_object = { 0 }; |
| 507 |
enum rist_log_level loglevel = RIST_LOG_INFO; |
| 508 |
int statsinterval = 1000; |
| 509 |
char *remote_log_address = NULL; |
| 510 |
if (pthread_mutex_init(&signal_lock, NULL) != 0) |
| 511 |
{ |
| 512 |
fprintf(stderr, "Could not initialize signal lock\n"); |
| 513 |
exit(1); |
| 514 |
} |
| 515 |
#ifndef _WIN32 |
| 516 |
|
| 517 |
int receiver_pipe[2]; |
| 518 |
#endif |
| 519 |
rist_tools_config_object *yaml_config = NULL; |
| 520 |
char *yamlfile = NULL; |
| 521 |
|
| 522 |
#if HAVE_SRP_SUPPORT |
| 523 |
char *srpfile = NULL; |
| 524 |
#endif |
| 525 |
|
| 526 |
for (size_t i = 0; i < MAX_OUTPUT_COUNT; i++) |
| 527 |
{ |
| 528 |
callback_object.mpeg[i] = 0; |
| 529 |
callback_object.udp_config[i] = NULL; |
| 530 |
} |
| 531 |
#ifdef _WIN32 |
| 532 |
#define STDERR_FILENO 2 |
| 533 |
signal(SIGINT, intHandler); |
| 534 |
signal(SIGTERM, intHandler); |
| 535 |
signal(SIGABRT, intHandler); |
| 536 |
#else |
| 537 |
struct sigaction act = { {0} }; |
| 538 |
act.sa_handler = intHandler; |
| 539 |
sigaction(SIGINT, &act, NULL); |
| 540 |
#endif |
| 541 |
|
| 542 |
struct rist_logging_settings *log_ptr = &logging_settings; |
| 543 |
if (rist_logging_set(&log_ptr, loglevel, NULL, NULL, NULL, |
| 544 |
stderr) != 0) { |
| 545 |
fprintf(stderr, "Failed to setup default logging!\n"); |
| 546 |
exit(1); |
| 547 |
} |
| 548 |
|
| 549 |
rist_log(&logging_settings, RIST_LOG_INFO, "Starting ristreceiver version: %s libRIST library: %s API version: %s\n", RISTRECEIVER_VERSION, librist_version(), librist_api_version()); |
| 550 |
|
| 551 |
while ((c = getopt_long(argc, argv, "r:i:o:b:s:e:t:m:p:S:v:F:c:h:x:uM", long_options, &option_index)) != -1) { |
| 552 |
switch (c) { |
| 553 |
case 'i': |
| 554 |
inputurl = strdup(optarg); |
| 555 |
break; |
| 556 |
case 'o': |
| 557 |
outputurl = strdup(optarg); |
| 558 |
break; |
| 559 |
case 'b': |
| 560 |
buffer = atoi(optarg); |
| 561 |
break; |
| 562 |
case 's': |
| 563 |
shared_secret = strdup(optarg); |
| 564 |
break; |
| 565 |
case 'e': |
| 566 |
encryption_type = atoi(optarg); |
| 567 |
break; |
| 568 |
case 't': |
| 569 |
oobtun = strdup(optarg); |
| 570 |
break; |
| 571 |
case 'm': |
| 572 |
callback_object.tun_mode = atoi(optarg); |
| 573 |
break; |
| 574 |
case 'p': |
| 575 |
profile = atoi(optarg); |
| 576 |
break; |
| 577 |
case 'S': |
| 578 |
statsinterval = atoi(optarg); |
| 579 |
break; |
| 580 |
case 'v': |
| 581 |
loglevel = atoi(optarg); |
| 582 |
break; |
| 583 |
case 'r': |
| 584 |
remote_log_address = strdup(optarg); |
| 585 |
break; |
| 586 |
#if HAVE_SRP_SUPPORT |
| 587 |
case 'F': { |
| 588 |
FILE* f = fopen(optarg, "r"); |
| 589 |
if (!f) { |
| 590 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not open srp file %s\n", optarg); |
| 591 |
return 1; |
| 592 |
} |
| 593 |
srpfile = strdup(optarg); |
| 594 |
} |
| 595 |
break; |
| 596 |
#endif |
| 597 |
case 'u': |
| 598 |
rist_log(&logging_settings, RIST_LOG_INFO, "%s", help_urlstr); |
| 599 |
exit(1); |
| 600 |
break; |
| 601 |
#if HAVE_PROMETHEUS_SUPPORT |
| 602 |
case 'M': |
| 603 |
enable_prometheus = true; |
| 604 |
break; |
| 605 |
case 0: |
| 606 |
|
| 607 |
break; |
| 608 |
case 1: |
| 609 |
|
| 610 |
prometheus_tags = strdup(optarg); |
| 611 |
break; |
| 612 |
case 2: |
| 613 |
|
| 614 |
prometheus_httpd = true; |
| 615 |
prometheus_port = atoi(optarg); |
| 616 |
break; |
| 617 |
case 3: |
| 618 |
|
| 619 |
prometheus_httpd = true; |
| 620 |
prometheus_ip = strdup(optarg); |
| 621 |
break; |
| 622 |
case 4: |
| 623 |
|
| 624 |
enable_prometheus = true; |
| 625 |
prometheus_unix_sock = strdup(optarg); |
| 626 |
break; |
| 627 |
#endif |
| 628 |
case 'c': |
| 629 |
yamlfile = strdup(optarg); |
| 630 |
yaml_config = parse_yaml(yamlfile); |
| 631 |
free(yamlfile); |
| 632 |
if (!yaml_config){ |
| 633 |
fprintf(stderr,"Could not import yaml file %s\n",optarg); |
| 634 |
cleanup_tools_config(yaml_config); |
| 635 |
exit(1); |
| 636 |
} |
| 637 |
if (yaml_config->input_url) |
| 638 |
inputurl = strdup(yaml_config->input_url); |
| 639 |
if (yaml_config->output_url) |
| 640 |
outputurl = strdup(yaml_config->output_url); |
| 641 |
buffer = yaml_config->buffer; |
| 642 |
if (yaml_config->secret) |
| 643 |
shared_secret = strdup(yaml_config->secret); |
| 644 |
encryption_type = yaml_config->encryption_type; |
| 645 |
loglevel = yaml_config->verbose_level; |
| 646 |
if (yaml_config->remote_log_address) |
| 647 |
remote_log_address = strdup(yaml_config->remote_log_address); |
| 648 |
profile = yaml_config->profile; |
| 649 |
statsinterval = yaml_config->stats_interval; |
| 650 |
|
| 651 |
|
| 652 |
if (yaml_config->tunnel_interface) |
| 653 |
oobtun = strdup(yaml_config->tunnel_interface); |
| 654 |
#if HAVE_SRP_SUPPORT |
| 655 |
if (yaml_config->srp_file) |
| 656 |
srpfile = strdup(yaml_config->srp_file); |
| 657 |
#endif |
| 658 |
#if HAVE_PROMETHEUS_SUPPORT |
| 659 |
enable_prometheus = yaml_config->enable_metrics; |
| 660 |
if (yaml_config->metrics_tags) |
| 661 |
prometheus_tags = strdup(yaml_config->metrics_tags); |
| 662 |
prometheus_multipoint = yaml_config->metrics_multipoint; |
| 663 |
prometheus_nocreated = yaml_config->metrics_nocreated; |
| 664 |
#if HAVE_LIBMICROHTTPD |
| 665 |
prometheus_httpd = yaml_config->metrics_http; |
| 666 |
prometheus_port = yaml_config->metrics_port; |
| 667 |
if (yaml_config->metrics_ip) |
| 668 |
prometheus_ip = strdup(yaml_config->metrics_ip); |
| 669 |
#endif |
| 670 |
#if HAVE_SOCK_UN_H |
| 671 |
if (yaml_config->metrics_unix) |
| 672 |
prometheus_unix_sock = strdup(yaml_config->metrics_unix); |
| 673 |
#endif |
| 674 |
#endif |
| 675 |
cleanup_tools_config(yaml_config); |
| 676 |
break; |
| 677 |
case 'x': |
| 678 |
callback_object.noflow_timeout_exit = atoi(optarg); |
| 679 |
break; |
| 680 |
case 'h': |
| 681 |
|
| 682 |
default: |
| 683 |
usage(argv[0]); |
| 684 |
break; |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
if (inputurl == NULL || outputurl == NULL) { |
| 689 |
usage(argv[0]); |
| 690 |
} |
| 691 |
|
| 692 |
if (argc < 2) { |
| 693 |
usage(argv[0]); |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
if (rist_logging_set(&log_ptr, loglevel, NULL, NULL, remote_log_address, stderr) != 0) { |
| 698 |
fprintf(stderr,"Failed to setup logging!\n"); |
| 699 |
exit(1); |
| 700 |
} |
| 701 |
|
| 702 |
#if HAVE_PROMETHEUS_SUPPORT |
| 703 |
if (enable_prometheus || prometheus_httpd) { |
| 704 |
rist_log(log_ptr, RIST_LOG_INFO, "Enabling Metrics output\n"); |
| 705 |
struct prometheus_httpd_options httpd_opt; |
| 706 |
httpd_opt.enabled = prometheus_httpd; |
| 707 |
httpd_opt.port = prometheus_port; |
| 708 |
httpd_opt.ip = prometheus_ip; |
| 709 |
prom_stats_ctx = rist_setup_prometheus_stats(log_ptr, prometheus_tags,prometheus_multipoint, prometheus_nocreated, &httpd_opt, prometheus_unix_sock); |
| 710 |
if (prom_stats_ctx == NULL) { |
| 711 |
rist_log(log_ptr, RIST_LOG_ERROR, "Failed to setup Metrics output\n"); |
| 712 |
exit(1); |
| 713 |
} |
| 714 |
} |
| 715 |
#endif |
| 716 |
|
| 717 |
|
| 718 |
struct rist_ctx *ctx; |
| 719 |
if (rist_receiver_create(&ctx, profile, &logging_settings) != 0) { |
| 720 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not create rist receiver context\n"); |
| 721 |
exit(1); |
| 722 |
} |
| 723 |
|
| 724 |
callback_object.receiver_ctx = ctx; |
| 725 |
|
| 726 |
if (rist_auth_handler_set(ctx, cb_auth_connect, cb_auth_disconnect, (void *)&callback_object) != 0) { |
| 727 |
|
| 728 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not init rist auth handler\n"); |
| 729 |
exit(1); |
| 730 |
} |
| 731 |
|
| 732 |
if (rist_connection_status_callback_set(ctx, connection_status_callback, NULL) == -1) { |
| 733 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not initialize rist connection status callback\n"); |
| 734 |
exit(1); |
| 735 |
} |
| 736 |
|
| 737 |
if (profile != RIST_PROFILE_SIMPLE) { |
| 738 |
if (rist_oob_callback_set(ctx, cb_recv_oob, (void *)&callback_object) == -1) { |
| 739 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not add enable out-of-band data\n"); |
| 740 |
exit(1); |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
if (rist_stats_callback_set(ctx, statsinterval, cb_stats, (void*)0) == -1) { |
| 745 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not enable stats callback\n"); |
| 746 |
exit(1); |
| 747 |
} |
| 748 |
|
| 749 |
double api_version = strtod(librist_api_version(), NULL); |
| 750 |
if (api_version > 4.5) |
| 751 |
{ |
| 752 |
if (rist_receiver_session_timeout_callback_set(ctx, session_timeout_callback, (void *)&callback_object) == -1) { |
| 753 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not enable session timeout callback\n"); |
| 754 |
exit(1); |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
if (oobtun) { |
| 760 |
char tun_ifname[64] = {0}; |
| 761 |
callback_object.tun = rist_tun_open(oobtun, tun_ifname, sizeof(tun_ifname)); |
| 762 |
if (callback_object.tun >= 0) |
| 763 |
rist_tun_bring_up(tun_ifname); |
| 764 |
if (callback_object.tun < 0) { |
| 765 |
rist_log(&logging_settings, RIST_LOG_ERROR, "tun open error: %s\n", strerror(errno)); |
| 766 |
exit(1); |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
char *saveptr1; |
| 771 |
char *inputtoken = strtok_r(inputurl, ",", &saveptr1); |
| 772 |
for (size_t i = 0; i < MAX_INPUT_COUNT; i++) { |
| 773 |
if (!inputtoken) |
| 774 |
break; |
| 775 |
|
| 776 |
|
| 777 |
struct rist_peer_config *peer_config = NULL; |
| 778 |
if (rist_parse_address2(inputtoken, &peer_config)) |
| 779 |
{ |
| 780 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not parse peer options for receiver #%d\n", (int)(i + 1)); |
| 781 |
exit(1); |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
struct rist_peer_config *overrides_peer_config = peer_config; |
| 786 |
if (shared_secret && peer_config->secret[0] == 0) { |
| 787 |
strncpy(overrides_peer_config->secret, shared_secret, RIST_MAX_STRING_SHORT -1); |
| 788 |
if (encryption_type) |
| 789 |
overrides_peer_config->key_size = encryption_type; |
| 790 |
else if (!overrides_peer_config->key_size) |
| 791 |
overrides_peer_config->key_size = 128; |
| 792 |
} |
| 793 |
if (buffer) { |
| 794 |
overrides_peer_config->recovery_length_min = buffer; |
| 795 |
overrides_peer_config->recovery_length_max = buffer; |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
rist_log(&logging_settings, RIST_LOG_INFO, "Link configured with maxrate=%d bufmin=%d bufmax=%d reorder=%d rttmin=%d rttmax=%d congestion_control=%d min_retries=%d max_retries=%d\n", |
| 800 |
peer_config->recovery_maxbitrate, peer_config->recovery_length_min, peer_config->recovery_length_max, |
| 801 |
peer_config->recovery_reorder_buffer, peer_config->recovery_rtt_min,peer_config->recovery_rtt_max, |
| 802 |
peer_config->congestion_control_mode, peer_config->min_retries, peer_config->max_retries); |
| 803 |
|
| 804 |
peer_input_config[i] = peer_config; |
| 805 |
|
| 806 |
struct rist_peer *peer; |
| 807 |
if (rist_peer_create(ctx, &peer, peer_input_config[i]) == -1) { |
| 808 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not add peer connector to receiver #%i\n", (int)(i + 1)); |
| 809 |
exit(1); |
| 810 |
} |
| 811 |
#if HAVE_SRP_SUPPORT |
| 812 |
int srp_error = 0; |
| 813 |
if (profile != RIST_PROFILE_SIMPLE) { |
| 814 |
if (strlen(peer_config->srp_username) > 0 && strlen(peer_config->srp_password) > 0) |
| 815 |
{ |
| 816 |
srp_error = rist_enable_eap_srp_2(peer, peer_config->srp_username, peer_config->srp_password, NULL, NULL); |
| 817 |
if (srp_error) |
| 818 |
rist_log(&logging_settings, RIST_LOG_WARN, "Error %d trying to enable SRP for peer\n", srp_error); |
| 819 |
} |
| 820 |
if (srpfile) |
| 821 |
{ |
| 822 |
srp_error = rist_enable_eap_srp_2(peer, NULL, NULL, user_verifier_lookup, srpfile); |
| 823 |
if (srp_error) |
| 824 |
rist_log(&logging_settings, RIST_LOG_WARN, "Error %d trying to enable SRP global authenticator, file %s\n", srp_error, srpfile); |
| 825 |
} |
| 826 |
} |
| 827 |
else |
| 828 |
rist_log(&logging_settings, RIST_LOG_WARN, "SRP Authentication is not available for Rist Simple Profile\n"); |
| 829 |
#endif |
| 830 |
|
| 831 |
rist_peer_config_free2(&peer_config); |
| 832 |
inputtoken = strtok_r(NULL, ",", &saveptr1); |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
bool atleast_one_socket_opened = false; |
| 837 |
char *saveptr2; |
| 838 |
char *outputtoken = strtok_r(outputurl, ",", &saveptr2); |
| 839 |
for (size_t i = 0; i < MAX_OUTPUT_COUNT; i++) { |
| 840 |
|
| 841 |
if (!outputtoken) |
| 842 |
break; |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
struct rist_udp_config *udp_config = NULL; |
| 849 |
if (rist_parse_udp_address2(outputtoken, &udp_config)) { |
| 850 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not parse outputurl %s\n", outputtoken); |
| 851 |
goto next; |
| 852 |
} |
| 853 |
|
| 854 |
if (strcmp(udp_config->prefix, "tun") == 0) { |
| 855 |
if (!callback_object.tun) { |
| 856 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Detected 'tun://' usage in output url but '--tun' argument was not given\n"); |
| 857 |
exit(1); |
| 858 |
} |
| 859 |
atleast_one_socket_opened = true; |
| 860 |
goto next; |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
char hostname[200] = {0}; |
| 865 |
int outputlisten; |
| 866 |
uint16_t outputport; |
| 867 |
if (udpsocket_parse_url((void *)udp_config->address, hostname, 200, &outputport, &outputlisten) || !outputport || strlen(hostname) == 0) { |
| 868 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not parse output url %s\n", outputtoken); |
| 869 |
goto next; |
| 870 |
} |
| 871 |
rist_log(&logging_settings, RIST_LOG_INFO, "URL parsed successfully: Host %s, Port %d\n", (char *) hostname, outputport); |
| 872 |
|
| 873 |
|
| 874 |
callback_object.mpeg[i] = udpsocket_open_connect(hostname, outputport, udp_config->miface); |
| 875 |
if (callback_object.mpeg[i] < 0) { |
| 876 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not connect to: Host %s, Port %d\n", (char *) hostname, outputport); |
| 877 |
goto next; |
| 878 |
} else { |
| 879 |
rist_log(&logging_settings, RIST_LOG_INFO, "Output socket is open and bound %s:%d\n", (char *) hostname, outputport); |
| 880 |
atleast_one_socket_opened = true; |
| 881 |
} |
| 882 |
callback_object.udp_config[i] = udp_config; |
| 883 |
|
| 884 |
next: |
| 885 |
outputtoken = strtok_r(NULL, ",", &saveptr2); |
| 886 |
} |
| 887 |
|
| 888 |
if (!atleast_one_socket_opened) { |
| 889 |
exit(1); |
| 890 |
} |
| 891 |
|
| 892 |
pthread_t thread_tun_loop = { 0 }; |
| 893 |
if (callback_object.tun && pthread_create(&thread_tun_loop, NULL, tun_loop, (void *)&callback_object) != 0) |
| 894 |
{ |
| 895 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not start tun read thread\n"); |
| 896 |
exit(1); |
| 897 |
} |
| 898 |
|
| 899 |
if (data_read_mode == DATA_READ_MODE_CALLBACK) { |
| 900 |
if (rist_receiver_data_callback_set2(ctx, cb_recv, &callback_object)) |
| 901 |
{ |
| 902 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not set data_callback pointer\n"); |
| 903 |
exit(1); |
| 904 |
} |
| 905 |
} |
| 906 |
#ifndef _WIN32 |
| 907 |
else if (data_read_mode == DATA_READ_MODE_POLL) { |
| 908 |
if (pipe(receiver_pipe)) |
| 909 |
{ |
| 910 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not create pipe for file descriptor channel\n"); |
| 911 |
exit(1); |
| 912 |
} |
| 913 |
if (fcntl(receiver_pipe[WriteEnd], F_SETFL, O_NONBLOCK) < 0) |
| 914 |
{ |
| 915 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not set pipe to non blocking mode\n"); |
| 916 |
exit(1); |
| 917 |
} |
| 918 |
if (fcntl(receiver_pipe[ReadEnd], F_SETFL, O_NONBLOCK) < 0) |
| 919 |
{ |
| 920 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not set pipe to non blocking mode\n"); |
| 921 |
exit(1); |
| 922 |
} |
| 923 |
if (rist_receiver_data_notify_fd_set(ctx, receiver_pipe[WriteEnd])) |
| 924 |
{ |
| 925 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not set file descriptor channel\n"); |
| 926 |
exit(1); |
| 927 |
} |
| 928 |
} |
| 929 |
#endif |
| 930 |
|
| 931 |
if (rist_start(ctx)) { |
| 932 |
rist_log(&logging_settings, RIST_LOG_ERROR, "Could not start rist receiver\n"); |
| 933 |
exit(1); |
| 934 |
} |
| 935 |
|
| 936 |
if (data_read_mode == DATA_READ_MODE_CALLBACK) { |
| 937 |
#ifdef _WIN32 |
| 938 |
system("pause"); |
| 939 |
#else |
| 940 |
pause(); |
| 941 |
#endif |
| 942 |
} |
| 943 |
else if (data_read_mode == DATA_READ_MODE_API) { |
| 944 |
#ifndef _WIN32 |
| 945 |
int prio_max = sched_get_priority_max(SCHED_RR); |
| 946 |
struct sched_param param = { 0 }; |
| 947 |
param.sched_priority = prio_max; |
| 948 |
if (pthread_setschedparam(pthread_self(), SCHED_RR, ¶m) != 0) |
| 949 |
rist_log(&logging_settings, RIST_LOG_WARN, "Failed to set data output thread to RR scheduler with prio of %i\n", prio_max); |
| 950 |
#else |
| 951 |
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); |
| 952 |
#endif |
| 953 |
|
| 954 |
while (true) |
| 955 |
{ |
| 956 |
struct rist_data_block *b = NULL; |
| 957 |
int queue_size = rist_receiver_data_read2(ctx, &b, 5); |
| 958 |
if (queue_size > 0) { |
| 959 |
if (queue_size % 10 == 0 || queue_size > 50) { |
| 960 |
|
| 961 |
uint32_t flow_id = b ? b->flow_id : 0; |
| 962 |
rist_log(&logging_settings, RIST_LOG_WARN, "Falling behind on rist_receiver_data_read: count %d, flow id %u\n", queue_size, flow_id); |
| 963 |
} |
| 964 |
if (b && b->payload) cb_recv(&callback_object, b); |
| 965 |
} |
| 966 |
pthread_mutex_lock(&signal_lock); |
| 967 |
if (signalReceived) |
| 968 |
{ |
| 969 |
rist_log(&logging_settings, RIST_LOG_INFO, "Signal %d received\n", signal); |
| 970 |
break; |
| 971 |
} |
| 972 |
pthread_mutex_unlock(&signal_lock); |
| 973 |
} |
| 974 |
} |
| 975 |
#ifndef _WIN32 |
| 976 |
else if (data_read_mode == DATA_READ_MODE_POLL) { |
| 977 |
char pipebuffer[256]; |
| 978 |
fd_set readfds; |
| 979 |
struct timeval timeout; |
| 980 |
timeout.tv_sec = 0; |
| 981 |
timeout.tv_usec = 5000; |
| 982 |
while (true) { |
| 983 |
FD_ZERO(&readfds); |
| 984 |
FD_SET(receiver_pipe[ReadEnd], &readfds); |
| 985 |
int ret = select(FD_SETSIZE, &readfds, NULL, NULL, &timeout); |
| 986 |
if (ret == -1 && errno != EINTR) { |
| 987 |
fprintf(stderr, "Pipe read error %d, exiting\n", errno); |
| 988 |
break; |
| 989 |
} |
| 990 |
else if (ret == 0) { |
| 991 |
|
| 992 |
continue; |
| 993 |
} |
| 994 |
|
| 995 |
for (;;) { |
| 996 |
if (read(receiver_pipe[ReadEnd], &pipebuffer, sizeof(pipebuffer)) <= 0) { |
| 997 |
if (errno != EAGAIN) |
| 998 |
fprintf(stderr, "Error reading data from pipe: %d\n", errno); |
| 999 |
break; |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
struct rist_data_block *b = NULL; |
| 1004 |
int queue_size = 0; |
| 1005 |
for (;;) { |
| 1006 |
queue_size = rist_receiver_data_read2(ctx, &b, 0); |
| 1007 |
if (queue_size > 0) { |
| 1008 |
if (queue_size % 10 == 0 || queue_size > 50) { |
| 1009 |
|
| 1010 |
uint32_t flow_id = b ? b->flow_id : 0; |
| 1011 |
rist_log(&logging_settings, RIST_LOG_WARN, "Falling behind on rist_receiver_data_read: count %d, flow id %u\n", queue_size, flow_id); |
| 1012 |
} |
| 1013 |
if (b && b->payload) cb_recv(&callback_object, b); |
| 1014 |
} |
| 1015 |
else |
| 1016 |
break; |
| 1017 |
} |
| 1018 |
pthread_mutex_lock(&signal_lock); |
| 1019 |
if (signalReceived) |
| 1020 |
{ |
| 1021 |
rist_log(&logging_settings, RIST_LOG_INFO, "Signal %d received\n", signal); |
| 1022 |
break; |
| 1023 |
} |
| 1024 |
pthread_mutex_unlock(&signal_lock); |
| 1025 |
} |
| 1026 |
} |
| 1027 |
#endif |
| 1028 |
rist_destroy(ctx); |
| 1029 |
|
| 1030 |
for (size_t i = 0; i < MAX_OUTPUT_COUNT; i++) { |
| 1031 |
|
| 1032 |
if ((void *)callback_object.udp_config[i]) |
| 1033 |
rist_udp_config_free2(&callback_object.udp_config[i]); |
| 1034 |
} |
| 1035 |
|
| 1036 |
rist_logging_unset_global(); |
| 1037 |
if (inputurl) |
| 1038 |
free(inputurl); |
| 1039 |
if (outputurl) |
| 1040 |
free(outputurl); |
| 1041 |
if (thread_tun_loop) |
| 1042 |
pthread_join(thread_tun_loop, NULL); |
| 1043 |
if (oobtun) |
| 1044 |
free(oobtun); |
| 1045 |
if (callback_object.tun) |
| 1046 |
close(callback_object.tun); |
| 1047 |
if (shared_secret) |
| 1048 |
free(shared_secret); |
| 1049 |
|
| 1050 |
struct ristreceiver_flow_cumulative_stats *stats, *next; |
| 1051 |
stats = stats_list; |
| 1052 |
while (stats) |
| 1053 |
{ |
| 1054 |
next = stats->next; |
| 1055 |
free(stats); |
| 1056 |
stats = next; |
| 1057 |
} |
| 1058 |
#if HAVE_PROMETHEUS_SUPPORT |
| 1059 |
rist_prometheus_stats_destroy(prom_stats_ctx); |
| 1060 |
free(prometheus_ip); |
| 1061 |
free(prometheus_tags); |
| 1062 |
free(prometheus_unix_sock); |
| 1063 |
#endif |
| 1064 |
return 0; |
| 1065 |
} |