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 #pragma once
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <sys/socket.h>
22 #include <time.h>
23 
24 #include "params.h"
25 
26 #define RCODE_INTERNAL_ERROR 254
27 #define RCODE_TIMEOUT 255
28 
29 struct res_sample {
30     time_t at;      // time in s at which the sample was recorded
31     uint16_t rtt;   // round-trip time in ms
32     uint8_t rcode;  // the DNS rcode or RCODE_XXX defined above
33 };
34 
35 // Resolver reachability statistics and run-time parameters.
36 struct res_stats {
37     // Stats of the last <sample_count> queries.
38     res_sample samples[MAXNSSAMPLES];
39     // The number of samples stored.
40     uint8_t sample_count;
41     // The next sample to modify.
42     uint8_t sample_next;
43 };
44 
45 // Aggregates the reachability statistics for the given server based on on the stored samples.
46 void android_net_res_stats_aggregate(res_stats* stats, int* successes, int* errors, int* timeouts,
47                                      int* internal_errors, int* rtt_avg, time_t* last_sample_time);
48 
49 int android_net_res_stats_get_info_for_net(unsigned netid, int* nscount,
50                                            sockaddr_storage servers[MAXNS], int* dcount,
51                                            char domains[MAXDNSRCH][MAXDNSRCHPATH],
52                                            res_params* params, res_stats stats[MAXNS],
53                                            int* wait_for_pending_req_timeout_count);
54 
55 // Returns an array of bools indicating which servers are considered good
56 int android_net_res_stats_get_usable_servers(const res_params* params, res_stats stats[],
57                                              int nscount, bool valid_servers[]);
58 
59 // Calculate the round-trip-time from start time t0 and end time t1.
60 int res_stats_calculate_rtt(const timespec* t1, const timespec* t0);
61 
62 // Create a sample for calculating server reachability statistics.
63 void res_stats_set_sample(res_sample* sample, time_t now, int rcode, int rtt);
64