Skip to content

Commit

Permalink
Single protocol in responder.
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasko committed May 22, 2019
1 parent 310d039 commit dcb4f0b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 136 deletions.
57 changes: 7 additions & 50 deletions src/ures/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#define DEF_KEY 0
#define DEF_TIMEOUT 0
#define DEF_LENGTH 0
#define DEF_PROTO_VERSION_4 true

/// Print the usage information to the standard output stream.
static void
Expand All @@ -47,8 +48,7 @@ print_usage(void)
" ures [OPTIONS]\n\n"

"Options:\n"
" -4 Use only the IPv4 protocol.\n"
" -6 Use only the IPv6 protocol.\n"
" -6 Use the IPv6 protocol.\n"
" -a OBJ Attach a plugin from a shared object file.\n"
" -b Reporting data to be in binary format.\n"
" -d DUR Time-out for lack of incoming requests.\n"
Expand All @@ -72,20 +72,6 @@ print_usage(void)
DEF_TIME_TO_LIVE);
}

/// Select IPv4 protocol only.
/// @return success/failure indication
///
/// @param[out] cf configuration
/// @param[in] in argument input (unused)
static bool
option_4(struct config* cf, const char* in)
{
(void)in;
cf->cf_ipv4 = true;

return true;
}

/// Select IPv6 protocol only.
/// @return success/failure indication
///
Expand All @@ -95,7 +81,7 @@ static bool
option_6(struct config* cf, const char* in)
{
(void)in;
cf->cf_ipv6 = true;
cf->cf_ipv4 = false;

return true;
}
Expand Down Expand Up @@ -339,37 +325,14 @@ set_defaults(struct config* cf)
cf->cf_bin = DEF_BINARY;
cf->cf_llvl = (log_lvl = DEF_LOG_LEVEL);
cf->cf_lcol = (log_col = DEF_LOG_COLOR);
cf->cf_ipv4 = false;
cf->cf_ipv6 = false;
cf->cf_ipv4 = DEF_PROTO_VERSION_4;
cf->cf_key = DEF_KEY;
cf->cf_ito = DEF_TIMEOUT;
cf->cf_len = DEF_LENGTH;

return true;
}

/// Sanitize the IPv4/IPv6 options.
/// @return success/failure indication
///
/// @param[out] cf configuration
static bool
organize_protocols(struct config* cf)
{
// Check whether two exclusive modes were selected.
if (cf->cf_ipv4 == true && cf->cf_ipv6 == true) {
log(LL_WARN, false, "options -4 and -6 are mutually exclusive");
return false;
}

// If no restrictions on the IP version were set, enable both versions.
if (cf->cf_ipv4 == false && cf->cf_ipv6 == false) {
cf->cf_ipv4 = true;
cf->cf_ipv6 = true;
}

return true;
}

/// Generate the getopt(3) DSL based on a set of option definitions.
///
/// @param[out] str DSL string
Expand Down Expand Up @@ -410,8 +373,7 @@ parse_config(struct config* cf, int argc, char* argv[])
bool retb;
uint64_t i;
char optdsl[128];
struct option opts[17] = {
{ '4', false, option_4 },
struct option opts[16] = {
{ '6', false, option_6 },
{ 'a', true , option_a },
{ 'b', false, option_b },
Expand All @@ -433,7 +395,7 @@ parse_config(struct config* cf, int argc, char* argv[])
log(LL_INFO, false, "parsing command-line options");

(void)memset(optdsl, '\0', sizeof(optdsl));
generate_getopt_string(optdsl, opts, 17);
generate_getopt_string(optdsl, opts, 16);

// Set optional arguments to sensible defaults.
retb = set_defaults(cf);
Expand All @@ -459,7 +421,7 @@ parse_config(struct config* cf, int argc, char* argv[])
}

// Find the relevant option.
for (i = 0; i < 17; i++) {
for (i = 0; i < 16; i++) {
if (opts[i].op_name == (char)opt) {
retb = opts[i].op_act(cf, optarg);
if (retb == false) {
Expand All @@ -478,11 +440,6 @@ parse_config(struct config* cf, int argc, char* argv[])
return false;
}

retb = organize_protocols(cf);
if (retb == false) {
return false;
}

// Assign the logging settings.
log_lvl = cf->cf_llvl;
log_col = cf->cf_lcol;
Expand Down
3 changes: 1 addition & 2 deletions src/ures/funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ bool handle_event(struct proto* pr,
const struct config* cf);

// Loop.
bool respond_loop(struct proto* p4,
struct proto* p6,
bool respond_loop(struct proto* pr,
const struct plugin* pi,
const uint64_t npi,
const struct config* cf);
Expand Down
70 changes: 20 additions & 50 deletions src/ures/loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@
/// @global sterm
/// @global susr1
///
/// @param[in] p4 IPv4 protocol
/// @param[in] p6 IPv6 protocol
/// @param[in] pr protocol
/// @param[in] cf configuration
static bool
handle_interrupt(const struct proto* p4,
const struct proto* p6,
handle_interrupt(const struct proto* pr,
const struct config* cf)
{
log(LL_TRACE, false, "handling interrupt");
Expand All @@ -50,16 +48,10 @@ handle_interrupt(const struct proto* p4,
// Print logging information and continue the process upon receiving SIGUSR1.
if (susr1 == true) {
log_config(cf);
if (cf->cf_ipv4 == true) {
log_stats(p4->pr_name, &p4->pr_stat);
}

if (cf->cf_ipv6 == true) {
log_stats(p6->pr_name, &p6->pr_stat);
}
log_stats(pr->pr_name, &pr->pr_stat);

// Reset the signal indicator, so that following signal handling will avoid
// the false positive.
// Reset the signal indicator, so that following signal handling will
// avoid the false positive.
susr1 = false;
return true;
}
Expand All @@ -75,14 +67,12 @@ handle_interrupt(const struct proto* p4,
/// @global sterm
/// @global susr1
///
/// @param[out] p4 IPv4 protocol
/// @param[out] p6 IPv6 protocol
/// @param[out] pr protocol
/// @param[in] pins array of plugins
/// @param[in] npins number of plugins
/// @param[in] cf configuration
bool
respond_loop(struct proto* p4,
struct proto* p6,
respond_loop(struct proto* pr,
const struct plugin* pins,
const uint64_t npins,
const struct config* cf)
Expand All @@ -103,19 +93,12 @@ respond_loop(struct proto* p4,
// Print the CSV header of the standard output.
report_header(cf);

// Add sockets to the event list.
// Add sockets to the event list. The number of file descriptors include the
// standard input, output, and error streams; accompanied by the protocol
// socket, plus one.
nfds = 4;
FD_ZERO(&rfd);
nfds = 3; //< Standard input, output, and error streams; plus one.

if (cf->cf_ipv4 == true) {
FD_SET(p4->pr_sock, &rfd);
nfds++;
}

if (cf->cf_ipv6 == true) {
FD_SET(p6->pr_sock, &rfd);
nfds++;
}
FD_SET(pr->pr_sock, &rfd);

// Create the signal mask used for enabling signals during the pselect(2)
// waiting.
Expand Down Expand Up @@ -148,7 +131,7 @@ respond_loop(struct proto* p4,
if (reti == -1) {
// Check for interrupt (possibly due to a signal).
if (errno == EINTR) {
retb = handle_interrupt(p4, p6, cf);
retb = handle_interrupt(pr, cf);
if (retb == true) {
continue;
}
Expand All @@ -167,28 +150,15 @@ respond_loop(struct proto* p4,
}

// Handle incoming IPv4 datagrams.
if (cf->cf_ipv4 == true) {
reti = FD_ISSET(p4->pr_sock, &rfd);
if (reti > 0) {
retb = handle_event(p4, pins, npins, cf);
if (retb == false) {
return false;
}

// Replenish the inactivity timeout.
lim = mono_now() + cf->cf_ito;
reti = FD_ISSET(pr->pr_sock, &rfd);
if (reti > 0) {
retb = handle_event(pr, pins, npins, cf);
if (retb == false) {
return false;
}
}

// Handle incoming IPv6 datagrams.
if (cf->cf_ipv6 == true) {
reti = FD_ISSET(p6->pr_sock, &rfd);
if (reti > 0) {
retb = handle_event(p6, pins, npins, cf);
if (retb == false) {
return false;
}
}
// Replenish the inactivity timeout.
lim = mono_now() + cf->cf_ito;
}
}

Expand Down
46 changes: 15 additions & 31 deletions src/ures/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ main(int argc, char* argv[])
struct config cf;
struct plugin pins[PLUG_MAX];
uint64_t npins;
struct proto p4;
struct proto p6;
struct proto pr;

// Parse configuration from command-line options.
retb = parse_config(&cf, argc, argv);
Expand Down Expand Up @@ -61,56 +60,41 @@ main(int argc, char* argv[])
return EXIT_FAILURE;
}

// Initialize the IPv4 connection.
// Initialize the IPv4 or IPv6 connection.
if (cf.cf_ipv4 == true) {
reset_stats(&p4.pr_stat);
p4.pr_name = "IPv4";
reset_stats(&pr.pr_stat);
pr.pr_name = "IPv4";

retb = create_socket4(&p4, &cf);
retb = create_socket4(&pr, &cf);
if (retb == false) {
log(LL_ERROR, false, "unable to create %s socket", p4.pr_name);
log(LL_ERROR, false, "unable to create %s socket", pr.pr_name);
return EXIT_FAILURE;
}
}

// Initialize the IPv6 connection.
if (cf.cf_ipv6 == true) {
reset_stats(&p6.pr_stat);
p6.pr_name = "IPv6";
} else {
reset_stats(&pr.pr_stat);
pr.pr_name = "IPv6";

retb = create_socket6(&p6, &cf);
retb = create_socket6(&pr, &cf);
if (retb == false) {
log(LL_ERROR, false, "unable to create %s socket", p6.pr_name);
log(LL_ERROR, false, "unable to create %s socket", pr.pr_name);
return EXIT_FAILURE;
}
}

// Start the main responding loop.
retb = respond_loop(&p4, &p6, pins, npins, &cf);
retb = respond_loop(&pr, pins, npins, &cf);
if (retb == false) {
log(LL_ERROR, false, "responding loop has been terminated");
}

// Delete sockets.
if (cf.cf_ipv4 == true) {
delete_socket(&p4);
}

if (cf.cf_ipv6 == true) {
delete_socket(&p6);
}
// Delete the socket.
delete_socket(&pr);

// Terminate plugins.
terminate_plugins(pins, npins);

// Print final values of counters.
if (cf.cf_ipv4 == true) {
log_stats(p4.pr_name, &p4.pr_stat);
}

if (cf.cf_ipv6 == true) {
log_stats(p6.pr_name, &p6.pr_stat);
}
log_stats(pr.pr_name, &pr.pr_stat);

// Flush the standard output and error streams.
retb = flush_report_stream(&cf);
Expand Down
5 changes: 2 additions & 3 deletions src/ures/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ struct config {
uint64_t cf_ito; ///< Inactivity timeout.
uint64_t cf_len; ///< Overall packet length.
bool cf_err; ///< Early exit on first network error.
bool cf_ipv4; ///< IPv4-only traffic.
bool cf_ipv6; ///< IPv6-only traffic.
bool cf_ipv4; ///< Usage of Internet Protocol version 4.
uint8_t cf_llvl; ///< Minimal log level.
bool cf_lcol; ///< Log coloring policy.
bool cf_mono; ///< Monologue mode (no responses).
bool cf_sil; ///< Standard output presence.
bool cf_bin; ///< Binary reporting mode.
uint8_t cf_pad[7]; ///< Padding (unused).
uint8_t cf_pad; ///< Padding (unused).
};

/// Command-line option.
Expand Down

0 comments on commit dcb4f0b

Please sign in to comment.