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