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