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;
969 char hoststr[16], hoststr2[16];
972 assert(host->interface);
974 ViceLog(125, ("addInterfaceAddr : host %s:d addr %s:%d\n",
975 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
976 afs_inet_ntoa_r(addr, hoststr2), ntohs(port)));
979 * Make sure this address is on the list of known addresses
982 number = host->interface->numberOfInterfaces;
983 for (i = 0, found = 0; i < number && !found; i++) {
984 if (host->interface->interface[i].addr == addr &&
985 host->interface->interface[i].port == port)
989 interface = (struct Interface *)
990 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
992 ViceLog(0, ("Failed malloc in addInterfaceAddr_r\n"));
995 interface->numberOfInterfaces = number + 1;
996 interface->uuid = host->interface->uuid;
997 for (i = 0; i < number; i++)
998 interface->interface[i] = host->interface->interface[i];
999 interface->interface[number].addr = addr;
1000 interface->interface[number].port = port;
1001 free(host->interface);
1002 host->interface = interface;
1006 * Create a hash table entry for this address
1008 hashInsert_r(addr, port, host);
1014 /* Host is returned held */
1016 h_GetHost_r(struct rx_connection *tcon)
1019 struct host *oldHost;
1022 struct interfaceAddr interf;
1023 int interfValid = 0;
1024 struct Identity *identP = NULL;
1027 char hoststr[16], hoststr2[16];
1029 struct rx_connection *cb_conn = NULL;
1031 caps.Capabilities_val = NULL;
1033 haddr = rxr_HostOf(tcon);
1034 hport = rxr_PortOf(tcon);
1036 if (caps.Capabilities_val)
1037 free(caps.Capabilities_val);
1038 caps.Capabilities_val = NULL;
1039 caps.Capabilities_len = 0;
1042 host = h_Lookup_r(haddr, hport, &held);
1043 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
1044 if (host && !identP && !(host->Console & 1)) {
1045 /* This is a new connection, and we already have a host
1046 * structure for this address. Verify that the identity
1047 * of the caller matches the identity in the host structure.
1050 if (!(host->hostFlags & ALTADDR)) {
1051 /* Another thread is doing initialization */
1056 ("Host %s:%d starting h_Lookup again\n",
1057 afs_inet_ntoa_r(host->host, hoststr),
1058 ntohs(host->port)));
1061 host->hostFlags &= ~ALTADDR;
1062 cb_conn = host->callback_rxcon;
1063 rx_GetConnection(cb_conn);
1066 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1067 if (code == RXGEN_OPCODE)
1068 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1069 rx_PutConnection(cb_conn);
1072 if (code == RXGEN_OPCODE) {
1073 identP = (struct Identity *)malloc(sizeof(struct Identity));
1075 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1079 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1080 /* The host on this connection was unable to respond to
1081 * the WhoAreYou. We will treat this as a new connection
1082 * from the existing host. The worst that can happen is
1083 * that we maintain some extra callback state information */
1084 if (host->interface) {
1086 ("Host %s:%d used to support WhoAreYou, deleting.\n",
1087 afs_inet_ntoa_r(host->host, hoststr),
1088 ntohs(host->port)));
1089 host->hostFlags |= HOSTDELETED;
1096 } else if (code == 0) {
1098 identP = (struct Identity *)malloc(sizeof(struct Identity));
1100 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1104 identP->uuid = interf.uuid;
1105 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1106 /* Check whether the UUID on this connection matches
1107 * the UUID in the host structure. If they don't match
1108 * then this is not the same host as before. */
1109 if (!host->interface
1110 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
1112 ("Host %s:%d has changed its identity, deleting.\n",
1113 afs_inet_ntoa_r(host->host, hoststr), host->port));
1114 host->hostFlags |= HOSTDELETED;
1122 afs_inet_ntoa_r(host->host, hoststr);
1124 ("CB: WhoAreYou failed for %s:%d, error %d\n", hoststr,
1125 ntohs(host->port), code));
1126 host->hostFlags |= VENUSDOWN;
1128 if (caps.Capabilities_val
1129 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1130 host->hostFlags |= HERRORTRANS;
1132 host->hostFlags &= ~(HERRORTRANS);
1133 host->hostFlags |= ALTADDR;
1136 if (!(host->hostFlags & ALTADDR)) {
1137 /* another thread is doing the initialisation */
1139 ("Host %s:%d waiting for host-init to complete\n",
1140 afs_inet_ntoa_r(host->host, hoststr),
1141 ntohs(host->port)));
1147 ("Host %s:%d starting h_Lookup again\n",
1148 afs_inet_ntoa_r(host->host, hoststr),
1149 ntohs(host->port)));
1152 /* We need to check whether the identity in the host structure
1153 * matches the identity on the connection. If they don't match
1154 * then treat this a new host. */
1155 if (!(host->Console & 1)
1156 && ((!identP->valid && host->interface)
1157 || (identP->valid && !host->interface)
1159 && !afs_uuid_equal(&identP->uuid,
1160 &host->interface->uuid)))) {
1161 char uuid1[128], uuid2[128];
1163 afsUUID_to_string(&identP->uuid, uuid1, 127);
1164 if (host->interface)
1165 afsUUID_to_string(&host->interface->uuid, uuid2, 127);
1167 ("CB: new identity for host %s:%d, deleting(%x %x %s %s)\n",
1168 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1169 identP->valid, host->interface,
1170 identP->valid ? uuid1 : "",
1171 host->interface ? uuid2 : ""));
1173 /* The host in the cache is not the host for this connection */
1174 host->hostFlags |= HOSTDELETED;
1181 host = h_Alloc_r(tcon); /* returned held and locked */
1182 h_gethostcps_r(host, FT_ApproxTime());
1183 if (!(host->Console & 1)) {
1185 cb_conn = host->callback_rxcon;
1186 rx_GetConnection(cb_conn);
1189 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1190 if (code == RXGEN_OPCODE)
1191 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1192 rx_PutConnection(cb_conn);
1195 if (code == RXGEN_OPCODE) {
1198 (struct Identity *)malloc(sizeof(struct Identity));
1203 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1208 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1210 ("Host %s:%d does not support WhoAreYou.\n",
1211 afs_inet_ntoa_r(host->host, hoststr),
1212 ntohs(host->port)));
1214 } else if (code == 0) {
1217 (struct Identity *)malloc(sizeof(struct Identity));
1222 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1227 identP->uuid = interf.uuid;
1229 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1231 ("WhoAreYou success on %s:%d\n",
1232 afs_inet_ntoa_r(host->host, hoststr),
1233 ntohs(host->port)));
1235 if (code == 0 && !identP->valid) {
1236 cb_conn = host->callback_rxcon;
1237 rx_GetConnection(cb_conn);
1239 code = RXAFSCB_InitCallBackState(cb_conn);
1240 rx_PutConnection(cb_conn);
1243 } else if (code == 0) {
1244 oldHost = h_LookupUuid_r(&identP->uuid);
1248 if (!(oheld = h_Held_r(oldHost)))
1252 if (oldHost->interface) {
1253 afsUUID uuid = oldHost->interface->uuid;
1254 cb_conn = host->callback_rxcon;
1255 rx_GetConnection(cb_conn);
1257 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
1258 rx_PutConnection(cb_conn);
1261 if (code && MultiProbeAlternateAddress_r(oldHost)) {
1269 /* The old host is either does not have a Uuid,
1270 * is not responding to Probes,
1271 * or does not have a matching Uuid.
1273 oldHost->hostFlags |= HOSTDELETED;
1274 h_Unlock_r(oldHost);
1275 /* Let the holder be last release */
1277 h_Release_r(oldHost);
1283 /* This is a new address for an existing host. Update
1284 * the list of interfaces for the existing host and
1285 * delete the host structure we just allocated. */
1287 ("CB: new addr %s:%d for old host %s:%d\n",
1288 afs_inet_ntoa_r(host->host, hoststr),
1289 ntohs(host->port), afs_inet_ntoa_r(oldHost->host,
1291 ntohs(oldHost->port)));
1292 addInterfaceAddr_r(oldHost, haddr, hport);
1293 host->hostFlags |= HOSTDELETED;
1299 /* This really is a new host */
1300 hashInsertUuid_r(&identP->uuid, host);
1301 cb_conn = host->callback_rxcon;
1302 rx_GetConnection(cb_conn);
1305 RXAFSCB_InitCallBackState3(cb_conn,
1307 rx_PutConnection(cb_conn);
1312 ("InitCallBackState3 success on %s:%d\n",
1313 afs_inet_ntoa_r(host->host, hoststr),
1314 ntohs(host->port)));
1315 assert(interfValid == 1);
1316 initInterfaceAddr_r(host, &interf);
1321 afs_inet_ntoa_r(host->host, hoststr);
1323 ("CB: RCallBackConnectBack failed for %s:%d\n",
1324 hoststr, ntohs(host->port)));
1325 host->hostFlags |= VENUSDOWN;
1327 host->hostFlags |= RESETDONE;
1330 if (caps.Capabilities_val
1331 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1332 host->hostFlags |= HERRORTRANS;
1334 host->hostFlags &= ~(HERRORTRANS);
1335 host->hostFlags |= ALTADDR; /* host structure initialization complete */
1338 if (caps.Capabilities_val)
1339 free(caps.Capabilities_val);
1340 caps.Capabilities_val = NULL;
1341 caps.Capabilities_len = 0;
1347 static char localcellname[PR_MAXNAMELEN + 1];
1348 char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
1349 int num_lrealms = -1;
1355 afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
1356 if (num_lrealms == -1) {
1358 for (i=0; i<AFS_NUM_LREALMS; i++) {
1359 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
1365 ("afs_krb_get_lrealm failed, using %s.\n",
1367 strncpy(local_realms[0], localcellname, AFS_REALM_SZ);
1373 /* initialize the rest of the local realms to nullstring for debugging */
1374 for (; i<AFS_NUM_LREALMS; i++)
1375 local_realms[i][0] = '\0';
1377 rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
1378 rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
1379 #ifdef AFS_PTHREAD_ENV
1380 assert(pthread_mutex_init(&host_glock_mutex, NULL) == 0);
1381 #endif /* AFS_PTHREAD_ENV */
1385 MapName_r(char *aname, char *acell, afs_int32 * aval)
1390 afs_int32 anamelen, cnamelen;
1394 anamelen = strlen(aname);
1395 if (anamelen >= PR_MAXNAMELEN)
1396 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
1398 lnames.namelist_len = 1;
1399 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
1400 lids.idlist_len = 0;
1401 lids.idlist_val = NULL;
1403 cnamelen = strlen(acell);
1405 if (afs_is_foreign_ticket_name(aname, "", acell, localcellname)) {
1407 ("MapName: cell is foreign. cell=%s, localcell=%s, localrealms={%s,%s,%s,%s}\n",
1408 acell, localcellname, local_realms[0],local_realms[1],local_realms[2],local_realms[3]));
1409 if ((anamelen + cnamelen + 1) >= PR_MAXNAMELEN) {
1411 ("MapName: Name too long, using AnonymousID for %s@%s\n",
1413 *aval = AnonymousID;
1416 foreign = 1; /* attempt cross-cell authentication */
1417 tname = (char *)malloc(PR_MAXNAMELEN);
1419 ViceLog(0, ("Failed malloc in MapName_r\n"));
1422 strcpy(tname, aname);
1423 tname[anamelen] = '@';
1424 strcpy(tname + anamelen + 1, acell);
1425 lnames.namelist_val = (prname *) tname;
1430 code = pr_NameToId(&lnames, &lids);
1433 if (lids.idlist_val) {
1434 *aval = lids.idlist_val[0];
1435 if (*aval == AnonymousID) {
1437 ("MapName: NameToId on %s returns anonymousID\n",
1438 lnames.namelist_val));
1440 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
1443 ("MapName: NameToId on '%s' is unknown\n",
1444 lnames.namelist_val));
1450 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
1458 /* NOTE: this returns the client with a Write lock */
1460 h_ID2Client(afs_int32 vid)
1462 register struct client *client;
1463 register struct host *host;
1466 for (host = hostList; host; host = host->next) {
1467 if (host->hostFlags & HOSTDELETED)
1469 for (client = host->FirstClient; client; client = client->next) {
1470 if (!client->deleted && client->ViceId == vid) {
1473 ObtainWriteLock(&client->lock);
1487 * Called by the server main loop. Returns a h_Held client, which must be
1488 * released later the main loop. Allocates a client if the matching one
1489 * isn't around. The client is returned with its reference count incremented
1490 * by one. The caller must call h_ReleaseClient_r when finished with
1494 h_FindClient_r(struct rx_connection *tcon)
1496 register struct client *client;
1497 register struct host *host;
1498 struct client *oldClient;
1503 #if (64-MAXKTCNAMELEN)
1504 ticket name length != 64
1508 char uname[PR_MAXNAMELEN];
1509 char tcell[MAXKTCREALMLEN];
1512 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1513 if (client && !client->deleted) {
1515 h_Hold_r(client->host);
1516 if (client->prfail != 2) { /* Could add shared lock on client here */
1517 /* note that we don't have to lock entry in this path to
1518 * ensure CPS is initialized, since we don't call rx_SetSpecific
1519 * until initialization is done, and we only get here if
1520 * rx_GetSpecific located the client structure.
1525 ObtainWriteLock(&client->lock); /* released at end */
1527 } else if (client) {
1531 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
1533 ("FindClient: authenticating connection: authClass=%d\n",
1535 if (authClass == 1) {
1536 /* A bcrypt tickets, no longer supported */
1537 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
1538 viceid = AnonymousID;
1539 expTime = 0x7fffffff;
1540 } else if (authClass == 2) {
1543 /* kerberos ticket */
1544 code = rxkad_GetServerInfo(tcon, /*level */ 0, &expTime,
1545 tname, tinst, tcell, &kvno);
1547 ViceLog(1, ("Failed to get rxkad ticket info\n"));
1548 viceid = AnonymousID;
1549 expTime = 0x7fffffff;
1551 int ilen = strlen(tinst);
1553 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
1554 tname, tinst, tcell, expTime, kvno));
1555 strncpy(uname, tname, sizeof(uname));
1557 if (strlen(uname) + 1 + ilen >= sizeof(uname))
1560 strcat(uname, tinst);
1562 /* translate the name to a vice id */
1563 code = MapName_r(uname, tcell, &viceid);
1567 ("failed to map name=%s, cell=%s -> code=%d\n", uname,
1570 viceid = AnonymousID;
1571 expTime = 0x7fffffff;
1575 viceid = AnonymousID; /* unknown security class */
1576 expTime = 0x7fffffff;
1580 host = h_GetHost_r(tcon); /* Returns it h_Held */
1583 /* First try to find the client structure */
1584 for (client = host->FirstClient; client; client = client->next) {
1585 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
1586 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
1587 if (client->tcon && (client->tcon != tcon)) {
1589 ("*** Vid=%d, sid=%x, tcon=%x, Tcon=%x ***\n",
1590 client->ViceId, client->sid, client->tcon,
1593 (struct client *)rx_GetSpecific(client->tcon,
1596 if (oldClient == client)
1597 rx_SetSpecific(client->tcon, rxcon_client_key,
1601 ("Client-conn mismatch: CL1=%x, CN=%x, CL2=%x\n",
1602 client, client->tcon, oldClient));
1604 client->tcon = (struct rx_connection *)0;
1608 ObtainWriteLock(&client->lock);
1614 /* Still no client structure - get one */
1617 /* Retry to find the client structure */
1618 for (client = host->FirstClient; client; client = client->next) {
1619 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
1620 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
1622 goto retryfirstclient;
1626 ObtainWriteLock(&client->lock);
1627 client->refCount = 1;
1628 client->host = host;
1629 client->next = host->FirstClient;
1630 host->FirstClient = client;
1631 #if FS_STATS_DETAILED
1632 client->InSameNetwork = host->InSameNetwork;
1633 #endif /* FS_STATS_DETAILED */
1634 client->ViceId = viceid;
1635 client->expTime = expTime; /* rx only */
1636 client->authClass = authClass; /* rx only */
1637 client->sid = rxr_CidOf(tcon);
1638 client->VenusEpoch = rxr_GetEpoch(tcon);
1639 client->CPS.prlist_val = 0;
1640 client->CPS.prlist_len = 0;
1642 CurrentConnections++; /* increment number of connections */
1645 client->prfail = fail;
1647 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
1648 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID)) {
1649 free(client->CPS.prlist_val);
1651 client->CPS.prlist_val = NULL;
1652 client->CPS.prlist_len = 0;
1653 client->ViceId = viceid;
1654 client->expTime = expTime;
1656 if (viceid == ANONYMOUSID) {
1657 client->CPS.prlist_len = AnonCPS.prlist_len;
1658 client->CPS.prlist_val = AnonCPS.prlist_val;
1661 code = pr_GetCPS(viceid, &client->CPS);
1666 ("pr_GetCPS failed(%d) for user %d, host %s:%d\n",
1667 code, viceid, afs_inet_ntoa_r(client->host->host,
1669 ntohs(client->host->port)));
1671 /* Although ubik_Call (called by pr_GetCPS) traverses thru
1672 * all protection servers and reevaluates things if no
1673 * sync server or quorum is found we could still end up
1674 * with one of these errors. In such case we would like to
1675 * reevaluate the rpc call to find if there's cps for this
1676 * guy. We treat other errors (except network failures
1677 * ones - i.e. code < 0) as an indication that there is no
1678 * CPS for this host. Ideally we could like to deal this
1679 * problem the other way around (i.e. if code == NOCPS
1680 * ignore else retry next time) but the problem is that
1681 * there're other errors (i.e. EPERM) for which we don't
1682 * want to retry and we don't know the whole code list!
1684 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
1688 /* the disabling of system:administrators is so iffy and has so many
1689 * possible failure modes that we will disable it again */
1690 /* Turn off System:Administrator for safety
1691 * if (AL_IsAMember(SystemId, client->CPS) == 0)
1692 * assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
1695 /* Now, tcon may already be set to a rock, since we blocked with no host
1696 * or client locks set above in pr_GetCPS (XXXX some locking is probably
1697 * required). So, before setting the RPC's rock, we should disconnect
1698 * the RPC from the other client structure's rock.
1700 oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1701 if (oldClient && oldClient->tcon == tcon) {
1703 oldClient->tcon = (struct rx_connection *)0;
1704 ViceLog(0, ("FindClient: client %x(%x) already had conn %x (host %s:%d), stolen by client %x(%x)\n",
1705 oldClient, oldClient->sid, tcon,
1706 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
1707 ntohs(rxr_PortOf(tcon)),
1708 client, client->sid));
1709 /* rx_SetSpecific will be done immediately below */
1711 client->tcon = tcon;
1712 rx_SetSpecific(tcon, rxcon_client_key, client);
1713 ReleaseWriteLock(&client->lock);
1717 } /*h_FindClient_r */
1720 h_ReleaseClient_r(struct client *client)
1722 assert(client->refCount > 0);
1729 * Sigh: this one is used to get the client AGAIN within the individual
1730 * server routines. This does not bother h_Holding the host, since
1731 * this is assumed already have been done by the server main loop.
1732 * It does check tokens, since only the server routines can return the
1733 * VICETOKENDEAD error code
1736 GetClient(struct rx_connection *tcon, struct client **cp)
1738 register struct client *client;
1741 *cp = client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1742 if (client == NULL || client->tcon == NULL) {
1744 ("GetClient: no client in conn %x (host %x:%d), VBUSYING\n",
1745 tcon, rxr_HostOf(tcon),rxr_PortOf(tcon)));
1749 if (rxr_CidOf(client->tcon) != client->sid) {
1751 ("GetClient: tcon %x tcon sid %d client sid %d\n",
1752 client->tcon, rxr_CidOf(client->tcon), client->sid));
1756 if (!(client && client->tcon && rxr_CidOf(client->tcon) == client->sid)) {
1758 ViceLog(0, ("GetClient: no client in conn %x\n", tcon));
1761 ("GetClient: tcon %x tcon sid %d client sid %d\n",
1762 client->tcon, client->tcon ? rxr_CidOf(client->tcon)
1763 : -1, client->sid));
1766 if (client && client->LastCall > client->expTime && client->expTime) {
1769 ("Token for %s at %s:%d expired %d\n", h_UserName(client),
1770 afs_inet_ntoa_r(client->host->host, hoststr),
1771 ntohs(client->host->port), client->expTime));
1773 return VICETOKENDEAD;
1782 /* Client user name for short term use. Note that this is NOT inexpensive */
1784 h_UserName(struct client *client)
1786 static char User[PR_MAXNAMELEN + 1];
1790 lids.idlist_len = 1;
1791 lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
1792 if (!lids.idlist_val) {
1793 ViceLog(0, ("Failed malloc in h_UserName\n"));
1796 lnames.namelist_len = 0;
1797 lnames.namelist_val = (prname *) 0;
1798 lids.idlist_val[0] = client->ViceId;
1799 if (pr_IdToName(&lids, &lnames)) {
1800 /* We need to free id we alloced above! */
1801 free(lids.idlist_val);
1802 return "*UNKNOWN USER NAME*";
1804 strncpy(User, lnames.namelist_val[0], PR_MAXNAMELEN);
1805 free(lids.idlist_val);
1806 free(lnames.namelist_val);
1816 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
1817 CEs, CEBlocks, HTs, HTBlocks));
1823 h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
1825 register struct client *client;
1832 if (host->hostFlags & HOSTDELETED) {
1836 (void)afs_snprintf(tmpStr, sizeof tmpStr,
1837 "Host %s:%d down = %d, LastCall %s",
1838 afs_inet_ntoa_r(host->host, hoststr),
1839 ntohs(host->port), (host->hostFlags & VENUSDOWN),
1840 afs_ctime((time_t *) & host->LastCall, tbuffer,
1842 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1843 for (client = host->FirstClient; client; client = client->next) {
1844 if (!client->deleted) {
1846 (void)afs_snprintf(tmpStr, sizeof tmpStr,
1847 " user id=%d, name=%s, sl=%s till %s",
1848 client->ViceId, h_UserName(client),
1850 authClass ? "Authenticated" :
1851 "Not authenticated",
1853 authClass ? afs_ctime((time_t *) & client->
1857 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1859 (void)afs_snprintf(tmpStr, sizeof tmpStr,
1860 " user=%s, no current server connection\n",
1861 h_UserName(client));
1862 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1864 (void)afs_snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [",
1865 client->CPS.prlist_len);
1866 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1867 if (client->CPS.prlist_val) {
1868 for (i = 0; i > client->CPS.prlist_len; i++) {
1869 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
1870 client->CPS.prlist_val[i]);
1871 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1874 sprintf(tmpStr, "]\n");
1875 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1881 } /*h_PrintClient */
1886 * Print a list of clients, with last security level and token value seen,
1896 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
1900 ("Couldn't create client dump file %s\n",
1901 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
1904 now = FT_ApproxTime();
1905 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n",
1906 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
1907 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1908 h_Enumerate(h_PrintClient, (char *)file);
1909 STREAM_REALLYCLOSE(file);
1910 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
1917 h_DumpHost(register struct host *host, int held, StreamHandle_t * file)
1924 (void)afs_snprintf(tmpStr, sizeof tmpStr,
1925 "ip:%s 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 [",
1926 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), host->index,
1927 host->cblist, CheckLock(&host->lock), host->LastCall,
1928 host->ActiveCall, (host->hostFlags & VENUSDOWN),
1929 host->hostFlags & HOSTDELETED, host->Console,
1930 host->hostFlags & CLIENTDELETED, host->hcpsfailed,
1932 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1933 if (host->hcps.prlist_val)
1934 for (i = 0; i < host->hcps.prlist_len; i++) {
1935 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
1936 host->hcps.prlist_val[i]);
1937 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1939 sprintf(tmpStr, "] [");
1940 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1941 if (host->interface)
1942 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
1944 sprintf(tmpStr, " %s:%d",
1945 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
1946 ntohs(host->interface->interface[i].port));
1947 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1949 sprintf(tmpStr, "] holds: ");
1950 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1952 for (i = 0; i < h_maxSlots; i++) {
1953 sprintf(tmpStr, "%04x", host->holds[i]);
1954 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1956 sprintf(tmpStr, " slot/bit: %d/%d\n", h_holdSlot(), h_holdbit());
1957 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1969 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
1975 ("Couldn't create host dump file %s\n",
1976 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
1979 now = FT_ApproxTime();
1980 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n",
1981 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
1982 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1983 h_Enumerate(h_DumpHost, (char *)file);
1984 STREAM_REALLYCLOSE(file);
1985 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
1991 * This counts the number of workstations, the number of active workstations,
1992 * and the number of workstations declared "down" (i.e. not heard from
1993 * recently). An active workstation has received a call since the cutoff
1994 * time argument passed.
1997 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
1999 register struct host *host;
2000 register int num = 0, active = 0, del = 0;
2003 for (host = hostList; host; host = host->next) {
2004 if (!(host->hostFlags & HOSTDELETED)) {
2006 if (host->ActiveCall > cutofftime)
2008 if (host->hostFlags & VENUSDOWN)
2020 } /*h_GetWorkStats */
2023 /*------------------------------------------------------------------------
2024 * PRIVATE h_ClassifyAddress
2027 * Given a target IP address and a candidate IP address (both
2028 * in host byte order), classify the candidate into one of three
2029 * buckets in relation to the target by bumping the counters passed
2033 * a_targetAddr : Target address.
2034 * a_candAddr : Candidate address.
2035 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
2036 * addresses are either in the same network
2037 * or the same subnet.
2038 * a_diffSubnetP : ...when the candidate is in a different
2040 * a_diffNetworkP : ...when the candidate is in a different
2047 * The target and candidate addresses are both in host byte
2048 * order, NOT network byte order, when passed in.
2052 *------------------------------------------------------------------------*/
2055 h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
2056 afs_int32 * a_sameNetOrSubnetP, afs_int32 * a_diffSubnetP,
2057 afs_int32 * a_diffNetworkP)
2058 { /*h_ClassifyAddress */
2060 afs_uint32 targetNet;
2061 afs_uint32 targetSubnet;
2063 afs_uint32 candSubnet;
2066 * Put bad values into the subnet info to start with.
2068 targetSubnet = (afs_uint32) 0;
2069 candSubnet = (afs_uint32) 0;
2072 * Pull out the network and subnetwork numbers from the target
2073 * and candidate addresses. We can short-circuit this whole
2074 * affair if the target and candidate addresses are not of the
2077 if (IN_CLASSA(a_targetAddr)) {
2078 if (!(IN_CLASSA(a_candAddr))) {
2079 (*a_diffNetworkP)++;
2082 targetNet = a_targetAddr & IN_CLASSA_NET;
2083 candNet = a_candAddr & IN_CLASSA_NET;
2084 if (IN_SUBNETA(a_targetAddr))
2085 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
2086 if (IN_SUBNETA(a_candAddr))
2087 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
2088 } else if (IN_CLASSB(a_targetAddr)) {
2089 if (!(IN_CLASSB(a_candAddr))) {
2090 (*a_diffNetworkP)++;
2093 targetNet = a_targetAddr & IN_CLASSB_NET;
2094 candNet = a_candAddr & IN_CLASSB_NET;
2095 if (IN_SUBNETB(a_targetAddr))
2096 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
2097 if (IN_SUBNETB(a_candAddr))
2098 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
2099 } /*Class B target */
2100 else if (IN_CLASSC(a_targetAddr)) {
2101 if (!(IN_CLASSC(a_candAddr))) {
2102 (*a_diffNetworkP)++;
2105 targetNet = a_targetAddr & IN_CLASSC_NET;
2106 candNet = a_candAddr & IN_CLASSC_NET;
2109 * Note that class C addresses can't have subnets,
2110 * so we leave the defaults untouched.
2112 } /*Class C target */
2114 targetNet = a_targetAddr;
2115 candNet = a_candAddr;
2116 } /*Class D address */
2119 * Now, simply compare the extracted net and subnet values for
2120 * the two addresses (which at this point are known to be of the
2123 if (targetNet == candNet) {
2124 if (targetSubnet == candSubnet)
2125 (*a_sameNetOrSubnetP)++;
2129 (*a_diffNetworkP)++;
2131 } /*h_ClassifyAddress */
2134 /*------------------------------------------------------------------------
2135 * EXPORTED h_GetHostNetStats
2138 * Iterate through the host table, and classify each (non-deleted)
2139 * host entry into ``proximity'' categories (same net or subnet,
2140 * different subnet, different network).
2143 * a_numHostsP : Set to total number of (non-deleted) hosts.
2144 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
2145 * a_diffSubnetP : Set to # hosts on diff subnet as server.
2146 * a_diffNetworkP : Set to # hosts on diff network as server.
2152 * We only count non-deleted hosts. The storage pointed to by our
2153 * parameters is zeroed upon entry.
2157 *------------------------------------------------------------------------*/
2160 h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
2161 afs_int32 * a_diffSubnetP, afs_int32 * a_diffNetworkP)
2162 { /*h_GetHostNetStats */
2164 register struct host *hostP; /*Ptr to current host entry */
2165 register afs_uint32 currAddr_HBO; /*Curr host addr, host byte order */
2168 * Clear out the storage pointed to by our parameters.
2170 *a_numHostsP = (afs_int32) 0;
2171 *a_sameNetOrSubnetP = (afs_int32) 0;
2172 *a_diffSubnetP = (afs_int32) 0;
2173 *a_diffNetworkP = (afs_int32) 0;
2176 for (hostP = hostList; hostP; hostP = hostP->next) {
2177 if (!(hostP->hostFlags & HOSTDELETED)) {
2179 * Bump the number of undeleted host entries found.
2180 * In classifying the current entry's address, make
2181 * sure to first convert to host byte order.
2184 currAddr_HBO = (afs_uint32) ntohl(hostP->host);
2185 h_ClassifyAddress(FS_HostAddr_HBO, currAddr_HBO,
2186 a_sameNetOrSubnetP, a_diffSubnetP,
2188 } /*Only look at non-deleted hosts */
2189 } /*For each host record hashed to this index */
2191 } /*h_GetHostNetStats */
2193 static afs_uint32 checktime;
2194 static afs_uint32 clientdeletetime;
2195 static struct AFSFid zerofid;
2199 * XXXX: This routine could use Multi-Rx to avoid serializing the timeouts.
2200 * Since it can serialize them, and pile up, it should be a separate LWP
2201 * from other events.
2204 CheckHost(register struct host *host, int held)
2206 register struct client *client;
2207 struct rx_connection *cb_conn = NULL;
2210 /* Host is held by h_Enumerate */
2212 for (client = host->FirstClient; client; client = client->next) {
2213 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
2214 client->deleted = 1;
2215 host->hostFlags |= CLIENTDELETED;
2218 if (host->LastCall < checktime) {
2220 if (!(host->hostFlags & HOSTDELETED)) {
2221 cb_conn = host->callback_rxcon;
2222 rx_GetConnection(cb_conn);
2223 if (host->LastCall < clientdeletetime) {
2224 host->hostFlags |= HOSTDELETED;
2225 if (!(host->hostFlags & VENUSDOWN)) {
2226 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
2227 if (host->interface) {
2230 RXAFSCB_InitCallBackState3(cb_conn,
2236 RXAFSCB_InitCallBackState(cb_conn);
2239 host->hostFlags |= ALTADDR; /* alternate addresses valid */
2242 (void)afs_inet_ntoa_r(host->host, hoststr);
2244 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
2245 hoststr, ntohs(host->port)));
2246 host->hostFlags |= VENUSDOWN;
2248 /* Note: it's safe to delete hosts even if they have call
2249 * back state, because break delayed callbacks (called when a
2250 * message is received from the workstation) will always send a
2251 * break all call backs to the workstation if there is no
2256 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
2257 if (host->interface) {
2258 afsUUID uuid = host->interface->uuid;
2260 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
2263 if (MultiProbeAlternateAddress_r(host)) {
2265 (void)afs_inet_ntoa_r(host->host, hoststr);
2267 ("ProbeUuid failed for host %s:%d\n",
2268 hoststr, ntohs(host->port)));
2269 host->hostFlags |= VENUSDOWN;
2274 code = RXAFSCB_Probe(cb_conn);
2278 (void)afs_inet_ntoa_r(host->host, hoststr);
2280 ("Probe failed for host %s:%d\n", hoststr,
2281 ntohs(host->port)));
2282 host->hostFlags |= VENUSDOWN;
2288 rx_PutConnection(cb_conn);
2301 * Set VenusDown for any hosts that have not had a call in 15 minutes and
2302 * don't respond to a probe. Note that VenusDown can only be cleared if
2303 * a message is received from the host (see ServerLWP in file.c).
2304 * Delete hosts that have not had any calls in 1 hour, clients that
2305 * have not had any calls in 15 minutes.
2307 * This routine is called roughly every 5 minutes.
2312 afs_uint32 now = FT_ApproxTime();
2314 memset((char *)&zerofid, 0, sizeof(zerofid));
2316 * Send a probe to the workstation if it hasn't been heard from in
2319 checktime = now - 15 * 60;
2320 clientdeletetime = now - 120 * 60; /* 2 hours ago */
2321 h_Enumerate(CheckHost, NULL);
2326 * This is called with host locked and held. At this point, the
2327 * hostHashTable should not have any entries for the alternate
2328 * interfaces. This function has to insert these entries in the
2331 * The addresses in the interfaceAddr list are in host byte order.
2334 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
2341 struct Interface *interface;
2347 ("initInterfaceAddr : host %x numAddr %d\n", host->host,
2348 interf->numberOfInterfaces));
2350 number = interf->numberOfInterfaces;
2351 myAddr = host->host; /* current interface address */
2352 myPort = host->port; /* current port */
2354 /* validation checks */
2355 if (number < 0 || number > AFS_MAX_INTERFACE_ADDR) {
2357 ("Number of alternate addresses returned is %d\n", number));
2362 * Convert IP addresses to network byte order, and remove for
2363 * duplicate IP addresses from the interface list.
2365 for (i = 0, count = 0, found = 0; i < number; i++) {
2366 interf->addr_in[i] = htonl(interf->addr_in[i]);
2367 for (j = 0; j < count; j++) {
2368 if (interf->addr_in[j] == interf->addr_in[i])
2372 interf->addr_in[count] = interf->addr_in[i];
2373 if (interf->addr_in[count] == myAddr)
2380 * Allocate and initialize an interface structure for this host.
2383 interface = (struct Interface *)
2384 malloc(sizeof(struct Interface) +
2385 (sizeof(struct AddrPort) * (count - 1)));
2387 ViceLog(0, ("Failed malloc in initInterfaceAddr_r\n"));
2390 interface->numberOfInterfaces = count;
2392 interface = (struct Interface *)
2393 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
2395 interface->numberOfInterfaces = count + 1;
2396 interface->interface[count].addr = myAddr;
2397 interface->interface[count].port = myPort;
2399 interface->uuid = interf->uuid;
2400 for (i = 0; i < count; i++) {
2401 interface->interface[i].addr = interf->addr_in[i];
2402 /* We store the port as 7001 because the addresses reported by
2403 * TellMeAboutYourself and WhoAreYou RPCs are only valid if they
2404 * are coming from fully connected hosts (no NAT/PATs)
2406 interface->interface[i].port = htons(7001);
2409 assert(!host->interface);
2410 host->interface = interface;
2412 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
2414 ViceLog(125, ("--- alt address %s:%d\n",
2415 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2416 ntohs(host->interface->interface[i].port)));
2422 /* deleted a HashChain structure for this address and host */
2423 /* returns 1 on success */
2425 hashDelete_r(afs_uint32 addr, afs_uint16 port, struct host *host)
2428 register struct h_hashChain **hp, *th;
2430 for (hp = &hostHashTable[h_HashIndex(addr)]; (th = *hp);) {
2431 assert(th->hostPtr);
2432 if (th->hostPtr == host && th->addr == addr && th->port == port) {
2446 ** prints out all alternate interface address for the host. The 'level'
2447 ** parameter indicates what level of debugging sets this output
2450 printInterfaceAddr(struct host *host, int level)
2455 if (host->interface) {
2456 /* check alternate addresses */
2457 number = host->interface->numberOfInterfaces;
2459 for (i = 0; i < number; i++)
2460 ViceLog(level, ("%s:%d ", afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2461 ntohs(host->interface->interface[i].port)));
2463 ViceLog(level, ("\n"));