michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TestCommon.h" michael@0: #include michael@0: #include michael@0: #include "nsIServiceManager.h" michael@0: #include "nsPIDNSService.h" michael@0: #include "nsIDNSListener.h" michael@0: #include "nsIDNSRecord.h" michael@0: #include "nsICancelable.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsStringAPI.h" michael@0: #include "nsNetCID.h" michael@0: #include "prinrval.h" michael@0: #include "prthread.h" michael@0: #include "prnetdb.h" michael@0: #include "nsXPCOM.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: michael@0: class myDNSListener : public nsIDNSListener michael@0: { michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: michael@0: myDNSListener(const char *host, int32_t index) michael@0: : mHost(host) michael@0: , mIndex(index) {} michael@0: virtual ~myDNSListener() {} michael@0: michael@0: NS_IMETHOD OnLookupComplete(nsICancelable *request, michael@0: nsIDNSRecord *rec, michael@0: nsresult status) michael@0: { michael@0: printf("%d: OnLookupComplete called [host=%s status=%x rec=%p]\n", michael@0: mIndex, mHost.get(), static_cast(status), (void*)rec); michael@0: michael@0: if (NS_SUCCEEDED(status)) { michael@0: nsAutoCString buf; michael@0: michael@0: rec->GetCanonicalName(buf); michael@0: printf("%d: canonname=%s\n", mIndex, buf.get()); michael@0: michael@0: bool hasMore; michael@0: while (NS_SUCCEEDED(rec->HasMore(&hasMore)) && hasMore) { michael@0: rec->GetNextAddrAsString(buf); michael@0: printf("%d: => %s\n", mIndex, buf.get()); michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: private: michael@0: nsCString mHost; michael@0: int32_t mIndex; michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(myDNSListener, nsIDNSListener) michael@0: michael@0: static bool IsAscii(const char *s) michael@0: { michael@0: for (; *s; ++s) { michael@0: if (*s & 0x80) michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: if (test_common_init(&argc, &argv) != 0) michael@0: return -1; michael@0: michael@0: int sleepLen = 10; // default: 10 seconds michael@0: michael@0: if (argc == 1) { michael@0: printf("usage: TestDNS [-N] hostname1 [hostname2 ...]\n"); michael@0: return -1; michael@0: } michael@0: michael@0: { michael@0: nsCOMPtr servMan; michael@0: NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); michael@0: michael@0: nsCOMPtr dns = do_GetService(NS_DNSSERVICE_CONTRACTID); michael@0: if (!dns) michael@0: return -1; michael@0: michael@0: if (argv[1][0] == '-') { michael@0: sleepLen = atoi(argv[1]+1); michael@0: argv++; michael@0: argc--; michael@0: } michael@0: michael@0: for (int j=0; j<2; ++j) { michael@0: for (int i=1; i listener = new myDNSListener(argv[i], i); michael@0: michael@0: nsCOMPtr req; michael@0: nsresult rv = dns->AsyncResolve(hostBuf, michael@0: nsIDNSService::RESOLVE_CANONICAL_NAME, michael@0: listener, nullptr, getter_AddRefs(req)); michael@0: if (NS_FAILED(rv)) michael@0: printf("### AsyncResolve failed [rv=%x]\n", michael@0: static_cast(rv)); michael@0: } michael@0: michael@0: printf("main thread sleeping for %d seconds...\n", sleepLen); michael@0: PR_Sleep(PR_SecondsToInterval(sleepLen)); michael@0: } michael@0: michael@0: printf("shutting down main thread...\n"); michael@0: dns->Shutdown(); michael@0: } michael@0: michael@0: NS_ShutdownXPCOM(nullptr); michael@0: return 0; michael@0: }