blob: 18f884e08c62d25672a21a5c4754833a2b2d6cf5 [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/*
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
Bernie Innocenti55864192018-08-30 04:05:20 +090020#include <stdbool.h>
21#include <stdint.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090022#include <sys/socket.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090023#include <time.h>
24
25#include "resolv_params.h"
26
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090027#define RCODE_INTERNAL_ERROR 254
28#define RCODE_TIMEOUT 255
Bernie Innocenti55864192018-08-30 04:05:20 +090029
30/*
31 * Resolver reachability statistics and run-time parameters.
32 */
33
34struct __res_sample {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090035 time_t at; // time in s at which the sample was recorded
36 uint16_t rtt; // round-trip time in ms
37 uint8_t rcode; // the DNS rcode or RCODE_XXX defined above
Bernie Innocenti55864192018-08-30 04:05:20 +090038};
39
40struct __res_stats {
41 // Stats of the last <sample_count> queries.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090042 struct __res_sample samples[MAXNSSAMPLES];
Bernie Innocenti55864192018-08-30 04:05:20 +090043 // The number of samples stored.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090044 uint8_t sample_count;
Bernie Innocenti55864192018-08-30 04:05:20 +090045 // The next sample to modify.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090046 uint8_t sample_next;
Bernie Innocenti55864192018-08-30 04:05:20 +090047};
48
49/* Calculate the round-trip-time from start time t0 and end time t1. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090050extern int _res_stats_calculate_rtt(const struct timespec* t1, const struct timespec* t0);
Bernie Innocenti55864192018-08-30 04:05:20 +090051
52/* Initialize a sample for calculating server reachability statistics. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090053extern void _res_stats_set_sample(struct __res_sample* sample, time_t now, int rcode, int rtt);
Bernie Innocenti55864192018-08-30 04:05:20 +090054
55/* Returns true if the server is considered unusable, i.e. if the success rate is not lower than the
56 * threshold for the stored stored samples. If not enough samples are stored, the server is
57 * considered usable.
58 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090059extern bool _res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats);
Bernie Innocenti55864192018-08-30 04:05:20 +090060
61__BEGIN_DECLS
62/* Aggregates the reachability statistics for the given server based on on the stored samples. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090063extern void android_net_res_stats_aggregate(struct __res_stats* stats, int* successes, int* errors,
64 int* timeouts, int* internal_errors, int* rtt_avg,
65 time_t* last_sample_time)
66 __attribute__((visibility("default")));
Bernie Innocenti55864192018-08-30 04:05:20 +090067
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090068extern int android_net_res_stats_get_info_for_net(
69 unsigned netid, int* nscount, struct sockaddr_storage servers[MAXNS], int* dcount,
70 char domains[MAXDNSRCH][MAXDNSRCHPATH], struct __res_params* params,
71 struct __res_stats stats[MAXNS]) __attribute__((visibility("default")));
Bernie Innocenti55864192018-08-30 04:05:20 +090072
73/* Returns an array of bools indicating which servers are considered good */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090074extern void android_net_res_stats_get_usable_servers(const struct __res_params* params,
75 struct __res_stats stats[], int nscount,
76 bool valid_servers[])
77 __attribute__((visibility("default")));
Bernie Innocenti55864192018-08-30 04:05:20 +090078__END_DECLS
79
80#endif // _RES_STATS_H