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 <afsconfig.h>
11 #include <afs/param.h>
24 #include <netinet/in.h>
37 #include <afs/assert.h>
40 #include <afs/afsint.h>
41 #include <afs/rxgen_consts.h>
43 #include <afs/errors.h>
44 #include <afs/ihandle.h>
45 #include <afs/vnode.h>
46 #include <afs/volume.h>
47 #ifdef AFS_ATHENA_STDENV
51 #include <afs/ptclient.h>
52 #include <afs/prs_fs.h>
54 #include <afs/afsutil.h>
56 #include <afs/cellconfig.h>
58 #include "viced_prototypes.h"
63 #ifdef AFS_PTHREAD_ENV
64 pthread_mutex_t host_glock_mutex;
65 #endif /* AFS_PTHREAD_ENV */
68 extern int CurrentConnections;
70 extern int AnonymousID;
71 extern prlist AnonCPS;
73 extern struct afsconf_dir *confDir; /* config dir object */
74 extern int lwps; /* the max number of server threads */
75 extern afsUUID FS_HostUUID;
77 int CEs = 0; /* active clients */
78 int CEBlocks = 0; /* number of blocks of CEs */
79 struct client *CEFree = 0; /* first free client */
80 struct host *hostList = 0; /* linked list of all hosts */
81 int hostCount = 0; /* number of hosts in hostList */
85 #define CESPERBLOCK 73
86 struct CEBlock { /* block of CESPERBLOCK file entries */
87 struct client entry[CESPERBLOCK];
90 static void h_TossStuff_r(register struct host *host);
93 * Make sure the subnet macros have been defined.
96 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
99 #ifndef IN_CLASSA_SUBNET
100 #define IN_CLASSA_SUBNET 0xffff0000
104 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
107 #ifndef IN_CLASSB_SUBNET
108 #define IN_CLASSB_SUBNET 0xffffff00
112 /* get a new block of CEs and chain it on CEFree */
116 register struct CEBlock *block;
119 block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
121 ViceLog(0, ("Failed malloc in GetCEBlock\n"));
122 ShutDownAndCore(PANIC);
125 for (i = 0; i < (CESPERBLOCK - 1); i++) {
126 Lock_Init(&block->entry[i].lock);
127 block->entry[i].next = &(block->entry[i + 1]);
129 block->entry[CESPERBLOCK - 1].next = 0;
130 Lock_Init(&block->entry[CESPERBLOCK - 1].lock);
131 CEFree = (struct client *)block;
137 /* get the next available CE */
138 static struct client *
141 register struct client *entry;
146 ViceLog(0, ("CEFree NULL in GetCE\n"));
147 ShutDownAndCore(PANIC);
151 CEFree = entry->next;
153 memset((char *)entry, 0, CLIENT_TO_ZERO(entry));
159 /* return an entry to the free list */
161 FreeCE(register struct client *entry)
163 entry->next = CEFree;
170 * The HTs and HTBlocks variables were formerly static, but they are
171 * now referenced elsewhere in the FileServer.
173 int HTs = 0; /* active file entries */
174 int HTBlocks = 0; /* number of blocks of HTs */
175 static struct host *HTFree = 0; /* first free file entry */
178 * Hash tables of host pointers. We need two tables, one
179 * to map IP addresses onto host pointers, and another
180 * to map host UUIDs onto host pointers.
182 static struct h_hashChain *hostHashTable[h_HASHENTRIES];
183 static struct h_hashChain *hostUuidHashTable[h_HASHENTRIES];
184 #define h_HashIndex(hostip) ((hostip) & (h_HASHENTRIES-1))
185 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
187 struct HTBlock { /* block of HTSPERBLOCK file entries */
188 struct host entry[h_HTSPERBLOCK];
192 /* get a new block of HTs and chain it on HTFree */
196 register struct HTBlock *block;
198 static int index = 0;
200 block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
202 ViceLog(0, ("Failed malloc in GetHTBlock\n"));
203 ShutDownAndCore(PANIC);
205 #ifdef AFS_PTHREAD_ENV
206 for (i = 0; i < (h_HTSPERBLOCK); i++)
207 assert(pthread_cond_init(&block->entry[i].cond, NULL) == 0);
208 #endif /* AFS_PTHREAD_ENV */
209 for (i = 0; i < (h_HTSPERBLOCK); i++)
210 Lock_Init(&block->entry[i].lock);
211 for (i = 0; i < (h_HTSPERBLOCK - 1); i++)
212 block->entry[i].next = &(block->entry[i + 1]);
213 for (i = 0; i < (h_HTSPERBLOCK); i++)
214 block->entry[i].index = index++;
215 block->entry[h_HTSPERBLOCK - 1].next = 0;
216 HTFree = (struct host *)block;
217 hosttableptrs[HTBlocks++] = block->entry;
222 /* get the next available HT */
226 register struct host *entry;
232 HTFree = entry->next;
234 memset((char *)entry, 0, HOST_TO_ZERO(entry));
240 /* return an entry to the free list */
242 FreeHT(register struct host *entry)
244 entry->next = HTFree;
251 static short consolePort = 0;
254 h_Release(register struct host *host)
263 * If this thread does not have a hold on this host AND
264 * if other threads also dont have any holds on this host AND
265 * If either the HOSTDELETED or CLIENTDELETED flags are set
269 h_Release_r(register struct host *host)
272 if (!((host)->holds[h_holdSlot()] & ~h_holdbit())) {
273 if (!h_OtherHolds_r(host)) {
274 /* must avoid masking this until after h_OtherHolds_r runs
275 * but it should be run before h_TossStuff_r */
276 (host)->holds[h_holdSlot()] &= ~h_holdbit();
277 if ((host->hostFlags & HOSTDELETED)
278 || (host->hostFlags & CLIENTDELETED)) {
282 (host)->holds[h_holdSlot()] &= ~h_holdbit();
284 (host)->holds[h_holdSlot()] &= ~h_holdbit();
290 h_OtherHolds_r(register struct host *host)
292 register int i, bit, slot;
295 for (i = 0; i < h_maxSlots; i++) {
296 if (host->holds[i] != ((i == slot) ? bit : 0)) {
304 h_Lock_r(register struct host *host)
314 * returns 1 if already locked
315 * else returns locks and returns 0
319 h_NBLock_r(register struct host *host)
321 struct Lock *hostLock = &host->lock;
326 if (!(hostLock->excl_locked) && !(hostLock->readers_reading))
327 hostLock->excl_locked = WRITE_LOCK;
331 LOCK_UNLOCK(hostLock);
340 #if FS_STATS_DETAILED
341 /*------------------------------------------------------------------------
342 * PRIVATE h_AddrInSameNetwork
345 * Given a target IP address and a candidate IP address (both
346 * in host byte order), return a non-zero value (1) if the
347 * candidate address is in a different network from the target
351 * a_targetAddr : Target address.
352 * a_candAddr : Candidate address.
355 * 1 if the candidate address is in the same net as the target,
359 * The target and candidate addresses are both in host byte
360 * order, NOT network byte order, when passed in. We return
361 * our value as a character, since that's the type of field in
362 * the host structure, where this info will be stored.
366 *------------------------------------------------------------------------*/
369 h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
370 { /*h_AddrInSameNetwork */
372 afs_uint32 targetNet;
376 * Pull out the network and subnetwork numbers from the target
377 * and candidate addresses. We can short-circuit this whole
378 * affair if the target and candidate addresses are not of the
381 if (IN_CLASSA(a_targetAddr)) {
382 if (!(IN_CLASSA(a_candAddr))) {
385 targetNet = a_targetAddr & IN_CLASSA_NET;
386 candNet = a_candAddr & IN_CLASSA_NET;
387 } else if (IN_CLASSB(a_targetAddr)) {
388 if (!(IN_CLASSB(a_candAddr))) {
391 targetNet = a_targetAddr & IN_CLASSB_NET;
392 candNet = a_candAddr & IN_CLASSB_NET;
393 } /*Class B target */
394 else if (IN_CLASSC(a_targetAddr)) {
395 if (!(IN_CLASSC(a_candAddr))) {
398 targetNet = a_targetAddr & IN_CLASSC_NET;
399 candNet = a_candAddr & IN_CLASSC_NET;
400 } /*Class C target */
402 targetNet = a_targetAddr;
403 candNet = a_candAddr;
404 } /*Class D address */
407 * Now, simply compare the extracted net values for the two addresses
408 * (which at this point are known to be of the same class)
410 if (targetNet == candNet)
415 } /*h_AddrInSameNetwork */
416 #endif /* FS_STATS_DETAILED */
419 /* Assumptions: called with held host */
421 h_gethostcps_r(register struct host *host, register afs_int32 now)
426 /* wait if somebody else is already doing the getCPS call */
427 while (host->hostFlags & HCPS_INPROGRESS) {
428 slept = 1; /* I did sleep */
429 host->hostFlags |= HCPS_WAITING; /* I am sleeping now */
430 #ifdef AFS_PTHREAD_ENV
431 pthread_cond_wait(&host->cond, &host_glock_mutex);
432 #else /* AFS_PTHREAD_ENV */
433 if ((code = LWP_WaitProcess(&(host->hostFlags))) != LWP_SUCCESS)
434 ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
435 #endif /* AFS_PTHREAD_ENV */
439 host->hostFlags |= HCPS_INPROGRESS; /* mark as CPSCall in progress */
440 if (host->hcps.prlist_val)
441 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
442 host->hcps.prlist_val = NULL;
443 host->hcps.prlist_len = 0;
444 slept ? (host->cpsCall = FT_ApproxTime()) : (host->cpsCall = now);
447 code = pr_GetHostCPS(htonl(host->host), &host->hcps);
451 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
452 * and reevaluates things if no sync server or quorum is found we could still end up
453 * with one of these errors. In such case we would like to reevaluate the rpc call to
454 * find if there's cps for this guy. We treat other errors (except network failures
455 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
456 * we could like to deal this problem the other way around (i.e. if code == NOCPS
457 * ignore else retry next time) but the problem is that there're other errors (i.e.
458 * EPERM) for which we don't want to retry and we don't know the whole code list!
460 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
462 * We would have preferred to use a while loop and try again since ops in protected
463 * acls for this host will fail now but they'll be reevaluated on any subsequent
464 * call. The attempt to wait for a quorum/sync site or network error won't work
465 * since this problems really should only occurs during a complete fileserver
466 * restart. Since the fileserver will start before the ptservers (and thus before
467 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
469 host->hcpsfailed = 1;
471 ("Warning: GetHostCPS failed (%d) for %x; will retry\n",
474 host->hcpsfailed = 0;
476 ("gethost: GetHostCPS failed (%d) for %x; ignored\n",
479 if (host->hcps.prlist_val)
480 free(host->hcps.prlist_val);
481 host->hcps.prlist_val = NULL;
482 host->hcps.prlist_len = 0; /* Make sure it's zero */
484 host->hcpsfailed = 0;
486 host->hostFlags &= ~HCPS_INPROGRESS;
487 /* signal all who are waiting */
488 if (host->hostFlags & HCPS_WAITING) { /* somebody is waiting */
489 host->hostFlags &= ~HCPS_WAITING;
490 #ifdef AFS_PTHREAD_ENV
491 assert(pthread_cond_broadcast(&host->cond) == 0);
492 #else /* AFS_PTHREAD_ENV */
493 if ((code = LWP_NoYieldSignal(&(host->hostFlags))) != LWP_SUCCESS)
494 ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
495 #endif /* AFS_PTHREAD_ENV */
499 /* args in net byte order */
501 h_flushhostcps(register afs_uint32 hostaddr, register afs_uint32 hport)
503 register struct host *host;
507 host = h_Lookup_r(hostaddr, hport, &held);
509 host->hcpsfailed = 1;
519 * Allocate a host. It will be identified by the peer (ip,port) info in the
520 * rx connection provided. The host is returned held and locked
522 #define DEF_ROPCONS 2115
525 h_Alloc_r(register struct rx_connection *r_con)
527 struct servent *serverentry;
528 register index = h_HashIndex(rxr_HostOf(r_con));
529 register struct host *host;
530 static struct rx_securityClass *sc = 0;
532 struct h_hashChain *h_hashChain;
533 #if FS_STATS_DETAILED
534 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order */
535 #endif /* FS_STATS_DETAILED */
539 h_hashChain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
541 ViceLog(0, ("Failed malloc in h_Alloc_r\n"));
544 h_hashChain->hostPtr = host;
545 h_hashChain->addr = rxr_HostOf(r_con);
546 h_hashChain->next = hostHashTable[index];
547 hostHashTable[index] = h_hashChain;
549 host->host = rxr_HostOf(r_con);
550 host->port = rxr_PortOf(r_con);
551 if (consolePort == 0) { /* find the portal number for console */
552 #if defined(AFS_OSF_ENV)
553 serverentry = getservbyname("ropcons", "");
555 serverentry = getservbyname("ropcons", 0);
558 consolePort = serverentry->s_port;
560 consolePort = htons(DEF_ROPCONS); /* Use a default */
562 if (host->port == consolePort)
564 /* Make a callback channel even for the console, on the off chance that it
565 * makes a request that causes a break call back. It shouldn't. */
568 sc = rxnull_NewClientSecurityObject();
569 host->callback_rxcon =
570 rx_NewConnection(host->host, host->port, 1, sc, 0);
571 rx_SetConnDeadTime(host->callback_rxcon, 50);
572 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
574 now = host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
576 host->hcps.prlist_val = NULL;
577 host->hcps.prlist_len = 0;
580 host->hcpsfailed = 0; /* save cycles */
581 h_gethostcps(host); /* do this under host hold/lock */
583 host->FirstClient = 0;
586 h_InsertList_r(host); /* update global host List */
587 #if FS_STATS_DETAILED
589 * Compare the new host's IP address (in host byte order) with ours
590 * (the File Server's), remembering if they are in the same network.
592 newHostAddr_HBO = (afs_uint32) ntohl(host->host);
593 host->InSameNetwork =
594 h_AddrInSameNetwork(FS_HostAddr_HBO, newHostAddr_HBO);
595 #endif /* FS_STATS_DETAILED */
601 /* Lookup a host given an IP address and UDP port number. */
602 /* hostaddr and hport are in network order */
603 /* Note: host should be released by caller if 0 == *heldp and non-null */
604 /* hostaddr and hport are in network order */
606 h_Lookup_r(afs_uint32 haddr, afs_uint32 hport, int *heldp)
608 register afs_int32 now;
609 register struct host *host = 0;
610 register struct h_hashChain *chain;
611 register index = h_HashIndex(haddr);
612 extern int hostaclRefresh;
615 for (chain = hostHashTable[index]; chain; chain = chain->next) {
616 host = chain->hostPtr;
618 if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
619 && chain->port == hport) {
620 *heldp = h_Held_r(host);
624 if (host->hostFlags & HOSTDELETED) {
631 now = FT_ApproxTime(); /* always evaluate "now" */
632 if (host->hcpsfailed || (host->cpsCall + hostaclRefresh < now)) {
634 * Every hostaclRefresh period (def 2 hrs) get the new
635 * membership list for the host. Note this could be the
636 * first time that the host is added to a group. Also
637 * here we also retry on previous legitimate hcps failures.
639 * If we get here we still have a host hold.
641 h_gethostcps_r(host, now);
651 /* Lookup a host given its UUID. */
653 h_LookupUuid_r(afsUUID * uuidp)
655 register struct host *host = 0;
656 register struct h_hashChain *chain;
657 register index = h_UuidHashIndex(uuidp);
659 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
660 host = chain->hostPtr;
662 if (!(host->hostFlags & HOSTDELETED) && host->interface
663 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
674 * h_Hold_r: Establish a hold by the current LWP on this host--the host
675 * or its clients will not be physically deleted until all holds have
677 * NOTE: h_Hold_r is a macro defined in host.h.
680 /* h_TossStuff_r: Toss anything in the host structure (the host or
681 * clients marked for deletion. Called from h_Release_r ONLY.
682 * To be called, there must be no holds, and either host->deleted
683 * or host->clientDeleted must be set.
686 h_TossStuff_r(register struct host *host)
688 register struct client **cp, *client;
691 /* if somebody still has this host held */
692 for (i = 0; (i < h_maxSlots) && (!(host)->holds[i]); i++);
696 /* if somebody still has this host locked */
697 if (h_NBLock_r(host) != 0) {
700 ("Warning: h_TossStuff_r failed; Host %s:%d was locked.\n",
701 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
707 /* ASSUMPTION: rxi_FreeConnection() does not yield */
708 for (cp = &host->FirstClient; (client = *cp);) {
709 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
710 if (client->refCount) {
713 ("Warning: Host %s:%d client %x refcount %d while deleting.\n",
714 afs_inet_ntoa_r(host->host, hoststr),
715 ntohs(host->port), client, client->refCount));
717 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val) {
718 free(client->CPS.prlist_val);
719 client->CPS.prlist_val = NULL;
721 client->CPS.prlist_len = 0;
723 rx_SetSpecific(client->tcon, rxcon_client_key, (void *)0);
725 CurrentConnections--;
732 /* We've just cleaned out all the deleted clients; clear the flag */
733 host->hostFlags &= ~CLIENTDELETED;
735 if (host->hostFlags & HOSTDELETED) {
736 register struct h_hashChain **hp, *th;
737 register struct rx_connection *rxconn;
739 struct AddrPort hostAddrPort;
742 if (host->Console & 1)
744 if ((rxconn = host->callback_rxcon)) {
745 host->callback_rxcon = (struct rx_connection *)0;
747 * If rx_DestroyConnection calls h_FreeConnection we will
748 * deadlock on the host_glock_mutex. Work around the problem
749 * by unhooking the client from the connection before
750 * destroying the connection.
752 client = rx_GetSpecific(rxconn, rxcon_client_key);
753 if (client && client->tcon == rxconn)
755 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
756 rx_DestroyConnection(rxconn);
758 if (host->hcps.prlist_val)
759 free(host->hcps.prlist_val);
760 host->hcps.prlist_val = NULL;
761 host->hcps.prlist_len = 0;
762 DeleteAllCallBacks_r(host, 1);
763 host->hostFlags &= ~RESETDONE; /* just to be safe */
765 /* if alternate addresses do not exist */
766 if (!(host->interface)) {
767 for (hp = &hostHashTable[h_HashIndex(host->host)]; (th = *hp);
770 if (th->hostPtr == host) {
772 h_DeleteList_r(host);
779 /* delete all hash entries for the UUID */
780 uuidp = &host->interface->uuid;
781 for (hp = &hostUuidHashTable[h_UuidHashIndex(uuidp)]; (th = *hp);
784 if (th->hostPtr == host) {
790 /* delete all hash entries for alternate addresses */
791 assert(host->interface->numberOfInterfaces > 0);
792 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
793 hostAddrPort = host->interface->interface[i];
795 for (hp = &hostHashTable[h_HashIndex(hostAddrPort.addr)]; (th = *hp);
798 if (th->hostPtr == host) {
805 free(host->interface);
806 host->interface = NULL;
807 h_DeleteList_r(host); /* remove host from global host List */
809 } /* if alternate address exists */
814 /* Called by rx when a server connection disappears */
816 h_FreeConnection(struct rx_connection *tcon)
818 register struct client *client;
820 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
823 if (client->tcon == tcon)
824 client->tcon = (struct rx_connection *)0;
828 } /*h_FreeConnection */
831 /* h_Enumerate: Calls (*proc)(host, held, param) for at least each host in the
832 * system at the start of the enumeration (perhaps more). Hosts may be deleted
833 * (have delete flag set); ditto for clients. (*proc) is always called with
834 * host h_held(). The hold state of the host with respect to this lwp is passed
835 * to (*proc) as the param held. The proc should return 0 if the host should be
836 * released, 1 if it should be held after enumeration.
839 h_Enumerate(int (*proc) (), char *param)
841 register struct host *host, **list;
843 register int i, count;
846 if (hostCount == 0) {
850 list = (struct host **)malloc(hostCount * sizeof(struct host *));
852 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
855 held = (int *)malloc(hostCount * sizeof(int));
857 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
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 (revised):
877 * Calls (*proc)(host, held, param) for each host in hostList, starting
879 * Hosts may be deleted (have delete flag set); ditto for clients.
880 * (*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.
887 h_Enumerate_r(int (*proc) (), struct host *enumstart, char *param)
889 register struct host *host, *next;
890 register int held, nheld;
892 if (hostCount == 0) {
895 if (enumstart && !(held = h_Held_r(enumstart)))
897 for (host = enumstart; host; host = next, held = nheld) {
898 held = (*proc) (host, held, param);
900 if (next && !(nheld = h_Held_r(next)))
903 h_Release_r(host); /* this might free up the host */
907 /* inserts a new HashChain structure corresponding to this UUID */
909 hashInsertUuid_r(struct afsUUID *uuid, struct host *host)
912 struct h_hashChain *chain;
914 /* hash into proper bucket */
915 index = h_UuidHashIndex(uuid);
917 /* insert into beginning of list for this bucket */
918 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
920 ViceLog(0, ("Failed malloc in hashInsertUuid_r\n"));
924 chain->hostPtr = host;
925 chain->next = hostUuidHashTable[index];
926 hostUuidHashTable[index] = chain;
930 /* inserts a new HashChain structure corresponding to this address */
932 hashInsert_r(afs_uint32 addr, afs_uint16 port, struct host *host)
935 struct h_hashChain *chain;
937 /* hash into proper bucket */
938 index = h_HashIndex(addr);
940 /* insert into beginning of list for this bucket */
941 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
943 ViceLog(0, ("Failed malloc in hashInsert_r\n"));
946 chain->hostPtr = host;
947 chain->next = hostHashTable[index];
950 hostHashTable[index] = chain;
955 * This is called with host locked and held. At this point, the
956 * hostHashTable should not be having entries for the alternate
957 * interfaces. This function has to insert these entries in the
960 * All addresses are in network byte order.
963 addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
968 struct Interface *interface;
971 assert(host->interface);
973 ViceLog(125, ("addInterfaceAddr : host %x addr %x:%d\n", host->host, addr, ntohs(port)));
976 * Make sure this address is on the list of known addresses
979 number = host->interface->numberOfInterfaces;
980 for (i = 0, found = 0; i < number && !found; i++) {
981 if (host->interface->interface[i].addr == addr &&
982 host->interface->interface[i].port == port)
986 interface = (struct Interface *)
987 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
989 ViceLog(0, ("Failed malloc in addInterfaceAddr_r\n"));
992 interface->numberOfInterfaces = number + 1;
993 interface->uuid = host->interface->uuid;
994 for (i = 0; i < number; i++)
995 interface->interface[i] = host->interface->interface[i];
996 interface->interface[number].addr = addr;
997 interface->interface[number].port = port;
998 free(host->interface);
999 host->interface = interface;
1003 * Create a hash table entry for this address
1005 hashInsert_r(addr, port, host);
1011 /* Host is returned held */
1013 h_GetHost_r(struct rx_connection *tcon)
1016 struct host *oldHost;
1019 struct interfaceAddr interf;
1020 int interfValid = 0;
1021 struct Identity *identP = NULL;
1024 char hoststr[16], hoststr2[16];
1026 struct rx_connection *cb_conn = NULL;
1028 caps.Capabilities_val = NULL;
1030 haddr = rxr_HostOf(tcon);
1031 hport = rxr_PortOf(tcon);
1033 if (caps.Capabilities_val)
1034 free(caps.Capabilities_val);
1035 caps.Capabilities_val = NULL;
1036 caps.Capabilities_len = 0;
1039 host = h_Lookup_r(haddr, hport, &held);
1040 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
1041 if (host && !identP && !(host->Console & 1)) {
1042 /* This is a new connection, and we already have a host
1043 * structure for this address. Verify that the identity
1044 * of the caller matches the identity in the host structure.
1047 if (!(host->hostFlags & ALTADDR)) {
1048 /* Another thread is doing initialization */
1053 ("Host %s:%d starting h_Lookup again\n",
1054 afs_inet_ntoa_r(host->host, hoststr),
1055 ntohs(host->port)));
1058 host->hostFlags &= ~ALTADDR;
1059 cb_conn = host->callback_rxcon;
1060 rx_GetConnection(cb_conn);
1063 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1064 if (code == RXGEN_OPCODE)
1065 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1066 rx_PutConnection(cb_conn);
1069 if (code == RXGEN_OPCODE) {
1070 identP = (struct Identity *)malloc(sizeof(struct Identity));
1072 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1076 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1077 /* The host on this connection was unable to respond to
1078 * the WhoAreYou. We will treat this as a new connection
1079 * from the existing host. The worst that can happen is
1080 * that we maintain some extra callback state information */
1081 if (host->interface) {
1083 ("Host %s:%d used to support WhoAreYou, deleting.\n",
1084 afs_inet_ntoa_r(host->host, hoststr),
1085 ntohs(host->port)));
1086 host->hostFlags |= HOSTDELETED;
1093 } else if (code == 0) {
1095 identP = (struct Identity *)malloc(sizeof(struct Identity));
1097 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1101 identP->uuid = interf.uuid;
1102 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1103 /* Check whether the UUID on this connection matches
1104 * the UUID in the host structure. If they don't match
1105 * then this is not the same host as before. */
1106 if (!host->interface
1107 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
1109 ("Host %s:%d has changed its identity, deleting.\n",
1110 afs_inet_ntoa_r(host->host, hoststr), host->port));
1111 host->hostFlags |= HOSTDELETED;
1119 afs_inet_ntoa_r(host->host, hoststr);
1121 ("CB: WhoAreYou failed for %s:%d, error %d\n", hoststr,
1122 ntohs(host->port), code));
1123 host->hostFlags |= VENUSDOWN;
1125 if (caps.Capabilities_val
1126 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1127 host->hostFlags |= HERRORTRANS;
1129 host->hostFlags &= ~(HERRORTRANS);
1130 host->hostFlags |= ALTADDR;
1133 if (!(host->hostFlags & ALTADDR)) {
1134 /* another thread is doing the initialisation */
1136 ("Host %s:%d waiting for host-init to complete\n",
1137 afs_inet_ntoa_r(host->host, hoststr),
1138 ntohs(host->port)));
1144 ("Host %s:%d starting h_Lookup again\n",
1145 afs_inet_ntoa_r(host->host, hoststr),
1146 ntohs(host->port)));
1149 /* We need to check whether the identity in the host structure
1150 * matches the identity on the connection. If they don't match
1151 * then treat this a new host. */
1152 if (!(host->Console & 1)
1153 && ((!identP->valid && host->interface)
1154 || (identP->valid && !host->interface)
1156 && !afs_uuid_equal(&identP->uuid,
1157 &host->interface->uuid)))) {
1158 char uuid1[128], uuid2[128];
1160 afsUUID_to_string(&identP->uuid, uuid1, 127);
1161 if (host->interface)
1162 afsUUID_to_string(&host->interface->uuid, uuid2, 127);
1164 ("CB: new identity for host %s:%d, deleting(%x %x %s %s)\n",
1165 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1166 identP->valid, host->interface,
1167 identP->valid ? uuid1 : "",
1168 host->interface ? uuid2 : ""));
1170 /* The host in the cache is not the host for this connection */
1171 host->hostFlags |= HOSTDELETED;
1178 host = h_Alloc_r(tcon); /* returned held and locked */
1179 h_gethostcps_r(host, FT_ApproxTime());
1180 if (!(host->Console & 1)) {
1182 cb_conn = host->callback_rxcon;
1183 rx_GetConnection(cb_conn);
1186 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1187 if (code == RXGEN_OPCODE)
1188 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1189 rx_PutConnection(cb_conn);
1192 if (code == RXGEN_OPCODE) {
1195 (struct Identity *)malloc(sizeof(struct Identity));
1200 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1205 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1207 ("Host %s:%d does not support WhoAreYou.\n",
1208 afs_inet_ntoa_r(host->host, hoststr),
1209 ntohs(host->port)));
1211 } else if (code == 0) {
1214 (struct Identity *)malloc(sizeof(struct Identity));
1219 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1224 identP->uuid = interf.uuid;
1226 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1228 ("WhoAreYou success on %s:%d\n",
1229 afs_inet_ntoa_r(host->host, hoststr),
1230 ntohs(host->port)));
1232 if (code == 0 && !identP->valid) {
1233 cb_conn = host->callback_rxcon;
1234 rx_GetConnection(cb_conn);
1236 code = RXAFSCB_InitCallBackState(cb_conn);
1237 rx_PutConnection(cb_conn);
1240 } else if (code == 0) {
1241 oldHost = h_LookupUuid_r(&identP->uuid);
1245 if (!(oheld = h_Held_r(oldHost)))
1249 if (oldHost->interface) {
1250 afsUUID uuid = oldHost->interface->uuid;
1251 cb_conn = host->callback_rxcon;
1252 rx_GetConnection(cb_conn);
1254 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
1255 rx_PutConnection(cb_conn);
1258 if (code && MultiProbeAlternateAddress_r(oldHost)) {
1266 /* The old host is either does not have a Uuid,
1267 * is not responding to Probes,
1268 * or does not have a matching Uuid.
1270 oldHost->hostFlags |= HOSTDELETED;
1271 h_Unlock_r(oldHost);
1272 /* Let the holder be last release */
1274 h_Release_r(oldHost);
1280 /* This is a new address for an existing host. Update
1281 * the list of interfaces for the existing host and
1282 * delete the host structure we just allocated. */
1284 ("CB: new addr %s:%d for old host %s:%d\n",
1285 afs_inet_ntoa_r(host->host, hoststr),
1286 ntohs(host->port), afs_inet_ntoa_r(oldHost->host,
1288 ntohs(oldHost->port)));
1289 addInterfaceAddr_r(oldHost, haddr, hport);
1290 host->hostFlags |= HOSTDELETED;
1296 /* This really is a new host */
1297 hashInsertUuid_r(&identP->uuid, host);
1298 cb_conn = host->callback_rxcon;
1299 rx_GetConnection(cb_conn);
1302 RXAFSCB_InitCallBackState3(cb_conn,
1304 rx_PutConnection(cb_conn);
1309 ("InitCallBackState3 success on %s:%d\n",
1310 afs_inet_ntoa_r(host->host, hoststr),
1311 ntohs(host->port)));
1312 assert(interfValid == 1);
1313 initInterfaceAddr_r(host, &interf);
1318 afs_inet_ntoa_r(host->host, hoststr);
1320 ("CB: RCallBackConnectBack failed for %s:%d\n",
1321 hoststr, ntohs(host->port)));
1322 host->hostFlags |= VENUSDOWN;
1324 host->hostFlags |= RESETDONE;
1327 if (caps.Capabilities_val
1328 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1329 host->hostFlags |= HERRORTRANS;
1331 host->hostFlags &= ~(HERRORTRANS);
1332 host->hostFlags |= ALTADDR; /* host structure initialization complete */
1335 if (caps.Capabilities_val)
1336 free(caps.Capabilities_val);
1337 caps.Capabilities_val = NULL;
1338 caps.Capabilities_len = 0;
1344 static char localcellname[PR_MAXNAMELEN + 1];
1345 char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
1346 int num_lrealms = -1;
1352 afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
1353 if (num_lrealms == -1) {
1355 for (i=0; i<AFS_NUM_LREALMS; i++) {
1356 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
1362 ("afs_krb_get_lrealm failed, using %s.\n",
1364 strncpy(local_realms[0], localcellname, AFS_REALM_SZ);
1370 /* initialize the rest of the local realms to nullstring for debugging */
1371 for (; i<AFS_NUM_LREALMS; i++)
1372 local_realms[i][0] = '\0';
1374 rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
1375 rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
1376 #ifdef AFS_PTHREAD_ENV
1377 assert(pthread_mutex_init(&host_glock_mutex, NULL) == 0);
1378 #endif /* AFS_PTHREAD_ENV */
1382 MapName_r(char *aname, char *acell, afs_int32 * aval)
1387 afs_int32 anamelen, cnamelen;
1391 anamelen = strlen(aname);
1392 if (anamelen >= PR_MAXNAMELEN)
1393 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
1395 lnames.namelist_len = 1;
1396 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
1397 lids.idlist_len = 0;
1398 lids.idlist_val = NULL;
1400 cnamelen = strlen(acell);
1402 if (afs_is_foreign_ticket_name(aname, "", acell, localcellname)) {
1404 ("MapName: cell is foreign. cell=%s, localcell=%s, localrealms={%s,%s,%s,%s}\n",
1405 acell, localcellname, local_realms[0],local_realms[1],local_realms[2],local_realms[3]));
1406 if ((anamelen + cnamelen + 1) >= PR_MAXNAMELEN) {
1408 ("MapName: Name too long, using AnonymousID for %s@%s\n",
1410 *aval = AnonymousID;
1413 foreign = 1; /* attempt cross-cell authentication */
1414 tname = (char *)malloc(PR_MAXNAMELEN);
1416 ViceLog(0, ("Failed malloc in MapName_r\n"));
1419 strcpy(tname, aname);
1420 tname[anamelen] = '@';
1421 strcpy(tname + anamelen + 1, acell);
1422 lnames.namelist_val = (prname *) tname;
1427 code = pr_NameToId(&lnames, &lids);
1430 if (lids.idlist_val) {
1431 *aval = lids.idlist_val[0];
1432 if (*aval == AnonymousID) {
1434 ("MapName: NameToId on %s returns anonymousID\n",
1435 lnames.namelist_val));
1437 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
1440 ("MapName: NameToId on '%s' is unknown\n",
1441 lnames.namelist_val));
1447 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
1455 /* NOTE: this returns the client with a Write lock */
1457 h_ID2Client(afs_int32 vid)
1459 register struct client *client;
1460 register struct host *host;
1463 for (host = hostList; host; host = host->next) {
1464 if (host->hostFlags & HOSTDELETED)
1466 for (client = host->FirstClient; client; client = client->next) {
1467 if (!client->deleted && client->ViceId == vid) {
1470 ObtainWriteLock(&client->lock);
1484 * Called by the server main loop. Returns a h_Held client, which must be
1485 * released later the main loop. Allocates a client if the matching one
1486 * isn't around. The client is returned with its reference count incremented
1487 * by one. The caller must call h_ReleaseClient_r when finished with
1491 h_FindClient_r(struct rx_connection *tcon)
1493 register struct client *client;
1494 register struct host *host;
1495 struct client *oldClient;
1500 #if (64-MAXKTCNAMELEN)
1501 ticket name length != 64
1505 char uname[PR_MAXNAMELEN];
1506 char tcell[MAXKTCREALMLEN];
1509 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1510 if (client && !client->deleted) {
1512 h_Hold_r(client->host);
1513 if (client->prfail != 2) { /* Could add shared lock on client here */
1514 /* note that we don't have to lock entry in this path to
1515 * ensure CPS is initialized, since we don't call rx_SetSpecific
1516 * until initialization is done, and we only get here if
1517 * rx_GetSpecific located the client structure.
1522 ObtainWriteLock(&client->lock); /* released at end */
1524 } else if (client) {
1528 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
1530 ("FindClient: authenticating connection: authClass=%d\n",
1532 if (authClass == 1) {
1533 /* A bcrypt tickets, no longer supported */
1534 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
1535 viceid = AnonymousID;
1536 expTime = 0x7fffffff;
1537 } else if (authClass == 2) {
1540 /* kerberos ticket */
1541 code = rxkad_GetServerInfo(tcon, /*level */ 0, &expTime,
1542 tname, tinst, tcell, &kvno);
1544 ViceLog(1, ("Failed to get rxkad ticket info\n"));
1545 viceid = AnonymousID;
1546 expTime = 0x7fffffff;
1548 int ilen = strlen(tinst);
1550 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
1551 tname, tinst, tcell, expTime, kvno));
1552 strncpy(uname, tname, sizeof(uname));
1554 if (strlen(uname) + 1 + ilen >= sizeof(uname))
1557 strcat(uname, tinst);
1559 /* translate the name to a vice id */
1560 code = MapName_r(uname, tcell, &viceid);
1564 ("failed to map name=%s, cell=%s -> code=%d\n", uname,
1567 viceid = AnonymousID;
1568 expTime = 0x7fffffff;
1572 viceid = AnonymousID; /* unknown security class */
1573 expTime = 0x7fffffff;
1577 host = h_GetHost_r(tcon); /* Returns it h_Held */
1580 /* First try to find the client structure */
1581 for (client = host->FirstClient; client; client = client->next) {
1582 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
1583 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
1584 if (client->tcon && (client->tcon != tcon)) {
1586 ("*** Vid=%d, sid=%x, tcon=%x, Tcon=%x ***\n",
1587 client->ViceId, client->sid, client->tcon,
1590 (struct client *)rx_GetSpecific(client->tcon,
1593 if (oldClient == client)
1594 rx_SetSpecific(client->tcon, rxcon_client_key,
1598 ("Client-conn mismatch: CL1=%x, CN=%x, CL2=%x\n",
1599 client, client->tcon, oldClient));
1601 client->tcon = (struct rx_connection *)0;
1605 ObtainWriteLock(&client->lock);
1611 /* Still no client structure - get one */
1614 /* Retry to find the client structure */
1615 for (client = host->FirstClient; client; client = client->next) {
1616 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
1617 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
1619 goto retryfirstclient;
1623 ObtainWriteLock(&client->lock);
1624 client->refCount = 1;
1625 client->host = host;
1626 client->next = host->FirstClient;
1627 host->FirstClient = client;
1628 #if FS_STATS_DETAILED
1629 client->InSameNetwork = host->InSameNetwork;
1630 #endif /* FS_STATS_DETAILED */
1631 client->ViceId = viceid;
1632 client->expTime = expTime; /* rx only */
1633 client->authClass = authClass; /* rx only */
1634 client->sid = rxr_CidOf(tcon);
1635 client->VenusEpoch = rxr_GetEpoch(tcon);
1636 client->CPS.prlist_val = 0;
1637 client->CPS.prlist_len = 0;
1639 CurrentConnections++; /* increment number of connections */
1642 client->prfail = fail;
1644 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
1645 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID)) {
1646 free(client->CPS.prlist_val);
1648 client->CPS.prlist_val = NULL;
1649 client->CPS.prlist_len = 0;
1650 client->ViceId = viceid;
1651 client->expTime = expTime;
1653 if (viceid == ANONYMOUSID) {
1654 client->CPS.prlist_len = AnonCPS.prlist_len;
1655 client->CPS.prlist_val = AnonCPS.prlist_val;
1658 code = pr_GetCPS(viceid, &client->CPS);
1663 ("pr_GetCPS failed(%d) for user %d, host %s:%d\n",
1664 code, viceid, afs_inet_ntoa_r(client->host->host,
1666 ntohs(client->host->port)));
1668 /* Although ubik_Call (called by pr_GetCPS) traverses thru
1669 * all protection servers and reevaluates things if no
1670 * sync server or quorum is found we could still end up
1671 * with one of these errors. In such case we would like to
1672 * reevaluate the rpc call to find if there's cps for this
1673 * guy. We treat other errors (except network failures
1674 * ones - i.e. code < 0) as an indication that there is no
1675 * CPS for this host. Ideally we could like to deal this
1676 * problem the other way around (i.e. if code == NOCPS
1677 * ignore else retry next time) but the problem is that
1678 * there're other errors (i.e. EPERM) for which we don't
1679 * want to retry and we don't know the whole code list!
1681 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
1685 /* the disabling of system:administrators is so iffy and has so many
1686 * possible failure modes that we will disable it again */
1687 /* Turn off System:Administrator for safety
1688 * if (AL_IsAMember(SystemId, client->CPS) == 0)
1689 * assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
1692 /* Now, tcon may already be set to a rock, since we blocked with no host
1693 * or client locks set above in pr_GetCPS (XXXX some locking is probably
1694 * required). So, before setting the RPC's rock, we should disconnect
1695 * the RPC from the other client structure's rock.
1697 oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1698 if (oldClient && oldClient->tcon == tcon) {
1700 oldClient->tcon = (struct rx_connection *)0;
1701 ViceLog(0, ("FindClient: client %x(%x) already had conn %x (host %s:%d), stolen by client %x(%x)\n",
1702 oldClient, oldClient->sid, tcon,
1703 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
1704 ntohs(rxr_PortOf(tcon)),
1705 client, client->sid));
1706 /* rx_SetSpecific will be done immediately below */
1708 client->tcon = tcon;
1709 rx_SetSpecific(tcon, rxcon_client_key, client);
1710 ReleaseWriteLock(&client->lock);
1714 } /*h_FindClient_r */
1717 h_ReleaseClient_r(struct client *client)
1719 assert(client->refCount > 0);
1726 * Sigh: this one is used to get the client AGAIN within the individual
1727 * server routines. This does not bother h_Holding the host, since
1728 * this is assumed already have been done by the server main loop.
1729 * It does check tokens, since only the server routines can return the
1730 * VICETOKENDEAD error code
1733 GetClient(struct rx_connection *tcon, struct client **cp)
1735 register struct client *client;
1738 *cp = client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1739 if (client == NULL || client->tcon == NULL) {
1741 ("GetClient: no client in conn %x (host %x:%d), VBUSYING\n",
1742 tcon, rxr_HostOf(tcon),rxr_PortOf(tcon)));
1746 if (rxr_CidOf(client->tcon) != client->sid) {
1748 ("GetClient: tcon %x tcon sid %d client sid %d\n",
1749 client->tcon, rxr_CidOf(client->tcon), client->sid));
1753 if (!(client && client->tcon && rxr_CidOf(client->tcon) == client->sid)) {
1755 ViceLog(0, ("GetClient: no client in conn %x\n", tcon));
1758 ("GetClient: tcon %x tcon sid %d client sid %d\n",
1759 client->tcon, client->tcon ? rxr_CidOf(client->tcon)
1760 : -1, client->sid));
1763 if (client && client->LastCall > client->expTime && client->expTime) {
1766 ("Token for %s at %s:%d expired %d\n", h_UserName(client),
1767 afs_inet_ntoa_r(client->host->host, hoststr),
1768 ntohs(client->host->port), client->expTime));
1770 return VICETOKENDEAD;
1779 /* Client user name for short term use. Note that this is NOT inexpensive */
1781 h_UserName(struct client *client)
1783 static char User[PR_MAXNAMELEN + 1];
1787 lids.idlist_len = 1;
1788 lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
1789 if (!lids.idlist_val) {
1790 ViceLog(0, ("Failed malloc in h_UserName\n"));
1793 lnames.namelist_len = 0;
1794 lnames.namelist_val = (prname *) 0;
1795 lids.idlist_val[0] = client->ViceId;
1796 if (pr_IdToName(&lids, &lnames)) {
1797 /* We need to free id we alloced above! */
1798 free(lids.idlist_val);
1799 return "*UNKNOWN USER NAME*";
1801 strncpy(User, lnames.namelist_val[0], PR_MAXNAMELEN);
1802 free(lids.idlist_val);
1803 free(lnames.namelist_val);
1813 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
1814 CEs, CEBlocks, HTs, HTBlocks));
1820 h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
1822 register struct client *client;
1829 if (host->hostFlags & HOSTDELETED) {
1833 (void)afs_snprintf(tmpStr, sizeof tmpStr,
1834 "Host %s:%d down = %d, LastCall %s",
1835 afs_inet_ntoa_r(host->host, hoststr),
1836 ntohs(host->port), (host->hostFlags & VENUSDOWN),
1837 afs_ctime((time_t *) & host->LastCall, tbuffer,
1839 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1840 for (client = host->FirstClient; client; client = client->next) {
1841 if (!client->deleted) {
1843 (void)afs_snprintf(tmpStr, sizeof tmpStr,
1844 " user id=%d, name=%s, sl=%s till %s",
1845 client->ViceId, h_UserName(client),
1847 authClass ? "Authenticated" :
1848 "Not authenticated",
1850 authClass ? afs_ctime((time_t *) & client->
1854 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1856 (void)afs_snprintf(tmpStr, sizeof tmpStr,
1857 " user=%s, no current server connection\n",
1858 h_UserName(client));
1859 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1861 (void)afs_snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [",
1862 client->CPS.prlist_len);
1863 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1864 if (client->CPS.prlist_val) {
1865 for (i = 0; i > client->CPS.prlist_len; i++) {
1866 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
1867 client->CPS.prlist_val[i]);
1868 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1871 sprintf(tmpStr, "]\n");
1872 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1878 } /*h_PrintClient */
1883 * Print a list of clients, with last security level and token value seen,
1893 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
1897 ("Couldn't create client dump file %s\n",
1898 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
1901 now = FT_ApproxTime();
1902 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n",
1903 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
1904 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1905 h_Enumerate(h_PrintClient, (char *)file);
1906 STREAM_REALLYCLOSE(file);
1907 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
1914 h_DumpHost(register struct host *host, int held, StreamHandle_t * file)
1920 (void)afs_snprintf(tmpStr, sizeof tmpStr,
1921 "ip:%x 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 [",
1922 host->host, ntohs(host->port), host->index,
1923 host->cblist, CheckLock(&host->lock), host->LastCall,
1924 host->ActiveCall, (host->hostFlags & VENUSDOWN),
1925 host->hostFlags & HOSTDELETED, host->Console,
1926 host->hostFlags & CLIENTDELETED, host->hcpsfailed,
1928 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1929 if (host->hcps.prlist_val)
1930 for (i = 0; i < host->hcps.prlist_len; i++) {
1931 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
1932 host->hcps.prlist_val[i]);
1933 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1935 sprintf(tmpStr, "] [");
1936 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1937 if (host->interface)
1938 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
1939 sprintf(tmpStr, " %x:%d", host->interface->interface[i].addr,
1940 ntohs(host->interface->interface[i].port));
1941 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1943 sprintf(tmpStr, "] holds: ");
1944 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1946 for (i = 0; i < h_maxSlots; i++) {
1947 sprintf(tmpStr, "%04x", host->holds[i]);
1948 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1950 sprintf(tmpStr, " slot/bit: %d/%d\n", h_holdSlot(), h_holdbit());
1951 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1963 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
1969 ("Couldn't create host dump file %s\n",
1970 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
1973 now = FT_ApproxTime();
1974 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n",
1975 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
1976 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1977 h_Enumerate(h_DumpHost, (char *)file);
1978 STREAM_REALLYCLOSE(file);
1979 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
1985 * This counts the number of workstations, the number of active workstations,
1986 * and the number of workstations declared "down" (i.e. not heard from
1987 * recently). An active workstation has received a call since the cutoff
1988 * time argument passed.
1991 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
1993 register struct host *host;
1994 register int num = 0, active = 0, del = 0;
1997 for (host = hostList; host; host = host->next) {
1998 if (!(host->hostFlags & HOSTDELETED)) {
2000 if (host->ActiveCall > cutofftime)
2002 if (host->hostFlags & VENUSDOWN)
2014 } /*h_GetWorkStats */
2017 /*------------------------------------------------------------------------
2018 * PRIVATE h_ClassifyAddress
2021 * Given a target IP address and a candidate IP address (both
2022 * in host byte order), classify the candidate into one of three
2023 * buckets in relation to the target by bumping the counters passed
2027 * a_targetAddr : Target address.
2028 * a_candAddr : Candidate address.
2029 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
2030 * addresses are either in the same network
2031 * or the same subnet.
2032 * a_diffSubnetP : ...when the candidate is in a different
2034 * a_diffNetworkP : ...when the candidate is in a different
2041 * The target and candidate addresses are both in host byte
2042 * order, NOT network byte order, when passed in.
2046 *------------------------------------------------------------------------*/
2049 h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
2050 afs_int32 * a_sameNetOrSubnetP, afs_int32 * a_diffSubnetP,
2051 afs_int32 * a_diffNetworkP)
2052 { /*h_ClassifyAddress */
2054 afs_uint32 targetNet;
2055 afs_uint32 targetSubnet;
2057 afs_uint32 candSubnet;
2060 * Put bad values into the subnet info to start with.
2062 targetSubnet = (afs_uint32) 0;
2063 candSubnet = (afs_uint32) 0;
2066 * Pull out the network and subnetwork numbers from the target
2067 * and candidate addresses. We can short-circuit this whole
2068 * affair if the target and candidate addresses are not of the
2071 if (IN_CLASSA(a_targetAddr)) {
2072 if (!(IN_CLASSA(a_candAddr))) {
2073 (*a_diffNetworkP)++;
2076 targetNet = a_targetAddr & IN_CLASSA_NET;
2077 candNet = a_candAddr & IN_CLASSA_NET;
2078 if (IN_SUBNETA(a_targetAddr))
2079 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
2080 if (IN_SUBNETA(a_candAddr))
2081 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
2082 } else if (IN_CLASSB(a_targetAddr)) {
2083 if (!(IN_CLASSB(a_candAddr))) {
2084 (*a_diffNetworkP)++;
2087 targetNet = a_targetAddr & IN_CLASSB_NET;
2088 candNet = a_candAddr & IN_CLASSB_NET;
2089 if (IN_SUBNETB(a_targetAddr))
2090 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
2091 if (IN_SUBNETB(a_candAddr))
2092 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
2093 } /*Class B target */
2094 else if (IN_CLASSC(a_targetAddr)) {
2095 if (!(IN_CLASSC(a_candAddr))) {
2096 (*a_diffNetworkP)++;
2099 targetNet = a_targetAddr & IN_CLASSC_NET;
2100 candNet = a_candAddr & IN_CLASSC_NET;
2103 * Note that class C addresses can't have subnets,
2104 * so we leave the defaults untouched.
2106 } /*Class C target */
2108 targetNet = a_targetAddr;
2109 candNet = a_candAddr;
2110 } /*Class D address */
2113 * Now, simply compare the extracted net and subnet values for
2114 * the two addresses (which at this point are known to be of the
2117 if (targetNet == candNet) {
2118 if (targetSubnet == candSubnet)
2119 (*a_sameNetOrSubnetP)++;
2123 (*a_diffNetworkP)++;
2125 } /*h_ClassifyAddress */
2128 /*------------------------------------------------------------------------
2129 * EXPORTED h_GetHostNetStats
2132 * Iterate through the host table, and classify each (non-deleted)
2133 * host entry into ``proximity'' categories (same net or subnet,
2134 * different subnet, different network).
2137 * a_numHostsP : Set to total number of (non-deleted) hosts.
2138 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
2139 * a_diffSubnetP : Set to # hosts on diff subnet as server.
2140 * a_diffNetworkP : Set to # hosts on diff network as server.
2146 * We only count non-deleted hosts. The storage pointed to by our
2147 * parameters is zeroed upon entry.
2151 *------------------------------------------------------------------------*/
2154 h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
2155 afs_int32 * a_diffSubnetP, afs_int32 * a_diffNetworkP)
2156 { /*h_GetHostNetStats */
2158 register struct host *hostP; /*Ptr to current host entry */
2159 register afs_uint32 currAddr_HBO; /*Curr host addr, host byte order */
2162 * Clear out the storage pointed to by our parameters.
2164 *a_numHostsP = (afs_int32) 0;
2165 *a_sameNetOrSubnetP = (afs_int32) 0;
2166 *a_diffSubnetP = (afs_int32) 0;
2167 *a_diffNetworkP = (afs_int32) 0;
2170 for (hostP = hostList; hostP; hostP = hostP->next) {
2171 if (!(hostP->hostFlags & HOSTDELETED)) {
2173 * Bump the number of undeleted host entries found.
2174 * In classifying the current entry's address, make
2175 * sure to first convert to host byte order.
2178 currAddr_HBO = (afs_uint32) ntohl(hostP->host);
2179 h_ClassifyAddress(FS_HostAddr_HBO, currAddr_HBO,
2180 a_sameNetOrSubnetP, a_diffSubnetP,
2182 } /*Only look at non-deleted hosts */
2183 } /*For each host record hashed to this index */
2185 } /*h_GetHostNetStats */
2187 static afs_uint32 checktime;
2188 static afs_uint32 clientdeletetime;
2189 static struct AFSFid zerofid;
2193 * XXXX: This routine could use Multi-Rx to avoid serializing the timeouts.
2194 * Since it can serialize them, and pile up, it should be a separate LWP
2195 * from other events.
2198 CheckHost(register struct host *host, int held)
2200 register struct client *client;
2201 struct rx_connection *cb_conn = NULL;
2204 /* Host is held by h_Enumerate */
2206 for (client = host->FirstClient; client; client = client->next) {
2207 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
2208 client->deleted = 1;
2209 host->hostFlags |= CLIENTDELETED;
2212 if (host->LastCall < checktime) {
2214 if (!(host->hostFlags & HOSTDELETED)) {
2215 cb_conn = host->callback_rxcon;
2216 rx_GetConnection(cb_conn);
2217 if (host->LastCall < clientdeletetime) {
2218 host->hostFlags |= HOSTDELETED;
2219 if (!(host->hostFlags & VENUSDOWN)) {
2220 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
2221 if (host->interface) {
2224 RXAFSCB_InitCallBackState3(cb_conn,
2230 RXAFSCB_InitCallBackState(cb_conn);
2233 host->hostFlags |= ALTADDR; /* alternate addresses valid */
2236 (void)afs_inet_ntoa_r(host->host, hoststr);
2238 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
2239 hoststr, ntohs(host->port)));
2240 host->hostFlags |= VENUSDOWN;
2242 /* Note: it's safe to delete hosts even if they have call
2243 * back state, because break delayed callbacks (called when a
2244 * message is received from the workstation) will always send a
2245 * break all call backs to the workstation if there is no
2250 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
2251 if (host->interface) {
2252 afsUUID uuid = host->interface->uuid;
2254 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
2257 if (MultiProbeAlternateAddress_r(host)) {
2259 (void)afs_inet_ntoa_r(host->host, hoststr);
2261 ("ProbeUuid failed for host %s:%d\n",
2262 hoststr, ntohs(host->port)));
2263 host->hostFlags |= VENUSDOWN;
2268 code = RXAFSCB_Probe(cb_conn);
2272 (void)afs_inet_ntoa_r(host->host, hoststr);
2274 ("Probe failed for host %s:%d\n", hoststr,
2275 ntohs(host->port)));
2276 host->hostFlags |= VENUSDOWN;
2282 rx_PutConnection(cb_conn);
2295 * Set VenusDown for any hosts that have not had a call in 15 minutes and
2296 * don't respond to a probe. Note that VenusDown can only be cleared if
2297 * a message is received from the host (see ServerLWP in file.c).
2298 * Delete hosts that have not had any calls in 1 hour, clients that
2299 * have not had any calls in 15 minutes.
2301 * This routine is called roughly every 5 minutes.
2306 afs_uint32 now = FT_ApproxTime();
2308 memset((char *)&zerofid, 0, sizeof(zerofid));
2310 * Send a probe to the workstation if it hasn't been heard from in
2313 checktime = now - 15 * 60;
2314 clientdeletetime = now - 120 * 60; /* 2 hours ago */
2315 h_Enumerate(CheckHost, NULL);
2320 * This is called with host locked and held. At this point, the
2321 * hostHashTable should not have any entries for the alternate
2322 * interfaces. This function has to insert these entries in the
2325 * The addresses in the interfaceAddr list are in host byte order.
2328 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
2335 struct Interface *interface;
2341 ("initInterfaceAddr : host %x numAddr %d\n", host->host,
2342 interf->numberOfInterfaces));
2344 number = interf->numberOfInterfaces;
2345 myAddr = host->host; /* current interface address */
2346 myPort = host->port; /* current port */
2348 /* validation checks */
2349 if (number < 0 || number > AFS_MAX_INTERFACE_ADDR) {
2351 ("Number of alternate addresses returned is %d\n", number));
2356 * Convert IP addresses to network byte order, and remove for
2357 * duplicate IP addresses from the interface list.
2359 for (i = 0, count = 0, found = 0; i < number; i++) {
2360 interf->addr_in[i] = htonl(interf->addr_in[i]);
2361 for (j = 0; j < count; j++) {
2362 if (interf->addr_in[j] == interf->addr_in[i])
2366 interf->addr_in[count] = interf->addr_in[i];
2367 if (interf->addr_in[count] == myAddr)
2374 * Allocate and initialize an interface structure for this host.
2377 interface = (struct Interface *)
2378 malloc(sizeof(struct Interface) +
2379 (sizeof(struct AddrPort) * (count - 1)));
2381 ViceLog(0, ("Failed malloc in initInterfaceAddr_r\n"));
2384 interface->numberOfInterfaces = count;
2386 interface = (struct Interface *)
2387 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
2389 interface->numberOfInterfaces = count + 1;
2390 interface->interface[count].addr = myAddr;
2391 interface->interface[count].port = myPort;
2393 interface->uuid = interf->uuid;
2394 for (i = 0; i < count; i++) {
2395 interface->interface[i].addr = interf->addr_in[i];
2396 /* We store the port as 7001 because the addresses reported by
2397 * TellMeAboutYourself and WhoAreYou RPCs are only valid if they
2398 * are coming from fully connected hosts (no NAT/PATs)
2400 interface->interface[i].port = htons(7001);
2403 assert(!host->interface);
2404 host->interface = interface;
2406 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
2407 ViceLog(125, ("--- alt address %x:%d\n", host->interface->interface[i].addr,
2408 ntohs(host->interface->interface[i].port)));
2414 /* deleted a HashChain structure for this address and host */
2415 /* returns 1 on success */
2417 hashDelete_r(afs_uint32 addr, afs_uint16 port, struct host *host)
2420 register struct h_hashChain **hp, *th;
2422 for (hp = &hostHashTable[h_HashIndex(addr)]; (th = *hp);) {
2423 assert(th->hostPtr);
2424 if (th->hostPtr == host && th->addr == addr && th->port == port) {
2438 ** prints out all alternate interface address for the host. The 'level'
2439 ** parameter indicates what level of debugging sets this output
2442 printInterfaceAddr(struct host *host, int level)
2445 if (host->interface) {
2446 /* check alternate addresses */
2447 number = host->interface->numberOfInterfaces;
2449 for (i = 0; i < number; i++)
2450 ViceLog(level, ("%x:%d ", host->interface->interface[i].addr,
2451 ntohs(host->interface->interface[i].port)));
2453 ViceLog(level, ("\n"));