feat: --patern
This commit is contained in:
parent
25ee8ad4c6
commit
ce8883d6cd
9 changed files with 48 additions and 6 deletions
|
|
@ -9,6 +9,7 @@ enum {
|
|||
OPT_ARG_SECONDS = OPT_ARG_STRING + 1,
|
||||
OPT_ARG_BYTES,
|
||||
OPT_ARG_TTL,
|
||||
OPT_ARG_PATTERN,
|
||||
};
|
||||
|
||||
#define PING_OPTIONS_LIST \
|
||||
|
|
@ -107,6 +108,14 @@ enum {
|
|||
cli_handle_dont_fragment, \
|
||||
OPT_ARG_NONE, \
|
||||
"Set the Don't Fragment bit" \
|
||||
) \
|
||||
X( \
|
||||
'p', \
|
||||
"pattern", \
|
||||
required_argument, \
|
||||
cli_handle_pattern, \
|
||||
OPT_ARG_PATTERN, \
|
||||
"Fill packet payload with hex pattern" \
|
||||
)
|
||||
|
||||
#undef X
|
||||
|
|
|
|||
|
|
@ -1,10 +1,13 @@
|
|||
#ifndef CLI_INTERNAL_PARSE_UTILS
|
||||
#define CLI_INTERNAL_PARSE_UTILS
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
int cli_parse_uint64(const char *s, uint64_t *out);
|
||||
int cli_parse_float(const char *s, float *out);
|
||||
int cli_parse_ufloat(const char *s, float *out);
|
||||
int cli_parse_hex(const char *str, uint8_t *out, size_t max_len, size_t
|
||||
*out_len);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
extern const struct option_descriptor g_options[];
|
||||
|
||||
int cli_handle_count(const char *arg, void *config);
|
||||
int cli_handle_pattern(const char *arg, void *config);
|
||||
int cli_handle_dont_fragment(const char *arg, void *config);
|
||||
int cli_handle_deadline(const char *arg, void *config);
|
||||
int cli_handle_flood(const char *arg, void *config);
|
||||
|
|
|
|||
|
|
@ -22,6 +22,10 @@ struct ping_config
|
|||
double timeout;
|
||||
double deadline;
|
||||
|
||||
/* Pattern (-p) */
|
||||
uint8_t pattern[16];
|
||||
uint8_t pattern_len;
|
||||
|
||||
/* Flags */
|
||||
uint8_t flags;
|
||||
};
|
||||
|
|
|
|||
2
libcli
2
libcli
|
|
@ -1 +1 @@
|
|||
Subproject commit ae7fdf2f94a7c3c759d449ee1c4d3439473c6e42
|
||||
Subproject commit ad6a542969dda29afe5431d0e674eca8d8794141
|
||||
|
|
@ -44,6 +44,7 @@ FORCE:
|
|||
libping_core_la_SOURCES = \
|
||||
cli/parse.c \
|
||||
cli/handlers/handle_count.c \
|
||||
cli/handlers/handle_pattern.c \
|
||||
cli/handlers/handle_dont_fragment.c \
|
||||
cli/handlers/handle_deadline.c \
|
||||
cli/handlers/handle_flood.c \
|
||||
|
|
|
|||
14
src/ping/cli/handlers/handle_pattern.c
Normal file
14
src/ping/cli/handlers/handle_pattern.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include "ping/cli.h"
|
||||
#include "internal/cli/parse_utils.h"
|
||||
|
||||
int
|
||||
cli_handle_pattern(const char *arg, void *config_void)
|
||||
{
|
||||
struct ping_config *config = (struct ping_config *)config_void;
|
||||
size_t pattern_len;
|
||||
|
||||
if (0 != cli_parse_hex(arg, config->pattern, 16, &pattern_len))
|
||||
return CLI_ERROR;
|
||||
config->pattern_len = (uint8_t)pattern_len;
|
||||
return CLI_SUCCESS;
|
||||
}
|
||||
|
|
@ -39,6 +39,7 @@ option_arg_type_to_str(int type)
|
|||
case OPT_ARG_SECONDS: return "<SEC>";
|
||||
case OPT_ARG_BYTES: return "<SIZE>";
|
||||
case OPT_ARG_TTL: return "<TTL>";
|
||||
case OPT_ARG_PATTERN: return "<HEX>";
|
||||
case OPT_ARG_STRING: return "<STR>";
|
||||
default: return "<ARG>";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@
|
|||
#include "ping/ft_ping_flags.h"
|
||||
|
||||
/* Forward declarations */
|
||||
static inline void fill_payload(uint8_t *payload, size_t payload_len);
|
||||
static inline void fill_payload(uint8_t *payload, size_t len, const struct
|
||||
ping_config *config);
|
||||
static int ping_send_one(struct ping_state *state, uint16_t seq,
|
||||
const uint8_t *payload, size_t payload_len, struct timespec *ts);
|
||||
/* -------------------- */
|
||||
|
|
@ -25,7 +26,7 @@ do_send(struct ping_state *state, size_t payload_len)
|
|||
int ret;
|
||||
|
||||
state->send_flag = 0;
|
||||
fill_payload(payload, payload_len);
|
||||
fill_payload(payload, payload_len, state->config);
|
||||
ret = ping_send_one(state, state->seq, payload, payload_len, &ts);
|
||||
if (-1 == ret)
|
||||
dprintf(STDERR_FILENO, "%s: %s\n",
|
||||
|
|
@ -68,8 +69,16 @@ ping_send_one(struct ping_state *state, uint16_t seq,
|
|||
}
|
||||
|
||||
static inline void
|
||||
fill_payload(uint8_t *payload, size_t payload_len)
|
||||
fill_payload(uint8_t *payload, size_t len, const struct ping_config *config)
|
||||
{
|
||||
for (size_t i = 0; i < payload_len; ++i)
|
||||
if (0 == config->pattern_len)
|
||||
{
|
||||
for (size_t i = 0; i < len; ++i)
|
||||
payload[i] = (uint8_t)('a' + (i % 26));
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t i = 0; i < len; ++i)
|
||||
payload[i] = config->pattern[i % config->pattern_len];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue