mirror of
https://github.com/AFLplusplus/AFLplusplus.git
synced 2025-06-15 19:38:09 +00:00
Refactor global var into afl_state_t struct
This commit is contained in:
@ -633,8 +633,11 @@ typedef struct afl_state {
|
|||||||
u64 plot_prev_qc, plot_prev_uc, plot_prev_uh, plot_prev_ed;
|
u64 plot_prev_qc, plot_prev_uc, plot_prev_uh, plot_prev_ed;
|
||||||
|
|
||||||
u64 stats_last_stats_ms, stats_last_plot_ms, stats_last_ms, stats_last_execs;
|
u64 stats_last_stats_ms, stats_last_plot_ms, stats_last_ms, stats_last_execs;
|
||||||
// StatsD
|
|
||||||
|
/* StatsD */
|
||||||
u64 statsd_last_send_ms;
|
u64 statsd_last_send_ms;
|
||||||
|
struct sockaddr_in statsd_server;
|
||||||
|
int statsd_sock;
|
||||||
|
|
||||||
double stats_avg_exec;
|
double stats_avg_exec;
|
||||||
|
|
||||||
@ -958,7 +961,7 @@ void show_init_stats(afl_state_t *);
|
|||||||
|
|
||||||
/* StatsD */
|
/* StatsD */
|
||||||
|
|
||||||
int statsd_socket_init(char *host, int port);
|
int statsd_socket_init(afl_state_t *afl);
|
||||||
int statsd_send_metric(afl_state_t *afl);
|
int statsd_send_metric(afl_state_t *afl);
|
||||||
int statsd_format_metric(afl_state_t *afl, char *buff, size_t bufflen);
|
int statsd_format_metric(afl_state_t *afl, char *buff, size_t bufflen);
|
||||||
|
|
||||||
|
@ -12,47 +12,7 @@
|
|||||||
#define MAX_TAG_LEN 200
|
#define MAX_TAG_LEN 200
|
||||||
#define METRIC_PREFIX "fuzzing"
|
#define METRIC_PREFIX "fuzzing"
|
||||||
|
|
||||||
struct sockaddr_in server;
|
int statsd_socket_init(afl_state_t *afl) {
|
||||||
int error = 0;
|
|
||||||
int statds_sock = 0;
|
|
||||||
|
|
||||||
int statsd_socket_init(char *host, int port) {
|
|
||||||
|
|
||||||
int sock;
|
|
||||||
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
|
|
||||||
|
|
||||||
FATAL("Failed to create socket");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&server, 0, sizeof(server));
|
|
||||||
server.sin_family = AF_INET;
|
|
||||||
server.sin_port = htons(port);
|
|
||||||
|
|
||||||
struct addrinfo *result;
|
|
||||||
struct addrinfo hints;
|
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(struct addrinfo));
|
|
||||||
hints.ai_family = AF_INET;
|
|
||||||
hints.ai_socktype = SOCK_DGRAM;
|
|
||||||
|
|
||||||
if ((error = getaddrinfo(host, NULL, &hints, &result))) {
|
|
||||||
|
|
||||||
FATAL("Fail to getaddrinfo");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(&(server.sin_addr), &((struct sockaddr_in *)result->ai_addr)->sin_addr,
|
|
||||||
sizeof(struct in_addr));
|
|
||||||
freeaddrinfo(result);
|
|
||||||
|
|
||||||
return sock;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int statsd_send_metric(afl_state_t *afl) {
|
|
||||||
|
|
||||||
char buff[MAX_STATSD_PACKET_SIZE] = {0};
|
|
||||||
/* Default port and host.
|
/* Default port and host.
|
||||||
Will be overwritten by AFL_STATSD_PORT and AFL_STATSD_HOST environment
|
Will be overwritten by AFL_STATSD_PORT and AFL_STATSD_HOST environment
|
||||||
variable, if they exists.
|
variable, if they exists.
|
||||||
@ -63,14 +23,49 @@ int statsd_send_metric(afl_state_t *afl) {
|
|||||||
if (afl->afl_env.afl_statsd_port) { port = atoi(afl->afl_env.afl_statsd_port); }
|
if (afl->afl_env.afl_statsd_port) { port = atoi(afl->afl_env.afl_statsd_port); }
|
||||||
if (afl->afl_env.afl_statsd_host) { host = afl->afl_env.afl_statsd_host; }
|
if (afl->afl_env.afl_statsd_host) { host = afl->afl_env.afl_statsd_host; }
|
||||||
|
|
||||||
/* statds_sock is a global variable. We set it once in the beginning and reuse
|
int sock;
|
||||||
the socket. If the sendto later fail, we reset it to 0 to be able to recreate
|
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
|
||||||
it.
|
|
||||||
*/
|
|
||||||
if (!statds_sock) {
|
|
||||||
|
|
||||||
statds_sock = statsd_socket_init(host, port);
|
FATAL("Failed to create socket");
|
||||||
if (!statds_sock) {
|
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&afl->statsd_server, 0, sizeof(afl->statsd_server));
|
||||||
|
afl->statsd_server.sin_family = AF_INET;
|
||||||
|
afl->statsd_server.sin_port = htons(port);
|
||||||
|
|
||||||
|
struct addrinfo *result;
|
||||||
|
struct addrinfo hints;
|
||||||
|
|
||||||
|
memset(&hints, 0, sizeof(struct addrinfo));
|
||||||
|
hints.ai_family = AF_INET;
|
||||||
|
hints.ai_socktype = SOCK_DGRAM;
|
||||||
|
|
||||||
|
if ((getaddrinfo(host, NULL, &hints, &result))) {
|
||||||
|
|
||||||
|
FATAL("Fail to getaddrinfo");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(&(afl->statsd_server.sin_addr), &((struct sockaddr_in *)result->ai_addr)->sin_addr,
|
||||||
|
sizeof(struct in_addr));
|
||||||
|
freeaddrinfo(result);
|
||||||
|
|
||||||
|
return sock;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int statsd_send_metric(afl_state_t *afl) {
|
||||||
|
|
||||||
|
char buff[MAX_STATSD_PACKET_SIZE] = {0};
|
||||||
|
|
||||||
|
/* afl->statsd_sock is set once in the initialisation of afl-fuzz and reused each time
|
||||||
|
If the sendto later fail, we reset it to 0 to be able to recreates it.
|
||||||
|
*/
|
||||||
|
if (!afl->statsd_sock) {
|
||||||
|
|
||||||
|
afl->statsd_sock = statsd_socket_init(afl);
|
||||||
|
if (!afl->statsd_sock) {
|
||||||
|
|
||||||
WARNF("Cannot create socket");
|
WARNF("Cannot create socket");
|
||||||
return -1;
|
return -1;
|
||||||
@ -80,11 +75,11 @@ int statsd_send_metric(afl_state_t *afl) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
statsd_format_metric(afl, buff, MAX_STATSD_PACKET_SIZE);
|
statsd_format_metric(afl, buff, MAX_STATSD_PACKET_SIZE);
|
||||||
if (sendto(statds_sock, buff, strlen(buff), 0, (struct sockaddr *)&server,
|
if (sendto(afl->statsd_sock, buff, strlen(buff), 0, (struct sockaddr *)&afl->statsd_server,
|
||||||
sizeof(server)) == -1) {
|
sizeof(afl->statsd_server)) == -1) {
|
||||||
|
|
||||||
if (!close(statds_sock)) { perror("Cannot close socket"); }
|
if (!close(afl->statsd_sock)) { perror("Cannot close socket"); }
|
||||||
statds_sock = 0;
|
afl->statsd_sock = 0;
|
||||||
WARNF("Cannot sendto");
|
WARNF("Cannot sendto");
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user