11 #define GUARDFRACTION_PRIVATE
32 guardfraction_line_apply(
const char *guard_id,
33 uint32_t guardfraction_percentage,
64 guardfraction_file_parse_guard_line(
const char *guard_line,
69 uint32_t guardfraction;
70 char *inputs_tmp = NULL;
81 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3);
82 if (smartlist_len(sl) < 3) {
87 inputs_tmp = smartlist_get(sl, 0);
95 inputs_tmp = smartlist_get(sl, 1);
100 tor_asprintf(err_msg,
"wrong percentage '%s'", inputs_tmp);
105 if (vote_routerstatuses) {
106 retval = guardfraction_line_apply(guard_id, guardfraction,
107 vote_routerstatuses);
129 guardfraction_file_parse_inputs_line(
const char *inputs_line,
130 int *total_consensuses,
135 char *inputs_tmp = NULL;
144 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3);
145 if (smartlist_len(sl) < 2) {
146 tor_asprintf(err_msg,
"incomplete line '%s'", inputs_line);
150 inputs_tmp = smartlist_get(sl, 0);
154 tor_asprintf(err_msg,
"unparseable consensus '%s'", inputs_tmp);
158 inputs_tmp = smartlist_get(sl, 1);
162 tor_asprintf(err_msg,
"unparseable days '%s'", inputs_tmp);
176 #define MAX_GUARDFRACTION_FILE_AGE (7*24*60*60)
179 #define GUARDFRACTION_DATE_STR "written-at"
180 #define GUARDFRACTION_INPUTS "n-inputs"
181 #define GUARDFRACTION_GUARD "guard-seen"
182 #define GUARDFRACTION_VERSION "guardfraction-file-version"
204 dirserv_read_guardfraction_file_from_str(
const char *guardfraction_file_str,
210 int current_line_n = 0;
213 int total_consensuses = 0;
217 int guards_read_n = 0;
218 int guards_applied_n = 0;
223 log_warn(
LD_CONFIG,
"Error reading from guardfraction file");
228 if (vote_routerstatuses)
229 smartlist_sort(vote_routerstatuses, compare_vote_routerstatus_entries);
231 for (line = front; line; line=line->next) {
234 if (!strcmp(line->key, GUARDFRACTION_VERSION)) {
236 unsigned int version;
240 10, 0, INT_MAX, &num_ok, NULL);
242 if (!num_ok || version != 1) {
243 log_warn(
LD_GENERAL,
"Got unknown guardfraction version %d.", version);
246 }
else if (!strcmp(line->key, GUARDFRACTION_DATE_STR)) {
247 time_t file_written_at;
248 time_t now = time(NULL);
252 log_warn(
LD_CONFIG,
"Guardfraction:%d: Bad date '%s'. Ignoring",
253 current_line_n, line->value);
256 if (file_written_at < now - MAX_GUARDFRACTION_FILE_AGE) {
257 log_warn(
LD_CONFIG,
"Guardfraction:%d: was written very long ago '%s'",
258 current_line_n, line->value);
261 }
else if (!strcmp(line->key, GUARDFRACTION_INPUTS)) {
262 char *err_msg = NULL;
264 if (guardfraction_file_parse_inputs_line(line->value,
268 log_warn(
LD_CONFIG,
"Guardfraction:%d: %s",
269 current_line_n, err_msg);
274 }
else if (!strcmp(line->key, GUARDFRACTION_GUARD)) {
275 char *err_msg = NULL;
277 ret_tmp = guardfraction_file_parse_guard_line(line->value,
281 log_warn(
LD_CONFIG,
"Guardfraction:%d: %s",
282 current_line_n, err_msg);
293 log_warn(
LD_CONFIG,
"Unknown guardfraction line %d (%s %s)",
294 current_line_n, line->key, line->value);
301 "Successfully parsed guardfraction file with %d consensuses over "
302 "%d days. Parsed %d nodes and applied %d of them%s.",
303 total_consensuses, total_days, guards_read_n, guards_applied_n,
304 vote_routerstatuses ?
"" :
" (no routerstatus provided)" );
307 config_free_lines(front);
312 return guards_read_n;
322 char *guardfraction_file_str;
326 if (!guardfraction_file_str) {
327 log_warn(
LD_FS,
"Cannot open guardfraction file '%s'. Failing.", fname);
331 return dirserv_read_guardfraction_file_from_str(guardfraction_file_str,
332 vote_routerstatuses);
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
int config_get_lines(const char *string, config_line_t **result, int extended)
#define RFTS_IGNORE_MISSING
Header file for guardfraction.c.
int dirserv_read_guardfraction_file(const char *fname, smartlist_t *vote_routerstatuses)
int compare_digest_to_vote_routerstatus_entry(const void *_key, const void **_member)
Header file for networkstatus.c.
Header file for ns_parse.c.
Master header file for Tor-specific functionality.
long tor_parse_long(const char *s, int base, long min, long max, int *ok, char **next)
int tor_asprintf(char **strp, const char *fmt,...)
void * smartlist_bsearch(const smartlist_t *sl, const void *key, int(*compare)(const void *key, const void **member))
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
smartlist_t * smartlist_new(void)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max)
unsigned int has_guardfraction
uint32_t guardfraction_percentage
int parse_iso_time(const char *cp, time_t *t)
Routerstatus (vote entry) structure.