dnschk.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #include <arpa/inet.h>
  17. #include <errno.h>
  18. #include <netdb.h>
  19. #include <netinet/in.h>
  20. #include <string.h>
  21. #include <sys/socket.h>
  22. #include <iostream>
  23. #include <string>
  24. #include <android/multinetwork.h>
  25. #include "common.h"
  26. int main(int argc, const char* argv[]) {
  27. int rval = -1;
  28. struct Arguments args;
  29. if (!args.parseArguments(argc, argv)) { return rval; }
  30. const struct addrinfo hints = {
  31. .ai_family = args.family,
  32. .ai_socktype = SOCK_DGRAM,
  33. };
  34. struct addrinfo *result = nullptr;
  35. std::cout << "# " << args.arg1
  36. << " (via nethandle " << args.nethandle << "):"
  37. << std::endl;
  38. switch (args.api_mode) {
  39. case ApiMode::EXPLICIT:
  40. rval = android_getaddrinfofornetwork(args.nethandle,
  41. args.arg1, nullptr, &hints, &result);
  42. break;
  43. case ApiMode::PROCESS:
  44. if (args.nethandle != NETWORK_UNSPECIFIED) {
  45. rval = android_setprocnetwork(args.nethandle);
  46. if (rval != 0) {
  47. std::cerr << "android_setprocnetwork returned " << rval
  48. << std::endl;
  49. return rval;
  50. }
  51. }
  52. rval = getaddrinfo(args.arg1, nullptr, &hints, &result);
  53. break;
  54. default:
  55. // Unreachable.
  56. std::cerr << "Unknown api mode." << std::endl;
  57. return -1;
  58. }
  59. if (rval != 0) {
  60. std::cerr << "DNS resolution failure; gaierror=" << rval
  61. << " [" << gai_strerror(rval) << "]"
  62. << std::endl;
  63. return rval;
  64. }
  65. for (struct addrinfo* rp = result; rp != nullptr; rp = rp->ai_next) {
  66. std::cout << inetSockaddrToString(rp->ai_addr) << std::endl;
  67. }
  68. freeaddrinfo(result);
  69. return 0;
  70. }