2004-09-01 19:55:58 +00:00
|
|
|
/* vim:set ts=4 sw=4 sts=4 et cin: */
|
2003-09-11 20:32:33 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is IBM Corporation.
|
|
|
|
* Portions created by IBM Corporation are Copyright (C) 2003
|
|
|
|
* IBM Corporation. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* IBM Corp.
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2004-04-17 00:57:26 +00:00
|
|
|
#if defined(MOZ_LOGGING)
|
|
|
|
#define FORCE_PR_LOG
|
|
|
|
#endif
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
#if defined(HAVE_RES_NINIT)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <arpa/nameser.h>
|
|
|
|
#include <resolv.h>
|
|
|
|
#define RES_RETRY_ON_FAILURE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "nsHostResolver.h"
|
|
|
|
#include "nsNetError.h"
|
|
|
|
#include "nsISupportsBase.h"
|
|
|
|
#include "nsISupportsUtils.h"
|
|
|
|
#include "nsAutoLock.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "pratom.h"
|
|
|
|
#include "prthread.h"
|
|
|
|
#include "prerror.h"
|
|
|
|
#include "prcvar.h"
|
|
|
|
#include "prtime.h"
|
|
|
|
#include "prlong.h"
|
2003-10-25 23:48:33 +00:00
|
|
|
#include "prlog.h"
|
2003-09-11 20:32:33 +00:00
|
|
|
#include "pldhash.h"
|
|
|
|
#include "plstr.h"
|
2005-09-22 21:38:12 +00:00
|
|
|
#include "nsURLHelper.h"
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define MAX_THREADS 8
|
2003-11-03 05:55:07 +00:00
|
|
|
#define IDLE_TIMEOUT PR_SecondsToInterval(60)
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2003-10-25 23:48:33 +00:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
static PRLogModuleInfo *gHostResolverLog = nsnull;
|
|
|
|
#define LOG(args) PR_LOG(gHostResolverLog, PR_LOG_DEBUG, args)
|
2003-09-11 20:32:33 +00:00
|
|
|
#else
|
|
|
|
#define LOG(args)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
MoveCList(PRCList &from, PRCList &to)
|
|
|
|
{
|
|
|
|
if (!PR_CLIST_IS_EMPTY(&from)) {
|
|
|
|
to.next = from.next;
|
|
|
|
to.prev = from.prev;
|
|
|
|
to.next->prev = &to;
|
|
|
|
to.prev->next = &to;
|
|
|
|
PR_INIT_CLIST(&from);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static PRUint32
|
|
|
|
NowInMinutes()
|
|
|
|
{
|
|
|
|
PRTime now = PR_Now(), minutes, factor;
|
|
|
|
LL_I2L(factor, 60 * PR_USEC_PER_SEC);
|
|
|
|
LL_DIV(minutes, now, factor);
|
|
|
|
PRUint32 result;
|
|
|
|
LL_L2UI(result, minutes);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#if defined(RES_RETRY_ON_FAILURE)
|
|
|
|
|
|
|
|
// this class represents the resolver state for a given thread. if we
|
|
|
|
// encounter a lookup failure, then we can invoke the Reset method on an
|
|
|
|
// instance of this class to reset the resolver (in case /etc/resolv.conf
|
|
|
|
// for example changed). this is mainly an issue on GNU systems since glibc
|
|
|
|
// only reads in /etc/resolv.conf once per thread. it may be an issue on
|
|
|
|
// other systems as well.
|
|
|
|
|
|
|
|
class nsResState
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsResState()
|
|
|
|
// initialize mLastReset to the time when this object
|
|
|
|
// is created. this means that a reset will not occur
|
|
|
|
// if a thread is too young. the alternative would be
|
|
|
|
// to initialize this to the beginning of time, so that
|
|
|
|
// the first failure would cause a reset, but since the
|
|
|
|
// thread would have just started up, it likely would
|
|
|
|
// already have current /etc/resolv.conf info.
|
|
|
|
: mLastReset(PR_IntervalNow())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool Reset()
|
|
|
|
{
|
|
|
|
// reset no more than once per second
|
|
|
|
if (PR_IntervalToSeconds(PR_IntervalNow() - mLastReset) < 1)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
LOG(("calling res_ninit\n"));
|
|
|
|
|
|
|
|
mLastReset = PR_IntervalNow();
|
|
|
|
return (res_ninit(&_res) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PRIntervalTime mLastReset;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // RES_RETRY_ON_FAILURE
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2004-09-01 19:55:58 +00:00
|
|
|
// this macro filters out any flags that are not used when constructing the
|
|
|
|
// host key. the significant flags are those that would affect the resulting
|
|
|
|
// host record (i.e., the flags that are passed down to PR_GetAddrInfoByName).
|
|
|
|
#define RES_KEY_FLAGS(_f) ((_f) & nsHostResolver::RES_CANON_NAME)
|
|
|
|
|
2003-10-07 05:11:41 +00:00
|
|
|
nsresult
|
2004-09-01 19:55:58 +00:00
|
|
|
nsHostRecord::Create(const nsHostKey *key, nsHostRecord **result)
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
2007-12-11 21:45:42 +00:00
|
|
|
PRLock *lock = PR_NewLock();
|
|
|
|
if (!lock)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2004-09-01 19:55:58 +00:00
|
|
|
size_t hostLen = strlen(key->host) + 1;
|
2003-10-07 05:11:41 +00:00
|
|
|
size_t size = hostLen + sizeof(nsHostRecord);
|
2003-09-11 20:32:33 +00:00
|
|
|
|
2003-10-07 05:11:41 +00:00
|
|
|
nsHostRecord *rec = (nsHostRecord*) ::operator new(size);
|
2007-12-11 21:45:42 +00:00
|
|
|
if (!rec) {
|
|
|
|
PR_DestroyLock(lock);
|
2003-10-07 05:11:41 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-12-11 21:45:42 +00:00
|
|
|
}
|
2003-09-11 20:32:33 +00:00
|
|
|
|
2003-10-07 05:11:41 +00:00
|
|
|
rec->host = ((char *) rec) + sizeof(nsHostRecord);
|
2004-09-01 19:55:58 +00:00
|
|
|
rec->flags = RES_KEY_FLAGS(key->flags);
|
|
|
|
rec->af = key->af;
|
|
|
|
|
|
|
|
rec->_refc = 1; // addref
|
2005-11-16 19:35:07 +00:00
|
|
|
NS_LOG_ADDREF(rec, 1, "nsHostRecord", sizeof(nsHostRecord));
|
2007-12-11 21:45:42 +00:00
|
|
|
rec->addr_info_lock = lock;
|
2004-04-17 00:57:26 +00:00
|
|
|
rec->addr_info = nsnull;
|
2007-12-11 21:45:42 +00:00
|
|
|
rec->addr_info_gencnt = 0;
|
2003-10-07 05:11:41 +00:00
|
|
|
rec->addr = nsnull;
|
|
|
|
rec->expiration = NowInMinutes();
|
2003-10-25 23:48:33 +00:00
|
|
|
rec->resolving = PR_FALSE;
|
2003-10-07 05:11:41 +00:00
|
|
|
PR_INIT_CLIST(rec);
|
|
|
|
PR_INIT_CLIST(&rec->callbacks);
|
2004-09-01 19:55:58 +00:00
|
|
|
memcpy((char *) rec->host, key->host, hostLen);
|
2003-09-11 20:32:33 +00:00
|
|
|
|
2003-10-07 05:11:41 +00:00
|
|
|
*result = rec;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHostRecord::~nsHostRecord()
|
|
|
|
{
|
2007-12-11 21:45:42 +00:00
|
|
|
if (addr_info_lock)
|
|
|
|
PR_DestroyLock(addr_info_lock);
|
2004-04-17 00:57:26 +00:00
|
|
|
if (addr_info)
|
|
|
|
PR_FreeAddrInfo(addr_info);
|
2003-10-07 05:11:41 +00:00
|
|
|
if (addr)
|
|
|
|
free(addr);
|
|
|
|
}
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
struct nsHostDBEnt : PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
nsHostRecord *rec;
|
|
|
|
};
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashNumber
|
2004-09-01 19:55:58 +00:00
|
|
|
HostDB_HashKey(PLDHashTable *table, const void *key)
|
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
const nsHostKey *hk = static_cast<const nsHostKey *>(key);
|
2004-09-01 22:35:37 +00:00
|
|
|
return PL_DHashStringKey(table, hk->host) ^ hk->flags ^ hk->af;
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static PRBool
|
2003-09-11 20:32:33 +00:00
|
|
|
HostDB_MatchEntry(PLDHashTable *table,
|
|
|
|
const PLDHashEntryHdr *entry,
|
|
|
|
const void *key)
|
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
const nsHostDBEnt *he = static_cast<const nsHostDBEnt *>(entry);
|
|
|
|
const nsHostKey *hk = static_cast<const nsHostKey *>(key);
|
2004-09-01 19:55:58 +00:00
|
|
|
|
|
|
|
return !strcmp(he->rec->host, hk->host) &&
|
|
|
|
he->rec->flags == hk->flags &&
|
|
|
|
he->rec->af == hk->af;
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static void
|
2003-09-11 20:32:33 +00:00
|
|
|
HostDB_MoveEntry(PLDHashTable *table,
|
|
|
|
const PLDHashEntryHdr *from,
|
|
|
|
PLDHashEntryHdr *to)
|
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
static_cast<nsHostDBEnt *>(to)->rec =
|
|
|
|
static_cast<const nsHostDBEnt *>(from)->rec;
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static void
|
2003-09-11 20:32:33 +00:00
|
|
|
HostDB_ClearEntry(PLDHashTable *table,
|
|
|
|
PLDHashEntryHdr *entry)
|
|
|
|
{
|
2004-04-20 17:57:45 +00:00
|
|
|
LOG(("evicting record\n"));
|
2007-07-08 07:08:04 +00:00
|
|
|
nsHostDBEnt *he = static_cast<nsHostDBEnt *>(entry);
|
2004-04-17 00:57:26 +00:00
|
|
|
#if defined(DEBUG) && defined(PR_LOGGING)
|
|
|
|
if (!he->rec->addr_info)
|
|
|
|
LOG(("%s: => no addr_info\n", he->rec->host));
|
2003-09-11 20:32:33 +00:00
|
|
|
else {
|
|
|
|
PRInt32 now = (PRInt32) NowInMinutes();
|
2003-10-07 05:11:41 +00:00
|
|
|
PRInt32 diff = (PRInt32) he->rec->expiration - now;
|
2003-09-11 20:32:33 +00:00
|
|
|
LOG(("%s: exp=%d => %s\n",
|
|
|
|
he->rec->host, diff,
|
2004-04-17 00:57:26 +00:00
|
|
|
PR_GetCanonNameFromAddrInfo(he->rec->addr_info)));
|
2003-09-11 20:32:33 +00:00
|
|
|
void *iter = nsnull;
|
|
|
|
PRNetAddr addr;
|
|
|
|
char buf[64];
|
2003-11-03 05:55:07 +00:00
|
|
|
for (;;) {
|
2004-04-17 00:57:26 +00:00
|
|
|
iter = PR_EnumerateAddrInfo(iter, he->rec->addr_info, 0, &addr);
|
2003-11-03 05:55:07 +00:00
|
|
|
if (!iter)
|
|
|
|
break;
|
2003-09-11 20:32:33 +00:00
|
|
|
PR_NetAddrToString(&addr, buf, sizeof(buf));
|
|
|
|
LOG((" %s\n", buf));
|
2003-11-03 05:55:07 +00:00
|
|
|
}
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
NS_RELEASE(he->rec);
|
|
|
|
}
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static PRBool
|
2003-09-11 20:32:33 +00:00
|
|
|
HostDB_InitEntry(PLDHashTable *table,
|
|
|
|
PLDHashEntryHdr *entry,
|
|
|
|
const void *key)
|
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
nsHostDBEnt *he = static_cast<nsHostDBEnt *>(entry);
|
|
|
|
nsHostRecord::Create(static_cast<const nsHostKey *>(key), &he->rec);
|
2003-09-11 20:32:33 +00:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashTableOps gHostDB_ops =
|
|
|
|
{
|
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
2004-09-01 19:55:58 +00:00
|
|
|
HostDB_HashKey,
|
2003-09-11 20:32:33 +00:00
|
|
|
HostDB_MatchEntry,
|
|
|
|
HostDB_MoveEntry,
|
|
|
|
HostDB_ClearEntry,
|
|
|
|
PL_DHashFinalizeStub,
|
|
|
|
HostDB_InitEntry,
|
|
|
|
};
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashOperator
|
2003-09-11 20:32:33 +00:00
|
|
|
HostDB_RemoveEntry(PLDHashTable *table,
|
|
|
|
PLDHashEntryHdr *hdr,
|
|
|
|
PRUint32 number,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsHostResolver::nsHostResolver(PRUint32 maxCacheEntries,
|
|
|
|
PRUint32 maxCacheLifetime)
|
2003-10-07 05:11:41 +00:00
|
|
|
: mMaxCacheEntries(maxCacheEntries)
|
2003-09-11 20:32:33 +00:00
|
|
|
, mMaxCacheLifetime(maxCacheLifetime)
|
|
|
|
, mLock(nsnull)
|
|
|
|
, mIdleThreadCV(nsnull)
|
|
|
|
, mHaveIdleThread(PR_FALSE)
|
|
|
|
, mThreadCount(0)
|
|
|
|
, mEvictionQSize(0)
|
|
|
|
, mShutdown(PR_TRUE)
|
|
|
|
{
|
|
|
|
mCreationTime = PR_Now();
|
|
|
|
PR_INIT_CLIST(&mPendingQ);
|
|
|
|
PR_INIT_CLIST(&mEvictionQ);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHostResolver::~nsHostResolver()
|
|
|
|
{
|
|
|
|
if (mIdleThreadCV)
|
|
|
|
PR_DestroyCondVar(mIdleThreadCV);
|
|
|
|
|
|
|
|
if (mLock)
|
|
|
|
PR_DestroyLock(mLock);
|
|
|
|
|
|
|
|
PL_DHashTableFinish(&mDB);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHostResolver::Init()
|
|
|
|
{
|
|
|
|
mLock = PR_NewLock();
|
|
|
|
if (!mLock)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
mIdleThreadCV = PR_NewCondVar(mLock);
|
|
|
|
if (!mIdleThreadCV)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
PL_DHashTableInit(&mDB, &gHostDB_ops, nsnull, sizeof(nsHostDBEnt), 0);
|
|
|
|
|
|
|
|
mShutdown = PR_FALSE;
|
2006-05-25 16:49:48 +00:00
|
|
|
|
|
|
|
#if defined(HAVE_RES_NINIT)
|
|
|
|
// We want to make sure the system is using the correct resolver settings,
|
|
|
|
// so we force it to reload those settings whenever we startup a subsequent
|
|
|
|
// nsHostResolver instance. We assume that there is no reason to do this
|
|
|
|
// for the first nsHostResolver instance since that is usually created
|
|
|
|
// during application startup.
|
|
|
|
static int initCount = 0;
|
|
|
|
if (initCount++ > 0) {
|
|
|
|
LOG(("calling res_ninit\n"));
|
|
|
|
res_ninit(&_res);
|
|
|
|
}
|
|
|
|
#endif
|
2003-09-11 20:32:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHostResolver::Shutdown()
|
|
|
|
{
|
|
|
|
LOG(("nsHostResolver::Shutdown\n"));
|
|
|
|
|
2006-04-29 04:37:07 +00:00
|
|
|
PRCList pendingQ, evictionQ;
|
2003-09-11 20:32:33 +00:00
|
|
|
PR_INIT_CLIST(&pendingQ);
|
2006-04-29 04:37:07 +00:00
|
|
|
PR_INIT_CLIST(&evictionQ);
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
|
|
|
|
mShutdown = PR_TRUE;
|
|
|
|
|
|
|
|
MoveCList(mPendingQ, pendingQ);
|
2006-04-29 04:37:07 +00:00
|
|
|
MoveCList(mEvictionQ, evictionQ);
|
|
|
|
mEvictionQSize = 0;
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
if (mHaveIdleThread)
|
|
|
|
PR_NotifyCondVar(mIdleThreadCV);
|
|
|
|
|
|
|
|
// empty host database
|
|
|
|
PL_DHashTableEnumerate(&mDB, HostDB_RemoveEntry, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop through pending queue, erroring out pending lookups.
|
|
|
|
if (!PR_CLIST_IS_EMPTY(&pendingQ)) {
|
|
|
|
PRCList *node = pendingQ.next;
|
|
|
|
while (node != &pendingQ) {
|
2007-07-08 07:08:04 +00:00
|
|
|
nsHostRecord *rec = static_cast<nsHostRecord *>(node);
|
2003-09-11 20:32:33 +00:00
|
|
|
node = node->next;
|
|
|
|
OnLookupComplete(rec, NS_ERROR_ABORT, nsnull);
|
|
|
|
}
|
|
|
|
}
|
2006-04-29 04:37:07 +00:00
|
|
|
|
|
|
|
if (!PR_CLIST_IS_EMPTY(&evictionQ)) {
|
|
|
|
PRCList *node = evictionQ.next;
|
|
|
|
while (node != &evictionQ) {
|
2007-07-08 07:08:04 +00:00
|
|
|
nsHostRecord *rec = static_cast<nsHostRecord *>(node);
|
2006-04-29 04:37:07 +00:00
|
|
|
node = node->next;
|
|
|
|
NS_RELEASE(rec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2003-10-07 05:11:41 +00:00
|
|
|
nsHostResolver::ResolveHost(const char *host,
|
2004-09-01 19:55:58 +00:00
|
|
|
PRUint16 flags,
|
|
|
|
PRUint16 af,
|
|
|
|
nsResolveHostCallback *callback)
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(host && *host, NS_ERROR_UNEXPECTED);
|
|
|
|
|
2003-10-25 23:48:33 +00:00
|
|
|
LOG(("nsHostResolver::ResolveHost [host=%s]\n", host));
|
|
|
|
|
2005-09-22 21:38:12 +00:00
|
|
|
// ensure that we are working with a valid hostname before proceeding. see
|
|
|
|
// bug 304904 for details.
|
|
|
|
if (!net_IsValidHostName(nsDependentCString(host)))
|
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
|
|
|
|
2003-10-07 05:11:41 +00:00
|
|
|
// if result is set inside the lock, then we need to issue the
|
|
|
|
// callback before returning.
|
|
|
|
nsRefPtr<nsHostRecord> result;
|
|
|
|
nsresult status = NS_OK, rv = NS_OK;
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
2003-09-11 20:32:33 +00:00
|
|
|
|
2003-10-07 05:11:41 +00:00
|
|
|
if (mShutdown)
|
|
|
|
rv = NS_ERROR_NOT_INITIALIZED;
|
|
|
|
else {
|
|
|
|
PRNetAddr tempAddr;
|
2003-11-03 09:10:57 +00:00
|
|
|
|
|
|
|
// unfortunately, PR_StringToNetAddr does not properly initialize
|
|
|
|
// the output buffer in the case of IPv6 input. see bug 223145.
|
2003-11-03 08:54:49 +00:00
|
|
|
memset(&tempAddr, 0, sizeof(PRNetAddr));
|
2003-10-07 05:11:41 +00:00
|
|
|
|
|
|
|
// check to see if there is already an entry for this |host|
|
|
|
|
// in the hash table. if so, then check to see if we can't
|
|
|
|
// just reuse the lookup result. otherwise, if there are
|
|
|
|
// any pending callbacks, then add to pending callbacks queue,
|
|
|
|
// and return. otherwise, add ourselves as first pending
|
|
|
|
// callback, and proceed to do the lookup.
|
|
|
|
|
2004-09-01 19:55:58 +00:00
|
|
|
nsHostKey key = { host, flags, af };
|
2007-07-08 07:08:04 +00:00
|
|
|
nsHostDBEnt *he = static_cast<nsHostDBEnt *>
|
|
|
|
(PL_DHashTableOperate(&mDB, &key, PL_DHASH_ADD));
|
2003-10-07 05:11:41 +00:00
|
|
|
|
|
|
|
// if the record is null, then HostDB_InitEntry failed.
|
|
|
|
if (!he || !he->rec)
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
// do we have a cached result that we can reuse?
|
2004-09-01 19:55:58 +00:00
|
|
|
else if (!(flags & RES_BYPASS_CACHE) &&
|
2003-10-07 05:11:41 +00:00
|
|
|
he->rec->HasResult() &&
|
|
|
|
NowInMinutes() <= he->rec->expiration) {
|
2004-04-20 17:57:45 +00:00
|
|
|
LOG(("using cached record\n"));
|
2003-10-07 05:11:41 +00:00
|
|
|
// put reference to host record on stack...
|
|
|
|
result = he->rec;
|
|
|
|
}
|
2008-02-27 00:27:33 +00:00
|
|
|
// if the host name is an IP address literal and has been parsed,
|
|
|
|
// go ahead and use it.
|
|
|
|
else if (he->rec->addr) {
|
|
|
|
result = he->rec;
|
|
|
|
}
|
2003-10-07 05:11:41 +00:00
|
|
|
// try parsing the host name as an IP address literal to short
|
|
|
|
// circuit full host resolution. (this is necessary on some
|
|
|
|
// platforms like Win9x. see bug 219376 for more details.)
|
|
|
|
else if (PR_StringToNetAddr(host, &tempAddr) == PR_SUCCESS) {
|
|
|
|
// ok, just copy the result into the host record, and be done
|
|
|
|
// with it! ;-)
|
|
|
|
he->rec->addr = (PRNetAddr *) malloc(sizeof(PRNetAddr));
|
|
|
|
if (!he->rec->addr)
|
|
|
|
status = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
else
|
|
|
|
memcpy(he->rec->addr, &tempAddr, sizeof(PRNetAddr));
|
|
|
|
// put reference to host record on stack...
|
|
|
|
result = he->rec;
|
|
|
|
}
|
|
|
|
// otherwise, hit the resolver...
|
|
|
|
else {
|
|
|
|
// add callback to the list of pending callbacks
|
|
|
|
PR_APPEND_LINK(callback, &he->rec->callbacks);
|
2003-09-11 20:32:33 +00:00
|
|
|
|
2003-10-25 23:48:33 +00:00
|
|
|
if (!he->rec->resolving) {
|
2003-10-07 05:11:41 +00:00
|
|
|
rv = IssueLookup(he->rec);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
PR_REMOVE_AND_INIT_LINK(callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
2003-10-07 05:11:41 +00:00
|
|
|
if (result)
|
|
|
|
callback->OnLookupComplete(this, result, status);
|
2003-09-11 20:32:33 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-10-07 05:11:41 +00:00
|
|
|
nsHostResolver::DetachCallback(const char *host,
|
2004-09-01 19:55:58 +00:00
|
|
|
PRUint16 flags,
|
|
|
|
PRUint16 af,
|
2005-04-06 01:33:28 +00:00
|
|
|
nsResolveHostCallback *callback,
|
|
|
|
nsresult status)
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
2003-10-07 05:11:41 +00:00
|
|
|
nsRefPtr<nsHostRecord> rec;
|
2003-09-11 20:32:33 +00:00
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
|
2004-09-01 19:55:58 +00:00
|
|
|
nsHostKey key = { host, flags, af };
|
2007-07-08 07:08:04 +00:00
|
|
|
nsHostDBEnt *he = static_cast<nsHostDBEnt *>
|
|
|
|
(PL_DHashTableOperate(&mDB, &key, PL_DHASH_LOOKUP));
|
2003-09-11 20:32:33 +00:00
|
|
|
if (he && he->rec) {
|
|
|
|
// walk list looking for |callback|... we cannot assume
|
|
|
|
// that it will be there!
|
|
|
|
PRCList *node = he->rec->callbacks.next;
|
|
|
|
while (node != &he->rec->callbacks) {
|
2007-07-08 07:08:04 +00:00
|
|
|
if (static_cast<nsResolveHostCallback *>(node) == callback) {
|
2003-09-11 20:32:33 +00:00
|
|
|
PR_REMOVE_LINK(callback);
|
2003-10-07 05:11:41 +00:00
|
|
|
rec = he->rec;
|
2003-09-11 20:32:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-06 01:33:28 +00:00
|
|
|
// complete callback with the given status code; this would only be done if
|
|
|
|
// the record was in the process of being resolved.
|
2003-10-07 05:11:41 +00:00
|
|
|
if (rec)
|
2005-04-06 01:33:28 +00:00
|
|
|
callback->OnLookupComplete(this, rec, status);
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHostResolver::IssueLookup(nsHostRecord *rec)
|
|
|
|
{
|
2003-10-25 23:48:33 +00:00
|
|
|
NS_ASSERTION(!rec->resolving, "record is already being resolved");
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
// add rec to mPendingQ, possibly removing it from mEvictionQ.
|
|
|
|
// if rec is on mEvictionQ, then we can just move the owning
|
|
|
|
// reference over to mPendingQ.
|
|
|
|
if (rec->next == rec)
|
|
|
|
NS_ADDREF(rec);
|
|
|
|
else {
|
|
|
|
PR_REMOVE_LINK(rec);
|
|
|
|
mEvictionQSize--;
|
|
|
|
}
|
|
|
|
PR_APPEND_LINK(rec, &mPendingQ);
|
2003-10-25 23:48:33 +00:00
|
|
|
rec->resolving = PR_TRUE;
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
if (mHaveIdleThread) {
|
|
|
|
// wake up idle thread to process this lookup
|
|
|
|
PR_NotifyCondVar(mIdleThreadCV);
|
|
|
|
}
|
|
|
|
else if (mThreadCount < MAX_THREADS) {
|
|
|
|
// dispatch new worker thread
|
|
|
|
NS_ADDREF_THIS(); // owning reference passed to thread
|
|
|
|
mThreadCount++;
|
|
|
|
PRThread *thr = PR_CreateThread(PR_SYSTEM_THREAD,
|
|
|
|
ThreadFunc,
|
|
|
|
this,
|
|
|
|
PR_PRIORITY_NORMAL,
|
|
|
|
PR_GLOBAL_THREAD,
|
|
|
|
PR_UNJOINABLE_THREAD,
|
|
|
|
0);
|
|
|
|
if (!thr) {
|
|
|
|
mThreadCount--;
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
2005-10-28 21:24:48 +00:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
else
|
|
|
|
LOG(("lookup waiting for thread - %s ...\n", rec->host));
|
|
|
|
#endif
|
2003-09-11 20:32:33 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsHostResolver::GetHostToLookup(nsHostRecord **result)
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
|
2003-10-25 23:48:33 +00:00
|
|
|
PRIntervalTime start = PR_IntervalNow(), timeout = IDLE_TIMEOUT;
|
|
|
|
//
|
|
|
|
// wait for one or more of the following to occur:
|
|
|
|
// (1) the pending queue has a host record to process
|
|
|
|
// (2) the shutdown flag has been set
|
|
|
|
// (3) the thread has been idle for too long
|
|
|
|
//
|
|
|
|
// PR_WaitCondVar will return when any of these conditions is true.
|
|
|
|
//
|
2003-09-11 20:32:33 +00:00
|
|
|
while (PR_CLIST_IS_EMPTY(&mPendingQ) && !mHaveIdleThread && !mShutdown) {
|
|
|
|
// become the idle thread and wait for a lookup
|
|
|
|
mHaveIdleThread = PR_TRUE;
|
2003-10-25 23:48:33 +00:00
|
|
|
PR_WaitCondVar(mIdleThreadCV, timeout);
|
2003-09-11 20:32:33 +00:00
|
|
|
mHaveIdleThread = PR_FALSE;
|
2003-10-25 23:48:33 +00:00
|
|
|
|
|
|
|
PRIntervalTime delta = PR_IntervalNow() - start;
|
|
|
|
if (delta >= timeout)
|
|
|
|
break;
|
|
|
|
timeout -= delta;
|
|
|
|
start += delta;
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!PR_CLIST_IS_EMPTY(&mPendingQ)) {
|
|
|
|
// remove next record from mPendingQ; hand over owning reference.
|
2007-07-08 07:08:04 +00:00
|
|
|
*result = static_cast<nsHostRecord *>(mPendingQ.next);
|
2003-09-11 20:32:33 +00:00
|
|
|
PR_REMOVE_AND_INIT_LINK(*result);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tell thread to exit...
|
|
|
|
mThreadCount--;
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHostResolver::OnLookupComplete(nsHostRecord *rec, nsresult status, PRAddrInfo *result)
|
|
|
|
{
|
|
|
|
// get the list of pending callbacks for this lookup, and notify
|
|
|
|
// them that the lookup is complete.
|
|
|
|
PRCList cbs;
|
|
|
|
PR_INIT_CLIST(&cbs);
|
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
|
2003-10-07 05:11:41 +00:00
|
|
|
// grab list of callbacks to notify
|
2003-09-11 20:32:33 +00:00
|
|
|
MoveCList(rec->callbacks, cbs);
|
2003-10-07 05:11:41 +00:00
|
|
|
|
2007-09-28 14:43:03 +00:00
|
|
|
// update record fields. We might have a rec->addr_info already if a
|
|
|
|
// previous lookup result expired and we're reresolving it..
|
2007-12-11 21:45:42 +00:00
|
|
|
PRAddrInfo *old_addr_info;
|
|
|
|
PR_Lock(rec->addr_info_lock);
|
|
|
|
old_addr_info = rec->addr_info;
|
2004-04-17 00:57:26 +00:00
|
|
|
rec->addr_info = result;
|
2007-12-11 21:45:42 +00:00
|
|
|
rec->addr_info_gencnt++;
|
|
|
|
PR_Unlock(rec->addr_info_lock);
|
|
|
|
if (old_addr_info)
|
|
|
|
PR_FreeAddrInfo(old_addr_info);
|
2008-04-09 00:42:52 +00:00
|
|
|
rec->expiration = NowInMinutes();
|
|
|
|
if (result)
|
|
|
|
rec->expiration += mMaxCacheLifetime;
|
2003-10-25 23:48:33 +00:00
|
|
|
rec->resolving = PR_FALSE;
|
2003-09-11 20:32:33 +00:00
|
|
|
|
2006-04-29 04:37:07 +00:00
|
|
|
if (rec->addr_info && !mShutdown) {
|
2003-09-11 20:32:33 +00:00
|
|
|
// add to mEvictionQ
|
|
|
|
PR_APPEND_LINK(rec, &mEvictionQ);
|
|
|
|
NS_ADDREF(rec);
|
|
|
|
if (mEvictionQSize < mMaxCacheEntries)
|
|
|
|
mEvictionQSize++;
|
|
|
|
else {
|
2004-04-20 17:57:45 +00:00
|
|
|
// remove first element on mEvictionQ
|
|
|
|
nsHostRecord *head =
|
2007-07-08 07:08:04 +00:00
|
|
|
static_cast<nsHostRecord *>(PR_LIST_HEAD(&mEvictionQ));
|
2004-04-20 17:57:45 +00:00
|
|
|
PR_REMOVE_AND_INIT_LINK(head);
|
2004-09-01 19:55:58 +00:00
|
|
|
PL_DHashTableOperate(&mDB, (nsHostKey *) head, PL_DHASH_REMOVE);
|
2003-09-11 20:32:33 +00:00
|
|
|
// release reference to rec owned by mEvictionQ
|
2004-04-20 17:57:45 +00:00
|
|
|
NS_RELEASE(head);
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PR_CLIST_IS_EMPTY(&cbs)) {
|
|
|
|
PRCList *node = cbs.next;
|
|
|
|
while (node != &cbs) {
|
2003-10-07 05:11:41 +00:00
|
|
|
nsResolveHostCallback *callback =
|
2007-07-08 07:08:04 +00:00
|
|
|
static_cast<nsResolveHostCallback *>(node);
|
2003-09-11 20:32:33 +00:00
|
|
|
node = node->next;
|
2003-10-07 05:11:41 +00:00
|
|
|
callback->OnLookupComplete(this, rec, status);
|
2003-09-11 20:32:33 +00:00
|
|
|
// NOTE: callback must not be dereferenced after this point!!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_RELEASE(rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
void
|
2003-09-11 20:32:33 +00:00
|
|
|
nsHostResolver::ThreadFunc(void *arg)
|
|
|
|
{
|
2003-10-25 23:48:33 +00:00
|
|
|
LOG(("nsHostResolver::ThreadFunc entering\n"));
|
2003-09-11 20:32:33 +00:00
|
|
|
#if defined(RES_RETRY_ON_FAILURE)
|
|
|
|
nsResState rs;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsHostResolver *resolver = (nsHostResolver *) arg;
|
|
|
|
nsHostRecord *rec;
|
|
|
|
PRAddrInfo *ai;
|
|
|
|
while (resolver->GetHostToLookup(&rec)) {
|
2003-10-25 23:48:33 +00:00
|
|
|
LOG(("resolving %s ...\n", rec->host));
|
2004-09-01 19:55:58 +00:00
|
|
|
|
|
|
|
PRIntn flags = PR_AI_ADDRCONFIG;
|
|
|
|
if (!(rec->flags & RES_CANON_NAME))
|
|
|
|
flags |= PR_AI_NOCANONNAME;
|
|
|
|
|
|
|
|
ai = PR_GetAddrInfoByName(rec->host, rec->af, flags);
|
2003-09-11 20:32:33 +00:00
|
|
|
#if defined(RES_RETRY_ON_FAILURE)
|
|
|
|
if (!ai && rs.Reset())
|
2004-09-01 19:55:58 +00:00
|
|
|
ai = PR_GetAddrInfoByName(rec->host, rec->af, flags);
|
2003-09-11 20:32:33 +00:00
|
|
|
#endif
|
2004-09-01 19:55:58 +00:00
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
// convert error code to nsresult.
|
|
|
|
nsresult status = ai ? NS_OK : NS_ERROR_UNKNOWN_HOST;
|
|
|
|
resolver->OnLookupComplete(rec, status, ai);
|
2005-10-28 21:24:48 +00:00
|
|
|
LOG(("lookup complete for %s ...\n", rec->host));
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
NS_RELEASE(resolver);
|
2003-10-25 23:48:33 +00:00
|
|
|
LOG(("nsHostResolver::ThreadFunc exiting\n"));
|
2003-09-11 20:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHostResolver::Create(PRUint32 maxCacheEntries,
|
|
|
|
PRUint32 maxCacheLifetime,
|
|
|
|
nsHostResolver **result)
|
|
|
|
{
|
2003-10-25 23:48:33 +00:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (!gHostResolverLog)
|
|
|
|
gHostResolverLog = PR_NewLogModule("nsHostResolver");
|
|
|
|
#endif
|
|
|
|
|
2003-09-11 20:32:33 +00:00
|
|
|
nsHostResolver *res = new nsHostResolver(maxCacheEntries,
|
|
|
|
maxCacheLifetime);
|
|
|
|
if (!res)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(res);
|
|
|
|
|
|
|
|
nsresult rv = res->Init();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_RELEASE(res);
|
|
|
|
|
|
|
|
*result = res;
|
|
|
|
return rv;
|
|
|
|
}
|