1 /* Copyright (C) 1991, 1990 Transarc Corporation - All rights reserved */
4 * (C) COPYRIGHT IBM CORPORATION 1987, 1998
5 * LICENSED MATERIALS - PROPERTY OF IBM
17 #include <netinet/in.h>
21 #include <afs/assert.h>
24 #include <afs/afsint.h>
25 #include <afs/rxgen_consts.h>
27 #include <afs/errors.h>
28 #include <afs/ihandle.h>
29 #include <afs/vnode.h>
30 #include <afs/volume.h>
31 #ifdef AFS_ATHENA_STDENV
35 #include <afs/ptclient.h>
36 #include <afs/prs_fs.h>
38 #include <afs/afsutil.h>
40 #include <afs/cellconfig.h>
46 #ifdef AFS_PTHREAD_ENV
47 pthread_mutex_t host_glock_mutex;
48 #endif /* AFS_PTHREAD_ENV */
51 extern int CurrentConnections;
53 extern int AnonymousID;
54 extern prlist AnonCPS;
56 extern struct afsconf_dir *confDir; /* config dir object */
57 extern int lwps; /* the max number of server threads */
58 extern afsUUID FS_HostUUID;
60 int CEs = 0; /* active clients */
61 int CEBlocks = 0; /* number of blocks of CEs */
62 struct client *CEFree = 0; /* first free client */
63 struct host *hostList = 0; /* linked list of all hosts */
64 int hostCount = 0; /* number of hosts in hostList */
68 #define CESPERBLOCK 73
69 struct CEBlock /* block of CESPERBLOCK file entries */
71 struct client entry[CESPERBLOCK];
75 * Make sure the subnet macros have been defined.
78 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
81 #ifndef IN_CLASSA_SUBNET
82 #define IN_CLASSA_SUBNET 0xffff0000
86 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
89 #ifndef IN_CLASSB_SUBNET
90 #define IN_CLASSB_SUBNET 0xffffff00
93 #define rxr_GetEpoch(aconn) (((struct rx_connection *)(aconn))->epoch)
95 #define rxr_CidOf(aconn) (((struct rx_connection *)(aconn))->cid)
97 #define rxr_PortOf(aconn) \
98 rx_PortOf(rx_PeerOf(((struct rx_connection *)(aconn))))
100 #define rxr_HostOf(aconn) \
101 rx_HostOf(rx_PeerOf((struct rx_connection *)(aconn)))
104 /* get a new block of CEs and chain it on CEFree */
105 static void GetCEBlock()
107 register struct CEBlock *block;
110 block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
112 ShutDownAndCore(PANIC);
114 for(i = 0; i < (CESPERBLOCK -1); i++) {
115 Lock_Init(&block->entry[i].lock);
116 block->entry[i].next = &(block->entry[i+1]);
118 block->entry[CESPERBLOCK-1].next = 0;
119 Lock_Init(&block->entry[CESPERBLOCK-1].lock);
120 CEFree = (struct client *)block;
126 /* get the next available CE */
127 static struct client *GetCE()
130 register struct client *entry;
135 ShutDownAndCore(PANIC);
138 CEFree = entry->next;
140 bzero((char *)entry, CLIENT_TO_ZERO(entry));
146 /* return an entry to the free list */
147 static void FreeCE(entry)
148 register struct client *entry;
151 entry->next = CEFree;
158 * The HTs and HTBlocks variables were formerly static, but they are
159 * now referenced elsewhere in the FileServer.
161 int HTs = 0; /* active file entries */
162 int HTBlocks = 0; /* number of blocks of HTs */
163 static struct host *HTFree = 0; /* first free file entry */
166 * Hash tables of host pointers. We need two tables, one
167 * to map IP addresses onto host pointers, and another
168 * to map host UUIDs onto host pointers.
170 static struct h_hashChain *hostHashTable[h_HASHENTRIES];
171 static struct h_hashChain *hostUuidHashTable[h_HASHENTRIES];
172 #define h_HashIndex(hostip) ((hostip) & (h_HASHENTRIES-1))
173 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
175 struct HTBlock /* block of HTSPERBLOCK file entries */
177 struct host entry[h_HTSPERBLOCK];
181 /* get a new block of HTs and chain it on HTFree */
182 static void GetHTBlock()
185 register struct HTBlock *block;
187 static int index = 0;
189 block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
191 ShutDownAndCore(PANIC);
193 #ifdef AFS_PTHREAD_ENV
194 for(i=0; i < (h_HTSPERBLOCK); i++)
195 assert(pthread_cond_init(&block->entry[i].cond, NULL) == 0);
196 #endif /* AFS_PTHREAD_ENV */
197 for(i=0; i < (h_HTSPERBLOCK); i++)
198 Lock_Init(&block->entry[i].lock);
199 for(i=0; i < (h_HTSPERBLOCK -1); i++)
200 block->entry[i].next = &(block->entry[i+1]);
201 for (i=0; i< (h_HTSPERBLOCK); i++)
202 block->entry[i].index = index++;
203 block->entry[h_HTSPERBLOCK-1].next = 0;
204 HTFree = (struct host *)block;
205 hosttableptrs[HTBlocks++] = block->entry;
210 /* get the next available HT */
211 static struct host *GetHT()
214 register struct host *entry;
220 HTFree = entry->next;
222 bzero((char *)entry, HOST_TO_ZERO(entry));
228 /* return an entry to the free list */
229 static void FreeHT(entry)
230 register struct host *entry;
233 entry->next = HTFree;
240 static short consolePort = 0;
243 register struct host *host;
252 * If this thread does not have a hold on this host AND
253 * if other threads also dont have any holds on this host AND
254 * If either the HOSTDELETED or CLIENTDELETED flags are set
257 int h_Release_r(host)
258 register struct host *host;
261 if (!((host)->holds[h_holdSlot()] &= ~h_holdbit()) ) {
262 if (! h_OtherHolds_r(host) ) {
263 if ( (host->hostFlags & HOSTDELETED) ||
264 (host->hostFlags & CLIENTDELETED) ) {
273 register struct host *host;
277 retVal = h_Held_r(host);
282 int h_OtherHolds_r(host)
283 register struct host *host;
285 register int i, bit, slot;
288 for (i = 0 ; i < h_maxSlots ; i++) {
289 if (host->holds[i] != ((i == slot) ? bit : 0)) {
296 int h_OtherHolds(host)
297 register struct host *host;
301 retVal = h_OtherHolds_r(host);
307 register struct host *host;
317 * returns 1 if already locked
318 * else returns locks and returns 0
322 register struct host *host;
324 struct Lock *hostLock = &host->lock;
329 if ( !(hostLock->excl_locked) && !(hostLock->readers_reading) )
330 hostLock->excl_locked = WRITE_LOCK;
334 LOCK_UNLOCK(hostLock)
343 #if FS_STATS_DETAILED
344 /*------------------------------------------------------------------------
345 * PRIVATE h_AddrInSameNetwork
348 * Given a target IP address and a candidate IP address (both
349 * in host byte order), return a non-zero value (1) if the
350 * candidate address is in a different network from the target
354 * a_targetAddr : Target address.
355 * a_candAddr : Candidate address.
358 * 1 if the candidate address is in the same net as the target,
362 * The target and candidate addresses are both in host byte
363 * order, NOT network byte order, when passed in. We return
364 * our value as a character, since that's the type of field in
365 * the host structure, where this info will be stored.
369 *------------------------------------------------------------------------*/
371 static char h_AddrInSameNetwork(a_targetAddr, a_candAddr)
372 afs_uint32 a_targetAddr;
373 afs_uint32 a_candAddr;
375 { /*h_AddrInSameNetwork*/
377 afs_uint32 targetNet;
381 * Pull out the network and subnetwork numbers from the target
382 * and candidate addresses. We can short-circuit this whole
383 * affair if the target and candidate addresses are not of the
386 if (IN_CLASSA(a_targetAddr)) {
387 if (!(IN_CLASSA(a_candAddr))) {
390 targetNet = a_targetAddr & IN_CLASSA_NET;
391 candNet = a_candAddr & IN_CLASSA_NET;
394 if (IN_CLASSB(a_targetAddr)) {
395 if (!(IN_CLASSB(a_candAddr))) {
398 targetNet = a_targetAddr & IN_CLASSB_NET;
399 candNet = a_candAddr & IN_CLASSB_NET;
402 if (IN_CLASSC(a_targetAddr)) {
403 if (!(IN_CLASSC(a_candAddr))) {
406 targetNet = a_targetAddr & IN_CLASSC_NET;
407 candNet = a_candAddr & IN_CLASSC_NET;
410 targetNet = a_targetAddr;
411 candNet = a_candAddr;
412 } /*Class D address*/
415 * Now, simply compare the extracted net values for the two addresses
416 * (which at this point are known to be of the same class)
418 if (targetNet == candNet)
423 } /*h_AddrInSameNetwork*/
424 #endif /* FS_STATS_DETAILED */
428 h_gethostcps_r(host,now)
429 register struct host *host;
430 register afs_int32 now;
435 /* at this point, the host might not be locked, nor held */
436 /* make sure that we do not disappear behind the RPC */
437 if ( !(held = h_Held_r(host)) )
440 /* wait if somebody else is already doing the getCPS call */
441 while ( host->hostFlags & HPCS_INPROGRESS )
443 slept = 1; /* I did sleep */
444 host->hostFlags |= HPCS_WAITING; /* I am sleeping now */
445 #ifdef AFS_PTHREAD_ENV
446 pthread_cond_wait(&host->cond, &host_glock_mutex);
447 #else /* AFS_PTHREAD_ENV */
448 if (( code = LWP_WaitProcess( &(host->hostFlags ))) != LWP_SUCCESS)
449 ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
450 #endif /* AFS_PTHREAD_ENV */
454 host->hostFlags |= HPCS_INPROGRESS; /* mark as CPSCall in progress */
455 if (host->hcps.prlist_val)
456 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
457 host->hcps.prlist_val = (afs_int32 *)0;
458 host->hcps.prlist_len = 0;
459 slept? (host->cpsCall = FT_ApproxTime()): (host->cpsCall = now );
462 code = pr_GetHostCPS(htonl(host->host), &host->hcps);
466 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
467 * and reevaluates things if no sync server or quorum is found we could still end up
468 * with one of these errors. In such case we would like to reevaluate the rpc call to
469 * find if there's cps for this guy. We treat other errors (except network failures
470 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
471 * we could like to deal this problem the other way around (i.e. if code == NOCPS
472 * ignore else retry next time) but the problem is that there're other errors (i.e.
473 * EPERM) for which we don't want to retry and we don't know the whole code list!
475 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
477 * We would have preferred to use a while loop and try again since ops in protected
478 * acls for this host will fail now but they'll be reevaluated on any subsequent
479 * call. The attempt to wait for a quorum/sync site or network error won't work
480 * since this problems really should only occurs during a complete fileserver
481 * restart. Since the fileserver will start before the ptservers (and thus before
482 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
484 host->hcpsfailed = 1;
485 ViceLog(0, ("Warning: GetHostCPS failed (%d) for %x; will retry\n", code, host->host));
487 host->hcpsfailed = 0;
488 ViceLog(1, ("gethost: GetHostCPS failed (%d) for %x; ignored\n", code, host->host));
490 if (host->hcps.prlist_val)
491 free(host->hcps.prlist_val);
492 host->hcps.prlist_val = (afs_int32 *)0;
493 host->hcps.prlist_len = 0; /* Make sure it's zero */
495 host->hcpsfailed = 0;
497 host->hostFlags &= ~HPCS_INPROGRESS;
498 /* signal all who are waiting */
499 if ( host->hostFlags & HPCS_WAITING) /* somebody is waiting */
501 host->hostFlags &= ~HPCS_WAITING;
502 #ifdef AFS_PTHREAD_ENV
503 assert(pthread_cond_broadcast(&host->cond) == 0);
504 #else /* AFS_PTHREAD_ENV */
505 if ( (code = LWP_NoYieldSignal( &(host->hostFlags) )) != LWP_SUCCESS )
506 ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
507 #endif /* AFS_PTHREAD_ENV */
510 /* if we had held the host, release it now */
515 void h_flushhostcps(hostaddr, hport)
516 register afs_uint32 hostaddr, hport; /* net byte order */
518 register struct host *host;
521 host = h_Lookup_r(hostaddr, hport);
523 host->hcpsfailed = 1;
532 * Allocate a host. It will be identified by the peer (ip,port) info in the
533 * rx connection provided. The host is returned un-held and un-locked
535 #define DEF_ROPCONS 2115
537 struct host *h_Alloc(r_con)
538 register struct rx_connection *r_con;
542 retVal = h_Alloc_r(r_con);
547 struct host *h_Alloc_r(r_con)
548 register struct rx_connection *r_con;
552 struct servent *serverentry;
553 register index = h_HashIndex(rxr_HostOf(r_con));
554 register struct host *host;
555 static struct rx_securityClass *sc = 0;
557 struct h_hashChain* h_hashChain;
558 #if FS_STATS_DETAILED
559 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order*/
560 #endif /* FS_STATS_DETAILED */
564 h_hashChain = (struct h_hashChain*) malloc(sizeof(struct h_hashChain));
566 h_hashChain->hostPtr = host;
567 h_hashChain->addr = rxr_HostOf(r_con);
568 h_hashChain->next = hostHashTable[index];
569 hostHashTable[index] = h_hashChain;
571 host->host = rxr_HostOf(r_con);
572 host->port = rxr_PortOf(r_con);
573 if(consolePort == 0 ) { /* find the portal number for console */
574 #if defined(AFS_OSF_ENV)
575 serverentry = getservbyname("ropcons", "");
577 serverentry = getservbyname("ropcons", 0);
580 consolePort = serverentry->s_port;
582 consolePort = DEF_ROPCONS; /* Use a default */
584 if (host->port == consolePort) host->Console = 1;
585 /* Make a callback channel even for the console, on the off chance that it
586 makes a request that causes a break call back. It shouldn't. */
589 sc = (struct rx_securityClass *) rxnull_NewClientSecurityObject();
590 host->callback_rxcon = rx_NewConnection (host->host, host->port,
592 rx_SetConnDeadTime(host->callback_rxcon, 50);
593 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
595 now = host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
597 host->hcps.prlist_val = (afs_int32 *)0;
598 host->hcps.prlist_len = 0;
599 host->hcps.prlist_val = (afs_int32 *)0;
601 /*host->hcpsfailed = 0; /* save cycles */
602 /* h_gethostcps(host); do this under host lock */
603 host->FirstClient = 0;
604 h_InsertList_r(host); /* update global host List */
605 #if FS_STATS_DETAILED
607 * Compare the new host's IP address (in host byte order) with ours
608 * (the File Server's), remembering if they are in the same network.
610 newHostAddr_HBO = (afs_uint32)ntohl(host->host);
611 host->InSameNetwork = h_AddrInSameNetwork(FS_HostAddr_HBO,
613 #endif /* FS_STATS_DETAILED */
619 /* Lookup a host given an IP address and UDP port number. */
620 struct host *h_Lookup(hostaddr, hport)
621 afs_uint32 hostaddr, hport; /* network byte order */
625 retVal = h_Lookup_r(hostaddr, hport);
630 struct host *h_Lookup_r(hostaddr, hport)
631 afs_uint32 hostaddr, hport; /* network byte order */
633 register afs_int32 now;
634 register struct host *host=0;
635 register struct h_hashChain* chain;
636 register index = h_HashIndex(hostaddr);
637 extern int hostaclRefresh;
639 for (chain=hostHashTable[index]; chain; chain=chain->next) {
640 host = chain->hostPtr;
642 if (!(host->hostFlags & HOSTDELETED) && chain->addr == hostaddr
643 && host->port == hport) {
644 now = FT_ApproxTime(); /* always evaluate "now" */
645 if (host->hcpsfailed || (host->cpsCall+hostaclRefresh < now )) {
647 * Every hostaclRefresh period (def 2 hrs) get the new membership list for the host.
648 * Note this could be the first time that the host is added to a group.
649 * Also here we also retry on previous legitimate hcps failures
651 h_gethostcps_r(host,now);
661 /* Lookup a host given its UUID. */
662 struct host *h_LookupUuid_r(uuidp)
665 register struct host *host=0;
666 register struct h_hashChain* chain;
667 register index = h_UuidHashIndex(uuidp);
669 for (chain=hostUuidHashTable[index]; chain; chain=chain->next) {
670 host = chain->hostPtr;
672 if (!(host->hostFlags & HOSTDELETED) && host->interface
673 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
684 * h_Hold: Establish a hold by the current LWP on this host--the host
685 * or its clients will not be physically deleted until all holds have
688 * NOTE: h_Hold_r is a macro defined in host.h.
692 register struct host *host;
701 /* h_TossStuff: Toss anything in the host structure (the host or
702 * clients marked for deletion. Called from r_Release ONLY.
703 * To be called, there must be no holds, and either host->deleted
704 * or host->clientDeleted must be set.
707 register struct host *host;
710 register struct client **cp, *client;
713 /* if somebody still has this host held */
714 for (i=0; (i<h_maxSlots)&&(!(host)->holds[i]); i++);
718 /* ASSUMPTION: r_FreeConnection() does not yield */
719 for (cp = &host->FirstClient; client = *cp; ) {
720 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
721 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val) {
722 free(client->CPS.prlist_val);
723 client->CPS.prlist_val = (afs_int32 *)0;
726 rx_SetSpecific(client->tcon, rxcon_client_key, (void *)0);
728 CurrentConnections--;
731 } else cp = &client->next;
733 if (host->hostFlags & HOSTDELETED) {
734 register struct h_hashChain **hp, *th;
735 register struct rx_connection *rxconn;
740 if (host->Console & 1) Console--;
741 if (rxconn = host->callback_rxcon) {
742 host->callback_rxcon = (struct rx_connection *)0;
744 * If rx_DestroyConnection calls h_FreeConnection we will
745 * deadlock on the host_glock_mutex. Work around the problem
746 * by unhooking the client from the connection before
747 * destroying the connection.
749 client = rx_GetSpecific(rxconn, rxcon_client_key);
750 if (client && client->tcon == rxconn)
752 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
753 rx_DestroyConnection(rxconn);
755 if (host->hcps.prlist_val)
756 free(host->hcps.prlist_val);
757 host->hcps.prlist_val = (afs_int32 *)0;
758 host->hcps.prlist_len = 0;
759 DeleteAllCallBacks_r(host);
760 host->hostFlags &= ~RESETDONE; /* just to be safe */
762 /* if alternate addresses do not exist */
763 if ( !(host->interface) )
765 for (hp = &hostHashTable[h_HashIndex(host->host)];
766 th = *hp; hp = &th->next)
769 if (th->hostPtr == host)
772 h_DeleteList_r(host);
780 /* delete all hash entries for the UUID */
781 uuidp = &host->interface->uuid;
782 for (hp = &hostUuidHashTable[h_UuidHashIndex(uuidp)];
783 th = *hp; hp = &th->next) {
785 if (th->hostPtr == host)
792 /* delete all hash entries for alternate addresses */
793 assert(host->interface->numberOfInterfaces > 0 );
794 for ( i=0; i < host->interface->numberOfInterfaces; i++)
796 hostAddr = host->interface->addr[i];
797 for (hp = &hostHashTable[h_HashIndex(hostAddr)];
798 th = *hp; hp = &th->next)
801 if (th->hostPtr == host)
809 free(host->interface);
810 host->interface = NULL;
811 h_DeleteList_r(host); /* remove host from global host List */
813 } /* if alternate address exists */
818 /* Called by rx when a server connection disappears */
819 h_FreeConnection(tcon)
820 struct rx_connection *tcon;
823 register struct client *client;
825 client = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
828 if (client->tcon == tcon)
829 client->tcon = (struct rx_connection *)0;
832 } /*h_FreeConnection*/
835 /* h_Enumerate: Calls (*proc)(host, held, param) for at least each host in the
836 * system at the start of the enumeration (perhaps more). Hosts may be deleted
837 * (have delete flag set); ditto for clients. (*proc) is always called with
838 * host h_held(). The hold state of the host with respect to this lwp is passed
839 * to (*proc) as the param held. The proc should return 0 if the host should be
840 * released, 1 if it should be held after enumeration.
842 h_Enumerate(proc, param)
847 register struct host *host, **list;
849 register int i, count;
852 if (hostCount == 0) {
856 list = (struct host **)malloc(hostCount * sizeof(struct host *));
857 assert(list != NULL);
858 held = (int *)malloc(hostCount * sizeof(int));
859 assert(held != NULL);
860 for (count = 0, host = hostList ; host ; host = host->next, count++) {
862 if (!(held[count] = h_Held_r(host)))
865 assert(count == hostCount);
867 for ( i = 0 ; i < count ; i++) {
868 held[i] = (*proc)(list[i], held[i], param);
870 h_Release(list[i]);/* this might free up the host */
876 /* h_Enumerate_r: Calls (*proc)(host, held, param) for at least each host in
877 * the at the start of the enumeration (perhaps more). Hosts may be deleted
878 * (have delete flag set); ditto for clients. (*proc) is always called with
879 * host h_held() and the global host lock (H_LOCK) locked.The hold state of the
880 * host with respect to this lwp is passed to (*proc) as the param held.
881 * The proc should return 0 if the host should be released, 1 if it should
882 * be held after enumeration.
884 h_Enumerate_r(proc, param)
889 register struct host *host;
892 if (hostCount == 0) {
895 for (host = hostList ; host ; host = host->next) {
896 if (!(held = h_Held_r(host)))
898 held = (*proc)(host, held, param);
900 h_Release_r(host);/* this might free up the host */
905 /* Host is returned held */
906 struct host *h_GetHost_r(tcon)
907 struct rx_connection *tcon;
911 struct host *oldHost;
914 struct interfaceAddr interf;
916 afs_int32 buffer[AFS_MAX_INTERFACE_ADDR];
917 struct Identity *identP = NULL;
922 haddr = rxr_HostOf(tcon);
923 hport = rxr_PortOf(tcon);
926 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
927 host = h_Lookup_r(haddr, hport);
928 if (host && !identP && !(host->Console&1)) {
929 /* This is a new connection, and we already have a host
930 * structure for this address. Verify that the identity
931 * of the caller matches the identity in the host structure.
933 if (!(held = h_Held_r(host)))
936 if ( !(host->hostFlags & ALTADDR) )
938 /* Another thread is doing initialization */
940 if ( !held) h_Release_r(host);
941 ViceLog(125, ("Host %x starting h_Lookup again\n", host));
944 host->hostFlags &= ~ALTADDR;
946 code = RXAFSCB_WhoAreYou(host->callback_rxcon, &interf);
948 if ( code == RXGEN_OPCODE ) {
949 identP = (struct Identity *)malloc(1);
951 rx_SetSpecific(tcon, rxcon_ident_key, identP);
952 /* The host on this connection was unable to respond to
953 * the WhoAreYou. We will treat this as a new connection
954 * from the existing host. The worst that can happen is
955 * that we maintain some extra callback state information */
956 if (host->interface) {
958 ("Host %x used to support WhoAreYou, deleting.\n",
960 host->hostFlags |= HOSTDELETED;
962 if (!held) h_Release_r(host);
966 } else if (code == 0) {
968 identP = (struct Identity *)malloc(sizeof(struct Identity));
970 identP->uuid = interf.uuid;
971 rx_SetSpecific(tcon, rxcon_ident_key, identP);
972 /* Check whether the UUID on this connection matches
973 * the UUID in the host structure. If they don't match
974 * then this is not the same host as before. */
975 if ( !host->interface
976 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid) ) {
978 ("Host %x has changed its identity, deleting.\n",
980 host->hostFlags |= HOSTDELETED;
982 if (!held) h_Release_r(host);
987 ViceLog(0,("CB: WhoAreYou failed for %x.%d, error %d\n",
988 host->host, host->port, code));
989 host->hostFlags |= VENUSDOWN;
991 host->hostFlags |= ALTADDR;
994 if (!(held = h_Held_r(host)))
996 if ( ! (host->hostFlags & ALTADDR) )
998 /* another thread is doing the initialisation */
999 ViceLog(125, ("Host %x waiting for host-init to complete\n",
1003 if ( !held) h_Release_r(host);
1004 ViceLog(125, ("Host %x starting h_Lookup again\n", host));
1007 /* We need to check whether the identity in the host structure
1008 * matches the identity on the connection. If they don't match
1009 * then treat this a new host. */
1010 if ( !(host->Console&1)
1011 && ( ( !identP->valid && host->interface )
1012 || ( identP->valid && !host->interface )
1014 && !afs_uuid_equal(&identP->uuid, &host->interface->uuid) ) ) ) {
1015 /* The host in the cache is not the host for this connection */
1016 host->hostFlags |= HOSTDELETED;
1018 if (!held) h_Release_r(host);
1019 ViceLog(0,("CB: new identity for host %x, deleting\n",
1024 host = h_Alloc_r(tcon);
1027 h_gethostcps_r(host,FT_ApproxTime());
1028 if (!(host->Console&1)) {
1029 if (!identP || !interfValid) {
1031 code = RXAFSCB_WhoAreYou(host->callback_rxcon, &interf);
1033 if ( code == RXGEN_OPCODE ) {
1034 identP = (struct Identity *)malloc(1);
1036 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1038 ("Host %x does not support WhoAreYou.\n",
1041 } else if (code == 0) {
1043 identP = (struct Identity *)malloc(sizeof(struct Identity));
1045 identP->uuid = interf.uuid;
1046 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1047 ViceLog(25,("WhoAreYou success on %x\n", host->host));
1050 if (code == 0 && !identP->valid) {
1052 code = RXAFSCB_InitCallBackState(host->callback_rxcon);
1054 } else if (code == 0) {
1055 oldHost = h_LookupUuid_r(&identP->uuid);
1057 /* This is a new address for an existing host. Update
1058 * the list of interfaces for the existing host and
1059 * delete the host structure we just allocated. */
1060 if (!(held = h_Held_r(oldHost)))
1063 ViceLog(25,("CB: new addr %x for old host %x\n",
1064 host->host, oldHost->host));
1065 host->hostFlags |= HOSTDELETED;
1069 addInterfaceAddr_r(host, haddr);
1071 /* This really is a new host */
1072 hashInsertUuid_r(&identP->uuid, host);
1074 code = RXAFSCB_InitCallBackState3(host->callback_rxcon,
1078 ViceLog(25,("InitCallBackState3 success on %x\n",
1080 assert(interfValid == 1);
1081 initInterfaceAddr_r(host, &interf);
1086 ViceLog(0,("CB: RCallBackConnectBack failed for %x.%d\n",
1087 host->host, host->port));
1088 host->hostFlags |= VENUSDOWN;
1091 host->hostFlags |= RESETDONE;
1094 host->hostFlags |= ALTADDR;/* host structure iniatilisation complete */
1102 static char localcellname[PR_MAXNAMELEN+1];
1103 char local_realm[AFS_REALM_SZ] = "";
1106 void h_InitHostPackage()
1108 afsconf_GetLocalCell (confDir, localcellname, PR_MAXNAMELEN);
1109 if (!local_realm[0]) {
1110 if (afs_krb_get_lrealm(local_realm, 0) != 0/*KSUCCESS*/) {
1111 ViceLog(0, ("afs_krb_get_lrealm failed, using %s.\n",localcellname));
1112 strcpy (local_realm, localcellname);
1115 rxcon_ident_key = rx_KeyCreate((rx_destructor_t)free);
1116 rxcon_client_key = rx_KeyCreate((rx_destructor_t)0);
1117 #ifdef AFS_PTHREAD_ENV
1118 assert(pthread_mutex_init(&host_glock_mutex, NULL) == 0);
1119 #endif /* AFS_PTHREAD_ENV */
1122 static MapName_r(aname, acell, aval)
1131 afs_int32 anamelen, cnamelen;
1135 anamelen=strlen(aname);
1136 if (anamelen >= PR_MAXNAMELEN)
1137 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
1139 lnames.namelist_len = 1;
1140 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
1141 lids.idlist_len = 0;
1142 lids.idlist_val = (afs_int32 *) 0;
1144 cnamelen=strlen(acell);
1146 if (strcasecmp(local_realm, acell) && strcasecmp(localcellname, acell)) {
1147 ViceLog(2, ("MapName: cell is foreign. cell=%s, localcell=%s, localrealm=%s\n",
1148 acell, localcellname, local_realm));
1149 if ((anamelen+cnamelen+1) >= PR_MAXNAMELEN) {
1150 ViceLog(2, ("MapName: Name too long, using AnonymousID for %s@%s\n",
1152 *aval = AnonymousID;
1155 foreign = 1; /* attempt cross-cell authentication */
1156 tname = (char *) malloc(anamelen+cnamelen+2);
1157 strcpy(tname, aname);
1158 tname[anamelen] = '@';
1159 strcpy(tname+anamelen+1, acell);
1160 lnames.namelist_val = (prname *) tname;
1165 code = pr_NameToId(&lnames, &lids);
1168 if (lids.idlist_val) {
1169 *aval = lids.idlist_val[0];
1170 if (*aval == AnonymousID) {
1171 ViceLog(2, ("MapName: NameToId on %s returns anonymousID\n", lnames.namelist_val));
1173 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
1175 ViceLog(0, ("MapName: NameToId on '%s' is unknown\n", lnames.namelist_val));
1181 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
1188 /* NOTE: this returns the client with a Shared lock */
1189 struct client *h_ID2Client(vid)
1192 register struct client *client;
1193 register struct host *host;
1197 for (host=hostList; host; host=host->next) {
1198 if (host->hostFlags & HOSTDELETED)
1200 for (client = host->FirstClient; client; client = client->next) {
1201 if (!client->deleted && client->ViceId == vid) {
1204 ObtainSharedLock(&client->lock);
1218 * Called by the server main loop. Returns a h_Held client, which must be
1219 * released later the main loop. Allocates a client if the matching one
1220 * isn't around. The client is returned with its reference count incremented
1221 * by one. The caller must call h_ReleaseClient_r when finished with
1224 struct client *h_FindClient_r(tcon)
1225 struct rx_connection *tcon;
1228 register struct client *client;
1229 register struct host *host;
1230 struct client *oldClient;
1235 #if (64-MAXKTCNAMELEN)
1236 ticket name length != 64
1240 char uname[PR_MAXNAMELEN];
1241 char tcell[MAXKTCREALMLEN];
1244 client = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
1245 if (client && !client->deleted) {
1247 h_Hold_r(client->host);
1248 if (client->prfail != 2) { /* Could add shared lock on client here */
1249 /* note that we don't have to lock entry in this path to
1250 * ensure CPS is initialized, since we don't call rxr_SetSpecific
1251 * until initialization is done, and we only get here if
1252 * rx_GetSpecific located the client structure.
1257 ObtainWriteLock(&client->lock); /* released at end */
1259 } else if (client) {
1263 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
1264 ViceLog(5,("FindClient: authenticating connection: authClass=%d\n",
1266 if (authClass == 1) {
1267 /* A bcrypt tickets, no longer supported */
1268 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
1269 viceid = AnonymousID;
1270 expTime = 0x7fffffff;
1271 } else if (authClass == 2) {
1274 /* kerberos ticket */
1275 code = rxkad_GetServerInfo (tcon, /*level*/0, &expTime,
1276 tname, tinst, tcell, &kvno);
1278 ViceLog(1, ("Failed to get rxkad ticket info\n"));
1279 viceid = AnonymousID;
1280 expTime = 0x7fffffff;
1282 int ilen = strlen(tinst);
1284 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
1285 tname, tinst, tcell, expTime, kvno));
1286 strncpy (uname, tname, sizeof(uname));
1288 if (strlen(uname) + 1 + ilen >= sizeof(uname))
1290 strcat (uname, ".");
1291 strcat (uname, tinst);
1293 /* translate the name to a vice id */
1294 code = MapName_r(uname, tcell, &viceid);
1297 ViceLog(1, ("failed to map name=%s, cell=%s -> code=%d\n",
1298 uname, tcell, code));
1300 viceid = AnonymousID;
1301 expTime = 0x7fffffff;
1305 viceid = AnonymousID; /* unknown security class */
1306 expTime = 0x7fffffff;
1310 host = h_GetHost_r(tcon); /* Returns it h_Held */
1312 /* First try to find the client structure */
1313 for (client = host->FirstClient; client; client = client->next) {
1314 if (!client->deleted && (client->sid == rxr_CidOf(tcon)) &&
1315 (client->VenusEpoch == rxr_GetEpoch(tcon))) {
1316 if (client->tcon && (client->tcon != tcon)) {
1317 ViceLog(0, ("*** Vid=%d, sid=%x, tcon=%x, Tcon=%x ***\n",
1318 client->ViceId, client->sid, client->tcon, tcon));
1319 client->tcon = (struct rx_connection *)0;
1323 ObtainWriteLock(&client->lock);
1329 /* Still no client structure - get one */
1332 ObtainWriteLock(&client->lock);
1333 client->host = host;
1334 client->next = host->FirstClient;
1335 host->FirstClient = client;
1336 #if FS_STATS_DETAILED
1337 client->InSameNetwork = host->InSameNetwork;
1338 #endif /* FS_STATS_DETAILED */
1339 client->ViceId = viceid;
1340 client->expTime = expTime; /* rx only */
1341 client->authClass = authClass; /* rx only */
1342 client->sid = rxr_CidOf(tcon);
1343 client->VenusEpoch = rxr_GetEpoch(tcon);
1344 client->CPS.prlist_val = 0;
1345 client->refCount = 1;
1346 CurrentConnections++; /* increment number of connections */
1349 client->prfail = fail;
1351 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
1352 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID)) {
1353 free(client->CPS.prlist_val);
1355 client->CPS.prlist_val = (afs_int32 *)0;
1356 client->ViceId = viceid;
1357 client->expTime = expTime;
1359 if (viceid == ANONYMOUSID) {
1360 client->CPS.prlist_len = AnonCPS.prlist_len;
1361 client->CPS.prlist_val = AnonCPS.prlist_val;
1364 code = pr_GetCPS(viceid, &client->CPS);
1367 ViceLog(0, ("pr_GetCPS failed(%d) for user %d, host %x.%d\n",
1368 code, viceid, client->host->host, ntohs(client->host->port)));
1370 /* Although ubik_Call (called by pr_GetCPS) traverses thru
1371 * all protection servers and reevaluates things if no
1372 * sync server or quorum is found we could still end up
1373 * with one of these errors. In such case we would like to
1374 * reevaluate the rpc call to find if there's cps for this
1375 * guy. We treat other errors (except network failures
1376 * ones - i.e. code < 0) as an indication that there is no
1377 * CPS for this host. Ideally we could like to deal this
1378 * problem the other way around (i.e. if code == NOCPS
1379 * ignore else retry next time) but the problem is that
1380 * there're other errors (i.e. EPERM) for which we don't
1381 * want to retry and we don't know the whole code list!
1383 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
1387 /* the disabling of system:administrators is so iffy and has so many
1388 * possible failure modes that we will disable it again */
1389 /* Turn off System:Administrator for safety
1390 if (AL_IsAMember(SystemId, client->CPS) == 0)
1391 assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
1394 /* Now, tcon may already be set to a rock, since we blocked with no host
1395 * or client locks set above in pr_GetCPS (XXXX some locking is probably
1396 * required). So, before setting the RPC's rock, we should disconnect
1397 * the RPC from the other client structure's rock.
1399 if (oldClient = (struct client *) rx_GetSpecific(tcon, rxcon_client_key)) {
1400 oldClient->tcon = (struct rx_connection *) 0;
1401 /* rx_SetSpecific will be done immediately below */
1403 client->tcon = tcon;
1404 rx_SetSpecific(tcon, rxcon_client_key, client);
1405 ReleaseWriteLock(&client->lock);
1409 } /*h_FindClient_r*/
1411 int h_ReleaseClient_r(client)
1412 struct client *client;
1414 assert(client->refCount > 0);
1421 * Sigh: this one is used to get the client AGAIN within the individual
1422 * server routines. This does not bother h_Holding the host, since
1423 * this is assumed already have been done by the server main loop.
1424 * It does check tokens, since only the server routines can return the
1425 * VICETOKENDEAD error code
1427 int GetClient(tcon, cp)
1428 struct rx_connection * tcon;
1432 register struct client *client;
1436 *cp = client = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
1438 assert(client && client->tcon && rxr_CidOf(client->tcon) == client->sid);
1440 client->LastCall > client->expTime && client->expTime) {
1441 ViceLog(1, ("Token for %s at %x.%d expired %d\n",
1442 h_UserName(client), client->host->host, client->host->port, client->expTime));
1444 return VICETOKENDEAD;
1453 /* Client user name for short term use. Note that this is NOT inexpensive */
1454 char *h_UserName(client)
1455 struct client *client;
1458 static char User[PR_MAXNAMELEN+1];
1462 lids.idlist_len = 1;
1463 lids.idlist_val = (afs_int32 *)malloc(1*sizeof(afs_int32));
1464 lnames.namelist_len = 0;
1465 lnames.namelist_val = (prname *)0;
1466 lids.idlist_val[0] = client->ViceId;
1467 if (pr_IdToName(&lids,&lnames)) {
1468 /* We need to free id we alloced above! */
1469 free(lids.idlist_val);
1470 return "*UNKNOWN USER NAME*";
1472 strncpy(User,lnames.namelist_val[0],PR_MAXNAMELEN);
1473 free(lids.idlist_val);
1474 free(lnames.namelist_val);
1484 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
1485 CEs, CEBlocks, HTs, HTBlocks));
1490 static int h_PrintClient(host, held, file)
1491 register struct host *host;
1493 StreamHandle_t *file;
1495 register struct client *client;
1501 if (host->hostFlags & HOSTDELETED) {
1505 sprintf(tmpStr,"Host %x.%d down = %d, LastCall %s", host->host,
1506 host->port, (host->hostFlags & VENUSDOWN),
1507 afs_ctime((time_t *)&host->LastCall, tbuffer, sizeof(tbuffer)));
1508 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1509 for (client = host->FirstClient; client; client=client->next) {
1510 if (!client->deleted) {
1512 sprintf(tmpStr, " user id=%d, name=%s, sl=%s till %s",
1513 client->ViceId, h_UserName(client),
1514 client->authClass ? "Authenticated" : "Not authenticated",
1516 afs_ctime((time_t *)&client->expTime, tbuffer, sizeof(tbuffer))
1518 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1521 sprintf(tmpStr, " user=%s, no current server connection\n",
1522 h_UserName(client));
1523 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1525 sprintf(tmpStr, " CPS-%d is [", client->CPS.prlist_len);
1526 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1527 if (client->CPS.prlist_val) {
1528 for (i=0; i > client->CPS.prlist_len; i++) {
1529 sprintf(tmpStr, " %d", client->CPS.prlist_val[i]);
1530 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1533 sprintf(tmpStr, "]\n");
1534 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1545 * Print a list of clients, with last security level and token value seen,
1555 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
1558 ViceLog(0, ("Couldn't create client dump file %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
1561 now = FT_ApproxTime();
1562 sprintf(tmpStr, "List of active users at %s\n",
1563 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
1564 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1565 h_Enumerate(h_PrintClient, (char *)file);
1566 STREAM_REALLYCLOSE(file);
1567 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
1573 static int h_DumpHost(host, held, file)
1574 register struct host *host;
1576 StreamHandle_t *file;
1583 sprintf(tmpStr, "ip:%x holds:%d port:%d hidx:%d cbid:%d lock:%x last:%u active:%u down:%d del:%d cons:%d cldel:%d\n\t hpfailed:%d hcpsCall:%u hcps [",
1584 host->host, host->holds, host->port, host->index, host->cblist,
1585 CheckLock(&host->lock), host->LastCall, host->ActiveCall,
1586 (host->hostFlags & VENUSDOWN), host->hostFlags&HOSTDELETED,
1587 host->Console, host->hostFlags & CLIENTDELETED,
1588 host->hcpsfailed, host->cpsCall);
1589 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1590 if (host->hcps.prlist_val)
1591 for (i=0; i < host->hcps.prlist_len; i++) {
1592 sprintf(tmpStr, " %d", host->hcps.prlist_val[i]);
1593 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1595 sprintf(tmpStr, "] [");
1596 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1597 if ( host->interface)
1598 for (i=0; i < host->interface->numberOfInterfaces; i++) {
1599 sprintf(tmpStr, " %x", host->interface->addr[i]);
1600 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1602 sprintf(tmpStr, "]\n");
1603 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1614 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
1619 ViceLog(0, ("Couldn't create host dump file %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
1622 now = FT_ApproxTime();
1623 sprintf(tmpStr, "List of active hosts at %s\n",
1624 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
1625 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1626 h_Enumerate(h_DumpHost, (char *) file);
1627 STREAM_REALLYCLOSE(file);
1628 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
1634 * This counts the number of workstations, the number of active workstations,
1635 * and the number of workstations declared "down" (i.e. not heard from
1636 * recently). An active workstation has received a call since the cutoff
1637 * time argument passed.
1639 h_GetWorkStats(nump, activep, delp, cutofftime)
1643 afs_int32 cutofftime;
1647 register struct host *host;
1648 register int num=0, active=0, del=0;
1651 for (host = hostList; host; host = host->next) {
1652 if (!(host->hostFlags & HOSTDELETED)) {
1654 if (host->ActiveCall > cutofftime)
1656 if (host->hostFlags & VENUSDOWN)
1668 } /*h_GetWorkStats*/
1671 /*------------------------------------------------------------------------
1672 * PRIVATE h_ClassifyAddress
1675 * Given a target IP address and a candidate IP address (both
1676 * in host byte order), classify the candidate into one of three
1677 * buckets in relation to the target by bumping the counters passed
1681 * a_targetAddr : Target address.
1682 * a_candAddr : Candidate address.
1683 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
1684 * addresses are either in the same network
1685 * or the same subnet.
1686 * a_diffSubnetP : ...when the candidate is in a different
1688 * a_diffNetworkP : ...when the candidate is in a different
1695 * The target and candidate addresses are both in host byte
1696 * order, NOT network byte order, when passed in.
1700 *------------------------------------------------------------------------*/
1702 static void h_ClassifyAddress(a_targetAddr, a_candAddr, a_sameNetOrSubnetP,
1703 a_diffSubnetP, a_diffNetworkP)
1704 afs_uint32 a_targetAddr;
1705 afs_uint32 a_candAddr;
1706 afs_int32 *a_sameNetOrSubnetP;
1707 afs_int32 *a_diffSubnetP;
1708 afs_int32 *a_diffNetworkP;
1710 { /*h_ClassifyAddress*/
1712 register int i; /*Iterator thru host hash table*/
1713 register struct host *hostP; /*Ptr to current host entry*/
1714 register afs_uint32 currHostAddr; /*Current host address*/
1715 afs_uint32 targetNet;
1716 afs_uint32 targetSubnet;
1718 afs_uint32 candSubnet;
1721 * Put bad values into the subnet info to start with.
1723 targetSubnet = (afs_uint32) 0;
1724 candSubnet = (afs_uint32) 0;
1727 * Pull out the network and subnetwork numbers from the target
1728 * and candidate addresses. We can short-circuit this whole
1729 * affair if the target and candidate addresses are not of the
1732 if (IN_CLASSA(a_targetAddr)) {
1733 if (!(IN_CLASSA(a_candAddr))) {
1734 (*a_diffNetworkP)++;
1737 targetNet = a_targetAddr & IN_CLASSA_NET;
1738 candNet = a_candAddr & IN_CLASSA_NET;
1739 if (IN_SUBNETA(a_targetAddr))
1740 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
1741 if (IN_SUBNETA(a_candAddr))
1742 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
1745 if (IN_CLASSB(a_targetAddr)) {
1746 if (!(IN_CLASSB(a_candAddr))) {
1747 (*a_diffNetworkP)++;
1750 targetNet = a_targetAddr & IN_CLASSB_NET;
1751 candNet = a_candAddr & IN_CLASSB_NET;
1752 if (IN_SUBNETB(a_targetAddr))
1753 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
1754 if (IN_SUBNETB(a_candAddr))
1755 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
1756 } /*Class B target*/
1758 if (IN_CLASSC(a_targetAddr)) {
1759 if (!(IN_CLASSC(a_candAddr))) {
1760 (*a_diffNetworkP)++;
1763 targetNet = a_targetAddr & IN_CLASSC_NET;
1764 candNet = a_candAddr & IN_CLASSC_NET;
1767 * Note that class C addresses can't have subnets,
1768 * so we leave the defaults untouched.
1770 } /*Class C target*/
1772 targetNet = a_targetAddr;
1773 candNet = a_candAddr;
1774 } /*Class D address*/
1777 * Now, simply compare the extracted net and subnet values for
1778 * the two addresses (which at this point are known to be of the
1781 if (targetNet == candNet) {
1782 if (targetSubnet == candSubnet)
1783 (*a_sameNetOrSubnetP)++;
1788 (*a_diffNetworkP)++;
1790 } /*h_ClassifyAddress*/
1793 /*------------------------------------------------------------------------
1794 * EXPORTED h_GetHostNetStats
1797 * Iterate through the host table, and classify each (non-deleted)
1798 * host entry into ``proximity'' categories (same net or subnet,
1799 * different subnet, different network).
1802 * a_numHostsP : Set to total number of (non-deleted) hosts.
1803 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
1804 * a_diffSubnetP : Set to # hosts on diff subnet as server.
1805 * a_diffNetworkP : Set to # hosts on diff network as server.
1811 * We only count non-deleted hosts. The storage pointed to by our
1812 * parameters is zeroed upon entry.
1816 *------------------------------------------------------------------------*/
1818 void h_GetHostNetStats(a_numHostsP, a_sameNetOrSubnetP, a_diffSubnetP,
1820 afs_int32 *a_numHostsP;
1821 afs_int32 *a_sameNetOrSubnetP;
1822 afs_int32 *a_diffSubnetP;
1823 afs_int32 *a_diffNetworkP;
1825 { /*h_GetHostNetStats*/
1827 register struct host *hostP; /*Ptr to current host entry*/
1828 register afs_uint32 currAddr_HBO; /*Curr host addr, host byte order*/
1831 * Clear out the storage pointed to by our parameters.
1833 *a_numHostsP = (afs_int32) 0;
1834 *a_sameNetOrSubnetP = (afs_int32) 0;
1835 *a_diffSubnetP = (afs_int32) 0;
1836 *a_diffNetworkP = (afs_int32) 0;
1839 for (hostP = hostList; hostP; hostP = hostP->next) {
1840 if (!(hostP->hostFlags & HOSTDELETED)) {
1842 * Bump the number of undeleted host entries found.
1843 * In classifying the current entry's address, make
1844 * sure to first convert to host byte order.
1847 currAddr_HBO = (afs_uint32)ntohl(hostP->host);
1848 h_ClassifyAddress(FS_HostAddr_HBO,
1853 } /*Only look at non-deleted hosts*/
1854 } /*For each host record hashed to this index*/
1857 } /*h_GetHostNetStats*/
1859 static afs_uint32 checktime;
1860 static afs_uint32 clientdeletetime;
1861 static struct AFSFid zerofid;
1865 * XXXX: This routine could use Multi-R to avoid serializing the timeouts.
1866 * Since it can serialize them, and pile up, it should be a separate LWP
1867 * from other events.
1869 int CheckHost(host, held)
1870 register struct host *host;
1874 register struct client *client;
1875 struct interfaceAddr interf;
1878 /* Host is held by h_Enumerate */
1880 for (client = host->FirstClient; client; client = client->next) {
1881 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
1882 client->deleted = 1;
1883 host->hostFlags |= CLIENTDELETED;
1886 if (host->LastCall < checktime) {
1888 if (!(host->hostFlags & HOSTDELETED)) {
1889 if (host->LastCall < clientdeletetime) {
1890 host->hostFlags |= HOSTDELETED;
1891 if (!(host->hostFlags & VENUSDOWN)) {
1892 host->hostFlags &= ~ALTADDR; /* alternate address invalid*/
1893 if (host->interface) {
1895 code = RXAFSCB_InitCallBackState3(host->callback_rxcon,
1900 code = RXAFSCB_InitCallBackState(host->callback_rxcon);
1903 host->hostFlags |= ALTADDR; /* alternate addresses valid */
1907 ("CB: RCallBackConnectBack (host.c) failed for host %x.%d\n",
1908 host->host, host->port));
1909 host->hostFlags |= VENUSDOWN;
1911 /* Note: it's safe to delete hosts even if they have call
1912 * back state, because break delayed callbacks (called when a
1913 * message is received from the workstation) will always send a
1914 * break all call backs to the workstation if there is no
1920 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
1921 if (host->interface) {
1922 afsUUID uuid = host->interface->uuid;
1924 code = RXAFSCB_ProbeUuid(host->callback_rxcon, &uuid);
1927 if ( MultiProbeAlternateAddress_r(host) ) {
1929 ("ProbeUuid failed for host %x.%d\n",
1930 host->host, host->port));
1931 host->hostFlags |= VENUSDOWN;
1936 code = RXAFSCB_Probe(host->callback_rxcon);
1939 ViceLog(0, ("ProbeUuid failed for host %x.%d\n",
1940 host->host, host->port));
1941 host->hostFlags |= VENUSDOWN;
1956 * Set VenusDown for any hosts that have not had a call in 15 minutes and
1957 * don't respond to a probe. Note that VenusDown can only be cleared if
1958 * a message is received from the host (see ServerLWP in file.c).
1959 * Delete hosts that have not had any calls in 1 hour, clients that
1960 * have not had any calls in 15 minutes.
1962 * This routine is called roughly every 5 minutes.
1966 afs_uint32 now = FT_ApproxTime();
1968 bzero((char *)&zerofid, sizeof(zerofid));
1970 * Send a probe to the workstation if it hasn't been heard from in
1973 checktime = now - 15*60;
1974 clientdeletetime = now - 120*60; /* 2 hours ago */
1975 h_Enumerate(CheckHost, (char *) 0);
1980 * This is called with host locked and held. At this point, the
1981 * hostHashTable should not be having entries for the alternate
1982 * interfaces. This function has to insert these entries in the
1985 * The addresses in the ineterfaceAddr list are in host byte order.
1988 initInterfaceAddr_r(host, interf)
1990 struct interfaceAddr *interf;
1994 afs_int32 myPort, myHost;
1996 struct Interface *interface;
2001 ViceLog(125,("initInterfaceAddr : host %x numAddr %d\n",
2002 host->host, interf->numberOfInterfaces));
2004 number = interf->numberOfInterfaces;
2005 myPort = host->port;
2006 myHost = host->host; /* current interface address */
2008 /* validation checks */
2011 ViceLog(0,("Number of alternate addresses returned is %d\n",
2017 * Convert IP addresses to network byte order, and remove for
2018 * duplicate IP addresses from the interface list.
2020 for (i = 0, count = 0, found = 0; i < number; i++)
2022 interf->addr_in[i] = htonl(interf->addr_in[i]);
2023 for (j = 0 ; j < count ; j++) {
2024 if (interf->addr_in[j] == interf->addr_in[i])
2028 interf->addr_in[count] = interf->addr_in[i];
2029 if (interf->addr_in[count] == myHost)
2036 * Allocate and initialize an interface structure for this host.
2039 interface = (struct Interface *)
2040 malloc(sizeof(struct Interface) +
2041 (sizeof(afs_int32) * (count-1)));
2043 interface->numberOfInterfaces = count;
2045 interface = (struct Interface *)
2046 malloc(sizeof(struct Interface) +
2047 (sizeof(afs_int32) * count));
2049 interface->numberOfInterfaces = count + 1;
2050 interface->addr[count] = myHost;
2052 interface->uuid = interf->uuid;
2053 for (i = 0 ; i < count ; i++)
2054 interface->addr[i] = interf->addr_in[i];
2056 assert(!host->interface);
2057 host->interface = interface;
2059 for ( i=0; i < host->interface->numberOfInterfaces; i++)
2061 ViceLog(125,("--- alt address %x\n", host->interface->addr[i]));
2068 * This is called with host locked and held. At this point, the
2069 * hostHashTable should not be having entries for the alternate
2070 * interfaces. This function has to insert these entries in the
2073 * All addresses are in network byte order.
2076 addInterfaceAddr_r(host, addr)
2083 struct Interface *interface;
2086 assert(host->interface);
2088 ViceLog(125,("addInterfaceAddr : host %x addr %d\n",
2092 * Make sure this address is on the list of known addresses
2095 number = host->interface->numberOfInterfaces;
2096 for ( i=0, found=0; i < number && !found; i++)
2098 if ( host->interface->addr[i] == addr)
2102 interface = (struct Interface *)
2103 malloc(sizeof(struct Interface) +
2104 (sizeof(afs_int32) * number));
2105 interface->numberOfInterfaces = number + 1;
2106 interface->uuid = host->interface->uuid;
2107 for (i = 0 ; i < number ; i++)
2108 interface->addr[i] = host->interface->addr[i];
2109 interface->addr[number] = addr;
2110 free(host->interface);
2111 host->interface = interface;
2115 * Create a hash table entry for this address
2117 hashInsert_r(addr, host);
2122 /* inserts a new HashChain structure corresponding to this address */
2123 hashInsert_r(addr, host)
2128 struct h_hashChain* chain;
2130 /* hash into proper bucket */
2131 index = h_HashIndex(addr);
2133 /* insert into beginning of list for this bucket */
2134 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
2136 chain->hostPtr = host;
2137 chain->next = hostHashTable[index];
2139 hostHashTable[index] = chain;
2143 /* inserts a new HashChain structure corresponding to this UUID */
2144 hashInsertUuid_r(uuid, host)
2145 struct afsUUID *uuid;
2149 struct h_hashChain* chain;
2151 /* hash into proper bucket */
2152 index = h_UuidHashIndex(uuid);
2154 /* insert into beginning of list for this bucket */
2155 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
2157 chain->hostPtr = host;
2158 chain->next = hostUuidHashTable[index];
2159 hostUuidHashTable[index] = chain;
2162 /* deleted a HashChain structure for this address and host */
2163 /* returns 1 on success */
2165 hashDelete_r(addr, host)
2171 register struct h_hashChain **hp, *th;
2173 for (hp = &hostHashTable[h_HashIndex(addr)]; th = *hp; )
2175 assert(th->hostPtr);
2176 if (th->hostPtr == host && th->addr == addr)
2191 ** prints out all alternate interface address for the host. The 'level'
2192 ** parameter indicates what level of debugging sets this output
2194 printInterfaceAddr(host, level)
2199 if ( host-> interface )
2201 /* check alternate addresses */
2202 number = host->interface->numberOfInterfaces;
2203 assert( number > 0 );
2204 for ( i=0; i < number; i++)
2205 ViceLog(level, ("%x ", host->interface->addr[i]));
2207 ViceLog(level, ("\n"));