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