uuid-corrected-duplicate-check-20080501
[openafs.git] / src / viced / host.c
index c4b0c10..28ba0e1 100644 (file)
@@ -17,6 +17,7 @@ RCSID
 
 #include <stdio.h>
 #include <errno.h>
+#include <string.h>
 #ifdef AFS_NT40_ENV
 #include <fcntl.h>
 #include <winsock2.h>
@@ -26,14 +27,6 @@ RCSID
 #include <netinet/in.h>
 #endif
 
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
-
 #include <afs/stds.h>
 #include <rx/xdr.h>
 #include <afs/assert.h>
@@ -82,6 +75,7 @@ extern struct afsconf_dir *confDir;   /* config dir object */
 extern int lwps;               /* the max number of server threads */
 extern afsUUID FS_HostUUID;
 
+afsUUID nulluuid;
 int CEs = 0;                   /* active clients */
 int CEBlocks = 0;              /* number of blocks of CEs */
 struct client *CEFree = 0;     /* first free client */
@@ -93,9 +87,6 @@ int rxcon_client_key;
 static struct rx_securityClass *sc = NULL;
 
 static void h_SetupCallbackConn_r(struct host * host);
-static void h_AddHostToHashTable_r(afs_uint32 addr, afs_uint16 port, struct host * host);
-static void h_AddHostToUuidHashTable_r(afsUUID * uuid, struct host * host);
-static int h_DeleteHostFromHashTableByAddr_r(afs_uint32 addr, afs_uint16 port, struct host *host);
 
 #define CESPERBLOCK 73
 struct CEBlock {               /* block of CESPERBLOCK file entries */
@@ -103,7 +94,6 @@ struct CEBlock {             /* block of CESPERBLOCK file entries */
 };
 
 static void h_TossStuff_r(register struct host *host);
-static int hashDelete_r(afs_uint32 addr, afs_uint16 port, struct host *host);
 
 /*
  * Make sure the subnet macros have been defined.
@@ -197,8 +187,8 @@ static struct host *HTFree = 0;     /* first free file entry */
  * to map IP addresses onto host pointers, and another
  * to map host UUIDs onto host pointers.
  */
-static struct h_hashChain *hostHashTable[h_HASHENTRIES];
-static struct h_hashChain *hostUuidHashTable[h_HASHENTRIES];
+static struct h_AddrHashChain *hostAddrHashTable[h_HASHENTRIES];
+static struct h_UuidHashChain *hostUuidHashTable[h_HASHENTRIES];
 #define h_HashIndex(hostip) ((hostip) & (h_HASHENTRIES-1))
 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
 
@@ -394,7 +384,10 @@ hpr_GetHostCPS(afs_int32 host, prlist *CPS)
 
     if (!uclient) {
         code = hpr_Initialize(&uclient);
-        assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
+       if (!code) 
+           assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
+       else
+           return code;
     }
 
     over = 0;
@@ -425,7 +418,10 @@ hpr_NameToId(namelist *names, idlist *ids)
 
     if (!uclient) {
         code = hpr_Initialize(&uclient);
-        assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
+       if (!code)
+           assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
+       else
+           return code;
     }
 
     for (i = 0; i < names->namelist_len; i++)
@@ -447,7 +443,10 @@ hpr_IdToName(idlist *ids, namelist *names)
     
     if (!uclient) {
         code = hpr_Initialize(&uclient);
-        assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
+       if (!code)
+           assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
+       else
+           return code;
     }
 
     code = ubik_PR_IDToName(uclient, 0, ids, names);
@@ -468,7 +467,10 @@ hpr_GetCPS(afs_int32 id, prlist *CPS)
 
     if (!uclient) {
         code = hpr_Initialize(&uclient);
-        assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
+       if (!code)
+           assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
+       else
+           return code;
     }
 
     over = 0;
@@ -686,6 +688,7 @@ h_gethostcps_r(register struct host *host, register afs_int32 now)
     code = hpr_GetHostCPS(ntohl(host->host), &host->hcps);
     H_LOCK;
     if (code) {
+        char hoststr[16];
        /*
         * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
         * and reevaluates things if no sync server or quorum is found we could still end up
@@ -707,13 +710,13 @@ h_gethostcps_r(register struct host *host, register afs_int32 now)
             */
            host->hcpsfailed = 1;
            ViceLog(0,
-                   ("Warning:  GetHostCPS failed (%d) for %x; will retry\n",
-                    code, host->host));
+                   ("Warning:  GetHostCPS failed (%d) for %x (%s:%d); will retry\n",
+                    code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
        } else {
            host->hcpsfailed = 0;
            ViceLog(1,
-                   ("gethost:  GetHostCPS failed (%d) for %x; ignored\n",
-                    code, host->host));
+                   ("gethost:  GetHostCPS failed (%d) for %x (%s:%d); ignored\n",
+                    code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
        }
        if (host->hcps.prlist_val)
            free(host->hcps.prlist_val);
@@ -739,11 +742,11 @@ h_gethostcps_r(register struct host *host, register afs_int32 now)
 void
 h_flushhostcps(register afs_uint32 hostaddr, register afs_uint16 hport)
 {
-    register struct host *host;
+    struct host *host;
     int held = 0;
 
     H_LOCK;
-    host = h_Lookup_r(hostaddr, hport, &held);
+    h_Lookup_r(hostaddr, hport, &held, &host);
     if (host) {
        host->hcpsfailed = 1;
        if (!held)
@@ -775,7 +778,7 @@ h_Alloc_r(register struct rx_connection *r_con)
     host->host = rxr_HostOf(r_con);
     host->port = rxr_PortOf(r_con);
 
-    h_AddHostToHashTable_r(host->host, host->port, host);
+    h_AddHostToAddrHashTable_r(host->host, host->port, host);
 
     if (consolePort == 0) {    /* find the portal number for console */
 #if    defined(AFS_OSF_ENV)
@@ -838,21 +841,26 @@ h_SetupCallbackConn_r(struct host * host)
 /* hostaddr and hport are in network order */
 /* Note: host should be released by caller if 0 == *heldp and non-null */
 /* hostaddr and hport are in network order */
-struct host *
-h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, int *heldp)
+int
+h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, int *heldp, struct host **hostp)
 {
     afs_int32 now;
-    struct host *host = 0;
-    struct h_hashChain *chain;
+    struct host *host = NULL;
+    struct h_AddrHashChain *chain;
     int index = h_HashIndex(haddr);
     extern int hostaclRefresh;
 
   restart:
-    for (chain = hostHashTable[index]; chain; chain = chain->next) {
+    for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
        host = chain->hostPtr;
        assert(host);
        if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
            && chain->port == hport) {
+           if ((host->hostFlags & HWHO_INPROGRESS) && 
+               h_threadquota(host->lock.num_waiting)) {
+               *hostp = 0;
+               return VBUSY;
+           }
            *heldp = h_Held_r(host);
            if (!*heldp)
                h_Hold_r(host);
@@ -880,8 +888,8 @@ h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, int *heldp)
        }
        host = NULL;
     }
-    return host;
-
+    *hostp = host;
+    return 0;
 }                              /*h_Lookup */
 
 /* Lookup a host given its UUID. */
@@ -889,7 +897,7 @@ struct host *
 h_LookupUuid_r(afsUUID * uuidp)
 {
     struct host *host = 0;
-    struct h_hashChain *chain;
+    struct h_UuidHashChain *chain;
     int index = h_UuidHashIndex(uuidp);
 
     for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
@@ -933,8 +941,8 @@ h_TossStuff_r(register struct host *host)
     if (h_NBLock_r(host) != 0) {
        char hoststr[16];
        ViceLog(0,
-               ("Warning:  h_TossStuff_r failed; Host %s:%d was locked.\n",
-                afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+               ("Warning:  h_TossStuff_r failed; Host %x (%s:%d) was locked.\n",
+                host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
        return;
     } else {
        h_Unlock_r(host);
@@ -948,8 +956,8 @@ h_TossStuff_r(register struct host *host)
            if (code < 0) {
                char hoststr[16];
                ViceLog(0,
-                       ("Warning: h_TossStuff_r failed: Host %s:%d client %x was locked.\n",
-                        afs_inet_ntoa_r(host->host, hoststr),
+                       ("Warning: h_TossStuff_r failed: Host %x (%s:%d) client %x was locked.\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr),
                         ntohs(host->port), client));
                return;
            }
@@ -957,8 +965,8 @@ h_TossStuff_r(register struct host *host)
            if (client->refCount) {
                char hoststr[16];
                ViceLog(0,
-                       ("Warning: h_TossStuff_r failed: Host %s:%d client %x refcount %d.\n",
-                        afs_inet_ntoa_r(host->host, hoststr),
+                       ("Warning: h_TossStuff_r failed: Host %x (%s:%d) client %x refcount %d.\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr),
                         ntohs(host->port), client, client->refCount));
                /* This is the same thing we do if the host is locked */
                ReleaseWriteLock(&client->lock);
@@ -980,7 +988,7 @@ h_TossStuff_r(register struct host *host)
     host->hostFlags &= ~CLIENTDELETED;
 
     if (host->hostFlags & HOSTDELETED) {
-       register struct h_hashChain **hp, *th;
+        register struct h_AddrHashChain **ahp, *ath;
        register struct rx_connection *rxconn;
        afsUUID *uuidp;
        struct AddrPort hostAddrPort;
@@ -1001,49 +1009,51 @@ h_TossStuff_r(register struct host *host)
 
        /* if alternate addresses do not exist */
        if (!(host->interface)) {
-           for (hp = &hostHashTable[h_HashIndex(host->host)]; (th = *hp);
-                hp = &th->next) {
-               assert(th->hostPtr);
-               if (th->hostPtr == host) {
-                   *hp = th->next;
-                   h_DeleteList_r(host);
-                   FreeHT(host);
-                   free(th);
+           for (ahp = &hostAddrHashTable[h_HashIndex(host->host)]; (ath = *ahp);
+                ahp = &ath->next) {
+               assert(ath->hostPtr);
+               if (ath->hostPtr == host) {
+                   *ahp = ath->next;
+                   free(ath);
                    break;
                }
            }
        } else {
-           /* delete all hash entries for the UUID */
+            register struct h_UuidHashChain **uhp, *uth;
+           /* delete the hash entry for the UUID */
            uuidp = &host->interface->uuid;
-           for (hp = &hostUuidHashTable[h_UuidHashIndex(uuidp)]; (th = *hp);
-                hp = &th->next) {
-               assert(th->hostPtr);
-               if (th->hostPtr == host) {
-                   *hp = th->next;
-                   free(th);
+           for (uhp = &hostUuidHashTable[h_UuidHashIndex(uuidp)]; (uth = *uhp);
+                uhp = &uth->next) {
+               assert(uth->hostPtr);
+               if (uth->hostPtr == host) {
+                   *uhp = uth->next;
+                   free(uth);
                    break;
                }
            }
-           /* delete all hash entries for alternate addresses */
-           assert(host->interface->numberOfInterfaces > 0);
+           /* delete the hash entry for each alternate addresses */
            for (i = 0; i < host->interface->numberOfInterfaces; i++) {
                hostAddrPort = host->interface->interface[i];
 
-               for (hp = &hostHashTable[h_HashIndex(hostAddrPort.addr)]; (th = *hp);
-                    hp = &th->next) {
-                   assert(th->hostPtr);
-                   if (th->hostPtr == host) {
-                       *hp = th->next;
-                       free(th);
+                if (!hostAddrPort.valid)
+                    continue;
+
+               for (ahp = &hostAddrHashTable[h_HashIndex(hostAddrPort.addr)]; (ath = *ahp);
+                    ahp = &ath->next) {
+                   assert(ath->hostPtr);
+                   if (ath->hostPtr == host) {
+                       *ahp = ath->next;
+                       free(ath);
                        break;
                    }
                }
            }
            free(host->interface);
            host->interface = NULL;
-           h_DeleteList_r(host);       /* remove host from global host List */
-           FreeHT(host);
        }                       /* if alternate address exists */
+
+        h_DeleteList_r(host);  /* remove host from global host List */
+        FreeHT(host);
     }
 }                              /*h_TossStuff_r */
 
@@ -1082,7 +1092,10 @@ h_Enumerate(int (*proc) (), char *param)
        if (!(held[count] = h_Held_r(host)))
            h_Hold_r(host);
     }
-    assert(count == hostCount);
+    if (count != hostCount) {
+       ViceLog(0, ("h_Enumerate found %d of %d hosts\n", count, hostCount));
+    }
+    assert(count <= hostCount);
     H_UNLOCK;
     for (i = 0; i < count; i++) {
        held[i] = (*proc) (list[i], held[i], param);
@@ -1133,17 +1146,24 @@ h_Enumerate_r(int (*proc) (), struct host *enumstart, char *param)
 }                              /*h_Enumerate_r */
 
 /* inserts a new HashChain structure corresponding to this UUID */
-static void
+void
 h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
 {
     int index;
-    struct h_hashChain *chain;
+    struct h_UuidHashChain *chain;
 
     /* hash into proper bucket */
     index = h_UuidHashIndex(uuid);
 
+    /* don't add the same entry multiple times */
+    for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
+       if (chain->hostPtr->interface && 
+           afs_uuid_equal(&chain->hostPtr->interface->uuid, uuid))
+           return;
+    }
+
     /* insert into beginning of list for this bucket */
-    chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
+    chain = (struct h_UuidHashChain *)malloc(sizeof(struct h_UuidHashChain));
     if (!chain) {
        ViceLog(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
        assert(0);
@@ -1156,39 +1176,50 @@ h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
 
 
 /* inserts a new HashChain structure corresponding to this address */
-static void
-h_AddHostToHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
+void
+h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
 {
     int index;
-    struct h_hashChain *chain;
+    struct h_AddrHashChain *chain;
+    int found = 0;
+    char hoststr[16];
 
     /* hash into proper bucket */
     index = h_HashIndex(addr);
 
     /* don't add the same entry multiple times */
-    for (chain = hostHashTable[index]; chain; chain = chain->next) {
-       if (chain->hostPtr == host && chain->addr == addr && chain->port == port)
-           return;
+    for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
+       if (chain->addr == addr && chain->port == port) {
+            if (chain->hostPtr == host)
+                found = 1;
+            else if (!(host->hostFlags & HOSTDELETED))
+                ViceLog(125, ("Addr %s:%d assigned to %x and %x.\n",
+                            afs_inet_ntoa_r(addr, hoststr), ntohs(port),
+                            host, chain));
+        }
     }
 
+    if (found)
+        return;
+
     /* insert into beginning of list for this bucket */
-    chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
+    chain = (struct h_AddrHashChain *)malloc(sizeof(struct h_AddrHashChain));
     if (!chain) {
-       ViceLog(0, ("Failed malloc in h_AddHostToHashTable_r\n"));
+       ViceLog(0, ("Failed malloc in h_AddHostToAddrHashTable_r\n"));
        assert(0);
     }
     chain->hostPtr = host;
-    chain->next = hostHashTable[index];
+    chain->next = hostAddrHashTable[index];
     chain->addr = addr;
     chain->port = port;
-    hostHashTable[index] = chain;
+    hostAddrHashTable[index] = chain;
 }
 
 /*
  * This is called with host locked and held. At this point, the
- * hostHashTable should not be having entries for the alternate
+ * hostAddrHashTable should not have entries for the alternate
  * interfaces. This function has to insert these entries in the
- * hostHashTable.
+ * hostAddrHashTable.
  *
  * All addresses are in network byte order.
  */
@@ -1204,10 +1235,6 @@ addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
     assert(host);
     assert(host->interface);
 
-    ViceLog(125, ("addInterfaceAddr : host %s:%d addr %s:%d\n", 
-                  afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), 
-                  afs_inet_ntoa_r(addr, hoststr2), ntohs(port)));
-
     /*
      * Make sure this address is on the list of known addresses
      * for this host.
@@ -1215,9 +1242,16 @@ addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
     number = host->interface->numberOfInterfaces;
     for (i = 0, found = 0; i < number && !found; i++) {
        if (host->interface->interface[i].addr == addr &&
-           host->interface->interface[i].port == port)
+             host->interface->interface[i].port == port) {
            found = 1;
+            host->interface->interface[i].valid = 1;
+        }
     }
+
+    ViceLog(125, ("addInterfaceAddr : host %s:%d addr %s:%d : found:%d\n", 
+                  afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), 
+                  afs_inet_ntoa_r(addr, hoststr2), ntohs(port), found));
+    
     if (!found) {
        interface = (struct Interface *)
            malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
@@ -1231,24 +1265,20 @@ addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
            interface->interface[i] = host->interface->interface[i];
        interface->interface[number].addr = addr;
        interface->interface[number].port = port;
+        interface->interface[number].valid = 1;
        free(host->interface);
        host->interface = interface;
     }
 
-    /*
-     * Create a hash table entry for this address
-     */
-    h_AddHostToHashTable_r(addr, port, host);
-
     return 0;
 }
 
 
 /*
  * This is called with host locked and held. At this point, the
- * hostHashTable should not be having entries for the alternate
+ * hostAddrHashTable should not be having entries for the alternate
  * interfaces. This function has to insert these entries in the
- * hostHashTable.
+ * hostAddrHashTable.
  *
  * All addresses are in network byte order.
  */
@@ -1274,18 +1304,17 @@ removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
      */
     interface = host->interface;
     number = host->interface->numberOfInterfaces;
-    for (i = 0, found = 0; i < number; i++) {
+    for (i = 0, found = 0; i < number && !found; i++) {
        if (interface->interface[i].addr == addr &&
            interface->interface[i].port == port) {
            found = 1;
-           break;
+            interface->interface[i].valid = 0;
        }
     }
     if (found) {
        number--;
        for (; i < number; i++) {
-           interface->interface[i].addr = interface->interface[i+1].addr;
-           interface->interface[i].port = interface->interface[i+1].port;
+           interface->interface[i] = interface->interface[i+1];
        }
        interface->numberOfInterfaces = number;
     }
@@ -1293,11 +1322,109 @@ removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
     /*
      * Remove the hash table entry for this address
      */
-    h_DeleteHostFromHashTableByAddr_r(addr, port, host);
+    h_DeleteHostFromAddrHashTable_r(addr, port, host);
 
     return 0;
 }
 
+/*
+ * This is called with host locked and held.  This function differs
+ * from removeInterfaceAddr_r in that it is called when the address
+ * is being removed from the host regardless of whether or not there
+ * is an interface list for the host.  This function will delete the
+ * host if there are no addresses left on it.
+ *
+ * All addresses are in network byte order.
+ */
+int
+removeAddress_r(struct host *host, afs_uint32 addr, afs_uint16 port)
+{
+    int i;
+    char hoststr[16], hoststr2[16];
+
+    if (!host->interface) {
+        if (host->host == addr && host->port == port) {
+            ViceLog(25,
+                    ("Removing only address for host %x (%s:%d), deleting host.\n",
+                     host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+            host->hostFlags |= HOSTDELETED;
+        }
+    } else {
+        removeInterfaceAddr_r(host, host->host, host->port);
+        if (host->interface->numberOfInterfaces == 0) {
+            ViceLog(25,
+                     ("Removed only address for host %x (%s:%d), no alternate interfaces, deleting host.\n",
+                       host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+            host->hostFlags |= HOSTDELETED;
+        } else {
+            struct rx_connection *rxconn;
+
+            rxconn = host->callback_rxcon;
+            host->callback_rxcon = NULL;
+
+            if (rxconn) {
+                struct client *client;
+                /*
+                * If rx_DestroyConnection calls h_FreeConnection we will
+                * deadlock on the host_glock_mutex. Work around the problem
+                * by unhooking the client from the connection before
+                * destroying the connection.
+                */
+                client = rx_GetSpecific(rxconn, rxcon_client_key);
+                rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
+                rx_DestroyConnection(rxconn);
+            }
+
+            for (i=0; i < host->interface->numberOfInterfaces; i++) {
+                if (host->interface->interface[i].valid) {
+                    ViceLog(25,
+                             ("Removed address for host %x (%s:%d), new primary interface %s:%d.\n",
+                               host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
+                               afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr2), 
+                               ntohs(host->interface->interface[i].port)));
+                    host->host = host->interface->interface[i].addr;
+                    host->port = host->interface->interface[i].port;
+                    h_AddHostToAddrHashTable_r(host->host, host->port, host);
+                    break;
+                }
+            }
+
+            if (i == host->interface->numberOfInterfaces) {
+                ViceLog(25,
+                         ("Removed only address for host %x (%s:%d), no valid alternate interfaces, deleting host.\n",
+                           host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+                host->hostFlags |= HOSTDELETED;
+            } else {
+                if (!sc)
+                    sc = rxnull_NewClientSecurityObject();
+                host->callback_rxcon =
+                    rx_NewConnection(host->host, host->port, 1, sc, 0);
+                rx_SetConnDeadTime(host->callback_rxcon, 50);
+                rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
+            }
+        }
+    }
+
+    return 0;
+}
+int 
+h_threadquota(int waiting) 
+{
+    if (lwps > 64) {
+       if (waiting > 5)
+           return 1;
+    } else if (lwps > 32) {
+       if (waiting > 4)
+           return 1;
+    } else if (lwps > 16) {
+       if (waiting > 3)
+           return 1;
+    } else {
+       if (waiting > 2)
+           return 1;
+    }
+    return 0;
+}
 
 /* Host is returned held */
 struct host *
@@ -1306,7 +1433,7 @@ h_GetHost_r(struct rx_connection *tcon)
     struct host *host;
     struct host *oldHost;
     int code;
-    int held, oheld;
+    int held;
     struct interfaceAddr interf;
     int interfValid = 0;
     struct Identity *identP = NULL;
@@ -1315,25 +1442,37 @@ h_GetHost_r(struct rx_connection *tcon)
     char hoststr[16], hoststr2[16];
     Capabilities caps;
     struct rx_connection *cb_conn = NULL;
+    struct rx_connection *cb_in = NULL;
 
     caps.Capabilities_val = NULL;
 
     haddr = rxr_HostOf(tcon);
     hport = rxr_PortOf(tcon);
   retry:
+    if (cb_in) {
+        rx_DestroyConnection(cb_in);
+        cb_in = NULL;
+    }
     if (caps.Capabilities_val)
        free(caps.Capabilities_val);
     caps.Capabilities_val = NULL;
     caps.Capabilities_len = 0;
 
     code = 0;
-    host = h_Lookup_r(haddr, hport, &held);
+    if (h_Lookup_r(haddr, hport, &held, &host))
+       return 0;
     identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
     if (host && !identP && !(host->Console & 1)) {
        /* This is a new connection, and we already have a host
         * structure for this address. Verify that the identity
         * of the caller matches the identity in the host structure.
         */
+       if ((host->hostFlags & HWHO_INPROGRESS) && 
+           h_threadquota(host->lock.num_waiting)) {
+           if (!held)
+               h_Release_r(host);
+           return 0;
+       }
        h_Lock_r(host);
        if (!(host->hostFlags & ALTADDR)) {
            /* Another thread is doing initialization */
@@ -1341,23 +1480,57 @@ h_GetHost_r(struct rx_connection *tcon)
            if (!held)
                h_Release_r(host);
            ViceLog(125,
-                   ("Host %s:%d starting h_Lookup again\n",
-                    afs_inet_ntoa_r(host->host, hoststr),
+                   ("Host %x (%s:%d) starting h_Lookup again\n",
+                    host, afs_inet_ntoa_r(host->host, hoststr),
                     ntohs(host->port)));
            goto retry;
        }
+       host->hostFlags |= HWHO_INPROGRESS;
        host->hostFlags &= ~ALTADDR;
+
+        /* We received a new connection from an IP address/port
+         * that is associated with 'host' but the address/port of
+         * the callback connection does not have to match it.
+         * If there is a match, we can use the existing callback
+         * connection to verify the UUID.  If they do not match
+         * we need to use a new callback connection to verify the
+         * UUID of the incoming caller and perhaps use the old 
+         * callback connection to verify that the old address/port
+         * is still valid.
+         */
+       
        cb_conn = host->callback_rxcon;
        rx_GetConnection(cb_conn);
        H_UNLOCK;
-       code =
-           RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
-       if (code == RXGEN_OPCODE)
-           code = RXAFSCB_WhoAreYou(cb_conn, &interf);
+        if (haddr == host->host && hport == host->port) {
+            /* The existing callback connection matches the 
+             * incoming connection so just use it.
+             */
+           code =
+               RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
+           if (code == RXGEN_OPCODE)
+               code = RXAFSCB_WhoAreYou(cb_conn, &interf);
+       } else {
+            /* We do not have a match.  Create a new connection
+             * for the new addr/port and use multi_Rx to probe
+             * both of them simultaneously.
+             */
+           if (!sc)
+                sc = rxnull_NewClientSecurityObject();
+            cb_in = rx_NewConnection(haddr, hport, 1, sc, 0);
+            rx_SetConnDeadTime(cb_in, 50);
+            rx_SetConnHardDeadTime(cb_in, AFS_HARDDEADTIME);
+           
+            code =
+                RXAFSCB_TellMeAboutYourself(cb_in, &interf, &caps);
+           if (code == RXGEN_OPCODE)
+                code = RXAFSCB_WhoAreYou(cb_in, &interf);
+       }
        rx_PutConnection(cb_conn);
        cb_conn=NULL;
        H_LOCK;
-       if (code == RXGEN_OPCODE) {
+       if ((code == RXGEN_OPCODE) || 
+           ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
            identP = (struct Identity *)malloc(sizeof(struct Identity));
            if (!identP) {
                ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
@@ -1365,21 +1538,39 @@ h_GetHost_r(struct rx_connection *tcon)
            }
            identP->valid = 0;
            rx_SetSpecific(tcon, rxcon_ident_key, identP);
-           /* The host on this connection was unable to respond to 
-            * the WhoAreYou. We will treat this as a new connection
-            * from the existing host. The worst that can happen is
-            * that we maintain some extra callback state information */
-           if (host->interface) {
-               ViceLog(0,
-                       ("Host %s:%d used to support WhoAreYou, deleting.\n",
-                        afs_inet_ntoa_r(host->host, hoststr),
-                        ntohs(host->port)));
-               host->hostFlags |= HOSTDELETED;
-               h_Unlock_r(host);
+           if (cb_in == NULL) {
+               /* The host on this connection was unable to respond to 
+                * the WhoAreYou. We will treat this as a new connection
+                * from the existing host. The worst that can happen is
+                * that we maintain some extra callback state information */
+               if (host->interface) {
+                   ViceLog(0,
+                           ("Host %x (%s:%d) used to support WhoAreYou, deleting.\n",
+                            host, 
+                            afs_inet_ntoa_r(host->host, hoststr),
+                            ntohs(host->port)));
+                   host->hostFlags |= HOSTDELETED;
+                   host->hostFlags &= ~HWHO_INPROGRESS;
+                   h_Unlock_r(host);
+                   if (!held)
+                       h_Release_r(host);
+                   host = NULL;
+                   goto retry;
+               }
+           } else {
+               /* The incoming connection does not support WhoAreYou but
+                * the original one might have.  Use removeAddress_r() to
+                 * remove this addr/port from the host that was found.
+                 * If there are no more addresses left for the host it 
+                 * will be deleted.  Then we retry.
+                 */
+                removeAddress_r(host, haddr, hport);
+                host->hostFlags &= ~HWHO_INPROGRESS;
+                h_Unlock_r(host);
                if (!held)
-                   h_Release_r(host);
-               host = NULL;
-               goto retry;
+                    h_Release_r(host);
+                host = NULL;
+                goto retry;
            }
        } else if (code == 0) {
            interfValid = 1;
@@ -1396,22 +1587,102 @@ h_GetHost_r(struct rx_connection *tcon)
             * then this is not the same host as before. */
            if (!host->interface
                || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
-               ViceLog(25,
-                       ("Host %s:%d has changed its identity, deleting.\n",
-                        afs_inet_ntoa_r(host->host, hoststr), host->port));
-               host->hostFlags |= HOSTDELETED;
+               if (cb_in) {
+                    ViceLog(25,
+                           ("Uuid doesn't match connection (%s:%d).\n",
+                            afs_inet_ntoa_r(haddr, hoststr), ntohs(hport)));
+                   
+                    removeAddress_r(host, haddr, hport);
+               } else {
+                   ViceLog(25,
+                           ("Uuid doesn't match host %x (%s:%d).\n",
+                            host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+                   
+                   removeAddress_r(host, host->host, host->port);
+               }
+               host->hostFlags &= ~HWHO_INPROGRESS;
                h_Unlock_r(host);
                if (!held)
                    h_Release_r(host);
                host = NULL;
                goto retry;
+           } else if (cb_in) {
+               /* the UUID matched the client at the incoming addr/port 
+                 * but this is not the address of the active callback 
+                 * connection.  Try that connection and see if the client
+                 * is still there and if the reported UUID is the same.
+                 */
+                int code2;
+                afsUUID uuid = host->interface->uuid;
+                cb_conn = host->callback_rxcon;
+                rx_GetConnection(cb_conn);
+                rx_SetConnDeadTime(cb_conn, 2);
+                rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
+                H_UNLOCK;
+                code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
+                H_LOCK;
+                rx_SetConnDeadTime(cb_conn, 50);
+                rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
+                rx_PutConnection(cb_conn);
+                cb_conn=NULL;
+                if (code2) {
+                    /* The primary address is either not responding or
+                     * is not the client we are looking for.  Need to
+                     * remove the primary address and add swap in the new 
+                     * callback connection, and destroy the old one.
+                     */
+                    struct rx_connection *rxconn;
+                    ViceLog(0,("CB: ProbeUuid for host %x (%s:%d) failed %d\n",
+                              host, 
+                              afs_inet_ntoa_r(host->host, hoststr),
+                              ntohs(host->port),code2));
+                   
+                    removeInterfaceAddr_r(host, host->host, host->port);
+                    addInterfaceAddr_r(host, haddr, hport);
+                    host->host = haddr;
+                    host->port = hport;
+                    rxconn = host->callback_rxcon;
+                    host->callback_rxcon = cb_in;
+                    cb_in = NULL;
+                   
+                    if (rxconn) {
+                        struct client *client;
+                        /*
+                         * If rx_DestroyConnection calls h_FreeConnection we will
+                        * deadlock on the host_glock_mutex. Work around the problem
+                         * by unhooking the client from the connection before
+                         * destroying the connection.
+                         */
+                        client = rx_GetSpecific(rxconn, rxcon_client_key);
+                        rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
+                        rx_DestroyConnection(rxconn);
+                   }
+               }
            }
        } else {
-           afs_inet_ntoa_r(host->host, hoststr);
-           ViceLog(0,
-                   ("CB: WhoAreYou failed for %s:%d, error %d\n", hoststr,
-                    ntohs(host->port), code));
-           host->hostFlags |= VENUSDOWN;
+            if (cb_in) {
+                /* A callback to the incoming connection address is failing.  
+                 * Assume that the addr/port is no longer associated with the host
+                 * returned by h_Lookup_r.
+                 */
+                ViceLog(0,
+                       ("CB: WhoAreYou failed for connection (%s:%d) , error %d\n",
+                        afs_inet_ntoa_r(haddr, hoststr), ntohs(hport), code));
+                removeAddress_r(host, haddr, hport);
+                host->hostFlags &= ~HWHO_INPROGRESS;
+                h_Unlock_r(host);
+                if (!held)
+                    h_Release_r(host);
+                host = NULL;
+                rx_DestroyConnection(cb_in);
+                return 0;
+           } else {
+               ViceLog(0,
+                       ("CB: WhoAreYou failed for host %x (%s:%d), error %d\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr),
+                        ntohs(host->port), code));
+               host->hostFlags |= VENUSDOWN;
+           }
        }
        if (caps.Capabilities_val
            && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
@@ -1419,21 +1690,22 @@ h_GetHost_r(struct rx_connection *tcon)
        else
            host->hostFlags &= ~(HERRORTRANS);
        host->hostFlags |= ALTADDR;
+       host->hostFlags &= ~HWHO_INPROGRESS;
        h_Unlock_r(host);
     } else if (host) {
        if (!(host->hostFlags & ALTADDR)) {
            /* another thread is doing the initialisation */
            ViceLog(125,
-                   ("Host %s:%d waiting for host-init to complete\n",
-                    afs_inet_ntoa_r(host->host, hoststr),
+                   ("Host %x (%s:%d) waiting for host-init to complete\n",
+                    host, afs_inet_ntoa_r(host->host, hoststr),
                     ntohs(host->port)));
            h_Lock_r(host);
            h_Unlock_r(host);
            if (!held)
                h_Release_r(host);
            ViceLog(125,
-                   ("Host %s:%d starting h_Lookup again\n",
-                    afs_inet_ntoa_r(host->host, hoststr),
+                   ("Host %x (%s:%d) starting h_Lookup again\n",
+                    host, afs_inet_ntoa_r(host->host, hoststr),
                     ntohs(host->port)));
            goto retry;
        }
@@ -1452,13 +1724,14 @@ h_GetHost_r(struct rx_connection *tcon)
            if (host->interface)
                afsUUID_to_string(&host->interface->uuid, uuid2, 127);
            ViceLog(0,
-                   ("CB: new identity for host %s:%d, deleting(%x %x %s %s)\n",
-                    afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
+                   ("CB: new identity for host %x (%s:%d), deleting(%x %x %s %s)\n",
+                    host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
                     identP->valid, host->interface,
-                    identP->valid ? uuid1 : "",
-                    host->interface ? uuid2 : ""));
+                    identP->valid ? uuid1 : "no_uuid",
+                    host->interface ? uuid2 : "no_uuid"));
 
            /* The host in the cache is not the host for this connection */
+            h_Lock_r(host);
            host->hostFlags |= HOSTDELETED;
            h_Unlock_r(host);
            if (!held)
@@ -1472,6 +1745,7 @@ h_GetHost_r(struct rx_connection *tcon)
            int pident = 0;
            cb_conn = host->callback_rxcon;
            rx_GetConnection(cb_conn);
+           host->hostFlags |= HWHO_INPROGRESS;
            H_UNLOCK;
            code =
                RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
@@ -1480,7 +1754,8 @@ h_GetHost_r(struct rx_connection *tcon)
            rx_PutConnection(cb_conn);
            cb_conn=NULL;
            H_LOCK;
-           if (code == RXGEN_OPCODE) {
+           if ((code == RXGEN_OPCODE) || 
+               ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
                if (!identP)
                    identP =
                        (struct Identity *)malloc(sizeof(struct Identity));
@@ -1495,8 +1770,8 @@ h_GetHost_r(struct rx_connection *tcon)
                if (!pident)
                    rx_SetSpecific(tcon, rxcon_ident_key, identP);
                ViceLog(25,
-                       ("Host %s:%d does not support WhoAreYou.\n",
-                        afs_inet_ntoa_r(host->host, hoststr),
+                       ("Host %x (%s:%d) does not support WhoAreYou.\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr),
                         ntohs(host->port)));
                code = 0;
            } else if (code == 0) {
@@ -1516,8 +1791,8 @@ h_GetHost_r(struct rx_connection *tcon)
                if (!pident)
                    rx_SetSpecific(tcon, rxcon_ident_key, identP);
                ViceLog(25,
-                       ("WhoAreYou success on %s:%d\n",
-                        afs_inet_ntoa_r(host->host, hoststr),
+                       ("WhoAreYou success on host %x (%s:%d)\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr),
                         ntohs(host->port)));
            }
            if (code == 0 && !identP->valid) {
@@ -1533,9 +1808,10 @@ h_GetHost_r(struct rx_connection *tcon)
                 if (oldHost) {
                     int probefail = 0;
 
-                   if (!(oheld = h_Held_r(oldHost)))
+                   if (!h_Held_r(oldHost))
                        h_Hold_r(oldHost);
                    h_Lock_r(oldHost);
+                   oldHost->hostFlags |= HWHO_INPROGRESS;
 
                     if (oldHost->interface) {
                        int code2;
@@ -1557,8 +1833,9 @@ h_GetHost_r(struct rx_connection *tcon)
                             * MultiProbeAlternateAddress_r() will remove the
                             * alternate interfaces that do not have the same
                             * Uuid. */
-                           ViceLog(0,("CB: ProbeUuid for %s:%d failed %d\n",
-                                        afs_inet_ntoa_r(oldHost->host, hoststr),
+                           ViceLog(0,("CB: ProbeUuid for host %x (%s:%d) failed %d\n",
+                                        oldHost, 
+                                         afs_inet_ntoa_r(oldHost->host, hoststr),
                                         ntohs(oldHost->port),code2));
                            MultiProbeAlternateAddress_r(oldHost);
                             probefail = 1;
@@ -1570,15 +1847,20 @@ h_GetHost_r(struct rx_connection *tcon)
                    /* This is a new address for an existing host. Update
                     * the list of interfaces for the existing host and
                     * delete the host structure we just allocated. */
+
+                    /* prevent warnings while manipulating interface lists */
+                   host->hostFlags |= HOSTDELETED;
+
                    if (oldHost->host != haddr || oldHost->port != hport) {
                        struct rx_connection *rxconn;
 
                        ViceLog(25,
-                               ("CB: new addr %s:%d for old host %s:%d\n",
-                                 afs_inet_ntoa_r(haddr, hoststr),
-                                 ntohs(hport), 
-                                 afs_inet_ntoa_r(oldHost->host, hoststr2),
-                                 ntohs(oldHost->port)));
+                                 ("CB: Host %x (%s:%d) has new addr %s:%d\n",
+                                   oldHost, 
+                                   afs_inet_ntoa_r(oldHost->host, hoststr2),
+                                   ntohs(oldHost->port),
+                                   afs_inet_ntoa_r(haddr, hoststr),
+                                   ntohs(hport)));
                        if (probefail || oldHost->host == haddr) {
                            /* The probe failed which means that the old address is 
                             * either unreachable or is not the same host we were just
@@ -1625,7 +1907,7 @@ h_GetHost_r(struct rx_connection *tcon)
                            rx_DestroyConnection(rxconn);
                        }
                    }
-                   host->hostFlags |= HOSTDELETED;
+                   host->hostFlags &= ~HWHO_INPROGRESS;
                    h_Unlock_r(host);
                    /* release host because it was allocated by h_Alloc_r */
                    h_Release_r(host);
@@ -1645,8 +1927,8 @@ h_GetHost_r(struct rx_connection *tcon)
                    H_LOCK;
                    if (code == 0) {
                        ViceLog(25,
-                               ("InitCallBackState3 success on %s:%d\n",
-                                afs_inet_ntoa_r(host->host, hoststr),
+                               ("InitCallBackState3 success on host %x (%s:%d)\n",
+                                host, afs_inet_ntoa_r(host->host, hoststr),
                                 ntohs(host->port)));
                        assert(interfValid == 1);
                        initInterfaceAddr_r(host, &interf);
@@ -1654,15 +1936,14 @@ h_GetHost_r(struct rx_connection *tcon)
                }
            }
            if (code) {
-               afs_inet_ntoa_r(host->host, hoststr);
                ViceLog(0,
-                       ("CB: RCallBackConnectBack failed for %s:%d\n",
-                        hoststr, ntohs(host->port)));
+                       ("CB: RCallBackConnectBack failed for %x (%s:%d)\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
                host->hostFlags |= VENUSDOWN;
            } else {
                ViceLog(125,
-                       ("CB: RCallBackConnectBack succeeded for %s:%d\n",
-                        hoststr, ntohs(host->port)));
+                       ("CB: RCallBackConnectBack succeeded for %x (%s:%d)\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
                host->hostFlags |= RESETDONE;
            }
        }
@@ -1672,6 +1953,7 @@ h_GetHost_r(struct rx_connection *tcon)
        else
            host->hostFlags &= ~(HERRORTRANS);
        host->hostFlags |= ALTADDR;     /* host structure initialization complete */
+       host->hostFlags &= ~HWHO_INPROGRESS;
        h_Unlock_r(host);
     }
     if (caps.Capabilities_val)
@@ -1691,6 +1973,7 @@ int  num_lrealms = -1;
 void
 h_InitHostPackage()
 {
+    memset(&nulluuid, 0, sizeof(afsUUID));
     afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
     if (num_lrealms == -1) {
        int i;
@@ -1921,6 +2204,9 @@ h_FindClient_r(struct rx_connection *tcon)
     if (!client) { /* loop */
        host = h_GetHost_r(tcon);       /* Returns it h_Held */
 
+       if (!host) 
+           return 0;
+
     retryfirstclient:
        /* First try to find the client structure */
        for (client = host->FirstClient; client; client = client->next) {
@@ -1983,9 +2269,9 @@ h_FindClient_r(struct rx_connection *tcon)
            if (code) {
                char hoststr[16];
                ViceLog(0,
-                       ("pr_GetCPS failed(%d) for user %d, host %s:%d\n",
-                        code, viceid, afs_inet_ntoa_r(client->host->host,
-                                                      hoststr),
+                       ("pr_GetCPS failed(%d) for user %d, host %x (%s:%d)\n",
+                        code, viceid, client->host, 
+                         afs_inet_ntoa_r(client->host->host,hoststr),
                         ntohs(client->host->port)));
 
                /* Although ubik_Call (called by pr_GetCPS) traverses thru
@@ -2090,14 +2376,16 @@ int
 GetClient(struct rx_connection *tcon, struct client **cp)
 {
     register struct client *client;
+    char hoststr[16];
 
     H_LOCK;
     *cp = NULL;
     client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
     if (client == NULL) {
        ViceLog(0,
-               ("GetClient: no client in conn %x (host %x:%d), VBUSYING\n",
-                tcon, rxr_HostOf(tcon),ntohs(rxr_PortOf(tcon))));
+               ("GetClient: no client in conn %x (host %s:%d), VBUSYING\n",
+                tcon, afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
+                 ntohs(rxr_PortOf(tcon))));
        H_UNLOCK;
        return VBUSY;
     }
@@ -2109,7 +2397,6 @@ GetClient(struct rx_connection *tcon, struct client **cp)
        return VBUSY;
     }
     if (client && client->LastCall > client->expTime && client->expTime) {
-       char hoststr[16];
        ViceLog(1,
                ("Token for %s at %s:%d expired %d\n", h_UserName(client),
                 afs_inet_ntoa_r(client->host->host, hoststr),
@@ -2145,7 +2432,6 @@ h_UserName(struct client *client)
     static char User[PR_MAXNAMELEN + 1];
     namelist lnames;
     idlist lids;
-    afs_int32 code;
 
     lids.idlist_len = 1;
     lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
@@ -2165,7 +2451,6 @@ h_UserName(struct client *client)
     free(lids.idlist_val);
     free(lnames.namelist_val);
     return User;
-
 }                              /*h_UserName */
 
 
@@ -2496,12 +2781,12 @@ h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32
 {
     int ret = 0, found = 0;
     struct host *host = NULL;
-    struct h_hashChain *chain;
+    struct h_AddrHashChain *chain;
     int index = h_HashIndex(addr);
     char tmp[16];
     int chain_len = 0;
 
-    for (chain = hostHashTable[index]; chain; chain = chain->next) {
+    for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
        host = chain->hostPtr;
        if (host == NULL) {
            afs_inet_ntoa_r(addr, tmp);
@@ -2548,7 +2833,7 @@ h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
 {
     int ret = 0, found = 0;
     struct host *host = NULL;
-    struct h_hashChain *chain;
+    struct h_UuidHashChain *chain;
     afsUUID * uuidp = &h->interface->uuid;
     int index = h_UuidHashIndex(uuidp);
     char tmp[40];
@@ -2788,13 +3073,10 @@ h_stateRestoreHost(struct fs_dump_state * state)
     if (ifp) {
        int i;
        for (i = ifp->numberOfInterfaces-1; i >= 0; i--) {
-           h_AddHostToHashTable_r(ifp->interface[i].addr, 
-                                  ifp->interface[i].port, host);
        }
        h_AddHostToUuidHashTable_r(&ifp->uuid, host);
-    } else {
-       h_AddHostToHashTable_r(host->host, host->port, host);
     }
+    h_AddHostToAddrHashTable_r(host->host, host->port, host);
     h_InsertList_r(host);
 
     /* setup host id map entry */
@@ -3116,6 +3398,7 @@ CheckHost(register struct host *host, int held)
     }
     if (host->LastCall < checktime) {
        h_Lock_r(host);
+       host->hostFlags |= HWHO_INPROGRESS;
        if (!(host->hostFlags & HOSTDELETED)) {
            cb_conn = host->callback_rxcon;
            rx_GetConnection(cb_conn);
@@ -3185,6 +3468,7 @@ CheckHost(register struct host *host, int held)
            cb_conn=NULL;
            H_LOCK;
        }
+       host->hostFlags &= ~HWHO_INPROGRESS;
        h_Unlock_r(host);
     }
     H_UNLOCK;
@@ -3192,6 +3476,107 @@ CheckHost(register struct host *host, int held)
 
 }                              /*CheckHost */
 
+int
+CheckHost_r(register struct host *host, int held, char *dummy)
+{
+    register struct client *client;
+    struct rx_connection *cb_conn = NULL;
+    int code;
+
+#ifdef AFS_DEMAND_ATTACH_FS
+    /* kill the checkhost lwp ASAP during shutdown */
+    FS_STATE_RDLOCK;
+    if (fs_state.mode == FS_MODE_SHUTDOWN) {
+       FS_STATE_UNLOCK;
+       return H_ENUMERATE_BAIL(held);
+    }
+    FS_STATE_UNLOCK;
+#endif
+
+    /* Host is held by h_Enumerate_r */
+    for (client = host->FirstClient; client; client = client->next) {
+       if (client->refCount == 0 && client->LastCall < clientdeletetime) {
+           client->deleted = 1;
+           host->hostFlags |= CLIENTDELETED;
+       }
+    }
+    if (host->LastCall < checktime) {
+       h_Lock_r(host);
+       if (!(host->hostFlags & HOSTDELETED)) {
+           cb_conn = host->callback_rxcon;
+           rx_GetConnection(cb_conn);
+           if (host->LastCall < clientdeletetime) {
+               host->hostFlags |= HOSTDELETED;
+               if (!(host->hostFlags & VENUSDOWN)) {
+                   host->hostFlags &= ~ALTADDR;        /* alternate address invalid */
+                   if (host->interface) {
+                       H_UNLOCK;
+                       code =
+                           RXAFSCB_InitCallBackState3(cb_conn,
+                                                      &FS_HostUUID);
+                       H_LOCK;
+                   } else {
+                       H_UNLOCK;
+                       code =
+                           RXAFSCB_InitCallBackState(cb_conn);
+                       H_LOCK;
+                   }
+                   host->hostFlags |= ALTADDR; /* alternate addresses valid */
+                   if (code) {
+                       char hoststr[16];
+                       (void)afs_inet_ntoa_r(host->host, hoststr);
+                       ViceLog(0,
+                               ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
+                                hoststr, ntohs(host->port)));
+                       host->hostFlags |= VENUSDOWN;
+                   }
+                   /* Note:  it's safe to delete hosts even if they have call
+                    * back state, because break delayed callbacks (called when a
+                    * message is received from the workstation) will always send a 
+                    * break all call backs to the workstation if there is no
+                    *callback.
+                    */
+               }
+           } else {
+               if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
+                   char hoststr[16];
+                   (void)afs_inet_ntoa_r(host->host, hoststr);
+                   if (host->interface) {
+                       afsUUID uuid = host->interface->uuid;
+                       H_UNLOCK;
+                       code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
+                       H_LOCK;
+                       if (code) {
+                           if (MultiProbeAlternateAddress_r(host)) {
+                               ViceLog(0,("CheckHost_r: Probing all interfaces of host %s:%d failed, code %d\n",
+                                           hoststr, ntohs(host->port), code));
+                               host->hostFlags |= VENUSDOWN;
+                           }
+                       }
+                   } else {
+                       H_UNLOCK;
+                       code = RXAFSCB_Probe(cb_conn);
+                       H_LOCK;
+                       if (code) {
+                           ViceLog(0,
+                                   ("CheckHost_r: Probe failed for host %s:%d, code %d\n", 
+                                    hoststr, ntohs(host->port), code));
+                           host->hostFlags |= VENUSDOWN;
+                       }
+                   }
+               }
+           }
+           H_UNLOCK;
+           rx_PutConnection(cb_conn);
+           cb_conn=NULL;
+           H_LOCK;
+       }
+       h_Unlock_r(host);
+    }
+    return held;
+
+}                              /*CheckHost_r */
+
 
 /*
  * Set VenusDown for any hosts that have not had a call in 15 minutes and
@@ -3214,15 +3599,17 @@ h_CheckHosts(void)
      */
     checktime = now - 15 * 60;
     clientdeletetime = now - 120 * 60; /* 2 hours ago */
-    h_Enumerate(CheckHost, NULL);
-
+    
+    H_LOCK;
+    h_Enumerate_r(CheckHost_r, hostList, NULL);
+    H_UNLOCK;
 }                              /*h_CheckHosts */
 
 /*
  * This is called with host locked and held. At this point, the
- * hostHashTable should not have any entries for the alternate
+ * hostAddrHashTable should not have any entries for the alternate
  * interfaces. This function has to insert these entries in the
- * hostHashTable.
+ * hostAddrHashTable.
  *
  * The addresses in the interfaceAddr list are in host byte order.
  */
@@ -3236,6 +3623,8 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
     int found;
     struct Interface *interface;
     char hoststr[16];
+    char uuidstr[128];
+    afs_uint16 port7001 = htons(7001);
 
     assert(host);
     assert(interf);
@@ -3256,8 +3645,39 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
     }
 
     /*
-     * Convert IP addresses to network byte order, and remove for
-     * duplicate IP addresses from the interface list.
+     * The client's notion of its own IP addresses is not reliable.  
+     *
+     * 1. The client list might contain private address ranges which
+     *    are likely to be re-used by many clients allocated addresses
+     *    by a NAT.
+     *
+     * 2. The client list will not include any public addresses that
+     *    are hidden by a NAT.
+     *
+     * 3. Private address ranges that are exposed to the server will
+     *    be obtained from the rx connections that use them.
+     *
+     * 4. Lists provided by the client are not necessarily truthful.
+     *    Many existing clients (UNIX) do not refresh the IP address
+     *    list as the actual assigned addresses change.  The end result
+     *    is that they report the initial address list for the lifetime
+     *    of the process.  In other words, a client can report addresses
+     *    that they are in fact not using.  Adding these addresses to
+     *    the host interface list without verification is not only
+     *    pointless, it is downright dangerous.
+     *
+     * We therefore do not add alternate addresses to the addr hash table.
+     * We only use them for multi-rx callback breaks.
+     */
+
+    /*
+     * Convert IP addresses to network byte order, and remove
+     * duplicate IP addresses from the interface list, and 
+     * determine whether or not the incoming addr/port is 
+     * listed.  Note that if the address matches it is not
+     * truly a match because the port number for the entries
+     * in the interface list are port 7001 and the port number
+     * for this connection might not be 7001.
      */
     for (i = 0, count = 0, found = 0; i < number; i++) {
        interf->addr_in[i] = htonl(interf->addr_in[i]);
@@ -3267,7 +3687,8 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
        }
        if (j == count) {
            interf->addr_in[count] = interf->addr_in[i];
-           if (interf->addr_in[count] == myAddr)
+           if (interf->addr_in[count] == myAddr &&
+                port7001 == myPort)
                found = 1;
            count++;
        }
@@ -3281,31 +3702,42 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
            malloc(sizeof(struct Interface) +
                   (sizeof(struct AddrPort) * (count - 1)));
        if (!interface) {
-           ViceLog(0, ("Failed malloc in initInterfaceAddr_r\n"));
+           ViceLog(0, ("Failed malloc in initInterfaceAddr_r 1\n"));
            assert(0);
        }
        interface->numberOfInterfaces = count;
     } else {
        interface = (struct Interface *)
            malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
-       assert(interface);
+       if (!interface) {
+           ViceLog(0, ("Failed malloc in initInterfaceAddr_r 2\n"));
+           assert(0);
+       }
        interface->numberOfInterfaces = count + 1;
        interface->interface[count].addr = myAddr;
        interface->interface[count].port = myPort;
+        interface->interface[count].valid = 1;
     }
-    interface->uuid = interf->uuid;
+
     for (i = 0; i < count; i++) {
-       interface->interface[i].addr = interf->addr_in[i];
+
+        interface->interface[i].addr = interf->addr_in[i];
        /* We store the port as 7001 because the addresses reported by 
         * TellMeAboutYourself and WhoAreYou RPCs are only valid if they
         * are coming from fully connected hosts (no NAT/PATs)
         */
-       interface->interface[i].port = htons(7001);
+       interface->interface[i].port = port7001;
+        interface->interface[i].valid = 1;      /* valid until a conflict is found */
     }
 
+    interface->uuid = interf->uuid;
+
     assert(!host->interface);
     host->interface = interface;
 
+    afsUUID_to_string(&interface->uuid, uuidstr, 127);
+
+    ViceLog(125, ("--- uuid %s\n", uuidstr));
     for (i = 0; i < host->interface->numberOfInterfaces; i++) {
        ViceLog(125, ("--- alt address %s:%d\n", 
                       afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
@@ -3317,13 +3749,13 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
 
 /* deleted a HashChain structure for this address and host */
 /* returns 1 on success */
-static int
-h_DeleteHostFromHashTableByAddr_r(afs_uint32 addr, afs_uint16 port, struct host *host)
+int
+h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
 {
     int flag;
-    register struct h_hashChain **hp, *th;
+    register struct h_AddrHashChain **hp, *th;
 
-    for (hp = &hostHashTable[h_HashIndex(addr)]; (th = *hp);) {
+    for (hp = &hostAddrHashTable[h_HashIndex(addr)]; (th = *hp);) {
        assert(th->hostPtr);
        if (th->hostPtr == host && th->addr == addr && th->port == port) {
            *hp = th->next;
@@ -3351,10 +3783,13 @@ printInterfaceAddr(struct host *host, int level)
     if (host->interface) {
        /* check alternate addresses */
        number = host->interface->numberOfInterfaces;
-       assert(number > 0);
-       for (i = 0; i < number; i++)
-           ViceLog(level, ("%s:%d ", afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
-                            ntohs(host->interface->interface[i].port)));
+        if (number == 0)
+            ViceLog(level, ("no-addresses "));
+        else {
+            for (i = 0; i < number; i++)
+                ViceLog(level, ("%s:%d ", afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
+                                ntohs(host->interface->interface[i].port)));
+        }
     }
     ViceLog(level, ("\n"));
 }