TestDNS.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "TestCommon.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "nsIServiceManager.h"
  8. #include "nsPIDNSService.h"
  9. #include "nsIDNSListener.h"
  10. #include "nsIDNSRecord.h"
  11. #include "nsICancelable.h"
  12. #include "nsCOMPtr.h"
  13. #include "nsStringAPI.h"
  14. #include "nsNetCID.h"
  15. #include "prinrval.h"
  16. #include "prthread.h"
  17. #include "prnetdb.h"
  18. #include "nsXPCOM.h"
  19. #include "nsServiceManagerUtils.h"
  20. class myDNSListener : public nsIDNSListener
  21. {
  22. public:
  23. NS_DECL_THREADSAFE_ISUPPORTS
  24. myDNSListener(const char *host, int32_t index)
  25. : mHost(host)
  26. , mIndex(index) {}
  27. NS_IMETHOD OnLookupComplete(nsICancelable *request,
  28. nsIDNSRecord *rec,
  29. nsresult status) override
  30. {
  31. printf("%d: OnLookupComplete called [host=%s status=%x rec=%p]\n",
  32. mIndex, mHost.get(), static_cast<uint32_t>(status), (void*)rec);
  33. if (NS_SUCCEEDED(status)) {
  34. nsAutoCString buf;
  35. rec->GetCanonicalName(buf);
  36. printf("%d: canonname=%s\n", mIndex, buf.get());
  37. bool hasMore;
  38. while (NS_SUCCEEDED(rec->HasMore(&hasMore)) && hasMore) {
  39. rec->GetNextAddrAsString(buf);
  40. printf("%d: => %s\n", mIndex, buf.get());
  41. }
  42. }
  43. return NS_OK;
  44. }
  45. private:
  46. virtual ~myDNSListener() = default;
  47. nsCString mHost;
  48. int32_t mIndex;
  49. };
  50. NS_IMPL_ISUPPORTS(myDNSListener, nsIDNSListener)
  51. static bool IsAscii(const char *s)
  52. {
  53. for (; *s; ++s) {
  54. if (*s & 0x80)
  55. return false;
  56. }
  57. return true;
  58. }
  59. int main(int argc, char **argv)
  60. {
  61. if (test_common_init(&argc, &argv) != 0)
  62. return -1;
  63. int sleepLen = 10; // default: 10 seconds
  64. if (argc == 1) {
  65. printf("usage: TestDNS [-N] hostname1 [hostname2 ...]\n");
  66. return -1;
  67. }
  68. {
  69. nsCOMPtr<nsIServiceManager> servMan;
  70. NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
  71. nsCOMPtr<nsPIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID);
  72. if (!dns)
  73. return -1;
  74. if (argv[1][0] == '-') {
  75. sleepLen = atoi(argv[1]+1);
  76. argv++;
  77. argc--;
  78. }
  79. for (int j=0; j<2; ++j) {
  80. for (int i=1; i<argc; ++i) {
  81. // assume non-ASCII input is given in the native charset
  82. nsAutoCString hostBuf;
  83. if (IsAscii(argv[i]))
  84. hostBuf.Assign(argv[i]);
  85. else
  86. hostBuf = NS_ConvertUTF16toUTF8(NS_ConvertASCIItoUTF16(argv[i]));
  87. nsCOMPtr<nsIDNSListener> listener = new myDNSListener(argv[i], i);
  88. nsCOMPtr<nsICancelable> req;
  89. nsresult rv = dns->AsyncResolve(hostBuf,
  90. nsIDNSService::RESOLVE_CANONICAL_NAME,
  91. listener, nullptr, getter_AddRefs(req));
  92. if (NS_FAILED(rv))
  93. printf("### AsyncResolve failed [rv=%x]\n",
  94. static_cast<uint32_t>(rv));
  95. }
  96. printf("main thread sleeping for %d seconds...\n", sleepLen);
  97. PR_Sleep(PR_SecondsToInterval(sleepLen));
  98. }
  99. printf("shutting down main thread...\n");
  100. dns->Shutdown();
  101. }
  102. NS_ShutdownXPCOM(nullptr);
  103. return 0;
  104. }