Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 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 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 20 | #include <stdbool.h> |
| 21 | #include <stdint.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 22 | #include <sys/socket.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 23 | #include <time.h> |
| 24 | |
| 25 | #include "resolv_params.h" |
| 26 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 27 | #define RCODE_INTERNAL_ERROR 254 |
| 28 | #define RCODE_TIMEOUT 255 |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * Resolver reachability statistics and run-time parameters. |
| 32 | */ |
| 33 | |
| 34 | struct __res_sample { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 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 |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | struct __res_stats { |
| 41 | // Stats of the last <sample_count> queries. |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 42 | struct __res_sample samples[MAXNSSAMPLES]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 43 | // The number of samples stored. |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 44 | uint8_t sample_count; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 45 | // The next sample to modify. |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 46 | uint8_t sample_next; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /* Calculate the round-trip-time from start time t0 and end time t1. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 50 | extern int _res_stats_calculate_rtt(const struct timespec* t1, const struct timespec* t0); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 51 | |
| 52 | /* Initialize a sample for calculating server reachability statistics. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 53 | extern void _res_stats_set_sample(struct __res_sample* sample, time_t now, int rcode, int rtt); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 54 | |
| 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 Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 59 | extern bool _res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 60 | |
| 61 | __BEGIN_DECLS |
| 62 | /* Aggregates the reachability statistics for the given server based on on the stored samples. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 63 | extern 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 67 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 68 | extern 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 72 | |
| 73 | /* Returns an array of bools indicating which servers are considered good */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 74 | extern 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 78 | __END_DECLS |
| 79 | |
| 80 | #endif // _RES_STATS_H |