Flowgrind
Advanced TCP traffic generator
flowgrind_stop.c File Reference

Utility to instruct the Flowgrind daemon to stop all flows. More...

#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/client.h>
#include "common.h"
#include "fg_definitions.h"
#include "fg_error.h"
#include "fg_progname.h"
#include "fg_rpc_client.h"
#include "fg_argparser.h"

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 
static void stop_flows (const char *address)
 
static void usage (short status)
 Print flowgrind-stop usage and exit. More...
 

Variables

static struct arg_parser parser
 Command line option parser. More...
 
const char * progname
 String containing name the program is called with. More...
 

Detailed Description

Utility to instruct the Flowgrind daemon to stop all flows.

Definition in file flowgrind_stop.c.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 141 of file flowgrind_stop.c.

142 {
143  /* update progname from argv[0] */
144  set_progname(argv[0]);
145 
146  const struct ap_Option options[] = {
147  {'h', "help", ap_no, 0, 0},
148  {'v', "version", ap_no, 0, 0},
149  {0, 0, ap_no, 0, 0}
150  };
151 
152  if (!ap_init(&parser, argc, (const char* const*) argv, options, 0))
153  critx("could not allocate memory for option parser");
154  if (ap_error(&parser)) {
155  errx("%s", ap_error(&parser));
156  usage(EXIT_FAILURE);
157  }
158 
159  /* parse command line */
160  for (int argind = 0; argind < ap_arguments(&parser); argind++) {
161  const int code = ap_code(&parser, argind);
162 
163  switch (code) {
164  case 0:
165  break;
166  case 'h':
167  usage(EXIT_SUCCESS);
168  break;
169  case 'v':
170  fprintf(stdout, "%s %s\n%s\n%s\n\n%s\n", progname,
173  exit(EXIT_SUCCESS);
174  break;
175  default:
176  errx("uncaught option: %s", ap_argument(&parser, argind));
177  usage(EXIT_FAILURE);
178  break;
179  }
180  }
181 
182  if (!ap_arguments(&parser)) {
183  errx("no address given");
184  usage(EXIT_FAILURE);
185  }
186 
187  xmlrpc_env rpc_env;
188  xmlrpc_env_init(&rpc_env);
189  xmlrpc_client_setup_global_const(&rpc_env);
190 
191  for (int argind = 0; argind < ap_arguments(&parser); argind++)
192  /* if non-option, it is an address */
193  if (!ap_code(&parser, argind))
194  stop_flows(ap_argument(&parser, argind));
195 
196  xmlrpc_env_clean(&rpc_env);
197  xmlrpc_client_teardown_global_const();
198  ap_free(&parser);
199 }

◆ stop_flows()

static void stop_flows ( const char *  address)
static

Definition at line 87 of file flowgrind_stop.c.

88 {
89  xmlrpc_env env;
90  xmlrpc_client *client = 0;
91  xmlrpc_value * resultP = 0;
93  bool is_ipv6 = false;
94  char *arg, *url = 0;
95  char *rpc_address = arg = strdup(address);
96  struct sockaddr_in6 source_in6;
97  source_in6.sin6_family = AF_INET6;
98 
99  parse_rpc_address(&rpc_address, &port, &is_ipv6);
100 
101  if (is_ipv6 && (inet_pton(AF_INET6, rpc_address,
102  (char*)&source_in6.sin6_addr) <= 0))
103  errx("invalid IPv6 address '%s' for RPC", rpc_address);
104 
105  if (port < 1 || port > 65535)
106  errx("invalid port for RPC");
107 
108  int rc = 0;
109  if (is_ipv6)
110  rc = asprintf(&url, "http://[%s]:%d/RPC2", rpc_address, port);
111  else
112  rc = asprintf(&url, "http://%s:%d/RPC2", rpc_address, port);
113 
114  if (rc == -1)
115  critx("could not allocate memory for RPC URL");
116 
117  printf("Stopping all flows on %s\n", url);
118 
119  /* Stop the flows */
120  xmlrpc_env_init(&env);
121  xmlrpc_client_create(&env, XMLRPC_CLIENT_NO_FLAGS, "Flowgrind", FLOWGRIND_VERSION, NULL, 0, &client);
122  if (env.fault_occurred)
123  goto cleanup;
124 
125  xmlrpc_client_call2f(&env, client, url, "stop_flow", &resultP,
126  "({s:i})", "flow_id", -1); /* -1 stops all flows */
127  if (resultP)
128  xmlrpc_DECREF(resultP);
129 
130 cleanup:
131  if (env.fault_occurred) {
132  warnx("could not stop flows on %s: %s (%d)",
133  url, env.fault_string, env.fault_code);
134  }
135  if (client)
136  xmlrpc_client_destroy(client);
137  xmlrpc_env_clean(&env);
138  free_all(arg, url);
139 }

◆ usage()

static void usage ( short  status)
static

Print flowgrind-stop usage and exit.

Definition at line 65 of file flowgrind_stop.c.

66 {
67  /* Syntax error. Emit 'try help' to stderr and exit */
68  if (status != EXIT_SUCCESS) {
69  fprintf(stderr, "Try '%s -h' for more information\n", progname);
70  exit(status);
71  }
72 
73  fprintf(stdout,
74  "Usage: %1$s [OPTION]... [ADDRESS]...\n"
75  "Stop all flows on the daemons running at the given addresses.\n\n"
76 
77  "Mandatory arguments to long options are mandatory for short options too.\n"
78  " -h, --help display this help and exit\n"
79  " -v, --version print version information and exit\n\n"
80 
81  "Example:\n"
82  " %1$s localhost 127.2.3.4:5999 example.com\n",
83  progname);
84  exit(EXIT_SUCCESS);
85 }

Variable Documentation

◆ parser

struct arg_parser parser
static

Command line option parser.

Definition at line 57 of file flowgrind_stop.c.

◆ progname

const char* progname

String containing name the program is called with.

Definition at line 35 of file fg_progname.c.

ap_code
int ap_code(const struct arg_parser *const ap, const int i)
Returns the code of a parsed option with given index.
Definition: fg_argparser.c:468
ap_argument
const char * ap_argument(const struct arg_parser *const ap, const int i)
Returns the argument of a parsed option.
Definition: fg_argparser.c:478
ap_error
const char * ap_error(const struct arg_parser *const ap)
Get the string containing errors encountered during parsing.
Definition: fg_argparser.c:458
FLOWGRIND_COPYRIGHT
#define FLOWGRIND_COPYRIGHT
Flowgrind's copyright year.
Definition: common.h:82
port
static unsigned port
Definition: flowgrindd.c:95
parser
static struct arg_parser parser
Command line option parser.
Definition: flowgrind_stop.c:57
ap_no
@ ap_no
Option without argument (flag).
Definition: fg_argparser.h:37
warnx
#define warnx(...)
To report a warning w/ a system error message.
Definition: fg_error.h:54
ap_Option::code
int code
Short option letter or code (code != 0).
Definition: fg_argparser.h:47
errx
#define errx(...)
To report an error w/o a system error message.
Definition: fg_error.h:47
ap_free
void ap_free(struct arg_parser *const ap)
Free internal state of arg-parser.
Definition: fg_argparser.c:448
FLOWGRIND_COPYING
#define FLOWGRIND_COPYING
Standard GPL3 no warranty message.
Definition: common.h:85
critx
#define critx(...)
To report an critical error w/o a system error message.
Definition: fg_error.h:40
progname
const char * progname
String containing name the program is called with.
Definition: fg_progname.c:35
FLOWGRIND_AUTHORS
#define FLOWGRIND_AUTHORS
Flowgrind's authors in a printable string.
Definition: common.h:91
ap_arguments
int ap_arguments(const struct arg_parser *const ap)
Number of arguments parsed (may be different from argc).
Definition: fg_argparser.c:463
rpc_env
static xmlrpc_env rpc_env
Definition: flowgrind.c:104
ap_Option
Defines a valid command line option.
Definition: fg_argparser.h:45
FLOWGRIND_VERSION
#define FLOWGRIND_VERSION
Flowgrind version number.
Definition: common.h:44
free_all
#define free_all(...)
To free() an arbitrary number of variables.
Definition: fg_definitions.h:59
set_progname
void set_progname(const char *argv0)
Set global variable 'progname', based on argv[0].
Definition: fg_progname.c:37
DEFAULT_LISTEN_PORT
#define DEFAULT_LISTEN_PORT
Daemon's default listen port.
Definition: common.h:55
ap_init
bool ap_init(struct arg_parser *const ap, const int argc, const char *const argv[], const struct ap_Option options[], const char in_order)
Initialize the arg-parser given command line and user-defined options.
Definition: fg_argparser.c:374
stop_flows
static void stop_flows(const char *address)
Definition: flowgrind_stop.c:87
parse_rpc_address
void parse_rpc_address(char **rpc_address, int *port, bool *is_ipv6)
Parse RPC address for the xmlrpc control connection.
Definition: fg_rpc_client.c:40
usage
static void usage(short status) __attribute__((noreturn))
Print flowgrind-stop usage and exit.
Definition: flowgrind_stop.c:65