From d5bcf8d0f5a2a549c3cbe71e7704bb557c7eb6a3 Mon Sep 17 00:00:00 2001 From: Vadim Kochan Date: Sat, 13 Aug 2016 02:11:12 +0300 Subject: trafgen: proto: Randomize proto field at runtime Add dynamic proto field function which can generate random value in specified range (default 0 - MAX_UINT32). Signed-off-by: Vadim Kochan Signed-off-by: Tobias Klauser --- trafgen_proto.c | 27 +++++++++++++++++++++++++++ trafgen_proto.h | 1 + 2 files changed, 28 insertions(+) diff --git a/trafgen_proto.c b/trafgen_proto.c index 6c37b21..8c316b1 100644 --- a/trafgen_proto.c +++ b/trafgen_proto.c @@ -469,6 +469,31 @@ static void field_inc_func(struct proto_field *field) } } +static inline uint32_t field_rand(struct proto_field *field) +{ + return field->func.min + (rand() % ((field->func.max - field->func.min) + 1)); +} + +static void field_rnd_func(struct proto_field *field) +{ + if (field->len == 1) { + proto_field_set_u8(field->hdr, field->id, + (uint8_t) field_rand(field)); + } else if (field->len == 2) { + proto_field_set_be16(field->hdr, field->id, + (uint16_t) field_rand(field)); + } else if (field->len == 4) { + proto_field_set_be32(field->hdr, field->id, + (uint32_t) field_rand(field)); + } else if (field->len > 4) { + uint8_t *bytes = __proto_field_get_bytes(field); + uint32_t i; + + for (i = 0; i < field->len; i++) + bytes[i] = (uint8_t) field_rand(field); + } +} + void proto_field_func_add(struct proto_hdr *hdr, uint32_t fid, struct proto_field_func *func) { @@ -499,6 +524,8 @@ void proto_field_func_add(struct proto_hdr *hdr, uint32_t fid, } field->func.update_field = field_inc_func; + } else if (func->type & PROTO_FIELD_FUNC_RND) { + field->func.update_field = field_rnd_func; } } diff --git a/trafgen_proto.h b/trafgen_proto.h index 0a1c3de..41e9383 100644 --- a/trafgen_proto.h +++ b/trafgen_proto.h @@ -54,6 +54,7 @@ struct proto_hdr { enum proto_field_func_t { PROTO_FIELD_FUNC_INC = 1 << 0, PROTO_FIELD_FUNC_MIN = 1 << 1, + PROTO_FIELD_FUNC_RND = 1 << 2, }; struct proto_field_func { -- cgit v1.2.3-54-g00ecf