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