Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add options for quiet mode and IPv4/v6 selection
  • Loading branch information
remcovanmook committed Jun 22, 2018
commit 498e554de5421a75dcbd9355e3bff66e115f9831
32 changes: 27 additions & 5 deletions bird-rtrlib-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ static struct config config;
*/
void sigpipe_handler(int signum)
{
syslog(LOG_ERR, "Caught SIGPIPE %d.", signum);
if (config.quiet != true)
syslog(LOG_ERR, "Caught SIGPIPE %d.", signum);
}

/**
Expand Down Expand Up @@ -92,7 +93,11 @@ void cleanup_bird_command(void)
*/
void init(void)
{
openlog(NULL, LOG_PERROR | LOG_CONS | LOG_PID, LOG_DAEMON);
if (config.quiet == true) {
openlog(NULL, LOG_PERROR, LOG_DAEMON);
} else {
openlog(NULL, LOG_PERROR | LOG_CONS | LOG_PID, LOG_DAEMON);
}
}

/**
Expand Down Expand Up @@ -150,6 +155,20 @@ static void pfx_update_callback(struct pfx_table *table,
static char bird_response[BIRD_RSP_SIZE];
// Fetch IP address as string.
lrtr_ip_addr_to_str(&(record.prefix), ip_addr_str, sizeof(ip_addr_str));
// Crude way of filtering out sending Bird the wrong kind of updates
int prefix_allowed = 0;
if (strchr(ip_addr_str,'.') != NULL)
{
if ( strchr(config.ip_version, '4') != NULL )
prefix_allowed++;
}
if (strchr(ip_addr_str,':') != NULL)
{
if ( strchr(config.ip_version, '6') != NULL )
prefix_allowed++;
}
if (prefix_allowed == 0)
return;
// Write BIRD command to buffer.
if (
snprintf(
Expand All @@ -169,11 +188,13 @@ static void pfx_update_callback(struct pfx_table *table,
return;
}
// Log the BIRD command and send it to the BIRD server.
syslog(LOG_INFO, "To BIRD: %s", bird_command);
if (config.quiet != true)
syslog(LOG_INFO, "To BIRD: %s", bird_command);
// reconnect bird_socket on SIGPIPE error, and resend BIRD command
while ((write(bird_socket, bird_command, strlen(bird_command)) < 0) &&
(errno == EPIPE)) {
syslog(LOG_ERR, "BIRD socket send failed, try reconnect!");
if (config.quiet != true)
syslog(LOG_ERR, "BIRD socket send failed, try reconnect!");
close(bird_socket);
bird_socket = bird_connect(config.bird_socket_path);
}
Expand All @@ -188,7 +209,8 @@ static void pfx_update_callback(struct pfx_table *table,
// log answer, if any valid response
if (size > 0) {
bird_response[size] = 0;
syslog(LOG_INFO, "From BIRD: %s", bird_response);
if (config.quiet != true)
syslog(LOG_INFO, "From BIRD: %s", bird_response);
}
}

Expand Down
24 changes: 24 additions & 0 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#define ARGKEY_RTRSSH_HOSTKEY 0x101
#define ARGKEY_RTRSSH_PRIVKEY 0x102
#define ARGKEY_RTRSSH_USERNAME 0x103
#define ARGKEY_IP_VERSION 'v'
#define ARGKEY_QUIET 'q'

// Parser function for argp_parse().
static error_t argp_parser(int key, char *arg, struct argp_state *state)
Expand Down Expand Up @@ -67,6 +69,12 @@ static error_t argp_parser(int key, char *arg, struct argp_state *state)
case ARGKEY_RTRSSH_USERNAME:
config->rtr_ssh_username = arg;
break;
case ARGKEY_IP_VERSION:
config->ip_version = arg;
break;
case ARGKEY_QUIET:
config->quiet = true;
break;
default:
// Process unknown argument.
return ARGP_ERR_UNKNOWN;
Expand Down Expand Up @@ -148,6 +156,22 @@ int parse_cli(int argc, char **argv, struct config *config)
"used. Uses the user's default identity file if not specified.",
2
},
{
"version",
ARGKEY_IP_VERSION,
"<4|6>",
0,
"(Optional) Indicate if the bird daemon wants IPv4 or IPv6 only updates.",
2
},
{
"quiet",
ARGKEY_QUIET,
0,
0,
"(Optional) Report only errors.",
1
},
{0}
};
// argp structure to be passed to argp_parse().
Expand Down
4 changes: 4 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ enum connection_type {
ssh // SSH connection
};

typedef enum { false, true } bool;

/**
* Application configuration structure.
*/
Expand All @@ -41,6 +43,8 @@ struct config {
char *rtr_ssh_username;
char *rtr_ssh_hostkey_file;
char *rtr_ssh_privkey_file;
char *ip_version;
bool quiet;
};

/**
Expand Down