diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
commit | 537a7180a0d5132c11636c4fd8b1450cd99d352c (patch) | |
tree | fb9f8c84955a411bdffbd6371ea32f2716fb3687 /utils | |
parent | 5fd7a90fdaa33f52c59bdb0ca84451e5c1e22365 (diff) | |
download | rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.tar.gz rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.zip |
[Rework] Use clang-format to unify formatting in all sources
No meaningful changes.
Diffstat (limited to 'utils')
-rw-r--r-- | utils/base64.c | 41 | ||||
-rw-r--r-- | utils/rspamd_http_bench.c | 340 | ||||
-rw-r--r-- | utils/rspamd_http_server.c | 234 |
3 files changed, 302 insertions, 313 deletions
diff --git a/utils/base64.c b/utils/base64.c index ea8bafda8..d1202db53 100644 --- a/utils/base64.c +++ b/utils/base64.c @@ -24,7 +24,7 @@ static gdouble total_time = 0; static void -rspamd_process_file (const gchar *fname, gint decode) +rspamd_process_file(const gchar *fname, gint decode) { gint fd; gpointer map; @@ -32,43 +32,42 @@ rspamd_process_file (const gchar *fname, gint decode) guint8 *dest; gsize destlen; - fd = open (fname, O_RDONLY); + fd = open(fname, O_RDONLY); if (fd == -1) { - rspamd_fprintf (stderr, "cannot open %s: %s", fname, strerror (errno)); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "cannot open %s: %s", fname, strerror(errno)); + exit(EXIT_FAILURE); } - if (fstat (fd, &st) == -1) { - rspamd_fprintf (stderr, "cannot stat %s: %s", fname, strerror (errno)); - exit (EXIT_FAILURE); + if (fstat(fd, &st) == -1) { + rspamd_fprintf(stderr, "cannot stat %s: %s", fname, strerror(errno)); + exit(EXIT_FAILURE); } - map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); - close (fd); + map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); + close(fd); if (map == MAP_FAILED) { - rspamd_fprintf (stderr, "cannot mmap %s: %s", fname, strerror (errno)); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "cannot mmap %s: %s", fname, strerror(errno)); + exit(EXIT_FAILURE); } if (decode) { destlen = st.st_size / 4 * 3 + 10; - dest = g_malloc (destlen); - rspamd_cryptobox_base64_decode (map, st.st_size, dest, &destlen); + dest = g_malloc(destlen); + rspamd_cryptobox_base64_decode(map, st.st_size, dest, &destlen); } else { - dest = rspamd_encode_base64 (map, st.st_size, 80, &destlen); + dest = rspamd_encode_base64(map, st.st_size, 80, &destlen); } - rspamd_printf ("%*s", (gint)destlen, dest); - g_free (dest); + rspamd_printf("%*s", (gint) destlen, dest); + g_free(dest); - munmap (map, st.st_size); + munmap(map, st.st_size); } -int -main (int argc, char **argv) +int main(int argc, char **argv) { gint i, start = 1, decode = 0; @@ -80,9 +79,9 @@ main (int argc, char **argv) } } - for (i = start; i < argc; i ++) { + for (i = start; i < argc; i++) { if (argv[i]) { - rspamd_process_file (argv[i], decode); + rspamd_process_file(argv[i], decode); } } diff --git a/utils/rspamd_http_bench.c b/utils/rspamd_http_bench.c index 030b0b4b6..232fc8a1a 100644 --- a/utils/rspamd_http_bench.c +++ b/utils/rspamd_http_bench.c @@ -48,30 +48,29 @@ static const guint store_latencies = 1000; static guint32 conns_pending = 0; static GOptionEntry entries[] = { - {"port", 'p', 0, G_OPTION_ARG_INT, &port, - "Port number (default: 43000)", NULL}, - {"cache", 'c', 0, G_OPTION_ARG_INT, &cache_size, - "Keys cache size (default: 10)", NULL}, - {"workers", 'n', 0, G_OPTION_ARG_INT, &nworkers, - "Number of workers to start (default: 1)", NULL}, - {"size", 's', 0, G_OPTION_ARG_INT, &file_size, - "Size of payload to transfer (default: 500)", NULL}, - {"conns", 'C', 0, G_OPTION_ARG_INT, &pconns, - "Number of parallel connections (default: 100)", NULL}, - {"time", 't', 0, G_OPTION_ARG_DOUBLE, &test_time, - "Time to run tests (default: 10.0 sec)", NULL}, - {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl_mode, - "Use openssl crypto", NULL}, - {"host", 'h', 0, G_OPTION_ARG_STRING, &host, - "Connect to the specified host (default: localhost)", NULL}, - {"key", 'k', 0, G_OPTION_ARG_STRING, &server_key, - "Use the specified key (base32 encoded)", NULL}, - {"latency", 'l', 0, G_OPTION_ARG_FILENAME, &latencies_file, - "Write latencies to the specified file", NULL}, - {"csv", 0, 0, G_OPTION_ARG_NONE, &csv_output, - "Output CSV", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + {"port", 'p', 0, G_OPTION_ARG_INT, &port, + "Port number (default: 43000)", NULL}, + {"cache", 'c', 0, G_OPTION_ARG_INT, &cache_size, + "Keys cache size (default: 10)", NULL}, + {"workers", 'n', 0, G_OPTION_ARG_INT, &nworkers, + "Number of workers to start (default: 1)", NULL}, + {"size", 's', 0, G_OPTION_ARG_INT, &file_size, + "Size of payload to transfer (default: 500)", NULL}, + {"conns", 'C', 0, G_OPTION_ARG_INT, &pconns, + "Number of parallel connections (default: 100)", NULL}, + {"time", 't', 0, G_OPTION_ARG_DOUBLE, &test_time, + "Time to run tests (default: 10.0 sec)", NULL}, + {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl_mode, + "Use openssl crypto", NULL}, + {"host", 'h', 0, G_OPTION_ARG_STRING, &host, + "Connect to the specified host (default: localhost)", NULL}, + {"key", 'k', 0, G_OPTION_ARG_STRING, &server_key, + "Use the specified key (base32 encoded)", NULL}, + {"latency", 'l', 0, G_OPTION_ARG_FILENAME, &latencies_file, + "Write latencies to the specified file", NULL}, + {"csv", 0, 0, G_OPTION_ARG_NONE, &csv_output, + "Output CSV", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; struct lat_elt { gdouble lat; @@ -81,11 +80,11 @@ struct lat_elt { static struct lat_elt *latencies; static gint -rspamd_client_body (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - const gchar *chunk, gsize len) +rspamd_client_body(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + const gchar *chunk, gsize len) { - g_assert (chunk[0] == '\0'); + g_assert(chunk[0] == '\0'); return 0; } @@ -98,43 +97,43 @@ struct client_cbdata { }; static void -rspamd_client_err (struct rspamd_http_connection *conn, GError *err) +rspamd_client_err(struct rspamd_http_connection *conn, GError *err) { - msg_info ("abnormally closing connection from: error: %s", - err->message); + msg_info("abnormally closing connection from: error: %s", + err->message); - g_assert (0); - close (conn->fd); - rspamd_http_connection_unref (conn); + g_assert(0); + close(conn->fd); + rspamd_http_connection_unref(conn); } static gint -rspamd_client_finish (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +rspamd_client_finish(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { struct client_cbdata *cb = conn->ud; - cb->lat->lat = rspamd_get_ticks () - cb->ts; + cb->lat->lat = rspamd_get_ticks() - cb->ts; cb->lat->checked = TRUE; - (*cb->wconns) ++; - conns_pending --; - close (conn->fd); - rspamd_http_connection_unref (conn); - g_free (cb); + (*cb->wconns)++; + conns_pending--; + close(conn->fd); + rspamd_http_connection_unref(conn); + g_free(cb); if (conns_pending == 0) { - event_base_loopexit (cb->ev_base, NULL); + event_base_loopexit(cb->ev_base, NULL); } return 0; } static void -rspamd_http_client_func (struct ev_loop *ev_base, struct lat_elt *latency, - guint32 *wconns, - struct rspamd_cryptobox_pubkey *peer_key, - struct rspamd_cryptobox_keypair* client_key, - struct rspamd_keypair_cache *c) +rspamd_http_client_func(struct ev_loop *ev_base, struct lat_elt *latency, + guint32 *wconns, + struct rspamd_cryptobox_pubkey *peer_key, + struct rspamd_cryptobox_keypair *client_key, + struct rspamd_keypair_cache *c) { struct rspamd_http_message *msg; struct rspamd_http_connection *conn; @@ -142,40 +141,40 @@ rspamd_http_client_func (struct ev_loop *ev_base, struct lat_elt *latency, struct client_cbdata *cb; gint fd, flags; - fd = rspamd_inet_address_connect (addr, SOCK_STREAM, TRUE); - g_assert (fd != -1); + fd = rspamd_inet_address_connect(addr, SOCK_STREAM, TRUE); + g_assert(fd != -1); flags = 1; - (void)setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof (flags)); - conn = rspamd_http_connection_new (rspamd_client_body, - rspamd_client_err, - rspamd_client_finish, - RSPAMD_HTTP_CLIENT_SIMPLE, - RSPAMD_HTTP_CLIENT, - c, - NULL); - rspamd_snprintf (urlbuf, sizeof (urlbuf), "http://%s/%d", host, file_size); - msg = rspamd_http_message_from_url (urlbuf); - - g_assert (conn != NULL && msg != NULL); + (void) setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags)); + conn = rspamd_http_connection_new(rspamd_client_body, + rspamd_client_err, + rspamd_client_finish, + RSPAMD_HTTP_CLIENT_SIMPLE, + RSPAMD_HTTP_CLIENT, + c, + NULL); + rspamd_snprintf(urlbuf, sizeof(urlbuf), "http://%s/%d", host, file_size); + msg = rspamd_http_message_from_url(urlbuf); + + g_assert(conn != NULL && msg != NULL); if (peer_key != NULL) { - g_assert (client_key != NULL); - rspamd_http_connection_set_key (conn, client_key); - msg->peer_key = rspamd_pubkey_ref (peer_key); + g_assert(client_key != NULL); + rspamd_http_connection_set_key(conn, client_key); + msg->peer_key = rspamd_pubkey_ref(peer_key); } - cb = g_malloc (sizeof (*cb)); - cb->ts = rspamd_get_ticks (); + cb = g_malloc(sizeof(*cb)); + cb->ts = rspamd_get_ticks(); cb->lat = latency; cb->ev_base = ev_base; cb->wconns = wconns; latency->checked = FALSE; - rspamd_http_connection_write_message (conn, msg, NULL, NULL, cb, - fd, NULL, ev_base); + rspamd_http_connection_write_message(conn, msg, NULL, NULL, cb, + fd, NULL, ev_base); } static void -rspamd_worker_func (struct lat_elt *plat, guint32 *wconns) +rspamd_worker_func(struct lat_elt *plat, guint32 *wconns) { guint i, j; struct ev_loop *ev_base; @@ -185,37 +184,37 @@ rspamd_worker_func (struct lat_elt *plat, guint32 *wconns) struct rspamd_cryptobox_pubkey *peer_key = NULL; if (server_key) { - peer_key = rspamd_pubkey_from_base32 (server_key, 0, RSPAMD_KEYPAIR_KEX, - openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); - g_assert (peer_key != NULL); - client_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); + peer_key = rspamd_pubkey_from_base32(server_key, 0, RSPAMD_KEYPAIR_KEX, + openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); + g_assert(peer_key != NULL); + client_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); if (cache_size > 0) { - c = rspamd_keypair_cache_new (cache_size); + c = rspamd_keypair_cache_new(cache_size); } } - memset (&itv, 0, sizeof (itv)); - double_to_tv (test_time, &itv.it_value); + memset(&itv, 0, sizeof(itv)); + double_to_tv(test_time, &itv.it_value); - ev_base = event_init (); - g_assert (setitimer (ITIMER_REAL, &itv, NULL) != -1); + ev_base = event_init(); + g_assert(setitimer(ITIMER_REAL, &itv, NULL) != -1); - for (i = 0; ; i = (i + 1) % store_latencies) { + for (i = 0;; i = (i + 1) % store_latencies) { for (j = 0; j < pconns; j++) { - rspamd_http_client_func (ev_base, &plat[i * pconns + j], - wconns, peer_key, client_key, c); + rspamd_http_client_func(ev_base, &plat[i * pconns + j], + wconns, peer_key, client_key, c); } conns_pending = pconns; - event_base_loop (ev_base, 0); + event_base_loop(ev_base, 0); } } static int -cmpd (const void *p1, const void *p2) +cmpd(const void *p1, const void *p2) { const struct lat_elt *d1 = p1, *d2 = p2; @@ -223,193 +222,190 @@ cmpd (const void *p1, const void *p2) } double -rspamd_http_calculate_mean (struct lat_elt *lats, double *std) +rspamd_http_calculate_mean(struct lat_elt *lats, double *std) { guint i, cnt, checked = 0; gdouble mean = 0., dev = 0.; cnt = store_latencies * pconns; - qsort (lats, cnt, sizeof (*lats), cmpd); + qsort(lats, cnt, sizeof(*lats), cmpd); for (i = 0; i < cnt; i++) { if (lats[i].checked) { mean += lats[i].lat; - checked ++; + checked++; } } - g_assert (checked > 0); + g_assert(checked > 0); mean /= checked; for (i = 0; i < cnt; i++) { if (lats[i].checked) { - dev += pow ((lats[i].lat - mean), 2); + dev += pow((lats[i].lat - mean), 2); } } dev /= checked; - *std = sqrt (dev); + *std = sqrt(dev); return mean; } static void -rspamd_http_start_workers (pid_t *sfd) +rspamd_http_start_workers(pid_t *sfd) { guint i; for (i = 0; i < nworkers; i++) { - sfd[i] = fork (); - g_assert (sfd[i] != -1); + sfd[i] = fork(); + g_assert(sfd[i] != -1); if (sfd[i] == 0) { - gperf_profiler_init (NULL, "http-bench"); - rspamd_worker_func (&latencies[i * pconns * store_latencies], - &conns_done[i]); - gperf_profiler_stop (); - exit (EXIT_SUCCESS); + gperf_profiler_init(NULL, "http-bench"); + rspamd_worker_func(&latencies[i * pconns * store_latencies], + &conns_done[i]); + gperf_profiler_stop(); + exit(EXIT_SUCCESS); } - workers_left ++; + workers_left++; } } static void -rspamd_http_stop_workers (pid_t *sfd) +rspamd_http_stop_workers(pid_t *sfd) { guint i; gint res; for (i = 0; i < nworkers; i++) { - kill (sfd[i], SIGTERM); - wait (&res); + kill(sfd[i], SIGTERM); + wait(&res); } } static void -rspamd_http_bench_term (int fd, short what, void *arg) +rspamd_http_bench_term(int fd, short what, void *arg) { pid_t *sfd = arg; - rspamd_http_stop_workers (sfd); - event_loopexit (NULL); + rspamd_http_stop_workers(sfd); + event_loopexit(NULL); } static void -rspamd_http_bench_cld (int fd, short what, void *arg) +rspamd_http_bench_cld(int fd, short what, void *arg) { gint res; - while (waitpid (-1, &res, WNOHANG) > 0) { + while (waitpid(-1, &res, WNOHANG) > 0) { if (--workers_left == 0) { - event_loopexit (NULL); + event_loopexit(NULL); } } } -int -main (int argc, char **argv) +int main(int argc, char **argv) { GOptionContext *context; GError *error = NULL; pid_t *sfd; struct ev_loop *ev_base; - rspamd_mempool_t *pool = rspamd_mempool_new (8192, "http-bench"); + rspamd_mempool_t *pool = rspamd_mempool_new(8192, "http-bench"); struct event term_ev, int_ev, cld_ev; guint64 total_done; FILE *lat_file; gdouble mean, std; guint i; - rspamd_init_libs (); - - context = g_option_context_new ( - "rspamd-http-bench - test server for benchmarks"); - g_option_context_set_summary (context, - "Summary:\n Rspamd test HTTP benchmark " - RVERSION - "\n Release id: " - RID); - g_option_context_add_main_entries (context, entries, NULL); - - if (!g_option_context_parse (context, &argc, &argv, &error)) { - rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - exit (EXIT_FAILURE); + rspamd_init_libs(); + + context = g_option_context_new( + "rspamd-http-bench - test server for benchmarks"); + g_option_context_set_summary(context, + "Summary:\n Rspamd test HTTP benchmark " RVERSION + "\n Release id: " RID); + g_option_context_add_main_entries(context, entries, NULL); + + if (!g_option_context_parse(context, &argc, &argv, &error)) { + rspamd_fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + exit(EXIT_FAILURE); } - rspamd_parse_inet_address (&addr, host, 0); - g_assert (addr != NULL); - rspamd_inet_address_set_port (addr, port); + rspamd_parse_inet_address(&addr, host, 0); + g_assert(addr != NULL); + rspamd_inet_address_set_port(addr, port); - latencies = rspamd_mempool_alloc_shared (pool, - nworkers * pconns * store_latencies * sizeof (*latencies)); - sfd = g_malloc (sizeof (*sfd) * nworkers); - conns_done = rspamd_mempool_alloc_shared (pool, sizeof (guint32) * nworkers); - memset (conns_done, 0, sizeof (guint32) * nworkers); + latencies = rspamd_mempool_alloc_shared(pool, + nworkers * pconns * store_latencies * sizeof(*latencies)); + sfd = g_malloc(sizeof(*sfd) * nworkers); + conns_done = rspamd_mempool_alloc_shared(pool, sizeof(guint32) * nworkers); + memset(conns_done, 0, sizeof(guint32) * nworkers); - rspamd_http_start_workers (sfd); + rspamd_http_start_workers(sfd); - ev_base = event_init (); + ev_base = event_init(); - event_set (&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_bench_term, sfd); - event_base_set (ev_base, &term_ev); - event_add (&term_ev, NULL); - event_set (&int_ev, SIGINT, EV_SIGNAL, rspamd_http_bench_term, sfd); - event_base_set (ev_base, &int_ev); - event_add (&int_ev, NULL); - event_set (&cld_ev, SIGCHLD, EV_SIGNAL|EV_PERSIST, - rspamd_http_bench_cld, NULL); - event_base_set (ev_base, &cld_ev); - event_add (&cld_ev, NULL); + event_set(&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_bench_term, sfd); + event_base_set(ev_base, &term_ev); + event_add(&term_ev, NULL); + event_set(&int_ev, SIGINT, EV_SIGNAL, rspamd_http_bench_term, sfd); + event_base_set(ev_base, &int_ev); + event_add(&int_ev, NULL); + event_set(&cld_ev, SIGCHLD, EV_SIGNAL | EV_PERSIST, + rspamd_http_bench_cld, NULL); + event_base_set(ev_base, &cld_ev); + event_add(&cld_ev, NULL); - event_base_loop (ev_base, 0); + event_base_loop(ev_base, 0); total_done = 0; - for (i = 0; i < nworkers; i ++) { + for (i = 0; i < nworkers; i++) { total_done += conns_done[i]; } - mean = rspamd_http_calculate_mean (latencies, &std); + mean = rspamd_http_calculate_mean(latencies, &std); if (!csv_output) { - rspamd_printf ( - "Made %L connections of size %d in %.6fs, %.6f cps, %.6f MB/sec\n", - total_done, - file_size, - test_time, - total_done / test_time, - total_done * file_size / test_time / (1024.0 * 1024.0)); - rspamd_printf ("Latency: %.6f ms mean, %.6f dev\n", - mean * 1000.0, std * 1000.0); + rspamd_printf( + "Made %L connections of size %d in %.6fs, %.6f cps, %.6f MB/sec\n", + total_done, + file_size, + test_time, + total_done / test_time, + total_done * file_size / test_time / (1024.0 * 1024.0)); + rspamd_printf("Latency: %.6f ms mean, %.6f dev\n", + mean * 1000.0, std * 1000.0); } else { /* size,connections,time,mean,stddev,conns,workers */ - rspamd_printf ("%ud,%L,%.1f,%.6f,%.6f,%ud,%ud\n", - file_size, - total_done, - test_time, - mean*1000.0, - std*1000.0, - pconns, - nworkers); + rspamd_printf("%ud,%L,%.1f,%.6f,%.6f,%ud,%ud\n", + file_size, + total_done, + test_time, + mean * 1000.0, + std * 1000.0, + pconns, + nworkers); } if (latencies_file) { - lat_file = fopen (latencies_file, "w"); + lat_file = fopen(latencies_file, "w"); if (lat_file) { - for (i = 0; i < store_latencies * pconns; i ++) { + for (i = 0; i < store_latencies * pconns; i++) { if (latencies[i].checked) { - rspamd_fprintf (lat_file, "%.6f\n", latencies[i].lat); + rspamd_fprintf(lat_file, "%.6f\n", latencies[i].lat); } } - fclose (lat_file); + fclose(lat_file); } } - rspamd_mempool_delete (pool); + rspamd_mempool_delete(pool); return 0; } diff --git a/utils/rspamd_http_server.c b/utils/rspamd_http_server.c index 679ffa0b6..ecd1d38c7 100644 --- a/utils/rspamd_http_server.c +++ b/utils/rspamd_http_server.c @@ -38,23 +38,21 @@ static gchar *key = NULL; static struct rspamd_keypair_cache *c; static struct rspamd_cryptobox_keypair *server_key; static struct timeval io_tv = { - .tv_sec = 20, - .tv_usec = 0 -}; + .tv_sec = 20, + .tv_usec = 0}; static GOptionEntry entries[] = { - {"port", 'p', 0, G_OPTION_ARG_INT, &port, - "Port number (default: 43000)", NULL}, - {"cache", 'c', 0, G_OPTION_ARG_INT, &cache_size, - "Keys cache size (default: 10)", NULL}, - {"workers", 'n', 0, G_OPTION_ARG_INT, &nworkers, - "Number of workers to start (default: 1)", NULL}, - {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl_mode, - "Use openssl crypto", NULL}, - {"key", 'k', 0, G_OPTION_ARG_STRING, &key, - "Use static keypair instead of new one (base32 encoded sk || pk)", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + {"port", 'p', 0, G_OPTION_ARG_INT, &port, + "Port number (default: 43000)", NULL}, + {"cache", 'c', 0, G_OPTION_ARG_INT, &cache_size, + "Keys cache size (default: 10)", NULL}, + {"workers", 'n', 0, G_OPTION_ARG_INT, &nworkers, + "Number of workers to start (default: 1)", NULL}, + {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl_mode, + "Use openssl crypto", NULL}, + {"key", 'k', 0, G_OPTION_ARG_STRING, &key, + "Use static keypair instead of new one (base32 encoded sk || pk)", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; struct rspamd_http_server_session { struct rspamd_http_connection *conn; @@ -65,20 +63,20 @@ struct rspamd_http_server_session { }; static void -rspamd_server_error (struct rspamd_http_connection *conn, - GError *err) +rspamd_server_error(struct rspamd_http_connection *conn, + GError *err) { struct rspamd_http_server_session *session = conn->ud; - rspamd_fprintf (stderr, "http error occurred: %s\n", err->message); - rspamd_http_connection_unref (conn); - close (session->fd); - g_slice_free1 (sizeof (*session), session); + rspamd_fprintf(stderr, "http error occurred: %s\n", err->message); + rspamd_http_connection_unref(conn); + close(session->fd); + g_slice_free1(sizeof(*session), session); } static int -rspamd_server_finish (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +rspamd_server_finish(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { struct rspamd_http_server_session *session = conn->ud; struct rspamd_http_message *reply; @@ -89,48 +87,47 @@ rspamd_server_finish (struct rspamd_http_connection *conn, if (!session->reply) { session->reply = TRUE; - reply = rspamd_http_new_message (HTTP_RESPONSE); + reply = rspamd_http_new_message(HTTP_RESPONSE); url_str = msg->url->str; url_len = msg->url->len; if (url_str[0] == '/') { - url_str ++; - url_len --; + url_str++; + url_len--; } - if (rspamd_strtoul (url_str, url_len, &size)) { + if (rspamd_strtoul(url_str, url_len, &size)) { session->req_size = size; reply->code = 200; - reply->status = rspamd_fstring_new_init ("OK", 2); - body = rspamd_fstring_sized_new (size); + reply->status = rspamd_fstring_new_init("OK", 2); + body = rspamd_fstring_sized_new(size); body->len = size; - memset (body->str, 0, size); - rspamd_http_message_set_body_from_fstring_steal (msg, body); - + memset(body->str, 0, size); + rspamd_http_message_set_body_from_fstring_steal(msg, body); } else { reply->code = 404; - reply->status = rspamd_fstring_new_init ("Not found", 9); + reply->status = rspamd_fstring_new_init("Not found", 9); } - rspamd_http_connection_reset (conn); - rspamd_http_connection_write_message (conn, reply, NULL, - "application/octet-stream", session, session->fd, - &io_tv, session->ev_base); + rspamd_http_connection_reset(conn); + rspamd_http_connection_write_message(conn, reply, NULL, + "application/octet-stream", session, session->fd, + &io_tv, session->ev_base); } else { /* Destroy session */ - rspamd_http_connection_unref (conn); - close (session->fd); - g_slice_free1 (sizeof (*session), session); + rspamd_http_connection_unref(conn); + close(session->fd); + g_slice_free1(sizeof(*session), session); } return 0; } static void -rspamd_server_accept (gint fd, short what, void *arg) +rspamd_server_accept(gint fd, short what, void *arg) { struct ev_loop *ev_base = arg; struct rspamd_http_server_session *session; @@ -139,31 +136,31 @@ rspamd_server_accept (gint fd, short what, void *arg) do { if ((nfd = - rspamd_accept_from_socket (fd, &addr, NULL)) == -1) { - rspamd_fprintf (stderr, "accept failed: %s", strerror (errno)); + rspamd_accept_from_socket(fd, &addr, NULL)) == -1) { + rspamd_fprintf(stderr, "accept failed: %s", strerror(errno)); return; } /* Check for EAGAIN */ if (nfd == 0) { - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); return; } - rspamd_inet_address_free (addr); - session = g_slice_alloc (sizeof (*session)); - session->conn = rspamd_http_connection_new (NULL, - rspamd_server_error, - rspamd_server_finish, - 0, - RSPAMD_HTTP_SERVER, - c, - NULL); - rspamd_http_connection_set_key (session->conn, server_key); - rspamd_http_connection_read_message (session->conn, - session, - nfd, - &io_tv, - ev_base); + rspamd_inet_address_free(addr); + session = g_slice_alloc(sizeof(*session)); + session->conn = rspamd_http_connection_new(NULL, + rspamd_server_error, + rspamd_server_finish, + 0, + RSPAMD_HTTP_SERVER, + c, + NULL); + rspamd_http_connection_set_key(session->conn, server_key); + rspamd_http_connection_read_message(session->conn, + session, + nfd, + &io_tv, + ev_base); session->reply = FALSE; session->fd = nfd; session->ev_base = ev_base; @@ -171,76 +168,75 @@ rspamd_server_accept (gint fd, short what, void *arg) } static void -rspamd_http_term_handler (gint fd, short what, void *arg) +rspamd_http_term_handler(gint fd, short what, void *arg) { struct ev_loop *ev_base = arg; struct timeval tv = {0, 0}; - event_base_loopexit (ev_base, &tv); + event_base_loopexit(ev_base, &tv); } static void -rspamd_http_server_func (gint fd, rspamd_inet_addr_t *addr) +rspamd_http_server_func(gint fd, rspamd_inet_addr_t *addr) { - struct ev_loop *ev_base = event_init (); + struct ev_loop *ev_base = event_init(); struct event accept_ev, term_ev; - event_set (&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, ev_base); - event_base_set (ev_base, &accept_ev); - event_add (&accept_ev, NULL); + event_set(&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, ev_base); + event_base_set(ev_base, &accept_ev); + event_add(&accept_ev, NULL); - evsignal_set (&term_ev, SIGTERM, rspamd_http_term_handler, ev_base); - event_base_set (ev_base, &term_ev); - event_add (&term_ev, NULL); + evsignal_set(&term_ev, SIGTERM, rspamd_http_term_handler, ev_base); + event_base_set(ev_base, &term_ev); + event_add(&term_ev, NULL); - event_base_loop (ev_base, 0); + event_base_loop(ev_base, 0); } static void -rspamd_http_start_servers (pid_t *sfd, rspamd_inet_addr_t *addr) +rspamd_http_start_servers(pid_t *sfd, rspamd_inet_addr_t *addr) { guint i; gint fd; - fd = rspamd_inet_address_listen (addr, SOCK_STREAM, TRUE); - g_assert (fd != -1); + fd = rspamd_inet_address_listen(addr, SOCK_STREAM, TRUE); + g_assert(fd != -1); for (i = 0; i < nworkers; i++) { - sfd[i] = fork (); - g_assert (sfd[i] != -1); + sfd[i] = fork(); + g_assert(sfd[i] != -1); if (sfd[i] == 0) { - rspamd_http_server_func (fd, addr); - exit (EXIT_SUCCESS); + rspamd_http_server_func(fd, addr); + exit(EXIT_SUCCESS); } } - close (fd); + close(fd); } static void -rspamd_http_stop_servers (pid_t *sfd) +rspamd_http_stop_servers(pid_t *sfd) { guint i; gint res; for (i = 0; i < nworkers; i++) { - kill (sfd[i], SIGTERM); - wait (&res); + kill(sfd[i], SIGTERM); + wait(&res); } } static void -rspamd_http_server_term (int fd, short what, void *arg) +rspamd_http_server_term(int fd, short what, void *arg) { pid_t *sfd = arg; - rspamd_http_stop_servers (sfd); - event_loopexit (NULL); + rspamd_http_stop_servers(sfd); + event_loopexit(NULL); } -int -main (int argc, gchar **argv) +int main(int argc, gchar **argv) { GOptionContext *context; GError *error = NULL; @@ -251,56 +247,54 @@ main (int argc, gchar **argv) struct event term_ev, int_ev; struct in_addr ina = {INADDR_ANY}; - rspamd_init_libs (); - - context = g_option_context_new ( - "rspamd-http-server - test server for benchmarks"); - g_option_context_set_summary (context, - "Summary:\n Rspamd test HTTP server " - RVERSION - "\n Release id: " - RID); - g_option_context_add_main_entries (context, entries, NULL); - - if (!g_option_context_parse (context, &argc, &argv, &error)) { - rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - exit (EXIT_FAILURE); + rspamd_init_libs(); + + context = g_option_context_new( + "rspamd-http-server - test server for benchmarks"); + g_option_context_set_summary(context, + "Summary:\n Rspamd test HTTP server " RVERSION + "\n Release id: " RID); + g_option_context_add_main_entries(context, entries, NULL); + + if (!g_option_context_parse(context, &argc, &argv, &error)) { + rspamd_fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + exit(EXIT_FAILURE); } - maps = g_hash_table_new (g_int_hash, g_int_equal); + maps = g_hash_table_new(g_int_hash, g_int_equal); if (key == NULL) { - server_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); - b32_key = rspamd_keypair_print (server_key, - RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); - rspamd_printf ("key: %v\n", b32_key); + server_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); + b32_key = rspamd_keypair_print(server_key, + RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); + rspamd_printf("key: %v\n", b32_key); } else { /* TODO: add key loading */ } if (cache_size > 0) { - c = rspamd_keypair_cache_new (cache_size); + c = rspamd_keypair_cache_new(cache_size); } - sfd = g_alloca (sizeof (*sfd) * nworkers); - addr = rspamd_inet_address_new (AF_INET, &ina); - rspamd_inet_address_set_port (addr, port); - rspamd_http_start_servers (sfd, addr); + sfd = g_alloca(sizeof(*sfd) * nworkers); + addr = rspamd_inet_address_new(AF_INET, &ina); + rspamd_inet_address_set_port(addr, port); + rspamd_http_start_servers(sfd, addr); /* Just wait for workers */ - ev_base = event_init (); + ev_base = event_init(); - event_set (&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_server_term, sfd); - event_base_set (ev_base, &term_ev); - event_add (&term_ev, NULL); - event_set (&int_ev, SIGINT, EV_SIGNAL, rspamd_http_server_term, sfd); - event_base_set (ev_base, &int_ev); - event_add (&int_ev, NULL); + event_set(&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_server_term, sfd); + event_base_set(ev_base, &term_ev); + event_add(&term_ev, NULL); + event_set(&int_ev, SIGINT, EV_SIGNAL, rspamd_http_server_term, sfd); + event_base_set(ev_base, &int_ev); + event_add(&int_ev, NULL); - event_base_loop (ev_base, 0); + event_base_loop(ev_base, 0); return 0; } |