| 1 |
/* |
| 2 |
* Copyright (c) 2018 by Farsight Security, Inc. |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 5 |
* a copy of this software and associated documentation files (the |
| 6 |
* "Software"), to deal in the Software without restriction, including |
| 7 |
* without limitation the rights to use, copy, modify, merge, publish, |
| 8 |
* distribute, sublicense, and/or sell copies of the Software, and to |
| 9 |
* permit persons to whom the Software is furnished to do so, subject to |
| 10 |
* the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included |
| 13 |
* in all copies or substantial portions of the Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 |
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 |
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 |
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <arpa/inet.h> |
| 26 |
#include <netinet/in.h> |
| 27 |
#include <sys/socket.h> |
| 28 |
#include <sys/uio.h> |
| 29 |
#include <string.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <stdbool.h> |
| 33 |
|
| 34 |
#include <fstrm.h> |
| 35 |
|
| 36 |
#include "libmy/argv.h" |
| 37 |
|
| 38 |
struct replay { |
| 39 |
struct replay_args *args; |
| 40 |
uint8_t *content_type; |
| 41 |
size_t len_content_type; |
| 42 |
}; |
| 43 |
|
| 44 |
struct replay_args { |
| 45 |
bool help; |
| 46 |
char *content_type; |
| 47 |
char *unix_address; |
| 48 |
char *tcp_address; |
| 49 |
char *tcp_port; |
| 50 |
argv_array_t files; |
| 51 |
}; |
| 52 |
|
| 53 |
static struct replay g_program_ctx; |
| 54 |
static struct replay_args g_program_args; |
| 55 |
|
| 56 |
static argv_t g_args[] = { |
| 57 |
{ 'h', "help", |
| 58 |
ARGV_BOOL, |
| 59 |
&g_program_args.help, |
| 60 |
NULL, |
| 61 |
"display this help text and exit" }, |
| 62 |
|
| 63 |
{ 't', "type", |
| 64 |
ARGV_CHAR_P | ARGV_FLAG_MAND, |
| 65 |
&g_program_args.content_type, |
| 66 |
"<STRING>", |
| 67 |
"Frame Streams content type" }, |
| 68 |
|
| 69 |
{ 'u', "unix", |
| 70 |
ARGV_CHAR_P, |
| 71 |
&g_program_args.unix_address, |
| 72 |
"<FILENAME>", |
| 73 |
"Unix socket path to write to" }, |
| 74 |
|
| 75 |
/* ARGV_ONE_OF indicates that the user must specify the |
| 76 |
* previous option (-u) or next option (-a), but not both. |
| 77 |
*/ |
| 78 |
{ ARGV_ONE_OF, 0, 0, 0, 0, 0 }, |
| 79 |
|
| 80 |
{ 'a', "tcp", |
| 81 |
ARGV_CHAR_P, |
| 82 |
&g_program_args.tcp_address, |
| 83 |
"<ADDRESS>", |
| 84 |
"TCP socket address to write to" }, |
| 85 |
|
| 86 |
{ 'p', "port", |
| 87 |
ARGV_CHAR_P, |
| 88 |
&g_program_args.tcp_port, |
| 89 |
"<PORT>", |
| 90 |
"TCP socket port to write to" }, |
| 91 |
|
| 92 |
{ 'r', "read-file", |
| 93 |
ARGV_CHAR_P | ARGV_FLAG_ARRAY | ARGV_FLAG_MAND, |
| 94 |
&g_program_args.files, |
| 95 |
"<FILE>", |
| 96 |
"Files to read Frame Streams data from" }, |
| 97 |
|
| 98 |
{ ARGV_LAST, 0, 0, 0, 0, 0 }, |
| 99 |
}; |
| 100 |
|
| 101 |
static void |
| 102 |
usage(const char *msg) |
| 103 |
{ |
| 104 |
if(msg) |
| 105 |
fprintf(stderr, "%s: Usage error: %s\n", argv_program, msg); |
| 106 |
argv_usage(g_args, ARGV_USAGE_DEFAULT); |
| 107 |
argv_cleanup(g_args); |
| 108 |
exit(EXIT_FAILURE); |
| 109 |
} |
| 110 |
|
| 111 |
static bool |
| 112 |
parse_args(int argc, char **argv, struct replay *ctx) |
| 113 |
{ |
| 114 |
if (argv_process(g_args, argc, argv) != 0) |
| 115 |
return false; |
| 116 |
|
| 117 |
if (g_program_args.help) |
| 118 |
return false; |
| 119 |
|
| 120 |
if ((g_program_args.tcp_address != NULL) && |
| 121 |
(g_program_args.tcp_port == NULL)) |
| 122 |
usage("--tcp requires --port"); |
| 123 |
|
| 124 |
ctx->content_type = (uint8_t *)g_program_args.content_type; |
| 125 |
ctx->len_content_type = strlen(g_program_args.content_type); |
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
static struct fstrm_writer * |
| 130 |
init_writer(void) |
| 131 |
{ |
| 132 |
struct fstrm_writer *w; |
| 133 |
struct fstrm_writer_options *wopt; |
| 134 |
fstrm_res res; |
| 135 |
|
| 136 |
/* Setup writer options. */ |
| 137 |
wopt = fstrm_writer_options_init(); |
| 138 |
res = fstrm_writer_options_add_content_type(wopt, |
| 139 |
g_program_ctx.content_type, |
| 140 |
g_program_ctx.len_content_type); |
| 141 |
if (res != fstrm_res_success) { |
| 142 |
fstrm_writer_options_destroy(&wopt); |
| 143 |
return NULL; |
| 144 |
} |
| 145 |
|
| 146 |
if (g_program_args.unix_address != NULL) { |
| 147 |
struct fstrm_unix_writer_options *uwopt; |
| 148 |
|
| 149 |
if (g_program_args.tcp_port != NULL) |
| 150 |
fputs("Warning: Ignoring --port with --unix.\n", stderr); |
| 151 |
|
| 152 |
uwopt = fstrm_unix_writer_options_init(); |
| 153 |
fstrm_unix_writer_options_set_socket_path(uwopt, g_program_args.unix_address); |
| 154 |
w = fstrm_unix_writer_init(uwopt, wopt); |
| 155 |
fstrm_unix_writer_options_destroy(&uwopt); |
| 156 |
fstrm_writer_options_destroy(&wopt); |
| 157 |
if (w == NULL) { |
| 158 |
fputs("Error: fstrm_unix_writer_init() failed.\n", stderr); |
| 159 |
return NULL; |
| 160 |
} |
| 161 |
} else { |
| 162 |
struct fstrm_tcp_writer_options *twopt; |
| 163 |
unsigned long port; |
| 164 |
char *endptr; |
| 165 |
struct sockaddr_in sin; |
| 166 |
struct sockaddr_in6 sin6; |
| 167 |
|
| 168 |
/* Parse TCP listen port. */ |
| 169 |
port = strtoul(g_program_args.tcp_port, &endptr, 0); |
| 170 |
if (*endptr != '\0' || port > UINT16_MAX) |
| 171 |
usage("Failed to parse TCP listen port"); |
| 172 |
|
| 173 |
/* Parse TCP listen address. */ |
| 174 |
if ((inet_pton(AF_INET, g_program_args.tcp_address, &sin) != 1) && |
| 175 |
(inet_pton(AF_INET6, g_program_args.tcp_address, &sin6) != 1)) |
| 176 |
usage("Failed to parse TCP listen address"); |
| 177 |
|
| 178 |
twopt = fstrm_tcp_writer_options_init(); |
| 179 |
fstrm_tcp_writer_options_set_socket_address(twopt, g_program_args.tcp_address); |
| 180 |
fstrm_tcp_writer_options_set_socket_port(twopt, g_program_args.tcp_port); |
| 181 |
|
| 182 |
w = fstrm_tcp_writer_init(twopt, wopt); |
| 183 |
fstrm_tcp_writer_options_destroy(&twopt); |
| 184 |
fstrm_writer_options_destroy(&wopt); |
| 185 |
|
| 186 |
if (w == NULL) { |
| 187 |
fputs("Error: fstrm_tcp_writer_init() failed.\n", stderr); |
| 188 |
return NULL; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
res = fstrm_writer_open(w); |
| 193 |
if (res != fstrm_res_success) { |
| 194 |
fstrm_writer_destroy(&w); |
| 195 |
fputs("Error: fstrm_writer_open() failed.\n", stderr); |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
|
| 199 |
return w; |
| 200 |
} |
| 201 |
|
| 202 |
static void |
| 203 |
process_file(const char *fname, struct fstrm_writer *w) |
| 204 |
{ |
| 205 |
struct fstrm_reader *r = NULL; |
| 206 |
struct fstrm_file_options *fopt = NULL; |
| 207 |
const struct fstrm_control *control = NULL; |
| 208 |
fstrm_res res; |
| 209 |
|
| 210 |
fopt = fstrm_file_options_init(); |
| 211 |
fstrm_file_options_set_file_path(fopt, fname); |
| 212 |
|
| 213 |
/* Initialize file reader. */ |
| 214 |
r = fstrm_file_reader_init(fopt, NULL); |
| 215 |
fstrm_file_options_destroy(&fopt); |
| 216 |
if (r == NULL) { |
| 217 |
fprintf(stderr, "Warning: failed to open %s, skipping\n", fname); |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
/* Check file content type. */ |
| 222 |
res = fstrm_reader_get_control(r, FSTRM_CONTROL_START, &control); |
| 223 |
if (res != fstrm_res_success) { |
| 224 |
fprintf(stderr, "Warning: failed to read control frame from %s, skipping\n", fname); |
| 225 |
fstrm_reader_destroy(&r); |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
res = fstrm_control_match_field_content_type(control, |
| 230 |
g_program_ctx.content_type, |
| 231 |
g_program_ctx.len_content_type); |
| 232 |
if (res != fstrm_res_success) { |
| 233 |
fprintf(stderr, "Warning: content type mismatch for %s, skipping\n", fname); |
| 234 |
fstrm_reader_destroy(&r); |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
/* Loop over file data. */ |
| 239 |
for (;;) { |
| 240 |
const uint8_t *data; |
| 241 |
size_t len_data; |
| 242 |
|
| 243 |
res = fstrm_reader_read(r, &data, &len_data); |
| 244 |
if (res == fstrm_res_success) { |
| 245 |
/* Write the data frame. */ |
| 246 |
res = fstrm_writer_write(w, data, len_data); |
| 247 |
if (res != fstrm_res_success) { |
| 248 |
fputs("Error: write_data_frame() failed", stderr); |
| 249 |
exit(EXIT_FAILURE); |
| 250 |
} |
| 251 |
} else if (res == fstrm_res_stop) { |
| 252 |
/* Normal end of data stream. */ |
| 253 |
res = fstrm_reader_get_control(r, FSTRM_CONTROL_STOP, &control); |
| 254 |
if (res != fstrm_res_success) { |
| 255 |
fprintf(stderr, "Error: unable to read STOP frame from %s.\n", fname); |
| 256 |
} |
| 257 |
break; |
| 258 |
} else { |
| 259 |
/* Abnormal end. */ |
| 260 |
fprintf(stderr, "Error: fstrm_reader_read() failed.\n"); |
| 261 |
break; |
| 262 |
} |
| 263 |
} |
| 264 |
fstrm_reader_destroy(&r); |
| 265 |
} |
| 266 |
|
| 267 |
int main(int argc, char **argv) |
| 268 |
{ |
| 269 |
int i; |
| 270 |
fstrm_res res; |
| 271 |
struct fstrm_writer *w = NULL; |
| 272 |
|
| 273 |
if (!parse_args(argc, argv, &g_program_ctx)) |
| 274 |
usage(NULL); |
| 275 |
|
| 276 |
w = init_writer(); |
| 277 |
if (w == NULL) |
| 278 |
exit(EXIT_FAILURE); |
| 279 |
|
| 280 |
for (i = 0; i < ARGV_ARRAY_COUNT(g_program_args.files); i++) { |
| 281 |
const char *in_fname; |
| 282 |
in_fname = ARGV_ARRAY_ENTRY(g_program_args.files, char *, i); |
| 283 |
process_file(in_fname, w); |
| 284 |
} |
| 285 |
|
| 286 |
res = fstrm_writer_close(w); |
| 287 |
if (res != fstrm_res_success) |
| 288 |
fputs("Error: fstrm_writer_close() failed", stderr); |
| 289 |
|
| 290 |
fstrm_writer_destroy(&w); |
| 291 |
return 0; |
| 292 |
} |