2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afs/param.h>
19 #include <netinet/in.h>
23 #include <afs/assert.h>
26 #include <afs/afsint.h>
27 #include <afs/rxgen_consts.h>
29 #include <afs/errors.h>
30 #include <afs/ihandle.h>
31 #include <afs/vnode.h>
32 #include <afs/volume.h>
33 #ifdef AFS_ATHENA_STDENV
37 #include <afs/ptclient.h>
38 #include <afs/prs_fs.h>
40 #include <afs/afsutil.h>
42 #include <afs/cellconfig.h>
48 #ifdef AFS_PTHREAD_ENV
49 pthread_mutex_t host_glock_mutex;
50 #endif /* AFS_PTHREAD_ENV */
53 extern int CurrentConnections;
55 extern int AnonymousID;
56 extern prlist AnonCPS;
58 extern struct afsconf_dir *confDir; /* config dir object */
59 extern int lwps; /* the max number of server threads */
60 extern afsUUID FS_HostUUID;
62 int CEs = 0; /* active clients */
63 int CEBlocks = 0; /* number of blocks of CEs */
64 struct client *CEFree = 0; /* first free client */
65 struct host *hostList = 0; /* linked list of all hosts */
66 int hostCount = 0; /* number of hosts in hostList */
70 #define CESPERBLOCK 73
71 struct CEBlock /* block of CESPERBLOCK file entries */
73 struct client entry[CESPERBLOCK];
77 * Make sure the subnet macros have been defined.
80 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
83 #ifndef IN_CLASSA_SUBNET
84 #define IN_CLASSA_SUBNET 0xffff0000
88 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
91 #ifndef IN_CLASSB_SUBNET
92 #define IN_CLASSB_SUBNET 0xffffff00
95 #define rxr_GetEpoch(aconn) (((struct rx_connection *)(aconn))->epoch)
97 #define rxr_CidOf(aconn) (((struct rx_connection *)(aconn))->cid)
99 #define rxr_PortOf(aconn) \
100 rx_PortOf(rx_PeerOf(((struct rx_connection *)(aconn))))
102 #define rxr_HostOf(aconn) \
103 rx_HostOf(rx_PeerOf((struct rx_connection *)(aconn)))
106 /* get a new block of CEs and chain it on CEFree */
107 static void GetCEBlock()
109 register struct CEBlock *block;
112 block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
114 ShutDownAndCore(PANIC);
116 for(i = 0; i < (CESPERBLOCK -1); i++) {
117 Lock_Init(&block->entry[i].lock);
118 block->entry[i].next = &(block->entry[i+1]);
120 block->entry[CESPERBLOCK-1].next = 0;
121 Lock_Init(&block->entry[CESPERBLOCK-1].lock);
122 CEFree = (struct client *)block;
128 /* get the next available CE */
129 static struct client *GetCE()
132 register struct client *entry;
137 ShutDownAndCore(PANIC);
140 CEFree = entry->next;
142 bzero((char *)entry, CLIENT_TO_ZERO(entry));
148 /* return an entry to the free list */
149 static void FreeCE(entry)
150 register struct client *entry;
153 entry->next = CEFree;
160 * The HTs and HTBlocks variables were formerly static, but they are
161 * now referenced elsewhere in the FileServer.
163 int HTs = 0; /* active file entries */
164 int HTBlocks = 0; /* number of blocks of HTs */
165 static struct host *HTFree = 0; /* first free file entry */
168 * Hash tables of host pointers. We need two tables, one
169 * to map IP addresses onto host pointers, and another
170 * to map host UUIDs onto host pointers.
172 static struct h_hashChain *hostHashTable[h_HASHENTRIES];
173 static struct h_hashChain *hostUuidHashTable[h_HASHENTRIES];
174 #define h_HashIndex(hostip) ((hostip) & (h_HASHENTRIES-1))
175 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
177 struct HTBlock /* block of HTSPERBLOCK file entries */
179 struct host entry[h_HTSPERBLOCK];
183 /* get a new block of HTs and chain it on HTFree */
184 static void GetHTBlock()
187 register struct HTBlock *block;
189 static int index = 0;
191 block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
193 ShutDownAndCore(PANIC);
195 #ifdef AFS_PTHREAD_ENV
196 for(i=0; i < (h_HTSPERBLOCK); i++)
197 assert(pthread_cond_init(&block->entry[i].cond, NULL) == 0);
198 #endif /* AFS_PTHREAD_ENV */
199 for(i=0; i < (h_HTSPERBLOCK); i++)
200 Lock_Init(&block->entry[i].lock);
201 for(i=0; i < (h_HTSPERBLOCK -1); i++)
202 block->entry[i].next = &(block->entry[i+1]);
203 for (i=0; i< (h_HTSPERBLOCK); i++)
204 block->entry[i].index = index++;
205 block->entry[h_HTSPERBLOCK-1].next = 0;
206 HTFree = (struct host *)block;
207 hosttableptrs[HTBlocks++] = block->entry;
212 /* get the next available HT */
213 static struct host *GetHT()
216 register struct host *entry;
222 HTFree = entry->next;
224 bzero((char *)entry, HOST_TO_ZERO(entry));
230 /* return an entry to the free list */
231 static void FreeHT(entry)
232 register struct host *entry;
235 entry->next = HTFree;
242 static short consolePort = 0;
245 register struct host *host;
254 * If this thread does not have a hold on this host AND
255 * if other threads also dont have any holds on this host AND
256 * If either the HOSTDELETED or CLIENTDELETED flags are set
259 int h_Release_r(host)
260 register struct host *host;
263 if (!((host)->holds[h_holdSlot()] &= ~h_holdbit()) ) {
264 if (! h_OtherHolds_r(host) ) {
265 if ( (host->hostFlags & HOSTDELETED) ||
266 (host->hostFlags & CLIENTDELETED) ) {
275 register struct host *host;
279 retVal = h_Held_r(host);
284 int h_OtherHolds_r(host)
285 register struct host *host;
287 register int i, bit, slot;
290 for (i = 0 ; i < h_maxSlots ; i++) {
291 if (host->holds[i] != ((i == slot) ? bit : 0)) {
298 int h_OtherHolds(host)
299 register struct host *host;
303 retVal = h_OtherHolds_r(host);
309 register struct host *host;
319 * returns 1 if already locked
320 * else returns locks and returns 0
324 register struct host *host;
326 struct Lock *hostLock = &host->lock;
331 if ( !(hostLock->excl_locked) && !(hostLock->readers_reading) )
332 hostLock->excl_locked = WRITE_LOCK;
336 LOCK_UNLOCK(hostLock)
345 #if FS_STATS_DETAILED
346 /*------------------------------------------------------------------------
347 * PRIVATE h_AddrInSameNetwork
350 * Given a target IP address and a candidate IP address (both
351 * in host byte order), return a non-zero value (1) if the
352 * candidate address is in a different network from the target
356 * a_targetAddr : Target address.
357 * a_candAddr : Candidate address.
360 * 1 if the candidate address is in the same net as the target,
364 * The target and candidate addresses are both in host byte
365 * order, NOT network byte order, when passed in. We return
366 * our value as a character, since that's the type of field in
367 * the host structure, where this info will be stored.
371 *------------------------------------------------------------------------*/
373 static char h_AddrInSameNetwork(a_targetAddr, a_candAddr)
374 afs_uint32 a_targetAddr;
375 afs_uint32 a_candAddr;
377 { /*h_AddrInSameNetwork*/
379 afs_uint32 targetNet;
383 * Pull out the network and subnetwork numbers from the target
384 * and candidate addresses. We can short-circuit this whole
385 * affair if the target and candidate addresses are not of the
388 if (IN_CLASSA(a_targetAddr)) {
389 if (!(IN_CLASSA(a_candAddr))) {
392 targetNet = a_targetAddr & IN_CLASSA_NET;
393 candNet = a_candAddr & IN_CLASSA_NET;
396 if (IN_CLASSB(a_targetAddr)) {
397 if (!(IN_CLASSB(a_candAddr))) {
400 targetNet = a_targetAddr & IN_CLASSB_NET;
401 candNet = a_candAddr & IN_CLASSB_NET;
404 if (IN_CLASSC(a_targetAddr)) {
405 if (!(IN_CLASSC(a_candAddr))) {
408 targetNet = a_targetAddr & IN_CLASSC_NET;
409 candNet = a_candAddr & IN_CLASSC_NET;
412 targetNet = a_targetAddr;
413 candNet = a_candAddr;
414 } /*Class D address*/
417 * Now, simply compare the extracted net values for the two addresses
418 * (which at this point are known to be of the same class)
420 if (targetNet == candNet)
425 } /*h_AddrInSameNetwork*/
426 #endif /* FS_STATS_DETAILED */
430 h_gethostcps_r(host,now)
431 register struct host *host;
432 register afs_int32 now;
437 /* at this point, the host might not be locked, nor held */
438 /* make sure that we do not disappear behind the RPC */
439 if ( !(held = h_Held_r(host)) )
442 /* wait if somebody else is already doing the getCPS call */
443 while ( host->hostFlags & HPCS_INPROGRESS )
445 slept = 1; /* I did sleep */
446 host->hostFlags |= HPCS_WAITING; /* I am sleeping now */
447 #ifdef AFS_PTHREAD_ENV
448 pthread_cond_wait(&host->cond, &host_glock_mutex);
449 #else /* AFS_PTHREAD_ENV */
450 if (( code = LWP_WaitProcess( &(host->hostFlags ))) != LWP_SUCCESS)
451 ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
452 #endif /* AFS_PTHREAD_ENV */
456 host->hostFlags |= HPCS_INPROGRESS; /* mark as CPSCall in progress */
457 if (host->hcps.prlist_val)
458 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
459 host->hcps.prlist_val = (afs_int32 *)0;
460 host->hcps.prlist_len = 0;
461 slept? (host->cpsCall = FT_ApproxTime()): (host->cpsCall = now );
464 code = pr_GetHostCPS(htonl(host->host), &host->hcps);
468 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
469 * and reevaluates things if no sync server or quorum is found we could still end up
470 * with one of these errors. In such case we would like to reevaluate the rpc call to
471 * find if there's cps for this guy. We treat other errors (except network failures
472 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
473 * we could like to deal this problem the other way around (i.e. if code == NOCPS
474 * ignore else retry next time) but the problem is that there're other errors (i.e.
475 * EPERM) for which we don't want to retry and we don't know the whole code list!
477 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
479 * We would have preferred to use a while loop and try again since ops in protected
480 * acls for this host will fail now but they'll be reevaluated on any subsequent
481 * call. The attempt to wait for a quorum/sync site or network error won't work
482 * since this problems really should only occurs during a complete fileserver
483 * restart. Since the fileserver will start before the ptservers (and thus before
484 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
486 host->hcpsfailed = 1;
487 ViceLog(0, ("Warning: GetHostCPS failed (%d) for %x; will retry\n", code, host->host));
489 host->hcpsfailed = 0;
490 ViceLog(1, ("gethost: GetHostCPS failed (%d) for %x; ignored\n", code, host->host));
492 if (host->hcps.prlist_val)
493 free(host->hcps.prlist_val);
494 host->hcps.prlist_val = (afs_int32 *)0;
495 host->hcps.prlist_len = 0; /* Make sure it's zero */
497 host->hcpsfailed = 0;
499 host->hostFlags &= ~HPCS_INPROGRESS;
500 /* signal all who are waiting */
501 if ( host->hostFlags & HPCS_WAITING) /* somebody is waiting */
503 host->hostFlags &= ~HPCS_WAITING;
504 #ifdef AFS_PTHREAD_ENV
505 assert(pthread_cond_broadcast(&host->cond) == 0);
506 #else /* AFS_PTHREAD_ENV */
507 if ( (code = LWP_NoYieldSignal( &(host->hostFlags) )) != LWP_SUCCESS )
508 ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
509 #endif /* AFS_PTHREAD_ENV */
512 /* if we had held the host, release it now */
517 void h_flushhostcps(hostaddr, hport)
518 register afs_uint32 hostaddr, hport; /* net byte order */
520 register struct host *host;
523 host = h_Lookup_r(hostaddr, hport);
525 host->hcpsfailed = 1;
534 * Allocate a host. It will be identified by the peer (ip,port) info in the
535 * rx connection provided. The host is returned un-held and un-locked
537 #define DEF_ROPCONS 2115
539 struct host *h_Alloc(r_con)
540 register struct rx_connection *r_con;
544 retVal = h_Alloc_r(r_con);
549 struct host *h_Alloc_r(r_con)
550 register struct rx_connection *r_con;
554 struct servent *serverentry;
555 register index = h_HashIndex(rxr_HostOf(r_con));
556 register struct host *host;
557 static struct rx_securityClass *sc = 0;
559 struct h_hashChain* h_hashChain;
560 #if FS_STATS_DETAILED
561 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order*/
562 #endif /* FS_STATS_DETAILED */
566 h_hashChain = (struct h_hashChain*) malloc(sizeof(struct h_hashChain));
568 h_hashChain->hostPtr = host;
569 h_hashChain->addr = rxr_HostOf(r_con);
570 h_hashChain->next = hostHashTable[index];
571 hostHashTable[index] = h_hashChain;
573 host->host = rxr_HostOf(r_con);
574 host->port = rxr_PortOf(r_con);
575 if(consolePort == 0 ) { /* find the portal number for console */
576 #if defined(AFS_OSF_ENV)
577 serverentry = getservbyname("ropcons", "");
579 serverentry = getservbyname("ropcons", 0);
582 consolePort = serverentry->s_port;
584 consolePort = DEF_ROPCONS; /* Use a default */
586 if (host->port == consolePort) host->Console = 1;
587 /* Make a callback channel even for the console, on the off chance that it
588 makes a request that causes a break call back. It shouldn't. */
591 sc = (struct rx_securityClass *) rxnull_NewClientSecurityObject();
592 host->callback_rxcon = rx_NewConnection (host->host, host->port,
594 rx_SetConnDeadTime(host->callback_rxcon, 50);
595 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
597 now = host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
599 host->hcps.prlist_val = (afs_int32 *)0;
600 host->hcps.prlist_len = 0;
601 host->hcps.prlist_val = (afs_int32 *)0;
603 /*host->hcpsfailed = 0; /* save cycles */
604 /* h_gethostcps(host); do this under host lock */
605 host->FirstClient = 0;
606 h_InsertList_r(host); /* update global host List */
607 #if FS_STATS_DETAILED
609 * Compare the new host's IP address (in host byte order) with ours
610 * (the File Server's), remembering if they are in the same network.
612 newHostAddr_HBO = (afs_uint32)ntohl(host->host);
613 host->InSameNetwork = h_AddrInSameNetwork(FS_HostAddr_HBO,
615 #endif /* FS_STATS_DETAILED */
621 /* Lookup a host given an IP address and UDP port number. */
622 struct host *h_Lookup(hostaddr, hport)
623 afs_uint32 hostaddr, hport; /* network byte order */
627 retVal = h_Lookup_r(hostaddr, hport);
632 struct host *h_Lookup_r(hostaddr, hport)
633 afs_uint32 hostaddr, hport; /* network byte order */
635 register afs_int32 now;
636 register struct host *host=0;
637 register struct h_hashChain* chain;
638 register index = h_HashIndex(hostaddr);
639 extern int hostaclRefresh;
641 for (chain=hostHashTable[index]; chain; chain=chain->next) {
642 host = chain->hostPtr;
644 if (!(host->hostFlags & HOSTDELETED) && chain->addr == hostaddr
645 && host->port == hport) {
646 now = FT_ApproxTime(); /* always evaluate "now" */
647 if (host->hcpsfailed || (host->cpsCall+hostaclRefresh < now )) {
649 * Every hostaclRefresh period (def 2 hrs) get the new membership list for the host.
650 * Note this could be the first time that the host is added to a group.
651 * Also here we also retry on previous legitimate hcps failures
653 h_gethostcps_r(host,now);
663 /* Lookup a host given its UUID. */
664 struct host *h_LookupUuid_r(uuidp)
667 register struct host *host=0;
668 register struct h_hashChain* chain;
669 register index = h_UuidHashIndex(uuidp);
671 for (chain=hostUuidHashTable[index]; chain; chain=chain->next) {
672 host = chain->hostPtr;
674 if (!(host->hostFlags & HOSTDELETED) && host->interface
675 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
686 * h_Hold: Establish a hold by the current LWP on this host--the host
687 * or its clients will not be physically deleted until all holds have
690 * NOTE: h_Hold_r is a macro defined in host.h.
694 register struct host *host;
703 /* h_TossStuff: Toss anything in the host structure (the host or
704 * clients marked for deletion. Called from r_Release ONLY.
705 * To be called, there must be no holds, and either host->deleted
706 * or host->clientDeleted must be set.
709 register struct host *host;
712 register struct client **cp, *client;
715 /* if somebody still has this host held */
716 for (i=0; (i<h_maxSlots)&&(!(host)->holds[i]); i++);
720 /* ASSUMPTION: r_FreeConnection() does not yield */
721 for (cp = &host->FirstClient; client = *cp; ) {
722 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
723 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val) {
724 free(client->CPS.prlist_val);
725 client->CPS.prlist_val = (afs_int32 *)0;
728 rx_SetSpecific(client->tcon, rxcon_client_key, (void *)0);
730 CurrentConnections--;
733 } else cp = &client->next;
735 if (host->hostFlags & HOSTDELETED) {
736 register struct h_hashChain **hp, *th;
737 register struct rx_connection *rxconn;
742 if (host->Console & 1) Console--;
743 if (rxconn = host->callback_rxcon) {
744 host->callback_rxcon = (struct rx_connection *)0;
746 * If rx_DestroyConnection calls h_FreeConnection we will
747 * deadlock on the host_glock_mutex. Work around the problem
748 * by unhooking the client from the connection before
749 * destroying the connection.
751 client = rx_GetSpecific(rxconn, rxcon_client_key);
752 if (client && client->tcon == rxconn)
754 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
755 rx_DestroyConnection(rxconn);
757 if (host->hcps.prlist_val)
758 free(host->hcps.prlist_val);
759 host->hcps.prlist_val = (afs_int32 *)0;
760 host->hcps.prlist_len = 0;
761 DeleteAllCallBacks_r(host);
762 host->hostFlags &= ~RESETDONE; /* just to be safe */
764 /* if alternate addresses do not exist */
765 if ( !(host->interface) )
767 for (hp = &hostHashTable[h_HashIndex(host->host)];
768 th = *hp; hp = &th->next)
771 if (th->hostPtr == host)
774 h_DeleteList_r(host);
782 /* delete all hash entries for the UUID */
783 uuidp = &host->interface->uuid;
784 for (hp = &hostUuidHashTable[h_UuidHashIndex(uuidp)];
785 th = *hp; hp = &th->next) {
787 if (th->hostPtr == host)
794 /* delete all hash entries for alternate addresses */
795 assert(host->interface->numberOfInterfaces > 0 );
796 for ( i=0; i < host->interface->numberOfInterfaces; i++)
798 hostAddr = host->interface->addr[i];
799 for (hp = &hostHashTable[h_HashIndex(hostAddr)];
800 th = *hp; hp = &th->next)
803 if (th->hostPtr == host)
811 free(host->interface);
812 host->interface = NULL;
813 h_DeleteList_r(host); /* remove host from global host List */
815 } /* if alternate address exists */
820 /* Called by rx when a server connection disappears */
821 h_FreeConnection(tcon)
822 struct rx_connection *tcon;
825 register struct client *client;
827 client = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
830 if (client->tcon == tcon)
831 client->tcon = (struct rx_connection *)0;
834 } /*h_FreeConnection*/
837 /* h_Enumerate: Calls (*proc)(host, held, param) for at least each host in the
838 * system at the start of the enumeration (perhaps more). Hosts may be deleted
839 * (have delete flag set); ditto for clients. (*proc) is always called with
840 * host h_held(). The hold state of the host with respect to this lwp is passed
841 * to (*proc) as the param held. The proc should return 0 if the host should be
842 * released, 1 if it should be held after enumeration.
844 h_Enumerate(proc, param)
849 register struct host *host, **list;
851 register int i, count;
854 if (hostCount == 0) {
858 list = (struct host **)malloc(hostCount * sizeof(struct host *));
859 assert(list != NULL);
860 held = (int *)malloc(hostCount * sizeof(int));
861 assert(held != NULL);
862 for (count = 0, host = hostList ; host ; host = host->next, count++) {
864 if (!(held[count] = h_Held_r(host)))
867 assert(count == hostCount);
869 for ( i = 0 ; i < count ; i++) {
870 held[i] = (*proc)(list[i], held[i], param);
872 h_Release(list[i]);/* this might free up the host */
878 /* h_Enumerate_r: Calls (*proc)(host, held, param) for at least each host in
879 * the at the start of the enumeration (perhaps more). Hosts may be deleted
880 * (have delete flag set); ditto for clients. (*proc) is always called with
881 * host h_held() and the global host lock (H_LOCK) locked.The hold state of the
882 * host with respect to this lwp is passed to (*proc) as the param held.
883 * The proc should return 0 if the host should be released, 1 if it should
884 * be held after enumeration.
886 h_Enumerate_r(proc, param)
891 register struct host *host;
894 if (hostCount == 0) {
897 for (host = hostList ; host ; host = host->next) {
898 if (!(held = h_Held_r(host)))
900 held = (*proc)(host, held, param);
902 h_Release_r(host);/* this might free up the host */
907 /* Host is returned held */
908 struct host *h_GetHost_r(tcon)
909 struct rx_connection *tcon;
913 struct host *oldHost;
916 struct interfaceAddr interf;
918 afs_int32 buffer[AFS_MAX_INTERFACE_ADDR];
919 struct Identity *identP = NULL;
924 haddr = rxr_HostOf(tcon);
925 hport = rxr_PortOf(tcon);
928 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
929 host = h_Lookup_r(haddr, hport);
930 if (host && !identP && !(host->Console&1)) {
931 /* This is a new connection, and we already have a host
932 * structure for this address. Verify that the identity
933 * of the caller matches the identity in the host structure.
935 if (!(held = h_Held_r(host)))
938 if ( !(host->hostFlags & ALTADDR) )
940 /* Another thread is doing initialization */
942 if ( !held) h_Release_r(host);
943 ViceLog(125, ("Host %x starting h_Lookup again\n", host));
946 host->hostFlags &= ~ALTADDR;
948 code = RXAFSCB_WhoAreYou(host->callback_rxcon, &interf);
950 if ( code == RXGEN_OPCODE ) {
951 identP = (struct Identity *)malloc(1);
953 rx_SetSpecific(tcon, rxcon_ident_key, identP);
954 /* The host on this connection was unable to respond to
955 * the WhoAreYou. We will treat this as a new connection
956 * from the existing host. The worst that can happen is
957 * that we maintain some extra callback state information */
958 if (host->interface) {
960 ("Host %x used to support WhoAreYou, deleting.\n",
962 host->hostFlags |= HOSTDELETED;
964 if (!held) h_Release_r(host);
968 } else if (code == 0) {
970 identP = (struct Identity *)malloc(sizeof(struct Identity));
972 identP->uuid = interf.uuid;
973 rx_SetSpecific(tcon, rxcon_ident_key, identP);
974 /* Check whether the UUID on this connection matches
975 * the UUID in the host structure. If they don't match
976 * then this is not the same host as before. */
977 if ( !host->interface
978 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid) ) {
980 ("Host %x has changed its identity, deleting.\n",
982 host->hostFlags |= HOSTDELETED;
984 if (!held) h_Release_r(host);
989 char *hoststr = afs_inet_ntoa_r(host->host);
990 ViceLog(0,("CB: WhoAreYou failed for %s:%d, error %d\n",
991 hoststr, ntohs(host->port), code));
992 host->hostFlags |= VENUSDOWN;
995 host->hostFlags |= ALTADDR;
998 if (!(held = h_Held_r(host)))
1000 if ( ! (host->hostFlags & ALTADDR) )
1002 /* another thread is doing the initialisation */
1003 ViceLog(125, ("Host %x waiting for host-init to complete\n",
1007 if ( !held) h_Release_r(host);
1008 ViceLog(125, ("Host %x starting h_Lookup again\n", host));
1011 /* We need to check whether the identity in the host structure
1012 * matches the identity on the connection. If they don't match
1013 * then treat this a new host. */
1014 if ( !(host->Console&1)
1015 && ( ( !identP->valid && host->interface )
1016 || ( identP->valid && !host->interface )
1018 && !afs_uuid_equal(&identP->uuid, &host->interface->uuid) ) ) ) {
1019 /* The host in the cache is not the host for this connection */
1020 host->hostFlags |= HOSTDELETED;
1022 if (!held) h_Release_r(host);
1023 ViceLog(0,("CB: new identity for host %x, deleting\n",
1028 host = h_Alloc_r(tcon);
1031 h_gethostcps_r(host,FT_ApproxTime());
1032 if (!(host->Console&1)) {
1033 if (!identP || !interfValid) {
1035 code = RXAFSCB_WhoAreYou(host->callback_rxcon, &interf);
1037 if ( code == RXGEN_OPCODE ) {
1038 identP = (struct Identity *)malloc(1);
1040 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1042 ("Host %x does not support WhoAreYou.\n",
1045 } else if (code == 0) {
1047 identP = (struct Identity *)malloc(sizeof(struct Identity));
1049 identP->uuid = interf.uuid;
1050 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1051 ViceLog(25,("WhoAreYou success on %x\n", host->host));
1054 if (code == 0 && !identP->valid) {
1056 code = RXAFSCB_InitCallBackState(host->callback_rxcon);
1058 } else if (code == 0) {
1059 oldHost = h_LookupUuid_r(&identP->uuid);
1061 /* This is a new address for an existing host. Update
1062 * the list of interfaces for the existing host and
1063 * delete the host structure we just allocated. */
1064 if (!(held = h_Held_r(oldHost)))
1067 ViceLog(25,("CB: new addr %x for old host %x\n",
1068 host->host, oldHost->host));
1069 host->hostFlags |= HOSTDELETED;
1073 addInterfaceAddr_r(host, haddr);
1075 /* This really is a new host */
1076 hashInsertUuid_r(&identP->uuid, host);
1078 code = RXAFSCB_InitCallBackState3(host->callback_rxcon,
1082 ViceLog(25,("InitCallBackState3 success on %x\n",
1084 assert(interfValid == 1);
1085 initInterfaceAddr_r(host, &interf);
1090 char *hoststr = afs_inet_ntoa_r(host->host);
1091 ViceLog(0,("CB: RCallBackConnectBack failed for %s:%d\n",
1092 hoststr, ntohs(host->port)));
1093 host->hostFlags |= VENUSDOWN;
1097 host->hostFlags |= RESETDONE;
1100 host->hostFlags |= ALTADDR;/* host structure iniatilisation complete */
1108 static char localcellname[PR_MAXNAMELEN+1];
1109 char local_realm[AFS_REALM_SZ] = "";
1112 void h_InitHostPackage()
1114 afsconf_GetLocalCell (confDir, localcellname, PR_MAXNAMELEN);
1115 if (!local_realm[0]) {
1116 if (afs_krb_get_lrealm(local_realm, 0) != 0/*KSUCCESS*/) {
1117 ViceLog(0, ("afs_krb_get_lrealm failed, using %s.\n",localcellname));
1118 strcpy (local_realm, localcellname);
1121 rxcon_ident_key = rx_KeyCreate((rx_destructor_t)free);
1122 rxcon_client_key = rx_KeyCreate((rx_destructor_t)0);
1123 #ifdef AFS_PTHREAD_ENV
1124 assert(pthread_mutex_init(&host_glock_mutex, NULL) == 0);
1125 #endif /* AFS_PTHREAD_ENV */
1128 static MapName_r(aname, acell, aval)
1137 afs_int32 anamelen, cnamelen;
1141 anamelen=strlen(aname);
1142 if (anamelen >= PR_MAXNAMELEN)
1143 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
1145 lnames.namelist_len = 1;
1146 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
1147 lids.idlist_len = 0;
1148 lids.idlist_val = (afs_int32 *) 0;
1150 cnamelen=strlen(acell);
1152 if (strcasecmp(local_realm, acell) && strcasecmp(localcellname, acell)) {
1153 ViceLog(2, ("MapName: cell is foreign. cell=%s, localcell=%s, localrealm=%s\n",
1154 acell, localcellname, local_realm));
1155 if ((anamelen+cnamelen+1) >= PR_MAXNAMELEN) {
1156 ViceLog(2, ("MapName: Name too long, using AnonymousID for %s@%s\n",
1158 *aval = AnonymousID;
1161 foreign = 1; /* attempt cross-cell authentication */
1162 tname = (char *) malloc(anamelen+cnamelen+2);
1163 strcpy(tname, aname);
1164 tname[anamelen] = '@';
1165 strcpy(tname+anamelen+1, acell);
1166 lnames.namelist_val = (prname *) tname;
1171 code = pr_NameToId(&lnames, &lids);
1174 if (lids.idlist_val) {
1175 *aval = lids.idlist_val[0];
1176 if (*aval == AnonymousID) {
1177 ViceLog(2, ("MapName: NameToId on %s returns anonymousID\n", lnames.namelist_val));
1179 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
1181 ViceLog(0, ("MapName: NameToId on '%s' is unknown\n", lnames.namelist_val));
1187 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
1194 /* NOTE: this returns the client with a Shared lock */
1195 struct client *h_ID2Client(vid)
1198 register struct client *client;
1199 register struct host *host;
1203 for (host=hostList; host; host=host->next) {
1204 if (host->hostFlags & HOSTDELETED)
1206 for (client = host->FirstClient; client; client = client->next) {
1207 if (!client->deleted && client->ViceId == vid) {
1210 ObtainSharedLock(&client->lock);
1224 * Called by the server main loop. Returns a h_Held client, which must be
1225 * released later the main loop. Allocates a client if the matching one
1226 * isn't around. The client is returned with its reference count incremented
1227 * by one. The caller must call h_ReleaseClient_r when finished with
1230 struct client *h_FindClient_r(tcon)
1231 struct rx_connection *tcon;
1234 register struct client *client;
1235 register struct host *host;
1236 struct client *oldClient;
1241 #if (64-MAXKTCNAMELEN)
1242 ticket name length != 64
1246 char uname[PR_MAXNAMELEN];
1247 char tcell[MAXKTCREALMLEN];
1250 client = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
1251 if (client && !client->deleted) {
1253 h_Hold_r(client->host);
1254 if (client->prfail != 2) { /* Could add shared lock on client here */
1255 /* note that we don't have to lock entry in this path to
1256 * ensure CPS is initialized, since we don't call rxr_SetSpecific
1257 * until initialization is done, and we only get here if
1258 * rx_GetSpecific located the client structure.
1263 ObtainWriteLock(&client->lock); /* released at end */
1265 } else if (client) {
1269 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
1270 ViceLog(5,("FindClient: authenticating connection: authClass=%d\n",
1272 if (authClass == 1) {
1273 /* A bcrypt tickets, no longer supported */
1274 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
1275 viceid = AnonymousID;
1276 expTime = 0x7fffffff;
1277 } else if (authClass == 2) {
1280 /* kerberos ticket */
1281 code = rxkad_GetServerInfo (tcon, /*level*/0, &expTime,
1282 tname, tinst, tcell, &kvno);
1284 ViceLog(1, ("Failed to get rxkad ticket info\n"));
1285 viceid = AnonymousID;
1286 expTime = 0x7fffffff;
1288 int ilen = strlen(tinst);
1290 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
1291 tname, tinst, tcell, expTime, kvno));
1292 strncpy (uname, tname, sizeof(uname));
1294 if (strlen(uname) + 1 + ilen >= sizeof(uname))
1296 strcat (uname, ".");
1297 strcat (uname, tinst);
1299 /* translate the name to a vice id */
1300 code = MapName_r(uname, tcell, &viceid);
1303 ViceLog(1, ("failed to map name=%s, cell=%s -> code=%d\n",
1304 uname, tcell, code));
1306 viceid = AnonymousID;
1307 expTime = 0x7fffffff;
1311 viceid = AnonymousID; /* unknown security class */
1312 expTime = 0x7fffffff;
1316 host = h_GetHost_r(tcon); /* Returns it h_Held */
1318 /* First try to find the client structure */
1319 for (client = host->FirstClient; client; client = client->next) {
1320 if (!client->deleted && (client->sid == rxr_CidOf(tcon)) &&
1321 (client->VenusEpoch == rxr_GetEpoch(tcon))) {
1322 if (client->tcon && (client->tcon != tcon)) {
1323 ViceLog(0, ("*** Vid=%d, sid=%x, tcon=%x, Tcon=%x ***\n",
1324 client->ViceId, client->sid, client->tcon, tcon));
1325 client->tcon = (struct rx_connection *)0;
1329 ObtainWriteLock(&client->lock);
1335 /* Still no client structure - get one */
1338 ObtainWriteLock(&client->lock);
1339 client->host = host;
1340 client->next = host->FirstClient;
1341 host->FirstClient = client;
1342 #if FS_STATS_DETAILED
1343 client->InSameNetwork = host->InSameNetwork;
1344 #endif /* FS_STATS_DETAILED */
1345 client->ViceId = viceid;
1346 client->expTime = expTime; /* rx only */
1347 client->authClass = authClass; /* rx only */
1348 client->sid = rxr_CidOf(tcon);
1349 client->VenusEpoch = rxr_GetEpoch(tcon);
1350 client->CPS.prlist_val = 0;
1351 client->refCount = 1;
1352 CurrentConnections++; /* increment number of connections */
1355 client->prfail = fail;
1357 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
1358 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID)) {
1359 free(client->CPS.prlist_val);
1361 client->CPS.prlist_val = (afs_int32 *)0;
1362 client->ViceId = viceid;
1363 client->expTime = expTime;
1365 if (viceid == ANONYMOUSID) {
1366 client->CPS.prlist_len = AnonCPS.prlist_len;
1367 client->CPS.prlist_val = AnonCPS.prlist_val;
1370 code = pr_GetCPS(viceid, &client->CPS);
1373 ViceLog(0, ("pr_GetCPS failed(%d) for user %d, host %x.%d\n",
1374 code, viceid, client->host->host, ntohs(client->host->port)));
1376 /* Although ubik_Call (called by pr_GetCPS) traverses thru
1377 * all protection servers and reevaluates things if no
1378 * sync server or quorum is found we could still end up
1379 * with one of these errors. In such case we would like to
1380 * reevaluate the rpc call to find if there's cps for this
1381 * guy. We treat other errors (except network failures
1382 * ones - i.e. code < 0) as an indication that there is no
1383 * CPS for this host. Ideally we could like to deal this
1384 * problem the other way around (i.e. if code == NOCPS
1385 * ignore else retry next time) but the problem is that
1386 * there're other errors (i.e. EPERM) for which we don't
1387 * want to retry and we don't know the whole code list!
1389 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
1393 /* the disabling of system:administrators is so iffy and has so many
1394 * possible failure modes that we will disable it again */
1395 /* Turn off System:Administrator for safety
1396 if (AL_IsAMember(SystemId, client->CPS) == 0)
1397 assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
1400 /* Now, tcon may already be set to a rock, since we blocked with no host
1401 * or client locks set above in pr_GetCPS (XXXX some locking is probably
1402 * required). So, before setting the RPC's rock, we should disconnect
1403 * the RPC from the other client structure's rock.
1405 if (oldClient = (struct client *) rx_GetSpecific(tcon, rxcon_client_key)) {
1406 oldClient->tcon = (struct rx_connection *) 0;
1407 /* rx_SetSpecific will be done immediately below */
1409 client->tcon = tcon;
1410 rx_SetSpecific(tcon, rxcon_client_key, client);
1411 ReleaseWriteLock(&client->lock);
1415 } /*h_FindClient_r*/
1417 int h_ReleaseClient_r(client)
1418 struct client *client;
1420 assert(client->refCount > 0);
1427 * Sigh: this one is used to get the client AGAIN within the individual
1428 * server routines. This does not bother h_Holding the host, since
1429 * this is assumed already have been done by the server main loop.
1430 * It does check tokens, since only the server routines can return the
1431 * VICETOKENDEAD error code
1433 int GetClient(tcon, cp)
1434 struct rx_connection * tcon;
1438 register struct client *client;
1442 *cp = client = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
1444 assert(client && client->tcon && rxr_CidOf(client->tcon) == client->sid);
1446 client->LastCall > client->expTime && client->expTime) {
1447 ViceLog(1, ("Token for %s at %x.%d expired %d\n",
1448 h_UserName(client), client->host->host, client->host->port, client->expTime));
1450 return VICETOKENDEAD;
1459 /* Client user name for short term use. Note that this is NOT inexpensive */
1460 char *h_UserName(client)
1461 struct client *client;
1464 static char User[PR_MAXNAMELEN+1];
1468 lids.idlist_len = 1;
1469 lids.idlist_val = (afs_int32 *)malloc(1*sizeof(afs_int32));
1470 lnames.namelist_len = 0;
1471 lnames.namelist_val = (prname *)0;
1472 lids.idlist_val[0] = client->ViceId;
1473 if (pr_IdToName(&lids,&lnames)) {
1474 /* We need to free id we alloced above! */
1475 free(lids.idlist_val);
1476 return "*UNKNOWN USER NAME*";
1478 strncpy(User,lnames.namelist_val[0],PR_MAXNAMELEN);
1479 free(lids.idlist_val);
1480 free(lnames.namelist_val);
1490 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
1491 CEs, CEBlocks, HTs, HTBlocks));
1496 static int h_PrintClient(host, held, file)
1497 register struct host *host;
1499 StreamHandle_t *file;
1501 register struct client *client;
1507 if (host->hostFlags & HOSTDELETED) {
1511 sprintf(tmpStr,"Host %x.%d down = %d, LastCall %s", host->host,
1512 host->port, (host->hostFlags & VENUSDOWN),
1513 afs_ctime((time_t *)&host->LastCall, tbuffer, sizeof(tbuffer)));
1514 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1515 for (client = host->FirstClient; client; client=client->next) {
1516 if (!client->deleted) {
1518 sprintf(tmpStr, " user id=%d, name=%s, sl=%s till %s",
1519 client->ViceId, h_UserName(client),
1520 client->authClass ? "Authenticated" : "Not authenticated",
1522 afs_ctime((time_t *)&client->expTime, tbuffer, sizeof(tbuffer))
1524 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1527 sprintf(tmpStr, " user=%s, no current server connection\n",
1528 h_UserName(client));
1529 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1531 sprintf(tmpStr, " CPS-%d is [", client->CPS.prlist_len);
1532 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1533 if (client->CPS.prlist_val) {
1534 for (i=0; i > client->CPS.prlist_len; i++) {
1535 sprintf(tmpStr, " %d", client->CPS.prlist_val[i]);
1536 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1539 sprintf(tmpStr, "]\n");
1540 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1551 * Print a list of clients, with last security level and token value seen,
1561 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
1564 ViceLog(0, ("Couldn't create client dump file %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
1567 now = FT_ApproxTime();
1568 sprintf(tmpStr, "List of active users at %s\n",
1569 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
1570 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1571 h_Enumerate(h_PrintClient, (char *)file);
1572 STREAM_REALLYCLOSE(file);
1573 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
1579 static int h_DumpHost(host, held, file)
1580 register struct host *host;
1582 StreamHandle_t *file;
1589 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 [",
1590 host->host, host->holds, host->port, host->index, host->cblist,
1591 CheckLock(&host->lock), host->LastCall, host->ActiveCall,
1592 (host->hostFlags & VENUSDOWN), host->hostFlags&HOSTDELETED,
1593 host->Console, host->hostFlags & CLIENTDELETED,
1594 host->hcpsfailed, host->cpsCall);
1595 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1596 if (host->hcps.prlist_val)
1597 for (i=0; i < host->hcps.prlist_len; i++) {
1598 sprintf(tmpStr, " %d", host->hcps.prlist_val[i]);
1599 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1601 sprintf(tmpStr, "] [");
1602 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1603 if ( host->interface)
1604 for (i=0; i < host->interface->numberOfInterfaces; i++) {
1605 sprintf(tmpStr, " %x", host->interface->addr[i]);
1606 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1608 sprintf(tmpStr, "]\n");
1609 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1620 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
1625 ViceLog(0, ("Couldn't create host dump file %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
1628 now = FT_ApproxTime();
1629 sprintf(tmpStr, "List of active hosts at %s\n",
1630 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
1631 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1632 h_Enumerate(h_DumpHost, (char *) file);
1633 STREAM_REALLYCLOSE(file);
1634 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
1640 * This counts the number of workstations, the number of active workstations,
1641 * and the number of workstations declared "down" (i.e. not heard from
1642 * recently). An active workstation has received a call since the cutoff
1643 * time argument passed.
1645 h_GetWorkStats(nump, activep, delp, cutofftime)
1649 afs_int32 cutofftime;
1653 register struct host *host;
1654 register int num=0, active=0, del=0;
1657 for (host = hostList; host; host = host->next) {
1658 if (!(host->hostFlags & HOSTDELETED)) {
1660 if (host->ActiveCall > cutofftime)
1662 if (host->hostFlags & VENUSDOWN)
1674 } /*h_GetWorkStats*/
1677 /*------------------------------------------------------------------------
1678 * PRIVATE h_ClassifyAddress
1681 * Given a target IP address and a candidate IP address (both
1682 * in host byte order), classify the candidate into one of three
1683 * buckets in relation to the target by bumping the counters passed
1687 * a_targetAddr : Target address.
1688 * a_candAddr : Candidate address.
1689 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
1690 * addresses are either in the same network
1691 * or the same subnet.
1692 * a_diffSubnetP : ...when the candidate is in a different
1694 * a_diffNetworkP : ...when the candidate is in a different
1701 * The target and candidate addresses are both in host byte
1702 * order, NOT network byte order, when passed in.
1706 *------------------------------------------------------------------------*/
1708 static void h_ClassifyAddress(a_targetAddr, a_candAddr, a_sameNetOrSubnetP,
1709 a_diffSubnetP, a_diffNetworkP)
1710 afs_uint32 a_targetAddr;
1711 afs_uint32 a_candAddr;
1712 afs_int32 *a_sameNetOrSubnetP;
1713 afs_int32 *a_diffSubnetP;
1714 afs_int32 *a_diffNetworkP;
1716 { /*h_ClassifyAddress*/
1718 register int i; /*Iterator thru host hash table*/
1719 register struct host *hostP; /*Ptr to current host entry*/
1720 register afs_uint32 currHostAddr; /*Current host address*/
1721 afs_uint32 targetNet;
1722 afs_uint32 targetSubnet;
1724 afs_uint32 candSubnet;
1727 * Put bad values into the subnet info to start with.
1729 targetSubnet = (afs_uint32) 0;
1730 candSubnet = (afs_uint32) 0;
1733 * Pull out the network and subnetwork numbers from the target
1734 * and candidate addresses. We can short-circuit this whole
1735 * affair if the target and candidate addresses are not of the
1738 if (IN_CLASSA(a_targetAddr)) {
1739 if (!(IN_CLASSA(a_candAddr))) {
1740 (*a_diffNetworkP)++;
1743 targetNet = a_targetAddr & IN_CLASSA_NET;
1744 candNet = a_candAddr & IN_CLASSA_NET;
1745 if (IN_SUBNETA(a_targetAddr))
1746 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
1747 if (IN_SUBNETA(a_candAddr))
1748 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
1751 if (IN_CLASSB(a_targetAddr)) {
1752 if (!(IN_CLASSB(a_candAddr))) {
1753 (*a_diffNetworkP)++;
1756 targetNet = a_targetAddr & IN_CLASSB_NET;
1757 candNet = a_candAddr & IN_CLASSB_NET;
1758 if (IN_SUBNETB(a_targetAddr))
1759 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
1760 if (IN_SUBNETB(a_candAddr))
1761 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
1762 } /*Class B target*/
1764 if (IN_CLASSC(a_targetAddr)) {
1765 if (!(IN_CLASSC(a_candAddr))) {
1766 (*a_diffNetworkP)++;
1769 targetNet = a_targetAddr & IN_CLASSC_NET;
1770 candNet = a_candAddr & IN_CLASSC_NET;
1773 * Note that class C addresses can't have subnets,
1774 * so we leave the defaults untouched.
1776 } /*Class C target*/
1778 targetNet = a_targetAddr;
1779 candNet = a_candAddr;
1780 } /*Class D address*/
1783 * Now, simply compare the extracted net and subnet values for
1784 * the two addresses (which at this point are known to be of the
1787 if (targetNet == candNet) {
1788 if (targetSubnet == candSubnet)
1789 (*a_sameNetOrSubnetP)++;
1794 (*a_diffNetworkP)++;
1796 } /*h_ClassifyAddress*/
1799 /*------------------------------------------------------------------------
1800 * EXPORTED h_GetHostNetStats
1803 * Iterate through the host table, and classify each (non-deleted)
1804 * host entry into ``proximity'' categories (same net or subnet,
1805 * different subnet, different network).
1808 * a_numHostsP : Set to total number of (non-deleted) hosts.
1809 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
1810 * a_diffSubnetP : Set to # hosts on diff subnet as server.
1811 * a_diffNetworkP : Set to # hosts on diff network as server.
1817 * We only count non-deleted hosts. The storage pointed to by our
1818 * parameters is zeroed upon entry.
1822 *------------------------------------------------------------------------*/
1824 void h_GetHostNetStats(a_numHostsP, a_sameNetOrSubnetP, a_diffSubnetP,
1826 afs_int32 *a_numHostsP;
1827 afs_int32 *a_sameNetOrSubnetP;
1828 afs_int32 *a_diffSubnetP;
1829 afs_int32 *a_diffNetworkP;
1831 { /*h_GetHostNetStats*/
1833 register struct host *hostP; /*Ptr to current host entry*/
1834 register afs_uint32 currAddr_HBO; /*Curr host addr, host byte order*/
1837 * Clear out the storage pointed to by our parameters.
1839 *a_numHostsP = (afs_int32) 0;
1840 *a_sameNetOrSubnetP = (afs_int32) 0;
1841 *a_diffSubnetP = (afs_int32) 0;
1842 *a_diffNetworkP = (afs_int32) 0;
1845 for (hostP = hostList; hostP; hostP = hostP->next) {
1846 if (!(hostP->hostFlags & HOSTDELETED)) {
1848 * Bump the number of undeleted host entries found.
1849 * In classifying the current entry's address, make
1850 * sure to first convert to host byte order.
1853 currAddr_HBO = (afs_uint32)ntohl(hostP->host);
1854 h_ClassifyAddress(FS_HostAddr_HBO,
1859 } /*Only look at non-deleted hosts*/
1860 } /*For each host record hashed to this index*/
1863 } /*h_GetHostNetStats*/
1865 static afs_uint32 checktime;
1866 static afs_uint32 clientdeletetime;
1867 static struct AFSFid zerofid;
1871 * XXXX: This routine could use Multi-R to avoid serializing the timeouts.
1872 * Since it can serialize them, and pile up, it should be a separate LWP
1873 * from other events.
1875 int CheckHost(host, held)
1876 register struct host *host;
1880 register struct client *client;
1881 struct interfaceAddr interf;
1884 /* Host is held by h_Enumerate */
1886 for (client = host->FirstClient; client; client = client->next) {
1887 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
1888 client->deleted = 1;
1889 host->hostFlags |= CLIENTDELETED;
1892 if (host->LastCall < checktime) {
1894 if (!(host->hostFlags & HOSTDELETED)) {
1895 if (host->LastCall < clientdeletetime) {
1896 host->hostFlags |= HOSTDELETED;
1897 if (!(host->hostFlags & VENUSDOWN)) {
1898 host->hostFlags &= ~ALTADDR; /* alternate address invalid*/
1899 if (host->interface) {
1901 code = RXAFSCB_InitCallBackState3(host->callback_rxcon,
1906 code = RXAFSCB_InitCallBackState(host->callback_rxcon);
1909 host->hostFlags |= ALTADDR; /* alternate addresses valid */
1912 char *hoststr = afs_inet_ntoa_r(host->host);
1914 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
1915 hoststr, ntohs(host->port)));
1916 host->hostFlags |= VENUSDOWN;
1919 /* Note: it's safe to delete hosts even if they have call
1920 * back state, because break delayed callbacks (called when a
1921 * message is received from the workstation) will always send a
1922 * break all call backs to the workstation if there is no
1928 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
1929 if (host->interface) {
1930 afsUUID uuid = host->interface->uuid;
1932 code = RXAFSCB_ProbeUuid(host->callback_rxcon, &uuid);
1935 if ( MultiProbeAlternateAddress_r(host) ) {
1936 char *hoststr = afs_inet_ntoa_r(host->host);
1938 ("ProbeUuid failed for host %s:%d\n",
1939 hoststr, ntohs(host->port)));
1940 host->hostFlags |= VENUSDOWN;
1946 code = RXAFSCB_Probe(host->callback_rxcon);
1949 char *hoststr = afs_inet_ntoa_r(host->host);
1950 ViceLog(0, ("ProbeUuid failed for host %s:%d\n",
1951 hoststr, ntohs(host->port)));
1952 host->hostFlags |= VENUSDOWN;
1968 * Set VenusDown for any hosts that have not had a call in 15 minutes and
1969 * don't respond to a probe. Note that VenusDown can only be cleared if
1970 * a message is received from the host (see ServerLWP in file.c).
1971 * Delete hosts that have not had any calls in 1 hour, clients that
1972 * have not had any calls in 15 minutes.
1974 * This routine is called roughly every 5 minutes.
1978 afs_uint32 now = FT_ApproxTime();
1980 bzero((char *)&zerofid, sizeof(zerofid));
1982 * Send a probe to the workstation if it hasn't been heard from in
1985 checktime = now - 15*60;
1986 clientdeletetime = now - 120*60; /* 2 hours ago */
1987 h_Enumerate(CheckHost, (char *) 0);
1992 * This is called with host locked and held. At this point, the
1993 * hostHashTable should not be having entries for the alternate
1994 * interfaces. This function has to insert these entries in the
1997 * The addresses in the ineterfaceAddr list are in host byte order.
2000 initInterfaceAddr_r(host, interf)
2002 struct interfaceAddr *interf;
2006 afs_int32 myPort, myHost;
2008 struct Interface *interface;
2013 ViceLog(125,("initInterfaceAddr : host %x numAddr %d\n",
2014 host->host, interf->numberOfInterfaces));
2016 number = interf->numberOfInterfaces;
2017 myPort = host->port;
2018 myHost = host->host; /* current interface address */
2020 /* validation checks */
2023 ViceLog(0,("Number of alternate addresses returned is %d\n",
2029 * Convert IP addresses to network byte order, and remove for
2030 * duplicate IP addresses from the interface list.
2032 for (i = 0, count = 0, found = 0; i < number; i++)
2034 interf->addr_in[i] = htonl(interf->addr_in[i]);
2035 for (j = 0 ; j < count ; j++) {
2036 if (interf->addr_in[j] == interf->addr_in[i])
2040 interf->addr_in[count] = interf->addr_in[i];
2041 if (interf->addr_in[count] == myHost)
2048 * Allocate and initialize an interface structure for this host.
2051 interface = (struct Interface *)
2052 malloc(sizeof(struct Interface) +
2053 (sizeof(afs_int32) * (count-1)));
2055 interface->numberOfInterfaces = count;
2057 interface = (struct Interface *)
2058 malloc(sizeof(struct Interface) +
2059 (sizeof(afs_int32) * count));
2061 interface->numberOfInterfaces = count + 1;
2062 interface->addr[count] = myHost;
2064 interface->uuid = interf->uuid;
2065 for (i = 0 ; i < count ; i++)
2066 interface->addr[i] = interf->addr_in[i];
2068 assert(!host->interface);
2069 host->interface = interface;
2071 for ( i=0; i < host->interface->numberOfInterfaces; i++)
2073 ViceLog(125,("--- alt address %x\n", host->interface->addr[i]));
2080 * This is called with host locked and held. At this point, the
2081 * hostHashTable should not be having entries for the alternate
2082 * interfaces. This function has to insert these entries in the
2085 * All addresses are in network byte order.
2088 addInterfaceAddr_r(host, addr)
2095 struct Interface *interface;
2098 assert(host->interface);
2100 ViceLog(125,("addInterfaceAddr : host %x addr %d\n",
2104 * Make sure this address is on the list of known addresses
2107 number = host->interface->numberOfInterfaces;
2108 for ( i=0, found=0; i < number && !found; i++)
2110 if ( host->interface->addr[i] == addr)
2114 interface = (struct Interface *)
2115 malloc(sizeof(struct Interface) +
2116 (sizeof(afs_int32) * number));
2117 interface->numberOfInterfaces = number + 1;
2118 interface->uuid = host->interface->uuid;
2119 for (i = 0 ; i < number ; i++)
2120 interface->addr[i] = host->interface->addr[i];
2121 interface->addr[number] = addr;
2122 free(host->interface);
2123 host->interface = interface;
2127 * Create a hash table entry for this address
2129 hashInsert_r(addr, host);
2134 /* inserts a new HashChain structure corresponding to this address */
2135 hashInsert_r(addr, host)
2140 struct h_hashChain* chain;
2142 /* hash into proper bucket */
2143 index = h_HashIndex(addr);
2145 /* insert into beginning of list for this bucket */
2146 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
2148 chain->hostPtr = host;
2149 chain->next = hostHashTable[index];
2151 hostHashTable[index] = chain;
2155 /* inserts a new HashChain structure corresponding to this UUID */
2156 hashInsertUuid_r(uuid, host)
2157 struct afsUUID *uuid;
2161 struct h_hashChain* chain;
2163 /* hash into proper bucket */
2164 index = h_UuidHashIndex(uuid);
2166 /* insert into beginning of list for this bucket */
2167 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
2169 chain->hostPtr = host;
2170 chain->next = hostUuidHashTable[index];
2171 hostUuidHashTable[index] = chain;
2174 /* deleted a HashChain structure for this address and host */
2175 /* returns 1 on success */
2177 hashDelete_r(addr, host)
2183 register struct h_hashChain **hp, *th;
2185 for (hp = &hostHashTable[h_HashIndex(addr)]; th = *hp; )
2187 assert(th->hostPtr);
2188 if (th->hostPtr == host && th->addr == addr)
2203 ** prints out all alternate interface address for the host. The 'level'
2204 ** parameter indicates what level of debugging sets this output
2206 printInterfaceAddr(host, level)
2211 if ( host-> interface )
2213 /* check alternate addresses */
2214 number = host->interface->numberOfInterfaces;
2215 assert( number > 0 );
2216 for ( i=0; i < number; i++)
2217 ViceLog(level, ("%x ", host->interface->addr[i]));
2219 ViceLog(level, ("\n"));