diff options
Diffstat (limited to 'src/ping903.h')
-rw-r--r-- | src/ping903.h | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/src/ping903.h b/src/ping903.h new file mode 100644 index 0000000..e93bf0a --- /dev/null +++ b/src/ping903.h @@ -0,0 +1,131 @@ +/* This file is part of Ping903 + Copyright (C) 2020 Sergey Poznyakoff + + Ping903 is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + Ping903 is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Ping903. If not, see <http://www.gnu.org/licenses/>. +*/ +#include <pthread.h> +#include <stdarg.h> +#include <microhttpd.h> + +void ping903(void); + +void vlogger(int prio, char const *fmt, va_list ap); +void fatal(char const *fmt, ...); +void error(char const *fmt, ...); +void info(char const *fmt, ...); +void syslog_enable(void); +void set_progname(char const *arg); +int set_log_facility(char const *arg); + +/* Configuration file parser */ + +/* Callback mode */ +enum { + CF_PARSE, /* parse input statement */ + CF_SERIALIZE /* serialize configuration setting */ +}; + +union cf_callback_arg { + struct json_value *output; /* Output value */ + struct cf_line { + char const *val; /* Statement value (after keyword) */ + char const *file; /* Statement location: file name */ + unsigned line; /* ... and input line number */ + } input; +}; + +enum { + CF_RET_OK, + CF_RET_FAIL, + CF_RET_IGNORE +}; + +/* Configuration parser callback function */ +typedef int (*CF_CALLBACK)(int mode, union cf_callback_arg *arg, void *data); + +/* Built-in statement types */ +enum { + STMT_T_STRING, + STMT_T_ULONG, + STMT_T_BOOL, + STMT_T_CALLBACK +}; + +int readconfig(char const *file); +int cf_trusted_ip(int mode, union cf_callback_arg *arg, void *data); + +void emalloc_die(void); +void *emalloc(size_t s); +char *estrdup(char const *s); +void *e2nrealloc(void *p, size_t *pn, size_t s); + +char *get_remote_ip(struct MHD_Connection *conn); + +struct host_stat { + struct timeval tv; + unsigned long xmit_count; + unsigned long recv_count; + double tmin; /* minimum round trip time */ + double tmax; /* maximum round trip time */ + double avg; + double stddev; +}; + +static inline int host_stat_is_valid(struct host_stat const *hs) { + return hs->tv.tv_sec > 0; +} + +typedef struct hostaddr { + char *name; + struct sockaddr *addr; + socklen_t addrlen; + + /* Send queue */ + struct hostaddr *next; + + /* Current ping statistics */ + struct timeval tv; + unsigned long xmit_count; + unsigned long recv_count; + double tmin; /* minimum round trip time */ + double tmax; /* maximum round trip time */ + double tsum; /* sum of all times, for doing average */ + double tsumsq; /* sum of all times squared, for std. dev. */ + + /* Last probe statistics */ + struct host_stat stat_last; +} HOSTADDR; + +extern char *progname; +extern int fatal_signals[]; +extern char *httpd_addr; +extern char *pidfile; +extern unsigned long ping_interval; +extern unsigned long ping_count; +extern unsigned long ping_tolerance; +extern size_t data_length; +extern HOSTADDR *hostaddr; +extern size_t hostaddr_count; +extern size_t hostaddr_max; + +struct json_value *config_to_json(void); +int get_host_stat(HOSTADDR *host, struct json_value **); +int get_hostname_stat(char const *name, struct json_value **retval); +int get_all_host_stat(struct json_value **); + +void p903_init(void); +void *p903_sender(void *p); +void *p903_receiver(void *p); +void *p903_scheduler(void *p); + |