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