/* 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 . */ #include #include #include #include void ping903(void); /* 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); int cf_syslog_facility(int mode, union cf_callback_arg *arg, void *data); int cf_auth(int mode, union cf_callback_arg *arg, void *data); void pinger_setup(void); int pinger_host_add(char const *name, struct sockaddr *addr, socklen_t addrlen); /* Return codes for pinger update functions */ enum { UPD_OK, /* Update successful. */ UPD_NOMEM, /* Not enough memory. */ UPD_EXISTS, /* When adding: such entry already exists. */ /* When deleting: such entry does not exist. */ UPD_NORESOLV /* Host name does not resolve */ }; int pinger_host_delete_by_name(char const *name); int pinger_host_add_name(char const *name); int pinger_hostlist_set(struct json_value *obj, char const **err_text, int *err_pos); char *get_remote_ip(struct MHD_Connection *conn); enum { HOST_STAT_INIT, /* Initial state: data are being collected */ HOST_STAT_VALID, /* The record is valid */ HOST_STAT_PENDING, /* The record is valid; new data are being collected */ HOST_STAT_INVALID /* The record is invalid (host unreachable) */ }; struct host_stat { int status; struct timeval start_tv; struct timeval stop_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->status == HOST_STAT_VALID || hs->status == HOST_STAT_PENDING; } typedef struct hostping { char *name; struct sockaddr *addr; socklen_t addrlen; pthread_mutex_t mutex; /* Current ping statistics */ struct timeval start_tv; struct timeval xmit_tv; struct timeval recv_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; struct hostping *prev, *next; } HOSTPING; extern int verbose; extern int fatal_signals[]; extern char *httpd_addr; extern int httpd_access_log; extern int httpd_log_verbose; extern char *pidfile; extern unsigned long probe_interval; extern unsigned long ping_interval; extern unsigned long ping_count; extern unsigned long ping_tolerance; extern size_t data_length; extern unsigned int httpd_backlog_size; struct json_value *config_to_json(void); /* Return codes for get_ function family. */ enum { GET_OK, /* Success. */ GET_NOMEM, /* Not enough memory. */ GET_NOENT /* Requested entry not found. */ /* Obviously, this is never returned by get_all_* functions. */ }; int get_hostname_stat(char const *name, struct json_value **retval); int get_ipaddr_stat(struct sockaddr *sa, int salen, struct json_value **retval); int get_all_hosts(struct json_value **); int get_all_host_stat(struct json_value **); struct addrinfo; int get_host_matches(struct addrinfo **aip, struct json_value **retval); int file_read_ip_list(FILE *fp, char const *fname); void p903_init(void); void *p903_sender(void *p); void *p903_receiver(void *p); void *p903_scheduler(void *p); void *p903_saver(void *p);