• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <format>
26 #include <iostream>
27 #include <string>
28 
29 #include <android/multinetwork.h>
30 #include "common.h"
31 
32 
main(int argc,const char * argv[])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_flags = AI_ADDRCONFIG,  // Use the same parameter as Inet6AddressImpl.java.
41             .ai_family = args.family,
42             .ai_socktype = SOCK_DGRAM,
43     };
44     struct addrinfo *result = nullptr;
45 
46     time_t t;
47     time(&t);
48     srand((unsigned long)time);
49 
50     for (int i = 0; i < args.attempts; i++) {
51         std::string name;
52 
53         if (args.random_name) {
54             name = std::format("{}-{}-ds.metric.gstatic.com", rand(), rand());
55         } else {
56             name = args.arg1;
57         }
58 
59         std::cout << "# " << name << " (via nethandle " << args.nethandle << "):" << std::endl;
60 
61         switch (args.api_mode) {
62             case ApiMode::EXPLICIT:
63                 rval = android_getaddrinfofornetwork(args.nethandle, name.c_str(), nullptr, &hints,
64                                                      &result);
65                 break;
66             case ApiMode::PROCESS:
67                 if (args.nethandle != NETWORK_UNSPECIFIED) {
68                     rval = android_setprocnetwork(args.nethandle);
69                     if (rval != 0) {
70                         std::cerr << "android_setprocnetwork returned " << rval << std::endl;
71                         return rval;
72                     }
73                 }
74                 rval = getaddrinfo(name.c_str(), nullptr, &hints, &result);
75                 break;
76             default:
77                 // Unreachable.
78                 std::cerr << "Unknown api mode." << std::endl;
79                 return -1;
80         }
81 
82         if (rval != 0) {
83             std::cerr << "DNS resolution failure; gaierror=" << rval << " [" << gai_strerror(rval)
84                       << "]" << std::endl;
85             return rval;
86         }
87 
88         for (struct addrinfo* rp = result; rp != nullptr; rp = rp->ai_next) {
89             std::cout << inetSockaddrToString(rp->ai_addr) << std::endl;
90         }
91 
92         freeaddrinfo(result);
93     }
94 
95     return 0;
96 }
97