diff options
-rw-r--r-- | src/ping903.h | 2 | ||||
-rw-r--r-- | src/pinger.c | 83 |
2 files changed, 48 insertions, 37 deletions
diff --git a/src/ping903.h b/src/ping903.h index 07a7f9f..d7ad922 100644 --- a/src/ping903.h +++ b/src/ping903.h @@ -106,6 +106,8 @@ typedef struct hostping { /* Last probe statistics */ struct host_stat stat_last; + + struct hostping *prev, *next; } HOSTPING; extern int verbose; diff --git a/src/pinger.c b/src/pinger.c index c131d5a..070cbe6 100644 --- a/src/pinger.c +++ b/src/pinger.c @@ -97,27 +97,34 @@ timespec_incr(struct timespec *a, struct timespec const *b) timespec_add(&t, b, a); } -static HOSTPING *hostping; +static HOSTPING *hostping_head, *hostping_tail; static size_t hostping_count; -static size_t hostping_max; void hostping_add(char const *name, struct sockaddr *addr, socklen_t addrlen) { - if (hostping_count == hostping_max) { - hostping = e2nrealloc(hostping, - &hostping_max, - sizeof(hostping[0])); - } - memset(&hostping[hostping_count], 0, sizeof(hostping[0])); - hostping[hostping_count].name = estrdup(name); - hostping[hostping_count].addr = emalloc(addrlen); - memcpy(hostping[hostping_count].addr, addr, addrlen); - hostping[hostping_count].addrlen =addrlen; - pthread_mutex_init(&hostping[hostping_count].mutex, NULL); + HOSTPING *hp = emalloc(sizeof(*hp)); + + memset(hp, 0, sizeof(*hp)); + hp->name = estrdup(name); + hp->addr = emalloc(addrlen); + memcpy(hp->addr, addr, addrlen); + hp->addrlen =addrlen; + pthread_mutex_init(&hp->mutex, NULL); + + hp->next = NULL; + hp->prev = hostping_tail; + if (hostping_tail) + hostping_tail->next = hp; + else + hostping_head = hp; + hostping_tail = hp; + hostping_count++; } +#define FOR_EACH_HOSTPING(hp) for (hp = hostping_head; hp; hp = hp->next) + static inline void hostping_lock(HOSTPING *host) { @@ -307,11 +314,11 @@ get_hostping_stat(HOSTPING *host, struct json_value **retval) int get_hostname_stat(char const *name, struct json_value **retval) { - size_t i; + HOSTPING *hp; - for (i = 0; i < hostping_count; i++) { - if (strcmp(hostping[i].name, name) == 0) - return get_hostping_stat(&hostping[i], retval); + FOR_EACH_HOSTPING(hp) { + if (strcmp(hp->name, name) == 0) + return get_hostping_stat(hp, retval); } *retval = NULL; return 0; @@ -320,12 +327,12 @@ get_hostname_stat(char const *name, struct json_value **retval) int get_ipaddr_stat(struct sockaddr *sa, int salen, struct json_value **retval) { - size_t i; + HOSTPING *hp; - for (i = 0; i < hostping_count; i++) { - if (hostping[i].addrlen == salen - && memcmp(hostping[i].addr, sa, salen) == 0) - return get_hostping_stat(&hostping[i], retval); + FOR_EACH_HOSTPING(hp) { + if (hp->addrlen == salen + && memcmp(hp->addr, sa, salen) == 0) + return get_hostping_stat(hp, retval); } *retval = NULL; return 0; @@ -335,13 +342,13 @@ int get_all_host_stat(struct json_value **retval) { struct json_value *ar; - size_t i; + HOSTPING *hp; if (!(ar = json_new_array())) goto err; - for (i = 0; i < hostping_count; i++) { + FOR_EACH_HOSTPING(hp) { struct json_value *v; - if (get_hostping_stat(&hostping[i], &v)) + if (get_hostping_stat(hp, &v)) goto err; if (json_array_append(ar, v)) { json_value_free(v); @@ -359,26 +366,26 @@ int get_host_matches(struct addrinfo **aip, struct json_value **retval) { struct json_value *ar; - size_t i; + HOSTPING *hp; struct addrinfo *ai_head = NULL, *ai_tail = NULL; struct addrinfo *ai = *aip; int ret = -1; if (!(ar = json_new_array())) goto err; - for (i = 0; i < hostping_count; i++) { + FOR_EACH_HOSTPING(hp) { struct addrinfo *prev = NULL, *p; if (!ai) break; p = ai; while (p) { struct addrinfo *next = p->ai_next; - if (p->ai_addrlen == hostping[i].addrlen - && memcmp(hostping[i].addr, p->ai_addr, - hostping[i].addrlen) == 0) { + if (p->ai_addrlen == hp->addrlen + && memcmp(hp->addr, p->ai_addr, + hp->addrlen) == 0) { struct json_value *jv; - jv = json_new_string(hostping[i].name); + jv = json_new_string(hp->name); if (!jv) goto err; if (json_array_append(ar, jv)) { @@ -448,7 +455,7 @@ struct seqidx { }; static struct seqidx *seqidx; -unsigned short next_seqno; +static unsigned short next_seqno; static int seqno_alloc(HOSTPING *addr, struct timeval *tv) @@ -668,6 +675,7 @@ p903_sender(void *p) pthread_mutex_lock(&sendq_mutex); while (1) { struct timespec ts; + HOSTPING *hp; if (!send_p) { while (!send_p) @@ -675,13 +683,13 @@ p903_sender(void *p) send_count = 0; } clock_gettime(CLOCK_MONOTONIC, &ts); - for (i = 0; i < hostping_count; i++) { + FOR_EACH_HOSTPING(hp) { struct timespec nts; clock_gettime(CLOCK_MONOTONIC, &nts); timespec_incr(&nts, &delay); n = poll(&pfd, 1, -1); if (n == 1) { - send_echo(&hostping[i], ping_buffer); + send_echo(hp, ping_buffer); } else if (n == -1) { fatal("poll: %s", strerror(errno)); exit(1); @@ -818,11 +826,12 @@ void * p903_scheduler(void *p) { while (1) { - size_t i; + HOSTPING *hp; /* Reset all statistics */ - for (i = 0; i < hostping_count; i++) - hostping_reset(hostping + i); + FOR_EACH_HOSTPING(hp) { + hostping_reset(hp); + } start_probe(); sleep(probe_interval); if (verbose) |