#include <unistd.h>
#include "ctx.h"
-#include "logging.h"
#include "random.h"
static int parse_address(const char* string, struct in6_addr* address6) {
// Initialize random pool
if (!c->zero) {
- c->pool = fireperf_random_pool_create(c, DEFAULT_RANDOM_POOL_SIZE);
- if (!c->pool) {
- ERROR(c, "Could not allocate random data\n");
- r = 1;
+ r = fireperf_random_pool_init(c);
+ if (r)
goto ERROR;
- }
}
// Return the context
}
void fireperf_ctx_free(struct fireperf_ctx* ctx) {
- if (ctx->pool)
- fireperf_random_pool_free(ctx->pool);
-
free(ctx);
}
#include "constants.h"
// Forward declarations
-struct fireperf_random_pool;
struct fireperf_worker;
struct fireperf_ctx {
int keepalive_only;
int keepalive_count;
int keepalive_interval;
- struct fireperf_random_pool* pool;
int port;
unsigned int listening_sockets;
unsigned long parallel;
int close;
int zero;
+ // Random Pool
+ char pool[DEFAULT_RANDOM_POOL_SIZE];
+
// Workers
struct fireperf_workers* workers[MAX_WORKERS];
unsigned int num_workers;
#include "logging.h"
#include "random.h"
-struct fireperf_random_pool* fireperf_random_pool_create(struct fireperf_ctx* ctx, size_t size) {
- struct fireperf_random_pool* pool = calloc(1, sizeof(*pool));
- if (!pool)
- return NULL;
-
- pool->size = size;
-
- // Allocate the data array
- pool->data = malloc(pool->size);
- if (!pool->data)
- goto ERROR;
-
+int fireperf_random_pool_init(struct fireperf_ctx* ctx) {
size_t offset = 0;
- while (offset < pool->size) {
- offset += getrandom(pool->data + offset, pool->size - offset, 0);
- }
- DEBUG(ctx, "Allocated random pool of %zu bytes(s)\n", pool->size);
-
- return pool;
-
-ERROR:
- fireperf_random_pool_free(pool);
-
- return NULL;
-}
+ while (offset < sizeof(ctx->pool)) {
+ offset += getrandom(ctx->pool + offset, sizeof(ctx->pool) - offset, 0);
+ }
-void fireperf_random_pool_free(struct fireperf_random_pool* pool) {
- if (pool->data)
- free(pool->data);
+ DEBUG(ctx, "Allocated random pool of %zu bytes(s)\n", sizeof(ctx->pool));
- free(pool);
+ return 0;
}
-const char* fireperf_random_pool_get_slice(struct fireperf_random_pool* pool, size_t size) {
- if (size > pool->size)
+const char* fireperf_random_pool_get_slice(struct fireperf_ctx* ctx, size_t size) {
+ if (size > sizeof(ctx->pool))
return NULL;
// Find a random value between the start and end of
// the data region that is at least size bytes long.
- off_t offset = random() % (pool->size - size);
+ off_t offset = random() % (sizeof(ctx->pool) - size);
- return pool->data + offset;
+ return ctx->pool + offset;
}
#ifndef FIREPERF_RANDOM_H
#define FIREPERF_RANDOM_H
-struct fireperf_random_pool {
- char* data;
- size_t size;
-};
-
#include "ctx.h"
-struct fireperf_random_pool* fireperf_random_pool_create(
- struct fireperf_ctx* ctx, size_t size);
-void fireperf_random_pool_free(struct fireperf_random_pool* pool);
+int fireperf_random_pool_init(struct fireperf_ctx* ctx);
-const char* fireperf_random_pool_get_slice(struct fireperf_random_pool* pool, size_t size);
+const char* fireperf_random_pool_get_slice(struct fireperf_ctx* ctx, size_t size);
#endif /* FIREPERF_RANDOM_H */