Back to index

lightning-sunbird  0.9+nobinonly
TestDNS.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla.
00015  *
00016  * The Initial Developer of the Original Code is IBM Corporation.
00017  * Portions created by IBM Corporation are Copyright (C) 2003
00018  * IBM Corporation. All Rights Reserved.
00019  *
00020  * Contributor(s):
00021  *   IBM Corp.
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "TestCommon.h"
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include "nsIServiceManager.h"
00041 #include "nsPIDNSService.h"
00042 #include "nsIDNSListener.h"
00043 #include "nsIDNSRecord.h"
00044 #include "nsICancelable.h"
00045 #include "nsCOMPtr.h"
00046 #include "nsString.h"
00047 #include "nsNetCID.h"
00048 #include "prinrval.h"
00049 #include "prthread.h"
00050 #include "prnetdb.h"
00051 #include "nsCRT.h"
00052 
00053 class myDNSListener : public nsIDNSListener
00054 {
00055 public:
00056     NS_DECL_ISUPPORTS
00057 
00058     myDNSListener(const char *host, PRInt32 index)
00059         : mHost(host)
00060         , mIndex(index) {}
00061     virtual ~myDNSListener() {}
00062 
00063     NS_IMETHOD OnLookupComplete(nsICancelable *request,
00064                                 nsIDNSRecord  *rec,
00065                                 nsresult       status)
00066     {
00067         printf("%d: OnLookupComplete called [host=%s status=%x rec=%p]\n",
00068             mIndex, mHost.get(), status, (void*)rec);
00069 
00070         if (NS_SUCCEEDED(status)) {
00071             nsCAutoString buf;
00072 
00073             rec->GetCanonicalName(buf);
00074             printf("%d: canonname=%s\n", mIndex, buf.get());
00075 
00076             PRBool hasMore;
00077             while (NS_SUCCEEDED(rec->HasMore(&hasMore)) && hasMore) {
00078                 rec->GetNextAddrAsString(buf);
00079                 printf("%d: => %s\n", mIndex, buf.get());
00080             }
00081         }
00082 
00083         return NS_OK;
00084     }
00085 
00086 private:
00087     nsCString mHost;
00088     PRInt32   mIndex;
00089 };
00090 
00091 NS_IMPL_THREADSAFE_ISUPPORTS1(myDNSListener, nsIDNSListener)
00092 
00093 int main(int argc, char **argv)
00094 {
00095     if (test_common_init(&argc, &argv) != 0)
00096         return -1;
00097 
00098     int sleepLen = 10; // default: 10 seconds
00099 
00100     if (argc == 1) {
00101         printf("usage: TestDNS [-N] hostname1 [hostname2 ...]\n");
00102         return -1;
00103     }
00104 
00105     nsCOMPtr<nsPIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID);
00106     if (!dns)
00107         return -1;
00108 
00109     if (argv[1][0] == '-') {
00110         sleepLen = atoi(argv[1]+1);
00111         argv++;
00112         argc--;
00113     }
00114 
00115     for (int j=0; j<2; ++j) {
00116         for (int i=1; i<argc; ++i) {
00117             // assume non-ASCII input is given in the native charset 
00118             nsCAutoString hostBuf;
00119             if (nsCRT::IsAscii(argv[i]))
00120                 hostBuf.Assign(argv[i]);
00121             else
00122                 hostBuf = NS_ConvertUCS2toUTF8(NS_ConvertASCIItoUCS2(argv[i]));
00123 
00124             nsCOMPtr<nsIDNSListener> listener = new myDNSListener(argv[i], i);
00125 
00126             nsCOMPtr<nsICancelable> req;
00127             nsresult rv = dns->AsyncResolve(hostBuf,
00128                                             nsIDNSService::RESOLVE_CANONICAL_NAME,
00129                                             listener, nsnull, getter_AddRefs(req));
00130             if (NS_FAILED(rv))
00131                 printf("### AsyncResolve failed [rv=%x]\n", rv);
00132         }
00133 
00134         printf("main thread sleeping for %d seconds...\n", sleepLen);
00135         PR_Sleep(PR_SecondsToInterval(sleepLen));
00136     }
00137 
00138     printf("shutting down main thread...\n");
00139     dns->Shutdown();
00140     return 0;
00141 }