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 <sys/socket.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <time.h>
24 
25 #include "resolv_params.h"
26 
27 #define RCODE_INTERNAL_ERROR    254
28 #define RCODE_TIMEOUT           255
29 
30 /*
31  * Resolver reachability statistics and run-time parameters.
32  */
33 
34 struct __res_sample {
35     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
38 };
39 
40 struct __res_stats {
41     // Stats of the last <sample_count> queries.
42     struct __res_sample		samples[MAXNSSAMPLES];
43     // The number of samples stored.
44     uint8_t			sample_count;
45     // The next sample to modify.
46     uint8_t			sample_next;
47 };
48 
49 /* Calculate the round-trip-time from start time t0 and end time t1. */
50 extern int
51 _res_stats_calculate_rtt(const struct timespec* t1, const struct timespec* t0);
52 
53 /* Initialize a sample for calculating server reachability statistics. */
54 extern void
55 _res_stats_set_sample(struct __res_sample* sample, time_t now, int rcode, int rtt);
56 
57 /* Returns true if the server is considered unusable, i.e. if the success rate is not lower than the
58  * threshold for the stored stored samples. If not enough samples are stored, the server is
59  * considered usable.
60  */
61 extern bool
62 _res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats);
63 
64 __BEGIN_DECLS
65 /* Aggregates the reachability statistics for the given server based on on the stored samples. */
66 extern void
67 android_net_res_stats_aggregate(struct __res_stats* stats, int* successes, int* errors,
68         int* timeouts, int* internal_errors, int* rtt_avg, time_t* last_sample_time)
69     __attribute__((visibility ("default")));
70 
71 extern int
72 android_net_res_stats_get_info_for_net(unsigned netid, int* nscount,
73         struct sockaddr_storage servers[MAXNS], int* dcount, char domains[MAXDNSRCH][MAXDNSRCHPATH],
74         struct __res_params* params, struct __res_stats stats[MAXNS])
75     __attribute__((visibility ("default")));
76 
77 /* Returns an array of bools indicating which servers are considered good */
78 extern void
79 android_net_res_stats_get_usable_servers(const struct __res_params* params,
80         struct __res_stats stats[], int nscount, bool valid_servers[])
81     __attribute__((visibility ("default")));
82 __END_DECLS
83 
84 #endif  // _RES_STATS_H
85