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