OSDN Git Service

merge in nyc-mr1-release history after reset to nyc-mr1-dev
[android-x86/system-extras.git] / multinetwork / dnschk.cpp
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
28 #include <android/multinetwork.h>
29 #include "common.h"
30
31
32 int main(int argc, const char* argv[]) {
33     int rval = -1;
34
35     struct Arguments args;
36     if (!args.parseArguments(argc, argv)) { return rval; }
37
38     const struct addrinfo hints = {
39             .ai_family = args.family,
40             .ai_socktype = SOCK_DGRAM,
41     };
42     struct addrinfo *result = nullptr;
43
44     std::cout << "# " << args.arg1
45               << " (via nethandle " << args.nethandle << "):"
46               << std::endl;
47
48     switch (args.api_mode) {
49         case ApiMode::EXPLICIT:
50             rval = android_getaddrinfofornetwork(args.nethandle,
51                     args.arg1, nullptr, &hints, &result);
52             break;
53         case ApiMode::PROCESS:
54             if (args.nethandle != NETWORK_UNSPECIFIED) {
55                 rval = android_setprocnetwork(args.nethandle);
56                 if (rval != 0) {
57                     std::cerr << "android_setprocnetwork returned " << rval
58                               << std::endl;
59                     return rval;
60                 }
61             }
62             rval = getaddrinfo(args.arg1, nullptr, &hints, &result);
63             break;
64         default:
65             // Unreachable.
66             std::cerr << "Unknown api mode." << std::endl;
67             return -1;
68     }
69
70     if (rval != 0) {
71         std::cerr << "DNS resolution failure; gaierror=" << rval
72                   << " [" << gai_strerror(rval) << "]"
73                   << std::endl;
74         return rval;
75     }
76
77     for (struct addrinfo* rp = result; rp != nullptr; rp = rp->ai_next) {
78         std::cout << inetSockaddrToString(rp->ai_addr) << std::endl;
79     }
80
81     freeaddrinfo(result);
82     return 0;
83 }