ubik: Do not count votes from error'd connections
[openafs.git] / src / ubik / beacon.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <roken.h>
14
15 #include <afs/opr.h>
16 #ifdef AFS_PTHREAD_ENV
17 # include <opr/lock.h>
18 #else
19 # include <opr/lockstub.h>
20 #endif
21
22 #include <lock.h>
23 #include <rx/rx.h>
24 #include <rx/rxkad.h>
25 #include <rx/rx_multi.h>
26 #include <afs/cellconfig.h>
27 #ifndef AFS_NT40_ENV
28 #include <afs/afsutil.h>
29 #endif
30
31 #define UBIK_INTERNALS
32 #include "ubik.h"
33 #include "ubik_int.h"
34
35 /* These global variables were used to set the function to use to initialise
36  * the client security layer. They are retained for backwards compatiblity with
37  * legacy callers - the ubik_SetClientSecurityProcs() interface should be used
38  * instead
39  */
40 int (*ubik_CRXSecurityProc) (void *rock, struct rx_securityClass **,
41                              afs_int32 *);
42 void *ubik_CRXSecurityRock;
43
44 /*! \name statics used to determine if we're the sync site */
45 static int nServers;            /*!< total number of servers */
46 static char amIMagic = 0;       /*!< is this host the magic host */
47 char amIClone = 0;              /*!< is this a clone which doesn't vote */
48 static char ubik_singleServer = 0;
49 /*\}*/
50 static int (*secLayerProc) (void *rock, struct rx_securityClass **,
51                             afs_int32 *) = NULL;
52 static int (*tokenCheckProc) (void *rock) = NULL;
53 static void * securityRock = NULL;
54
55 afs_int32 ubikSecIndex;
56 struct rx_securityClass *ubikSecClass;
57
58 /* Values protected by the address lock */
59 struct addr_data addr_globals;
60
61 /* Values protected by the beacon lock */
62 struct beacon_data beacon_globals;
63
64 static int ubeacon_InitServerListCommon(afs_uint32 ame,
65                                         struct afsconf_cell *info,
66                                         char clones[],
67                                         afs_uint32 aservers[]);
68 static int verifyInterfaceAddress(afs_uint32 *ame, struct afsconf_cell *info,
69                                   afs_uint32 aservers[]);
70
71 /*! \file
72  * Module responsible for both deciding if we're currently the sync site,
73  * and keeping collecting votes so as to stay sync site.
74  *
75  * The basic module contacts all of the servers it can, trying to get them to vote
76  * for this server for sync site.  The vote request message (called a beacon message)
77  * also specifies until which time this site claims to be the sync site, if at all, thus enabling
78  * receiving sites to know how long the sync site guarantee is made for.
79  *
80  * Each  of these beacon messages is thus both a declaration of how long this site will
81  * remain sync site, and an attempt to extend that time by collecting votes for a later
82  * sync site extension.
83  *
84  * The voting module is responsible for choosing a reasonable time until which it promises
85  * not to vote for someone else.  This parameter (BIG seconds) is not actually passed in
86  * the interface (perhaps it should be?) but is instead a compile time constant that both
87  * sides know about.
88
89  * The beacon and vote modules work intimately together; the vote module decides how long
90  * it should promise the beacon module its vote, and the beacon module takes all of these
91  * votes and decides for how long it is the synchronization site.
92  */
93
94 /*! \brief procedure called from debug rpc call to get this module's state for debugging */
95 void
96 ubeacon_Debug(struct ubik_debug *aparm)
97 {
98     /* fill in beacon's state fields in the ubik_debug structure */
99     aparm->syncSiteUntil = beacon_globals.syncSiteUntil;
100     aparm->nServers = nServers;
101 }
102
103 static int
104 amSyncSite(void) {
105     afs_int32 now;
106     afs_int32 rcode;
107
108     /* special case for fast startup */
109     if (nServers == 1 && !amIClone)
110         return 1;               /* one guy is always the sync site */
111
112     UBIK_BEACON_LOCK;
113     if (beacon_globals.ubik_amSyncSite == 0 || amIClone)
114         rcode = 0;              /* if I don't think I'm the sync site, say so */
115     else {
116         now = FT_ApproxTime();
117         if (beacon_globals.syncSiteUntil <= now) {      /* if my votes have expired, say so */
118             if (beacon_globals.ubik_amSyncSite)
119                 ubik_dprint("Ubik: I am no longer the sync site\n");
120             beacon_globals.ubik_amSyncSite = 0;
121             rcode = 0;
122         } else {
123             rcode = 1;          /* otherwise still have the required votes */
124         }
125     }
126     UBIK_BEACON_UNLOCK;
127     ubik_dprint("beacon: amSyncSite is %d\n", rcode);
128     return rcode;
129 }
130
131 /*!
132  * \brief Procedure that determines whether this site has enough current votes to remain sync site.
133  *
134  * Called from higher-level modules (everything but the vote module).
135  *
136  * If we're the sync site, check that our guarantees, obtained by the ubeacon_Interact()
137  * light-weight process, haven't expired.  We're sync site as long as a majority of the
138  * servers in existence have promised us unexpired guarantees.  The variable #ubik_syncSiteUntil
139  * contains the time at which the latest of the majority of the sync site guarantees expires
140  * (if the variable #ubik_amSyncSite is true)
141  * This module also calls up to the recovery module if it thinks that the recovery module
142  * may have to pick up a new database (which offucr sif [sic] we lose the sync site votes).
143  *
144  * \return 1 if local site is the sync site
145  * \return 0 if sync site is elsewhere
146  */
147 int
148 ubeacon_AmSyncSite(void)
149 {
150     afs_int32 rcode;
151
152     rcode = amSyncSite();
153
154     if (!rcode)
155         urecovery_ResetState();
156
157     return rcode;
158 }
159
160 /*!
161  * \see ubeacon_InitServerListCommon()
162  */
163 int
164 ubeacon_InitServerListByInfo(afs_uint32 ame, struct afsconf_cell *info,
165                              char clones[])
166 {
167     afs_int32 code;
168
169     code = ubeacon_InitServerListCommon(ame, info, clones, 0);
170     return code;
171 }
172
173 /*!
174  * \param ame "address of me"
175  * \param aservers list of other servers
176  *
177  * \see ubeacon_InitServerListCommon()
178  */
179 int
180 ubeacon_InitServerList(afs_uint32 ame, afs_uint32 aservers[])
181 {
182     afs_int32 code;
183
184     code =
185         ubeacon_InitServerListCommon(ame, (struct afsconf_cell *)0, 0,
186                                      aservers);
187     return code;
188 }
189
190 /* Must be called with address lock held */
191 void
192 ubeacon_InitSecurityClass(void)
193 {
194     int i;
195     /* get the security index to use, if we can */
196     if (secLayerProc) {
197         i = (*secLayerProc) (securityRock, &addr_globals.ubikSecClass, &addr_globals.ubikSecIndex);
198     } else if (ubik_CRXSecurityProc) {
199         i = (*ubik_CRXSecurityProc) (ubik_CRXSecurityRock, &addr_globals.ubikSecClass,
200                                      &addr_globals.ubikSecIndex);
201     } else
202         i = 1;
203     if (i) {
204         /* don't have sec module yet */
205         addr_globals.ubikSecIndex = 0;
206         addr_globals.ubikSecClass = rxnull_NewClientSecurityObject();
207     }
208 }
209
210 void
211 ubeacon_ReinitServer(struct ubik_server *ts)
212 {
213     if (tokenCheckProc && !(*tokenCheckProc) (securityRock)) {
214         struct rx_connection *disk_rxcid;
215         struct rx_connection *vote_rxcid;
216         struct rx_connection *tmp;
217         UBIK_ADDR_LOCK;
218         ubeacon_InitSecurityClass();
219         disk_rxcid =
220             rx_NewConnection(rx_HostOf(rx_PeerOf(ts->disk_rxcid)),
221                              ubik_callPortal, DISK_SERVICE_ID,
222                              addr_globals.ubikSecClass, addr_globals.ubikSecIndex);
223         if (disk_rxcid) {
224             tmp = ts->disk_rxcid;
225             ts->disk_rxcid = disk_rxcid;
226             rx_PutConnection(tmp);
227         }
228         vote_rxcid =
229             rx_NewConnection(rx_HostOf(rx_PeerOf(ts->vote_rxcid)),
230                              ubik_callPortal, VOTE_SERVICE_ID,
231                              addr_globals.ubikSecClass, addr_globals.ubikSecIndex);
232         if (vote_rxcid) {
233             tmp = ts->vote_rxcid;
234             ts->vote_rxcid = vote_rxcid;
235             rx_PutConnection(tmp);
236         }
237         UBIK_ADDR_UNLOCK;
238     }
239 }
240
241 /*!
242  * \brief setup server list
243  *
244  * \param ame "address of me"
245  * \param aservers list of other servers
246  *
247  * called only at initialization to set up the list of servers to
248  * contact for votes.  Just creates the server structure.
249  *
250  * The "magic" host is the one with the lowest internet address.  It is
251  * magic because its vote counts epsilon more than the others.  This acts
252  * as a tie-breaker when we have an even number of hosts in the system.
253  * For example, if the "magic" host is up in a 2 site system, then it
254  * is sync site.  Without the magic host hack, if anyone crashed in a 2
255  * site system, we'd be out of business.
256  *
257  * \note There are two connections in every server structure, one for
258  * vote calls (which must always go through quickly) and one for database
259  * operations, which are subject to waiting for locks.  If we used only
260  * one, the votes would sometimes get held up behind database operations,
261  * and the sync site guarantees would timeout even though the host would be
262  * up for communication.
263  *
264  * \see ubeacon_InitServerList(), ubeacon_InitServerListByInfo()
265  */
266 int
267 ubeacon_InitServerListCommon(afs_uint32 ame, struct afsconf_cell *info,
268                              char clones[], afs_uint32 aservers[])
269 {
270     struct ubik_server *ts;
271     afs_int32 me = -1;
272     afs_int32 servAddr;
273     afs_int32 i, code;
274     afs_int32 magicHost;
275     struct ubik_server *magicServer;
276
277     /* verify that the addresses passed in are correct */
278     if ((code = verifyInterfaceAddress(&ame, info, aservers)))
279         return code;
280
281     ubeacon_InitSecurityClass();
282
283     magicHost = ntohl(ame);     /* do comparisons in host order */
284     magicServer = (struct ubik_server *)0;
285
286     if (info) {
287         for (i = 0; i < info->numServers; i++) {
288             if (ntohl((afs_uint32) info->hostAddr[i].sin_addr.s_addr) ==
289                 ntohl((afs_uint32) ame)) {
290                 me = i;
291                 if (clones[i]) {
292                     amIClone = 1;
293                     magicHost = 0;
294                 }
295             }
296         }
297         nServers = 0;
298         for (i = 0; i < info->numServers; i++) {
299             if (i == me)
300                 continue;
301             ts = calloc(1, sizeof(struct ubik_server));
302             ts->next = ubik_servers;
303             ubik_servers = ts;
304             ts->addr[0] = info->hostAddr[i].sin_addr.s_addr;
305             if (clones[i]) {
306                 ts->isClone = 1;
307             } else {
308                 if (!magicHost
309                     || ntohl((afs_uint32) ts->addr[0]) <
310                     (afs_uint32) magicHost) {
311                     magicHost = ntohl(ts->addr[0]);
312                     magicServer = ts;
313                 }
314                 ++nServers;
315             }
316             /* for vote reqs */
317             ts->vote_rxcid =
318                 rx_NewConnection(info->hostAddr[i].sin_addr.s_addr,
319                                  ubik_callPortal, VOTE_SERVICE_ID,
320                                  addr_globals.ubikSecClass, addr_globals.ubikSecIndex);
321             /* for disk reqs */
322             ts->disk_rxcid =
323                 rx_NewConnection(info->hostAddr[i].sin_addr.s_addr,
324                                  ubik_callPortal, DISK_SERVICE_ID,
325                                  addr_globals.ubikSecClass, addr_globals.ubikSecIndex);
326             ts->up = 1;
327         }
328     } else {
329         i = 0;
330         while ((servAddr = *aservers++)) {
331             if (i >= MAXSERVERS)
332                 return UNHOSTS; /* too many hosts */
333             ts = calloc(1, sizeof(struct ubik_server));
334             ts->next = ubik_servers;
335             ubik_servers = ts;
336             ts->addr[0] = servAddr;     /* primary address in  net byte order */
337             ts->vote_rxcid = rx_NewConnection(servAddr, ubik_callPortal, VOTE_SERVICE_ID,
338                         addr_globals.ubikSecClass, addr_globals.ubikSecIndex);  /* for vote reqs */
339             ts->disk_rxcid = rx_NewConnection(servAddr, ubik_callPortal, DISK_SERVICE_ID,
340                         addr_globals.ubikSecClass, addr_globals.ubikSecIndex);  /* for disk reqs */
341             ts->isClone = 0;    /* don't know about clones */
342             ts->up = 1;
343             if (ntohl((afs_uint32) servAddr) < (afs_uint32) magicHost) {
344                 magicHost = ntohl(servAddr);
345                 magicServer = ts;
346             }
347             i++;
348         }
349     }
350     if (magicServer)
351         magicServer->magic = 1; /* remember for when counting votes */
352
353     if (!amIClone && !magicServer)
354         amIMagic = 1;
355     if (info) {
356         if (!amIClone)
357             ++nServers;         /* count this server as well as the remotes */
358     } else
359         nServers = i + 1;       /* count this server as well as the remotes */
360
361     ubik_quorum = (nServers >> 1) + 1;  /* compute the majority figure */
362
363 /* Shoud we set some defaults for RX??
364     r_retryInterval = 2;
365     r_nRetries = (RPCTIMEOUT/r_retryInterval);
366 */
367     if (info) {
368         if (!ubik_servers)      /* special case 1 server */
369             ubik_singleServer = 1;
370         if (nServers == 1 && !amIClone) {
371             beacon_globals.ubik_amSyncSite = 1; /* let's start as sync site */
372             beacon_globals.syncSiteUntil = 0x7fffffff;  /* and be it quite a while */
373         }
374     } else {
375         if (nServers == 1)      /* special case 1 server */
376             ubik_singleServer = 1;
377     }
378
379     if (ubik_singleServer) {
380         if (!beacon_globals.ubik_amSyncSite)
381             ubik_dprint("Ubik: I am the sync site - 1 server\n");
382         beacon_globals.ubik_amSyncSite = 1;
383         beacon_globals.syncSiteUntil = 0x7fffffff;      /* quite a while */
384     }
385     return 0;
386 }
387
388 /*!
389  * \brief main lwp loop for code that sends out beacons.
390  *
391  * This code only runs while we're sync site or we want to be the sync site.
392  * It runs in its very own light-weight process.
393  */
394 void *
395 ubeacon_Interact(void *dummy)
396 {
397     afs_int32 code;
398     struct timeval tt;
399     struct rx_connection *connections[MAXSERVERS];
400     struct ubik_server *servers[MAXSERVERS];
401     afs_int32 i;
402     struct ubik_server *ts;
403     afs_int32 temp, yesVotes, lastWakeupTime, oldestYesVote, syncsite;
404     struct ubik_tid ttid;
405     struct ubik_version tversion;
406     afs_int32 startTime;
407
408     afs_pthread_setname_self("beacon");
409
410     /* loop forever getting votes */
411     lastWakeupTime = 0;         /* keep track of time we last started a vote collection */
412     while (1) {
413
414         /* don't wakeup more than every POLLTIME seconds */
415         temp = (lastWakeupTime + POLLTIME) - FT_ApproxTime();
416         /* don't sleep if last collection phase took too long (probably timed someone out ) */
417         if (temp > 0) {
418             if (temp > POLLTIME)
419                 temp = POLLTIME;
420             tt.tv_sec = temp;
421             tt.tv_usec = 0;
422 #ifdef AFS_PTHREAD_ENV
423             code = select(0, 0, 0, 0, &tt);
424 #else
425             code = IOMGR_Select(0, 0, 0, 0, &tt);
426 #endif
427         } else
428             code = 0;
429
430         lastWakeupTime = FT_ApproxTime();       /* started a new collection phase */
431
432         if (ubik_singleServer)
433             continue;           /* special-case 1 server for speedy startup */
434
435         if (!uvote_ShouldIRun())
436             continue;           /* if voter has heard from a better candidate than us, don't bother running */
437
438         /* otherwise we should run for election, or we're the sync site (and have already won);
439          * send out the beacon packets */
440         /* build list of all up hosts (noticing dead hosts are running again
441          * is a task for the recovery module, not the beacon module), and
442          * prepare to send them an r multi-call containing the beacon message */
443         i = 0;                  /* collect connections */
444         UBIK_BEACON_LOCK;
445         UBIK_ADDR_LOCK;
446         for (ts = ubik_servers; ts; ts = ts->next) {
447             if (ts->up && ts->addr[0] != ubik_host[0]) {
448                 servers[i] = ts;
449                 connections[i++] = ts->vote_rxcid;
450             }
451         }
452         UBIK_ADDR_UNLOCK;
453         UBIK_BEACON_UNLOCK;
454         servers[i] = (struct ubik_server *)0;   /* end of list */
455         /* note that we assume in the vote module that we'll always get at least BIGTIME
456          * seconds of vote from anyone who votes for us, which means we can conservatively
457          * assume we'll be fine until SMALLTIME seconds after we start collecting votes */
458         /* this next is essentially an expansion of rgen's ServBeacon routine */
459
460         UBIK_VERSION_LOCK;
461         ttid.epoch = version_globals.ubik_epochTime;
462         if (ubik_dbase->flags & DBWRITING) {
463             /*
464              * if a write is in progress, we have to send the writeTidCounter
465              * which holds the tid counter of the write transaction , and not
466              * send the tidCounter value which holds the tid counter of the
467              * last transaction.
468              */
469             ttid.counter = ubik_dbase->writeTidCounter;
470         } else
471             ttid.counter = ubik_dbase->tidCounter + 1;
472         tversion.epoch = ubik_dbase->version.epoch;
473         tversion.counter = ubik_dbase->version.counter;
474         UBIK_VERSION_UNLOCK;
475
476         /* now analyze return codes, counting up our votes */
477         yesVotes = 0;           /* count how many to ensure we have quorum */
478         oldestYesVote = 0x7fffffff;     /* time quorum expires */
479         syncsite = amSyncSite();
480         if (!syncsite) {
481             /* Ok to use the DB lock here since we aren't sync site */
482             DBHOLD(ubik_dbase);
483             urecovery_ResetState();
484             DBRELE(ubik_dbase);
485         }
486         startTime = FT_ApproxTime();
487         /*
488          * Don't waste time using mult Rx calls if there are no connections out there
489          */
490         if (i > 0) {
491             char hoststr[16];
492             multi_Rx(connections, i) {
493                 multi_VOTE_Beacon(syncsite, startTime, &tversion,
494                                   &ttid);
495                 temp = FT_ApproxTime(); /* now, more or less */
496                 ts = servers[multi_i];
497                 UBIK_BEACON_LOCK;
498                 ts->lastBeaconSent = temp;
499                 code = multi_error;
500
501                 if (code > 0 && ((code < temp && code < temp - 3600) ||
502                                  (code > temp && code > temp + 3600))) {
503                     /* if we reached here, supposedly the remote host voted
504                      * for us based on a computation from over an hour ago in
505                      * the past, or over an hour in the future. this is
506                      * unlikely; what actually probably happened is that the
507                      * call generated some error and was aborted. this can
508                      * happen due to errors with the rx security class in play
509                      * (rxkad, rxgk, etc). treat the host as if we got a
510                      * timeout, since this is not a valid vote. */
511                     ubik_print("assuming distant vote time %d from %s is an error; marking host down\n",
512                                (int)code, afs_inet_ntoa_r(ts->addr[0], hoststr));
513                     code = -1;
514                 }
515                 if (code > 0 && rx_ConnError(connections[multi_i])) {
516                     ubik_print("assuming vote from %s is invalid due to conn error %d; marking host down\n",
517                                afs_inet_ntoa_r(ts->addr[0], hoststr),
518                                (int)rx_ConnError(connections[multi_i]));
519                     code = -1;
520                 }
521
522                 /* note that the vote time (the return code) represents the time
523                  * the vote was computed, *not* the time the vote expires.  We compute
524                  * the latter down below if we got enough votes to go with */
525                 if (code > 0) {
526                     if ((code & ~0xff) == ERROR_TABLE_BASE_RXK) {
527                         ubik_dprint("token error %d from host %s\n",
528                                     code, afs_inet_ntoa_r(ts->addr[0], hoststr));
529                         ts->up = 0;
530                         ts->beaconSinceDown = 0;
531                         urecovery_LostServer(ts);
532                     } else {
533                         ts->lastVoteTime = code;
534                         if (code < oldestYesVote)
535                             oldestYesVote = code;
536                         ts->lastVote = 1;
537                         if (!ts->isClone)
538                             yesVotes += 2;
539                         if (ts->magic)
540                             yesVotes++; /* the extra epsilon */
541                         ts->up = 1;     /* server is up (not really necessary: recovery does this for real) */
542                         ts->beaconSinceDown = 1;
543                         ubik_dprint("yes vote from host %s\n",
544                                     afs_inet_ntoa_r(ts->addr[0], hoststr));
545                     }
546                 } else if (code == 0) {
547                     ts->lastVoteTime = temp;
548                     ts->lastVote = 0;
549                     ts->beaconSinceDown = 1;
550                     ubik_dprint("no vote from %s\n",
551                                 afs_inet_ntoa_r(ts->addr[0], hoststr));
552                 } else if (code < 0) {
553                     ts->up = 0;
554                     ts->beaconSinceDown = 0;
555                     urecovery_LostServer(ts);
556                     ubik_dprint("time out from %s\n",
557                                 afs_inet_ntoa_r(ts->addr[0], hoststr));
558                 }
559                 UBIK_BEACON_UNLOCK;
560             }
561             multi_End;
562         }
563         /* now call our own voter module to see if we'll vote for ourself.  Note that
564          * the same restrictions apply for our voting for ourself as for our voting
565          * for anyone else. */
566         i = SVOTE_Beacon((struct rx_call *)0, ubeacon_AmSyncSite(), startTime,
567                          &tversion, &ttid);
568         if (i) {
569             yesVotes += 2;
570             if (amIMagic)
571                 yesVotes++;     /* extra epsilon */
572             if (i < oldestYesVote)
573                 oldestYesVote = i;
574         }
575
576         /* now decide if we have enough votes to become sync site.
577          * Note that we can still get enough votes even if we didn't for ourself. */
578         if (yesVotes > nServers) {      /* yesVotes is bumped by 2 or 3 for each site */
579             UBIK_BEACON_LOCK;
580             if (!beacon_globals.ubik_amSyncSite)
581                 ubik_dprint("Ubik: I am the sync site\n");
582             beacon_globals.ubik_amSyncSite = 1;
583             beacon_globals.syncSiteUntil = oldestYesVote + SMALLTIME;
584 #ifndef AFS_PTHREAD_ENV
585                 /* I did not find a corresponding LWP_WaitProcess(&ubik_amSyncSite) --
586                    this may be a spurious signal call -- sjenkins */
587                 LWP_NoYieldSignal(&beacon_globals.ubik_amSyncSite);
588 #endif
589             UBIK_BEACON_UNLOCK;
590         } else {
591             UBIK_BEACON_LOCK;
592             if (beacon_globals.ubik_amSyncSite)
593                 ubik_dprint("Ubik: I am no longer the sync site\n");
594             beacon_globals.ubik_amSyncSite = 0;
595             UBIK_BEACON_UNLOCK;
596             DBHOLD(ubik_dbase);
597             urecovery_ResetState();     /* tell recovery we're no longer the sync site */
598             DBRELE(ubik_dbase);
599         }
600
601     }                           /* while loop */
602     return NULL;
603 }
604
605 /*!
606  * \brief Verify that a given IP addresses does actually exist on this machine.
607  *
608  * \param ame      the pointer to my IP address specified in the
609  *                 CellServDB file.
610  * \param aservers an array containing IP
611  *                 addresses of remote ubik servers. The array is
612  *                 terminated by a zero address.
613  *
614  * Algorithm     : Verify that my IP addresses \p ame does actually exist
615  *                 on this machine.  If any of my IP addresses are there
616  *                 in the remote server list \p aserver, remove them from
617  *                 this list.  Update global variable \p ubik_host[] with
618  *                 my IP addresses.
619  *
620  * \return 0 on success, non-zero on failure
621  */
622 static int
623 verifyInterfaceAddress(afs_uint32 *ame, struct afsconf_cell *info,
624                        afs_uint32 aservers[]) {
625     afs_uint32 myAddr[UBIK_MAX_INTERFACE_ADDR], *servList, tmpAddr;
626     afs_uint32 myAddr2[UBIK_MAX_INTERFACE_ADDR];
627     char hoststr[16];
628     int tcount, count, found, i, j, totalServers, start, end, usednetfiles =
629         0;
630
631     if (info)
632         totalServers = info->numServers;
633     else {                      /* count the number of servers */
634         for (totalServers = 0, servList = aservers; *servList; servList++)
635             totalServers++;
636     }
637
638     if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || AFSDIR_SERVER_NETINFO_FILEPATH) {
639         /*
640          * Find addresses we are supposed to register as per the netrestrict file
641          * if it exists, else just register all the addresses we find on this
642          * host as returned by rx_getAllAddr (in NBO)
643          */
644         char reason[1024];
645         count = afsconf_ParseNetFiles(myAddr, NULL, NULL,
646                                       UBIK_MAX_INTERFACE_ADDR, reason,
647                                       AFSDIR_SERVER_NETINFO_FILEPATH,
648                                       AFSDIR_SERVER_NETRESTRICT_FILEPATH);
649         if (count < 0) {
650             ubik_print("ubik: Can't register any valid addresses:%s\n",
651                        reason);
652             ubik_print("Aborting..\n");
653             return UBADHOST;
654         }
655         usednetfiles++;
656     } else {
657         /* get all my interface addresses in net byte order */
658         count = rx_getAllAddr(myAddr, UBIK_MAX_INTERFACE_ADDR);
659     }
660
661     if (count <= 0) {           /* no address found */
662         ubik_print("ubik: No network addresses found, aborting..");
663         return UBADHOST;
664     }
665
666     /* verify that the My-address passed in by ubik is correct */
667     for (j = 0, found = 0; j < count; j++) {
668         if (*ame == myAddr[j]) {        /* both in net byte order */
669             found = 1;
670             break;
671         }
672     }
673
674     if (!found) {
675         ubik_print("ubik: primary address %s does not exist\n",
676                    afs_inet_ntoa_r(*ame, hoststr));
677         /* if we had the result of rx_getAllAddr already, avoid subverting
678          * the "is gethostbyname(gethostname()) us" check. If we're
679          * using NetInfo/NetRestrict, we assume they have enough clue
680          * to avoid that big hole in their foot from the loaded gun. */
681         if (usednetfiles) {
682             /* take the address we did get, then see if ame was masked */
683             *ame = myAddr[0];
684             tcount = rx_getAllAddr(myAddr2, UBIK_MAX_INTERFACE_ADDR);
685             if (tcount <= 0) {  /* no address found */
686                 ubik_print("ubik: No network addresses found, aborting..");
687                 return UBADHOST;
688             }
689
690             /* verify that the My-address passed in by ubik is correct */
691             for (j = 0, found = 0; j < tcount; j++) {
692                 if (*ame == myAddr2[j]) {       /* both in net byte order */
693                     found = 1;
694                     break;
695                 }
696             }
697         }
698         if (!found)
699             return UBADHOST;
700     }
701
702     /* if any of my addresses are there in serverList, then
703      ** use that as my primary addresses : the higher level
704      ** application screwed up in dealing with multihomed concepts
705      */
706     for (j = 0, found = 0; j < count; j++) {
707         for (i = 0; i < totalServers; i++) {
708             if (info)
709                 tmpAddr = (afs_uint32) info->hostAddr[i].sin_addr.s_addr;
710             else
711                 tmpAddr = aservers[i];
712             if (myAddr[j] == tmpAddr) {
713                 *ame = tmpAddr;
714                 if (!info)
715                     aservers[i] = 0;
716                 found = 1;
717             }
718         }
719     }
720     if (found)
721         ubik_print("Using %s as my primary address\n", afs_inet_ntoa_r(*ame, hoststr));
722
723     if (!info) {
724         /* get rid of servers which were purged because all
725          ** those interface addresses are myself
726          */
727         for (start = 0, end = totalServers - 1; (start < end); start++, end--) {
728             /* find the first zero entry from the beginning */
729             for (; (start < end) && (aservers[start]); start++);
730
731             /* find the last non-zero entry from the end */
732             for (; (end >= 0) && (!aservers[end]); end--);
733
734             /* if there is nothing more to purge, exit from loop */
735             if (start >= end)
736                 break;
737
738             /* move the entry */
739             aservers[start] = aservers[end];
740             aservers[end] = 0;  /* this entry was moved */
741         }
742     }
743
744     /* update all my addresses in ubik_host in such a way
745      ** that ubik_host[0] has the primary address
746      */
747     ubik_host[0] = *ame;
748     for (j = 0, i = 1; j < count; j++)
749         if (*ame != myAddr[j])
750             ubik_host[i++] = myAddr[j];
751
752     return 0;                   /* return success */
753 }
754
755
756 /*!
757  * \brief Exchange IP address information with remote servers.
758  *
759  * \param ubik_host an array containing all my IP addresses.
760  *
761  * Algorithm     : Do an RPC to all remote ubik servers informing them
762  *                 about my IP addresses. Get their IP addresses and
763  *                 update my linked list of ubik servers \p ubik_servers
764  *
765  * \return 0 on success, non-zero on failure
766  */
767 int
768 ubeacon_updateUbikNetworkAddress(afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR])
769 {
770     int j, count, code = 0;
771     UbikInterfaceAddr inAddr, outAddr;
772     struct rx_connection *conns[MAXSERVERS];
773     struct ubik_server *ts, *server[MAXSERVERS];
774     char buffer[32];
775     char hoststr[16];
776
777     UBIK_ADDR_LOCK;
778     for (count = 0, ts = ubik_servers; ts; count++, ts = ts->next) {
779         conns[count] = ts->disk_rxcid;
780         server[count] = ts;
781     }
782     UBIK_ADDR_UNLOCK;
783
784
785     /* inform all other servers only if there are more than one
786      * database servers in the cell */
787
788     if (count > 0) {
789
790         for (j = 0; j < UBIK_MAX_INTERFACE_ADDR; j++)
791             inAddr.hostAddr[j] = ntohl(ubik_host[j]);
792
793
794         /* do the multi-RX RPC to all other servers */
795         multi_Rx(conns, count) {
796             multi_DISK_UpdateInterfaceAddr(&inAddr, &outAddr);
797             ts = server[multi_i];       /* reply received from this server */
798             if (!multi_error) {
799                 UBIK_ADDR_LOCK;
800                 if (ts->addr[0] != htonl(outAddr.hostAddr[0])) {
801                     code = UBADHOST;
802                     strcpy(buffer, afs_inet_ntoa_r(ts->addr[0], hoststr));
803                     ubik_print("ubik:Two primary addresses for same server \
804                     %s %s\n", buffer,
805                     afs_inet_ntoa_r(htonl(outAddr.hostAddr[0]), hoststr));
806                 } else {
807                     for (j = 1; j < UBIK_MAX_INTERFACE_ADDR; j++)
808                         ts->addr[j] = htonl(outAddr.hostAddr[j]);
809                 }
810                 UBIK_ADDR_UNLOCK;
811             } else if (multi_error == RXGEN_OPCODE) {   /* pre 3.5 remote server */
812                 UBIK_ADDR_LOCK;
813                 ubik_print
814                     ("ubik server %s does not support UpdateInterfaceAddr RPC\n",
815                      afs_inet_ntoa_r(ts->addr[0], hoststr));
816                 UBIK_ADDR_UNLOCK;
817             } else if (multi_error == UBADHOST) {
818                 code = UBADHOST;        /* remote CellServDB inconsistency */
819                 ubik_print("Inconsistent Cell Info on server: ");
820                 UBIK_ADDR_LOCK;
821                 for (j = 0; j < UBIK_MAX_INTERFACE_ADDR && ts->addr[j]; j++)
822                     ubik_print("%s ", afs_inet_ntoa_r(ts->addr[j], hoststr));
823                 UBIK_ADDR_UNLOCK;
824                 ubik_print("\n");
825             } else {
826                 UBIK_BEACON_LOCK;
827                 ts->up = 0;     /* mark the remote server as down */
828                 UBIK_BEACON_UNLOCK;
829             }
830         }
831         multi_End;
832     }
833     return code;
834 }
835
836 void
837 ubik_SetClientSecurityProcs(int (*secproc) (void *,
838                                             struct rx_securityClass **,
839                                             afs_int32 *),
840                             int (*checkproc) (void *),
841                             void *rock)
842 {
843     secLayerProc = secproc;
844     tokenCheckProc = checkproc;
845     securityRock = rock;
846 }