--- openafs/src/rx/rx.c	10 Sep 2002 22:33:54 -0000	1.22.2.13
+++ openafs/src/rx/rx.c	18 Apr 2003 19:43:20 -0000
@@ -2247,7 +2247,6 @@
 {
     int hashindex, flag;
     register struct rx_connection *conn;
-    struct rx_peer *peer;
     hashindex = CONN_HASH(host, port, cid, epoch, type);
     MUTEX_ENTER(&rx_connHashTable_lock);
     rxLastConn ? (conn = rxLastConn, flag = 0) :
@@ -2264,13 +2263,12 @@
 	    MUTEX_EXIT(&rx_connHashTable_lock);
 	    return (struct rx_connection *) 0;
 	}
-	/* epoch's high order bits mean route for security reasons only on
-	 * the cid, not the host and port fields.
-	 */
-	if (conn->epoch & 0x80000000) break;
-	if (((type == RX_CLIENT_CONNECTION) 
-	     || (pp->host == host)) && (pp->port == port))
-	  break;
+	if (pp->host == host && pp->port == port)
+	    break;
+	if (type == RX_CLIENT_CONNECTION && pp->port == port)
+	    break;
+	if (type == RX_CLIENT_CONNECTION && (conn->epoch & 0x80000000))
+	    break;
       }
       if ( !flag )
       {
@@ -2302,7 +2300,7 @@
 	CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
 	conn->next = rx_connHashTable[hashindex];
 	rx_connHashTable[hashindex] = conn;
-	peer = conn->peer = rxi_FindPeer(host, port, 0, 1);
+	conn->peer = rxi_FindPeer(host, port, 0, 1);
 	conn->type = RX_SERVER_CONNECTION;
 	conn->lastSendTime = clock_Sec();   /* don't GC immediately */
 	conn->epoch = epoch;
@@ -2325,27 +2323,9 @@
 	rx_stats.nServerConns++;
 	MUTEX_EXIT(&rx_stats_mutex);
     }
-    else
-    {
-    /* Ensure that the peer structure is set up in such a way that
-    ** replies in this connection go back to that remote interface
-    ** from which the last packet was sent out. In case, this packet's
-    ** source IP address does not match the peer struct for this conn,
-    ** then drop the refCount on conn->peer and get a new peer structure.
-    ** We can check the host,port field in the peer structure without the
-    ** rx_peerHashTable_lock because the peer structure has its refCount
-    ** incremented and the only time the host,port in the peer struct gets
-    ** updated is when the peer structure is created.
-    */
-	if (conn->peer->host == host )
-		peer = conn->peer; /* no change to the peer structure */
-	else
-        	peer = rxi_FindPeer(host, port, conn->peer, 1);
-    }
 
     MUTEX_ENTER(&conn->conn_data_lock);
     conn->refCount++;
-    conn->peer = peer;
     MUTEX_EXIT(&conn->conn_data_lock);
 
     rxLastConn = conn;	/* store this connection as the last conn used */
--- openafs/src/viced/host.c	21 Aug 2002 21:01:55 -0000	1.7.2.7
+++ openafs/src/viced/host.c	18 Apr 2003 19:43:20 -0000
@@ -1293,7 +1293,7 @@
        h_Hold_r(client->host);
        if (client->prfail != 2) {  /* Could add shared lock on client here */
 	  /* note that we don't have to lock entry in this path to
-	   * ensure CPS is initialized, since we don't call rxr_SetSpecific
+	   * ensure CPS is initialized, since we don't call rx_SetSpecific
 	   * until initialization is done, and we only get here if
 	   * rx_GetSpecific located the client structure.
 	   */
@@ -1362,6 +1362,16 @@
 	     if (client->tcon && (client->tcon != tcon)) {
 	        ViceLog(0, ("*** Vid=%d, sid=%x, tcon=%x, Tcon=%x ***\n", 
 			    client->ViceId, client->sid, client->tcon, tcon));
+		oldClient = (struct client *) rx_GetSpecific(client->tcon,
+							     rxcon_client_key);
+		if (oldClient) {
+		    if (oldClient == client)
+			rx_SetSpecific(client->tcon, rxcon_client_key, NULL);
+		    else
+			ViceLog(0,
+			    ("Client-conn mismatch: CL1=%x, CN=%x, CL2=%x\n",
+			     client, client->tcon, oldClient));
+		}
 		client->tcon = (struct rx_connection *)0;
 	     }
 	     client->refCount++;
@@ -1445,7 +1455,8 @@
      * required).  So, before setting the RPC's rock, we should disconnect
      * the RPC from the other client structure's rock.
      */
-    if (oldClient = (struct client *) rx_GetSpecific(tcon, rxcon_client_key)) {
+    oldClient = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
+    if (oldClient && oldClient->tcon == tcon) {
 	oldClient->tcon = (struct rx_connection *) 0;
 	/* rx_SetSpecific will be done immediately below */
     }
@@ -1483,8 +1494,28 @@
     H_LOCK
 
     *cp = client = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
-    /* XXXX debug */
-    assert(client && client->tcon && rxr_CidOf(client->tcon) == client->sid);
+    if (client == NULL || client->tcon == NULL) {
+	ViceLog(0, ("GetClient: no client in conn %x (host %x), VBUSYING\n", 
+		    tcon, rx_HostOf(rx_PeerOf(tcon))));
+	H_UNLOCK
+	return VBUSY;
+    }
+    if (rxr_CidOf(client->tcon) != client->sid) {
+	ViceLog(0, ("GetClient: tcon %x tcon sid %d client sid %d\n",
+		    client->tcon, rxr_CidOf(client->tcon),
+		    client->sid));
+	H_UNLOCK
+	return VBUSY;
+    }
+    if (!(client && client->tcon && rxr_CidOf(client->tcon) == client->sid)) {
+	if (!client)
+	    ViceLog(0, ("GetClient: no client in conn %x\n", tcon));
+	else
+	    ViceLog(0, ("GetClient: tcon %x tcon sid %d client sid %d\n", 
+			client->tcon, client->tcon ? rxr_CidOf(client->tcon)
+			: -1, client->sid));
+	assert(0);
+    }
     if (client &&
 	client->LastCall > client->expTime && client->expTime) {
 	char hoststr[16];
