11 #include <libconfig.h++>
12 #include <boost/asio.hpp>
14 #include "spdlog/async.h"
15 #include "spdlog/spdlog.h"
16 #include "spdlog/sinks/syslog_sink.h"
21 using libconfig::Config;
22 using libconfig::FileIOException;
23 using libconfig::ParseException;
25 using std::placeholders::_1;
26 using std::placeholders::_2;
27 using std::placeholders::_3;
29 static void print_version(FILE *stream,
struct argp_state *state);
32 static char doc[] =
"FLUTE/ALC transmitter demo";
34 static struct argp_option
options[] = {
35 {
"target",
'm',
"IP", 0,
"Target multicast address (default: 238.1.1.95)", 0},
36 {
"port",
'p',
"PORT", 0,
"Target port (default: 40085)", 0},
37 {
"mtu",
't',
"BYTES", 0,
"Path MTU to size ALC packets for (default: 1500)", 0},
38 {
"rate-limit",
'r',
"KBPS", 0,
"Transmit rate limit (kbps), 0 = no limit, default: 1000 (1 Mbps)", 0},
39 {
"ipsec-key",
'k',
"KEY", 0,
"To enable IPSec/ESP encryption of packets, provide a hex-encoded AES key here", 0},
40 {
"log-level",
'l',
"LEVEL", 0,
41 "Log verbosity: 0 = trace, 1 = debug, 2 = info, 3 = warn, 4 = error, 5 = "
42 "critical, 6 = none. Default: 2.",
44 {
nullptr, 0,
nullptr, 0,
nullptr, 0}};
54 unsigned short mtu = 1500;
63 static auto parse_opt(
int key,
char *arg,
struct argp_state *state) -> error_t {
64 auto arguments =
static_cast<struct
ft_arguments *
>(state->input);
70 arguments->aes_key = arg;
71 arguments->enable_ipsec =
true;
74 arguments->mcast_port =
static_cast<unsigned short>(strtoul(arg,
nullptr, 10));
77 arguments->mtu =
static_cast<unsigned short>(strtoul(arg,
nullptr, 10));
80 arguments->rate_limit =
static_cast<uint32_t
>(strtoul(arg,
nullptr, 10));
83 arguments->log_level =
static_cast<unsigned>(strtoul(arg,
nullptr, 10));
85 case ARGP_KEY_NO_ARGS:
88 arguments->files = &state->argv[state->next-1];
89 state->next = state->argc;
92 return ARGP_ERR_UNKNOWN;
99 nullptr,
nullptr,
nullptr};
105 fprintf(stream,
"1.0.0\n");
117 auto main(
int argc,
char **argv) ->
int {
122 argp_parse(&
argp, argc, argv, 0,
nullptr, &arguments);
125 std::string ident =
"flute-transmitter";
126 auto syslog_logger = spdlog::syslog_logger_mt(
"syslog", ident, LOG_PID | LOG_PERROR | LOG_CONS );
129 static_cast<spdlog::level::level_enum
>(arguments.
log_level));
130 spdlog::set_pattern(
"[%H:%M:%S.%f %z] [%^%l%$] [thr %t] %v");
132 spdlog::set_default_logger(syslog_logger);
133 spdlog::info(
"FLUTE transmitter demo starting up");
139 std::string location;
144 std::vector<FsFile> files;
147 for (
int j = 0; arguments.
files[j]; j++) {
148 std::string location = arguments.
files[j];
149 std::ifstream file(arguments.
files[j], std::ios::binary | std::ios::ate);
150 std::streamsize size = file.tellg();
151 file.seekg(0, std::ios::beg);
153 char* buffer = (
char*)malloc(size);
154 file.read(buffer, size);
155 files.push_back(FsFile{ arguments.
files[j], buffer, (size_t)size});
159 boost::asio::io_service io;
178 [&files](uint32_t toi) {
179 for (
auto& file : files) {
180 if (file.toi == toi) {
181 spdlog::info(
"{} (TOI {}) has been transmitted",
182 file.location, file.toi);
189 for (
auto& file : files) {
190 file.toi = transmitter.
send( file.location,
191 "application/octet-stream",
196 spdlog::info(
"Queued {} ({} bytes) for transmission, TOI is {}",
197 file.location, file.len, file.toi);
uint64_t seconds_since_epoch()
Convenience function to get the current timestamp for expiry calculation.
uint16_t send(const std::string &content_location, const std::string &content_type, uint32_t expires, char *data, size_t length)
Transmit a file.
void enable_ipsec(uint32_t spi, const std::string &aes_key)
Enable IPSEC ESP encryption of FLUTE payloads.
void register_completion_callback(completion_callback_t cb)
Register a callback for file transmission completion notifications.
static void print_version(FILE *stream, struct argp_state *state)
Print the program version in MAJOR.MINOR.PATCH format.
auto main(int argc, char **argv) -> int
Main entry point for the program.
const char * argp_program_bug_address
static struct argp_option options[]
void(* argp_program_version_hook)(FILE *, struct argp_state *)
static auto parse_opt(int key, char *arg, struct argp_state *state) -> error_t
Parses the command line options into the arguments struct.
Holds all options passed on the command line.
unsigned log_level
log level
const char * mcast_target
unsigned short mcast_port