resolv_stats.h revision 4d8e740b0868b2670599b949ff07d74e5f317114
1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef _RES_STATS_H 18#define _RES_STATS_H 19 20#include <stdbool.h> 21#include <stdint.h> 22#include <time.h> 23 24#include "resolv_params.h" 25 26#define RCODE_INTERNAL_ERROR 254 27#define RCODE_TIMEOUT 255 28 29/* 30 * Resolver reachability statistics and run-time parameters. 31 */ 32 33struct __res_sample { 34 time_t at; // time in s at which the sample was recorded 35 uint16_t rtt; // round-trip time in ms 36 uint8_t rcode; // the DNS rcode or RCODE_XXX defined above 37}; 38 39struct __res_stats { 40 // Stats of the last <sample_count> queries. 41 struct __res_sample samples[MAXNSSAMPLES]; 42 // The number of samples stored. 43 uint8_t sample_count; 44 // The next sample to modify. 45 uint8_t sample_next; 46}; 47 48/* Calculate the round-trip-time from start time t0 and end time t1. */ 49int 50_res_stats_calculate_rtt(const struct timespec* t1, const struct timespec* t0); 51 52/* Initialize a sample for calculating server reachability statistics. */ 53extern void 54_res_stats_set_sample(struct __res_sample* sample, time_t now, int rcode, int rtt); 55 56/* Aggregates the reachability statistics for the given server based on on the stored samples. */ 57extern void 58_res_stats_aggregate(struct __res_stats* stats, int* successes, int* errors, int* timeouts, 59 int* internal_errors, int* rtt_avg, time_t* last_sample_time); 60 61/* Returns true if the server is considered unusable, i.e. if the success rate is not lower than the 62 * threshold for the stored stored samples. If not enough samples are stored, the server is 63 * considered usable. 64 */ 65extern bool 66_res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats); 67 68/* Returns an array of bools indicating which servers are considered good */ 69extern void 70_res_stats_get_usable_servers(const struct __res_params* params, struct __res_stats stats[MAXNS], 71 int nscount, bool valid_servers[MAXNS]); 72 73#endif // _RES_STATS_H 74