2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <arpa/inet.h>
21 #include <netinet/in.h>
23 #include <sys/socket.h>
28 #include <android/multinetwork.h>
32 int main(int argc, const char* argv[]) {
35 struct Arguments args;
36 if (!args.parseArguments(argc, argv)) { return rval; }
38 const struct addrinfo hints = {
39 .ai_family = args.family,
40 .ai_socktype = SOCK_DGRAM,
42 struct addrinfo *result = nullptr;
44 std::cout << "# " << args.arg1
45 << " (via nethandle " << args.nethandle << "):"
48 switch (args.api_mode) {
49 case ApiMode::EXPLICIT:
50 rval = android_getaddrinfofornetwork(args.nethandle,
51 args.arg1, nullptr, &hints, &result);
53 case ApiMode::PROCESS:
54 if (args.nethandle != NETWORK_UNSPECIFIED) {
55 rval = android_setprocnetwork(args.nethandle);
57 std::cerr << "android_setprocnetwork returned " << rval
62 rval = getaddrinfo(args.arg1, nullptr, &hints, &result);
66 std::cerr << "Unknown api mode." << std::endl;
71 std::cerr << "DNS resolution failure; gaierror=" << rval
72 << " [" << gai_strerror(rval) << "]"
77 for (struct addrinfo* rp = result; rp != nullptr; rp = rp->ai_next) {
78 std::cout << inetSockaddrToString(rp->ai_addr) << std::endl;