Erik Kline | 871e63d | 2016-01-22 09:07:44 +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 | |
| 18 | #include <arpa/inet.h> |
| 19 | #include <errno.h> |
| 20 | #include <netdb.h> |
| 21 | #include <netinet/in.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/socket.h> |
| 24 | |
| 25 | #include <iostream> |
| 26 | #include <string> |
| 27 | |
Lorenzo Colitti | fa48d72 | 2020-11-05 19:44:46 +0900 | [diff] [blame] | 28 | #include <android-base/format.h> |
Erik Kline | 871e63d | 2016-01-22 09:07:44 +0900 | [diff] [blame] | 29 | #include <android/multinetwork.h> |
| 30 | #include "common.h" |
| 31 | |
| 32 | |
| 33 | int main(int argc, const char* argv[]) { |
| 34 | int rval = -1; |
| 35 | |
| 36 | struct Arguments args; |
| 37 | if (!args.parseArguments(argc, argv)) { return rval; } |
| 38 | |
| 39 | const struct addrinfo hints = { |
| 40 | .ai_family = args.family, |
| 41 | .ai_socktype = SOCK_DGRAM, |
| 42 | }; |
| 43 | struct addrinfo *result = nullptr; |
| 44 | |
Lorenzo Colitti | fa48d72 | 2020-11-05 19:44:46 +0900 | [diff] [blame] | 45 | time_t t; |
| 46 | time(&t); |
| 47 | srand((unsigned long)time); |
Erik Kline | 871e63d | 2016-01-22 09:07:44 +0900 | [diff] [blame] | 48 | |
Lorenzo Colitti | fa48d72 | 2020-11-05 19:44:46 +0900 | [diff] [blame] | 49 | for (int i = 0; i < args.attempts; i++) { |
| 50 | std::string name; |
| 51 | |
| 52 | if (args.random_name) { |
| 53 | name = fmt::format("{}-{}-ds.metric.gstatic.com", rand(), rand()); |
| 54 | } else { |
| 55 | name = args.arg1; |
| 56 | } |
| 57 | |
| 58 | std::cout << "# " << name << " (via nethandle " << args.nethandle << "):" << std::endl; |
| 59 | |
| 60 | switch (args.api_mode) { |
| 61 | case ApiMode::EXPLICIT: |
| 62 | rval = android_getaddrinfofornetwork(args.nethandle, name.c_str(), nullptr, &hints, |
| 63 | &result); |
| 64 | break; |
| 65 | case ApiMode::PROCESS: |
| 66 | if (args.nethandle != NETWORK_UNSPECIFIED) { |
| 67 | rval = android_setprocnetwork(args.nethandle); |
| 68 | if (rval != 0) { |
| 69 | std::cerr << "android_setprocnetwork returned " << rval << std::endl; |
| 70 | return rval; |
| 71 | } |
Erik Kline | 871e63d | 2016-01-22 09:07:44 +0900 | [diff] [blame] | 72 | } |
Lorenzo Colitti | fa48d72 | 2020-11-05 19:44:46 +0900 | [diff] [blame] | 73 | rval = getaddrinfo(name.c_str(), nullptr, &hints, &result); |
| 74 | break; |
| 75 | default: |
| 76 | // Unreachable. |
| 77 | std::cerr << "Unknown api mode." << std::endl; |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | if (rval != 0) { |
| 82 | std::cerr << "DNS resolution failure; gaierror=" << rval << " [" << gai_strerror(rval) |
| 83 | << "]" << std::endl; |
| 84 | return rval; |
| 85 | } |
| 86 | |
| 87 | for (struct addrinfo* rp = result; rp != nullptr; rp = rp->ai_next) { |
| 88 | std::cout << inetSockaddrToString(rp->ai_addr) << std::endl; |
| 89 | } |
| 90 | |
| 91 | freeaddrinfo(result); |
Erik Kline | 871e63d | 2016-01-22 09:07:44 +0900 | [diff] [blame] | 92 | } |
| 93 | |
Erik Kline | 871e63d | 2016-01-22 09:07:44 +0900 | [diff] [blame] | 94 | return 0; |
| 95 | } |