On 2016-07-26 at 21:35:11 +0200, Vadim Kochan <vadi...@gmail.com> wrote: > Add dynamic proto field function which can generate > random value in specified range (default 0 - MAX_UINT32). > > Signed-off-by: Vadim Kochan <vadi...@gmail.com> > --- > trafgen_proto.c | 29 +++++++++++++++++++++++++++++ > trafgen_proto.h | 1 + > 2 files changed, 30 insertions(+) > > diff --git a/trafgen_proto.c b/trafgen_proto.c > index 069aa00..f57d390 100644 > --- a/trafgen_proto.c > +++ b/trafgen_proto.c > @@ -486,6 +486,30 @@ static void field_inc_func(struct proto_field *field) > } > } > > +static inline unsigned int 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) > +{ > + unsigned int val = field_rand(field); > + > + if (field->len == 1) { > + proto_field_set_u8(field->hdr, field->id, (uint8_t) val); > + } else if (field->len == 2) { > + proto_field_set_be16(field->hdr, field->id, (uint16_t) val); > + } else if (field->len == 4) { > + proto_field_set_be32(field->hdr, field->id, (uint32_t) val); > + } else if (field->len > 4) { > + uint8_t *bytes = __proto_field_get_bytes(field); > + uint32_t i; > + > + for (i = 0; i < field->len; i++, val = field_rand(field)) > + bytes[i] = (uint8_t) val;
No need for the assigment of `val' in the loop, should rather be like this: 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) > { > @@ -522,6 +546,11 @@ void proto_field_func_add(struct proto_hdr *hdr, > uint32_t fid, > > field->func.update_field = field_inc_func; > } > + > + if (func->type & PROTO_FIELD_FUNC_RND) { > + field->func.max = field->func.max ?: (uint32_t)~0 - 1; UINT32_MAX, please. Also in all other places in this series. > + field->func.update_field = field_rnd_func; > + } > } > > void proto_field_dyn_apply(struct proto_field *field) > diff --git a/trafgen_proto.h b/trafgen_proto.h > index 64f4366..40817a8 100644 > --- a/trafgen_proto.h > +++ b/trafgen_proto.h > @@ -38,6 +38,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 { > -- > 2.6.3 > -- You received this message because you are subscribed to the Google Groups "netsniff-ng" group. To unsubscribe from this group and stop receiving emails from it, send an email to netsniff-ng+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.