2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "TestCommon.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsPIDNSService.h"
|
|
|
|
#include "nsIDNSListener.h"
|
|
|
|
#include "nsIDNSRecord.h"
|
|
|
|
#include "nsICancelable.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsStringAPI.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "prinrval.h"
|
|
|
|
#include "prthread.h"
|
|
|
|
#include "prnetdb.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
|
|
|
|
class myDNSListener : public nsIDNSListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
myDNSListener(const char *host, int32_t index)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mHost(host)
|
|
|
|
, mIndex(index) {}
|
|
|
|
virtual ~myDNSListener() {}
|
|
|
|
|
|
|
|
NS_IMETHOD OnLookupComplete(nsICancelable *request,
|
|
|
|
nsIDNSRecord *rec,
|
|
|
|
nsresult status)
|
|
|
|
{
|
|
|
|
printf("%d: OnLookupComplete called [host=%s status=%x rec=%p]\n",
|
2012-10-15 20:52:17 -07:00
|
|
|
mIndex, mHost.get(), status, (void*)rec);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(status)) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString buf;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rec->GetCanonicalName(buf);
|
|
|
|
printf("%d: canonname=%s\n", mIndex, buf.get());
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasMore;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (NS_SUCCEEDED(rec->HasMore(&hasMore)) && hasMore) {
|
|
|
|
rec->GetNextAddrAsString(buf);
|
|
|
|
printf("%d: => %s\n", mIndex, buf.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCString mHost;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(myDNSListener, nsIDNSListener)
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool IsAscii(const char *s)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
for (; *s; ++s) {
|
|
|
|
if (*s & 0x80)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (test_common_init(&argc, &argv) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int sleepLen = 10; // default: 10 seconds
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
printf("usage: TestDNS [-N] hostname1 [hostname2 ...]\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIServiceManager> servMan;
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsPIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID);
|
|
|
|
if (!dns)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (argv[1][0] == '-') {
|
|
|
|
sleepLen = atoi(argv[1]+1);
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j=0; j<2; ++j) {
|
|
|
|
for (int i=1; i<argc; ++i) {
|
|
|
|
// assume non-ASCII input is given in the native charset
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString hostBuf;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (IsAscii(argv[i]))
|
|
|
|
hostBuf.Assign(argv[i]);
|
|
|
|
else
|
|
|
|
hostBuf = NS_ConvertUTF16toUTF8(NS_ConvertASCIItoUTF16(argv[i]));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDNSListener> listener = new myDNSListener(argv[i], i);
|
|
|
|
|
|
|
|
nsCOMPtr<nsICancelable> req;
|
|
|
|
nsresult rv = dns->AsyncResolve(hostBuf,
|
|
|
|
nsIDNSService::RESOLVE_CANONICAL_NAME,
|
2012-07-30 07:20:58 -07:00
|
|
|
listener, nullptr, getter_AddRefs(req));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
2012-10-15 20:52:17 -07:00
|
|
|
printf("### AsyncResolve failed [rv=%x]\n", rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("main thread sleeping for %d seconds...\n", sleepLen);
|
|
|
|
PR_Sleep(PR_SecondsToInterval(sleepLen));
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("shutting down main thread...\n");
|
|
|
|
dns->Shutdown();
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ShutdownXPCOM(nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
return 0;
|
|
|
|
}
|