/
pingerd_conf.c
136 lines (121 loc) · 3.09 KB
/
pingerd_conf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <libconfig.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "log.h"
#include "pingerd_conf.h"
#define MAXPACKET 65536 - 60 - 8 /* @see pingerd.c */
struct pingerd_conf_s conf;
void init_config() {
conf.config_file = "/etc/pingerd.conf";
conf.num_connections = 100;
conf.report_freq = 900;
conf.packet_size = 64;
conf.packets_count = 4;
conf.reports_dir = strdup("/var/log/pingerd/");
conf.hosts_file = strdup("/etc/pingerd/ip.txt");
conf.log_file = NULL;
conf.hosts = NULL;
conf.hosts_num = 0;
}
void free_config_hosts() {
if (conf.hosts) {
int i;
for (i = 0; i < conf.hosts_num; ++i) {
free(conf.hosts[i]);
}
free(conf.hosts);
conf.hosts = NULL;
}
conf.hosts_num = 0;
}
void free_config() {
free_config_hosts();
free(conf.reports_dir);
free(conf.hosts_file);
free(conf.log_file);
}
/**
* Read hosts database file
*/
int read_hosts() {
FILE* fp = fopen(conf.hosts_file, "r");
if (fp == NULL) {
log_error("%s: %s", conf.hosts_file, strerror(errno));
return -1;
}
free_config_hosts();
char buf[250];
char* tok;
while (fgets(buf, sizeof(buf), fp) != NULL) {
tok = strtok(buf, "\r\n");
if (strlen (tok) > 0) {
conf.hosts = (char **) realloc (conf.hosts, sizeof(char *) * (conf.hosts_num+1));
if (!conf.hosts) {
log_fatal("can't allocate memory");
}
conf.hosts[conf.hosts_num] = strdup(tok);
++conf.hosts_num;
}
}
fclose(fp);
return 0;
}
int reload_config() {
log_info("Reloading hosts from: %s", conf.hosts_file);
return read_hosts();
}
/**
* Re-reads configuration from the given file
*/
int read_config() {
config_t cfg;
config_init(&cfg);
if (config_read_file(&cfg, conf.config_file) == CONFIG_FALSE) {
if (config_error_type(&cfg) == CONFIG_ERR_FILE_IO) {
log_error("Configuration file not found: %s", conf.config_file);
config_destroy(&cfg);
return -1;
}
log_error("%s:%d - %s", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
return -1;
}
int intVal;
const char* charVal;
if (config_lookup_int(&cfg, "num_connections", &intVal) == CONFIG_TRUE) {
conf.num_connections = intVal;
}
if (config_lookup_int(&cfg, "report_freq", &intVal) == CONFIG_TRUE) {
conf.report_freq = intVal;
}
if (config_lookup_int(&cfg, "packet_size", &intVal) == CONFIG_TRUE) {
if (intVal > MAXPACKET) {
log_error("packet_size: %d exceeds maximal packet size (%d)", intVal, MAXPACKET);
config_destroy(&cfg);
return -1;
}
conf.packet_size = intVal;
}
if (config_lookup_int(&cfg, "packets_count", &intVal) == CONFIG_TRUE) {
conf.packets_count = intVal;
}
if (config_lookup_string(&cfg, "reports_dir", &charVal) == CONFIG_TRUE) {
free(conf.reports_dir);
conf.reports_dir = strdup(charVal);
}
if (config_lookup_string(&cfg, "hosts_file", &charVal) == CONFIG_TRUE) {
free(conf.hosts_file);
conf.hosts_file = strdup(charVal);
}
if (config_lookup_string(&cfg, "log_file", &charVal) == CONFIG_TRUE) {
free(conf.log_file);
conf.log_file = strdup(charVal);
}
config_destroy(&cfg);
if (read_hosts() == -1) {
return -1;
}
return 0;
}